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
42 struct eth_uclass_priv {
43 struct udevice *current;
46 /* eth_errno - This stores the most recent failure code from DM functions */
49 static struct eth_uclass_priv *eth_get_uclass_priv(void)
54 ret = uclass_get(UCLASS_ETH, &uc);
59 return uclass_get_priv(uc);
62 void eth_set_current_to_next(void)
64 struct eth_uclass_priv *uc_priv;
66 uc_priv = eth_get_uclass_priv();
68 uclass_next_device(&uc_priv->current);
69 if (!uc_priv->current)
70 uclass_first_device(UCLASS_ETH, &uc_priv->current);
74 * Typically this will simply return the active device.
75 * In the case where the most recent active device was unset, this will attempt
76 * to return the device with sequence id 0 (which can be configured by the
77 * device tree). If this fails, fall back to just getting the first device.
78 * The latter is non-deterministic and depends on the order of the probing.
79 * If that device doesn't exist or fails to probe, this function will return
82 struct udevice *eth_get_dev(void)
84 struct eth_uclass_priv *uc_priv;
86 uc_priv = eth_get_uclass_priv();
90 if (!uc_priv->current) {
91 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
94 eth_errno = uclass_first_device(UCLASS_ETH,
97 return uc_priv->current;
101 * Typically this will just store a device pointer.
102 * In case it was not probed, we will attempt to do so.
103 * dev may be NULL to unset the active device.
105 void eth_set_dev(struct udevice *dev)
107 if (dev && !device_active(dev)) {
108 eth_errno = device_probe(dev);
113 eth_get_uclass_priv()->current = dev;
117 * Find the udevice that either has the name passed in as devname or has an
118 * alias named devname.
120 struct udevice *eth_get_dev_by_name(const char *devname)
124 const char *startp = NULL;
127 int len = strlen("eth");
130 /* Must be longer than 3 to be an alias */
131 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
132 startp = devname + len;
133 seq = dectoul(startp, &endp);
136 ret = uclass_get(UCLASS_ETH, &uc);
140 uclass_foreach_dev(it, uc) {
142 * We don't care about errors from probe here. Either they won't
143 * match an alias or it will match a literal name and we'll pick
144 * up the error when we try to probe again in eth_set_dev().
146 if (device_probe(it))
148 /* Check for the name or the sequence number to match */
149 if (strcmp(it->name, devname) == 0 ||
150 (endp > startp && dev_seq(it) == seq))
157 unsigned char *eth_get_ethaddr(void)
159 struct eth_pdata *pdata;
162 pdata = dev_get_plat(eth_get_dev());
163 return pdata->enetaddr;
169 /* Set active state without calling start on the driver */
170 int eth_init_state_only(void)
172 struct udevice *current;
173 struct eth_device_priv *priv;
175 current = eth_get_dev();
176 if (!current || !device_active(current))
179 priv = dev_get_uclass_priv(current);
180 priv->state = ETH_STATE_ACTIVE;
185 /* Set passive state without calling stop on the driver */
186 void eth_halt_state_only(void)
188 struct udevice *current;
189 struct eth_device_priv *priv;
191 current = eth_get_dev();
192 if (!current || !device_active(current))
195 priv = dev_get_uclass_priv(current);
196 priv->state = ETH_STATE_PASSIVE;
199 int eth_get_dev_index(void)
202 return dev_seq(eth_get_dev());
206 static int eth_write_hwaddr(struct udevice *dev)
208 struct eth_pdata *pdata;
211 if (!dev || !device_active(dev))
214 /* seq is valid since the device is active */
215 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
216 pdata = dev_get_plat(dev);
217 if (!is_valid_ethaddr(pdata->enetaddr)) {
218 printf("\nError: %s address %pM illegal value\n",
219 dev->name, pdata->enetaddr);
224 * Drivers are allowed to decide not to implement this at
225 * run-time. E.g. Some devices may use it and some may not.
227 ret = eth_get_ops(dev)->write_hwaddr(dev);
231 printf("\nWarning: %s failed to set MAC address\n",
238 static int on_ethaddr(const char *name, const char *value, enum env_op op,
245 /* look for an index after "eth" */
246 index = dectoul(name + 3, NULL);
248 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
250 struct eth_pdata *pdata = dev_get_plat(dev);
253 case env_op_overwrite:
254 string_to_enetaddr(value, pdata->enetaddr);
255 eth_write_hwaddr(dev);
258 memset(pdata->enetaddr, 0, ARP_HLEN);
264 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
268 char *ethact = env_get("ethact");
269 char *ethrotate = env_get("ethrotate");
270 struct udevice *current = NULL;
271 struct udevice *old_current;
275 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
276 * is already set to an ethernet device, we should stick to 'ethact'.
278 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
280 current = eth_get_dev_by_name(ethact);
287 current = eth_get_dev();
289 log_err("No ethernet found.\n");
294 old_current = current;
297 debug("Trying %s\n", current->name);
299 if (device_active(current)) {
300 ret = eth_get_ops(current)->start(current);
302 struct eth_device_priv *priv =
303 dev_get_uclass_priv(current);
305 priv->state = ETH_STATE_ACTIVE;
306 priv->running = true;
315 debug("PROBE FAIL\n");
319 * If ethrotate is enabled, this will change "current",
320 * otherwise we will drop out of this while loop immediately
323 /* This will ensure the new "current" attempted to probe */
324 current = eth_get_dev();
325 } while (old_current != current);
332 struct udevice *current;
333 struct eth_device_priv *priv;
335 current = eth_get_dev();
339 priv = dev_get_uclass_priv(current);
340 if (!priv || !priv->running)
343 eth_get_ops(current)->stop(current);
344 priv->state = ETH_STATE_PASSIVE;
345 priv->running = false;
348 int eth_is_active(struct udevice *dev)
350 struct eth_device_priv *priv;
352 if (!dev || !device_active(dev))
355 priv = dev_get_uclass_priv(dev);
356 return priv->state == ETH_STATE_ACTIVE;
359 int eth_send(void *packet, int length)
361 struct udevice *current;
364 current = eth_get_dev();
368 if (!eth_is_active(current))
371 ret = eth_get_ops(current)->send(current, packet, length);
373 /* We cannot completely return the error at present */
374 debug("%s: send() returned error %d\n", __func__, ret);
376 #if defined(CONFIG_CMD_PCAP)
378 pcap_post(packet, length, true);
385 struct udevice *current;
391 current = eth_get_dev();
395 if (!eth_is_active(current))
398 /* Process up to 32 packets at one time */
399 flags = ETH_RECV_CHECK_DEVICE;
400 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
401 ret = eth_get_ops(current)->recv(current, flags, &packet);
404 net_process_received_packet(packet, ret);
405 if (ret >= 0 && eth_get_ops(current)->free_pkt)
406 eth_get_ops(current)->free_pkt(current, packet, ret);
413 /* We cannot completely return the error at present */
414 debug("%s: recv() returned error %d\n", __func__, ret);
419 int eth_initialize(void)
427 * Devices need to write the hwaddr even if not started so that Linux
428 * will have access to the hwaddr that u-boot stored for the device.
429 * This is accomplished by attempting to probe each device and calling
430 * their write_hwaddr() operation.
432 uclass_first_device_check(UCLASS_ETH, &dev);
434 log_err("No ethernet found.\n");
435 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
437 char *ethprime = env_get("ethprime");
438 struct udevice *prime_dev = NULL;
441 prime_dev = eth_get_dev_by_name(ethprime);
443 eth_set_dev(prime_dev);
444 eth_current_changed();
449 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
451 if (device_active(dev)) {
455 printf("eth%d: %s", dev_seq(dev), dev->name);
457 if (ethprime && dev == prime_dev)
461 eth_write_hwaddr(dev);
463 if (device_active(dev))
465 uclass_next_device_check(&dev);
469 log_err("No ethernet found.\n");
476 static int eth_post_bind(struct udevice *dev)
480 if (strchr(dev->name, ' ')) {
481 printf("\nError: eth device name \"%s\" has a space!\n",
486 #ifdef CONFIG_DM_ETH_PHY
487 eth_phy_binds_nodes(dev);
489 if (CONFIG_IS_ENABLED(BOOTDEV_ETH)) {
490 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
492 return log_msg_ret("bootdev", ret);
498 static int eth_pre_unbind(struct udevice *dev)
500 /* Don't hang onto a pointer that is going away */
501 if (dev == eth_get_uclass_priv()->current)
507 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
509 #if CONFIG_IS_ENABLED(OF_CONTROL)
511 struct nvmem_cell mac_cell;
513 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
515 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
518 memcpy(mac, p, ARP_HLEN);
522 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
525 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
531 static int eth_post_probe(struct udevice *dev)
533 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
534 struct eth_pdata *pdata = dev_get_plat(dev);
535 unsigned char env_enetaddr[ARP_HLEN];
538 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
539 struct eth_ops *ops = eth_get_ops(dev);
540 static int reloc_done;
544 ops->start += gd->reloc_off;
546 ops->send += gd->reloc_off;
548 ops->recv += gd->reloc_off;
550 ops->free_pkt += gd->reloc_off;
552 ops->stop += gd->reloc_off;
554 ops->mcast += gd->reloc_off;
555 if (ops->write_hwaddr)
556 ops->write_hwaddr += gd->reloc_off;
557 if (ops->read_rom_hwaddr)
558 ops->read_rom_hwaddr += gd->reloc_off;
564 priv->state = ETH_STATE_INIT;
565 priv->running = false;
567 /* Check if the device has a valid MAC address in device tree */
568 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
569 !is_valid_ethaddr(pdata->enetaddr)) {
571 /* Check if the device has a MAC address in ROM */
572 if (eth_get_ops(dev)->read_rom_hwaddr)
573 eth_get_ops(dev)->read_rom_hwaddr(dev);
576 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
577 if (!is_zero_ethaddr(env_enetaddr)) {
578 if (!is_zero_ethaddr(pdata->enetaddr) &&
579 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
580 printf("\nWarning: %s MAC addresses don't match:\n",
582 printf("Address in %s is\t\t%pM\n",
583 source, pdata->enetaddr);
584 printf("Address in environment is\t%pM\n",
588 /* Override the ROM MAC address */
589 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
590 } else if (is_valid_ethaddr(pdata->enetaddr)) {
591 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
593 } else if (is_zero_ethaddr(pdata->enetaddr) ||
594 !is_valid_ethaddr(pdata->enetaddr)) {
595 #ifdef CONFIG_NET_RANDOM_ETHADDR
596 net_random_ethaddr(pdata->enetaddr);
597 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
598 dev->name, dev_seq(dev), pdata->enetaddr);
599 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
602 printf("\nError: %s address not set.\n",
608 eth_write_hwaddr(dev);
613 static int eth_pre_remove(struct udevice *dev)
615 struct eth_pdata *pdata = dev_get_plat(dev);
617 eth_get_ops(dev)->stop(dev);
619 /* clear the MAC address */
620 memset(pdata->enetaddr, 0, ARP_HLEN);
625 UCLASS_DRIVER(ethernet) = {
628 .post_bind = eth_post_bind,
629 .pre_unbind = eth_pre_unbind,
630 .post_probe = eth_post_probe,
631 .pre_remove = eth_pre_remove,
632 .priv_auto = sizeof(struct eth_uclass_priv),
633 .per_device_auto = sizeof(struct eth_device_priv),
634 .flags = DM_UC_FLAG_SEQ_ALIAS,