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+
12 #include <environment.h>
16 #include <asm/errno.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
19 #include "eth_internal.h"
21 DECLARE_GLOBAL_DATA_PTR;
24 * CPU and board-specific Ethernet initializations. Aliased function
25 * signals caller to move on
27 static int __def_eth_init(bd_t *bis)
31 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
32 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
36 * struct eth_device_priv - private structure for each Ethernet device
38 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
40 struct eth_device_priv {
41 enum eth_state_t state;
45 * struct eth_uclass_priv - The structure attached to the uclass itself
47 * @current: The Ethernet device that the network functions are using
49 struct eth_uclass_priv {
50 struct udevice *current;
53 /* eth_errno - This stores the most recent failure code from DM functions */
56 static struct eth_uclass_priv *eth_get_uclass_priv(void)
60 uclass_get(UCLASS_ETH, &uc);
65 void eth_set_current_to_next(void)
67 struct eth_uclass_priv *uc_priv;
69 uc_priv = eth_get_uclass_priv();
71 uclass_next_device(&uc_priv->current);
72 if (!uc_priv->current)
73 uclass_first_device(UCLASS_ETH, &uc_priv->current);
77 * Typically this will simply return the active device.
78 * In the case where the most recent active device was unset, this will attempt
79 * to return the first device. If that device doesn't exist or fails to probe,
80 * this function will return NULL.
82 struct udevice *eth_get_dev(void)
84 struct eth_uclass_priv *uc_priv;
86 uc_priv = eth_get_uclass_priv();
87 if (!uc_priv->current)
88 eth_errno = uclass_first_device(UCLASS_ETH,
90 return uc_priv->current;
94 * Typically this will just store a device pointer.
95 * In case it was not probed, we will attempt to do so.
96 * dev may be NULL to unset the active device.
98 void eth_set_dev(struct udevice *dev)
100 if (dev && !device_active(dev)) {
101 eth_errno = device_probe(dev);
106 eth_get_uclass_priv()->current = dev;
110 * Find the udevice that either has the name passed in as devname or has an
111 * alias named devname.
113 struct udevice *eth_get_dev_by_name(const char *devname)
117 const char *startp = NULL;
120 int len = strlen("eth");
122 /* Must be longer than 3 to be an alias */
123 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
124 startp = devname + len;
125 seq = simple_strtoul(startp, &endp, 10);
128 uclass_get(UCLASS_ETH, &uc);
129 uclass_foreach_dev(it, uc) {
131 * We need the seq to be valid, so try to probe it.
132 * If the probe fails, the seq will not match since it will be
133 * -1 instead of what we are looking for.
134 * We don't care about errors from probe here. Either they won't
135 * match an alias or it will match a literal name and we'll pick
136 * up the error when we try to probe again in eth_set_dev().
138 if (device_probe(it))
140 /* Check for the name or the sequence number to match */
141 if (strcmp(it->name, devname) == 0 ||
142 (endp > startp && it->seq == seq))
149 unsigned char *eth_get_ethaddr(void)
151 struct eth_pdata *pdata;
154 pdata = eth_get_dev()->platdata;
155 return pdata->enetaddr;
161 /* Set active state without calling start on the driver */
162 int eth_init_state_only(void)
164 struct udevice *current;
165 struct eth_device_priv *priv;
167 current = eth_get_dev();
168 if (!current || !device_active(current))
171 priv = current->uclass_priv;
172 priv->state = ETH_STATE_ACTIVE;
177 /* Set passive state without calling stop on the driver */
178 void eth_halt_state_only(void)
180 struct udevice *current;
181 struct eth_device_priv *priv;
183 current = eth_get_dev();
184 if (!current || !device_active(current))
187 priv = current->uclass_priv;
188 priv->state = ETH_STATE_PASSIVE;
191 int eth_get_dev_index(void)
194 return eth_get_dev()->seq;
198 static int eth_write_hwaddr(struct udevice *dev)
200 struct eth_pdata *pdata = dev->platdata;
203 if (!dev || !device_active(dev))
206 /* seq is valid since the device is active */
207 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
208 if (!is_valid_ethaddr(pdata->enetaddr)) {
209 printf("\nError: %s address %pM illegal value\n",
210 dev->name, pdata->enetaddr);
215 * Drivers are allowed to decide not to implement this at
216 * run-time. E.g. Some devices may use it and some may not.
218 ret = eth_get_ops(dev)->write_hwaddr(dev);
222 printf("\nWarning: %s failed to set MAC address\n",
229 static int on_ethaddr(const char *name, const char *value, enum env_op op,
236 /* look for an index after "eth" */
237 index = simple_strtoul(name + 3, NULL, 10);
239 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
241 struct eth_pdata *pdata = dev->platdata;
244 case env_op_overwrite:
245 eth_parse_enetaddr(value, pdata->enetaddr);
248 memset(pdata->enetaddr, 0, 6);
254 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
258 char *ethact = getenv("ethact");
259 char *ethrotate = getenv("ethrotate");
260 struct udevice *current = NULL;
261 struct udevice *old_current;
265 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
266 * is already set to an ethernet device, we should stick to 'ethact'.
268 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
270 current = eth_get_dev_by_name(ethact);
277 current = eth_get_dev();
279 printf("No ethernet found.\n");
284 old_current = current;
287 debug("Trying %s\n", current->name);
289 if (device_active(current)) {
290 ret = eth_get_ops(current)->start(current);
292 struct eth_device_priv *priv =
293 current->uclass_priv;
295 priv->state = ETH_STATE_ACTIVE;
304 debug("PROBE FAIL\n");
308 * If ethrotate is enabled, this will change "current",
309 * otherwise we will drop out of this while loop immediately
312 /* This will ensure the new "current" attempted to probe */
313 current = eth_get_dev();
314 } while (old_current != current);
321 struct udevice *current;
322 struct eth_device_priv *priv;
324 current = eth_get_dev();
325 if (!current || !device_active(current))
328 eth_get_ops(current)->stop(current);
329 priv = current->uclass_priv;
330 priv->state = ETH_STATE_PASSIVE;
333 int eth_is_active(struct udevice *dev)
335 struct eth_device_priv *priv;
337 if (!dev || !device_active(dev))
340 priv = dev_get_uclass_priv(dev);
341 return priv->state == ETH_STATE_ACTIVE;
344 int eth_send(void *packet, int length)
346 struct udevice *current;
349 current = eth_get_dev();
353 if (!device_active(current))
356 ret = eth_get_ops(current)->send(current, packet, length);
358 /* We cannot completely return the error at present */
359 debug("%s: send() returned error %d\n", __func__, ret);
366 struct udevice *current;
372 current = eth_get_dev();
376 if (!device_active(current))
379 /* Process up to 32 packets at one time */
380 flags = ETH_RECV_CHECK_DEVICE;
381 for (i = 0; i < 32; i++) {
382 ret = eth_get_ops(current)->recv(current, flags, &packet);
385 net_process_received_packet(packet, ret);
386 if (ret >= 0 && eth_get_ops(current)->free_pkt)
387 eth_get_ops(current)->free_pkt(current, packet, ret);
394 /* We cannot completely return the error at present */
395 debug("%s: recv() returned error %d\n", __func__, ret);
400 int eth_initialize(void)
408 * Devices need to write the hwaddr even if not started so that Linux
409 * will have access to the hwaddr that u-boot stored for the device.
410 * This is accomplished by attempting to probe each device and calling
411 * their write_hwaddr() operation.
413 uclass_first_device(UCLASS_ETH, &dev);
415 printf("No ethernet found.\n");
416 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
418 char *ethprime = getenv("ethprime");
419 struct udevice *prime_dev = NULL;
422 prime_dev = eth_get_dev_by_name(ethprime);
424 eth_set_dev(prime_dev);
425 eth_current_changed();
430 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
435 printf("eth%d: %s", dev->seq, dev->name);
437 if (ethprime && dev == prime_dev)
440 eth_write_hwaddr(dev);
442 uclass_next_device(&dev);
452 static int eth_post_bind(struct udevice *dev)
454 if (strchr(dev->name, ' ')) {
455 printf("\nError: eth device name \"%s\" has a space!\n",
463 static int eth_pre_unbind(struct udevice *dev)
465 /* Don't hang onto a pointer that is going away */
466 if (dev == eth_get_uclass_priv()->current)
472 static int eth_post_probe(struct udevice *dev)
474 struct eth_device_priv *priv = dev->uclass_priv;
475 struct eth_pdata *pdata = dev->platdata;
476 unsigned char env_enetaddr[6];
478 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
479 struct eth_ops *ops = eth_get_ops(dev);
480 static int reloc_done;
484 ops->start += gd->reloc_off;
486 ops->send += gd->reloc_off;
488 ops->recv += gd->reloc_off;
490 ops->free_pkt += gd->reloc_off;
492 ops->stop += gd->reloc_off;
493 #ifdef CONFIG_MCAST_TFTP
495 ops->mcast += gd->reloc_off;
497 if (ops->write_hwaddr)
498 ops->write_hwaddr += gd->reloc_off;
499 if (ops->read_rom_hwaddr)
500 ops->read_rom_hwaddr += gd->reloc_off;
506 priv->state = ETH_STATE_INIT;
508 /* Check if the device has a MAC address in ROM */
509 if (eth_get_ops(dev)->read_rom_hwaddr)
510 eth_get_ops(dev)->read_rom_hwaddr(dev);
512 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
513 if (!is_zero_ethaddr(env_enetaddr)) {
514 if (!is_zero_ethaddr(pdata->enetaddr) &&
515 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
516 printf("\nWarning: %s MAC addresses don't match:\n",
518 printf("Address in SROM is %pM\n",
520 printf("Address in environment is %pM\n",
524 /* Override the ROM MAC address */
525 memcpy(pdata->enetaddr, env_enetaddr, 6);
526 } else if (is_valid_ethaddr(pdata->enetaddr)) {
527 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
528 printf("\nWarning: %s using MAC address from ROM\n",
530 } else if (is_zero_ethaddr(pdata->enetaddr)) {
531 #ifdef CONFIG_NET_RANDOM_ETHADDR
532 net_random_ethaddr(pdata->enetaddr);
533 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
534 dev->name, dev->seq, pdata->enetaddr);
536 printf("\nError: %s address not set.\n",
545 static int eth_pre_remove(struct udevice *dev)
547 struct eth_pdata *pdata = dev->platdata;
549 eth_get_ops(dev)->stop(dev);
551 /* clear the MAC address */
552 memset(pdata->enetaddr, 0, 6);
557 UCLASS_DRIVER(eth) = {
560 .post_bind = eth_post_bind,
561 .pre_unbind = eth_pre_unbind,
562 .post_probe = eth_post_probe,
563 .pre_remove = eth_pre_remove,
564 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
565 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
566 .flags = DM_UC_FLAG_SEQ_ALIAS,
568 #endif /* #ifdef CONFIG_DM_ETH */
570 #ifndef CONFIG_DM_ETH
576 } eth_rcv_bufs[PKTBUFSRX];
578 static unsigned int eth_rcv_current, eth_rcv_last;
581 static struct eth_device *eth_devices;
582 struct eth_device *eth_current;
584 void eth_set_current_to_next(void)
586 eth_current = eth_current->next;
589 void eth_set_dev(struct eth_device *dev)
594 struct eth_device *eth_get_dev_by_name(const char *devname)
596 struct eth_device *dev, *target_dev;
598 BUG_ON(devname == NULL);
606 if (strcmp(devname, dev->name) == 0) {
611 } while (dev != eth_devices);
616 struct eth_device *eth_get_dev_by_index(int index)
618 struct eth_device *dev, *target_dev;
626 if (dev->index == index) {
631 } while (dev != eth_devices);
636 int eth_get_dev_index(void)
641 return eth_current->index;
644 static int on_ethaddr(const char *name, const char *value, enum env_op op,
648 struct eth_device *dev;
653 /* look for an index after "eth" */
654 index = simple_strtoul(name + 3, NULL, 10);
658 if (dev->index == index) {
661 case env_op_overwrite:
662 eth_parse_enetaddr(value, dev->enetaddr);
665 memset(dev->enetaddr, 0, 6);
669 } while (dev != eth_devices);
673 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
675 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
678 unsigned char env_enetaddr[6];
681 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
683 if (!is_zero_ethaddr(env_enetaddr)) {
684 if (!is_zero_ethaddr(dev->enetaddr) &&
685 memcmp(dev->enetaddr, env_enetaddr, 6)) {
686 printf("\nWarning: %s MAC addresses don't match:\n",
688 printf("Address in SROM is %pM\n",
690 printf("Address in environment is %pM\n",
694 memcpy(dev->enetaddr, env_enetaddr, 6);
695 } else if (is_valid_ethaddr(dev->enetaddr)) {
696 eth_setenv_enetaddr_by_index(base_name, eth_number,
698 } else if (is_zero_ethaddr(dev->enetaddr)) {
699 #ifdef CONFIG_NET_RANDOM_ETHADDR
700 net_random_ethaddr(dev->enetaddr);
701 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
702 dev->name, eth_number, dev->enetaddr);
704 printf("\nError: %s address not set.\n",
710 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
711 if (!is_valid_ethaddr(dev->enetaddr)) {
712 printf("\nError: %s address %pM illegal value\n",
713 dev->name, dev->enetaddr);
717 ret = dev->write_hwaddr(dev);
719 printf("\nWarning: %s failed to set MAC address\n",
726 int eth_register(struct eth_device *dev)
728 struct eth_device *d;
731 assert(strlen(dev->name) < sizeof(dev->name));
736 eth_current_changed();
738 for (d = eth_devices; d->next != eth_devices; d = d->next)
743 dev->state = ETH_STATE_INIT;
744 dev->next = eth_devices;
745 dev->index = index++;
750 int eth_unregister(struct eth_device *dev)
752 struct eth_device *cur;
758 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
762 /* Device not found */
763 if (cur->next != dev)
766 cur->next = dev->next;
768 if (eth_devices == dev)
769 eth_devices = dev->next == eth_devices ? NULL : dev->next;
771 if (eth_current == dev) {
772 eth_current = eth_devices;
773 eth_current_changed();
779 int eth_initialize(void)
787 * If board-specific initialization exists, call it.
788 * If not, call a CPU-specific one
790 if (board_eth_init != __def_eth_init) {
791 if (board_eth_init(gd->bd) < 0)
792 printf("Board Net Initialization Failed\n");
793 } else if (cpu_eth_init != __def_eth_init) {
794 if (cpu_eth_init(gd->bd) < 0)
795 printf("CPU Net Initialization Failed\n");
797 printf("Net Initialization Skipped\n");
801 puts("No ethernet found.\n");
802 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
804 struct eth_device *dev = eth_devices;
805 char *ethprime = getenv("ethprime");
807 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
812 printf("%s", dev->name);
814 if (ethprime && strcmp(dev->name, ethprime) == 0) {
819 if (strchr(dev->name, ' '))
820 puts("\nWarning: eth device name has a space!"
823 eth_write_hwaddr(dev, "eth", dev->index);
827 } while (dev != eth_devices);
829 eth_current_changed();
836 #ifdef CONFIG_MCAST_TFTP
838 * mcast_addr: multicast ipaddr from which multicast Mac is made
839 * join: 1=join, 0=leave.
841 int eth_mcast_join(struct in_addr mcast_ip, int join)
844 if (!eth_current || !eth_current->mcast)
846 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
847 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
848 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
852 return eth_current->mcast(eth_current, mcast_mac, join);
855 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
856 * and this is the ethernet-crc method needed for TSEC -- and perhaps
857 * some other adapter -- hash tables
859 #define CRCPOLY_LE 0xedb88320
860 u32 ether_crc(size_t len, unsigned char const *p)
867 for (i = 0; i < 8; i++)
868 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
870 /* an reverse the bits, cuz of way they arrive -- last-first */
871 crc = (crc >> 16) | (crc << 16);
872 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
873 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
874 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
875 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
884 struct eth_device *old_current;
887 puts("No ethernet found.\n");
891 old_current = eth_current;
893 debug("Trying %s\n", eth_current->name);
895 if (eth_current->init(eth_current, gd->bd) >= 0) {
896 eth_current->state = ETH_STATE_ACTIVE;
903 } while (old_current != eth_current);
913 eth_current->halt(eth_current);
915 eth_current->state = ETH_STATE_PASSIVE;
918 int eth_is_active(struct eth_device *dev)
920 return dev && dev->state == ETH_STATE_ACTIVE;
923 int eth_send(void *packet, int length)
928 return eth_current->send(eth_current, packet, length);
936 return eth_current->recv(eth_current);
938 #endif /* ifndef CONFIG_DM_ETH */
941 static void eth_save_packet(void *packet, int length)
946 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
949 if (PKTSIZE < length)
952 for (i = 0; i < length; i++)
953 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
955 eth_rcv_bufs[eth_rcv_last].length = length;
956 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
959 int eth_receive(void *packet, int length)
962 void *pp = push_packet;
965 if (eth_rcv_current == eth_rcv_last) {
966 push_packet = eth_save_packet;
970 if (eth_rcv_current == eth_rcv_last)
974 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
976 for (i = 0; i < length; i++)
977 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
979 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
982 #endif /* CONFIG_API */