2 * (C) Copyright 2001-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
32 * CPU and board-specific Ethernet initializations. Aliased function
33 * signals caller to move on
35 static int __def_eth_init(bd_t *bis)
39 int cpu_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init")));
40 int board_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init")));
43 extern int gt6426x_eth_initialize(bd_t *bis);
46 extern int au1x00_enet_initialize(bd_t*);
47 extern int dc21x4x_initialize(bd_t*);
48 extern int e1000_initialize(bd_t*);
49 extern int eepro100_initialize(bd_t*);
50 extern int eth_3com_initialize(bd_t*);
51 extern int fec_initialize(bd_t*);
52 extern int inca_switch_initialize(bd_t*);
53 extern int mpc5xxx_fec_initialize(bd_t*);
54 extern int mpc512x_fec_initialize(bd_t*);
55 extern int mpc8220_fec_initialize(bd_t*);
56 extern int mv6436x_eth_initialize(bd_t *);
57 extern int mv6446x_eth_initialize(bd_t *);
58 extern int natsemi_initialize(bd_t*);
59 extern int pcnet_initialize(bd_t*);
60 extern int plb2800_eth_initialize(bd_t*);
61 extern int ppc_4xx_eth_initialize(bd_t *);
62 extern int scc_initialize(bd_t*);
63 extern int npe_initialize(bd_t *);
64 extern int uec_initialize(int);
67 extern void (*push_packet)(volatile void *, int);
72 } eth_rcv_bufs[PKTBUFSRX];
74 static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
77 static struct eth_device *eth_devices, *eth_current;
79 struct eth_device *eth_get_dev(void)
84 struct eth_device *eth_get_dev_by_name(char *devname)
86 struct eth_device *dev, *target_dev;
94 if (strcmp(devname, dev->name) == 0) {
99 } while (dev != eth_devices);
104 int eth_get_dev_index (void)
106 struct eth_device *dev;
113 for (dev = eth_devices; dev; dev = dev->next) {
114 if (dev == eth_current)
126 int eth_register(struct eth_device* dev)
128 struct eth_device *d;
131 eth_current = eth_devices = dev;
132 #ifdef CONFIG_NET_MULTI
133 /* update current ethernet name */
135 char *act = getenv("ethact");
136 if (act == NULL || strcmp(act, eth_current->name) != 0)
137 setenv("ethact", eth_current->name);
141 for (d=eth_devices; d->next!=eth_devices; d=d->next);
145 dev->state = ETH_STATE_INIT;
146 dev->next = eth_devices;
151 int eth_initialize(bd_t *bis)
154 unsigned char env_enetaddr[6];
155 int i, eth_number = 0;
161 show_boot_progress (64);
162 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
165 /* Try board-specific initialization first. If it fails or isn't
166 * present, try the cpu-specific initialization */
167 if (board_eth_init(bis) < 0)
170 #if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
171 mv6436x_eth_initialize(bis);
173 #if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
174 mv6446x_eth_initialize(bis);
176 #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
177 ppc_4xx_eth_initialize(bis);
179 #ifdef CONFIG_INCA_IP_SWITCH
180 inca_switch_initialize(bis);
182 #ifdef CONFIG_PLB2800_ETHER
183 plb2800_eth_initialize(bis);
188 #if defined(CONFIG_MPC5xxx_FEC)
189 mpc5xxx_fec_initialize(bis);
191 #if defined(CONFIG_MPC512x_FEC)
192 mpc512x_fec_initialize (bis);
194 #if defined(CONFIG_MPC8220_FEC)
195 mpc8220_fec_initialize(bis);
197 #if defined(CONFIG_UEC_ETH1)
200 #if defined(CONFIG_UEC_ETH2)
203 #if defined(CONFIG_UEC_ETH3)
206 #if defined(CONFIG_UEC_ETH4)
210 #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
213 #if defined(CONFIG_AU1X00)
214 au1x00_enet_initialize(bis);
216 #if defined(CONFIG_IXP4XX_NPE)
220 e1000_initialize(bis);
222 #ifdef CONFIG_EEPRO100
223 eepro100_initialize(bis);
226 dc21x4x_initialize(bis);
229 eth_3com_initialize(bis);
232 pcnet_initialize(bis);
235 gt6426x_eth_initialize(bis);
237 #ifdef CONFIG_NATSEMI
238 natsemi_initialize(bis);
241 puts ("No ethernet found.\n");
242 show_boot_progress (-64);
244 struct eth_device *dev = eth_devices;
245 char *ethprime = getenv ("ethprime");
247 show_boot_progress (65);
252 printf("%s", dev->name);
254 if (ethprime && strcmp (dev->name, ethprime) == 0) {
259 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
260 tmp = getenv (enetvar);
262 for (i=0; i<6; i++) {
263 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
265 tmp = (*end) ? end+1 : end;
268 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
269 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
270 memcmp(dev->enetaddr, env_enetaddr, 6))
272 printf ("\nWarning: %s MAC addresses don't match:\n",
274 printf ("Address in SROM is "
275 "%02X:%02X:%02X:%02X:%02X:%02X\n",
276 dev->enetaddr[0], dev->enetaddr[1],
277 dev->enetaddr[2], dev->enetaddr[3],
278 dev->enetaddr[4], dev->enetaddr[5]);
279 printf ("Address in environment is "
280 "%02X:%02X:%02X:%02X:%02X:%02X\n",
281 env_enetaddr[0], env_enetaddr[1],
282 env_enetaddr[2], env_enetaddr[3],
283 env_enetaddr[4], env_enetaddr[5]);
286 memcpy(dev->enetaddr, env_enetaddr, 6);
291 } while(dev != eth_devices);
293 #ifdef CONFIG_NET_MULTI
294 /* update current ethernet name */
296 char *act = getenv("ethact");
297 if (act == NULL || strcmp(act, eth_current->name) != 0)
298 setenv("ethact", eth_current->name);
300 setenv("ethact", NULL);
309 void eth_set_enetaddr(int num, char *addr) {
310 struct eth_device *dev;
311 unsigned char enetaddr[6];
315 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
320 for (i=0; i<6; i++) {
321 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
323 addr = (*end) ? end+1 : end;
330 if (dev == eth_devices)
334 debug ( "Setting new HW address on %s\n"
335 "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
337 enetaddr[0], enetaddr[1],
338 enetaddr[2], enetaddr[3],
339 enetaddr[4], enetaddr[5]);
341 memcpy(dev->enetaddr, enetaddr, 6);
343 #ifdef CONFIG_MCAST_TFTP
345 * mcast_addr: multicast ipaddr from which multicast Mac is made
346 * join: 1=join, 0=leave.
348 int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
351 if (!eth_current || !eth_current->mcast)
353 mcast_mac[5] = htonl(mcast_ip) & 0xff;
354 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
355 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
359 return eth_current->mcast(eth_current, mcast_mac, join);
362 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
363 * and this is the ethernet-crc method needed for TSEC -- and perhaps
364 * some other adapter -- hash tables
366 #define CRCPOLY_LE 0xedb88320
367 u32 ether_crc (size_t len, unsigned char const *p)
374 for (i = 0; i < 8; i++)
375 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
377 /* an reverse the bits, cuz of way they arrive -- last-first */
378 crc = (crc >> 16) | (crc << 16);
379 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
380 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
381 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
382 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
389 int eth_init(bd_t *bis)
391 struct eth_device* old_current;
394 puts ("No ethernet found.\n");
398 old_current = eth_current;
400 debug ("Trying %s\n", eth_current->name);
402 if (eth_current->init(eth_current,bis) >= 0) {
403 eth_current->state = ETH_STATE_ACTIVE;
410 } while (old_current != eth_current);
420 eth_current->halt(eth_current);
422 eth_current->state = ETH_STATE_PASSIVE;
425 int eth_send(volatile void *packet, int length)
430 return eth_current->send(eth_current, packet, length);
438 return eth_current->recv(eth_current);
442 static void eth_save_packet(volatile void *packet, int length)
444 volatile char *p = packet;
447 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
450 if (PKTSIZE < length)
453 for (i = 0; i < length; i++)
454 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
456 eth_rcv_bufs[eth_rcv_last].length = length;
457 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
460 int eth_receive(volatile void *packet, int length)
462 volatile char *p = packet;
463 void *pp = push_packet;
466 if (eth_rcv_current == eth_rcv_last) {
467 push_packet = eth_save_packet;
471 if (eth_rcv_current == eth_rcv_last)
475 if (length < eth_rcv_bufs[eth_rcv_current].length)
478 length = eth_rcv_bufs[eth_rcv_current].length;
480 for (i = 0; i < length; i++)
481 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
483 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
486 #endif /* CONFIG_API */
488 void eth_try_another(int first_restart)
490 static struct eth_device *first_failed = NULL;
494 * Do not rotate between network interfaces when
495 * 'ethrotate' variable is set to 'no'.
497 if (((ethrotate = getenv ("ethrotate")) != NULL) &&
498 (strcmp(ethrotate, "no") == 0))
505 first_failed = eth_current;
508 eth_current = eth_current->next;
510 #ifdef CONFIG_NET_MULTI
511 /* update current ethernet name */
513 char *act = getenv("ethact");
514 if (act == NULL || strcmp(act, eth_current->name) != 0)
515 setenv("ethact", eth_current->name);
519 if (first_failed == eth_current) {
524 #ifdef CONFIG_NET_MULTI
525 void eth_set_current(void)
528 struct eth_device* old_current;
530 if (!eth_current) /* XXX no current */
533 act = getenv("ethact");
535 old_current = eth_current;
537 if (strcmp(eth_current->name, act) == 0)
539 eth_current = eth_current->next;
540 } while (old_current != eth_current);
543 setenv("ethact", eth_current->name);
547 char *eth_get_name (void)
549 return (eth_current ? eth_current->name : "unknown");
551 #elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
553 extern int at91rm9200_miiphy_initialize(bd_t *bis);
554 extern int emac4xx_miiphy_initialize(bd_t *bis);
555 extern int mcf52x2_miiphy_initialize(bd_t *bis);
556 extern int ns7520_miiphy_initialize(bd_t *bis);
557 extern int davinci_eth_miiphy_initialize(bd_t *bis);
560 int eth_initialize(bd_t *bis)
562 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
566 #if defined(CONFIG_AT91RM9200)
567 at91rm9200_miiphy_initialize(bis);
569 #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) \
570 && !defined(CONFIG_AP1000) && !defined(CONFIG_405)
571 emac4xx_miiphy_initialize(bis);
573 #if defined(CONFIG_MCF52x2)
574 mcf52x2_miiphy_initialize(bis);
576 #if defined(CONFIG_DRIVER_NS7520_ETHERNET)
577 ns7520_miiphy_initialize(bis);
579 #if defined(CONFIG_DRIVER_TI_EMAC)
580 davinci_eth_miiphy_initialize(bis);