1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
5 * Joe Hershberger, National Instruments
8 #define LOG_CATEGORY UCLASS_ETH
11 #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
40 * @no_bootdevs: true to skip binding Ethernet bootdevs (this is a negative flag
41 * so that the default value enables it)
43 struct eth_uclass_priv {
44 struct udevice *current;
48 /* eth_errno - This stores the most recent failure code from DM functions */
50 /* Are we currently in eth_init() or eth_halt()? */
51 static bool in_init_halt;
53 /* board-specific Ethernet Interface initializations. */
54 __weak int board_interface_eth_init(struct udevice *dev,
55 phy_interface_t interface_type)
60 static struct eth_uclass_priv *eth_get_uclass_priv(void)
65 ret = uclass_get(UCLASS_ETH, &uc);
70 return uclass_get_priv(uc);
73 void eth_set_enable_bootdevs(bool enable)
75 struct eth_uclass_priv *priv = eth_get_uclass_priv();
78 priv->no_bootdevs = !enable;
81 void eth_set_current_to_next(void)
83 struct eth_uclass_priv *uc_priv;
85 uc_priv = eth_get_uclass_priv();
87 uclass_next_device(&uc_priv->current);
88 if (!uc_priv->current)
89 uclass_first_device(UCLASS_ETH, &uc_priv->current);
93 * Typically this will simply return the active device.
94 * In the case where the most recent active device was unset, this will attempt
95 * to return the device with sequence id 0 (which can be configured by the
96 * device tree). If this fails, fall back to just getting the first device.
97 * The latter is non-deterministic and depends on the order of the probing.
98 * If that device doesn't exist or fails to probe, this function will return
101 struct udevice *eth_get_dev(void)
103 struct eth_uclass_priv *uc_priv;
105 uc_priv = eth_get_uclass_priv();
109 if (!uc_priv->current) {
110 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
113 eth_errno = uclass_first_device_err(UCLASS_ETH,
116 uc_priv->current = NULL;
118 return uc_priv->current;
122 * Typically this will just store a device pointer.
123 * In case it was not probed, we will attempt to do so.
124 * dev may be NULL to unset the active device.
126 void eth_set_dev(struct udevice *dev)
128 if (dev && !device_active(dev)) {
129 eth_errno = device_probe(dev);
134 eth_get_uclass_priv()->current = dev;
138 * Find the udevice that either has the name passed in as devname or has an
139 * alias named devname.
141 struct udevice *eth_get_dev_by_name(const char *devname)
145 const char *startp = NULL;
148 int len = strlen("eth");
151 /* Must be longer than 3 to be an alias */
152 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
153 startp = devname + len;
154 seq = dectoul(startp, &endp);
157 ret = uclass_get(UCLASS_ETH, &uc);
161 uclass_foreach_dev(it, uc) {
163 * We don't care about errors from probe here. Either they won't
164 * match an alias or it will match a literal name and we'll pick
165 * up the error when we try to probe again in eth_set_dev().
167 if (device_probe(it))
169 /* Check for the name or the sequence number to match */
170 if (strcmp(it->name, devname) == 0 ||
171 (endp > startp && dev_seq(it) == seq))
178 unsigned char *eth_get_ethaddr(void)
180 struct eth_pdata *pdata;
183 pdata = dev_get_plat(eth_get_dev());
184 return pdata->enetaddr;
190 /* Set active state without calling start on the driver */
191 int eth_init_state_only(void)
193 struct udevice *current;
194 struct eth_device_priv *priv;
196 current = eth_get_dev();
197 if (!current || !device_active(current))
200 priv = dev_get_uclass_priv(current);
201 priv->state = ETH_STATE_ACTIVE;
206 /* Set passive state without calling stop on the driver */
207 void eth_halt_state_only(void)
209 struct udevice *current;
210 struct eth_device_priv *priv;
212 current = eth_get_dev();
213 if (!current || !device_active(current))
216 priv = dev_get_uclass_priv(current);
217 priv->state = ETH_STATE_PASSIVE;
220 int eth_get_dev_index(void)
223 return dev_seq(eth_get_dev());
227 static int eth_write_hwaddr(struct udevice *dev)
229 struct eth_pdata *pdata;
232 if (!dev || !device_active(dev))
235 /* seq is valid since the device is active */
236 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
237 pdata = dev_get_plat(dev);
238 if (!is_valid_ethaddr(pdata->enetaddr)) {
239 printf("\nError: %s address %pM illegal value\n",
240 dev->name, pdata->enetaddr);
245 * Drivers are allowed to decide not to implement this at
246 * run-time. E.g. Some devices may use it and some may not.
248 ret = eth_get_ops(dev)->write_hwaddr(dev);
252 printf("\nWarning: %s failed to set MAC address\n",
259 static int on_ethaddr(const char *name, const char *value, enum env_op op,
266 /* look for an index after "eth" */
267 index = dectoul(name + 3, NULL);
269 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
271 struct eth_pdata *pdata = dev_get_plat(dev);
274 case env_op_overwrite:
275 string_to_enetaddr(value, pdata->enetaddr);
276 eth_write_hwaddr(dev);
279 memset(pdata->enetaddr, 0, ARP_HLEN);
285 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
287 int eth_start_udev(struct udevice *dev)
289 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
295 if (!device_active(dev))
298 ret = eth_get_ops(dev)->start(dev);
302 priv->state = ETH_STATE_ACTIVE;
303 priv->running = true;
310 struct udevice *current = NULL;
311 struct udevice *old_current;
321 ethact = env_get("ethact");
322 ethrotate = env_get("ethrotate");
325 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
326 * is already set to an ethernet device, we should stick to 'ethact'.
328 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
330 current = eth_get_dev_by_name(ethact);
339 current = eth_get_dev();
341 log_err("No ethernet found.\n");
347 old_current = current;
350 debug("Trying %s\n", current->name);
352 ret = eth_start_udev(current);
360 debug("PROBE FAIL\n");
364 * If ethrotate is enabled, this will change "current",
365 * otherwise we will drop out of this while loop immediately
368 /* This will ensure the new "current" attempted to probe */
369 current = eth_get_dev();
370 } while (old_current != current);
373 in_init_halt = false;
379 struct udevice *current;
380 struct eth_device_priv *priv;
387 current = eth_get_dev();
391 priv = dev_get_uclass_priv(current);
392 if (!priv || !priv->running)
395 eth_get_ops(current)->stop(current);
396 priv->state = ETH_STATE_PASSIVE;
397 priv->running = false;
400 in_init_halt = false;
403 int eth_is_active(struct udevice *dev)
405 struct eth_device_priv *priv;
407 if (!dev || !device_active(dev))
410 priv = dev_get_uclass_priv(dev);
411 return priv->state == ETH_STATE_ACTIVE;
414 int eth_send(void *packet, int length)
416 struct udevice *current;
419 current = eth_get_dev();
423 if (!eth_is_active(current))
426 ret = eth_get_ops(current)->send(current, packet, length);
428 /* We cannot completely return the error at present */
429 debug("%s: send() returned error %d\n", __func__, ret);
431 #if defined(CONFIG_CMD_PCAP)
433 pcap_post(packet, length, true);
440 struct udevice *current;
446 current = eth_get_dev();
450 if (!eth_is_active(current))
453 /* Process up to 32 packets at one time */
454 flags = ETH_RECV_CHECK_DEVICE;
455 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
456 ret = eth_get_ops(current)->recv(current, flags, &packet);
459 net_process_received_packet(packet, ret);
460 if (ret >= 0 && eth_get_ops(current)->free_pkt)
461 eth_get_ops(current)->free_pkt(current, packet, ret);
468 /* We cannot completely return the error at present */
469 debug("%s: recv() returned error %d\n", __func__, ret);
474 int eth_initialize(void)
482 * Devices need to write the hwaddr even if not started so that Linux
483 * will have access to the hwaddr that u-boot stored for the device.
484 * This is accomplished by attempting to probe each device and calling
485 * their write_hwaddr() operation.
487 uclass_first_device_check(UCLASS_ETH, &dev);
489 log_err("No ethernet found.\n");
490 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
492 char *ethprime = env_get("ethprime");
493 struct udevice *prime_dev = NULL;
496 prime_dev = eth_get_dev_by_name(ethprime);
498 eth_set_dev(prime_dev);
499 eth_current_changed();
504 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
506 if (device_active(dev)) {
510 printf("eth%d: %s", dev_seq(dev), dev->name);
512 if (ethprime && dev == prime_dev)
516 eth_write_hwaddr(dev);
518 if (device_active(dev))
520 uclass_next_device_check(&dev);
524 log_err("No ethernet found.\n");
531 static int eth_post_bind(struct udevice *dev)
533 struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
536 if (strchr(dev->name, ' ')) {
537 printf("\nError: eth device name \"%s\" has a space!\n",
542 #ifdef CONFIG_DM_ETH_PHY
543 eth_phy_binds_nodes(dev);
545 if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
546 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
548 return log_msg_ret("bootdev", ret);
554 static int eth_pre_unbind(struct udevice *dev)
556 /* Don't hang onto a pointer that is going away */
557 if (dev == eth_get_uclass_priv()->current)
563 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
565 #if CONFIG_IS_ENABLED(OF_CONTROL)
567 struct nvmem_cell mac_cell;
569 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
571 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
574 memcpy(mac, p, ARP_HLEN);
578 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
581 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
587 static int eth_post_probe(struct udevice *dev)
589 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
590 struct eth_pdata *pdata = dev_get_plat(dev);
591 unsigned char env_enetaddr[ARP_HLEN];
594 priv->state = ETH_STATE_INIT;
595 priv->running = false;
597 /* Check if the device has a valid MAC address in device tree */
598 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
599 !is_valid_ethaddr(pdata->enetaddr)) {
600 /* Check if the device has a MAC address in ROM */
601 if (eth_get_ops(dev)->read_rom_hwaddr) {
604 ret = eth_get_ops(dev)->read_rom_hwaddr(dev);
610 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
611 if (!is_zero_ethaddr(env_enetaddr)) {
612 if (!is_zero_ethaddr(pdata->enetaddr) &&
613 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
614 printf("\nWarning: %s MAC addresses don't match:\n",
616 printf("Address in %s is\t\t%pM\n",
617 source, pdata->enetaddr);
618 printf("Address in environment is\t%pM\n",
622 /* Override the ROM MAC address */
623 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
624 } else if (is_valid_ethaddr(pdata->enetaddr)) {
625 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
627 } else if (is_zero_ethaddr(pdata->enetaddr) ||
628 !is_valid_ethaddr(pdata->enetaddr)) {
629 #ifdef CONFIG_NET_RANDOM_ETHADDR
630 net_random_ethaddr(pdata->enetaddr);
631 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
632 dev->name, dev_seq(dev), pdata->enetaddr);
633 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
636 printf("\nError: %s No valid MAC address found.\n",
642 eth_write_hwaddr(dev);
647 static int eth_pre_remove(struct udevice *dev)
649 struct eth_pdata *pdata = dev_get_plat(dev);
651 eth_get_ops(dev)->stop(dev);
653 /* clear the MAC address */
654 memset(pdata->enetaddr, 0, ARP_HLEN);
659 UCLASS_DRIVER(ethernet) = {
662 .post_bind = eth_post_bind,
663 .pre_unbind = eth_pre_unbind,
664 .post_probe = eth_post_probe,
665 .pre_remove = eth_pre_remove,
666 .priv_auto = sizeof(struct eth_uclass_priv),
667 .per_device_auto = sizeof(struct eth_device_priv),
668 .flags = DM_UC_FLAG_SEQ_ALIAS,