]> git.ipfire.org Git - thirdparty/u-boot.git/blame - net/eth-uclass.c
configs: Disable now unbuildable SPI options for boards
[thirdparty/u-boot.git] / net / eth-uclass.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
db9391e1
SG
2/*
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
db9391e1
SG
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <environment.h>
11#include <net.h>
12#include <dm/device-internal.h>
13#include <dm/uclass-internal.h>
14#include "eth_internal.h"
15
a7c45ec4
SG
16DECLARE_GLOBAL_DATA_PTR;
17
db9391e1
SG
18/**
19 * struct eth_device_priv - private structure for each Ethernet device
20 *
21 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
22 */
23struct eth_device_priv {
24 enum eth_state_t state;
25};
26
27/**
28 * struct eth_uclass_priv - The structure attached to the uclass itself
29 *
30 * @current: The Ethernet device that the network functions are using
31 */
32struct eth_uclass_priv {
33 struct udevice *current;
34};
35
36/* eth_errno - This stores the most recent failure code from DM functions */
37static int eth_errno;
38
39static struct eth_uclass_priv *eth_get_uclass_priv(void)
40{
41 struct uclass *uc;
42
43 uclass_get(UCLASS_ETH, &uc);
44 assert(uc);
45 return uc->priv;
46}
47
48void eth_set_current_to_next(void)
49{
50 struct eth_uclass_priv *uc_priv;
51
52 uc_priv = eth_get_uclass_priv();
53 if (uc_priv->current)
54 uclass_next_device(&uc_priv->current);
55 if (!uc_priv->current)
56 uclass_first_device(UCLASS_ETH, &uc_priv->current);
57}
58
59/*
60 * Typically this will simply return the active device.
61 * In the case where the most recent active device was unset, this will attempt
62 * to return the first device. If that device doesn't exist or fails to probe,
63 * this function will return NULL.
64 */
65struct udevice *eth_get_dev(void)
66{
67 struct eth_uclass_priv *uc_priv;
68
69 uc_priv = eth_get_uclass_priv();
70 if (!uc_priv->current)
71 eth_errno = uclass_first_device(UCLASS_ETH,
72 &uc_priv->current);
73 return uc_priv->current;
74}
75
76/*
77 * Typically this will just store a device pointer.
78 * In case it was not probed, we will attempt to do so.
79 * dev may be NULL to unset the active device.
80 */
81void eth_set_dev(struct udevice *dev)
82{
83 if (dev && !device_active(dev)) {
84 eth_errno = device_probe(dev);
85 if (eth_errno)
86 dev = NULL;
87 }
88
89 eth_get_uclass_priv()->current = dev;
90}
91
92/*
93 * Find the udevice that either has the name passed in as devname or has an
94 * alias named devname.
95 */
96struct udevice *eth_get_dev_by_name(const char *devname)
97{
98 int seq = -1;
99 char *endp = NULL;
100 const char *startp = NULL;
101 struct udevice *it;
102 struct uclass *uc;
103 int len = strlen("eth");
104
105 /* Must be longer than 3 to be an alias */
106 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
107 startp = devname + len;
108 seq = simple_strtoul(startp, &endp, 10);
109 }
110
111 uclass_get(UCLASS_ETH, &uc);
112 uclass_foreach_dev(it, uc) {
113 /*
114 * We need the seq to be valid, so try to probe it.
115 * If the probe fails, the seq will not match since it will be
116 * -1 instead of what we are looking for.
117 * We don't care about errors from probe here. Either they won't
118 * match an alias or it will match a literal name and we'll pick
119 * up the error when we try to probe again in eth_set_dev().
120 */
121 if (device_probe(it))
122 continue;
123 /* Check for the name or the sequence number to match */
124 if (strcmp(it->name, devname) == 0 ||
125 (endp > startp && it->seq == seq))
126 return it;
127 }
128
129 return NULL;
130}
131
132unsigned char *eth_get_ethaddr(void)
133{
134 struct eth_pdata *pdata;
135
136 if (eth_get_dev()) {
137 pdata = eth_get_dev()->platdata;
138 return pdata->enetaddr;
139 }
140
141 return NULL;
142}
143
144/* Set active state without calling start on the driver */
145int eth_init_state_only(void)
146{
147 struct udevice *current;
148 struct eth_device_priv *priv;
149
150 current = eth_get_dev();
151 if (!current || !device_active(current))
152 return -EINVAL;
153
154 priv = current->uclass_priv;
155 priv->state = ETH_STATE_ACTIVE;
156
157 return 0;
158}
159
160/* Set passive state without calling stop on the driver */
161void eth_halt_state_only(void)
162{
163 struct udevice *current;
164 struct eth_device_priv *priv;
165
166 current = eth_get_dev();
167 if (!current || !device_active(current))
168 return;
169
170 priv = current->uclass_priv;
171 priv->state = ETH_STATE_PASSIVE;
172}
173
174int eth_get_dev_index(void)
175{
176 if (eth_get_dev())
177 return eth_get_dev()->seq;
178 return -1;
179}
180
181static int eth_write_hwaddr(struct udevice *dev)
182{
c08248d6 183 struct eth_pdata *pdata;
db9391e1
SG
184 int ret = 0;
185
186 if (!dev || !device_active(dev))
187 return -EINVAL;
188
189 /* seq is valid since the device is active */
190 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
c08248d6 191 pdata = dev->platdata;
db9391e1
SG
192 if (!is_valid_ethaddr(pdata->enetaddr)) {
193 printf("\nError: %s address %pM illegal value\n",
194 dev->name, pdata->enetaddr);
195 return -EINVAL;
196 }
197
198 /*
199 * Drivers are allowed to decide not to implement this at
200 * run-time. E.g. Some devices may use it and some may not.
201 */
202 ret = eth_get_ops(dev)->write_hwaddr(dev);
203 if (ret == -ENOSYS)
204 ret = 0;
205 if (ret)
206 printf("\nWarning: %s failed to set MAC address\n",
207 dev->name);
208 }
209
210 return ret;
211}
212
213static int on_ethaddr(const char *name, const char *value, enum env_op op,
214 int flags)
215{
216 int index;
217 int retval;
218 struct udevice *dev;
219
220 /* look for an index after "eth" */
221 index = simple_strtoul(name + 3, NULL, 10);
222
223 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
224 if (!retval) {
225 struct eth_pdata *pdata = dev->platdata;
226 switch (op) {
227 case env_op_create:
228 case env_op_overwrite:
229 eth_parse_enetaddr(value, pdata->enetaddr);
c86ff7fd 230 eth_write_hwaddr(dev);
db9391e1
SG
231 break;
232 case env_op_delete:
a40db6d5 233 memset(pdata->enetaddr, 0, ARP_HLEN);
db9391e1
SG
234 }
235 }
236
237 return 0;
238}
239U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
240
241int eth_init(void)
242{
00caae6d
SG
243 char *ethact = env_get("ethact");
244 char *ethrotate = env_get("ethrotate");
db9391e1
SG
245 struct udevice *current = NULL;
246 struct udevice *old_current;
247 int ret = -ENODEV;
248
249 /*
250 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
251 * is already set to an ethernet device, we should stick to 'ethact'.
252 */
253 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
254 if (ethact) {
255 current = eth_get_dev_by_name(ethact);
256 if (!current)
257 return -EINVAL;
258 }
259 }
260
261 if (!current) {
262 current = eth_get_dev();
263 if (!current) {
264 printf("No ethernet found.\n");
265 return -ENODEV;
266 }
267 }
268
269 old_current = current;
270 do {
271 if (current) {
272 debug("Trying %s\n", current->name);
273
274 if (device_active(current)) {
275 ret = eth_get_ops(current)->start(current);
276 if (ret >= 0) {
277 struct eth_device_priv *priv =
278 current->uclass_priv;
279
280 priv->state = ETH_STATE_ACTIVE;
281 return 0;
282 }
283 } else {
284 ret = eth_errno;
285 }
286
287 debug("FAIL\n");
288 } else {
289 debug("PROBE FAIL\n");
290 }
291
292 /*
293 * If ethrotate is enabled, this will change "current",
294 * otherwise we will drop out of this while loop immediately
295 */
296 eth_try_another(0);
297 /* This will ensure the new "current" attempted to probe */
298 current = eth_get_dev();
299 } while (old_current != current);
300
301 return ret;
302}
303
304void eth_halt(void)
305{
306 struct udevice *current;
307 struct eth_device_priv *priv;
308
309 current = eth_get_dev();
68acb51f 310 if (!current || !eth_is_active(current))
db9391e1
SG
311 return;
312
313 eth_get_ops(current)->stop(current);
314 priv = current->uclass_priv;
c3211708
JJH
315 if (priv)
316 priv->state = ETH_STATE_PASSIVE;
db9391e1
SG
317}
318
319int eth_is_active(struct udevice *dev)
320{
321 struct eth_device_priv *priv;
322
323 if (!dev || !device_active(dev))
324 return 0;
325
326 priv = dev_get_uclass_priv(dev);
327 return priv->state == ETH_STATE_ACTIVE;
328}
329
330int eth_send(void *packet, int length)
331{
332 struct udevice *current;
333 int ret;
334
335 current = eth_get_dev();
336 if (!current)
337 return -ENODEV;
338
a532e2f2 339 if (!eth_is_active(current))
db9391e1
SG
340 return -EINVAL;
341
342 ret = eth_get_ops(current)->send(current, packet, length);
343 if (ret < 0) {
344 /* We cannot completely return the error at present */
345 debug("%s: send() returned error %d\n", __func__, ret);
346 }
347 return ret;
348}
349
350int eth_rx(void)
351{
352 struct udevice *current;
353 uchar *packet;
354 int flags;
355 int ret;
356 int i;
357
358 current = eth_get_dev();
359 if (!current)
360 return -ENODEV;
361
a532e2f2 362 if (!eth_is_active(current))
db9391e1
SG
363 return -EINVAL;
364
365 /* Process up to 32 packets at one time */
366 flags = ETH_RECV_CHECK_DEVICE;
367 for (i = 0; i < 32; i++) {
368 ret = eth_get_ops(current)->recv(current, flags, &packet);
369 flags = 0;
370 if (ret > 0)
371 net_process_received_packet(packet, ret);
372 if (ret >= 0 && eth_get_ops(current)->free_pkt)
373 eth_get_ops(current)->free_pkt(current, packet, ret);
374 if (ret <= 0)
375 break;
376 }
377 if (ret == -EAGAIN)
378 ret = 0;
379 if (ret < 0) {
380 /* We cannot completely return the error at present */
381 debug("%s: recv() returned error %d\n", __func__, ret);
382 }
383 return ret;
384}
385
386int eth_initialize(void)
387{
388 int num_devices = 0;
389 struct udevice *dev;
390
391 eth_common_init();
392
393 /*
394 * Devices need to write the hwaddr even if not started so that Linux
395 * will have access to the hwaddr that u-boot stored for the device.
396 * This is accomplished by attempting to probe each device and calling
397 * their write_hwaddr() operation.
398 */
3ce43042 399 uclass_first_device_check(UCLASS_ETH, &dev);
db9391e1
SG
400 if (!dev) {
401 printf("No ethernet found.\n");
402 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
403 } else {
00caae6d 404 char *ethprime = env_get("ethprime");
db9391e1
SG
405 struct udevice *prime_dev = NULL;
406
407 if (ethprime)
408 prime_dev = eth_get_dev_by_name(ethprime);
409 if (prime_dev) {
410 eth_set_dev(prime_dev);
411 eth_current_changed();
412 } else {
413 eth_set_dev(NULL);
414 }
415
416 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
417 do {
418 if (num_devices)
419 printf(", ");
420
421 printf("eth%d: %s", dev->seq, dev->name);
422
423 if (ethprime && dev == prime_dev)
424 printf(" [PRIME]");
425
426 eth_write_hwaddr(dev);
427
3ce43042 428 uclass_next_device_check(&dev);
db9391e1
SG
429 num_devices++;
430 } while (dev);
431
432 putc('\n');
433 }
434
435 return num_devices;
436}
437
438static int eth_post_bind(struct udevice *dev)
439{
440 if (strchr(dev->name, ' ')) {
441 printf("\nError: eth device name \"%s\" has a space!\n",
442 dev->name);
443 return -EINVAL;
444 }
445
446 return 0;
447}
448
449static int eth_pre_unbind(struct udevice *dev)
450{
451 /* Don't hang onto a pointer that is going away */
452 if (dev == eth_get_uclass_priv()->current)
453 eth_set_dev(NULL);
454
455 return 0;
456}
457
379af67a
TR
458static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
459{
460#if IS_ENABLED(CONFIG_OF_CONTROL)
461 const uint8_t *p;
462
463 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
464 if (!p)
465 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
466
467 if (!p)
468 return false;
469
470 memcpy(mac, p, ARP_HLEN);
471
472 return true;
473#else
474 return false;
475#endif
476}
477
db9391e1
SG
478static int eth_post_probe(struct udevice *dev)
479{
480 struct eth_device_priv *priv = dev->uclass_priv;
481 struct eth_pdata *pdata = dev->platdata;
a40db6d5 482 unsigned char env_enetaddr[ARP_HLEN];
db9391e1
SG
483
484#if defined(CONFIG_NEEDS_MANUAL_RELOC)
485 struct eth_ops *ops = eth_get_ops(dev);
486 static int reloc_done;
487
488 if (!reloc_done) {
489 if (ops->start)
490 ops->start += gd->reloc_off;
491 if (ops->send)
492 ops->send += gd->reloc_off;
493 if (ops->recv)
494 ops->recv += gd->reloc_off;
495 if (ops->free_pkt)
496 ops->free_pkt += gd->reloc_off;
497 if (ops->stop)
498 ops->stop += gd->reloc_off;
db9391e1
SG
499 if (ops->mcast)
500 ops->mcast += gd->reloc_off;
db9391e1
SG
501 if (ops->write_hwaddr)
502 ops->write_hwaddr += gd->reloc_off;
503 if (ops->read_rom_hwaddr)
504 ops->read_rom_hwaddr += gd->reloc_off;
505
506 reloc_done++;
507 }
508#endif
509
510 priv->state = ETH_STATE_INIT;
511
379af67a
TR
512 /* Check if the device has a valid MAC address in device tree */
513 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
514 !is_valid_ethaddr(pdata->enetaddr)) {
515 /* Check if the device has a MAC address in ROM */
516 if (eth_get_ops(dev)->read_rom_hwaddr)
517 eth_get_ops(dev)->read_rom_hwaddr(dev);
518 }
db9391e1 519
35affd7a 520 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
db9391e1
SG
521 if (!is_zero_ethaddr(env_enetaddr)) {
522 if (!is_zero_ethaddr(pdata->enetaddr) &&
a40db6d5 523 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
db9391e1
SG
524 printf("\nWarning: %s MAC addresses don't match:\n",
525 dev->name);
26d40b0a 526 printf("Address in ROM is %pM\n",
db9391e1
SG
527 pdata->enetaddr);
528 printf("Address in environment is %pM\n",
529 env_enetaddr);
530 }
531
532 /* Override the ROM MAC address */
a40db6d5 533 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
db9391e1 534 } else if (is_valid_ethaddr(pdata->enetaddr)) {
fd1e959e 535 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
db9391e1
SG
536 printf("\nWarning: %s using MAC address from ROM\n",
537 dev->name);
aa555fe9
SDPP
538 } else if (is_zero_ethaddr(pdata->enetaddr) ||
539 !is_valid_ethaddr(pdata->enetaddr)) {
db9391e1
SG
540#ifdef CONFIG_NET_RANDOM_ETHADDR
541 net_random_ethaddr(pdata->enetaddr);
542 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
543 dev->name, dev->seq, pdata->enetaddr);
544#else
545 printf("\nError: %s address not set.\n",
546 dev->name);
547 return -EINVAL;
548#endif
549 }
550
b743bbd2
TR
551 eth_write_hwaddr(dev);
552
db9391e1
SG
553 return 0;
554}
555
556static int eth_pre_remove(struct udevice *dev)
557{
558 struct eth_pdata *pdata = dev->platdata;
559
560 eth_get_ops(dev)->stop(dev);
561
562 /* clear the MAC address */
a40db6d5 563 memset(pdata->enetaddr, 0, ARP_HLEN);
db9391e1
SG
564
565 return 0;
566}
567
568UCLASS_DRIVER(eth) = {
569 .name = "eth",
570 .id = UCLASS_ETH,
571 .post_bind = eth_post_bind,
572 .pre_unbind = eth_pre_unbind,
573 .post_probe = eth_post_probe,
574 .pre_remove = eth_pre_remove,
575 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
576 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
577 .flags = DM_UC_FLAG_SEQ_ALIAS,
578};