Initial code commit
[oweals/u-boot_mod.git] / 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 #include <miiphy.h>
28
29 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
30 extern int ag7240_enet_initialize(bd_t * bis);
31
32 /*
33 static struct eth_device *eth_devices, *eth_current;
34
35 struct eth_device *eth_get_dev(void){
36         return eth_current;
37 }
38 */
39
40 static struct eth_device *eth_devices;
41 struct eth_device *eth_current;
42
43
44 struct eth_device *eth_get_dev_by_name(char *devname){
45         struct eth_device *dev, *target_dev;
46
47         if(!eth_devices){
48                 return NULL;
49         }
50
51         dev = eth_devices;
52         target_dev = NULL;
53
54         do {
55                 if(strcmp(devname, dev->name) == 0){
56                         target_dev = dev;
57                         break;
58                 }
59                 dev = dev->next;
60         } while(dev != eth_devices);
61
62         return(target_dev);
63 }
64
65 int eth_get_dev_index(void){
66         struct eth_device *dev;
67         int num = 0;
68
69         if (!eth_devices){
70                 return(-1);
71         }
72
73         for(dev = eth_devices; dev; dev = dev->next){
74                 if(dev == eth_current){
75                         break;
76                 }
77                 ++num;
78         }
79
80         if(dev){
81                 return(num);
82         }
83
84         return(0);
85 }
86
87 int eth_register(struct eth_device* dev){
88         struct eth_device *d;
89
90         if(!eth_devices){
91                 eth_current = eth_devices = dev;
92 #ifdef CONFIG_NET_MULTI
93                 /* update current ethernet name */
94                 char *act = getenv("ethact");
95                 if(act == NULL || strcmp(act, eth_current->name) != 0){
96                         setenv("ethact", eth_current->name);
97                 }
98 #endif
99         } else {
100                 for (d = eth_devices; d->next != eth_devices; d = d->next);
101                 d->next = dev;
102         }
103
104         dev->state = ETH_STATE_INIT;
105         dev->next = eth_devices;
106
107         return(0);
108 }
109
110 int eth_initialize(bd_t *bis){
111         char enetvar[32];
112         int i, eth_number = 0;
113         char *tmp = NULL, *end = NULL;
114
115         eth_devices = NULL;
116         eth_current = NULL;
117
118 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
119         miiphy_init();
120 #endif
121
122         // ag7240 initialization
123         ag7240_enet_initialize(bis);
124
125         if(!eth_devices){
126                 puts("## Error: no ethernet found\n");
127         } else {
128                 struct eth_device *dev = eth_devices;
129                 char *ethprime = getenv("ethprime");
130
131                 do {
132                         /*
133                         if (eth_number) {
134                                 puts(", ");
135                         }
136
137                         printf("%s", dev->name);
138                         */
139
140                         if(ethprime && strcmp(dev->name, ethprime) == 0){
141                                 eth_current = dev;
142                                 //puts(" [PRIME]");
143                         }
144
145                         sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
146                         tmp = getenv(enetvar);
147
148                         for(i = 0; i < 6; i++){
149                                 if(tmp){
150                                         tmp = (*end) ? end + 1 : end;
151                                 }
152                         }
153
154                         eth_number++;
155                         dev = dev->next;
156
157                 } while(dev != eth_devices);
158
159 #ifdef CONFIG_NET_MULTI
160                 /* update current ethernet name */
161                 if(eth_current){
162                         char *act = getenv("ethact");
163                         if(act == NULL || strcmp(act, eth_current->name) != 0){
164                                 setenv("ethact", eth_current->name);
165                         }
166                 } else {
167                         setenv("ethact", NULL);
168                 }
169 #endif
170
171                 //putc('\n');
172         }
173
174         return(eth_number);
175 }
176
177 void eth_set_enetaddr(int num, char *addr){
178         struct eth_device *dev;
179         unsigned char enetaddr[6];
180         char *end;
181         int i;
182
183         //debug("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
184
185         if(!eth_devices){
186                 return;
187         }
188
189         for(i = 0; i < 6; i++){
190                 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
191                 if (addr){
192                         addr = (*end) ? end + 1 : end;
193                 }
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
205         //debug("Setting new HW address on %s\nNew Address is %02X:%02X:%02X:%02X:%02X:%02X\n", dev->name, enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3], enetaddr[4], enetaddr[5]);
206         memcpy(dev->enetaddr, enetaddr, 6);
207 }
208
209 int eth_init(bd_t *bis){
210         struct eth_device* old_current;
211
212         if (!eth_current){
213                 return(0);
214         }
215
216         old_current = eth_current;
217
218         do {
219 #if !defined(CFG_ATHRS26_PHY) && !defined(CFG_ATHRHDR_EN)
220                 //debug("Trying %s\n", eth_current->name);
221 #endif
222                 if(eth_current->init(eth_current, bis)){
223                         eth_current->state = ETH_STATE_ACTIVE;
224                         return(1);
225                 }
226
227                 //debug("FAIL\n");
228
229                 eth_try_another(0);
230         } while(old_current != eth_current);
231
232         return(0);
233 }
234
235 void eth_halt(void){
236         if(!eth_current){
237                 return;
238         }
239
240         eth_current->halt(eth_current);
241         eth_current->state = ETH_STATE_PASSIVE;
242 }
243
244 int eth_send(volatile void *packet, int length){
245         if(!eth_current){
246                 return(-1);
247         }
248
249         return(eth_current->send(eth_current, packet, length));
250 }
251
252 int eth_rx(void){
253         if(!eth_current){
254                 return(-1);
255         }
256
257         return(eth_current->recv(eth_current));
258 }
259
260 void eth_try_another(int first_restart){
261         static struct eth_device *first_failed = NULL;
262
263         if(!eth_current){
264                 return;
265         }
266
267         if(first_restart){
268                 first_failed = eth_current;
269         }
270
271         eth_current = eth_current->next;
272
273 #ifdef CONFIG_NET_MULTI
274         /* update current ethernet name */
275         char *act = getenv("ethact");
276         if(act == NULL || strcmp(act, eth_current->name) != 0){
277                 setenv("ethact", eth_current->name);
278         }
279 #endif
280
281         if (first_failed == eth_current){
282                 NetRestartWrap = 1;
283         }
284 }
285
286 #ifdef CONFIG_NET_MULTI
287 void eth_set_current(void){
288
289         char *act;
290         struct eth_device* old_current;
291
292         if (!eth_current){
293                 return;
294         }
295
296         act = getenv("ethact");
297
298         if(act != NULL){
299                 old_current = eth_current;
300                 do {
301                         if(strcmp(eth_current->name, act) == 0){
302                                 return;
303                         }
304                         eth_current = eth_current->next;
305                 } while(old_current != eth_current);
306         }
307
308         setenv("ethact", eth_current->name);
309 }
310 #endif
311
312 char *eth_get_name(void){
313         return(eth_current ? eth_current->name : "unknown");
314 }
315 #elif (CONFIG_COMMANDS & CFG_CMD_NET) && !defined(CONFIG_NET_MULTI)
316
317 int eth_initialize(bd_t *bis){
318 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
319         miiphy_init();
320 #endif
321         return 0;
322 }
323 #endif