2 * (C) Copyright 2001-2015
4 * Joe Hershberger, National Instruments
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
16 #include <asm/errno.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
27 for (i = 0; i < 6; ++i) {
28 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
30 addr = (*end) ? end + 1 : end;
34 int eth_getenv_enetaddr(const char *name, uchar *enetaddr)
36 eth_parse_enetaddr(getenv(name), enetaddr);
37 return is_valid_ethaddr(enetaddr);
40 int eth_setenv_enetaddr(const char *name, const uchar *enetaddr)
44 sprintf(buf, "%pM", enetaddr);
46 return setenv(name, buf);
49 int eth_getenv_enetaddr_by_index(const char *base_name, int index,
53 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
54 return eth_getenv_enetaddr(enetvar, enetaddr);
57 static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
61 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
62 return eth_setenv_enetaddr(enetvar, enetaddr);
65 static int eth_mac_skip(int index)
70 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
71 skip_state = getenv(enetvar);
72 return skip_state != NULL;
75 static void eth_current_changed(void);
78 * CPU and board-specific Ethernet initializations. Aliased function
79 * signals caller to move on
81 static int __def_eth_init(bd_t *bis)
85 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
86 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
88 static void eth_common_init(void)
90 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
91 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
100 * If board-specific initialization exists, call it.
101 * If not, call a CPU-specific one
103 if (board_eth_init != __def_eth_init) {
104 if (board_eth_init(gd->bd) < 0)
105 printf("Board Net Initialization Failed\n");
106 } else if (cpu_eth_init != __def_eth_init) {
107 if (cpu_eth_init(gd->bd) < 0)
108 printf("CPU Net Initialization Failed\n");
110 #ifndef CONFIG_DM_ETH
111 printf("Net Initialization Skipped\n");
118 * struct eth_device_priv - private structure for each Ethernet device
120 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
122 struct eth_device_priv {
123 enum eth_state_t state;
127 * struct eth_uclass_priv - The structure attached to the uclass itself
129 * @current: The Ethernet device that the network functions are using
131 struct eth_uclass_priv {
132 struct udevice *current;
135 /* eth_errno - This stores the most recent failure code from DM functions */
136 static int eth_errno;
138 static struct eth_uclass_priv *eth_get_uclass_priv(void)
142 uclass_get(UCLASS_ETH, &uc);
147 static void eth_set_current_to_next(void)
149 struct eth_uclass_priv *uc_priv;
151 uc_priv = eth_get_uclass_priv();
152 if (uc_priv->current)
153 uclass_next_device(&uc_priv->current);
154 if (!uc_priv->current)
155 uclass_first_device(UCLASS_ETH, &uc_priv->current);
159 * Typically this will simply return the active device.
160 * In the case where the most recent active device was unset, this will attempt
161 * to return the first device. If that device doesn't exist or fails to probe,
162 * this function will return NULL.
164 struct udevice *eth_get_dev(void)
166 struct eth_uclass_priv *uc_priv;
168 uc_priv = eth_get_uclass_priv();
169 if (!uc_priv->current)
170 eth_errno = uclass_first_device(UCLASS_ETH,
172 return uc_priv->current;
176 * Typically this will just store a device pointer.
177 * In case it was not probed, we will attempt to do so.
178 * dev may be NULL to unset the active device.
180 static void eth_set_dev(struct udevice *dev)
182 if (dev && !device_active(dev)) {
183 eth_errno = device_probe(dev);
188 eth_get_uclass_priv()->current = dev;
192 * Find the udevice that either has the name passed in as devname or has an
193 * alias named devname.
195 struct udevice *eth_get_dev_by_name(const char *devname)
199 const char *startp = NULL;
202 int len = strlen("eth");
204 /* Must be longer than 3 to be an alias */
205 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
206 startp = devname + len;
207 seq = simple_strtoul(startp, &endp, 10);
210 uclass_get(UCLASS_ETH, &uc);
211 uclass_foreach_dev(it, uc) {
213 * We need the seq to be valid, so try to probe it.
214 * If the probe fails, the seq will not match since it will be
215 * -1 instead of what we are looking for.
216 * We don't care about errors from probe here. Either they won't
217 * match an alias or it will match a literal name and we'll pick
218 * up the error when we try to probe again in eth_set_dev().
220 if (device_probe(it))
222 /* Check for the name or the sequence number to match */
223 if (strcmp(it->name, devname) == 0 ||
224 (endp > startp && it->seq == seq))
231 unsigned char *eth_get_ethaddr(void)
233 struct eth_pdata *pdata;
236 pdata = eth_get_dev()->platdata;
237 return pdata->enetaddr;
243 /* Set active state without calling start on the driver */
244 int eth_init_state_only(void)
246 struct udevice *current;
247 struct eth_device_priv *priv;
249 current = eth_get_dev();
250 if (!current || !device_active(current))
253 priv = current->uclass_priv;
254 priv->state = ETH_STATE_ACTIVE;
259 /* Set passive state without calling stop on the driver */
260 void eth_halt_state_only(void)
262 struct udevice *current;
263 struct eth_device_priv *priv;
265 current = eth_get_dev();
266 if (!current || !device_active(current))
269 priv = current->uclass_priv;
270 priv->state = ETH_STATE_PASSIVE;
273 int eth_get_dev_index(void)
276 return eth_get_dev()->seq;
280 static int eth_write_hwaddr(struct udevice *dev)
282 struct eth_pdata *pdata = dev->platdata;
285 if (!dev || !device_active(dev))
288 /* seq is valid since the device is active */
289 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
290 if (!is_valid_ethaddr(pdata->enetaddr)) {
291 printf("\nError: %s address %pM illegal value\n",
292 dev->name, pdata->enetaddr);
297 * Drivers are allowed to decide not to implement this at
298 * run-time. E.g. Some devices may use it and some may not.
300 ret = eth_get_ops(dev)->write_hwaddr(dev);
304 printf("\nWarning: %s failed to set MAC address\n",
311 static int on_ethaddr(const char *name, const char *value, enum env_op op,
318 /* look for an index after "eth" */
319 index = simple_strtoul(name + 3, NULL, 10);
321 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
323 struct eth_pdata *pdata = dev->platdata;
326 case env_op_overwrite:
327 eth_parse_enetaddr(value, pdata->enetaddr);
330 memset(pdata->enetaddr, 0, 6);
336 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
340 struct udevice *current;
341 struct udevice *old_current;
344 current = eth_get_dev();
346 printf("No ethernet found.\n");
350 old_current = current;
353 debug("Trying %s\n", current->name);
355 if (device_active(current)) {
356 ret = eth_get_ops(current)->start(current);
358 struct eth_device_priv *priv =
359 current->uclass_priv;
361 priv->state = ETH_STATE_ACTIVE;
370 debug("PROBE FAIL\n");
374 * If ethrotate is enabled, this will change "current",
375 * otherwise we will drop out of this while loop immediately
378 /* This will ensure the new "current" attempted to probe */
379 current = eth_get_dev();
380 } while (old_current != current);
387 struct udevice *current;
388 struct eth_device_priv *priv;
390 current = eth_get_dev();
391 if (!current || !device_active(current))
394 eth_get_ops(current)->stop(current);
395 priv = current->uclass_priv;
396 priv->state = ETH_STATE_PASSIVE;
399 int eth_is_active(struct udevice *dev)
401 struct eth_device_priv *priv;
403 if (!dev || !device_active(dev))
406 priv = dev_get_uclass_priv(dev);
407 return priv->state == ETH_STATE_ACTIVE;
410 int eth_send(void *packet, int length)
412 struct udevice *current;
415 current = eth_get_dev();
419 if (!device_active(current))
422 ret = eth_get_ops(current)->send(current, packet, length);
424 /* We cannot completely return the error at present */
425 debug("%s: send() returned error %d\n", __func__, ret);
432 struct udevice *current;
438 current = eth_get_dev();
442 if (!device_active(current))
445 /* Process up to 32 packets at one time */
446 flags = ETH_RECV_CHECK_DEVICE;
447 for (i = 0; i < 32; i++) {
448 ret = eth_get_ops(current)->recv(current, flags, &packet);
451 net_process_received_packet(packet, ret);
452 if (ret >= 0 && eth_get_ops(current)->free_pkt)
453 eth_get_ops(current)->free_pkt(current, packet, ret);
460 /* We cannot completely return the error at present */
461 debug("%s: recv() returned error %d\n", __func__, ret);
466 int eth_initialize(void)
474 * Devices need to write the hwaddr even if not started so that Linux
475 * will have access to the hwaddr that u-boot stored for the device.
476 * This is accomplished by attempting to probe each device and calling
477 * their write_hwaddr() operation.
479 uclass_first_device(UCLASS_ETH, &dev);
481 printf("No ethernet found.\n");
482 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
484 char *ethprime = getenv("ethprime");
485 struct udevice *prime_dev = NULL;
488 prime_dev = eth_get_dev_by_name(ethprime);
490 eth_set_dev(prime_dev);
491 eth_current_changed();
496 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
501 printf("eth%d: %s", dev->seq, dev->name);
503 if (ethprime && dev == prime_dev)
506 eth_write_hwaddr(dev);
508 uclass_next_device(&dev);
518 static int eth_post_bind(struct udevice *dev)
520 if (strchr(dev->name, ' ')) {
521 printf("\nError: eth device name \"%s\" has a space!\n",
529 static int eth_pre_unbind(struct udevice *dev)
531 /* Don't hang onto a pointer that is going away */
532 if (dev == eth_get_uclass_priv()->current)
538 static int eth_post_probe(struct udevice *dev)
540 struct eth_device_priv *priv = dev->uclass_priv;
541 struct eth_pdata *pdata = dev->platdata;
542 unsigned char env_enetaddr[6];
544 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
545 struct eth_ops *ops = eth_get_ops(dev);
546 static int reloc_done;
550 ops->start += gd->reloc_off;
552 ops->send += gd->reloc_off;
554 ops->recv += gd->reloc_off;
556 ops->free_pkt += gd->reloc_off;
558 ops->stop += gd->reloc_off;
559 #ifdef CONFIG_MCAST_TFTP
561 ops->mcast += gd->reloc_off;
563 if (ops->write_hwaddr)
564 ops->write_hwaddr += gd->reloc_off;
565 if (ops->read_rom_hwaddr)
566 ops->read_rom_hwaddr += gd->reloc_off;
572 priv->state = ETH_STATE_INIT;
574 /* Check if the device has a MAC address in ROM */
575 if (eth_get_ops(dev)->read_rom_hwaddr)
576 eth_get_ops(dev)->read_rom_hwaddr(dev);
578 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
579 if (!is_zero_ethaddr(env_enetaddr)) {
580 if (!is_zero_ethaddr(pdata->enetaddr) &&
581 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
582 printf("\nWarning: %s MAC addresses don't match:\n",
584 printf("Address in SROM is %pM\n",
586 printf("Address in environment is %pM\n",
590 /* Override the ROM MAC address */
591 memcpy(pdata->enetaddr, env_enetaddr, 6);
592 } else if (is_valid_ethaddr(pdata->enetaddr)) {
593 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
594 printf("\nWarning: %s using MAC address from ROM\n",
596 } else if (is_zero_ethaddr(pdata->enetaddr)) {
597 #ifdef CONFIG_NET_RANDOM_ETHADDR
598 net_random_ethaddr(pdata->enetaddr);
599 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
600 dev->name, dev->seq, pdata->enetaddr);
602 printf("\nError: %s address not set.\n",
611 static int eth_pre_remove(struct udevice *dev)
613 struct eth_pdata *pdata = dev->platdata;
615 eth_get_ops(dev)->stop(dev);
617 /* clear the MAC address */
618 memset(pdata->enetaddr, 0, 6);
623 UCLASS_DRIVER(eth) = {
626 .post_bind = eth_post_bind,
627 .pre_unbind = eth_pre_unbind,
628 .post_probe = eth_post_probe,
629 .pre_remove = eth_pre_remove,
630 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
631 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
632 .flags = DM_UC_FLAG_SEQ_ALIAS,
634 #endif /* #ifdef CONFIG_DM_ETH */
636 #ifndef CONFIG_DM_ETH
642 } eth_rcv_bufs[PKTBUFSRX];
644 static unsigned int eth_rcv_current, eth_rcv_last;
647 static struct eth_device *eth_devices;
648 struct eth_device *eth_current;
650 static void eth_set_current_to_next(void)
652 eth_current = eth_current->next;
655 static void eth_set_dev(struct eth_device *dev)
660 struct eth_device *eth_get_dev_by_name(const char *devname)
662 struct eth_device *dev, *target_dev;
664 BUG_ON(devname == NULL);
672 if (strcmp(devname, dev->name) == 0) {
677 } while (dev != eth_devices);
682 struct eth_device *eth_get_dev_by_index(int index)
684 struct eth_device *dev, *target_dev;
692 if (dev->index == index) {
697 } while (dev != eth_devices);
702 int eth_get_dev_index(void)
707 return eth_current->index;
710 static int on_ethaddr(const char *name, const char *value, enum env_op op,
714 struct eth_device *dev;
719 /* look for an index after "eth" */
720 index = simple_strtoul(name + 3, NULL, 10);
724 if (dev->index == index) {
727 case env_op_overwrite:
728 eth_parse_enetaddr(value, dev->enetaddr);
731 memset(dev->enetaddr, 0, 6);
735 } while (dev != eth_devices);
739 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
741 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
744 unsigned char env_enetaddr[6];
747 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
749 if (!is_zero_ethaddr(env_enetaddr)) {
750 if (!is_zero_ethaddr(dev->enetaddr) &&
751 memcmp(dev->enetaddr, env_enetaddr, 6)) {
752 printf("\nWarning: %s MAC addresses don't match:\n",
754 printf("Address in SROM is %pM\n",
756 printf("Address in environment is %pM\n",
760 memcpy(dev->enetaddr, env_enetaddr, 6);
761 } else if (is_valid_ethaddr(dev->enetaddr)) {
762 eth_setenv_enetaddr_by_index(base_name, eth_number,
764 printf("\nWarning: %s using MAC address from net device\n",
766 } else if (is_zero_ethaddr(dev->enetaddr)) {
767 #ifdef CONFIG_NET_RANDOM_ETHADDR
768 net_random_ethaddr(dev->enetaddr);
769 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
770 dev->name, eth_number, dev->enetaddr);
772 printf("\nError: %s address not set.\n",
778 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
779 if (!is_valid_ethaddr(dev->enetaddr)) {
780 printf("\nError: %s address %pM illegal value\n",
781 dev->name, dev->enetaddr);
785 ret = dev->write_hwaddr(dev);
787 printf("\nWarning: %s failed to set MAC address\n",
794 int eth_register(struct eth_device *dev)
796 struct eth_device *d;
799 assert(strlen(dev->name) < sizeof(dev->name));
804 eth_current_changed();
806 for (d = eth_devices; d->next != eth_devices; d = d->next)
811 dev->state = ETH_STATE_INIT;
812 dev->next = eth_devices;
813 dev->index = index++;
818 int eth_unregister(struct eth_device *dev)
820 struct eth_device *cur;
826 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
830 /* Device not found */
831 if (cur->next != dev)
834 cur->next = dev->next;
836 if (eth_devices == dev)
837 eth_devices = dev->next == eth_devices ? NULL : dev->next;
839 if (eth_current == dev) {
840 eth_current = eth_devices;
841 eth_current_changed();
847 int eth_initialize(void)
856 puts("No ethernet found.\n");
857 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
859 struct eth_device *dev = eth_devices;
860 char *ethprime = getenv("ethprime");
862 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
867 printf("%s", dev->name);
869 if (ethprime && strcmp(dev->name, ethprime) == 0) {
874 if (strchr(dev->name, ' '))
875 puts("\nWarning: eth device name has a space!"
878 eth_write_hwaddr(dev, "eth", dev->index);
882 } while (dev != eth_devices);
884 eth_current_changed();
891 #ifdef CONFIG_MCAST_TFTP
893 * mcast_addr: multicast ipaddr from which multicast Mac is made
894 * join: 1=join, 0=leave.
896 int eth_mcast_join(struct in_addr mcast_ip, int join)
899 if (!eth_current || !eth_current->mcast)
901 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
902 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
903 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
907 return eth_current->mcast(eth_current, mcast_mac, join);
910 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
911 * and this is the ethernet-crc method needed for TSEC -- and perhaps
912 * some other adapter -- hash tables
914 #define CRCPOLY_LE 0xedb88320
915 u32 ether_crc(size_t len, unsigned char const *p)
922 for (i = 0; i < 8; i++)
923 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
925 /* an reverse the bits, cuz of way they arrive -- last-first */
926 crc = (crc >> 16) | (crc << 16);
927 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
928 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
929 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
930 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
939 struct eth_device *old_current;
942 puts("No ethernet found.\n");
946 old_current = eth_current;
948 debug("Trying %s\n", eth_current->name);
950 if (eth_current->init(eth_current, gd->bd) >= 0) {
951 eth_current->state = ETH_STATE_ACTIVE;
958 } while (old_current != eth_current);
968 eth_current->halt(eth_current);
970 eth_current->state = ETH_STATE_PASSIVE;
973 int eth_is_active(struct eth_device *dev)
975 return dev && dev->state == ETH_STATE_ACTIVE;
978 int eth_send(void *packet, int length)
983 return eth_current->send(eth_current, packet, length);
991 return eth_current->recv(eth_current);
993 #endif /* ifndef CONFIG_DM_ETH */
996 static void eth_save_packet(void *packet, int length)
1001 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
1004 if (PKTSIZE < length)
1007 for (i = 0; i < length; i++)
1008 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
1010 eth_rcv_bufs[eth_rcv_last].length = length;
1011 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
1014 int eth_receive(void *packet, int length)
1017 void *pp = push_packet;
1020 if (eth_rcv_current == eth_rcv_last) {
1021 push_packet = eth_save_packet;
1025 if (eth_rcv_current == eth_rcv_last)
1029 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
1031 for (i = 0; i < length; i++)
1032 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
1034 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
1037 #endif /* CONFIG_API */
1039 static void eth_current_changed(void)
1041 char *act = getenv("ethact");
1045 * The call to eth_get_dev() below has a side effect of rotating
1046 * ethernet device if uc_priv->current == NULL. This is not what
1047 * we want when 'ethrotate' variable is 'no'.
1049 ethrotate = getenv("ethrotate");
1050 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
1053 /* update current ethernet name */
1054 if (eth_get_dev()) {
1055 if (act == NULL || strcmp(act, eth_get_name()) != 0)
1056 setenv("ethact", eth_get_name());
1059 * remove the variable completely if there is no active
1062 else if (act != NULL)
1063 setenv("ethact", NULL);
1066 void eth_try_another(int first_restart)
1068 static void *first_failed;
1072 * Do not rotate between network interfaces when
1073 * 'ethrotate' variable is set to 'no'.
1075 ethrotate = getenv("ethrotate");
1076 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
1083 first_failed = eth_get_dev();
1085 eth_set_current_to_next();
1087 eth_current_changed();
1089 if (first_failed == eth_get_dev())
1090 net_restart_wrap = 1;
1093 void eth_set_current(void)
1096 static int env_changed_id;
1099 env_id = get_env_id();
1100 if ((act == NULL) || (env_changed_id != env_id)) {
1101 act = getenv("ethact");
1102 env_changed_id = env_id;
1106 char *ethprime = getenv("ethprime");
1110 dev = eth_get_dev_by_name(ethprime);
1116 eth_set_dev(eth_get_dev_by_name(act));
1119 eth_current_changed();
1122 const char *eth_get_name(void)
1124 return eth_get_dev() ? eth_get_dev()->name : "unknown";