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;
286 struct udevice *current;
287 struct udevice *old_current;
290 current = eth_get_dev();
292 printf("No ethernet found.\n");
296 old_current = current;
298 debug("Trying %s\n", current->name);
300 if (device_active(current)) {
301 uchar env_enetaddr[6];
302 struct eth_pdata *pdata = current->platdata;
304 /* Sync environment with network device */
305 if (eth_getenv_enetaddr_by_index("eth", current->seq,
307 memcpy(pdata->enetaddr, env_enetaddr, 6);
309 memset(pdata->enetaddr, 0, 6);
311 ret = eth_get_ops(current)->start(current);
313 struct eth_device_priv *priv =
314 current->uclass_priv;
316 priv->state = ETH_STATE_ACTIVE;
326 * If ethrotate is enabled, this will change "current",
327 * otherwise we will drop out of this while loop immediately
330 /* This will ensure the new "current" attempted to probe */
331 current = eth_get_dev();
332 } while (old_current != current);
339 struct udevice *current;
340 struct eth_device_priv *priv;
342 current = eth_get_dev();
343 if (!current || !device_active(current))
346 eth_get_ops(current)->stop(current);
347 priv = current->uclass_priv;
348 priv->state = ETH_STATE_PASSIVE;
351 int eth_send(void *packet, int length)
353 struct udevice *current;
356 current = eth_get_dev();
360 if (!device_active(current))
363 ret = eth_get_ops(current)->send(current, packet, length);
365 /* We cannot completely return the error at present */
366 debug("%s: send() returned error %d\n", __func__, ret);
373 struct udevice *current;
378 current = eth_get_dev();
382 if (!device_active(current))
385 /* Process up to 32 packets at one time */
386 for (i = 0; i < 32; i++) {
387 ret = eth_get_ops(current)->recv(current, &packet);
389 net_process_received_packet(packet, ret);
390 if (ret >= 0 && eth_get_ops(current)->free_pkt)
391 eth_get_ops(current)->free_pkt(current, packet, ret);
398 /* We cannot completely return the error at present */
399 debug("%s: recv() returned error %d\n", __func__, ret);
404 static int eth_write_hwaddr(struct udevice *dev)
406 struct eth_pdata *pdata = dev->platdata;
409 if (!dev || !device_active(dev))
412 /* seq is valid since the device is active */
413 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
414 if (!is_valid_ethaddr(pdata->enetaddr)) {
415 printf("\nError: %s address %pM illegal value\n",
416 dev->name, pdata->enetaddr);
420 ret = eth_get_ops(dev)->write_hwaddr(dev);
422 printf("\nWarning: %s failed to set MAC address\n",
429 int eth_initialize(void)
437 * Devices need to write the hwaddr even if not started so that Linux
438 * will have access to the hwaddr that u-boot stored for the device.
439 * This is accomplished by attempting to probe each device and calling
440 * their write_hwaddr() operation.
442 uclass_first_device(UCLASS_ETH, &dev);
444 printf("No ethernet found.\n");
445 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
447 char *ethprime = getenv("ethprime");
448 struct udevice *prime_dev = NULL;
451 prime_dev = eth_get_dev_by_name(ethprime);
453 eth_set_dev(prime_dev);
454 eth_current_changed();
459 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
464 printf("eth%d: %s", dev->seq, dev->name);
466 if (ethprime && dev == prime_dev)
469 eth_write_hwaddr(dev);
471 uclass_next_device(&dev);
481 static int eth_post_bind(struct udevice *dev)
483 if (strchr(dev->name, ' ')) {
484 printf("\nError: eth device name \"%s\" has a space!\n",
492 static int eth_pre_unbind(struct udevice *dev)
494 /* Don't hang onto a pointer that is going away */
495 if (dev == eth_get_uclass_priv()->current)
501 static int eth_post_probe(struct udevice *dev)
503 struct eth_device_priv *priv = dev->uclass_priv;
504 struct eth_pdata *pdata = dev->platdata;
505 unsigned char env_enetaddr[6];
507 priv->state = ETH_STATE_INIT;
509 /* Check if the device has a MAC address in ROM */
510 if (eth_get_ops(dev)->read_rom_hwaddr)
511 eth_get_ops(dev)->read_rom_hwaddr(dev);
513 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
514 if (!is_zero_ethaddr(env_enetaddr)) {
515 if (!is_zero_ethaddr(pdata->enetaddr) &&
516 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
517 printf("\nWarning: %s MAC addresses don't match:\n",
519 printf("Address in SROM is %pM\n",
521 printf("Address in environment is %pM\n",
525 /* Override the ROM MAC address */
526 memcpy(pdata->enetaddr, env_enetaddr, 6);
527 } else if (is_valid_ethaddr(pdata->enetaddr)) {
528 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
529 printf("\nWarning: %s using MAC address from ROM\n",
531 } else if (is_zero_ethaddr(pdata->enetaddr)) {
532 printf("\nError: %s address not set.\n",
540 static int eth_pre_remove(struct udevice *dev)
542 eth_get_ops(dev)->stop(dev);
547 UCLASS_DRIVER(eth) = {
550 .post_bind = eth_post_bind,
551 .pre_unbind = eth_pre_unbind,
552 .post_probe = eth_post_probe,
553 .pre_remove = eth_pre_remove,
554 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
555 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
556 .flags = DM_UC_FLAG_SEQ_ALIAS,
560 #ifndef CONFIG_DM_ETH
566 } eth_rcv_bufs[PKTBUFSRX];
568 static unsigned int eth_rcv_current, eth_rcv_last;
571 static struct eth_device *eth_devices;
572 struct eth_device *eth_current;
574 static void eth_set_current_to_next(void)
576 eth_current = eth_current->next;
579 static void eth_set_dev(struct eth_device *dev)
584 struct eth_device *eth_get_dev_by_name(const char *devname)
586 struct eth_device *dev, *target_dev;
588 BUG_ON(devname == NULL);
596 if (strcmp(devname, dev->name) == 0) {
601 } while (dev != eth_devices);
606 struct eth_device *eth_get_dev_by_index(int index)
608 struct eth_device *dev, *target_dev;
616 if (dev->index == index) {
621 } while (dev != eth_devices);
626 int eth_get_dev_index(void)
631 return eth_current->index;
634 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
637 unsigned char env_enetaddr[6];
640 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
642 if (!is_zero_ethaddr(env_enetaddr)) {
643 if (!is_zero_ethaddr(dev->enetaddr) &&
644 memcmp(dev->enetaddr, env_enetaddr, 6)) {
645 printf("\nWarning: %s MAC addresses don't match:\n",
647 printf("Address in SROM is %pM\n",
649 printf("Address in environment is %pM\n",
653 memcpy(dev->enetaddr, env_enetaddr, 6);
654 } else if (is_valid_ethaddr(dev->enetaddr)) {
655 eth_setenv_enetaddr_by_index(base_name, eth_number,
657 printf("\nWarning: %s using MAC address from net device\n",
659 } else if (is_zero_ethaddr(dev->enetaddr)) {
660 printf("\nError: %s address not set.\n",
665 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
666 if (!is_valid_ethaddr(dev->enetaddr)) {
667 printf("\nError: %s address %pM illegal value\n",
668 dev->name, dev->enetaddr);
672 ret = dev->write_hwaddr(dev);
674 printf("\nWarning: %s failed to set MAC address\n",
681 int eth_register(struct eth_device *dev)
683 struct eth_device *d;
686 assert(strlen(dev->name) < sizeof(dev->name));
691 eth_current_changed();
693 for (d = eth_devices; d->next != eth_devices; d = d->next)
698 dev->state = ETH_STATE_INIT;
699 dev->next = eth_devices;
700 dev->index = index++;
705 int eth_unregister(struct eth_device *dev)
707 struct eth_device *cur;
713 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
717 /* Device not found */
718 if (cur->next != dev)
721 cur->next = dev->next;
723 if (eth_devices == dev)
724 eth_devices = dev->next == eth_devices ? NULL : dev->next;
726 if (eth_current == dev) {
727 eth_current = eth_devices;
728 eth_current_changed();
734 int eth_initialize(void)
743 puts("No ethernet found.\n");
744 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
746 struct eth_device *dev = eth_devices;
747 char *ethprime = getenv("ethprime");
749 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
754 printf("%s", dev->name);
756 if (ethprime && strcmp(dev->name, ethprime) == 0) {
761 if (strchr(dev->name, ' '))
762 puts("\nWarning: eth device name has a space!"
765 eth_write_hwaddr(dev, "eth", dev->index);
769 } while (dev != eth_devices);
771 eth_current_changed();
778 #ifdef CONFIG_MCAST_TFTP
780 * mcast_addr: multicast ipaddr from which multicast Mac is made
781 * join: 1=join, 0=leave.
783 int eth_mcast_join(struct in_addr mcast_ip, int join)
786 if (!eth_current || !eth_current->mcast)
788 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
789 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
790 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
794 return eth_current->mcast(eth_current, mcast_mac, join);
797 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
798 * and this is the ethernet-crc method needed for TSEC -- and perhaps
799 * some other adapter -- hash tables
801 #define CRCPOLY_LE 0xedb88320
802 u32 ether_crc(size_t len, unsigned char const *p)
809 for (i = 0; i < 8; i++)
810 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
812 /* an reverse the bits, cuz of way they arrive -- last-first */
813 crc = (crc >> 16) | (crc << 16);
814 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
815 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
816 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
817 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
826 struct eth_device *old_current, *dev;
829 puts("No ethernet found.\n");
833 /* Sync environment with network devices */
836 uchar env_enetaddr[6];
838 if (eth_getenv_enetaddr_by_index("eth", dev->index,
840 memcpy(dev->enetaddr, env_enetaddr, 6);
843 } while (dev != eth_devices);
845 old_current = eth_current;
847 debug("Trying %s\n", eth_current->name);
849 if (eth_current->init(eth_current, gd->bd) >= 0) {
850 eth_current->state = ETH_STATE_ACTIVE;
857 } while (old_current != eth_current);
867 eth_current->halt(eth_current);
869 eth_current->state = ETH_STATE_PASSIVE;
872 int eth_send(void *packet, int length)
877 return eth_current->send(eth_current, packet, length);
885 return eth_current->recv(eth_current);
887 #endif /* ifndef CONFIG_DM_ETH */
890 static void eth_save_packet(void *packet, int length)
895 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
898 if (PKTSIZE < length)
901 for (i = 0; i < length; i++)
902 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
904 eth_rcv_bufs[eth_rcv_last].length = length;
905 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
908 int eth_receive(void *packet, int length)
911 void *pp = push_packet;
914 if (eth_rcv_current == eth_rcv_last) {
915 push_packet = eth_save_packet;
919 if (eth_rcv_current == eth_rcv_last)
923 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
925 for (i = 0; i < length; i++)
926 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
928 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
931 #endif /* CONFIG_API */
933 static void eth_current_changed(void)
935 char *act = getenv("ethact");
936 /* update current ethernet name */
938 if (act == NULL || strcmp(act, eth_get_name()) != 0)
939 setenv("ethact", eth_get_name());
942 * remove the variable completely if there is no active
945 else if (act != NULL)
946 setenv("ethact", NULL);
949 void eth_try_another(int first_restart)
951 static void *first_failed;
955 * Do not rotate between network interfaces when
956 * 'ethrotate' variable is set to 'no'.
958 ethrotate = getenv("ethrotate");
959 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
966 first_failed = eth_get_dev();
968 eth_set_current_to_next();
970 eth_current_changed();
972 if (first_failed == eth_get_dev())
973 net_restart_wrap = 1;
976 void eth_set_current(void)
979 static int env_changed_id;
982 env_id = get_env_id();
983 if ((act == NULL) || (env_changed_id != env_id)) {
984 act = getenv("ethact");
985 env_changed_id = env_id;
989 char *ethprime = getenv("ethprime");
993 dev = eth_get_dev_by_name(ethprime);
999 eth_set_dev(eth_get_dev_by_name(act));
1002 eth_current_changed();
1005 const char *eth_get_name(void)
1007 return eth_get_dev() ? eth_get_dev()->name : "unknown";