blob: 4e3933fd05f35b4fbccbb26a6ec83b4502038f99 [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;
50
Marek Vasutb506e7d2023-03-06 15:53:42 +010051/* board-specific Ethernet Interface initializations. */
52__weak int board_interface_eth_init(struct udevice *dev,
53 phy_interface_t interface_type)
54{
55 return 0;
56}
57
Simon Glassc6011d22016-01-17 14:52:00 -070058static struct eth_uclass_priv *eth_get_uclass_priv(void)
59{
60 struct uclass *uc;
Peng Fan46ec1042020-05-03 22:41:13 +080061 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -070062
Peng Fan46ec1042020-05-03 22:41:13 +080063 ret = uclass_get(UCLASS_ETH, &uc);
64 if (ret)
65 return NULL;
66
Simon Glassc6011d22016-01-17 14:52:00 -070067 assert(uc);
Simon Glass95588622020-12-22 19:30:28 -070068 return uclass_get_priv(uc);
Simon Glassc6011d22016-01-17 14:52:00 -070069}
70
Simon Glassb490f5f2023-01-17 10:47:28 -070071void eth_set_enable_bootdevs(bool enable)
72{
73 struct eth_uclass_priv *priv = eth_get_uclass_priv();
74
75 if (priv)
76 priv->no_bootdevs = !enable;
77}
78
Simon Glassc6011d22016-01-17 14:52:00 -070079void eth_set_current_to_next(void)
80{
81 struct eth_uclass_priv *uc_priv;
82
83 uc_priv = eth_get_uclass_priv();
84 if (uc_priv->current)
85 uclass_next_device(&uc_priv->current);
86 if (!uc_priv->current)
87 uclass_first_device(UCLASS_ETH, &uc_priv->current);
88}
89
90/*
91 * Typically this will simply return the active device.
92 * In the case where the most recent active device was unset, this will attempt
Michael Walle384023e2021-02-24 17:30:44 +010093 * to return the device with sequence id 0 (which can be configured by the
94 * device tree). If this fails, fall back to just getting the first device.
95 * The latter is non-deterministic and depends on the order of the probing.
96 * If that device doesn't exist or fails to probe, this function will return
97 * NULL.
Simon Glassc6011d22016-01-17 14:52:00 -070098 */
99struct udevice *eth_get_dev(void)
100{
101 struct eth_uclass_priv *uc_priv;
102
103 uc_priv = eth_get_uclass_priv();
Sean Anderson35e82982020-09-12 17:45:43 -0400104 if (!uc_priv)
105 return NULL;
106
Michael Walle384023e2021-02-24 17:30:44 +0100107 if (!uc_priv->current) {
108 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
109 &uc_priv->current);
110 if (eth_errno)
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200111 eth_errno = uclass_first_device_err(UCLASS_ETH,
112 &uc_priv->current);
Michal Suchanek050aa362022-10-12 21:58:02 +0200113 if (eth_errno)
114 uc_priv->current = NULL;
Michael Walle384023e2021-02-24 17:30:44 +0100115 }
Simon Glassc6011d22016-01-17 14:52:00 -0700116 return uc_priv->current;
117}
118
119/*
120 * Typically this will just store a device pointer.
121 * In case it was not probed, we will attempt to do so.
122 * dev may be NULL to unset the active device.
123 */
124void eth_set_dev(struct udevice *dev)
125{
126 if (dev && !device_active(dev)) {
127 eth_errno = device_probe(dev);
128 if (eth_errno)
129 dev = NULL;
130 }
131
132 eth_get_uclass_priv()->current = dev;
133}
134
135/*
136 * Find the udevice that either has the name passed in as devname or has an
137 * alias named devname.
138 */
139struct udevice *eth_get_dev_by_name(const char *devname)
140{
141 int seq = -1;
142 char *endp = NULL;
143 const char *startp = NULL;
144 struct udevice *it;
145 struct uclass *uc;
146 int len = strlen("eth");
Peng Fan46ec1042020-05-03 22:41:13 +0800147 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -0700148
149 /* Must be longer than 3 to be an alias */
150 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
151 startp = devname + len;
Simon Glassff9b9032021-07-24 09:03:30 -0600152 seq = dectoul(startp, &endp);
Simon Glassc6011d22016-01-17 14:52:00 -0700153 }
154
Peng Fan46ec1042020-05-03 22:41:13 +0800155 ret = uclass_get(UCLASS_ETH, &uc);
156 if (ret)
157 return NULL;
158
Simon Glassc6011d22016-01-17 14:52:00 -0700159 uclass_foreach_dev(it, uc) {
160 /*
Simon Glassc6011d22016-01-17 14:52:00 -0700161 * We don't care about errors from probe here. Either they won't
162 * match an alias or it will match a literal name and we'll pick
163 * up the error when we try to probe again in eth_set_dev().
164 */
165 if (device_probe(it))
166 continue;
167 /* Check for the name or the sequence number to match */
168 if (strcmp(it->name, devname) == 0 ||
Simon Glass75e534b2020-12-16 21:20:07 -0700169 (endp > startp && dev_seq(it) == seq))
Simon Glassc6011d22016-01-17 14:52:00 -0700170 return it;
171 }
172
173 return NULL;
174}
175
176unsigned char *eth_get_ethaddr(void)
177{
178 struct eth_pdata *pdata;
179
180 if (eth_get_dev()) {
Simon Glass95588622020-12-22 19:30:28 -0700181 pdata = dev_get_plat(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700182 return pdata->enetaddr;
183 }
184
185 return NULL;
186}
187
188/* Set active state without calling start on the driver */
189int eth_init_state_only(void)
190{
191 struct udevice *current;
192 struct eth_device_priv *priv;
193
194 current = eth_get_dev();
195 if (!current || !device_active(current))
196 return -EINVAL;
197
Simon Glass95588622020-12-22 19:30:28 -0700198 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700199 priv->state = ETH_STATE_ACTIVE;
200
201 return 0;
202}
203
204/* Set passive state without calling stop on the driver */
205void eth_halt_state_only(void)
206{
207 struct udevice *current;
208 struct eth_device_priv *priv;
209
210 current = eth_get_dev();
211 if (!current || !device_active(current))
212 return;
213
Simon Glass95588622020-12-22 19:30:28 -0700214 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700215 priv->state = ETH_STATE_PASSIVE;
216}
217
218int eth_get_dev_index(void)
219{
220 if (eth_get_dev())
Simon Glass75e534b2020-12-16 21:20:07 -0700221 return dev_seq(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700222 return -1;
223}
224
225static int eth_write_hwaddr(struct udevice *dev)
226{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200227 struct eth_pdata *pdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700228 int ret = 0;
229
230 if (!dev || !device_active(dev))
231 return -EINVAL;
232
233 /* seq is valid since the device is active */
Simon Glass75e534b2020-12-16 21:20:07 -0700234 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass95588622020-12-22 19:30:28 -0700235 pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700236 if (!is_valid_ethaddr(pdata->enetaddr)) {
237 printf("\nError: %s address %pM illegal value\n",
238 dev->name, pdata->enetaddr);
239 return -EINVAL;
240 }
241
242 /*
243 * Drivers are allowed to decide not to implement this at
244 * run-time. E.g. Some devices may use it and some may not.
245 */
246 ret = eth_get_ops(dev)->write_hwaddr(dev);
247 if (ret == -ENOSYS)
248 ret = 0;
249 if (ret)
250 printf("\nWarning: %s failed to set MAC address\n",
251 dev->name);
252 }
253
254 return ret;
255}
256
257static int on_ethaddr(const char *name, const char *value, enum env_op op,
258 int flags)
259{
260 int index;
261 int retval;
262 struct udevice *dev;
263
264 /* look for an index after "eth" */
Simon Glassff9b9032021-07-24 09:03:30 -0600265 index = dectoul(name + 3, NULL);
Simon Glassc6011d22016-01-17 14:52:00 -0700266
Simon Glass07e13382020-12-16 21:20:29 -0700267 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700268 if (!retval) {
Simon Glass95588622020-12-22 19:30:28 -0700269 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700270 switch (op) {
271 case env_op_create:
272 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500273 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200274 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700275 break;
276 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100277 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700278 }
279 }
280
281 return 0;
282}
283U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
284
285int eth_init(void)
286{
Simon Glass64b723f2017-08-03 12:22:12 -0600287 char *ethact = env_get("ethact");
288 char *ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-01-17 14:52:00 -0700289 struct udevice *current = NULL;
290 struct udevice *old_current;
291 int ret = -ENODEV;
292
293 /*
294 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
295 * is already set to an ethernet device, we should stick to 'ethact'.
296 */
297 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
298 if (ethact) {
299 current = eth_get_dev_by_name(ethact);
300 if (!current)
301 return -EINVAL;
302 }
303 }
304
305 if (!current) {
306 current = eth_get_dev();
307 if (!current) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200308 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700309 return -ENODEV;
310 }
311 }
312
313 old_current = current;
314 do {
315 if (current) {
316 debug("Trying %s\n", current->name);
317
318 if (device_active(current)) {
319 ret = eth_get_ops(current)->start(current);
320 if (ret >= 0) {
321 struct eth_device_priv *priv =
Simon Glass95588622020-12-22 19:30:28 -0700322 dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700323
324 priv->state = ETH_STATE_ACTIVE;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100325 priv->running = true;
Simon Glassc6011d22016-01-17 14:52:00 -0700326 return 0;
327 }
328 } else {
329 ret = eth_errno;
330 }
331
332 debug("FAIL\n");
333 } else {
334 debug("PROBE FAIL\n");
335 }
336
337 /*
338 * If ethrotate is enabled, this will change "current",
339 * otherwise we will drop out of this while loop immediately
340 */
341 eth_try_another(0);
342 /* This will ensure the new "current" attempted to probe */
343 current = eth_get_dev();
344 } while (old_current != current);
345
346 return ret;
347}
348
349void eth_halt(void)
350{
351 struct udevice *current;
352 struct eth_device_priv *priv;
353
354 current = eth_get_dev();
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100355 if (!current)
Simon Glassc6011d22016-01-17 14:52:00 -0700356 return;
357
Simon Glass95588622020-12-22 19:30:28 -0700358 priv = dev_get_uclass_priv(current);
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100359 if (!priv || !priv->running)
360 return;
361
362 eth_get_ops(current)->stop(current);
363 priv->state = ETH_STATE_PASSIVE;
364 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700365}
366
367int eth_is_active(struct udevice *dev)
368{
369 struct eth_device_priv *priv;
370
371 if (!dev || !device_active(dev))
372 return 0;
373
374 priv = dev_get_uclass_priv(dev);
375 return priv->state == ETH_STATE_ACTIVE;
376}
377
378int eth_send(void *packet, int length)
379{
380 struct udevice *current;
381 int ret;
382
383 current = eth_get_dev();
384 if (!current)
385 return -ENODEV;
386
Alexander Graf71511492018-03-15 15:07:09 +0100387 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700388 return -EINVAL;
389
390 ret = eth_get_ops(current)->send(current, packet, length);
391 if (ret < 0) {
392 /* We cannot completely return the error at present */
393 debug("%s: send() returned error %d\n", __func__, ret);
394 }
Ramon Friedac598c12019-07-18 21:43:30 +0300395#if defined(CONFIG_CMD_PCAP)
396 if (ret >= 0)
397 pcap_post(packet, length, true);
398#endif
Simon Glassc6011d22016-01-17 14:52:00 -0700399 return ret;
400}
401
402int eth_rx(void)
403{
404 struct udevice *current;
405 uchar *packet;
406 int flags;
407 int ret;
408 int i;
409
410 current = eth_get_dev();
411 if (!current)
412 return -ENODEV;
413
Alexander Graf71511492018-03-15 15:07:09 +0100414 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700415 return -EINVAL;
416
417 /* Process up to 32 packets at one time */
418 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildtd47430a2020-10-07 11:03:30 +0200419 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassc6011d22016-01-17 14:52:00 -0700420 ret = eth_get_ops(current)->recv(current, flags, &packet);
421 flags = 0;
422 if (ret > 0)
423 net_process_received_packet(packet, ret);
424 if (ret >= 0 && eth_get_ops(current)->free_pkt)
425 eth_get_ops(current)->free_pkt(current, packet, ret);
426 if (ret <= 0)
427 break;
428 }
429 if (ret == -EAGAIN)
430 ret = 0;
431 if (ret < 0) {
432 /* We cannot completely return the error at present */
433 debug("%s: recv() returned error %d\n", __func__, ret);
434 }
435 return ret;
436}
437
438int eth_initialize(void)
439{
440 int num_devices = 0;
441 struct udevice *dev;
442
443 eth_common_init();
444
445 /*
446 * Devices need to write the hwaddr even if not started so that Linux
447 * will have access to the hwaddr that u-boot stored for the device.
448 * This is accomplished by attempting to probe each device and calling
449 * their write_hwaddr() operation.
450 */
Mario Six865c3872018-04-27 14:52:56 +0200451 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700452 if (!dev) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200453 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700454 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
455 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600456 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-01-17 14:52:00 -0700457 struct udevice *prime_dev = NULL;
458
459 if (ethprime)
460 prime_dev = eth_get_dev_by_name(ethprime);
461 if (prime_dev) {
462 eth_set_dev(prime_dev);
463 eth_current_changed();
464 } else {
465 eth_set_dev(NULL);
466 }
467
468 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
469 do {
Simon Glass3e14a222020-12-16 21:20:16 -0700470 if (device_active(dev)) {
Michael Walle902c7962019-10-22 01:03:10 +0200471 if (num_devices)
472 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700473
Simon Glass75e534b2020-12-16 21:20:07 -0700474 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700475
Michael Walle902c7962019-10-22 01:03:10 +0200476 if (ethprime && dev == prime_dev)
477 printf(" [PRIME]");
478 }
Simon Glassc6011d22016-01-17 14:52:00 -0700479
480 eth_write_hwaddr(dev);
481
Simon Glass3e14a222020-12-16 21:20:16 -0700482 if (device_active(dev))
Michael Walle902c7962019-10-22 01:03:10 +0200483 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200484 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700485 } while (dev);
486
Michael Walle902c7962019-10-22 01:03:10 +0200487 if (!num_devices)
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200488 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700489 putc('\n');
490 }
491
492 return num_devices;
493}
494
495static int eth_post_bind(struct udevice *dev)
496{
Simon Glassb490f5f2023-01-17 10:47:28 -0700497 struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
Simon Glass4f840a22022-04-24 23:31:15 -0600498 int ret;
499
Simon Glassc6011d22016-01-17 14:52:00 -0700500 if (strchr(dev->name, ' ')) {
501 printf("\nError: eth device name \"%s\" has a space!\n",
502 dev->name);
503 return -EINVAL;
504 }
505
Ye Licd5bb772020-05-03 22:41:14 +0800506#ifdef CONFIG_DM_ETH_PHY
507 eth_phy_binds_nodes(dev);
508#endif
Simon Glassb490f5f2023-01-17 10:47:28 -0700509 if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
Simon Glass4f840a22022-04-24 23:31:15 -0600510 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
511 if (ret)
512 return log_msg_ret("bootdev", ret);
513 }
Ye Licd5bb772020-05-03 22:41:14 +0800514
Simon Glassc6011d22016-01-17 14:52:00 -0700515 return 0;
516}
517
518static int eth_pre_unbind(struct udevice *dev)
519{
520 /* Don't hang onto a pointer that is going away */
521 if (dev == eth_get_uclass_priv()->current)
522 eth_set_dev(NULL);
523
524 return 0;
525}
526
Thierry Reding8fe08532019-05-20 17:59:57 +0200527static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
528{
Simon Glass1eb72942021-01-13 20:29:47 -0700529#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding8fe08532019-05-20 17:59:57 +0200530 const uint8_t *p;
Sean Andersondfdda782022-05-05 13:11:41 -0400531 struct nvmem_cell mac_cell;
Thierry Reding8fe08532019-05-20 17:59:57 +0200532
533 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
534 if (!p)
535 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
536
Sean Andersondfdda782022-05-05 13:11:41 -0400537 if (p) {
538 memcpy(mac, p, ARP_HLEN);
539 return true;
540 }
Thierry Reding8fe08532019-05-20 17:59:57 +0200541
Sean Andersondfdda782022-05-05 13:11:41 -0400542 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
543 return false;
Thierry Reding8fe08532019-05-20 17:59:57 +0200544
Sean Andersondfdda782022-05-05 13:11:41 -0400545 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
Thierry Reding8fe08532019-05-20 17:59:57 +0200546#else
547 return false;
548#endif
549}
550
Simon Glassc6011d22016-01-17 14:52:00 -0700551static int eth_post_probe(struct udevice *dev)
552{
Simon Glass95588622020-12-22 19:30:28 -0700553 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
554 struct eth_pdata *pdata = dev_get_plat(dev);
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100555 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100556 char *source = "DT";
Simon Glassc6011d22016-01-17 14:52:00 -0700557
Simon Glassc6011d22016-01-17 14:52:00 -0700558 priv->state = ETH_STATE_INIT;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100559 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700560
Thierry Reding8fe08532019-05-20 17:59:57 +0200561 /* Check if the device has a valid MAC address in device tree */
562 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
563 !is_valid_ethaddr(pdata->enetaddr)) {
564 /* Check if the device has a MAC address in ROM */
Michal Simek58e609b2023-09-15 16:10:06 +0200565 if (eth_get_ops(dev)->read_rom_hwaddr) {
566 int ret;
567
568 ret = eth_get_ops(dev)->read_rom_hwaddr(dev);
569 if (!ret)
570 source = "ROM";
571 }
Thierry Reding8fe08532019-05-20 17:59:57 +0200572 }
Simon Glassc6011d22016-01-17 14:52:00 -0700573
Simon Glass75e534b2020-12-16 21:20:07 -0700574 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700575 if (!is_zero_ethaddr(env_enetaddr)) {
576 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100577 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700578 printf("\nWarning: %s MAC addresses don't match:\n",
579 dev->name);
Michal Simek31411132020-03-16 11:36:17 +0100580 printf("Address in %s is\t\t%pM\n",
581 source, pdata->enetaddr);
582 printf("Address in environment is\t%pM\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700583 env_enetaddr);
584 }
585
586 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100587 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700588 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700589 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
590 pdata->enetaddr);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100591 } else if (is_zero_ethaddr(pdata->enetaddr) ||
592 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700593#ifdef CONFIG_NET_RANDOM_ETHADDR
594 net_random_ethaddr(pdata->enetaddr);
595 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700596 dev->name, dev_seq(dev), pdata->enetaddr);
Michal Simek2b254022022-01-11 10:28:09 +0100597 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
598 pdata->enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700599#else
Fabio Estevam3cc84442023-10-20 09:41:51 -0300600 printf("\nError: %s No valid MAC address found.\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700601 dev->name);
602 return -EINVAL;
603#endif
604 }
605
Thierry Reding13064872019-05-20 17:59:56 +0200606 eth_write_hwaddr(dev);
607
Simon Glassc6011d22016-01-17 14:52:00 -0700608 return 0;
609}
610
611static int eth_pre_remove(struct udevice *dev)
612{
Simon Glass95588622020-12-22 19:30:28 -0700613 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700614
615 eth_get_ops(dev)->stop(dev);
616
617 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100618 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700619
620 return 0;
621}
622
Michael Walle7efcdfd2021-02-25 16:51:11 +0100623UCLASS_DRIVER(ethernet) = {
624 .name = "ethernet",
Simon Glassc6011d22016-01-17 14:52:00 -0700625 .id = UCLASS_ETH,
626 .post_bind = eth_post_bind,
627 .pre_unbind = eth_pre_unbind,
628 .post_probe = eth_post_probe,
629 .pre_remove = eth_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700630 .priv_auto = sizeof(struct eth_uclass_priv),
631 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassc6011d22016-01-17 14:52:00 -0700632 .flags = DM_UC_FLAG_SEQ_ALIAS,
633};