blob: 5146bd6666c13afa3ad16d9f2ee38dc320ce8593 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdb9391e2016-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 Glassdb9391e2016-01-17 14:52:00 -07006 */
7
8#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -06009#include <bootstage.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070010#include <dm.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060011#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070013#include <net.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070015#include <dm/device-internal.h>
16#include <dm/uclass-internal.h>
Ramon Fried3eaac632019-07-18 21:43:30 +030017#include <net/pcap.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070018#include "eth_internal.h"
Ye Li5fe419e2020-05-03 22:41:14 +080019#include <eth_phy.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070020
Simon Glassa7c45ec2016-01-30 15:45:14 -070021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glassdb9391e2016-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 Schifferfa795f42020-11-04 14:45:14 +010030 bool running;
Simon Glassdb9391e2016-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 Fand2b70202020-05-03 22:41:13 +080048 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -070049
Peng Fand2b70202020-05-03 22:41:13 +080050 ret = uclass_get(UCLASS_ETH, &uc);
51 if (ret)
52 return NULL;
53
Simon Glassdb9391e2016-01-17 14:52:00 -070054 assert(uc);
Simon Glass0fd3d912020-12-22 19:30:28 -070055 return uclass_get_priv(uc);
Simon Glassdb9391e2016-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 Walle6e424b42021-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 Glassdb9391e2016-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 Andersonc3f02782020-09-12 17:45:43 -040083 if (!uc_priv)
84 return NULL;
85
Michael Walle6e424b42021-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 Glassdb9391e2016-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 Fand2b70202020-05-03 22:41:13 +0800124 int ret;
Simon Glassdb9391e2016-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 Fand2b70202020-05-03 22:41:13 +0800132 ret = uclass_get(UCLASS_ETH, &uc);
133 if (ret)
134 return NULL;
135
Simon Glassdb9391e2016-01-17 14:52:00 -0700136 uclass_foreach_dev(it, uc) {
137 /*
Simon Glassdb9391e2016-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 Glass8b85dfc2020-12-16 21:20:07 -0700146 (endp > startp && dev_seq(it) == seq))
Simon Glassdb9391e2016-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 Glass0fd3d912020-12-22 19:30:28 -0700158 pdata = dev_get_plat(eth_get_dev());
Simon Glassdb9391e2016-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 Glass0fd3d912020-12-22 19:30:28 -0700175 priv = dev_get_uclass_priv(current);
Simon Glassdb9391e2016-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 Glass0fd3d912020-12-22 19:30:28 -0700191 priv = dev_get_uclass_priv(current);
Simon Glassdb9391e2016-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 Glass8b85dfc2020-12-16 21:20:07 -0700198 return dev_seq(eth_get_dev());
Simon Glassdb9391e2016-01-17 14:52:00 -0700199 return -1;
200}
201
202static int eth_write_hwaddr(struct udevice *dev)
203{
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200204 struct eth_pdata *pdata;
Simon Glassdb9391e2016-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 Glass8b85dfc2020-12-16 21:20:07 -0700211 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700212 pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-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 Glass99175912020-12-16 21:20:29 -0700244 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700245 if (!retval) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700246 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700247 switch (op) {
248 case env_op_create:
249 case env_op_overwrite:
Joe Hershbergerfb8977c2019-09-13 19:21:16 -0500250 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzerc86ff7f2016-09-02 14:48:17 +0200251 eth_write_hwaddr(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700252 break;
253 case env_op_delete:
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100254 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-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 Glass00caae62017-08-03 12:22:12 -0600264 char *ethact = env_get("ethact");
265 char *ethrotate = env_get("ethrotate");
Simon Glassdb9391e2016-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 Schuchardtad895912020-09-14 11:00:18 +0200285 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-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 Glass0fd3d912020-12-22 19:30:28 -0700299 dev_get_uclass_priv(current);
Simon Glassdb9391e2016-01-17 14:52:00 -0700300
301 priv->state = ETH_STATE_ACTIVE;
Matthias Schifferfa795f42020-11-04 14:45:14 +0100302 priv->running = true;
Simon Glassdb9391e2016-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 Schifferfa795f42020-11-04 14:45:14 +0100332 if (!current)
333 return;
334
335 priv = dev_get_uclass_priv(current);
336 if (!priv || !priv->running)
Simon Glassdb9391e2016-01-17 14:52:00 -0700337 return;
338
339 eth_get_ops(current)->stop(current);
Matthias Schifferfa795f42020-11-04 14:45:14 +0100340 priv->state = ETH_STATE_PASSIVE;
341 priv->running = false;
Simon Glassdb9391e2016-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 Grafa532e2f2018-03-15 15:07:09 +0100364 if (!eth_is_active(current))
Simon Glassdb9391e2016-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 Fried3eaac632019-07-18 21:43:30 +0300372#if defined(CONFIG_CMD_PCAP)
373 if (ret >= 0)
374 pcap_post(packet, length, true);
375#endif
Simon Glassdb9391e2016-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 Grafa532e2f2018-03-15 15:07:09 +0100391 if (!eth_is_active(current))
Simon Glassdb9391e2016-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 Wildt36ea0ca2020-10-07 11:03:30 +0200396 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassdb9391e2016-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 Six3ce43042018-04-27 14:52:56 +0200428 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700429 if (!dev) {
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200430 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700431 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
432 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600433 char *ethprime = env_get("ethprime");
Simon Glassdb9391e2016-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 Glass552da332020-12-16 21:20:16 -0700447 if (device_active(dev)) {
Michael Walle19820db2019-10-22 01:03:10 +0200448 if (num_devices)
449 printf(", ");
Simon Glassdb9391e2016-01-17 14:52:00 -0700450
Simon Glass8b85dfc2020-12-16 21:20:07 -0700451 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassdb9391e2016-01-17 14:52:00 -0700452
Michael Walle19820db2019-10-22 01:03:10 +0200453 if (ethprime && dev == prime_dev)
454 printf(" [PRIME]");
455 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700456
457 eth_write_hwaddr(dev);
458
Simon Glass552da332020-12-16 21:20:16 -0700459 if (device_active(dev))
Michael Walle19820db2019-10-22 01:03:10 +0200460 num_devices++;
Mario Six3ce43042018-04-27 14:52:56 +0200461 uclass_next_device_check(&dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700462 } while (dev);
463
Michael Walle19820db2019-10-22 01:03:10 +0200464 if (!num_devices)
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200465 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-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 Li5fe419e2020-05-03 22:41:14 +0800480#ifdef CONFIG_DM_ETH_PHY
481 eth_phy_binds_nodes(dev);
482#endif
483
Simon Glassdb9391e2016-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 Reding379af672019-05-20 17:59:57 +0200496static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
497{
Simon Glass177e7f92021-01-13 20:29:47 -0700498#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding379af672019-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 Glassdb9391e2016-01-17 14:52:00 -0700516static int eth_post_probe(struct udevice *dev)
517{
Simon Glass0fd3d912020-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.nla40db6d2016-11-25 16:30:19 +0100520 unsigned char env_enetaddr[ARP_HLEN];
Michal Simekd4172c92020-03-16 11:36:17 +0100521 char *source = "DT";
Simon Glassdb9391e2016-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 Glassdb9391e2016-01-17 14:52:00 -0700538 if (ops->mcast)
539 ops->mcast += gd->reloc_off;
Simon Glassdb9391e2016-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 Schifferfa795f42020-11-04 14:45:14 +0100550 priv->running = false;
Simon Glassdb9391e2016-01-17 14:52:00 -0700551
Thierry Reding379af672019-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 Simekd4172c92020-03-16 11:36:17 +0100555 source = "ROM";
Thierry Reding379af672019-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 Glassdb9391e2016-01-17 14:52:00 -0700560
Simon Glass8b85dfc2020-12-16 21:20:07 -0700561 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700562 if (!is_zero_ethaddr(env_enetaddr)) {
563 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100564 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700565 printf("\nWarning: %s MAC addresses don't match:\n",
566 dev->name);
Michal Simekd4172c92020-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 Glassdb9391e2016-01-17 14:52:00 -0700570 env_enetaddr);
571 }
572
573 /* Override the ROM MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100574 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700575 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass8b85dfc2020-12-16 21:20:07 -0700576 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
577 pdata->enetaddr);
Siva Durga Prasad Paladuguaa555fe2016-11-02 12:52:13 +0100578 } else if (is_zero_ethaddr(pdata->enetaddr) ||
579 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassdb9391e2016-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 Glass8b85dfc2020-12-16 21:20:07 -0700583 dev->name, dev_seq(dev), pdata->enetaddr);
Simon Glassdb9391e2016-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 Redingb743bbd2019-05-20 17:59:56 +0200591 eth_write_hwaddr(dev);
592
Simon Glassdb9391e2016-01-17 14:52:00 -0700593 return 0;
594}
595
596static int eth_pre_remove(struct udevice *dev)
597{
Simon Glass0fd3d912020-12-22 19:30:28 -0700598 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700599
600 eth_get_ops(dev)->stop(dev);
601
602 /* clear the MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100603 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700604
605 return 0;
606}
607
Michael Walle82a3c9e2021-02-25 16:51:11 +0100608UCLASS_DRIVER(ethernet) = {
609 .name = "ethernet",
Simon Glassdb9391e2016-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 Glass41575d82020-12-03 16:55:17 -0700615 .priv_auto = sizeof(struct eth_uclass_priv),
616 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassdb9391e2016-01-17 14:52:00 -0700617 .flags = DM_UC_FLAG_SEQ_ALIAS,
618};