2 * (C) Copyright 2001-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/errno.h>
15 void eth_parse_enetaddr(const char *addr
, uchar
*enetaddr
)
20 for (i
= 0; i
< 6; ++i
) {
21 enetaddr
[i
] = addr
? simple_strtoul(addr
, &end
, 16) : 0;
23 addr
= (*end
) ? end
+ 1 : end
;
27 int eth_getenv_enetaddr(char *name
, uchar
*enetaddr
)
29 eth_parse_enetaddr(getenv(name
), enetaddr
);
30 return is_valid_ether_addr(enetaddr
);
33 int eth_setenv_enetaddr(char *name
, const uchar
*enetaddr
)
37 sprintf(buf
, "%pM", enetaddr
);
39 return setenv(name
, buf
);
42 int eth_getenv_enetaddr_by_index(const char *base_name
, int index
,
46 sprintf(enetvar
, index
? "%s%daddr" : "%saddr", base_name
, index
);
47 return eth_getenv_enetaddr(enetvar
, enetaddr
);
50 static inline int eth_setenv_enetaddr_by_index(const char *base_name
, int index
,
54 sprintf(enetvar
, index
? "%s%daddr" : "%saddr", base_name
, index
);
55 return eth_setenv_enetaddr(enetvar
, enetaddr
);
58 static void eth_env_init(void)
62 s
= getenv("bootfile");
64 copy_filename(BootFile
, s
, sizeof(BootFile
));
67 static int eth_mac_skip(int index
)
71 sprintf(enetvar
, index
? "eth%dmacskip" : "ethmacskip", index
);
72 return ((skip_state
= getenv(enetvar
)) != NULL
);
75 static void eth_current_changed(void);
78 * CPU and board-specific Ethernet initializations. Aliased function
79 * signals caller to move on
81 static int __def_eth_init(bd_t
*bis
)
85 int cpu_eth_init(bd_t
*bis
) __attribute__((weak
, alias("__def_eth_init")));
86 int board_eth_init(bd_t
*bis
) __attribute__((weak
, alias("__def_eth_init")));
92 } eth_rcv_bufs
[PKTBUFSRX
];
94 static unsigned int eth_rcv_current
, eth_rcv_last
;
97 static struct eth_device
*eth_devices
;
98 struct eth_device
*eth_current
;
100 static void eth_set_current_to_next(void)
102 eth_current
= eth_current
->next
;
105 struct eth_device
*eth_get_dev_by_name(const char *devname
)
107 struct eth_device
*dev
, *target_dev
;
109 BUG_ON(devname
== NULL
);
117 if (strcmp(devname
, dev
->name
) == 0) {
122 } while (dev
!= eth_devices
);
127 struct eth_device
*eth_get_dev_by_index(int index
)
129 struct eth_device
*dev
, *target_dev
;
137 if (dev
->index
== index
) {
142 } while (dev
!= eth_devices
);
147 int eth_get_dev_index(void)
152 return eth_current
->index
;
155 int eth_write_hwaddr(struct eth_device
*dev
, const char *base_name
,
158 unsigned char env_enetaddr
[6];
161 eth_getenv_enetaddr_by_index(base_name
, eth_number
, env_enetaddr
);
163 if (!is_zero_ether_addr(env_enetaddr
)) {
164 if (!is_zero_ether_addr(dev
->enetaddr
) &&
165 memcmp(dev
->enetaddr
, env_enetaddr
, 6)) {
166 printf("\nWarning: %s MAC addresses don't match:\n",
168 printf("Address in SROM is %pM\n",
170 printf("Address in environment is %pM\n",
174 memcpy(dev
->enetaddr
, env_enetaddr
, 6);
175 } else if (is_valid_ether_addr(dev
->enetaddr
)) {
176 eth_setenv_enetaddr_by_index(base_name
, eth_number
,
178 printf("\nWarning: %s using MAC address from net device\n",
180 } else if (is_zero_ether_addr(dev
->enetaddr
)) {
181 printf("\nError: %s address not set.\n",
186 if (dev
->write_hwaddr
&& !eth_mac_skip(eth_number
)) {
187 if (!is_valid_ether_addr(dev
->enetaddr
)) {
188 printf("\nError: %s address %pM illegal value\n",
189 dev
->name
, dev
->enetaddr
);
193 ret
= dev
->write_hwaddr(dev
);
195 printf("\nWarning: %s failed to set MAC address\n", dev
->name
);
201 int eth_register(struct eth_device
*dev
)
203 struct eth_device
*d
;
206 assert(strlen(dev
->name
) < sizeof(dev
->name
));
209 eth_current
= eth_devices
= dev
;
210 eth_current_changed();
212 for (d
= eth_devices
; d
->next
!= eth_devices
; d
= d
->next
)
217 dev
->state
= ETH_STATE_INIT
;
218 dev
->next
= eth_devices
;
219 dev
->index
= index
++;
224 int eth_unregister(struct eth_device
*dev
)
226 struct eth_device
*cur
;
232 for (cur
= eth_devices
; cur
->next
!= eth_devices
&& cur
->next
!= dev
;
236 /* Device not found */
237 if (cur
->next
!= dev
)
240 cur
->next
= dev
->next
;
242 if (eth_devices
== dev
)
243 eth_devices
= dev
->next
== eth_devices
? NULL
: dev
->next
;
245 if (eth_current
== dev
) {
246 eth_current
= eth_devices
;
247 eth_current_changed();
253 int eth_initialize(bd_t
*bis
)
259 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START
);
260 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
271 * If board-specific initialization exists, call it.
272 * If not, call a CPU-specific one
274 if (board_eth_init
!= __def_eth_init
) {
275 if (board_eth_init(bis
) < 0)
276 printf("Board Net Initialization Failed\n");
277 } else if (cpu_eth_init
!= __def_eth_init
) {
278 if (cpu_eth_init(bis
) < 0)
279 printf("CPU Net Initialization Failed\n");
281 printf("Net Initialization Skipped\n");
284 puts("No ethernet found.\n");
285 bootstage_error(BOOTSTAGE_ID_NET_ETH_START
);
287 struct eth_device
*dev
= eth_devices
;
288 char *ethprime
= getenv("ethprime");
290 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT
);
295 printf("%s", dev
->name
);
297 if (ethprime
&& strcmp(dev
->name
, ethprime
) == 0) {
302 if (strchr(dev
->name
, ' '))
303 puts("\nWarning: eth device name has a space!"
306 eth_write_hwaddr(dev
, "eth", dev
->index
);
310 } while (dev
!= eth_devices
);
312 eth_current_changed();
319 #ifdef CONFIG_MCAST_TFTP
321 * mcast_addr: multicast ipaddr from which multicast Mac is made
322 * join: 1=join, 0=leave.
324 int eth_mcast_join(IPaddr_t mcast_ip
, u8 join
)
327 if (!eth_current
|| !eth_current
->mcast
)
329 mcast_mac
[5] = htonl(mcast_ip
) & 0xff;
330 mcast_mac
[4] = (htonl(mcast_ip
)>>8) & 0xff;
331 mcast_mac
[3] = (htonl(mcast_ip
)>>16) & 0x7f;
335 return eth_current
->mcast(eth_current
, mcast_mac
, join
);
338 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
339 * and this is the ethernet-crc method needed for TSEC -- and perhaps
340 * some other adapter -- hash tables
342 #define CRCPOLY_LE 0xedb88320
343 u32
ether_crc(size_t len
, unsigned char const *p
)
350 for (i
= 0; i
< 8; i
++)
351 crc
= (crc
>> 1) ^ ((crc
& 1) ? CRCPOLY_LE
: 0);
353 /* an reverse the bits, cuz of way they arrive -- last-first */
354 crc
= (crc
>> 16) | (crc
<< 16);
355 crc
= (crc
>> 8 & 0x00ff00ff) | (crc
<< 8 & 0xff00ff00);
356 crc
= (crc
>> 4 & 0x0f0f0f0f) | (crc
<< 4 & 0xf0f0f0f0);
357 crc
= (crc
>> 2 & 0x33333333) | (crc
<< 2 & 0xcccccccc);
358 crc
= (crc
>> 1 & 0x55555555) | (crc
<< 1 & 0xaaaaaaaa);
365 int eth_init(bd_t
*bis
)
367 struct eth_device
*old_current
, *dev
;
370 puts("No ethernet found.\n");
374 /* Sync environment with network devices */
377 uchar env_enetaddr
[6];
379 if (eth_getenv_enetaddr_by_index("eth", dev
->index
,
381 memcpy(dev
->enetaddr
, env_enetaddr
, 6);
384 } while (dev
!= eth_devices
);
386 old_current
= eth_current
;
388 debug("Trying %s\n", eth_current
->name
);
390 if (eth_current
->init(eth_current
, bis
) >= 0) {
391 eth_current
->state
= ETH_STATE_ACTIVE
;
398 } while (old_current
!= eth_current
);
408 eth_current
->halt(eth_current
);
410 eth_current
->state
= ETH_STATE_PASSIVE
;
413 int eth_send(void *packet
, int length
)
418 return eth_current
->send(eth_current
, packet
, length
);
426 return eth_current
->recv(eth_current
);
430 static void eth_save_packet(void *packet
, int length
)
435 if ((eth_rcv_last
+1) % PKTBUFSRX
== eth_rcv_current
)
438 if (PKTSIZE
< length
)
441 for (i
= 0; i
< length
; i
++)
442 eth_rcv_bufs
[eth_rcv_last
].data
[i
] = p
[i
];
444 eth_rcv_bufs
[eth_rcv_last
].length
= length
;
445 eth_rcv_last
= (eth_rcv_last
+ 1) % PKTBUFSRX
;
448 int eth_receive(void *packet
, int length
)
451 void *pp
= push_packet
;
454 if (eth_rcv_current
== eth_rcv_last
) {
455 push_packet
= eth_save_packet
;
459 if (eth_rcv_current
== eth_rcv_last
)
463 length
= min(eth_rcv_bufs
[eth_rcv_current
].length
, length
);
465 for (i
= 0; i
< length
; i
++)
466 p
[i
] = eth_rcv_bufs
[eth_rcv_current
].data
[i
];
468 eth_rcv_current
= (eth_rcv_current
+ 1) % PKTBUFSRX
;
471 #endif /* CONFIG_API */
473 static void eth_current_changed(void)
475 char *act
= getenv("ethact");
476 /* update current ethernet name */
478 if (act
== NULL
|| strcmp(act
, eth_get_name()) != 0)
479 setenv("ethact", eth_get_name());
482 * remove the variable completely if there is no active
485 else if (act
!= NULL
)
486 setenv("ethact", NULL
);
489 void eth_try_another(int first_restart
)
491 static struct eth_device
*first_failed
;
495 * Do not rotate between network interfaces when
496 * 'ethrotate' variable is set to 'no'.
498 ethrotate
= getenv("ethrotate");
499 if ((ethrotate
!= NULL
) && (strcmp(ethrotate
, "no") == 0))
506 first_failed
= eth_get_dev();
508 eth_set_current_to_next();
510 eth_current_changed();
512 if (first_failed
== eth_get_dev())
516 void eth_set_current(void)
519 static int env_changed_id
;
520 struct eth_device
*old_current
;
523 if (!eth_get_dev()) /* XXX no current */
526 env_id
= get_env_id();
527 if ((act
== NULL
) || (env_changed_id
!= env_id
)) {
528 act
= getenv("ethact");
529 env_changed_id
= env_id
;
532 old_current
= eth_get_dev();
534 if (strcmp(eth_get_name(), act
) == 0)
536 eth_set_current_to_next();
537 } while (old_current
!= eth_get_dev());
540 eth_current_changed();
543 const char *eth_get_name(void)
545 return eth_get_dev() ? eth_get_dev()->name
: "unknown";