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