blob: 193218a328b752007726ae487e71759c10d9010b [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
Tom Riniabb9a042024-05-18 20:20:43 -060010#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
Simon Glassb490f5f2023-01-17 10:47:28 -070041 * @no_bootdevs: true to skip binding Ethernet bootdevs (this is a negative flag
42 * so that the default value enables it)
Simon Glassc6011d22016-01-17 14:52:00 -070043 */
44struct eth_uclass_priv {
45 struct udevice *current;
Simon Glassb490f5f2023-01-17 10:47:28 -070046 bool no_bootdevs;
Simon Glassc6011d22016-01-17 14:52:00 -070047};
48
49/* eth_errno - This stores the most recent failure code from DM functions */
50static int eth_errno;
Matthias Schiffer2469b132024-04-26 10:02:24 +020051/* Are we currently in eth_init() or eth_halt()? */
52static bool in_init_halt;
Simon Glassc6011d22016-01-17 14:52:00 -070053
Marek Vasutb506e7d2023-03-06 15:53:42 +010054/* board-specific Ethernet Interface initializations. */
55__weak int board_interface_eth_init(struct udevice *dev,
56 phy_interface_t interface_type)
57{
58 return 0;
59}
60
Simon Glassc6011d22016-01-17 14:52:00 -070061static struct eth_uclass_priv *eth_get_uclass_priv(void)
62{
63 struct uclass *uc;
Peng Fan46ec1042020-05-03 22:41:13 +080064 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -070065
Peng Fan46ec1042020-05-03 22:41:13 +080066 ret = uclass_get(UCLASS_ETH, &uc);
67 if (ret)
68 return NULL;
69
Simon Glassc6011d22016-01-17 14:52:00 -070070 assert(uc);
Simon Glass95588622020-12-22 19:30:28 -070071 return uclass_get_priv(uc);
Simon Glassc6011d22016-01-17 14:52:00 -070072}
73
Simon Glassb490f5f2023-01-17 10:47:28 -070074void eth_set_enable_bootdevs(bool enable)
75{
76 struct eth_uclass_priv *priv = eth_get_uclass_priv();
77
78 if (priv)
79 priv->no_bootdevs = !enable;
80}
81
Simon Glassc6011d22016-01-17 14:52:00 -070082void eth_set_current_to_next(void)
83{
84 struct eth_uclass_priv *uc_priv;
85
86 uc_priv = eth_get_uclass_priv();
87 if (uc_priv->current)
88 uclass_next_device(&uc_priv->current);
89 if (!uc_priv->current)
90 uclass_first_device(UCLASS_ETH, &uc_priv->current);
91}
92
93/*
94 * Typically this will simply return the active device.
95 * In the case where the most recent active device was unset, this will attempt
Michael Walle384023e2021-02-24 17:30:44 +010096 * to return the device with sequence id 0 (which can be configured by the
97 * device tree). If this fails, fall back to just getting the first device.
98 * The latter is non-deterministic and depends on the order of the probing.
99 * If that device doesn't exist or fails to probe, this function will return
100 * NULL.
Simon Glassc6011d22016-01-17 14:52:00 -0700101 */
102struct udevice *eth_get_dev(void)
103{
104 struct eth_uclass_priv *uc_priv;
105
106 uc_priv = eth_get_uclass_priv();
Sean Anderson35e82982020-09-12 17:45:43 -0400107 if (!uc_priv)
108 return NULL;
109
Michael Walle384023e2021-02-24 17:30:44 +0100110 if (!uc_priv->current) {
111 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
112 &uc_priv->current);
113 if (eth_errno)
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200114 eth_errno = uclass_first_device_err(UCLASS_ETH,
115 &uc_priv->current);
Michal Suchanek050aa362022-10-12 21:58:02 +0200116 if (eth_errno)
117 uc_priv->current = NULL;
Michael Walle384023e2021-02-24 17:30:44 +0100118 }
Simon Glassc6011d22016-01-17 14:52:00 -0700119 return uc_priv->current;
120}
121
122/*
123 * Typically this will just store a device pointer.
124 * In case it was not probed, we will attempt to do so.
125 * dev may be NULL to unset the active device.
126 */
127void eth_set_dev(struct udevice *dev)
128{
129 if (dev && !device_active(dev)) {
130 eth_errno = device_probe(dev);
131 if (eth_errno)
132 dev = NULL;
133 }
134
135 eth_get_uclass_priv()->current = dev;
136}
137
138/*
139 * Find the udevice that either has the name passed in as devname or has an
140 * alias named devname.
141 */
142struct udevice *eth_get_dev_by_name(const char *devname)
143{
144 int seq = -1;
145 char *endp = NULL;
146 const char *startp = NULL;
147 struct udevice *it;
148 struct uclass *uc;
149 int len = strlen("eth");
Peng Fan46ec1042020-05-03 22:41:13 +0800150 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -0700151
152 /* Must be longer than 3 to be an alias */
153 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
154 startp = devname + len;
Simon Glassff9b9032021-07-24 09:03:30 -0600155 seq = dectoul(startp, &endp);
Simon Glassc6011d22016-01-17 14:52:00 -0700156 }
157
Peng Fan46ec1042020-05-03 22:41:13 +0800158 ret = uclass_get(UCLASS_ETH, &uc);
159 if (ret)
160 return NULL;
161
Simon Glassc6011d22016-01-17 14:52:00 -0700162 uclass_foreach_dev(it, uc) {
163 /*
Simon Glassc6011d22016-01-17 14:52:00 -0700164 * We don't care about errors from probe here. Either they won't
165 * match an alias or it will match a literal name and we'll pick
166 * up the error when we try to probe again in eth_set_dev().
167 */
168 if (device_probe(it))
169 continue;
170 /* Check for the name or the sequence number to match */
171 if (strcmp(it->name, devname) == 0 ||
Simon Glass75e534b2020-12-16 21:20:07 -0700172 (endp > startp && dev_seq(it) == seq))
Simon Glassc6011d22016-01-17 14:52:00 -0700173 return it;
174 }
175
176 return NULL;
177}
178
179unsigned char *eth_get_ethaddr(void)
180{
181 struct eth_pdata *pdata;
182
183 if (eth_get_dev()) {
Simon Glass95588622020-12-22 19:30:28 -0700184 pdata = dev_get_plat(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700185 return pdata->enetaddr;
186 }
187
188 return NULL;
189}
190
191/* Set active state without calling start on the driver */
192int eth_init_state_only(void)
193{
194 struct udevice *current;
195 struct eth_device_priv *priv;
196
197 current = eth_get_dev();
198 if (!current || !device_active(current))
199 return -EINVAL;
200
Simon Glass95588622020-12-22 19:30:28 -0700201 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700202 priv->state = ETH_STATE_ACTIVE;
203
204 return 0;
205}
206
207/* Set passive state without calling stop on the driver */
208void eth_halt_state_only(void)
209{
210 struct udevice *current;
211 struct eth_device_priv *priv;
212
213 current = eth_get_dev();
214 if (!current || !device_active(current))
215 return;
216
Simon Glass95588622020-12-22 19:30:28 -0700217 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700218 priv->state = ETH_STATE_PASSIVE;
219}
220
221int eth_get_dev_index(void)
222{
223 if (eth_get_dev())
Simon Glass75e534b2020-12-16 21:20:07 -0700224 return dev_seq(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700225 return -1;
226}
227
228static int eth_write_hwaddr(struct udevice *dev)
229{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200230 struct eth_pdata *pdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700231 int ret = 0;
232
233 if (!dev || !device_active(dev))
234 return -EINVAL;
235
236 /* seq is valid since the device is active */
Simon Glass75e534b2020-12-16 21:20:07 -0700237 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass95588622020-12-22 19:30:28 -0700238 pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700239 if (!is_valid_ethaddr(pdata->enetaddr)) {
240 printf("\nError: %s address %pM illegal value\n",
241 dev->name, pdata->enetaddr);
242 return -EINVAL;
243 }
244
245 /*
246 * Drivers are allowed to decide not to implement this at
247 * run-time. E.g. Some devices may use it and some may not.
248 */
249 ret = eth_get_ops(dev)->write_hwaddr(dev);
250 if (ret == -ENOSYS)
251 ret = 0;
252 if (ret)
253 printf("\nWarning: %s failed to set MAC address\n",
254 dev->name);
255 }
256
257 return ret;
258}
259
260static int on_ethaddr(const char *name, const char *value, enum env_op op,
261 int flags)
262{
263 int index;
264 int retval;
265 struct udevice *dev;
266
267 /* look for an index after "eth" */
Simon Glassff9b9032021-07-24 09:03:30 -0600268 index = dectoul(name + 3, NULL);
Simon Glassc6011d22016-01-17 14:52:00 -0700269
Simon Glass07e13382020-12-16 21:20:29 -0700270 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700271 if (!retval) {
Simon Glass95588622020-12-22 19:30:28 -0700272 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700273 switch (op) {
274 case env_op_create:
275 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500276 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200277 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700278 break;
279 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100280 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700281 }
282 }
283
284 return 0;
285}
286U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
287
288int eth_init(void)
289{
Simon Glassc6011d22016-01-17 14:52:00 -0700290 struct udevice *current = NULL;
291 struct udevice *old_current;
292 int ret = -ENODEV;
Matthias Schiffer2469b132024-04-26 10:02:24 +0200293 char *ethrotate;
294 char *ethact;
295
296 if (in_init_halt)
297 return -EBUSY;
298
299 in_init_halt = true;
300
301 ethact = env_get("ethact");
302 ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-01-17 14:52:00 -0700303
304 /*
305 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
306 * is already set to an ethernet device, we should stick to 'ethact'.
307 */
308 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
309 if (ethact) {
310 current = eth_get_dev_by_name(ethact);
Matthias Schiffer2469b132024-04-26 10:02:24 +0200311 if (!current) {
312 ret = -EINVAL;
313 goto end;
314 }
Simon Glassc6011d22016-01-17 14:52:00 -0700315 }
316 }
317
318 if (!current) {
319 current = eth_get_dev();
320 if (!current) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200321 log_err("No ethernet found.\n");
Matthias Schiffer2469b132024-04-26 10:02:24 +0200322 ret = -ENODEV;
323 goto end;
Simon Glassc6011d22016-01-17 14:52:00 -0700324 }
325 }
326
327 old_current = current;
328 do {
329 if (current) {
330 debug("Trying %s\n", current->name);
331
332 if (device_active(current)) {
333 ret = eth_get_ops(current)->start(current);
334 if (ret >= 0) {
335 struct eth_device_priv *priv =
Simon Glass95588622020-12-22 19:30:28 -0700336 dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700337
338 priv->state = ETH_STATE_ACTIVE;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100339 priv->running = true;
Matthias Schiffer2469b132024-04-26 10:02:24 +0200340 ret = 0;
341 goto end;
Simon Glassc6011d22016-01-17 14:52:00 -0700342 }
343 } else {
344 ret = eth_errno;
345 }
346
347 debug("FAIL\n");
348 } else {
349 debug("PROBE FAIL\n");
350 }
351
352 /*
353 * If ethrotate is enabled, this will change "current",
354 * otherwise we will drop out of this while loop immediately
355 */
356 eth_try_another(0);
357 /* This will ensure the new "current" attempted to probe */
358 current = eth_get_dev();
359 } while (old_current != current);
360
Matthias Schiffer2469b132024-04-26 10:02:24 +0200361end:
362 in_init_halt = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700363 return ret;
364}
365
366void eth_halt(void)
367{
368 struct udevice *current;
369 struct eth_device_priv *priv;
370
Matthias Schiffer2469b132024-04-26 10:02:24 +0200371 if (in_init_halt)
372 return;
373
374 in_init_halt = true;
375
Simon Glassc6011d22016-01-17 14:52:00 -0700376 current = eth_get_dev();
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100377 if (!current)
Matthias Schiffer2469b132024-04-26 10:02:24 +0200378 goto end;
Simon Glassc6011d22016-01-17 14:52:00 -0700379
Simon Glass95588622020-12-22 19:30:28 -0700380 priv = dev_get_uclass_priv(current);
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100381 if (!priv || !priv->running)
Matthias Schiffer2469b132024-04-26 10:02:24 +0200382 goto end;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100383
384 eth_get_ops(current)->stop(current);
385 priv->state = ETH_STATE_PASSIVE;
386 priv->running = false;
Matthias Schiffer2469b132024-04-26 10:02:24 +0200387
388end:
389 in_init_halt = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700390}
391
392int eth_is_active(struct udevice *dev)
393{
394 struct eth_device_priv *priv;
395
396 if (!dev || !device_active(dev))
397 return 0;
398
399 priv = dev_get_uclass_priv(dev);
400 return priv->state == ETH_STATE_ACTIVE;
401}
402
403int eth_send(void *packet, int length)
404{
405 struct udevice *current;
406 int ret;
407
408 current = eth_get_dev();
409 if (!current)
410 return -ENODEV;
411
Alexander Graf71511492018-03-15 15:07:09 +0100412 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700413 return -EINVAL;
414
415 ret = eth_get_ops(current)->send(current, packet, length);
416 if (ret < 0) {
417 /* We cannot completely return the error at present */
418 debug("%s: send() returned error %d\n", __func__, ret);
419 }
Ramon Friedac598c12019-07-18 21:43:30 +0300420#if defined(CONFIG_CMD_PCAP)
421 if (ret >= 0)
422 pcap_post(packet, length, true);
423#endif
Simon Glassc6011d22016-01-17 14:52:00 -0700424 return ret;
425}
426
427int eth_rx(void)
428{
429 struct udevice *current;
430 uchar *packet;
431 int flags;
432 int ret;
433 int i;
434
435 current = eth_get_dev();
436 if (!current)
437 return -ENODEV;
438
Alexander Graf71511492018-03-15 15:07:09 +0100439 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700440 return -EINVAL;
441
442 /* Process up to 32 packets at one time */
443 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildtd47430a2020-10-07 11:03:30 +0200444 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassc6011d22016-01-17 14:52:00 -0700445 ret = eth_get_ops(current)->recv(current, flags, &packet);
446 flags = 0;
447 if (ret > 0)
448 net_process_received_packet(packet, ret);
449 if (ret >= 0 && eth_get_ops(current)->free_pkt)
450 eth_get_ops(current)->free_pkt(current, packet, ret);
451 if (ret <= 0)
452 break;
453 }
454 if (ret == -EAGAIN)
455 ret = 0;
456 if (ret < 0) {
457 /* We cannot completely return the error at present */
458 debug("%s: recv() returned error %d\n", __func__, ret);
459 }
460 return ret;
461}
462
463int eth_initialize(void)
464{
465 int num_devices = 0;
466 struct udevice *dev;
467
468 eth_common_init();
469
470 /*
471 * Devices need to write the hwaddr even if not started so that Linux
472 * will have access to the hwaddr that u-boot stored for the device.
473 * This is accomplished by attempting to probe each device and calling
474 * their write_hwaddr() operation.
475 */
Mario Six865c3872018-04-27 14:52:56 +0200476 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700477 if (!dev) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200478 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700479 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
480 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600481 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-01-17 14:52:00 -0700482 struct udevice *prime_dev = NULL;
483
484 if (ethprime)
485 prime_dev = eth_get_dev_by_name(ethprime);
486 if (prime_dev) {
487 eth_set_dev(prime_dev);
488 eth_current_changed();
489 } else {
490 eth_set_dev(NULL);
491 }
492
493 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
494 do {
Simon Glass3e14a222020-12-16 21:20:16 -0700495 if (device_active(dev)) {
Michael Walle902c7962019-10-22 01:03:10 +0200496 if (num_devices)
497 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700498
Simon Glass75e534b2020-12-16 21:20:07 -0700499 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700500
Michael Walle902c7962019-10-22 01:03:10 +0200501 if (ethprime && dev == prime_dev)
502 printf(" [PRIME]");
503 }
Simon Glassc6011d22016-01-17 14:52:00 -0700504
505 eth_write_hwaddr(dev);
506
Simon Glass3e14a222020-12-16 21:20:16 -0700507 if (device_active(dev))
Michael Walle902c7962019-10-22 01:03:10 +0200508 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200509 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700510 } while (dev);
511
Michael Walle902c7962019-10-22 01:03:10 +0200512 if (!num_devices)
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200513 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700514 putc('\n');
515 }
516
517 return num_devices;
518}
519
520static int eth_post_bind(struct udevice *dev)
521{
Simon Glassb490f5f2023-01-17 10:47:28 -0700522 struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
Simon Glass4f840a22022-04-24 23:31:15 -0600523 int ret;
524
Simon Glassc6011d22016-01-17 14:52:00 -0700525 if (strchr(dev->name, ' ')) {
526 printf("\nError: eth device name \"%s\" has a space!\n",
527 dev->name);
528 return -EINVAL;
529 }
530
Ye Licd5bb772020-05-03 22:41:14 +0800531#ifdef CONFIG_DM_ETH_PHY
532 eth_phy_binds_nodes(dev);
533#endif
Simon Glassb490f5f2023-01-17 10:47:28 -0700534 if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
Simon Glass4f840a22022-04-24 23:31:15 -0600535 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
536 if (ret)
537 return log_msg_ret("bootdev", ret);
538 }
Ye Licd5bb772020-05-03 22:41:14 +0800539
Simon Glassc6011d22016-01-17 14:52:00 -0700540 return 0;
541}
542
543static int eth_pre_unbind(struct udevice *dev)
544{
545 /* Don't hang onto a pointer that is going away */
546 if (dev == eth_get_uclass_priv()->current)
547 eth_set_dev(NULL);
548
549 return 0;
550}
551
Thierry Reding8fe08532019-05-20 17:59:57 +0200552static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
553{
Simon Glass1eb72942021-01-13 20:29:47 -0700554#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding8fe08532019-05-20 17:59:57 +0200555 const uint8_t *p;
Sean Andersondfdda782022-05-05 13:11:41 -0400556 struct nvmem_cell mac_cell;
Thierry Reding8fe08532019-05-20 17:59:57 +0200557
558 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
559 if (!p)
560 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
561
Sean Andersondfdda782022-05-05 13:11:41 -0400562 if (p) {
563 memcpy(mac, p, ARP_HLEN);
564 return true;
565 }
Thierry Reding8fe08532019-05-20 17:59:57 +0200566
Sean Andersondfdda782022-05-05 13:11:41 -0400567 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
568 return false;
Thierry Reding8fe08532019-05-20 17:59:57 +0200569
Sean Andersondfdda782022-05-05 13:11:41 -0400570 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
Thierry Reding8fe08532019-05-20 17:59:57 +0200571#else
572 return false;
573#endif
574}
575
Simon Glassc6011d22016-01-17 14:52:00 -0700576static int eth_post_probe(struct udevice *dev)
577{
Simon Glass95588622020-12-22 19:30:28 -0700578 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
579 struct eth_pdata *pdata = dev_get_plat(dev);
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100580 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100581 char *source = "DT";
Simon Glassc6011d22016-01-17 14:52:00 -0700582
Simon Glassc6011d22016-01-17 14:52:00 -0700583 priv->state = ETH_STATE_INIT;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100584 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700585
Thierry Reding8fe08532019-05-20 17:59:57 +0200586 /* Check if the device has a valid MAC address in device tree */
587 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
588 !is_valid_ethaddr(pdata->enetaddr)) {
589 /* Check if the device has a MAC address in ROM */
Michal Simek58e609b2023-09-15 16:10:06 +0200590 if (eth_get_ops(dev)->read_rom_hwaddr) {
591 int ret;
592
593 ret = eth_get_ops(dev)->read_rom_hwaddr(dev);
594 if (!ret)
595 source = "ROM";
596 }
Thierry Reding8fe08532019-05-20 17:59:57 +0200597 }
Simon Glassc6011d22016-01-17 14:52:00 -0700598
Simon Glass75e534b2020-12-16 21:20:07 -0700599 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700600 if (!is_zero_ethaddr(env_enetaddr)) {
601 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100602 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700603 printf("\nWarning: %s MAC addresses don't match:\n",
604 dev->name);
Michal Simek31411132020-03-16 11:36:17 +0100605 printf("Address in %s is\t\t%pM\n",
606 source, pdata->enetaddr);
607 printf("Address in environment is\t%pM\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700608 env_enetaddr);
609 }
610
611 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100612 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700613 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700614 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
615 pdata->enetaddr);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100616 } else if (is_zero_ethaddr(pdata->enetaddr) ||
617 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700618#ifdef CONFIG_NET_RANDOM_ETHADDR
619 net_random_ethaddr(pdata->enetaddr);
620 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700621 dev->name, dev_seq(dev), pdata->enetaddr);
Michal Simek2b254022022-01-11 10:28:09 +0100622 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
623 pdata->enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700624#else
Fabio Estevam3cc84442023-10-20 09:41:51 -0300625 printf("\nError: %s No valid MAC address found.\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700626 dev->name);
627 return -EINVAL;
628#endif
629 }
630
Thierry Reding13064872019-05-20 17:59:56 +0200631 eth_write_hwaddr(dev);
632
Simon Glassc6011d22016-01-17 14:52:00 -0700633 return 0;
634}
635
636static int eth_pre_remove(struct udevice *dev)
637{
Simon Glass95588622020-12-22 19:30:28 -0700638 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700639
640 eth_get_ops(dev)->stop(dev);
641
642 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100643 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700644
645 return 0;
646}
647
Michael Walle7efcdfd2021-02-25 16:51:11 +0100648UCLASS_DRIVER(ethernet) = {
649 .name = "ethernet",
Simon Glassc6011d22016-01-17 14:52:00 -0700650 .id = UCLASS_ETH,
651 .post_bind = eth_post_bind,
652 .pre_unbind = eth_pre_unbind,
653 .post_probe = eth_post_probe,
654 .pre_remove = eth_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700655 .priv_auto = sizeof(struct eth_uclass_priv),
656 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassc6011d22016-01-17 14:52:00 -0700657 .flags = DM_UC_FLAG_SEQ_ALIAS,
658};