blob: 432dd60bdc8f56c7db9d77853f6a34f61b987749 [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
Jon Loeliger54f35c22007-07-09 17:45:14 -050029#if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
wdenkc6097192002-11-03 00:24:07 +000030
Ben Warrend448a492008-06-23 22:57:27 -070031/*
32 * CPU and board-specific Ethernet initializations. Aliased function
33 * signals caller to move on
34 */
35static int __def_eth_init(bd_t *bis)
36{
37 return -1;
38}
39int cpu_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init")));
40int board_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init")));
41
wdenk5da7f2f2004-01-03 00:43:19 +000042extern int au1x00_enet_initialize(bd_t*);
wdenkc6097192002-11-03 00:24:07 +000043extern int fec_initialize(bd_t*);
wdenk337f5652004-10-28 00:09:35 +000044extern int mpc8220_fec_initialize(bd_t*);
wdenk5da7f2f2004-01-03 00:43:19 +000045extern int mv6436x_eth_initialize(bd_t *);
46extern int mv6446x_eth_initialize(bd_t *);
wdenk5da7f2f2004-01-03 00:43:19 +000047extern int ppc_4xx_eth_initialize(bd_t *);
wdenk5da7f2f2004-01-03 00:43:19 +000048extern int scc_initialize(bd_t*);
Wolfgang Denk4646d2a2006-05-30 15:56:48 +020049extern int npe_initialize(bd_t *);
Dave Liue732e9c2006-11-03 12:11:15 -060050extern int uec_initialize(int);
wdenkc6097192002-11-03 00:24:07 +000051
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010052#ifdef CONFIG_API
53extern void (*push_packet)(volatile void *, int);
54
55static struct {
56 uchar data[PKTSIZE];
57 int length;
58} eth_rcv_bufs[PKTBUFSRX];
59
60static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
61#endif
62
wdenkc6097192002-11-03 00:24:07 +000063static struct eth_device *eth_devices, *eth_current;
64
65struct eth_device *eth_get_dev(void)
66{
67 return eth_current;
68}
69
Marian Balakowiczaab8c492005-10-28 22:30:33 +020070struct eth_device *eth_get_dev_by_name(char *devname)
71{
72 struct eth_device *dev, *target_dev;
73
74 if (!eth_devices)
75 return NULL;
76
77 dev = eth_devices;
78 target_dev = NULL;
79 do {
80 if (strcmp(devname, dev->name) == 0) {
81 target_dev = dev;
82 break;
83 }
84 dev = dev->next;
85 } while (dev != eth_devices);
86
87 return target_dev;
88}
89
wdenkc6097192002-11-03 00:24:07 +000090int eth_get_dev_index (void)
91{
92 struct eth_device *dev;
93 int num = 0;
94
95 if (!eth_devices) {
96 return (-1);
97 }
98
99 for (dev = eth_devices; dev; dev = dev->next) {
100 if (dev == eth_current)
101 break;
102 ++num;
103 }
104
105 if (dev) {
106 return (num);
107 }
108
109 return (0);
110}
111
112int eth_register(struct eth_device* dev)
113{
114 struct eth_device *d;
115
116 if (!eth_devices) {
117 eth_current = eth_devices = dev;
wdenk145d2c12004-04-15 21:48:45 +0000118#ifdef CONFIG_NET_MULTI
119 /* update current ethernet name */
120 {
121 char *act = getenv("ethact");
122 if (act == NULL || strcmp(act, eth_current->name) != 0)
123 setenv("ethact", eth_current->name);
124 }
125#endif
wdenkc6097192002-11-03 00:24:07 +0000126 } else {
127 for (d=eth_devices; d->next!=eth_devices; d=d->next);
128 d->next = dev;
129 }
130
131 dev->state = ETH_STATE_INIT;
132 dev->next = eth_devices;
133
134 return 0;
135}
136
137int eth_initialize(bd_t *bis)
138{
Shinya Kuribayashidb7101e2007-11-19 20:27:04 +0900139 char enetvar[32];
140 unsigned char env_enetaddr[6];
wdenkc6097192002-11-03 00:24:07 +0000141 int i, eth_number = 0;
142 char *tmp, *end;
143
144 eth_devices = NULL;
145 eth_current = NULL;
146
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200147 show_boot_progress (64);
Jon Loeliger54f35c22007-07-09 17:45:14 -0500148#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100149 miiphy_init();
150#endif
Ben Warrend448a492008-06-23 22:57:27 -0700151 /* Try board-specific initialization first. If it fails or isn't
152 * present, try the cpu-specific initialization */
153 if (board_eth_init(bis) < 0)
154 cpu_eth_init(bis);
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100155
Stefan Roese45993ea2006-11-29 15:42:37 +0100156#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
wdenk5da7f2f2004-01-03 00:43:19 +0000157 mv6436x_eth_initialize(bis);
158#endif
Stefan Roese45993ea2006-11-29 15:42:37 +0100159#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
wdenk5da7f2f2004-01-03 00:43:19 +0000160 mv6446x_eth_initialize(bis);
161#endif
Wolfgang Denk56811f62005-10-09 01:04:33 +0200162#if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
wdenk57b2d802003-06-27 21:31:46 +0000163 ppc_4xx_eth_initialize(bis);
wdenk77934442003-06-05 19:37:36 +0000164#endif
wdenk63a10be2004-05-08 20:33:20 +0000165#ifdef SCC_ENET
166 scc_initialize(bis);
167#endif
wdenk5c71a7a2005-05-16 15:23:22 +0000168#if defined(CONFIG_MPC8220_FEC)
wdenk337f5652004-10-28 00:09:35 +0000169 mpc8220_fec_initialize(bis);
170#endif
Dave Liue732e9c2006-11-03 12:11:15 -0600171#if defined(CONFIG_UEC_ETH1)
172 uec_initialize(0);
173#endif
174#if defined(CONFIG_UEC_ETH2)
175 uec_initialize(1);
176#endif
Joakim Tjernlund4e483202007-12-06 16:43:40 +0100177#if defined(CONFIG_UEC_ETH3)
178 uec_initialize(2);
179#endif
David Saadaf3978832008-01-15 10:40:24 +0200180#if defined(CONFIG_UEC_ETH4)
181 uec_initialize(3);
182#endif
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500183
Stefan Roese09554022005-11-30 13:06:40 +0100184#if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
185 fec_initialize(bis);
186#endif
wdenk63a10be2004-05-08 20:33:20 +0000187#if defined(CONFIG_AU1X00)
188 au1x00_enet_initialize(bis);
189#endif
Wolfgang Denk4646d2a2006-05-30 15:56:48 +0200190#if defined(CONFIG_IXP4XX_NPE)
191 npe_initialize(bis);
192#endif
wdenkc6097192002-11-03 00:24:07 +0000193 if (!eth_devices) {
194 puts ("No ethernet found.\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200195 show_boot_progress (-64);
wdenkc6097192002-11-03 00:24:07 +0000196 } else {
197 struct eth_device *dev = eth_devices;
198 char *ethprime = getenv ("ethprime");
199
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200200 show_boot_progress (65);
wdenkc6097192002-11-03 00:24:07 +0000201 do {
202 if (eth_number)
203 puts (", ");
204
205 printf("%s", dev->name);
206
207 if (ethprime && strcmp (dev->name, ethprime) == 0) {
208 eth_current = dev;
209 puts (" [PRIME]");
210 }
211
212 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
213 tmp = getenv (enetvar);
214
215 for (i=0; i<6; i++) {
216 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
217 if (tmp)
218 tmp = (*end) ? end+1 : end;
219 }
220
221 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
222 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
223 memcmp(dev->enetaddr, env_enetaddr, 6))
224 {
wdenk63a10be2004-05-08 20:33:20 +0000225 printf ("\nWarning: %s MAC addresses don't match:\n",
226 dev->name);
227 printf ("Address in SROM is "
wdenkc6097192002-11-03 00:24:07 +0000228 "%02X:%02X:%02X:%02X:%02X:%02X\n",
229 dev->enetaddr[0], dev->enetaddr[1],
230 dev->enetaddr[2], dev->enetaddr[3],
231 dev->enetaddr[4], dev->enetaddr[5]);
wdenk63a10be2004-05-08 20:33:20 +0000232 printf ("Address in environment is "
wdenkc6097192002-11-03 00:24:07 +0000233 "%02X:%02X:%02X:%02X:%02X:%02X\n",
234 env_enetaddr[0], env_enetaddr[1],
235 env_enetaddr[2], env_enetaddr[3],
236 env_enetaddr[4], env_enetaddr[5]);
237 }
238
239 memcpy(dev->enetaddr, env_enetaddr, 6);
240 }
241
242 eth_number++;
243 dev = dev->next;
244 } while(dev != eth_devices);
245
wdenk145d2c12004-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];
265 char *end;
266 int i;
267
wdenk1ebf41e2004-01-02 14:00:00 +0000268 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
269
wdenkc6097192002-11-03 00:24:07 +0000270 if (!eth_devices)
271 return;
272
273 for (i=0; i<6; i++) {
274 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
275 if (addr)
276 addr = (*end) ? end+1 : end;
277 }
278
279 dev = eth_devices;
280 while(num-- > 0) {
281 dev = dev->next;
282
283 if (dev == eth_devices)
284 return;
285 }
286
wdenk1ebf41e2004-01-02 14:00:00 +0000287 debug ( "Setting new HW address on %s\n"
288 "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
289 dev->name,
Wolfgang Denkb6ea6302005-09-25 17:05:57 +0200290 enetaddr[0], enetaddr[1],
291 enetaddr[2], enetaddr[3],
292 enetaddr[4], enetaddr[5]);
wdenkc6097192002-11-03 00:24:07 +0000293
294 memcpy(dev->enetaddr, enetaddr, 6);
295}
David Updegraff7280da72007-06-11 10:41:07 -0500296#ifdef CONFIG_MCAST_TFTP
297/* Multicast.
298 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200299 * join: 1=join, 0=leave.
David Updegraff7280da72007-06-11 10:41:07 -0500300 */
301int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
302{
303 u8 mcast_mac[6];
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200304 if (!eth_current || !eth_current->mcast)
David Updegraff7280da72007-06-11 10:41:07 -0500305 return -1;
306 mcast_mac[5] = htonl(mcast_ip) & 0xff;
307 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
308 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
309 mcast_mac[2] = 0x5e;
310 mcast_mac[1] = 0x0;
311 mcast_mac[0] = 0x1;
312 return eth_current->mcast(eth_current, mcast_mac, join);
313}
314
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200315/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
316 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff7280da72007-06-11 10:41:07 -0500317 * some other adapter -- hash tables
318 */
319#define CRCPOLY_LE 0xedb88320
320u32 ether_crc (size_t len, unsigned char const *p)
321{
322 int i;
323 u32 crc;
324 crc = ~0;
325 while (len--) {
326 crc ^= *p++;
327 for (i = 0; i < 8; i++)
328 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
329 }
330 /* an reverse the bits, cuz of way they arrive -- last-first */
331 crc = (crc >> 16) | (crc << 16);
332 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
333 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
334 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
335 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
336 return crc;
337}
wdenkc6097192002-11-03 00:24:07 +0000338
David Updegraff7280da72007-06-11 10:41:07 -0500339#endif
340
wdenkc6097192002-11-03 00:24:07 +0000341
342int eth_init(bd_t *bis)
343{
344 struct eth_device* old_current;
345
Stefan Roese11da2be2008-03-04 17:40:41 +0100346 if (!eth_current) {
347 puts ("No ethernet found.\n");
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530348 return -1;
Stefan Roese11da2be2008-03-04 17:40:41 +0100349 }
wdenkc6097192002-11-03 00:24:07 +0000350
351 old_current = eth_current;
352 do {
wdenk1ebf41e2004-01-02 14:00:00 +0000353 debug ("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000354
Ben Warrende9fcb52008-01-09 18:15:53 -0500355 if (eth_current->init(eth_current,bis) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000356 eth_current->state = ETH_STATE_ACTIVE;
357
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530358 return 0;
wdenkc6097192002-11-03 00:24:07 +0000359 }
wdenk1ebf41e2004-01-02 14:00:00 +0000360 debug ("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000361
362 eth_try_another(0);
363 } while (old_current != eth_current);
364
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530365 return -1;
wdenkc6097192002-11-03 00:24:07 +0000366}
367
368void eth_halt(void)
369{
370 if (!eth_current)
371 return;
372
373 eth_current->halt(eth_current);
374
375 eth_current->state = ETH_STATE_PASSIVE;
376}
377
378int eth_send(volatile void *packet, int length)
379{
380 if (!eth_current)
381 return -1;
382
383 return eth_current->send(eth_current, packet, length);
384}
385
386int eth_rx(void)
387{
388 if (!eth_current)
389 return -1;
390
391 return eth_current->recv(eth_current);
392}
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100393
394#ifdef CONFIG_API
395static void eth_save_packet(volatile void *packet, int length)
396{
397 volatile char *p = packet;
398 int i;
399
400 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
401 return;
402
403 if (PKTSIZE < length)
404 return;
405
406 for (i = 0; i < length; i++)
407 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
408
409 eth_rcv_bufs[eth_rcv_last].length = length;
410 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
411}
412
413int eth_receive(volatile void *packet, int length)
414{
415 volatile char *p = packet;
416 void *pp = push_packet;
417 int i;
418
419 if (eth_rcv_current == eth_rcv_last) {
420 push_packet = eth_save_packet;
421 eth_rx();
422 push_packet = pp;
423
424 if (eth_rcv_current == eth_rcv_last)
425 return -1;
426 }
427
428 if (length < eth_rcv_bufs[eth_rcv_current].length)
429 return -1;
430
431 length = eth_rcv_bufs[eth_rcv_current].length;
432
433 for (i = 0; i < length; i++)
434 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
435
436 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
437 return length;
438}
439#endif /* CONFIG_API */
wdenkc6097192002-11-03 00:24:07 +0000440
441void eth_try_another(int first_restart)
442{
443 static struct eth_device *first_failed = NULL;
Matthias Fuchs204f0ec2008-01-17 07:45:05 +0100444 char *ethrotate;
445
446 /*
447 * Do not rotate between network interfaces when
448 * 'ethrotate' variable is set to 'no'.
449 */
450 if (((ethrotate = getenv ("ethrotate")) != NULL) &&
451 (strcmp(ethrotate, "no") == 0))
452 return;
wdenkc6097192002-11-03 00:24:07 +0000453
454 if (!eth_current)
455 return;
456
wdenk63a10be2004-05-08 20:33:20 +0000457 if (first_restart) {
wdenkc6097192002-11-03 00:24:07 +0000458 first_failed = eth_current;
459 }
460
461 eth_current = eth_current->next;
462
wdenk145d2c12004-04-15 21:48:45 +0000463#ifdef CONFIG_NET_MULTI
464 /* update current ethernet name */
465 {
466 char *act = getenv("ethact");
467 if (act == NULL || strcmp(act, eth_current->name) != 0)
468 setenv("ethact", eth_current->name);
469 }
470#endif
471
wdenk63a10be2004-05-08 20:33:20 +0000472 if (first_failed == eth_current) {
wdenkc6097192002-11-03 00:24:07 +0000473 NetRestartWrap = 1;
474 }
475}
476
wdenk145d2c12004-04-15 21:48:45 +0000477#ifdef CONFIG_NET_MULTI
478void eth_set_current(void)
479{
480 char *act;
481 struct eth_device* old_current;
482
483 if (!eth_current) /* XXX no current */
484 return;
485
486 act = getenv("ethact");
487 if (act != NULL) {
488 old_current = eth_current;
489 do {
490 if (strcmp(eth_current->name, act) == 0)
491 return;
492 eth_current = eth_current->next;
493 } while (old_current != eth_current);
494 }
495
496 setenv("ethact", eth_current->name);
497}
498#endif
499
wdenkc6abb7e2003-11-17 21:14:37 +0000500char *eth_get_name (void)
501{
502 return (eth_current ? eth_current->name : "unknown");
503}
Jon Loeliger54f35c22007-07-09 17:45:14 -0500504#elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200505
506extern int at91rm9200_miiphy_initialize(bd_t *bis);
507extern int emac4xx_miiphy_initialize(bd_t *bis);
508extern int mcf52x2_miiphy_initialize(bd_t *bis);
509extern int ns7520_miiphy_initialize(bd_t *bis);
Jean-Christophe PLAGNIOL-VILLARDbcf268b2008-08-31 04:45:42 +0200510extern int davinci_eth_miiphy_initialize(bd_t *bis);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200511
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200512
513int eth_initialize(bd_t *bis)
514{
Jon Loeliger54f35c22007-07-09 17:45:14 -0500515#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100516 miiphy_init();
517#endif
518
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200519#if defined(CONFIG_AT91RM9200)
520 at91rm9200_miiphy_initialize(bis);
521#endif
522#if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) \
523 && !defined(CONFIG_AP1000) && !defined(CONFIG_405)
524 emac4xx_miiphy_initialize(bis);
525#endif
526#if defined(CONFIG_MCF52x2)
527 mcf52x2_miiphy_initialize(bis);
528#endif
Wolfgang Denk81490f42008-07-13 23:07:35 +0200529#if defined(CONFIG_DRIVER_NS7520_ETHERNET)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200530 ns7520_miiphy_initialize(bis);
531#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200532#if defined(CONFIG_DRIVER_TI_EMAC)
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200533 davinci_eth_miiphy_initialize(bis);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200534#endif
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200535 return 0;
536}
wdenkc6097192002-11-03 00:24:07 +0000537#endif