3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 /**************************************************************
32 * FEC Ethernet Initialization Routine
34 *************************************************************/
36 #define FEC_ECNTRL_ETHER_EN 0x00000002
37 #define FEC_ECNTRL_RESET 0x00000001
39 #define FEC_RCNTRL_BC_REJ 0x00000010
40 #define FEC_RCNTRL_PROM 0x00000008
41 #define FEC_RCNTRL_MII_MODE 0x00000004
42 #define FEC_RCNTRL_DRT 0x00000002
43 #define FEC_RCNTRL_LOOP 0x00000001
45 #define FEC_TCNTRL_FDEN 0x00000004
46 #define FEC_TCNTRL_HBC 0x00000002
47 #define FEC_TCNTRL_GTS 0x00000001
49 #define FEC_RESET_DELAY 50000
52 /* Ethernet Transmit and Receive Buffers */
53 #define DBUF_LENGTH 1520
57 #define PKT_MAXBUF_SIZE 1518
58 #define PKT_MINBUF_SIZE 64
59 #define PKT_MAXBLR_SIZE 1520
63 #define FEC_ADDR 0x10000840
66 #define FEC_ADDR 0x40001000
71 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET)
74 static char txbuf[DBUF_LENGTH];
76 static uint rxIdx; /* index of the current RX buffer */
77 static uint txIdx; /* index of the current TX buffer */
80 * FEC Ethernet Tx and Rx buffer descriptors allocated at the
81 * immr->udata_bd address on Dual-Port RAM
82 * Provide for Double Buffering
85 typedef volatile struct CommonBufferDescriptor {
86 cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
87 cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
90 static RTXBD *rtx = 0x380000;
93 int eth_send (volatile void *packet, int length)
96 volatile fec_t *fecp = FEC_ADDR;
102 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY)
103 && (j < TOUT_LOOP)) {
107 if (j >= TOUT_LOOP) {
108 printf ("TX not ready\n");
111 rtx->txbd[txIdx].cbd_bufaddr = (uint) packet;
112 rtx->txbd[txIdx].cbd_datlen = length;
113 rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
115 /* Activate transmit Buffer Descriptor polling */
116 fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
119 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY)
120 && (j < TOUT_LOOP)) {
124 if (j >= TOUT_LOOP) {
125 printf ("TX timeout\n");
128 printf ("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
129 __FILE__, __LINE__, __FUNCTION__, j, rtx->txbd[txIdx].cbd_sc,
130 (rtx->txbd[txIdx].cbd_sc & 0x003C) >> 2);
132 /* return only status bits */ ;
133 rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
135 txIdx = (txIdx + 1) % TX_BUF_CNT;
143 volatile fec_t *fecp = FEC_ADDR;
146 /* section 16.9.23.2 */
147 if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
149 break; /* nothing received - leave for() loop */
152 length = rtx->rxbd[rxIdx].cbd_datlen;
154 if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
156 printf ("%s[%d] err: %x\n",
157 __FUNCTION__, __LINE__,
158 rtx->rxbd[rxIdx].cbd_sc);
161 /* Pass the packet up to the protocol layers. */
162 NetReceive (NetRxPackets[rxIdx], length - 4);
165 /* Give the buffer back to the FEC. */
166 rtx->rxbd[rxIdx].cbd_datlen = 0;
168 /* wrap around buffer index when necessary */
169 if ((rxIdx + 1) >= PKTBUFSRX) {
170 rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
171 (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
174 rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
178 /* Try to fill Buffer Descriptors */
179 fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
186 int eth_init (bd_t * bd)
190 volatile fec_t *fecp = FEC_ADDR;
193 * A delay is required between a reset of the FEC block and
194 * initialization of other FEC registers because the reset takes
195 * some time to complete. If you don't delay, subsequent writes
196 * to FEC registers might get killed by the reset routine which is
200 fecp->fec_ecntrl = FEC_ECNTRL_RESET;
202 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
206 if (i == FEC_RESET_DELAY) {
207 printf ("FEC_RESET_DELAY timeout\n");
211 /* We use strictly polling mode only
215 /* Clear any pending interrupt */
216 fecp->fec_ievent = 0xffffffff;
218 /* Set station address */
219 #define ea bd->bi_enetaddr
220 fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) |
221 (ea[2] << 8) | (ea[3]);
222 fecp->fec_addr_high = (ea[4] << 24) | (ea[5] << 16);
225 /* Clear multicast address hash table
227 fecp->fec_hash_table_high = 0;
228 fecp->fec_hash_table_low = 0;
230 /* Set maximum receive buffer size.
232 fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
235 * Setup Buffers and Buffer Desriptors
241 * Setup Receiver Buffer Descriptors (13.14.24.18)
245 for (i = 0; i < PKTBUFSRX; i++) {
246 rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
247 rtx->rxbd[i].cbd_datlen = 0; /* Reset */
248 rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
250 rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
253 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
257 for (i = 0; i < TX_BUF_CNT; i++) {
258 rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
259 rtx->txbd[i].cbd_datlen = 0; /* Reset */
260 rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
262 rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
264 /* Set receive and transmit descriptor base
266 fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
267 fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
272 /* Half duplex mode */
273 fecp->fec_r_cntrl = (PKT_MAXBUF_SIZE << 16) | FEC_RCNTRL_MII_MODE;
274 fecp->fec_r_cntrl = (PKT_MAXBUF_SIZE << 16) | FEC_RCNTRL_MII_MODE;
275 fecp->fec_x_cntrl = 0;
277 fecp->fec_mii_speed = 0;
279 /* Now enable the transmit and receive processing
281 fecp->fec_ecntrl = FEC_ECNTRL_ETHER_EN;
283 /* And last, try to fill Rx Buffer Descriptors */
284 fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
292 volatile fec_t *fecp = FEC_ADDR;
294 fecp->fec_ecntrl = 0;