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> 10 #include <dm.h> 11 #include <miiphy.h> 12 #include <net.h> 13 #include "eth_internal.h" 14 15 void eth_parse_enetaddr(const char *addr, uchar *enetaddr) 16 { 17 char *end; 18 int i; 19 20 for (i = 0; i < 6; ++i) { 21 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; 22 if (addr) 23 addr = (*end) ? end + 1 : end; 24 } 25 } 26 27 int eth_getenv_enetaddr(const char *name, uchar *enetaddr) 28 { 29 eth_parse_enetaddr(getenv(name), enetaddr); 30 return is_valid_ethaddr(enetaddr); 31 } 32 33 int eth_setenv_enetaddr(const char *name, const uchar *enetaddr) 34 { 35 char buf[ARP_HLEN_ASCII + 1]; 36 37 sprintf(buf, "%pM", enetaddr); 38 39 return setenv(name, buf); 40 } 41 42 int eth_getenv_enetaddr_by_index(const char *base_name, int index, 43 uchar *enetaddr) 44 { 45 char enetvar[32]; 46 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); 47 return eth_getenv_enetaddr(enetvar, enetaddr); 48 } 49 50 int eth_setenv_enetaddr_by_index(const char *base_name, int index, 51 uchar *enetaddr) 52 { 53 char enetvar[32]; 54 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); 55 return eth_setenv_enetaddr(enetvar, enetaddr); 56 } 57 58 void eth_common_init(void) 59 { 60 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); 61 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) 62 miiphy_init(); 63 #endif 64 65 #ifdef CONFIG_PHYLIB 66 phy_init(); 67 #endif 68 } 69 70 int eth_mac_skip(int index) 71 { 72 char enetvar[15]; 73 char *skip_state; 74 75 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); 76 skip_state = getenv(enetvar); 77 return skip_state != NULL; 78 } 79 80 void eth_current_changed(void) 81 { 82 char *act = getenv("ethact"); 83 char *ethrotate; 84 85 /* 86 * The call to eth_get_dev() below has a side effect of rotating 87 * ethernet device if uc_priv->current == NULL. This is not what 88 * we want when 'ethrotate' variable is 'no'. 89 */ 90 ethrotate = getenv("ethrotate"); 91 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) 92 return; 93 94 /* update current ethernet name */ 95 if (eth_get_dev()) { 96 if (act == NULL || strcmp(act, eth_get_name()) != 0) 97 setenv("ethact", eth_get_name()); 98 } 99 /* 100 * remove the variable completely if there is no active 101 * interface 102 */ 103 else if (act != NULL) 104 setenv("ethact", NULL); 105 } 106 107 void eth_try_another(int first_restart) 108 { 109 static void *first_failed; 110 char *ethrotate; 111 112 /* 113 * Do not rotate between network interfaces when 114 * 'ethrotate' variable is set to 'no'. 115 */ 116 ethrotate = getenv("ethrotate"); 117 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) 118 return; 119 120 if (!eth_get_dev()) 121 return; 122 123 if (first_restart) 124 first_failed = eth_get_dev(); 125 126 eth_set_current_to_next(); 127 128 eth_current_changed(); 129 130 if (first_failed == eth_get_dev()) 131 net_restart_wrap = 1; 132 } 133 134 void eth_set_current(void) 135 { 136 static char *act; 137 static int env_changed_id; 138 int env_id; 139 140 env_id = get_env_id(); 141 if ((act == NULL) || (env_changed_id != env_id)) { 142 act = getenv("ethact"); 143 env_changed_id = env_id; 144 } 145 146 if (act == NULL) { 147 char *ethprime = getenv("ethprime"); 148 void *dev = NULL; 149 150 if (ethprime) 151 dev = eth_get_dev_by_name(ethprime); 152 if (dev) 153 eth_set_dev(dev); 154 else 155 eth_set_dev(NULL); 156 } else { 157 eth_set_dev(eth_get_dev_by_name(act)); 158 } 159 160 eth_current_changed(); 161 } 162 163 const char *eth_get_name(void) 164 { 165 return eth_get_dev() ? eth_get_dev()->name : "unknown"; 166 } 167