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(char *name, uchar *enetaddr)
36 eth_parse_enetaddr(getenv(name), enetaddr);
37 return is_valid_ethaddr(enetaddr);
40 int eth_setenv_enetaddr(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 printf("Net Initialization Skipped\n");
116 * struct eth_device_priv - private structure for each Ethernet device
118 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
120 struct eth_device_priv {
121 enum eth_state_t state;
125 * struct eth_uclass_priv - The structure attached to the uclass itself
127 * @current: The Ethernet device that the network functions are using
129 struct eth_uclass_priv {
130 struct udevice *current;
133 /* eth_errno - This stores the most recent failure code from DM functions */
134 static int eth_errno;
136 static struct eth_uclass_priv *eth_get_uclass_priv(void)
140 uclass_get(UCLASS_ETH, &uc);
145 static void eth_set_current_to_next(void)
147 struct eth_uclass_priv *uc_priv;
149 uc_priv = eth_get_uclass_priv();
150 if (uc_priv->current)
151 uclass_next_device(&uc_priv->current);
152 if (!uc_priv->current)
153 uclass_first_device(UCLASS_ETH, &uc_priv->current);
157 * Typically this will simply return the active device.
158 * In the case where the most recent active device was unset, this will attempt
159 * to return the first device. If that device doesn't exist or fails to probe,
160 * this function will return NULL.
162 struct udevice *eth_get_dev(void)
164 struct eth_uclass_priv *uc_priv;
166 uc_priv = eth_get_uclass_priv();
167 if (!uc_priv->current)
168 eth_errno = uclass_first_device(UCLASS_ETH,
170 return uc_priv->current;
174 * Typically this will just store a device pointer.
175 * In case it was not probed, we will attempt to do so.
176 * dev may be NULL to unset the active device.
178 static void eth_set_dev(struct udevice *dev)
180 if (dev && !device_active(dev))
181 eth_errno = device_probe(dev);
182 eth_get_uclass_priv()->current = dev;
186 * Find the udevice that either has the name passed in as devname or has an
187 * alias named devname.
189 struct udevice *eth_get_dev_by_name(const char *devname)
193 const char *startp = NULL;
197 /* Must be longer than 3 to be an alias */
198 if (strlen(devname) > strlen("eth")) {
199 startp = devname + strlen("eth");
200 seq = simple_strtoul(startp, &endp, 10);
203 uclass_get(UCLASS_ETH, &uc);
204 uclass_foreach_dev(it, uc) {
206 * We need the seq to be valid, so try to probe it.
207 * If the probe fails, the seq will not match since it will be
208 * -1 instead of what we are looking for.
209 * We don't care about errors from probe here. Either they won't
210 * match an alias or it will match a literal name and we'll pick
211 * up the error when we try to probe again in eth_set_dev().
215 * Check for the name or the sequence number to match
217 if (strcmp(it->name, devname) == 0 ||
218 (endp > startp && it->seq == seq))
225 unsigned char *eth_get_ethaddr(void)
227 struct eth_pdata *pdata;
230 pdata = eth_get_dev()->platdata;
231 return pdata->enetaddr;
237 /* Set active state without calling start on the driver */
238 int eth_init_state_only(void)
240 struct udevice *current;
241 struct eth_device_priv *priv;
243 current = eth_get_dev();
244 if (!current || !device_active(current))
247 priv = current->uclass_priv;
248 priv->state = ETH_STATE_ACTIVE;
253 /* Set passive state without calling stop on the driver */
254 void eth_halt_state_only(void)
256 struct udevice *current;
257 struct eth_device_priv *priv;
259 current = eth_get_dev();
260 if (!current || !device_active(current))
263 priv = current->uclass_priv;
264 priv->state = ETH_STATE_PASSIVE;
267 int eth_get_dev_index(void)
270 return eth_get_dev()->seq;
274 static int eth_write_hwaddr(struct udevice *dev)
276 struct eth_pdata *pdata = dev->platdata;
279 if (!dev || !device_active(dev))
282 /* seq is valid since the device is active */
283 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
284 if (!is_valid_ethaddr(pdata->enetaddr)) {
285 printf("\nError: %s address %pM illegal value\n",
286 dev->name, pdata->enetaddr);
291 * Drivers are allowed to decide not to implement this at
292 * run-time. E.g. Some devices may use it and some may not.
294 ret = eth_get_ops(dev)->write_hwaddr(dev);
298 printf("\nWarning: %s failed to set MAC address\n",
305 static int on_ethaddr(const char *name, const char *value, enum env_op op,
312 /* look for an index after "eth" */
313 index = simple_strtoul(name + 3, NULL, 10);
315 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
317 struct eth_pdata *pdata = dev->platdata;
320 case env_op_overwrite:
321 eth_parse_enetaddr(value, pdata->enetaddr);
324 memset(pdata->enetaddr, 0, 6);
330 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
334 struct udevice *current;
335 struct udevice *old_current;
338 current = eth_get_dev();
340 printf("No ethernet found.\n");
344 old_current = current;
346 debug("Trying %s\n", current->name);
348 if (device_active(current)) {
349 ret = eth_get_ops(current)->start(current);
351 struct eth_device_priv *priv =
352 current->uclass_priv;
354 priv->state = ETH_STATE_ACTIVE;
364 * If ethrotate is enabled, this will change "current",
365 * otherwise we will drop out of this while loop immediately
368 /* This will ensure the new "current" attempted to probe */
369 current = eth_get_dev();
370 } while (old_current != current);
377 struct udevice *current;
378 struct eth_device_priv *priv;
380 current = eth_get_dev();
381 if (!current || !device_active(current))
384 eth_get_ops(current)->stop(current);
385 priv = current->uclass_priv;
386 priv->state = ETH_STATE_PASSIVE;
389 int eth_send(void *packet, int length)
391 struct udevice *current;
394 current = eth_get_dev();
398 if (!device_active(current))
401 ret = eth_get_ops(current)->send(current, packet, length);
403 /* We cannot completely return the error at present */
404 debug("%s: send() returned error %d\n", __func__, ret);
411 struct udevice *current;
417 current = eth_get_dev();
421 if (!device_active(current))
424 /* Process up to 32 packets at one time */
425 flags = ETH_RECV_CHECK_DEVICE;
426 for (i = 0; i < 32; i++) {
427 ret = eth_get_ops(current)->recv(current, flags, &packet);
430 net_process_received_packet(packet, ret);
431 if (ret >= 0 && eth_get_ops(current)->free_pkt)
432 eth_get_ops(current)->free_pkt(current, packet, ret);
439 /* We cannot completely return the error at present */
440 debug("%s: recv() returned error %d\n", __func__, ret);
445 int eth_initialize(void)
453 * Devices need to write the hwaddr even if not started so that Linux
454 * will have access to the hwaddr that u-boot stored for the device.
455 * This is accomplished by attempting to probe each device and calling
456 * their write_hwaddr() operation.
458 uclass_first_device(UCLASS_ETH, &dev);
460 printf("No ethernet found.\n");
461 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
463 char *ethprime = getenv("ethprime");
464 struct udevice *prime_dev = NULL;
467 prime_dev = eth_get_dev_by_name(ethprime);
469 eth_set_dev(prime_dev);
470 eth_current_changed();
475 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
480 printf("eth%d: %s", dev->seq, dev->name);
482 if (ethprime && dev == prime_dev)
485 eth_write_hwaddr(dev);
487 uclass_next_device(&dev);
497 static int eth_post_bind(struct udevice *dev)
499 if (strchr(dev->name, ' ')) {
500 printf("\nError: eth device name \"%s\" has a space!\n",
508 static int eth_pre_unbind(struct udevice *dev)
510 /* Don't hang onto a pointer that is going away */
511 if (dev == eth_get_uclass_priv()->current)
517 static int eth_post_probe(struct udevice *dev)
519 struct eth_device_priv *priv = dev->uclass_priv;
520 struct eth_pdata *pdata = dev->platdata;
521 unsigned char env_enetaddr[6];
523 priv->state = ETH_STATE_INIT;
525 /* Check if the device has a MAC address in ROM */
526 if (eth_get_ops(dev)->read_rom_hwaddr)
527 eth_get_ops(dev)->read_rom_hwaddr(dev);
529 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
530 if (!is_zero_ethaddr(env_enetaddr)) {
531 if (!is_zero_ethaddr(pdata->enetaddr) &&
532 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
533 printf("\nWarning: %s MAC addresses don't match:\n",
535 printf("Address in SROM is %pM\n",
537 printf("Address in environment is %pM\n",
541 /* Override the ROM MAC address */
542 memcpy(pdata->enetaddr, env_enetaddr, 6);
543 } else if (is_valid_ethaddr(pdata->enetaddr)) {
544 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
545 printf("\nWarning: %s using MAC address from ROM\n",
547 } else if (is_zero_ethaddr(pdata->enetaddr)) {
548 #ifdef CONFIG_NET_RANDOM_ETHADDR
549 net_random_ethaddr(pdata->enetaddr);
550 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
551 dev->name, dev->seq, pdata->enetaddr);
553 printf("\nError: %s address not set.\n",
562 static int eth_pre_remove(struct udevice *dev)
564 eth_get_ops(dev)->stop(dev);
569 UCLASS_DRIVER(eth) = {
572 .post_bind = eth_post_bind,
573 .pre_unbind = eth_pre_unbind,
574 .post_probe = eth_post_probe,
575 .pre_remove = eth_pre_remove,
576 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
577 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
578 .flags = DM_UC_FLAG_SEQ_ALIAS,
582 #ifndef CONFIG_DM_ETH
588 } eth_rcv_bufs[PKTBUFSRX];
590 static unsigned int eth_rcv_current, eth_rcv_last;
593 static struct eth_device *eth_devices;
594 struct eth_device *eth_current;
596 static void eth_set_current_to_next(void)
598 eth_current = eth_current->next;
601 static void eth_set_dev(struct eth_device *dev)
606 struct eth_device *eth_get_dev_by_name(const char *devname)
608 struct eth_device *dev, *target_dev;
610 BUG_ON(devname == NULL);
618 if (strcmp(devname, dev->name) == 0) {
623 } while (dev != eth_devices);
628 struct eth_device *eth_get_dev_by_index(int index)
630 struct eth_device *dev, *target_dev;
638 if (dev->index == index) {
643 } while (dev != eth_devices);
648 int eth_get_dev_index(void)
653 return eth_current->index;
656 static int on_ethaddr(const char *name, const char *value, enum env_op op,
660 struct eth_device *dev;
665 /* look for an index after "eth" */
666 index = simple_strtoul(name + 3, NULL, 10);
670 if (dev->index == index) {
673 case env_op_overwrite:
674 eth_parse_enetaddr(value, dev->enetaddr);
677 memset(dev->enetaddr, 0, 6);
680 } while (dev != eth_devices);
684 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
686 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
689 unsigned char env_enetaddr[6];
692 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
694 if (!is_zero_ethaddr(env_enetaddr)) {
695 if (!is_zero_ethaddr(dev->enetaddr) &&
696 memcmp(dev->enetaddr, env_enetaddr, 6)) {
697 printf("\nWarning: %s MAC addresses don't match:\n",
699 printf("Address in SROM is %pM\n",
701 printf("Address in environment is %pM\n",
705 memcpy(dev->enetaddr, env_enetaddr, 6);
706 } else if (is_valid_ethaddr(dev->enetaddr)) {
707 eth_setenv_enetaddr_by_index(base_name, eth_number,
709 printf("\nWarning: %s using MAC address from net device\n",
711 } else if (is_zero_ethaddr(dev->enetaddr)) {
712 #ifdef CONFIG_NET_RANDOM_ETHADDR
713 net_random_ethaddr(dev->enetaddr);
714 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
715 dev->name, eth_number, dev->enetaddr);
717 printf("\nError: %s address not set.\n",
723 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
724 if (!is_valid_ethaddr(dev->enetaddr)) {
725 printf("\nError: %s address %pM illegal value\n",
726 dev->name, dev->enetaddr);
730 ret = dev->write_hwaddr(dev);
732 printf("\nWarning: %s failed to set MAC address\n",
739 int eth_register(struct eth_device *dev)
741 struct eth_device *d;
744 assert(strlen(dev->name) < sizeof(dev->name));
749 eth_current_changed();
751 for (d = eth_devices; d->next != eth_devices; d = d->next)
756 dev->state = ETH_STATE_INIT;
757 dev->next = eth_devices;
758 dev->index = index++;
763 int eth_unregister(struct eth_device *dev)
765 struct eth_device *cur;
771 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
775 /* Device not found */
776 if (cur->next != dev)
779 cur->next = dev->next;
781 if (eth_devices == dev)
782 eth_devices = dev->next == eth_devices ? NULL : dev->next;
784 if (eth_current == dev) {
785 eth_current = eth_devices;
786 eth_current_changed();
792 int eth_initialize(void)
801 puts("No ethernet found.\n");
802 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
804 struct eth_device *dev = eth_devices;
805 char *ethprime = getenv("ethprime");
807 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
812 printf("%s", dev->name);
814 if (ethprime && strcmp(dev->name, ethprime) == 0) {
819 if (strchr(dev->name, ' '))
820 puts("\nWarning: eth device name has a space!"
823 eth_write_hwaddr(dev, "eth", dev->index);
827 } while (dev != eth_devices);
829 eth_current_changed();
836 #ifdef CONFIG_MCAST_TFTP
838 * mcast_addr: multicast ipaddr from which multicast Mac is made
839 * join: 1=join, 0=leave.
841 int eth_mcast_join(struct in_addr mcast_ip, int join)
844 if (!eth_current || !eth_current->mcast)
846 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
847 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
848 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
852 return eth_current->mcast(eth_current, mcast_mac, join);
855 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
856 * and this is the ethernet-crc method needed for TSEC -- and perhaps
857 * some other adapter -- hash tables
859 #define CRCPOLY_LE 0xedb88320
860 u32 ether_crc(size_t len, unsigned char const *p)
867 for (i = 0; i < 8; i++)
868 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
870 /* an reverse the bits, cuz of way they arrive -- last-first */
871 crc = (crc >> 16) | (crc << 16);
872 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
873 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
874 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
875 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
884 struct eth_device *old_current;
887 puts("No ethernet found.\n");
891 old_current = eth_current;
893 debug("Trying %s\n", eth_current->name);
895 if (eth_current->init(eth_current, gd->bd) >= 0) {
896 eth_current->state = ETH_STATE_ACTIVE;
903 } while (old_current != eth_current);
913 eth_current->halt(eth_current);
915 eth_current->state = ETH_STATE_PASSIVE;
918 int eth_send(void *packet, int length)
923 return eth_current->send(eth_current, packet, length);
931 return eth_current->recv(eth_current);
933 #endif /* ifndef CONFIG_DM_ETH */
936 static void eth_save_packet(void *packet, int length)
941 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
944 if (PKTSIZE < length)
947 for (i = 0; i < length; i++)
948 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
950 eth_rcv_bufs[eth_rcv_last].length = length;
951 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
954 int eth_receive(void *packet, int length)
957 void *pp = push_packet;
960 if (eth_rcv_current == eth_rcv_last) {
961 push_packet = eth_save_packet;
965 if (eth_rcv_current == eth_rcv_last)
969 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
971 for (i = 0; i < length; i++)
972 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
974 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
977 #endif /* CONFIG_API */
979 static void eth_current_changed(void)
981 char *act = getenv("ethact");
982 /* update current ethernet name */
984 if (act == NULL || strcmp(act, eth_get_name()) != 0)
985 setenv("ethact", eth_get_name());
988 * remove the variable completely if there is no active
991 else if (act != NULL)
992 setenv("ethact", NULL);
995 void eth_try_another(int first_restart)
997 static void *first_failed;
1001 * Do not rotate between network interfaces when
1002 * 'ethrotate' variable is set to 'no'.
1004 ethrotate = getenv("ethrotate");
1005 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
1012 first_failed = eth_get_dev();
1014 eth_set_current_to_next();
1016 eth_current_changed();
1018 if (first_failed == eth_get_dev())
1019 net_restart_wrap = 1;
1022 void eth_set_current(void)
1025 static int env_changed_id;
1028 env_id = get_env_id();
1029 if ((act == NULL) || (env_changed_id != env_id)) {
1030 act = getenv("ethact");
1031 env_changed_id = env_id;
1035 char *ethprime = getenv("ethprime");
1039 dev = eth_get_dev_by_name(ethprime);
1045 eth_set_dev(eth_get_dev_by_name(act));
1048 eth_current_changed();
1051 const char *eth_get_name(void)
1053 return eth_get_dev() ? eth_get_dev()->name : "unknown";