1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
8 #define LOG_CATEGORY UCLASS_ETH
12 #include <bootstage.h>
17 #include <asm/global_data.h>
18 #include <dm/device-internal.h>
19 #include <dm/uclass-internal.h>
21 #include "eth_internal.h"
24 DECLARE_GLOBAL_DATA_PTR;
27 * struct eth_device_priv - private structure for each Ethernet device
29 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
31 struct eth_device_priv {
32 enum eth_state_t state;
37 * struct eth_uclass_priv - The structure attached to the uclass itself
39 * @current: The Ethernet device that the network functions are using
41 struct eth_uclass_priv {
42 struct udevice *current;
45 /* eth_errno - This stores the most recent failure code from DM functions */
48 static struct eth_uclass_priv *eth_get_uclass_priv(void)
53 ret = uclass_get(UCLASS_ETH, &uc);
58 return uclass_get_priv(uc);
61 void eth_set_current_to_next(void)
63 struct eth_uclass_priv *uc_priv;
65 uc_priv = eth_get_uclass_priv();
67 uclass_next_device(&uc_priv->current);
68 if (!uc_priv->current)
69 uclass_first_device(UCLASS_ETH, &uc_priv->current);
73 * Typically this will simply return the active device.
74 * In the case where the most recent active device was unset, this will attempt
75 * to return the device with sequence id 0 (which can be configured by the
76 * device tree). If this fails, fall back to just getting the first device.
77 * The latter is non-deterministic and depends on the order of the probing.
78 * If that device doesn't exist or fails to probe, this function will return
81 struct udevice *eth_get_dev(void)
83 struct eth_uclass_priv *uc_priv;
85 uc_priv = eth_get_uclass_priv();
89 if (!uc_priv->current) {
90 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
93 eth_errno = uclass_first_device(UCLASS_ETH,
96 return uc_priv->current;
100 * Typically this will just store a device pointer.
101 * In case it was not probed, we will attempt to do so.
102 * dev may be NULL to unset the active device.
104 void eth_set_dev(struct udevice *dev)
106 if (dev && !device_active(dev)) {
107 eth_errno = device_probe(dev);
112 eth_get_uclass_priv()->current = dev;
116 * Find the udevice that either has the name passed in as devname or has an
117 * alias named devname.
119 struct udevice *eth_get_dev_by_name(const char *devname)
123 const char *startp = NULL;
126 int len = strlen("eth");
129 /* Must be longer than 3 to be an alias */
130 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
131 startp = devname + len;
132 seq = dectoul(startp, &endp);
135 ret = uclass_get(UCLASS_ETH, &uc);
139 uclass_foreach_dev(it, uc) {
141 * We don't care about errors from probe here. Either they won't
142 * match an alias or it will match a literal name and we'll pick
143 * up the error when we try to probe again in eth_set_dev().
145 if (device_probe(it))
147 /* Check for the name or the sequence number to match */
148 if (strcmp(it->name, devname) == 0 ||
149 (endp > startp && dev_seq(it) == seq))
156 unsigned char *eth_get_ethaddr(void)
158 struct eth_pdata *pdata;
161 pdata = dev_get_plat(eth_get_dev());
162 return pdata->enetaddr;
168 /* Set active state without calling start on the driver */
169 int eth_init_state_only(void)
171 struct udevice *current;
172 struct eth_device_priv *priv;
174 current = eth_get_dev();
175 if (!current || !device_active(current))
178 priv = dev_get_uclass_priv(current);
179 priv->state = ETH_STATE_ACTIVE;
184 /* Set passive state without calling stop on the driver */
185 void eth_halt_state_only(void)
187 struct udevice *current;
188 struct eth_device_priv *priv;
190 current = eth_get_dev();
191 if (!current || !device_active(current))
194 priv = dev_get_uclass_priv(current);
195 priv->state = ETH_STATE_PASSIVE;
198 int eth_get_dev_index(void)
201 return dev_seq(eth_get_dev());
205 static int eth_write_hwaddr(struct udevice *dev)
207 struct eth_pdata *pdata;
210 if (!dev || !device_active(dev))
213 /* seq is valid since the device is active */
214 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
215 pdata = dev_get_plat(dev);
216 if (!is_valid_ethaddr(pdata->enetaddr)) {
217 printf("\nError: %s address %pM illegal value\n",
218 dev->name, pdata->enetaddr);
223 * Drivers are allowed to decide not to implement this at
224 * run-time. E.g. Some devices may use it and some may not.
226 ret = eth_get_ops(dev)->write_hwaddr(dev);
230 printf("\nWarning: %s failed to set MAC address\n",
237 static int on_ethaddr(const char *name, const char *value, enum env_op op,
244 /* look for an index after "eth" */
245 index = dectoul(name + 3, NULL);
247 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
249 struct eth_pdata *pdata = dev_get_plat(dev);
252 case env_op_overwrite:
253 string_to_enetaddr(value, pdata->enetaddr);
254 eth_write_hwaddr(dev);
257 memset(pdata->enetaddr, 0, ARP_HLEN);
263 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
267 char *ethact = env_get("ethact");
268 char *ethrotate = env_get("ethrotate");
269 struct udevice *current = NULL;
270 struct udevice *old_current;
274 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
275 * is already set to an ethernet device, we should stick to 'ethact'.
277 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
279 current = eth_get_dev_by_name(ethact);
286 current = eth_get_dev();
288 log_err("No ethernet found.\n");
293 old_current = current;
296 debug("Trying %s\n", current->name);
298 if (device_active(current)) {
299 ret = eth_get_ops(current)->start(current);
301 struct eth_device_priv *priv =
302 dev_get_uclass_priv(current);
304 priv->state = ETH_STATE_ACTIVE;
305 priv->running = true;
314 debug("PROBE FAIL\n");
318 * If ethrotate is enabled, this will change "current",
319 * otherwise we will drop out of this while loop immediately
322 /* This will ensure the new "current" attempted to probe */
323 current = eth_get_dev();
324 } while (old_current != current);
331 struct udevice *current;
332 struct eth_device_priv *priv;
334 current = eth_get_dev();
338 priv = dev_get_uclass_priv(current);
339 if (!priv || !priv->running)
342 eth_get_ops(current)->stop(current);
343 priv->state = ETH_STATE_PASSIVE;
344 priv->running = false;
347 int eth_is_active(struct udevice *dev)
349 struct eth_device_priv *priv;
351 if (!dev || !device_active(dev))
354 priv = dev_get_uclass_priv(dev);
355 return priv->state == ETH_STATE_ACTIVE;
358 int eth_send(void *packet, int length)
360 struct udevice *current;
363 current = eth_get_dev();
367 if (!eth_is_active(current))
370 ret = eth_get_ops(current)->send(current, packet, length);
372 /* We cannot completely return the error at present */
373 debug("%s: send() returned error %d\n", __func__, ret);
375 #if defined(CONFIG_CMD_PCAP)
377 pcap_post(packet, length, true);
384 struct udevice *current;
390 current = eth_get_dev();
394 if (!eth_is_active(current))
397 /* Process up to 32 packets at one time */
398 flags = ETH_RECV_CHECK_DEVICE;
399 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
400 ret = eth_get_ops(current)->recv(current, flags, &packet);
403 net_process_received_packet(packet, ret);
404 if (ret >= 0 && eth_get_ops(current)->free_pkt)
405 eth_get_ops(current)->free_pkt(current, packet, ret);
412 /* We cannot completely return the error at present */
413 debug("%s: recv() returned error %d\n", __func__, ret);
418 int eth_initialize(void)
426 * Devices need to write the hwaddr even if not started so that Linux
427 * will have access to the hwaddr that u-boot stored for the device.
428 * This is accomplished by attempting to probe each device and calling
429 * their write_hwaddr() operation.
431 uclass_first_device_check(UCLASS_ETH, &dev);
433 log_err("No ethernet found.\n");
434 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
436 char *ethprime = env_get("ethprime");
437 struct udevice *prime_dev = NULL;
440 prime_dev = eth_get_dev_by_name(ethprime);
442 eth_set_dev(prime_dev);
443 eth_current_changed();
448 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
450 if (device_active(dev)) {
454 printf("eth%d: %s", dev_seq(dev), dev->name);
456 if (ethprime && dev == prime_dev)
460 eth_write_hwaddr(dev);
462 if (device_active(dev))
464 uclass_next_device_check(&dev);
468 log_err("No ethernet found.\n");
475 static int eth_post_bind(struct udevice *dev)
479 if (strchr(dev->name, ' ')) {
480 printf("\nError: eth device name \"%s\" has a space!\n",
485 #ifdef CONFIG_DM_ETH_PHY
486 eth_phy_binds_nodes(dev);
488 if (CONFIG_IS_ENABLED(BOOTDEV_ETH)) {
489 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
491 return log_msg_ret("bootdev", ret);
497 static int eth_pre_unbind(struct udevice *dev)
499 /* Don't hang onto a pointer that is going away */
500 if (dev == eth_get_uclass_priv()->current)
506 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
508 #if CONFIG_IS_ENABLED(OF_CONTROL)
511 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
513 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
518 memcpy(mac, p, ARP_HLEN);
526 static int eth_post_probe(struct udevice *dev)
528 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
529 struct eth_pdata *pdata = dev_get_plat(dev);
530 unsigned char env_enetaddr[ARP_HLEN];
533 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
534 struct eth_ops *ops = eth_get_ops(dev);
535 static int reloc_done;
539 ops->start += gd->reloc_off;
541 ops->send += gd->reloc_off;
543 ops->recv += gd->reloc_off;
545 ops->free_pkt += gd->reloc_off;
547 ops->stop += gd->reloc_off;
549 ops->mcast += gd->reloc_off;
550 if (ops->write_hwaddr)
551 ops->write_hwaddr += gd->reloc_off;
552 if (ops->read_rom_hwaddr)
553 ops->read_rom_hwaddr += gd->reloc_off;
559 priv->state = ETH_STATE_INIT;
560 priv->running = false;
562 /* Check if the device has a valid MAC address in device tree */
563 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
564 !is_valid_ethaddr(pdata->enetaddr)) {
566 /* Check if the device has a MAC address in ROM */
567 if (eth_get_ops(dev)->read_rom_hwaddr)
568 eth_get_ops(dev)->read_rom_hwaddr(dev);
571 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
572 if (!is_zero_ethaddr(env_enetaddr)) {
573 if (!is_zero_ethaddr(pdata->enetaddr) &&
574 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
575 printf("\nWarning: %s MAC addresses don't match:\n",
577 printf("Address in %s is\t\t%pM\n",
578 source, pdata->enetaddr);
579 printf("Address in environment is\t%pM\n",
583 /* Override the ROM MAC address */
584 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
585 } else if (is_valid_ethaddr(pdata->enetaddr)) {
586 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
588 } else if (is_zero_ethaddr(pdata->enetaddr) ||
589 !is_valid_ethaddr(pdata->enetaddr)) {
590 #ifdef CONFIG_NET_RANDOM_ETHADDR
591 net_random_ethaddr(pdata->enetaddr);
592 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
593 dev->name, dev_seq(dev), pdata->enetaddr);
594 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
597 printf("\nError: %s address not set.\n",
603 eth_write_hwaddr(dev);
608 static int eth_pre_remove(struct udevice *dev)
610 struct eth_pdata *pdata = dev_get_plat(dev);
612 eth_get_ops(dev)->stop(dev);
614 /* clear the MAC address */
615 memset(pdata->enetaddr, 0, ARP_HLEN);
620 UCLASS_DRIVER(ethernet) = {
623 .post_bind = eth_post_bind,
624 .pre_unbind = eth_pre_unbind,
625 .post_probe = eth_post_probe,
626 .pre_remove = eth_pre_remove,
627 .priv_auto = sizeof(struct eth_uclass_priv),
628 .per_device_auto = sizeof(struct eth_device_priv),
629 .flags = DM_UC_FLAG_SEQ_ALIAS,