blob: 16a6dcbd8ca1dc09847ab0391550282c29d81e7b [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
wdenkd4ca31c2004-01-02 14:00:00 +00002 * (C) Copyright 2001-2004
wdenkc6097192002-11-03 00:24:07 +00003 * 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>
Marian Balakowiczd9785c12005-11-30 18:06:04 +010027#include <miiphy.h>
wdenkc6097192002-11-03 00:24:07 +000028
Jon Loeliger643d1ab2007-07-09 17:45:14 -050029#if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
wdenkc6097192002-11-03 00:24:07 +000030
31#ifdef CFG_GT_6426x
32extern int gt6426x_eth_initialize(bd_t *bis);
33#endif
34
wdenk3a473b22004-01-03 00:43:19 +000035extern int au1x00_enet_initialize(bd_t*);
36extern int dc21x4x_initialize(bd_t*);
wdenk682011f2003-06-03 23:54:09 +000037extern int e1000_initialize(bd_t*);
wdenkc6097192002-11-03 00:24:07 +000038extern int eepro100_initialize(bd_t*);
wdenk3a473b22004-01-03 00:43:19 +000039extern int eth_3com_initialize(bd_t*);
40extern int fec_initialize(bd_t*);
41extern int inca_switch_initialize(bd_t*);
42extern int mpc5xxx_fec_initialize(bd_t*);
Rafal Jaworowski8993e542007-07-27 14:43:59 +020043extern int mpc512x_fec_initialize(bd_t*);
wdenk983fda82004-10-28 00:09:35 +000044extern int mpc8220_fec_initialize(bd_t*);
wdenk3a473b22004-01-03 00:43:19 +000045extern int mv6436x_eth_initialize(bd_t *);
46extern int mv6446x_eth_initialize(bd_t *);
wdenkc6097192002-11-03 00:24:07 +000047extern int natsemi_initialize(bd_t*);
48extern int ns8382x_initialize(bd_t*);
wdenkc6097192002-11-03 00:24:07 +000049extern int pcnet_initialize(bd_t*);
wdenk3e386912003-04-05 00:53:31 +000050extern int plb2800_eth_initialize(bd_t*);
wdenk3a473b22004-01-03 00:43:19 +000051extern int ppc_4xx_eth_initialize(bd_t *);
52extern int rtl8139_initialize(bd_t*);
wdenka8bd82d2004-04-18 22:03:42 +000053extern int rtl8169_initialize(bd_t*);
wdenk3a473b22004-01-03 00:43:19 +000054extern int scc_initialize(bd_t*);
wdenk7152b1d2003-09-05 23:19:14 +000055extern int skge_initialize(bd_t*);
roy zangd1927ce2006-11-02 19:08:55 +080056extern int tsi108_eth_initialize(bd_t*);
Roy Zang1f103102007-11-05 17:39:24 +080057extern int uli526x_initialize(bd_t *);
Jon Loeligerd9b94f22005-07-25 14:05:07 -050058extern int tsec_initialize(bd_t*, int, char *);
Wolfgang Denkba94a1b2006-05-30 15:56:48 +020059extern int npe_initialize(bd_t *);
Dave Liu7737d5c2006-11-03 12:11:15 -060060extern int uec_initialize(int);
Aubrey Li9fd437b2007-04-05 18:30:25 +080061extern int bfin_EMAC_initialize(bd_t *);
Haavard Skinnemoen9a24f472006-12-17 17:14:30 +010062extern int atstk1000_eth_initialize(bd_t *);
Haavard Skinnemoen6b443942007-04-14 17:11:49 +020063extern int atngw100_eth_initialize(bd_t *);
TsiChung Liew8e585f02007-06-18 13:50:13 -050064extern int mcffec_initialize(bd_t*);
TsiChungLiew777d1ab2008-01-15 14:00:25 -060065extern int mcdmafec_initialize(bd_t*);
Stelian Pop20b197c2008-01-20 19:49:21 +000066extern int at91cap9_eth_initialize(bd_t *);
wdenkc6097192002-11-03 00:24:07 +000067
Rafal Jaworowskif85b6072007-12-27 18:19:02 +010068#ifdef CONFIG_API
69extern void (*push_packet)(volatile void *, int);
70
71static struct {
72 uchar data[PKTSIZE];
73 int length;
74} eth_rcv_bufs[PKTBUFSRX];
75
76static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
77#endif
78
wdenkc6097192002-11-03 00:24:07 +000079static struct eth_device *eth_devices, *eth_current;
80
81struct eth_device *eth_get_dev(void)
82{
83 return eth_current;
84}
85
Marian Balakowicz63ff0042005-10-28 22:30:33 +020086struct eth_device *eth_get_dev_by_name(char *devname)
87{
88 struct eth_device *dev, *target_dev;
89
90 if (!eth_devices)
91 return NULL;
92
93 dev = eth_devices;
94 target_dev = NULL;
95 do {
96 if (strcmp(devname, dev->name) == 0) {
97 target_dev = dev;
98 break;
99 }
100 dev = dev->next;
101 } while (dev != eth_devices);
102
103 return target_dev;
104}
105
wdenkc6097192002-11-03 00:24:07 +0000106int eth_get_dev_index (void)
107{
108 struct eth_device *dev;
109 int num = 0;
110
111 if (!eth_devices) {
112 return (-1);
113 }
114
115 for (dev = eth_devices; dev; dev = dev->next) {
116 if (dev == eth_current)
117 break;
118 ++num;
119 }
120
121 if (dev) {
122 return (num);
123 }
124
125 return (0);
126}
127
128int eth_register(struct eth_device* dev)
129{
130 struct eth_device *d;
131
132 if (!eth_devices) {
133 eth_current = eth_devices = dev;
wdenka3d991b2004-04-15 21:48:45 +0000134#ifdef CONFIG_NET_MULTI
135 /* update current ethernet name */
136 {
137 char *act = getenv("ethact");
138 if (act == NULL || strcmp(act, eth_current->name) != 0)
139 setenv("ethact", eth_current->name);
140 }
141#endif
wdenkc6097192002-11-03 00:24:07 +0000142 } else {
143 for (d=eth_devices; d->next!=eth_devices; d=d->next);
144 d->next = dev;
145 }
146
147 dev->state = ETH_STATE_INIT;
148 dev->next = eth_devices;
149
150 return 0;
151}
152
153int eth_initialize(bd_t *bis)
154{
Shinya Kuribayashi5ca2d092007-11-19 20:27:04 +0900155 char enetvar[32];
156 unsigned char env_enetaddr[6];
wdenkc6097192002-11-03 00:24:07 +0000157 int i, eth_number = 0;
158 char *tmp, *end;
159
160 eth_devices = NULL;
161 eth_current = NULL;
162
Heiko Schocherfad63402007-07-13 09:54:17 +0200163 show_boot_progress (64);
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500164#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100165 miiphy_init();
166#endif
167
Stefan Roese1eac2a72006-11-29 15:42:37 +0100168#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
wdenk3a473b22004-01-03 00:43:19 +0000169 mv6436x_eth_initialize(bis);
170#endif
Stefan Roese1eac2a72006-11-29 15:42:37 +0100171#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
wdenk3a473b22004-01-03 00:43:19 +0000172 mv6446x_eth_initialize(bis);
173#endif
Wolfgang Denk7521af12005-10-09 01:04:33 +0200174#if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
wdenk8bde7f72003-06-27 21:31:46 +0000175 ppc_4xx_eth_initialize(bis);
wdenka3ed3992003-06-05 19:37:36 +0000176#endif
wdenk6069ff22003-02-28 00:49:47 +0000177#ifdef CONFIG_INCA_IP_SWITCH
178 inca_switch_initialize(bis);
179#endif
wdenk3e386912003-04-05 00:53:31 +0000180#ifdef CONFIG_PLB2800_ETHER
181 plb2800_eth_initialize(bis);
182#endif
wdenkbaac6072004-05-08 20:33:20 +0000183#ifdef SCC_ENET
184 scc_initialize(bis);
185#endif
wdenkbaac6072004-05-08 20:33:20 +0000186#if defined(CONFIG_MPC5xxx_FEC)
187 mpc5xxx_fec_initialize(bis);
188#endif
Rafal Jaworowski8993e542007-07-27 14:43:59 +0200189#if defined(CONFIG_MPC512x_FEC)
190 mpc512x_fec_initialize (bis);
191#endif
wdenk7680c142005-05-16 15:23:22 +0000192#if defined(CONFIG_MPC8220_FEC)
wdenk983fda82004-10-28 00:09:35 +0000193 mpc8220_fec_initialize(bis);
194#endif
wdenkbaac6072004-05-08 20:33:20 +0000195#if defined(CONFIG_SK98)
196 skge_initialize(bis);
197#endif
Kim Phillips255a35772007-05-16 16:52:19 -0500198#if defined(CONFIG_TSEC1)
199 tsec_initialize(bis, 0, CONFIG_TSEC1_NAME);
wdenk0ac6f8b2004-07-09 23:27:13 +0000200#endif
Kim Phillips255a35772007-05-16 16:52:19 -0500201#if defined(CONFIG_TSEC2)
202 tsec_initialize(bis, 1, CONFIG_TSEC2_NAME);
wdenk0ac6f8b2004-07-09 23:27:13 +0000203#endif
204#if defined(CONFIG_MPC85XX_FEC)
Jon Loeligerd9b94f22005-07-25 14:05:07 -0500205 tsec_initialize(bis, 2, CONFIG_MPC85XX_FEC_NAME);
206#else
Kim Phillips255a35772007-05-16 16:52:19 -0500207# if defined(CONFIG_TSEC3)
208 tsec_initialize(bis, 2, CONFIG_TSEC3_NAME);
Jon Loeligerd9b94f22005-07-25 14:05:07 -0500209# endif
Kim Phillips255a35772007-05-16 16:52:19 -0500210# if defined(CONFIG_TSEC4)
211 tsec_initialize(bis, 3, CONFIG_TSEC4_NAME);
Jon Loeligerd9b94f22005-07-25 14:05:07 -0500212# endif
wdenkbaac6072004-05-08 20:33:20 +0000213#endif
Dave Liu7737d5c2006-11-03 12:11:15 -0600214#if defined(CONFIG_UEC_ETH1)
215 uec_initialize(0);
216#endif
217#if defined(CONFIG_UEC_ETH2)
218 uec_initialize(1);
219#endif
Joakim Tjernlundccf21c32007-12-06 16:43:40 +0100220#if defined(CONFIG_UEC_ETH3)
221 uec_initialize(2);
222#endif
David Saada24656652008-01-15 10:40:24 +0200223#if defined(CONFIG_UEC_ETH4)
224 uec_initialize(3);
225#endif
Jon Loeligerdebb7352006-04-26 17:58:56 -0500226
Stefan Roesed96f41e2005-11-30 13:06:40 +0100227#if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
228 fec_initialize(bis);
229#endif
wdenkbaac6072004-05-08 20:33:20 +0000230#if defined(CONFIG_AU1X00)
231 au1x00_enet_initialize(bis);
232#endif
Wolfgang Denkba94a1b2006-05-30 15:56:48 +0200233#if defined(CONFIG_IXP4XX_NPE)
234 npe_initialize(bis);
235#endif
wdenk682011f2003-06-03 23:54:09 +0000236#ifdef CONFIG_E1000
237 e1000_initialize(bis);
238#endif
wdenkc6097192002-11-03 00:24:07 +0000239#ifdef CONFIG_EEPRO100
240 eepro100_initialize(bis);
241#endif
242#ifdef CONFIG_TULIP
243 dc21x4x_initialize(bis);
244#endif
wdenkc7de8292002-11-19 11:04:11 +0000245#ifdef CONFIG_3COM
246 eth_3com_initialize(bis);
247#endif
wdenkc6097192002-11-03 00:24:07 +0000248#ifdef CONFIG_PCNET
249 pcnet_initialize(bis);
250#endif
251#ifdef CFG_GT_6426x
252 gt6426x_eth_initialize(bis);
253#endif
254#ifdef CONFIG_NATSEMI
255 natsemi_initialize(bis);
256#endif
257#ifdef CONFIG_NS8382X
258 ns8382x_initialize(bis);
259#endif
roy zangd1927ce2006-11-02 19:08:55 +0800260#if defined(CONFIG_TSI108_ETH)
261 tsi108_eth_initialize(bis);
262#endif
Roy Zang1f103102007-11-05 17:39:24 +0800263#if defined(CONFIG_ULI526X)
264 uli526x_initialize(bis);
265#endif
wdenk63f34912004-01-02 15:01:32 +0000266#if defined(CONFIG_RTL8139)
267 rtl8139_initialize(bis);
268#endif
wdenka8bd82d2004-04-18 22:03:42 +0000269#if defined(CONFIG_RTL8169)
270 rtl8169_initialize(bis);
271#endif
Aubrey Li9fd437b2007-04-05 18:30:25 +0800272#if defined(CONFIG_BF537)
273 bfin_EMAC_initialize(bis);
274#endif
Haavard Skinnemoen9a24f472006-12-17 17:14:30 +0100275#if defined(CONFIG_ATSTK1000)
276 atstk1000_eth_initialize(bis);
277#endif
Haavard Skinnemoen6b443942007-04-14 17:11:49 +0200278#if defined(CONFIG_ATNGW100)
279 atngw100_eth_initialize(bis);
280#endif
TsiChungLiew2870e982007-07-05 23:29:21 -0500281#if defined(CONFIG_MCFFEC)
282 mcffec_initialize(bis);
283#endif
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600284#if defined(CONFIG_FSLDMAFEC)
285 mcdmafec_initialize(bis);
286#endif
Stelian Pop20b197c2008-01-20 19:49:21 +0000287#if defined(CONFIG_AT91CAP9)
288 at91cap9_eth_initialize(bis);
289#endif
wdenkc6097192002-11-03 00:24:07 +0000290
291 if (!eth_devices) {
292 puts ("No ethernet found.\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200293 show_boot_progress (-64);
wdenkc6097192002-11-03 00:24:07 +0000294 } else {
295 struct eth_device *dev = eth_devices;
296 char *ethprime = getenv ("ethprime");
297
Heiko Schocherfad63402007-07-13 09:54:17 +0200298 show_boot_progress (65);
wdenkc6097192002-11-03 00:24:07 +0000299 do {
300 if (eth_number)
301 puts (", ");
302
303 printf("%s", dev->name);
304
305 if (ethprime && strcmp (dev->name, ethprime) == 0) {
306 eth_current = dev;
307 puts (" [PRIME]");
308 }
309
310 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
311 tmp = getenv (enetvar);
312
313 for (i=0; i<6; i++) {
314 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
315 if (tmp)
316 tmp = (*end) ? end+1 : end;
317 }
318
319 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
320 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
321 memcmp(dev->enetaddr, env_enetaddr, 6))
322 {
wdenkbaac6072004-05-08 20:33:20 +0000323 printf ("\nWarning: %s MAC addresses don't match:\n",
324 dev->name);
325 printf ("Address in SROM is "
wdenkc6097192002-11-03 00:24:07 +0000326 "%02X:%02X:%02X:%02X:%02X:%02X\n",
327 dev->enetaddr[0], dev->enetaddr[1],
328 dev->enetaddr[2], dev->enetaddr[3],
329 dev->enetaddr[4], dev->enetaddr[5]);
wdenkbaac6072004-05-08 20:33:20 +0000330 printf ("Address in environment is "
wdenkc6097192002-11-03 00:24:07 +0000331 "%02X:%02X:%02X:%02X:%02X:%02X\n",
332 env_enetaddr[0], env_enetaddr[1],
333 env_enetaddr[2], env_enetaddr[3],
334 env_enetaddr[4], env_enetaddr[5]);
335 }
336
337 memcpy(dev->enetaddr, env_enetaddr, 6);
338 }
339
340 eth_number++;
341 dev = dev->next;
342 } while(dev != eth_devices);
343
wdenka3d991b2004-04-15 21:48:45 +0000344#ifdef CONFIG_NET_MULTI
345 /* update current ethernet name */
346 if (eth_current) {
347 char *act = getenv("ethact");
348 if (act == NULL || strcmp(act, eth_current->name) != 0)
349 setenv("ethact", eth_current->name);
350 } else
351 setenv("ethact", NULL);
352#endif
353
wdenkc6097192002-11-03 00:24:07 +0000354 putc ('\n');
355 }
356
357 return eth_number;
358}
359
360void eth_set_enetaddr(int num, char *addr) {
361 struct eth_device *dev;
362 unsigned char enetaddr[6];
363 char *end;
364 int i;
365
wdenkd4ca31c2004-01-02 14:00:00 +0000366 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
367
wdenkc6097192002-11-03 00:24:07 +0000368 if (!eth_devices)
369 return;
370
371 for (i=0; i<6; i++) {
372 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
373 if (addr)
374 addr = (*end) ? end+1 : end;
375 }
376
377 dev = eth_devices;
378 while(num-- > 0) {
379 dev = dev->next;
380
381 if (dev == eth_devices)
382 return;
383 }
384
wdenkd4ca31c2004-01-02 14:00:00 +0000385 debug ( "Setting new HW address on %s\n"
386 "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
387 dev->name,
Wolfgang Denka188b582005-09-25 17:05:57 +0200388 enetaddr[0], enetaddr[1],
389 enetaddr[2], enetaddr[3],
390 enetaddr[4], enetaddr[5]);
wdenkc6097192002-11-03 00:24:07 +0000391
392 memcpy(dev->enetaddr, enetaddr, 6);
393}
David Updegraff53a5c422007-06-11 10:41:07 -0500394#ifdef CONFIG_MCAST_TFTP
395/* Multicast.
396 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200397 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500398 */
399int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
400{
401 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200402 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500403 return -1;
404 mcast_mac[5] = htonl(mcast_ip) & 0xff;
405 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
406 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
407 mcast_mac[2] = 0x5e;
408 mcast_mac[1] = 0x0;
409 mcast_mac[0] = 0x1;
410 return eth_current->mcast(eth_current, mcast_mac, join);
411}
412
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200413/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
414 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500415 * some other adapter -- hash tables
416 */
417#define CRCPOLY_LE 0xedb88320
418u32 ether_crc (size_t len, unsigned char const *p)
419{
420 int i;
421 u32 crc;
422 crc = ~0;
423 while (len--) {
424 crc ^= *p++;
425 for (i = 0; i < 8; i++)
426 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
427 }
428 /* an reverse the bits, cuz of way they arrive -- last-first */
429 crc = (crc >> 16) | (crc << 16);
430 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
431 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
432 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
433 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
434 return crc;
435}
436
437#endif
438
wdenkc6097192002-11-03 00:24:07 +0000439
440int eth_init(bd_t *bis)
441{
442 struct eth_device* old_current;
443
Stefan Roese6bc11382008-03-04 17:40:41 +0100444 if (!eth_current) {
445 puts ("No ethernet found.\n");
Upakul Barkakaty505be872007-11-29 12:16:13 +0530446 return -1;
Stefan Roese6bc11382008-03-04 17:40:41 +0100447 }
wdenkc6097192002-11-03 00:24:07 +0000448
449 old_current = eth_current;
450 do {
wdenkd4ca31c2004-01-02 14:00:00 +0000451 debug ("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000452
Ben Warren422b1a02008-01-09 18:15:53 -0500453 if (eth_current->init(eth_current,bis) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000454 eth_current->state = ETH_STATE_ACTIVE;
455
Upakul Barkakaty505be872007-11-29 12:16:13 +0530456 return 0;
wdenkc6097192002-11-03 00:24:07 +0000457 }
wdenkd4ca31c2004-01-02 14:00:00 +0000458 debug ("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000459
460 eth_try_another(0);
461 } while (old_current != eth_current);
462
Upakul Barkakaty505be872007-11-29 12:16:13 +0530463 return -1;
wdenkc6097192002-11-03 00:24:07 +0000464}
465
466void eth_halt(void)
467{
468 if (!eth_current)
469 return;
470
471 eth_current->halt(eth_current);
472
473 eth_current->state = ETH_STATE_PASSIVE;
474}
475
476int eth_send(volatile void *packet, int length)
477{
478 if (!eth_current)
479 return -1;
480
481 return eth_current->send(eth_current, packet, length);
482}
483
484int eth_rx(void)
485{
486 if (!eth_current)
487 return -1;
488
489 return eth_current->recv(eth_current);
490}
491
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100492#ifdef CONFIG_API
493static void eth_save_packet(volatile void *packet, int length)
494{
495 volatile char *p = packet;
496 int i;
497
498 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
499 return;
500
501 if (PKTSIZE < length)
502 return;
503
504 for (i = 0; i < length; i++)
505 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
506
507 eth_rcv_bufs[eth_rcv_last].length = length;
508 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
509}
510
511int eth_receive(volatile void *packet, int length)
512{
513 volatile char *p = packet;
514 void *pp = push_packet;
515 int i;
516
517 if (eth_rcv_current == eth_rcv_last) {
518 push_packet = eth_save_packet;
519 eth_rx();
520 push_packet = pp;
521
522 if (eth_rcv_current == eth_rcv_last)
523 return -1;
524 }
525
526 if (length < eth_rcv_bufs[eth_rcv_current].length)
527 return -1;
528
529 length = eth_rcv_bufs[eth_rcv_current].length;
530
531 for (i = 0; i < length; i++)
532 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
533
534 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
535 return length;
536}
537#endif /* CONFIG_API */
538
wdenkc6097192002-11-03 00:24:07 +0000539void eth_try_another(int first_restart)
540{
541 static struct eth_device *first_failed = NULL;
Matthias Fuchse1692572008-01-17 07:45:05 +0100542 char *ethrotate;
543
544 /*
545 * Do not rotate between network interfaces when
546 * 'ethrotate' variable is set to 'no'.
547 */
548 if (((ethrotate = getenv ("ethrotate")) != NULL) &&
549 (strcmp(ethrotate, "no") == 0))
550 return;
wdenkc6097192002-11-03 00:24:07 +0000551
552 if (!eth_current)
553 return;
554
wdenkbaac6072004-05-08 20:33:20 +0000555 if (first_restart) {
wdenkc6097192002-11-03 00:24:07 +0000556 first_failed = eth_current;
557 }
558
559 eth_current = eth_current->next;
560
wdenka3d991b2004-04-15 21:48:45 +0000561#ifdef CONFIG_NET_MULTI
562 /* update current ethernet name */
563 {
564 char *act = getenv("ethact");
565 if (act == NULL || strcmp(act, eth_current->name) != 0)
566 setenv("ethact", eth_current->name);
567 }
568#endif
569
wdenkbaac6072004-05-08 20:33:20 +0000570 if (first_failed == eth_current) {
wdenkc6097192002-11-03 00:24:07 +0000571 NetRestartWrap = 1;
572 }
573}
574
wdenka3d991b2004-04-15 21:48:45 +0000575#ifdef CONFIG_NET_MULTI
576void eth_set_current(void)
577{
578 char *act;
579 struct eth_device* old_current;
580
581 if (!eth_current) /* XXX no current */
582 return;
583
584 act = getenv("ethact");
585 if (act != NULL) {
586 old_current = eth_current;
587 do {
588 if (strcmp(eth_current->name, act) == 0)
589 return;
590 eth_current = eth_current->next;
591 } while (old_current != eth_current);
592 }
593
594 setenv("ethact", eth_current->name);
595}
596#endif
597
wdenk5bb226e2003-11-17 21:14:37 +0000598char *eth_get_name (void)
599{
600 return (eth_current ? eth_current->name : "unknown");
601}
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500602#elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200603
604extern int at91rm9200_miiphy_initialize(bd_t *bis);
605extern int emac4xx_miiphy_initialize(bd_t *bis);
606extern int mcf52x2_miiphy_initialize(bd_t *bis);
607extern int ns7520_miiphy_initialize(bd_t *bis);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200608extern int dm644x_eth_miiphy_initialize(bd_t *bis);
609
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200610
611int eth_initialize(bd_t *bis)
612{
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500613#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100614 miiphy_init();
615#endif
616
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200617#if defined(CONFIG_AT91RM9200)
618 at91rm9200_miiphy_initialize(bis);
619#endif
620#if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) \
621 && !defined(CONFIG_AP1000) && !defined(CONFIG_405)
622 emac4xx_miiphy_initialize(bis);
623#endif
624#if defined(CONFIG_MCF52x2)
625 mcf52x2_miiphy_initialize(bis);
626#endif
627#if defined(CONFIG_NETARM)
628 ns7520_miiphy_initialize(bis);
629#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200630#if defined(CONFIG_DRIVER_TI_EMAC)
631 dm644x_eth_miiphy_initialize(bis);
632#endif
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200633 return 0;
634}
wdenkc6097192002-11-03 00:24:07 +0000635#endif