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"
17 DECLARE_GLOBAL_DATA_PTR
;
20 * struct eth_device_priv - private structure for each Ethernet device
22 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
24 struct eth_device_priv
{
25 enum eth_state_t state
;
29 * struct eth_uclass_priv - The structure attached to the uclass itself
31 * @current: The Ethernet device that the network functions are using
33 struct eth_uclass_priv
{
34 struct udevice
*current
;
37 /* eth_errno - This stores the most recent failure code from DM functions */
40 static struct eth_uclass_priv
*eth_get_uclass_priv(void)
44 uclass_get(UCLASS_ETH
, &uc
);
49 void eth_set_current_to_next(void)
51 struct eth_uclass_priv
*uc_priv
;
53 uc_priv
= eth_get_uclass_priv();
55 uclass_next_device(&uc_priv
->current
);
56 if (!uc_priv
->current
)
57 uclass_first_device(UCLASS_ETH
, &uc_priv
->current
);
61 * Typically this will simply return the active device.
62 * In the case where the most recent active device was unset, this will attempt
63 * to return the first device. If that device doesn't exist or fails to probe,
64 * this function will return NULL.
66 struct udevice
*eth_get_dev(void)
68 struct eth_uclass_priv
*uc_priv
;
70 uc_priv
= eth_get_uclass_priv();
71 if (!uc_priv
->current
)
72 eth_errno
= uclass_first_device(UCLASS_ETH
,
74 return uc_priv
->current
;
78 * Typically this will just store a device pointer.
79 * In case it was not probed, we will attempt to do so.
80 * dev may be NULL to unset the active device.
82 void eth_set_dev(struct udevice
*dev
)
84 if (dev
&& !device_active(dev
)) {
85 eth_errno
= device_probe(dev
);
90 eth_get_uclass_priv()->current
= dev
;
94 * Find the udevice that either has the name passed in as devname or has an
95 * alias named devname.
97 struct udevice
*eth_get_dev_by_name(const char *devname
)
101 const char *startp
= NULL
;
104 int len
= strlen("eth");
106 /* Must be longer than 3 to be an alias */
107 if (!strncmp(devname
, "eth", len
) && strlen(devname
) > len
) {
108 startp
= devname
+ len
;
109 seq
= simple_strtoul(startp
, &endp
, 10);
112 uclass_get(UCLASS_ETH
, &uc
);
113 uclass_foreach_dev(it
, uc
) {
115 * We need the seq to be valid, so try to probe it.
116 * If the probe fails, the seq will not match since it will be
117 * -1 instead of what we are looking for.
118 * We don't care about errors from probe here. Either they won't
119 * match an alias or it will match a literal name and we'll pick
120 * up the error when we try to probe again in eth_set_dev().
122 if (device_probe(it
))
124 /* Check for the name or the sequence number to match */
125 if (strcmp(it
->name
, devname
) == 0 ||
126 (endp
> startp
&& it
->seq
== seq
))
133 unsigned char *eth_get_ethaddr(void)
135 struct eth_pdata
*pdata
;
138 pdata
= eth_get_dev()->platdata
;
139 return pdata
->enetaddr
;
145 /* Set active state without calling start on the driver */
146 int eth_init_state_only(void)
148 struct udevice
*current
;
149 struct eth_device_priv
*priv
;
151 current
= eth_get_dev();
152 if (!current
|| !device_active(current
))
155 priv
= current
->uclass_priv
;
156 priv
->state
= ETH_STATE_ACTIVE
;
161 /* Set passive state without calling stop on the driver */
162 void eth_halt_state_only(void)
164 struct udevice
*current
;
165 struct eth_device_priv
*priv
;
167 current
= eth_get_dev();
168 if (!current
|| !device_active(current
))
171 priv
= current
->uclass_priv
;
172 priv
->state
= ETH_STATE_PASSIVE
;
175 int eth_get_dev_index(void)
178 return eth_get_dev()->seq
;
182 static int eth_write_hwaddr(struct udevice
*dev
)
184 struct eth_pdata
*pdata
= dev
->platdata
;
187 if (!dev
|| !device_active(dev
))
190 /* seq is valid since the device is active */
191 if (eth_get_ops(dev
)->write_hwaddr
&& !eth_mac_skip(dev
->seq
)) {
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
);
232 memset(pdata
->enetaddr
, 0, 6);
238 U_BOOT_ENV_CALLBACK(ethaddr
, on_ethaddr
);
242 char *ethact
= getenv("ethact");
243 char *ethrotate
= getenv("ethrotate");
244 struct udevice
*current
= NULL
;
245 struct udevice
*old_current
;
249 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
250 * is already set to an ethernet device, we should stick to 'ethact'.
252 if ((ethrotate
!= NULL
) && (strcmp(ethrotate
, "no") == 0)) {
254 current
= eth_get_dev_by_name(ethact
);
261 current
= eth_get_dev();
263 printf("No ethernet found.\n");
268 old_current
= current
;
271 debug("Trying %s\n", current
->name
);
273 if (device_active(current
)) {
274 ret
= eth_get_ops(current
)->start(current
);
276 struct eth_device_priv
*priv
=
277 current
->uclass_priv
;
279 priv
->state
= ETH_STATE_ACTIVE
;
288 debug("PROBE FAIL\n");
292 * If ethrotate is enabled, this will change "current",
293 * otherwise we will drop out of this while loop immediately
296 /* This will ensure the new "current" attempted to probe */
297 current
= eth_get_dev();
298 } while (old_current
!= current
);
305 struct udevice
*current
;
306 struct eth_device_priv
*priv
;
308 current
= eth_get_dev();
309 if (!current
|| !device_active(current
))
312 eth_get_ops(current
)->stop(current
);
313 priv
= current
->uclass_priv
;
314 priv
->state
= ETH_STATE_PASSIVE
;
317 int eth_is_active(struct udevice
*dev
)
319 struct eth_device_priv
*priv
;
321 if (!dev
|| !device_active(dev
))
324 priv
= dev_get_uclass_priv(dev
);
325 return priv
->state
== ETH_STATE_ACTIVE
;
328 int eth_send(void *packet
, int length
)
330 struct udevice
*current
;
333 current
= eth_get_dev();
337 if (!device_active(current
))
340 ret
= eth_get_ops(current
)->send(current
, packet
, length
);
342 /* We cannot completely return the error at present */
343 debug("%s: send() returned error %d\n", __func__
, ret
);
350 struct udevice
*current
;
356 current
= eth_get_dev();
360 if (!device_active(current
))
363 /* Process up to 32 packets at one time */
364 flags
= ETH_RECV_CHECK_DEVICE
;
365 for (i
= 0; i
< 32; i
++) {
366 ret
= eth_get_ops(current
)->recv(current
, flags
, &packet
);
369 net_process_received_packet(packet
, ret
);
370 if (ret
>= 0 && eth_get_ops(current
)->free_pkt
)
371 eth_get_ops(current
)->free_pkt(current
, packet
, ret
);
378 /* We cannot completely return the error at present */
379 debug("%s: recv() returned error %d\n", __func__
, ret
);
384 int eth_initialize(void)
392 * Devices need to write the hwaddr even if not started so that Linux
393 * will have access to the hwaddr that u-boot stored for the device.
394 * This is accomplished by attempting to probe each device and calling
395 * their write_hwaddr() operation.
397 uclass_first_device(UCLASS_ETH
, &dev
);
399 printf("No ethernet found.\n");
400 bootstage_error(BOOTSTAGE_ID_NET_ETH_START
);
402 char *ethprime
= getenv("ethprime");
403 struct udevice
*prime_dev
= NULL
;
406 prime_dev
= eth_get_dev_by_name(ethprime
);
408 eth_set_dev(prime_dev
);
409 eth_current_changed();
414 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT
);
419 printf("eth%d: %s", dev
->seq
, dev
->name
);
421 if (ethprime
&& dev
== prime_dev
)
424 eth_write_hwaddr(dev
);
426 uclass_next_device(&dev
);
436 static int eth_post_bind(struct udevice
*dev
)
438 if (strchr(dev
->name
, ' ')) {
439 printf("\nError: eth device name \"%s\" has a space!\n",
447 static int eth_pre_unbind(struct udevice
*dev
)
449 /* Don't hang onto a pointer that is going away */
450 if (dev
== eth_get_uclass_priv()->current
)
456 static int eth_post_probe(struct udevice
*dev
)
458 struct eth_device_priv
*priv
= dev
->uclass_priv
;
459 struct eth_pdata
*pdata
= dev
->platdata
;
460 unsigned char env_enetaddr
[6];
462 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
463 struct eth_ops
*ops
= eth_get_ops(dev
);
464 static int reloc_done
;
468 ops
->start
+= gd
->reloc_off
;
470 ops
->send
+= gd
->reloc_off
;
472 ops
->recv
+= gd
->reloc_off
;
474 ops
->free_pkt
+= gd
->reloc_off
;
476 ops
->stop
+= gd
->reloc_off
;
477 #ifdef CONFIG_MCAST_TFTP
479 ops
->mcast
+= gd
->reloc_off
;
481 if (ops
->write_hwaddr
)
482 ops
->write_hwaddr
+= gd
->reloc_off
;
483 if (ops
->read_rom_hwaddr
)
484 ops
->read_rom_hwaddr
+= gd
->reloc_off
;
490 priv
->state
= ETH_STATE_INIT
;
492 /* Check if the device has a MAC address in ROM */
493 if (eth_get_ops(dev
)->read_rom_hwaddr
)
494 eth_get_ops(dev
)->read_rom_hwaddr(dev
);
496 eth_getenv_enetaddr_by_index("eth", dev
->seq
, env_enetaddr
);
497 if (!is_zero_ethaddr(env_enetaddr
)) {
498 if (!is_zero_ethaddr(pdata
->enetaddr
) &&
499 memcmp(pdata
->enetaddr
, env_enetaddr
, 6)) {
500 printf("\nWarning: %s MAC addresses don't match:\n",
502 printf("Address in SROM is %pM\n",
504 printf("Address in environment is %pM\n",
508 /* Override the ROM MAC address */
509 memcpy(pdata
->enetaddr
, env_enetaddr
, 6);
510 } else if (is_valid_ethaddr(pdata
->enetaddr
)) {
511 eth_setenv_enetaddr_by_index("eth", dev
->seq
, pdata
->enetaddr
);
512 printf("\nWarning: %s using MAC address from ROM\n",
514 } else if (is_zero_ethaddr(pdata
->enetaddr
)) {
515 #ifdef CONFIG_NET_RANDOM_ETHADDR
516 net_random_ethaddr(pdata
->enetaddr
);
517 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
518 dev
->name
, dev
->seq
, pdata
->enetaddr
);
520 printf("\nError: %s address not set.\n",
529 static int eth_pre_remove(struct udevice
*dev
)
531 struct eth_pdata
*pdata
= dev
->platdata
;
533 eth_get_ops(dev
)->stop(dev
);
535 /* clear the MAC address */
536 memset(pdata
->enetaddr
, 0, 6);
541 UCLASS_DRIVER(eth
) = {
544 .post_bind
= eth_post_bind
,
545 .pre_unbind
= eth_pre_unbind
,
546 .post_probe
= eth_post_probe
,
547 .pre_remove
= eth_pre_remove
,
548 .priv_auto_alloc_size
= sizeof(struct eth_uclass_priv
),
549 .per_device_auto_alloc_size
= sizeof(struct eth_device_priv
),
550 .flags
= DM_UC_FLAG_SEQ_ALIAS
,