blob: b7ef09f4479b0619b3b79c50d953abb606950247 [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 mv6436x_eth_initialize(bd_t *);
43extern int mv6446x_eth_initialize(bd_t *);
wdenkc6097192002-11-03 00:24:07 +000044
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010045#ifdef CONFIG_API
46extern void (*push_packet)(volatile void *, int);
47
48static struct {
49 uchar data[PKTSIZE];
50 int length;
51} eth_rcv_bufs[PKTBUFSRX];
52
53static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
54#endif
55
wdenkc6097192002-11-03 00:24:07 +000056static struct eth_device *eth_devices, *eth_current;
57
58struct eth_device *eth_get_dev(void)
59{
60 return eth_current;
61}
62
Marian Balakowiczaab8c492005-10-28 22:30:33 +020063struct eth_device *eth_get_dev_by_name(char *devname)
64{
65 struct eth_device *dev, *target_dev;
66
67 if (!eth_devices)
68 return NULL;
69
70 dev = eth_devices;
71 target_dev = NULL;
72 do {
73 if (strcmp(devname, dev->name) == 0) {
74 target_dev = dev;
75 break;
76 }
77 dev = dev->next;
78 } while (dev != eth_devices);
79
80 return target_dev;
81}
82
wdenkc6097192002-11-03 00:24:07 +000083int eth_get_dev_index (void)
84{
85 struct eth_device *dev;
86 int num = 0;
87
88 if (!eth_devices) {
89 return (-1);
90 }
91
92 for (dev = eth_devices; dev; dev = dev->next) {
93 if (dev == eth_current)
94 break;
95 ++num;
96 }
97
98 if (dev) {
99 return (num);
100 }
101
102 return (0);
103}
104
105int eth_register(struct eth_device* dev)
106{
107 struct eth_device *d;
108
109 if (!eth_devices) {
110 eth_current = eth_devices = dev;
wdenk145d2c12004-04-15 21:48:45 +0000111#ifdef CONFIG_NET_MULTI
112 /* update current ethernet name */
113 {
114 char *act = getenv("ethact");
115 if (act == NULL || strcmp(act, eth_current->name) != 0)
116 setenv("ethact", eth_current->name);
117 }
118#endif
wdenkc6097192002-11-03 00:24:07 +0000119 } else {
120 for (d=eth_devices; d->next!=eth_devices; d=d->next);
121 d->next = dev;
122 }
123
124 dev->state = ETH_STATE_INIT;
125 dev->next = eth_devices;
126
127 return 0;
128}
129
130int eth_initialize(bd_t *bis)
131{
Shinya Kuribayashidb7101e2007-11-19 20:27:04 +0900132 char enetvar[32];
133 unsigned char env_enetaddr[6];
wdenkc6097192002-11-03 00:24:07 +0000134 int i, eth_number = 0;
135 char *tmp, *end;
136
137 eth_devices = NULL;
138 eth_current = NULL;
139
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200140 show_boot_progress (64);
Jon Loeliger54f35c22007-07-09 17:45:14 -0500141#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100142 miiphy_init();
143#endif
Ben Warrend448a492008-06-23 22:57:27 -0700144 /* Try board-specific initialization first. If it fails or isn't
145 * present, try the cpu-specific initialization */
146 if (board_eth_init(bis) < 0)
147 cpu_eth_init(bis);
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100148
Stefan Roese45993ea2006-11-29 15:42:37 +0100149#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
wdenk5da7f2f2004-01-03 00:43:19 +0000150 mv6436x_eth_initialize(bis);
151#endif
Stefan Roese45993ea2006-11-29 15:42:37 +0100152#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
wdenk5da7f2f2004-01-03 00:43:19 +0000153 mv6446x_eth_initialize(bis);
154#endif
wdenkc6097192002-11-03 00:24:07 +0000155 if (!eth_devices) {
156 puts ("No ethernet found.\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200157 show_boot_progress (-64);
wdenkc6097192002-11-03 00:24:07 +0000158 } else {
159 struct eth_device *dev = eth_devices;
160 char *ethprime = getenv ("ethprime");
161
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200162 show_boot_progress (65);
wdenkc6097192002-11-03 00:24:07 +0000163 do {
164 if (eth_number)
165 puts (", ");
166
167 printf("%s", dev->name);
168
169 if (ethprime && strcmp (dev->name, ethprime) == 0) {
170 eth_current = dev;
171 puts (" [PRIME]");
172 }
173
174 sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
175 tmp = getenv (enetvar);
176
177 for (i=0; i<6; i++) {
178 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
179 if (tmp)
180 tmp = (*end) ? end+1 : end;
181 }
182
183 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
184 if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
185 memcmp(dev->enetaddr, env_enetaddr, 6))
186 {
wdenk63a10be2004-05-08 20:33:20 +0000187 printf ("\nWarning: %s MAC addresses don't match:\n",
188 dev->name);
189 printf ("Address in SROM is "
wdenkc6097192002-11-03 00:24:07 +0000190 "%02X:%02X:%02X:%02X:%02X:%02X\n",
191 dev->enetaddr[0], dev->enetaddr[1],
192 dev->enetaddr[2], dev->enetaddr[3],
193 dev->enetaddr[4], dev->enetaddr[5]);
wdenk63a10be2004-05-08 20:33:20 +0000194 printf ("Address in environment is "
wdenkc6097192002-11-03 00:24:07 +0000195 "%02X:%02X:%02X:%02X:%02X:%02X\n",
196 env_enetaddr[0], env_enetaddr[1],
197 env_enetaddr[2], env_enetaddr[3],
198 env_enetaddr[4], env_enetaddr[5]);
199 }
200
201 memcpy(dev->enetaddr, env_enetaddr, 6);
202 }
203
204 eth_number++;
205 dev = dev->next;
206 } while(dev != eth_devices);
207
wdenk145d2c12004-04-15 21:48:45 +0000208#ifdef CONFIG_NET_MULTI
209 /* update current ethernet name */
210 if (eth_current) {
211 char *act = getenv("ethact");
212 if (act == NULL || strcmp(act, eth_current->name) != 0)
213 setenv("ethact", eth_current->name);
214 } else
215 setenv("ethact", NULL);
216#endif
217
wdenkc6097192002-11-03 00:24:07 +0000218 putc ('\n');
219 }
220
221 return eth_number;
222}
223
224void eth_set_enetaddr(int num, char *addr) {
225 struct eth_device *dev;
226 unsigned char enetaddr[6];
227 char *end;
228 int i;
229
wdenk1ebf41e2004-01-02 14:00:00 +0000230 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
231
wdenkc6097192002-11-03 00:24:07 +0000232 if (!eth_devices)
233 return;
234
235 for (i=0; i<6; i++) {
236 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
237 if (addr)
238 addr = (*end) ? end+1 : end;
239 }
240
241 dev = eth_devices;
242 while(num-- > 0) {
243 dev = dev->next;
244
245 if (dev == eth_devices)
246 return;
247 }
248
wdenk1ebf41e2004-01-02 14:00:00 +0000249 debug ( "Setting new HW address on %s\n"
250 "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
251 dev->name,
Wolfgang Denkb6ea6302005-09-25 17:05:57 +0200252 enetaddr[0], enetaddr[1],
253 enetaddr[2], enetaddr[3],
254 enetaddr[4], enetaddr[5]);
wdenkc6097192002-11-03 00:24:07 +0000255
256 memcpy(dev->enetaddr, enetaddr, 6);
257}
David Updegraff7280da72007-06-11 10:41:07 -0500258#ifdef CONFIG_MCAST_TFTP
259/* Multicast.
260 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200261 * join: 1=join, 0=leave.
David Updegraff7280da72007-06-11 10:41:07 -0500262 */
263int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
264{
265 u8 mcast_mac[6];
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200266 if (!eth_current || !eth_current->mcast)
David Updegraff7280da72007-06-11 10:41:07 -0500267 return -1;
268 mcast_mac[5] = htonl(mcast_ip) & 0xff;
269 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
270 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
271 mcast_mac[2] = 0x5e;
272 mcast_mac[1] = 0x0;
273 mcast_mac[0] = 0x1;
274 return eth_current->mcast(eth_current, mcast_mac, join);
275}
276
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200277/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
278 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff7280da72007-06-11 10:41:07 -0500279 * some other adapter -- hash tables
280 */
281#define CRCPOLY_LE 0xedb88320
282u32 ether_crc (size_t len, unsigned char const *p)
283{
284 int i;
285 u32 crc;
286 crc = ~0;
287 while (len--) {
288 crc ^= *p++;
289 for (i = 0; i < 8; i++)
290 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
291 }
292 /* an reverse the bits, cuz of way they arrive -- last-first */
293 crc = (crc >> 16) | (crc << 16);
294 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
295 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
296 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
297 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
298 return crc;
299}
wdenkc6097192002-11-03 00:24:07 +0000300
David Updegraff7280da72007-06-11 10:41:07 -0500301#endif
302
wdenkc6097192002-11-03 00:24:07 +0000303
304int eth_init(bd_t *bis)
305{
306 struct eth_device* old_current;
307
Stefan Roese11da2be2008-03-04 17:40:41 +0100308 if (!eth_current) {
309 puts ("No ethernet found.\n");
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530310 return -1;
Stefan Roese11da2be2008-03-04 17:40:41 +0100311 }
wdenkc6097192002-11-03 00:24:07 +0000312
313 old_current = eth_current;
314 do {
wdenk1ebf41e2004-01-02 14:00:00 +0000315 debug ("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000316
Ben Warrende9fcb52008-01-09 18:15:53 -0500317 if (eth_current->init(eth_current,bis) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000318 eth_current->state = ETH_STATE_ACTIVE;
319
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530320 return 0;
wdenkc6097192002-11-03 00:24:07 +0000321 }
wdenk1ebf41e2004-01-02 14:00:00 +0000322 debug ("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000323
324 eth_try_another(0);
325 } while (old_current != eth_current);
326
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530327 return -1;
wdenkc6097192002-11-03 00:24:07 +0000328}
329
330void eth_halt(void)
331{
332 if (!eth_current)
333 return;
334
335 eth_current->halt(eth_current);
336
337 eth_current->state = ETH_STATE_PASSIVE;
338}
339
340int eth_send(volatile void *packet, int length)
341{
342 if (!eth_current)
343 return -1;
344
345 return eth_current->send(eth_current, packet, length);
346}
347
348int eth_rx(void)
349{
350 if (!eth_current)
351 return -1;
352
353 return eth_current->recv(eth_current);
354}
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100355
356#ifdef CONFIG_API
357static void eth_save_packet(volatile void *packet, int length)
358{
359 volatile char *p = packet;
360 int i;
361
362 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
363 return;
364
365 if (PKTSIZE < length)
366 return;
367
368 for (i = 0; i < length; i++)
369 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
370
371 eth_rcv_bufs[eth_rcv_last].length = length;
372 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
373}
374
375int eth_receive(volatile void *packet, int length)
376{
377 volatile char *p = packet;
378 void *pp = push_packet;
379 int i;
380
381 if (eth_rcv_current == eth_rcv_last) {
382 push_packet = eth_save_packet;
383 eth_rx();
384 push_packet = pp;
385
386 if (eth_rcv_current == eth_rcv_last)
387 return -1;
388 }
389
390 if (length < eth_rcv_bufs[eth_rcv_current].length)
391 return -1;
392
393 length = eth_rcv_bufs[eth_rcv_current].length;
394
395 for (i = 0; i < length; i++)
396 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
397
398 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
399 return length;
400}
401#endif /* CONFIG_API */
wdenkc6097192002-11-03 00:24:07 +0000402
403void eth_try_another(int first_restart)
404{
405 static struct eth_device *first_failed = NULL;
Matthias Fuchs204f0ec2008-01-17 07:45:05 +0100406 char *ethrotate;
407
408 /*
409 * Do not rotate between network interfaces when
410 * 'ethrotate' variable is set to 'no'.
411 */
412 if (((ethrotate = getenv ("ethrotate")) != NULL) &&
413 (strcmp(ethrotate, "no") == 0))
414 return;
wdenkc6097192002-11-03 00:24:07 +0000415
416 if (!eth_current)
417 return;
418
wdenk63a10be2004-05-08 20:33:20 +0000419 if (first_restart) {
wdenkc6097192002-11-03 00:24:07 +0000420 first_failed = eth_current;
421 }
422
423 eth_current = eth_current->next;
424
wdenk145d2c12004-04-15 21:48:45 +0000425#ifdef CONFIG_NET_MULTI
426 /* update current ethernet name */
427 {
428 char *act = getenv("ethact");
429 if (act == NULL || strcmp(act, eth_current->name) != 0)
430 setenv("ethact", eth_current->name);
431 }
432#endif
433
wdenk63a10be2004-05-08 20:33:20 +0000434 if (first_failed == eth_current) {
wdenkc6097192002-11-03 00:24:07 +0000435 NetRestartWrap = 1;
436 }
437}
438
wdenk145d2c12004-04-15 21:48:45 +0000439#ifdef CONFIG_NET_MULTI
440void eth_set_current(void)
441{
442 char *act;
443 struct eth_device* old_current;
444
445 if (!eth_current) /* XXX no current */
446 return;
447
448 act = getenv("ethact");
449 if (act != NULL) {
450 old_current = eth_current;
451 do {
452 if (strcmp(eth_current->name, act) == 0)
453 return;
454 eth_current = eth_current->next;
455 } while (old_current != eth_current);
456 }
457
458 setenv("ethact", eth_current->name);
459}
460#endif
461
wdenkc6abb7e2003-11-17 21:14:37 +0000462char *eth_get_name (void)
463{
464 return (eth_current ? eth_current->name : "unknown");
465}
Jon Loeliger54f35c22007-07-09 17:45:14 -0500466#elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200467
468extern int at91rm9200_miiphy_initialize(bd_t *bis);
469extern int emac4xx_miiphy_initialize(bd_t *bis);
470extern int mcf52x2_miiphy_initialize(bd_t *bis);
471extern int ns7520_miiphy_initialize(bd_t *bis);
Jean-Christophe PLAGNIOL-VILLARDbcf268b2008-08-31 04:45:42 +0200472extern int davinci_eth_miiphy_initialize(bd_t *bis);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200473
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200474
475int eth_initialize(bd_t *bis)
476{
Jon Loeliger54f35c22007-07-09 17:45:14 -0500477#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100478 miiphy_init();
479#endif
480
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200481#if defined(CONFIG_AT91RM9200)
482 at91rm9200_miiphy_initialize(bis);
483#endif
Ben Warren3a918a62008-10-27 23:50:15 -0700484#if defined(CONFIG_PPC4xx_EMAC)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200485 emac4xx_miiphy_initialize(bis);
486#endif
487#if defined(CONFIG_MCF52x2)
488 mcf52x2_miiphy_initialize(bis);
489#endif
Wolfgang Denk81490f42008-07-13 23:07:35 +0200490#if defined(CONFIG_DRIVER_NS7520_ETHERNET)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200491 ns7520_miiphy_initialize(bis);
492#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200493#if defined(CONFIG_DRIVER_TI_EMAC)
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200494 davinci_eth_miiphy_initialize(bis);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200495#endif
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200496 return 0;
497}
wdenkc6097192002-11-03 00:24:07 +0000498#endif