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,
34 #ifdef CONFIG_FSLDMAFEC
38 /* Ethernet Transmit and Receive Buffers */
39 #define DBUF_LENGTH 1520
40 #define PKT_MAXBUF_SIZE 1518
41 #define PKT_MINBUF_SIZE 64
42 #define PKT_MAXBLR_SIZE 1536
43 #define LAST_PKTBUFSRX PKTBUFSRX - 1
44 #define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
45 #define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
46 #define FIFO_ERRSTAT (FIFO_STAT_RXW | FIFO_STAT_UF | FIFO_STAT_OF)
48 /* RxBD bits definitions */
49 #define BD_ENET_RX_ERR (BD_ENET_RX_LG | BD_ENET_RX_NO | BD_ENET_RX_CR | \
50 BD_ENET_RX_OV | BD_ENET_RX_TR)
52 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
53 #include <asm/immap.h>
54 #include <asm/fsl_mcdmafec.h>
58 DECLARE_GLOBAL_DATA_PTR;
60 struct fec_info_dma fec_info[] = {
61 #ifdef CFG_FEC0_IOBASE
64 CFG_FEC0_IOBASE, /* io base */
65 CFG_FEC0_PINMUX, /* gpio pin muxing */
66 CFG_FEC0_MIIBASE, /* mii base */
68 0, /* duplex and speed */
76 0, /* initialized flag */
77 (struct fec_info_dma *)-1, /* next */
78 FEC0_RX_TASK, /* rxTask */
79 FEC0_TX_TASK, /* txTask */
80 FEC0_RX_PRIORITY, /* rxPri */
81 FEC0_TX_PRIORITY, /* txPri */
82 FEC0_RX_INIT, /* rxInit */
83 FEC0_TX_INIT, /* txInit */
88 #ifdef CFG_FEC1_IOBASE
91 CFG_FEC1_IOBASE, /* io base */
92 CFG_FEC1_PINMUX, /* gpio pin muxing */
93 CFG_FEC1_MIIBASE, /* mii base */
95 0, /* duplex and speed */
97 0, /* phy name init */
98 #ifdef CFG_DMA_USE_INTSRAM
99 DBUF_LENGTH, /* RX BD */
107 0, /* initialized flag */
108 (struct fec_info_dma *)-1, /* next */
109 FEC1_RX_TASK, /* rxTask */
110 FEC1_TX_TASK, /* txTask */
111 FEC1_RX_PRIORITY, /* rxPri */
112 FEC1_TX_PRIORITY, /* txPri */
113 FEC1_RX_INIT, /* rxInit */
114 FEC1_TX_INIT, /* txInit */
115 0, /* usedTbdIndex */
121 static int fec_send(struct eth_device *dev, volatile void *packet, int length);
122 static int fec_recv(struct eth_device *dev);
123 static int fec_init(struct eth_device *dev, bd_t * bd);
124 static void fec_halt(struct eth_device *dev);
127 static void dbg_fec_regs(struct eth_device *dev)
129 struct fec_info_dma *info = dev->priv;
130 volatile fecdma_t *fecp = (fecdma_t *) (info->iobase);
133 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
134 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
135 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
136 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
137 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
138 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
139 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
140 printf("r hash %x - %x\n", (int)&fecp->rhr, fecp->rhr);
141 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
142 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
143 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
144 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
145 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
146 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
147 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
148 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
149 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
150 printf("r_fdata %x - %x\n", (int)&fecp->rfdr, fecp->rfdr);
151 printf("r_fstat %x - %x\n", (int)&fecp->rfsr, fecp->rfsr);
152 printf("r_fctrl %x - %x\n", (int)&fecp->rfcr, fecp->rfcr);
153 printf("r_flrfp %x - %x\n", (int)&fecp->rlrfp, fecp->rlrfp);
154 printf("r_flwfp %x - %x\n", (int)&fecp->rlwfp, fecp->rlwfp);
155 printf("r_frfar %x - %x\n", (int)&fecp->rfar, fecp->rfar);
156 printf("r_frfrp %x - %x\n", (int)&fecp->rfrp, fecp->rfrp);
157 printf("r_frfwp %x - %x\n", (int)&fecp->rfwp, fecp->rfwp);
158 printf("t_fdata %x - %x\n", (int)&fecp->tfdr, fecp->tfdr);
159 printf("t_fstat %x - %x\n", (int)&fecp->tfsr, fecp->tfsr);
160 printf("t_fctrl %x - %x\n", (int)&fecp->tfcr, fecp->tfcr);
161 printf("t_flrfp %x - %x\n", (int)&fecp->tlrfp, fecp->tlrfp);
162 printf("t_flwfp %x - %x\n", (int)&fecp->tlwfp, fecp->tlwfp);
163 printf("t_ftfar %x - %x\n", (int)&fecp->tfar, fecp->tfar);
164 printf("t_ftfrp %x - %x\n", (int)&fecp->tfrp, fecp->tfrp);
165 printf("t_ftfwp %x - %x\n", (int)&fecp->tfwp, fecp->tfwp);
166 printf("frst %x - %x\n", (int)&fecp->frst, fecp->frst);
167 printf("ctcwr %x - %x\n", (int)&fecp->ctcwr, fecp->ctcwr);
171 static void set_fec_duplex_speed(volatile fecdma_t * fecp, bd_t * bd,
174 if ((dup_spd >> 16) == FULL) {
175 /* Set maximum frame length */
176 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
177 FEC_RCR_PROM | 0x100;
178 fecp->tcr = FEC_TCR_FDEN;
180 /* Half duplex mode */
181 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
182 FEC_RCR_MII_MODE | FEC_RCR_DRT;
183 fecp->tcr &= ~FEC_TCR_FDEN;
186 if ((dup_spd & 0xFFFF) == _100BASET) {
190 bd->bi_ethspeed = 100;
195 bd->bi_ethspeed = 10;
199 static int fec_send(struct eth_device *dev, volatile void *packet, int length)
201 struct fec_info_dma *info = dev->priv;
202 cbd_t *pTbd, *pUsedTbd;
205 miiphy_read(dev->name, info->phy_addr, PHY_BMSR, &phyStatus);
207 /* process all the consumed TBDs */
208 while (info->cleanTbdNum < CFG_TX_ETH_BUFFER) {
209 pUsedTbd = &info->txbd[info->usedTbdIdx];
210 if (pUsedTbd->cbd_sc & BD_ENET_TX_READY) {
212 printf("Cannot clean TBD %d, in use\n",
218 /* clean this buffer descriptor */
219 if (info->usedTbdIdx == (CFG_TX_ETH_BUFFER - 1))
220 pUsedTbd->cbd_sc = BD_ENET_TX_WRAP;
222 pUsedTbd->cbd_sc = 0;
224 /* update some indeces for a correct handling of the TBD ring */
226 info->usedTbdIdx = (info->usedTbdIdx + 1) % CFG_TX_ETH_BUFFER;
229 /* Check for valid length of data. */
230 if ((length > 1500) || (length <= 0)) {
234 /* Check the number of vacant TxBDs. */
235 if (info->cleanTbdNum < 1) {
236 printf("No available TxBDs ...\n");
240 /* Get the first TxBD to send the mac header */
241 pTbd = &info->txbd[info->txIdx];
242 pTbd->cbd_datlen = length;
243 pTbd->cbd_bufaddr = (u32) packet;
244 pTbd->cbd_sc |= BD_ENET_TX_LAST | BD_ENET_TX_TC | BD_ENET_TX_READY;
245 info->txIdx = (info->txIdx + 1) % CFG_TX_ETH_BUFFER;
247 /* Enable DMA transmit task */
248 MCD_continDma(info->txTask);
250 info->cleanTbdNum -= 1;
252 /* wait until frame is sent . */
253 while (pTbd->cbd_sc & BD_ENET_TX_READY) {
257 return (int)(info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
260 static int fec_recv(struct eth_device *dev)
262 struct fec_info_dma *info = dev->priv;
263 volatile fecdma_t *fecp = (fecdma_t *) (info->iobase);
265 cbd_t *pRbd = &info->rxbd[info->rxIdx];
267 int frame_length, len = 0;
269 /* Check if any critical events have happened */
274 if (ievent & (FEC_EIR_BABT | FEC_EIR_TXERR | FEC_EIR_RXERR)) {
275 printf("fec_recv: error\n");
281 if (ievent & FEC_EIR_HBERR) {
282 /* Heartbeat error */
283 fecp->tcr |= FEC_TCR_GTS;
286 if (ievent & FEC_EIR_GRA) {
287 /* Graceful stop complete */
288 if (fecp->tcr & FEC_TCR_GTS) {
289 printf("fec_recv: tcr_gts\n");
291 fecp->tcr &= ~FEC_TCR_GTS;
297 if (!(pRbd->cbd_sc & BD_ENET_RX_EMPTY)) {
298 if ((pRbd->cbd_sc & BD_ENET_RX_LAST)
299 && !(pRbd->cbd_sc & BD_ENET_RX_ERR)
300 && ((pRbd->cbd_datlen - 4) > 14)) {
302 /* Get buffer address and size */
303 frame_length = pRbd->cbd_datlen - 4;
305 /* Fill the buffer and pass it to upper layers */
306 NetReceive((volatile uchar *)pRbd->cbd_bufaddr,
311 /* Reset buffer descriptor as empty */
312 if ((info->rxIdx) == (PKTBUFSRX - 1))
313 pRbd->cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
315 pRbd->cbd_sc = BD_ENET_RX_EMPTY;
317 pRbd->cbd_datlen = PKTSIZE_ALIGN;
319 /* Now, we have an empty RxBD, restart the DMA receive task */
320 MCD_continDma(info->rxTask);
322 /* Increment BD count */
323 info->rxIdx = (info->rxIdx + 1) % PKTBUFSRX;
329 static void fec_set_hwaddr(volatile fecdma_t * fecp, u8 * mac)
331 u8 currByte; /* byte for which to compute the CRC */
332 int byte; /* loop - counter */
333 int bit; /* loop - counter */
334 u32 crc = 0xffffffff; /* initial value */
336 for (byte = 0; byte < 6; byte++) {
337 currByte = mac[byte];
338 for (bit = 0; bit < 8; bit++) {
339 if ((currByte & 0x01) ^ (crc & 0x01)) {
341 crc = crc ^ 0xedb88320;
351 /* Set individual hash table register */
353 fecp->ialr = (1 << (crc - 32));
357 fecp->iaur = (1 << crc);
360 /* Set physical address */
361 fecp->palr = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
362 fecp->paur = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
364 /* Clear multicast address hash table */
369 static int fec_init(struct eth_device *dev, bd_t * bd)
371 struct fec_info_dma *info = dev->priv;
372 volatile fecdma_t *fecp = (fecdma_t *) (info->iobase);
376 printf("fec_init: iobase 0x%08x ...\n", info->iobase);
379 fecpin_setclear(dev, 1);
383 #if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
384 defined (CFG_DISCOVER_PHY)
388 set_fec_duplex_speed(fecp, bd, info->dup_spd);
390 #ifndef CFG_DISCOVER_PHY
391 set_fec_duplex_speed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
392 #endif /* ifndef CFG_DISCOVER_PHY */
393 #endif /* CONFIG_CMD_MII || CONFIG_MII */
395 /* We use strictly polling mode only */
398 /* Clear any pending interrupt */
399 fecp->eir = 0xffffffff;
401 /* Set station address */
402 if ((u32) fecp == CFG_FEC0_IOBASE) {
403 fec_set_hwaddr(fecp, bd->bi_enetaddr);
405 fec_set_hwaddr(fecp, bd->bi_enet1addr);
408 /* Set Opcode/Pause Duration Register */
409 fecp->opd = 0x00010020;
411 /* Setup Buffers and Buffer Desriptors */
415 /* Setup Receiver Buffer Descriptors (13.14.24.18)
416 * Settings: Empty, Wrap */
417 for (i = 0; i < PKTBUFSRX; i++) {
418 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
419 info->rxbd[i].cbd_datlen = PKTSIZE_ALIGN;
420 info->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
422 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
424 /* Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
425 * Settings: Last, Tx CRC */
426 for (i = 0; i < CFG_TX_ETH_BUFFER; i++) {
427 info->txbd[i].cbd_sc = 0;
428 info->txbd[i].cbd_datlen = 0;
429 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
431 info->txbd[CFG_TX_ETH_BUFFER - 1].cbd_sc |= BD_ENET_TX_WRAP;
433 info->usedTbdIdx = 0;
434 info->cleanTbdNum = CFG_TX_ETH_BUFFER;
436 /* Set Rx FIFO alarm and granularity value */
437 fecp->rfcr = 0x0c000000;
438 fecp->rfar = 0x0000030c;
440 /* Set Tx FIFO granularity value */
441 fecp->tfcr = FIFO_CTRL_FRAME | FIFO_CTRL_GR(6) | 0x00040000;
442 fecp->tfar = 0x00000080;
445 fecp->ctcwr = 0x03000000;
447 /* Enable DMA receive task */
448 MCD_startDma(info->rxTask, /* Dma channel */
449 (s8 *) info->rxbd, /*Source Address */
450 0, /* Source increment */
451 (s8 *) (&fecp->rfdr), /* dest */
452 4, /* dest increment */
455 info->rxInit, /* initiator */
456 info->rxPri, /* priority */
457 (MCD_FECRX_DMA | MCD_TT_FLAGS_DEF), /* Flags */
458 (MCD_NO_CSUM | MCD_NO_BYTE_SWAP) /* Function description */
461 /* Enable DMA tx task with no ready buffer descriptors */
462 MCD_startDma(info->txTask, /* Dma channel */
463 (s8 *) info->txbd, /*Source Address */
464 0, /* Source increment */
465 (s8 *) (&fecp->tfdr), /* dest */
469 info->txInit, /* initiator */
470 info->txPri, /* priority */
471 (MCD_FECTX_DMA | MCD_TT_FLAGS_DEF), /* Flags */
472 (MCD_NO_CSUM | MCD_NO_BYTE_SWAP) /* Function description */
475 /* Now enable the transmit and receive processing */
476 fecp->ecr |= FEC_ECR_ETHER_EN;
481 static void fec_halt(struct eth_device *dev)
483 struct fec_info_dma *info = dev->priv;
484 volatile fecdma_t *fecp = (fecdma_t *) (info->iobase);
485 int counter = 0xffff;
487 /* issue graceful stop command to the FEC transmitter if necessary */
488 fecp->tcr |= FEC_TCR_GTS;
490 /* wait for graceful stop to register */
491 while ((counter--) && (!(fecp->eir & FEC_EIR_GRA))) ;
493 /* Disable DMA tasks */
494 MCD_killDma(info->txTask);
495 MCD_killDma(info->rxTask);;
497 /* Disable the Ethernet Controller */
498 fecp->ecr &= ~FEC_ECR_ETHER_EN;
500 /* Clear FIFO status registers */
501 fecp->rfsr &= FIFO_ERRSTAT;
502 fecp->tfsr &= FIFO_ERRSTAT;
504 fecp->frst = 0x01000000;
506 /* Issue a reset command to the FEC chip */
507 fecp->ecr |= FEC_ECR_RESET;
509 /* wait at least 20 clock cycles */
513 printf("Ethernet task stopped\n");
517 int mcdmafec_initialize(bd_t * bis)
519 struct eth_device *dev;
521 #ifdef CFG_DMA_USE_INTSRAM
522 u32 tmp = CFG_INTSRAM + 0x2000;
525 for (i = 0; i < sizeof(fec_info) / sizeof(fec_info[0]); i++) {
528 (struct eth_device *)memalign(CFG_CACHELINE_SIZE,
533 memset(dev, 0, sizeof(*dev));
535 sprintf(dev->name, "FEC%d", fec_info[i].index);
537 dev->priv = &fec_info[i];
538 dev->init = fec_init;
539 dev->halt = fec_halt;
540 dev->send = fec_send;
541 dev->recv = fec_recv;
543 /* setup Receive and Transmit buffer descriptor */
544 #ifdef CFG_DMA_USE_INTSRAM
545 fec_info[i].rxbd = (int)fec_info[i].rxbd + tmp;
546 tmp = fec_info[i].rxbd;
548 (int)fec_info[i].txbd + tmp + (PKTBUFSRX * sizeof(cbd_t));
549 tmp = fec_info[i].txbd;
551 (int)fec_info[i].txbuf + tmp +
552 (CFG_TX_ETH_BUFFER * sizeof(cbd_t));
553 tmp = fec_info[i].txbuf;
556 (cbd_t *) memalign(CFG_CACHELINE_SIZE,
557 (PKTBUFSRX * sizeof(cbd_t)));
559 (cbd_t *) memalign(CFG_CACHELINE_SIZE,
560 (CFG_TX_ETH_BUFFER * sizeof(cbd_t)));
562 (char *)memalign(CFG_CACHELINE_SIZE, DBUF_LENGTH);
566 printf("rxbd %x txbd %x\n",
567 (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
570 fec_info[i].phy_name = (char *)memalign(CFG_CACHELINE_SIZE, 32);
574 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
575 miiphy_register(dev->name,
576 mcffec_miiphy_read, mcffec_miiphy_write);
580 fec_info[i - 1].next = &fec_info[i];
582 fec_info[i - 1].next = &fec_info[0];
585 bis->bi_ethspeed = 10;
590 #endif /* CONFIG_CMD_NET && CONFIG_NET_MULTI */
591 #endif /* CONFIG_FSLDMAFEC */