blob: 5555f82f23e80227167fae828ad67dde9ec8bc96 [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 Glass4f840a22022-04-24 23:31:15 -060010#include <bootdev.h>
Simon Glass1ea97892020-05-10 11:40:00 -060011#include <bootstage.h>
Simon Glassc6011d22016-01-17 14:52:00 -070012#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glassc6011d22016-01-17 14:52:00 -070015#include <net.h>
Sean Andersondfdda782022-05-05 13:11:41 -040016#include <nvmem.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
Simon Glassb490f5f2023-01-17 10:47:28 -070040 * @no_bootdevs: true to skip binding Ethernet bootdevs (this is a negative flag
41 * so that the default value enables it)
Simon Glassc6011d22016-01-17 14:52:00 -070042 */
43struct eth_uclass_priv {
44 struct udevice *current;
Simon Glassb490f5f2023-01-17 10:47:28 -070045 bool no_bootdevs;
Simon Glassc6011d22016-01-17 14:52:00 -070046};
47
48/* eth_errno - This stores the most recent failure code from DM functions */
49static int eth_errno;
Matthias Schiffer2469b132024-04-26 10:02:24 +020050/* Are we currently in eth_init() or eth_halt()? */
51static bool in_init_halt;
Simon Glassc6011d22016-01-17 14:52:00 -070052
Marek Vasutb506e7d2023-03-06 15:53:42 +010053/* board-specific Ethernet Interface initializations. */
54__weak int board_interface_eth_init(struct udevice *dev,
55 phy_interface_t interface_type)
56{
57 return 0;
58}
59
Simon Glassc6011d22016-01-17 14:52:00 -070060static struct eth_uclass_priv *eth_get_uclass_priv(void)
61{
62 struct uclass *uc;
Peng Fan46ec1042020-05-03 22:41:13 +080063 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -070064
Peng Fan46ec1042020-05-03 22:41:13 +080065 ret = uclass_get(UCLASS_ETH, &uc);
66 if (ret)
67 return NULL;
68
Simon Glassc6011d22016-01-17 14:52:00 -070069 assert(uc);
Simon Glass95588622020-12-22 19:30:28 -070070 return uclass_get_priv(uc);
Simon Glassc6011d22016-01-17 14:52:00 -070071}
72
Simon Glassb490f5f2023-01-17 10:47:28 -070073void eth_set_enable_bootdevs(bool enable)
74{
75 struct eth_uclass_priv *priv = eth_get_uclass_priv();
76
77 if (priv)
78 priv->no_bootdevs = !enable;
79}
80
Simon Glassc6011d22016-01-17 14:52:00 -070081void eth_set_current_to_next(void)
82{
83 struct eth_uclass_priv *uc_priv;
84
85 uc_priv = eth_get_uclass_priv();
86 if (uc_priv->current)
87 uclass_next_device(&uc_priv->current);
88 if (!uc_priv->current)
89 uclass_first_device(UCLASS_ETH, &uc_priv->current);
90}
91
92/*
93 * Typically this will simply return the active device.
94 * In the case where the most recent active device was unset, this will attempt
Michael Walle384023e2021-02-24 17:30:44 +010095 * to return the device with sequence id 0 (which can be configured by the
96 * device tree). If this fails, fall back to just getting the first device.
97 * The latter is non-deterministic and depends on the order of the probing.
98 * If that device doesn't exist or fails to probe, this function will return
99 * NULL.
Simon Glassc6011d22016-01-17 14:52:00 -0700100 */
101struct udevice *eth_get_dev(void)
102{
103 struct eth_uclass_priv *uc_priv;
104
105 uc_priv = eth_get_uclass_priv();
Sean Anderson35e82982020-09-12 17:45:43 -0400106 if (!uc_priv)
107 return NULL;
108
Michael Walle384023e2021-02-24 17:30:44 +0100109 if (!uc_priv->current) {
110 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
111 &uc_priv->current);
112 if (eth_errno)
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200113 eth_errno = uclass_first_device_err(UCLASS_ETH,
114 &uc_priv->current);
Michal Suchanek050aa362022-10-12 21:58:02 +0200115 if (eth_errno)
116 uc_priv->current = NULL;
Michael Walle384023e2021-02-24 17:30:44 +0100117 }
Simon Glassc6011d22016-01-17 14:52:00 -0700118 return uc_priv->current;
119}
120
121/*
122 * Typically this will just store a device pointer.
123 * In case it was not probed, we will attempt to do so.
124 * dev may be NULL to unset the active device.
125 */
126void eth_set_dev(struct udevice *dev)
127{
128 if (dev && !device_active(dev)) {
129 eth_errno = device_probe(dev);
130 if (eth_errno)
131 dev = NULL;
132 }
133
134 eth_get_uclass_priv()->current = dev;
135}
136
137/*
138 * Find the udevice that either has the name passed in as devname or has an
139 * alias named devname.
140 */
141struct udevice *eth_get_dev_by_name(const char *devname)
142{
143 int seq = -1;
144 char *endp = NULL;
145 const char *startp = NULL;
146 struct udevice *it;
147 struct uclass *uc;
148 int len = strlen("eth");
Peng Fan46ec1042020-05-03 22:41:13 +0800149 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -0700150
151 /* Must be longer than 3 to be an alias */
152 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
153 startp = devname + len;
Simon Glassff9b9032021-07-24 09:03:30 -0600154 seq = dectoul(startp, &endp);
Simon Glassc6011d22016-01-17 14:52:00 -0700155 }
156
Peng Fan46ec1042020-05-03 22:41:13 +0800157 ret = uclass_get(UCLASS_ETH, &uc);
158 if (ret)
159 return NULL;
160
Simon Glassc6011d22016-01-17 14:52:00 -0700161 uclass_foreach_dev(it, uc) {
162 /*
Simon Glassc6011d22016-01-17 14:52:00 -0700163 * We don't care about errors from probe here. Either they won't
164 * match an alias or it will match a literal name and we'll pick
165 * up the error when we try to probe again in eth_set_dev().
166 */
167 if (device_probe(it))
168 continue;
169 /* Check for the name or the sequence number to match */
170 if (strcmp(it->name, devname) == 0 ||
Simon Glass75e534b2020-12-16 21:20:07 -0700171 (endp > startp && dev_seq(it) == seq))
Simon Glassc6011d22016-01-17 14:52:00 -0700172 return it;
173 }
174
175 return NULL;
176}
177
178unsigned char *eth_get_ethaddr(void)
179{
180 struct eth_pdata *pdata;
181
182 if (eth_get_dev()) {
Simon Glass95588622020-12-22 19:30:28 -0700183 pdata = dev_get_plat(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700184 return pdata->enetaddr;
185 }
186
187 return NULL;
188}
189
190/* Set active state without calling start on the driver */
191int eth_init_state_only(void)
192{
193 struct udevice *current;
194 struct eth_device_priv *priv;
195
196 current = eth_get_dev();
197 if (!current || !device_active(current))
198 return -EINVAL;
199
Simon Glass95588622020-12-22 19:30:28 -0700200 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700201 priv->state = ETH_STATE_ACTIVE;
202
203 return 0;
204}
205
206/* Set passive state without calling stop on the driver */
207void eth_halt_state_only(void)
208{
209 struct udevice *current;
210 struct eth_device_priv *priv;
211
212 current = eth_get_dev();
213 if (!current || !device_active(current))
214 return;
215
Simon Glass95588622020-12-22 19:30:28 -0700216 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700217 priv->state = ETH_STATE_PASSIVE;
218}
219
220int eth_get_dev_index(void)
221{
222 if (eth_get_dev())
Simon Glass75e534b2020-12-16 21:20:07 -0700223 return dev_seq(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700224 return -1;
225}
226
227static int eth_write_hwaddr(struct udevice *dev)
228{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200229 struct eth_pdata *pdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700230 int ret = 0;
231
232 if (!dev || !device_active(dev))
233 return -EINVAL;
234
235 /* seq is valid since the device is active */
Simon Glass75e534b2020-12-16 21:20:07 -0700236 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass95588622020-12-22 19:30:28 -0700237 pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700238 if (!is_valid_ethaddr(pdata->enetaddr)) {
239 printf("\nError: %s address %pM illegal value\n",
240 dev->name, pdata->enetaddr);
241 return -EINVAL;
242 }
243
244 /*
245 * Drivers are allowed to decide not to implement this at
246 * run-time. E.g. Some devices may use it and some may not.
247 */
248 ret = eth_get_ops(dev)->write_hwaddr(dev);
249 if (ret == -ENOSYS)
250 ret = 0;
251 if (ret)
252 printf("\nWarning: %s failed to set MAC address\n",
253 dev->name);
254 }
255
256 return ret;
257}
258
259static int on_ethaddr(const char *name, const char *value, enum env_op op,
260 int flags)
261{
262 int index;
263 int retval;
264 struct udevice *dev;
265
266 /* look for an index after "eth" */
Simon Glassff9b9032021-07-24 09:03:30 -0600267 index = dectoul(name + 3, NULL);
Simon Glassc6011d22016-01-17 14:52:00 -0700268
Simon Glass07e13382020-12-16 21:20:29 -0700269 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700270 if (!retval) {
Simon Glass95588622020-12-22 19:30:28 -0700271 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700272 switch (op) {
273 case env_op_create:
274 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500275 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200276 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700277 break;
278 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100279 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700280 }
281 }
282
283 return 0;
284}
285U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
286
Jerome Forissierc8d070c2024-10-16 12:04:01 +0200287int eth_start_udev(struct udevice *dev)
288{
289 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
290 int ret;
291
292 if (priv->running)
293 return 0;
294
295 if (!device_active(dev))
296 return -EINVAL;
297
298 ret = eth_get_ops(dev)->start(dev);
299 if (ret < 0)
300 return ret;
301
302 priv->state = ETH_STATE_ACTIVE;
303 priv->running = true;
304
305 return 0;
306}
307
Simon Glassc6011d22016-01-17 14:52:00 -0700308int eth_init(void)
309{
Simon Glassc6011d22016-01-17 14:52:00 -0700310 struct udevice *current = NULL;
311 struct udevice *old_current;
312 int ret = -ENODEV;
Matthias Schiffer2469b132024-04-26 10:02:24 +0200313 char *ethrotate;
314 char *ethact;
315
316 if (in_init_halt)
317 return -EBUSY;
318
319 in_init_halt = true;
320
321 ethact = env_get("ethact");
322 ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-01-17 14:52:00 -0700323
324 /*
325 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
326 * is already set to an ethernet device, we should stick to 'ethact'.
327 */
328 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
329 if (ethact) {
330 current = eth_get_dev_by_name(ethact);
Matthias Schiffer2469b132024-04-26 10:02:24 +0200331 if (!current) {
332 ret = -EINVAL;
333 goto end;
334 }
Simon Glassc6011d22016-01-17 14:52:00 -0700335 }
336 }
337
338 if (!current) {
339 current = eth_get_dev();
340 if (!current) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200341 log_err("No ethernet found.\n");
Matthias Schiffer2469b132024-04-26 10:02:24 +0200342 ret = -ENODEV;
343 goto end;
Simon Glassc6011d22016-01-17 14:52:00 -0700344 }
345 }
346
347 old_current = current;
348 do {
349 if (current) {
350 debug("Trying %s\n", current->name);
351
Jerome Forissierc8d070c2024-10-16 12:04:01 +0200352 ret = eth_start_udev(current);
353 if (ret < 0)
Simon Glassc6011d22016-01-17 14:52:00 -0700354 ret = eth_errno;
Jerome Forissierc8d070c2024-10-16 12:04:01 +0200355 else
356 break;
Simon Glassc6011d22016-01-17 14:52:00 -0700357
358 debug("FAIL\n");
359 } else {
360 debug("PROBE FAIL\n");
361 }
362
363 /*
364 * If ethrotate is enabled, this will change "current",
365 * otherwise we will drop out of this while loop immediately
366 */
367 eth_try_another(0);
368 /* This will ensure the new "current" attempted to probe */
369 current = eth_get_dev();
370 } while (old_current != current);
371
Matthias Schiffer2469b132024-04-26 10:02:24 +0200372end:
373 in_init_halt = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700374 return ret;
375}
376
377void eth_halt(void)
378{
379 struct udevice *current;
380 struct eth_device_priv *priv;
381
Matthias Schiffer2469b132024-04-26 10:02:24 +0200382 if (in_init_halt)
383 return;
384
385 in_init_halt = true;
386
Simon Glassc6011d22016-01-17 14:52:00 -0700387 current = eth_get_dev();
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100388 if (!current)
Matthias Schiffer2469b132024-04-26 10:02:24 +0200389 goto end;
Simon Glassc6011d22016-01-17 14:52:00 -0700390
Simon Glass95588622020-12-22 19:30:28 -0700391 priv = dev_get_uclass_priv(current);
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100392 if (!priv || !priv->running)
Matthias Schiffer2469b132024-04-26 10:02:24 +0200393 goto end;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100394
395 eth_get_ops(current)->stop(current);
396 priv->state = ETH_STATE_PASSIVE;
397 priv->running = false;
Matthias Schiffer2469b132024-04-26 10:02:24 +0200398
399end:
400 in_init_halt = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700401}
402
403int eth_is_active(struct udevice *dev)
404{
405 struct eth_device_priv *priv;
406
407 if (!dev || !device_active(dev))
408 return 0;
409
410 priv = dev_get_uclass_priv(dev);
411 return priv->state == ETH_STATE_ACTIVE;
412}
413
414int eth_send(void *packet, int length)
415{
416 struct udevice *current;
417 int ret;
418
419 current = eth_get_dev();
420 if (!current)
421 return -ENODEV;
422
Alexander Graf71511492018-03-15 15:07:09 +0100423 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700424 return -EINVAL;
425
426 ret = eth_get_ops(current)->send(current, packet, length);
427 if (ret < 0) {
428 /* We cannot completely return the error at present */
429 debug("%s: send() returned error %d\n", __func__, ret);
430 }
Ramon Friedac598c12019-07-18 21:43:30 +0300431#if defined(CONFIG_CMD_PCAP)
432 if (ret >= 0)
433 pcap_post(packet, length, true);
434#endif
Simon Glassc6011d22016-01-17 14:52:00 -0700435 return ret;
436}
437
438int eth_rx(void)
439{
440 struct udevice *current;
441 uchar *packet;
442 int flags;
443 int ret;
444 int i;
445
446 current = eth_get_dev();
447 if (!current)
448 return -ENODEV;
449
Alexander Graf71511492018-03-15 15:07:09 +0100450 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700451 return -EINVAL;
452
453 /* Process up to 32 packets at one time */
454 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildtd47430a2020-10-07 11:03:30 +0200455 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassc6011d22016-01-17 14:52:00 -0700456 ret = eth_get_ops(current)->recv(current, flags, &packet);
457 flags = 0;
458 if (ret > 0)
459 net_process_received_packet(packet, ret);
460 if (ret >= 0 && eth_get_ops(current)->free_pkt)
461 eth_get_ops(current)->free_pkt(current, packet, ret);
462 if (ret <= 0)
463 break;
464 }
465 if (ret == -EAGAIN)
466 ret = 0;
467 if (ret < 0) {
468 /* We cannot completely return the error at present */
469 debug("%s: recv() returned error %d\n", __func__, ret);
470 }
471 return ret;
472}
473
474int eth_initialize(void)
475{
476 int num_devices = 0;
477 struct udevice *dev;
478
479 eth_common_init();
480
481 /*
482 * Devices need to write the hwaddr even if not started so that Linux
483 * will have access to the hwaddr that u-boot stored for the device.
484 * This is accomplished by attempting to probe each device and calling
485 * their write_hwaddr() operation.
486 */
Mario Six865c3872018-04-27 14:52:56 +0200487 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700488 if (!dev) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200489 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700490 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
491 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600492 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-01-17 14:52:00 -0700493 struct udevice *prime_dev = NULL;
494
495 if (ethprime)
496 prime_dev = eth_get_dev_by_name(ethprime);
497 if (prime_dev) {
498 eth_set_dev(prime_dev);
499 eth_current_changed();
500 } else {
501 eth_set_dev(NULL);
502 }
503
504 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
505 do {
Simon Glass3e14a222020-12-16 21:20:16 -0700506 if (device_active(dev)) {
Michael Walle902c7962019-10-22 01:03:10 +0200507 if (num_devices)
508 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700509
Simon Glass75e534b2020-12-16 21:20:07 -0700510 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700511
Michael Walle902c7962019-10-22 01:03:10 +0200512 if (ethprime && dev == prime_dev)
513 printf(" [PRIME]");
514 }
Simon Glassc6011d22016-01-17 14:52:00 -0700515
516 eth_write_hwaddr(dev);
517
Simon Glass3e14a222020-12-16 21:20:16 -0700518 if (device_active(dev))
Michael Walle902c7962019-10-22 01:03:10 +0200519 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200520 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700521 } while (dev);
522
Michael Walle902c7962019-10-22 01:03:10 +0200523 if (!num_devices)
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200524 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700525 putc('\n');
526 }
527
528 return num_devices;
529}
530
531static int eth_post_bind(struct udevice *dev)
532{
Simon Glassb490f5f2023-01-17 10:47:28 -0700533 struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
Simon Glass4f840a22022-04-24 23:31:15 -0600534 int ret;
535
Simon Glassc6011d22016-01-17 14:52:00 -0700536 if (strchr(dev->name, ' ')) {
537 printf("\nError: eth device name \"%s\" has a space!\n",
538 dev->name);
539 return -EINVAL;
540 }
541
Ye Licd5bb772020-05-03 22:41:14 +0800542#ifdef CONFIG_DM_ETH_PHY
543 eth_phy_binds_nodes(dev);
544#endif
Simon Glassb490f5f2023-01-17 10:47:28 -0700545 if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
Simon Glass4f840a22022-04-24 23:31:15 -0600546 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
547 if (ret)
548 return log_msg_ret("bootdev", ret);
549 }
Ye Licd5bb772020-05-03 22:41:14 +0800550
Simon Glassc6011d22016-01-17 14:52:00 -0700551 return 0;
552}
553
554static int eth_pre_unbind(struct udevice *dev)
555{
556 /* Don't hang onto a pointer that is going away */
557 if (dev == eth_get_uclass_priv()->current)
558 eth_set_dev(NULL);
559
560 return 0;
561}
562
Thierry Reding8fe08532019-05-20 17:59:57 +0200563static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
564{
Simon Glass1eb72942021-01-13 20:29:47 -0700565#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding8fe08532019-05-20 17:59:57 +0200566 const uint8_t *p;
Sean Andersondfdda782022-05-05 13:11:41 -0400567 struct nvmem_cell mac_cell;
Thierry Reding8fe08532019-05-20 17:59:57 +0200568
569 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
570 if (!p)
571 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
572
Sean Andersondfdda782022-05-05 13:11:41 -0400573 if (p) {
574 memcpy(mac, p, ARP_HLEN);
575 return true;
576 }
Thierry Reding8fe08532019-05-20 17:59:57 +0200577
Sean Andersondfdda782022-05-05 13:11:41 -0400578 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
579 return false;
Thierry Reding8fe08532019-05-20 17:59:57 +0200580
Sean Andersondfdda782022-05-05 13:11:41 -0400581 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
Thierry Reding8fe08532019-05-20 17:59:57 +0200582#else
583 return false;
584#endif
585}
586
Simon Glassc6011d22016-01-17 14:52:00 -0700587static int eth_post_probe(struct udevice *dev)
588{
Simon Glass95588622020-12-22 19:30:28 -0700589 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
590 struct eth_pdata *pdata = dev_get_plat(dev);
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100591 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100592 char *source = "DT";
Simon Glassc6011d22016-01-17 14:52:00 -0700593
Simon Glassc6011d22016-01-17 14:52:00 -0700594 priv->state = ETH_STATE_INIT;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100595 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700596
Thierry Reding8fe08532019-05-20 17:59:57 +0200597 /* Check if the device has a valid MAC address in device tree */
598 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
599 !is_valid_ethaddr(pdata->enetaddr)) {
600 /* Check if the device has a MAC address in ROM */
Michal Simek58e609b2023-09-15 16:10:06 +0200601 if (eth_get_ops(dev)->read_rom_hwaddr) {
602 int ret;
603
604 ret = eth_get_ops(dev)->read_rom_hwaddr(dev);
605 if (!ret)
606 source = "ROM";
607 }
Thierry Reding8fe08532019-05-20 17:59:57 +0200608 }
Simon Glassc6011d22016-01-17 14:52:00 -0700609
Simon Glass75e534b2020-12-16 21:20:07 -0700610 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700611 if (!is_zero_ethaddr(env_enetaddr)) {
612 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100613 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700614 printf("\nWarning: %s MAC addresses don't match:\n",
615 dev->name);
Michal Simek31411132020-03-16 11:36:17 +0100616 printf("Address in %s is\t\t%pM\n",
617 source, pdata->enetaddr);
618 printf("Address in environment is\t%pM\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700619 env_enetaddr);
620 }
621
622 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100623 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700624 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700625 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
626 pdata->enetaddr);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100627 } else if (is_zero_ethaddr(pdata->enetaddr) ||
628 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700629#ifdef CONFIG_NET_RANDOM_ETHADDR
630 net_random_ethaddr(pdata->enetaddr);
631 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700632 dev->name, dev_seq(dev), pdata->enetaddr);
Michal Simek2b254022022-01-11 10:28:09 +0100633 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
634 pdata->enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700635#else
Fabio Estevam3cc84442023-10-20 09:41:51 -0300636 printf("\nError: %s No valid MAC address found.\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700637 dev->name);
638 return -EINVAL;
639#endif
640 }
641
Thierry Reding13064872019-05-20 17:59:56 +0200642 eth_write_hwaddr(dev);
643
Simon Glassc6011d22016-01-17 14:52:00 -0700644 return 0;
645}
646
647static int eth_pre_remove(struct udevice *dev)
648{
Simon Glass95588622020-12-22 19:30:28 -0700649 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700650
651 eth_get_ops(dev)->stop(dev);
652
653 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100654 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700655
656 return 0;
657}
658
Michael Walle7efcdfd2021-02-25 16:51:11 +0100659UCLASS_DRIVER(ethernet) = {
660 .name = "ethernet",
Simon Glassc6011d22016-01-17 14:52:00 -0700661 .id = UCLASS_ETH,
662 .post_bind = eth_post_bind,
663 .pre_unbind = eth_pre_unbind,
664 .post_probe = eth_post_probe,
665 .pre_remove = eth_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700666 .priv_auto = sizeof(struct eth_uclass_priv),
667 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassc6011d22016-01-17 14:52:00 -0700668 .flags = DM_UC_FLAG_SEQ_ALIAS,
669};