blob: e60098f5d04e518b48d051de55ecb3767b6ae262 [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>
27
28#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
29
30#ifdef CFG_GT_6426x
31extern int gt6426x_eth_initialize(bd_t *bis);
32#endif
33
wdenk682011f2003-06-03 23:54:09 +000034extern int e1000_initialize(bd_t*);
wdenkc6097192002-11-03 00:24:07 +000035extern int eepro100_initialize(bd_t*);
36extern int natsemi_initialize(bd_t*);
37extern int ns8382x_initialize(bd_t*);
38extern int dc21x4x_initialize(bd_t*);
wdenkc7de8292002-11-19 11:04:11 +000039extern int eth_3com_initialize(bd_t*);
wdenkc6097192002-11-03 00:24:07 +000040extern int pcnet_initialize(bd_t*);
41extern int fec_initialize(bd_t*);
42extern int scc_initialize(bd_t*);
wdenk6069ff22003-02-28 00:49:47 +000043extern int inca_switch_initialize(bd_t*);
wdenka3ed3992003-06-05 19:37:36 +000044extern int ppc_4xx_eth_initialize(bd_t *);
wdenk3e386912003-04-05 00:53:31 +000045extern int plb2800_eth_initialize(bd_t*);
wdenk945af8d2003-07-16 21:53:01 +000046extern int mpc5xxx_fec_initialize(bd_t*);
wdenk7152b1d2003-09-05 23:19:14 +000047extern int skge_initialize(bd_t*);
wdenk42d1f032003-10-15 23:53:47 +000048extern int tsec_initialize(bd_t*);
wdenk5da627a2003-10-09 20:09:04 +000049extern int au1x00_enet_initialize(bd_t*);
wdenk63f34912004-01-02 15:01:32 +000050extern int rtl8139_initialize(bd_t*);
wdenkc6097192002-11-03 00:24:07 +000051
52static struct eth_device *eth_devices, *eth_current;
53
54struct eth_device *eth_get_dev(void)
55{
56 return eth_current;
57}
58
59int eth_get_dev_index (void)
60{
61 struct eth_device *dev;
62 int num = 0;
63
64 if (!eth_devices) {
65 return (-1);
66 }
67
68 for (dev = eth_devices; dev; dev = dev->next) {
69 if (dev == eth_current)
70 break;
71 ++num;
72 }
73
74 if (dev) {
75 return (num);
76 }
77
78 return (0);
79}
80
81int eth_register(struct eth_device* dev)
82{
83 struct eth_device *d;
84
85 if (!eth_devices) {
86 eth_current = eth_devices = dev;
87 } else {
88 for (d=eth_devices; d->next!=eth_devices; d=d->next);
89 d->next = dev;
90 }
91
92 dev->state = ETH_STATE_INIT;
93 dev->next = eth_devices;
94
95 return 0;
96}
97
98int eth_initialize(bd_t *bis)
99{
100 unsigned char enetvar[32], env_enetaddr[6];
101 int i, eth_number = 0;
102 char *tmp, *end;
103
104 eth_devices = NULL;
105 eth_current = NULL;
106
wdenk71f95112003-06-15 22:40:42 +0000107#if defined(CONFIG_405GP) || defined(CONFIG_440) || defined(CONFIG_405EP)
wdenk8bde7f72003-06-27 21:31:46 +0000108 ppc_4xx_eth_initialize(bis);
wdenka3ed3992003-06-05 19:37:36 +0000109#endif
wdenk6069ff22003-02-28 00:49:47 +0000110#ifdef CONFIG_INCA_IP_SWITCH
111 inca_switch_initialize(bis);
112#endif
wdenk3e386912003-04-05 00:53:31 +0000113#ifdef CONFIG_PLB2800_ETHER
114 plb2800_eth_initialize(bis);
115#endif
wdenk682011f2003-06-03 23:54:09 +0000116#ifdef CONFIG_E1000
117 e1000_initialize(bis);
118#endif
wdenkc6097192002-11-03 00:24:07 +0000119#ifdef CONFIG_EEPRO100
120 eepro100_initialize(bis);
121#endif
122#ifdef CONFIG_TULIP
123 dc21x4x_initialize(bis);
124#endif
wdenkc7de8292002-11-19 11:04:11 +0000125#ifdef CONFIG_3COM
126 eth_3com_initialize(bis);
127#endif
wdenkc6097192002-11-03 00:24:07 +0000128#ifdef CONFIG_PCNET
129 pcnet_initialize(bis);
130#endif
131#ifdef CFG_GT_6426x
132 gt6426x_eth_initialize(bis);
133#endif
134#ifdef CONFIG_NATSEMI
135 natsemi_initialize(bis);
136#endif
137#ifdef CONFIG_NS8382X
138 ns8382x_initialize(bis);
139#endif
140#ifdef SCC_ENET
141 scc_initialize(bis);
142#endif
wdenkaacf9a42003-01-17 16:27:01 +0000143#if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
wdenkc6097192002-11-03 00:24:07 +0000144 fec_initialize(bis);
145#endif
wdenk945af8d2003-07-16 21:53:01 +0000146#if defined(CONFIG_MPC5XXX_FEC)
147 mpc5xxx_fec_initialize(bis);
148#endif
wdenk7152b1d2003-09-05 23:19:14 +0000149#if defined(CONFIG_SK98)
150 skge_initialize(bis);
151#endif
wdenk42d1f032003-10-15 23:53:47 +0000152#ifdef CONFIG_TSEC_ENET
153 tsec_initialize(bis);
154#endif
wdenk5da627a2003-10-09 20:09:04 +0000155#if defined(CONFIG_AU1X00)
156 au1x00_enet_initialize(bis);
157#endif
wdenk63f34912004-01-02 15:01:32 +0000158#if defined(CONFIG_RTL8139)
159 rtl8139_initialize(bis);
160#endif
wdenkc6097192002-11-03 00:24:07 +0000161
162 if (!eth_devices) {
163 puts ("No ethernet found.\n");
164 } else {
165 struct eth_device *dev = eth_devices;
166 char *ethprime = getenv ("ethprime");
167
168 do {
169 if (eth_number)
170 puts (", ");
171
172 printf("%s", dev->name);
173
174 if (ethprime && strcmp (dev->name, ethprime) == 0) {
175 eth_current = dev;
176 puts (" [PRIME]");
177 }
178
179 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
180 tmp = getenv (enetvar);
181
182 for (i=0; i<6; i++) {
183 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
184 if (tmp)
185 tmp = (*end) ? end+1 : end;
186 }
187
188 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
189 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
190 memcmp(dev->enetaddr, env_enetaddr, 6))
191 {
wdenk6069ff22003-02-28 00:49:47 +0000192 printf("\nWarning: %s MAC addresses don't match:\n", dev->name);
wdenkc6097192002-11-03 00:24:07 +0000193 printf("Address in SROM is "
194 "%02X:%02X:%02X:%02X:%02X:%02X\n",
195 dev->enetaddr[0], dev->enetaddr[1],
196 dev->enetaddr[2], dev->enetaddr[3],
197 dev->enetaddr[4], dev->enetaddr[5]);
198 printf("Address in environment is "
199 "%02X:%02X:%02X:%02X:%02X:%02X\n",
200 env_enetaddr[0], env_enetaddr[1],
201 env_enetaddr[2], env_enetaddr[3],
202 env_enetaddr[4], env_enetaddr[5]);
203 }
204
205 memcpy(dev->enetaddr, env_enetaddr, 6);
206 }
207
208 eth_number++;
209 dev = dev->next;
210 } while(dev != eth_devices);
211
212 putc ('\n');
213 }
214
215 return eth_number;
216}
217
218void eth_set_enetaddr(int num, char *addr) {
219 struct eth_device *dev;
220 unsigned char enetaddr[6];
221 char *end;
222 int i;
223
wdenkd4ca31c2004-01-02 14:00:00 +0000224 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
225
wdenkc6097192002-11-03 00:24:07 +0000226 if (!eth_devices)
227 return;
228
229 for (i=0; i<6; i++) {
230 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
231 if (addr)
232 addr = (*end) ? end+1 : end;
233 }
234
235 dev = eth_devices;
236 while(num-- > 0) {
237 dev = dev->next;
238
239 if (dev == eth_devices)
240 return;
241 }
242
wdenkd4ca31c2004-01-02 14:00:00 +0000243 debug ( "Setting new HW address on %s\n"
244 "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
245 dev->name,
246 dev->enetaddr[0], dev->enetaddr[1],
247 dev->enetaddr[2], dev->enetaddr[3],
248 dev->enetaddr[4], dev->enetaddr[5]);
wdenkc6097192002-11-03 00:24:07 +0000249
250 memcpy(dev->enetaddr, enetaddr, 6);
251}
252
253int eth_init(bd_t *bis)
254{
255 struct eth_device* old_current;
256
257 if (!eth_current)
258 return 0;
259
260 old_current = eth_current;
261 do {
wdenkd4ca31c2004-01-02 14:00:00 +0000262 debug ("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000263
264 if (eth_current->init(eth_current, bis)) {
265 eth_current->state = ETH_STATE_ACTIVE;
266
wdenkc6097192002-11-03 00:24:07 +0000267 return 1;
268 }
wdenkd4ca31c2004-01-02 14:00:00 +0000269 debug ("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000270
271 eth_try_another(0);
272 } while (old_current != eth_current);
273
274 return 0;
275}
276
277void eth_halt(void)
278{
279 if (!eth_current)
280 return;
281
282 eth_current->halt(eth_current);
283
284 eth_current->state = ETH_STATE_PASSIVE;
285}
286
287int eth_send(volatile void *packet, int length)
288{
289 if (!eth_current)
290 return -1;
291
292 return eth_current->send(eth_current, packet, length);
293}
294
295int eth_rx(void)
296{
297 if (!eth_current)
298 return -1;
299
300 return eth_current->recv(eth_current);
301}
302
303void eth_try_another(int first_restart)
304{
305 static struct eth_device *first_failed = NULL;
306
307 if (!eth_current)
308 return;
309
310 if (first_restart)
311 {
312 first_failed = eth_current;
313 }
314
315 eth_current = eth_current->next;
316
317 if (first_failed == eth_current)
318 {
319 NetRestartWrap = 1;
320 }
321}
322
wdenk5bb226e2003-11-17 21:14:37 +0000323char *eth_get_name (void)
324{
325 return (eth_current ? eth_current->name : "unknown");
326}
wdenkc6097192002-11-03 00:24:07 +0000327#endif