2 * (C) Copyright 2001-2015
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * Joe Hershberger, National Instruments
6 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/errno.h>
16 #include <dm/device-internal.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
25 for (i = 0; i < 6; ++i) {
26 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
28 addr = (*end) ? end + 1 : end;
32 int eth_getenv_enetaddr(char *name, uchar *enetaddr)
34 eth_parse_enetaddr(getenv(name), enetaddr);
35 return is_valid_ether_addr(enetaddr);
38 int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
42 sprintf(buf, "%pM", enetaddr);
44 return setenv(name, buf);
47 int eth_getenv_enetaddr_by_index(const char *base_name, int index,
51 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
52 return eth_getenv_enetaddr(enetvar, enetaddr);
55 static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
59 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
60 return eth_setenv_enetaddr(enetvar, enetaddr);
63 static void eth_env_init(void)
67 s = getenv("bootfile");
69 copy_filename(BootFile, s, sizeof(BootFile));
72 static int eth_mac_skip(int index)
76 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
77 return ((skip_state = getenv(enetvar)) != NULL);
80 static void eth_current_changed(void);
84 * struct eth_device_priv - private structure for each Ethernet device
86 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
88 struct eth_device_priv {
89 enum eth_state_t state;
93 * struct eth_uclass_priv - The structure attached to the uclass itself
95 * @current: The Ethernet device that the network functions are using
97 struct eth_uclass_priv {
98 struct udevice *current;
101 static struct eth_uclass_priv *eth_get_uclass_priv(void)
105 uclass_get(UCLASS_ETH, &uc);
110 static void eth_set_current_to_next(void)
112 struct eth_uclass_priv *uc_priv;
114 uc_priv = eth_get_uclass_priv();
115 if (uc_priv->current)
116 uclass_next_device(&uc_priv->current);
117 if (!uc_priv->current)
118 uclass_first_device(UCLASS_ETH, &uc_priv->current);
121 struct udevice *eth_get_dev(void)
123 struct eth_uclass_priv *uc_priv;
125 uc_priv = eth_get_uclass_priv();
126 if (!uc_priv->current)
127 uclass_first_device(UCLASS_ETH,
129 return uc_priv->current;
132 static void eth_set_dev(struct udevice *dev)
135 eth_get_uclass_priv()->current = dev;
139 * Find the udevice that either has the name passed in as devname or has an
140 * alias named devname.
142 struct udevice *eth_get_dev_by_name(const char *devname)
146 const char *startp = NULL;
150 /* Must be longer than 3 to be an alias */
151 if (strlen(devname) > strlen("eth")) {
152 startp = devname + strlen("eth");
153 seq = simple_strtoul(startp, &endp, 10);
156 uclass_get(UCLASS_ETH, &uc);
157 uclass_foreach_dev(it, uc) {
158 /* We need the seq to be valid, so make sure it's probed */
161 * Check for the name or the sequence number to match
163 if (strcmp(it->name, devname) == 0 ||
164 (endp > startp && it->seq == seq))
171 unsigned char *eth_get_ethaddr(void)
173 struct eth_pdata *pdata;
176 pdata = eth_get_dev()->platdata;
177 return pdata->enetaddr;
183 /* Set active state without calling start on the driver */
184 int eth_init_state_only(void)
186 struct udevice *current;
187 struct eth_device_priv *priv;
189 current = eth_get_dev();
190 if (!current || !device_active(current))
193 priv = current->uclass_priv;
194 priv->state = ETH_STATE_ACTIVE;
199 /* Set passive state without calling stop on the driver */
200 void eth_halt_state_only(void)
202 struct udevice *current;
203 struct eth_device_priv *priv;
205 current = eth_get_dev();
206 if (!current || !device_active(current))
209 priv = current->uclass_priv;
210 priv->state = ETH_STATE_PASSIVE;
213 int eth_get_dev_index(void)
216 return eth_get_dev()->seq;
222 struct udevice *current;
223 struct udevice *old_current;
225 current = eth_get_dev();
227 printf("No ethernet found.\n");
231 old_current = current;
233 debug("Trying %s\n", current->name);
235 if (device_active(current)) {
236 uchar env_enetaddr[6];
237 struct eth_pdata *pdata = current->platdata;
239 /* Sync environment with network device */
240 if (eth_getenv_enetaddr_by_index("eth", current->seq,
242 memcpy(pdata->enetaddr, env_enetaddr, 6);
244 memset(pdata->enetaddr, 0, 6);
246 if (eth_get_ops(current)->start(current) >= 0) {
247 struct eth_device_priv *priv =
248 current->uclass_priv;
250 priv->state = ETH_STATE_ACTIVE;
256 /* This will ensure the new "current" attempted to probe */
258 current = eth_get_dev();
259 } while (old_current != current);
266 struct udevice *current;
267 struct eth_device_priv *priv;
269 current = eth_get_dev();
270 if (!current || !device_active(current))
273 eth_get_ops(current)->stop(current);
274 priv = current->uclass_priv;
275 priv->state = ETH_STATE_PASSIVE;
278 int eth_send(void *packet, int length)
280 struct udevice *current;
282 current = eth_get_dev();
286 if (!device_active(current))
289 return eth_get_ops(current)->send(current, packet, length);
294 struct udevice *current;
299 current = eth_get_dev();
303 if (!device_active(current))
306 /* Process up to 32 packets at one time */
307 for (i = 0; i < 32; i++) {
308 ret = eth_get_ops(current)->recv(current, &packet);
310 net_process_received_packet(packet, ret);
319 static int eth_write_hwaddr(struct udevice *dev)
321 struct eth_pdata *pdata = dev->platdata;
324 if (!dev || !device_active(dev))
327 /* seq is valid since the device is active */
328 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
329 if (!is_valid_ether_addr(pdata->enetaddr)) {
330 printf("\nError: %s address %pM illegal value\n",
331 dev->name, pdata->enetaddr);
335 ret = eth_get_ops(dev)->write_hwaddr(dev);
337 printf("\nWarning: %s failed to set MAC address\n",
344 int eth_initialize(void)
349 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
353 * Devices need to write the hwaddr even if not started so that Linux
354 * will have access to the hwaddr that u-boot stored for the device.
355 * This is accomplished by attempting to probe each device and calling
356 * their write_hwaddr() operation.
358 uclass_first_device(UCLASS_ETH, &dev);
360 printf("No ethernet found.\n");
361 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
363 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
368 printf("eth%d: %s", dev->seq, dev->name);
370 eth_write_hwaddr(dev);
372 uclass_next_device(&dev);
382 static int eth_post_bind(struct udevice *dev)
384 if (strchr(dev->name, ' ')) {
385 printf("\nError: eth device name \"%s\" has a space!\n",
393 static int eth_pre_unbind(struct udevice *dev)
395 /* Don't hang onto a pointer that is going away */
396 if (dev == eth_get_uclass_priv()->current)
402 static int eth_post_probe(struct udevice *dev)
404 struct eth_device_priv *priv = dev->uclass_priv;
405 struct eth_pdata *pdata = dev->platdata;
406 unsigned char env_enetaddr[6];
408 priv->state = ETH_STATE_INIT;
410 /* Check if the device has a MAC address in ROM */
411 if (eth_get_ops(dev)->read_rom_hwaddr)
412 eth_get_ops(dev)->read_rom_hwaddr(dev);
414 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
415 if (!is_zero_ether_addr(env_enetaddr)) {
416 if (!is_zero_ether_addr(pdata->enetaddr) &&
417 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
418 printf("\nWarning: %s MAC addresses don't match:\n",
420 printf("Address in SROM is %pM\n",
422 printf("Address in environment is %pM\n",
426 /* Override the ROM MAC address */
427 memcpy(pdata->enetaddr, env_enetaddr, 6);
428 } else if (is_valid_ether_addr(pdata->enetaddr)) {
429 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
430 printf("\nWarning: %s using MAC address from ROM\n",
432 } else if (is_zero_ether_addr(pdata->enetaddr)) {
433 printf("\nError: %s address not set.\n",
441 static int eth_pre_remove(struct udevice *dev)
443 eth_get_ops(dev)->stop(dev);
448 UCLASS_DRIVER(eth) = {
451 .post_bind = eth_post_bind,
452 .pre_unbind = eth_pre_unbind,
453 .post_probe = eth_post_probe,
454 .pre_remove = eth_pre_remove,
455 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
456 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
457 .flags = DM_UC_FLAG_SEQ_ALIAS,
461 #ifndef CONFIG_DM_ETH
463 * CPU and board-specific Ethernet initializations. Aliased function
464 * signals caller to move on
466 static int __def_eth_init(bd_t *bis)
470 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
471 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
477 } eth_rcv_bufs[PKTBUFSRX];
479 static unsigned int eth_rcv_current, eth_rcv_last;
482 static struct eth_device *eth_devices;
483 struct eth_device *eth_current;
485 static void eth_set_current_to_next(void)
487 eth_current = eth_current->next;
490 static void eth_set_dev(struct eth_device *dev)
495 struct eth_device *eth_get_dev_by_name(const char *devname)
497 struct eth_device *dev, *target_dev;
499 BUG_ON(devname == NULL);
507 if (strcmp(devname, dev->name) == 0) {
512 } while (dev != eth_devices);
517 struct eth_device *eth_get_dev_by_index(int index)
519 struct eth_device *dev, *target_dev;
527 if (dev->index == index) {
532 } while (dev != eth_devices);
537 int eth_get_dev_index(void)
542 return eth_current->index;
545 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
548 unsigned char env_enetaddr[6];
551 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
553 if (!is_zero_ether_addr(env_enetaddr)) {
554 if (!is_zero_ether_addr(dev->enetaddr) &&
555 memcmp(dev->enetaddr, env_enetaddr, 6)) {
556 printf("\nWarning: %s MAC addresses don't match:\n",
558 printf("Address in SROM is %pM\n",
560 printf("Address in environment is %pM\n",
564 memcpy(dev->enetaddr, env_enetaddr, 6);
565 } else if (is_valid_ether_addr(dev->enetaddr)) {
566 eth_setenv_enetaddr_by_index(base_name, eth_number,
568 printf("\nWarning: %s using MAC address from net device\n",
570 } else if (is_zero_ether_addr(dev->enetaddr)) {
571 printf("\nError: %s address not set.\n",
576 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
577 if (!is_valid_ether_addr(dev->enetaddr)) {
578 printf("\nError: %s address %pM illegal value\n",
579 dev->name, dev->enetaddr);
583 ret = dev->write_hwaddr(dev);
585 printf("\nWarning: %s failed to set MAC address\n", dev->name);
591 int eth_register(struct eth_device *dev)
593 struct eth_device *d;
596 assert(strlen(dev->name) < sizeof(dev->name));
599 eth_current = eth_devices = dev;
600 eth_current_changed();
602 for (d = eth_devices; d->next != eth_devices; d = d->next)
607 dev->state = ETH_STATE_INIT;
608 dev->next = eth_devices;
609 dev->index = index++;
614 int eth_unregister(struct eth_device *dev)
616 struct eth_device *cur;
622 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
626 /* Device not found */
627 if (cur->next != dev)
630 cur->next = dev->next;
632 if (eth_devices == dev)
633 eth_devices = dev->next == eth_devices ? NULL : dev->next;
635 if (eth_current == dev) {
636 eth_current = eth_devices;
637 eth_current_changed();
643 int eth_initialize(void)
649 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
650 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
661 * If board-specific initialization exists, call it.
662 * If not, call a CPU-specific one
664 if (board_eth_init != __def_eth_init) {
665 if (board_eth_init(gd->bd) < 0)
666 printf("Board Net Initialization Failed\n");
667 } else if (cpu_eth_init != __def_eth_init) {
668 if (cpu_eth_init(gd->bd) < 0)
669 printf("CPU Net Initialization Failed\n");
671 printf("Net Initialization Skipped\n");
674 puts("No ethernet found.\n");
675 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
677 struct eth_device *dev = eth_devices;
678 char *ethprime = getenv("ethprime");
680 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
685 printf("%s", dev->name);
687 if (ethprime && strcmp(dev->name, ethprime) == 0) {
692 if (strchr(dev->name, ' '))
693 puts("\nWarning: eth device name has a space!"
696 eth_write_hwaddr(dev, "eth", dev->index);
700 } while (dev != eth_devices);
702 eth_current_changed();
709 #ifdef CONFIG_MCAST_TFTP
711 * mcast_addr: multicast ipaddr from which multicast Mac is made
712 * join: 1=join, 0=leave.
714 int eth_mcast_join(IPaddr_t mcast_ip, int join)
717 if (!eth_current || !eth_current->mcast)
719 mcast_mac[5] = htonl(mcast_ip) & 0xff;
720 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
721 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
725 return eth_current->mcast(eth_current, mcast_mac, join);
728 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
729 * and this is the ethernet-crc method needed for TSEC -- and perhaps
730 * some other adapter -- hash tables
732 #define CRCPOLY_LE 0xedb88320
733 u32 ether_crc(size_t len, unsigned char const *p)
740 for (i = 0; i < 8; i++)
741 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
743 /* an reverse the bits, cuz of way they arrive -- last-first */
744 crc = (crc >> 16) | (crc << 16);
745 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
746 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
747 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
748 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
757 struct eth_device *old_current, *dev;
760 puts("No ethernet found.\n");
764 /* Sync environment with network devices */
767 uchar env_enetaddr[6];
769 if (eth_getenv_enetaddr_by_index("eth", dev->index,
771 memcpy(dev->enetaddr, env_enetaddr, 6);
774 } while (dev != eth_devices);
776 old_current = eth_current;
778 debug("Trying %s\n", eth_current->name);
780 if (eth_current->init(eth_current, gd->bd) >= 0) {
781 eth_current->state = ETH_STATE_ACTIVE;
788 } while (old_current != eth_current);
798 eth_current->halt(eth_current);
800 eth_current->state = ETH_STATE_PASSIVE;
803 int eth_send(void *packet, int length)
808 return eth_current->send(eth_current, packet, length);
816 return eth_current->recv(eth_current);
818 #endif /* ifndef CONFIG_DM_ETH */
821 static void eth_save_packet(void *packet, int length)
826 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
829 if (PKTSIZE < length)
832 for (i = 0; i < length; i++)
833 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
835 eth_rcv_bufs[eth_rcv_last].length = length;
836 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
839 int eth_receive(void *packet, int length)
842 void *pp = push_packet;
845 if (eth_rcv_current == eth_rcv_last) {
846 push_packet = eth_save_packet;
850 if (eth_rcv_current == eth_rcv_last)
854 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
856 for (i = 0; i < length; i++)
857 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
859 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
862 #endif /* CONFIG_API */
864 static void eth_current_changed(void)
866 char *act = getenv("ethact");
867 /* update current ethernet name */
869 if (act == NULL || strcmp(act, eth_get_name()) != 0)
870 setenv("ethact", eth_get_name());
873 * remove the variable completely if there is no active
876 else if (act != NULL)
877 setenv("ethact", NULL);
880 void eth_try_another(int first_restart)
882 static void *first_failed;
886 * Do not rotate between network interfaces when
887 * 'ethrotate' variable is set to 'no'.
889 ethrotate = getenv("ethrotate");
890 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
897 first_failed = eth_get_dev();
899 eth_set_current_to_next();
901 eth_current_changed();
903 if (first_failed == eth_get_dev())
907 void eth_set_current(void)
910 static int env_changed_id;
913 env_id = get_env_id();
914 if ((act == NULL) || (env_changed_id != env_id)) {
915 act = getenv("ethact");
916 env_changed_id = env_id;
919 eth_set_dev(eth_get_dev_by_name(act));
921 eth_current_changed();
924 const char *eth_get_name(void)
926 return eth_get_dev() ? eth_get_dev()->name : "unknown";