blob: 14d4c07b695d5d68076eafc146b4b0a0fd710bf3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass0a9bde12016-01-17 14:51:57 -07002/*
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
Simon Glass0a9bde12016-01-17 14:51:57 -07006 */
7
8#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06009#include <bootstage.h>
Simon Glassd9582c32016-01-17 14:51:59 -070010#include <dm.h>
Simon Glass2d85a752019-08-01 09:46:41 -060011#include <env.h>
Simon Glass0a9bde12016-01-17 14:51:57 -070012#include <miiphy.h>
Simon Glassd9582c32016-01-17 14:51:59 -070013#include <net.h>
Simon Glass0a9bde12016-01-17 14:51:57 -070014#include "eth_internal.h"
15
Simon Glass399a9ce2017-08-03 12:22:14 -060016int eth_env_get_enetaddr_by_index(const char *base_name, int index,
Simon Glassce63f892016-01-17 14:51:58 -070017 uchar *enetaddr)
18{
19 char enetvar[32];
20 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Simon Glass399a9ce2017-08-03 12:22:14 -060021 return eth_env_get_enetaddr(enetvar, enetaddr);
Simon Glassce63f892016-01-17 14:51:58 -070022}
23
Simon Glass8551d552017-08-03 12:22:11 -060024int eth_env_set_enetaddr_by_index(const char *base_name, int index,
Simon Glassce63f892016-01-17 14:51:58 -070025 uchar *enetaddr)
26{
27 char enetvar[32];
28 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Simon Glass8551d552017-08-03 12:22:11 -060029 return eth_env_set_enetaddr(enetvar, enetaddr);
Simon Glassce63f892016-01-17 14:51:58 -070030}
31
Simon Glass0a9bde12016-01-17 14:51:57 -070032void eth_common_init(void)
33{
34 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
Simon Glass1d926242021-08-08 12:20:31 -060035#if CONFIG_IS_ENABLED(ETH)
Simon Glass0a9bde12016-01-17 14:51:57 -070036#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
37 miiphy_init();
38#endif
Simon Glass1d926242021-08-08 12:20:31 -060039#endif
Simon Glass0a9bde12016-01-17 14:51:57 -070040}
Simon Glassd9582c32016-01-17 14:51:59 -070041
42int eth_mac_skip(int index)
43{
44 char enetvar[15];
45 char *skip_state;
46
47 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
Simon Glass64b723f2017-08-03 12:22:12 -060048 skip_state = env_get(enetvar);
Simon Glassd9582c32016-01-17 14:51:59 -070049 return skip_state != NULL;
50}
51
52void eth_current_changed(void)
53{
Simon Glass64b723f2017-08-03 12:22:12 -060054 char *act = env_get("ethact");
Simon Glassd9582c32016-01-17 14:51:59 -070055 char *ethrotate;
56
57 /*
58 * The call to eth_get_dev() below has a side effect of rotating
59 * ethernet device if uc_priv->current == NULL. This is not what
60 * we want when 'ethrotate' variable is 'no'.
61 */
Simon Glass64b723f2017-08-03 12:22:12 -060062 ethrotate = env_get("ethrotate");
Simon Glassd9582c32016-01-17 14:51:59 -070063 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
64 return;
65
66 /* update current ethernet name */
67 if (eth_get_dev()) {
68 if (act == NULL || strcmp(act, eth_get_name()) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -060069 env_set("ethact", eth_get_name());
Simon Glassd9582c32016-01-17 14:51:59 -070070 }
71 /*
72 * remove the variable completely if there is no active
73 * interface
74 */
75 else if (act != NULL)
Simon Glass6a38e412017-08-03 12:22:09 -060076 env_set("ethact", NULL);
Simon Glassd9582c32016-01-17 14:51:59 -070077}
78
79void eth_try_another(int first_restart)
80{
81 static void *first_failed;
82 char *ethrotate;
83
84 /*
85 * Do not rotate between network interfaces when
86 * 'ethrotate' variable is set to 'no'.
87 */
Simon Glass64b723f2017-08-03 12:22:12 -060088 ethrotate = env_get("ethrotate");
Simon Glassd9582c32016-01-17 14:51:59 -070089 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
90 return;
91
92 if (!eth_get_dev())
93 return;
94
95 if (first_restart)
96 first_failed = eth_get_dev();
97
98 eth_set_current_to_next();
99
100 eth_current_changed();
101
102 if (first_failed == eth_get_dev())
103 net_restart_wrap = 1;
104}
105
106void eth_set_current(void)
107{
108 static char *act;
109 static int env_changed_id;
110 int env_id;
111
Simon Glass2d85a752019-08-01 09:46:41 -0600112 env_id = env_get_id();
Simon Glassd9582c32016-01-17 14:51:59 -0700113 if ((act == NULL) || (env_changed_id != env_id)) {
Simon Glass64b723f2017-08-03 12:22:12 -0600114 act = env_get("ethact");
Simon Glassd9582c32016-01-17 14:51:59 -0700115 env_changed_id = env_id;
116 }
117
118 if (act == NULL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600119 char *ethprime = env_get("ethprime");
Simon Glassd9582c32016-01-17 14:51:59 -0700120 void *dev = NULL;
121
122 if (ethprime)
123 dev = eth_get_dev_by_name(ethprime);
124 if (dev)
125 eth_set_dev(dev);
126 else
127 eth_set_dev(NULL);
128 } else {
129 eth_set_dev(eth_get_dev_by_name(act));
130 }
131
132 eth_current_changed();
133}
134
135const char *eth_get_name(void)
136{
137 return eth_get_dev() ? eth_get_dev()->name : "unknown";
138}