]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/eth.c
8610: Move include of config.h earlier.
[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 *);
8e585f02 63extern int mcffec_initialize(bd_t*);
c609719b
WD
64
65static struct eth_device *eth_devices, *eth_current;
66
67struct eth_device *eth_get_dev(void)
68{
69 return eth_current;
70}
71
63ff004c
MB
72struct eth_device *eth_get_dev_by_name(char *devname)
73{
74 struct eth_device *dev, *target_dev;
75
76 if (!eth_devices)
77 return NULL;
78
79 dev = eth_devices;
80 target_dev = NULL;
81 do {
82 if (strcmp(devname, dev->name) == 0) {
83 target_dev = dev;
84 break;
85 }
86 dev = dev->next;
87 } while (dev != eth_devices);
88
89 return target_dev;
90}
91
c609719b
WD
92int eth_get_dev_index (void)
93{
94 struct eth_device *dev;
95 int num = 0;
96
97 if (!eth_devices) {
98 return (-1);
99 }
100
101 for (dev = eth_devices; dev; dev = dev->next) {
102 if (dev == eth_current)
103 break;
104 ++num;
105 }
106
107 if (dev) {
108 return (num);
109 }
110
111 return (0);
112}
113
114int eth_register(struct eth_device* dev)
115{
116 struct eth_device *d;
117
118 if (!eth_devices) {
119 eth_current = eth_devices = dev;
a3d991bd
WD
120#ifdef CONFIG_NET_MULTI
121 /* update current ethernet name */
122 {
123 char *act = getenv("ethact");
124 if (act == NULL || strcmp(act, eth_current->name) != 0)
125 setenv("ethact", eth_current->name);
126 }
127#endif
c609719b
WD
128 } else {
129 for (d=eth_devices; d->next!=eth_devices; d=d->next);
130 d->next = dev;
131 }
132
133 dev->state = ETH_STATE_INIT;
134 dev->next = eth_devices;
135
136 return 0;
137}
138
139int eth_initialize(bd_t *bis)
140{
77ddac94 141 char enetvar[32], env_enetaddr[6];
c609719b
WD
142 int i, eth_number = 0;
143 char *tmp, *end;
144
145 eth_devices = NULL;
146 eth_current = NULL;
147
fad63407 148 show_boot_progress (64);
643d1ab2 149#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
d9785c14
MB
150 miiphy_init();
151#endif
152
1eac2a71 153#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
3a473b2a
WD
154 mv6436x_eth_initialize(bis);
155#endif
1eac2a71 156#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
3a473b2a
WD
157 mv6446x_eth_initialize(bis);
158#endif
7521af1c 159#if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
8bde7f77 160 ppc_4xx_eth_initialize(bis);
a3ed3996 161#endif
6069ff26
WD
162#ifdef CONFIG_INCA_IP_SWITCH
163 inca_switch_initialize(bis);
164#endif
3e38691e
WD
165#ifdef CONFIG_PLB2800_ETHER
166 plb2800_eth_initialize(bis);
167#endif
baac607c
WD
168#ifdef SCC_ENET
169 scc_initialize(bis);
170#endif
baac607c
WD
171#if defined(CONFIG_MPC5xxx_FEC)
172 mpc5xxx_fec_initialize(bis);
173#endif
8993e54b
RJ
174#if defined(CONFIG_MPC512x_FEC)
175 mpc512x_fec_initialize (bis);
176#endif
7680c140 177#if defined(CONFIG_MPC8220_FEC)
983fda83
WD
178 mpc8220_fec_initialize(bis);
179#endif
baac607c
WD
180#if defined(CONFIG_SK98)
181 skge_initialize(bis);
182#endif
255a3577
KP
183#if defined(CONFIG_TSEC1)
184 tsec_initialize(bis, 0, CONFIG_TSEC1_NAME);
0ac6f8b7 185#endif
255a3577
KP
186#if defined(CONFIG_TSEC2)
187 tsec_initialize(bis, 1, CONFIG_TSEC2_NAME);
0ac6f8b7
WD
188#endif
189#if defined(CONFIG_MPC85XX_FEC)
d9b94f28
JL
190 tsec_initialize(bis, 2, CONFIG_MPC85XX_FEC_NAME);
191#else
255a3577
KP
192# if defined(CONFIG_TSEC3)
193 tsec_initialize(bis, 2, CONFIG_TSEC3_NAME);
d9b94f28 194# endif
255a3577
KP
195# if defined(CONFIG_TSEC4)
196 tsec_initialize(bis, 3, CONFIG_TSEC4_NAME);
d9b94f28 197# endif
baac607c 198#endif
7737d5c6
DL
199#if defined(CONFIG_UEC_ETH1)
200 uec_initialize(0);
201#endif
202#if defined(CONFIG_UEC_ETH2)
203 uec_initialize(1);
204#endif
debb7354 205
d96f41e0
SR
206#if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
207 fec_initialize(bis);
208#endif
baac607c
WD
209#if defined(CONFIG_AU1X00)
210 au1x00_enet_initialize(bis);
211#endif
ba94a1bb
WD
212#if defined(CONFIG_IXP4XX_NPE)
213 npe_initialize(bis);
214#endif
682011ff
WD
215#ifdef CONFIG_E1000
216 e1000_initialize(bis);
217#endif
c609719b
WD
218#ifdef CONFIG_EEPRO100
219 eepro100_initialize(bis);
220#endif
221#ifdef CONFIG_TULIP
222 dc21x4x_initialize(bis);
223#endif
c7de829c
WD
224#ifdef CONFIG_3COM
225 eth_3com_initialize(bis);
226#endif
c609719b
WD
227#ifdef CONFIG_PCNET
228 pcnet_initialize(bis);
229#endif
230#ifdef CFG_GT_6426x
231 gt6426x_eth_initialize(bis);
232#endif
233#ifdef CONFIG_NATSEMI
234 natsemi_initialize(bis);
235#endif
236#ifdef CONFIG_NS8382X
237 ns8382x_initialize(bis);
238#endif
d1927cee 239#if defined(CONFIG_TSI108_ETH)
240 tsi108_eth_initialize(bis);
c609719b 241#endif
1f103105
RZ
242#if defined(CONFIG_ULI526X)
243 uli526x_initialize(bis);
244#endif
63f34912
WD
245#if defined(CONFIG_RTL8139)
246 rtl8139_initialize(bis);
247#endif
a8bd82de
WD
248#if defined(CONFIG_RTL8169)
249 rtl8169_initialize(bis);
250#endif
9fd437bb
AL
251#if defined(CONFIG_BF537)
252 bfin_EMAC_initialize(bis);
253#endif
9a24f477
HS
254#if defined(CONFIG_ATSTK1000)
255 atstk1000_eth_initialize(bis);
256#endif
2870e98a
TL
257#if defined(CONFIG_MCFFEC)
258 mcffec_initialize(bis);
259#endif
c609719b
WD
260
261 if (!eth_devices) {
262 puts ("No ethernet found.\n");
fad63407 263 show_boot_progress (-64);
c609719b
WD
264 } else {
265 struct eth_device *dev = eth_devices;
266 char *ethprime = getenv ("ethprime");
267
fad63407 268 show_boot_progress (65);
c609719b
WD
269 do {
270 if (eth_number)
271 puts (", ");
272
273 printf("%s", dev->name);
274
275 if (ethprime && strcmp (dev->name, ethprime) == 0) {
276 eth_current = dev;
277 puts (" [PRIME]");
278 }
279
280 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
281 tmp = getenv (enetvar);
282
283 for (i=0; i<6; i++) {
284 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
285 if (tmp)
286 tmp = (*end) ? end+1 : end;
287 }
288
289 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
290 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
291 memcmp(dev->enetaddr, env_enetaddr, 6))
292 {
baac607c
WD
293 printf ("\nWarning: %s MAC addresses don't match:\n",
294 dev->name);
295 printf ("Address in SROM is "
c609719b
WD
296 "%02X:%02X:%02X:%02X:%02X:%02X\n",
297 dev->enetaddr[0], dev->enetaddr[1],
298 dev->enetaddr[2], dev->enetaddr[3],
299 dev->enetaddr[4], dev->enetaddr[5]);
baac607c 300 printf ("Address in environment is "
c609719b
WD
301 "%02X:%02X:%02X:%02X:%02X:%02X\n",
302 env_enetaddr[0], env_enetaddr[1],
303 env_enetaddr[2], env_enetaddr[3],
304 env_enetaddr[4], env_enetaddr[5]);
305 }
306
307 memcpy(dev->enetaddr, env_enetaddr, 6);
308 }
309
310 eth_number++;
311 dev = dev->next;
312 } while(dev != eth_devices);
313
a3d991bd
WD
314#ifdef CONFIG_NET_MULTI
315 /* update current ethernet name */
316 if (eth_current) {
317 char *act = getenv("ethact");
318 if (act == NULL || strcmp(act, eth_current->name) != 0)
319 setenv("ethact", eth_current->name);
320 } else
321 setenv("ethact", NULL);
322#endif
323
c609719b
WD
324 putc ('\n');
325 }
326
327 return eth_number;
328}
329
330void eth_set_enetaddr(int num, char *addr) {
331 struct eth_device *dev;
332 unsigned char enetaddr[6];
333 char *end;
334 int i;
335
d4ca31c4
WD
336 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
337
c609719b
WD
338 if (!eth_devices)
339 return;
340
341 for (i=0; i<6; i++) {
342 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
343 if (addr)
344 addr = (*end) ? end+1 : end;
345 }
346
347 dev = eth_devices;
348 while(num-- > 0) {
349 dev = dev->next;
350
351 if (dev == eth_devices)
352 return;
353 }
354
d4ca31c4
WD
355 debug ( "Setting new HW address on %s\n"
356 "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
357 dev->name,
a188b585
WD
358 enetaddr[0], enetaddr[1],
359 enetaddr[2], enetaddr[3],
360 enetaddr[4], enetaddr[5]);
c609719b
WD
361
362 memcpy(dev->enetaddr, enetaddr, 6);
363}
53a5c424
DU
364#ifdef CONFIG_MCAST_TFTP
365/* Multicast.
366 * mcast_addr: multicast ipaddr from which multicast Mac is made
85eb5caf 367 * join: 1=join, 0=leave.
53a5c424
DU
368 */
369int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
370{
371 u8 mcast_mac[6];
85eb5caf 372 if (!eth_current || !eth_current->mcast)
53a5c424
DU
373 return -1;
374 mcast_mac[5] = htonl(mcast_ip) & 0xff;
375 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
376 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
377 mcast_mac[2] = 0x5e;
378 mcast_mac[1] = 0x0;
379 mcast_mac[0] = 0x1;
380 return eth_current->mcast(eth_current, mcast_mac, join);
381}
382
85eb5caf
WD
383/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
384 * and this is the ethernet-crc method needed for TSEC -- and perhaps
53a5c424
DU
385 * some other adapter -- hash tables
386 */
387#define CRCPOLY_LE 0xedb88320
388u32 ether_crc (size_t len, unsigned char const *p)
389{
390 int i;
391 u32 crc;
392 crc = ~0;
393 while (len--) {
394 crc ^= *p++;
395 for (i = 0; i < 8; i++)
396 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
397 }
398 /* an reverse the bits, cuz of way they arrive -- last-first */
399 crc = (crc >> 16) | (crc << 16);
400 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
401 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
402 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
403 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
404 return crc;
405}
406
407#endif
408
c609719b
WD
409
410int eth_init(bd_t *bis)
411{
412 struct eth_device* old_current;
413
414 if (!eth_current)
415 return 0;
416
417 old_current = eth_current;
418 do {
d4ca31c4 419 debug ("Trying %s\n", eth_current->name);
c609719b
WD
420
421 if (eth_current->init(eth_current, bis)) {
422 eth_current->state = ETH_STATE_ACTIVE;
423
c609719b
WD
424 return 1;
425 }
d4ca31c4 426 debug ("FAIL\n");
c609719b
WD
427
428 eth_try_another(0);
429 } while (old_current != eth_current);
430
431 return 0;
432}
433
434void eth_halt(void)
435{
436 if (!eth_current)
437 return;
438
439 eth_current->halt(eth_current);
440
441 eth_current->state = ETH_STATE_PASSIVE;
442}
443
444int eth_send(volatile void *packet, int length)
445{
446 if (!eth_current)
447 return -1;
448
449 return eth_current->send(eth_current, packet, length);
450}
451
452int eth_rx(void)
453{
454 if (!eth_current)
455 return -1;
456
457 return eth_current->recv(eth_current);
458}
459
460void eth_try_another(int first_restart)
461{
462 static struct eth_device *first_failed = NULL;
463
464 if (!eth_current)
465 return;
466
baac607c 467 if (first_restart) {
c609719b
WD
468 first_failed = eth_current;
469 }
470
471 eth_current = eth_current->next;
472
a3d991bd
WD
473#ifdef CONFIG_NET_MULTI
474 /* update current ethernet name */
475 {
476 char *act = getenv("ethact");
477 if (act == NULL || strcmp(act, eth_current->name) != 0)
478 setenv("ethact", eth_current->name);
479 }
480#endif
481
baac607c 482 if (first_failed == eth_current) {
c609719b
WD
483 NetRestartWrap = 1;
484 }
485}
486
a3d991bd
WD
487#ifdef CONFIG_NET_MULTI
488void eth_set_current(void)
489{
490 char *act;
491 struct eth_device* old_current;
492
493 if (!eth_current) /* XXX no current */
494 return;
495
496 act = getenv("ethact");
497 if (act != NULL) {
498 old_current = eth_current;
499 do {
500 if (strcmp(eth_current->name, act) == 0)
501 return;
502 eth_current = eth_current->next;
503 } while (old_current != eth_current);
504 }
505
506 setenv("ethact", eth_current->name);
507}
508#endif
509
5bb226e8
WD
510char *eth_get_name (void)
511{
512 return (eth_current ? eth_current->name : "unknown");
513}
643d1ab2 514#elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
63ff004c
MB
515
516extern int at91rm9200_miiphy_initialize(bd_t *bis);
517extern int emac4xx_miiphy_initialize(bd_t *bis);
518extern int mcf52x2_miiphy_initialize(bd_t *bis);
519extern int ns7520_miiphy_initialize(bd_t *bis);
c74b2108
SK
520extern int dm644x_eth_miiphy_initialize(bd_t *bis);
521
63ff004c
MB
522
523int eth_initialize(bd_t *bis)
524{
643d1ab2 525#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
d9785c14
MB
526 miiphy_init();
527#endif
528
63ff004c
MB
529#if defined(CONFIG_AT91RM9200)
530 at91rm9200_miiphy_initialize(bis);
531#endif
532#if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) \
533 && !defined(CONFIG_AP1000) && !defined(CONFIG_405)
534 emac4xx_miiphy_initialize(bis);
535#endif
536#if defined(CONFIG_MCF52x2)
537 mcf52x2_miiphy_initialize(bis);
538#endif
539#if defined(CONFIG_NETARM)
540 ns7520_miiphy_initialize(bis);
c74b2108
SK
541#endif
542#if defined(CONFIG_DRIVER_TI_EMAC)
543 dm644x_eth_miiphy_initialize(bis);
63ff004c
MB
544#endif
545 return 0;
546}
c609719b 547#endif