blob: c94a7ba6ae7ae2a1a277653d43030c18023d1bd7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass818f91e2016-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 Glass818f91e2016-01-17 14:51:57 -07006 */
7
8#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -06009#include <bootstage.h>
Simon Glass8607a6b2016-01-17 14:51:59 -070010#include <dm.h>
Simon Glassf1f0ae62019-08-01 09:46:41 -060011#include <env.h>
Simon Glass818f91e2016-01-17 14:51:57 -070012#include <miiphy.h>
Simon Glass8607a6b2016-01-17 14:51:59 -070013#include <net.h>
Simon Glass818f91e2016-01-17 14:51:57 -070014#include "eth_internal.h"
15
Simon Glass35affd72017-08-03 12:22:14 -060016int eth_env_get_enetaddr_by_index(const char *base_name, int index,
Simon Glass9987ecd2016-01-17 14:51:58 -070017 uchar *enetaddr)
18{
19 char enetvar[32];
20 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Simon Glass35affd72017-08-03 12:22:14 -060021 return eth_env_get_enetaddr(enetvar, enetaddr);
Simon Glass9987ecd2016-01-17 14:51:58 -070022}
23
Simon Glassfd1e9592017-08-03 12:22:11 -060024int eth_env_set_enetaddr_by_index(const char *base_name, int index,
Simon Glass9987ecd2016-01-17 14:51:58 -070025 uchar *enetaddr)
26{
27 char enetvar[32];
28 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Simon Glassfd1e9592017-08-03 12:22:11 -060029 return eth_env_set_enetaddr(enetvar, enetaddr);
Simon Glass9987ecd2016-01-17 14:51:58 -070030}
31
Simon Glass818f91e2016-01-17 14:51:57 -070032void eth_common_init(void)
33{
34 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
Simon Glass9f664922021-08-08 12:20:31 -060035#if CONFIG_IS_ENABLED(ETH)
Simon Glass818f91e2016-01-17 14:51:57 -070036#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
37 miiphy_init();
38#endif
39
Marek Vasut9461d732023-03-19 18:03:17 +010040#if defined(CONFIG_NEEDS_MANUAL_RELOC) && defined(CONFIG_PHYLIB)
Simon Glass818f91e2016-01-17 14:51:57 -070041 phy_init();
42#endif
Simon Glass9f664922021-08-08 12:20:31 -060043#endif
Simon Glass818f91e2016-01-17 14:51:57 -070044}
Simon Glass8607a6b2016-01-17 14:51:59 -070045
46int eth_mac_skip(int index)
47{
48 char enetvar[15];
49 char *skip_state;
50
51 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
Simon Glass00caae62017-08-03 12:22:12 -060052 skip_state = env_get(enetvar);
Simon Glass8607a6b2016-01-17 14:51:59 -070053 return skip_state != NULL;
54}
55
56void eth_current_changed(void)
57{
Simon Glass00caae62017-08-03 12:22:12 -060058 char *act = env_get("ethact");
Simon Glass8607a6b2016-01-17 14:51:59 -070059 char *ethrotate;
60
61 /*
62 * The call to eth_get_dev() below has a side effect of rotating
63 * ethernet device if uc_priv->current == NULL. This is not what
64 * we want when 'ethrotate' variable is 'no'.
65 */
Simon Glass00caae62017-08-03 12:22:12 -060066 ethrotate = env_get("ethrotate");
Simon Glass8607a6b2016-01-17 14:51:59 -070067 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
68 return;
69
70 /* update current ethernet name */
71 if (eth_get_dev()) {
72 if (act == NULL || strcmp(act, eth_get_name()) != 0)
Simon Glass382bee52017-08-03 12:22:09 -060073 env_set("ethact", eth_get_name());
Simon Glass8607a6b2016-01-17 14:51:59 -070074 }
75 /*
76 * remove the variable completely if there is no active
77 * interface
78 */
79 else if (act != NULL)
Simon Glass382bee52017-08-03 12:22:09 -060080 env_set("ethact", NULL);
Simon Glass8607a6b2016-01-17 14:51:59 -070081}
82
83void eth_try_another(int first_restart)
84{
85 static void *first_failed;
86 char *ethrotate;
87
88 /*
89 * Do not rotate between network interfaces when
90 * 'ethrotate' variable is set to 'no'.
91 */
Simon Glass00caae62017-08-03 12:22:12 -060092 ethrotate = env_get("ethrotate");
Simon Glass8607a6b2016-01-17 14:51:59 -070093 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
94 return;
95
96 if (!eth_get_dev())
97 return;
98
99 if (first_restart)
100 first_failed = eth_get_dev();
101
102 eth_set_current_to_next();
103
104 eth_current_changed();
105
106 if (first_failed == eth_get_dev())
107 net_restart_wrap = 1;
108}
109
110void eth_set_current(void)
111{
112 static char *act;
113 static int env_changed_id;
114 int env_id;
115
Simon Glassf1f0ae62019-08-01 09:46:41 -0600116 env_id = env_get_id();
Simon Glass8607a6b2016-01-17 14:51:59 -0700117 if ((act == NULL) || (env_changed_id != env_id)) {
Simon Glass00caae62017-08-03 12:22:12 -0600118 act = env_get("ethact");
Simon Glass8607a6b2016-01-17 14:51:59 -0700119 env_changed_id = env_id;
120 }
121
122 if (act == NULL) {
Simon Glass00caae62017-08-03 12:22:12 -0600123 char *ethprime = env_get("ethprime");
Simon Glass8607a6b2016-01-17 14:51:59 -0700124 void *dev = NULL;
125
126 if (ethprime)
127 dev = eth_get_dev_by_name(ethprime);
128 if (dev)
129 eth_set_dev(dev);
130 else
131 eth_set_dev(NULL);
132 } else {
133 eth_set_dev(eth_get_dev_by_name(act));
134 }
135
136 eth_current_changed();
137}
138
139const char *eth_get_name(void)
140{
141 return eth_get_dev() ? eth_get_dev()->name : "unknown";
142}