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
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
16 #include "eth_internal.h"
19 DECLARE_GLOBAL_DATA_PTR;
22 * struct eth_device_priv - private structure for each Ethernet device
24 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
26 struct eth_device_priv {
27 enum eth_state_t state;
31 * struct eth_uclass_priv - The structure attached to the uclass itself
33 * @current: The Ethernet device that the network functions are using
35 struct eth_uclass_priv {
36 struct udevice *current;
39 /* eth_errno - This stores the most recent failure code from DM functions */
42 static struct eth_uclass_priv *eth_get_uclass_priv(void)
47 ret = uclass_get(UCLASS_ETH, &uc);
55 void eth_set_current_to_next(void)
57 struct eth_uclass_priv *uc_priv;
59 uc_priv = eth_get_uclass_priv();
61 uclass_next_device(&uc_priv->current);
62 if (!uc_priv->current)
63 uclass_first_device(UCLASS_ETH, &uc_priv->current);
67 * Typically this will simply return the active device.
68 * In the case where the most recent active device was unset, this will attempt
69 * to return the first device. If that device doesn't exist or fails to probe,
70 * this function will return NULL.
72 struct udevice *eth_get_dev(void)
74 struct eth_uclass_priv *uc_priv;
76 uc_priv = eth_get_uclass_priv();
77 if (!uc_priv->current)
78 eth_errno = uclass_first_device(UCLASS_ETH,
80 return uc_priv->current;
84 * Typically this will just store a device pointer.
85 * In case it was not probed, we will attempt to do so.
86 * dev may be NULL to unset the active device.
88 void eth_set_dev(struct udevice *dev)
90 if (dev && !device_active(dev)) {
91 eth_errno = device_probe(dev);
96 eth_get_uclass_priv()->current = dev;
100 * Find the udevice that either has the name passed in as devname or has an
101 * alias named devname.
103 struct udevice *eth_get_dev_by_name(const char *devname)
107 const char *startp = NULL;
110 int len = strlen("eth");
113 /* Must be longer than 3 to be an alias */
114 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
115 startp = devname + len;
116 seq = simple_strtoul(startp, &endp, 10);
119 ret = uclass_get(UCLASS_ETH, &uc);
123 uclass_foreach_dev(it, uc) {
125 * We need the seq to be valid, so try to probe it.
126 * If the probe fails, the seq will not match since it will be
127 * -1 instead of what we are looking for.
128 * We don't care about errors from probe here. Either they won't
129 * match an alias or it will match a literal name and we'll pick
130 * up the error when we try to probe again in eth_set_dev().
132 if (device_probe(it))
134 /* Check for the name or the sequence number to match */
135 if (strcmp(it->name, devname) == 0 ||
136 (endp > startp && it->seq == seq))
143 unsigned char *eth_get_ethaddr(void)
145 struct eth_pdata *pdata;
148 pdata = eth_get_dev()->platdata;
149 return pdata->enetaddr;
155 /* Set active state without calling start on the driver */
156 int eth_init_state_only(void)
158 struct udevice *current;
159 struct eth_device_priv *priv;
161 current = eth_get_dev();
162 if (!current || !device_active(current))
165 priv = current->uclass_priv;
166 priv->state = ETH_STATE_ACTIVE;
171 /* Set passive state without calling stop on the driver */
172 void eth_halt_state_only(void)
174 struct udevice *current;
175 struct eth_device_priv *priv;
177 current = eth_get_dev();
178 if (!current || !device_active(current))
181 priv = current->uclass_priv;
182 priv->state = ETH_STATE_PASSIVE;
185 int eth_get_dev_index(void)
188 return eth_get_dev()->seq;
192 static int eth_write_hwaddr(struct udevice *dev)
194 struct eth_pdata *pdata;
197 if (!dev || !device_active(dev))
200 /* seq is valid since the device is active */
201 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
202 pdata = dev->platdata;
203 if (!is_valid_ethaddr(pdata->enetaddr)) {
204 printf("\nError: %s address %pM illegal value\n",
205 dev->name, pdata->enetaddr);
210 * Drivers are allowed to decide not to implement this at
211 * run-time. E.g. Some devices may use it and some may not.
213 ret = eth_get_ops(dev)->write_hwaddr(dev);
217 printf("\nWarning: %s failed to set MAC address\n",
224 static int on_ethaddr(const char *name, const char *value, enum env_op op,
231 /* look for an index after "eth" */
232 index = simple_strtoul(name + 3, NULL, 10);
234 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
236 struct eth_pdata *pdata = dev->platdata;
239 case env_op_overwrite:
240 string_to_enetaddr(value, pdata->enetaddr);
241 eth_write_hwaddr(dev);
244 memset(pdata->enetaddr, 0, ARP_HLEN);
250 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
254 char *ethact = env_get("ethact");
255 char *ethrotate = env_get("ethrotate");
256 struct udevice *current = NULL;
257 struct udevice *old_current;
261 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
262 * is already set to an ethernet device, we should stick to 'ethact'.
264 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
266 current = eth_get_dev_by_name(ethact);
273 current = eth_get_dev();
275 printf("No ethernet found.\n");
280 old_current = current;
283 debug("Trying %s\n", current->name);
285 if (device_active(current)) {
286 ret = eth_get_ops(current)->start(current);
288 struct eth_device_priv *priv =
289 current->uclass_priv;
291 priv->state = ETH_STATE_ACTIVE;
300 debug("PROBE FAIL\n");
304 * If ethrotate is enabled, this will change "current",
305 * otherwise we will drop out of this while loop immediately
308 /* This will ensure the new "current" attempted to probe */
309 current = eth_get_dev();
310 } while (old_current != current);
317 struct udevice *current;
318 struct eth_device_priv *priv;
320 current = eth_get_dev();
321 if (!current || !eth_is_active(current))
324 eth_get_ops(current)->stop(current);
325 priv = current->uclass_priv;
327 priv->state = ETH_STATE_PASSIVE;
330 int eth_is_active(struct udevice *dev)
332 struct eth_device_priv *priv;
334 if (!dev || !device_active(dev))
337 priv = dev_get_uclass_priv(dev);
338 return priv->state == ETH_STATE_ACTIVE;
341 int eth_send(void *packet, int length)
343 struct udevice *current;
346 current = eth_get_dev();
350 if (!eth_is_active(current))
353 ret = eth_get_ops(current)->send(current, packet, length);
355 /* We cannot completely return the error at present */
356 debug("%s: send() returned error %d\n", __func__, ret);
358 #if defined(CONFIG_CMD_PCAP)
360 pcap_post(packet, length, true);
367 struct udevice *current;
373 current = eth_get_dev();
377 if (!eth_is_active(current))
380 /* Process up to 32 packets at one time */
381 flags = ETH_RECV_CHECK_DEVICE;
382 for (i = 0; i < 32; i++) {
383 ret = eth_get_ops(current)->recv(current, flags, &packet);
386 net_process_received_packet(packet, ret);
387 if (ret >= 0 && eth_get_ops(current)->free_pkt)
388 eth_get_ops(current)->free_pkt(current, packet, ret);
395 /* We cannot completely return the error at present */
396 debug("%s: recv() returned error %d\n", __func__, ret);
401 int eth_initialize(void)
409 * Devices need to write the hwaddr even if not started so that Linux
410 * will have access to the hwaddr that u-boot stored for the device.
411 * This is accomplished by attempting to probe each device and calling
412 * their write_hwaddr() operation.
414 uclass_first_device_check(UCLASS_ETH, &dev);
416 printf("No ethernet found.\n");
417 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
419 char *ethprime = env_get("ethprime");
420 struct udevice *prime_dev = NULL;
423 prime_dev = eth_get_dev_by_name(ethprime);
425 eth_set_dev(prime_dev);
426 eth_current_changed();
431 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
433 if (dev->seq != -1) {
437 printf("eth%d: %s", dev->seq, dev->name);
439 if (ethprime && dev == prime_dev)
443 eth_write_hwaddr(dev);
447 uclass_next_device_check(&dev);
451 printf("No ethernet found.\n");
458 static int eth_post_bind(struct udevice *dev)
460 if (strchr(dev->name, ' ')) {
461 printf("\nError: eth device name \"%s\" has a space!\n",
466 #ifdef CONFIG_DM_ETH_PHY
467 eth_phy_binds_nodes(dev);
473 static int eth_pre_unbind(struct udevice *dev)
475 /* Don't hang onto a pointer that is going away */
476 if (dev == eth_get_uclass_priv()->current)
482 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
484 #if IS_ENABLED(CONFIG_OF_CONTROL)
487 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
489 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
494 memcpy(mac, p, ARP_HLEN);
502 static int eth_post_probe(struct udevice *dev)
504 struct eth_device_priv *priv = dev->uclass_priv;
505 struct eth_pdata *pdata = dev->platdata;
506 unsigned char env_enetaddr[ARP_HLEN];
509 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
510 struct eth_ops *ops = eth_get_ops(dev);
511 static int reloc_done;
515 ops->start += gd->reloc_off;
517 ops->send += gd->reloc_off;
519 ops->recv += gd->reloc_off;
521 ops->free_pkt += gd->reloc_off;
523 ops->stop += gd->reloc_off;
525 ops->mcast += gd->reloc_off;
526 if (ops->write_hwaddr)
527 ops->write_hwaddr += gd->reloc_off;
528 if (ops->read_rom_hwaddr)
529 ops->read_rom_hwaddr += gd->reloc_off;
535 priv->state = ETH_STATE_INIT;
537 /* Check if the device has a valid MAC address in device tree */
538 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
539 !is_valid_ethaddr(pdata->enetaddr)) {
541 /* Check if the device has a MAC address in ROM */
542 if (eth_get_ops(dev)->read_rom_hwaddr)
543 eth_get_ops(dev)->read_rom_hwaddr(dev);
546 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
547 if (!is_zero_ethaddr(env_enetaddr)) {
548 if (!is_zero_ethaddr(pdata->enetaddr) &&
549 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
550 printf("\nWarning: %s MAC addresses don't match:\n",
552 printf("Address in %s is\t\t%pM\n",
553 source, pdata->enetaddr);
554 printf("Address in environment is\t%pM\n",
558 /* Override the ROM MAC address */
559 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
560 } else if (is_valid_ethaddr(pdata->enetaddr)) {
561 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
562 printf("\nWarning: %s using MAC address from %s\n",
564 } else if (is_zero_ethaddr(pdata->enetaddr) ||
565 !is_valid_ethaddr(pdata->enetaddr)) {
566 #ifdef CONFIG_NET_RANDOM_ETHADDR
567 net_random_ethaddr(pdata->enetaddr);
568 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
569 dev->name, dev->seq, pdata->enetaddr);
571 printf("\nError: %s address not set.\n",
577 eth_write_hwaddr(dev);
582 static int eth_pre_remove(struct udevice *dev)
584 struct eth_pdata *pdata = dev->platdata;
586 eth_get_ops(dev)->stop(dev);
588 /* clear the MAC address */
589 memset(pdata->enetaddr, 0, ARP_HLEN);
594 UCLASS_DRIVER(eth) = {
597 .post_bind = eth_post_bind,
598 .pre_unbind = eth_pre_unbind,
599 .post_probe = eth_post_probe,
600 .pre_remove = eth_pre_remove,
601 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
602 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
603 .flags = DM_UC_FLAG_SEQ_ALIAS,