]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/eth.c
dts: sunxi: Bring in Ethernet device tree bindings
[people/ms/u-boot.git] / net / eth.c
CommitLineData
c609719b 1/*
05c3e68f 2 * (C) Copyright 2001-2015
c609719b 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
05c3e68f 4 * Joe Hershberger, National Instruments
c609719b 5 *
1a459660 6 * SPDX-License-Identifier: GPL-2.0+
c609719b
WD
7 */
8
9#include <common.h>
10#include <command.h>
05c3e68f 11#include <dm.h>
c609719b 12#include <net.h>
d9785c14 13#include <miiphy.h>
5f184715 14#include <phy.h>
75d9a45c 15#include <asm/errno.h>
05c3e68f 16#include <dm/device-internal.h>
c609719b 17
d2eaec60
JH
18DECLARE_GLOBAL_DATA_PTR;
19
3f6e6993
MF
20void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
21{
22 char *end;
23 int i;
24
25 for (i = 0; i < 6; ++i) {
26 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
27 if (addr)
28 addr = (*end) ? end + 1 : end;
29 }
30}
31
32int eth_getenv_enetaddr(char *name, uchar *enetaddr)
33{
34 eth_parse_enetaddr(getenv(name), enetaddr);
0adb5b76 35 return is_valid_ethaddr(enetaddr);
3f6e6993
MF
36}
37
38int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
39{
40 char buf[20];
41
42 sprintf(buf, "%pM", enetaddr);
43
44 return setenv(name, buf);
45}
86848a74 46
7616e785
SG
47int eth_getenv_enetaddr_by_index(const char *base_name, int index,
48 uchar *enetaddr)
86848a74
MF
49{
50 char enetvar[32];
7616e785 51 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
86848a74
MF
52 return eth_getenv_enetaddr(enetvar, enetaddr);
53}
3f6e6993 54
154177e1 55static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
c88ef3c1
RH
56 uchar *enetaddr)
57{
58 char enetvar[32];
59 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
60 return eth_setenv_enetaddr(enetvar, enetaddr);
61}
62
84eb1fba
JH
63static void eth_env_init(void)
64{
65 const char *s;
66
67 s = getenv("bootfile");
68 if (s != NULL)
1411157d
JH
69 copy_filename(net_boot_file_name, s,
70 sizeof(net_boot_file_name));
84eb1fba 71}
c88ef3c1 72
ecee9324
BW
73static int eth_mac_skip(int index)
74{
75 char enetvar[15];
76 char *skip_state;
ff819a3a 77
ecee9324 78 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
ff819a3a
JH
79 skip_state = getenv(enetvar);
80 return skip_state != NULL;
ecee9324
BW
81}
82
84eb1fba
JH
83static void eth_current_changed(void);
84
05c3e68f
JH
85#ifdef CONFIG_DM_ETH
86/**
87 * struct eth_device_priv - private structure for each Ethernet device
88 *
89 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
90 */
91struct eth_device_priv {
92 enum eth_state_t state;
93};
94
95/**
96 * struct eth_uclass_priv - The structure attached to the uclass itself
97 *
98 * @current: The Ethernet device that the network functions are using
99 */
100struct eth_uclass_priv {
101 struct udevice *current;
102};
103
60304592
JH
104/* eth_errno - This stores the most recent failure code from DM functions */
105static int eth_errno;
106
05c3e68f
JH
107static struct eth_uclass_priv *eth_get_uclass_priv(void)
108{
109 struct uclass *uc;
110
111 uclass_get(UCLASS_ETH, &uc);
112 assert(uc);
113 return uc->priv;
114}
115
116static void eth_set_current_to_next(void)
117{
118 struct eth_uclass_priv *uc_priv;
119
120 uc_priv = eth_get_uclass_priv();
121 if (uc_priv->current)
122 uclass_next_device(&uc_priv->current);
123 if (!uc_priv->current)
124 uclass_first_device(UCLASS_ETH, &uc_priv->current);
125}
126
60304592
JH
127/*
128 * Typically this will simply return the active device.
129 * In the case where the most recent active device was unset, this will attempt
130 * to return the first device. If that device doesn't exist or fails to probe,
131 * this function will return NULL.
132 */
05c3e68f
JH
133struct udevice *eth_get_dev(void)
134{
135 struct eth_uclass_priv *uc_priv;
136
137 uc_priv = eth_get_uclass_priv();
138 if (!uc_priv->current)
60304592 139 eth_errno = uclass_first_device(UCLASS_ETH,
05c3e68f
JH
140 &uc_priv->current);
141 return uc_priv->current;
142}
143
60304592
JH
144/*
145 * Typically this will just store a device pointer.
146 * In case it was not probed, we will attempt to do so.
147 * dev may be NULL to unset the active device.
148 */
05c3e68f
JH
149static void eth_set_dev(struct udevice *dev)
150{
60304592
JH
151 if (dev && !device_active(dev))
152 eth_errno = device_probe(dev);
05c3e68f
JH
153 eth_get_uclass_priv()->current = dev;
154}
155
e58780dc
JH
156/*
157 * Find the udevice that either has the name passed in as devname or has an
158 * alias named devname.
159 */
160struct udevice *eth_get_dev_by_name(const char *devname)
161{
162 int seq = -1;
163 char *endp = NULL;
164 const char *startp = NULL;
165 struct udevice *it;
166 struct uclass *uc;
167
168 /* Must be longer than 3 to be an alias */
169 if (strlen(devname) > strlen("eth")) {
170 startp = devname + strlen("eth");
171 seq = simple_strtoul(startp, &endp, 10);
172 }
173
174 uclass_get(UCLASS_ETH, &uc);
175 uclass_foreach_dev(it, uc) {
60304592
JH
176 /*
177 * We need the seq to be valid, so try to probe it.
178 * If the probe fails, the seq will not match since it will be
179 * -1 instead of what we are looking for.
180 * We don't care about errors from probe here. Either they won't
181 * match an alias or it will match a literal name and we'll pick
182 * up the error when we try to probe again in eth_set_dev().
183 */
e58780dc
JH
184 device_probe(it);
185 /*
186 * Check for the name or the sequence number to match
187 */
188 if (strcmp(it->name, devname) == 0 ||
189 (endp > startp && it->seq == seq))
190 return it;
191 }
192
193 return NULL;
194}
195
05c3e68f
JH
196unsigned char *eth_get_ethaddr(void)
197{
198 struct eth_pdata *pdata;
199
200 if (eth_get_dev()) {
201 pdata = eth_get_dev()->platdata;
202 return pdata->enetaddr;
203 }
204
205 return NULL;
206}
207
208/* Set active state without calling start on the driver */
209int eth_init_state_only(void)
210{
211 struct udevice *current;
212 struct eth_device_priv *priv;
213
214 current = eth_get_dev();
215 if (!current || !device_active(current))
216 return -EINVAL;
217
218 priv = current->uclass_priv;
219 priv->state = ETH_STATE_ACTIVE;
220
221 return 0;
222}
223
224/* Set passive state without calling stop on the driver */
225void eth_halt_state_only(void)
226{
227 struct udevice *current;
228 struct eth_device_priv *priv;
229
230 current = eth_get_dev();
231 if (!current || !device_active(current))
232 return;
233
234 priv = current->uclass_priv;
235 priv->state = ETH_STATE_PASSIVE;
236}
237
238int eth_get_dev_index(void)
239{
240 if (eth_get_dev())
241 return eth_get_dev()->seq;
242 return -1;
243}
244
245int eth_init(void)
246{
247 struct udevice *current;
248 struct udevice *old_current;
60304592 249 int ret = -ENODEV;
05c3e68f
JH
250
251 current = eth_get_dev();
252 if (!current) {
253 printf("No ethernet found.\n");
254 return -ENODEV;
255 }
256
257 old_current = current;
258 do {
259 debug("Trying %s\n", current->name);
260
261 if (device_active(current)) {
262 uchar env_enetaddr[6];
263 struct eth_pdata *pdata = current->platdata;
264
265 /* Sync environment with network device */
266 if (eth_getenv_enetaddr_by_index("eth", current->seq,
267 env_enetaddr))
268 memcpy(pdata->enetaddr, env_enetaddr, 6);
269 else
270 memset(pdata->enetaddr, 0, 6);
271
60304592
JH
272 ret = eth_get_ops(current)->start(current);
273 if (ret >= 0) {
05c3e68f
JH
274 struct eth_device_priv *priv =
275 current->uclass_priv;
276
277 priv->state = ETH_STATE_ACTIVE;
278 return 0;
279 }
ff819a3a 280 } else {
60304592 281 ret = eth_errno;
ff819a3a 282 }
60304592 283
05c3e68f
JH
284 debug("FAIL\n");
285
60304592
JH
286 /*
287 * If ethrotate is enabled, this will change "current",
288 * otherwise we will drop out of this while loop immediately
289 */
05c3e68f 290 eth_try_another(0);
60304592 291 /* This will ensure the new "current" attempted to probe */
05c3e68f
JH
292 current = eth_get_dev();
293 } while (old_current != current);
294
60304592 295 return ret;
05c3e68f
JH
296}
297
298void eth_halt(void)
299{
300 struct udevice *current;
301 struct eth_device_priv *priv;
302
303 current = eth_get_dev();
304 if (!current || !device_active(current))
305 return;
306
307 eth_get_ops(current)->stop(current);
308 priv = current->uclass_priv;
309 priv->state = ETH_STATE_PASSIVE;
310}
311
312int eth_send(void *packet, int length)
313{
314 struct udevice *current;
60304592 315 int ret;
05c3e68f
JH
316
317 current = eth_get_dev();
318 if (!current)
319 return -ENODEV;
320
321 if (!device_active(current))
322 return -EINVAL;
323
60304592
JH
324 ret = eth_get_ops(current)->send(current, packet, length);
325 if (ret < 0) {
326 /* We cannot completely return the error at present */
327 debug("%s: send() returned error %d\n", __func__, ret);
328 }
329 return ret;
05c3e68f
JH
330}
331
332int eth_rx(void)
333{
334 struct udevice *current;
17591405
JH
335 uchar *packet;
336 int ret;
337 int i;
05c3e68f
JH
338
339 current = eth_get_dev();
340 if (!current)
341 return -ENODEV;
342
343 if (!device_active(current))
344 return -EINVAL;
345
17591405
JH
346 /* Process up to 32 packets at one time */
347 for (i = 0; i < 32; i++) {
348 ret = eth_get_ops(current)->recv(current, &packet);
349 if (ret > 0)
350 net_process_received_packet(packet, ret);
63c9729a
JH
351 if (ret >= 0 && eth_get_ops(current)->free_pkt)
352 eth_get_ops(current)->free_pkt(current, packet, ret);
353 if (ret <= 0)
17591405
JH
354 break;
355 }
356 if (ret == -EAGAIN)
357 ret = 0;
60304592
JH
358 if (ret < 0) {
359 /* We cannot completely return the error at present */
360 debug("%s: recv() returned error %d\n", __func__, ret);
361 }
17591405 362 return ret;
05c3e68f
JH
363}
364
365static int eth_write_hwaddr(struct udevice *dev)
366{
367 struct eth_pdata *pdata = dev->platdata;
368 int ret = 0;
369
370 if (!dev || !device_active(dev))
371 return -EINVAL;
372
373 /* seq is valid since the device is active */
374 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
0adb5b76 375 if (!is_valid_ethaddr(pdata->enetaddr)) {
05c3e68f
JH
376 printf("\nError: %s address %pM illegal value\n",
377 dev->name, pdata->enetaddr);
378 return -EINVAL;
379 }
380
381 ret = eth_get_ops(dev)->write_hwaddr(dev);
382 if (ret)
383 printf("\nWarning: %s failed to set MAC address\n",
384 dev->name);
385 }
386
387 return ret;
388}
389
390int eth_initialize(void)
391{
392 int num_devices = 0;
393 struct udevice *dev;
394
395 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
396 eth_env_init();
397
398 /*
399 * Devices need to write the hwaddr even if not started so that Linux
400 * will have access to the hwaddr that u-boot stored for the device.
401 * This is accomplished by attempting to probe each device and calling
402 * their write_hwaddr() operation.
403 */
404 uclass_first_device(UCLASS_ETH, &dev);
405 if (!dev) {
406 printf("No ethernet found.\n");
407 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
408 } else {
6536b9bb
JH
409 char *ethprime = getenv("ethprime");
410 struct udevice *prime_dev = NULL;
411
412 if (ethprime)
413 prime_dev = eth_get_dev_by_name(ethprime);
414 if (prime_dev) {
415 eth_set_dev(prime_dev);
416 eth_current_changed();
417 } else {
418 eth_set_dev(NULL);
419 }
420
05c3e68f
JH
421 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
422 do {
423 if (num_devices)
424 printf(", ");
425
426 printf("eth%d: %s", dev->seq, dev->name);
427
6536b9bb
JH
428 if (ethprime && dev == prime_dev)
429 printf(" [PRIME]");
430
05c3e68f
JH
431 eth_write_hwaddr(dev);
432
433 uclass_next_device(&dev);
434 num_devices++;
435 } while (dev);
436
437 putc('\n');
438 }
439
440 return num_devices;
441}
442
443static int eth_post_bind(struct udevice *dev)
444{
445 if (strchr(dev->name, ' ')) {
446 printf("\nError: eth device name \"%s\" has a space!\n",
447 dev->name);
448 return -EINVAL;
449 }
450
451 return 0;
452}
453
454static int eth_pre_unbind(struct udevice *dev)
455{
456 /* Don't hang onto a pointer that is going away */
457 if (dev == eth_get_uclass_priv()->current)
458 eth_set_dev(NULL);
459
460 return 0;
461}
462
463static int eth_post_probe(struct udevice *dev)
464{
465 struct eth_device_priv *priv = dev->uclass_priv;
466 struct eth_pdata *pdata = dev->platdata;
467 unsigned char env_enetaddr[6];
468
469 priv->state = ETH_STATE_INIT;
470
471 /* Check if the device has a MAC address in ROM */
472 if (eth_get_ops(dev)->read_rom_hwaddr)
473 eth_get_ops(dev)->read_rom_hwaddr(dev);
474
475 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
0adb5b76
JH
476 if (!is_zero_ethaddr(env_enetaddr)) {
477 if (!is_zero_ethaddr(pdata->enetaddr) &&
05c3e68f
JH
478 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
479 printf("\nWarning: %s MAC addresses don't match:\n",
480 dev->name);
481 printf("Address in SROM is %pM\n",
482 pdata->enetaddr);
483 printf("Address in environment is %pM\n",
484 env_enetaddr);
485 }
486
487 /* Override the ROM MAC address */
488 memcpy(pdata->enetaddr, env_enetaddr, 6);
0adb5b76 489 } else if (is_valid_ethaddr(pdata->enetaddr)) {
05c3e68f
JH
490 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
491 printf("\nWarning: %s using MAC address from ROM\n",
492 dev->name);
0adb5b76 493 } else if (is_zero_ethaddr(pdata->enetaddr)) {
05c3e68f
JH
494 printf("\nError: %s address not set.\n",
495 dev->name);
496 return -EINVAL;
497 }
498
499 return 0;
500}
501
502static int eth_pre_remove(struct udevice *dev)
503{
504 eth_get_ops(dev)->stop(dev);
505
506 return 0;
507}
508
509UCLASS_DRIVER(eth) = {
510 .name = "eth",
511 .id = UCLASS_ETH,
512 .post_bind = eth_post_bind,
513 .pre_unbind = eth_pre_unbind,
514 .post_probe = eth_post_probe,
515 .pre_remove = eth_pre_remove,
516 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
517 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
e58780dc 518 .flags = DM_UC_FLAG_SEQ_ALIAS,
05c3e68f
JH
519};
520#endif
521
522#ifndef CONFIG_DM_ETH
dd35479a
BW
523/*
524 * CPU and board-specific Ethernet initializations. Aliased function
525 * signals caller to move on
526 */
527static int __def_eth_init(bd_t *bis)
528{
529 return -1;
530}
f9a109b3
PT
531int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
532int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
dd35479a 533
f85b6071 534#ifdef CONFIG_API
f85b6071
RJ
535static struct {
536 uchar data[PKTSIZE];
537 int length;
538} eth_rcv_bufs[PKTBUFSRX];
539
66c7385a 540static unsigned int eth_rcv_current, eth_rcv_last;
f85b6071
RJ
541#endif
542
f8be7d65
JH
543static struct eth_device *eth_devices;
544struct eth_device *eth_current;
c609719b 545
84eb1fba
JH
546static void eth_set_current_to_next(void)
547{
548 eth_current = eth_current->next;
549}
550
e58780dc
JH
551static void eth_set_dev(struct eth_device *dev)
552{
553 eth_current = dev;
554}
555
d7fb9bcf 556struct eth_device *eth_get_dev_by_name(const char *devname)
63ff004c
MB
557{
558 struct eth_device *dev, *target_dev;
559
7e7f903f
HR
560 BUG_ON(devname == NULL);
561
63ff004c
MB
562 if (!eth_devices)
563 return NULL;
564
565 dev = eth_devices;
566 target_dev = NULL;
567 do {
568 if (strcmp(devname, dev->name) == 0) {
569 target_dev = dev;
570 break;
571 }
572 dev = dev->next;
573 } while (dev != eth_devices);
574
575 return target_dev;
576}
577
9e56986a
AF
578struct eth_device *eth_get_dev_by_index(int index)
579{
580 struct eth_device *dev, *target_dev;
9e56986a
AF
581
582 if (!eth_devices)
583 return NULL;
584
585 dev = eth_devices;
586 target_dev = NULL;
587 do {
fea7dcae 588 if (dev->index == index) {
9e56986a
AF
589 target_dev = dev;
590 break;
591 }
592 dev = dev->next;
9e56986a
AF
593 } while (dev != eth_devices);
594
595 return target_dev;
596}
597
66c7385a 598int eth_get_dev_index(void)
c609719b 599{
66c7385a 600 if (!eth_current)
fea7dcae 601 return -1;
c609719b 602
fea7dcae 603 return eth_current->index;
c609719b
WD
604}
605
7616e785
SG
606int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
607 int eth_number)
608{
609 unsigned char env_enetaddr[6];
610 int ret = 0;
611
69376644 612 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
7616e785 613
0adb5b76
JH
614 if (!is_zero_ethaddr(env_enetaddr)) {
615 if (!is_zero_ethaddr(dev->enetaddr) &&
4c7c65af 616 memcmp(dev->enetaddr, env_enetaddr, 6)) {
7616e785 617 printf("\nWarning: %s MAC addresses don't match:\n",
ff819a3a 618 dev->name);
7616e785 619 printf("Address in SROM is %pM\n",
ff819a3a 620 dev->enetaddr);
7616e785 621 printf("Address in environment is %pM\n",
ff819a3a 622 env_enetaddr);
7616e785
SG
623 }
624
625 memcpy(dev->enetaddr, env_enetaddr, 6);
0adb5b76 626 } else if (is_valid_ethaddr(dev->enetaddr)) {
c88ef3c1
RH
627 eth_setenv_enetaddr_by_index(base_name, eth_number,
628 dev->enetaddr);
629 printf("\nWarning: %s using MAC address from net device\n",
ff819a3a 630 dev->name);
0adb5b76 631 } else if (is_zero_ethaddr(dev->enetaddr)) {
75d9a45c
PM
632 printf("\nError: %s address not set.\n",
633 dev->name);
634 return -EINVAL;
7616e785
SG
635 }
636
75d9a45c 637 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
0adb5b76 638 if (!is_valid_ethaddr(dev->enetaddr)) {
75d9a45c 639 printf("\nError: %s address %pM illegal value\n",
ff819a3a 640 dev->name, dev->enetaddr);
75d9a45c
PM
641 return -EINVAL;
642 }
460f949f 643
7616e785 644 ret = dev->write_hwaddr(dev);
75d9a45c 645 if (ret)
ff819a3a
JH
646 printf("\nWarning: %s failed to set MAC address\n",
647 dev->name);
460f949f 648 }
7616e785
SG
649
650 return ret;
651}
652
89d48367
SG
653int eth_register(struct eth_device *dev)
654{
655 struct eth_device *d;
66c7385a 656 static int index;
58c583b6 657
f6add132 658 assert(strlen(dev->name) < sizeof(dev->name));
58c583b6 659
89d48367 660 if (!eth_devices) {
ff819a3a
JH
661 eth_devices = dev;
662 eth_current = dev;
89d48367 663 eth_current_changed();
c609719b 664 } else {
66c7385a 665 for (d = eth_devices; d->next != eth_devices; d = d->next)
aba4b69d 666 ;
c609719b
WD
667 d->next = dev;
668 }
669
670 dev->state = ETH_STATE_INIT;
671 dev->next = eth_devices;
fea7dcae 672 dev->index = index++;
c609719b
WD
673
674 return 0;
675}
676
e7e982d6
VP
677int eth_unregister(struct eth_device *dev)
678{
679 struct eth_device *cur;
680
681 /* No device */
682 if (!eth_devices)
05324a48 683 return -ENODEV;
e7e982d6
VP
684
685 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
686 cur = cur->next)
687 ;
688
689 /* Device not found */
690 if (cur->next != dev)
05324a48 691 return -ENODEV;
e7e982d6
VP
692
693 cur->next = dev->next;
694
695 if (eth_devices == dev)
696 eth_devices = dev->next == eth_devices ? NULL : dev->next;
697
698 if (eth_current == dev) {
699 eth_current = eth_devices;
700 eth_current_changed();
701 }
702
703 return 0;
704}
705
d2eaec60 706int eth_initialize(void)
c609719b 707{
fea7dcae 708 int num_devices = 0;
c609719b
WD
709 eth_devices = NULL;
710 eth_current = NULL;
711
770605e4 712 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
27ee59af 713#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
d9785c14
MB
714 miiphy_init();
715#endif
5f184715
AF
716
717#ifdef CONFIG_PHYLIB
718 phy_init();
719#endif
720
84eb1fba 721 eth_env_init();
de30122b 722
8ad25bf8
BW
723 /*
724 * If board-specific initialization exists, call it.
725 * If not, call a CPU-specific one
726 */
727 if (board_eth_init != __def_eth_init) {
d2eaec60 728 if (board_eth_init(gd->bd) < 0)
8ad25bf8
BW
729 printf("Board Net Initialization Failed\n");
730 } else if (cpu_eth_init != __def_eth_init) {
d2eaec60 731 if (cpu_eth_init(gd->bd) < 0)
8ad25bf8 732 printf("CPU Net Initialization Failed\n");
ff819a3a 733 } else {
8ad25bf8 734 printf("Net Initialization Skipped\n");
ff819a3a 735 }
d9785c14 736
c609719b 737 if (!eth_devices) {
66c7385a 738 puts("No ethernet found.\n");
770605e4 739 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
c609719b
WD
740 } else {
741 struct eth_device *dev = eth_devices;
66c7385a 742 char *ethprime = getenv("ethprime");
c609719b 743
770605e4 744 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
c609719b 745 do {
fea7dcae 746 if (dev->index)
66c7385a 747 puts(", ");
c609719b
WD
748
749 printf("%s", dev->name);
750
66c7385a 751 if (ethprime && strcmp(dev->name, ethprime) == 0) {
c609719b 752 eth_current = dev;
66c7385a 753 puts(" [PRIME]");
c609719b
WD
754 }
755
1384f3bb 756 if (strchr(dev->name, ' '))
66c7385a
JH
757 puts("\nWarning: eth device name has a space!"
758 "\n");
1384f3bb 759
75d9a45c 760 eth_write_hwaddr(dev, "eth", dev->index);
c609719b 761
c609719b 762 dev = dev->next;
fea7dcae 763 num_devices++;
66c7385a 764 } while (dev != eth_devices);
c609719b 765
89d48367 766 eth_current_changed();
66c7385a 767 putc('\n');
c609719b
WD
768 }
769
fea7dcae 770 return num_devices;
c609719b
WD
771}
772
53a5c424
DU
773#ifdef CONFIG_MCAST_TFTP
774/* Multicast.
775 * mcast_addr: multicast ipaddr from which multicast Mac is made
85eb5caf 776 * join: 1=join, 0=leave.
53a5c424 777 */
049a95a7 778int eth_mcast_join(struct in_addr mcast_ip, int join)
53a5c424 779{
66c7385a 780 u8 mcast_mac[6];
85eb5caf 781 if (!eth_current || !eth_current->mcast)
53a5c424 782 return -1;
049a95a7
JH
783 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
784 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
785 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
53a5c424
DU
786 mcast_mac[2] = 0x5e;
787 mcast_mac[1] = 0x0;
788 mcast_mac[0] = 0x1;
789 return eth_current->mcast(eth_current, mcast_mac, join);
790}
791
85eb5caf
WD
792/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
793 * and this is the ethernet-crc method needed for TSEC -- and perhaps
53a5c424
DU
794 * some other adapter -- hash tables
795 */
796#define CRCPOLY_LE 0xedb88320
66c7385a 797u32 ether_crc(size_t len, unsigned char const *p)
53a5c424
DU
798{
799 int i;
800 u32 crc;
801 crc = ~0;
802 while (len--) {
803 crc ^= *p++;
804 for (i = 0; i < 8; i++)
805 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
806 }
807 /* an reverse the bits, cuz of way they arrive -- last-first */
808 crc = (crc >> 16) | (crc << 16);
809 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
810 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
811 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
812 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
813 return crc;
814}
815
816#endif
817
c609719b 818
d2eaec60 819int eth_init(void)
c609719b 820{
86848a74 821 struct eth_device *old_current, *dev;
c609719b 822
6bc11388 823 if (!eth_current) {
66c7385a 824 puts("No ethernet found.\n");
05324a48 825 return -ENODEV;
6bc11388 826 }
c609719b 827
86848a74 828 /* Sync environment with network devices */
86848a74
MF
829 dev = eth_devices;
830 do {
831 uchar env_enetaddr[6];
832
fea7dcae 833 if (eth_getenv_enetaddr_by_index("eth", dev->index,
7616e785 834 env_enetaddr))
86848a74
MF
835 memcpy(dev->enetaddr, env_enetaddr, 6);
836
86848a74
MF
837 dev = dev->next;
838 } while (dev != eth_devices);
839
c609719b
WD
840 old_current = eth_current;
841 do {
0ebf04c6 842 debug("Trying %s\n", eth_current->name);
c609719b 843
d2eaec60 844 if (eth_current->init(eth_current, gd->bd) >= 0) {
c609719b
WD
845 eth_current->state = ETH_STATE_ACTIVE;
846
505be87a 847 return 0;
c609719b 848 }
0ebf04c6 849 debug("FAIL\n");
c609719b
WD
850
851 eth_try_another(0);
852 } while (old_current != eth_current);
853
05324a48 854 return -ETIMEDOUT;
c609719b
WD
855}
856
857void eth_halt(void)
858{
859 if (!eth_current)
860 return;
861
862 eth_current->halt(eth_current);
863
864 eth_current->state = ETH_STATE_PASSIVE;
865}
866
db288a96 867int eth_send(void *packet, int length)
c609719b
WD
868{
869 if (!eth_current)
05324a48 870 return -ENODEV;
c609719b
WD
871
872 return eth_current->send(eth_current, packet, length);
873}
874
875int eth_rx(void)
876{
877 if (!eth_current)
05324a48 878 return -ENODEV;
c609719b
WD
879
880 return eth_current->recv(eth_current);
881}
05c3e68f 882#endif /* ifndef CONFIG_DM_ETH */
c609719b 883
f85b6071 884#ifdef CONFIG_API
db288a96 885static void eth_save_packet(void *packet, int length)
f85b6071 886{
db288a96 887 char *p = packet;
f85b6071
RJ
888 int i;
889
890 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
891 return;
892
893 if (PKTSIZE < length)
894 return;
895
896 for (i = 0; i < length; i++)
897 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
898
899 eth_rcv_bufs[eth_rcv_last].length = length;
900 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
901}
902
db288a96 903int eth_receive(void *packet, int length)
f85b6071 904{
db288a96 905 char *p = packet;
f85b6071
RJ
906 void *pp = push_packet;
907 int i;
908
909 if (eth_rcv_current == eth_rcv_last) {
910 push_packet = eth_save_packet;
911 eth_rx();
912 push_packet = pp;
913
914 if (eth_rcv_current == eth_rcv_last)
915 return -1;
916 }
917
46c07bcf 918 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
f85b6071
RJ
919
920 for (i = 0; i < length; i++)
921 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
922
923 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
924 return length;
925}
926#endif /* CONFIG_API */
927
84eb1fba
JH
928static void eth_current_changed(void)
929{
930 char *act = getenv("ethact");
931 /* update current ethernet name */
932 if (eth_get_dev()) {
933 if (act == NULL || strcmp(act, eth_get_name()) != 0)
934 setenv("ethact", eth_get_name());
935 }
936 /*
937 * remove the variable completely if there is no active
938 * interface
939 */
940 else if (act != NULL)
941 setenv("ethact", NULL);
942}
943
c609719b
WD
944void eth_try_another(int first_restart)
945{
05c3e68f 946 static void *first_failed;
c650e1be 947 char *ethrotate;
e1692577
MF
948
949 /*
950 * Do not rotate between network interfaces when
951 * 'ethrotate' variable is set to 'no'.
952 */
66c7385a
JH
953 ethrotate = getenv("ethrotate");
954 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
e1692577 955 return;
c609719b 956
84eb1fba 957 if (!eth_get_dev())
c609719b
WD
958 return;
959
66c7385a 960 if (first_restart)
84eb1fba 961 first_failed = eth_get_dev();
c609719b 962
84eb1fba 963 eth_set_current_to_next();
c609719b 964
89d48367 965 eth_current_changed();
a3d991bd 966
84eb1fba 967 if (first_failed == eth_get_dev())
bc0571fc 968 net_restart_wrap = 1;
c609719b
WD
969}
970
a3d991bd
WD
971void eth_set_current(void)
972{
66c7385a
JH
973 static char *act;
974 static int env_changed_id;
2f70c49e 975 int env_id;
a3d991bd 976
2f70c49e
HS
977 env_id = get_env_id();
978 if ((act == NULL) || (env_changed_id != env_id)) {
979 act = getenv("ethact");
980 env_changed_id = env_id;
981 }
6536b9bb
JH
982
983 if (act == NULL) {
984 char *ethprime = getenv("ethprime");
985 void *dev = NULL;
986
987 if (ethprime)
988 dev = eth_get_dev_by_name(ethprime);
989 if (dev)
990 eth_set_dev(dev);
991 else
992 eth_set_dev(NULL);
993 } else {
e58780dc 994 eth_set_dev(eth_get_dev_by_name(act));
6536b9bb 995 }
a3d991bd 996
89d48367 997 eth_current_changed();
a3d991bd 998}
a3d991bd 999
84eb1fba 1000const char *eth_get_name(void)
5bb226e8 1001{
84eb1fba 1002 return eth_get_dev() ? eth_get_dev()->name : "unknown";
5bb226e8 1003}