blob: b01a910938e1d2712144a0610995dc73c43c1c3e [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
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;
51
52static struct eth_uclass_priv *eth_get_uclass_priv(void)
53{
54 struct uclass *uc;
Peng Fan46ec1042020-05-03 22:41:13 +080055 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -070056
Peng Fan46ec1042020-05-03 22:41:13 +080057 ret = uclass_get(UCLASS_ETH, &uc);
58 if (ret)
59 return NULL;
60
Simon Glassc6011d22016-01-17 14:52:00 -070061 assert(uc);
Simon Glass95588622020-12-22 19:30:28 -070062 return uclass_get_priv(uc);
Simon Glassc6011d22016-01-17 14:52:00 -070063}
64
Simon Glassb490f5f2023-01-17 10:47:28 -070065void eth_set_enable_bootdevs(bool enable)
66{
67 struct eth_uclass_priv *priv = eth_get_uclass_priv();
68
69 if (priv)
70 priv->no_bootdevs = !enable;
71}
72
Simon Glassc6011d22016-01-17 14:52:00 -070073void eth_set_current_to_next(void)
74{
75 struct eth_uclass_priv *uc_priv;
76
77 uc_priv = eth_get_uclass_priv();
78 if (uc_priv->current)
79 uclass_next_device(&uc_priv->current);
80 if (!uc_priv->current)
81 uclass_first_device(UCLASS_ETH, &uc_priv->current);
82}
83
84/*
85 * Typically this will simply return the active device.
86 * In the case where the most recent active device was unset, this will attempt
Michael Walle384023e2021-02-24 17:30:44 +010087 * to return the device with sequence id 0 (which can be configured by the
88 * device tree). If this fails, fall back to just getting the first device.
89 * The latter is non-deterministic and depends on the order of the probing.
90 * If that device doesn't exist or fails to probe, this function will return
91 * NULL.
Simon Glassc6011d22016-01-17 14:52:00 -070092 */
93struct udevice *eth_get_dev(void)
94{
95 struct eth_uclass_priv *uc_priv;
96
97 uc_priv = eth_get_uclass_priv();
Sean Anderson35e82982020-09-12 17:45:43 -040098 if (!uc_priv)
99 return NULL;
100
Michael Walle384023e2021-02-24 17:30:44 +0100101 if (!uc_priv->current) {
102 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
103 &uc_priv->current);
104 if (eth_errno)
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200105 eth_errno = uclass_first_device_err(UCLASS_ETH,
106 &uc_priv->current);
Michal Suchanek050aa362022-10-12 21:58:02 +0200107 if (eth_errno)
108 uc_priv->current = NULL;
Michael Walle384023e2021-02-24 17:30:44 +0100109 }
Simon Glassc6011d22016-01-17 14:52:00 -0700110 return uc_priv->current;
111}
112
113/*
114 * Typically this will just store a device pointer.
115 * In case it was not probed, we will attempt to do so.
116 * dev may be NULL to unset the active device.
117 */
118void eth_set_dev(struct udevice *dev)
119{
120 if (dev && !device_active(dev)) {
121 eth_errno = device_probe(dev);
122 if (eth_errno)
123 dev = NULL;
124 }
125
126 eth_get_uclass_priv()->current = dev;
127}
128
129/*
130 * Find the udevice that either has the name passed in as devname or has an
131 * alias named devname.
132 */
133struct udevice *eth_get_dev_by_name(const char *devname)
134{
135 int seq = -1;
136 char *endp = NULL;
137 const char *startp = NULL;
138 struct udevice *it;
139 struct uclass *uc;
140 int len = strlen("eth");
Peng Fan46ec1042020-05-03 22:41:13 +0800141 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -0700142
143 /* Must be longer than 3 to be an alias */
144 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
145 startp = devname + len;
Simon Glassff9b9032021-07-24 09:03:30 -0600146 seq = dectoul(startp, &endp);
Simon Glassc6011d22016-01-17 14:52:00 -0700147 }
148
Peng Fan46ec1042020-05-03 22:41:13 +0800149 ret = uclass_get(UCLASS_ETH, &uc);
150 if (ret)
151 return NULL;
152
Simon Glassc6011d22016-01-17 14:52:00 -0700153 uclass_foreach_dev(it, uc) {
154 /*
Simon Glassc6011d22016-01-17 14:52:00 -0700155 * We don't care about errors from probe here. Either they won't
156 * match an alias or it will match a literal name and we'll pick
157 * up the error when we try to probe again in eth_set_dev().
158 */
159 if (device_probe(it))
160 continue;
161 /* Check for the name or the sequence number to match */
162 if (strcmp(it->name, devname) == 0 ||
Simon Glass75e534b2020-12-16 21:20:07 -0700163 (endp > startp && dev_seq(it) == seq))
Simon Glassc6011d22016-01-17 14:52:00 -0700164 return it;
165 }
166
167 return NULL;
168}
169
170unsigned char *eth_get_ethaddr(void)
171{
172 struct eth_pdata *pdata;
173
174 if (eth_get_dev()) {
Simon Glass95588622020-12-22 19:30:28 -0700175 pdata = dev_get_plat(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700176 return pdata->enetaddr;
177 }
178
179 return NULL;
180}
181
182/* Set active state without calling start on the driver */
183int eth_init_state_only(void)
184{
185 struct udevice *current;
186 struct eth_device_priv *priv;
187
188 current = eth_get_dev();
189 if (!current || !device_active(current))
190 return -EINVAL;
191
Simon Glass95588622020-12-22 19:30:28 -0700192 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700193 priv->state = ETH_STATE_ACTIVE;
194
195 return 0;
196}
197
198/* Set passive state without calling stop on the driver */
199void eth_halt_state_only(void)
200{
201 struct udevice *current;
202 struct eth_device_priv *priv;
203
204 current = eth_get_dev();
205 if (!current || !device_active(current))
206 return;
207
Simon Glass95588622020-12-22 19:30:28 -0700208 priv = dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700209 priv->state = ETH_STATE_PASSIVE;
210}
211
212int eth_get_dev_index(void)
213{
214 if (eth_get_dev())
Simon Glass75e534b2020-12-16 21:20:07 -0700215 return dev_seq(eth_get_dev());
Simon Glassc6011d22016-01-17 14:52:00 -0700216 return -1;
217}
218
219static int eth_write_hwaddr(struct udevice *dev)
220{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200221 struct eth_pdata *pdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700222 int ret = 0;
223
224 if (!dev || !device_active(dev))
225 return -EINVAL;
226
227 /* seq is valid since the device is active */
Simon Glass75e534b2020-12-16 21:20:07 -0700228 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
Simon Glass95588622020-12-22 19:30:28 -0700229 pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700230 if (!is_valid_ethaddr(pdata->enetaddr)) {
231 printf("\nError: %s address %pM illegal value\n",
232 dev->name, pdata->enetaddr);
233 return -EINVAL;
234 }
235
236 /*
237 * Drivers are allowed to decide not to implement this at
238 * run-time. E.g. Some devices may use it and some may not.
239 */
240 ret = eth_get_ops(dev)->write_hwaddr(dev);
241 if (ret == -ENOSYS)
242 ret = 0;
243 if (ret)
244 printf("\nWarning: %s failed to set MAC address\n",
245 dev->name);
246 }
247
248 return ret;
249}
250
251static int on_ethaddr(const char *name, const char *value, enum env_op op,
252 int flags)
253{
254 int index;
255 int retval;
256 struct udevice *dev;
257
258 /* look for an index after "eth" */
Simon Glassff9b9032021-07-24 09:03:30 -0600259 index = dectoul(name + 3, NULL);
Simon Glassc6011d22016-01-17 14:52:00 -0700260
Simon Glass07e13382020-12-16 21:20:29 -0700261 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700262 if (!retval) {
Simon Glass95588622020-12-22 19:30:28 -0700263 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700264 switch (op) {
265 case env_op_create:
266 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500267 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200268 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700269 break;
270 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100271 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700272 }
273 }
274
275 return 0;
276}
277U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
278
279int eth_init(void)
280{
Simon Glass64b723f2017-08-03 12:22:12 -0600281 char *ethact = env_get("ethact");
282 char *ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-01-17 14:52:00 -0700283 struct udevice *current = NULL;
284 struct udevice *old_current;
285 int ret = -ENODEV;
286
287 /*
288 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
289 * is already set to an ethernet device, we should stick to 'ethact'.
290 */
291 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
292 if (ethact) {
293 current = eth_get_dev_by_name(ethact);
294 if (!current)
295 return -EINVAL;
296 }
297 }
298
299 if (!current) {
300 current = eth_get_dev();
301 if (!current) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200302 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700303 return -ENODEV;
304 }
305 }
306
307 old_current = current;
308 do {
309 if (current) {
310 debug("Trying %s\n", current->name);
311
312 if (device_active(current)) {
313 ret = eth_get_ops(current)->start(current);
314 if (ret >= 0) {
315 struct eth_device_priv *priv =
Simon Glass95588622020-12-22 19:30:28 -0700316 dev_get_uclass_priv(current);
Simon Glassc6011d22016-01-17 14:52:00 -0700317
318 priv->state = ETH_STATE_ACTIVE;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100319 priv->running = true;
Simon Glassc6011d22016-01-17 14:52:00 -0700320 return 0;
321 }
322 } else {
323 ret = eth_errno;
324 }
325
326 debug("FAIL\n");
327 } else {
328 debug("PROBE FAIL\n");
329 }
330
331 /*
332 * If ethrotate is enabled, this will change "current",
333 * otherwise we will drop out of this while loop immediately
334 */
335 eth_try_another(0);
336 /* This will ensure the new "current" attempted to probe */
337 current = eth_get_dev();
338 } while (old_current != current);
339
340 return ret;
341}
342
343void eth_halt(void)
344{
345 struct udevice *current;
346 struct eth_device_priv *priv;
347
348 current = eth_get_dev();
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100349 if (!current)
Simon Glassc6011d22016-01-17 14:52:00 -0700350 return;
351
Simon Glass95588622020-12-22 19:30:28 -0700352 priv = dev_get_uclass_priv(current);
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100353 if (!priv || !priv->running)
354 return;
355
356 eth_get_ops(current)->stop(current);
357 priv->state = ETH_STATE_PASSIVE;
358 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700359}
360
361int eth_is_active(struct udevice *dev)
362{
363 struct eth_device_priv *priv;
364
365 if (!dev || !device_active(dev))
366 return 0;
367
368 priv = dev_get_uclass_priv(dev);
369 return priv->state == ETH_STATE_ACTIVE;
370}
371
372int eth_send(void *packet, int length)
373{
374 struct udevice *current;
375 int ret;
376
377 current = eth_get_dev();
378 if (!current)
379 return -ENODEV;
380
Alexander Graf71511492018-03-15 15:07:09 +0100381 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700382 return -EINVAL;
383
384 ret = eth_get_ops(current)->send(current, packet, length);
385 if (ret < 0) {
386 /* We cannot completely return the error at present */
387 debug("%s: send() returned error %d\n", __func__, ret);
388 }
Ramon Friedac598c12019-07-18 21:43:30 +0300389#if defined(CONFIG_CMD_PCAP)
390 if (ret >= 0)
391 pcap_post(packet, length, true);
392#endif
Simon Glassc6011d22016-01-17 14:52:00 -0700393 return ret;
394}
395
396int eth_rx(void)
397{
398 struct udevice *current;
399 uchar *packet;
400 int flags;
401 int ret;
402 int i;
403
404 current = eth_get_dev();
405 if (!current)
406 return -ENODEV;
407
Alexander Graf71511492018-03-15 15:07:09 +0100408 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700409 return -EINVAL;
410
411 /* Process up to 32 packets at one time */
412 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildtd47430a2020-10-07 11:03:30 +0200413 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassc6011d22016-01-17 14:52:00 -0700414 ret = eth_get_ops(current)->recv(current, flags, &packet);
415 flags = 0;
416 if (ret > 0)
417 net_process_received_packet(packet, ret);
418 if (ret >= 0 && eth_get_ops(current)->free_pkt)
419 eth_get_ops(current)->free_pkt(current, packet, ret);
420 if (ret <= 0)
421 break;
422 }
423 if (ret == -EAGAIN)
424 ret = 0;
425 if (ret < 0) {
426 /* We cannot completely return the error at present */
427 debug("%s: recv() returned error %d\n", __func__, ret);
428 }
429 return ret;
430}
431
432int eth_initialize(void)
433{
434 int num_devices = 0;
435 struct udevice *dev;
436
437 eth_common_init();
438
439 /*
440 * Devices need to write the hwaddr even if not started so that Linux
441 * will have access to the hwaddr that u-boot stored for the device.
442 * This is accomplished by attempting to probe each device and calling
443 * their write_hwaddr() operation.
444 */
Mario Six865c3872018-04-27 14:52:56 +0200445 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700446 if (!dev) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200447 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700448 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
449 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600450 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-01-17 14:52:00 -0700451 struct udevice *prime_dev = NULL;
452
453 if (ethprime)
454 prime_dev = eth_get_dev_by_name(ethprime);
455 if (prime_dev) {
456 eth_set_dev(prime_dev);
457 eth_current_changed();
458 } else {
459 eth_set_dev(NULL);
460 }
461
462 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
463 do {
Simon Glass3e14a222020-12-16 21:20:16 -0700464 if (device_active(dev)) {
Michael Walle902c7962019-10-22 01:03:10 +0200465 if (num_devices)
466 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700467
Simon Glass75e534b2020-12-16 21:20:07 -0700468 printf("eth%d: %s", dev_seq(dev), dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700469
Michael Walle902c7962019-10-22 01:03:10 +0200470 if (ethprime && dev == prime_dev)
471 printf(" [PRIME]");
472 }
Simon Glassc6011d22016-01-17 14:52:00 -0700473
474 eth_write_hwaddr(dev);
475
Simon Glass3e14a222020-12-16 21:20:16 -0700476 if (device_active(dev))
Michael Walle902c7962019-10-22 01:03:10 +0200477 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200478 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700479 } while (dev);
480
Michael Walle902c7962019-10-22 01:03:10 +0200481 if (!num_devices)
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200482 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700483 putc('\n');
484 }
485
486 return num_devices;
487}
488
489static int eth_post_bind(struct udevice *dev)
490{
Simon Glassb490f5f2023-01-17 10:47:28 -0700491 struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
Simon Glass4f840a22022-04-24 23:31:15 -0600492 int ret;
493
Simon Glassc6011d22016-01-17 14:52:00 -0700494 if (strchr(dev->name, ' ')) {
495 printf("\nError: eth device name \"%s\" has a space!\n",
496 dev->name);
497 return -EINVAL;
498 }
499
Ye Licd5bb772020-05-03 22:41:14 +0800500#ifdef CONFIG_DM_ETH_PHY
501 eth_phy_binds_nodes(dev);
502#endif
Simon Glassb490f5f2023-01-17 10:47:28 -0700503 if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
Simon Glass4f840a22022-04-24 23:31:15 -0600504 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
505 if (ret)
506 return log_msg_ret("bootdev", ret);
507 }
Ye Licd5bb772020-05-03 22:41:14 +0800508
Simon Glassc6011d22016-01-17 14:52:00 -0700509 return 0;
510}
511
512static int eth_pre_unbind(struct udevice *dev)
513{
514 /* Don't hang onto a pointer that is going away */
515 if (dev == eth_get_uclass_priv()->current)
516 eth_set_dev(NULL);
517
518 return 0;
519}
520
Thierry Reding8fe08532019-05-20 17:59:57 +0200521static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
522{
Simon Glass1eb72942021-01-13 20:29:47 -0700523#if CONFIG_IS_ENABLED(OF_CONTROL)
Thierry Reding8fe08532019-05-20 17:59:57 +0200524 const uint8_t *p;
Sean Andersondfdda782022-05-05 13:11:41 -0400525 struct nvmem_cell mac_cell;
Thierry Reding8fe08532019-05-20 17:59:57 +0200526
527 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
528 if (!p)
529 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
530
Sean Andersondfdda782022-05-05 13:11:41 -0400531 if (p) {
532 memcpy(mac, p, ARP_HLEN);
533 return true;
534 }
Thierry Reding8fe08532019-05-20 17:59:57 +0200535
Sean Andersondfdda782022-05-05 13:11:41 -0400536 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
537 return false;
Thierry Reding8fe08532019-05-20 17:59:57 +0200538
Sean Andersondfdda782022-05-05 13:11:41 -0400539 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
Thierry Reding8fe08532019-05-20 17:59:57 +0200540#else
541 return false;
542#endif
543}
544
Simon Glassc6011d22016-01-17 14:52:00 -0700545static int eth_post_probe(struct udevice *dev)
546{
Simon Glass95588622020-12-22 19:30:28 -0700547 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
548 struct eth_pdata *pdata = dev_get_plat(dev);
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100549 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100550 char *source = "DT";
Simon Glassc6011d22016-01-17 14:52:00 -0700551
552#if defined(CONFIG_NEEDS_MANUAL_RELOC)
553 struct eth_ops *ops = eth_get_ops(dev);
554 static int reloc_done;
555
556 if (!reloc_done) {
557 if (ops->start)
558 ops->start += gd->reloc_off;
559 if (ops->send)
560 ops->send += gd->reloc_off;
561 if (ops->recv)
562 ops->recv += gd->reloc_off;
563 if (ops->free_pkt)
564 ops->free_pkt += gd->reloc_off;
565 if (ops->stop)
566 ops->stop += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700567 if (ops->mcast)
568 ops->mcast += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700569 if (ops->write_hwaddr)
570 ops->write_hwaddr += gd->reloc_off;
571 if (ops->read_rom_hwaddr)
572 ops->read_rom_hwaddr += gd->reloc_off;
573
574 reloc_done++;
575 }
576#endif
577
578 priv->state = ETH_STATE_INIT;
Matthias Schiffer9f5c1a52020-11-04 14:45:14 +0100579 priv->running = false;
Simon Glassc6011d22016-01-17 14:52:00 -0700580
Thierry Reding8fe08532019-05-20 17:59:57 +0200581 /* Check if the device has a valid MAC address in device tree */
582 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
583 !is_valid_ethaddr(pdata->enetaddr)) {
Michal Simek31411132020-03-16 11:36:17 +0100584 source = "ROM";
Thierry Reding8fe08532019-05-20 17:59:57 +0200585 /* Check if the device has a MAC address in ROM */
586 if (eth_get_ops(dev)->read_rom_hwaddr)
587 eth_get_ops(dev)->read_rom_hwaddr(dev);
588 }
Simon Glassc6011d22016-01-17 14:52:00 -0700589
Simon Glass75e534b2020-12-16 21:20:07 -0700590 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700591 if (!is_zero_ethaddr(env_enetaddr)) {
592 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100593 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700594 printf("\nWarning: %s MAC addresses don't match:\n",
595 dev->name);
Michal Simek31411132020-03-16 11:36:17 +0100596 printf("Address in %s is\t\t%pM\n",
597 source, pdata->enetaddr);
598 printf("Address in environment is\t%pM\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700599 env_enetaddr);
600 }
601
602 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100603 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700604 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700605 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
606 pdata->enetaddr);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100607 } else if (is_zero_ethaddr(pdata->enetaddr) ||
608 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700609#ifdef CONFIG_NET_RANDOM_ETHADDR
610 net_random_ethaddr(pdata->enetaddr);
611 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700612 dev->name, dev_seq(dev), pdata->enetaddr);
Michal Simek2b254022022-01-11 10:28:09 +0100613 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
614 pdata->enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700615#else
616 printf("\nError: %s address not set.\n",
617 dev->name);
618 return -EINVAL;
619#endif
620 }
621
Thierry Reding13064872019-05-20 17:59:56 +0200622 eth_write_hwaddr(dev);
623
Simon Glassc6011d22016-01-17 14:52:00 -0700624 return 0;
625}
626
627static int eth_pre_remove(struct udevice *dev)
628{
Simon Glass95588622020-12-22 19:30:28 -0700629 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700630
631 eth_get_ops(dev)->stop(dev);
632
633 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100634 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700635
636 return 0;
637}
638
Michael Walle7efcdfd2021-02-25 16:51:11 +0100639UCLASS_DRIVER(ethernet) = {
640 .name = "ethernet",
Simon Glassc6011d22016-01-17 14:52:00 -0700641 .id = UCLASS_ETH,
642 .post_bind = eth_post_bind,
643 .pre_unbind = eth_pre_unbind,
644 .post_probe = eth_post_probe,
645 .pre_remove = eth_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700646 .priv_auto = sizeof(struct eth_uclass_priv),
647 .per_device_auto = sizeof(struct eth_device_priv),
Simon Glassc6011d22016-01-17 14:52:00 -0700648 .flags = DM_UC_FLAG_SEQ_ALIAS,
649};