2 * (C) Copyright 2001-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/errno.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
22 for (i = 0; i < 6; ++i) {
23 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
25 addr = (*end) ? end + 1 : end;
29 int eth_getenv_enetaddr(char *name, uchar *enetaddr)
31 eth_parse_enetaddr(getenv(name), enetaddr);
32 return is_valid_ether_addr(enetaddr);
35 int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
39 sprintf(buf, "%pM", enetaddr);
41 return setenv(name, buf);
44 int eth_getenv_enetaddr_by_index(const char *base_name, int index,
48 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
49 return eth_getenv_enetaddr(enetvar, enetaddr);
52 static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
56 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
57 return eth_setenv_enetaddr(enetvar, enetaddr);
60 static void eth_env_init(void)
64 s = getenv("bootfile");
66 copy_filename(BootFile, s, sizeof(BootFile));
69 static int eth_mac_skip(int index)
73 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
74 return ((skip_state = getenv(enetvar)) != NULL);
77 static void eth_current_changed(void);
80 * CPU and board-specific Ethernet initializations. Aliased function
81 * signals caller to move on
83 static int __def_eth_init(bd_t *bis)
87 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
88 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
94 } eth_rcv_bufs[PKTBUFSRX];
96 static unsigned int eth_rcv_current, eth_rcv_last;
99 static struct eth_device *eth_devices;
100 struct eth_device *eth_current;
102 static void eth_set_current_to_next(void)
104 eth_current = eth_current->next;
107 struct eth_device *eth_get_dev_by_name(const char *devname)
109 struct eth_device *dev, *target_dev;
111 BUG_ON(devname == NULL);
119 if (strcmp(devname, dev->name) == 0) {
124 } while (dev != eth_devices);
129 struct eth_device *eth_get_dev_by_index(int index)
131 struct eth_device *dev, *target_dev;
139 if (dev->index == index) {
144 } while (dev != eth_devices);
149 int eth_get_dev_index(void)
154 return eth_current->index;
157 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
160 unsigned char env_enetaddr[6];
163 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
165 if (!is_zero_ether_addr(env_enetaddr)) {
166 if (!is_zero_ether_addr(dev->enetaddr) &&
167 memcmp(dev->enetaddr, env_enetaddr, 6)) {
168 printf("\nWarning: %s MAC addresses don't match:\n",
170 printf("Address in SROM is %pM\n",
172 printf("Address in environment is %pM\n",
176 memcpy(dev->enetaddr, env_enetaddr, 6);
177 } else if (is_valid_ether_addr(dev->enetaddr)) {
178 eth_setenv_enetaddr_by_index(base_name, eth_number,
180 printf("\nWarning: %s using MAC address from net device\n",
182 } else if (is_zero_ether_addr(dev->enetaddr)) {
183 printf("\nError: %s address not set.\n",
188 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
189 if (!is_valid_ether_addr(dev->enetaddr)) {
190 printf("\nError: %s address %pM illegal value\n",
191 dev->name, dev->enetaddr);
195 ret = dev->write_hwaddr(dev);
197 printf("\nWarning: %s failed to set MAC address\n", dev->name);
203 int eth_register(struct eth_device *dev)
205 struct eth_device *d;
208 assert(strlen(dev->name) < sizeof(dev->name));
211 eth_current = eth_devices = dev;
212 eth_current_changed();
214 for (d = eth_devices; d->next != eth_devices; d = d->next)
219 dev->state = ETH_STATE_INIT;
220 dev->next = eth_devices;
221 dev->index = index++;
226 int eth_unregister(struct eth_device *dev)
228 struct eth_device *cur;
234 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
238 /* Device not found */
239 if (cur->next != dev)
242 cur->next = dev->next;
244 if (eth_devices == dev)
245 eth_devices = dev->next == eth_devices ? NULL : dev->next;
247 if (eth_current == dev) {
248 eth_current = eth_devices;
249 eth_current_changed();
255 int eth_initialize(void)
261 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
262 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
273 * If board-specific initialization exists, call it.
274 * If not, call a CPU-specific one
276 if (board_eth_init != __def_eth_init) {
277 if (board_eth_init(gd->bd) < 0)
278 printf("Board Net Initialization Failed\n");
279 } else if (cpu_eth_init != __def_eth_init) {
280 if (cpu_eth_init(gd->bd) < 0)
281 printf("CPU Net Initialization Failed\n");
283 printf("Net Initialization Skipped\n");
286 puts("No ethernet found.\n");
287 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
289 struct eth_device *dev = eth_devices;
290 char *ethprime = getenv("ethprime");
292 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
297 printf("%s", dev->name);
299 if (ethprime && strcmp(dev->name, ethprime) == 0) {
304 if (strchr(dev->name, ' '))
305 puts("\nWarning: eth device name has a space!"
308 eth_write_hwaddr(dev, "eth", dev->index);
312 } while (dev != eth_devices);
314 eth_current_changed();
321 #ifdef CONFIG_MCAST_TFTP
323 * mcast_addr: multicast ipaddr from which multicast Mac is made
324 * join: 1=join, 0=leave.
326 int eth_mcast_join(IPaddr_t mcast_ip, int join)
329 if (!eth_current || !eth_current->mcast)
331 mcast_mac[5] = htonl(mcast_ip) & 0xff;
332 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
333 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
337 return eth_current->mcast(eth_current, mcast_mac, join);
340 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
341 * and this is the ethernet-crc method needed for TSEC -- and perhaps
342 * some other adapter -- hash tables
344 #define CRCPOLY_LE 0xedb88320
345 u32 ether_crc(size_t len, unsigned char const *p)
352 for (i = 0; i < 8; i++)
353 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
355 /* an reverse the bits, cuz of way they arrive -- last-first */
356 crc = (crc >> 16) | (crc << 16);
357 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
358 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
359 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
360 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
369 struct eth_device *old_current, *dev;
372 puts("No ethernet found.\n");
376 /* Sync environment with network devices */
379 uchar env_enetaddr[6];
381 if (eth_getenv_enetaddr_by_index("eth", dev->index,
383 memcpy(dev->enetaddr, env_enetaddr, 6);
386 } while (dev != eth_devices);
388 old_current = eth_current;
390 debug("Trying %s\n", eth_current->name);
392 if (eth_current->init(eth_current, gd->bd) >= 0) {
393 eth_current->state = ETH_STATE_ACTIVE;
400 } while (old_current != eth_current);
410 eth_current->halt(eth_current);
412 eth_current->state = ETH_STATE_PASSIVE;
415 int eth_send(void *packet, int length)
420 return eth_current->send(eth_current, packet, length);
428 return eth_current->recv(eth_current);
432 static void eth_save_packet(void *packet, int length)
437 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
440 if (PKTSIZE < length)
443 for (i = 0; i < length; i++)
444 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
446 eth_rcv_bufs[eth_rcv_last].length = length;
447 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
450 int eth_receive(void *packet, int length)
453 void *pp = push_packet;
456 if (eth_rcv_current == eth_rcv_last) {
457 push_packet = eth_save_packet;
461 if (eth_rcv_current == eth_rcv_last)
465 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
467 for (i = 0; i < length; i++)
468 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
470 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
473 #endif /* CONFIG_API */
475 static void eth_current_changed(void)
477 char *act = getenv("ethact");
478 /* update current ethernet name */
480 if (act == NULL || strcmp(act, eth_get_name()) != 0)
481 setenv("ethact", eth_get_name());
484 * remove the variable completely if there is no active
487 else if (act != NULL)
488 setenv("ethact", NULL);
491 void eth_try_another(int first_restart)
493 static struct eth_device *first_failed;
497 * Do not rotate between network interfaces when
498 * 'ethrotate' variable is set to 'no'.
500 ethrotate = getenv("ethrotate");
501 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
508 first_failed = eth_get_dev();
510 eth_set_current_to_next();
512 eth_current_changed();
514 if (first_failed == eth_get_dev())
518 void eth_set_current(void)
521 static int env_changed_id;
522 struct eth_device *old_current;
525 if (!eth_get_dev()) /* XXX no current */
528 env_id = get_env_id();
529 if ((act == NULL) || (env_changed_id != env_id)) {
530 act = getenv("ethact");
531 env_changed_id = env_id;
534 old_current = eth_get_dev();
536 if (strcmp(eth_get_name(), act) == 0)
538 eth_set_current_to_next();
539 } while (old_current != eth_get_dev());
542 eth_current_changed();
545 const char *eth_get_name(void)
547 return eth_get_dev() ? eth_get_dev()->name : "unknown";