]>
git.ipfire.org Git - people/ms/u-boot.git/blob - net/eth.c
2 * (C) Copyright 2001-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 void eth_parse_enetaddr(const char *addr
, uchar
*enetaddr
)
35 for (i
= 0; i
< 6; ++i
) {
36 enetaddr
[i
] = addr
? simple_strtoul(addr
, &end
, 16) : 0;
38 addr
= (*end
) ? end
+ 1 : end
;
42 int eth_getenv_enetaddr(char *name
, uchar
*enetaddr
)
44 eth_parse_enetaddr(getenv(name
), enetaddr
);
45 return is_valid_ether_addr(enetaddr
);
48 int eth_setenv_enetaddr(char *name
, const uchar
*enetaddr
)
52 sprintf(buf
, "%pM", enetaddr
);
54 return setenv(name
, buf
);
58 #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
61 * CPU and board-specific Ethernet initializations. Aliased function
62 * signals caller to move on
64 static int __def_eth_init(bd_t
*bis
)
68 int cpu_eth_init(bd_t
*bis
) __attribute__((weak
, alias("__def_eth_init")));
69 int board_eth_init(bd_t
*bis
) __attribute__((weak
, alias("__def_eth_init")));
71 extern int mv6436x_eth_initialize(bd_t
*);
72 extern int mv6446x_eth_initialize(bd_t
*);
75 extern void (*push_packet
)(volatile void *, int);
80 } eth_rcv_bufs
[PKTBUFSRX
];
82 static unsigned int eth_rcv_current
= 0, eth_rcv_last
= 0;
85 static struct eth_device
*eth_devices
, *eth_current
;
87 struct eth_device
*eth_get_dev(void)
92 struct eth_device
*eth_get_dev_by_name(char *devname
)
94 struct eth_device
*dev
, *target_dev
;
102 if (strcmp(devname
, dev
->name
) == 0) {
107 } while (dev
!= eth_devices
);
112 struct eth_device
*eth_get_dev_by_index(int index
)
114 struct eth_device
*dev
, *target_dev
;
129 } while (dev
!= eth_devices
);
134 int eth_get_dev_index (void)
136 struct eth_device
*dev
;
143 for (dev
= eth_devices
; dev
; dev
= dev
->next
) {
144 if (dev
== eth_current
)
156 int eth_register(struct eth_device
* dev
)
158 struct eth_device
*d
;
161 eth_current
= eth_devices
= dev
;
162 #ifdef CONFIG_NET_MULTI
163 /* update current ethernet name */
165 char *act
= getenv("ethact");
166 if (act
== NULL
|| strcmp(act
, eth_current
->name
) != 0)
167 setenv("ethact", eth_current
->name
);
171 for (d
=eth_devices
; d
->next
!=eth_devices
; d
=d
->next
);
175 dev
->state
= ETH_STATE_INIT
;
176 dev
->next
= eth_devices
;
181 int eth_initialize(bd_t
*bis
)
184 unsigned char env_enetaddr
[6];
190 show_boot_progress (64);
191 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
194 /* 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)
199 #if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
200 mv6436x_eth_initialize(bis
);
202 #if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
203 mv6446x_eth_initialize(bis
);
206 puts ("No ethernet found.\n");
207 show_boot_progress (-64);
209 struct eth_device
*dev
= eth_devices
;
210 char *ethprime
= getenv ("ethprime");
212 show_boot_progress (65);
217 printf("%s", dev
->name
);
219 if (ethprime
&& strcmp (dev
->name
, ethprime
) == 0) {
224 sprintf(enetvar
, eth_number
? "eth%daddr" : "ethaddr", eth_number
);
225 eth_getenv_enetaddr(enetvar
, env_enetaddr
);
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))
231 printf ("\nWarning: %s MAC addresses don't match:\n",
233 printf ("Address in SROM is %pM\n",
235 printf ("Address in environment is %pM\n",
239 memcpy(dev
->enetaddr
, env_enetaddr
, 6);
244 } while(dev
!= eth_devices
);
246 #ifdef CONFIG_NET_MULTI
247 /* update current ethernet name */
249 char *act
= getenv("ethact");
250 if (act
== NULL
|| strcmp(act
, eth_current
->name
) != 0)
251 setenv("ethact", eth_current
->name
);
253 setenv("ethact", NULL
);
262 void eth_set_enetaddr(int num
, char *addr
) {
263 struct eth_device
*dev
;
264 unsigned char enetaddr
[6];
266 debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num
, addr
);
271 eth_parse_enetaddr(addr
, enetaddr
);
277 if (dev
== eth_devices
)
281 debug ( "Setting new HW address on %s\n"
282 "New Address is %pM\n",
283 dev
->name
, enetaddr
);
285 memcpy(dev
->enetaddr
, enetaddr
, 6);
287 #ifdef CONFIG_MCAST_TFTP
289 * mcast_addr: multicast ipaddr from which multicast Mac is made
290 * join: 1=join, 0=leave.
292 int eth_mcast_join( IPaddr_t mcast_ip
, u8 join
)
295 if (!eth_current
|| !eth_current
->mcast
)
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;
303 return eth_current
->mcast(eth_current
, mcast_mac
, join
);
306 /* 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
308 * some other adapter -- hash tables
310 #define CRCPOLY_LE 0xedb88320
311 u32
ether_crc (size_t len
, unsigned char const *p
)
318 for (i
= 0; i
< 8; i
++)
319 crc
= (crc
>> 1) ^ ((crc
& 1) ? CRCPOLY_LE
: 0);
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);
333 int eth_init(bd_t
*bis
)
335 struct eth_device
* old_current
;
338 puts ("No ethernet found.\n");
342 old_current
= eth_current
;
344 debug ("Trying %s\n", eth_current
->name
);
346 if (eth_current
->init(eth_current
,bis
) >= 0) {
347 eth_current
->state
= ETH_STATE_ACTIVE
;
354 } while (old_current
!= eth_current
);
364 eth_current
->halt(eth_current
);
366 eth_current
->state
= ETH_STATE_PASSIVE
;
369 int eth_send(volatile void *packet
, int length
)
374 return eth_current
->send(eth_current
, packet
, length
);
382 return eth_current
->recv(eth_current
);
386 static void eth_save_packet(volatile void *packet
, int length
)
388 volatile char *p
= packet
;
391 if ((eth_rcv_last
+1) % PKTBUFSRX
== eth_rcv_current
)
394 if (PKTSIZE
< length
)
397 for (i
= 0; i
< length
; i
++)
398 eth_rcv_bufs
[eth_rcv_last
].data
[i
] = p
[i
];
400 eth_rcv_bufs
[eth_rcv_last
].length
= length
;
401 eth_rcv_last
= (eth_rcv_last
+ 1) % PKTBUFSRX
;
404 int eth_receive(volatile void *packet
, int length
)
406 volatile char *p
= packet
;
407 void *pp
= push_packet
;
410 if (eth_rcv_current
== eth_rcv_last
) {
411 push_packet
= eth_save_packet
;
415 if (eth_rcv_current
== eth_rcv_last
)
419 if (length
< eth_rcv_bufs
[eth_rcv_current
].length
)
422 length
= eth_rcv_bufs
[eth_rcv_current
].length
;
424 for (i
= 0; i
< length
; i
++)
425 p
[i
] = eth_rcv_bufs
[eth_rcv_current
].data
[i
];
427 eth_rcv_current
= (eth_rcv_current
+ 1) % PKTBUFSRX
;
430 #endif /* CONFIG_API */
432 void eth_try_another(int first_restart
)
434 static struct eth_device
*first_failed
= NULL
;
438 * Do not rotate between network interfaces when
439 * 'ethrotate' variable is set to 'no'.
441 if (((ethrotate
= getenv ("ethrotate")) != NULL
) &&
442 (strcmp(ethrotate
, "no") == 0))
449 first_failed
= eth_current
;
452 eth_current
= eth_current
->next
;
454 #ifdef CONFIG_NET_MULTI
455 /* update current ethernet name */
457 char *act
= getenv("ethact");
458 if (act
== NULL
|| strcmp(act
, eth_current
->name
) != 0)
459 setenv("ethact", eth_current
->name
);
463 if (first_failed
== eth_current
) {
468 #ifdef CONFIG_NET_MULTI
469 void eth_set_current(void)
471 static char *act
= NULL
;
472 static int env_changed_id
= 0;
473 struct eth_device
* old_current
;
476 if (!eth_current
) /* XXX no current */
479 env_id
= get_env_id();
480 if ((act
== NULL
) || (env_changed_id
!= env_id
)) {
481 act
= getenv("ethact");
482 env_changed_id
= env_id
;
485 old_current
= eth_current
;
487 if (strcmp(eth_current
->name
, act
) == 0)
489 eth_current
= eth_current
->next
;
490 } while (old_current
!= eth_current
);
493 setenv("ethact", eth_current
->name
);
497 char *eth_get_name (void)
499 return (eth_current
? eth_current
->name
: "unknown");
501 #elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
503 extern int at91rm9200_miiphy_initialize(bd_t
*bis
);
504 extern int emac4xx_miiphy_initialize(bd_t
*bis
);
505 extern int mcf52x2_miiphy_initialize(bd_t
*bis
);
506 extern int ns7520_miiphy_initialize(bd_t
*bis
);
507 extern int davinci_eth_miiphy_initialize(bd_t
*bis
);
510 int eth_initialize(bd_t
*bis
)
512 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
516 #if defined(CONFIG_AT91RM9200)
517 at91rm9200_miiphy_initialize(bis
);
519 #if defined(CONFIG_PPC4xx_EMAC)
520 emac4xx_miiphy_initialize(bis
);
522 #if defined(CONFIG_MCF52x2)
523 mcf52x2_miiphy_initialize(bis
);
525 #if defined(CONFIG_DRIVER_NS7520_ETHERNET)
526 ns7520_miiphy_initialize(bis
);
528 #if defined(CONFIG_DRIVER_TI_EMAC)
529 davinci_eth_miiphy_initialize(bis
);