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 |
| 39 | |
| 40 | #ifdef CONFIG_PHYLIB |
| 41 | phy_init(); |
| 42 | #endif |
Simon Glass | 1d92624 | 2021-08-08 12:20:31 -0600 | [diff] [blame] | 43 | #endif |
Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 44 | } |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 45 | |
| 46 | int eth_mac_skip(int index) |
| 47 | { |
| 48 | char enetvar[15]; |
| 49 | char *skip_state; |
| 50 | |
| 51 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 52 | skip_state = env_get(enetvar); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 53 | return skip_state != NULL; |
| 54 | } |
| 55 | |
| 56 | void eth_current_changed(void) |
| 57 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 58 | char *act = env_get("ethact"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 59 | 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 Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 66 | ethrotate = env_get("ethrotate"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 67 | 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 73 | env_set("ethact", eth_get_name()); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 74 | } |
| 75 | /* |
| 76 | * remove the variable completely if there is no active |
| 77 | * interface |
| 78 | */ |
| 79 | else if (act != NULL) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 80 | env_set("ethact", NULL); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void 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 Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 92 | ethrotate = env_get("ethrotate"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 93 | 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 | |
| 110 | void eth_set_current(void) |
| 111 | { |
| 112 | static char *act; |
| 113 | static int env_changed_id; |
| 114 | int env_id; |
| 115 | |
Simon Glass | 2d85a75 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 116 | env_id = env_get_id(); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 117 | if ((act == NULL) || (env_changed_id != env_id)) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 118 | act = env_get("ethact"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 119 | env_changed_id = env_id; |
| 120 | } |
| 121 | |
| 122 | if (act == NULL) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 123 | char *ethprime = env_get("ethprime"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 124 | 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 | |
| 139 | const char *eth_get_name(void) |
| 140 | { |
| 141 | return eth_get_dev() ? eth_get_dev()->name : "unknown"; |
| 142 | } |