blob: 8940ebf1179e96711e959e2b5394cc9a99671bee [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>
Marian Balakowiczd9785c12005-11-30 18:06:04 +010027#include <miiphy.h>
wdenkc6097192002-11-03 00:24:07 +000028
Mike Frysinger3f6e6992009-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 Loeliger643d1ab2007-07-09 17:45:14 -050058#if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
wdenkc6097192002-11-03 00:24:07 +000059
Ben Warrendd354792008-06-23 22:57:27 -070060/*
61 * CPU and board-specific Ethernet initializations. Aliased function
62 * signals caller to move on
63 */
64static int __def_eth_init(bd_t *bis)
65{
66 return -1;
67}
Peter Tyserf9a109b2009-04-20 11:08:46 -050068int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
69int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
Ben Warrendd354792008-06-23 22:57:27 -070070
wdenk3a473b22004-01-03 00:43:19 +000071extern int mv6436x_eth_initialize(bd_t *);
72extern int mv6446x_eth_initialize(bd_t *);
wdenkc6097192002-11-03 00:24:07 +000073
Rafal Jaworowskif85b6072007-12-27 18:19:02 +010074#ifdef CONFIG_API
75extern void (*push_packet)(volatile void *, int);
76
77static struct {
78 uchar data[PKTSIZE];
79 int length;
80} eth_rcv_bufs[PKTBUFSRX];
81
82static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
83#endif
84
wdenkc6097192002-11-03 00:24:07 +000085static struct eth_device *eth_devices, *eth_current;
86
87struct eth_device *eth_get_dev(void)
88{
89 return eth_current;
90}
91
Marian Balakowicz63ff0042005-10-28 22:30:33 +020092struct eth_device *eth_get_dev_by_name(char *devname)
93{
94 struct eth_device *dev, *target_dev;
95
96 if (!eth_devices)
97 return NULL;
98
99 dev = eth_devices;
100 target_dev = NULL;
101 do {
102 if (strcmp(devname, dev->name) == 0) {
103 target_dev = dev;
104 break;
105 }
106 dev = dev->next;
107 } while (dev != eth_devices);
108
109 return target_dev;
110}
111
Andy Fleming9e569862009-02-11 15:07:24 -0600112struct eth_device *eth_get_dev_by_index(int index)
113{
114 struct eth_device *dev, *target_dev;
115 int idx = 0;
116
117 if (!eth_devices)
118 return NULL;
119
120 dev = eth_devices;
121 target_dev = NULL;
122 do {
123 if (idx == index) {
124 target_dev = dev;
125 break;
126 }
127 dev = dev->next;
128 idx++;
129 } while (dev != eth_devices);
130
131 return target_dev;
132}
133
wdenkc6097192002-11-03 00:24:07 +0000134int eth_get_dev_index (void)
135{
136 struct eth_device *dev;
137 int num = 0;
138
139 if (!eth_devices) {
140 return (-1);
141 }
142
143 for (dev = eth_devices; dev; dev = dev->next) {
144 if (dev == eth_current)
145 break;
146 ++num;
147 }
148
149 if (dev) {
150 return (num);
151 }
152
153 return (0);
154}
155
156int eth_register(struct eth_device* dev)
157{
158 struct eth_device *d;
159
160 if (!eth_devices) {
161 eth_current = eth_devices = dev;
wdenka3d991b2004-04-15 21:48:45 +0000162#ifdef CONFIG_NET_MULTI
163 /* update current ethernet name */
164 {
165 char *act = getenv("ethact");
166 if (act == NULL || strcmp(act, eth_current->name) != 0)
167 setenv("ethact", eth_current->name);
168 }
169#endif
wdenkc6097192002-11-03 00:24:07 +0000170 } else {
171 for (d=eth_devices; d->next!=eth_devices; d=d->next);
172 d->next = dev;
173 }
174
175 dev->state = ETH_STATE_INIT;
176 dev->next = eth_devices;
177
178 return 0;
179}
180
181int eth_initialize(bd_t *bis)
182{
Shinya Kuribayashi5ca2d092007-11-19 20:27:04 +0900183 char enetvar[32];
184 unsigned char env_enetaddr[6];
Mike Frysinger3f6e6992009-01-29 19:43:44 -0500185 int eth_number = 0;
wdenkc6097192002-11-03 00:24:07 +0000186
187 eth_devices = NULL;
188 eth_current = NULL;
189
Heiko Schocherfad63402007-07-13 09:54:17 +0200190 show_boot_progress (64);
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500191#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100192 miiphy_init();
193#endif
Ben Warrendd354792008-06-23 22:57:27 -0700194 /* Try board-specific initialization first. If it fails or isn't
195 * present, try the cpu-specific initialization */
196 if (board_eth_init(bis) < 0)
197 cpu_eth_init(bis);
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100198
Stefan Roese1eac2a72006-11-29 15:42:37 +0100199#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
wdenk3a473b22004-01-03 00:43:19 +0000200 mv6436x_eth_initialize(bis);
201#endif
Stefan Roese1eac2a72006-11-29 15:42:37 +0100202#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
wdenk3a473b22004-01-03 00:43:19 +0000203 mv6446x_eth_initialize(bis);
204#endif
wdenkc6097192002-11-03 00:24:07 +0000205 if (!eth_devices) {
206 puts ("No ethernet found.\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200207 show_boot_progress (-64);
wdenkc6097192002-11-03 00:24:07 +0000208 } else {
209 struct eth_device *dev = eth_devices;
210 char *ethprime = getenv ("ethprime");
211
Heiko Schocherfad63402007-07-13 09:54:17 +0200212 show_boot_progress (65);
wdenkc6097192002-11-03 00:24:07 +0000213 do {
214 if (eth_number)
215 puts (", ");
216
217 printf("%s", dev->name);
218
219 if (ethprime && strcmp (dev->name, ethprime) == 0) {
220 eth_current = dev;
221 puts (" [PRIME]");
222 }
223
224 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
Mike Frysinger3f6e6992009-01-29 19:43:44 -0500225 eth_getenv_enetaddr(enetvar, env_enetaddr);
wdenkc6097192002-11-03 00:24:07 +0000226
227 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
228 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
229 memcmp(dev->enetaddr, env_enetaddr, 6))
230 {
wdenkbaac6072004-05-08 20:33:20 +0000231 printf ("\nWarning: %s MAC addresses don't match:\n",
232 dev->name);
Mike Frysinger3f6e6992009-01-29 19:43:44 -0500233 printf ("Address in SROM is %pM\n",
234 dev->enetaddr);
235 printf ("Address in environment is %pM\n",
236 env_enetaddr);
wdenkc6097192002-11-03 00:24:07 +0000237 }
238
239 memcpy(dev->enetaddr, env_enetaddr, 6);
240 }
241
242 eth_number++;
243 dev = dev->next;
244 } while(dev != eth_devices);
245
wdenka3d991b2004-04-15 21:48:45 +0000246#ifdef CONFIG_NET_MULTI
247 /* update current ethernet name */
248 if (eth_current) {
249 char *act = getenv("ethact");
250 if (act == NULL || strcmp(act, eth_current->name) != 0)
251 setenv("ethact", eth_current->name);
252 } else
253 setenv("ethact", NULL);
254#endif
255
wdenkc6097192002-11-03 00:24:07 +0000256 putc ('\n');
257 }
258
259 return eth_number;
260}
261
262void eth_set_enetaddr(int num, char *addr) {
263 struct eth_device *dev;
264 unsigned char enetaddr[6];
wdenkc6097192002-11-03 00:24:07 +0000265
wdenkd4ca31c2004-01-02 14:00:00 +0000266 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
267
wdenkc6097192002-11-03 00:24:07 +0000268 if (!eth_devices)
269 return;
270
Mike Frysinger3f6e6992009-01-29 19:43:44 -0500271 eth_parse_enetaddr(addr, enetaddr);
wdenkc6097192002-11-03 00:24:07 +0000272
273 dev = eth_devices;
274 while(num-- > 0) {
275 dev = dev->next;
276
277 if (dev == eth_devices)
278 return;
279 }
280
wdenkd4ca31c2004-01-02 14:00:00 +0000281 debug ( "Setting new HW address on %s\n"
Mike Frysinger3f6e6992009-01-29 19:43:44 -0500282 "New Address is %pM\n",
283 dev->name, enetaddr);
wdenkc6097192002-11-03 00:24:07 +0000284
285 memcpy(dev->enetaddr, enetaddr, 6);
286}
David Updegraff53a5c422007-06-11 10:41:07 -0500287#ifdef CONFIG_MCAST_TFTP
288/* Multicast.
289 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200290 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500291 */
292int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
293{
294 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200295 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500296 return -1;
297 mcast_mac[5] = htonl(mcast_ip) & 0xff;
298 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
299 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
300 mcast_mac[2] = 0x5e;
301 mcast_mac[1] = 0x0;
302 mcast_mac[0] = 0x1;
303 return eth_current->mcast(eth_current, mcast_mac, join);
304}
305
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200306/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
307 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500308 * some other adapter -- hash tables
309 */
310#define CRCPOLY_LE 0xedb88320
311u32 ether_crc (size_t len, unsigned char const *p)
312{
313 int i;
314 u32 crc;
315 crc = ~0;
316 while (len--) {
317 crc ^= *p++;
318 for (i = 0; i < 8; i++)
319 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
320 }
321 /* an reverse the bits, cuz of way they arrive -- last-first */
322 crc = (crc >> 16) | (crc << 16);
323 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
324 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
325 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
326 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
327 return crc;
328}
329
330#endif
331
wdenkc6097192002-11-03 00:24:07 +0000332
333int eth_init(bd_t *bis)
334{
335 struct eth_device* old_current;
336
Stefan Roese6bc11382008-03-04 17:40:41 +0100337 if (!eth_current) {
338 puts ("No ethernet found.\n");
Upakul Barkakaty505be872007-11-29 12:16:13 +0530339 return -1;
Stefan Roese6bc11382008-03-04 17:40:41 +0100340 }
wdenkc6097192002-11-03 00:24:07 +0000341
342 old_current = eth_current;
343 do {
wdenkd4ca31c2004-01-02 14:00:00 +0000344 debug ("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000345
Ben Warren422b1a02008-01-09 18:15:53 -0500346 if (eth_current->init(eth_current,bis) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000347 eth_current->state = ETH_STATE_ACTIVE;
348
Upakul Barkakaty505be872007-11-29 12:16:13 +0530349 return 0;
wdenkc6097192002-11-03 00:24:07 +0000350 }
wdenkd4ca31c2004-01-02 14:00:00 +0000351 debug ("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000352
353 eth_try_another(0);
354 } while (old_current != eth_current);
355
Upakul Barkakaty505be872007-11-29 12:16:13 +0530356 return -1;
wdenkc6097192002-11-03 00:24:07 +0000357}
358
359void eth_halt(void)
360{
361 if (!eth_current)
362 return;
363
364 eth_current->halt(eth_current);
365
366 eth_current->state = ETH_STATE_PASSIVE;
367}
368
369int eth_send(volatile void *packet, int length)
370{
371 if (!eth_current)
372 return -1;
373
374 return eth_current->send(eth_current, packet, length);
375}
376
377int eth_rx(void)
378{
379 if (!eth_current)
380 return -1;
381
382 return eth_current->recv(eth_current);
383}
384
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100385#ifdef CONFIG_API
386static void eth_save_packet(volatile void *packet, int length)
387{
388 volatile char *p = packet;
389 int i;
390
391 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
392 return;
393
394 if (PKTSIZE < length)
395 return;
396
397 for (i = 0; i < length; i++)
398 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
399
400 eth_rcv_bufs[eth_rcv_last].length = length;
401 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
402}
403
404int eth_receive(volatile void *packet, int length)
405{
406 volatile char *p = packet;
407 void *pp = push_packet;
408 int i;
409
410 if (eth_rcv_current == eth_rcv_last) {
411 push_packet = eth_save_packet;
412 eth_rx();
413 push_packet = pp;
414
415 if (eth_rcv_current == eth_rcv_last)
416 return -1;
417 }
418
419 if (length < eth_rcv_bufs[eth_rcv_current].length)
420 return -1;
421
422 length = eth_rcv_bufs[eth_rcv_current].length;
423
424 for (i = 0; i < length; i++)
425 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
426
427 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
428 return length;
429}
430#endif /* CONFIG_API */
431
wdenkc6097192002-11-03 00:24:07 +0000432void eth_try_another(int first_restart)
433{
434 static struct eth_device *first_failed = NULL;
Matthias Fuchse1692572008-01-17 07:45:05 +0100435 char *ethrotate;
436
437 /*
438 * Do not rotate between network interfaces when
439 * 'ethrotate' variable is set to 'no'.
440 */
441 if (((ethrotate = getenv ("ethrotate")) != NULL) &&
442 (strcmp(ethrotate, "no") == 0))
443 return;
wdenkc6097192002-11-03 00:24:07 +0000444
445 if (!eth_current)
446 return;
447
wdenkbaac6072004-05-08 20:33:20 +0000448 if (first_restart) {
wdenkc6097192002-11-03 00:24:07 +0000449 first_failed = eth_current;
450 }
451
452 eth_current = eth_current->next;
453
wdenka3d991b2004-04-15 21:48:45 +0000454#ifdef CONFIG_NET_MULTI
455 /* update current ethernet name */
456 {
457 char *act = getenv("ethact");
458 if (act == NULL || strcmp(act, eth_current->name) != 0)
459 setenv("ethact", eth_current->name);
460 }
461#endif
462
wdenkbaac6072004-05-08 20:33:20 +0000463 if (first_failed == eth_current) {
wdenkc6097192002-11-03 00:24:07 +0000464 NetRestartWrap = 1;
465 }
466}
467
wdenka3d991b2004-04-15 21:48:45 +0000468#ifdef CONFIG_NET_MULTI
469void eth_set_current(void)
470{
Heiko Schocherda954272009-04-28 08:36:11 +0200471 static char *act = NULL;
472 static int env_changed_id = 0;
wdenka3d991b2004-04-15 21:48:45 +0000473 struct eth_device* old_current;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100474 int env_id;
wdenka3d991b2004-04-15 21:48:45 +0000475
476 if (!eth_current) /* XXX no current */
477 return;
478
Heiko Schocher2f70c492009-02-10 09:38:52 +0100479 env_id = get_env_id();
480 if ((act == NULL) || (env_changed_id != env_id)) {
481 act = getenv("ethact");
482 env_changed_id = env_id;
483 }
wdenka3d991b2004-04-15 21:48:45 +0000484 if (act != NULL) {
485 old_current = eth_current;
486 do {
487 if (strcmp(eth_current->name, act) == 0)
488 return;
489 eth_current = eth_current->next;
490 } while (old_current != eth_current);
491 }
492
493 setenv("ethact", eth_current->name);
494}
495#endif
496
wdenk5bb226e2003-11-17 21:14:37 +0000497char *eth_get_name (void)
498{
499 return (eth_current ? eth_current->name : "unknown");
500}
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500501#elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200502
503extern int at91rm9200_miiphy_initialize(bd_t *bis);
504extern int emac4xx_miiphy_initialize(bd_t *bis);
505extern int mcf52x2_miiphy_initialize(bd_t *bis);
506extern int ns7520_miiphy_initialize(bd_t *bis);
Jean-Christophe PLAGNIOL-VILLARDd6e04252008-08-31 04:45:42 +0200507extern int davinci_eth_miiphy_initialize(bd_t *bis);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200508
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200509
510int eth_initialize(bd_t *bis)
511{
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500512#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100513 miiphy_init();
514#endif
515
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200516#if defined(CONFIG_AT91RM9200)
517 at91rm9200_miiphy_initialize(bis);
518#endif
Ben Warren96e21f82008-10-27 23:50:15 -0700519#if defined(CONFIG_PPC4xx_EMAC)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200520 emac4xx_miiphy_initialize(bis);
521#endif
522#if defined(CONFIG_MCF52x2)
523 mcf52x2_miiphy_initialize(bis);
524#endif
Wolfgang Denk25dbe982008-07-13 23:07:35 +0200525#if defined(CONFIG_DRIVER_NS7520_ETHERNET)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200526 ns7520_miiphy_initialize(bis);
527#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200528#if defined(CONFIG_DRIVER_TI_EMAC)
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200529 davinci_eth_miiphy_initialize(bis);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200530#endif
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200531 return 0;
532}
wdenkc6097192002-11-03 00:24:07 +0000533#endif