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+
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 #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);
184 eth_get_uclass_priv()->current = dev;
188 * Find the udevice that either has the name passed in as devname or has an
189 * alias named devname.
191 struct udevice *eth_get_dev_by_name(const char *devname)
195 const char *startp = NULL;
199 /* Must be longer than 3 to be an alias */
200 if (strlen(devname) > strlen("eth")) {
201 startp = devname + strlen("eth");
202 seq = simple_strtoul(startp, &endp, 10);
205 uclass_get(UCLASS_ETH, &uc);
206 uclass_foreach_dev(it, uc) {
208 * We need the seq to be valid, so try to probe it.
209 * If the probe fails, the seq will not match since it will be
210 * -1 instead of what we are looking for.
211 * We don't care about errors from probe here. Either they won't
212 * match an alias or it will match a literal name and we'll pick
213 * up the error when we try to probe again in eth_set_dev().
217 * Check for the name or the sequence number to match
219 if (strcmp(it->name, devname) == 0 ||
220 (endp > startp && it->seq == seq))
227 unsigned char *eth_get_ethaddr(void)
229 struct eth_pdata *pdata;
232 pdata = eth_get_dev()->platdata;
233 return pdata->enetaddr;
239 /* Set active state without calling start on the driver */
240 int eth_init_state_only(void)
242 struct udevice *current;
243 struct eth_device_priv *priv;
245 current = eth_get_dev();
246 if (!current || !device_active(current))
249 priv = current->uclass_priv;
250 priv->state = ETH_STATE_ACTIVE;
255 /* Set passive state without calling stop on the driver */
256 void eth_halt_state_only(void)
258 struct udevice *current;
259 struct eth_device_priv *priv;
261 current = eth_get_dev();
262 if (!current || !device_active(current))
265 priv = current->uclass_priv;
266 priv->state = ETH_STATE_PASSIVE;
269 int eth_get_dev_index(void)
272 return eth_get_dev()->seq;
276 static int eth_write_hwaddr(struct udevice *dev)
278 struct eth_pdata *pdata = dev->platdata;
281 if (!dev || !device_active(dev))
284 /* seq is valid since the device is active */
285 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
286 if (!is_valid_ethaddr(pdata->enetaddr)) {
287 printf("\nError: %s address %pM illegal value\n",
288 dev->name, pdata->enetaddr);
293 * Drivers are allowed to decide not to implement this at
294 * run-time. E.g. Some devices may use it and some may not.
296 ret = eth_get_ops(dev)->write_hwaddr(dev);
300 printf("\nWarning: %s failed to set MAC address\n",
307 static int on_ethaddr(const char *name, const char *value, enum env_op op,
314 /* look for an index after "eth" */
315 index = simple_strtoul(name + 3, NULL, 10);
317 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
319 struct eth_pdata *pdata = dev->platdata;
322 case env_op_overwrite:
323 eth_parse_enetaddr(value, pdata->enetaddr);
326 memset(pdata->enetaddr, 0, 6);
332 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
336 struct udevice *current;
337 struct udevice *old_current;
340 current = eth_get_dev();
342 printf("No ethernet found.\n");
346 old_current = current;
348 debug("Trying %s\n", current->name);
350 if (device_active(current)) {
351 ret = eth_get_ops(current)->start(current);
353 struct eth_device_priv *priv =
354 current->uclass_priv;
356 priv->state = ETH_STATE_ACTIVE;
366 * If ethrotate is enabled, this will change "current",
367 * otherwise we will drop out of this while loop immediately
370 /* This will ensure the new "current" attempted to probe */
371 current = eth_get_dev();
372 } while (old_current != current);
379 struct udevice *current;
380 struct eth_device_priv *priv;
382 current = eth_get_dev();
383 if (!current || !device_active(current))
386 eth_get_ops(current)->stop(current);
387 priv = current->uclass_priv;
388 priv->state = ETH_STATE_PASSIVE;
391 int eth_send(void *packet, int length)
393 struct udevice *current;
396 current = eth_get_dev();
400 if (!device_active(current))
403 ret = eth_get_ops(current)->send(current, packet, length);
405 /* We cannot completely return the error at present */
406 debug("%s: send() returned error %d\n", __func__, ret);
413 struct udevice *current;
419 current = eth_get_dev();
423 if (!device_active(current))
426 /* Process up to 32 packets at one time */
427 flags = ETH_RECV_CHECK_DEVICE;
428 for (i = 0; i < 32; i++) {
429 ret = eth_get_ops(current)->recv(current, flags, &packet);
432 net_process_received_packet(packet, ret);
433 if (ret >= 0 && eth_get_ops(current)->free_pkt)
434 eth_get_ops(current)->free_pkt(current, packet, ret);
441 /* We cannot completely return the error at present */
442 debug("%s: recv() returned error %d\n", __func__, ret);
447 int eth_initialize(void)
455 * Devices need to write the hwaddr even if not started so that Linux
456 * will have access to the hwaddr that u-boot stored for the device.
457 * This is accomplished by attempting to probe each device and calling
458 * their write_hwaddr() operation.
460 uclass_first_device(UCLASS_ETH, &dev);
462 printf("No ethernet found.\n");
463 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
465 char *ethprime = getenv("ethprime");
466 struct udevice *prime_dev = NULL;
469 prime_dev = eth_get_dev_by_name(ethprime);
471 eth_set_dev(prime_dev);
472 eth_current_changed();
477 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
482 printf("eth%d: %s", dev->seq, dev->name);
484 if (ethprime && dev == prime_dev)
487 eth_write_hwaddr(dev);
489 uclass_next_device(&dev);
499 static int eth_post_bind(struct udevice *dev)
501 if (strchr(dev->name, ' ')) {
502 printf("\nError: eth device name \"%s\" has a space!\n",
510 static int eth_pre_unbind(struct udevice *dev)
512 /* Don't hang onto a pointer that is going away */
513 if (dev == eth_get_uclass_priv()->current)
519 static int eth_post_probe(struct udevice *dev)
521 struct eth_device_priv *priv = dev->uclass_priv;
522 struct eth_pdata *pdata = dev->platdata;
523 unsigned char env_enetaddr[6];
525 priv->state = ETH_STATE_INIT;
527 /* Check if the device has a MAC address in ROM */
528 if (eth_get_ops(dev)->read_rom_hwaddr)
529 eth_get_ops(dev)->read_rom_hwaddr(dev);
531 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
532 if (!is_zero_ethaddr(env_enetaddr)) {
533 if (!is_zero_ethaddr(pdata->enetaddr) &&
534 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
535 printf("\nWarning: %s MAC addresses don't match:\n",
537 printf("Address in SROM is %pM\n",
539 printf("Address in environment is %pM\n",
543 /* Override the ROM MAC address */
544 memcpy(pdata->enetaddr, env_enetaddr, 6);
545 } else if (is_valid_ethaddr(pdata->enetaddr)) {
546 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
547 printf("\nWarning: %s using MAC address from ROM\n",
549 } else if (is_zero_ethaddr(pdata->enetaddr)) {
550 #ifdef CONFIG_NET_RANDOM_ETHADDR
551 net_random_ethaddr(pdata->enetaddr);
552 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
553 dev->name, dev->seq, pdata->enetaddr);
555 printf("\nError: %s address not set.\n",
564 static int eth_pre_remove(struct udevice *dev)
566 eth_get_ops(dev)->stop(dev);
571 UCLASS_DRIVER(eth) = {
574 .post_bind = eth_post_bind,
575 .pre_unbind = eth_pre_unbind,
576 .post_probe = eth_post_probe,
577 .pre_remove = eth_pre_remove,
578 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
579 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
580 .flags = DM_UC_FLAG_SEQ_ALIAS,
584 #ifndef CONFIG_DM_ETH
590 } eth_rcv_bufs[PKTBUFSRX];
592 static unsigned int eth_rcv_current, eth_rcv_last;
595 static struct eth_device *eth_devices;
596 struct eth_device *eth_current;
598 static void eth_set_current_to_next(void)
600 eth_current = eth_current->next;
603 static void eth_set_dev(struct eth_device *dev)
608 struct eth_device *eth_get_dev_by_name(const char *devname)
610 struct eth_device *dev, *target_dev;
612 BUG_ON(devname == NULL);
620 if (strcmp(devname, dev->name) == 0) {
625 } while (dev != eth_devices);
630 struct eth_device *eth_get_dev_by_index(int index)
632 struct eth_device *dev, *target_dev;
640 if (dev->index == index) {
645 } while (dev != eth_devices);
650 int eth_get_dev_index(void)
655 return eth_current->index;
658 static int on_ethaddr(const char *name, const char *value, enum env_op op,
662 struct eth_device *dev;
667 /* look for an index after "eth" */
668 index = simple_strtoul(name + 3, NULL, 10);
672 if (dev->index == index) {
675 case env_op_overwrite:
676 eth_parse_enetaddr(value, dev->enetaddr);
679 memset(dev->enetaddr, 0, 6);
682 } while (dev != eth_devices);
686 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
688 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
691 unsigned char env_enetaddr[6];
694 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
696 if (!is_zero_ethaddr(env_enetaddr)) {
697 if (!is_zero_ethaddr(dev->enetaddr) &&
698 memcmp(dev->enetaddr, env_enetaddr, 6)) {
699 printf("\nWarning: %s MAC addresses don't match:\n",
701 printf("Address in SROM is %pM\n",
703 printf("Address in environment is %pM\n",
707 memcpy(dev->enetaddr, env_enetaddr, 6);
708 } else if (is_valid_ethaddr(dev->enetaddr)) {
709 eth_setenv_enetaddr_by_index(base_name, eth_number,
711 printf("\nWarning: %s using MAC address from net device\n",
713 } else if (is_zero_ethaddr(dev->enetaddr)) {
714 #ifdef CONFIG_NET_RANDOM_ETHADDR
715 net_random_ethaddr(dev->enetaddr);
716 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
717 dev->name, eth_number, dev->enetaddr);
719 printf("\nError: %s address not set.\n",
725 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
726 if (!is_valid_ethaddr(dev->enetaddr)) {
727 printf("\nError: %s address %pM illegal value\n",
728 dev->name, dev->enetaddr);
732 ret = dev->write_hwaddr(dev);
734 printf("\nWarning: %s failed to set MAC address\n",
741 int eth_register(struct eth_device *dev)
743 struct eth_device *d;
746 assert(strlen(dev->name) < sizeof(dev->name));
751 eth_current_changed();
753 for (d = eth_devices; d->next != eth_devices; d = d->next)
758 dev->state = ETH_STATE_INIT;
759 dev->next = eth_devices;
760 dev->index = index++;
765 int eth_unregister(struct eth_device *dev)
767 struct eth_device *cur;
773 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
777 /* Device not found */
778 if (cur->next != dev)
781 cur->next = dev->next;
783 if (eth_devices == dev)
784 eth_devices = dev->next == eth_devices ? NULL : dev->next;
786 if (eth_current == dev) {
787 eth_current = eth_devices;
788 eth_current_changed();
794 int eth_initialize(void)
803 puts("No ethernet found.\n");
804 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
806 struct eth_device *dev = eth_devices;
807 char *ethprime = getenv("ethprime");
809 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
814 printf("%s", dev->name);
816 if (ethprime && strcmp(dev->name, ethprime) == 0) {
821 if (strchr(dev->name, ' '))
822 puts("\nWarning: eth device name has a space!"
825 eth_write_hwaddr(dev, "eth", dev->index);
829 } while (dev != eth_devices);
831 eth_current_changed();
838 #ifdef CONFIG_MCAST_TFTP
840 * mcast_addr: multicast ipaddr from which multicast Mac is made
841 * join: 1=join, 0=leave.
843 int eth_mcast_join(struct in_addr mcast_ip, int join)
846 if (!eth_current || !eth_current->mcast)
848 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
849 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
850 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
854 return eth_current->mcast(eth_current, mcast_mac, join);
857 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
858 * and this is the ethernet-crc method needed for TSEC -- and perhaps
859 * some other adapter -- hash tables
861 #define CRCPOLY_LE 0xedb88320
862 u32 ether_crc(size_t len, unsigned char const *p)
869 for (i = 0; i < 8; i++)
870 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
872 /* an reverse the bits, cuz of way they arrive -- last-first */
873 crc = (crc >> 16) | (crc << 16);
874 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
875 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
876 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
877 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
886 struct eth_device *old_current;
889 puts("No ethernet found.\n");
893 old_current = eth_current;
895 debug("Trying %s\n", eth_current->name);
897 if (eth_current->init(eth_current, gd->bd) >= 0) {
898 eth_current->state = ETH_STATE_ACTIVE;
905 } while (old_current != eth_current);
915 eth_current->halt(eth_current);
917 eth_current->state = ETH_STATE_PASSIVE;
920 int eth_send(void *packet, int length)
925 return eth_current->send(eth_current, packet, length);
933 return eth_current->recv(eth_current);
935 #endif /* ifndef CONFIG_DM_ETH */
938 static void eth_save_packet(void *packet, int length)
943 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
946 if (PKTSIZE < length)
949 for (i = 0; i < length; i++)
950 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
952 eth_rcv_bufs[eth_rcv_last].length = length;
953 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
956 int eth_receive(void *packet, int length)
959 void *pp = push_packet;
962 if (eth_rcv_current == eth_rcv_last) {
963 push_packet = eth_save_packet;
967 if (eth_rcv_current == eth_rcv_last)
971 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
973 for (i = 0; i < length; i++)
974 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
976 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
979 #endif /* CONFIG_API */
981 static void eth_current_changed(void)
983 char *act = getenv("ethact");
984 /* update current ethernet name */
986 if (act == NULL || strcmp(act, eth_get_name()) != 0)
987 setenv("ethact", eth_get_name());
990 * remove the variable completely if there is no active
993 else if (act != NULL)
994 setenv("ethact", NULL);
997 void eth_try_another(int first_restart)
999 static void *first_failed;
1003 * Do not rotate between network interfaces when
1004 * 'ethrotate' variable is set to 'no'.
1006 ethrotate = getenv("ethrotate");
1007 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
1014 first_failed = eth_get_dev();
1016 eth_set_current_to_next();
1018 eth_current_changed();
1020 if (first_failed == eth_get_dev())
1021 net_restart_wrap = 1;
1024 void eth_set_current(void)
1027 static int env_changed_id;
1030 env_id = get_env_id();
1031 if ((act == NULL) || (env_changed_id != env_id)) {
1032 act = getenv("ethact");
1033 env_changed_id = env_id;
1037 char *ethprime = getenv("ethprime");
1041 dev = eth_get_dev_by_name(ethprime);
1047 eth_set_dev(eth_get_dev_by_name(act));
1050 eth_current_changed();
1053 const char *eth_get_name(void)
1055 return eth_get_dev() ? eth_get_dev()->name : "unknown";