Simon Glass | 0a9bde1 | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001-2015 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * Joe Hershberger, National Instruments |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 10 | #include <dm.h> |
Alex Kiernan | 9c21549 | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 11 | #include <environment.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); |
| 35 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
| 36 | miiphy_init(); |
| 37 | #endif |
| 38 | |
| 39 | #ifdef CONFIG_PHYLIB |
| 40 | phy_init(); |
| 41 | #endif |
| 42 | } |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 43 | |
| 44 | int eth_mac_skip(int index) |
| 45 | { |
| 46 | char enetvar[15]; |
| 47 | char *skip_state; |
| 48 | |
| 49 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 50 | skip_state = env_get(enetvar); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 51 | return skip_state != NULL; |
| 52 | } |
| 53 | |
| 54 | void eth_current_changed(void) |
| 55 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 56 | char *act = env_get("ethact"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 57 | char *ethrotate; |
| 58 | |
| 59 | /* |
| 60 | * The call to eth_get_dev() below has a side effect of rotating |
| 61 | * ethernet device if uc_priv->current == NULL. This is not what |
| 62 | * we want when 'ethrotate' variable is 'no'. |
| 63 | */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 64 | ethrotate = env_get("ethrotate"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 65 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
| 66 | return; |
| 67 | |
| 68 | /* update current ethernet name */ |
| 69 | if (eth_get_dev()) { |
| 70 | if (act == NULL || strcmp(act, eth_get_name()) != 0) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 71 | env_set("ethact", eth_get_name()); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 72 | } |
| 73 | /* |
| 74 | * remove the variable completely if there is no active |
| 75 | * interface |
| 76 | */ |
| 77 | else if (act != NULL) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 78 | env_set("ethact", NULL); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void eth_try_another(int first_restart) |
| 82 | { |
| 83 | static void *first_failed; |
| 84 | char *ethrotate; |
| 85 | |
| 86 | /* |
| 87 | * Do not rotate between network interfaces when |
| 88 | * 'ethrotate' variable is set to 'no'. |
| 89 | */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 90 | ethrotate = env_get("ethrotate"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 91 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
| 92 | return; |
| 93 | |
| 94 | if (!eth_get_dev()) |
| 95 | return; |
| 96 | |
| 97 | if (first_restart) |
| 98 | first_failed = eth_get_dev(); |
| 99 | |
| 100 | eth_set_current_to_next(); |
| 101 | |
| 102 | eth_current_changed(); |
| 103 | |
| 104 | if (first_failed == eth_get_dev()) |
| 105 | net_restart_wrap = 1; |
| 106 | } |
| 107 | |
| 108 | void eth_set_current(void) |
| 109 | { |
| 110 | static char *act; |
| 111 | static int env_changed_id; |
| 112 | int env_id; |
| 113 | |
| 114 | env_id = get_env_id(); |
| 115 | if ((act == NULL) || (env_changed_id != env_id)) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 116 | act = env_get("ethact"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 117 | env_changed_id = env_id; |
| 118 | } |
| 119 | |
| 120 | if (act == NULL) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 121 | char *ethprime = env_get("ethprime"); |
Simon Glass | d9582c3 | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 122 | void *dev = NULL; |
| 123 | |
| 124 | if (ethprime) |
| 125 | dev = eth_get_dev_by_name(ethprime); |
| 126 | if (dev) |
| 127 | eth_set_dev(dev); |
| 128 | else |
| 129 | eth_set_dev(NULL); |
| 130 | } else { |
| 131 | eth_set_dev(eth_get_dev_by_name(act)); |
| 132 | } |
| 133 | |
| 134 | eth_current_changed(); |
| 135 | } |
| 136 | |
| 137 | const char *eth_get_name(void) |
| 138 | { |
| 139 | return eth_get_dev() ? eth_get_dev()->name : "unknown"; |
| 140 | } |