]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/eth.c
Remove support for non-CONFIG_NET_MULTI on PPC4xx EMAC
[people/ms/u-boot.git] / net / eth.c
CommitLineData
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
3f6e6993
MF
29#ifdef CONFIG_CMD_NET
30void 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
42int eth_getenv_enetaddr(char *name, uchar *enetaddr)
43{
44 eth_parse_enetaddr(getenv(name), enetaddr);
45 return is_valid_ether_addr(enetaddr);
46}
47
48int 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
643d1ab2 58#if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
c609719b 59
dd35479a
BW
60/*
61 * CPU and board-specific Ethernet initializations. Aliased function
62 * signals caller to move on
63 */
64static int __def_eth_init(bd_t *bis)
65{
66 return -1;
67}
f9a109b3
PT
68int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
69int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
dd35479a 70
3a473b2a
WD
71extern int mv6436x_eth_initialize(bd_t *);
72extern int mv6446x_eth_initialize(bd_t *);
c609719b 73
f85b6071
RJ
74#ifdef CONFIG_API
75extern void (*push_packet)(volatile void *, int);
76
77static struct {
78 uchar data[PKTSIZE];
79 int length;
80} eth_rcv_bufs[PKTBUFSRX];
81
82static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
83#endif
84
c609719b
WD
85static struct eth_device *eth_devices, *eth_current;
86
87struct eth_device *eth_get_dev(void)
88{
89 return eth_current;
90}
91
63ff004c
MB
92struct 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
9e56986a
AF
112struct 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
c609719b
WD
134int 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
156int eth_register(struct eth_device* dev)
157{
158 struct eth_device *d;
159
160 if (!eth_devices) {
161 eth_current = eth_devices = dev;
a3d991bd
WD
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
c609719b
WD
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
181int eth_initialize(bd_t *bis)
182{
5ca2d095
SK
183 char enetvar[32];
184 unsigned char env_enetaddr[6];
3f6e6993 185 int eth_number = 0;
c609719b
WD
186
187 eth_devices = NULL;
188 eth_current = NULL;
189
fad63407 190 show_boot_progress (64);
643d1ab2 191#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
d9785c14
MB
192 miiphy_init();
193#endif
dd35479a
BW
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);
d9785c14 198
1eac2a71 199#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
3a473b2a
WD
200 mv6436x_eth_initialize(bis);
201#endif
1eac2a71 202#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
3a473b2a 203 mv6446x_eth_initialize(bis);
c68a05fe 204#endif
c609719b
WD
205 if (!eth_devices) {
206 puts ("No ethernet found.\n");
fad63407 207 show_boot_progress (-64);
c609719b
WD
208 } else {
209 struct eth_device *dev = eth_devices;
210 char *ethprime = getenv ("ethprime");
211
fad63407 212 show_boot_progress (65);
c609719b
WD
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);
3f6e6993 225 eth_getenv_enetaddr(enetvar, env_enetaddr);
c609719b
WD
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 {
baac607c
WD
231 printf ("\nWarning: %s MAC addresses don't match:\n",
232 dev->name);
3f6e6993
MF
233 printf ("Address in SROM is %pM\n",
234 dev->enetaddr);
235 printf ("Address in environment is %pM\n",
236 env_enetaddr);
c609719b
WD
237 }
238
239 memcpy(dev->enetaddr, env_enetaddr, 6);
240 }
241
242 eth_number++;
243 dev = dev->next;
244 } while(dev != eth_devices);
245
a3d991bd
WD
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
c609719b
WD
256 putc ('\n');
257 }
258
259 return eth_number;
260}
261
262void eth_set_enetaddr(int num, char *addr) {
263 struct eth_device *dev;
264 unsigned char enetaddr[6];
c609719b 265
d4ca31c4
WD
266 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
267
c609719b
WD
268 if (!eth_devices)
269 return;
270
3f6e6993 271 eth_parse_enetaddr(addr, enetaddr);
c609719b
WD
272
273 dev = eth_devices;
274 while(num-- > 0) {
275 dev = dev->next;
276
277 if (dev == eth_devices)
278 return;
279 }
280
d4ca31c4 281 debug ( "Setting new HW address on %s\n"
3f6e6993
MF
282 "New Address is %pM\n",
283 dev->name, enetaddr);
c609719b
WD
284
285 memcpy(dev->enetaddr, enetaddr, 6);
286}
53a5c424
DU
287#ifdef CONFIG_MCAST_TFTP
288/* Multicast.
289 * mcast_addr: multicast ipaddr from which multicast Mac is made
85eb5caf 290 * join: 1=join, 0=leave.
53a5c424
DU
291 */
292int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
293{
294 u8 mcast_mac[6];
85eb5caf 295 if (!eth_current || !eth_current->mcast)
53a5c424
DU
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
85eb5caf
WD
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
53a5c424
DU
308 * some other adapter -- hash tables
309 */
310#define CRCPOLY_LE 0xedb88320
311u32 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
c609719b
WD
332
333int eth_init(bd_t *bis)
334{
335 struct eth_device* old_current;
336
6bc11388
SR
337 if (!eth_current) {
338 puts ("No ethernet found.\n");
505be87a 339 return -1;
6bc11388 340 }
c609719b
WD
341
342 old_current = eth_current;
343 do {
d4ca31c4 344 debug ("Trying %s\n", eth_current->name);
c609719b 345
422b1a01 346 if (eth_current->init(eth_current,bis) >= 0) {
c609719b
WD
347 eth_current->state = ETH_STATE_ACTIVE;
348
505be87a 349 return 0;
c609719b 350 }
d4ca31c4 351 debug ("FAIL\n");
c609719b
WD
352
353 eth_try_another(0);
354 } while (old_current != eth_current);
355
505be87a 356 return -1;
c609719b
WD
357}
358
359void 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
369int 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
377int eth_rx(void)
378{
379 if (!eth_current)
380 return -1;
381
382 return eth_current->recv(eth_current);
383}
384
f85b6071
RJ
385#ifdef CONFIG_API
386static 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
404int 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
c609719b
WD
432void eth_try_another(int first_restart)
433{
434 static struct eth_device *first_failed = NULL;
e1692577
MF
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;
c609719b
WD
444
445 if (!eth_current)
446 return;
447
baac607c 448 if (first_restart) {
c609719b
WD
449 first_failed = eth_current;
450 }
451
452 eth_current = eth_current->next;
453
a3d991bd
WD
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
baac607c 463 if (first_failed == eth_current) {
c609719b
WD
464 NetRestartWrap = 1;
465 }
466}
467
a3d991bd
WD
468#ifdef CONFIG_NET_MULTI
469void eth_set_current(void)
470{
da95427c
HS
471 static char *act = NULL;
472 static int env_changed_id = 0;
a3d991bd 473 struct eth_device* old_current;
2f70c49e 474 int env_id;
a3d991bd
WD
475
476 if (!eth_current) /* XXX no current */
477 return;
478
2f70c49e
HS
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 }
a3d991bd
WD
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
5bb226e8
WD
497char *eth_get_name (void)
498{
499 return (eth_current ? eth_current->name : "unknown");
500}
643d1ab2 501#elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
63ff004c
MB
502
503extern int at91rm9200_miiphy_initialize(bd_t *bis);
63ff004c
MB
504extern int mcf52x2_miiphy_initialize(bd_t *bis);
505extern int ns7520_miiphy_initialize(bd_t *bis);
c74b2108 506
63ff004c
MB
507
508int eth_initialize(bd_t *bis)
509{
643d1ab2 510#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
d9785c14
MB
511 miiphy_init();
512#endif
513
63ff004c
MB
514#if defined(CONFIG_AT91RM9200)
515 at91rm9200_miiphy_initialize(bis);
516#endif
63ff004c
MB
517#if defined(CONFIG_MCF52x2)
518 mcf52x2_miiphy_initialize(bis);
519#endif
25dbe98a 520#if defined(CONFIG_DRIVER_NS7520_ETHERNET)
63ff004c
MB
521 ns7520_miiphy_initialize(bis);
522#endif
523 return 0;
524}
c609719b 525#endif