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