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 void eth_env_init(void)
67 s = getenv("bootfile");
69 copy_filename(net_boot_file_name, s,
70 sizeof(net_boot_file_name));
73 static int eth_mac_skip(int index)
78 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
79 skip_state = getenv(enetvar);
80 return skip_state != NULL;
83 static void eth_current_changed(void);
86 * CPU and board-specific Ethernet initializations. Aliased function
87 * signals caller to move on
89 static int __def_eth_init(bd_t *bis)
93 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
94 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
96 static void eth_common_init(void)
98 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
99 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
110 * If board-specific initialization exists, call it.
111 * If not, call a CPU-specific one
113 if (board_eth_init != __def_eth_init) {
114 if (board_eth_init(gd->bd) < 0)
115 printf("Board Net Initialization Failed\n");
116 } else if (cpu_eth_init != __def_eth_init) {
117 if (cpu_eth_init(gd->bd) < 0)
118 printf("CPU Net Initialization Failed\n");
120 printf("Net Initialization Skipped\n");
126 * struct eth_device_priv - private structure for each Ethernet device
128 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
130 struct eth_device_priv {
131 enum eth_state_t state;
135 * struct eth_uclass_priv - The structure attached to the uclass itself
137 * @current: The Ethernet device that the network functions are using
139 struct eth_uclass_priv {
140 struct udevice *current;
143 /* eth_errno - This stores the most recent failure code from DM functions */
144 static int eth_errno;
146 static struct eth_uclass_priv *eth_get_uclass_priv(void)
150 uclass_get(UCLASS_ETH, &uc);
155 static void eth_set_current_to_next(void)
157 struct eth_uclass_priv *uc_priv;
159 uc_priv = eth_get_uclass_priv();
160 if (uc_priv->current)
161 uclass_next_device(&uc_priv->current);
162 if (!uc_priv->current)
163 uclass_first_device(UCLASS_ETH, &uc_priv->current);
167 * Typically this will simply return the active device.
168 * In the case where the most recent active device was unset, this will attempt
169 * to return the first device. If that device doesn't exist or fails to probe,
170 * this function will return NULL.
172 struct udevice *eth_get_dev(void)
174 struct eth_uclass_priv *uc_priv;
176 uc_priv = eth_get_uclass_priv();
177 if (!uc_priv->current)
178 eth_errno = uclass_first_device(UCLASS_ETH,
180 return uc_priv->current;
184 * Typically this will just store a device pointer.
185 * In case it was not probed, we will attempt to do so.
186 * dev may be NULL to unset the active device.
188 static void eth_set_dev(struct udevice *dev)
190 if (dev && !device_active(dev))
191 eth_errno = device_probe(dev);
192 eth_get_uclass_priv()->current = dev;
196 * Find the udevice that either has the name passed in as devname or has an
197 * alias named devname.
199 struct udevice *eth_get_dev_by_name(const char *devname)
203 const char *startp = NULL;
207 /* Must be longer than 3 to be an alias */
208 if (strlen(devname) > strlen("eth")) {
209 startp = devname + strlen("eth");
210 seq = simple_strtoul(startp, &endp, 10);
213 uclass_get(UCLASS_ETH, &uc);
214 uclass_foreach_dev(it, uc) {
216 * We need the seq to be valid, so try to probe it.
217 * If the probe fails, the seq will not match since it will be
218 * -1 instead of what we are looking for.
219 * We don't care about errors from probe here. Either they won't
220 * match an alias or it will match a literal name and we'll pick
221 * up the error when we try to probe again in eth_set_dev().
225 * Check for the name or the sequence number to match
227 if (strcmp(it->name, devname) == 0 ||
228 (endp > startp && it->seq == seq))
235 unsigned char *eth_get_ethaddr(void)
237 struct eth_pdata *pdata;
240 pdata = eth_get_dev()->platdata;
241 return pdata->enetaddr;
247 /* Set active state without calling start on the driver */
248 int eth_init_state_only(void)
250 struct udevice *current;
251 struct eth_device_priv *priv;
253 current = eth_get_dev();
254 if (!current || !device_active(current))
257 priv = current->uclass_priv;
258 priv->state = ETH_STATE_ACTIVE;
263 /* Set passive state without calling stop on the driver */
264 void eth_halt_state_only(void)
266 struct udevice *current;
267 struct eth_device_priv *priv;
269 current = eth_get_dev();
270 if (!current || !device_active(current))
273 priv = current->uclass_priv;
274 priv->state = ETH_STATE_PASSIVE;
277 int eth_get_dev_index(void)
280 return eth_get_dev()->seq;
284 static int eth_write_hwaddr(struct udevice *dev)
286 struct eth_pdata *pdata = dev->platdata;
289 if (!dev || !device_active(dev))
292 /* seq is valid since the device is active */
293 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
294 if (!is_valid_ethaddr(pdata->enetaddr)) {
295 printf("\nError: %s address %pM illegal value\n",
296 dev->name, pdata->enetaddr);
300 ret = eth_get_ops(dev)->write_hwaddr(dev);
302 printf("\nWarning: %s failed to set MAC address\n",
311 struct udevice *current;
312 struct udevice *old_current;
315 current = eth_get_dev();
317 printf("No ethernet found.\n");
321 old_current = current;
323 debug("Trying %s\n", current->name);
325 if (device_active(current)) {
326 uchar env_enetaddr[6];
327 struct eth_pdata *pdata = current->platdata;
328 int enetaddr_changed = 0;
330 /* Sync environment with network device */
331 if (eth_getenv_enetaddr_by_index("eth", current->seq,
333 enetaddr_changed = memcmp(pdata->enetaddr,
335 memcpy(pdata->enetaddr, env_enetaddr, 6);
337 memset(env_enetaddr, 0, 6);
338 enetaddr_changed = memcmp(pdata->enetaddr,
340 memset(pdata->enetaddr, 0, 6);
342 if (enetaddr_changed)
343 eth_write_hwaddr(current);
345 ret = eth_get_ops(current)->start(current);
347 struct eth_device_priv *priv =
348 current->uclass_priv;
350 priv->state = ETH_STATE_ACTIVE;
360 * If ethrotate is enabled, this will change "current",
361 * otherwise we will drop out of this while loop immediately
364 /* This will ensure the new "current" attempted to probe */
365 current = eth_get_dev();
366 } while (old_current != current);
373 struct udevice *current;
374 struct eth_device_priv *priv;
376 current = eth_get_dev();
377 if (!current || !device_active(current))
380 eth_get_ops(current)->stop(current);
381 priv = current->uclass_priv;
382 priv->state = ETH_STATE_PASSIVE;
385 int eth_send(void *packet, int length)
387 struct udevice *current;
390 current = eth_get_dev();
394 if (!device_active(current))
397 ret = eth_get_ops(current)->send(current, packet, length);
399 /* We cannot completely return the error at present */
400 debug("%s: send() returned error %d\n", __func__, ret);
407 struct udevice *current;
412 current = eth_get_dev();
416 if (!device_active(current))
419 /* Process up to 32 packets at one time */
420 for (i = 0; i < 32; i++) {
421 ret = eth_get_ops(current)->recv(current, &packet);
423 net_process_received_packet(packet, ret);
424 if (ret >= 0 && eth_get_ops(current)->free_pkt)
425 eth_get_ops(current)->free_pkt(current, packet, ret);
432 /* We cannot completely return the error at present */
433 debug("%s: recv() returned error %d\n", __func__, ret);
438 int eth_initialize(void)
446 * Devices need to write the hwaddr even if not started so that Linux
447 * will have access to the hwaddr that u-boot stored for the device.
448 * This is accomplished by attempting to probe each device and calling
449 * their write_hwaddr() operation.
451 uclass_first_device(UCLASS_ETH, &dev);
453 printf("No ethernet found.\n");
454 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
456 char *ethprime = getenv("ethprime");
457 struct udevice *prime_dev = NULL;
460 prime_dev = eth_get_dev_by_name(ethprime);
462 eth_set_dev(prime_dev);
463 eth_current_changed();
468 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
473 printf("eth%d: %s", dev->seq, dev->name);
475 if (ethprime && dev == prime_dev)
478 eth_write_hwaddr(dev);
480 uclass_next_device(&dev);
490 static int eth_post_bind(struct udevice *dev)
492 if (strchr(dev->name, ' ')) {
493 printf("\nError: eth device name \"%s\" has a space!\n",
501 static int eth_pre_unbind(struct udevice *dev)
503 /* Don't hang onto a pointer that is going away */
504 if (dev == eth_get_uclass_priv()->current)
510 static int eth_post_probe(struct udevice *dev)
512 struct eth_device_priv *priv = dev->uclass_priv;
513 struct eth_pdata *pdata = dev->platdata;
514 unsigned char env_enetaddr[6];
516 priv->state = ETH_STATE_INIT;
518 /* Check if the device has a MAC address in ROM */
519 if (eth_get_ops(dev)->read_rom_hwaddr)
520 eth_get_ops(dev)->read_rom_hwaddr(dev);
522 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
523 if (!is_zero_ethaddr(env_enetaddr)) {
524 if (!is_zero_ethaddr(pdata->enetaddr) &&
525 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
526 printf("\nWarning: %s MAC addresses don't match:\n",
528 printf("Address in SROM is %pM\n",
530 printf("Address in environment is %pM\n",
534 /* Override the ROM MAC address */
535 memcpy(pdata->enetaddr, env_enetaddr, 6);
536 } else if (is_valid_ethaddr(pdata->enetaddr)) {
537 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
538 printf("\nWarning: %s using MAC address from ROM\n",
540 } else if (is_zero_ethaddr(pdata->enetaddr)) {
541 #ifdef CONFIG_NET_RANDOM_ETHADDR
542 net_random_ethaddr(pdata->enetaddr);
543 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
544 dev->name, dev->seq, pdata->enetaddr);
546 printf("\nError: %s address not set.\n",
555 static int eth_pre_remove(struct udevice *dev)
557 eth_get_ops(dev)->stop(dev);
562 UCLASS_DRIVER(eth) = {
565 .post_bind = eth_post_bind,
566 .pre_unbind = eth_pre_unbind,
567 .post_probe = eth_post_probe,
568 .pre_remove = eth_pre_remove,
569 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
570 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
571 .flags = DM_UC_FLAG_SEQ_ALIAS,
575 #ifndef CONFIG_DM_ETH
581 } eth_rcv_bufs[PKTBUFSRX];
583 static unsigned int eth_rcv_current, eth_rcv_last;
586 static struct eth_device *eth_devices;
587 struct eth_device *eth_current;
589 static void eth_set_current_to_next(void)
591 eth_current = eth_current->next;
594 static void eth_set_dev(struct eth_device *dev)
599 struct eth_device *eth_get_dev_by_name(const char *devname)
601 struct eth_device *dev, *target_dev;
603 BUG_ON(devname == NULL);
611 if (strcmp(devname, dev->name) == 0) {
616 } while (dev != eth_devices);
621 struct eth_device *eth_get_dev_by_index(int index)
623 struct eth_device *dev, *target_dev;
631 if (dev->index == index) {
636 } while (dev != eth_devices);
641 int eth_get_dev_index(void)
646 return eth_current->index;
649 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
652 unsigned char env_enetaddr[6];
655 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
657 if (!is_zero_ethaddr(env_enetaddr)) {
658 if (!is_zero_ethaddr(dev->enetaddr) &&
659 memcmp(dev->enetaddr, env_enetaddr, 6)) {
660 printf("\nWarning: %s MAC addresses don't match:\n",
662 printf("Address in SROM is %pM\n",
664 printf("Address in environment is %pM\n",
668 memcpy(dev->enetaddr, env_enetaddr, 6);
669 } else if (is_valid_ethaddr(dev->enetaddr)) {
670 eth_setenv_enetaddr_by_index(base_name, eth_number,
672 printf("\nWarning: %s using MAC address from net device\n",
674 } else if (is_zero_ethaddr(dev->enetaddr)) {
675 #ifdef CONFIG_NET_RANDOM_ETHADDR
676 net_random_ethaddr(dev->enetaddr);
677 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
678 dev->name, eth_number, dev->enetaddr);
680 printf("\nError: %s address not set.\n",
686 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
687 if (!is_valid_ethaddr(dev->enetaddr)) {
688 printf("\nError: %s address %pM illegal value\n",
689 dev->name, dev->enetaddr);
693 ret = dev->write_hwaddr(dev);
695 printf("\nWarning: %s failed to set MAC address\n",
702 int eth_register(struct eth_device *dev)
704 struct eth_device *d;
707 assert(strlen(dev->name) < sizeof(dev->name));
712 eth_current_changed();
714 for (d = eth_devices; d->next != eth_devices; d = d->next)
719 dev->state = ETH_STATE_INIT;
720 dev->next = eth_devices;
721 dev->index = index++;
726 int eth_unregister(struct eth_device *dev)
728 struct eth_device *cur;
734 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
738 /* Device not found */
739 if (cur->next != dev)
742 cur->next = dev->next;
744 if (eth_devices == dev)
745 eth_devices = dev->next == eth_devices ? NULL : dev->next;
747 if (eth_current == dev) {
748 eth_current = eth_devices;
749 eth_current_changed();
755 int eth_initialize(void)
764 puts("No ethernet found.\n");
765 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
767 struct eth_device *dev = eth_devices;
768 char *ethprime = getenv("ethprime");
770 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
775 printf("%s", dev->name);
777 if (ethprime && strcmp(dev->name, ethprime) == 0) {
782 if (strchr(dev->name, ' '))
783 puts("\nWarning: eth device name has a space!"
786 eth_write_hwaddr(dev, "eth", dev->index);
790 } while (dev != eth_devices);
792 eth_current_changed();
799 #ifdef CONFIG_MCAST_TFTP
801 * mcast_addr: multicast ipaddr from which multicast Mac is made
802 * join: 1=join, 0=leave.
804 int eth_mcast_join(struct in_addr mcast_ip, int join)
807 if (!eth_current || !eth_current->mcast)
809 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
810 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
811 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
815 return eth_current->mcast(eth_current, mcast_mac, join);
818 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
819 * and this is the ethernet-crc method needed for TSEC -- and perhaps
820 * some other adapter -- hash tables
822 #define CRCPOLY_LE 0xedb88320
823 u32 ether_crc(size_t len, unsigned char const *p)
830 for (i = 0; i < 8; i++)
831 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
833 /* an reverse the bits, cuz of way they arrive -- last-first */
834 crc = (crc >> 16) | (crc << 16);
835 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
836 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
837 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
838 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
847 struct eth_device *old_current, *dev;
850 puts("No ethernet found.\n");
854 /* Sync environment with network devices */
857 uchar env_enetaddr[6];
858 int enetaddr_changed = 0;
860 if (eth_getenv_enetaddr_by_index("eth", dev->index,
862 enetaddr_changed = memcmp(dev->enetaddr,
864 memcpy(dev->enetaddr, env_enetaddr, 6);
866 memset(env_enetaddr, 0, 6);
867 enetaddr_changed = memcmp(dev->enetaddr,
869 memset(dev->enetaddr, 0, 6);
871 if (enetaddr_changed)
872 eth_write_hwaddr(dev, "eth", dev->index);
875 } while (dev != eth_devices);
877 old_current = eth_current;
879 debug("Trying %s\n", eth_current->name);
881 if (eth_current->init(eth_current, gd->bd) >= 0) {
882 eth_current->state = ETH_STATE_ACTIVE;
889 } while (old_current != eth_current);
899 eth_current->halt(eth_current);
901 eth_current->state = ETH_STATE_PASSIVE;
904 int eth_send(void *packet, int length)
909 return eth_current->send(eth_current, packet, length);
917 return eth_current->recv(eth_current);
919 #endif /* ifndef CONFIG_DM_ETH */
922 static void eth_save_packet(void *packet, int length)
927 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
930 if (PKTSIZE < length)
933 for (i = 0; i < length; i++)
934 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
936 eth_rcv_bufs[eth_rcv_last].length = length;
937 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
940 int eth_receive(void *packet, int length)
943 void *pp = push_packet;
946 if (eth_rcv_current == eth_rcv_last) {
947 push_packet = eth_save_packet;
951 if (eth_rcv_current == eth_rcv_last)
955 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
957 for (i = 0; i < length; i++)
958 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
960 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
963 #endif /* CONFIG_API */
965 static void eth_current_changed(void)
967 char *act = getenv("ethact");
968 /* update current ethernet name */
970 if (act == NULL || strcmp(act, eth_get_name()) != 0)
971 setenv("ethact", eth_get_name());
974 * remove the variable completely if there is no active
977 else if (act != NULL)
978 setenv("ethact", NULL);
981 void eth_try_another(int first_restart)
983 static void *first_failed;
987 * Do not rotate between network interfaces when
988 * 'ethrotate' variable is set to 'no'.
990 ethrotate = getenv("ethrotate");
991 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
998 first_failed = eth_get_dev();
1000 eth_set_current_to_next();
1002 eth_current_changed();
1004 if (first_failed == eth_get_dev())
1005 net_restart_wrap = 1;
1008 void eth_set_current(void)
1011 static int env_changed_id;
1014 env_id = get_env_id();
1015 if ((act == NULL) || (env_changed_id != env_id)) {
1016 act = getenv("ethact");
1017 env_changed_id = env_id;
1021 char *ethprime = getenv("ethprime");
1025 dev = eth_get_dev_by_name(ethprime);
1031 eth_set_dev(eth_get_dev_by_name(act));
1034 eth_current_changed();
1037 const char *eth_get_name(void)
1039 return eth_get_dev() ? eth_get_dev()->name : "unknown";