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