Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 818f91e | 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 | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 52f2423 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 8 | #include <bootstage.h> |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 11 | #include <miiphy.h> |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 12 | #include <net.h> |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 13 | #include "eth_internal.h" |
| 14 | |
Simon Glass | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 15 | int eth_env_get_enetaddr_by_index(const char *base_name, int index, |
Simon Glass | 9987ecd | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 16 | uchar *enetaddr) |
| 17 | { |
| 18 | char enetvar[32]; |
| 19 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
Simon Glass | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 20 | return eth_env_get_enetaddr(enetvar, enetaddr); |
Simon Glass | 9987ecd | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Simon Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 23 | int eth_env_set_enetaddr_by_index(const char *base_name, int index, |
Simon Glass | 9987ecd | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 24 | uchar *enetaddr) |
| 25 | { |
| 26 | char enetvar[32]; |
| 27 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
Simon Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 28 | return eth_env_set_enetaddr(enetvar, enetaddr); |
Simon Glass | 9987ecd | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 31 | void eth_common_init(void) |
| 32 | { |
| 33 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); |
Simon Glass | 9f66492 | 2021-08-08 12:20:31 -0600 | [diff] [blame] | 34 | #if CONFIG_IS_ENABLED(ETH) |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 35 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
| 36 | miiphy_init(); |
| 37 | #endif |
Simon Glass | 9f66492 | 2021-08-08 12:20:31 -0600 | [diff] [blame] | 38 | #endif |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 39 | } |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 40 | |
| 41 | int eth_mac_skip(int index) |
| 42 | { |
| 43 | char enetvar[15]; |
| 44 | char *skip_state; |
| 45 | |
| 46 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 47 | skip_state = env_get(enetvar); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 48 | return skip_state != NULL; |
| 49 | } |
| 50 | |
| 51 | void eth_current_changed(void) |
| 52 | { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 53 | char *act = env_get("ethact"); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 54 | char *ethrotate; |
| 55 | |
| 56 | /* |
| 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'. |
| 60 | */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 61 | ethrotate = env_get("ethrotate"); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 62 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
| 63 | return; |
| 64 | |
| 65 | /* update current ethernet name */ |
| 66 | if (eth_get_dev()) { |
| 67 | if (act == NULL || strcmp(act, eth_get_name()) != 0) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 68 | env_set("ethact", eth_get_name()); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 69 | } |
| 70 | /* |
| 71 | * remove the variable completely if there is no active |
| 72 | * interface |
| 73 | */ |
| 74 | else if (act != NULL) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 75 | env_set("ethact", NULL); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void eth_try_another(int first_restart) |
| 79 | { |
| 80 | static void *first_failed; |
| 81 | char *ethrotate; |
| 82 | |
| 83 | /* |
| 84 | * Do not rotate between network interfaces when |
| 85 | * 'ethrotate' variable is set to 'no'. |
| 86 | */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 87 | ethrotate = env_get("ethrotate"); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 88 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
| 89 | return; |
| 90 | |
| 91 | if (!eth_get_dev()) |
| 92 | return; |
| 93 | |
| 94 | if (first_restart) |
| 95 | first_failed = eth_get_dev(); |
| 96 | |
| 97 | eth_set_current_to_next(); |
| 98 | |
| 99 | eth_current_changed(); |
| 100 | |
| 101 | if (first_failed == eth_get_dev()) |
| 102 | net_restart_wrap = 1; |
| 103 | } |
| 104 | |
| 105 | void eth_set_current(void) |
| 106 | { |
| 107 | static char *act; |
| 108 | static int env_changed_id; |
| 109 | int env_id; |
| 110 | |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 111 | env_id = env_get_id(); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 112 | if ((act == NULL) || (env_changed_id != env_id)) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 113 | act = env_get("ethact"); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 114 | env_changed_id = env_id; |
| 115 | } |
| 116 | |
| 117 | if (act == NULL) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 118 | char *ethprime = env_get("ethprime"); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 119 | void *dev = NULL; |
| 120 | |
| 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)); |
| 129 | } |
| 130 | |
| 131 | eth_current_changed(); |
| 132 | } |
| 133 | |
| 134 | const char *eth_get_name(void) |
| 135 | { |
| 136 | return eth_get_dev() ? eth_get_dev()->name : "unknown"; |
| 137 | } |