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
12 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
15 #include "eth_internal.h"
18 DECLARE_GLOBAL_DATA_PTR;
21 * struct eth_device_priv - private structure for each Ethernet device
23 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
25 struct eth_device_priv {
26 enum eth_state_t state;
30 * struct eth_uclass_priv - The structure attached to the uclass itself
32 * @current: The Ethernet device that the network functions are using
34 struct eth_uclass_priv {
35 struct udevice *current;
38 /* eth_errno - This stores the most recent failure code from DM functions */
41 static struct eth_uclass_priv *eth_get_uclass_priv(void)
46 ret = uclass_get(UCLASS_ETH, &uc);
54 void eth_set_current_to_next(void)
56 struct eth_uclass_priv *uc_priv;
58 uc_priv = eth_get_uclass_priv();
60 uclass_next_device(&uc_priv->current);
61 if (!uc_priv->current)
62 uclass_first_device(UCLASS_ETH, &uc_priv->current);
66 * Typically this will simply return the active device.
67 * In the case where the most recent active device was unset, this will attempt
68 * to return the first device. If that device doesn't exist or fails to probe,
69 * this function will return NULL.
71 struct udevice *eth_get_dev(void)
73 struct eth_uclass_priv *uc_priv;
75 uc_priv = eth_get_uclass_priv();
76 if (!uc_priv->current)
77 eth_errno = uclass_first_device(UCLASS_ETH,
79 return uc_priv->current;
83 * Typically this will just store a device pointer.
84 * In case it was not probed, we will attempt to do so.
85 * dev may be NULL to unset the active device.
87 void eth_set_dev(struct udevice *dev)
89 if (dev && !device_active(dev)) {
90 eth_errno = device_probe(dev);
95 eth_get_uclass_priv()->current = dev;
99 * Find the udevice that either has the name passed in as devname or has an
100 * alias named devname.
102 struct udevice *eth_get_dev_by_name(const char *devname)
106 const char *startp = NULL;
109 int len = strlen("eth");
112 /* Must be longer than 3 to be an alias */
113 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
114 startp = devname + len;
115 seq = simple_strtoul(startp, &endp, 10);
118 ret = uclass_get(UCLASS_ETH, &uc);
122 uclass_foreach_dev(it, uc) {
124 * We need the seq to be valid, so try to probe it.
125 * If the probe fails, the seq will not match since it will be
126 * -1 instead of what we are looking for.
127 * We don't care about errors from probe here. Either they won't
128 * match an alias or it will match a literal name and we'll pick
129 * up the error when we try to probe again in eth_set_dev().
131 if (device_probe(it))
133 /* Check for the name or the sequence number to match */
134 if (strcmp(it->name, devname) == 0 ||
135 (endp > startp && it->seq == seq))
142 unsigned char *eth_get_ethaddr(void)
144 struct eth_pdata *pdata;
147 pdata = eth_get_dev()->platdata;
148 return pdata->enetaddr;
154 /* Set active state without calling start on the driver */
155 int eth_init_state_only(void)
157 struct udevice *current;
158 struct eth_device_priv *priv;
160 current = eth_get_dev();
161 if (!current || !device_active(current))
164 priv = current->uclass_priv;
165 priv->state = ETH_STATE_ACTIVE;
170 /* Set passive state without calling stop on the driver */
171 void eth_halt_state_only(void)
173 struct udevice *current;
174 struct eth_device_priv *priv;
176 current = eth_get_dev();
177 if (!current || !device_active(current))
180 priv = current->uclass_priv;
181 priv->state = ETH_STATE_PASSIVE;
184 int eth_get_dev_index(void)
187 return eth_get_dev()->seq;
191 static int eth_write_hwaddr(struct udevice *dev)
193 struct eth_pdata *pdata;
196 if (!dev || !device_active(dev))
199 /* seq is valid since the device is active */
200 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
201 pdata = dev->platdata;
202 if (!is_valid_ethaddr(pdata->enetaddr)) {
203 printf("\nError: %s address %pM illegal value\n",
204 dev->name, pdata->enetaddr);
209 * Drivers are allowed to decide not to implement this at
210 * run-time. E.g. Some devices may use it and some may not.
212 ret = eth_get_ops(dev)->write_hwaddr(dev);
216 printf("\nWarning: %s failed to set MAC address\n",
223 static int on_ethaddr(const char *name, const char *value, enum env_op op,
230 /* look for an index after "eth" */
231 index = simple_strtoul(name + 3, NULL, 10);
233 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
235 struct eth_pdata *pdata = dev->platdata;
238 case env_op_overwrite:
239 string_to_enetaddr(value, pdata->enetaddr);
240 eth_write_hwaddr(dev);
243 memset(pdata->enetaddr, 0, ARP_HLEN);
249 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
253 char *ethact = env_get("ethact");
254 char *ethrotate = env_get("ethrotate");
255 struct udevice *current = NULL;
256 struct udevice *old_current;
260 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
261 * is already set to an ethernet device, we should stick to 'ethact'.
263 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
265 current = eth_get_dev_by_name(ethact);
272 current = eth_get_dev();
274 printf("No ethernet found.\n");
279 old_current = current;
282 debug("Trying %s\n", current->name);
284 if (device_active(current)) {
285 ret = eth_get_ops(current)->start(current);
287 struct eth_device_priv *priv =
288 current->uclass_priv;
290 priv->state = ETH_STATE_ACTIVE;
299 debug("PROBE FAIL\n");
303 * If ethrotate is enabled, this will change "current",
304 * otherwise we will drop out of this while loop immediately
307 /* This will ensure the new "current" attempted to probe */
308 current = eth_get_dev();
309 } while (old_current != current);
316 struct udevice *current;
317 struct eth_device_priv *priv;
319 current = eth_get_dev();
320 if (!current || !eth_is_active(current))
323 eth_get_ops(current)->stop(current);
324 priv = current->uclass_priv;
326 priv->state = ETH_STATE_PASSIVE;
329 int eth_is_active(struct udevice *dev)
331 struct eth_device_priv *priv;
333 if (!dev || !device_active(dev))
336 priv = dev_get_uclass_priv(dev);
337 return priv->state == ETH_STATE_ACTIVE;
340 int eth_send(void *packet, int length)
342 struct udevice *current;
345 current = eth_get_dev();
349 if (!eth_is_active(current))
352 ret = eth_get_ops(current)->send(current, packet, length);
354 /* We cannot completely return the error at present */
355 debug("%s: send() returned error %d\n", __func__, ret);
357 #if defined(CONFIG_CMD_PCAP)
359 pcap_post(packet, length, true);
366 struct udevice *current;
372 current = eth_get_dev();
376 if (!eth_is_active(current))
379 /* Process up to 32 packets at one time */
380 flags = ETH_RECV_CHECK_DEVICE;
381 for (i = 0; i < 32; i++) {
382 ret = eth_get_ops(current)->recv(current, flags, &packet);
385 net_process_received_packet(packet, ret);
386 if (ret >= 0 && eth_get_ops(current)->free_pkt)
387 eth_get_ops(current)->free_pkt(current, packet, ret);
394 /* We cannot completely return the error at present */
395 debug("%s: recv() returned error %d\n", __func__, ret);
400 int eth_initialize(void)
408 * Devices need to write the hwaddr even if not started so that Linux
409 * will have access to the hwaddr that u-boot stored for the device.
410 * This is accomplished by attempting to probe each device and calling
411 * their write_hwaddr() operation.
413 uclass_first_device_check(UCLASS_ETH, &dev);
415 printf("No ethernet found.\n");
416 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
418 char *ethprime = env_get("ethprime");
419 struct udevice *prime_dev = NULL;
422 prime_dev = eth_get_dev_by_name(ethprime);
424 eth_set_dev(prime_dev);
425 eth_current_changed();
430 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
432 if (dev->seq != -1) {
436 printf("eth%d: %s", dev->seq, dev->name);
438 if (ethprime && dev == prime_dev)
442 eth_write_hwaddr(dev);
446 uclass_next_device_check(&dev);
450 printf("No ethernet found.\n");
457 static int eth_post_bind(struct udevice *dev)
459 if (strchr(dev->name, ' ')) {
460 printf("\nError: eth device name \"%s\" has a space!\n",
465 #ifdef CONFIG_DM_ETH_PHY
466 eth_phy_binds_nodes(dev);
472 static int eth_pre_unbind(struct udevice *dev)
474 /* Don't hang onto a pointer that is going away */
475 if (dev == eth_get_uclass_priv()->current)
481 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
483 #if IS_ENABLED(CONFIG_OF_CONTROL)
486 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
488 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
493 memcpy(mac, p, ARP_HLEN);
501 static int eth_post_probe(struct udevice *dev)
503 struct eth_device_priv *priv = dev->uclass_priv;
504 struct eth_pdata *pdata = dev->platdata;
505 unsigned char env_enetaddr[ARP_HLEN];
508 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
509 struct eth_ops *ops = eth_get_ops(dev);
510 static int reloc_done;
514 ops->start += gd->reloc_off;
516 ops->send += gd->reloc_off;
518 ops->recv += gd->reloc_off;
520 ops->free_pkt += gd->reloc_off;
522 ops->stop += gd->reloc_off;
524 ops->mcast += gd->reloc_off;
525 if (ops->write_hwaddr)
526 ops->write_hwaddr += gd->reloc_off;
527 if (ops->read_rom_hwaddr)
528 ops->read_rom_hwaddr += gd->reloc_off;
534 priv->state = ETH_STATE_INIT;
536 /* Check if the device has a valid MAC address in device tree */
537 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
538 !is_valid_ethaddr(pdata->enetaddr)) {
540 /* Check if the device has a MAC address in ROM */
541 if (eth_get_ops(dev)->read_rom_hwaddr)
542 eth_get_ops(dev)->read_rom_hwaddr(dev);
545 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
546 if (!is_zero_ethaddr(env_enetaddr)) {
547 if (!is_zero_ethaddr(pdata->enetaddr) &&
548 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
549 printf("\nWarning: %s MAC addresses don't match:\n",
551 printf("Address in %s is\t\t%pM\n",
552 source, pdata->enetaddr);
553 printf("Address in environment is\t%pM\n",
557 /* Override the ROM MAC address */
558 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
559 } else if (is_valid_ethaddr(pdata->enetaddr)) {
560 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
561 printf("\nWarning: %s using MAC address from %s\n",
563 } else if (is_zero_ethaddr(pdata->enetaddr) ||
564 !is_valid_ethaddr(pdata->enetaddr)) {
565 #ifdef CONFIG_NET_RANDOM_ETHADDR
566 net_random_ethaddr(pdata->enetaddr);
567 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
568 dev->name, dev->seq, pdata->enetaddr);
570 printf("\nError: %s address not set.\n",
576 eth_write_hwaddr(dev);
581 static int eth_pre_remove(struct udevice *dev)
583 struct eth_pdata *pdata = dev->platdata;
585 eth_get_ops(dev)->stop(dev);
587 /* clear the MAC address */
588 memset(pdata->enetaddr, 0, ARP_HLEN);
593 UCLASS_DRIVER(eth) = {
596 .post_bind = eth_post_bind,
597 .pre_unbind = eth_pre_unbind,
598 .post_probe = eth_post_probe,
599 .pre_remove = eth_pre_remove,
600 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
601 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
602 .flags = DM_UC_FLAG_SEQ_ALIAS,