]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - net/eth.c
* Vince Husovsky, 7 Nov 2002:
[karo-tx-uboot.git] / net / eth.c
1 /*
2  * (C) Copyright 2001, 2002
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 eepro100_initialize(bd_t*);
35 extern int natsemi_initialize(bd_t*);
36 extern int ns8382x_initialize(bd_t*);
37 extern int dc21x4x_initialize(bd_t*);
38 extern int pcnet_initialize(bd_t*);
39 extern int fec_initialize(bd_t*);
40 extern int scc_initialize(bd_t*);
41
42 static struct eth_device *eth_devices, *eth_current;
43
44 struct eth_device *eth_get_dev(void)
45 {
46         return eth_current;
47 }
48
49 int eth_get_dev_index (void)
50 {
51         struct eth_device *dev;
52         int num = 0;
53
54         if (!eth_devices) {
55                 return (-1);
56         }
57
58         for (dev = eth_devices; dev; dev = dev->next) {
59                 if (dev == eth_current)
60                         break;
61                 ++num;
62         }
63
64         if (dev) {
65                 return (num);
66         }
67
68         return (0);
69 }
70
71 int eth_register(struct eth_device* dev)
72 {
73         struct eth_device *d;
74
75         if (!eth_devices) {
76                 eth_current = eth_devices = dev;
77         } else {
78                 for (d=eth_devices; d->next!=eth_devices; d=d->next);
79                 d->next = dev;
80         }
81
82         dev->state = ETH_STATE_INIT;
83         dev->next  = eth_devices;
84
85         return 0;
86 }
87
88 int eth_initialize(bd_t *bis)
89 {
90         unsigned char enetvar[32], env_enetaddr[6];
91         int i, eth_number = 0;
92         char *tmp, *end;
93
94         eth_devices = NULL;
95         eth_current = NULL;
96
97 #ifdef CONFIG_EEPRO100
98         eepro100_initialize(bis);
99 #endif
100 #ifdef CONFIG_TULIP
101         dc21x4x_initialize(bis);
102 #endif
103 #ifdef CONFIG_PCNET
104         pcnet_initialize(bis);
105 #endif
106 #ifdef CFG_GT_6426x
107         gt6426x_eth_initialize(bis);
108 #endif
109 #ifdef CONFIG_NATSEMI
110         natsemi_initialize(bis);
111 #endif
112 #ifdef CONFIG_NS8382X
113         ns8382x_initialize(bis);
114 #endif
115 #ifdef SCC_ENET
116         scc_initialize(bis);
117 #endif
118 #ifdef FEC_ENET
119         fec_initialize(bis);
120 #endif
121
122         if (!eth_devices) {
123                 puts ("No ethernet found.\n");
124         } else {
125                 struct eth_device *dev = eth_devices;
126                 char *ethprime = getenv ("ethprime");
127
128                 do {
129                         if (eth_number)
130                                 puts (", ");
131
132                         printf("%s", dev->name);
133
134                         if (ethprime && strcmp (dev->name, ethprime) == 0) {
135                                 eth_current = dev;
136                                 puts (" [PRIME]");
137                         }
138
139                         sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
140                         tmp = getenv (enetvar);
141
142                         for (i=0; i<6; i++) {
143                                 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
144                                 if (tmp)
145                                         tmp = (*end) ? end+1 : end;
146                         }
147
148                         if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
149                                 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
150                                     memcmp(dev->enetaddr, env_enetaddr, 6))
151                                 {
152                                         printf("\nWarning: %s HW address don't match:\n", dev->name);
153                                         printf("Address in SROM is         "
154                                                "%02X:%02X:%02X:%02X:%02X:%02X\n",
155                                                dev->enetaddr[0], dev->enetaddr[1],
156                                                dev->enetaddr[2], dev->enetaddr[3],
157                                                dev->enetaddr[4], dev->enetaddr[5]);
158                                         printf("Address in environment is  "
159                                                "%02X:%02X:%02X:%02X:%02X:%02X\n",
160                                                env_enetaddr[0], env_enetaddr[1],
161                                                env_enetaddr[2], env_enetaddr[3],
162                                                env_enetaddr[4], env_enetaddr[5]);
163                                 }
164
165                                 memcpy(dev->enetaddr, env_enetaddr, 6);
166                         }
167
168                         eth_number++;
169                         dev = dev->next;
170                 } while(dev != eth_devices);
171
172                 putc ('\n');
173         }
174
175         return eth_number;
176 }
177
178 void eth_set_enetaddr(int num, char *addr) {
179         struct eth_device *dev;
180         unsigned char enetaddr[6];
181         char *end;
182         int i;
183
184 #ifdef DEBUG
185         printf("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
186 #endif
187         if (!eth_devices)
188                 return;
189
190         for (i=0; i<6; i++) {
191                 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
192                 if (addr)
193                         addr = (*end) ? end+1 : end;
194         }
195
196         dev = eth_devices;
197         while(num-- > 0) {
198                 dev = dev->next;
199
200                 if (dev == eth_devices)
201                         return;
202         }
203
204 #ifdef DEBUG
205         printf("Setting new HW address on %s\n", dev->name);
206         printf("New Address is             "
207                "%02X:%02X:%02X:%02X:%02X:%02X\n",
208                dev->enetaddr[0], dev->enetaddr[1],
209                dev->enetaddr[2], dev->enetaddr[3],
210                dev->enetaddr[4], dev->enetaddr[5]);
211 #endif
212
213         memcpy(dev->enetaddr, enetaddr, 6);
214 }
215
216 int eth_init(bd_t *bis)
217 {
218         struct eth_device* old_current;
219
220         if (!eth_current)
221                 return 0;
222
223         old_current = eth_current;
224         do {
225 #ifdef DEBUG
226                 printf("Trying %s\n", eth_current->name);
227 #endif
228
229                 if (eth_current->init(eth_current, bis)) {
230                         eth_current->state = ETH_STATE_ACTIVE;
231
232                         return 1;
233                 }
234
235 #ifdef DEBUG
236                 puts ("FAIL\n");
237 #endif
238
239                 eth_try_another(0);
240         } while (old_current != eth_current);
241
242         return 0;
243 }
244
245 void eth_halt(void)
246 {
247         if (!eth_current)
248                 return;
249
250         eth_current->halt(eth_current);
251
252         eth_current->state = ETH_STATE_PASSIVE;
253 }
254
255 int eth_send(volatile void *packet, int length)
256 {
257         if (!eth_current)
258                 return -1;
259
260         return eth_current->send(eth_current, packet, length);
261 }
262
263 int eth_rx(void)
264 {
265         if (!eth_current)
266                 return -1;
267
268         return eth_current->recv(eth_current);
269 }
270
271 void eth_try_another(int first_restart)
272 {
273         static struct eth_device *first_failed = NULL;
274
275         if (!eth_current)
276                 return;
277
278         if (first_restart)
279         {
280                 first_failed = eth_current;
281         }
282
283         eth_current = eth_current->next;
284
285         if (first_failed == eth_current)
286         {
287                 NetRestartWrap = 1;
288         }
289 }
290
291 #endif