2 * (C) Copyright 2001-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
14 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
19 for (i = 0; i < 6; ++i) {
20 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
22 addr = (*end) ? end + 1 : end;
26 int eth_getenv_enetaddr(char *name, uchar *enetaddr)
28 eth_parse_enetaddr(getenv(name), enetaddr);
29 return is_valid_ether_addr(enetaddr);
32 int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
36 sprintf(buf, "%pM", enetaddr);
38 return setenv(name, buf);
41 int eth_getenv_enetaddr_by_index(const char *base_name, int index,
45 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
46 return eth_getenv_enetaddr(enetvar, enetaddr);
49 static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
53 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
54 return eth_setenv_enetaddr(enetvar, enetaddr);
58 static int eth_mac_skip(int index)
62 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
63 return ((skip_state = getenv(enetvar)) != NULL);
66 #ifdef CONFIG_RANDOM_MACADDR
67 void eth_random_enetaddr(uchar *enetaddr)
74 enetaddr[0] = rval & 0xff;
75 enetaddr[1] = (rval >> 8) & 0xff;
76 enetaddr[2] = (rval >> 16) & 0xff;
79 enetaddr[3] = rval & 0xff;
80 enetaddr[4] = (rval >> 8) & 0xff;
81 enetaddr[5] = (rval >> 16) & 0xff;
83 /* make sure it's local and unicast */
84 enetaddr[0] = (enetaddr[0] | 0x02) & ~0x01;
89 * CPU and board-specific Ethernet initializations. Aliased function
90 * signals caller to move on
92 static int __def_eth_init(bd_t *bis)
96 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
97 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
103 } eth_rcv_bufs[PKTBUFSRX];
105 static unsigned int eth_rcv_current, eth_rcv_last;
108 static struct eth_device *eth_devices;
109 struct eth_device *eth_current;
111 struct eth_device *eth_get_dev_by_name(const char *devname)
113 struct eth_device *dev, *target_dev;
115 BUG_ON(devname == NULL);
123 if (strcmp(devname, dev->name) == 0) {
128 } while (dev != eth_devices);
133 struct eth_device *eth_get_dev_by_index(int index)
135 struct eth_device *dev, *target_dev;
143 if (dev->index == index) {
148 } while (dev != eth_devices);
153 int eth_get_dev_index(void)
158 return eth_current->index;
161 static void eth_current_changed(void)
163 char *act = getenv("ethact");
164 /* update current ethernet name */
166 if (act == NULL || strcmp(act, eth_current->name) != 0)
167 setenv("ethact", eth_current->name);
170 * remove the variable completely if there is no active
173 else if (act != NULL)
174 setenv("ethact", NULL);
177 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
180 unsigned char env_enetaddr[6];
183 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
185 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
186 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
187 memcmp(dev->enetaddr, env_enetaddr, 6)) {
188 printf("\nWarning: %s MAC addresses don't match:\n",
190 printf("Address in SROM is %pM\n",
192 printf("Address in environment is %pM\n",
196 memcpy(dev->enetaddr, env_enetaddr, 6);
197 } else if (is_valid_ether_addr(dev->enetaddr)) {
198 eth_setenv_enetaddr_by_index(base_name, eth_number,
200 printf("\nWarning: %s using MAC address from net device\n",
204 if (dev->write_hwaddr &&
205 !eth_mac_skip(eth_number)) {
206 if (!is_valid_ether_addr(dev->enetaddr))
209 ret = dev->write_hwaddr(dev);
215 int eth_register(struct eth_device *dev)
217 struct eth_device *d;
220 assert(strlen(dev->name) < sizeof(dev->name));
223 eth_current = eth_devices = dev;
224 eth_current_changed();
226 for (d = eth_devices; d->next != eth_devices; d = d->next)
231 dev->state = ETH_STATE_INIT;
232 dev->next = eth_devices;
233 dev->index = index++;
238 int eth_unregister(struct eth_device *dev)
240 struct eth_device *cur;
246 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
250 /* Device not found */
251 if (cur->next != dev)
254 cur->next = dev->next;
256 if (eth_devices == dev)
257 eth_devices = dev->next == eth_devices ? NULL : dev->next;
259 if (eth_current == dev) {
260 eth_current = eth_devices;
261 eth_current_changed();
267 static void eth_env_init(bd_t *bis)
271 if ((s = getenv("bootfile")) != NULL)
272 copy_filename(BootFile, s, sizeof(BootFile));
275 int eth_initialize(bd_t *bis)
281 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
282 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
293 * If board-specific initialization exists, call it.
294 * If not, call a CPU-specific one
296 if (board_eth_init != __def_eth_init) {
297 if (board_eth_init(bis) < 0)
298 printf("Board Net Initialization Failed\n");
299 } else if (cpu_eth_init != __def_eth_init) {
300 if (cpu_eth_init(bis) < 0)
301 printf("CPU Net Initialization Failed\n");
303 printf("Net Initialization Skipped\n");
306 puts("No ethernet found.\n");
307 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
309 struct eth_device *dev = eth_devices;
310 char *ethprime = getenv("ethprime");
312 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
317 printf("%s", dev->name);
319 if (ethprime && strcmp(dev->name, ethprime) == 0) {
324 if (strchr(dev->name, ' '))
325 puts("\nWarning: eth device name has a space!"
328 if (eth_write_hwaddr(dev, "eth", dev->index))
329 puts("\nWarning: failed to set MAC address\n");
333 } while (dev != eth_devices);
335 eth_current_changed();
342 #ifdef CONFIG_MCAST_TFTP
344 * mcast_addr: multicast ipaddr from which multicast Mac is made
345 * join: 1=join, 0=leave.
347 int eth_mcast_join(IPaddr_t mcast_ip, u8 join)
350 if (!eth_current || !eth_current->mcast)
352 mcast_mac[5] = htonl(mcast_ip) & 0xff;
353 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
354 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
358 return eth_current->mcast(eth_current, mcast_mac, join);
361 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
362 * and this is the ethernet-crc method needed for TSEC -- and perhaps
363 * some other adapter -- hash tables
365 #define CRCPOLY_LE 0xedb88320
366 u32 ether_crc(size_t len, unsigned char const *p)
373 for (i = 0; i < 8; i++)
374 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
376 /* an reverse the bits, cuz of way they arrive -- last-first */
377 crc = (crc >> 16) | (crc << 16);
378 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
379 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
380 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
381 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
388 int eth_init(bd_t *bis)
390 struct eth_device *old_current, *dev;
393 puts("No ethernet found.\n");
397 /* Sync environment with network devices */
400 uchar env_enetaddr[6];
402 if (eth_getenv_enetaddr_by_index("eth", dev->index,
404 memcpy(dev->enetaddr, env_enetaddr, 6);
407 } while (dev != eth_devices);
409 old_current = eth_current;
411 debug("Trying %s\n", eth_current->name);
413 if (eth_current->init(eth_current, bis) >= 0) {
414 eth_current->state = ETH_STATE_ACTIVE;
421 } while (old_current != eth_current);
431 eth_current->halt(eth_current);
433 eth_current->state = ETH_STATE_PASSIVE;
436 int eth_send(void *packet, int length)
441 return eth_current->send(eth_current, packet, length);
449 return eth_current->recv(eth_current);
453 static void eth_save_packet(void *packet, int length)
458 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
461 if (PKTSIZE < length)
464 for (i = 0; i < length; i++)
465 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
467 eth_rcv_bufs[eth_rcv_last].length = length;
468 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
471 int eth_receive(void *packet, int length)
474 void *pp = push_packet;
477 if (eth_rcv_current == eth_rcv_last) {
478 push_packet = eth_save_packet;
482 if (eth_rcv_current == eth_rcv_last)
486 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
488 for (i = 0; i < length; i++)
489 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
491 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
494 #endif /* CONFIG_API */
496 void eth_try_another(int first_restart)
498 static struct eth_device *first_failed;
502 * Do not rotate between network interfaces when
503 * 'ethrotate' variable is set to 'no'.
505 ethrotate = getenv("ethrotate");
506 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
513 first_failed = eth_current;
515 eth_current = eth_current->next;
517 eth_current_changed();
519 if (first_failed == eth_current)
523 void eth_set_current(void)
526 static int env_changed_id;
527 struct eth_device *old_current;
530 if (!eth_current) /* XXX no current */
533 env_id = get_env_id();
534 if ((act == NULL) || (env_changed_id != env_id)) {
535 act = getenv("ethact");
536 env_changed_id = env_id;
539 old_current = eth_current;
541 if (strcmp(eth_current->name, act) == 0)
543 eth_current = eth_current->next;
544 } while (old_current != eth_current);
547 eth_current_changed();
550 char *eth_get_name(void)
552 return eth_current ? eth_current->name : "unknown";