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_send(struct eth_device *dev, volatile void *packet, int length);
99 int fec_recv(struct eth_device *dev);
100 int fec_init(struct eth_device *dev, bd_t * bd);
101 void fec_halt(struct eth_device *dev);
102 void fec_reset(struct eth_device *dev);
104 void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
106 if ((dup_spd >> 16) == FULL) {
107 /* Set maximum frame length */
108 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
109 FEC_RCR_PROM | 0x100;
110 fecp->tcr = FEC_TCR_FDEN;
112 /* Half duplex mode */
113 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
114 FEC_RCR_MII_MODE | FEC_RCR_DRT;
115 fecp->tcr &= ~FEC_TCR_FDEN;
118 if ((dup_spd & 0xFFFF) == _100BASET) {
119 #ifdef CONFIG_MCF5445x
120 fecp->rcr &= ~0x200; /* disabled 10T base */
125 bd->bi_ethspeed = 100;
127 #ifdef CONFIG_MCF5445x
128 fecp->rcr |= 0x200; /* enabled 10T base */
133 bd->bi_ethspeed = 10;
137 int fec_send(struct eth_device *dev, volatile void *packet, int length)
139 struct fec_info_s *info = dev->priv;
140 volatile fec_t *fecp = (fec_t *) (info->iobase);
144 miiphy_read(dev->name, info->phy_addr, PHY_BMSR, &phyStatus);
150 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
151 (j < MCFFEC_TOUT_LOOP)) {
155 if (j >= MCFFEC_TOUT_LOOP) {
156 printf("TX not ready\n");
159 info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
160 info->txbd[info->txIdx].cbd_datlen = length;
161 info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
163 /* Activate transmit Buffer Descriptor polling */
164 fecp->tdar = 0x01000000; /* Descriptor polling active */
166 #ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
168 * FEC unable to initial transmit data packet.
169 * A nop will ensure the descriptor polling active completed.
170 * CF Internal RAM has shorter cycle access than DRAM. If use
171 * DRAM as Buffer descriptor and data, a nop is a must.
172 * Affect only V2 and V3.
178 #ifdef CONFIG_SYS_UNIFY_CACHE
183 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
184 (j < MCFFEC_TOUT_LOOP)) {
188 if (j >= MCFFEC_TOUT_LOOP) {
189 printf("TX timeout\n");
193 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
194 __FILE__, __LINE__, __FUNCTION__, j,
195 info->txbd[info->txIdx].cbd_sc,
196 (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
199 /* return only status bits */
200 rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
201 info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
206 int fec_recv(struct eth_device *dev)
208 struct fec_info_s *info = dev->priv;
209 volatile fec_t *fecp = (fec_t *) (info->iobase);
213 #ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
215 #ifdef CONFIG_SYS_UNIFY_CACHE
218 /* section 16.9.23.2 */
219 if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
221 break; /* nothing received - leave for() loop */
224 length = info->rxbd[info->rxIdx].cbd_datlen;
226 if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
227 printf("%s[%d] err: %x\n",
228 __FUNCTION__, __LINE__,
229 info->rxbd[info->rxIdx].cbd_sc);
231 printf("%s[%d] err: %x\n",
232 __FUNCTION__, __LINE__,
233 info->rxbd[info->rxIdx].cbd_sc);
238 /* Pass the packet up to the protocol layers. */
239 NetReceive(NetRxPackets[info->rxIdx], length);
241 fecp->eir |= FEC_EIR_RXF;
244 /* Give the buffer back to the FEC. */
245 info->rxbd[info->rxIdx].cbd_datlen = 0;
247 /* wrap around buffer index when necessary */
248 if (info->rxIdx == LAST_PKTBUFSRX) {
249 info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
252 info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
256 /* Try to fill Buffer Descriptors */
257 fecp->rdar = 0x01000000; /* Descriptor polling active */
264 void dbgFecRegs(struct eth_device *dev)
266 struct fec_info_s *info = dev->priv;
267 volatile fec_t *fecp = (fec_t *) (info->iobase);
270 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
271 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
272 printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
273 printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
274 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
275 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
276 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
277 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
278 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
279 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
280 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
281 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
282 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
283 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
284 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
285 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
286 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
287 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
288 printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
289 printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
290 printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
291 printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
292 printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
295 printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
297 printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
298 fecp->rmon_t_packets);
299 printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
300 fecp->rmon_t_bc_pkt);
301 printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
302 fecp->rmon_t_mc_pkt);
303 printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
304 fecp->rmon_t_crc_align);
305 printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
306 fecp->rmon_t_undersize);
307 printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
308 fecp->rmon_t_oversize);
309 printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
311 printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
313 printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
315 printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
317 printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
318 fecp->rmon_t_p65to127);
319 printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
320 fecp->rmon_t_p128to255);
321 printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
322 fecp->rmon_t_p256to511);
323 printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
324 fecp->rmon_t_p512to1023);
325 printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
326 fecp->rmon_t_p1024to2047);
327 printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
328 fecp->rmon_t_p_gte2048);
329 printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
330 fecp->rmon_t_octets);
333 printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
335 printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
336 fecp->ieee_t_frame_ok);
337 printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
339 printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
341 printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
343 printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
345 printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
347 printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
348 fecp->ieee_t_macerr);
349 printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
351 printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
353 printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
355 printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
356 fecp->ieee_t_octets_ok);
359 printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
361 printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
362 fecp->rmon_r_packets);
363 printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
364 fecp->rmon_r_bc_pkt);
365 printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
366 fecp->rmon_r_mc_pkt);
367 printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
368 fecp->rmon_r_crc_align);
369 printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
370 fecp->rmon_r_undersize);
371 printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
372 fecp->rmon_r_oversize);
373 printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
375 printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
377 printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
379 printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
380 fecp->rmon_r_p65to127);
381 printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
382 fecp->rmon_r_p128to255);
383 printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
384 fecp->rmon_r_p256to511);
385 printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
386 fecp->rmon_r_p512to1023);
387 printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
388 fecp->rmon_r_p1024to2047);
389 printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
390 fecp->rmon_r_p_gte2048);
391 printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
392 fecp->rmon_r_octets);
395 printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
397 printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
398 fecp->ieee_r_frame_ok);
399 printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
401 printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
403 printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
404 fecp->ieee_r_macerr);
405 printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
407 printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
408 fecp->ieee_r_octets_ok);
414 int fec_init(struct eth_device *dev, bd_t * bd)
416 struct fec_info_s *info = dev->priv;
417 volatile fec_t *fecp = (fec_t *) (info->iobase);
421 fecpin_setclear(dev, 1);
425 #if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
426 defined (CONFIG_SYS_DISCOVER_PHY)
430 setFecDuplexSpeed(fecp, bd, info->dup_spd);
432 #ifndef CONFIG_SYS_DISCOVER_PHY
433 setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
434 #endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
435 #endif /* CONFIG_CMD_MII || CONFIG_MII */
437 /* We use strictly polling mode only */
440 /* Clear any pending interrupt */
441 fecp->eir = 0xffffffff;
443 /* Set station address */
444 if ((u32) fecp == CONFIG_SYS_FEC0_IOBASE) {
445 #ifdef CONFIG_SYS_FEC1_IOBASE
446 volatile fec_t *fecp1 = (fec_t *) (CONFIG_SYS_FEC1_IOBASE);
447 ea = &bd->bi_enet1addr[0];
449 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
450 fecp1->paur = (ea[4] << 24) | (ea[5] << 16);
452 ea = &bd->bi_enetaddr[0];
454 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
455 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
457 #ifdef CONFIG_SYS_FEC0_IOBASE
458 volatile fec_t *fecp0 = (fec_t *) (CONFIG_SYS_FEC0_IOBASE);
459 ea = &bd->bi_enetaddr[0];
461 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
462 fecp0->paur = (ea[4] << 24) | (ea[5] << 16);
464 #ifdef CONFIG_SYS_FEC1_IOBASE
465 ea = &bd->bi_enet1addr[0];
467 (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
468 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
472 /* Clear unicast address hash table */
476 /* Clear multicast address hash table */
480 /* Set maximum receive buffer size. */
481 fecp->emrbr = PKT_MAXBLR_SIZE;
484 * Setup Buffers and Buffer Desriptors
490 * Setup Receiver Buffer Descriptors (13.14.24.18)
494 for (i = 0; i < PKTBUFSRX; i++) {
495 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
496 info->rxbd[i].cbd_datlen = 0; /* Reset */
497 info->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
499 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
502 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
506 for (i = 0; i < TX_BUF_CNT; i++) {
507 info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
508 info->txbd[i].cbd_datlen = 0; /* Reset */
509 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
511 info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
513 /* Set receive and transmit descriptor base */
514 fecp->erdsr = (unsigned int)(&info->rxbd[0]);
515 fecp->etdsr = (unsigned int)(&info->txbd[0]);
517 /* Now enable the transmit and receive processing */
518 fecp->ecr |= FEC_ECR_ETHER_EN;
520 /* And last, try to fill Rx Buffer Descriptors */
521 fecp->rdar = 0x01000000; /* Descriptor polling active */
526 void fec_reset(struct eth_device *dev)
528 struct fec_info_s *info = dev->priv;
529 volatile fec_t *fecp = (fec_t *) (info->iobase);
532 fecp->ecr = FEC_ECR_RESET;
533 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
536 if (i == FEC_RESET_DELAY) {
537 printf("FEC_RESET_DELAY timeout\n");
541 void fec_halt(struct eth_device *dev)
543 struct fec_info_s *info = dev->priv;
547 fecpin_setclear(dev, 0);
549 info->rxIdx = info->txIdx = 0;
550 memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
551 memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
552 memset(info->txbuf, 0, DBUF_LENGTH);
555 int mcffec_initialize(bd_t * bis)
557 struct eth_device *dev;
559 #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
560 u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
563 for (i = 0; i < sizeof(fec_info) / sizeof(fec_info[0]); i++) {
566 (struct eth_device *)memalign(CONFIG_SYS_CACHELINE_SIZE,
571 memset(dev, 0, sizeof(*dev));
573 sprintf(dev->name, "FEC%d", fec_info[i].index);
575 dev->priv = &fec_info[i];
576 dev->init = fec_init;
577 dev->halt = fec_halt;
578 dev->send = fec_send;
579 dev->recv = fec_recv;
581 /* setup Receive and Transmit buffer descriptor */
582 #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
583 fec_info[i].rxbd = (cbd_t *)((u32)fec_info[i].rxbd + tmp);
584 tmp = (u32)fec_info[i].rxbd;
586 (cbd_t *)((u32)fec_info[i].txbd + tmp +
587 (PKTBUFSRX * sizeof(cbd_t)));
588 tmp = (u32)fec_info[i].txbd;
590 (char *)((u32)fec_info[i].txbuf + tmp +
591 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
592 tmp = (u32)fec_info[i].txbuf;
595 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
596 (PKTBUFSRX * sizeof(cbd_t)));
598 (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
599 (TX_BUF_CNT * sizeof(cbd_t)));
601 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
605 printf("rxbd %x txbd %x\n",
606 (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
609 fec_info[i].phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
613 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
614 miiphy_register(dev->name,
615 mcffec_miiphy_read, mcffec_miiphy_write);
618 fec_info[i - 1].next = &fec_info[i];
620 fec_info[i - 1].next = &fec_info[0];
623 bis->bi_ethspeed = 10;