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