]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/eth.c
Remove erroneous or extra spd.h #includers.
[people/ms/u-boot.git] / net / eth.c
CommitLineData
c609719b 1/*
d4ca31c4 2 * (C) Copyright 2001-2004
c609719b
WD
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <command.h>
26#include <net.h>
d9785c14 27#include <miiphy.h>
c609719b 28
643d1ab2 29#if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
c609719b
WD
30
31#ifdef CFG_GT_6426x
32extern int gt6426x_eth_initialize(bd_t *bis);
33#endif
34
3a473b2a
WD
35extern int au1x00_enet_initialize(bd_t*);
36extern int dc21x4x_initialize(bd_t*);
682011ff 37extern int e1000_initialize(bd_t*);
c609719b 38extern int eepro100_initialize(bd_t*);
c7de829c 39extern int eth_3com_initialize(bd_t*);
c609719b 40extern int fec_initialize(bd_t*);
6069ff26 41extern int inca_switch_initialize(bd_t*);
945af8d7 42extern int mpc5xxx_fec_initialize(bd_t*);
8993e54b 43extern int mpc512x_fec_initialize(bd_t*);
983fda83 44extern int mpc8220_fec_initialize(bd_t*);
3a473b2a
WD
45extern int mv6436x_eth_initialize(bd_t *);
46extern int mv6446x_eth_initialize(bd_t *);
47extern int natsemi_initialize(bd_t*);
48extern int ns8382x_initialize(bd_t*);
49extern int pcnet_initialize(bd_t*);
50extern int plb2800_eth_initialize(bd_t*);
51extern int ppc_4xx_eth_initialize(bd_t *);
52extern int rtl8139_initialize(bd_t*);
a8bd82de 53extern int rtl8169_initialize(bd_t*);
3a473b2a 54extern int scc_initialize(bd_t*);
7152b1d0 55extern int skge_initialize(bd_t*);
d1927cee 56extern int tsi108_eth_initialize(bd_t*);
1f103105 57extern int uli526x_initialize(bd_t *);
d9b94f28 58extern int tsec_initialize(bd_t*, int, char *);
ba94a1bb 59extern int npe_initialize(bd_t *);
7737d5c6 60extern int uec_initialize(int);
9fd437bb 61extern int bfin_EMAC_initialize(bd_t *);
9a24f477 62extern int atstk1000_eth_initialize(bd_t *);
6b443944 63extern int atngw100_eth_initialize(bd_t *);
8e585f02 64extern int mcffec_initialize(bd_t*);
777d1abd 65extern int mcdmafec_initialize(bd_t*);
20b197c6 66extern int at91cap9_eth_initialize(bd_t *);
c609719b 67
f85b6071
RJ
68#ifdef CONFIG_API
69extern void (*push_packet)(volatile void *, int);
70
71static struct {
72 uchar data[PKTSIZE];
73 int length;
74} eth_rcv_bufs[PKTBUFSRX];
75
76static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
77#endif
78
c609719b
WD
79static struct eth_device *eth_devices, *eth_current;
80
81struct eth_device *eth_get_dev(void)
82{
83 return eth_current;
84}
85
63ff004c
MB
86struct eth_device *eth_get_dev_by_name(char *devname)
87{
88 struct eth_device *dev, *target_dev;
89
90 if (!eth_devices)
91 return NULL;
92
93 dev = eth_devices;
94 target_dev = NULL;
95 do {
96 if (strcmp(devname, dev->name) == 0) {
97 target_dev = dev;
98 break;
99 }
100 dev = dev->next;
101 } while (dev != eth_devices);
102
103 return target_dev;
104}
105
c609719b
WD
106int eth_get_dev_index (void)
107{
108 struct eth_device *dev;
109 int num = 0;
110
111 if (!eth_devices) {
112 return (-1);
113 }
114
115 for (dev = eth_devices; dev; dev = dev->next) {
116 if (dev == eth_current)
117 break;
118 ++num;
119 }
120
121 if (dev) {
122 return (num);
123 }
124
125 return (0);
126}
127
128int eth_register(struct eth_device* dev)
129{
130 struct eth_device *d;
131
132 if (!eth_devices) {
133 eth_current = eth_devices = dev;
a3d991bd
WD
134#ifdef CONFIG_NET_MULTI
135 /* update current ethernet name */
136 {
137 char *act = getenv("ethact");
138 if (act == NULL || strcmp(act, eth_current->name) != 0)
139 setenv("ethact", eth_current->name);
140 }
141#endif
c609719b
WD
142 } else {
143 for (d=eth_devices; d->next!=eth_devices; d=d->next);
144 d->next = dev;
145 }
146
147 dev->state = ETH_STATE_INIT;
148 dev->next = eth_devices;
149
150 return 0;
151}
152
153int eth_initialize(bd_t *bis)
154{
5ca2d095
SK
155 char enetvar[32];
156 unsigned char env_enetaddr[6];
c609719b
WD
157 int i, eth_number = 0;
158 char *tmp, *end;
159
160 eth_devices = NULL;
161 eth_current = NULL;
162
fad63407 163 show_boot_progress (64);
643d1ab2 164#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
d9785c14
MB
165 miiphy_init();
166#endif
167
1eac2a71 168#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
3a473b2a
WD
169 mv6436x_eth_initialize(bis);
170#endif
1eac2a71 171#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
3a473b2a
WD
172 mv6446x_eth_initialize(bis);
173#endif
7521af1c 174#if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
8bde7f77 175 ppc_4xx_eth_initialize(bis);
a3ed3996 176#endif
6069ff26
WD
177#ifdef CONFIG_INCA_IP_SWITCH
178 inca_switch_initialize(bis);
179#endif
3e38691e
WD
180#ifdef CONFIG_PLB2800_ETHER
181 plb2800_eth_initialize(bis);
182#endif
baac607c
WD
183#ifdef SCC_ENET
184 scc_initialize(bis);
185#endif
baac607c
WD
186#if defined(CONFIG_MPC5xxx_FEC)
187 mpc5xxx_fec_initialize(bis);
188#endif
8993e54b
RJ
189#if defined(CONFIG_MPC512x_FEC)
190 mpc512x_fec_initialize (bis);
191#endif
7680c140 192#if defined(CONFIG_MPC8220_FEC)
983fda83
WD
193 mpc8220_fec_initialize(bis);
194#endif
baac607c
WD
195#if defined(CONFIG_SK98)
196 skge_initialize(bis);
197#endif
255a3577
KP
198#if defined(CONFIG_TSEC1)
199 tsec_initialize(bis, 0, CONFIG_TSEC1_NAME);
0ac6f8b7 200#endif
255a3577
KP
201#if defined(CONFIG_TSEC2)
202 tsec_initialize(bis, 1, CONFIG_TSEC2_NAME);
0ac6f8b7
WD
203#endif
204#if defined(CONFIG_MPC85XX_FEC)
d9b94f28
JL
205 tsec_initialize(bis, 2, CONFIG_MPC85XX_FEC_NAME);
206#else
255a3577
KP
207# if defined(CONFIG_TSEC3)
208 tsec_initialize(bis, 2, CONFIG_TSEC3_NAME);
d9b94f28 209# endif
255a3577
KP
210# if defined(CONFIG_TSEC4)
211 tsec_initialize(bis, 3, CONFIG_TSEC4_NAME);
d9b94f28 212# endif
baac607c 213#endif
7737d5c6
DL
214#if defined(CONFIG_UEC_ETH1)
215 uec_initialize(0);
216#endif
217#if defined(CONFIG_UEC_ETH2)
218 uec_initialize(1);
219#endif
ccf21c31
JT
220#if defined(CONFIG_UEC_ETH3)
221 uec_initialize(2);
222#endif
2465665b
DS
223#if defined(CONFIG_UEC_ETH4)
224 uec_initialize(3);
225#endif
debb7354 226
d96f41e0
SR
227#if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
228 fec_initialize(bis);
229#endif
baac607c
WD
230#if defined(CONFIG_AU1X00)
231 au1x00_enet_initialize(bis);
232#endif
ba94a1bb
WD
233#if defined(CONFIG_IXP4XX_NPE)
234 npe_initialize(bis);
235#endif
682011ff
WD
236#ifdef CONFIG_E1000
237 e1000_initialize(bis);
238#endif
c609719b
WD
239#ifdef CONFIG_EEPRO100
240 eepro100_initialize(bis);
241#endif
242#ifdef CONFIG_TULIP
243 dc21x4x_initialize(bis);
244#endif
c7de829c
WD
245#ifdef CONFIG_3COM
246 eth_3com_initialize(bis);
247#endif
c609719b
WD
248#ifdef CONFIG_PCNET
249 pcnet_initialize(bis);
250#endif
251#ifdef CFG_GT_6426x
252 gt6426x_eth_initialize(bis);
253#endif
254#ifdef CONFIG_NATSEMI
255 natsemi_initialize(bis);
256#endif
257#ifdef CONFIG_NS8382X
258 ns8382x_initialize(bis);
259#endif
d1927cee 260#if defined(CONFIG_TSI108_ETH)
261 tsi108_eth_initialize(bis);
c609719b 262#endif
1f103105
RZ
263#if defined(CONFIG_ULI526X)
264 uli526x_initialize(bis);
265#endif
63f34912
WD
266#if defined(CONFIG_RTL8139)
267 rtl8139_initialize(bis);
268#endif
a8bd82de
WD
269#if defined(CONFIG_RTL8169)
270 rtl8169_initialize(bis);
271#endif
9fd437bb
AL
272#if defined(CONFIG_BF537)
273 bfin_EMAC_initialize(bis);
274#endif
9a24f477
HS
275#if defined(CONFIG_ATSTK1000)
276 atstk1000_eth_initialize(bis);
277#endif
6b443944
HS
278#if defined(CONFIG_ATNGW100)
279 atngw100_eth_initialize(bis);
280#endif
2870e98a
TL
281#if defined(CONFIG_MCFFEC)
282 mcffec_initialize(bis);
283#endif
777d1abd
TL
284#if defined(CONFIG_FSLDMAFEC)
285 mcdmafec_initialize(bis);
286#endif
20b197c6
SP
287#if defined(CONFIG_AT91CAP9)
288 at91cap9_eth_initialize(bis);
289#endif
c609719b
WD
290
291 if (!eth_devices) {
292 puts ("No ethernet found.\n");
fad63407 293 show_boot_progress (-64);
c609719b
WD
294 } else {
295 struct eth_device *dev = eth_devices;
296 char *ethprime = getenv ("ethprime");
297
fad63407 298 show_boot_progress (65);
c609719b
WD
299 do {
300 if (eth_number)
301 puts (", ");
302
303 printf("%s", dev->name);
304
305 if (ethprime && strcmp (dev->name, ethprime) == 0) {
306 eth_current = dev;
307 puts (" [PRIME]");
308 }
309
310 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
311 tmp = getenv (enetvar);
312
313 for (i=0; i<6; i++) {
314 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
315 if (tmp)
316 tmp = (*end) ? end+1 : end;
317 }
318
319 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
320 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
321 memcmp(dev->enetaddr, env_enetaddr, 6))
322 {
baac607c
WD
323 printf ("\nWarning: %s MAC addresses don't match:\n",
324 dev->name);
325 printf ("Address in SROM is "
c609719b
WD
326 "%02X:%02X:%02X:%02X:%02X:%02X\n",
327 dev->enetaddr[0], dev->enetaddr[1],
328 dev->enetaddr[2], dev->enetaddr[3],
329 dev->enetaddr[4], dev->enetaddr[5]);
baac607c 330 printf ("Address in environment is "
c609719b
WD
331 "%02X:%02X:%02X:%02X:%02X:%02X\n",
332 env_enetaddr[0], env_enetaddr[1],
333 env_enetaddr[2], env_enetaddr[3],
334 env_enetaddr[4], env_enetaddr[5]);
335 }
336
337 memcpy(dev->enetaddr, env_enetaddr, 6);
338 }
339
340 eth_number++;
341 dev = dev->next;
342 } while(dev != eth_devices);
343
a3d991bd
WD
344#ifdef CONFIG_NET_MULTI
345 /* update current ethernet name */
346 if (eth_current) {
347 char *act = getenv("ethact");
348 if (act == NULL || strcmp(act, eth_current->name) != 0)
349 setenv("ethact", eth_current->name);
350 } else
351 setenv("ethact", NULL);
352#endif
353
c609719b
WD
354 putc ('\n');
355 }
356
357 return eth_number;
358}
359
360void eth_set_enetaddr(int num, char *addr) {
361 struct eth_device *dev;
362 unsigned char enetaddr[6];
363 char *end;
364 int i;
365
d4ca31c4
WD
366 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
367
c609719b
WD
368 if (!eth_devices)
369 return;
370
371 for (i=0; i<6; i++) {
372 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
373 if (addr)
374 addr = (*end) ? end+1 : end;
375 }
376
377 dev = eth_devices;
378 while(num-- > 0) {
379 dev = dev->next;
380
381 if (dev == eth_devices)
382 return;
383 }
384
d4ca31c4
WD
385 debug ( "Setting new HW address on %s\n"
386 "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
387 dev->name,
a188b585
WD
388 enetaddr[0], enetaddr[1],
389 enetaddr[2], enetaddr[3],
390 enetaddr[4], enetaddr[5]);
c609719b
WD
391
392 memcpy(dev->enetaddr, enetaddr, 6);
393}
53a5c424
DU
394#ifdef CONFIG_MCAST_TFTP
395/* Multicast.
396 * mcast_addr: multicast ipaddr from which multicast Mac is made
85eb5caf 397 * join: 1=join, 0=leave.
53a5c424
DU
398 */
399int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
400{
401 u8 mcast_mac[6];
85eb5caf 402 if (!eth_current || !eth_current->mcast)
53a5c424
DU
403 return -1;
404 mcast_mac[5] = htonl(mcast_ip) & 0xff;
405 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
406 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
407 mcast_mac[2] = 0x5e;
408 mcast_mac[1] = 0x0;
409 mcast_mac[0] = 0x1;
410 return eth_current->mcast(eth_current, mcast_mac, join);
411}
412
85eb5caf
WD
413/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
414 * and this is the ethernet-crc method needed for TSEC -- and perhaps
53a5c424
DU
415 * some other adapter -- hash tables
416 */
417#define CRCPOLY_LE 0xedb88320
418u32 ether_crc (size_t len, unsigned char const *p)
419{
420 int i;
421 u32 crc;
422 crc = ~0;
423 while (len--) {
424 crc ^= *p++;
425 for (i = 0; i < 8; i++)
426 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
427 }
428 /* an reverse the bits, cuz of way they arrive -- last-first */
429 crc = (crc >> 16) | (crc << 16);
430 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
431 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
432 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
433 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
434 return crc;
435}
436
437#endif
438
c609719b
WD
439
440int eth_init(bd_t *bis)
441{
442 struct eth_device* old_current;
443
444 if (!eth_current)
505be87a 445 return -1;
c609719b
WD
446
447 old_current = eth_current;
448 do {
d4ca31c4 449 debug ("Trying %s\n", eth_current->name);
c609719b 450
422b1a01 451 if (eth_current->init(eth_current,bis) >= 0) {
c609719b
WD
452 eth_current->state = ETH_STATE_ACTIVE;
453
505be87a 454 return 0;
c609719b 455 }
d4ca31c4 456 debug ("FAIL\n");
c609719b
WD
457
458 eth_try_another(0);
459 } while (old_current != eth_current);
460
505be87a 461 return -1;
c609719b
WD
462}
463
464void eth_halt(void)
465{
466 if (!eth_current)
467 return;
468
469 eth_current->halt(eth_current);
470
471 eth_current->state = ETH_STATE_PASSIVE;
472}
473
474int eth_send(volatile void *packet, int length)
475{
476 if (!eth_current)
477 return -1;
478
479 return eth_current->send(eth_current, packet, length);
480}
481
482int eth_rx(void)
483{
484 if (!eth_current)
485 return -1;
486
487 return eth_current->recv(eth_current);
488}
489
f85b6071
RJ
490#ifdef CONFIG_API
491static void eth_save_packet(volatile void *packet, int length)
492{
493 volatile char *p = packet;
494 int i;
495
496 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
497 return;
498
499 if (PKTSIZE < length)
500 return;
501
502 for (i = 0; i < length; i++)
503 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
504
505 eth_rcv_bufs[eth_rcv_last].length = length;
506 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
507}
508
509int eth_receive(volatile void *packet, int length)
510{
511 volatile char *p = packet;
512 void *pp = push_packet;
513 int i;
514
515 if (eth_rcv_current == eth_rcv_last) {
516 push_packet = eth_save_packet;
517 eth_rx();
518 push_packet = pp;
519
520 if (eth_rcv_current == eth_rcv_last)
521 return -1;
522 }
523
524 if (length < eth_rcv_bufs[eth_rcv_current].length)
525 return -1;
526
527 length = eth_rcv_bufs[eth_rcv_current].length;
528
529 for (i = 0; i < length; i++)
530 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
531
532 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
533 return length;
534}
535#endif /* CONFIG_API */
536
c609719b
WD
537void eth_try_another(int first_restart)
538{
539 static struct eth_device *first_failed = NULL;
e1692577
MF
540 char *ethrotate;
541
542 /*
543 * Do not rotate between network interfaces when
544 * 'ethrotate' variable is set to 'no'.
545 */
546 if (((ethrotate = getenv ("ethrotate")) != NULL) &&
547 (strcmp(ethrotate, "no") == 0))
548 return;
c609719b
WD
549
550 if (!eth_current)
551 return;
552
baac607c 553 if (first_restart) {
c609719b
WD
554 first_failed = eth_current;
555 }
556
557 eth_current = eth_current->next;
558
a3d991bd
WD
559#ifdef CONFIG_NET_MULTI
560 /* update current ethernet name */
561 {
562 char *act = getenv("ethact");
563 if (act == NULL || strcmp(act, eth_current->name) != 0)
564 setenv("ethact", eth_current->name);
565 }
566#endif
567
baac607c 568 if (first_failed == eth_current) {
c609719b
WD
569 NetRestartWrap = 1;
570 }
571}
572
a3d991bd
WD
573#ifdef CONFIG_NET_MULTI
574void eth_set_current(void)
575{
576 char *act;
577 struct eth_device* old_current;
578
579 if (!eth_current) /* XXX no current */
580 return;
581
582 act = getenv("ethact");
583 if (act != NULL) {
584 old_current = eth_current;
585 do {
586 if (strcmp(eth_current->name, act) == 0)
587 return;
588 eth_current = eth_current->next;
589 } while (old_current != eth_current);
590 }
591
592 setenv("ethact", eth_current->name);
593}
594#endif
595
5bb226e8
WD
596char *eth_get_name (void)
597{
598 return (eth_current ? eth_current->name : "unknown");
599}
643d1ab2 600#elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
63ff004c
MB
601
602extern int at91rm9200_miiphy_initialize(bd_t *bis);
603extern int emac4xx_miiphy_initialize(bd_t *bis);
604extern int mcf52x2_miiphy_initialize(bd_t *bis);
605extern int ns7520_miiphy_initialize(bd_t *bis);
c74b2108
SK
606extern int dm644x_eth_miiphy_initialize(bd_t *bis);
607
63ff004c
MB
608
609int eth_initialize(bd_t *bis)
610{
643d1ab2 611#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
d9785c14
MB
612 miiphy_init();
613#endif
614
63ff004c
MB
615#if defined(CONFIG_AT91RM9200)
616 at91rm9200_miiphy_initialize(bis);
617#endif
618#if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) \
619 && !defined(CONFIG_AP1000) && !defined(CONFIG_405)
620 emac4xx_miiphy_initialize(bis);
621#endif
622#if defined(CONFIG_MCF52x2)
623 mcf52x2_miiphy_initialize(bis);
624#endif
625#if defined(CONFIG_NETARM)
626 ns7520_miiphy_initialize(bis);
c74b2108
SK
627#endif
628#if defined(CONFIG_DRIVER_TI_EMAC)
629 dm644x_eth_miiphy_initialize(bis);
63ff004c
MB
630#endif
631 return 0;
632}
c609719b 633#endif