blob: 8c3f9cc31b760597134ec67f743d4577ca575cc1 [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>
Sean Anderson97d0f9b2022-05-05 13:11:41 -040017#include <nvmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070019#include <dm/device-internal.h>
20#include <dm/uclass-internal.h>
Ramon Fried3eaac632019-07-18 21:43:30 +030021#include <net/pcap.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070022#include "eth_internal.h"
Ye Li5fe419e2020-05-03 22:41:14 +080023#include <eth_phy.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070024
Simon Glassa7c45ec2016-01-30 15:45:14 -070025DECLARE_GLOBAL_DATA_PTR;
26
Simon Glassdb9391e2016-01-17 14:52:00 -070027/**
28 * struct eth_device_priv - private structure for each Ethernet device
29 *
30 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
31 */
32struct eth_device_priv {
33 enum eth_state_t state;
Matthias Schifferfa795f42020-11-04 14:45:14 +010034 bool running;
Simon Glassdb9391e2016-01-17 14:52:00 -070035};
36
37/**
38 * struct eth_uclass_priv - The structure attached to the uclass itself
39 *
40 * @current: The Ethernet device that the network functions are using
41 */
42struct eth_uclass_priv {
43 struct udevice *current;
44};
45
46/* eth_errno - This stores the most recent failure code from DM functions */
47static int eth_errno;
48
49static struct eth_uclass_priv *eth_get_uclass_priv(void)
50{
51 struct uclass *uc;
Peng Fand2b70202020-05-03 22:41:13 +080052 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -070053
Peng Fand2b70202020-05-03 22:41:13 +080054 ret = uclass_get(UCLASS_ETH, &uc);
55 if (ret)
56 return NULL;
57
Simon Glassdb9391e2016-01-17 14:52:00 -070058 assert(uc);
Simon Glass0fd3d912020-12-22 19:30:28 -070059 return uclass_get_priv(uc);
Simon Glassdb9391e2016-01-17 14:52:00 -070060}
61
62void eth_set_current_to_next(void)
63{
64 struct eth_uclass_priv *uc_priv;
65
66 uc_priv = eth_get_uclass_priv();
67 if (uc_priv->current)
68 uclass_next_device(&uc_priv->current);
69 if (!uc_priv->current)
70 uclass_first_device(UCLASS_ETH, &uc_priv->current);
71}
72
73/*
74 * Typically this will simply return the active device.
75 * In the case where the most recent active device was unset, this will attempt
Michael Walle6e424b42021-02-24 17:30:44 +010076 * to return the device with sequence id 0 (which can be configured by the
77 * device tree). If this fails, fall back to just getting the first device.
78 * The latter is non-deterministic and depends on the order of the probing.
79 * If that device doesn't exist or fails to probe, this function will return
80 * NULL.
Simon Glassdb9391e2016-01-17 14:52:00 -070081 */
82struct udevice *eth_get_dev(void)
83{
84 struct eth_uclass_priv *uc_priv;
85
86 uc_priv = eth_get_uclass_priv();
Sean Andersonc3f02782020-09-12 17:45:43 -040087 if (!uc_priv)
88 return NULL;
89
Michael Walle6e424b42021-02-24 17:30:44 +010090 if (!uc_priv->current) {
91 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
92 &uc_priv->current);
93 if (eth_errno)
Michal Suchanekc726fc02022-10-12 21:57:59 +020094 eth_errno = uclass_first_device_err(UCLASS_ETH,
95 &uc_priv->current);
Michael Walle6e424b42021-02-24 17:30:44 +010096 }
Simon Glassdb9391e2016-01-17 14:52:00 -070097 return uc_priv->current;
98}
99
100/*
101 * Typically this will just store a device pointer.
102 * In case it was not probed, we will attempt to do so.
103 * dev may be NULL to unset the active device.
104 */
105void eth_set_dev(struct udevice *dev)
106{
107 if (dev && !device_active(dev)) {
108 eth_errno = device_probe(dev);
109 if (eth_errno)
110 dev = NULL;
111 }
112
113 eth_get_uclass_priv()->current = dev;
114}
115
116/*
117 * Find the udevice that either has the name passed in as devname or has an
118 * alias named devname.
119 */
120struct udevice *eth_get_dev_by_name(const char *devname)
121{
122 int seq = -1;
123 char *endp = NULL;
124 const char *startp = NULL;
125 struct udevice *it;
126 struct uclass *uc;
127 int len = strlen("eth");
Peng Fand2b70202020-05-03 22:41:13 +0800128 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -0700129
130 /* Must be longer than 3 to be an alias */
131 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
132 startp = devname + len;
Simon Glass0b1284e2021-07-24 09:03:30 -0600133 seq = dectoul(startp, &endp);
Simon Glassdb9391e2016-01-17 14:52:00 -0700134 }
135
Peng Fand2b70202020-05-03 22:41:13 +0800136 ret = uclass_get(UCLASS_ETH, &uc);
137 if (ret)
138 return NULL;
139
Simon Glassdb9391e2016-01-17 14:52:00 -0700140 uclass_foreach_dev(it, uc) {
141 /*
Simon Glassdb9391e2016-01-17 14:52:00 -0700142 * We don't care about errors from probe here. Either they won't
143 * match an alias or it will match a literal name and we'll pick
144 * up the error when we try to probe again in eth_set_dev().
145 */
146 if (device_probe(it))
147 continue;
148 /* Check for the name or the sequence number to match */
149 if (strcmp(it->name, devname) == 0 ||
Simon Glass8b85dfc2020-12-16 21:20:07 -0700150 (endp > startp && dev_seq(it) == seq))
Simon Glassdb9391e2016-01-17 14:52:00 -0700151 return it;
152 }
153
154 return NULL;
155}
156
157unsigned char *eth_get_ethaddr(void)
158{
159 struct eth_pdata *pdata;
160
161 if (eth_get_dev()) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700162 pdata = dev_get_plat(eth_get_dev());
Simon Glassdb9391e2016-01-17 14:52:00 -0700163 return pdata->enetaddr;
164 }
165
166 return NULL;
167}
168
169/* Set active state without calling start on the driver */
170int eth_init_state_only(void)
171{
172 struct udevice *current;
173 struct eth_device_priv *priv;
174
175 current = eth_get_dev();
176 if (!current || !device_active(current))
177 return -EINVAL;
178
Simon Glass0fd3d912020-12-22 19:30:28 -0700179 priv = dev_get_uclass_priv(current);
Simon Glassdb9391e2016-01-17 14:52:00 -0700180 priv->state = ETH_STATE_ACTIVE;
181
182 return 0;
183}
184
185/* Set passive state without calling stop on the driver */
186void eth_halt_state_only(void)
187{
188 struct udevice *current;
189 struct eth_device_priv *priv;
190
191 current = eth_get_dev();
192 if (!current || !device_active(current))
193 return;
194
Simon Glass0fd3d912020-12-22 19:30:28 -0700195 priv = dev_get_uclass_priv(current);
Simon Glassdb9391e2016-01-17 14:52:00 -0700196 priv->state = ETH_STATE_PASSIVE;
197}
198
199int eth_get_dev_index(void)
200{
201 if (eth_get_dev())
Simon Glass8b85dfc2020-12-16 21:20:07 -0700202 return dev_seq(eth_get_dev());
Simon Glassdb9391e2016-01-17 14:52:00 -0700203 return -1;
204}
205
206static int eth_write_hwaddr(struct udevice *dev)
207{
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200208 struct eth_pdata *pdata;
Simon Glassdb9391e2016-01-17 14:52:00 -0700209 int ret = 0;
210
211 if (!dev || !device_active(dev))
212 return -EINVAL;
213
214 /* seq is valid since the device is active */
Simon Glass8b85dfc2020-12-16 21:20:07 -0700215 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700216 pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700217 if (!is_valid_ethaddr(pdata->enetaddr)) {
218 printf("\nError: %s address %pM illegal value\n",
219 dev->name, pdata->enetaddr);
220 return -EINVAL;
221 }
222
223 /*
224 * Drivers are allowed to decide not to implement this at
225 * run-time. E.g. Some devices may use it and some may not.
226 */
227 ret = eth_get_ops(dev)->write_hwaddr(dev);
228 if (ret == -ENOSYS)
229 ret = 0;
230 if (ret)
231 printf("\nWarning: %s failed to set MAC address\n",
232 dev->name);
233 }
234
235 return ret;
236}
237
238static int on_ethaddr(const char *name, const char *value, enum env_op op,
239 int flags)
240{
241 int index;
242 int retval;
243 struct udevice *dev;
244
245 /* look for an index after "eth" */
Simon Glass0b1284e2021-07-24 09:03:30 -0600246 index = dectoul(name + 3, NULL);
Simon Glassdb9391e2016-01-17 14:52:00 -0700247
Simon Glass99175912020-12-16 21:20:29 -0700248 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700249 if (!retval) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700250 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700251 switch (op) {
252 case env_op_create:
253 case env_op_overwrite:
Joe Hershbergerfb8977c2019-09-13 19:21:16 -0500254 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzerc86ff7f2016-09-02 14:48:17 +0200255 eth_write_hwaddr(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700256 break;
257 case env_op_delete:
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100258 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700259 }
260 }
261
262 return 0;
263}
264U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
265
266int eth_init(void)
267{
Simon Glass00caae62017-08-03 12:22:12 -0600268 char *ethact = env_get("ethact");
269 char *ethrotate = env_get("ethrotate");
Simon Glassdb9391e2016-01-17 14:52:00 -0700270 struct udevice *current = NULL;
271 struct udevice *old_current;
272 int ret = -ENODEV;
273
274 /*
275 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
276 * is already set to an ethernet device, we should stick to 'ethact'.
277 */
278 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
279 if (ethact) {
280 current = eth_get_dev_by_name(ethact);
281 if (!current)
282 return -EINVAL;
283 }
284 }
285
286 if (!current) {
287 current = eth_get_dev();
288 if (!current) {
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200289 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700290 return -ENODEV;
291 }
292 }
293
294 old_current = current;
295 do {
296 if (current) {
297 debug("Trying %s\n", current->name);
298
299 if (device_active(current)) {
300 ret = eth_get_ops(current)->start(current);
301 if (ret >= 0) {
302 struct eth_device_priv *priv =
Simon Glass0fd3d912020-12-22 19:30:28 -0700303 dev_get_uclass_priv(current);
Simon Glassdb9391e2016-01-17 14:52:00 -0700304
305 priv->state = ETH_STATE_ACTIVE;
Matthias Schifferfa795f42020-11-04 14:45:14 +0100306 priv->running = true;
Simon Glassdb9391e2016-01-17 14:52:00 -0700307 return 0;
308 }
309 } else {
310 ret = eth_errno;
311 }
312
313 debug("FAIL\n");
314 } else {
315 debug("PROBE FAIL\n");
316 }
317
318 /*
319 * If ethrotate is enabled, this will change "current",
320 * otherwise we will drop out of this while loop immediately
321 */
322 eth_try_another(0);
323 /* This will ensure the new "current" attempted to probe */
324 current = eth_get_dev();
325 } while (old_current != current);
326
327 return ret;
328}
329
330void eth_halt(void)
331{
332 struct udevice *current;
333 struct eth_device_priv *priv;
334
335 current = eth_get_dev();
Matthias Schifferfa795f42020-11-04 14:45:14 +0100336 if (!current)
337 return;
338
339 priv = dev_get_uclass_priv(current);
340 if (!priv || !priv->running)
Simon Glassdb9391e2016-01-17 14:52:00 -0700341 return;
342
343 eth_get_ops(current)->stop(current);
Matthias Schifferfa795f42020-11-04 14:45:14 +0100344 priv->state = ETH_STATE_PASSIVE;
345 priv->running = false;
Simon Glassdb9391e2016-01-17 14:52:00 -0700346}
347
348int eth_is_active(struct udevice *dev)
349{
350 struct eth_device_priv *priv;
351
352 if (!dev || !device_active(dev))
353 return 0;
354
355 priv = dev_get_uclass_priv(dev);
356 return priv->state == ETH_STATE_ACTIVE;
357}
358
359int eth_send(void *packet, int length)
360{
361 struct udevice *current;
362 int ret;
363
364 current = eth_get_dev();
365 if (!current)
366 return -ENODEV;
367
Alexander Grafa532e2f2018-03-15 15:07:09 +0100368 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700369 return -EINVAL;
370
371 ret = eth_get_ops(current)->send(current, packet, length);
372 if (ret < 0) {
373 /* We cannot completely return the error at present */
374 debug("%s: send() returned error %d\n", __func__, ret);
375 }
Ramon Fried3eaac632019-07-18 21:43:30 +0300376#if defined(CONFIG_CMD_PCAP)
377 if (ret >= 0)
378 pcap_post(packet, length, true);
379#endif
Simon Glassdb9391e2016-01-17 14:52:00 -0700380 return ret;
381}
382
383int eth_rx(void)
384{
385 struct udevice *current;
386 uchar *packet;
387 int flags;
388 int ret;
389 int i;
390
391 current = eth_get_dev();
392 if (!current)
393 return -ENODEV;
394
Alexander Grafa532e2f2018-03-15 15:07:09 +0100395 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700396 return -EINVAL;
397
398 /* Process up to 32 packets at one time */
399 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildt36ea0ca2020-10-07 11:03:30 +0200400 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700401 ret = eth_get_ops(current)->recv(current, flags, &packet);
402 flags = 0;
403 if (ret > 0)
404 net_process_received_packet(packet, ret);
405 if (ret >= 0 && eth_get_ops(current)->free_pkt)
406 eth_get_ops(current)->free_pkt(current, packet, ret);
407 if (ret <= 0)
408 break;
409 }
410 if (ret == -EAGAIN)
411 ret = 0;
412 if (ret < 0) {
413 /* We cannot completely return the error at present */
414 debug("%s: recv() returned error %d\n", __func__, ret);
415 }
416 return ret;
417}
418
419int eth_initialize(void)
420{
421 int num_devices = 0;
422 struct udevice *dev;
423
424 eth_common_init();
425
426 /*
427 * Devices need to write the hwaddr even if not started so that Linux
428 * will have access to the hwaddr that u-boot stored for the device.
429 * This is accomplished by attempting to probe each device and calling
430 * their write_hwaddr() operation.
431 */
Mario Six3ce43042018-04-27 14:52:56 +0200432 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700433 if (!dev) {
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200434 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700435 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
436 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600437 char *ethprime = env_get("ethprime");
Simon Glassdb9391e2016-01-17 14:52:00 -0700438 struct udevice *prime_dev = NULL;
439
440 if (ethprime)
441 prime_dev = eth_get_dev_by_name(ethprime);
442 if (prime_dev) {
443 eth_set_dev(prime_dev);
444 eth_current_changed();
445 } else {
446 eth_set_dev(NULL);
447 }
448
449 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
450 do {
Simon Glass552da332020-12-16 21:20:16 -0700451 if (device_active(dev)) {
Michael Walle19820db2019-10-22 01:03:10 +0200452 if (num_devices)
453 printf(", ");
Simon Glassdb9391e2016-01-17 14:52:00 -0700454
Simon Glass8b85dfc2020-12-16 21:20:07 -0700455 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassdb9391e2016-01-17 14:52:00 -0700456
Michael Walle19820db2019-10-22 01:03:10 +0200457 if (ethprime && dev == prime_dev)
458 printf(" [PRIME]");
459 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700460
461 eth_write_hwaddr(dev);
462
Simon Glass552da332020-12-16 21:20:16 -0700463 if (device_active(dev))
Michael Walle19820db2019-10-22 01:03:10 +0200464 num_devices++;
Mario Six3ce43042018-04-27 14:52:56 +0200465 uclass_next_device_check(&dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700466 } while (dev);
467
Michael Walle19820db2019-10-22 01:03:10 +0200468 if (!num_devices)
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200469 log_err("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700470 putc('\n');
471 }
472
473 return num_devices;
474}
475
476static int eth_post_bind(struct udevice *dev)
477{
Simon Glass4fd8d072022-04-24 23:31:15 -0600478 int ret;
479
Simon Glassdb9391e2016-01-17 14:52:00 -0700480 if (strchr(dev->name, ' ')) {
481 printf("\nError: eth device name \"%s\" has a space!\n",
482 dev->name);
483 return -EINVAL;
484 }
485
Ye Li5fe419e2020-05-03 22:41:14 +0800486#ifdef CONFIG_DM_ETH_PHY
487 eth_phy_binds_nodes(dev);
488#endif
Simon Glass4fd8d072022-04-24 23:31:15 -0600489 if (CONFIG_IS_ENABLED(BOOTDEV_ETH)) {
490 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
491 if (ret)
492 return log_msg_ret("bootdev", ret);
493 }
Ye Li5fe419e2020-05-03 22:41:14 +0800494
Simon Glassdb9391e2016-01-17 14:52:00 -0700495 return 0;
496}
497
498static int eth_pre_unbind(struct udevice *dev)
499{
500 /* Don't hang onto a pointer that is going away */
501 if (dev == eth_get_uclass_priv()->current)
502 eth_set_dev(NULL);
503
504 return 0;
505}
506
Thierry Reding379af672019-05-20 17:59:57 +0200507static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
508{
Simon Glass177e7f92021-01-13 20:29:47 -0700509#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding379af672019-05-20 17:59:57 +0200510 const uint8_t *p;
Sean Anderson97d0f9b2022-05-05 13:11:41 -0400511 struct nvmem_cell mac_cell;
Thierry Reding379af672019-05-20 17:59:57 +0200512
513 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
514 if (!p)
515 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
516
Sean Anderson97d0f9b2022-05-05 13:11:41 -0400517 if (p) {
518 memcpy(mac, p, ARP_HLEN);
519 return true;
520 }
521
522 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
Thierry Reding379af672019-05-20 17:59:57 +0200523 return false;
524
Sean Anderson97d0f9b2022-05-05 13:11:41 -0400525 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
Thierry Reding379af672019-05-20 17:59:57 +0200526#else
527 return false;
528#endif
529}
530
Simon Glassdb9391e2016-01-17 14:52:00 -0700531static int eth_post_probe(struct udevice *dev)
532{
Simon Glass0fd3d912020-12-22 19:30:28 -0700533 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
534 struct eth_pdata *pdata = dev_get_plat(dev);
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100535 unsigned char env_enetaddr[ARP_HLEN];
Michal Simekd4172c92020-03-16 11:36:17 +0100536 char *source = "DT";
Simon Glassdb9391e2016-01-17 14:52:00 -0700537
538#if defined(CONFIG_NEEDS_MANUAL_RELOC)
539 struct eth_ops *ops = eth_get_ops(dev);
540 static int reloc_done;
541
542 if (!reloc_done) {
543 if (ops->start)
544 ops->start += gd->reloc_off;
545 if (ops->send)
546 ops->send += gd->reloc_off;
547 if (ops->recv)
548 ops->recv += gd->reloc_off;
549 if (ops->free_pkt)
550 ops->free_pkt += gd->reloc_off;
551 if (ops->stop)
552 ops->stop += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700553 if (ops->mcast)
554 ops->mcast += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700555 if (ops->write_hwaddr)
556 ops->write_hwaddr += gd->reloc_off;
557 if (ops->read_rom_hwaddr)
558 ops->read_rom_hwaddr += gd->reloc_off;
559
560 reloc_done++;
561 }
562#endif
563
564 priv->state = ETH_STATE_INIT;
Matthias Schifferfa795f42020-11-04 14:45:14 +0100565 priv->running = false;
Simon Glassdb9391e2016-01-17 14:52:00 -0700566
Thierry Reding379af672019-05-20 17:59:57 +0200567 /* Check if the device has a valid MAC address in device tree */
568 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
569 !is_valid_ethaddr(pdata->enetaddr)) {
Michal Simekd4172c92020-03-16 11:36:17 +0100570 source = "ROM";
Thierry Reding379af672019-05-20 17:59:57 +0200571 /* Check if the device has a MAC address in ROM */
572 if (eth_get_ops(dev)->read_rom_hwaddr)
573 eth_get_ops(dev)->read_rom_hwaddr(dev);
574 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700575
Simon Glass8b85dfc2020-12-16 21:20:07 -0700576 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700577 if (!is_zero_ethaddr(env_enetaddr)) {
578 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100579 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700580 printf("\nWarning: %s MAC addresses don't match:\n",
581 dev->name);
Michal Simekd4172c92020-03-16 11:36:17 +0100582 printf("Address in %s is\t\t%pM\n",
583 source, pdata->enetaddr);
584 printf("Address in environment is\t%pM\n",
Simon Glassdb9391e2016-01-17 14:52:00 -0700585 env_enetaddr);
586 }
587
588 /* Override the ROM MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100589 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700590 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass8b85dfc2020-12-16 21:20:07 -0700591 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
592 pdata->enetaddr);
Siva Durga Prasad Paladuguaa555fe2016-11-02 12:52:13 +0100593 } else if (is_zero_ethaddr(pdata->enetaddr) ||
594 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700595#ifdef CONFIG_NET_RANDOM_ETHADDR
596 net_random_ethaddr(pdata->enetaddr);
597 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
Simon Glass8b85dfc2020-12-16 21:20:07 -0700598 dev->name, dev_seq(dev), pdata->enetaddr);
Michal Simek381e6e52022-01-11 10:28:09 +0100599 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
600 pdata->enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700601#else
602 printf("\nError: %s address not set.\n",
603 dev->name);
604 return -EINVAL;
605#endif
606 }
607
Thierry Redingb743bbd2019-05-20 17:59:56 +0200608 eth_write_hwaddr(dev);
609
Simon Glassdb9391e2016-01-17 14:52:00 -0700610 return 0;
611}
612
613static int eth_pre_remove(struct udevice *dev)
614{
Simon Glass0fd3d912020-12-22 19:30:28 -0700615 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700616
617 eth_get_ops(dev)->stop(dev);
618
619 /* clear the MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100620 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700621
622 return 0;
623}
624
Michael Walle82a3c9e2021-02-25 16:51:11 +0100625UCLASS_DRIVER(ethernet) = {
626 .name = "ethernet",
Simon Glassdb9391e2016-01-17 14:52:00 -0700627 .id = UCLASS_ETH,
628 .post_bind = eth_post_bind,
629 .pre_unbind = eth_pre_unbind,
630 .post_probe = eth_post_probe,
631 .pre_remove = eth_pre_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700632 .priv_auto = sizeof(struct eth_uclass_priv),
633 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassdb9391e2016-01-17 14:52:00 -0700634 .flags = DM_UC_FLAG_SEQ_ALIAS,
635};