]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - net/eth.c
Merge branch 'master' of /home/wd/git/u-boot/work
[karo-tx-uboot.git] / net / eth.c
1 /*
2  * (C) Copyright 2001-2004
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>
27 #include <miiphy.h>
28
29 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
30
31 #ifdef CFG_GT_6426x
32 extern int gt6426x_eth_initialize(bd_t *bis);
33 #endif
34
35 extern int au1x00_enet_initialize(bd_t*);
36 extern int dc21x4x_initialize(bd_t*);
37 extern int e1000_initialize(bd_t*);
38 extern int eepro100_initialize(bd_t*);
39 extern int eth_3com_initialize(bd_t*);
40 extern int fec_initialize(bd_t*);
41 extern int inca_switch_initialize(bd_t*);
42 extern int mpc5xxx_fec_initialize(bd_t*);
43 extern int mpc512x_fec_initialize(bd_t*);
44 extern int mpc8220_fec_initialize(bd_t*);
45 extern int mv6436x_eth_initialize(bd_t *);
46 extern int mv6446x_eth_initialize(bd_t *);
47 extern int natsemi_initialize(bd_t*);
48 extern int ns8382x_initialize(bd_t*);
49 extern int pcnet_initialize(bd_t*);
50 extern int plb2800_eth_initialize(bd_t*);
51 extern int ppc_4xx_eth_initialize(bd_t *);
52 extern int rtl8139_initialize(bd_t*);
53 extern int rtl8169_initialize(bd_t*);
54 extern int scc_initialize(bd_t*);
55 extern int skge_initialize(bd_t*);
56 extern int tsi108_eth_initialize(bd_t*);
57 extern int uli526x_initialize(bd_t *);
58 extern int tsec_initialize(bd_t*, int, char *);
59 extern int npe_initialize(bd_t *);
60 extern int uec_initialize(int);
61 extern int bfin_EMAC_initialize(bd_t *);
62 extern int atstk1000_eth_initialize(bd_t *);
63 extern int mcffec_initialize(bd_t*);
64
65 static struct eth_device *eth_devices, *eth_current;
66
67 struct eth_device *eth_get_dev(void)
68 {
69         return eth_current;
70 }
71
72 struct 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
92 int 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
114 int eth_register(struct eth_device* dev)
115 {
116         struct eth_device *d;
117
118         if (!eth_devices) {
119                 eth_current = eth_devices = dev;
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
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
139 int eth_initialize(bd_t *bis)
140 {
141         char enetvar[32], env_enetaddr[6];
142         int i, eth_number = 0;
143         char *tmp, *end;
144
145         eth_devices = NULL;
146         eth_current = NULL;
147
148         show_boot_progress (64);
149 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
150         miiphy_init();
151 #endif
152
153 #if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
154         mv6436x_eth_initialize(bis);
155 #endif
156 #if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
157         mv6446x_eth_initialize(bis);
158 #endif
159 #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
160         ppc_4xx_eth_initialize(bis);
161 #endif
162 #ifdef CONFIG_INCA_IP_SWITCH
163         inca_switch_initialize(bis);
164 #endif
165 #ifdef CONFIG_PLB2800_ETHER
166         plb2800_eth_initialize(bis);
167 #endif
168 #ifdef SCC_ENET
169         scc_initialize(bis);
170 #endif
171 #if defined(CONFIG_MPC5xxx_FEC)
172         mpc5xxx_fec_initialize(bis);
173 #endif
174 #if defined(CONFIG_MPC512x_FEC)
175         mpc512x_fec_initialize (bis);
176 #endif
177 #if defined(CONFIG_MPC8220_FEC)
178         mpc8220_fec_initialize(bis);
179 #endif
180 #if defined(CONFIG_SK98)
181         skge_initialize(bis);
182 #endif
183 #if defined(CONFIG_TSEC1)
184         tsec_initialize(bis, 0, CONFIG_TSEC1_NAME);
185 #endif
186 #if defined(CONFIG_TSEC2)
187         tsec_initialize(bis, 1, CONFIG_TSEC2_NAME);
188 #endif
189 #if defined(CONFIG_MPC85XX_FEC)
190         tsec_initialize(bis, 2, CONFIG_MPC85XX_FEC_NAME);
191 #else
192 #    if defined(CONFIG_TSEC3)
193         tsec_initialize(bis, 2, CONFIG_TSEC3_NAME);
194 #    endif
195 #    if defined(CONFIG_TSEC4)
196         tsec_initialize(bis, 3, CONFIG_TSEC4_NAME);
197 #    endif
198 #endif
199 #if defined(CONFIG_UEC_ETH1)
200         uec_initialize(0);
201 #endif
202 #if defined(CONFIG_UEC_ETH2)
203         uec_initialize(1);
204 #endif
205
206 #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
207         fec_initialize(bis);
208 #endif
209 #if defined(CONFIG_AU1X00)
210         au1x00_enet_initialize(bis);
211 #endif
212 #if defined(CONFIG_IXP4XX_NPE)
213         npe_initialize(bis);
214 #endif
215 #ifdef CONFIG_E1000
216         e1000_initialize(bis);
217 #endif
218 #ifdef CONFIG_EEPRO100
219         eepro100_initialize(bis);
220 #endif
221 #ifdef CONFIG_TULIP
222         dc21x4x_initialize(bis);
223 #endif
224 #ifdef CONFIG_3COM
225         eth_3com_initialize(bis);
226 #endif
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
239 #if defined(CONFIG_TSI108_ETH)
240         tsi108_eth_initialize(bis);
241 #endif
242 #if defined(CONFIG_ULI526X)
243         uli526x_initialize(bis);
244 #endif
245 #if defined(CONFIG_RTL8139)
246         rtl8139_initialize(bis);
247 #endif
248 #if defined(CONFIG_RTL8169)
249         rtl8169_initialize(bis);
250 #endif
251 #if defined(CONFIG_BF537)
252         bfin_EMAC_initialize(bis);
253 #endif
254 #if defined(CONFIG_ATSTK1000)
255         atstk1000_eth_initialize(bis);
256 #endif
257 #if defined(CONFIG_MCFFEC)
258         mcffec_initialize(bis);
259 #endif
260
261         if (!eth_devices) {
262                 puts ("No ethernet found.\n");
263                 show_boot_progress (-64);
264         } else {
265                 struct eth_device *dev = eth_devices;
266                 char *ethprime = getenv ("ethprime");
267
268                 show_boot_progress (65);
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                                 {
293                                         printf ("\nWarning: %s MAC addresses don't match:\n",
294                                                 dev->name);
295                                         printf ("Address in SROM is         "
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]);
300                                         printf ("Address in environment is  "
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
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
324                 putc ('\n');
325         }
326
327         return eth_number;
328 }
329
330 void eth_set_enetaddr(int num, char *addr) {
331         struct eth_device *dev;
332         unsigned char enetaddr[6];
333         char *end;
334         int i;
335
336         debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
337
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
355         debug ( "Setting new HW address on %s\n"
356                 "New Address is             %02X:%02X:%02X:%02X:%02X:%02X\n",
357                 dev->name,
358                 enetaddr[0], enetaddr[1],
359                 enetaddr[2], enetaddr[3],
360                 enetaddr[4], enetaddr[5]);
361
362         memcpy(dev->enetaddr, enetaddr, 6);
363 }
364 #ifdef CONFIG_MCAST_TFTP
365 /* Multicast.
366  * mcast_addr: multicast ipaddr from which multicast Mac is made
367  * join: 1=join, 0=leave.
368  */
369 int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
370 {
371  u8 mcast_mac[6];
372         if (!eth_current || !eth_current->mcast)
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
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
385  * some other adapter -- hash tables
386  */
387 #define CRCPOLY_LE 0xedb88320
388 u32 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
409
410 int 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 {
419                 debug ("Trying %s\n", eth_current->name);
420
421                 if (eth_current->init(eth_current, bis)) {
422                         eth_current->state = ETH_STATE_ACTIVE;
423
424                         return 1;
425                 }
426                 debug  ("FAIL\n");
427
428                 eth_try_another(0);
429         } while (old_current != eth_current);
430
431         return 0;
432 }
433
434 void 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
444 int 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
452 int eth_rx(void)
453 {
454         if (!eth_current)
455                 return -1;
456
457         return eth_current->recv(eth_current);
458 }
459
460 void eth_try_another(int first_restart)
461 {
462         static struct eth_device *first_failed = NULL;
463
464         if (!eth_current)
465                 return;
466
467         if (first_restart) {
468                 first_failed = eth_current;
469         }
470
471         eth_current = eth_current->next;
472
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
482         if (first_failed == eth_current) {
483                 NetRestartWrap = 1;
484         }
485 }
486
487 #ifdef CONFIG_NET_MULTI
488 void 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
510 char *eth_get_name (void)
511 {
512         return (eth_current ? eth_current->name : "unknown");
513 }
514 #elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
515
516 extern int at91rm9200_miiphy_initialize(bd_t *bis);
517 extern int emac4xx_miiphy_initialize(bd_t *bis);
518 extern int mcf52x2_miiphy_initialize(bd_t *bis);
519 extern int ns7520_miiphy_initialize(bd_t *bis);
520 extern int dm644x_eth_miiphy_initialize(bd_t *bis);
521
522
523 int eth_initialize(bd_t *bis)
524 {
525 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
526         miiphy_init();
527 #endif
528
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);
541 #endif
542 #if defined(CONFIG_DRIVER_TI_EMAC)
543         dm644x_eth_miiphy_initialize(bis);
544 #endif
545         return 0;
546 }
547 #endif