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 */
103 0, /* initialized flag */
104 (struct fec_info_dma
*)-1, /* next */
105 FEC1_RX_TASK
, /* rxTask */
106 FEC1_TX_TASK
, /* txTask */
107 FEC1_RX_PRIORITY
, /* rxPri */
108 FEC1_TX_PRIORITY
, /* txPri */
109 FEC1_RX_INIT
, /* rxInit */
110 FEC1_TX_INIT
, /* txInit */
111 0, /* usedTbdIndex */
117 static int fec_send(struct eth_device
*dev
, volatile void *packet
, int length
);
118 static int fec_recv(struct eth_device
*dev
);
119 static int fec_init(struct eth_device
*dev
, bd_t
* bd
);
120 static void fec_halt(struct eth_device
*dev
);
123 static void dbg_fec_regs(struct eth_device
*dev
)
125 struct fec_info_dma
*info
= dev
->priv
;
126 volatile fecdma_t
*fecp
= (fecdma_t
*) (info
->iobase
);
129 printf("ievent %x - %x\n", (int)&fecp
->eir
, fecp
->eir
);
130 printf("imask %x - %x\n", (int)&fecp
->eimr
, fecp
->eimr
);
131 printf("ecntrl %x - %x\n", (int)&fecp
->ecr
, fecp
->ecr
);
132 printf("mii_mframe %x - %x\n", (int)&fecp
->mmfr
, fecp
->mmfr
);
133 printf("mii_speed %x - %x\n", (int)&fecp
->mscr
, fecp
->mscr
);
134 printf("mii_ctrlstat %x - %x\n", (int)&fecp
->mibc
, fecp
->mibc
);
135 printf("r_cntrl %x - %x\n", (int)&fecp
->rcr
, fecp
->rcr
);
136 printf("r hash %x - %x\n", (int)&fecp
->rhr
, fecp
->rhr
);
137 printf("x_cntrl %x - %x\n", (int)&fecp
->tcr
, fecp
->tcr
);
138 printf("padr_l %x - %x\n", (int)&fecp
->palr
, fecp
->palr
);
139 printf("padr_u %x - %x\n", (int)&fecp
->paur
, fecp
->paur
);
140 printf("op_pause %x - %x\n", (int)&fecp
->opd
, fecp
->opd
);
141 printf("iadr_u %x - %x\n", (int)&fecp
->iaur
, fecp
->iaur
);
142 printf("iadr_l %x - %x\n", (int)&fecp
->ialr
, fecp
->ialr
);
143 printf("gadr_u %x - %x\n", (int)&fecp
->gaur
, fecp
->gaur
);
144 printf("gadr_l %x - %x\n", (int)&fecp
->galr
, fecp
->galr
);
145 printf("x_wmrk %x - %x\n", (int)&fecp
->tfwr
, fecp
->tfwr
);
146 printf("r_fdata %x - %x\n", (int)&fecp
->rfdr
, fecp
->rfdr
);
147 printf("r_fstat %x - %x\n", (int)&fecp
->rfsr
, fecp
->rfsr
);
148 printf("r_fctrl %x - %x\n", (int)&fecp
->rfcr
, fecp
->rfcr
);
149 printf("r_flrfp %x - %x\n", (int)&fecp
->rlrfp
, fecp
->rlrfp
);
150 printf("r_flwfp %x - %x\n", (int)&fecp
->rlwfp
, fecp
->rlwfp
);
151 printf("r_frfar %x - %x\n", (int)&fecp
->rfar
, fecp
->rfar
);
152 printf("r_frfrp %x - %x\n", (int)&fecp
->rfrp
, fecp
->rfrp
);
153 printf("r_frfwp %x - %x\n", (int)&fecp
->rfwp
, fecp
->rfwp
);
154 printf("t_fdata %x - %x\n", (int)&fecp
->tfdr
, fecp
->tfdr
);
155 printf("t_fstat %x - %x\n", (int)&fecp
->tfsr
, fecp
->tfsr
);
156 printf("t_fctrl %x - %x\n", (int)&fecp
->tfcr
, fecp
->tfcr
);
157 printf("t_flrfp %x - %x\n", (int)&fecp
->tlrfp
, fecp
->tlrfp
);
158 printf("t_flwfp %x - %x\n", (int)&fecp
->tlwfp
, fecp
->tlwfp
);
159 printf("t_ftfar %x - %x\n", (int)&fecp
->tfar
, fecp
->tfar
);
160 printf("t_ftfrp %x - %x\n", (int)&fecp
->tfrp
, fecp
->tfrp
);
161 printf("t_ftfwp %x - %x\n", (int)&fecp
->tfwp
, fecp
->tfwp
);
162 printf("frst %x - %x\n", (int)&fecp
->frst
, fecp
->frst
);
163 printf("ctcwr %x - %x\n", (int)&fecp
->ctcwr
, fecp
->ctcwr
);
167 static void set_fec_duplex_speed(volatile fecdma_t
* fecp
, bd_t
* bd
, int dup_spd
)
169 if ((dup_spd
>> 16) == FULL
) {
170 /* Set maximum frame length */
171 fecp
->rcr
= FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE
) | FEC_RCR_MII_MODE
|
172 FEC_RCR_PROM
| 0x100;
173 fecp
->tcr
= FEC_TCR_FDEN
;
175 /* Half duplex mode */
176 fecp
->rcr
= FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE
) |
177 FEC_RCR_MII_MODE
| FEC_RCR_DRT
;
178 fecp
->tcr
&= ~FEC_TCR_FDEN
;
181 if ((dup_spd
& 0xFFFF) == _100BASET
) {
185 bd
->bi_ethspeed
= 100;
190 bd
->bi_ethspeed
= 10;
194 static int fec_send(struct eth_device
*dev
, volatile void *packet
, int length
)
196 struct fec_info_dma
*info
= dev
->priv
;
197 cbd_t
*pTbd
, *pUsedTbd
;
200 miiphy_read(dev
->name
, info
->phy_addr
, PHY_BMSR
, &phyStatus
);
202 /* process all the consumed TBDs */
203 while (info
->cleanTbdNum
< CFG_TX_ETH_BUFFER
) {
204 pUsedTbd
= &info
->txbd
[info
->usedTbdIdx
];
205 if (pUsedTbd
->cbd_sc
& BD_ENET_TX_READY
) {
207 printf("Cannot clean TBD %d, in use\n",
213 /* clean this buffer descriptor */
214 if (info
->usedTbdIdx
== (CFG_TX_ETH_BUFFER
- 1))
215 pUsedTbd
->cbd_sc
= BD_ENET_TX_WRAP
;
217 pUsedTbd
->cbd_sc
= 0;
219 /* update some indeces for a correct handling of the TBD ring */
221 info
->usedTbdIdx
= (info
->usedTbdIdx
+ 1) % CFG_TX_ETH_BUFFER
;
224 /* Check for valid length of data. */
225 if ((length
> 1500) || (length
<= 0)) {
229 /* Check the number of vacant TxBDs. */
230 if (info
->cleanTbdNum
< 1) {
231 printf("No available TxBDs ...\n");
235 /* Get the first TxBD to send the mac header */
236 pTbd
= &info
->txbd
[info
->txIdx
];
237 pTbd
->cbd_datlen
= length
;
238 pTbd
->cbd_bufaddr
= (u32
) packet
;
239 pTbd
->cbd_sc
|= BD_ENET_TX_LAST
| BD_ENET_TX_TC
| BD_ENET_TX_READY
;
240 info
->txIdx
= (info
->txIdx
+ 1) % CFG_TX_ETH_BUFFER
;
242 /* Enable DMA transmit task */
243 MCD_continDma(info
->txTask
);
245 info
->cleanTbdNum
-= 1;
247 /* wait until frame is sent . */
248 while (pTbd
->cbd_sc
& BD_ENET_TX_READY
) {
252 return (int)(info
->txbd
[info
->txIdx
].cbd_sc
& BD_ENET_TX_STATS
);
255 static int fec_recv(struct eth_device
*dev
)
257 struct fec_info_dma
*info
= dev
->priv
;
258 volatile fecdma_t
*fecp
= (fecdma_t
*) (info
->iobase
);
260 cbd_t
*pRbd
= &info
->rxbd
[info
->rxIdx
];
262 int frame_length
, len
= 0;
264 /* Check if any critical events have happened */
269 if (ievent
& (FEC_EIR_BABT
| FEC_EIR_TXERR
| FEC_EIR_RXERR
)) {
270 printf("fec_recv: error\n");
276 if (ievent
& FEC_EIR_HBERR
) {
277 /* Heartbeat error */
278 fecp
->tcr
|= FEC_TCR_GTS
;
281 if (ievent
& FEC_EIR_GRA
) {
282 /* Graceful stop complete */
283 if (fecp
->tcr
& FEC_TCR_GTS
) {
284 printf("fec_recv: tcr_gts\n");
286 fecp
->tcr
&= ~FEC_TCR_GTS
;
292 if (!(pRbd
->cbd_sc
& BD_ENET_RX_EMPTY
)) {
293 if ((pRbd
->cbd_sc
& BD_ENET_RX_LAST
)
294 && !(pRbd
->cbd_sc
& BD_ENET_RX_ERR
)
295 && ((pRbd
->cbd_datlen
- 4) > 14)) {
297 /* Get buffer address and size */
298 frame_length
= pRbd
->cbd_datlen
- 4;
300 /* Fill the buffer and pass it to upper layers */
301 NetReceive((volatile uchar
*)pRbd
->cbd_bufaddr
,
306 /* Reset buffer descriptor as empty */
307 if ((info
->rxIdx
) == (PKTBUFSRX
- 1))
308 pRbd
->cbd_sc
= (BD_ENET_RX_WRAP
| BD_ENET_RX_EMPTY
);
310 pRbd
->cbd_sc
= BD_ENET_RX_EMPTY
;
312 pRbd
->cbd_datlen
= PKTSIZE_ALIGN
;
314 /* Now, we have an empty RxBD, restart the DMA receive task */
315 MCD_continDma(info
->rxTask
);
317 /* Increment BD count */
318 info
->rxIdx
= (info
->rxIdx
+ 1) % PKTBUFSRX
;
324 static void fec_set_hwaddr(volatile fecdma_t
* fecp
, u8
* mac
)
326 u8 currByte
; /* byte for which to compute the CRC */
327 int byte
; /* loop - counter */
328 int bit
; /* loop - counter */
329 u32 crc
= 0xffffffff; /* initial value */
331 for (byte
= 0; byte
< 6; byte
++) {
332 currByte
= mac
[byte
];
333 for (bit
= 0; bit
< 8; bit
++) {
334 if ((currByte
& 0x01) ^ (crc
& 0x01)) {
336 crc
= crc
^ 0xedb88320;
346 /* Set individual hash table register */
348 fecp
->ialr
= (1 << (crc
- 32));
352 fecp
->iaur
= (1 << crc
);
355 /* Set physical address */
356 fecp
->palr
= (mac
[0] << 24) + (mac
[1] << 16) + (mac
[2] << 8) + mac
[3];
357 fecp
->paur
= (mac
[4] << 24) + (mac
[5] << 16) + 0x8808;
359 /* Clear multicast address hash table */
364 static int fec_init(struct eth_device
*dev
, bd_t
* bd
)
366 struct fec_info_dma
*info
= dev
->priv
;
367 volatile fecdma_t
*fecp
= (fecdma_t
*) (info
->iobase
);
371 printf("fec_init: iobase 0x%08x ...\n", info
->iobase
);
374 fecpin_setclear(dev
, 1);
378 #if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
379 defined (CFG_DISCOVER_PHY)
383 set_fec_duplex_speed(fecp
, bd
, info
->dup_spd
);
385 #ifndef CFG_DISCOVER_PHY
386 set_fec_duplex_speed(fecp
, bd
, (FECDUPLEX
<< 16) | FECSPEED
);
387 #endif /* ifndef CFG_DISCOVER_PHY */
388 #endif /* CONFIG_CMD_MII || CONFIG_MII */
390 /* We use strictly polling mode only */
393 /* Clear any pending interrupt */
394 fecp
->eir
= 0xffffffff;
396 /* Set station address */
397 if ((u32
) fecp
== CFG_FEC0_IOBASE
) {
398 fec_set_hwaddr(fecp
, bd
->bi_enetaddr
);
400 fec_set_hwaddr(fecp
, bd
->bi_enet1addr
);
403 /* Set Opcode/Pause Duration Register */
404 fecp
->opd
= 0x00010020;
406 /* Setup Buffers and Buffer Desriptors */
410 /* Setup Receiver Buffer Descriptors (13.14.24.18)
411 * Settings: Empty, Wrap */
412 for (i
= 0; i
< PKTBUFSRX
; i
++) {
413 info
->rxbd
[i
].cbd_sc
= BD_ENET_RX_EMPTY
;
414 info
->rxbd
[i
].cbd_datlen
= PKTSIZE_ALIGN
;
415 info
->rxbd
[i
].cbd_bufaddr
= (uint
) NetRxPackets
[i
];
417 info
->rxbd
[PKTBUFSRX
- 1].cbd_sc
|= BD_ENET_RX_WRAP
;
419 /* Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
420 * Settings: Last, Tx CRC */
421 for (i
= 0; i
< CFG_TX_ETH_BUFFER
; i
++) {
422 info
->txbd
[i
].cbd_sc
= 0;
423 info
->txbd
[i
].cbd_datlen
= 0;
424 info
->txbd
[i
].cbd_bufaddr
= (uint
) (&info
->txbuf
[0]);
426 info
->txbd
[CFG_TX_ETH_BUFFER
- 1].cbd_sc
|= BD_ENET_TX_WRAP
;
428 info
->usedTbdIdx
= 0;
429 info
->cleanTbdNum
= CFG_TX_ETH_BUFFER
;
431 /* Set Rx FIFO alarm and granularity value */
432 fecp
->rfcr
= 0x0c000000;
433 fecp
->rfar
= 0x0000030c;
435 /* Set Tx FIFO granularity value */
436 fecp
->tfcr
= FIFO_CTRL_FRAME
| FIFO_CTRL_GR(6) | 0x00040000;
437 fecp
->tfar
= 0x00000080;
440 fecp
->ctcwr
= 0x03000000;
442 /* Enable DMA receive task */
443 MCD_startDma(info
->rxTask
, /* Dma channel */
444 (s8
*) info
->rxbd
, /*Source Address */
445 0, /* Source increment */
446 (s8
*) (&fecp
->rfdr
), /* dest */
447 4, /* dest increment */
450 info
->rxInit
, /* initiator */
451 info
->rxPri
, /* priority */
452 (MCD_FECRX_DMA
| MCD_TT_FLAGS_DEF
), /* Flags */
453 (MCD_NO_CSUM
| MCD_NO_BYTE_SWAP
) /* Function description */
456 /* Enable DMA tx task with no ready buffer descriptors */
457 MCD_startDma(info
->txTask
, /* Dma channel */
458 (s8
*) info
->txbd
, /*Source Address */
459 0, /* Source increment */
460 (s8
*) (&fecp
->tfdr
), /* dest */
464 info
->txInit
, /* initiator */
465 info
->txPri
, /* priority */
466 (MCD_FECTX_DMA
| MCD_TT_FLAGS_DEF
), /* Flags */
467 (MCD_NO_CSUM
| MCD_NO_BYTE_SWAP
) /* Function description */
470 /* Now enable the transmit and receive processing */
471 fecp
->ecr
|= FEC_ECR_ETHER_EN
;
476 static void fec_halt(struct eth_device
*dev
)
478 struct fec_info_dma
*info
= dev
->priv
;
479 volatile fecdma_t
*fecp
= (fecdma_t
*) (info
->iobase
);
480 int counter
= 0xffff;
482 /* issue graceful stop command to the FEC transmitter if necessary */
483 fecp
->tcr
|= FEC_TCR_GTS
;
485 /* wait for graceful stop to register */
486 while ((counter
--) && (!(fecp
->eir
& FEC_EIR_GRA
))) ;
488 /* Disable DMA tasks */
489 MCD_killDma(info
->txTask
);
490 MCD_killDma(info
->rxTask
);;
492 /* Disable the Ethernet Controller */
493 fecp
->ecr
&= ~FEC_ECR_ETHER_EN
;
495 /* Clear FIFO status registers */
496 fecp
->rfsr
&= FIFO_ERRSTAT
;
497 fecp
->tfsr
&= FIFO_ERRSTAT
;
499 fecp
->frst
= 0x01000000;
501 /* Issue a reset command to the FEC chip */
502 fecp
->ecr
|= FEC_ECR_RESET
;
504 /* wait at least 20 clock cycles */
508 printf("Ethernet task stopped\n");
512 int mcdmafec_initialize(bd_t
* bis
)
514 struct eth_device
*dev
;
517 for (i
= 0; i
< sizeof(fec_info
) / sizeof(fec_info
[0]); i
++) {
520 (struct eth_device
*)memalign(CFG_CACHELINE_SIZE
,
525 memset(dev
, 0, sizeof(*dev
));
527 sprintf(dev
->name
, "FEC%d", fec_info
[i
].index
);
529 dev
->priv
= &fec_info
[i
];
530 dev
->init
= fec_init
;
531 dev
->halt
= fec_halt
;
532 dev
->send
= fec_send
;
533 dev
->recv
= fec_recv
;
535 /* setup Receive and Transmit buffer descriptor */
537 (cbd_t
*) memalign(CFG_CACHELINE_SIZE
,
538 (PKTBUFSRX
* sizeof(cbd_t
)));
540 (cbd_t
*) memalign(CFG_CACHELINE_SIZE
,
541 (CFG_TX_ETH_BUFFER
* sizeof(cbd_t
)));
543 (char *)memalign(CFG_CACHELINE_SIZE
, DBUF_LENGTH
);
546 printf("rxbd %x txbd %x\n",
547 (int)fec_info
[i
].rxbd
, (int)fec_info
[i
].txbd
);
550 fec_info
[i
].phy_name
= (char *)memalign(CFG_CACHELINE_SIZE
, 32);
554 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
555 miiphy_register(dev
->name
,
556 mcffec_miiphy_read
, mcffec_miiphy_write
);
560 fec_info
[i
- 1].next
= &fec_info
[i
];
562 fec_info
[i
- 1].next
= &fec_info
[0];
565 bis
->bi_ethspeed
= 10;
570 #endif /* CONFIG_CMD_NET && CONFIG_NET_MULTI */
571 #endif /* CONFIG_FSLDMAFEC */