2 * (C) Copyright 2001-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
31 extern int gt6426x_eth_initialize(bd_t *bis);
34 extern int au1x00_enet_initialize(bd_t*);
35 extern int dc21x4x_initialize(bd_t*);
36 extern int e1000_initialize(bd_t*);
37 extern int eepro100_initialize(bd_t*);
38 extern int eth_3com_initialize(bd_t*);
39 extern int fec_initialize(bd_t*);
40 extern int inca_switch_initialize(bd_t*);
41 extern int mpc5xxx_fec_initialize(bd_t*);
42 extern int mpc8220_fec_initialize(bd_t*);
43 extern int mv6436x_eth_initialize(bd_t *);
44 extern int mv6446x_eth_initialize(bd_t *);
45 extern int natsemi_initialize(bd_t*);
46 extern int ns8382x_initialize(bd_t*);
47 extern int pcnet_initialize(bd_t*);
48 extern int plb2800_eth_initialize(bd_t*);
49 extern int ppc_4xx_eth_initialize(bd_t *);
50 extern int ppc_440x_eth_initialize(bd_t *);
51 extern int rtl8139_initialize(bd_t*);
52 extern int rtl8169_initialize(bd_t*);
53 extern int scc_initialize(bd_t*);
54 extern int skge_initialize(bd_t*);
55 extern int tsec_initialize(bd_t*, int);
57 static struct eth_device *eth_devices, *eth_current;
59 struct eth_device *eth_get_dev(void)
64 int eth_get_dev_index (void)
66 struct eth_device *dev;
73 for (dev = eth_devices; dev; dev = dev->next) {
74 if (dev == eth_current)
86 int eth_register(struct eth_device* dev)
91 eth_current = eth_devices = dev;
92 #ifdef CONFIG_NET_MULTI
93 /* update current ethernet name */
95 char *act = getenv("ethact");
96 if (act == NULL || strcmp(act, eth_current->name) != 0)
97 setenv("ethact", eth_current->name);
101 for (d=eth_devices; d->next!=eth_devices; d=d->next);
105 dev->state = ETH_STATE_INIT;
106 dev->next = eth_devices;
111 int eth_initialize(bd_t *bis)
113 unsigned char enetvar[32], env_enetaddr[6];
114 int i, eth_number = 0;
120 #ifdef CONFIG_DB64360
121 mv6436x_eth_initialize(bis);
123 #ifdef CONFIG_DB64460
124 mv6446x_eth_initialize(bis);
126 #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
127 ( defined(CONFIG_440) && !defined(CONFIG_NET_MULTI) )
128 ppc_4xx_eth_initialize(bis);
130 #if defined(CONFIG_440) && defined(CONFIG_NET_MULTI)
131 ppc_440x_eth_initialize(bis);
133 #ifdef CONFIG_INCA_IP_SWITCH
134 inca_switch_initialize(bis);
136 #ifdef CONFIG_PLB2800_ETHER
137 plb2800_eth_initialize(bis);
142 #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
145 #if defined(CONFIG_MPC5xxx_FEC)
146 mpc5xxx_fec_initialize(bis);
148 #if defined(CONFIG_MPC8220)
149 mpc8220_fec_initialize(bis);
151 #if defined(CONFIG_SK98)
152 skge_initialize(bis);
154 #if defined(CONFIG_MPC85XX_TSEC1)
155 tsec_initialize(bis, 0);
157 #if defined(CONFIG_MPC85XX_TSEC2)
158 tsec_initialize(bis, 1);
160 #if defined(CONFIG_MPC85XX_FEC)
161 tsec_initialize(bis, 2);
163 #if defined(CONFIG_AU1X00)
164 au1x00_enet_initialize(bis);
167 e1000_initialize(bis);
169 #ifdef CONFIG_EEPRO100
170 eepro100_initialize(bis);
173 dc21x4x_initialize(bis);
176 eth_3com_initialize(bis);
179 pcnet_initialize(bis);
182 gt6426x_eth_initialize(bis);
184 #ifdef CONFIG_NATSEMI
185 natsemi_initialize(bis);
187 #ifdef CONFIG_NS8382X
188 ns8382x_initialize(bis);
190 #if defined(CONFIG_RTL8139)
191 rtl8139_initialize(bis);
193 #if defined(CONFIG_RTL8169)
194 rtl8169_initialize(bis);
198 puts ("No ethernet found.\n");
200 struct eth_device *dev = eth_devices;
201 char *ethprime = getenv ("ethprime");
207 printf("%s", dev->name);
209 if (ethprime && strcmp (dev->name, ethprime) == 0) {
214 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
215 tmp = getenv (enetvar);
217 for (i=0; i<6; i++) {
218 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
220 tmp = (*end) ? end+1 : end;
223 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
224 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
225 memcmp(dev->enetaddr, env_enetaddr, 6))
227 printf ("\nWarning: %s MAC addresses don't match:\n",
229 printf ("Address in SROM is "
230 "%02X:%02X:%02X:%02X:%02X:%02X\n",
231 dev->enetaddr[0], dev->enetaddr[1],
232 dev->enetaddr[2], dev->enetaddr[3],
233 dev->enetaddr[4], dev->enetaddr[5]);
234 printf ("Address in environment is "
235 "%02X:%02X:%02X:%02X:%02X:%02X\n",
236 env_enetaddr[0], env_enetaddr[1],
237 env_enetaddr[2], env_enetaddr[3],
238 env_enetaddr[4], env_enetaddr[5]);
241 memcpy(dev->enetaddr, env_enetaddr, 6);
246 } while(dev != eth_devices);
248 #ifdef CONFIG_NET_MULTI
249 /* update current ethernet name */
251 char *act = getenv("ethact");
252 if (act == NULL || strcmp(act, eth_current->name) != 0)
253 setenv("ethact", eth_current->name);
255 setenv("ethact", NULL);
264 void eth_set_enetaddr(int num, char *addr) {
265 struct eth_device *dev;
266 unsigned char enetaddr[6];
270 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
275 for (i=0; i<6; i++) {
276 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
278 addr = (*end) ? end+1 : end;
285 if (dev == eth_devices)
289 debug ( "Setting new HW address on %s\n"
290 "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
292 dev->enetaddr[0], dev->enetaddr[1],
293 dev->enetaddr[2], dev->enetaddr[3],
294 dev->enetaddr[4], dev->enetaddr[5]);
296 memcpy(dev->enetaddr, enetaddr, 6);
299 int eth_init(bd_t *bis)
301 struct eth_device* old_current;
306 old_current = eth_current;
308 debug ("Trying %s\n", eth_current->name);
310 if (eth_current->init(eth_current, bis)) {
311 eth_current->state = ETH_STATE_ACTIVE;
318 } while (old_current != eth_current);
328 eth_current->halt(eth_current);
330 eth_current->state = ETH_STATE_PASSIVE;
333 int eth_send(volatile void *packet, int length)
338 return eth_current->send(eth_current, packet, length);
346 return eth_current->recv(eth_current);
349 void eth_try_another(int first_restart)
351 static struct eth_device *first_failed = NULL;
357 first_failed = eth_current;
360 eth_current = eth_current->next;
362 #ifdef CONFIG_NET_MULTI
363 /* update current ethernet name */
365 char *act = getenv("ethact");
366 if (act == NULL || strcmp(act, eth_current->name) != 0)
367 setenv("ethact", eth_current->name);
371 if (first_failed == eth_current) {
376 #ifdef CONFIG_NET_MULTI
377 void eth_set_current(void)
380 struct eth_device* old_current;
382 if (!eth_current) /* XXX no current */
385 act = getenv("ethact");
387 old_current = eth_current;
389 if (strcmp(eth_current->name, act) == 0)
391 eth_current = eth_current->next;
392 } while (old_current != eth_current);
395 setenv("ethact", eth_current->name);
399 char *eth_get_name (void)
401 return (eth_current ? eth_current->name : "unknown");