1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
10 #include <environment.h>
13 #include <linux/errno.h>
14 #include "eth_internal.h"
16 DECLARE_GLOBAL_DATA_PTR;
19 * CPU and board-specific Ethernet initializations. Aliased function
20 * signals caller to move on
22 static int __def_eth_init(bd_t *bis)
26 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
27 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
33 } eth_rcv_bufs[PKTBUFSRX];
35 static unsigned int eth_rcv_current, eth_rcv_last;
38 static struct eth_device *eth_devices;
39 struct eth_device *eth_current;
41 void eth_set_current_to_next(void)
43 eth_current = eth_current->next;
46 void eth_set_dev(struct eth_device *dev)
51 struct eth_device *eth_get_dev_by_name(const char *devname)
53 struct eth_device *dev, *target_dev;
55 BUG_ON(devname == NULL);
63 if (strcmp(devname, dev->name) == 0) {
68 } while (dev != eth_devices);
73 struct eth_device *eth_get_dev_by_index(int index)
75 struct eth_device *dev, *target_dev;
83 if (dev->index == index) {
88 } while (dev != eth_devices);
93 int eth_get_dev_index(void)
98 return eth_current->index;
101 static int on_ethaddr(const char *name, const char *value, enum env_op op,
105 struct eth_device *dev;
110 /* look for an index after "eth" */
111 index = simple_strtoul(name + 3, NULL, 10);
115 if (dev->index == index) {
118 case env_op_overwrite:
119 eth_parse_enetaddr(value, dev->enetaddr);
120 eth_write_hwaddr(dev, "eth", dev->index);
123 memset(dev->enetaddr, 0, ARP_HLEN);
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[ARP_HLEN];
139 eth_env_get_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, ARP_HLEN)) {
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, ARP_HLEN);
153 } else if (is_valid_ethaddr(dev->enetaddr)) {
154 eth_env_set_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 = env_get("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();
295 * mcast_addr: multicast ipaddr from which multicast Mac is made
296 * join: 1=join, 0=leave.
298 int eth_mcast_join(struct in_addr mcast_ip, int join)
300 u8 mcast_mac[ARP_HLEN];
301 if (!eth_current || !eth_current->mcast)
303 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
304 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
305 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
309 return eth_current->mcast(eth_current, mcast_mac, join);
314 struct eth_device *old_current;
317 puts("No ethernet found.\n");
321 old_current = eth_current;
323 debug("Trying %s\n", eth_current->name);
325 if (eth_current->init(eth_current, gd->bd) >= 0) {
326 eth_current->state = ETH_STATE_ACTIVE;
333 } while (old_current != eth_current);
343 eth_current->halt(eth_current);
345 eth_current->state = ETH_STATE_PASSIVE;
348 int eth_is_active(struct eth_device *dev)
350 return dev && dev->state == ETH_STATE_ACTIVE;
353 int eth_send(void *packet, int length)
358 return eth_current->send(eth_current, packet, length);
366 return eth_current->recv(eth_current);
370 static void eth_save_packet(void *packet, int length)
375 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
378 if (PKTSIZE < length)
381 for (i = 0; i < length; i++)
382 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
384 eth_rcv_bufs[eth_rcv_last].length = length;
385 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
388 int eth_receive(void *packet, int length)
391 void *pp = push_packet;
394 if (eth_rcv_current == eth_rcv_last) {
395 push_packet = eth_save_packet;
399 if (eth_rcv_current == eth_rcv_last)
403 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
405 for (i = 0; i < length; i++)
406 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
408 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
411 #endif /* CONFIG_API */