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 static struct eth_uclass_priv *eth_get_uclass_priv(void)
105 uclass_get(UCLASS_ETH, &uc);
110 static void eth_set_current_to_next(void)
112 struct eth_uclass_priv *uc_priv;
114 uc_priv = eth_get_uclass_priv();
115 if (uc_priv->current)
116 uclass_next_device(&uc_priv->current);
117 if (!uc_priv->current)
118 uclass_first_device(UCLASS_ETH, &uc_priv->current);
121 struct udevice *eth_get_dev(void)
123 struct eth_uclass_priv *uc_priv;
125 uc_priv = eth_get_uclass_priv();
126 if (!uc_priv->current)
127 uclass_first_device(UCLASS_ETH,
129 return uc_priv->current;
132 static void eth_set_dev(struct udevice *dev)
135 eth_get_uclass_priv()->current = dev;
139 * Find the udevice that either has the name passed in as devname or has an
140 * alias named devname.
142 struct udevice *eth_get_dev_by_name(const char *devname)
146 const char *startp = NULL;
150 /* Must be longer than 3 to be an alias */
151 if (strlen(devname) > strlen("eth")) {
152 startp = devname + strlen("eth");
153 seq = simple_strtoul(startp, &endp, 10);
156 uclass_get(UCLASS_ETH, &uc);
157 uclass_foreach_dev(it, uc) {
158 /* We need the seq to be valid, so make sure it's probed */
161 * Check for the name or the sequence number to match
163 if (strcmp(it->name, devname) == 0 ||
164 (endp > startp && it->seq == seq))
171 unsigned char *eth_get_ethaddr(void)
173 struct eth_pdata *pdata;
176 pdata = eth_get_dev()->platdata;
177 return pdata->enetaddr;
183 /* Set active state without calling start on the driver */
184 int eth_init_state_only(void)
186 struct udevice *current;
187 struct eth_device_priv *priv;
189 current = eth_get_dev();
190 if (!current || !device_active(current))
193 priv = current->uclass_priv;
194 priv->state = ETH_STATE_ACTIVE;
199 /* Set passive state without calling stop on the driver */
200 void eth_halt_state_only(void)
202 struct udevice *current;
203 struct eth_device_priv *priv;
205 current = eth_get_dev();
206 if (!current || !device_active(current))
209 priv = current->uclass_priv;
210 priv->state = ETH_STATE_PASSIVE;
213 int eth_get_dev_index(void)
216 return eth_get_dev()->seq;
222 struct udevice *current;
223 struct udevice *old_current;
225 current = eth_get_dev();
227 printf("No ethernet found.\n");
231 old_current = current;
233 debug("Trying %s\n", current->name);
235 if (device_active(current)) {
236 uchar env_enetaddr[6];
237 struct eth_pdata *pdata = current->platdata;
239 /* Sync environment with network device */
240 if (eth_getenv_enetaddr_by_index("eth", current->seq,
242 memcpy(pdata->enetaddr, env_enetaddr, 6);
244 memset(pdata->enetaddr, 0, 6);
246 if (eth_get_ops(current)->start(current) >= 0) {
247 struct eth_device_priv *priv =
248 current->uclass_priv;
250 priv->state = ETH_STATE_ACTIVE;
256 /* This will ensure the new "current" attempted to probe */
258 current = eth_get_dev();
259 } while (old_current != current);
266 struct udevice *current;
267 struct eth_device_priv *priv;
269 current = eth_get_dev();
270 if (!current || !device_active(current))
273 eth_get_ops(current)->stop(current);
274 priv = current->uclass_priv;
275 priv->state = ETH_STATE_PASSIVE;
278 int eth_send(void *packet, int length)
280 struct udevice *current;
282 current = eth_get_dev();
286 if (!device_active(current))
289 return eth_get_ops(current)->send(current, packet, length);
294 struct udevice *current;
299 current = eth_get_dev();
303 if (!device_active(current))
306 /* Process up to 32 packets at one time */
307 for (i = 0; i < 32; i++) {
308 ret = eth_get_ops(current)->recv(current, &packet);
310 net_process_received_packet(packet, ret);
319 static int eth_write_hwaddr(struct udevice *dev)
321 struct eth_pdata *pdata = dev->platdata;
324 if (!dev || !device_active(dev))
327 /* seq is valid since the device is active */
328 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
329 if (!is_valid_ether_addr(pdata->enetaddr)) {
330 printf("\nError: %s address %pM illegal value\n",
331 dev->name, pdata->enetaddr);
335 ret = eth_get_ops(dev)->write_hwaddr(dev);
337 printf("\nWarning: %s failed to set MAC address\n",
344 int eth_initialize(void)
349 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
353 * Devices need to write the hwaddr even if not started so that Linux
354 * will have access to the hwaddr that u-boot stored for the device.
355 * This is accomplished by attempting to probe each device and calling
356 * their write_hwaddr() operation.
358 uclass_first_device(UCLASS_ETH, &dev);
360 printf("No ethernet found.\n");
361 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
363 char *ethprime = getenv("ethprime");
364 struct udevice *prime_dev = NULL;
367 prime_dev = eth_get_dev_by_name(ethprime);
369 eth_set_dev(prime_dev);
370 eth_current_changed();
375 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
380 printf("eth%d: %s", dev->seq, dev->name);
382 if (ethprime && dev == prime_dev)
385 eth_write_hwaddr(dev);
387 uclass_next_device(&dev);
397 static int eth_post_bind(struct udevice *dev)
399 if (strchr(dev->name, ' ')) {
400 printf("\nError: eth device name \"%s\" has a space!\n",
408 static int eth_pre_unbind(struct udevice *dev)
410 /* Don't hang onto a pointer that is going away */
411 if (dev == eth_get_uclass_priv()->current)
417 static int eth_post_probe(struct udevice *dev)
419 struct eth_device_priv *priv = dev->uclass_priv;
420 struct eth_pdata *pdata = dev->platdata;
421 unsigned char env_enetaddr[6];
423 priv->state = ETH_STATE_INIT;
425 /* Check if the device has a MAC address in ROM */
426 if (eth_get_ops(dev)->read_rom_hwaddr)
427 eth_get_ops(dev)->read_rom_hwaddr(dev);
429 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
430 if (!is_zero_ether_addr(env_enetaddr)) {
431 if (!is_zero_ether_addr(pdata->enetaddr) &&
432 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
433 printf("\nWarning: %s MAC addresses don't match:\n",
435 printf("Address in SROM is %pM\n",
437 printf("Address in environment is %pM\n",
441 /* Override the ROM MAC address */
442 memcpy(pdata->enetaddr, env_enetaddr, 6);
443 } else if (is_valid_ether_addr(pdata->enetaddr)) {
444 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
445 printf("\nWarning: %s using MAC address from ROM\n",
447 } else if (is_zero_ether_addr(pdata->enetaddr)) {
448 printf("\nError: %s address not set.\n",
456 static int eth_pre_remove(struct udevice *dev)
458 eth_get_ops(dev)->stop(dev);
463 UCLASS_DRIVER(eth) = {
466 .post_bind = eth_post_bind,
467 .pre_unbind = eth_pre_unbind,
468 .post_probe = eth_post_probe,
469 .pre_remove = eth_pre_remove,
470 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
471 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
472 .flags = DM_UC_FLAG_SEQ_ALIAS,
476 #ifndef CONFIG_DM_ETH
478 * CPU and board-specific Ethernet initializations. Aliased function
479 * signals caller to move on
481 static int __def_eth_init(bd_t *bis)
485 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
486 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
492 } eth_rcv_bufs[PKTBUFSRX];
494 static unsigned int eth_rcv_current, eth_rcv_last;
497 static struct eth_device *eth_devices;
498 struct eth_device *eth_current;
500 static void eth_set_current_to_next(void)
502 eth_current = eth_current->next;
505 static void eth_set_dev(struct eth_device *dev)
510 struct eth_device *eth_get_dev_by_name(const char *devname)
512 struct eth_device *dev, *target_dev;
514 BUG_ON(devname == NULL);
522 if (strcmp(devname, dev->name) == 0) {
527 } while (dev != eth_devices);
532 struct eth_device *eth_get_dev_by_index(int index)
534 struct eth_device *dev, *target_dev;
542 if (dev->index == index) {
547 } while (dev != eth_devices);
552 int eth_get_dev_index(void)
557 return eth_current->index;
560 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
563 unsigned char env_enetaddr[6];
566 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
568 if (!is_zero_ether_addr(env_enetaddr)) {
569 if (!is_zero_ether_addr(dev->enetaddr) &&
570 memcmp(dev->enetaddr, env_enetaddr, 6)) {
571 printf("\nWarning: %s MAC addresses don't match:\n",
573 printf("Address in SROM is %pM\n",
575 printf("Address in environment is %pM\n",
579 memcpy(dev->enetaddr, env_enetaddr, 6);
580 } else if (is_valid_ether_addr(dev->enetaddr)) {
581 eth_setenv_enetaddr_by_index(base_name, eth_number,
583 printf("\nWarning: %s using MAC address from net device\n",
585 } else if (is_zero_ether_addr(dev->enetaddr)) {
586 printf("\nError: %s address not set.\n",
591 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
592 if (!is_valid_ether_addr(dev->enetaddr)) {
593 printf("\nError: %s address %pM illegal value\n",
594 dev->name, dev->enetaddr);
598 ret = dev->write_hwaddr(dev);
600 printf("\nWarning: %s failed to set MAC address\n", dev->name);
606 int eth_register(struct eth_device *dev)
608 struct eth_device *d;
611 assert(strlen(dev->name) < sizeof(dev->name));
614 eth_current = eth_devices = dev;
615 eth_current_changed();
617 for (d = eth_devices; d->next != eth_devices; d = d->next)
622 dev->state = ETH_STATE_INIT;
623 dev->next = eth_devices;
624 dev->index = index++;
629 int eth_unregister(struct eth_device *dev)
631 struct eth_device *cur;
637 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
641 /* Device not found */
642 if (cur->next != dev)
645 cur->next = dev->next;
647 if (eth_devices == dev)
648 eth_devices = dev->next == eth_devices ? NULL : dev->next;
650 if (eth_current == dev) {
651 eth_current = eth_devices;
652 eth_current_changed();
658 int eth_initialize(void)
664 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
665 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
676 * If board-specific initialization exists, call it.
677 * If not, call a CPU-specific one
679 if (board_eth_init != __def_eth_init) {
680 if (board_eth_init(gd->bd) < 0)
681 printf("Board Net Initialization Failed\n");
682 } else if (cpu_eth_init != __def_eth_init) {
683 if (cpu_eth_init(gd->bd) < 0)
684 printf("CPU Net Initialization Failed\n");
686 printf("Net Initialization Skipped\n");
689 puts("No ethernet found.\n");
690 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
692 struct eth_device *dev = eth_devices;
693 char *ethprime = getenv("ethprime");
695 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
700 printf("%s", dev->name);
702 if (ethprime && strcmp(dev->name, ethprime) == 0) {
707 if (strchr(dev->name, ' '))
708 puts("\nWarning: eth device name has a space!"
711 eth_write_hwaddr(dev, "eth", dev->index);
715 } while (dev != eth_devices);
717 eth_current_changed();
724 #ifdef CONFIG_MCAST_TFTP
726 * mcast_addr: multicast ipaddr from which multicast Mac is made
727 * join: 1=join, 0=leave.
729 int eth_mcast_join(IPaddr_t mcast_ip, int join)
732 if (!eth_current || !eth_current->mcast)
734 mcast_mac[5] = htonl(mcast_ip) & 0xff;
735 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
736 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
740 return eth_current->mcast(eth_current, mcast_mac, join);
743 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
744 * and this is the ethernet-crc method needed for TSEC -- and perhaps
745 * some other adapter -- hash tables
747 #define CRCPOLY_LE 0xedb88320
748 u32 ether_crc(size_t len, unsigned char const *p)
755 for (i = 0; i < 8; i++)
756 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
758 /* an reverse the bits, cuz of way they arrive -- last-first */
759 crc = (crc >> 16) | (crc << 16);
760 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
761 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
762 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
763 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
772 struct eth_device *old_current, *dev;
775 puts("No ethernet found.\n");
779 /* Sync environment with network devices */
782 uchar env_enetaddr[6];
784 if (eth_getenv_enetaddr_by_index("eth", dev->index,
786 memcpy(dev->enetaddr, env_enetaddr, 6);
789 } while (dev != eth_devices);
791 old_current = eth_current;
793 debug("Trying %s\n", eth_current->name);
795 if (eth_current->init(eth_current, gd->bd) >= 0) {
796 eth_current->state = ETH_STATE_ACTIVE;
803 } while (old_current != eth_current);
813 eth_current->halt(eth_current);
815 eth_current->state = ETH_STATE_PASSIVE;
818 int eth_send(void *packet, int length)
823 return eth_current->send(eth_current, packet, length);
831 return eth_current->recv(eth_current);
833 #endif /* ifndef CONFIG_DM_ETH */
836 static void eth_save_packet(void *packet, int length)
841 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
844 if (PKTSIZE < length)
847 for (i = 0; i < length; i++)
848 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
850 eth_rcv_bufs[eth_rcv_last].length = length;
851 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
854 int eth_receive(void *packet, int length)
857 void *pp = push_packet;
860 if (eth_rcv_current == eth_rcv_last) {
861 push_packet = eth_save_packet;
865 if (eth_rcv_current == eth_rcv_last)
869 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
871 for (i = 0; i < length; i++)
872 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
874 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
877 #endif /* CONFIG_API */
879 static void eth_current_changed(void)
881 char *act = getenv("ethact");
882 /* update current ethernet name */
884 if (act == NULL || strcmp(act, eth_get_name()) != 0)
885 setenv("ethact", eth_get_name());
888 * remove the variable completely if there is no active
891 else if (act != NULL)
892 setenv("ethact", NULL);
895 void eth_try_another(int first_restart)
897 static void *first_failed;
901 * Do not rotate between network interfaces when
902 * 'ethrotate' variable is set to 'no'.
904 ethrotate = getenv("ethrotate");
905 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
912 first_failed = eth_get_dev();
914 eth_set_current_to_next();
916 eth_current_changed();
918 if (first_failed == eth_get_dev())
922 void eth_set_current(void)
925 static int env_changed_id;
928 env_id = get_env_id();
929 if ((act == NULL) || (env_changed_id != env_id)) {
930 act = getenv("ethact");
931 env_changed_id = env_id;
935 char *ethprime = getenv("ethprime");
939 dev = eth_get_dev_by_name(ethprime);
945 eth_set_dev(eth_get_dev_by_name(act));
948 eth_current_changed();
951 const char *eth_get_name(void)
953 return eth_get_dev() ? eth_get_dev()->name : "unknown";