blob: bcefc54ded891df8e3adb3e808059c3f484a5fd2 [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
Patrick Delaunay79d191e2021-07-20 20:15:30 +02008#define LOG_CATEGORY UCLASS_ETH
9
Simon Glassdb9391e2016-01-17 14:52:00 -070010#include <common.h>
Simon Glass4fd8d072022-04-24 23:31:15 -060011#include <bootdev.h>
Simon Glass52f24232020-05-10 11:40:00 -060012#include <bootstage.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070013#include <dm.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060014#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070016#include <net.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070018#include <dm/device-internal.h>
19#include <dm/uclass-internal.h>
Ramon Fried3eaac632019-07-18 21:43:30 +030020#include <net/pcap.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070021#include "eth_internal.h"
Ye Li5fe419e2020-05-03 22:41:14 +080022#include <eth_phy.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070023
Simon Glassa7c45ec2016-01-30 15:45:14 -070024DECLARE_GLOBAL_DATA_PTR;
25
Simon Glassdb9391e2016-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 Schifferfa795f42020-11-04 14:45:14 +010033 bool running;
Simon Glassdb9391e2016-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 Fand2b70202020-05-03 22:41:13 +080051 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -070052
Peng Fand2b70202020-05-03 22:41:13 +080053 ret = uclass_get(UCLASS_ETH, &uc);
54 if (ret)
55 return NULL;
56
Simon Glassdb9391e2016-01-17 14:52:00 -070057 assert(uc);
Simon Glass0fd3d912020-12-22 19:30:28 -070058 return uclass_get_priv(uc);
Simon Glassdb9391e2016-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 Walle6e424b42021-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 Glassdb9391e2016-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 Andersonc3f02782020-09-12 17:45:43 -040086 if (!uc_priv)
87 return NULL;
88
Michael Walle6e424b42021-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 Glassdb9391e2016-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 Fand2b70202020-05-03 22:41:13 +0800127 int ret;
Simon Glassdb9391e2016-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 Glass0b1284e2021-07-24 09:03:30 -0600132 seq = dectoul(startp, &endp);
Simon Glassdb9391e2016-01-17 14:52:00 -0700133 }
134
Peng Fand2b70202020-05-03 22:41:13 +0800135 ret = uclass_get(UCLASS_ETH, &uc);
136 if (ret)
137 return NULL;
138
Simon Glassdb9391e2016-01-17 14:52:00 -0700139 uclass_foreach_dev(it, uc) {
140 /*
Simon Glassdb9391e2016-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 Glass8b85dfc2020-12-16 21:20:07 -0700149 (endp > startp && dev_seq(it) == seq))
Simon Glassdb9391e2016-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 Glass0fd3d912020-12-22 19:30:28 -0700161 pdata = dev_get_plat(eth_get_dev());
Simon Glassdb9391e2016-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 Glass0fd3d912020-12-22 19:30:28 -0700178 priv = dev_get_uclass_priv(current);
Simon Glassdb9391e2016-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 Glass0fd3d912020-12-22 19:30:28 -0700194 priv = dev_get_uclass_priv(current);
Simon Glassdb9391e2016-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 Glass8b85dfc2020-12-16 21:20:07 -0700201 return dev_seq(eth_get_dev());
Simon Glassdb9391e2016-01-17 14:52:00 -0700202 return -1;
203}
204
205static int eth_write_hwaddr(struct udevice *dev)
206{
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200207 struct eth_pdata *pdata;
Simon Glassdb9391e2016-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 Glass8b85dfc2020-12-16 21:20:07 -0700214 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700215 pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-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 Glass0b1284e2021-07-24 09:03:30 -0600245 index = dectoul(name + 3, NULL);
Simon Glassdb9391e2016-01-17 14:52:00 -0700246
Simon Glass99175912020-12-16 21:20:29 -0700247 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700248 if (!retval) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700249 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700250 switch (op) {
251 case env_op_create:
252 case env_op_overwrite:
Joe Hershbergerfb8977c2019-09-13 19:21:16 -0500253 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzerc86ff7f2016-09-02 14:48:17 +0200254 eth_write_hwaddr(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700255 break;
256 case env_op_delete:
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100257 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-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 Glass00caae62017-08-03 12:22:12 -0600267 char *ethact = env_get("ethact");
268 char *ethrotate = env_get("ethrotate");
Simon Glassdb9391e2016-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 Schuchardtad895912020-09-14 11:00:18 +0200288 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-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 Glass0fd3d912020-12-22 19:30:28 -0700302 dev_get_uclass_priv(current);
Simon Glassdb9391e2016-01-17 14:52:00 -0700303
304 priv->state = ETH_STATE_ACTIVE;
Matthias Schifferfa795f42020-11-04 14:45:14 +0100305 priv->running = true;
Simon Glassdb9391e2016-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 Schifferfa795f42020-11-04 14:45:14 +0100335 if (!current)
336 return;
337
338 priv = dev_get_uclass_priv(current);
339 if (!priv || !priv->running)
Simon Glassdb9391e2016-01-17 14:52:00 -0700340 return;
341
342 eth_get_ops(current)->stop(current);
Matthias Schifferfa795f42020-11-04 14:45:14 +0100343 priv->state = ETH_STATE_PASSIVE;
344 priv->running = false;
Simon Glassdb9391e2016-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 Grafa532e2f2018-03-15 15:07:09 +0100367 if (!eth_is_active(current))
Simon Glassdb9391e2016-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 Fried3eaac632019-07-18 21:43:30 +0300375#if defined(CONFIG_CMD_PCAP)
376 if (ret >= 0)
377 pcap_post(packet, length, true);
378#endif
Simon Glassdb9391e2016-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 Grafa532e2f2018-03-15 15:07:09 +0100394 if (!eth_is_active(current))
Simon Glassdb9391e2016-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 Wildt36ea0ca2020-10-07 11:03:30 +0200399 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassdb9391e2016-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 Six3ce43042018-04-27 14:52:56 +0200431 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700432 if (!dev) {
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200433 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700434 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
435 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600436 char *ethprime = env_get("ethprime");
Simon Glassdb9391e2016-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 Glass552da332020-12-16 21:20:16 -0700450 if (device_active(dev)) {
Michael Walle19820db2019-10-22 01:03:10 +0200451 if (num_devices)
452 printf(", ");
Simon Glassdb9391e2016-01-17 14:52:00 -0700453
Simon Glass8b85dfc2020-12-16 21:20:07 -0700454 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassdb9391e2016-01-17 14:52:00 -0700455
Michael Walle19820db2019-10-22 01:03:10 +0200456 if (ethprime && dev == prime_dev)
457 printf(" [PRIME]");
458 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700459
460 eth_write_hwaddr(dev);
461
Simon Glass552da332020-12-16 21:20:16 -0700462 if (device_active(dev))
Michael Walle19820db2019-10-22 01:03:10 +0200463 num_devices++;
Mario Six3ce43042018-04-27 14:52:56 +0200464 uclass_next_device_check(&dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700465 } while (dev);
466
Michael Walle19820db2019-10-22 01:03:10 +0200467 if (!num_devices)
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200468 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-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 Glass4fd8d072022-04-24 23:31:15 -0600477 int ret;
478
Simon Glassdb9391e2016-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 Li5fe419e2020-05-03 22:41:14 +0800485#ifdef CONFIG_DM_ETH_PHY
486 eth_phy_binds_nodes(dev);
487#endif
Simon Glass4fd8d072022-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 Li5fe419e2020-05-03 22:41:14 +0800493
Simon Glassdb9391e2016-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 Reding379af672019-05-20 17:59:57 +0200506static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
507{
Simon Glass177e7f92021-01-13 20:29:47 -0700508#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding379af672019-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 Glassdb9391e2016-01-17 14:52:00 -0700526static int eth_post_probe(struct udevice *dev)
527{
Simon Glass0fd3d912020-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.nla40db6d2016-11-25 16:30:19 +0100530 unsigned char env_enetaddr[ARP_HLEN];
Michal Simekd4172c92020-03-16 11:36:17 +0100531 char *source = "DT";
Simon Glassdb9391e2016-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 Glassdb9391e2016-01-17 14:52:00 -0700548 if (ops->mcast)
549 ops->mcast += gd->reloc_off;
Simon Glassdb9391e2016-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 Schifferfa795f42020-11-04 14:45:14 +0100560 priv->running = false;
Simon Glassdb9391e2016-01-17 14:52:00 -0700561
Thierry Reding379af672019-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 Simekd4172c92020-03-16 11:36:17 +0100565 source = "ROM";
Thierry Reding379af672019-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 Glassdb9391e2016-01-17 14:52:00 -0700570
Simon Glass8b85dfc2020-12-16 21:20:07 -0700571 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700572 if (!is_zero_ethaddr(env_enetaddr)) {
573 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100574 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700575 printf("\nWarning: %s MAC addresses don't match:\n",
576 dev->name);
Michal Simekd4172c92020-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 Glassdb9391e2016-01-17 14:52:00 -0700580 env_enetaddr);
581 }
582
583 /* Override the ROM MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100584 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700585 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass8b85dfc2020-12-16 21:20:07 -0700586 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
587 pdata->enetaddr);
Siva Durga Prasad Paladuguaa555fe2016-11-02 12:52:13 +0100588 } else if (is_zero_ethaddr(pdata->enetaddr) ||
589 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassdb9391e2016-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 Glass8b85dfc2020-12-16 21:20:07 -0700593 dev->name, dev_seq(dev), pdata->enetaddr);
Michal Simek381e6e52022-01-11 10:28:09 +0100594 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
595 pdata->enetaddr);
Simon Glassdb9391e2016-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 Redingb743bbd2019-05-20 17:59:56 +0200603 eth_write_hwaddr(dev);
604
Simon Glassdb9391e2016-01-17 14:52:00 -0700605 return 0;
606}
607
608static int eth_pre_remove(struct udevice *dev)
609{
Simon Glass0fd3d912020-12-22 19:30:28 -0700610 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700611
612 eth_get_ops(dev)->stop(dev);
613
614 /* clear the MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100615 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700616
617 return 0;
618}
619
Michael Walle82a3c9e2021-02-25 16:51:11 +0100620UCLASS_DRIVER(ethernet) = {
621 .name = "ethernet",
Simon Glassdb9391e2016-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 Glass41575d82020-12-03 16:55:17 -0700627 .priv_auto = sizeof(struct eth_uclass_priv),
628 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassdb9391e2016-01-17 14:52:00 -0700629 .flags = DM_UC_FLAG_SEQ_ALIAS,
630};