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