2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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,
31 #include <asm/m5329.h>
32 #include <asm/immap_5329.h>
43 /* Ethernet Transmit and Receive Buffers */
44 #define DBUF_LENGTH 1520
49 NOTE: PKT_MAXBUF_SIZE must be larger or equal to PKT_MAXBLR_SIZE,
50 see M54455 User Manual for MAX_FL of Receive Control Register for more
51 description. If PKT_MAXBUF_SIZE set to 1518, the FEC bandwidth will
52 reduce to about 20~40% of normal bandwidth. Changing PKT_MAXBLR_SIZE
53 will not make any improvement on speed
55 #define PKT_MAXBUF_SIZE 1518
56 #define PKT_MINBUF_SIZE 64
57 #define PKT_MAXBLR_SIZE 1520
58 #define LAST_PKTBUFSRX PKTBUFSRX - 1
59 #define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
60 #define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
62 DECLARE_GLOBAL_DATA_PTR;
64 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
66 struct fec_info_s fec_info[] = {
67 #ifdef CFG_FEC0_IOBASE
70 CFG_FEC0_IOBASE, /* io base */
71 CFG_FEC0_PINMUX, /* gpio pin muxing */
72 CFG_FEC0_MIIBASE, /* mii base */
74 0, /* duplex and speed */
82 0, /* initialized flag */
85 #ifdef CFG_FEC1_IOBASE
88 CFG_FEC1_IOBASE, /* io base */
89 CFG_FEC1_PINMUX, /* gpio pin muxing */
90 CFG_FEC1_MIIBASE, /* mii base */
92 0, /* duplex and speed */
94 0, /* phy name init */
100 0, /* initialized flag */
106 * FEC Ethernet Tx and Rx buffer descriptors allocated at the
107 * immr->udata_bd address on Dual-Port RAM
108 * Provide for Double Buffering
111 int fec_send(struct eth_device *dev, volatile void *packet, int length);
112 int fec_recv(struct eth_device *dev);
113 int fec_init(struct eth_device *dev, bd_t * bd);
114 void fec_halt(struct eth_device *dev);
115 void fec_reset(struct eth_device *dev);
117 extern int fecpin_setclear(struct eth_device *dev, int setclear);
119 #ifdef CFG_DISCOVER_PHY
120 extern void mii_init(void);
121 extern uint mii_send(uint mii_cmd);
122 extern int mii_discover_phy(struct eth_device *dev);
123 extern int mcffec_miiphy_read(char *devname, unsigned char addr,
124 unsigned char reg, unsigned short *value);
125 extern int mcffec_miiphy_write(char *devname, unsigned char addr,
126 unsigned char reg, unsigned short value);
129 void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
131 if ((dup_spd >> 16) == FULL) {
132 /* Set maximum frame length */
133 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
134 FEC_RCR_PROM | 0x100;
135 fecp->tcr = FEC_TCR_FDEN;
137 /* Half duplex mode */
138 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
139 FEC_RCR_MII_MODE | FEC_RCR_DRT;
140 fecp->tcr &= ~FEC_TCR_FDEN;
143 if ((dup_spd & 0xFFFF) == _100BASET) {
147 bd->bi_ethspeed = 100;
152 bd->bi_ethspeed = 10;
156 int fec_send(struct eth_device *dev, volatile void *packet, int length)
158 struct fec_info_s *info = dev->priv;
159 volatile fec_t *fecp = (fec_t *) (info->iobase);
163 miiphy_read(dev->name, info->phy_addr, PHY_BMSR, &phyStatus);
169 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
170 (j < MCFFEC_TOUT_LOOP)) {
174 if (j >= MCFFEC_TOUT_LOOP) {
175 printf("TX not ready\n");
178 info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
179 info->txbd[info->txIdx].cbd_datlen = length;
180 info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
182 /* Activate transmit Buffer Descriptor polling */
183 fecp->tdar = 0x01000000; /* Descriptor polling active */
186 while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
187 (j < MCFFEC_TOUT_LOOP)) {
191 if (j >= MCFFEC_TOUT_LOOP) {
192 printf("TX timeout\n");
195 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
196 __FILE__, __LINE__, __FUNCTION__, j,
197 info->txbd[info->txIdx].cbd_sc,
198 (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
201 /* return only status bits */ ;
202 rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
203 info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
208 int fec_recv(struct eth_device *dev)
210 struct fec_info_s *info = dev->priv;
211 volatile fec_t *fecp = (fec_t *) (info->iobase);
215 /* section 16.9.23.2 */
216 if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
218 break; /* nothing received - leave for() loop */
221 length = info->rxbd[info->rxIdx].cbd_datlen;
223 if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
224 printf("%s[%d] err: %x\n",
225 __FUNCTION__, __LINE__,
226 info->rxbd[info->rxIdx].cbd_sc);
228 printf("%s[%d] err: %x\n",
229 __FUNCTION__, __LINE__,
230 info->rxbd[info->rxIdx].cbd_sc);
235 /* Pass the packet up to the protocol layers. */
236 NetReceive(NetRxPackets[info->rxIdx], length);
238 fecp->eir |= FEC_EIR_RXF;
241 /* Give the buffer back to the FEC. */
242 info->rxbd[info->rxIdx].cbd_datlen = 0;
244 /* wrap around buffer index when necessary */
245 if (info->rxIdx == LAST_PKTBUFSRX) {
246 info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
249 info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
253 /* Try to fill Buffer Descriptors */
254 fecp->rdar = 0x01000000; /* Descriptor polling active */
260 /**************************************************************
262 * FEC Ethernet Initialization Routine
264 *************************************************************/
267 void dbgFecRegs(struct eth_device *dev)
269 struct fec_info_s *info = dev->priv;
270 volatile fec_t *fecp = (fec_t *) (info->iobase);
273 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
274 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
275 printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
276 printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
277 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
278 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
279 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
280 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
281 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
282 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
283 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
284 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
285 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
286 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
287 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
288 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
289 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
290 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
291 printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
292 printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
293 printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
294 printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
295 printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
298 printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
300 printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
301 fecp->rmon_t_packets);
302 printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
303 fecp->rmon_t_bc_pkt);
304 printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
305 fecp->rmon_t_mc_pkt);
306 printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
307 fecp->rmon_t_crc_align);
308 printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
309 fecp->rmon_t_undersize);
310 printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
311 fecp->rmon_t_oversize);
312 printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
314 printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
316 printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
318 printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
320 printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
321 fecp->rmon_t_p65to127);
322 printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
323 fecp->rmon_t_p128to255);
324 printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
325 fecp->rmon_t_p256to511);
326 printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
327 fecp->rmon_t_p512to1023);
328 printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
329 fecp->rmon_t_p1024to2047);
330 printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
331 fecp->rmon_t_p_gte2048);
332 printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
333 fecp->rmon_t_octets);
336 printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
338 printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
339 fecp->ieee_t_frame_ok);
340 printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
342 printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
344 printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
346 printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
348 printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
350 printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
351 fecp->ieee_t_macerr);
352 printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
354 printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
356 printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
358 printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
359 fecp->ieee_t_octets_ok);
362 printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
364 printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
365 fecp->rmon_r_packets);
366 printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
367 fecp->rmon_r_bc_pkt);
368 printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
369 fecp->rmon_r_mc_pkt);
370 printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
371 fecp->rmon_r_crc_align);
372 printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
373 fecp->rmon_r_undersize);
374 printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
375 fecp->rmon_r_oversize);
376 printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
378 printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
380 printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
382 printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
383 fecp->rmon_r_p65to127);
384 printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
385 fecp->rmon_r_p128to255);
386 printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
387 fecp->rmon_r_p256to511);
388 printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
389 fecp->rmon_r_p512to1023);
390 printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
391 fecp->rmon_r_p1024to2047);
392 printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
393 fecp->rmon_r_p_gte2048);
394 printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
395 fecp->rmon_r_octets);
398 printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
400 printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
401 fecp->ieee_r_frame_ok);
402 printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
404 printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
406 printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
407 fecp->ieee_r_macerr);
408 printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
410 printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
411 fecp->ieee_r_octets_ok);
417 int fec_init(struct eth_device *dev, bd_t * bd)
419 struct fec_info_s *info = dev->priv;
420 volatile fec_t *fecp = (fec_t *) (info->iobase);
424 fecpin_setclear(dev, 1);
428 #if (CONFIG_COMMANDS & CFG_CMD_MII) || defined (CONFIG_MII) || \
429 defined (CFG_DISCOVER_PHY)
433 setFecDuplexSpeed(fecp, bd, info->dup_spd);
435 #ifndef CFG_DISCOVER_PHY
436 setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
437 #endif /* ifndef CFG_DISCOVER_PHY */
438 #endif /* CFG_CMD_MII || CONFIG_MII */
440 /* We use strictly polling mode only */
443 /* Clear any pending interrupt */
444 fecp->eir = 0xffffffff;
446 /* Set station address */
447 if ((u32) fecp == CFG_FEC0_IOBASE) {
448 ea = &bd->bi_enetaddr[0];
450 #ifdef CFG_FEC1_IOBASE
451 ea = &bd->bi_enet1addr[0];
455 fecp->palr = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
456 fecp->paur = (ea[4] << 24) | (ea[5] << 16);
458 printf("Eth Addrs: %02x:%02x:%02x:%02x:%02x:%02x\n",
459 ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
462 /* Clear unicast address hash table */
466 /* Clear multicast address hash table */
470 /* Set maximum receive buffer size. */
471 fecp->emrbr = PKT_MAXBLR_SIZE;
474 * Setup Buffers and Buffer Desriptors
480 * Setup Receiver Buffer Descriptors (13.14.24.18)
484 for (i = 0; i < PKTBUFSRX; i++) {
485 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
486 info->rxbd[i].cbd_datlen = 0; /* Reset */
487 info->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
489 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
492 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
496 for (i = 0; i < TX_BUF_CNT; i++) {
497 info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
498 info->txbd[i].cbd_datlen = 0; /* Reset */
499 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
501 info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
503 /* Set receive and transmit descriptor base */
504 fecp->erdsr = (unsigned int)(&info->rxbd[0]);
505 fecp->etdsr = (unsigned int)(&info->txbd[0]);
507 /* Now enable the transmit and receive processing */
508 fecp->ecr |= FEC_ECR_ETHER_EN;
510 /* And last, try to fill Rx Buffer Descriptors */
511 fecp->rdar = 0x01000000; /* Descriptor polling active */
516 void fec_reset(struct eth_device *dev)
518 struct fec_info_s *info = dev->priv;
519 volatile fec_t *fecp = (fec_t *) (info->iobase);
522 fecp->ecr = FEC_ECR_RESET;
523 for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
526 if (i == FEC_RESET_DELAY) {
527 printf("FEC_RESET_DELAY timeout\n");
531 void fec_halt(struct eth_device *dev)
533 struct fec_info_s *info = dev->priv;
537 fecpin_setclear(dev, 0);
539 info->rxIdx = info->txIdx = 0;
540 memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
541 memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
542 memset(info->txbuf, 0, DBUF_LENGTH);
545 int mcffec_initialize(bd_t * bis)
547 struct eth_device *dev;
550 for (i = 0; i < sizeof(fec_info) / sizeof(fec_info[0]); i++) {
552 dev = (struct eth_device *)malloc(sizeof *dev);
556 memset(dev, 0, sizeof(*dev));
558 sprintf(dev->name, "FEC%d", fec_info[i].index);
560 dev->priv = &fec_info[i];
561 dev->init = fec_init;
562 dev->halt = fec_halt;
563 dev->send = fec_send;
564 dev->recv = fec_recv;
566 /* setup Receive and Transmit buffer descriptor */
568 (cbd_t *) memalign(32, (PKTBUFSRX * sizeof(cbd_t) + 31));
570 (cbd_t *) memalign(32, (TX_BUF_CNT * sizeof(cbd_t) + 31));
571 fec_info[i].txbuf = (char *)memalign(32, DBUF_LENGTH + 31);
573 printf("rxbd %x txbd %x\n",
574 (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
577 fec_info[i].phy_name = (char *)malloc(32);
581 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
582 miiphy_register(dev->name,
583 mcffec_miiphy_read, mcffec_miiphy_write);
588 bis->bi_ethspeed = 10;
593 #endif /* CFG_CMD_NET, FEC_ENET & NET_MULTI */
594 #endif /* CONFIG_MCFFEC */