2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2007 Freescale Semiconductor, Inc.
6 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 #include <asm/immap.h>
41 /* Ethernet Transmit and Receive Buffers */
42 #define DBUF_LENGTH 1520
44 #define PKT_MAXBUF_SIZE 1518
45 #define PKT_MINBUF_SIZE 64
46 #define PKT_MAXBLR_SIZE 1520
47 #define LAST_PKTBUFSRX PKTBUFSRX - 1
48 #define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
49 #define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
51 DECLARE_GLOBAL_DATA_PTR;
53 struct fec_info_s fec_info[] = {
54 #ifdef CONFIG_SYS_FEC0_IOBASE
57 CONFIG_SYS_FEC0_IOBASE, /* io base */
58 CONFIG_SYS_FEC0_PINMUX, /* gpio pin muxing */
59 CONFIG_SYS_FEC0_MIIBASE, /* mii base */
61 0, /* duplex and speed */
69 0, /* initialized flag */
70 (struct fec_info_s *)-1,
73 #ifdef CONFIG_SYS_FEC1_IOBASE
76 CONFIG_SYS_FEC1_IOBASE, /* io base */
77 CONFIG_SYS_FEC1_PINMUX, /* gpio pin muxing */
78 CONFIG_SYS_FEC1_MIIBASE, /* mii base */
80 0, /* duplex and speed */
82 0, /* phy name init */
83 #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
84 (cbd_t *)DBUF_LENGTH, /* RX BD */
92 0, /* initialized flag */
93 (struct fec_info_s *)-1,
98 int fec_recv(struct eth_device *dev);
99 int fec_init(struct eth_device *dev, bd_t * bd);
100 void fec_halt(struct eth_device *dev);
101 void fec_reset(struct eth_device *dev);
103 void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
105 if ((dup_spd >> 16) == FULL) {
106 /* Set maximum frame length */
107 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
108 FEC_RCR_PROM | 0x100;
109 fecp->tcr = FEC_TCR_FDEN;
111 /* Half duplex mode */
112 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
113 FEC_RCR_MII_MODE | FEC_RCR_DRT;
114 fecp->tcr &= ~FEC_TCR_FDEN;
117 if ((dup_spd & 0xFFFF) == _100BASET) {
118 #ifdef CONFIG_MCF5445x
119 fecp->rcr &= ~0x200; /* disabled 10T base */
124 bd->bi_ethspeed = 100;
126 #ifdef CONFIG_MCF5445x
127 fecp->rcr |= 0x200; /* enabled 10T base */
132 bd->bi_ethspeed = 10;
136 static int fec_send(struct eth_device *dev, void *packet, int length)
138 struct fec_info_s *info = dev->priv;
139 volatile fec_t *fecp = (fec_t *) (info->iobase);
143 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phyStatus);
149 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
150 (j < MCFFEC_TOUT_LOOP)) {
154 if (j >= MCFFEC_TOUT_LOOP) {
155 printf("TX not ready\n");
158 info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
159 info->txbd[info->txIdx].cbd_datlen = length;
160 info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
162 /* Activate transmit Buffer Descriptor polling */
163 fecp->tdar = 0x01000000; /* Descriptor polling active */
165 #ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
167 * FEC unable to initial transmit data packet.
168 * A nop will ensure the descriptor polling active completed.
169 * CF Internal RAM has shorter cycle access than DRAM. If use
170 * DRAM as Buffer descriptor and data, a nop is a must.
171 * Affect only V2 and V3.
177 #ifdef CONFIG_SYS_UNIFY_CACHE
182 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
183 (j < MCFFEC_TOUT_LOOP)) {
187 if (j >= MCFFEC_TOUT_LOOP) {
188 printf("TX timeout\n");
192 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
193 __FILE__, __LINE__, __FUNCTION__, j,
194 info->txbd[info->txIdx].cbd_sc,
195 (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
198 /* return only status bits */
199 rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
200 info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
205 int fec_recv(struct eth_device *dev)
207 struct fec_info_s *info = dev->priv;
208 volatile fec_t *fecp = (fec_t *) (info->iobase);
212 #ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
214 #ifdef CONFIG_SYS_UNIFY_CACHE
217 /* section 16.9.23.2 */
218 if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
220 break; /* nothing received - leave for() loop */
223 length = info->rxbd[info->rxIdx].cbd_datlen;
225 if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
226 printf("%s[%d] err: %x\n",
227 __FUNCTION__, __LINE__,
228 info->rxbd[info->rxIdx].cbd_sc);
230 printf("%s[%d] err: %x\n",
231 __FUNCTION__, __LINE__,
232 info->rxbd[info->rxIdx].cbd_sc);
237 /* Pass the packet up to the protocol layers. */
238 NetReceive(NetRxPackets[info->rxIdx], length);
240 fecp->eir |= FEC_EIR_RXF;
243 /* Give the buffer back to the FEC. */
244 info->rxbd[info->rxIdx].cbd_datlen = 0;
246 /* wrap around buffer index when necessary */
247 if (info->rxIdx == LAST_PKTBUFSRX) {
248 info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
251 info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
255 /* Try to fill Buffer Descriptors */
256 fecp->rdar = 0x01000000; /* Descriptor polling active */
263 void dbgFecRegs(struct eth_device *dev)
265 struct fec_info_s *info = dev->priv;
266 volatile fec_t *fecp = (fec_t *) (info->iobase);
269 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
270 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
271 printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
272 printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
273 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
274 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
275 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
276 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
277 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
278 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
279 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
280 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
281 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
282 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
283 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
284 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
285 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
286 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
287 printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
288 printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
289 printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
290 printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
291 printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
294 printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
296 printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
297 fecp->rmon_t_packets);
298 printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
299 fecp->rmon_t_bc_pkt);
300 printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
301 fecp->rmon_t_mc_pkt);
302 printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
303 fecp->rmon_t_crc_align);
304 printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
305 fecp->rmon_t_undersize);
306 printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
307 fecp->rmon_t_oversize);
308 printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
310 printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
312 printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
314 printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
316 printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
317 fecp->rmon_t_p65to127);
318 printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
319 fecp->rmon_t_p128to255);
320 printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
321 fecp->rmon_t_p256to511);
322 printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
323 fecp->rmon_t_p512to1023);
324 printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
325 fecp->rmon_t_p1024to2047);
326 printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
327 fecp->rmon_t_p_gte2048);
328 printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
329 fecp->rmon_t_octets);
332 printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
334 printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
335 fecp->ieee_t_frame_ok);
336 printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
338 printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
340 printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
342 printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
344 printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
346 printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
347 fecp->ieee_t_macerr);
348 printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
350 printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
352 printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
354 printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
355 fecp->ieee_t_octets_ok);
358 printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
360 printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
361 fecp->rmon_r_packets);
362 printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
363 fecp->rmon_r_bc_pkt);
364 printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
365 fecp->rmon_r_mc_pkt);
366 printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
367 fecp->rmon_r_crc_align);
368 printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
369 fecp->rmon_r_undersize);
370 printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
371 fecp->rmon_r_oversize);
372 printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
374 printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
376 printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
378 printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
379 fecp->rmon_r_p65to127);
380 printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
381 fecp->rmon_r_p128to255);
382 printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
383 fecp->rmon_r_p256to511);
384 printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
385 fecp->rmon_r_p512to1023);
386 printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
387 fecp->rmon_r_p1024to2047);
388 printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
389 fecp->rmon_r_p_gte2048);
390 printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
391 fecp->rmon_r_octets);
394 printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
396 printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
397 fecp->ieee_r_frame_ok);
398 printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
400 printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
402 printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
403 fecp->ieee_r_macerr);
404 printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
406 printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
407 fecp->ieee_r_octets_ok);
413 int fec_init(struct eth_device *dev, bd_t * bd)
415 struct fec_info_s *info = dev->priv;
416 volatile fec_t *fecp = (fec_t *) (info->iobase);
420 fecpin_setclear(dev, 1);
424 #if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
425 defined (CONFIG_SYS_DISCOVER_PHY)
429 setFecDuplexSpeed(fecp, bd, info->dup_spd);
431 #ifndef CONFIG_SYS_DISCOVER_PHY
432 setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
433 #endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
434 #endif /* CONFIG_CMD_MII || CONFIG_MII */
436 /* We use strictly polling mode only */
439 /* Clear any pending interrupt */
440 fecp->eir = 0xffffffff;
442 /* Set station address */
443 if ((u32) fecp == CONFIG_SYS_FEC0_IOBASE) {
444 #ifdef CONFIG_SYS_FEC1_IOBASE
445 volatile fec_t *fecp1 = (fec_t *) (CONFIG_SYS_FEC1_IOBASE);
446 eth_getenv_enetaddr("eth1addr", ea);
448 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
449 fecp1->paur = (ea[4] << 24) | (ea[5] << 16);
451 eth_getenv_enetaddr("ethaddr", ea);
453 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
454 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
456 #ifdef CONFIG_SYS_FEC0_IOBASE
457 volatile fec_t *fecp0 = (fec_t *) (CONFIG_SYS_FEC0_IOBASE);
458 eth_getenv_enetaddr("ethaddr", ea);
460 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
461 fecp0->paur = (ea[4] << 24) | (ea[5] << 16);
463 #ifdef CONFIG_SYS_FEC1_IOBASE
464 eth_getenv_enetaddr("eth1addr", ea);
466 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
467 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
471 /* Clear unicast address hash table */
475 /* Clear multicast address hash table */
479 /* Set maximum receive buffer size. */
480 fecp->emrbr = PKT_MAXBLR_SIZE;
483 * Setup Buffers and Buffer Desriptors
489 * Setup Receiver Buffer Descriptors (13.14.24.18)
493 for (i = 0; i < PKTBUFSRX; i++) {
494 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
495 info->rxbd[i].cbd_datlen = 0; /* Reset */
496 info->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
498 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
501 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
505 for (i = 0; i < TX_BUF_CNT; i++) {
506 info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
507 info->txbd[i].cbd_datlen = 0; /* Reset */
508 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
510 info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
512 /* Set receive and transmit descriptor base */
513 fecp->erdsr = (unsigned int)(&info->rxbd[0]);
514 fecp->etdsr = (unsigned int)(&info->txbd[0]);
516 /* Now enable the transmit and receive processing */
517 fecp->ecr |= FEC_ECR_ETHER_EN;
519 /* And last, try to fill Rx Buffer Descriptors */
520 fecp->rdar = 0x01000000; /* Descriptor polling active */
525 void fec_reset(struct eth_device *dev)
527 struct fec_info_s *info = dev->priv;
528 volatile fec_t *fecp = (fec_t *) (info->iobase);
531 fecp->ecr = FEC_ECR_RESET;
532 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
535 if (i == FEC_RESET_DELAY) {
536 printf("FEC_RESET_DELAY timeout\n");
540 void fec_halt(struct eth_device *dev)
542 struct fec_info_s *info = dev->priv;
546 fecpin_setclear(dev, 0);
548 info->rxIdx = info->txIdx = 0;
549 memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
550 memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
551 memset(info->txbuf, 0, DBUF_LENGTH);
554 int mcffec_initialize(bd_t * bis)
556 struct eth_device *dev;
558 #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
559 u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
562 for (i = 0; i < sizeof(fec_info) / sizeof(fec_info[0]); i++) {
565 (struct eth_device *)memalign(CONFIG_SYS_CACHELINE_SIZE,
570 memset(dev, 0, sizeof(*dev));
572 sprintf(dev->name, "FEC%d", fec_info[i].index);
574 dev->priv = &fec_info[i];
575 dev->init = fec_init;
576 dev->halt = fec_halt;
577 dev->send = fec_send;
578 dev->recv = fec_recv;
580 /* setup Receive and Transmit buffer descriptor */
581 #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
582 fec_info[i].rxbd = (cbd_t *)((u32)fec_info[i].rxbd + tmp);
583 tmp = (u32)fec_info[i].rxbd;
585 (cbd_t *)((u32)fec_info[i].txbd + tmp +
586 (PKTBUFSRX * sizeof(cbd_t)));
587 tmp = (u32)fec_info[i].txbd;
589 (char *)((u32)fec_info[i].txbuf + tmp +
590 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
591 tmp = (u32)fec_info[i].txbuf;
594 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
595 (PKTBUFSRX * sizeof(cbd_t)));
597 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
598 (TX_BUF_CNT * sizeof(cbd_t)));
600 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
604 printf("rxbd %x txbd %x\n",
605 (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
608 fec_info[i].phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
612 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
613 miiphy_register(dev->name,
614 mcffec_miiphy_read, mcffec_miiphy_write);
617 fec_info[i - 1].next = &fec_info[i];
619 fec_info[i - 1].next = &fec_info[0];
622 bis->bi_ethspeed = 10;