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
10 #include <environment.h>
12 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
14 #include "eth_internal.h"
16 DECLARE_GLOBAL_DATA_PTR;
19 * struct eth_device_priv - private structure for each Ethernet device
21 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
23 struct eth_device_priv {
24 enum eth_state_t state;
28 * struct eth_uclass_priv - The structure attached to the uclass itself
30 * @current: The Ethernet device that the network functions are using
32 struct eth_uclass_priv {
33 struct udevice *current;
36 /* eth_errno - This stores the most recent failure code from DM functions */
39 static struct eth_uclass_priv *eth_get_uclass_priv(void)
43 uclass_get(UCLASS_ETH, &uc);
48 void eth_set_current_to_next(void)
50 struct eth_uclass_priv *uc_priv;
52 uc_priv = eth_get_uclass_priv();
54 uclass_next_device(&uc_priv->current);
55 if (!uc_priv->current)
56 uclass_first_device(UCLASS_ETH, &uc_priv->current);
60 * Typically this will simply return the active device.
61 * In the case where the most recent active device was unset, this will attempt
62 * to return the first device. If that device doesn't exist or fails to probe,
63 * this function will return NULL.
65 struct udevice *eth_get_dev(void)
67 struct eth_uclass_priv *uc_priv;
69 uc_priv = eth_get_uclass_priv();
70 if (!uc_priv->current)
71 eth_errno = uclass_first_device(UCLASS_ETH,
73 return uc_priv->current;
77 * Typically this will just store a device pointer.
78 * In case it was not probed, we will attempt to do so.
79 * dev may be NULL to unset the active device.
81 void eth_set_dev(struct udevice *dev)
83 if (dev && !device_active(dev)) {
84 eth_errno = device_probe(dev);
89 eth_get_uclass_priv()->current = dev;
93 * Find the udevice that either has the name passed in as devname or has an
94 * alias named devname.
96 struct udevice *eth_get_dev_by_name(const char *devname)
100 const char *startp = NULL;
103 int len = strlen("eth");
105 /* Must be longer than 3 to be an alias */
106 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
107 startp = devname + len;
108 seq = simple_strtoul(startp, &endp, 10);
111 uclass_get(UCLASS_ETH, &uc);
112 uclass_foreach_dev(it, uc) {
114 * We need the seq to be valid, so try to probe it.
115 * If the probe fails, the seq will not match since it will be
116 * -1 instead of what we are looking for.
117 * We don't care about errors from probe here. Either they won't
118 * match an alias or it will match a literal name and we'll pick
119 * up the error when we try to probe again in eth_set_dev().
121 if (device_probe(it))
123 /* Check for the name or the sequence number to match */
124 if (strcmp(it->name, devname) == 0 ||
125 (endp > startp && it->seq == seq))
132 unsigned char *eth_get_ethaddr(void)
134 struct eth_pdata *pdata;
137 pdata = eth_get_dev()->platdata;
138 return pdata->enetaddr;
144 /* Set active state without calling start on the driver */
145 int eth_init_state_only(void)
147 struct udevice *current;
148 struct eth_device_priv *priv;
150 current = eth_get_dev();
151 if (!current || !device_active(current))
154 priv = current->uclass_priv;
155 priv->state = ETH_STATE_ACTIVE;
160 /* Set passive state without calling stop on the driver */
161 void eth_halt_state_only(void)
163 struct udevice *current;
164 struct eth_device_priv *priv;
166 current = eth_get_dev();
167 if (!current || !device_active(current))
170 priv = current->uclass_priv;
171 priv->state = ETH_STATE_PASSIVE;
174 int eth_get_dev_index(void)
177 return eth_get_dev()->seq;
181 static int eth_write_hwaddr(struct udevice *dev)
183 struct eth_pdata *pdata;
186 if (!dev || !device_active(dev))
189 /* seq is valid since the device is active */
190 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
191 pdata = dev->platdata;
192 if (!is_valid_ethaddr(pdata->enetaddr)) {
193 printf("\nError: %s address %pM illegal value\n",
194 dev->name, pdata->enetaddr);
199 * Drivers are allowed to decide not to implement this at
200 * run-time. E.g. Some devices may use it and some may not.
202 ret = eth_get_ops(dev)->write_hwaddr(dev);
206 printf("\nWarning: %s failed to set MAC address\n",
213 static int on_ethaddr(const char *name, const char *value, enum env_op op,
220 /* look for an index after "eth" */
221 index = simple_strtoul(name + 3, NULL, 10);
223 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
225 struct eth_pdata *pdata = dev->platdata;
228 case env_op_overwrite:
229 eth_parse_enetaddr(value, pdata->enetaddr);
230 eth_write_hwaddr(dev);
233 memset(pdata->enetaddr, 0, ARP_HLEN);
239 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
243 char *ethact = env_get("ethact");
244 char *ethrotate = env_get("ethrotate");
245 struct udevice *current = NULL;
246 struct udevice *old_current;
250 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
251 * is already set to an ethernet device, we should stick to 'ethact'.
253 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
255 current = eth_get_dev_by_name(ethact);
262 current = eth_get_dev();
264 printf("No ethernet found.\n");
269 old_current = current;
272 debug("Trying %s\n", current->name);
274 if (device_active(current)) {
275 ret = eth_get_ops(current)->start(current);
277 struct eth_device_priv *priv =
278 current->uclass_priv;
280 priv->state = ETH_STATE_ACTIVE;
289 debug("PROBE FAIL\n");
293 * If ethrotate is enabled, this will change "current",
294 * otherwise we will drop out of this while loop immediately
297 /* This will ensure the new "current" attempted to probe */
298 current = eth_get_dev();
299 } while (old_current != current);
306 struct udevice *current;
307 struct eth_device_priv *priv;
309 current = eth_get_dev();
310 if (!current || !device_active(current))
313 eth_get_ops(current)->stop(current);
314 priv = current->uclass_priv;
315 priv->state = ETH_STATE_PASSIVE;
318 int eth_is_active(struct udevice *dev)
320 struct eth_device_priv *priv;
322 if (!dev || !device_active(dev))
325 priv = dev_get_uclass_priv(dev);
326 return priv->state == ETH_STATE_ACTIVE;
329 int eth_send(void *packet, int length)
331 struct udevice *current;
334 current = eth_get_dev();
338 if (!eth_is_active(current))
341 ret = eth_get_ops(current)->send(current, packet, length);
343 /* We cannot completely return the error at present */
344 debug("%s: send() returned error %d\n", __func__, ret);
351 struct udevice *current;
357 current = eth_get_dev();
361 if (!eth_is_active(current))
364 /* Process up to 32 packets at one time */
365 flags = ETH_RECV_CHECK_DEVICE;
366 for (i = 0; i < 32; i++) {
367 ret = eth_get_ops(current)->recv(current, flags, &packet);
370 net_process_received_packet(packet, ret);
371 if (ret >= 0 && eth_get_ops(current)->free_pkt)
372 eth_get_ops(current)->free_pkt(current, packet, ret);
379 /* We cannot completely return the error at present */
380 debug("%s: recv() returned error %d\n", __func__, ret);
385 int eth_initialize(void)
393 * Devices need to write the hwaddr even if not started so that Linux
394 * will have access to the hwaddr that u-boot stored for the device.
395 * This is accomplished by attempting to probe each device and calling
396 * their write_hwaddr() operation.
398 uclass_first_device_check(UCLASS_ETH, &dev);
400 printf("No ethernet found.\n");
401 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
403 char *ethprime = env_get("ethprime");
404 struct udevice *prime_dev = NULL;
407 prime_dev = eth_get_dev_by_name(ethprime);
409 eth_set_dev(prime_dev);
410 eth_current_changed();
415 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
420 printf("eth%d: %s", dev->seq, dev->name);
422 if (ethprime && dev == prime_dev)
425 eth_write_hwaddr(dev);
427 uclass_next_device_check(&dev);
437 static int eth_post_bind(struct udevice *dev)
439 if (strchr(dev->name, ' ')) {
440 printf("\nError: eth device name \"%s\" has a space!\n",
448 static int eth_pre_unbind(struct udevice *dev)
450 /* Don't hang onto a pointer that is going away */
451 if (dev == eth_get_uclass_priv()->current)
457 static int eth_post_probe(struct udevice *dev)
459 struct eth_device_priv *priv = dev->uclass_priv;
460 struct eth_pdata *pdata = dev->platdata;
461 unsigned char env_enetaddr[ARP_HLEN];
463 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
464 struct eth_ops *ops = eth_get_ops(dev);
465 static int reloc_done;
469 ops->start += gd->reloc_off;
471 ops->send += gd->reloc_off;
473 ops->recv += gd->reloc_off;
475 ops->free_pkt += gd->reloc_off;
477 ops->stop += gd->reloc_off;
478 #ifdef CONFIG_MCAST_TFTP
480 ops->mcast += gd->reloc_off;
482 if (ops->write_hwaddr)
483 ops->write_hwaddr += gd->reloc_off;
484 if (ops->read_rom_hwaddr)
485 ops->read_rom_hwaddr += gd->reloc_off;
491 priv->state = ETH_STATE_INIT;
493 /* Check if the device has a MAC address in ROM */
494 if (eth_get_ops(dev)->read_rom_hwaddr)
495 eth_get_ops(dev)->read_rom_hwaddr(dev);
497 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
498 if (!is_zero_ethaddr(env_enetaddr)) {
499 if (!is_zero_ethaddr(pdata->enetaddr) &&
500 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
501 printf("\nWarning: %s MAC addresses don't match:\n",
503 printf("Address in ROM is %pM\n",
505 printf("Address in environment is %pM\n",
509 /* Override the ROM MAC address */
510 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
511 } else if (is_valid_ethaddr(pdata->enetaddr)) {
512 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
513 printf("\nWarning: %s using MAC address from ROM\n",
515 } else if (is_zero_ethaddr(pdata->enetaddr) ||
516 !is_valid_ethaddr(pdata->enetaddr)) {
517 #ifdef CONFIG_NET_RANDOM_ETHADDR
518 net_random_ethaddr(pdata->enetaddr);
519 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
520 dev->name, dev->seq, pdata->enetaddr);
522 printf("\nError: %s address not set.\n",
531 static int eth_pre_remove(struct udevice *dev)
533 struct eth_pdata *pdata = dev->platdata;
535 eth_get_ops(dev)->stop(dev);
537 /* clear the MAC address */
538 memset(pdata->enetaddr, 0, ARP_HLEN);
543 UCLASS_DRIVER(eth) = {
546 .post_bind = eth_post_bind,
547 .pre_unbind = eth_pre_unbind,
548 .post_probe = eth_post_probe,
549 .pre_remove = eth_pre_remove,
550 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
551 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
552 .flags = DM_UC_FLAG_SEQ_ALIAS,