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