blob: 6db621472206e0dcb5af25c5c73c523337176f14 [file] [log] [blame]
wdenkbf9e3b32004-02-12 00:47:09 +00001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <malloc.h>
26#include <asm/fec.h>
27
Zachary P. Landaueacbd312006-01-26 17:35:56 -050028#ifdef CONFIG_M5271
29#include <asm/m5271.h>
30#include <asm/immap_5271.h>
31#endif
32
wdenkbf9e3b32004-02-12 00:47:09 +000033#ifdef CONFIG_M5272
34#include <asm/m5272.h>
35#include <asm/immap_5272.h>
36#endif
37
38#ifdef CONFIG_M5282
39#include <asm/m5282.h>
40#include <asm/immap_5282.h>
41#endif
42
43#include <net.h>
44#include <command.h>
45
46#ifdef CONFIG_M5272
47#define FEC_ADDR (CFG_MBAR + 0x840)
48#endif
Zachary P. Landaueacbd312006-01-26 17:35:56 -050049#if defined(CONFIG_M5282) || defined(CONFIG_M5271)
wdenkbf9e3b32004-02-12 00:47:09 +000050#define FEC_ADDR (CFG_MBAR + 0x1000)
51#endif
52
53#undef ET_DEBUG
54#undef MII_DEBUG
55
56#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET)
57
58#ifdef CFG_DISCOVER_PHY
59#include <miiphy.h>
60static void mii_discover_phy (void);
61#endif
62
63/* Ethernet Transmit and Receive Buffers */
64#define DBUF_LENGTH 1520
65
66#define TX_BUF_CNT 2
67
68#define TOUT_LOOP 100
69
70#define PKT_MAXBUF_SIZE 1518
71#define PKT_MINBUF_SIZE 64
72#define PKT_MAXBLR_SIZE 1520
73
74
75static char txbuf[DBUF_LENGTH];
76
77static uint rxIdx; /* index of the current RX buffer */
78static uint txIdx; /* index of the current TX buffer */
79
80/*
81 * FEC Ethernet Tx and Rx buffer descriptors allocated at the
82 * immr->udata_bd address on Dual-Port RAM
83 * Provide for Double Buffering
84 */
85
86typedef volatile struct CommonBufferDescriptor {
87 cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
88 cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
89} RTXBD;
90
91static RTXBD *rtx = NULL;
92
93int eth_send (volatile void *packet, int length)
94{
95 int j, rc;
96 volatile fec_t *fecp = (fec_t *) (FEC_ADDR);
97
98 /* section 16.9.23.3
99 * Wait for ready
100 */
101 j = 0;
102 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY)
103 && (j < TOUT_LOOP)) {
104 udelay (1);
105 j++;
106 }
107 if (j >= TOUT_LOOP) {
108 printf ("TX not ready\n");
109 }
110
111 rtx->txbd[txIdx].cbd_bufaddr = (uint) packet;
112 rtx->txbd[txIdx].cbd_datlen = length;
113 rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
114
115 /* Activate transmit Buffer Descriptor polling */
116 fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
117
118 j = 0;
119 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY)
120 && (j < TOUT_LOOP)) {
121 udelay (1);
122 j++;
123 }
124 if (j >= TOUT_LOOP) {
125 printf ("TX timeout\n");
126 }
127#ifdef ET_DEBUG
128 printf ("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
129 __FILE__, __LINE__, __FUNCTION__, j, rtx->txbd[txIdx].cbd_sc,
130 (rtx->txbd[txIdx].cbd_sc & 0x003C) >> 2);
131#endif
132
133 /* return only status bits */ ;
134 rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
135
136 txIdx = (txIdx + 1) % TX_BUF_CNT;
137
138 return rc;
139}
140
141int eth_rx (void)
142{
143 int length;
144 volatile fec_t *fecp = (fec_t *) FEC_ADDR;
145
146 for (;;) {
147 /* section 16.9.23.2 */
148 if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
149 length = -1;
150 break; /* nothing received - leave for() loop */
151 }
152
153 length = rtx->rxbd[rxIdx].cbd_datlen;
154
155 if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
156#ifdef ET_DEBUG
157 printf ("%s[%d] err: %x\n",
158 __FUNCTION__, __LINE__,
159 rtx->rxbd[rxIdx].cbd_sc);
160#endif
161 } else {
162 /* Pass the packet up to the protocol layers. */
163 NetReceive (NetRxPackets[rxIdx], length - 4);
164 }
165
166 /* Give the buffer back to the FEC. */
167 rtx->rxbd[rxIdx].cbd_datlen = 0;
168
169 /* wrap around buffer index when necessary */
170 if ((rxIdx + 1) >= PKTBUFSRX) {
171 rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
172 (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
173 rxIdx = 0;
174 } else {
175 rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
176 rxIdx++;
177 }
178
179 /* Try to fill Buffer Descriptors */
180 fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
181 }
182
183 return length;
184}
185
186/**************************************************************
187 *
188 * FEC Ethernet Initialization Routine
189 *
190 *************************************************************/
191#define FEC_ECNTRL_ETHER_EN 0x00000002
192#define FEC_ECNTRL_RESET 0x00000001
193
194#define FEC_RCNTRL_BC_REJ 0x00000010
195#define FEC_RCNTRL_PROM 0x00000008
196#define FEC_RCNTRL_MII_MODE 0x00000004
197#define FEC_RCNTRL_DRT 0x00000002
198#define FEC_RCNTRL_LOOP 0x00000001
199
200#define FEC_TCNTRL_FDEN 0x00000004
201#define FEC_TCNTRL_HBC 0x00000002
202#define FEC_TCNTRL_GTS 0x00000001
203
204#define FEC_RESET_DELAY 50000
205
206int eth_init (bd_t * bd)
207{
Heiko Schocher9acb6262006-04-20 08:42:42 +0200208#ifndef CFG_ENET_BD_BASE
209 DECLARE_GLOBAL_DATA_PTR;
210#endif
wdenkbf9e3b32004-02-12 00:47:09 +0000211 int i;
212 volatile fec_t *fecp = (fec_t *) (FEC_ADDR);
213
214 /* Whack a reset.
215 * A delay is required between a reset of the FEC block and
216 * initialization of other FEC registers because the reset takes
217 * some time to complete. If you don't delay, subsequent writes
218 * to FEC registers might get killed by the reset routine which is
219 * still in progress.
220 */
221 fecp->fec_ecntrl = FEC_ECNTRL_RESET;
222 for (i = 0;
223 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
224 ++i) {
225 udelay (1);
226 }
227 if (i == FEC_RESET_DELAY) {
228 printf ("FEC_RESET_DELAY timeout\n");
229 return 0;
230 }
231
232 /* We use strictly polling mode only
233 */
234 fecp->fec_imask = 0;
235
236 /* Clear any pending interrupt */
237 fecp->fec_ievent = 0xffffffff;
238
239 /* Set station address */
240#define ea bd->bi_enetaddr
241 fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) |
242 (ea[2] << 8) | (ea[3]);
243 fecp->fec_addr_high = (ea[4] << 24) | (ea[5] << 16);
244#ifdef ET_DEBUG
245 printf ("Eth Addrs: %02x:%02x:%02x:%02x:%02x:%02x\n",
246 ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
247#endif
248#undef ea
249
Zachary P. Landaueacbd312006-01-26 17:35:56 -0500250#ifdef CONFIG_M5271
251 /* Clear multicast address hash table
252 */
253 fecp->fec_ghash_table_high = 0;
254 fecp->fec_ghash_table_low = 0;
255
256 /* Clear individual address hash table
257 */
258 fecp->fec_ihash_table_high = 0;
259 fecp->fec_ihash_table_low = 0;
260#else
wdenkbf9e3b32004-02-12 00:47:09 +0000261 /* Clear multicast address hash table
262 */
Heiko Schocher9acb6262006-04-20 08:42:42 +0200263#ifdef CONFIG_M5282
264 fecp->fec_ihash_table_high = 0;
265 fecp->fec_ihash_table_low = 0;
266#else
wdenkbf9e3b32004-02-12 00:47:09 +0000267 fecp->fec_hash_table_high = 0;
268 fecp->fec_hash_table_low = 0;
Zachary P. Landaueacbd312006-01-26 17:35:56 -0500269#endif
wdenkbf9e3b32004-02-12 00:47:09 +0000270
271 /* Set maximum receive buffer size.
272 */
273 fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
274
275 /*
276 * Setup Buffers and Buffer Desriptors
277 */
278 rxIdx = 0;
279 txIdx = 0;
280
281 if (!rtx) {
Heiko Schocher9acb6262006-04-20 08:42:42 +0200282#ifdef CFG_ENET_BD_BASE
wdenkbf9e3b32004-02-12 00:47:09 +0000283 rtx = (RTXBD *) CFG_ENET_BD_BASE;
Heiko Schocher9acb6262006-04-20 08:42:42 +0200284#else
285 rtx = (RTXBD *) (CFG_MONITOR_BASE+gd->reloc_off -
Wolfgang Denkb1d71352006-06-10 22:00:40 +0200286 (((PKTBUFSRX+TX_BUF_CNT)*+sizeof(cbd_t)
Heiko Schocher9acb6262006-04-20 08:42:42 +0200287 +0xFF)
288 & ~0xFF)
289 );
290 debug("set ENET_DB_BASE to %lX\n",(long) rtx);
291#endif
wdenkbf9e3b32004-02-12 00:47:09 +0000292 }
293
294 /*
295 * Setup Receiver Buffer Descriptors (13.14.24.18)
296 * Settings:
297 * Empty, Wrap
298 */
299 for (i = 0; i < PKTBUFSRX; i++) {
300 rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
301 rtx->rxbd[i].cbd_datlen = 0; /* Reset */
302 rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
303 }
304 rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
305
306 /*
307 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
308 * Settings:
309 * Last, Tx CRC
310 */
311 for (i = 0; i < TX_BUF_CNT; i++) {
312 rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
313 rtx->txbd[i].cbd_datlen = 0; /* Reset */
314 rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
315 }
316 rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
317
318 /* Set receive and transmit descriptor base
319 */
320 fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
321 fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
322
323 /* Enable MII mode
324 */
Wolfgang Denk4176c792006-06-10 19:27:47 +0200325
326#if 0 /* Full duplex mode */
wdenkbf9e3b32004-02-12 00:47:09 +0000327 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
328 fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
Wolfgang Denk4176c792006-06-10 19:27:47 +0200329#else /* Half duplex mode */
Wolfgang Denkb1d71352006-06-10 22:00:40 +0200330 fecp->fec_r_cntrl = (PKT_MAXBUF_SIZE << 16); /* set max frame length */
Heiko Schocher9acb6262006-04-20 08:42:42 +0200331 fecp->fec_r_cntrl |= FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
wdenkbf9e3b32004-02-12 00:47:09 +0000332 fecp->fec_x_cntrl = 0;
333#endif
334 /* Set MII speed */
Wolfgang Denkb1d71352006-06-10 22:00:40 +0200335 fecp->fec_mii_speed = (((CFG_CLK / 2) / (2500000 / 10)) + 5) / 10;
336 fecp->fec_mii_speed *= 2;
wdenkbf9e3b32004-02-12 00:47:09 +0000337
338 /* Configure port B for MII.
339 */
340 /* port initialization was already made in cpu_init_f() */
341
342 /* Now enable the transmit and receive processing
343 */
344 fecp->fec_ecntrl = FEC_ECNTRL_ETHER_EN;
345
346#ifdef CFG_DISCOVER_PHY
347 /* wait for the PHY to wake up after reset */
348 mii_discover_phy ();
349#endif
350
351 /* And last, try to fill Rx Buffer Descriptors */
352 fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
353
354 return 1;
355}
356
357void eth_halt (void)
358{
359 volatile fec_t *fecp = (fec_t *) FEC_ADDR;
360
361 fecp->fec_ecntrl = 0;
362}
363
364
365#if defined(CFG_DISCOVER_PHY) || (CONFIG_COMMANDS & CFG_CMD_MII)
366
367static int phyaddr = -1; /* didn't find a PHY yet */
368static uint phytype;
369
370/* Make MII read/write commands for the FEC.
371*/
372
373#define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
374 (REG & 0x1f) << 18))
375
376#define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
377 (REG & 0x1f) << 18) | \
378 (VAL & 0xffff))
379
380/* Interrupt events/masks.
381*/
382#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
383#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
384#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
385#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
386#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
387#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
388#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
389#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
390#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
391#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
392
393/* PHY identification
394 */
395#define PHY_ID_LXT970 0x78100000 /* LXT970 */
396#define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
397#define PHY_ID_82555 0x02a80150 /* Intel 82555 */
398#define PHY_ID_QS6612 0x01814400 /* QS6612 */
399#define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
400#define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
401#define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
402
403/* send command to phy using mii, wait for result */
404static uint mii_send (uint mii_cmd)
405{
406 uint mii_reply;
407 volatile fec_t *ep = (fec_t *) (FEC_ADDR);
408
409 ep->fec_mii_data = mii_cmd; /* command to phy */
410
411 /* wait for mii complete */
412 while (!(ep->fec_ievent & FEC_ENET_MII)); /* spin until done */
413 mii_reply = ep->fec_mii_data; /* result from phy */
414 ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
415#ifdef ET_DEBUG
416 printf ("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
417 __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
418#endif
419 return (mii_reply & 0xffff); /* data read from phy */
420}
421#endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */
422
423#if defined(CFG_DISCOVER_PHY)
424static void mii_discover_phy (void)
425{
426#define MAX_PHY_PASSES 11
427 uint phyno;
428 int pass;
429
430 phyaddr = -1; /* didn't find a PHY yet */
431 for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
432 if (pass > 1) {
433 /* PHY may need more time to recover from reset.
434 * The LXT970 needs 50ms typical, no maximum is
435 * specified, so wait 10ms before try again.
436 * With 11 passes this gives it 100ms to wake up.
437 */
438 udelay (10000); /* wait 10ms */
439 }
Heiko Schocher9acb6262006-04-20 08:42:42 +0200440 for (phyno = 1; phyno < 32 && phyaddr < 0; ++phyno) {
wdenkbf9e3b32004-02-12 00:47:09 +0000441 phytype = mii_send (mk_mii_read (phyno, PHY_PHYIDR1));
442#ifdef ET_DEBUG
443 printf ("PHY type 0x%x pass %d type ", phytype, pass);
444#endif
445 if (phytype != 0xffff) {
446 phyaddr = phyno;
447 phytype <<= 16;
448 phytype |= mii_send (mk_mii_read (phyno,
449 PHY_PHYIDR2));
450
451#ifdef ET_DEBUG
452 printf ("PHY @ 0x%x pass %d type ", phyno,
453 pass);
454 switch (phytype & 0xfffffff0) {
455 case PHY_ID_LXT970:
456 printf ("LXT970\n");
457 break;
458 case PHY_ID_LXT971:
459 printf ("LXT971\n");
460 break;
461 case PHY_ID_82555:
462 printf ("82555\n");
463 break;
464 case PHY_ID_QS6612:
465 printf ("QS6612\n");
466 break;
467 case PHY_ID_AMD79C784:
468 printf ("AMD79C784\n");
469 break;
470 case PHY_ID_LSI80225B:
471 printf ("LSI L80225/B\n");
472 break;
473 default:
474 printf ("0x%08x\n", phytype);
475 break;
476 }
477#endif
478 }
479 }
480 }
481 if (phyaddr < 0) {
482 printf ("No PHY device found.\n");
483 }
484}
485#endif /* CFG_DISCOVER_PHY */
486
487#if (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)
488
489static int mii_init_done = 0;
490
491/****************************************************************************
492 * mii_init -- Initialize the MII for MII command without ethernet
493 * This function is a subset of eth_init
494 ****************************************************************************
495 */
496void mii_init (void)
497{
498 volatile fec_t *fecp = (fec_t *) (FEC_ADDR);
499
500 int i;
501
502 if (mii_init_done != 0) {
503 return;
504 }
505
506 /* Whack a reset.
507 * A delay is required between a reset of the FEC block and
508 * initialization of other FEC registers because the reset takes
509 * some time to complete. If you don't delay, subsequent writes
510 * to FEC registers might get killed by the reset routine which is
511 * still in progress.
512 */
513
514 fecp->fec_ecntrl = FEC_ECNTRL_RESET;
515 for (i = 0;
516 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
517 ++i) {
518 udelay (1);
519 }
520 if (i == FEC_RESET_DELAY) {
521 printf ("FEC_RESET_DELAY timeout\n");
522 return;
523 }
524
525 /* We use strictly polling mode only
526 */
527 fecp->fec_imask = 0;
528
529 /* Clear any pending interrupt
530 */
531 fecp->fec_ievent = 0xffffffff;
532
533 /* Set MII speed */
534 fecp->fec_mii_speed = 0x0e;
535
536 /* Configure port B for MII.
537 */
538 /* port initialization was already made in cpu_init_f() */
539
540 /* Now enable the transmit and receive processing */
541 fecp->fec_ecntrl = FEC_ECNTRL_ETHER_EN;
542
543 mii_init_done = 1;
544}
545
546/*****************************************************************************
547 * Read and write a MII PHY register, routines used by MII Utilities
548 *
549 * FIXME: These routines are expected to return 0 on success, but mii_send
550 * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
551 * no PHY connected...
552 * For now always return 0.
553 * FIXME: These routines only work after calling eth_init() at least once!
554 * Otherwise they hang in mii_send() !!! Sorry!
555 *****************************************************************************/
556
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200557int mcf52x2_miiphy_read (char *devname, unsigned char addr,
558 unsigned char reg, unsigned short *value)
wdenkbf9e3b32004-02-12 00:47:09 +0000559{
560 short rdreg; /* register working value */
561
562#ifdef MII_DEBUG
563 printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
564#endif
565 rdreg = mii_send (mk_mii_read (addr, reg));
566
567 *value = rdreg;
568
569#ifdef MII_DEBUG
570 printf ("0x%04x\n", *value);
571#endif
572
573 return 0;
574}
575
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200576int mcf52x2_miiphy_write (char *devname, unsigned char addr,
577 unsigned char reg, unsigned short value)
wdenkbf9e3b32004-02-12 00:47:09 +0000578{
579 short rdreg; /* register working value */
580
581#ifdef MII_DEBUG
582 printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
583#endif
584
585 rdreg = mii_send (mk_mii_write (addr, reg, value));
586
587#ifdef MII_DEBUG
588 printf ("0x%04x\n", value);
589#endif
590
591 return 0;
592}
593#endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII) */
wdenkbf9e3b32004-02-12 00:47:09 +0000594#endif /* CFG_CMD_NET, FEC_ENET */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200595
596int mcf52x2_miiphy_initialize(bd_t *bis)
597{
598#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET)
599#if (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)
600 miiphy_register("mcf52x2phy", mcf52x2_miiphy_read, mcf52x2_miiphy_write);
601#endif
602#endif
603 return 0;
604}