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(net_boot_file_name, s,
70 sizeof(net_boot_file_name));
73 static int eth_mac_skip(int index)
77 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
78 return ((skip_state = getenv(enetvar)) != NULL);
81 static void eth_current_changed(void);
85 * struct eth_device_priv - private structure for each Ethernet device
87 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
89 struct eth_device_priv {
90 enum eth_state_t state;
94 * struct eth_uclass_priv - The structure attached to the uclass itself
96 * @current: The Ethernet device that the network functions are using
98 struct eth_uclass_priv {
99 struct udevice *current;
102 /* eth_errno - This stores the most recent failure code from DM functions */
103 static int eth_errno;
105 static struct eth_uclass_priv *eth_get_uclass_priv(void)
109 uclass_get(UCLASS_ETH, &uc);
114 static void eth_set_current_to_next(void)
116 struct eth_uclass_priv *uc_priv;
118 uc_priv = eth_get_uclass_priv();
119 if (uc_priv->current)
120 uclass_next_device(&uc_priv->current);
121 if (!uc_priv->current)
122 uclass_first_device(UCLASS_ETH, &uc_priv->current);
126 * Typically this will simply return the active device.
127 * In the case where the most recent active device was unset, this will attempt
128 * to return the first device. If that device doesn't exist or fails to probe,
129 * this function will return NULL.
131 struct udevice *eth_get_dev(void)
133 struct eth_uclass_priv *uc_priv;
135 uc_priv = eth_get_uclass_priv();
136 if (!uc_priv->current)
137 eth_errno = uclass_first_device(UCLASS_ETH,
139 return uc_priv->current;
143 * Typically this will just store a device pointer.
144 * In case it was not probed, we will attempt to do so.
145 * dev may be NULL to unset the active device.
147 static void eth_set_dev(struct udevice *dev)
149 if (dev && !device_active(dev))
150 eth_errno = device_probe(dev);
151 eth_get_uclass_priv()->current = dev;
155 * Find the udevice that either has the name passed in as devname or has an
156 * alias named devname.
158 struct udevice *eth_get_dev_by_name(const char *devname)
162 const char *startp = NULL;
166 /* Must be longer than 3 to be an alias */
167 if (strlen(devname) > strlen("eth")) {
168 startp = devname + strlen("eth");
169 seq = simple_strtoul(startp, &endp, 10);
172 uclass_get(UCLASS_ETH, &uc);
173 uclass_foreach_dev(it, uc) {
175 * We need the seq to be valid, so try to probe it.
176 * If the probe fails, the seq will not match since it will be
177 * -1 instead of what we are looking for.
178 * We don't care about errors from probe here. Either they won't
179 * match an alias or it will match a literal name and we'll pick
180 * up the error when we try to probe again in eth_set_dev().
184 * Check for the name or the sequence number to match
186 if (strcmp(it->name, devname) == 0 ||
187 (endp > startp && it->seq == seq))
194 unsigned char *eth_get_ethaddr(void)
196 struct eth_pdata *pdata;
199 pdata = eth_get_dev()->platdata;
200 return pdata->enetaddr;
206 /* Set active state without calling start on the driver */
207 int eth_init_state_only(void)
209 struct udevice *current;
210 struct eth_device_priv *priv;
212 current = eth_get_dev();
213 if (!current || !device_active(current))
216 priv = current->uclass_priv;
217 priv->state = ETH_STATE_ACTIVE;
222 /* Set passive state without calling stop on the driver */
223 void eth_halt_state_only(void)
225 struct udevice *current;
226 struct eth_device_priv *priv;
228 current = eth_get_dev();
229 if (!current || !device_active(current))
232 priv = current->uclass_priv;
233 priv->state = ETH_STATE_PASSIVE;
236 int eth_get_dev_index(void)
239 return eth_get_dev()->seq;
245 struct udevice *current;
246 struct udevice *old_current;
249 current = eth_get_dev();
251 printf("No ethernet found.\n");
255 old_current = current;
257 debug("Trying %s\n", current->name);
259 if (device_active(current)) {
260 uchar env_enetaddr[6];
261 struct eth_pdata *pdata = current->platdata;
263 /* Sync environment with network device */
264 if (eth_getenv_enetaddr_by_index("eth", current->seq,
266 memcpy(pdata->enetaddr, env_enetaddr, 6);
268 memset(pdata->enetaddr, 0, 6);
270 ret = eth_get_ops(current)->start(current);
272 struct eth_device_priv *priv =
273 current->uclass_priv;
275 priv->state = ETH_STATE_ACTIVE;
284 * If ethrotate is enabled, this will change "current",
285 * otherwise we will drop out of this while loop immediately
288 /* This will ensure the new "current" attempted to probe */
289 current = eth_get_dev();
290 } while (old_current != current);
297 struct udevice *current;
298 struct eth_device_priv *priv;
300 current = eth_get_dev();
301 if (!current || !device_active(current))
304 eth_get_ops(current)->stop(current);
305 priv = current->uclass_priv;
306 priv->state = ETH_STATE_PASSIVE;
309 int eth_send(void *packet, int length)
311 struct udevice *current;
314 current = eth_get_dev();
318 if (!device_active(current))
321 ret = eth_get_ops(current)->send(current, packet, length);
323 /* We cannot completely return the error at present */
324 debug("%s: send() returned error %d\n", __func__, ret);
331 struct udevice *current;
336 current = eth_get_dev();
340 if (!device_active(current))
343 /* Process up to 32 packets at one time */
344 for (i = 0; i < 32; i++) {
345 ret = eth_get_ops(current)->recv(current, &packet);
347 net_process_received_packet(packet, ret);
348 if (ret >= 0 && eth_get_ops(current)->free_pkt)
349 eth_get_ops(current)->free_pkt(current, packet, ret);
356 /* We cannot completely return the error at present */
357 debug("%s: recv() returned error %d\n", __func__, ret);
362 static int eth_write_hwaddr(struct udevice *dev)
364 struct eth_pdata *pdata = dev->platdata;
367 if (!dev || !device_active(dev))
370 /* seq is valid since the device is active */
371 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
372 if (!is_valid_ether_addr(pdata->enetaddr)) {
373 printf("\nError: %s address %pM illegal value\n",
374 dev->name, pdata->enetaddr);
378 ret = eth_get_ops(dev)->write_hwaddr(dev);
380 printf("\nWarning: %s failed to set MAC address\n",
387 int eth_initialize(void)
392 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
396 * Devices need to write the hwaddr even if not started so that Linux
397 * will have access to the hwaddr that u-boot stored for the device.
398 * This is accomplished by attempting to probe each device and calling
399 * their write_hwaddr() operation.
401 uclass_first_device(UCLASS_ETH, &dev);
403 printf("No ethernet found.\n");
404 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
406 char *ethprime = getenv("ethprime");
407 struct udevice *prime_dev = NULL;
410 prime_dev = eth_get_dev_by_name(ethprime);
412 eth_set_dev(prime_dev);
413 eth_current_changed();
418 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
423 printf("eth%d: %s", dev->seq, dev->name);
425 if (ethprime && dev == prime_dev)
428 eth_write_hwaddr(dev);
430 uclass_next_device(&dev);
440 static int eth_post_bind(struct udevice *dev)
442 if (strchr(dev->name, ' ')) {
443 printf("\nError: eth device name \"%s\" has a space!\n",
451 static int eth_pre_unbind(struct udevice *dev)
453 /* Don't hang onto a pointer that is going away */
454 if (dev == eth_get_uclass_priv()->current)
460 static int eth_post_probe(struct udevice *dev)
462 struct eth_device_priv *priv = dev->uclass_priv;
463 struct eth_pdata *pdata = dev->platdata;
464 unsigned char env_enetaddr[6];
466 priv->state = ETH_STATE_INIT;
468 /* Check if the device has a MAC address in ROM */
469 if (eth_get_ops(dev)->read_rom_hwaddr)
470 eth_get_ops(dev)->read_rom_hwaddr(dev);
472 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
473 if (!is_zero_ether_addr(env_enetaddr)) {
474 if (!is_zero_ether_addr(pdata->enetaddr) &&
475 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
476 printf("\nWarning: %s MAC addresses don't match:\n",
478 printf("Address in SROM is %pM\n",
480 printf("Address in environment is %pM\n",
484 /* Override the ROM MAC address */
485 memcpy(pdata->enetaddr, env_enetaddr, 6);
486 } else if (is_valid_ether_addr(pdata->enetaddr)) {
487 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
488 printf("\nWarning: %s using MAC address from ROM\n",
490 } else if (is_zero_ether_addr(pdata->enetaddr)) {
491 printf("\nError: %s address not set.\n",
499 static int eth_pre_remove(struct udevice *dev)
501 eth_get_ops(dev)->stop(dev);
506 UCLASS_DRIVER(eth) = {
509 .post_bind = eth_post_bind,
510 .pre_unbind = eth_pre_unbind,
511 .post_probe = eth_post_probe,
512 .pre_remove = eth_pre_remove,
513 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
514 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
515 .flags = DM_UC_FLAG_SEQ_ALIAS,
519 #ifndef CONFIG_DM_ETH
521 * CPU and board-specific Ethernet initializations. Aliased function
522 * signals caller to move on
524 static int __def_eth_init(bd_t *bis)
528 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
529 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
535 } eth_rcv_bufs[PKTBUFSRX];
537 static unsigned int eth_rcv_current, eth_rcv_last;
540 static struct eth_device *eth_devices;
541 struct eth_device *eth_current;
543 static void eth_set_current_to_next(void)
545 eth_current = eth_current->next;
548 static void eth_set_dev(struct eth_device *dev)
553 struct eth_device *eth_get_dev_by_name(const char *devname)
555 struct eth_device *dev, *target_dev;
557 BUG_ON(devname == NULL);
565 if (strcmp(devname, dev->name) == 0) {
570 } while (dev != eth_devices);
575 struct eth_device *eth_get_dev_by_index(int index)
577 struct eth_device *dev, *target_dev;
585 if (dev->index == index) {
590 } while (dev != eth_devices);
595 int eth_get_dev_index(void)
600 return eth_current->index;
603 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
606 unsigned char env_enetaddr[6];
609 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
611 if (!is_zero_ether_addr(env_enetaddr)) {
612 if (!is_zero_ether_addr(dev->enetaddr) &&
613 memcmp(dev->enetaddr, env_enetaddr, 6)) {
614 printf("\nWarning: %s MAC addresses don't match:\n",
616 printf("Address in SROM is %pM\n",
618 printf("Address in environment is %pM\n",
622 memcpy(dev->enetaddr, env_enetaddr, 6);
623 } else if (is_valid_ether_addr(dev->enetaddr)) {
624 eth_setenv_enetaddr_by_index(base_name, eth_number,
626 printf("\nWarning: %s using MAC address from net device\n",
628 } else if (is_zero_ether_addr(dev->enetaddr)) {
629 printf("\nError: %s address not set.\n",
634 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
635 if (!is_valid_ether_addr(dev->enetaddr)) {
636 printf("\nError: %s address %pM illegal value\n",
637 dev->name, dev->enetaddr);
641 ret = dev->write_hwaddr(dev);
643 printf("\nWarning: %s failed to set MAC address\n", dev->name);
649 int eth_register(struct eth_device *dev)
651 struct eth_device *d;
654 assert(strlen(dev->name) < sizeof(dev->name));
657 eth_current = eth_devices = dev;
658 eth_current_changed();
660 for (d = eth_devices; d->next != eth_devices; d = d->next)
665 dev->state = ETH_STATE_INIT;
666 dev->next = eth_devices;
667 dev->index = index++;
672 int eth_unregister(struct eth_device *dev)
674 struct eth_device *cur;
680 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
684 /* Device not found */
685 if (cur->next != dev)
688 cur->next = dev->next;
690 if (eth_devices == dev)
691 eth_devices = dev->next == eth_devices ? NULL : dev->next;
693 if (eth_current == dev) {
694 eth_current = eth_devices;
695 eth_current_changed();
701 int eth_initialize(void)
707 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
708 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
719 * If board-specific initialization exists, call it.
720 * If not, call a CPU-specific one
722 if (board_eth_init != __def_eth_init) {
723 if (board_eth_init(gd->bd) < 0)
724 printf("Board Net Initialization Failed\n");
725 } else if (cpu_eth_init != __def_eth_init) {
726 if (cpu_eth_init(gd->bd) < 0)
727 printf("CPU Net Initialization Failed\n");
729 printf("Net Initialization Skipped\n");
732 puts("No ethernet found.\n");
733 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
735 struct eth_device *dev = eth_devices;
736 char *ethprime = getenv("ethprime");
738 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
743 printf("%s", dev->name);
745 if (ethprime && strcmp(dev->name, ethprime) == 0) {
750 if (strchr(dev->name, ' '))
751 puts("\nWarning: eth device name has a space!"
754 eth_write_hwaddr(dev, "eth", dev->index);
758 } while (dev != eth_devices);
760 eth_current_changed();
767 #ifdef CONFIG_MCAST_TFTP
769 * mcast_addr: multicast ipaddr from which multicast Mac is made
770 * join: 1=join, 0=leave.
772 int eth_mcast_join(struct in_addr mcast_ip, int join)
775 if (!eth_current || !eth_current->mcast)
777 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
778 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
779 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
783 return eth_current->mcast(eth_current, mcast_mac, join);
786 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
787 * and this is the ethernet-crc method needed for TSEC -- and perhaps
788 * some other adapter -- hash tables
790 #define CRCPOLY_LE 0xedb88320
791 u32 ether_crc(size_t len, unsigned char const *p)
798 for (i = 0; i < 8; i++)
799 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
801 /* an reverse the bits, cuz of way they arrive -- last-first */
802 crc = (crc >> 16) | (crc << 16);
803 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
804 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
805 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
806 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
815 struct eth_device *old_current, *dev;
818 puts("No ethernet found.\n");
822 /* Sync environment with network devices */
825 uchar env_enetaddr[6];
827 if (eth_getenv_enetaddr_by_index("eth", dev->index,
829 memcpy(dev->enetaddr, env_enetaddr, 6);
832 } while (dev != eth_devices);
834 old_current = eth_current;
836 debug("Trying %s\n", eth_current->name);
838 if (eth_current->init(eth_current, gd->bd) >= 0) {
839 eth_current->state = ETH_STATE_ACTIVE;
846 } while (old_current != eth_current);
856 eth_current->halt(eth_current);
858 eth_current->state = ETH_STATE_PASSIVE;
861 int eth_send(void *packet, int length)
866 return eth_current->send(eth_current, packet, length);
874 return eth_current->recv(eth_current);
876 #endif /* ifndef CONFIG_DM_ETH */
879 static void eth_save_packet(void *packet, int length)
884 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
887 if (PKTSIZE < length)
890 for (i = 0; i < length; i++)
891 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
893 eth_rcv_bufs[eth_rcv_last].length = length;
894 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
897 int eth_receive(void *packet, int length)
900 void *pp = push_packet;
903 if (eth_rcv_current == eth_rcv_last) {
904 push_packet = eth_save_packet;
908 if (eth_rcv_current == eth_rcv_last)
912 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
914 for (i = 0; i < length; i++)
915 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
917 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
920 #endif /* CONFIG_API */
922 static void eth_current_changed(void)
924 char *act = getenv("ethact");
925 /* update current ethernet name */
927 if (act == NULL || strcmp(act, eth_get_name()) != 0)
928 setenv("ethact", eth_get_name());
931 * remove the variable completely if there is no active
934 else if (act != NULL)
935 setenv("ethact", NULL);
938 void eth_try_another(int first_restart)
940 static void *first_failed;
944 * Do not rotate between network interfaces when
945 * 'ethrotate' variable is set to 'no'.
947 ethrotate = getenv("ethrotate");
948 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
955 first_failed = eth_get_dev();
957 eth_set_current_to_next();
959 eth_current_changed();
961 if (first_failed == eth_get_dev())
965 void eth_set_current(void)
968 static int env_changed_id;
971 env_id = get_env_id();
972 if ((act == NULL) || (env_changed_id != env_id)) {
973 act = getenv("ethact");
974 env_changed_id = env_id;
978 char *ethprime = getenv("ethprime");
982 dev = eth_get_dev_by_name(ethprime);
988 eth_set_dev(eth_get_dev_by_name(act));
991 eth_current_changed();
994 const char *eth_get_name(void)
996 return eth_get_dev() ? eth_get_dev()->name : "unknown";