Merge https://source.denx.de/u-boot/custodians/u-boot-snapdragon
[u-boot.git] / net / eth_common.c
blob89b5bb371897ee77e3de7cb50043aba90159123a
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
6 */
8 #include <bootstage.h>
9 #include <dm.h>
10 #include <env.h>
11 #include <miiphy.h>
12 #include <net.h>
13 #include "eth_internal.h"
15 int eth_env_get_enetaddr_by_index(const char *base_name, int index,
16 uchar *enetaddr)
18 char enetvar[32];
19 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
20 return eth_env_get_enetaddr(enetvar, enetaddr);
23 int eth_env_set_enetaddr_by_index(const char *base_name, int index,
24 uchar *enetaddr)
26 char enetvar[32];
27 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
28 return eth_env_set_enetaddr(enetvar, enetaddr);
31 void eth_common_init(void)
33 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
34 #if CONFIG_IS_ENABLED(ETH)
35 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
36 miiphy_init();
37 #endif
38 #endif
41 int eth_mac_skip(int index)
43 char enetvar[15];
44 char *skip_state;
46 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
47 skip_state = env_get(enetvar);
48 return skip_state != NULL;
51 void eth_current_changed(void)
53 char *act = env_get("ethact");
54 char *ethrotate;
57 * The call to eth_get_dev() below has a side effect of rotating
58 * ethernet device if uc_priv->current == NULL. This is not what
59 * we want when 'ethrotate' variable is 'no'.
61 ethrotate = env_get("ethrotate");
62 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
63 return;
65 /* update current ethernet name */
66 if (eth_get_dev()) {
67 if (act == NULL || strcmp(act, eth_get_name()) != 0)
68 env_set("ethact", eth_get_name());
71 * remove the variable completely if there is no active
72 * interface
74 else if (act != NULL)
75 env_set("ethact", NULL);
78 void eth_try_another(int first_restart)
80 static void *first_failed;
81 char *ethrotate;
84 * Do not rotate between network interfaces when
85 * 'ethrotate' variable is set to 'no'.
87 ethrotate = env_get("ethrotate");
88 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
89 return;
91 if (!eth_get_dev())
92 return;
94 if (first_restart)
95 first_failed = eth_get_dev();
97 eth_set_current_to_next();
99 eth_current_changed();
101 if (first_failed == eth_get_dev())
102 net_restart_wrap = 1;
105 void eth_set_current(void)
107 static char *act;
108 static int env_changed_id;
109 int env_id;
111 env_id = env_get_id();
112 if ((act == NULL) || (env_changed_id != env_id)) {
113 act = env_get("ethact");
114 env_changed_id = env_id;
117 if (act == NULL) {
118 char *ethprime = env_get("ethprime");
119 void *dev = NULL;
121 if (ethprime)
122 dev = eth_get_dev_by_name(ethprime);
123 if (dev)
124 eth_set_dev(dev);
125 else
126 eth_set_dev(NULL);
127 } else {
128 eth_set_dev(eth_get_dev_by_name(act));
131 eth_current_changed();
134 const char *eth_get_name(void)
136 return eth_get_dev() ? eth_get_dev()->name : "unknown";