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