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