Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001-2015 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * Joe Hershberger, National Instruments |
Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 9 | #include <bootstage.h> |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 2d85a75 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 11 | #include <env.h> |
Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 12 | #include <miiphy.h> |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 13 | #include <net.h> |
Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 14 | #include "eth_internal.h" |
| 15 | |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 16 | int eth_env_get_enetaddr_by_index(const char *base_name, int index, |
Simon Glass | ce63f89 | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 17 | uchar *enetaddr) |
| 18 | { |
| 19 | char enetvar[32]; |
| 20 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 21 | return eth_env_get_enetaddr(enetvar, enetaddr); |
Simon Glass | ce63f89 | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 24 | int eth_env_set_enetaddr_by_index(const char *base_name, int index, |
Simon Glass | ce63f89 | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 25 | uchar *enetaddr) |
| 26 | { |
| 27 | char enetvar[32]; |
| 28 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 29 | return eth_env_set_enetaddr(enetvar, enetaddr); |
Simon Glass | ce63f89 | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 32 | void eth_common_init(void) |
| 33 | { |
| 34 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); |
Simon Glass | 1d92624 | 2021-08-08 12:20:31 -0600 | [diff] [blame] | 35 | #if CONFIG_IS_ENABLED(ETH) |
Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 36 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
| 37 | miiphy_init(); |
| 38 | #endif |
Simon Glass | 1d92624 | 2021-08-08 12:20:31 -0600 | [diff] [blame] | 39 | #endif |
Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 40 | } |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 41 | |
| 42 | int eth_mac_skip(int index) |
| 43 | { |
| 44 | char enetvar[15]; |
| 45 | char *skip_state; |
| 46 | |
| 47 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 48 | skip_state = env_get(enetvar); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 49 | return skip_state != NULL; |
| 50 | } |
| 51 | |
| 52 | void eth_current_changed(void) |
| 53 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 54 | char *act = env_get("ethact"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 55 | 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 Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 62 | ethrotate = env_get("ethrotate"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 63 | 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 69 | env_set("ethact", eth_get_name()); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 70 | } |
| 71 | /* |
| 72 | * remove the variable completely if there is no active |
| 73 | * interface |
| 74 | */ |
| 75 | else if (act != NULL) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 76 | env_set("ethact", NULL); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void 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 Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 88 | ethrotate = env_get("ethrotate"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 89 | 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 | |
| 106 | void eth_set_current(void) |
| 107 | { |
| 108 | static char *act; |
| 109 | static int env_changed_id; |
| 110 | int env_id; |
| 111 | |
Simon Glass | 2d85a75 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 112 | env_id = env_get_id(); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 113 | if ((act == NULL) || (env_changed_id != env_id)) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 114 | act = env_get("ethact"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 115 | env_changed_id = env_id; |
| 116 | } |
| 117 | |
| 118 | if (act == NULL) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 119 | char *ethprime = env_get("ethprime"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 120 | 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 | |
| 135 | const char *eth_get_name(void) |
| 136 | { |
| 137 | return eth_get_dev() ? eth_get_dev()->name : "unknown"; |
| 138 | } |