blob: 34ca731d1e31c15c40c2604a2230245e728fd3e8 [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
72 * to return the first device. If that device doesn't exist or fails to probe,
73 * this function will return NULL.
74 */
75struct udevice *eth_get_dev(void)
76{
77 struct eth_uclass_priv *uc_priv;
78
79 uc_priv = eth_get_uclass_priv();
Sean Andersonc3f02782020-09-12 17:45:43 -040080 if (!uc_priv)
81 return NULL;
82
Simon Glassdb9391e2016-01-17 14:52:00 -070083 if (!uc_priv->current)
84 eth_errno = uclass_first_device(UCLASS_ETH,
85 &uc_priv->current);
86 return uc_priv->current;
87}
88
89/*
90 * Typically this will just store a device pointer.
91 * In case it was not probed, we will attempt to do so.
92 * dev may be NULL to unset the active device.
93 */
94void eth_set_dev(struct udevice *dev)
95{
96 if (dev && !device_active(dev)) {
97 eth_errno = device_probe(dev);
98 if (eth_errno)
99 dev = NULL;
100 }
101
102 eth_get_uclass_priv()->current = dev;
103}
104
105/*
106 * Find the udevice that either has the name passed in as devname or has an
107 * alias named devname.
108 */
109struct udevice *eth_get_dev_by_name(const char *devname)
110{
111 int seq = -1;
112 char *endp = NULL;
113 const char *startp = NULL;
114 struct udevice *it;
115 struct uclass *uc;
116 int len = strlen("eth");
Peng Fand2b70202020-05-03 22:41:13 +0800117 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -0700118
119 /* Must be longer than 3 to be an alias */
120 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
121 startp = devname + len;
122 seq = simple_strtoul(startp, &endp, 10);
123 }
124
Peng Fand2b70202020-05-03 22:41:13 +0800125 ret = uclass_get(UCLASS_ETH, &uc);
126 if (ret)
127 return NULL;
128
Simon Glassdb9391e2016-01-17 14:52:00 -0700129 uclass_foreach_dev(it, uc) {
130 /*
Simon Glassdb9391e2016-01-17 14:52:00 -0700131 * We don't care about errors from probe here. Either they won't
132 * match an alias or it will match a literal name and we'll pick
133 * up the error when we try to probe again in eth_set_dev().
134 */
135 if (device_probe(it))
136 continue;
137 /* Check for the name or the sequence number to match */
138 if (strcmp(it->name, devname) == 0 ||
Simon Glass8b85dfc2020-12-16 21:20:07 -0700139 (endp > startp && dev_seq(it) == seq))
Simon Glassdb9391e2016-01-17 14:52:00 -0700140 return it;
141 }
142
143 return NULL;
144}
145
146unsigned char *eth_get_ethaddr(void)
147{
148 struct eth_pdata *pdata;
149
150 if (eth_get_dev()) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700151 pdata = dev_get_plat(eth_get_dev());
Simon Glassdb9391e2016-01-17 14:52:00 -0700152 return pdata->enetaddr;
153 }
154
155 return NULL;
156}
157
158/* Set active state without calling start on the driver */
159int eth_init_state_only(void)
160{
161 struct udevice *current;
162 struct eth_device_priv *priv;
163
164 current = eth_get_dev();
165 if (!current || !device_active(current))
166 return -EINVAL;
167
Simon Glass0fd3d912020-12-22 19:30:28 -0700168 priv = dev_get_uclass_priv(current);
Simon Glassdb9391e2016-01-17 14:52:00 -0700169 priv->state = ETH_STATE_ACTIVE;
170
171 return 0;
172}
173
174/* Set passive state without calling stop on the driver */
175void eth_halt_state_only(void)
176{
177 struct udevice *current;
178 struct eth_device_priv *priv;
179
180 current = eth_get_dev();
181 if (!current || !device_active(current))
182 return;
183
Simon Glass0fd3d912020-12-22 19:30:28 -0700184 priv = dev_get_uclass_priv(current);
Simon Glassdb9391e2016-01-17 14:52:00 -0700185 priv->state = ETH_STATE_PASSIVE;
186}
187
188int eth_get_dev_index(void)
189{
190 if (eth_get_dev())
Simon Glass8b85dfc2020-12-16 21:20:07 -0700191 return dev_seq(eth_get_dev());
Simon Glassdb9391e2016-01-17 14:52:00 -0700192 return -1;
193}
194
195static int eth_write_hwaddr(struct udevice *dev)
196{
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200197 struct eth_pdata *pdata;
Simon Glassdb9391e2016-01-17 14:52:00 -0700198 int ret = 0;
199
200 if (!dev || !device_active(dev))
201 return -EINVAL;
202
203 /* seq is valid since the device is active */
Simon Glass8b85dfc2020-12-16 21:20:07 -0700204 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700205 pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700206 if (!is_valid_ethaddr(pdata->enetaddr)) {
207 printf("\nError: %s address %pM illegal value\n",
208 dev->name, pdata->enetaddr);
209 return -EINVAL;
210 }
211
212 /*
213 * Drivers are allowed to decide not to implement this at
214 * run-time. E.g. Some devices may use it and some may not.
215 */
216 ret = eth_get_ops(dev)->write_hwaddr(dev);
217 if (ret == -ENOSYS)
218 ret = 0;
219 if (ret)
220 printf("\nWarning: %s failed to set MAC address\n",
221 dev->name);
222 }
223
224 return ret;
225}
226
227static int on_ethaddr(const char *name, const char *value, enum env_op op,
228 int flags)
229{
230 int index;
231 int retval;
232 struct udevice *dev;
233
234 /* look for an index after "eth" */
235 index = simple_strtoul(name + 3, NULL, 10);
236
Simon Glass99175912020-12-16 21:20:29 -0700237 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700238 if (!retval) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700239 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700240 switch (op) {
241 case env_op_create:
242 case env_op_overwrite:
Joe Hershbergerfb8977c2019-09-13 19:21:16 -0500243 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzerc86ff7f2016-09-02 14:48:17 +0200244 eth_write_hwaddr(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700245 break;
246 case env_op_delete:
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100247 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700248 }
249 }
250
251 return 0;
252}
253U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
254
255int eth_init(void)
256{
Simon Glass00caae62017-08-03 12:22:12 -0600257 char *ethact = env_get("ethact");
258 char *ethrotate = env_get("ethrotate");
Simon Glassdb9391e2016-01-17 14:52:00 -0700259 struct udevice *current = NULL;
260 struct udevice *old_current;
261 int ret = -ENODEV;
262
263 /*
264 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
265 * is already set to an ethernet device, we should stick to 'ethact'.
266 */
267 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
268 if (ethact) {
269 current = eth_get_dev_by_name(ethact);
270 if (!current)
271 return -EINVAL;
272 }
273 }
274
275 if (!current) {
276 current = eth_get_dev();
277 if (!current) {
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200278 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700279 return -ENODEV;
280 }
281 }
282
283 old_current = current;
284 do {
285 if (current) {
286 debug("Trying %s\n", current->name);
287
288 if (device_active(current)) {
289 ret = eth_get_ops(current)->start(current);
290 if (ret >= 0) {
291 struct eth_device_priv *priv =
Simon Glass0fd3d912020-12-22 19:30:28 -0700292 dev_get_uclass_priv(current);
Simon Glassdb9391e2016-01-17 14:52:00 -0700293
294 priv->state = ETH_STATE_ACTIVE;
Matthias Schifferfa795f42020-11-04 14:45:14 +0100295 priv->running = true;
Simon Glassdb9391e2016-01-17 14:52:00 -0700296 return 0;
297 }
298 } else {
299 ret = eth_errno;
300 }
301
302 debug("FAIL\n");
303 } else {
304 debug("PROBE FAIL\n");
305 }
306
307 /*
308 * If ethrotate is enabled, this will change "current",
309 * otherwise we will drop out of this while loop immediately
310 */
311 eth_try_another(0);
312 /* This will ensure the new "current" attempted to probe */
313 current = eth_get_dev();
314 } while (old_current != current);
315
316 return ret;
317}
318
319void eth_halt(void)
320{
321 struct udevice *current;
322 struct eth_device_priv *priv;
323
324 current = eth_get_dev();
Matthias Schifferfa795f42020-11-04 14:45:14 +0100325 if (!current)
326 return;
327
328 priv = dev_get_uclass_priv(current);
329 if (!priv || !priv->running)
Simon Glassdb9391e2016-01-17 14:52:00 -0700330 return;
331
332 eth_get_ops(current)->stop(current);
Matthias Schifferfa795f42020-11-04 14:45:14 +0100333 priv->state = ETH_STATE_PASSIVE;
334 priv->running = false;
Simon Glassdb9391e2016-01-17 14:52:00 -0700335}
336
337int eth_is_active(struct udevice *dev)
338{
339 struct eth_device_priv *priv;
340
341 if (!dev || !device_active(dev))
342 return 0;
343
344 priv = dev_get_uclass_priv(dev);
345 return priv->state == ETH_STATE_ACTIVE;
346}
347
348int eth_send(void *packet, int length)
349{
350 struct udevice *current;
351 int ret;
352
353 current = eth_get_dev();
354 if (!current)
355 return -ENODEV;
356
Alexander Grafa532e2f2018-03-15 15:07:09 +0100357 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700358 return -EINVAL;
359
360 ret = eth_get_ops(current)->send(current, packet, length);
361 if (ret < 0) {
362 /* We cannot completely return the error at present */
363 debug("%s: send() returned error %d\n", __func__, ret);
364 }
Ramon Fried3eaac632019-07-18 21:43:30 +0300365#if defined(CONFIG_CMD_PCAP)
366 if (ret >= 0)
367 pcap_post(packet, length, true);
368#endif
Simon Glassdb9391e2016-01-17 14:52:00 -0700369 return ret;
370}
371
372int eth_rx(void)
373{
374 struct udevice *current;
375 uchar *packet;
376 int flags;
377 int ret;
378 int i;
379
380 current = eth_get_dev();
381 if (!current)
382 return -ENODEV;
383
Alexander Grafa532e2f2018-03-15 15:07:09 +0100384 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700385 return -EINVAL;
386
387 /* Process up to 32 packets at one time */
388 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildt36ea0ca2020-10-07 11:03:30 +0200389 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700390 ret = eth_get_ops(current)->recv(current, flags, &packet);
391 flags = 0;
392 if (ret > 0)
393 net_process_received_packet(packet, ret);
394 if (ret >= 0 && eth_get_ops(current)->free_pkt)
395 eth_get_ops(current)->free_pkt(current, packet, ret);
396 if (ret <= 0)
397 break;
398 }
399 if (ret == -EAGAIN)
400 ret = 0;
401 if (ret < 0) {
402 /* We cannot completely return the error at present */
403 debug("%s: recv() returned error %d\n", __func__, ret);
404 }
405 return ret;
406}
407
408int eth_initialize(void)
409{
410 int num_devices = 0;
411 struct udevice *dev;
412
413 eth_common_init();
414
415 /*
416 * Devices need to write the hwaddr even if not started so that Linux
417 * will have access to the hwaddr that u-boot stored for the device.
418 * This is accomplished by attempting to probe each device and calling
419 * their write_hwaddr() operation.
420 */
Mario Six3ce43042018-04-27 14:52:56 +0200421 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700422 if (!dev) {
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200423 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700424 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
425 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600426 char *ethprime = env_get("ethprime");
Simon Glassdb9391e2016-01-17 14:52:00 -0700427 struct udevice *prime_dev = NULL;
428
429 if (ethprime)
430 prime_dev = eth_get_dev_by_name(ethprime);
431 if (prime_dev) {
432 eth_set_dev(prime_dev);
433 eth_current_changed();
434 } else {
435 eth_set_dev(NULL);
436 }
437
438 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
439 do {
Simon Glass552da332020-12-16 21:20:16 -0700440 if (device_active(dev)) {
Michael Walle19820db2019-10-22 01:03:10 +0200441 if (num_devices)
442 printf(", ");
Simon Glassdb9391e2016-01-17 14:52:00 -0700443
Simon Glass8b85dfc2020-12-16 21:20:07 -0700444 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassdb9391e2016-01-17 14:52:00 -0700445
Michael Walle19820db2019-10-22 01:03:10 +0200446 if (ethprime && dev == prime_dev)
447 printf(" [PRIME]");
448 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700449
450 eth_write_hwaddr(dev);
451
Simon Glass552da332020-12-16 21:20:16 -0700452 if (device_active(dev))
Michael Walle19820db2019-10-22 01:03:10 +0200453 num_devices++;
Mario Six3ce43042018-04-27 14:52:56 +0200454 uclass_next_device_check(&dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700455 } while (dev);
456
Michael Walle19820db2019-10-22 01:03:10 +0200457 if (!num_devices)
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200458 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700459 putc('\n');
460 }
461
462 return num_devices;
463}
464
465static int eth_post_bind(struct udevice *dev)
466{
467 if (strchr(dev->name, ' ')) {
468 printf("\nError: eth device name \"%s\" has a space!\n",
469 dev->name);
470 return -EINVAL;
471 }
472
Ye Li5fe419e2020-05-03 22:41:14 +0800473#ifdef CONFIG_DM_ETH_PHY
474 eth_phy_binds_nodes(dev);
475#endif
476
Simon Glassdb9391e2016-01-17 14:52:00 -0700477 return 0;
478}
479
480static int eth_pre_unbind(struct udevice *dev)
481{
482 /* Don't hang onto a pointer that is going away */
483 if (dev == eth_get_uclass_priv()->current)
484 eth_set_dev(NULL);
485
486 return 0;
487}
488
Thierry Reding379af672019-05-20 17:59:57 +0200489static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
490{
Simon Glass177e7f92021-01-13 20:29:47 -0700491#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding379af672019-05-20 17:59:57 +0200492 const uint8_t *p;
493
494 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
495 if (!p)
496 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
497
498 if (!p)
499 return false;
500
501 memcpy(mac, p, ARP_HLEN);
502
503 return true;
504#else
505 return false;
506#endif
507}
508
Simon Glassdb9391e2016-01-17 14:52:00 -0700509static int eth_post_probe(struct udevice *dev)
510{
Simon Glass0fd3d912020-12-22 19:30:28 -0700511 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
512 struct eth_pdata *pdata = dev_get_plat(dev);
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100513 unsigned char env_enetaddr[ARP_HLEN];
Michal Simekd4172c92020-03-16 11:36:17 +0100514 char *source = "DT";
Simon Glassdb9391e2016-01-17 14:52:00 -0700515
516#if defined(CONFIG_NEEDS_MANUAL_RELOC)
517 struct eth_ops *ops = eth_get_ops(dev);
518 static int reloc_done;
519
520 if (!reloc_done) {
521 if (ops->start)
522 ops->start += gd->reloc_off;
523 if (ops->send)
524 ops->send += gd->reloc_off;
525 if (ops->recv)
526 ops->recv += gd->reloc_off;
527 if (ops->free_pkt)
528 ops->free_pkt += gd->reloc_off;
529 if (ops->stop)
530 ops->stop += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700531 if (ops->mcast)
532 ops->mcast += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700533 if (ops->write_hwaddr)
534 ops->write_hwaddr += gd->reloc_off;
535 if (ops->read_rom_hwaddr)
536 ops->read_rom_hwaddr += gd->reloc_off;
537
538 reloc_done++;
539 }
540#endif
541
542 priv->state = ETH_STATE_INIT;
Matthias Schifferfa795f42020-11-04 14:45:14 +0100543 priv->running = false;
Simon Glassdb9391e2016-01-17 14:52:00 -0700544
Thierry Reding379af672019-05-20 17:59:57 +0200545 /* Check if the device has a valid MAC address in device tree */
546 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
547 !is_valid_ethaddr(pdata->enetaddr)) {
Michal Simekd4172c92020-03-16 11:36:17 +0100548 source = "ROM";
Thierry Reding379af672019-05-20 17:59:57 +0200549 /* Check if the device has a MAC address in ROM */
550 if (eth_get_ops(dev)->read_rom_hwaddr)
551 eth_get_ops(dev)->read_rom_hwaddr(dev);
552 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700553
Simon Glass8b85dfc2020-12-16 21:20:07 -0700554 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700555 if (!is_zero_ethaddr(env_enetaddr)) {
556 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100557 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700558 printf("\nWarning: %s MAC addresses don't match:\n",
559 dev->name);
Michal Simekd4172c92020-03-16 11:36:17 +0100560 printf("Address in %s is\t\t%pM\n",
561 source, pdata->enetaddr);
562 printf("Address in environment is\t%pM\n",
Simon Glassdb9391e2016-01-17 14:52:00 -0700563 env_enetaddr);
564 }
565
566 /* Override the ROM MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100567 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700568 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass8b85dfc2020-12-16 21:20:07 -0700569 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
570 pdata->enetaddr);
Siva Durga Prasad Paladuguaa555fe2016-11-02 12:52:13 +0100571 } else if (is_zero_ethaddr(pdata->enetaddr) ||
572 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700573#ifdef CONFIG_NET_RANDOM_ETHADDR
574 net_random_ethaddr(pdata->enetaddr);
575 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
Simon Glass8b85dfc2020-12-16 21:20:07 -0700576 dev->name, dev_seq(dev), pdata->enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700577#else
578 printf("\nError: %s address not set.\n",
579 dev->name);
580 return -EINVAL;
581#endif
582 }
583
Thierry Redingb743bbd2019-05-20 17:59:56 +0200584 eth_write_hwaddr(dev);
585
Simon Glassdb9391e2016-01-17 14:52:00 -0700586 return 0;
587}
588
589static int eth_pre_remove(struct udevice *dev)
590{
Simon Glass0fd3d912020-12-22 19:30:28 -0700591 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700592
593 eth_get_ops(dev)->stop(dev);
594
595 /* clear the MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100596 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700597
598 return 0;
599}
600
Tom Rini26e85bf2021-01-19 15:35:00 -0500601UCLASS_DRIVER(eth) = {
602 .name = "eth",
Simon Glassdb9391e2016-01-17 14:52:00 -0700603 .id = UCLASS_ETH,
604 .post_bind = eth_post_bind,
605 .pre_unbind = eth_pre_unbind,
606 .post_probe = eth_post_probe,
607 .pre_remove = eth_pre_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700608 .priv_auto = sizeof(struct eth_uclass_priv),
609 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassdb9391e2016-01-17 14:52:00 -0700610 .flags = DM_UC_FLAG_SEQ_ALIAS,
611};