blob: 0f6b45b002c0be52ad15cd08c064d960ed475dd1 [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>
Sean Andersondfdda782022-05-05 13:11:41 -040017#include <nvmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glassc6011d22016-01-17 14:52:00 -070019#include <dm/device-internal.h>
20#include <dm/uclass-internal.h>
Ramon Friedac598c12019-07-18 21:43:30 +030021#include <net/pcap.h>
Simon Glassc6011d22016-01-17 14:52:00 -070022#include "eth_internal.h"
Ye Licd5bb772020-05-03 22:41:14 +080023#include <eth_phy.h>
Simon Glassc6011d22016-01-17 14:52:00 -070024
Simon Glass004b8912016-01-30 15:45:14 -070025DECLARE_GLOBAL_DATA_PTR;
26
Simon Glassc6011d22016-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 Schiffer9f5c1a52020-11-04 14:45:14 +010034 bool running;
Simon Glassc6011d22016-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 Fan46ec1042020-05-03 22:41:13 +080052 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -070053
Peng Fan46ec1042020-05-03 22:41:13 +080054 ret = uclass_get(UCLASS_ETH, &uc);
55 if (ret)
56 return NULL;
57
Simon Glassc6011d22016-01-17 14:52:00 -070058 assert(uc);
Simon Glass95588622020-12-22 19:30:28 -070059 return uclass_get_priv(uc);
Simon Glassc6011d22016-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 Walle384023e2021-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 Glassc6011d22016-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 Anderson35e82982020-09-12 17:45:43 -040087 if (!uc_priv)
88 return NULL;
89
Michael Walle384023e2021-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)
94 eth_errno = uclass_first_device(UCLASS_ETH,
95 &uc_priv->current);
96 }
Simon Glassc6011d22016-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 Fan46ec1042020-05-03 22:41:13 +0800128 int ret;
Simon Glassc6011d22016-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 Glassff9b9032021-07-24 09:03:30 -0600133 seq = dectoul(startp, &endp);
Simon Glassc6011d22016-01-17 14:52:00 -0700134 }
135
Peng Fan46ec1042020-05-03 22:41:13 +0800136 ret = uclass_get(UCLASS_ETH, &uc);
137 if (ret)
138 return NULL;
139
Simon Glassc6011d22016-01-17 14:52:00 -0700140 uclass_foreach_dev(it, uc) {
141 /*
Simon Glassc6011d22016-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 Glass75e534b2020-12-16 21:20:07 -0700150 (endp > startp && dev_seq(it) == seq))
Simon Glassc6011d22016-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 Glass95588622020-12-22 19:30:28 -0700162 pdata = dev_get_plat(eth_get_dev());
Simon Glassc6011d22016-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 Glass95588622020-12-22 19:30:28 -0700179 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-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 Glass95588622020-12-22 19:30:28 -0700195 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-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 Glass75e534b2020-12-16 21:20:07 -0700202 return dev_seq(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700203 return -1;
204}
205
206static int eth_write_hwaddr(struct udevice *dev)
207{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200208 struct eth_pdata *pdata;
Simon Glassc6011d22016-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 Glass75e534b2020-12-16 21:20:07 -0700215 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass95588622020-12-22 19:30:28 -0700216 pdata = dev_get_plat(dev);
Simon Glassc6011d22016-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 Glassff9b9032021-07-24 09:03:30 -0600246 index = dectoul(name + 3, NULL);
Simon Glassc6011d22016-01-17 14:52:00 -0700247
Simon Glass07e13382020-12-16 21:20:29 -0700248 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700249 if (!retval) {
Simon Glass95588622020-12-22 19:30:28 -0700250 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700251 switch (op) {
252 case env_op_create:
253 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500254 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200255 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700256 break;
257 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100258 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-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 Glass64b723f2017-08-03 12:22:12 -0600268 char *ethact = env_get("ethact");
269 char *ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-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 Schuchardt653f3ee2020-09-14 11:00:18 +0200289 log_err("No ethernet found.\n");
Simon Glassc6011d22016-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 Glass95588622020-12-22 19:30:28 -0700303 dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700304
305 priv->state = ETH_STATE_ACTIVE;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100306 priv->running = true;
Simon Glassc6011d22016-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 Schiffer9f5c1a52020-11-04 14:45:14 +0100336 if (!current)
Simon Glassc6011d22016-01-17 14:52:00 -0700337 return;
338
Simon Glass95588622020-12-22 19:30:28 -0700339 priv = dev_get_uclass_priv(current);
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100340 if (!priv || !priv->running)
341 return;
342
343 eth_get_ops(current)->stop(current);
344 priv->state = ETH_STATE_PASSIVE;
345 priv->running = false;
Simon Glassc6011d22016-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 Graf71511492018-03-15 15:07:09 +0100368 if (!eth_is_active(current))
Simon Glassc6011d22016-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 Friedac598c12019-07-18 21:43:30 +0300376#if defined(CONFIG_CMD_PCAP)
377 if (ret >= 0)
378 pcap_post(packet, length, true);
379#endif
Simon Glassc6011d22016-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 Graf71511492018-03-15 15:07:09 +0100395 if (!eth_is_active(current))
Simon Glassc6011d22016-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 Wildtd47430a2020-10-07 11:03:30 +0200400 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassc6011d22016-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 Six865c3872018-04-27 14:52:56 +0200432 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700433 if (!dev) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200434 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700435 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
436 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600437 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-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 Glass3e14a222020-12-16 21:20:16 -0700451 if (device_active(dev)) {
Michael Walle902c7962019-10-22 01:03:10 +0200452 if (num_devices)
453 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700454
Simon Glass75e534b2020-12-16 21:20:07 -0700455 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700456
Michael Walle902c7962019-10-22 01:03:10 +0200457 if (ethprime && dev == prime_dev)
458 printf(" [PRIME]");
459 }
Simon Glassc6011d22016-01-17 14:52:00 -0700460
461 eth_write_hwaddr(dev);
462
Simon Glass3e14a222020-12-16 21:20:16 -0700463 if (device_active(dev))
Michael Walle902c7962019-10-22 01:03:10 +0200464 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200465 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700466 } while (dev);
467
Michael Walle902c7962019-10-22 01:03:10 +0200468 if (!num_devices)
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200469 log_err("No ethernet found.\n");
Simon Glassc6011d22016-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 Glass4f840a22022-04-24 23:31:15 -0600478 int ret;
479
Simon Glassc6011d22016-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 Licd5bb772020-05-03 22:41:14 +0800486#ifdef CONFIG_DM_ETH_PHY
487 eth_phy_binds_nodes(dev);
488#endif
Simon Glass4f840a22022-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 Licd5bb772020-05-03 22:41:14 +0800494
Simon Glassc6011d22016-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 Reding8fe08532019-05-20 17:59:57 +0200507static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
508{
Simon Glass1eb72942021-01-13 20:29:47 -0700509#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding8fe08532019-05-20 17:59:57 +0200510 const uint8_t *p;
Sean Andersondfdda782022-05-05 13:11:41 -0400511 struct nvmem_cell mac_cell;
Thierry Reding8fe08532019-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 Andersondfdda782022-05-05 13:11:41 -0400517 if (p) {
518 memcpy(mac, p, ARP_HLEN);
519 return true;
520 }
Thierry Reding8fe08532019-05-20 17:59:57 +0200521
Sean Andersondfdda782022-05-05 13:11:41 -0400522 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
523 return false;
Thierry Reding8fe08532019-05-20 17:59:57 +0200524
Sean Andersondfdda782022-05-05 13:11:41 -0400525 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
Thierry Reding8fe08532019-05-20 17:59:57 +0200526#else
527 return false;
528#endif
529}
530
Simon Glassc6011d22016-01-17 14:52:00 -0700531static int eth_post_probe(struct udevice *dev)
532{
Simon Glass95588622020-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.nl496904b2016-11-25 16:30:19 +0100535 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100536 char *source = "DT";
Simon Glassc6011d22016-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 Glassc6011d22016-01-17 14:52:00 -0700553 if (ops->mcast)
554 ops->mcast += gd->reloc_off;
Simon Glassc6011d22016-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 Schiffer9f5c1a52020-11-04 14:45:14 +0100565 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700566
Thierry Reding8fe08532019-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 Simek31411132020-03-16 11:36:17 +0100570 source = "ROM";
Thierry Reding8fe08532019-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 Glassc6011d22016-01-17 14:52:00 -0700575
Simon Glass75e534b2020-12-16 21:20:07 -0700576 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700577 if (!is_zero_ethaddr(env_enetaddr)) {
578 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100579 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700580 printf("\nWarning: %s MAC addresses don't match:\n",
581 dev->name);
Michal Simek31411132020-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 Glassc6011d22016-01-17 14:52:00 -0700585 env_enetaddr);
586 }
587
588 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100589 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700590 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700591 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
592 pdata->enetaddr);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100593 } else if (is_zero_ethaddr(pdata->enetaddr) ||
594 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-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 Glass75e534b2020-12-16 21:20:07 -0700598 dev->name, dev_seq(dev), pdata->enetaddr);
Michal Simek2b254022022-01-11 10:28:09 +0100599 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
600 pdata->enetaddr);
Simon Glassc6011d22016-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 Reding13064872019-05-20 17:59:56 +0200608 eth_write_hwaddr(dev);
609
Simon Glassc6011d22016-01-17 14:52:00 -0700610 return 0;
611}
612
613static int eth_pre_remove(struct udevice *dev)
614{
Simon Glass95588622020-12-22 19:30:28 -0700615 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700616
617 eth_get_ops(dev)->stop(dev);
618
619 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100620 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700621
622 return 0;
623}
624
Michael Walle7efcdfd2021-02-25 16:51:11 +0100625UCLASS_DRIVER(ethernet) = {
626 .name = "ethernet",
Simon Glassc6011d22016-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 Glass8a2b47f2020-12-03 16:55:17 -0700632 .priv_auto = sizeof(struct eth_uclass_priv),
633 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassc6011d22016-01-17 14:52:00 -0700634 .flags = DM_UC_FLAG_SEQ_ALIAS,
635};