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