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,
30 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
35 for (i = 0; i < 6; ++i) {
36 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
38 addr = (*end) ? end + 1 : end;
42 int eth_getenv_enetaddr(char *name, uchar *enetaddr)
44 eth_parse_enetaddr(getenv(name), enetaddr);
45 return is_valid_ether_addr(enetaddr);
48 int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
52 sprintf(buf, "%pM", enetaddr);
54 return setenv(name, buf);
58 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
60 static char *act = NULL;
61 static int env_changed_id = 0;
64 * CPU and board-specific Ethernet initializations. Aliased function
65 * signals caller to move on
67 static int __def_eth_init(bd_t *bis)
71 int cpu_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init")));
72 int board_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init")));
74 extern int mv6436x_eth_initialize(bd_t *);
75 extern int mv6446x_eth_initialize(bd_t *);
78 extern void (*push_packet)(volatile void *, int);
83 } eth_rcv_bufs[PKTBUFSRX];
85 static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
88 static struct eth_device *eth_devices, *eth_current;
90 struct eth_device *eth_get_dev(void)
95 struct eth_device *eth_get_dev_by_name(char *devname)
97 struct eth_device *dev, *target_dev;
105 if (strcmp(devname, dev->name) == 0) {
110 } while (dev != eth_devices);
115 struct eth_device *eth_get_dev_by_index(int index)
117 struct eth_device *dev, *target_dev;
132 } while (dev != eth_devices);
137 int eth_get_dev_index (void)
139 struct eth_device *dev;
146 for (dev = eth_devices; dev; dev = dev->next) {
147 if (dev == eth_current)
159 int eth_register(struct eth_device* dev)
161 struct eth_device *d;
164 eth_current = eth_devices = dev;
165 #ifdef CONFIG_NET_MULTI
166 /* update current ethernet name */
168 char *act = getenv("ethact");
169 if (act == NULL || strcmp(act, eth_current->name) != 0)
170 setenv("ethact", eth_current->name);
174 for (d=eth_devices; d->next!=eth_devices; d=d->next);
178 dev->state = ETH_STATE_INIT;
179 dev->next = eth_devices;
184 int eth_initialize(bd_t *bis)
187 unsigned char env_enetaddr[6];
193 show_boot_progress (64);
194 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
197 /* Try board-specific initialization first. If it fails or isn't
198 * present, try the cpu-specific initialization */
199 if (board_eth_init(bis) < 0)
202 #if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
203 mv6436x_eth_initialize(bis);
205 #if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
206 mv6446x_eth_initialize(bis);
209 puts ("No ethernet found.\n");
210 show_boot_progress (-64);
212 struct eth_device *dev = eth_devices;
213 char *ethprime = getenv ("ethprime");
215 show_boot_progress (65);
220 printf("%s", dev->name);
222 if (ethprime && strcmp (dev->name, ethprime) == 0) {
227 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
228 eth_getenv_enetaddr(enetvar, env_enetaddr);
230 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
231 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
232 memcmp(dev->enetaddr, env_enetaddr, 6))
234 printf ("\nWarning: %s MAC addresses don't match:\n",
236 printf ("Address in SROM is %pM\n",
238 printf ("Address in environment is %pM\n",
242 memcpy(dev->enetaddr, env_enetaddr, 6);
247 } while(dev != eth_devices);
249 #ifdef CONFIG_NET_MULTI
250 /* update current ethernet name */
252 char *act = getenv("ethact");
253 if (act == NULL || strcmp(act, eth_current->name) != 0)
254 setenv("ethact", eth_current->name);
256 setenv("ethact", NULL);
265 void eth_set_enetaddr(int num, char *addr) {
266 struct eth_device *dev;
267 unsigned char enetaddr[6];
269 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
274 eth_parse_enetaddr(addr, enetaddr);
280 if (dev == eth_devices)
284 debug ( "Setting new HW address on %s\n"
285 "New Address is %pM\n",
286 dev->name, enetaddr);
288 memcpy(dev->enetaddr, enetaddr, 6);
290 #ifdef CONFIG_MCAST_TFTP
292 * mcast_addr: multicast ipaddr from which multicast Mac is made
293 * join: 1=join, 0=leave.
295 int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
298 if (!eth_current || !eth_current->mcast)
300 mcast_mac[5] = htonl(mcast_ip) & 0xff;
301 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
302 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
306 return eth_current->mcast(eth_current, mcast_mac, join);
309 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
310 * and this is the ethernet-crc method needed for TSEC -- and perhaps
311 * some other adapter -- hash tables
313 #define CRCPOLY_LE 0xedb88320
314 u32 ether_crc (size_t len, unsigned char const *p)
321 for (i = 0; i < 8; i++)
322 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
324 /* an reverse the bits, cuz of way they arrive -- last-first */
325 crc = (crc >> 16) | (crc << 16);
326 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
327 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
328 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
329 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
336 int eth_init(bd_t *bis)
338 struct eth_device* old_current;
341 puts ("No ethernet found.\n");
345 old_current = eth_current;
347 debug ("Trying %s\n", eth_current->name);
349 if (eth_current->init(eth_current,bis) >= 0) {
350 eth_current->state = ETH_STATE_ACTIVE;
357 } while (old_current != eth_current);
367 eth_current->halt(eth_current);
369 eth_current->state = ETH_STATE_PASSIVE;
372 int eth_send(volatile void *packet, int length)
377 return eth_current->send(eth_current, packet, length);
385 return eth_current->recv(eth_current);
389 static void eth_save_packet(volatile void *packet, int length)
391 volatile char *p = packet;
394 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
397 if (PKTSIZE < length)
400 for (i = 0; i < length; i++)
401 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
403 eth_rcv_bufs[eth_rcv_last].length = length;
404 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
407 int eth_receive(volatile void *packet, int length)
409 volatile char *p = packet;
410 void *pp = push_packet;
413 if (eth_rcv_current == eth_rcv_last) {
414 push_packet = eth_save_packet;
418 if (eth_rcv_current == eth_rcv_last)
422 if (length < eth_rcv_bufs[eth_rcv_current].length)
425 length = eth_rcv_bufs[eth_rcv_current].length;
427 for (i = 0; i < length; i++)
428 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
430 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
433 #endif /* CONFIG_API */
435 void eth_try_another(int first_restart)
437 static struct eth_device *first_failed = NULL;
441 * Do not rotate between network interfaces when
442 * 'ethrotate' variable is set to 'no'.
444 if (((ethrotate = getenv ("ethrotate")) != NULL) &&
445 (strcmp(ethrotate, "no") == 0))
452 first_failed = eth_current;
455 eth_current = eth_current->next;
457 #ifdef CONFIG_NET_MULTI
458 /* update current ethernet name */
460 char *act = getenv("ethact");
461 if (act == NULL || strcmp(act, eth_current->name) != 0)
462 setenv("ethact", eth_current->name);
466 if (first_failed == eth_current) {
471 #ifdef CONFIG_NET_MULTI
472 void eth_set_current(void)
474 struct eth_device* old_current;
477 if (!eth_current) /* XXX no current */
480 env_id = get_env_id();
481 if ((act == NULL) || (env_changed_id != env_id)) {
482 act = getenv("ethact");
483 env_changed_id = env_id;
486 old_current = eth_current;
488 if (strcmp(eth_current->name, act) == 0)
490 eth_current = eth_current->next;
491 } while (old_current != eth_current);
494 setenv("ethact", eth_current->name);
498 char *eth_get_name (void)
500 return (eth_current ? eth_current->name : "unknown");
502 #elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
504 extern int at91rm9200_miiphy_initialize(bd_t *bis);
505 extern int emac4xx_miiphy_initialize(bd_t *bis);
506 extern int mcf52x2_miiphy_initialize(bd_t *bis);
507 extern int ns7520_miiphy_initialize(bd_t *bis);
508 extern int davinci_eth_miiphy_initialize(bd_t *bis);
511 int eth_initialize(bd_t *bis)
513 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
517 #if defined(CONFIG_AT91RM9200)
518 at91rm9200_miiphy_initialize(bis);
520 #if defined(CONFIG_PPC4xx_EMAC)
521 emac4xx_miiphy_initialize(bis);
523 #if defined(CONFIG_MCF52x2)
524 mcf52x2_miiphy_initialize(bis);
526 #if defined(CONFIG_DRIVER_NS7520_ETHERNET)
527 ns7520_miiphy_initialize(bis);
529 #if defined(CONFIG_DRIVER_TI_EMAC)
530 davinci_eth_miiphy_initialize(bis);