]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - net/eth.c
Moved initialization of AVR32 Ethernet controllers to board_eth_init()
[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 /*
32  * CPU and board-specific Ethernet initializations.  Aliased function
33  * signals caller to move on
34  */
35 static int __def_eth_init(bd_t *bis)
36 {
37         return -1;
38 }
39 int cpu_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init")));
40 int board_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init")));
41
42 #ifdef CFG_GT_6426x
43 extern int gt6426x_eth_initialize(bd_t *bis);
44 #endif
45
46 extern int au1x00_enet_initialize(bd_t*);
47 extern int dc21x4x_initialize(bd_t*);
48 extern int e1000_initialize(bd_t*);
49 extern int eepro100_initialize(bd_t*);
50 extern int eth_3com_initialize(bd_t*);
51 extern int fec_initialize(bd_t*);
52 extern int inca_switch_initialize(bd_t*);
53 extern int mpc5xxx_fec_initialize(bd_t*);
54 extern int mpc512x_fec_initialize(bd_t*);
55 extern int mpc8220_fec_initialize(bd_t*);
56 extern int mv6436x_eth_initialize(bd_t *);
57 extern int mv6446x_eth_initialize(bd_t *);
58 extern int natsemi_initialize(bd_t*);
59 extern int ns8382x_initialize(bd_t*);
60 extern int pcnet_initialize(bd_t*);
61 extern int plb2800_eth_initialize(bd_t*);
62 extern int ppc_4xx_eth_initialize(bd_t *);
63 extern int rtl8139_initialize(bd_t*);
64 extern int rtl8169_initialize(bd_t*);
65 extern int scc_initialize(bd_t*);
66 extern int skge_initialize(bd_t*);
67 extern int tsi108_eth_initialize(bd_t*);
68 extern int uli526x_initialize(bd_t *);
69 extern int npe_initialize(bd_t *);
70 extern int uec_initialize(int);
71 extern int bfin_EMAC_initialize(bd_t *);
72 extern int greth_initialize(bd_t *);
73 extern int mcffec_initialize(bd_t*);
74 extern int mcdmafec_initialize(bd_t*);
75 extern int at91sam9_eth_initialize(bd_t *);
76
77 #ifdef CONFIG_API
78 extern void (*push_packet)(volatile void *, int);
79
80 static struct {
81         uchar data[PKTSIZE];
82         int length;
83 } eth_rcv_bufs[PKTBUFSRX];
84
85 static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
86 #endif
87
88 static struct eth_device *eth_devices, *eth_current;
89
90 struct eth_device *eth_get_dev(void)
91 {
92         return eth_current;
93 }
94
95 struct eth_device *eth_get_dev_by_name(char *devname)
96 {
97         struct eth_device *dev, *target_dev;
98
99         if (!eth_devices)
100                 return NULL;
101
102         dev = eth_devices;
103         target_dev = NULL;
104         do {
105                 if (strcmp(devname, dev->name) == 0) {
106                         target_dev = dev;
107                         break;
108                 }
109                 dev = dev->next;
110         } while (dev != eth_devices);
111
112         return target_dev;
113 }
114
115 int eth_get_dev_index (void)
116 {
117         struct eth_device *dev;
118         int num = 0;
119
120         if (!eth_devices) {
121                 return (-1);
122         }
123
124         for (dev = eth_devices; dev; dev = dev->next) {
125                 if (dev == eth_current)
126                         break;
127                 ++num;
128         }
129
130         if (dev) {
131                 return (num);
132         }
133
134         return (0);
135 }
136
137 int eth_register(struct eth_device* dev)
138 {
139         struct eth_device *d;
140
141         if (!eth_devices) {
142                 eth_current = eth_devices = dev;
143 #ifdef CONFIG_NET_MULTI
144                 /* update current ethernet name */
145                 {
146                         char *act = getenv("ethact");
147                         if (act == NULL || strcmp(act, eth_current->name) != 0)
148                                 setenv("ethact", eth_current->name);
149                 }
150 #endif
151         } else {
152                 for (d=eth_devices; d->next!=eth_devices; d=d->next);
153                 d->next = dev;
154         }
155
156         dev->state = ETH_STATE_INIT;
157         dev->next  = eth_devices;
158
159         return 0;
160 }
161
162 int eth_initialize(bd_t *bis)
163 {
164         char enetvar[32];
165         unsigned char env_enetaddr[6];
166         int i, eth_number = 0;
167         char *tmp, *end;
168
169         eth_devices = NULL;
170         eth_current = NULL;
171
172         show_boot_progress (64);
173 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
174         miiphy_init();
175 #endif
176         /* Try board-specific initialization first.  If it fails or isn't
177          * present, try the cpu-specific initialization */
178         if (board_eth_init(bis) < 0)
179                 cpu_eth_init(bis);
180
181 #if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
182         mv6436x_eth_initialize(bis);
183 #endif
184 #if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
185         mv6446x_eth_initialize(bis);
186 #endif
187 #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
188         ppc_4xx_eth_initialize(bis);
189 #endif
190 #ifdef CONFIG_INCA_IP_SWITCH
191         inca_switch_initialize(bis);
192 #endif
193 #ifdef CONFIG_PLB2800_ETHER
194         plb2800_eth_initialize(bis);
195 #endif
196 #ifdef SCC_ENET
197         scc_initialize(bis);
198 #endif
199 #if defined(CONFIG_MPC5xxx_FEC)
200         mpc5xxx_fec_initialize(bis);
201 #endif
202 #if defined(CONFIG_MPC512x_FEC)
203         mpc512x_fec_initialize (bis);
204 #endif
205 #if defined(CONFIG_MPC8220_FEC)
206         mpc8220_fec_initialize(bis);
207 #endif
208 #if defined(CONFIG_SK98)
209         skge_initialize(bis);
210 #endif
211 #if defined(CONFIG_UEC_ETH1)
212         uec_initialize(0);
213 #endif
214 #if defined(CONFIG_UEC_ETH2)
215         uec_initialize(1);
216 #endif
217 #if defined(CONFIG_UEC_ETH3)
218         uec_initialize(2);
219 #endif
220 #if defined(CONFIG_UEC_ETH4)
221         uec_initialize(3);
222 #endif
223
224 #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
225         fec_initialize(bis);
226 #endif
227 #if defined(CONFIG_AU1X00)
228         au1x00_enet_initialize(bis);
229 #endif
230 #if defined(CONFIG_IXP4XX_NPE)
231         npe_initialize(bis);
232 #endif
233 #ifdef CONFIG_E1000
234         e1000_initialize(bis);
235 #endif
236 #ifdef CONFIG_EEPRO100
237         eepro100_initialize(bis);
238 #endif
239 #ifdef CONFIG_TULIP
240         dc21x4x_initialize(bis);
241 #endif
242 #ifdef CONFIG_3COM
243         eth_3com_initialize(bis);
244 #endif
245 #ifdef CONFIG_PCNET
246         pcnet_initialize(bis);
247 #endif
248 #ifdef CFG_GT_6426x
249         gt6426x_eth_initialize(bis);
250 #endif
251 #ifdef CONFIG_NATSEMI
252         natsemi_initialize(bis);
253 #endif
254 #ifdef CONFIG_NS8382X
255         ns8382x_initialize(bis);
256 #endif
257 #if defined(CONFIG_TSI108_ETH)
258         tsi108_eth_initialize(bis);
259 #endif
260 #if defined(CONFIG_ULI526X)
261         uli526x_initialize(bis);
262 #endif
263 #if defined(CONFIG_RTL8139)
264         rtl8139_initialize(bis);
265 #endif
266 #if defined(CONFIG_RTL8169)
267         rtl8169_initialize(bis);
268 #endif
269 #if defined(CONFIG_BF537)
270         bfin_EMAC_initialize(bis);
271 #endif
272 #if defined(CONFIG_GRETH)
273         greth_initialize(bis);
274 #endif
275 #if defined(CONFIG_MCFFEC)
276         mcffec_initialize(bis);
277 #endif
278 #if defined(CONFIG_FSLDMAFEC)
279         mcdmafec_initialize(bis);
280 #endif
281 #if defined(CONFIG_AT91CAP9) || defined(CONFIG_AT91SAM9260) || \
282     defined(CONFIG_AT91SAM9263)
283         at91sam9_eth_initialize(bis);
284 #endif
285
286         if (!eth_devices) {
287                 puts ("No ethernet found.\n");
288                 show_boot_progress (-64);
289         } else {
290                 struct eth_device *dev = eth_devices;
291                 char *ethprime = getenv ("ethprime");
292
293                 show_boot_progress (65);
294                 do {
295                         if (eth_number)
296                                 puts (", ");
297
298                         printf("%s", dev->name);
299
300                         if (ethprime && strcmp (dev->name, ethprime) == 0) {
301                                 eth_current = dev;
302                                 puts (" [PRIME]");
303                         }
304
305                         sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
306                         tmp = getenv (enetvar);
307
308                         for (i=0; i<6; i++) {
309                                 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
310                                 if (tmp)
311                                         tmp = (*end) ? end+1 : end;
312                         }
313
314                         if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
315                                 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
316                                     memcmp(dev->enetaddr, env_enetaddr, 6))
317                                 {
318                                         printf ("\nWarning: %s MAC addresses don't match:\n",
319                                                 dev->name);
320                                         printf ("Address in SROM is         "
321                                                "%02X:%02X:%02X:%02X:%02X:%02X\n",
322                                                dev->enetaddr[0], dev->enetaddr[1],
323                                                dev->enetaddr[2], dev->enetaddr[3],
324                                                dev->enetaddr[4], dev->enetaddr[5]);
325                                         printf ("Address in environment is  "
326                                                "%02X:%02X:%02X:%02X:%02X:%02X\n",
327                                                env_enetaddr[0], env_enetaddr[1],
328                                                env_enetaddr[2], env_enetaddr[3],
329                                                env_enetaddr[4], env_enetaddr[5]);
330                                 }
331
332                                 memcpy(dev->enetaddr, env_enetaddr, 6);
333                         }
334
335                         eth_number++;
336                         dev = dev->next;
337                 } while(dev != eth_devices);
338
339 #ifdef CONFIG_NET_MULTI
340                 /* update current ethernet name */
341                 if (eth_current) {
342                         char *act = getenv("ethact");
343                         if (act == NULL || strcmp(act, eth_current->name) != 0)
344                                 setenv("ethact", eth_current->name);
345                 } else
346                         setenv("ethact", NULL);
347 #endif
348
349                 putc ('\n');
350         }
351
352         return eth_number;
353 }
354
355 void eth_set_enetaddr(int num, char *addr) {
356         struct eth_device *dev;
357         unsigned char enetaddr[6];
358         char *end;
359         int i;
360
361         debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
362
363         if (!eth_devices)
364                 return;
365
366         for (i=0; i<6; i++) {
367                 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
368                 if (addr)
369                         addr = (*end) ? end+1 : end;
370         }
371
372         dev = eth_devices;
373         while(num-- > 0) {
374                 dev = dev->next;
375
376                 if (dev == eth_devices)
377                         return;
378         }
379
380         debug ( "Setting new HW address on %s\n"
381                 "New Address is             %02X:%02X:%02X:%02X:%02X:%02X\n",
382                 dev->name,
383                 enetaddr[0], enetaddr[1],
384                 enetaddr[2], enetaddr[3],
385                 enetaddr[4], enetaddr[5]);
386
387         memcpy(dev->enetaddr, enetaddr, 6);
388 }
389 #ifdef CONFIG_MCAST_TFTP
390 /* Multicast.
391  * mcast_addr: multicast ipaddr from which multicast Mac is made
392  * join: 1=join, 0=leave.
393  */
394 int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
395 {
396  u8 mcast_mac[6];
397         if (!eth_current || !eth_current->mcast)
398                 return -1;
399         mcast_mac[5] = htonl(mcast_ip) & 0xff;
400         mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
401         mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
402         mcast_mac[2] = 0x5e;
403         mcast_mac[1] = 0x0;
404         mcast_mac[0] = 0x1;
405         return eth_current->mcast(eth_current, mcast_mac, join);
406 }
407
408 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
409  * and this is the ethernet-crc method needed for TSEC -- and perhaps
410  * some other adapter -- hash tables
411  */
412 #define CRCPOLY_LE 0xedb88320
413 u32 ether_crc (size_t len, unsigned char const *p)
414 {
415         int i;
416         u32 crc;
417         crc = ~0;
418         while (len--) {
419                 crc ^= *p++;
420                 for (i = 0; i < 8; i++)
421                         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
422         }
423         /* an reverse the bits, cuz of way they arrive -- last-first */
424         crc = (crc >> 16) | (crc << 16);
425         crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
426         crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
427         crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
428         crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
429         return crc;
430 }
431
432 #endif
433
434
435 int eth_init(bd_t *bis)
436 {
437         struct eth_device* old_current;
438
439         if (!eth_current) {
440                 puts ("No ethernet found.\n");
441                 return -1;
442         }
443
444         old_current = eth_current;
445         do {
446                 debug ("Trying %s\n", eth_current->name);
447
448                 if (eth_current->init(eth_current,bis) >= 0) {
449                         eth_current->state = ETH_STATE_ACTIVE;
450
451                         return 0;
452                 }
453                 debug  ("FAIL\n");
454
455                 eth_try_another(0);
456         } while (old_current != eth_current);
457
458         return -1;
459 }
460
461 void eth_halt(void)
462 {
463         if (!eth_current)
464                 return;
465
466         eth_current->halt(eth_current);
467
468         eth_current->state = ETH_STATE_PASSIVE;
469 }
470
471 int eth_send(volatile void *packet, int length)
472 {
473         if (!eth_current)
474                 return -1;
475
476         return eth_current->send(eth_current, packet, length);
477 }
478
479 int eth_rx(void)
480 {
481         if (!eth_current)
482                 return -1;
483
484         return eth_current->recv(eth_current);
485 }
486
487 #ifdef CONFIG_API
488 static void eth_save_packet(volatile void *packet, int length)
489 {
490         volatile char *p = packet;
491         int i;
492
493         if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
494                 return;
495
496         if (PKTSIZE < length)
497                 return;
498
499         for (i = 0; i < length; i++)
500                 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
501
502         eth_rcv_bufs[eth_rcv_last].length = length;
503         eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
504 }
505
506 int eth_receive(volatile void *packet, int length)
507 {
508         volatile char *p = packet;
509         void *pp = push_packet;
510         int i;
511
512         if (eth_rcv_current == eth_rcv_last) {
513                 push_packet = eth_save_packet;
514                 eth_rx();
515                 push_packet = pp;
516
517                 if (eth_rcv_current == eth_rcv_last)
518                         return -1;
519         }
520
521         if (length < eth_rcv_bufs[eth_rcv_current].length)
522                 return -1;
523
524         length = eth_rcv_bufs[eth_rcv_current].length;
525
526         for (i = 0; i < length; i++)
527                 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
528
529         eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
530         return length;
531 }
532 #endif /* CONFIG_API */
533
534 void eth_try_another(int first_restart)
535 {
536         static struct eth_device *first_failed = NULL;
537         char *ethrotate;
538
539         /*
540          * Do not rotate between network interfaces when
541          * 'ethrotate' variable is set to 'no'.
542          */
543         if (((ethrotate = getenv ("ethrotate")) != NULL) &&
544             (strcmp(ethrotate, "no") == 0))
545                 return;
546
547         if (!eth_current)
548                 return;
549
550         if (first_restart) {
551                 first_failed = eth_current;
552         }
553
554         eth_current = eth_current->next;
555
556 #ifdef CONFIG_NET_MULTI
557         /* update current ethernet name */
558         {
559                 char *act = getenv("ethact");
560                 if (act == NULL || strcmp(act, eth_current->name) != 0)
561                         setenv("ethact", eth_current->name);
562         }
563 #endif
564
565         if (first_failed == eth_current) {
566                 NetRestartWrap = 1;
567         }
568 }
569
570 #ifdef CONFIG_NET_MULTI
571 void eth_set_current(void)
572 {
573         char *act;
574         struct eth_device* old_current;
575
576         if (!eth_current)       /* XXX no current */
577                 return;
578
579         act = getenv("ethact");
580         if (act != NULL) {
581                 old_current = eth_current;
582                 do {
583                         if (strcmp(eth_current->name, act) == 0)
584                                 return;
585                         eth_current = eth_current->next;
586                 } while (old_current != eth_current);
587         }
588
589         setenv("ethact", eth_current->name);
590 }
591 #endif
592
593 char *eth_get_name (void)
594 {
595         return (eth_current ? eth_current->name : "unknown");
596 }
597 #elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
598
599 extern int at91rm9200_miiphy_initialize(bd_t *bis);
600 extern int emac4xx_miiphy_initialize(bd_t *bis);
601 extern int mcf52x2_miiphy_initialize(bd_t *bis);
602 extern int ns7520_miiphy_initialize(bd_t *bis);
603 extern int dm644x_eth_miiphy_initialize(bd_t *bis);
604
605
606 int eth_initialize(bd_t *bis)
607 {
608 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
609         miiphy_init();
610 #endif
611
612 #if defined(CONFIG_AT91RM9200)
613         at91rm9200_miiphy_initialize(bis);
614 #endif
615 #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) \
616         && !defined(CONFIG_AP1000) && !defined(CONFIG_405)
617         emac4xx_miiphy_initialize(bis);
618 #endif
619 #if defined(CONFIG_MCF52x2)
620         mcf52x2_miiphy_initialize(bis);
621 #endif
622 #if defined(CONFIG_DRIVER_NS7520_ETHERNET)
623         ns7520_miiphy_initialize(bis);
624 #endif
625 #if defined(CONFIG_DRIVER_TI_EMAC)
626         dm644x_eth_miiphy_initialize(bis);
627 #endif
628         return 0;
629 }
630 #endif