blob: c6fa5b94318cad1e4d3466397033455360205d9c [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
wdenk1ebf41e2004-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>
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +010027#include <miiphy.h>
wdenkc6097192002-11-03 00:24:07 +000028
Mike Frysingerc34319c2009-01-29 19:43:44 -050029#ifdef CONFIG_CMD_NET
30void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
31{
32 char *end;
33 int i;
34
35 for (i = 0; i < 6; ++i) {
36 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
37 if (addr)
38 addr = (*end) ? end + 1 : end;
39 }
40}
41
42int eth_getenv_enetaddr(char *name, uchar *enetaddr)
43{
44 eth_parse_enetaddr(getenv(name), enetaddr);
45 return is_valid_ether_addr(enetaddr);
46}
47
48int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
49{
50 char buf[20];
51
52 sprintf(buf, "%pM", enetaddr);
53
54 return setenv(name, buf);
55}
56#endif
57
Jon Loeliger54f35c22007-07-09 17:45:14 -050058#if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
wdenkc6097192002-11-03 00:24:07 +000059
Heiko Schocher0c303fa2009-02-10 09:38:52 +010060static char *act = NULL;
61static int env_changed_id = 0;
62
Ben Warrend448a492008-06-23 22:57:27 -070063/*
64 * CPU and board-specific Ethernet initializations. Aliased function
65 * signals caller to move on
66 */
67static int __def_eth_init(bd_t *bis)
68{
69 return -1;
70}
Peter Tyser21d2cd22009-04-20 11:08:46 -050071int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
72int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
Ben Warrend448a492008-06-23 22:57:27 -070073
wdenk5da7f2f2004-01-03 00:43:19 +000074extern int mv6436x_eth_initialize(bd_t *);
75extern int mv6446x_eth_initialize(bd_t *);
wdenkc6097192002-11-03 00:24:07 +000076
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010077#ifdef CONFIG_API
78extern void (*push_packet)(volatile void *, int);
79
80static struct {
81 uchar data[PKTSIZE];
82 int length;
83} eth_rcv_bufs[PKTBUFSRX];
84
85static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
86#endif
87
wdenkc6097192002-11-03 00:24:07 +000088static struct eth_device *eth_devices, *eth_current;
89
90struct eth_device *eth_get_dev(void)
91{
92 return eth_current;
93}
94
Marian Balakowiczaab8c492005-10-28 22:30:33 +020095struct eth_device *eth_get_dev_by_name(char *devname)
96{
97 struct eth_device *dev, *target_dev;
98
99 if (!eth_devices)
100 return NULL;
101
102 dev = eth_devices;
103 target_dev = NULL;
104 do {
105 if (strcmp(devname, dev->name) == 0) {
106 target_dev = dev;
107 break;
108 }
109 dev = dev->next;
110 } while (dev != eth_devices);
111
112 return target_dev;
113}
114
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600115struct eth_device *eth_get_dev_by_index(int index)
116{
117 struct eth_device *dev, *target_dev;
118 int idx = 0;
119
120 if (!eth_devices)
121 return NULL;
122
123 dev = eth_devices;
124 target_dev = NULL;
125 do {
126 if (idx == index) {
127 target_dev = dev;
128 break;
129 }
130 dev = dev->next;
131 idx++;
132 } while (dev != eth_devices);
133
134 return target_dev;
135}
136
wdenkc6097192002-11-03 00:24:07 +0000137int eth_get_dev_index (void)
138{
139 struct eth_device *dev;
140 int num = 0;
141
142 if (!eth_devices) {
143 return (-1);
144 }
145
146 for (dev = eth_devices; dev; dev = dev->next) {
147 if (dev == eth_current)
148 break;
149 ++num;
150 }
151
152 if (dev) {
153 return (num);
154 }
155
156 return (0);
157}
158
159int eth_register(struct eth_device* dev)
160{
161 struct eth_device *d;
162
163 if (!eth_devices) {
164 eth_current = eth_devices = dev;
wdenk145d2c12004-04-15 21:48:45 +0000165#ifdef CONFIG_NET_MULTI
166 /* update current ethernet name */
167 {
168 char *act = getenv("ethact");
169 if (act == NULL || strcmp(act, eth_current->name) != 0)
170 setenv("ethact", eth_current->name);
171 }
172#endif
wdenkc6097192002-11-03 00:24:07 +0000173 } else {
174 for (d=eth_devices; d->next!=eth_devices; d=d->next);
175 d->next = dev;
176 }
177
178 dev->state = ETH_STATE_INIT;
179 dev->next = eth_devices;
180
181 return 0;
182}
183
184int eth_initialize(bd_t *bis)
185{
Shinya Kuribayashidb7101e2007-11-19 20:27:04 +0900186 char enetvar[32];
187 unsigned char env_enetaddr[6];
Mike Frysingerc34319c2009-01-29 19:43:44 -0500188 int eth_number = 0;
wdenkc6097192002-11-03 00:24:07 +0000189
190 eth_devices = NULL;
191 eth_current = NULL;
192
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200193 show_boot_progress (64);
Jon Loeliger54f35c22007-07-09 17:45:14 -0500194#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100195 miiphy_init();
196#endif
Ben Warrend448a492008-06-23 22:57:27 -0700197 /* Try board-specific initialization first. If it fails or isn't
198 * present, try the cpu-specific initialization */
199 if (board_eth_init(bis) < 0)
200 cpu_eth_init(bis);
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100201
Stefan Roese45993ea2006-11-29 15:42:37 +0100202#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
wdenk5da7f2f2004-01-03 00:43:19 +0000203 mv6436x_eth_initialize(bis);
204#endif
Stefan Roese45993ea2006-11-29 15:42:37 +0100205#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
wdenk5da7f2f2004-01-03 00:43:19 +0000206 mv6446x_eth_initialize(bis);
207#endif
wdenkc6097192002-11-03 00:24:07 +0000208 if (!eth_devices) {
209 puts ("No ethernet found.\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200210 show_boot_progress (-64);
wdenkc6097192002-11-03 00:24:07 +0000211 } else {
212 struct eth_device *dev = eth_devices;
213 char *ethprime = getenv ("ethprime");
214
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200215 show_boot_progress (65);
wdenkc6097192002-11-03 00:24:07 +0000216 do {
217 if (eth_number)
218 puts (", ");
219
220 printf("%s", dev->name);
221
222 if (ethprime && strcmp (dev->name, ethprime) == 0) {
223 eth_current = dev;
224 puts (" [PRIME]");
225 }
226
227 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
Mike Frysingerc34319c2009-01-29 19:43:44 -0500228 eth_getenv_enetaddr(enetvar, env_enetaddr);
wdenkc6097192002-11-03 00:24:07 +0000229
230 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
231 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
232 memcmp(dev->enetaddr, env_enetaddr, 6))
233 {
wdenk63a10be2004-05-08 20:33:20 +0000234 printf ("\nWarning: %s MAC addresses don't match:\n",
235 dev->name);
Mike Frysingerc34319c2009-01-29 19:43:44 -0500236 printf ("Address in SROM is %pM\n",
237 dev->enetaddr);
238 printf ("Address in environment is %pM\n",
239 env_enetaddr);
wdenkc6097192002-11-03 00:24:07 +0000240 }
241
242 memcpy(dev->enetaddr, env_enetaddr, 6);
243 }
244
245 eth_number++;
246 dev = dev->next;
247 } while(dev != eth_devices);
248
wdenk145d2c12004-04-15 21:48:45 +0000249#ifdef CONFIG_NET_MULTI
250 /* update current ethernet name */
251 if (eth_current) {
252 char *act = getenv("ethact");
253 if (act == NULL || strcmp(act, eth_current->name) != 0)
254 setenv("ethact", eth_current->name);
255 } else
256 setenv("ethact", NULL);
257#endif
258
wdenkc6097192002-11-03 00:24:07 +0000259 putc ('\n');
260 }
261
262 return eth_number;
263}
264
265void eth_set_enetaddr(int num, char *addr) {
266 struct eth_device *dev;
267 unsigned char enetaddr[6];
wdenkc6097192002-11-03 00:24:07 +0000268
wdenk1ebf41e2004-01-02 14:00:00 +0000269 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
270
wdenkc6097192002-11-03 00:24:07 +0000271 if (!eth_devices)
272 return;
273
Mike Frysingerc34319c2009-01-29 19:43:44 -0500274 eth_parse_enetaddr(addr, enetaddr);
wdenkc6097192002-11-03 00:24:07 +0000275
276 dev = eth_devices;
277 while(num-- > 0) {
278 dev = dev->next;
279
280 if (dev == eth_devices)
281 return;
282 }
283
wdenk1ebf41e2004-01-02 14:00:00 +0000284 debug ( "Setting new HW address on %s\n"
Mike Frysingerc34319c2009-01-29 19:43:44 -0500285 "New Address is %pM\n",
286 dev->name, enetaddr);
wdenkc6097192002-11-03 00:24:07 +0000287
288 memcpy(dev->enetaddr, enetaddr, 6);
289}
David Updegraff7280da72007-06-11 10:41:07 -0500290#ifdef CONFIG_MCAST_TFTP
291/* Multicast.
292 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200293 * join: 1=join, 0=leave.
David Updegraff7280da72007-06-11 10:41:07 -0500294 */
295int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
296{
297 u8 mcast_mac[6];
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200298 if (!eth_current || !eth_current->mcast)
David Updegraff7280da72007-06-11 10:41:07 -0500299 return -1;
300 mcast_mac[5] = htonl(mcast_ip) & 0xff;
301 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
302 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
303 mcast_mac[2] = 0x5e;
304 mcast_mac[1] = 0x0;
305 mcast_mac[0] = 0x1;
306 return eth_current->mcast(eth_current, mcast_mac, join);
307}
308
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200309/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
310 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff7280da72007-06-11 10:41:07 -0500311 * some other adapter -- hash tables
312 */
313#define CRCPOLY_LE 0xedb88320
314u32 ether_crc (size_t len, unsigned char const *p)
315{
316 int i;
317 u32 crc;
318 crc = ~0;
319 while (len--) {
320 crc ^= *p++;
321 for (i = 0; i < 8; i++)
322 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
323 }
324 /* an reverse the bits, cuz of way they arrive -- last-first */
325 crc = (crc >> 16) | (crc << 16);
326 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
327 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
328 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
329 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
330 return crc;
331}
wdenkc6097192002-11-03 00:24:07 +0000332
David Updegraff7280da72007-06-11 10:41:07 -0500333#endif
334
wdenkc6097192002-11-03 00:24:07 +0000335
336int eth_init(bd_t *bis)
337{
338 struct eth_device* old_current;
339
Stefan Roese11da2be2008-03-04 17:40:41 +0100340 if (!eth_current) {
341 puts ("No ethernet found.\n");
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530342 return -1;
Stefan Roese11da2be2008-03-04 17:40:41 +0100343 }
wdenkc6097192002-11-03 00:24:07 +0000344
345 old_current = eth_current;
346 do {
wdenk1ebf41e2004-01-02 14:00:00 +0000347 debug ("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000348
Ben Warrende9fcb52008-01-09 18:15:53 -0500349 if (eth_current->init(eth_current,bis) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000350 eth_current->state = ETH_STATE_ACTIVE;
351
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530352 return 0;
wdenkc6097192002-11-03 00:24:07 +0000353 }
wdenk1ebf41e2004-01-02 14:00:00 +0000354 debug ("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000355
356 eth_try_another(0);
357 } while (old_current != eth_current);
358
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530359 return -1;
wdenkc6097192002-11-03 00:24:07 +0000360}
361
362void eth_halt(void)
363{
364 if (!eth_current)
365 return;
366
367 eth_current->halt(eth_current);
368
369 eth_current->state = ETH_STATE_PASSIVE;
370}
371
372int eth_send(volatile void *packet, int length)
373{
374 if (!eth_current)
375 return -1;
376
377 return eth_current->send(eth_current, packet, length);
378}
379
380int eth_rx(void)
381{
382 if (!eth_current)
383 return -1;
384
385 return eth_current->recv(eth_current);
386}
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100387
388#ifdef CONFIG_API
389static void eth_save_packet(volatile void *packet, int length)
390{
391 volatile char *p = packet;
392 int i;
393
394 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
395 return;
396
397 if (PKTSIZE < length)
398 return;
399
400 for (i = 0; i < length; i++)
401 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
402
403 eth_rcv_bufs[eth_rcv_last].length = length;
404 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
405}
406
407int eth_receive(volatile void *packet, int length)
408{
409 volatile char *p = packet;
410 void *pp = push_packet;
411 int i;
412
413 if (eth_rcv_current == eth_rcv_last) {
414 push_packet = eth_save_packet;
415 eth_rx();
416 push_packet = pp;
417
418 if (eth_rcv_current == eth_rcv_last)
419 return -1;
420 }
421
422 if (length < eth_rcv_bufs[eth_rcv_current].length)
423 return -1;
424
425 length = eth_rcv_bufs[eth_rcv_current].length;
426
427 for (i = 0; i < length; i++)
428 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
429
430 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
431 return length;
432}
433#endif /* CONFIG_API */
wdenkc6097192002-11-03 00:24:07 +0000434
435void eth_try_another(int first_restart)
436{
437 static struct eth_device *first_failed = NULL;
Matthias Fuchs204f0ec2008-01-17 07:45:05 +0100438 char *ethrotate;
439
440 /*
441 * Do not rotate between network interfaces when
442 * 'ethrotate' variable is set to 'no'.
443 */
444 if (((ethrotate = getenv ("ethrotate")) != NULL) &&
445 (strcmp(ethrotate, "no") == 0))
446 return;
wdenkc6097192002-11-03 00:24:07 +0000447
448 if (!eth_current)
449 return;
450
wdenk63a10be2004-05-08 20:33:20 +0000451 if (first_restart) {
wdenkc6097192002-11-03 00:24:07 +0000452 first_failed = eth_current;
453 }
454
455 eth_current = eth_current->next;
456
wdenk145d2c12004-04-15 21:48:45 +0000457#ifdef CONFIG_NET_MULTI
458 /* update current ethernet name */
459 {
460 char *act = getenv("ethact");
461 if (act == NULL || strcmp(act, eth_current->name) != 0)
462 setenv("ethact", eth_current->name);
463 }
464#endif
465
wdenk63a10be2004-05-08 20:33:20 +0000466 if (first_failed == eth_current) {
wdenkc6097192002-11-03 00:24:07 +0000467 NetRestartWrap = 1;
468 }
469}
470
wdenk145d2c12004-04-15 21:48:45 +0000471#ifdef CONFIG_NET_MULTI
472void eth_set_current(void)
473{
wdenk145d2c12004-04-15 21:48:45 +0000474 struct eth_device* old_current;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100475 int env_id;
wdenk145d2c12004-04-15 21:48:45 +0000476
477 if (!eth_current) /* XXX no current */
478 return;
479
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100480 env_id = get_env_id();
481 if ((act == NULL) || (env_changed_id != env_id)) {
482 act = getenv("ethact");
483 env_changed_id = env_id;
484 }
wdenk145d2c12004-04-15 21:48:45 +0000485 if (act != NULL) {
486 old_current = eth_current;
487 do {
488 if (strcmp(eth_current->name, act) == 0)
489 return;
490 eth_current = eth_current->next;
491 } while (old_current != eth_current);
492 }
493
494 setenv("ethact", eth_current->name);
495}
496#endif
497
wdenkc6abb7e2003-11-17 21:14:37 +0000498char *eth_get_name (void)
499{
500 return (eth_current ? eth_current->name : "unknown");
501}
Jon Loeliger54f35c22007-07-09 17:45:14 -0500502#elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200503
504extern int at91rm9200_miiphy_initialize(bd_t *bis);
505extern int emac4xx_miiphy_initialize(bd_t *bis);
506extern int mcf52x2_miiphy_initialize(bd_t *bis);
507extern int ns7520_miiphy_initialize(bd_t *bis);
Jean-Christophe PLAGNIOL-VILLARDbcf268b2008-08-31 04:45:42 +0200508extern int davinci_eth_miiphy_initialize(bd_t *bis);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200509
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200510
511int eth_initialize(bd_t *bis)
512{
Jon Loeliger54f35c22007-07-09 17:45:14 -0500513#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100514 miiphy_init();
515#endif
516
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200517#if defined(CONFIG_AT91RM9200)
518 at91rm9200_miiphy_initialize(bis);
519#endif
Ben Warren3a918a62008-10-27 23:50:15 -0700520#if defined(CONFIG_PPC4xx_EMAC)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200521 emac4xx_miiphy_initialize(bis);
522#endif
523#if defined(CONFIG_MCF52x2)
524 mcf52x2_miiphy_initialize(bis);
525#endif
Wolfgang Denk81490f42008-07-13 23:07:35 +0200526#if defined(CONFIG_DRIVER_NS7520_ETHERNET)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200527 ns7520_miiphy_initialize(bis);
528#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200529#if defined(CONFIG_DRIVER_TI_EMAC)
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200530 davinci_eth_miiphy_initialize(bis);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200531#endif
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200532 return 0;
533}
wdenkc6097192002-11-03 00:24:07 +0000534#endif