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