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_ether_addr(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(BootFile, s, sizeof(BootFile));
72 static int eth_mac_skip(int index)
76 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
77 return ((skip_state = getenv(enetvar)) != NULL);
80 static void eth_current_changed(void);
84 * struct eth_device_priv - private structure for each Ethernet device
86 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
88 struct eth_device_priv {
89 enum eth_state_t state;
93 * struct eth_uclass_priv - The structure attached to the uclass itself
95 * @current: The Ethernet device that the network functions are using
97 struct eth_uclass_priv {
98 struct udevice *current;
101 /* eth_errno - This stores the most recent failure code from DM functions */
102 static int eth_errno;
104 static struct eth_uclass_priv *eth_get_uclass_priv(void)
108 uclass_get(UCLASS_ETH, &uc);
113 static void eth_set_current_to_next(void)
115 struct eth_uclass_priv *uc_priv;
117 uc_priv = eth_get_uclass_priv();
118 if (uc_priv->current)
119 uclass_next_device(&uc_priv->current);
120 if (!uc_priv->current)
121 uclass_first_device(UCLASS_ETH, &uc_priv->current);
125 * Typically this will simply return the active device.
126 * In the case where the most recent active device was unset, this will attempt
127 * to return the first device. If that device doesn't exist or fails to probe,
128 * this function will return NULL.
130 struct udevice *eth_get_dev(void)
132 struct eth_uclass_priv *uc_priv;
134 uc_priv = eth_get_uclass_priv();
135 if (!uc_priv->current)
136 eth_errno = uclass_first_device(UCLASS_ETH,
138 return uc_priv->current;
142 * Typically this will just store a device pointer.
143 * In case it was not probed, we will attempt to do so.
144 * dev may be NULL to unset the active device.
146 static void eth_set_dev(struct udevice *dev)
148 if (dev && !device_active(dev))
149 eth_errno = device_probe(dev);
150 eth_get_uclass_priv()->current = dev;
154 * Find the udevice that either has the name passed in as devname or has an
155 * alias named devname.
157 struct udevice *eth_get_dev_by_name(const char *devname)
161 const char *startp = NULL;
165 /* Must be longer than 3 to be an alias */
166 if (strlen(devname) > strlen("eth")) {
167 startp = devname + strlen("eth");
168 seq = simple_strtoul(startp, &endp, 10);
171 uclass_get(UCLASS_ETH, &uc);
172 uclass_foreach_dev(it, uc) {
174 * We need the seq to be valid, so try to probe it.
175 * If the probe fails, the seq will not match since it will be
176 * -1 instead of what we are looking for.
177 * We don't care about errors from probe here. Either they won't
178 * match an alias or it will match a literal name and we'll pick
179 * up the error when we try to probe again in eth_set_dev().
183 * Check for the name or the sequence number to match
185 if (strcmp(it->name, devname) == 0 ||
186 (endp > startp && it->seq == seq))
193 unsigned char *eth_get_ethaddr(void)
195 struct eth_pdata *pdata;
198 pdata = eth_get_dev()->platdata;
199 return pdata->enetaddr;
205 /* Set active state without calling start on the driver */
206 int eth_init_state_only(void)
208 struct udevice *current;
209 struct eth_device_priv *priv;
211 current = eth_get_dev();
212 if (!current || !device_active(current))
215 priv = current->uclass_priv;
216 priv->state = ETH_STATE_ACTIVE;
221 /* Set passive state without calling stop on the driver */
222 void eth_halt_state_only(void)
224 struct udevice *current;
225 struct eth_device_priv *priv;
227 current = eth_get_dev();
228 if (!current || !device_active(current))
231 priv = current->uclass_priv;
232 priv->state = ETH_STATE_PASSIVE;
235 int eth_get_dev_index(void)
238 return eth_get_dev()->seq;
244 struct udevice *current;
245 struct udevice *old_current;
248 current = eth_get_dev();
250 printf("No ethernet found.\n");
254 old_current = current;
256 debug("Trying %s\n", current->name);
258 if (device_active(current)) {
259 uchar env_enetaddr[6];
260 struct eth_pdata *pdata = current->platdata;
262 /* Sync environment with network device */
263 if (eth_getenv_enetaddr_by_index("eth", current->seq,
265 memcpy(pdata->enetaddr, env_enetaddr, 6);
267 memset(pdata->enetaddr, 0, 6);
269 ret = eth_get_ops(current)->start(current);
271 struct eth_device_priv *priv =
272 current->uclass_priv;
274 priv->state = ETH_STATE_ACTIVE;
283 * If ethrotate is enabled, this will change "current",
284 * otherwise we will drop out of this while loop immediately
287 /* This will ensure the new "current" attempted to probe */
288 current = eth_get_dev();
289 } while (old_current != current);
296 struct udevice *current;
297 struct eth_device_priv *priv;
299 current = eth_get_dev();
300 if (!current || !device_active(current))
303 eth_get_ops(current)->stop(current);
304 priv = current->uclass_priv;
305 priv->state = ETH_STATE_PASSIVE;
308 int eth_send(void *packet, int length)
310 struct udevice *current;
313 current = eth_get_dev();
317 if (!device_active(current))
320 ret = eth_get_ops(current)->send(current, packet, length);
322 /* We cannot completely return the error at present */
323 debug("%s: send() returned error %d\n", __func__, ret);
330 struct udevice *current;
335 current = eth_get_dev();
339 if (!device_active(current))
342 /* Process up to 32 packets at one time */
343 for (i = 0; i < 32; i++) {
344 ret = eth_get_ops(current)->recv(current, &packet);
346 net_process_received_packet(packet, ret);
347 if (ret >= 0 && eth_get_ops(current)->free_pkt)
348 eth_get_ops(current)->free_pkt(current, packet, ret);
355 /* We cannot completely return the error at present */
356 debug("%s: recv() returned error %d\n", __func__, ret);
361 static int eth_write_hwaddr(struct udevice *dev)
363 struct eth_pdata *pdata = dev->platdata;
366 if (!dev || !device_active(dev))
369 /* seq is valid since the device is active */
370 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
371 if (!is_valid_ether_addr(pdata->enetaddr)) {
372 printf("\nError: %s address %pM illegal value\n",
373 dev->name, pdata->enetaddr);
377 ret = eth_get_ops(dev)->write_hwaddr(dev);
379 printf("\nWarning: %s failed to set MAC address\n",
386 int eth_initialize(void)
391 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
395 * Devices need to write the hwaddr even if not started so that Linux
396 * will have access to the hwaddr that u-boot stored for the device.
397 * This is accomplished by attempting to probe each device and calling
398 * their write_hwaddr() operation.
400 uclass_first_device(UCLASS_ETH, &dev);
402 printf("No ethernet found.\n");
403 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
405 char *ethprime = getenv("ethprime");
406 struct udevice *prime_dev = NULL;
409 prime_dev = eth_get_dev_by_name(ethprime);
411 eth_set_dev(prime_dev);
412 eth_current_changed();
417 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
422 printf("eth%d: %s", dev->seq, dev->name);
424 if (ethprime && dev == prime_dev)
427 eth_write_hwaddr(dev);
429 uclass_next_device(&dev);
439 static int eth_post_bind(struct udevice *dev)
441 if (strchr(dev->name, ' ')) {
442 printf("\nError: eth device name \"%s\" has a space!\n",
450 static int eth_pre_unbind(struct udevice *dev)
452 /* Don't hang onto a pointer that is going away */
453 if (dev == eth_get_uclass_priv()->current)
459 static int eth_post_probe(struct udevice *dev)
461 struct eth_device_priv *priv = dev->uclass_priv;
462 struct eth_pdata *pdata = dev->platdata;
463 unsigned char env_enetaddr[6];
465 priv->state = ETH_STATE_INIT;
467 /* Check if the device has a MAC address in ROM */
468 if (eth_get_ops(dev)->read_rom_hwaddr)
469 eth_get_ops(dev)->read_rom_hwaddr(dev);
471 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
472 if (!is_zero_ether_addr(env_enetaddr)) {
473 if (!is_zero_ether_addr(pdata->enetaddr) &&
474 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
475 printf("\nWarning: %s MAC addresses don't match:\n",
477 printf("Address in SROM is %pM\n",
479 printf("Address in environment is %pM\n",
483 /* Override the ROM MAC address */
484 memcpy(pdata->enetaddr, env_enetaddr, 6);
485 } else if (is_valid_ether_addr(pdata->enetaddr)) {
486 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
487 printf("\nWarning: %s using MAC address from ROM\n",
489 } else if (is_zero_ether_addr(pdata->enetaddr)) {
490 printf("\nError: %s address not set.\n",
498 static int eth_pre_remove(struct udevice *dev)
500 eth_get_ops(dev)->stop(dev);
505 UCLASS_DRIVER(eth) = {
508 .post_bind = eth_post_bind,
509 .pre_unbind = eth_pre_unbind,
510 .post_probe = eth_post_probe,
511 .pre_remove = eth_pre_remove,
512 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
513 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
514 .flags = DM_UC_FLAG_SEQ_ALIAS,
518 #ifndef CONFIG_DM_ETH
520 * CPU and board-specific Ethernet initializations. Aliased function
521 * signals caller to move on
523 static int __def_eth_init(bd_t *bis)
527 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
528 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
534 } eth_rcv_bufs[PKTBUFSRX];
536 static unsigned int eth_rcv_current, eth_rcv_last;
539 static struct eth_device *eth_devices;
540 struct eth_device *eth_current;
542 static void eth_set_current_to_next(void)
544 eth_current = eth_current->next;
547 static void eth_set_dev(struct eth_device *dev)
552 struct eth_device *eth_get_dev_by_name(const char *devname)
554 struct eth_device *dev, *target_dev;
556 BUG_ON(devname == NULL);
564 if (strcmp(devname, dev->name) == 0) {
569 } while (dev != eth_devices);
574 struct eth_device *eth_get_dev_by_index(int index)
576 struct eth_device *dev, *target_dev;
584 if (dev->index == index) {
589 } while (dev != eth_devices);
594 int eth_get_dev_index(void)
599 return eth_current->index;
602 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
605 unsigned char env_enetaddr[6];
608 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
610 if (!is_zero_ether_addr(env_enetaddr)) {
611 if (!is_zero_ether_addr(dev->enetaddr) &&
612 memcmp(dev->enetaddr, env_enetaddr, 6)) {
613 printf("\nWarning: %s MAC addresses don't match:\n",
615 printf("Address in SROM is %pM\n",
617 printf("Address in environment is %pM\n",
621 memcpy(dev->enetaddr, env_enetaddr, 6);
622 } else if (is_valid_ether_addr(dev->enetaddr)) {
623 eth_setenv_enetaddr_by_index(base_name, eth_number,
625 printf("\nWarning: %s using MAC address from net device\n",
627 } else if (is_zero_ether_addr(dev->enetaddr)) {
628 printf("\nError: %s address not set.\n",
633 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
634 if (!is_valid_ether_addr(dev->enetaddr)) {
635 printf("\nError: %s address %pM illegal value\n",
636 dev->name, dev->enetaddr);
640 ret = dev->write_hwaddr(dev);
642 printf("\nWarning: %s failed to set MAC address\n", dev->name);
648 int eth_register(struct eth_device *dev)
650 struct eth_device *d;
653 assert(strlen(dev->name) < sizeof(dev->name));
656 eth_current = eth_devices = dev;
657 eth_current_changed();
659 for (d = eth_devices; d->next != eth_devices; d = d->next)
664 dev->state = ETH_STATE_INIT;
665 dev->next = eth_devices;
666 dev->index = index++;
671 int eth_unregister(struct eth_device *dev)
673 struct eth_device *cur;
679 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
683 /* Device not found */
684 if (cur->next != dev)
687 cur->next = dev->next;
689 if (eth_devices == dev)
690 eth_devices = dev->next == eth_devices ? NULL : dev->next;
692 if (eth_current == dev) {
693 eth_current = eth_devices;
694 eth_current_changed();
700 int eth_initialize(void)
706 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
707 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
718 * If board-specific initialization exists, call it.
719 * If not, call a CPU-specific one
721 if (board_eth_init != __def_eth_init) {
722 if (board_eth_init(gd->bd) < 0)
723 printf("Board Net Initialization Failed\n");
724 } else if (cpu_eth_init != __def_eth_init) {
725 if (cpu_eth_init(gd->bd) < 0)
726 printf("CPU Net Initialization Failed\n");
728 printf("Net Initialization Skipped\n");
731 puts("No ethernet found.\n");
732 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
734 struct eth_device *dev = eth_devices;
735 char *ethprime = getenv("ethprime");
737 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
742 printf("%s", dev->name);
744 if (ethprime && strcmp(dev->name, ethprime) == 0) {
749 if (strchr(dev->name, ' '))
750 puts("\nWarning: eth device name has a space!"
753 eth_write_hwaddr(dev, "eth", dev->index);
757 } while (dev != eth_devices);
759 eth_current_changed();
766 #ifdef CONFIG_MCAST_TFTP
768 * mcast_addr: multicast ipaddr from which multicast Mac is made
769 * join: 1=join, 0=leave.
771 int eth_mcast_join(IPaddr_t mcast_ip, int join)
774 if (!eth_current || !eth_current->mcast)
776 mcast_mac[5] = htonl(mcast_ip) & 0xff;
777 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
778 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
782 return eth_current->mcast(eth_current, mcast_mac, join);
785 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
786 * and this is the ethernet-crc method needed for TSEC -- and perhaps
787 * some other adapter -- hash tables
789 #define CRCPOLY_LE 0xedb88320
790 u32 ether_crc(size_t len, unsigned char const *p)
797 for (i = 0; i < 8; i++)
798 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
800 /* an reverse the bits, cuz of way they arrive -- last-first */
801 crc = (crc >> 16) | (crc << 16);
802 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
803 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
804 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
805 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
814 struct eth_device *old_current, *dev;
817 puts("No ethernet found.\n");
821 /* Sync environment with network devices */
824 uchar env_enetaddr[6];
826 if (eth_getenv_enetaddr_by_index("eth", dev->index,
828 memcpy(dev->enetaddr, env_enetaddr, 6);
831 } while (dev != eth_devices);
833 old_current = eth_current;
835 debug("Trying %s\n", eth_current->name);
837 if (eth_current->init(eth_current, gd->bd) >= 0) {
838 eth_current->state = ETH_STATE_ACTIVE;
845 } while (old_current != eth_current);
855 eth_current->halt(eth_current);
857 eth_current->state = ETH_STATE_PASSIVE;
860 int eth_send(void *packet, int length)
865 return eth_current->send(eth_current, packet, length);
873 return eth_current->recv(eth_current);
875 #endif /* ifndef CONFIG_DM_ETH */
878 static void eth_save_packet(void *packet, int length)
883 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
886 if (PKTSIZE < length)
889 for (i = 0; i < length; i++)
890 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
892 eth_rcv_bufs[eth_rcv_last].length = length;
893 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
896 int eth_receive(void *packet, int length)
899 void *pp = push_packet;
902 if (eth_rcv_current == eth_rcv_last) {
903 push_packet = eth_save_packet;
907 if (eth_rcv_current == eth_rcv_last)
911 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
913 for (i = 0; i < length; i++)
914 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
916 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
919 #endif /* CONFIG_API */
921 static void eth_current_changed(void)
923 char *act = getenv("ethact");
924 /* update current ethernet name */
926 if (act == NULL || strcmp(act, eth_get_name()) != 0)
927 setenv("ethact", eth_get_name());
930 * remove the variable completely if there is no active
933 else if (act != NULL)
934 setenv("ethact", NULL);
937 void eth_try_another(int first_restart)
939 static void *first_failed;
943 * Do not rotate between network interfaces when
944 * 'ethrotate' variable is set to 'no'.
946 ethrotate = getenv("ethrotate");
947 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
954 first_failed = eth_get_dev();
956 eth_set_current_to_next();
958 eth_current_changed();
960 if (first_failed == eth_get_dev())
964 void eth_set_current(void)
967 static int env_changed_id;
970 env_id = get_env_id();
971 if ((act == NULL) || (env_changed_id != env_id)) {
972 act = getenv("ethact");
973 env_changed_id = env_id;
977 char *ethprime = getenv("ethprime");
981 dev = eth_get_dev_by_name(ethprime);
987 eth_set_dev(eth_get_dev_by_name(act));
990 eth_current_changed();
993 const char *eth_get_name(void)
995 return eth_get_dev() ? eth_get_dev()->name : "unknown";