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);
87 * struct eth_device_priv - private structure for each Ethernet device
89 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
91 struct eth_device_priv {
92 enum eth_state_t state;
96 * struct eth_uclass_priv - The structure attached to the uclass itself
98 * @current: The Ethernet device that the network functions are using
100 struct eth_uclass_priv {
101 struct udevice *current;
104 /* eth_errno - This stores the most recent failure code from DM functions */
105 static int eth_errno;
107 static struct eth_uclass_priv *eth_get_uclass_priv(void)
111 uclass_get(UCLASS_ETH, &uc);
116 static void eth_set_current_to_next(void)
118 struct eth_uclass_priv *uc_priv;
120 uc_priv = eth_get_uclass_priv();
121 if (uc_priv->current)
122 uclass_next_device(&uc_priv->current);
123 if (!uc_priv->current)
124 uclass_first_device(UCLASS_ETH, &uc_priv->current);
128 * Typically this will simply return the active device.
129 * In the case where the most recent active device was unset, this will attempt
130 * to return the first device. If that device doesn't exist or fails to probe,
131 * this function will return NULL.
133 struct udevice *eth_get_dev(void)
135 struct eth_uclass_priv *uc_priv;
137 uc_priv = eth_get_uclass_priv();
138 if (!uc_priv->current)
139 eth_errno = uclass_first_device(UCLASS_ETH,
141 return uc_priv->current;
145 * Typically this will just store a device pointer.
146 * In case it was not probed, we will attempt to do so.
147 * dev may be NULL to unset the active device.
149 static void eth_set_dev(struct udevice *dev)
151 if (dev && !device_active(dev))
152 eth_errno = device_probe(dev);
153 eth_get_uclass_priv()->current = dev;
157 * Find the udevice that either has the name passed in as devname or has an
158 * alias named devname.
160 struct udevice *eth_get_dev_by_name(const char *devname)
164 const char *startp = NULL;
168 /* Must be longer than 3 to be an alias */
169 if (strlen(devname) > strlen("eth")) {
170 startp = devname + strlen("eth");
171 seq = simple_strtoul(startp, &endp, 10);
174 uclass_get(UCLASS_ETH, &uc);
175 uclass_foreach_dev(it, uc) {
177 * We need the seq to be valid, so try to probe it.
178 * If the probe fails, the seq will not match since it will be
179 * -1 instead of what we are looking for.
180 * We don't care about errors from probe here. Either they won't
181 * match an alias or it will match a literal name and we'll pick
182 * up the error when we try to probe again in eth_set_dev().
186 * Check for the name or the sequence number to match
188 if (strcmp(it->name, devname) == 0 ||
189 (endp > startp && it->seq == seq))
196 unsigned char *eth_get_ethaddr(void)
198 struct eth_pdata *pdata;
201 pdata = eth_get_dev()->platdata;
202 return pdata->enetaddr;
208 /* Set active state without calling start on the driver */
209 int eth_init_state_only(void)
211 struct udevice *current;
212 struct eth_device_priv *priv;
214 current = eth_get_dev();
215 if (!current || !device_active(current))
218 priv = current->uclass_priv;
219 priv->state = ETH_STATE_ACTIVE;
224 /* Set passive state without calling stop on the driver */
225 void eth_halt_state_only(void)
227 struct udevice *current;
228 struct eth_device_priv *priv;
230 current = eth_get_dev();
231 if (!current || !device_active(current))
234 priv = current->uclass_priv;
235 priv->state = ETH_STATE_PASSIVE;
238 int eth_get_dev_index(void)
241 return eth_get_dev()->seq;
247 struct udevice *current;
248 struct udevice *old_current;
251 current = eth_get_dev();
253 printf("No ethernet found.\n");
257 old_current = current;
259 debug("Trying %s\n", current->name);
261 if (device_active(current)) {
262 uchar env_enetaddr[6];
263 struct eth_pdata *pdata = current->platdata;
265 /* Sync environment with network device */
266 if (eth_getenv_enetaddr_by_index("eth", current->seq,
268 memcpy(pdata->enetaddr, env_enetaddr, 6);
270 memset(pdata->enetaddr, 0, 6);
272 ret = eth_get_ops(current)->start(current);
274 struct eth_device_priv *priv =
275 current->uclass_priv;
277 priv->state = ETH_STATE_ACTIVE;
287 * If ethrotate is enabled, this will change "current",
288 * otherwise we will drop out of this while loop immediately
291 /* This will ensure the new "current" attempted to probe */
292 current = eth_get_dev();
293 } while (old_current != current);
300 struct udevice *current;
301 struct eth_device_priv *priv;
303 current = eth_get_dev();
304 if (!current || !device_active(current))
307 eth_get_ops(current)->stop(current);
308 priv = current->uclass_priv;
309 priv->state = ETH_STATE_PASSIVE;
312 int eth_send(void *packet, int length)
314 struct udevice *current;
317 current = eth_get_dev();
321 if (!device_active(current))
324 ret = eth_get_ops(current)->send(current, packet, length);
326 /* We cannot completely return the error at present */
327 debug("%s: send() returned error %d\n", __func__, ret);
334 struct udevice *current;
339 current = eth_get_dev();
343 if (!device_active(current))
346 /* Process up to 32 packets at one time */
347 for (i = 0; i < 32; i++) {
348 ret = eth_get_ops(current)->recv(current, &packet);
350 net_process_received_packet(packet, ret);
351 if (ret >= 0 && eth_get_ops(current)->free_pkt)
352 eth_get_ops(current)->free_pkt(current, packet, ret);
359 /* We cannot completely return the error at present */
360 debug("%s: recv() returned error %d\n", __func__, ret);
365 static int eth_write_hwaddr(struct udevice *dev)
367 struct eth_pdata *pdata = dev->platdata;
370 if (!dev || !device_active(dev))
373 /* seq is valid since the device is active */
374 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
375 if (!is_valid_ethaddr(pdata->enetaddr)) {
376 printf("\nError: %s address %pM illegal value\n",
377 dev->name, pdata->enetaddr);
381 ret = eth_get_ops(dev)->write_hwaddr(dev);
383 printf("\nWarning: %s failed to set MAC address\n",
390 int eth_initialize(void)
395 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
399 * Devices need to write the hwaddr even if not started so that Linux
400 * will have access to the hwaddr that u-boot stored for the device.
401 * This is accomplished by attempting to probe each device and calling
402 * their write_hwaddr() operation.
404 uclass_first_device(UCLASS_ETH, &dev);
406 printf("No ethernet found.\n");
407 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
409 char *ethprime = getenv("ethprime");
410 struct udevice *prime_dev = NULL;
413 prime_dev = eth_get_dev_by_name(ethprime);
415 eth_set_dev(prime_dev);
416 eth_current_changed();
421 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
426 printf("eth%d: %s", dev->seq, dev->name);
428 if (ethprime && dev == prime_dev)
431 eth_write_hwaddr(dev);
433 uclass_next_device(&dev);
443 static int eth_post_bind(struct udevice *dev)
445 if (strchr(dev->name, ' ')) {
446 printf("\nError: eth device name \"%s\" has a space!\n",
454 static int eth_pre_unbind(struct udevice *dev)
456 /* Don't hang onto a pointer that is going away */
457 if (dev == eth_get_uclass_priv()->current)
463 static int eth_post_probe(struct udevice *dev)
465 struct eth_device_priv *priv = dev->uclass_priv;
466 struct eth_pdata *pdata = dev->platdata;
467 unsigned char env_enetaddr[6];
469 priv->state = ETH_STATE_INIT;
471 /* Check if the device has a MAC address in ROM */
472 if (eth_get_ops(dev)->read_rom_hwaddr)
473 eth_get_ops(dev)->read_rom_hwaddr(dev);
475 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
476 if (!is_zero_ethaddr(env_enetaddr)) {
477 if (!is_zero_ethaddr(pdata->enetaddr) &&
478 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
479 printf("\nWarning: %s MAC addresses don't match:\n",
481 printf("Address in SROM is %pM\n",
483 printf("Address in environment is %pM\n",
487 /* Override the ROM MAC address */
488 memcpy(pdata->enetaddr, env_enetaddr, 6);
489 } else if (is_valid_ethaddr(pdata->enetaddr)) {
490 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
491 printf("\nWarning: %s using MAC address from ROM\n",
493 } else if (is_zero_ethaddr(pdata->enetaddr)) {
494 printf("\nError: %s address not set.\n",
502 static int eth_pre_remove(struct udevice *dev)
504 eth_get_ops(dev)->stop(dev);
509 UCLASS_DRIVER(eth) = {
512 .post_bind = eth_post_bind,
513 .pre_unbind = eth_pre_unbind,
514 .post_probe = eth_post_probe,
515 .pre_remove = eth_pre_remove,
516 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
517 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
518 .flags = DM_UC_FLAG_SEQ_ALIAS,
522 #ifndef CONFIG_DM_ETH
524 * CPU and board-specific Ethernet initializations. Aliased function
525 * signals caller to move on
527 static int __def_eth_init(bd_t *bis)
531 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
532 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
538 } eth_rcv_bufs[PKTBUFSRX];
540 static unsigned int eth_rcv_current, eth_rcv_last;
543 static struct eth_device *eth_devices;
544 struct eth_device *eth_current;
546 static void eth_set_current_to_next(void)
548 eth_current = eth_current->next;
551 static void eth_set_dev(struct eth_device *dev)
556 struct eth_device *eth_get_dev_by_name(const char *devname)
558 struct eth_device *dev, *target_dev;
560 BUG_ON(devname == NULL);
568 if (strcmp(devname, dev->name) == 0) {
573 } while (dev != eth_devices);
578 struct eth_device *eth_get_dev_by_index(int index)
580 struct eth_device *dev, *target_dev;
588 if (dev->index == index) {
593 } while (dev != eth_devices);
598 int eth_get_dev_index(void)
603 return eth_current->index;
606 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
609 unsigned char env_enetaddr[6];
612 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
614 if (!is_zero_ethaddr(env_enetaddr)) {
615 if (!is_zero_ethaddr(dev->enetaddr) &&
616 memcmp(dev->enetaddr, env_enetaddr, 6)) {
617 printf("\nWarning: %s MAC addresses don't match:\n",
619 printf("Address in SROM is %pM\n",
621 printf("Address in environment is %pM\n",
625 memcpy(dev->enetaddr, env_enetaddr, 6);
626 } else if (is_valid_ethaddr(dev->enetaddr)) {
627 eth_setenv_enetaddr_by_index(base_name, eth_number,
629 printf("\nWarning: %s using MAC address from net device\n",
631 } else if (is_zero_ethaddr(dev->enetaddr)) {
632 printf("\nError: %s address not set.\n",
637 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
638 if (!is_valid_ethaddr(dev->enetaddr)) {
639 printf("\nError: %s address %pM illegal value\n",
640 dev->name, dev->enetaddr);
644 ret = dev->write_hwaddr(dev);
646 printf("\nWarning: %s failed to set MAC address\n",
653 int eth_register(struct eth_device *dev)
655 struct eth_device *d;
658 assert(strlen(dev->name) < sizeof(dev->name));
663 eth_current_changed();
665 for (d = eth_devices; d->next != eth_devices; d = d->next)
670 dev->state = ETH_STATE_INIT;
671 dev->next = eth_devices;
672 dev->index = index++;
677 int eth_unregister(struct eth_device *dev)
679 struct eth_device *cur;
685 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
689 /* Device not found */
690 if (cur->next != dev)
693 cur->next = dev->next;
695 if (eth_devices == dev)
696 eth_devices = dev->next == eth_devices ? NULL : dev->next;
698 if (eth_current == dev) {
699 eth_current = eth_devices;
700 eth_current_changed();
706 int eth_initialize(void)
712 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
713 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
724 * If board-specific initialization exists, call it.
725 * If not, call a CPU-specific one
727 if (board_eth_init != __def_eth_init) {
728 if (board_eth_init(gd->bd) < 0)
729 printf("Board Net Initialization Failed\n");
730 } else if (cpu_eth_init != __def_eth_init) {
731 if (cpu_eth_init(gd->bd) < 0)
732 printf("CPU Net Initialization Failed\n");
734 printf("Net Initialization Skipped\n");
738 puts("No ethernet found.\n");
739 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
741 struct eth_device *dev = eth_devices;
742 char *ethprime = getenv("ethprime");
744 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
749 printf("%s", dev->name);
751 if (ethprime && strcmp(dev->name, ethprime) == 0) {
756 if (strchr(dev->name, ' '))
757 puts("\nWarning: eth device name has a space!"
760 eth_write_hwaddr(dev, "eth", dev->index);
764 } while (dev != eth_devices);
766 eth_current_changed();
773 #ifdef CONFIG_MCAST_TFTP
775 * mcast_addr: multicast ipaddr from which multicast Mac is made
776 * join: 1=join, 0=leave.
778 int eth_mcast_join(struct in_addr mcast_ip, int join)
781 if (!eth_current || !eth_current->mcast)
783 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
784 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
785 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
789 return eth_current->mcast(eth_current, mcast_mac, join);
792 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
793 * and this is the ethernet-crc method needed for TSEC -- and perhaps
794 * some other adapter -- hash tables
796 #define CRCPOLY_LE 0xedb88320
797 u32 ether_crc(size_t len, unsigned char const *p)
804 for (i = 0; i < 8; i++)
805 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
807 /* an reverse the bits, cuz of way they arrive -- last-first */
808 crc = (crc >> 16) | (crc << 16);
809 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
810 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
811 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
812 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
821 struct eth_device *old_current, *dev;
824 puts("No ethernet found.\n");
828 /* Sync environment with network devices */
831 uchar env_enetaddr[6];
833 if (eth_getenv_enetaddr_by_index("eth", dev->index,
835 memcpy(dev->enetaddr, env_enetaddr, 6);
838 } while (dev != eth_devices);
840 old_current = eth_current;
842 debug("Trying %s\n", eth_current->name);
844 if (eth_current->init(eth_current, gd->bd) >= 0) {
845 eth_current->state = ETH_STATE_ACTIVE;
852 } while (old_current != eth_current);
862 eth_current->halt(eth_current);
864 eth_current->state = ETH_STATE_PASSIVE;
867 int eth_send(void *packet, int length)
872 return eth_current->send(eth_current, packet, length);
880 return eth_current->recv(eth_current);
882 #endif /* ifndef CONFIG_DM_ETH */
885 static void eth_save_packet(void *packet, int length)
890 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
893 if (PKTSIZE < length)
896 for (i = 0; i < length; i++)
897 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
899 eth_rcv_bufs[eth_rcv_last].length = length;
900 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
903 int eth_receive(void *packet, int length)
906 void *pp = push_packet;
909 if (eth_rcv_current == eth_rcv_last) {
910 push_packet = eth_save_packet;
914 if (eth_rcv_current == eth_rcv_last)
918 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
920 for (i = 0; i < length; i++)
921 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
923 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
926 #endif /* CONFIG_API */
928 static void eth_current_changed(void)
930 char *act = getenv("ethact");
931 /* update current ethernet name */
933 if (act == NULL || strcmp(act, eth_get_name()) != 0)
934 setenv("ethact", eth_get_name());
937 * remove the variable completely if there is no active
940 else if (act != NULL)
941 setenv("ethact", NULL);
944 void eth_try_another(int first_restart)
946 static void *first_failed;
950 * Do not rotate between network interfaces when
951 * 'ethrotate' variable is set to 'no'.
953 ethrotate = getenv("ethrotate");
954 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
961 first_failed = eth_get_dev();
963 eth_set_current_to_next();
965 eth_current_changed();
967 if (first_failed == eth_get_dev())
968 net_restart_wrap = 1;
971 void eth_set_current(void)
974 static int env_changed_id;
977 env_id = get_env_id();
978 if ((act == NULL) || (env_changed_id != env_id)) {
979 act = getenv("ethact");
980 env_changed_id = env_id;
984 char *ethprime = getenv("ethprime");
988 dev = eth_get_dev_by_name(ethprime);
994 eth_set_dev(eth_get_dev_by_name(act));
997 eth_current_changed();
1000 const char *eth_get_name(void)
1002 return eth_get_dev() ? eth_get_dev()->name : "unknown";