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>
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 static struct eth_uclass_priv *eth_get_uclass_priv(void)
57 ret = uclass_get(UCLASS_ETH, &uc);
62 return uclass_get_priv(uc);
65 void eth_set_enable_bootdevs(bool enable)
67 struct eth_uclass_priv *priv = eth_get_uclass_priv();
70 priv->no_bootdevs = !enable;
73 void eth_set_current_to_next(void)
75 struct eth_uclass_priv *uc_priv;
77 uc_priv = eth_get_uclass_priv();
79 uclass_next_device(&uc_priv->current);
80 if (!uc_priv->current)
81 uclass_first_device(UCLASS_ETH, &uc_priv->current);
85 * Typically this will simply return the active device.
86 * In the case where the most recent active device was unset, this will attempt
87 * to return the device with sequence id 0 (which can be configured by the
88 * device tree). If this fails, fall back to just getting the first device.
89 * The latter is non-deterministic and depends on the order of the probing.
90 * If that device doesn't exist or fails to probe, this function will return
93 struct udevice *eth_get_dev(void)
95 struct eth_uclass_priv *uc_priv;
97 uc_priv = eth_get_uclass_priv();
101 if (!uc_priv->current) {
102 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
105 eth_errno = uclass_first_device_err(UCLASS_ETH,
108 uc_priv->current = NULL;
110 return uc_priv->current;
114 * Typically this will just store a device pointer.
115 * In case it was not probed, we will attempt to do so.
116 * dev may be NULL to unset the active device.
118 void eth_set_dev(struct udevice *dev)
120 if (dev && !device_active(dev)) {
121 eth_errno = device_probe(dev);
126 eth_get_uclass_priv()->current = dev;
130 * Find the udevice that either has the name passed in as devname or has an
131 * alias named devname.
133 struct udevice *eth_get_dev_by_name(const char *devname)
137 const char *startp = NULL;
140 int len = strlen("eth");
143 /* Must be longer than 3 to be an alias */
144 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
145 startp = devname + len;
146 seq = dectoul(startp, &endp);
149 ret = uclass_get(UCLASS_ETH, &uc);
153 uclass_foreach_dev(it, uc) {
155 * We don't care about errors from probe here. Either they won't
156 * match an alias or it will match a literal name and we'll pick
157 * up the error when we try to probe again in eth_set_dev().
159 if (device_probe(it))
161 /* Check for the name or the sequence number to match */
162 if (strcmp(it->name, devname) == 0 ||
163 (endp > startp && dev_seq(it) == seq))
170 unsigned char *eth_get_ethaddr(void)
172 struct eth_pdata *pdata;
175 pdata = dev_get_plat(eth_get_dev());
176 return pdata->enetaddr;
182 /* Set active state without calling start on the driver */
183 int eth_init_state_only(void)
185 struct udevice *current;
186 struct eth_device_priv *priv;
188 current = eth_get_dev();
189 if (!current || !device_active(current))
192 priv = dev_get_uclass_priv(current);
193 priv->state = ETH_STATE_ACTIVE;
198 /* Set passive state without calling stop on the driver */
199 void eth_halt_state_only(void)
201 struct udevice *current;
202 struct eth_device_priv *priv;
204 current = eth_get_dev();
205 if (!current || !device_active(current))
208 priv = dev_get_uclass_priv(current);
209 priv->state = ETH_STATE_PASSIVE;
212 int eth_get_dev_index(void)
215 return dev_seq(eth_get_dev());
219 static int eth_write_hwaddr(struct udevice *dev)
221 struct eth_pdata *pdata;
224 if (!dev || !device_active(dev))
227 /* seq is valid since the device is active */
228 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
229 pdata = dev_get_plat(dev);
230 if (!is_valid_ethaddr(pdata->enetaddr)) {
231 printf("\nError: %s address %pM illegal value\n",
232 dev->name, pdata->enetaddr);
237 * Drivers are allowed to decide not to implement this at
238 * run-time. E.g. Some devices may use it and some may not.
240 ret = eth_get_ops(dev)->write_hwaddr(dev);
244 printf("\nWarning: %s failed to set MAC address\n",
251 static int on_ethaddr(const char *name, const char *value, enum env_op op,
258 /* look for an index after "eth" */
259 index = dectoul(name + 3, NULL);
261 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
263 struct eth_pdata *pdata = dev_get_plat(dev);
266 case env_op_overwrite:
267 string_to_enetaddr(value, pdata->enetaddr);
268 eth_write_hwaddr(dev);
271 memset(pdata->enetaddr, 0, ARP_HLEN);
277 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
281 char *ethact = env_get("ethact");
282 char *ethrotate = env_get("ethrotate");
283 struct udevice *current = NULL;
284 struct udevice *old_current;
288 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
289 * is already set to an ethernet device, we should stick to 'ethact'.
291 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
293 current = eth_get_dev_by_name(ethact);
300 current = eth_get_dev();
302 log_err("No ethernet found.\n");
307 old_current = current;
310 debug("Trying %s\n", current->name);
312 if (device_active(current)) {
313 ret = eth_get_ops(current)->start(current);
315 struct eth_device_priv *priv =
316 dev_get_uclass_priv(current);
318 priv->state = ETH_STATE_ACTIVE;
319 priv->running = true;
328 debug("PROBE FAIL\n");
332 * If ethrotate is enabled, this will change "current",
333 * otherwise we will drop out of this while loop immediately
336 /* This will ensure the new "current" attempted to probe */
337 current = eth_get_dev();
338 } while (old_current != current);
345 struct udevice *current;
346 struct eth_device_priv *priv;
348 current = eth_get_dev();
352 priv = dev_get_uclass_priv(current);
353 if (!priv || !priv->running)
356 eth_get_ops(current)->stop(current);
357 priv->state = ETH_STATE_PASSIVE;
358 priv->running = false;
361 int eth_is_active(struct udevice *dev)
363 struct eth_device_priv *priv;
365 if (!dev || !device_active(dev))
368 priv = dev_get_uclass_priv(dev);
369 return priv->state == ETH_STATE_ACTIVE;
372 int eth_send(void *packet, int length)
374 struct udevice *current;
377 current = eth_get_dev();
381 if (!eth_is_active(current))
384 ret = eth_get_ops(current)->send(current, packet, length);
386 /* We cannot completely return the error at present */
387 debug("%s: send() returned error %d\n", __func__, ret);
389 #if defined(CONFIG_CMD_PCAP)
391 pcap_post(packet, length, true);
398 struct udevice *current;
404 current = eth_get_dev();
408 if (!eth_is_active(current))
411 /* Process up to 32 packets at one time */
412 flags = ETH_RECV_CHECK_DEVICE;
413 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
414 ret = eth_get_ops(current)->recv(current, flags, &packet);
417 net_process_received_packet(packet, ret);
418 if (ret >= 0 && eth_get_ops(current)->free_pkt)
419 eth_get_ops(current)->free_pkt(current, packet, ret);
426 /* We cannot completely return the error at present */
427 debug("%s: recv() returned error %d\n", __func__, ret);
432 int eth_initialize(void)
440 * Devices need to write the hwaddr even if not started so that Linux
441 * will have access to the hwaddr that u-boot stored for the device.
442 * This is accomplished by attempting to probe each device and calling
443 * their write_hwaddr() operation.
445 uclass_first_device_check(UCLASS_ETH, &dev);
447 log_err("No ethernet found.\n");
448 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
450 char *ethprime = env_get("ethprime");
451 struct udevice *prime_dev = NULL;
454 prime_dev = eth_get_dev_by_name(ethprime);
456 eth_set_dev(prime_dev);
457 eth_current_changed();
462 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
464 if (device_active(dev)) {
468 printf("eth%d: %s", dev_seq(dev), dev->name);
470 if (ethprime && dev == prime_dev)
474 eth_write_hwaddr(dev);
476 if (device_active(dev))
478 uclass_next_device_check(&dev);
482 log_err("No ethernet found.\n");
489 static int eth_post_bind(struct udevice *dev)
491 struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
494 if (strchr(dev->name, ' ')) {
495 printf("\nError: eth device name \"%s\" has a space!\n",
500 #ifdef CONFIG_DM_ETH_PHY
501 eth_phy_binds_nodes(dev);
503 if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
504 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
506 return log_msg_ret("bootdev", ret);
512 static int eth_pre_unbind(struct udevice *dev)
514 /* Don't hang onto a pointer that is going away */
515 if (dev == eth_get_uclass_priv()->current)
521 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
523 #if CONFIG_IS_ENABLED(OF_CONTROL)
525 struct nvmem_cell mac_cell;
527 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
529 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
532 memcpy(mac, p, ARP_HLEN);
536 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
539 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
545 static int eth_post_probe(struct udevice *dev)
547 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
548 struct eth_pdata *pdata = dev_get_plat(dev);
549 unsigned char env_enetaddr[ARP_HLEN];
552 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
553 struct eth_ops *ops = eth_get_ops(dev);
554 static int reloc_done;
558 ops->start += gd->reloc_off;
560 ops->send += gd->reloc_off;
562 ops->recv += gd->reloc_off;
564 ops->free_pkt += gd->reloc_off;
566 ops->stop += gd->reloc_off;
568 ops->mcast += gd->reloc_off;
569 if (ops->write_hwaddr)
570 ops->write_hwaddr += gd->reloc_off;
571 if (ops->read_rom_hwaddr)
572 ops->read_rom_hwaddr += gd->reloc_off;
578 priv->state = ETH_STATE_INIT;
579 priv->running = false;
581 /* Check if the device has a valid MAC address in device tree */
582 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
583 !is_valid_ethaddr(pdata->enetaddr)) {
585 /* Check if the device has a MAC address in ROM */
586 if (eth_get_ops(dev)->read_rom_hwaddr)
587 eth_get_ops(dev)->read_rom_hwaddr(dev);
590 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
591 if (!is_zero_ethaddr(env_enetaddr)) {
592 if (!is_zero_ethaddr(pdata->enetaddr) &&
593 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
594 printf("\nWarning: %s MAC addresses don't match:\n",
596 printf("Address in %s is\t\t%pM\n",
597 source, pdata->enetaddr);
598 printf("Address in environment is\t%pM\n",
602 /* Override the ROM MAC address */
603 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
604 } else if (is_valid_ethaddr(pdata->enetaddr)) {
605 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
607 } else if (is_zero_ethaddr(pdata->enetaddr) ||
608 !is_valid_ethaddr(pdata->enetaddr)) {
609 #ifdef CONFIG_NET_RANDOM_ETHADDR
610 net_random_ethaddr(pdata->enetaddr);
611 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
612 dev->name, dev_seq(dev), pdata->enetaddr);
613 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
616 printf("\nError: %s address not set.\n",
622 eth_write_hwaddr(dev);
627 static int eth_pre_remove(struct udevice *dev)
629 struct eth_pdata *pdata = dev_get_plat(dev);
631 eth_get_ops(dev)->stop(dev);
633 /* clear the MAC address */
634 memset(pdata->enetaddr, 0, ARP_HLEN);
639 UCLASS_DRIVER(ethernet) = {
642 .post_bind = eth_post_bind,
643 .pre_unbind = eth_pre_unbind,
644 .post_probe = eth_post_probe,
645 .pre_remove = eth_pre_remove,
646 .priv_auto = sizeof(struct eth_uclass_priv),
647 .per_device_auto = sizeof(struct eth_device_priv),
648 .flags = DM_UC_FLAG_SEQ_ALIAS,