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_ethaddr(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 int eth_mac_skip(int index)
68 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
69 skip_state = getenv(enetvar);
70 return skip_state != NULL;
73 static void eth_current_changed(void);
76 * CPU and board-specific Ethernet initializations. Aliased function
77 * signals caller to move on
79 static int __def_eth_init(bd_t *bis)
83 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
84 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
86 static void eth_common_init(void)
88 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
89 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
98 * If board-specific initialization exists, call it.
99 * If not, call a CPU-specific one
101 if (board_eth_init != __def_eth_init) {
102 if (board_eth_init(gd->bd) < 0)
103 printf("Board Net Initialization Failed\n");
104 } else if (cpu_eth_init != __def_eth_init) {
105 if (cpu_eth_init(gd->bd) < 0)
106 printf("CPU Net Initialization Failed\n");
108 printf("Net Initialization Skipped\n");
114 * struct eth_device_priv - private structure for each Ethernet device
116 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
118 struct eth_device_priv {
119 enum eth_state_t state;
123 * struct eth_uclass_priv - The structure attached to the uclass itself
125 * @current: The Ethernet device that the network functions are using
127 struct eth_uclass_priv {
128 struct udevice *current;
131 /* eth_errno - This stores the most recent failure code from DM functions */
132 static int eth_errno;
134 static struct eth_uclass_priv *eth_get_uclass_priv(void)
138 uclass_get(UCLASS_ETH, &uc);
143 static void eth_set_current_to_next(void)
145 struct eth_uclass_priv *uc_priv;
147 uc_priv = eth_get_uclass_priv();
148 if (uc_priv->current)
149 uclass_next_device(&uc_priv->current);
150 if (!uc_priv->current)
151 uclass_first_device(UCLASS_ETH, &uc_priv->current);
155 * Typically this will simply return the active device.
156 * In the case where the most recent active device was unset, this will attempt
157 * to return the first device. If that device doesn't exist or fails to probe,
158 * this function will return NULL.
160 struct udevice *eth_get_dev(void)
162 struct eth_uclass_priv *uc_priv;
164 uc_priv = eth_get_uclass_priv();
165 if (!uc_priv->current)
166 eth_errno = uclass_first_device(UCLASS_ETH,
168 return uc_priv->current;
172 * Typically this will just store a device pointer.
173 * In case it was not probed, we will attempt to do so.
174 * dev may be NULL to unset the active device.
176 static void eth_set_dev(struct udevice *dev)
178 if (dev && !device_active(dev))
179 eth_errno = device_probe(dev);
180 eth_get_uclass_priv()->current = dev;
184 * Find the udevice that either has the name passed in as devname or has an
185 * alias named devname.
187 struct udevice *eth_get_dev_by_name(const char *devname)
191 const char *startp = NULL;
195 /* Must be longer than 3 to be an alias */
196 if (strlen(devname) > strlen("eth")) {
197 startp = devname + strlen("eth");
198 seq = simple_strtoul(startp, &endp, 10);
201 uclass_get(UCLASS_ETH, &uc);
202 uclass_foreach_dev(it, uc) {
204 * We need the seq to be valid, so try to probe it.
205 * If the probe fails, the seq will not match since it will be
206 * -1 instead of what we are looking for.
207 * We don't care about errors from probe here. Either they won't
208 * match an alias or it will match a literal name and we'll pick
209 * up the error when we try to probe again in eth_set_dev().
213 * Check for the name or the sequence number to match
215 if (strcmp(it->name, devname) == 0 ||
216 (endp > startp && it->seq == seq))
223 unsigned char *eth_get_ethaddr(void)
225 struct eth_pdata *pdata;
228 pdata = eth_get_dev()->platdata;
229 return pdata->enetaddr;
235 /* Set active state without calling start on the driver */
236 int eth_init_state_only(void)
238 struct udevice *current;
239 struct eth_device_priv *priv;
241 current = eth_get_dev();
242 if (!current || !device_active(current))
245 priv = current->uclass_priv;
246 priv->state = ETH_STATE_ACTIVE;
251 /* Set passive state without calling stop on the driver */
252 void eth_halt_state_only(void)
254 struct udevice *current;
255 struct eth_device_priv *priv;
257 current = eth_get_dev();
258 if (!current || !device_active(current))
261 priv = current->uclass_priv;
262 priv->state = ETH_STATE_PASSIVE;
265 int eth_get_dev_index(void)
268 return eth_get_dev()->seq;
272 static int eth_write_hwaddr(struct udevice *dev)
274 struct eth_pdata *pdata = dev->platdata;
277 if (!dev || !device_active(dev))
280 /* seq is valid since the device is active */
281 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
282 if (!is_valid_ethaddr(pdata->enetaddr)) {
283 printf("\nError: %s address %pM illegal value\n",
284 dev->name, pdata->enetaddr);
288 ret = eth_get_ops(dev)->write_hwaddr(dev);
290 printf("\nWarning: %s failed to set MAC address\n",
299 struct udevice *current;
300 struct udevice *old_current;
303 current = eth_get_dev();
305 printf("No ethernet found.\n");
309 old_current = current;
311 debug("Trying %s\n", current->name);
313 if (device_active(current)) {
314 uchar env_enetaddr[6];
315 struct eth_pdata *pdata = current->platdata;
316 int enetaddr_changed = 0;
318 /* Sync environment with network device */
319 if (eth_getenv_enetaddr_by_index("eth", current->seq,
321 enetaddr_changed = memcmp(pdata->enetaddr,
323 memcpy(pdata->enetaddr, env_enetaddr, 6);
325 memset(env_enetaddr, 0, 6);
326 enetaddr_changed = memcmp(pdata->enetaddr,
328 memset(pdata->enetaddr, 0, 6);
330 if (enetaddr_changed)
331 eth_write_hwaddr(current);
333 ret = eth_get_ops(current)->start(current);
335 struct eth_device_priv *priv =
336 current->uclass_priv;
338 priv->state = ETH_STATE_ACTIVE;
348 * If ethrotate is enabled, this will change "current",
349 * otherwise we will drop out of this while loop immediately
352 /* This will ensure the new "current" attempted to probe */
353 current = eth_get_dev();
354 } while (old_current != current);
361 struct udevice *current;
362 struct eth_device_priv *priv;
364 current = eth_get_dev();
365 if (!current || !device_active(current))
368 eth_get_ops(current)->stop(current);
369 priv = current->uclass_priv;
370 priv->state = ETH_STATE_PASSIVE;
373 int eth_send(void *packet, int length)
375 struct udevice *current;
378 current = eth_get_dev();
382 if (!device_active(current))
385 ret = eth_get_ops(current)->send(current, packet, length);
387 /* We cannot completely return the error at present */
388 debug("%s: send() returned error %d\n", __func__, ret);
395 struct udevice *current;
400 current = eth_get_dev();
404 if (!device_active(current))
407 /* Process up to 32 packets at one time */
408 for (i = 0; i < 32; i++) {
409 ret = eth_get_ops(current)->recv(current, &packet);
411 net_process_received_packet(packet, ret);
412 if (ret >= 0 && eth_get_ops(current)->free_pkt)
413 eth_get_ops(current)->free_pkt(current, packet, ret);
420 /* We cannot completely return the error at present */
421 debug("%s: recv() returned error %d\n", __func__, ret);
426 int eth_initialize(void)
434 * Devices need to write the hwaddr even if not started so that Linux
435 * will have access to the hwaddr that u-boot stored for the device.
436 * This is accomplished by attempting to probe each device and calling
437 * their write_hwaddr() operation.
439 uclass_first_device(UCLASS_ETH, &dev);
441 printf("No ethernet found.\n");
442 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
444 char *ethprime = getenv("ethprime");
445 struct udevice *prime_dev = NULL;
448 prime_dev = eth_get_dev_by_name(ethprime);
450 eth_set_dev(prime_dev);
451 eth_current_changed();
456 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
461 printf("eth%d: %s", dev->seq, dev->name);
463 if (ethprime && dev == prime_dev)
466 eth_write_hwaddr(dev);
468 uclass_next_device(&dev);
478 static int eth_post_bind(struct udevice *dev)
480 if (strchr(dev->name, ' ')) {
481 printf("\nError: eth device name \"%s\" has a space!\n",
489 static int eth_pre_unbind(struct udevice *dev)
491 /* Don't hang onto a pointer that is going away */
492 if (dev == eth_get_uclass_priv()->current)
498 static int eth_post_probe(struct udevice *dev)
500 struct eth_device_priv *priv = dev->uclass_priv;
501 struct eth_pdata *pdata = dev->platdata;
502 unsigned char env_enetaddr[6];
504 priv->state = ETH_STATE_INIT;
506 /* Check if the device has a MAC address in ROM */
507 if (eth_get_ops(dev)->read_rom_hwaddr)
508 eth_get_ops(dev)->read_rom_hwaddr(dev);
510 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
511 if (!is_zero_ethaddr(env_enetaddr)) {
512 if (!is_zero_ethaddr(pdata->enetaddr) &&
513 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
514 printf("\nWarning: %s MAC addresses don't match:\n",
516 printf("Address in SROM is %pM\n",
518 printf("Address in environment is %pM\n",
522 /* Override the ROM MAC address */
523 memcpy(pdata->enetaddr, env_enetaddr, 6);
524 } else if (is_valid_ethaddr(pdata->enetaddr)) {
525 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
526 printf("\nWarning: %s using MAC address from ROM\n",
528 } else if (is_zero_ethaddr(pdata->enetaddr)) {
529 #ifdef CONFIG_NET_RANDOM_ETHADDR
530 net_random_ethaddr(pdata->enetaddr);
531 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
532 dev->name, dev->seq, pdata->enetaddr);
534 printf("\nError: %s address not set.\n",
543 static int eth_pre_remove(struct udevice *dev)
545 eth_get_ops(dev)->stop(dev);
550 UCLASS_DRIVER(eth) = {
553 .post_bind = eth_post_bind,
554 .pre_unbind = eth_pre_unbind,
555 .post_probe = eth_post_probe,
556 .pre_remove = eth_pre_remove,
557 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
558 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
559 .flags = DM_UC_FLAG_SEQ_ALIAS,
563 #ifndef CONFIG_DM_ETH
569 } eth_rcv_bufs[PKTBUFSRX];
571 static unsigned int eth_rcv_current, eth_rcv_last;
574 static struct eth_device *eth_devices;
575 struct eth_device *eth_current;
577 static void eth_set_current_to_next(void)
579 eth_current = eth_current->next;
582 static void eth_set_dev(struct eth_device *dev)
587 struct eth_device *eth_get_dev_by_name(const char *devname)
589 struct eth_device *dev, *target_dev;
591 BUG_ON(devname == NULL);
599 if (strcmp(devname, dev->name) == 0) {
604 } while (dev != eth_devices);
609 struct eth_device *eth_get_dev_by_index(int index)
611 struct eth_device *dev, *target_dev;
619 if (dev->index == index) {
624 } while (dev != eth_devices);
629 int eth_get_dev_index(void)
634 return eth_current->index;
637 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
640 unsigned char env_enetaddr[6];
643 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
645 if (!is_zero_ethaddr(env_enetaddr)) {
646 if (!is_zero_ethaddr(dev->enetaddr) &&
647 memcmp(dev->enetaddr, env_enetaddr, 6)) {
648 printf("\nWarning: %s MAC addresses don't match:\n",
650 printf("Address in SROM is %pM\n",
652 printf("Address in environment is %pM\n",
656 memcpy(dev->enetaddr, env_enetaddr, 6);
657 } else if (is_valid_ethaddr(dev->enetaddr)) {
658 eth_setenv_enetaddr_by_index(base_name, eth_number,
660 printf("\nWarning: %s using MAC address from net device\n",
662 } else if (is_zero_ethaddr(dev->enetaddr)) {
663 #ifdef CONFIG_NET_RANDOM_ETHADDR
664 net_random_ethaddr(dev->enetaddr);
665 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
666 dev->name, eth_number, dev->enetaddr);
668 printf("\nError: %s address not set.\n",
674 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
675 if (!is_valid_ethaddr(dev->enetaddr)) {
676 printf("\nError: %s address %pM illegal value\n",
677 dev->name, dev->enetaddr);
681 ret = dev->write_hwaddr(dev);
683 printf("\nWarning: %s failed to set MAC address\n",
690 int eth_register(struct eth_device *dev)
692 struct eth_device *d;
695 assert(strlen(dev->name) < sizeof(dev->name));
700 eth_current_changed();
702 for (d = eth_devices; d->next != eth_devices; d = d->next)
707 dev->state = ETH_STATE_INIT;
708 dev->next = eth_devices;
709 dev->index = index++;
714 int eth_unregister(struct eth_device *dev)
716 struct eth_device *cur;
722 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
726 /* Device not found */
727 if (cur->next != dev)
730 cur->next = dev->next;
732 if (eth_devices == dev)
733 eth_devices = dev->next == eth_devices ? NULL : dev->next;
735 if (eth_current == dev) {
736 eth_current = eth_devices;
737 eth_current_changed();
743 int eth_initialize(void)
752 puts("No ethernet found.\n");
753 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
755 struct eth_device *dev = eth_devices;
756 char *ethprime = getenv("ethprime");
758 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
763 printf("%s", dev->name);
765 if (ethprime && strcmp(dev->name, ethprime) == 0) {
770 if (strchr(dev->name, ' '))
771 puts("\nWarning: eth device name has a space!"
774 eth_write_hwaddr(dev, "eth", dev->index);
778 } while (dev != eth_devices);
780 eth_current_changed();
787 #ifdef CONFIG_MCAST_TFTP
789 * mcast_addr: multicast ipaddr from which multicast Mac is made
790 * join: 1=join, 0=leave.
792 int eth_mcast_join(struct in_addr mcast_ip, int join)
795 if (!eth_current || !eth_current->mcast)
797 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
798 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
799 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
803 return eth_current->mcast(eth_current, mcast_mac, join);
806 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
807 * and this is the ethernet-crc method needed for TSEC -- and perhaps
808 * some other adapter -- hash tables
810 #define CRCPOLY_LE 0xedb88320
811 u32 ether_crc(size_t len, unsigned char const *p)
818 for (i = 0; i < 8; i++)
819 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
821 /* an reverse the bits, cuz of way they arrive -- last-first */
822 crc = (crc >> 16) | (crc << 16);
823 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
824 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
825 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
826 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
835 struct eth_device *old_current, *dev;
838 puts("No ethernet found.\n");
842 /* Sync environment with network devices */
845 uchar env_enetaddr[6];
846 int enetaddr_changed = 0;
848 if (eth_getenv_enetaddr_by_index("eth", dev->index,
850 enetaddr_changed = memcmp(dev->enetaddr,
852 memcpy(dev->enetaddr, env_enetaddr, 6);
854 memset(env_enetaddr, 0, 6);
855 enetaddr_changed = memcmp(dev->enetaddr,
857 memset(dev->enetaddr, 0, 6);
859 if (enetaddr_changed)
860 eth_write_hwaddr(dev, "eth", dev->index);
863 } while (dev != eth_devices);
865 old_current = eth_current;
867 debug("Trying %s\n", eth_current->name);
869 if (eth_current->init(eth_current, gd->bd) >= 0) {
870 eth_current->state = ETH_STATE_ACTIVE;
877 } while (old_current != eth_current);
887 eth_current->halt(eth_current);
889 eth_current->state = ETH_STATE_PASSIVE;
892 int eth_send(void *packet, int length)
897 return eth_current->send(eth_current, packet, length);
905 return eth_current->recv(eth_current);
907 #endif /* ifndef CONFIG_DM_ETH */
910 static void eth_save_packet(void *packet, int length)
915 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
918 if (PKTSIZE < length)
921 for (i = 0; i < length; i++)
922 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
924 eth_rcv_bufs[eth_rcv_last].length = length;
925 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
928 int eth_receive(void *packet, int length)
931 void *pp = push_packet;
934 if (eth_rcv_current == eth_rcv_last) {
935 push_packet = eth_save_packet;
939 if (eth_rcv_current == eth_rcv_last)
943 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
945 for (i = 0; i < length; i++)
946 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
948 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
951 #endif /* CONFIG_API */
953 static void eth_current_changed(void)
955 char *act = getenv("ethact");
956 /* update current ethernet name */
958 if (act == NULL || strcmp(act, eth_get_name()) != 0)
959 setenv("ethact", eth_get_name());
962 * remove the variable completely if there is no active
965 else if (act != NULL)
966 setenv("ethact", NULL);
969 void eth_try_another(int first_restart)
971 static void *first_failed;
975 * Do not rotate between network interfaces when
976 * 'ethrotate' variable is set to 'no'.
978 ethrotate = getenv("ethrotate");
979 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
986 first_failed = eth_get_dev();
988 eth_set_current_to_next();
990 eth_current_changed();
992 if (first_failed == eth_get_dev())
993 net_restart_wrap = 1;
996 void eth_set_current(void)
999 static int env_changed_id;
1002 env_id = get_env_id();
1003 if ((act == NULL) || (env_changed_id != env_id)) {
1004 act = getenv("ethact");
1005 env_changed_id = env_id;
1009 char *ethprime = getenv("ethprime");
1013 dev = eth_get_dev_by_name(ethprime);
1019 eth_set_dev(eth_get_dev_by_name(act));
1022 eth_current_changed();
1025 const char *eth_get_name(void)
1027 return eth_get_dev() ? eth_get_dev()->name : "unknown";