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 | |
| 8 | #include <common.h> |
Simon Glass | 52f2423 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 9 | #include <bootstage.h> |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 11 | #include <env.h> |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 12 | #include <miiphy.h> |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 13 | #include <net.h> |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 14 | #include "eth_internal.h" |
| 15 | |
Simon Glass | 35affd7 | 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 | 9987ecd | 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 | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 21 | return eth_env_get_enetaddr(enetvar, enetaddr); |
Simon Glass | 9987ecd | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Simon Glass | fd1e959 | 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 | 9987ecd | 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 | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 29 | return eth_env_set_enetaddr(enetvar, enetaddr); |
Simon Glass | 9987ecd | 2016-01-17 14:51:58 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Simon Glass | 818f91e | 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 | 9f66492 | 2021-08-08 12:20:31 -0600 | [diff] [blame] | 35 | #if CONFIG_IS_ENABLED(ETH) |
Simon Glass | 818f91e | 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 | |
Marek Vasut | 9461d73 | 2023-03-19 18:03:17 +0100 | [diff] [blame] | 40 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) && defined(CONFIG_PHYLIB) |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 41 | phy_init(); |
| 42 | #endif |
Simon Glass | 9f66492 | 2021-08-08 12:20:31 -0600 | [diff] [blame] | 43 | #endif |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 44 | } |
Simon Glass | 8607a6b | 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 | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 52 | skip_state = env_get(enetvar); |
Simon Glass | 8607a6b | 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 | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 58 | char *act = env_get("ethact"); |
Simon Glass | 8607a6b | 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 | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 66 | ethrotate = env_get("ethrotate"); |
Simon Glass | 8607a6b | 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 | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 73 | env_set("ethact", eth_get_name()); |
Simon Glass | 8607a6b | 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 | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 80 | env_set("ethact", NULL); |
Simon Glass | 8607a6b | 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 | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 92 | ethrotate = env_get("ethrotate"); |
Simon Glass | 8607a6b | 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 | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 116 | env_id = env_get_id(); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 117 | if ((act == NULL) || (env_changed_id != env_id)) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 118 | act = env_get("ethact"); |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 119 | env_changed_id = env_id; |
| 120 | } |
| 121 | |
| 122 | if (act == NULL) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 123 | char *ethprime = env_get("ethprime"); |
Simon Glass | 8607a6b | 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 | } |