1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
5 * Joe Hershberger, National Instruments
8 #define LOG_CATEGORY UCLASS_ETH
12 #include <bootstage.h>
18 #include <asm/global_data.h>
19 #include <dm/device-internal.h>
20 #include <dm/uclass-internal.h>
22 #include "eth_internal.h"
25 DECLARE_GLOBAL_DATA_PTR;
28 * struct eth_device_priv - private structure for each Ethernet device
30 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
32 struct eth_device_priv {
33 enum eth_state_t state;
38 * struct eth_uclass_priv - The structure attached to the uclass itself
40 * @current: The Ethernet device that the network functions are using
41 * @no_bootdevs: true to skip binding Ethernet bootdevs (this is a negative flag
42 * so that the default value enables it)
44 struct eth_uclass_priv {
45 struct udevice *current;
49 /* eth_errno - This stores the most recent failure code from DM functions */
52 /* board-specific Ethernet Interface initializations. */
53 __weak int board_interface_eth_init(struct udevice *dev,
54 phy_interface_t interface_type)
59 static struct eth_uclass_priv *eth_get_uclass_priv(void)
64 ret = uclass_get(UCLASS_ETH, &uc);
69 return uclass_get_priv(uc);
72 void eth_set_enable_bootdevs(bool enable)
74 struct eth_uclass_priv *priv = eth_get_uclass_priv();
77 priv->no_bootdevs = !enable;
80 void eth_set_current_to_next(void)
82 struct eth_uclass_priv *uc_priv;
84 uc_priv = eth_get_uclass_priv();
86 uclass_next_device(&uc_priv->current);
87 if (!uc_priv->current)
88 uclass_first_device(UCLASS_ETH, &uc_priv->current);
92 * Typically this will simply return the active device.
93 * In the case where the most recent active device was unset, this will attempt
94 * to return the device with sequence id 0 (which can be configured by the
95 * device tree). If this fails, fall back to just getting the first device.
96 * The latter is non-deterministic and depends on the order of the probing.
97 * If that device doesn't exist or fails to probe, this function will return
100 struct udevice *eth_get_dev(void)
102 struct eth_uclass_priv *uc_priv;
104 uc_priv = eth_get_uclass_priv();
108 if (!uc_priv->current) {
109 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
112 eth_errno = uclass_first_device_err(UCLASS_ETH,
115 uc_priv->current = NULL;
117 return uc_priv->current;
121 * Typically this will just store a device pointer.
122 * In case it was not probed, we will attempt to do so.
123 * dev may be NULL to unset the active device.
125 void eth_set_dev(struct udevice *dev)
127 if (dev && !device_active(dev)) {
128 eth_errno = device_probe(dev);
133 eth_get_uclass_priv()->current = dev;
137 * Find the udevice that either has the name passed in as devname or has an
138 * alias named devname.
140 struct udevice *eth_get_dev_by_name(const char *devname)
144 const char *startp = NULL;
147 int len = strlen("eth");
150 /* Must be longer than 3 to be an alias */
151 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
152 startp = devname + len;
153 seq = dectoul(startp, &endp);
156 ret = uclass_get(UCLASS_ETH, &uc);
160 uclass_foreach_dev(it, uc) {
162 * We don't care about errors from probe here. Either they won't
163 * match an alias or it will match a literal name and we'll pick
164 * up the error when we try to probe again in eth_set_dev().
166 if (device_probe(it))
168 /* Check for the name or the sequence number to match */
169 if (strcmp(it->name, devname) == 0 ||
170 (endp > startp && dev_seq(it) == seq))
177 unsigned char *eth_get_ethaddr(void)
179 struct eth_pdata *pdata;
182 pdata = dev_get_plat(eth_get_dev());
183 return pdata->enetaddr;
189 /* Set active state without calling start on the driver */
190 int eth_init_state_only(void)
192 struct udevice *current;
193 struct eth_device_priv *priv;
195 current = eth_get_dev();
196 if (!current || !device_active(current))
199 priv = dev_get_uclass_priv(current);
200 priv->state = ETH_STATE_ACTIVE;
205 /* Set passive state without calling stop on the driver */
206 void eth_halt_state_only(void)
208 struct udevice *current;
209 struct eth_device_priv *priv;
211 current = eth_get_dev();
212 if (!current || !device_active(current))
215 priv = dev_get_uclass_priv(current);
216 priv->state = ETH_STATE_PASSIVE;
219 int eth_get_dev_index(void)
222 return dev_seq(eth_get_dev());
226 static int eth_write_hwaddr(struct udevice *dev)
228 struct eth_pdata *pdata;
231 if (!dev || !device_active(dev))
234 /* seq is valid since the device is active */
235 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
236 pdata = dev_get_plat(dev);
237 if (!is_valid_ethaddr(pdata->enetaddr)) {
238 printf("\nError: %s address %pM illegal value\n",
239 dev->name, pdata->enetaddr);
244 * Drivers are allowed to decide not to implement this at
245 * run-time. E.g. Some devices may use it and some may not.
247 ret = eth_get_ops(dev)->write_hwaddr(dev);
251 printf("\nWarning: %s failed to set MAC address\n",
258 static int on_ethaddr(const char *name, const char *value, enum env_op op,
265 /* look for an index after "eth" */
266 index = dectoul(name + 3, NULL);
268 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
270 struct eth_pdata *pdata = dev_get_plat(dev);
273 case env_op_overwrite:
274 string_to_enetaddr(value, pdata->enetaddr);
275 eth_write_hwaddr(dev);
278 memset(pdata->enetaddr, 0, ARP_HLEN);
284 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
288 char *ethact = env_get("ethact");
289 char *ethrotate = env_get("ethrotate");
290 struct udevice *current = NULL;
291 struct udevice *old_current;
295 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
296 * is already set to an ethernet device, we should stick to 'ethact'.
298 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
300 current = eth_get_dev_by_name(ethact);
307 current = eth_get_dev();
309 log_err("No ethernet found.\n");
314 old_current = current;
317 debug("Trying %s\n", current->name);
319 if (device_active(current)) {
320 ret = eth_get_ops(current)->start(current);
322 struct eth_device_priv *priv =
323 dev_get_uclass_priv(current);
325 priv->state = ETH_STATE_ACTIVE;
326 priv->running = true;
335 debug("PROBE FAIL\n");
339 * If ethrotate is enabled, this will change "current",
340 * otherwise we will drop out of this while loop immediately
343 /* This will ensure the new "current" attempted to probe */
344 current = eth_get_dev();
345 } while (old_current != current);
352 struct udevice *current;
353 struct eth_device_priv *priv;
355 current = eth_get_dev();
359 priv = dev_get_uclass_priv(current);
360 if (!priv || !priv->running)
363 eth_get_ops(current)->stop(current);
364 priv->state = ETH_STATE_PASSIVE;
365 priv->running = false;
368 int eth_is_active(struct udevice *dev)
370 struct eth_device_priv *priv;
372 if (!dev || !device_active(dev))
375 priv = dev_get_uclass_priv(dev);
376 return priv->state == ETH_STATE_ACTIVE;
379 int eth_send(void *packet, int length)
381 struct udevice *current;
384 current = eth_get_dev();
388 if (!eth_is_active(current))
391 ret = eth_get_ops(current)->send(current, packet, length);
393 /* We cannot completely return the error at present */
394 debug("%s: send() returned error %d\n", __func__, ret);
396 #if defined(CONFIG_CMD_PCAP)
398 pcap_post(packet, length, true);
405 struct udevice *current;
411 current = eth_get_dev();
415 if (!eth_is_active(current))
418 /* Process up to 32 packets at one time */
419 flags = ETH_RECV_CHECK_DEVICE;
420 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
421 ret = eth_get_ops(current)->recv(current, flags, &packet);
424 net_process_received_packet(packet, ret);
425 if (ret >= 0 && eth_get_ops(current)->free_pkt)
426 eth_get_ops(current)->free_pkt(current, packet, ret);
433 /* We cannot completely return the error at present */
434 debug("%s: recv() returned error %d\n", __func__, ret);
439 int eth_initialize(void)
447 * Devices need to write the hwaddr even if not started so that Linux
448 * will have access to the hwaddr that u-boot stored for the device.
449 * This is accomplished by attempting to probe each device and calling
450 * their write_hwaddr() operation.
452 uclass_first_device_check(UCLASS_ETH, &dev);
454 log_err("No ethernet found.\n");
455 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
457 char *ethprime = env_get("ethprime");
458 struct udevice *prime_dev = NULL;
461 prime_dev = eth_get_dev_by_name(ethprime);
463 eth_set_dev(prime_dev);
464 eth_current_changed();
469 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
471 if (device_active(dev)) {
475 printf("eth%d: %s", dev_seq(dev), dev->name);
477 if (ethprime && dev == prime_dev)
481 eth_write_hwaddr(dev);
483 if (device_active(dev))
485 uclass_next_device_check(&dev);
489 log_err("No ethernet found.\n");
496 static int eth_post_bind(struct udevice *dev)
498 struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
501 if (strchr(dev->name, ' ')) {
502 printf("\nError: eth device name \"%s\" has a space!\n",
507 #ifdef CONFIG_DM_ETH_PHY
508 eth_phy_binds_nodes(dev);
510 if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
511 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
513 return log_msg_ret("bootdev", ret);
519 static int eth_pre_unbind(struct udevice *dev)
521 /* Don't hang onto a pointer that is going away */
522 if (dev == eth_get_uclass_priv()->current)
528 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
530 #if CONFIG_IS_ENABLED(OF_CONTROL)
532 struct nvmem_cell mac_cell;
534 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
536 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
539 memcpy(mac, p, ARP_HLEN);
543 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
546 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
552 static int eth_post_probe(struct udevice *dev)
554 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
555 struct eth_pdata *pdata = dev_get_plat(dev);
556 unsigned char env_enetaddr[ARP_HLEN];
559 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
560 struct eth_ops *ops = eth_get_ops(dev);
561 static int reloc_done;
565 ops->start += gd->reloc_off;
567 ops->send += gd->reloc_off;
569 ops->recv += gd->reloc_off;
571 ops->free_pkt += gd->reloc_off;
573 ops->stop += gd->reloc_off;
575 ops->mcast += gd->reloc_off;
576 if (ops->write_hwaddr)
577 ops->write_hwaddr += gd->reloc_off;
578 if (ops->read_rom_hwaddr)
579 ops->read_rom_hwaddr += gd->reloc_off;
585 priv->state = ETH_STATE_INIT;
586 priv->running = false;
588 /* Check if the device has a valid MAC address in device tree */
589 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
590 !is_valid_ethaddr(pdata->enetaddr)) {
592 /* Check if the device has a MAC address in ROM */
593 if (eth_get_ops(dev)->read_rom_hwaddr)
594 eth_get_ops(dev)->read_rom_hwaddr(dev);
597 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
598 if (!is_zero_ethaddr(env_enetaddr)) {
599 if (!is_zero_ethaddr(pdata->enetaddr) &&
600 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
601 printf("\nWarning: %s MAC addresses don't match:\n",
603 printf("Address in %s is\t\t%pM\n",
604 source, pdata->enetaddr);
605 printf("Address in environment is\t%pM\n",
609 /* Override the ROM MAC address */
610 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
611 } else if (is_valid_ethaddr(pdata->enetaddr)) {
612 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
614 } else if (is_zero_ethaddr(pdata->enetaddr) ||
615 !is_valid_ethaddr(pdata->enetaddr)) {
616 #ifdef CONFIG_NET_RANDOM_ETHADDR
617 net_random_ethaddr(pdata->enetaddr);
618 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
619 dev->name, dev_seq(dev), pdata->enetaddr);
620 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
623 printf("\nError: %s address not set.\n",
629 eth_write_hwaddr(dev);
634 static int eth_pre_remove(struct udevice *dev)
636 struct eth_pdata *pdata = dev_get_plat(dev);
638 eth_get_ops(dev)->stop(dev);
640 /* clear the MAC address */
641 memset(pdata->enetaddr, 0, ARP_HLEN);
646 UCLASS_DRIVER(ethernet) = {
649 .post_bind = eth_post_bind,
650 .pre_unbind = eth_pre_unbind,
651 .post_probe = eth_post_probe,
652 .pre_remove = eth_pre_remove,
653 .priv_auto = sizeof(struct eth_uclass_priv),
654 .per_device_auto = sizeof(struct eth_device_priv),
655 .flags = DM_UC_FLAG_SEQ_ALIAS,