]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - net/eth.c
Merge branch '080202_at91rm9200dk' of git://linux-arm.org/u-boot-armdev
[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 atngw100_eth_initialize(bd_t *);
64 extern int mcffec_initialize(bd_t*);
65 extern int mcdmafec_initialize(bd_t*);
66 extern int at91cap9_eth_initialize(bd_t *);
67
68 #ifdef CONFIG_API
69 extern void (*push_packet)(volatile void *, int);
70
71 static struct {
72         uchar data[PKTSIZE];
73         int length;
74 } eth_rcv_bufs[PKTBUFSRX];
75
76 static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
77 #endif
78
79 static struct eth_device *eth_devices, *eth_current;
80
81 struct eth_device *eth_get_dev(void)
82 {
83         return eth_current;
84 }
85
86 struct 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
106 int 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
128 int eth_register(struct eth_device* dev)
129 {
130         struct eth_device *d;
131
132         if (!eth_devices) {
133                 eth_current = eth_devices = dev;
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
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
153 int eth_initialize(bd_t *bis)
154 {
155         char enetvar[32];
156         unsigned char env_enetaddr[6];
157         int i, eth_number = 0;
158         char *tmp, *end;
159
160         eth_devices = NULL;
161         eth_current = NULL;
162
163         show_boot_progress (64);
164 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
165         miiphy_init();
166 #endif
167
168 #if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
169         mv6436x_eth_initialize(bis);
170 #endif
171 #if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
172         mv6446x_eth_initialize(bis);
173 #endif
174 #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
175         ppc_4xx_eth_initialize(bis);
176 #endif
177 #ifdef CONFIG_INCA_IP_SWITCH
178         inca_switch_initialize(bis);
179 #endif
180 #ifdef CONFIG_PLB2800_ETHER
181         plb2800_eth_initialize(bis);
182 #endif
183 #ifdef SCC_ENET
184         scc_initialize(bis);
185 #endif
186 #if defined(CONFIG_MPC5xxx_FEC)
187         mpc5xxx_fec_initialize(bis);
188 #endif
189 #if defined(CONFIG_MPC512x_FEC)
190         mpc512x_fec_initialize (bis);
191 #endif
192 #if defined(CONFIG_MPC8220_FEC)
193         mpc8220_fec_initialize(bis);
194 #endif
195 #if defined(CONFIG_SK98)
196         skge_initialize(bis);
197 #endif
198 #if defined(CONFIG_TSEC1)
199         tsec_initialize(bis, 0, CONFIG_TSEC1_NAME);
200 #endif
201 #if defined(CONFIG_TSEC2)
202         tsec_initialize(bis, 1, CONFIG_TSEC2_NAME);
203 #endif
204 #if defined(CONFIG_MPC85XX_FEC)
205         tsec_initialize(bis, 2, CONFIG_MPC85XX_FEC_NAME);
206 #else
207 #    if defined(CONFIG_TSEC3)
208         tsec_initialize(bis, 2, CONFIG_TSEC3_NAME);
209 #    endif
210 #    if defined(CONFIG_TSEC4)
211         tsec_initialize(bis, 3, CONFIG_TSEC4_NAME);
212 #    endif
213 #endif
214 #if defined(CONFIG_UEC_ETH1)
215         uec_initialize(0);
216 #endif
217 #if defined(CONFIG_UEC_ETH2)
218         uec_initialize(1);
219 #endif
220 #if defined(CONFIG_UEC_ETH3)
221         uec_initialize(2);
222 #endif
223 #if defined(CONFIG_UEC_ETH4)
224         uec_initialize(3);
225 #endif
226
227 #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
228         fec_initialize(bis);
229 #endif
230 #if defined(CONFIG_AU1X00)
231         au1x00_enet_initialize(bis);
232 #endif
233 #if defined(CONFIG_IXP4XX_NPE)
234         npe_initialize(bis);
235 #endif
236 #ifdef CONFIG_E1000
237         e1000_initialize(bis);
238 #endif
239 #ifdef CONFIG_EEPRO100
240         eepro100_initialize(bis);
241 #endif
242 #ifdef CONFIG_TULIP
243         dc21x4x_initialize(bis);
244 #endif
245 #ifdef CONFIG_3COM
246         eth_3com_initialize(bis);
247 #endif
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
260 #if defined(CONFIG_TSI108_ETH)
261         tsi108_eth_initialize(bis);
262 #endif
263 #if defined(CONFIG_ULI526X)
264         uli526x_initialize(bis);
265 #endif
266 #if defined(CONFIG_RTL8139)
267         rtl8139_initialize(bis);
268 #endif
269 #if defined(CONFIG_RTL8169)
270         rtl8169_initialize(bis);
271 #endif
272 #if defined(CONFIG_BF537)
273         bfin_EMAC_initialize(bis);
274 #endif
275 #if defined(CONFIG_ATSTK1000)
276         atstk1000_eth_initialize(bis);
277 #endif
278 #if defined(CONFIG_ATNGW100)
279         atngw100_eth_initialize(bis);
280 #endif
281 #if defined(CONFIG_MCFFEC)
282         mcffec_initialize(bis);
283 #endif
284 #if defined(CONFIG_FSLDMAFEC)
285         mcdmafec_initialize(bis);
286 #endif
287 #if defined(CONFIG_AT91CAP9)
288         at91cap9_eth_initialize(bis);
289 #endif
290
291         if (!eth_devices) {
292                 puts ("No ethernet found.\n");
293                 show_boot_progress (-64);
294         } else {
295                 struct eth_device *dev = eth_devices;
296                 char *ethprime = getenv ("ethprime");
297
298                 show_boot_progress (65);
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                                 {
323                                         printf ("\nWarning: %s MAC addresses don't match:\n",
324                                                 dev->name);
325                                         printf ("Address in SROM is         "
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]);
330                                         printf ("Address in environment is  "
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
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
354                 putc ('\n');
355         }
356
357         return eth_number;
358 }
359
360 void eth_set_enetaddr(int num, char *addr) {
361         struct eth_device *dev;
362         unsigned char enetaddr[6];
363         char *end;
364         int i;
365
366         debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
367
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
385         debug ( "Setting new HW address on %s\n"
386                 "New Address is             %02X:%02X:%02X:%02X:%02X:%02X\n",
387                 dev->name,
388                 enetaddr[0], enetaddr[1],
389                 enetaddr[2], enetaddr[3],
390                 enetaddr[4], enetaddr[5]);
391
392         memcpy(dev->enetaddr, enetaddr, 6);
393 }
394 #ifdef CONFIG_MCAST_TFTP
395 /* Multicast.
396  * mcast_addr: multicast ipaddr from which multicast Mac is made
397  * join: 1=join, 0=leave.
398  */
399 int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
400 {
401  u8 mcast_mac[6];
402         if (!eth_current || !eth_current->mcast)
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
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
415  * some other adapter -- hash tables
416  */
417 #define CRCPOLY_LE 0xedb88320
418 u32 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
439
440 int eth_init(bd_t *bis)
441 {
442         struct eth_device* old_current;
443
444         if (!eth_current)
445                 return -1;
446
447         old_current = eth_current;
448         do {
449                 debug ("Trying %s\n", eth_current->name);
450
451                 if (eth_current->init(eth_current,bis) >= 0) {
452                         eth_current->state = ETH_STATE_ACTIVE;
453
454                         return 0;
455                 }
456                 debug  ("FAIL\n");
457
458                 eth_try_another(0);
459         } while (old_current != eth_current);
460
461         return -1;
462 }
463
464 void 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
474 int 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
482 int eth_rx(void)
483 {
484         if (!eth_current)
485                 return -1;
486
487         return eth_current->recv(eth_current);
488 }
489
490 #ifdef CONFIG_API
491 static 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
509 int 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
537 void eth_try_another(int first_restart)
538 {
539         static struct eth_device *first_failed = NULL;
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;
549
550         if (!eth_current)
551                 return;
552
553         if (first_restart) {
554                 first_failed = eth_current;
555         }
556
557         eth_current = eth_current->next;
558
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
568         if (first_failed == eth_current) {
569                 NetRestartWrap = 1;
570         }
571 }
572
573 #ifdef CONFIG_NET_MULTI
574 void 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
596 char *eth_get_name (void)
597 {
598         return (eth_current ? eth_current->name : "unknown");
599 }
600 #elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
601
602 extern int at91rm9200_miiphy_initialize(bd_t *bis);
603 extern int emac4xx_miiphy_initialize(bd_t *bis);
604 extern int mcf52x2_miiphy_initialize(bd_t *bis);
605 extern int ns7520_miiphy_initialize(bd_t *bis);
606 extern int dm644x_eth_miiphy_initialize(bd_t *bis);
607
608
609 int eth_initialize(bd_t *bis)
610 {
611 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
612         miiphy_init();
613 #endif
614
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);
627 #endif
628 #if defined(CONFIG_DRIVER_TI_EMAC)
629         dm644x_eth_miiphy_initialize(bis);
630 #endif
631         return 0;
632 }
633 #endif