]> git.sur5r.net Git - u-boot/blob - net/eth.c
* Patch by André Schwarz, 8 Dec 2003:
[u-boot] / net / eth.c
1 /*
2  * (C) Copyright 2001-2004
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>
27
28 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
29
30 #ifdef CFG_GT_6426x
31 extern int gt6426x_eth_initialize(bd_t *bis);
32 #endif
33
34 extern int e1000_initialize(bd_t*);
35 extern int eepro100_initialize(bd_t*);
36 extern int natsemi_initialize(bd_t*);
37 extern int ns8382x_initialize(bd_t*);
38 extern int dc21x4x_initialize(bd_t*);
39 extern int eth_3com_initialize(bd_t*);
40 extern int pcnet_initialize(bd_t*);
41 extern int fec_initialize(bd_t*);
42 extern int scc_initialize(bd_t*);
43 extern int inca_switch_initialize(bd_t*);
44 extern int ppc_4xx_eth_initialize(bd_t *);
45 extern int plb2800_eth_initialize(bd_t*);
46 extern int mpc5xxx_fec_initialize(bd_t*);
47 extern int skge_initialize(bd_t*);
48 extern int tsec_initialize(bd_t*);
49 extern int au1x00_enet_initialize(bd_t*);
50 extern int rtl8139_initialize(bd_t*);
51
52 static struct eth_device *eth_devices, *eth_current;
53
54 struct eth_device *eth_get_dev(void)
55 {
56         return eth_current;
57 }
58
59 int 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
81 int 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
98 int 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
107 #if defined(CONFIG_405GP) || defined(CONFIG_440) || defined(CONFIG_405EP)
108         ppc_4xx_eth_initialize(bis);
109 #endif
110 #ifdef CONFIG_INCA_IP_SWITCH
111         inca_switch_initialize(bis);
112 #endif
113 #ifdef CONFIG_PLB2800_ETHER
114         plb2800_eth_initialize(bis);
115 #endif
116 #ifdef CONFIG_E1000
117         e1000_initialize(bis);
118 #endif
119 #ifdef CONFIG_EEPRO100
120         eepro100_initialize(bis);
121 #endif
122 #ifdef CONFIG_TULIP
123         dc21x4x_initialize(bis);
124 #endif
125 #ifdef CONFIG_3COM
126         eth_3com_initialize(bis);
127 #endif
128 #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
143 #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
144         fec_initialize(bis);
145 #endif
146 #if defined(CONFIG_MPC5XXX_FEC)
147         mpc5xxx_fec_initialize(bis);
148 #endif
149 #if defined(CONFIG_SK98)
150         skge_initialize(bis);
151 #endif
152 #ifdef CONFIG_TSEC_ENET
153        tsec_initialize(bis);
154 #endif
155 #if defined(CONFIG_AU1X00)
156         au1x00_enet_initialize(bis);
157 #endif
158 #if defined(CONFIG_RTL8139)
159         rtl8139_initialize(bis);
160 #endif
161
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                                 {
192                                         printf("\nWarning: %s MAC addresses don't match:\n", dev->name);
193                                         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
218 void eth_set_enetaddr(int num, char *addr) {
219         struct eth_device *dev;
220         unsigned char enetaddr[6];
221         char *end;
222         int i;
223
224         debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
225
226         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
243         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]);
249
250         memcpy(dev->enetaddr, enetaddr, 6);
251 }
252
253 int 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 {
262                 debug ("Trying %s\n", eth_current->name);
263
264                 if (eth_current->init(eth_current, bis)) {
265                         eth_current->state = ETH_STATE_ACTIVE;
266
267                         return 1;
268                 }
269                 debug  ("FAIL\n");
270
271                 eth_try_another(0);
272         } while (old_current != eth_current);
273
274         return 0;
275 }
276
277 void 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
287 int 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
295 int eth_rx(void)
296 {
297         if (!eth_current)
298                 return -1;
299
300         return eth_current->recv(eth_current);
301 }
302
303 void 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
323 char *eth_get_name (void)
324 {
325         return (eth_current ? eth_current->name : "unknown");
326 }
327 #endif