2 * (C) Copyright 2001-2015
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * Joe Hershberger, National Instruments
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <environment.h>
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
15 #include "eth_internal.h"
18 * struct eth_device_priv - private structure for each Ethernet device
20 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
22 struct eth_device_priv
{
23 enum eth_state_t state
;
27 * struct eth_uclass_priv - The structure attached to the uclass itself
29 * @current: The Ethernet device that the network functions are using
31 struct eth_uclass_priv
{
32 struct udevice
*current
;
35 /* eth_errno - This stores the most recent failure code from DM functions */
38 static struct eth_uclass_priv
*eth_get_uclass_priv(void)
42 uclass_get(UCLASS_ETH
, &uc
);
47 void eth_set_current_to_next(void)
49 struct eth_uclass_priv
*uc_priv
;
51 uc_priv
= eth_get_uclass_priv();
53 uclass_next_device(&uc_priv
->current
);
54 if (!uc_priv
->current
)
55 uclass_first_device(UCLASS_ETH
, &uc_priv
->current
);
59 * Typically this will simply return the active device.
60 * In the case where the most recent active device was unset, this will attempt
61 * to return the first device. If that device doesn't exist or fails to probe,
62 * this function will return NULL.
64 struct udevice
*eth_get_dev(void)
66 struct eth_uclass_priv
*uc_priv
;
68 uc_priv
= eth_get_uclass_priv();
69 if (!uc_priv
->current
)
70 eth_errno
= uclass_first_device(UCLASS_ETH
,
72 return uc_priv
->current
;
76 * Typically this will just store a device pointer.
77 * In case it was not probed, we will attempt to do so.
78 * dev may be NULL to unset the active device.
80 void eth_set_dev(struct udevice
*dev
)
82 if (dev
&& !device_active(dev
)) {
83 eth_errno
= device_probe(dev
);
88 eth_get_uclass_priv()->current
= dev
;
92 * Find the udevice that either has the name passed in as devname or has an
93 * alias named devname.
95 struct udevice
*eth_get_dev_by_name(const char *devname
)
99 const char *startp
= NULL
;
102 int len
= strlen("eth");
104 /* Must be longer than 3 to be an alias */
105 if (!strncmp(devname
, "eth", len
) && strlen(devname
) > len
) {
106 startp
= devname
+ len
;
107 seq
= simple_strtoul(startp
, &endp
, 10);
110 uclass_get(UCLASS_ETH
, &uc
);
111 uclass_foreach_dev(it
, uc
) {
113 * We need the seq to be valid, so try to probe it.
114 * If the probe fails, the seq will not match since it will be
115 * -1 instead of what we are looking for.
116 * We don't care about errors from probe here. Either they won't
117 * match an alias or it will match a literal name and we'll pick
118 * up the error when we try to probe again in eth_set_dev().
120 if (device_probe(it
))
122 /* Check for the name or the sequence number to match */
123 if (strcmp(it
->name
, devname
) == 0 ||
124 (endp
> startp
&& it
->seq
== seq
))
131 unsigned char *eth_get_ethaddr(void)
133 struct eth_pdata
*pdata
;
136 pdata
= eth_get_dev()->platdata
;
137 return pdata
->enetaddr
;
143 /* Set active state without calling start on the driver */
144 int eth_init_state_only(void)
146 struct udevice
*current
;
147 struct eth_device_priv
*priv
;
149 current
= eth_get_dev();
150 if (!current
|| !device_active(current
))
153 priv
= current
->uclass_priv
;
154 priv
->state
= ETH_STATE_ACTIVE
;
159 /* Set passive state without calling stop on the driver */
160 void eth_halt_state_only(void)
162 struct udevice
*current
;
163 struct eth_device_priv
*priv
;
165 current
= eth_get_dev();
166 if (!current
|| !device_active(current
))
169 priv
= current
->uclass_priv
;
170 priv
->state
= ETH_STATE_PASSIVE
;
173 int eth_get_dev_index(void)
176 return eth_get_dev()->seq
;
180 static int eth_write_hwaddr(struct udevice
*dev
)
182 struct eth_pdata
*pdata
= dev
->platdata
;
185 if (!dev
|| !device_active(dev
))
188 /* seq is valid since the device is active */
189 if (eth_get_ops(dev
)->write_hwaddr
&& !eth_mac_skip(dev
->seq
)) {
190 if (!is_valid_ethaddr(pdata
->enetaddr
)) {
191 printf("\nError: %s address %pM illegal value\n",
192 dev
->name
, pdata
->enetaddr
);
197 * Drivers are allowed to decide not to implement this at
198 * run-time. E.g. Some devices may use it and some may not.
200 ret
= eth_get_ops(dev
)->write_hwaddr(dev
);
204 printf("\nWarning: %s failed to set MAC address\n",
211 static int on_ethaddr(const char *name
, const char *value
, enum env_op op
,
218 /* look for an index after "eth" */
219 index
= simple_strtoul(name
+ 3, NULL
, 10);
221 retval
= uclass_find_device_by_seq(UCLASS_ETH
, index
, false, &dev
);
223 struct eth_pdata
*pdata
= dev
->platdata
;
226 case env_op_overwrite
:
227 eth_parse_enetaddr(value
, pdata
->enetaddr
);
230 memset(pdata
->enetaddr
, 0, 6);
236 U_BOOT_ENV_CALLBACK(ethaddr
, on_ethaddr
);
240 char *ethact
= getenv("ethact");
241 char *ethrotate
= getenv("ethrotate");
242 struct udevice
*current
= NULL
;
243 struct udevice
*old_current
;
247 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
248 * is already set to an ethernet device, we should stick to 'ethact'.
250 if ((ethrotate
!= NULL
) && (strcmp(ethrotate
, "no") == 0)) {
252 current
= eth_get_dev_by_name(ethact
);
259 current
= eth_get_dev();
261 printf("No ethernet found.\n");
266 old_current
= current
;
269 debug("Trying %s\n", current
->name
);
271 if (device_active(current
)) {
272 ret
= eth_get_ops(current
)->start(current
);
274 struct eth_device_priv
*priv
=
275 current
->uclass_priv
;
277 priv
->state
= ETH_STATE_ACTIVE
;
286 debug("PROBE FAIL\n");
290 * If ethrotate is enabled, this will change "current",
291 * otherwise we will drop out of this while loop immediately
294 /* This will ensure the new "current" attempted to probe */
295 current
= eth_get_dev();
296 } while (old_current
!= current
);
303 struct udevice
*current
;
304 struct eth_device_priv
*priv
;
306 current
= eth_get_dev();
307 if (!current
|| !device_active(current
))
310 eth_get_ops(current
)->stop(current
);
311 priv
= current
->uclass_priv
;
312 priv
->state
= ETH_STATE_PASSIVE
;
315 int eth_is_active(struct udevice
*dev
)
317 struct eth_device_priv
*priv
;
319 if (!dev
|| !device_active(dev
))
322 priv
= dev_get_uclass_priv(dev
);
323 return priv
->state
== ETH_STATE_ACTIVE
;
326 int eth_send(void *packet
, int length
)
328 struct udevice
*current
;
331 current
= eth_get_dev();
335 if (!device_active(current
))
338 ret
= eth_get_ops(current
)->send(current
, packet
, length
);
340 /* We cannot completely return the error at present */
341 debug("%s: send() returned error %d\n", __func__
, ret
);
348 struct udevice
*current
;
354 current
= eth_get_dev();
358 if (!device_active(current
))
361 /* Process up to 32 packets at one time */
362 flags
= ETH_RECV_CHECK_DEVICE
;
363 for (i
= 0; i
< 32; i
++) {
364 ret
= eth_get_ops(current
)->recv(current
, flags
, &packet
);
367 net_process_received_packet(packet
, ret
);
368 if (ret
>= 0 && eth_get_ops(current
)->free_pkt
)
369 eth_get_ops(current
)->free_pkt(current
, packet
, ret
);
376 /* We cannot completely return the error at present */
377 debug("%s: recv() returned error %d\n", __func__
, ret
);
382 int eth_initialize(void)
390 * Devices need to write the hwaddr even if not started so that Linux
391 * will have access to the hwaddr that u-boot stored for the device.
392 * This is accomplished by attempting to probe each device and calling
393 * their write_hwaddr() operation.
395 uclass_first_device(UCLASS_ETH
, &dev
);
397 printf("No ethernet found.\n");
398 bootstage_error(BOOTSTAGE_ID_NET_ETH_START
);
400 char *ethprime
= getenv("ethprime");
401 struct udevice
*prime_dev
= NULL
;
404 prime_dev
= eth_get_dev_by_name(ethprime
);
406 eth_set_dev(prime_dev
);
407 eth_current_changed();
412 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT
);
417 printf("eth%d: %s", dev
->seq
, dev
->name
);
419 if (ethprime
&& dev
== prime_dev
)
422 eth_write_hwaddr(dev
);
424 uclass_next_device(&dev
);
434 static int eth_post_bind(struct udevice
*dev
)
436 if (strchr(dev
->name
, ' ')) {
437 printf("\nError: eth device name \"%s\" has a space!\n",
445 static int eth_pre_unbind(struct udevice
*dev
)
447 /* Don't hang onto a pointer that is going away */
448 if (dev
== eth_get_uclass_priv()->current
)
454 static int eth_post_probe(struct udevice
*dev
)
456 struct eth_device_priv
*priv
= dev
->uclass_priv
;
457 struct eth_pdata
*pdata
= dev
->platdata
;
458 unsigned char env_enetaddr
[6];
460 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
461 struct eth_ops
*ops
= eth_get_ops(dev
);
462 static int reloc_done
;
466 ops
->start
+= gd
->reloc_off
;
468 ops
->send
+= gd
->reloc_off
;
470 ops
->recv
+= gd
->reloc_off
;
472 ops
->free_pkt
+= gd
->reloc_off
;
474 ops
->stop
+= gd
->reloc_off
;
475 #ifdef CONFIG_MCAST_TFTP
477 ops
->mcast
+= gd
->reloc_off
;
479 if (ops
->write_hwaddr
)
480 ops
->write_hwaddr
+= gd
->reloc_off
;
481 if (ops
->read_rom_hwaddr
)
482 ops
->read_rom_hwaddr
+= gd
->reloc_off
;
488 priv
->state
= ETH_STATE_INIT
;
490 /* Check if the device has a MAC address in ROM */
491 if (eth_get_ops(dev
)->read_rom_hwaddr
)
492 eth_get_ops(dev
)->read_rom_hwaddr(dev
);
494 eth_getenv_enetaddr_by_index("eth", dev
->seq
, env_enetaddr
);
495 if (!is_zero_ethaddr(env_enetaddr
)) {
496 if (!is_zero_ethaddr(pdata
->enetaddr
) &&
497 memcmp(pdata
->enetaddr
, env_enetaddr
, 6)) {
498 printf("\nWarning: %s MAC addresses don't match:\n",
500 printf("Address in SROM is %pM\n",
502 printf("Address in environment is %pM\n",
506 /* Override the ROM MAC address */
507 memcpy(pdata
->enetaddr
, env_enetaddr
, 6);
508 } else if (is_valid_ethaddr(pdata
->enetaddr
)) {
509 eth_setenv_enetaddr_by_index("eth", dev
->seq
, pdata
->enetaddr
);
510 printf("\nWarning: %s using MAC address from ROM\n",
512 } else if (is_zero_ethaddr(pdata
->enetaddr
)) {
513 #ifdef CONFIG_NET_RANDOM_ETHADDR
514 net_random_ethaddr(pdata
->enetaddr
);
515 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
516 dev
->name
, dev
->seq
, pdata
->enetaddr
);
518 printf("\nError: %s address not set.\n",
527 static int eth_pre_remove(struct udevice
*dev
)
529 struct eth_pdata
*pdata
= dev
->platdata
;
531 eth_get_ops(dev
)->stop(dev
);
533 /* clear the MAC address */
534 memset(pdata
->enetaddr
, 0, 6);
539 UCLASS_DRIVER(eth
) = {
542 .post_bind
= eth_post_bind
,
543 .pre_unbind
= eth_pre_unbind
,
544 .post_probe
= eth_post_probe
,
545 .pre_remove
= eth_pre_remove
,
546 .priv_auto_alloc_size
= sizeof(struct eth_uclass_priv
),
547 .per_device_auto_alloc_size
= sizeof(struct eth_device_priv
),
548 .flags
= DM_UC_FLAG_SEQ_ALIAS
,