2 * (C) Copyright 2001-2015
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * Joe Hershberger, National Instruments
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <environment.h>
14 #include <asm/errno.h>
15 #include "eth_internal.h"
17 DECLARE_GLOBAL_DATA_PTR;
20 * CPU and board-specific Ethernet initializations. Aliased function
21 * signals caller to move on
23 static int __def_eth_init(bd_t *bis)
27 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
28 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
34 } eth_rcv_bufs[PKTBUFSRX];
36 static unsigned int eth_rcv_current, eth_rcv_last;
39 static struct eth_device *eth_devices;
40 struct eth_device *eth_current;
42 void eth_set_current_to_next(void)
44 eth_current = eth_current->next;
47 void eth_set_dev(struct eth_device *dev)
52 struct eth_device *eth_get_dev_by_name(const char *devname)
54 struct eth_device *dev, *target_dev;
56 BUG_ON(devname == NULL);
64 if (strcmp(devname, dev->name) == 0) {
69 } while (dev != eth_devices);
74 struct eth_device *eth_get_dev_by_index(int index)
76 struct eth_device *dev, *target_dev;
84 if (dev->index == index) {
89 } while (dev != eth_devices);
94 int eth_get_dev_index(void)
99 return eth_current->index;
102 static int on_ethaddr(const char *name, const char *value, enum env_op op,
106 struct eth_device *dev;
111 /* look for an index after "eth" */
112 index = simple_strtoul(name + 3, NULL, 10);
116 if (dev->index == index) {
119 case env_op_overwrite:
120 eth_parse_enetaddr(value, dev->enetaddr);
123 memset(dev->enetaddr, 0, 6);
127 } while (dev != eth_devices);
131 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
133 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
136 unsigned char env_enetaddr[6];
139 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
141 if (!is_zero_ethaddr(env_enetaddr)) {
142 if (!is_zero_ethaddr(dev->enetaddr) &&
143 memcmp(dev->enetaddr, env_enetaddr, 6)) {
144 printf("\nWarning: %s MAC addresses don't match:\n",
146 printf("Address in SROM is %pM\n",
148 printf("Address in environment is %pM\n",
152 memcpy(dev->enetaddr, env_enetaddr, 6);
153 } else if (is_valid_ethaddr(dev->enetaddr)) {
154 eth_setenv_enetaddr_by_index(base_name, eth_number,
156 } else if (is_zero_ethaddr(dev->enetaddr)) {
157 #ifdef CONFIG_NET_RANDOM_ETHADDR
158 net_random_ethaddr(dev->enetaddr);
159 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
160 dev->name, eth_number, dev->enetaddr);
162 printf("\nError: %s address not set.\n",
168 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
169 if (!is_valid_ethaddr(dev->enetaddr)) {
170 printf("\nError: %s address %pM illegal value\n",
171 dev->name, dev->enetaddr);
175 ret = dev->write_hwaddr(dev);
177 printf("\nWarning: %s failed to set MAC address\n",
184 int eth_register(struct eth_device *dev)
186 struct eth_device *d;
189 assert(strlen(dev->name) < sizeof(dev->name));
194 eth_current_changed();
196 for (d = eth_devices; d->next != eth_devices; d = d->next)
201 dev->state = ETH_STATE_INIT;
202 dev->next = eth_devices;
203 dev->index = index++;
208 int eth_unregister(struct eth_device *dev)
210 struct eth_device *cur;
216 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
220 /* Device not found */
221 if (cur->next != dev)
224 cur->next = dev->next;
226 if (eth_devices == dev)
227 eth_devices = dev->next == eth_devices ? NULL : dev->next;
229 if (eth_current == dev) {
230 eth_current = eth_devices;
231 eth_current_changed();
237 int eth_initialize(void)
245 * If board-specific initialization exists, call it.
246 * If not, call a CPU-specific one
248 if (board_eth_init != __def_eth_init) {
249 if (board_eth_init(gd->bd) < 0)
250 printf("Board Net Initialization Failed\n");
251 } else if (cpu_eth_init != __def_eth_init) {
252 if (cpu_eth_init(gd->bd) < 0)
253 printf("CPU Net Initialization Failed\n");
255 printf("Net Initialization Skipped\n");
259 puts("No ethernet found.\n");
260 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
262 struct eth_device *dev = eth_devices;
263 char *ethprime = getenv("ethprime");
265 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
270 printf("%s", dev->name);
272 if (ethprime && strcmp(dev->name, ethprime) == 0) {
277 if (strchr(dev->name, ' '))
278 puts("\nWarning: eth device name has a space!"
281 eth_write_hwaddr(dev, "eth", dev->index);
285 } while (dev != eth_devices);
287 eth_current_changed();
294 #ifdef CONFIG_MCAST_TFTP
296 * mcast_addr: multicast ipaddr from which multicast Mac is made
297 * join: 1=join, 0=leave.
299 int eth_mcast_join(struct in_addr mcast_ip, int join)
302 if (!eth_current || !eth_current->mcast)
304 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
305 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
306 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
310 return eth_current->mcast(eth_current, mcast_mac, join);
313 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
314 * and this is the ethernet-crc method needed for TSEC -- and perhaps
315 * some other adapter -- hash tables
317 #define CRCPOLY_LE 0xedb88320
318 u32 ether_crc(size_t len, unsigned char const *p)
325 for (i = 0; i < 8; i++)
326 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
328 /* an reverse the bits, cuz of way they arrive -- last-first */
329 crc = (crc >> 16) | (crc << 16);
330 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
331 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
332 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
333 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
342 struct eth_device *old_current;
345 puts("No ethernet found.\n");
349 old_current = eth_current;
351 debug("Trying %s\n", eth_current->name);
353 if (eth_current->init(eth_current, gd->bd) >= 0) {
354 eth_current->state = ETH_STATE_ACTIVE;
361 } while (old_current != eth_current);
371 eth_current->halt(eth_current);
373 eth_current->state = ETH_STATE_PASSIVE;
376 int eth_is_active(struct eth_device *dev)
378 return dev && dev->state == ETH_STATE_ACTIVE;
381 int eth_send(void *packet, int length)
386 return eth_current->send(eth_current, packet, length);
394 return eth_current->recv(eth_current);
398 static void eth_save_packet(void *packet, int length)
403 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
406 if (PKTSIZE < length)
409 for (i = 0; i < length; i++)
410 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
412 eth_rcv_bufs[eth_rcv_last].length = length;
413 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
416 int eth_receive(void *packet, int length)
419 void *pp = push_packet;
422 if (eth_rcv_current == eth_rcv_last) {
423 push_packet = eth_save_packet;
427 if (eth_rcv_current == eth_rcv_last)
431 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
433 for (i = 0; i < length; i++)
434 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
436 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
439 #endif /* CONFIG_API */