blob: 5146bd6666c13afa3ad16d9f2ee38dc320ce8593 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc6011d22016-01-17 14:52:00 -07002/*
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
Simon Glassc6011d22016-01-17 14:52:00 -07006 */
7
8#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06009#include <bootstage.h>
Simon Glassc6011d22016-01-17 14:52:00 -070010#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassc6011d22016-01-17 14:52:00 -070013#include <net.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glassc6011d22016-01-17 14:52:00 -070015#include <dm/device-internal.h>
16#include <dm/uclass-internal.h>
Ramon Friedac598c12019-07-18 21:43:30 +030017#include <net/pcap.h>
Simon Glassc6011d22016-01-17 14:52:00 -070018#include "eth_internal.h"
Ye Licd5bb772020-05-03 22:41:14 +080019#include <eth_phy.h>
Simon Glassc6011d22016-01-17 14:52:00 -070020
Simon Glass004b8912016-01-30 15:45:14 -070021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glassc6011d22016-01-17 14:52:00 -070023/**
24 * struct eth_device_priv - private structure for each Ethernet device
25 *
26 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
27 */
28struct eth_device_priv {
29 enum eth_state_t state;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +010030 bool running;
Simon Glassc6011d22016-01-17 14:52:00 -070031};
32
33/**
34 * struct eth_uclass_priv - The structure attached to the uclass itself
35 *
36 * @current: The Ethernet device that the network functions are using
37 */
38struct eth_uclass_priv {
39 struct udevice *current;
40};
41
42/* eth_errno - This stores the most recent failure code from DM functions */
43static int eth_errno;
44
45static struct eth_uclass_priv *eth_get_uclass_priv(void)
46{
47 struct uclass *uc;
Peng Fan46ec1042020-05-03 22:41:13 +080048 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -070049
Peng Fan46ec1042020-05-03 22:41:13 +080050 ret = uclass_get(UCLASS_ETH, &uc);
51 if (ret)
52 return NULL;
53
Simon Glassc6011d22016-01-17 14:52:00 -070054 assert(uc);
Simon Glass95588622020-12-22 19:30:28 -070055 return uclass_get_priv(uc);
Simon Glassc6011d22016-01-17 14:52:00 -070056}
57
58void eth_set_current_to_next(void)
59{
60 struct eth_uclass_priv *uc_priv;
61
62 uc_priv = eth_get_uclass_priv();
63 if (uc_priv->current)
64 uclass_next_device(&uc_priv->current);
65 if (!uc_priv->current)
66 uclass_first_device(UCLASS_ETH, &uc_priv->current);
67}
68
69/*
70 * Typically this will simply return the active device.
71 * In the case where the most recent active device was unset, this will attempt
Michael Walle384023e2021-02-24 17:30:44 +010072 * to return the device with sequence id 0 (which can be configured by the
73 * device tree). If this fails, fall back to just getting the first device.
74 * The latter is non-deterministic and depends on the order of the probing.
75 * If that device doesn't exist or fails to probe, this function will return
76 * NULL.
Simon Glassc6011d22016-01-17 14:52:00 -070077 */
78struct udevice *eth_get_dev(void)
79{
80 struct eth_uclass_priv *uc_priv;
81
82 uc_priv = eth_get_uclass_priv();
Sean Anderson35e82982020-09-12 17:45:43 -040083 if (!uc_priv)
84 return NULL;
85
Michael Walle384023e2021-02-24 17:30:44 +010086 if (!uc_priv->current) {
87 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
88 &uc_priv->current);
89 if (eth_errno)
90 eth_errno = uclass_first_device(UCLASS_ETH,
91 &uc_priv->current);
92 }
Simon Glassc6011d22016-01-17 14:52:00 -070093 return uc_priv->current;
94}
95
96/*
97 * Typically this will just store a device pointer.
98 * In case it was not probed, we will attempt to do so.
99 * dev may be NULL to unset the active device.
100 */
101void eth_set_dev(struct udevice *dev)
102{
103 if (dev && !device_active(dev)) {
104 eth_errno = device_probe(dev);
105 if (eth_errno)
106 dev = NULL;
107 }
108
109 eth_get_uclass_priv()->current = dev;
110}
111
112/*
113 * Find the udevice that either has the name passed in as devname or has an
114 * alias named devname.
115 */
116struct udevice *eth_get_dev_by_name(const char *devname)
117{
118 int seq = -1;
119 char *endp = NULL;
120 const char *startp = NULL;
121 struct udevice *it;
122 struct uclass *uc;
123 int len = strlen("eth");
Peng Fan46ec1042020-05-03 22:41:13 +0800124 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -0700125
126 /* Must be longer than 3 to be an alias */
127 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
128 startp = devname + len;
129 seq = simple_strtoul(startp, &endp, 10);
130 }
131
Peng Fan46ec1042020-05-03 22:41:13 +0800132 ret = uclass_get(UCLASS_ETH, &uc);
133 if (ret)
134 return NULL;
135
Simon Glassc6011d22016-01-17 14:52:00 -0700136 uclass_foreach_dev(it, uc) {
137 /*
Simon Glassc6011d22016-01-17 14:52:00 -0700138 * We don't care about errors from probe here. Either they won't
139 * match an alias or it will match a literal name and we'll pick
140 * up the error when we try to probe again in eth_set_dev().
141 */
142 if (device_probe(it))
143 continue;
144 /* Check for the name or the sequence number to match */
145 if (strcmp(it->name, devname) == 0 ||
Simon Glass75e534b2020-12-16 21:20:07 -0700146 (endp > startp && dev_seq(it) == seq))
Simon Glassc6011d22016-01-17 14:52:00 -0700147 return it;
148 }
149
150 return NULL;
151}
152
153unsigned char *eth_get_ethaddr(void)
154{
155 struct eth_pdata *pdata;
156
157 if (eth_get_dev()) {
Simon Glass95588622020-12-22 19:30:28 -0700158 pdata = dev_get_plat(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700159 return pdata->enetaddr;
160 }
161
162 return NULL;
163}
164
165/* Set active state without calling start on the driver */
166int eth_init_state_only(void)
167{
168 struct udevice *current;
169 struct eth_device_priv *priv;
170
171 current = eth_get_dev();
172 if (!current || !device_active(current))
173 return -EINVAL;
174
Simon Glass95588622020-12-22 19:30:28 -0700175 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700176 priv->state = ETH_STATE_ACTIVE;
177
178 return 0;
179}
180
181/* Set passive state without calling stop on the driver */
182void eth_halt_state_only(void)
183{
184 struct udevice *current;
185 struct eth_device_priv *priv;
186
187 current = eth_get_dev();
188 if (!current || !device_active(current))
189 return;
190
Simon Glass95588622020-12-22 19:30:28 -0700191 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700192 priv->state = ETH_STATE_PASSIVE;
193}
194
195int eth_get_dev_index(void)
196{
197 if (eth_get_dev())
Simon Glass75e534b2020-12-16 21:20:07 -0700198 return dev_seq(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700199 return -1;
200}
201
202static int eth_write_hwaddr(struct udevice *dev)
203{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200204 struct eth_pdata *pdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700205 int ret = 0;
206
207 if (!dev || !device_active(dev))
208 return -EINVAL;
209
210 /* seq is valid since the device is active */
Simon Glass75e534b2020-12-16 21:20:07 -0700211 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass95588622020-12-22 19:30:28 -0700212 pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700213 if (!is_valid_ethaddr(pdata->enetaddr)) {
214 printf("\nError: %s address %pM illegal value\n",
215 dev->name, pdata->enetaddr);
216 return -EINVAL;
217 }
218
219 /*
220 * Drivers are allowed to decide not to implement this at
221 * run-time. E.g. Some devices may use it and some may not.
222 */
223 ret = eth_get_ops(dev)->write_hwaddr(dev);
224 if (ret == -ENOSYS)
225 ret = 0;
226 if (ret)
227 printf("\nWarning: %s failed to set MAC address\n",
228 dev->name);
229 }
230
231 return ret;
232}
233
234static int on_ethaddr(const char *name, const char *value, enum env_op op,
235 int flags)
236{
237 int index;
238 int retval;
239 struct udevice *dev;
240
241 /* look for an index after "eth" */
242 index = simple_strtoul(name + 3, NULL, 10);
243
Simon Glass07e13382020-12-16 21:20:29 -0700244 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700245 if (!retval) {
Simon Glass95588622020-12-22 19:30:28 -0700246 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700247 switch (op) {
248 case env_op_create:
249 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500250 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200251 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700252 break;
253 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100254 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700255 }
256 }
257
258 return 0;
259}
260U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
261
262int eth_init(void)
263{
Simon Glass64b723f2017-08-03 12:22:12 -0600264 char *ethact = env_get("ethact");
265 char *ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-01-17 14:52:00 -0700266 struct udevice *current = NULL;
267 struct udevice *old_current;
268 int ret = -ENODEV;
269
270 /*
271 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
272 * is already set to an ethernet device, we should stick to 'ethact'.
273 */
274 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
275 if (ethact) {
276 current = eth_get_dev_by_name(ethact);
277 if (!current)
278 return -EINVAL;
279 }
280 }
281
282 if (!current) {
283 current = eth_get_dev();
284 if (!current) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200285 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700286 return -ENODEV;
287 }
288 }
289
290 old_current = current;
291 do {
292 if (current) {
293 debug("Trying %s\n", current->name);
294
295 if (device_active(current)) {
296 ret = eth_get_ops(current)->start(current);
297 if (ret >= 0) {
298 struct eth_device_priv *priv =
Simon Glass95588622020-12-22 19:30:28 -0700299 dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700300
301 priv->state = ETH_STATE_ACTIVE;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100302 priv->running = true;
Simon Glassc6011d22016-01-17 14:52:00 -0700303 return 0;
304 }
305 } else {
306 ret = eth_errno;
307 }
308
309 debug("FAIL\n");
310 } else {
311 debug("PROBE FAIL\n");
312 }
313
314 /*
315 * If ethrotate is enabled, this will change "current",
316 * otherwise we will drop out of this while loop immediately
317 */
318 eth_try_another(0);
319 /* This will ensure the new "current" attempted to probe */
320 current = eth_get_dev();
321 } while (old_current != current);
322
323 return ret;
324}
325
326void eth_halt(void)
327{
328 struct udevice *current;
329 struct eth_device_priv *priv;
330
331 current = eth_get_dev();
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100332 if (!current)
Simon Glassc6011d22016-01-17 14:52:00 -0700333 return;
334
Simon Glass95588622020-12-22 19:30:28 -0700335 priv = dev_get_uclass_priv(current);
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100336 if (!priv || !priv->running)
337 return;
338
339 eth_get_ops(current)->stop(current);
340 priv->state = ETH_STATE_PASSIVE;
341 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700342}
343
344int eth_is_active(struct udevice *dev)
345{
346 struct eth_device_priv *priv;
347
348 if (!dev || !device_active(dev))
349 return 0;
350
351 priv = dev_get_uclass_priv(dev);
352 return priv->state == ETH_STATE_ACTIVE;
353}
354
355int eth_send(void *packet, int length)
356{
357 struct udevice *current;
358 int ret;
359
360 current = eth_get_dev();
361 if (!current)
362 return -ENODEV;
363
Alexander Graf71511492018-03-15 15:07:09 +0100364 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700365 return -EINVAL;
366
367 ret = eth_get_ops(current)->send(current, packet, length);
368 if (ret < 0) {
369 /* We cannot completely return the error at present */
370 debug("%s: send() returned error %d\n", __func__, ret);
371 }
Ramon Friedac598c12019-07-18 21:43:30 +0300372#if defined(CONFIG_CMD_PCAP)
373 if (ret >= 0)
374 pcap_post(packet, length, true);
375#endif
Simon Glassc6011d22016-01-17 14:52:00 -0700376 return ret;
377}
378
379int eth_rx(void)
380{
381 struct udevice *current;
382 uchar *packet;
383 int flags;
384 int ret;
385 int i;
386
387 current = eth_get_dev();
388 if (!current)
389 return -ENODEV;
390
Alexander Graf71511492018-03-15 15:07:09 +0100391 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700392 return -EINVAL;
393
394 /* Process up to 32 packets at one time */
395 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildtd47430a2020-10-07 11:03:30 +0200396 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassc6011d22016-01-17 14:52:00 -0700397 ret = eth_get_ops(current)->recv(current, flags, &packet);
398 flags = 0;
399 if (ret > 0)
400 net_process_received_packet(packet, ret);
401 if (ret >= 0 && eth_get_ops(current)->free_pkt)
402 eth_get_ops(current)->free_pkt(current, packet, ret);
403 if (ret <= 0)
404 break;
405 }
406 if (ret == -EAGAIN)
407 ret = 0;
408 if (ret < 0) {
409 /* We cannot completely return the error at present */
410 debug("%s: recv() returned error %d\n", __func__, ret);
411 }
412 return ret;
413}
414
415int eth_initialize(void)
416{
417 int num_devices = 0;
418 struct udevice *dev;
419
420 eth_common_init();
421
422 /*
423 * Devices need to write the hwaddr even if not started so that Linux
424 * will have access to the hwaddr that u-boot stored for the device.
425 * This is accomplished by attempting to probe each device and calling
426 * their write_hwaddr() operation.
427 */
Mario Six865c3872018-04-27 14:52:56 +0200428 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700429 if (!dev) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200430 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700431 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
432 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600433 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-01-17 14:52:00 -0700434 struct udevice *prime_dev = NULL;
435
436 if (ethprime)
437 prime_dev = eth_get_dev_by_name(ethprime);
438 if (prime_dev) {
439 eth_set_dev(prime_dev);
440 eth_current_changed();
441 } else {
442 eth_set_dev(NULL);
443 }
444
445 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
446 do {
Simon Glass3e14a222020-12-16 21:20:16 -0700447 if (device_active(dev)) {
Michael Walle902c7962019-10-22 01:03:10 +0200448 if (num_devices)
449 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700450
Simon Glass75e534b2020-12-16 21:20:07 -0700451 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700452
Michael Walle902c7962019-10-22 01:03:10 +0200453 if (ethprime && dev == prime_dev)
454 printf(" [PRIME]");
455 }
Simon Glassc6011d22016-01-17 14:52:00 -0700456
457 eth_write_hwaddr(dev);
458
Simon Glass3e14a222020-12-16 21:20:16 -0700459 if (device_active(dev))
Michael Walle902c7962019-10-22 01:03:10 +0200460 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200461 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700462 } while (dev);
463
Michael Walle902c7962019-10-22 01:03:10 +0200464 if (!num_devices)
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200465 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700466 putc('\n');
467 }
468
469 return num_devices;
470}
471
472static int eth_post_bind(struct udevice *dev)
473{
474 if (strchr(dev->name, ' ')) {
475 printf("\nError: eth device name \"%s\" has a space!\n",
476 dev->name);
477 return -EINVAL;
478 }
479
Ye Licd5bb772020-05-03 22:41:14 +0800480#ifdef CONFIG_DM_ETH_PHY
481 eth_phy_binds_nodes(dev);
482#endif
483
Simon Glassc6011d22016-01-17 14:52:00 -0700484 return 0;
485}
486
487static int eth_pre_unbind(struct udevice *dev)
488{
489 /* Don't hang onto a pointer that is going away */
490 if (dev == eth_get_uclass_priv()->current)
491 eth_set_dev(NULL);
492
493 return 0;
494}
495
Thierry Reding8fe08532019-05-20 17:59:57 +0200496static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
497{
Simon Glass1eb72942021-01-13 20:29:47 -0700498#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding8fe08532019-05-20 17:59:57 +0200499 const uint8_t *p;
500
501 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
502 if (!p)
503 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
504
505 if (!p)
506 return false;
507
508 memcpy(mac, p, ARP_HLEN);
509
510 return true;
511#else
512 return false;
513#endif
514}
515
Simon Glassc6011d22016-01-17 14:52:00 -0700516static int eth_post_probe(struct udevice *dev)
517{
Simon Glass95588622020-12-22 19:30:28 -0700518 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
519 struct eth_pdata *pdata = dev_get_plat(dev);
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100520 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100521 char *source = "DT";
Simon Glassc6011d22016-01-17 14:52:00 -0700522
523#if defined(CONFIG_NEEDS_MANUAL_RELOC)
524 struct eth_ops *ops = eth_get_ops(dev);
525 static int reloc_done;
526
527 if (!reloc_done) {
528 if (ops->start)
529 ops->start += gd->reloc_off;
530 if (ops->send)
531 ops->send += gd->reloc_off;
532 if (ops->recv)
533 ops->recv += gd->reloc_off;
534 if (ops->free_pkt)
535 ops->free_pkt += gd->reloc_off;
536 if (ops->stop)
537 ops->stop += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700538 if (ops->mcast)
539 ops->mcast += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700540 if (ops->write_hwaddr)
541 ops->write_hwaddr += gd->reloc_off;
542 if (ops->read_rom_hwaddr)
543 ops->read_rom_hwaddr += gd->reloc_off;
544
545 reloc_done++;
546 }
547#endif
548
549 priv->state = ETH_STATE_INIT;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100550 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700551
Thierry Reding8fe08532019-05-20 17:59:57 +0200552 /* Check if the device has a valid MAC address in device tree */
553 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
554 !is_valid_ethaddr(pdata->enetaddr)) {
Michal Simek31411132020-03-16 11:36:17 +0100555 source = "ROM";
Thierry Reding8fe08532019-05-20 17:59:57 +0200556 /* Check if the device has a MAC address in ROM */
557 if (eth_get_ops(dev)->read_rom_hwaddr)
558 eth_get_ops(dev)->read_rom_hwaddr(dev);
559 }
Simon Glassc6011d22016-01-17 14:52:00 -0700560
Simon Glass75e534b2020-12-16 21:20:07 -0700561 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700562 if (!is_zero_ethaddr(env_enetaddr)) {
563 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100564 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700565 printf("\nWarning: %s MAC addresses don't match:\n",
566 dev->name);
Michal Simek31411132020-03-16 11:36:17 +0100567 printf("Address in %s is\t\t%pM\n",
568 source, pdata->enetaddr);
569 printf("Address in environment is\t%pM\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700570 env_enetaddr);
571 }
572
573 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100574 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700575 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700576 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
577 pdata->enetaddr);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100578 } else if (is_zero_ethaddr(pdata->enetaddr) ||
579 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700580#ifdef CONFIG_NET_RANDOM_ETHADDR
581 net_random_ethaddr(pdata->enetaddr);
582 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700583 dev->name, dev_seq(dev), pdata->enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700584#else
585 printf("\nError: %s address not set.\n",
586 dev->name);
587 return -EINVAL;
588#endif
589 }
590
Thierry Reding13064872019-05-20 17:59:56 +0200591 eth_write_hwaddr(dev);
592
Simon Glassc6011d22016-01-17 14:52:00 -0700593 return 0;
594}
595
596static int eth_pre_remove(struct udevice *dev)
597{
Simon Glass95588622020-12-22 19:30:28 -0700598 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700599
600 eth_get_ops(dev)->stop(dev);
601
602 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100603 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700604
605 return 0;
606}
607
Michael Walle7efcdfd2021-02-25 16:51:11 +0100608UCLASS_DRIVER(ethernet) = {
609 .name = "ethernet",
Simon Glassc6011d22016-01-17 14:52:00 -0700610 .id = UCLASS_ETH,
611 .post_bind = eth_post_bind,
612 .pre_unbind = eth_pre_unbind,
613 .post_probe = eth_post_probe,
614 .pre_remove = eth_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700615 .priv_auto = sizeof(struct eth_uclass_priv),
616 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassc6011d22016-01-17 14:52:00 -0700617 .flags = DM_UC_FLAG_SEQ_ALIAS,
618};