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;
138 unsigned char *eth_get_ethaddr(void)
140 struct eth_pdata *pdata;
143 pdata = eth_get_dev()->platdata;
144 return pdata->enetaddr;
150 /* Set active state without calling start on the driver */
151 int eth_init_state_only(void)
153 struct udevice *current;
154 struct eth_device_priv *priv;
156 current = eth_get_dev();
157 if (!current || !device_active(current))
160 priv = current->uclass_priv;
161 priv->state = ETH_STATE_ACTIVE;
166 /* Set passive state without calling stop on the driver */
167 void eth_halt_state_only(void)
169 struct udevice *current;
170 struct eth_device_priv *priv;
172 current = eth_get_dev();
173 if (!current || !device_active(current))
176 priv = current->uclass_priv;
177 priv->state = ETH_STATE_PASSIVE;
180 int eth_get_dev_index(void)
183 return eth_get_dev()->seq;
189 struct udevice *current;
190 struct udevice *old_current;
192 current = eth_get_dev();
194 printf("No ethernet found.\n");
198 old_current = current;
200 debug("Trying %s\n", current->name);
202 if (device_active(current)) {
203 uchar env_enetaddr[6];
204 struct eth_pdata *pdata = current->platdata;
206 /* Sync environment with network device */
207 if (eth_getenv_enetaddr_by_index("eth", current->seq,
209 memcpy(pdata->enetaddr, env_enetaddr, 6);
211 memset(pdata->enetaddr, 0, 6);
213 if (eth_get_ops(current)->start(current) >= 0) {
214 struct eth_device_priv *priv =
215 current->uclass_priv;
217 priv->state = ETH_STATE_ACTIVE;
223 /* This will ensure the new "current" attempted to probe */
225 current = eth_get_dev();
226 } while (old_current != current);
233 struct udevice *current;
234 struct eth_device_priv *priv;
236 current = eth_get_dev();
237 if (!current || !device_active(current))
240 eth_get_ops(current)->stop(current);
241 priv = current->uclass_priv;
242 priv->state = ETH_STATE_PASSIVE;
245 int eth_send(void *packet, int length)
247 struct udevice *current;
249 current = eth_get_dev();
253 if (!device_active(current))
256 return eth_get_ops(current)->send(current, packet, length);
261 struct udevice *current;
266 current = eth_get_dev();
270 if (!device_active(current))
273 /* Process up to 32 packets at one time */
274 for (i = 0; i < 32; i++) {
275 ret = eth_get_ops(current)->recv(current, &packet);
277 net_process_received_packet(packet, ret);
286 static int eth_write_hwaddr(struct udevice *dev)
288 struct eth_pdata *pdata = dev->platdata;
291 if (!dev || !device_active(dev))
294 /* seq is valid since the device is active */
295 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
296 if (!is_valid_ether_addr(pdata->enetaddr)) {
297 printf("\nError: %s address %pM illegal value\n",
298 dev->name, pdata->enetaddr);
302 ret = eth_get_ops(dev)->write_hwaddr(dev);
304 printf("\nWarning: %s failed to set MAC address\n",
311 int eth_initialize(void)
316 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
320 * Devices need to write the hwaddr even if not started so that Linux
321 * will have access to the hwaddr that u-boot stored for the device.
322 * This is accomplished by attempting to probe each device and calling
323 * their write_hwaddr() operation.
325 uclass_first_device(UCLASS_ETH, &dev);
327 printf("No ethernet found.\n");
328 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
330 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
335 printf("eth%d: %s", dev->seq, dev->name);
337 eth_write_hwaddr(dev);
339 uclass_next_device(&dev);
349 static int eth_post_bind(struct udevice *dev)
351 if (strchr(dev->name, ' ')) {
352 printf("\nError: eth device name \"%s\" has a space!\n",
360 static int eth_pre_unbind(struct udevice *dev)
362 /* Don't hang onto a pointer that is going away */
363 if (dev == eth_get_uclass_priv()->current)
369 static int eth_post_probe(struct udevice *dev)
371 struct eth_device_priv *priv = dev->uclass_priv;
372 struct eth_pdata *pdata = dev->platdata;
373 unsigned char env_enetaddr[6];
375 priv->state = ETH_STATE_INIT;
377 /* Check if the device has a MAC address in ROM */
378 if (eth_get_ops(dev)->read_rom_hwaddr)
379 eth_get_ops(dev)->read_rom_hwaddr(dev);
381 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
382 if (!is_zero_ether_addr(env_enetaddr)) {
383 if (!is_zero_ether_addr(pdata->enetaddr) &&
384 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
385 printf("\nWarning: %s MAC addresses don't match:\n",
387 printf("Address in SROM is %pM\n",
389 printf("Address in environment is %pM\n",
393 /* Override the ROM MAC address */
394 memcpy(pdata->enetaddr, env_enetaddr, 6);
395 } else if (is_valid_ether_addr(pdata->enetaddr)) {
396 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
397 printf("\nWarning: %s using MAC address from ROM\n",
399 } else if (is_zero_ether_addr(pdata->enetaddr)) {
400 printf("\nError: %s address not set.\n",
408 static int eth_pre_remove(struct udevice *dev)
410 eth_get_ops(dev)->stop(dev);
415 UCLASS_DRIVER(eth) = {
418 .post_bind = eth_post_bind,
419 .pre_unbind = eth_pre_unbind,
420 .post_probe = eth_post_probe,
421 .pre_remove = eth_pre_remove,
422 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
423 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
427 #ifndef CONFIG_DM_ETH
429 * CPU and board-specific Ethernet initializations. Aliased function
430 * signals caller to move on
432 static int __def_eth_init(bd_t *bis)
436 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
437 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
443 } eth_rcv_bufs[PKTBUFSRX];
445 static unsigned int eth_rcv_current, eth_rcv_last;
448 static struct eth_device *eth_devices;
449 struct eth_device *eth_current;
451 static void eth_set_current_to_next(void)
453 eth_current = eth_current->next;
456 struct eth_device *eth_get_dev_by_name(const char *devname)
458 struct eth_device *dev, *target_dev;
460 BUG_ON(devname == NULL);
468 if (strcmp(devname, dev->name) == 0) {
473 } while (dev != eth_devices);
478 struct eth_device *eth_get_dev_by_index(int index)
480 struct eth_device *dev, *target_dev;
488 if (dev->index == index) {
493 } while (dev != eth_devices);
498 int eth_get_dev_index(void)
503 return eth_current->index;
506 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
509 unsigned char env_enetaddr[6];
512 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
514 if (!is_zero_ether_addr(env_enetaddr)) {
515 if (!is_zero_ether_addr(dev->enetaddr) &&
516 memcmp(dev->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 memcpy(dev->enetaddr, env_enetaddr, 6);
526 } else if (is_valid_ether_addr(dev->enetaddr)) {
527 eth_setenv_enetaddr_by_index(base_name, eth_number,
529 printf("\nWarning: %s using MAC address from net device\n",
531 } else if (is_zero_ether_addr(dev->enetaddr)) {
532 printf("\nError: %s address not set.\n",
537 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
538 if (!is_valid_ether_addr(dev->enetaddr)) {
539 printf("\nError: %s address %pM illegal value\n",
540 dev->name, dev->enetaddr);
544 ret = dev->write_hwaddr(dev);
546 printf("\nWarning: %s failed to set MAC address\n", dev->name);
552 int eth_register(struct eth_device *dev)
554 struct eth_device *d;
557 assert(strlen(dev->name) < sizeof(dev->name));
560 eth_current = eth_devices = dev;
561 eth_current_changed();
563 for (d = eth_devices; d->next != eth_devices; d = d->next)
568 dev->state = ETH_STATE_INIT;
569 dev->next = eth_devices;
570 dev->index = index++;
575 int eth_unregister(struct eth_device *dev)
577 struct eth_device *cur;
583 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
587 /* Device not found */
588 if (cur->next != dev)
591 cur->next = dev->next;
593 if (eth_devices == dev)
594 eth_devices = dev->next == eth_devices ? NULL : dev->next;
596 if (eth_current == dev) {
597 eth_current = eth_devices;
598 eth_current_changed();
604 int eth_initialize(void)
610 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
611 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
622 * If board-specific initialization exists, call it.
623 * If not, call a CPU-specific one
625 if (board_eth_init != __def_eth_init) {
626 if (board_eth_init(gd->bd) < 0)
627 printf("Board Net Initialization Failed\n");
628 } else if (cpu_eth_init != __def_eth_init) {
629 if (cpu_eth_init(gd->bd) < 0)
630 printf("CPU Net Initialization Failed\n");
632 printf("Net Initialization Skipped\n");
635 puts("No ethernet found.\n");
636 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
638 struct eth_device *dev = eth_devices;
639 char *ethprime = getenv("ethprime");
641 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
646 printf("%s", dev->name);
648 if (ethprime && strcmp(dev->name, ethprime) == 0) {
653 if (strchr(dev->name, ' '))
654 puts("\nWarning: eth device name has a space!"
657 eth_write_hwaddr(dev, "eth", dev->index);
661 } while (dev != eth_devices);
663 eth_current_changed();
670 #ifdef CONFIG_MCAST_TFTP
672 * mcast_addr: multicast ipaddr from which multicast Mac is made
673 * join: 1=join, 0=leave.
675 int eth_mcast_join(IPaddr_t mcast_ip, int join)
678 if (!eth_current || !eth_current->mcast)
680 mcast_mac[5] = htonl(mcast_ip) & 0xff;
681 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
682 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
686 return eth_current->mcast(eth_current, mcast_mac, join);
689 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
690 * and this is the ethernet-crc method needed for TSEC -- and perhaps
691 * some other adapter -- hash tables
693 #define CRCPOLY_LE 0xedb88320
694 u32 ether_crc(size_t len, unsigned char const *p)
701 for (i = 0; i < 8; i++)
702 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
704 /* an reverse the bits, cuz of way they arrive -- last-first */
705 crc = (crc >> 16) | (crc << 16);
706 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
707 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
708 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
709 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
718 struct eth_device *old_current, *dev;
721 puts("No ethernet found.\n");
725 /* Sync environment with network devices */
728 uchar env_enetaddr[6];
730 if (eth_getenv_enetaddr_by_index("eth", dev->index,
732 memcpy(dev->enetaddr, env_enetaddr, 6);
735 } while (dev != eth_devices);
737 old_current = eth_current;
739 debug("Trying %s\n", eth_current->name);
741 if (eth_current->init(eth_current, gd->bd) >= 0) {
742 eth_current->state = ETH_STATE_ACTIVE;
749 } while (old_current != eth_current);
759 eth_current->halt(eth_current);
761 eth_current->state = ETH_STATE_PASSIVE;
764 int eth_send(void *packet, int length)
769 return eth_current->send(eth_current, packet, length);
777 return eth_current->recv(eth_current);
779 #endif /* ifndef CONFIG_DM_ETH */
782 static void eth_save_packet(void *packet, int length)
787 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
790 if (PKTSIZE < length)
793 for (i = 0; i < length; i++)
794 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
796 eth_rcv_bufs[eth_rcv_last].length = length;
797 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
800 int eth_receive(void *packet, int length)
803 void *pp = push_packet;
806 if (eth_rcv_current == eth_rcv_last) {
807 push_packet = eth_save_packet;
811 if (eth_rcv_current == eth_rcv_last)
815 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
817 for (i = 0; i < length; i++)
818 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
820 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
823 #endif /* CONFIG_API */
825 static void eth_current_changed(void)
827 char *act = getenv("ethact");
828 /* update current ethernet name */
830 if (act == NULL || strcmp(act, eth_get_name()) != 0)
831 setenv("ethact", eth_get_name());
834 * remove the variable completely if there is no active
837 else if (act != NULL)
838 setenv("ethact", NULL);
841 void eth_try_another(int first_restart)
843 static void *first_failed;
847 * Do not rotate between network interfaces when
848 * 'ethrotate' variable is set to 'no'.
850 ethrotate = getenv("ethrotate");
851 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
858 first_failed = eth_get_dev();
860 eth_set_current_to_next();
862 eth_current_changed();
864 if (first_failed == eth_get_dev())
868 void eth_set_current(void)
871 static int env_changed_id;
875 env_id = get_env_id();
876 if ((act == NULL) || (env_changed_id != env_id)) {
877 act = getenv("ethact");
878 env_changed_id = env_id;
881 old_current = eth_get_dev();
883 if (strcmp(eth_get_name(), act) == 0)
885 eth_set_current_to_next();
886 } while (old_current != eth_get_dev());
889 eth_current_changed();
892 const char *eth_get_name(void)
894 return eth_get_dev() ? eth_get_dev()->name : "unknown";