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 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
20 for (i = 0; i < 6; ++i) {
21 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
23 addr = (*end) ? end + 1 : end;
27 int eth_getenv_enetaddr(char *name, uchar *enetaddr)
29 eth_parse_enetaddr(getenv(name), enetaddr);
30 return is_valid_ether_addr(enetaddr);
33 int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
37 sprintf(buf, "%pM", enetaddr);
39 return setenv(name, buf);
42 int eth_getenv_enetaddr_by_index(const char *base_name, int index,
46 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
47 return eth_getenv_enetaddr(enetvar, enetaddr);
50 static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
54 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
55 return eth_setenv_enetaddr(enetvar, enetaddr);
59 static int eth_mac_skip(int index)
63 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
64 return ((skip_state = getenv(enetvar)) != NULL);
68 * CPU and board-specific Ethernet initializations. Aliased function
69 * signals caller to move on
71 static int __def_eth_init(bd_t *bis)
75 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
76 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
82 } eth_rcv_bufs[PKTBUFSRX];
84 static unsigned int eth_rcv_current, eth_rcv_last;
87 static struct eth_device *eth_devices;
88 struct eth_device *eth_current;
90 struct eth_device *eth_get_dev_by_name(const char *devname)
92 struct eth_device *dev, *target_dev;
94 BUG_ON(devname == NULL);
102 if (strcmp(devname, dev->name) == 0) {
107 } while (dev != eth_devices);
112 struct eth_device *eth_get_dev_by_index(int index)
114 struct eth_device *dev, *target_dev;
122 if (dev->index == index) {
127 } while (dev != eth_devices);
132 int eth_get_dev_index(void)
137 return eth_current->index;
140 static void eth_current_changed(void)
142 char *act = getenv("ethact");
143 /* update current ethernet name */
145 if (act == NULL || strcmp(act, eth_current->name) != 0)
146 setenv("ethact", eth_current->name);
149 * remove the variable completely if there is no active
152 else if (act != NULL)
153 setenv("ethact", NULL);
156 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
159 unsigned char env_enetaddr[6];
162 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
164 if (!is_zero_ether_addr(env_enetaddr)) {
165 if (!is_zero_ether_addr(dev->enetaddr) &&
166 memcmp(dev->enetaddr, env_enetaddr, 6)) {
167 printf("\nWarning: %s MAC addresses don't match:\n",
169 printf("Address in SROM is %pM\n",
171 printf("Address in environment is %pM\n",
175 memcpy(dev->enetaddr, env_enetaddr, 6);
176 } else if (is_valid_ether_addr(dev->enetaddr)) {
177 eth_setenv_enetaddr_by_index(base_name, eth_number,
179 printf("\nWarning: %s using MAC address from net device\n",
181 } else if (is_zero_ether_addr(dev->enetaddr)) {
182 printf("\nError: %s address not set.\n",
187 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
188 if (!is_valid_ether_addr(dev->enetaddr)) {
189 printf("\nError: %s address %pM illegal value\n",
190 dev->name, dev->enetaddr);
194 ret = dev->write_hwaddr(dev);
196 printf("\nWarning: %s failed to set MAC address\n", dev->name);
202 int eth_register(struct eth_device *dev)
204 struct eth_device *d;
207 assert(strlen(dev->name) < sizeof(dev->name));
210 eth_current = eth_devices = dev;
211 eth_current_changed();
213 for (d = eth_devices; d->next != eth_devices; d = d->next)
218 dev->state = ETH_STATE_INIT;
219 dev->next = eth_devices;
220 dev->index = index++;
225 int eth_unregister(struct eth_device *dev)
227 struct eth_device *cur;
233 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
237 /* Device not found */
238 if (cur->next != dev)
241 cur->next = dev->next;
243 if (eth_devices == dev)
244 eth_devices = dev->next == eth_devices ? NULL : dev->next;
246 if (eth_current == dev) {
247 eth_current = eth_devices;
248 eth_current_changed();
254 static void eth_env_init(bd_t *bis)
258 if ((s = getenv("bootfile")) != NULL)
259 copy_filename(BootFile, s, sizeof(BootFile));
262 int eth_initialize(bd_t *bis)
268 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
269 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
280 * If board-specific initialization exists, call it.
281 * If not, call a CPU-specific one
283 if (board_eth_init != __def_eth_init) {
284 if (board_eth_init(bis) < 0)
285 printf("Board Net Initialization Failed\n");
286 } else if (cpu_eth_init != __def_eth_init) {
287 if (cpu_eth_init(bis) < 0)
288 printf("CPU Net Initialization Failed\n");
290 printf("Net Initialization Skipped\n");
293 puts("No ethernet found.\n");
294 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
296 struct eth_device *dev = eth_devices;
297 char *ethprime = getenv("ethprime");
299 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
304 printf("%s", dev->name);
306 if (ethprime && strcmp(dev->name, ethprime) == 0) {
311 if (strchr(dev->name, ' '))
312 puts("\nWarning: eth device name has a space!"
315 eth_write_hwaddr(dev, "eth", dev->index);
319 } while (dev != eth_devices);
321 eth_current_changed();
328 #ifdef CONFIG_MCAST_TFTP
330 * mcast_addr: multicast ipaddr from which multicast Mac is made
331 * join: 1=join, 0=leave.
333 int eth_mcast_join(IPaddr_t mcast_ip, u8 join)
336 if (!eth_current || !eth_current->mcast)
338 mcast_mac[5] = htonl(mcast_ip) & 0xff;
339 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
340 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
344 return eth_current->mcast(eth_current, mcast_mac, join);
347 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
348 * and this is the ethernet-crc method needed for TSEC -- and perhaps
349 * some other adapter -- hash tables
351 #define CRCPOLY_LE 0xedb88320
352 u32 ether_crc(size_t len, unsigned char const *p)
359 for (i = 0; i < 8; i++)
360 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
362 /* an reverse the bits, cuz of way they arrive -- last-first */
363 crc = (crc >> 16) | (crc << 16);
364 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
365 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
366 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
367 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
374 int eth_init(bd_t *bis)
376 struct eth_device *old_current, *dev;
379 puts("No ethernet found.\n");
383 /* Sync environment with network devices */
386 uchar env_enetaddr[6];
388 if (eth_getenv_enetaddr_by_index("eth", dev->index,
390 memcpy(dev->enetaddr, env_enetaddr, 6);
393 } while (dev != eth_devices);
395 old_current = eth_current;
397 debug("Trying %s\n", eth_current->name);
399 if (eth_current->init(eth_current, bis) >= 0) {
400 eth_current->state = ETH_STATE_ACTIVE;
407 } while (old_current != eth_current);
417 eth_current->halt(eth_current);
419 eth_current->state = ETH_STATE_PASSIVE;
422 int eth_send(void *packet, int length)
427 return eth_current->send(eth_current, packet, length);
435 return eth_current->recv(eth_current);
439 static void eth_save_packet(void *packet, int length)
444 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
447 if (PKTSIZE < length)
450 for (i = 0; i < length; i++)
451 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
453 eth_rcv_bufs[eth_rcv_last].length = length;
454 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
457 int eth_receive(void *packet, int length)
460 void *pp = push_packet;
463 if (eth_rcv_current == eth_rcv_last) {
464 push_packet = eth_save_packet;
468 if (eth_rcv_current == eth_rcv_last)
472 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
474 for (i = 0; i < length; i++)
475 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
477 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
480 #endif /* CONFIG_API */
482 void eth_try_another(int first_restart)
484 static struct eth_device *first_failed;
488 * Do not rotate between network interfaces when
489 * 'ethrotate' variable is set to 'no'.
491 ethrotate = getenv("ethrotate");
492 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
499 first_failed = eth_current;
501 eth_current = eth_current->next;
503 eth_current_changed();
505 if (first_failed == eth_current)
509 void eth_set_current(void)
512 static int env_changed_id;
513 struct eth_device *old_current;
516 if (!eth_current) /* XXX no current */
519 env_id = get_env_id();
520 if ((act == NULL) || (env_changed_id != env_id)) {
521 act = getenv("ethact");
522 env_changed_id = env_id;
525 old_current = eth_current;
527 if (strcmp(eth_current->name, act) == 0)
529 eth_current = eth_current->next;
530 } while (old_current != eth_current);
533 eth_current_changed();
536 char *eth_get_name(void)
538 return eth_current ? eth_current->name : "unknown";