blob: bcefc54ded891df8e3adb3e808059c3f484a5fd2 [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
Patrick Delaunaya8f2ca22021-07-20 20:15:30 +02008#define LOG_CATEGORY UCLASS_ETH
9
Simon Glassc6011d22016-01-17 14:52:00 -070010#include <common.h>
Simon Glass4f840a22022-04-24 23:31:15 -060011#include <bootdev.h>
Simon Glass1ea97892020-05-10 11:40:00 -060012#include <bootstage.h>
Simon Glassc6011d22016-01-17 14:52:00 -070013#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060014#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glassc6011d22016-01-17 14:52:00 -070016#include <net.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glassc6011d22016-01-17 14:52:00 -070018#include <dm/device-internal.h>
19#include <dm/uclass-internal.h>
Ramon Friedac598c12019-07-18 21:43:30 +030020#include <net/pcap.h>
Simon Glassc6011d22016-01-17 14:52:00 -070021#include "eth_internal.h"
Ye Licd5bb772020-05-03 22:41:14 +080022#include <eth_phy.h>
Simon Glassc6011d22016-01-17 14:52:00 -070023
Simon Glass004b8912016-01-30 15:45:14 -070024DECLARE_GLOBAL_DATA_PTR;
25
Simon Glassc6011d22016-01-17 14:52:00 -070026/**
27 * struct eth_device_priv - private structure for each Ethernet device
28 *
29 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
30 */
31struct eth_device_priv {
32 enum eth_state_t state;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +010033 bool running;
Simon Glassc6011d22016-01-17 14:52:00 -070034};
35
36/**
37 * struct eth_uclass_priv - The structure attached to the uclass itself
38 *
39 * @current: The Ethernet device that the network functions are using
40 */
41struct eth_uclass_priv {
42 struct udevice *current;
43};
44
45/* eth_errno - This stores the most recent failure code from DM functions */
46static int eth_errno;
47
48static struct eth_uclass_priv *eth_get_uclass_priv(void)
49{
50 struct uclass *uc;
Peng Fan46ec1042020-05-03 22:41:13 +080051 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -070052
Peng Fan46ec1042020-05-03 22:41:13 +080053 ret = uclass_get(UCLASS_ETH, &uc);
54 if (ret)
55 return NULL;
56
Simon Glassc6011d22016-01-17 14:52:00 -070057 assert(uc);
Simon Glass95588622020-12-22 19:30:28 -070058 return uclass_get_priv(uc);
Simon Glassc6011d22016-01-17 14:52:00 -070059}
60
61void eth_set_current_to_next(void)
62{
63 struct eth_uclass_priv *uc_priv;
64
65 uc_priv = eth_get_uclass_priv();
66 if (uc_priv->current)
67 uclass_next_device(&uc_priv->current);
68 if (!uc_priv->current)
69 uclass_first_device(UCLASS_ETH, &uc_priv->current);
70}
71
72/*
73 * Typically this will simply return the active device.
74 * In the case where the most recent active device was unset, this will attempt
Michael Walle384023e2021-02-24 17:30:44 +010075 * to return the device with sequence id 0 (which can be configured by the
76 * device tree). If this fails, fall back to just getting the first device.
77 * The latter is non-deterministic and depends on the order of the probing.
78 * If that device doesn't exist or fails to probe, this function will return
79 * NULL.
Simon Glassc6011d22016-01-17 14:52:00 -070080 */
81struct udevice *eth_get_dev(void)
82{
83 struct eth_uclass_priv *uc_priv;
84
85 uc_priv = eth_get_uclass_priv();
Sean Anderson35e82982020-09-12 17:45:43 -040086 if (!uc_priv)
87 return NULL;
88
Michael Walle384023e2021-02-24 17:30:44 +010089 if (!uc_priv->current) {
90 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
91 &uc_priv->current);
92 if (eth_errno)
93 eth_errno = uclass_first_device(UCLASS_ETH,
94 &uc_priv->current);
95 }
Simon Glassc6011d22016-01-17 14:52:00 -070096 return uc_priv->current;
97}
98
99/*
100 * Typically this will just store a device pointer.
101 * In case it was not probed, we will attempt to do so.
102 * dev may be NULL to unset the active device.
103 */
104void eth_set_dev(struct udevice *dev)
105{
106 if (dev && !device_active(dev)) {
107 eth_errno = device_probe(dev);
108 if (eth_errno)
109 dev = NULL;
110 }
111
112 eth_get_uclass_priv()->current = dev;
113}
114
115/*
116 * Find the udevice that either has the name passed in as devname or has an
117 * alias named devname.
118 */
119struct udevice *eth_get_dev_by_name(const char *devname)
120{
121 int seq = -1;
122 char *endp = NULL;
123 const char *startp = NULL;
124 struct udevice *it;
125 struct uclass *uc;
126 int len = strlen("eth");
Peng Fan46ec1042020-05-03 22:41:13 +0800127 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -0700128
129 /* Must be longer than 3 to be an alias */
130 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
131 startp = devname + len;
Simon Glassff9b9032021-07-24 09:03:30 -0600132 seq = dectoul(startp, &endp);
Simon Glassc6011d22016-01-17 14:52:00 -0700133 }
134
Peng Fan46ec1042020-05-03 22:41:13 +0800135 ret = uclass_get(UCLASS_ETH, &uc);
136 if (ret)
137 return NULL;
138
Simon Glassc6011d22016-01-17 14:52:00 -0700139 uclass_foreach_dev(it, uc) {
140 /*
Simon Glassc6011d22016-01-17 14:52:00 -0700141 * We don't care about errors from probe here. Either they won't
142 * match an alias or it will match a literal name and we'll pick
143 * up the error when we try to probe again in eth_set_dev().
144 */
145 if (device_probe(it))
146 continue;
147 /* Check for the name or the sequence number to match */
148 if (strcmp(it->name, devname) == 0 ||
Simon Glass75e534b2020-12-16 21:20:07 -0700149 (endp > startp && dev_seq(it) == seq))
Simon Glassc6011d22016-01-17 14:52:00 -0700150 return it;
151 }
152
153 return NULL;
154}
155
156unsigned char *eth_get_ethaddr(void)
157{
158 struct eth_pdata *pdata;
159
160 if (eth_get_dev()) {
Simon Glass95588622020-12-22 19:30:28 -0700161 pdata = dev_get_plat(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700162 return pdata->enetaddr;
163 }
164
165 return NULL;
166}
167
168/* Set active state without calling start on the driver */
169int eth_init_state_only(void)
170{
171 struct udevice *current;
172 struct eth_device_priv *priv;
173
174 current = eth_get_dev();
175 if (!current || !device_active(current))
176 return -EINVAL;
177
Simon Glass95588622020-12-22 19:30:28 -0700178 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700179 priv->state = ETH_STATE_ACTIVE;
180
181 return 0;
182}
183
184/* Set passive state without calling stop on the driver */
185void eth_halt_state_only(void)
186{
187 struct udevice *current;
188 struct eth_device_priv *priv;
189
190 current = eth_get_dev();
191 if (!current || !device_active(current))
192 return;
193
Simon Glass95588622020-12-22 19:30:28 -0700194 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700195 priv->state = ETH_STATE_PASSIVE;
196}
197
198int eth_get_dev_index(void)
199{
200 if (eth_get_dev())
Simon Glass75e534b2020-12-16 21:20:07 -0700201 return dev_seq(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700202 return -1;
203}
204
205static int eth_write_hwaddr(struct udevice *dev)
206{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200207 struct eth_pdata *pdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700208 int ret = 0;
209
210 if (!dev || !device_active(dev))
211 return -EINVAL;
212
213 /* seq is valid since the device is active */
Simon Glass75e534b2020-12-16 21:20:07 -0700214 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass95588622020-12-22 19:30:28 -0700215 pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700216 if (!is_valid_ethaddr(pdata->enetaddr)) {
217 printf("\nError: %s address %pM illegal value\n",
218 dev->name, pdata->enetaddr);
219 return -EINVAL;
220 }
221
222 /*
223 * Drivers are allowed to decide not to implement this at
224 * run-time. E.g. Some devices may use it and some may not.
225 */
226 ret = eth_get_ops(dev)->write_hwaddr(dev);
227 if (ret == -ENOSYS)
228 ret = 0;
229 if (ret)
230 printf("\nWarning: %s failed to set MAC address\n",
231 dev->name);
232 }
233
234 return ret;
235}
236
237static int on_ethaddr(const char *name, const char *value, enum env_op op,
238 int flags)
239{
240 int index;
241 int retval;
242 struct udevice *dev;
243
244 /* look for an index after "eth" */
Simon Glassff9b9032021-07-24 09:03:30 -0600245 index = dectoul(name + 3, NULL);
Simon Glassc6011d22016-01-17 14:52:00 -0700246
Simon Glass07e13382020-12-16 21:20:29 -0700247 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700248 if (!retval) {
Simon Glass95588622020-12-22 19:30:28 -0700249 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700250 switch (op) {
251 case env_op_create:
252 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500253 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200254 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700255 break;
256 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100257 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700258 }
259 }
260
261 return 0;
262}
263U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
264
265int eth_init(void)
266{
Simon Glass64b723f2017-08-03 12:22:12 -0600267 char *ethact = env_get("ethact");
268 char *ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-01-17 14:52:00 -0700269 struct udevice *current = NULL;
270 struct udevice *old_current;
271 int ret = -ENODEV;
272
273 /*
274 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
275 * is already set to an ethernet device, we should stick to 'ethact'.
276 */
277 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
278 if (ethact) {
279 current = eth_get_dev_by_name(ethact);
280 if (!current)
281 return -EINVAL;
282 }
283 }
284
285 if (!current) {
286 current = eth_get_dev();
287 if (!current) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200288 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700289 return -ENODEV;
290 }
291 }
292
293 old_current = current;
294 do {
295 if (current) {
296 debug("Trying %s\n", current->name);
297
298 if (device_active(current)) {
299 ret = eth_get_ops(current)->start(current);
300 if (ret >= 0) {
301 struct eth_device_priv *priv =
Simon Glass95588622020-12-22 19:30:28 -0700302 dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700303
304 priv->state = ETH_STATE_ACTIVE;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100305 priv->running = true;
Simon Glassc6011d22016-01-17 14:52:00 -0700306 return 0;
307 }
308 } else {
309 ret = eth_errno;
310 }
311
312 debug("FAIL\n");
313 } else {
314 debug("PROBE FAIL\n");
315 }
316
317 /*
318 * If ethrotate is enabled, this will change "current",
319 * otherwise we will drop out of this while loop immediately
320 */
321 eth_try_another(0);
322 /* This will ensure the new "current" attempted to probe */
323 current = eth_get_dev();
324 } while (old_current != current);
325
326 return ret;
327}
328
329void eth_halt(void)
330{
331 struct udevice *current;
332 struct eth_device_priv *priv;
333
334 current = eth_get_dev();
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100335 if (!current)
Simon Glassc6011d22016-01-17 14:52:00 -0700336 return;
337
Simon Glass95588622020-12-22 19:30:28 -0700338 priv = dev_get_uclass_priv(current);
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100339 if (!priv || !priv->running)
340 return;
341
342 eth_get_ops(current)->stop(current);
343 priv->state = ETH_STATE_PASSIVE;
344 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700345}
346
347int eth_is_active(struct udevice *dev)
348{
349 struct eth_device_priv *priv;
350
351 if (!dev || !device_active(dev))
352 return 0;
353
354 priv = dev_get_uclass_priv(dev);
355 return priv->state == ETH_STATE_ACTIVE;
356}
357
358int eth_send(void *packet, int length)
359{
360 struct udevice *current;
361 int ret;
362
363 current = eth_get_dev();
364 if (!current)
365 return -ENODEV;
366
Alexander Graf71511492018-03-15 15:07:09 +0100367 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700368 return -EINVAL;
369
370 ret = eth_get_ops(current)->send(current, packet, length);
371 if (ret < 0) {
372 /* We cannot completely return the error at present */
373 debug("%s: send() returned error %d\n", __func__, ret);
374 }
Ramon Friedac598c12019-07-18 21:43:30 +0300375#if defined(CONFIG_CMD_PCAP)
376 if (ret >= 0)
377 pcap_post(packet, length, true);
378#endif
Simon Glassc6011d22016-01-17 14:52:00 -0700379 return ret;
380}
381
382int eth_rx(void)
383{
384 struct udevice *current;
385 uchar *packet;
386 int flags;
387 int ret;
388 int i;
389
390 current = eth_get_dev();
391 if (!current)
392 return -ENODEV;
393
Alexander Graf71511492018-03-15 15:07:09 +0100394 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700395 return -EINVAL;
396
397 /* Process up to 32 packets at one time */
398 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildtd47430a2020-10-07 11:03:30 +0200399 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassc6011d22016-01-17 14:52:00 -0700400 ret = eth_get_ops(current)->recv(current, flags, &packet);
401 flags = 0;
402 if (ret > 0)
403 net_process_received_packet(packet, ret);
404 if (ret >= 0 && eth_get_ops(current)->free_pkt)
405 eth_get_ops(current)->free_pkt(current, packet, ret);
406 if (ret <= 0)
407 break;
408 }
409 if (ret == -EAGAIN)
410 ret = 0;
411 if (ret < 0) {
412 /* We cannot completely return the error at present */
413 debug("%s: recv() returned error %d\n", __func__, ret);
414 }
415 return ret;
416}
417
418int eth_initialize(void)
419{
420 int num_devices = 0;
421 struct udevice *dev;
422
423 eth_common_init();
424
425 /*
426 * Devices need to write the hwaddr even if not started so that Linux
427 * will have access to the hwaddr that u-boot stored for the device.
428 * This is accomplished by attempting to probe each device and calling
429 * their write_hwaddr() operation.
430 */
Mario Six865c3872018-04-27 14:52:56 +0200431 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700432 if (!dev) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200433 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700434 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
435 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600436 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-01-17 14:52:00 -0700437 struct udevice *prime_dev = NULL;
438
439 if (ethprime)
440 prime_dev = eth_get_dev_by_name(ethprime);
441 if (prime_dev) {
442 eth_set_dev(prime_dev);
443 eth_current_changed();
444 } else {
445 eth_set_dev(NULL);
446 }
447
448 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
449 do {
Simon Glass3e14a222020-12-16 21:20:16 -0700450 if (device_active(dev)) {
Michael Walle902c7962019-10-22 01:03:10 +0200451 if (num_devices)
452 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700453
Simon Glass75e534b2020-12-16 21:20:07 -0700454 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700455
Michael Walle902c7962019-10-22 01:03:10 +0200456 if (ethprime && dev == prime_dev)
457 printf(" [PRIME]");
458 }
Simon Glassc6011d22016-01-17 14:52:00 -0700459
460 eth_write_hwaddr(dev);
461
Simon Glass3e14a222020-12-16 21:20:16 -0700462 if (device_active(dev))
Michael Walle902c7962019-10-22 01:03:10 +0200463 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200464 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700465 } while (dev);
466
Michael Walle902c7962019-10-22 01:03:10 +0200467 if (!num_devices)
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200468 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700469 putc('\n');
470 }
471
472 return num_devices;
473}
474
475static int eth_post_bind(struct udevice *dev)
476{
Simon Glass4f840a22022-04-24 23:31:15 -0600477 int ret;
478
Simon Glassc6011d22016-01-17 14:52:00 -0700479 if (strchr(dev->name, ' ')) {
480 printf("\nError: eth device name \"%s\" has a space!\n",
481 dev->name);
482 return -EINVAL;
483 }
484
Ye Licd5bb772020-05-03 22:41:14 +0800485#ifdef CONFIG_DM_ETH_PHY
486 eth_phy_binds_nodes(dev);
487#endif
Simon Glass4f840a22022-04-24 23:31:15 -0600488 if (CONFIG_IS_ENABLED(BOOTDEV_ETH)) {
489 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
490 if (ret)
491 return log_msg_ret("bootdev", ret);
492 }
Ye Licd5bb772020-05-03 22:41:14 +0800493
Simon Glassc6011d22016-01-17 14:52:00 -0700494 return 0;
495}
496
497static int eth_pre_unbind(struct udevice *dev)
498{
499 /* Don't hang onto a pointer that is going away */
500 if (dev == eth_get_uclass_priv()->current)
501 eth_set_dev(NULL);
502
503 return 0;
504}
505
Thierry Reding8fe08532019-05-20 17:59:57 +0200506static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
507{
Simon Glass1eb72942021-01-13 20:29:47 -0700508#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding8fe08532019-05-20 17:59:57 +0200509 const uint8_t *p;
510
511 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
512 if (!p)
513 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
514
515 if (!p)
516 return false;
517
518 memcpy(mac, p, ARP_HLEN);
519
520 return true;
521#else
522 return false;
523#endif
524}
525
Simon Glassc6011d22016-01-17 14:52:00 -0700526static int eth_post_probe(struct udevice *dev)
527{
Simon Glass95588622020-12-22 19:30:28 -0700528 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
529 struct eth_pdata *pdata = dev_get_plat(dev);
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100530 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100531 char *source = "DT";
Simon Glassc6011d22016-01-17 14:52:00 -0700532
533#if defined(CONFIG_NEEDS_MANUAL_RELOC)
534 struct eth_ops *ops = eth_get_ops(dev);
535 static int reloc_done;
536
537 if (!reloc_done) {
538 if (ops->start)
539 ops->start += gd->reloc_off;
540 if (ops->send)
541 ops->send += gd->reloc_off;
542 if (ops->recv)
543 ops->recv += gd->reloc_off;
544 if (ops->free_pkt)
545 ops->free_pkt += gd->reloc_off;
546 if (ops->stop)
547 ops->stop += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700548 if (ops->mcast)
549 ops->mcast += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700550 if (ops->write_hwaddr)
551 ops->write_hwaddr += gd->reloc_off;
552 if (ops->read_rom_hwaddr)
553 ops->read_rom_hwaddr += gd->reloc_off;
554
555 reloc_done++;
556 }
557#endif
558
559 priv->state = ETH_STATE_INIT;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100560 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700561
Thierry Reding8fe08532019-05-20 17:59:57 +0200562 /* Check if the device has a valid MAC address in device tree */
563 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
564 !is_valid_ethaddr(pdata->enetaddr)) {
Michal Simek31411132020-03-16 11:36:17 +0100565 source = "ROM";
Thierry Reding8fe08532019-05-20 17:59:57 +0200566 /* Check if the device has a MAC address in ROM */
567 if (eth_get_ops(dev)->read_rom_hwaddr)
568 eth_get_ops(dev)->read_rom_hwaddr(dev);
569 }
Simon Glassc6011d22016-01-17 14:52:00 -0700570
Simon Glass75e534b2020-12-16 21:20:07 -0700571 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700572 if (!is_zero_ethaddr(env_enetaddr)) {
573 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100574 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700575 printf("\nWarning: %s MAC addresses don't match:\n",
576 dev->name);
Michal Simek31411132020-03-16 11:36:17 +0100577 printf("Address in %s is\t\t%pM\n",
578 source, pdata->enetaddr);
579 printf("Address in environment is\t%pM\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700580 env_enetaddr);
581 }
582
583 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100584 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700585 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700586 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
587 pdata->enetaddr);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100588 } else if (is_zero_ethaddr(pdata->enetaddr) ||
589 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700590#ifdef CONFIG_NET_RANDOM_ETHADDR
591 net_random_ethaddr(pdata->enetaddr);
592 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700593 dev->name, dev_seq(dev), pdata->enetaddr);
Michal Simek2b254022022-01-11 10:28:09 +0100594 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
595 pdata->enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700596#else
597 printf("\nError: %s address not set.\n",
598 dev->name);
599 return -EINVAL;
600#endif
601 }
602
Thierry Reding13064872019-05-20 17:59:56 +0200603 eth_write_hwaddr(dev);
604
Simon Glassc6011d22016-01-17 14:52:00 -0700605 return 0;
606}
607
608static int eth_pre_remove(struct udevice *dev)
609{
Simon Glass95588622020-12-22 19:30:28 -0700610 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700611
612 eth_get_ops(dev)->stop(dev);
613
614 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100615 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700616
617 return 0;
618}
619
Michael Walle7efcdfd2021-02-25 16:51:11 +0100620UCLASS_DRIVER(ethernet) = {
621 .name = "ethernet",
Simon Glassc6011d22016-01-17 14:52:00 -0700622 .id = UCLASS_ETH,
623 .post_bind = eth_post_bind,
624 .pre_unbind = eth_pre_unbind,
625 .post_probe = eth_post_probe,
626 .pre_remove = eth_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700627 .priv_auto = sizeof(struct eth_uclass_priv),
628 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassc6011d22016-01-17 14:52:00 -0700629 .flags = DM_UC_FLAG_SEQ_ALIAS,
630};