wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 2 | * (C) Copyright 2001-2015 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 4 | * Joe Hershberger, National Instruments |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 11 | #include <dm.h> |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 12 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 13 | #include <net.h> |
Marian Balakowicz | d9785c1 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 14 | #include <miiphy.h> |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 15 | #include <phy.h> |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 16 | #include <asm/errno.h> |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 18 | #include <dm/uclass-internal.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 19 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 22 | void eth_parse_enetaddr(const char *addr, uchar *enetaddr) |
| 23 | { |
| 24 | char *end; |
| 25 | int i; |
| 26 | |
| 27 | for (i = 0; i < 6; ++i) { |
| 28 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; |
| 29 | if (addr) |
| 30 | addr = (*end) ? end + 1 : end; |
| 31 | } |
| 32 | } |
| 33 | |
Josh Wu | 219cc94 | 2015-09-01 18:22:55 +0800 | [diff] [blame] | 34 | int eth_getenv_enetaddr(const char *name, uchar *enetaddr) |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 35 | { |
| 36 | eth_parse_enetaddr(getenv(name), enetaddr); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 37 | return is_valid_ethaddr(enetaddr); |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Josh Wu | 219cc94 | 2015-09-01 18:22:55 +0800 | [diff] [blame] | 40 | int eth_setenv_enetaddr(const char *name, const uchar *enetaddr) |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 41 | { |
| 42 | char buf[20]; |
| 43 | |
| 44 | sprintf(buf, "%pM", enetaddr); |
| 45 | |
| 46 | return setenv(name, buf); |
| 47 | } |
Mike Frysinger | 86848a7 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 48 | |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 49 | int eth_getenv_enetaddr_by_index(const char *base_name, int index, |
| 50 | uchar *enetaddr) |
Mike Frysinger | 86848a7 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 51 | { |
| 52 | char enetvar[32]; |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 53 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
Mike Frysinger | 86848a7 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 54 | return eth_getenv_enetaddr(enetvar, enetaddr); |
| 55 | } |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 56 | |
Joe Hershberger | 154177e | 2012-07-10 16:23:22 -0500 | [diff] [blame] | 57 | static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, |
Rob Herring | c88ef3c | 2012-04-14 18:06:49 +0000 | [diff] [blame] | 58 | uchar *enetaddr) |
| 59 | { |
| 60 | char enetvar[32]; |
| 61 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
| 62 | return eth_setenv_enetaddr(enetvar, enetaddr); |
| 63 | } |
| 64 | |
Ben Warren | ecee932 | 2010-04-26 11:11:46 -0700 | [diff] [blame] | 65 | static int eth_mac_skip(int index) |
| 66 | { |
| 67 | char enetvar[15]; |
| 68 | char *skip_state; |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 69 | |
Ben Warren | ecee932 | 2010-04-26 11:11:46 -0700 | [diff] [blame] | 70 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 71 | skip_state = getenv(enetvar); |
| 72 | return skip_state != NULL; |
Ben Warren | ecee932 | 2010-04-26 11:11:46 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 75 | static void eth_current_changed(void); |
| 76 | |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 77 | /* |
| 78 | * CPU and board-specific Ethernet initializations. Aliased function |
| 79 | * signals caller to move on |
| 80 | */ |
| 81 | static int __def_eth_init(bd_t *bis) |
| 82 | { |
| 83 | return -1; |
| 84 | } |
| 85 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 86 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 87 | |
| 88 | static void eth_common_init(void) |
| 89 | { |
| 90 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); |
| 91 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
| 92 | miiphy_init(); |
| 93 | #endif |
| 94 | |
| 95 | #ifdef CONFIG_PHYLIB |
| 96 | phy_init(); |
| 97 | #endif |
| 98 | |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 99 | /* |
| 100 | * If board-specific initialization exists, call it. |
| 101 | * If not, call a CPU-specific one |
| 102 | */ |
| 103 | if (board_eth_init != __def_eth_init) { |
| 104 | if (board_eth_init(gd->bd) < 0) |
| 105 | printf("Board Net Initialization Failed\n"); |
| 106 | } else if (cpu_eth_init != __def_eth_init) { |
| 107 | if (cpu_eth_init(gd->bd) < 0) |
| 108 | printf("CPU Net Initialization Failed\n"); |
| 109 | } else { |
Bin Meng | d8f79af | 2015-08-27 22:25:52 -0700 | [diff] [blame] | 110 | #ifndef CONFIG_DM_ETH |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 111 | printf("Net Initialization Skipped\n"); |
Bin Meng | d8f79af | 2015-08-27 22:25:52 -0700 | [diff] [blame] | 112 | #endif |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 116 | #ifdef CONFIG_DM_ETH |
| 117 | /** |
| 118 | * struct eth_device_priv - private structure for each Ethernet device |
| 119 | * |
| 120 | * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t) |
| 121 | */ |
| 122 | struct eth_device_priv { |
| 123 | enum eth_state_t state; |
| 124 | }; |
| 125 | |
| 126 | /** |
| 127 | * struct eth_uclass_priv - The structure attached to the uclass itself |
| 128 | * |
| 129 | * @current: The Ethernet device that the network functions are using |
| 130 | */ |
| 131 | struct eth_uclass_priv { |
| 132 | struct udevice *current; |
| 133 | }; |
| 134 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 135 | /* eth_errno - This stores the most recent failure code from DM functions */ |
| 136 | static int eth_errno; |
| 137 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 138 | static struct eth_uclass_priv *eth_get_uclass_priv(void) |
| 139 | { |
| 140 | struct uclass *uc; |
| 141 | |
| 142 | uclass_get(UCLASS_ETH, &uc); |
| 143 | assert(uc); |
| 144 | return uc->priv; |
| 145 | } |
| 146 | |
| 147 | static void eth_set_current_to_next(void) |
| 148 | { |
| 149 | struct eth_uclass_priv *uc_priv; |
| 150 | |
| 151 | uc_priv = eth_get_uclass_priv(); |
| 152 | if (uc_priv->current) |
| 153 | uclass_next_device(&uc_priv->current); |
| 154 | if (!uc_priv->current) |
| 155 | uclass_first_device(UCLASS_ETH, &uc_priv->current); |
| 156 | } |
| 157 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 158 | /* |
| 159 | * Typically this will simply return the active device. |
| 160 | * In the case where the most recent active device was unset, this will attempt |
| 161 | * to return the first device. If that device doesn't exist or fails to probe, |
| 162 | * this function will return NULL. |
| 163 | */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 164 | struct udevice *eth_get_dev(void) |
| 165 | { |
| 166 | struct eth_uclass_priv *uc_priv; |
| 167 | |
| 168 | uc_priv = eth_get_uclass_priv(); |
| 169 | if (!uc_priv->current) |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 170 | eth_errno = uclass_first_device(UCLASS_ETH, |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 171 | &uc_priv->current); |
| 172 | return uc_priv->current; |
| 173 | } |
| 174 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 175 | /* |
| 176 | * Typically this will just store a device pointer. |
| 177 | * In case it was not probed, we will attempt to do so. |
| 178 | * dev may be NULL to unset the active device. |
| 179 | */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 180 | static void eth_set_dev(struct udevice *dev) |
| 181 | { |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 182 | if (dev && !device_active(dev)) |
| 183 | eth_errno = device_probe(dev); |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 184 | eth_get_uclass_priv()->current = dev; |
| 185 | } |
| 186 | |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 187 | /* |
| 188 | * Find the udevice that either has the name passed in as devname or has an |
| 189 | * alias named devname. |
| 190 | */ |
| 191 | struct udevice *eth_get_dev_by_name(const char *devname) |
| 192 | { |
| 193 | int seq = -1; |
| 194 | char *endp = NULL; |
| 195 | const char *startp = NULL; |
| 196 | struct udevice *it; |
| 197 | struct uclass *uc; |
Bin Meng | e408c42 | 2015-08-27 22:25:54 -0700 | [diff] [blame] | 198 | int len = strlen("eth"); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 199 | |
| 200 | /* Must be longer than 3 to be an alias */ |
Bin Meng | e408c42 | 2015-08-27 22:25:54 -0700 | [diff] [blame] | 201 | if (!strncmp(devname, "eth", len) && strlen(devname) > len) { |
| 202 | startp = devname + len; |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 203 | seq = simple_strtoul(startp, &endp, 10); |
| 204 | } |
| 205 | |
| 206 | uclass_get(UCLASS_ETH, &uc); |
| 207 | uclass_foreach_dev(it, uc) { |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 208 | /* |
| 209 | * We need the seq to be valid, so try to probe it. |
| 210 | * If the probe fails, the seq will not match since it will be |
| 211 | * -1 instead of what we are looking for. |
| 212 | * We don't care about errors from probe here. Either they won't |
| 213 | * match an alias or it will match a literal name and we'll pick |
| 214 | * up the error when we try to probe again in eth_set_dev(). |
| 215 | */ |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 216 | device_probe(it); |
| 217 | /* |
| 218 | * Check for the name or the sequence number to match |
| 219 | */ |
| 220 | if (strcmp(it->name, devname) == 0 || |
| 221 | (endp > startp && it->seq == seq)) |
| 222 | return it; |
| 223 | } |
| 224 | |
| 225 | return NULL; |
| 226 | } |
| 227 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 228 | unsigned char *eth_get_ethaddr(void) |
| 229 | { |
| 230 | struct eth_pdata *pdata; |
| 231 | |
| 232 | if (eth_get_dev()) { |
| 233 | pdata = eth_get_dev()->platdata; |
| 234 | return pdata->enetaddr; |
| 235 | } |
| 236 | |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | /* Set active state without calling start on the driver */ |
| 241 | int eth_init_state_only(void) |
| 242 | { |
| 243 | struct udevice *current; |
| 244 | struct eth_device_priv *priv; |
| 245 | |
| 246 | current = eth_get_dev(); |
| 247 | if (!current || !device_active(current)) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | priv = current->uclass_priv; |
| 251 | priv->state = ETH_STATE_ACTIVE; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* Set passive state without calling stop on the driver */ |
| 257 | void eth_halt_state_only(void) |
| 258 | { |
| 259 | struct udevice *current; |
| 260 | struct eth_device_priv *priv; |
| 261 | |
| 262 | current = eth_get_dev(); |
| 263 | if (!current || !device_active(current)) |
| 264 | return; |
| 265 | |
| 266 | priv = current->uclass_priv; |
| 267 | priv->state = ETH_STATE_PASSIVE; |
| 268 | } |
| 269 | |
| 270 | int eth_get_dev_index(void) |
| 271 | { |
| 272 | if (eth_get_dev()) |
| 273 | return eth_get_dev()->seq; |
| 274 | return -1; |
| 275 | } |
| 276 | |
Joe Hershberger | f566c99 | 2015-03-24 02:41:49 -0500 | [diff] [blame] | 277 | static int eth_write_hwaddr(struct udevice *dev) |
| 278 | { |
| 279 | struct eth_pdata *pdata = dev->platdata; |
| 280 | int ret = 0; |
| 281 | |
| 282 | if (!dev || !device_active(dev)) |
| 283 | return -EINVAL; |
| 284 | |
| 285 | /* seq is valid since the device is active */ |
| 286 | if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) { |
| 287 | if (!is_valid_ethaddr(pdata->enetaddr)) { |
| 288 | printf("\nError: %s address %pM illegal value\n", |
| 289 | dev->name, pdata->enetaddr); |
| 290 | return -EINVAL; |
| 291 | } |
| 292 | |
Simon Glass | b86f795 | 2015-07-06 16:47:55 -0600 | [diff] [blame] | 293 | /* |
| 294 | * Drivers are allowed to decide not to implement this at |
| 295 | * run-time. E.g. Some devices may use it and some may not. |
| 296 | */ |
Joe Hershberger | f566c99 | 2015-03-24 02:41:49 -0500 | [diff] [blame] | 297 | ret = eth_get_ops(dev)->write_hwaddr(dev); |
Simon Glass | b86f795 | 2015-07-06 16:47:55 -0600 | [diff] [blame] | 298 | if (ret == -ENOSYS) |
| 299 | ret = 0; |
Joe Hershberger | f566c99 | 2015-03-24 02:41:49 -0500 | [diff] [blame] | 300 | if (ret) |
| 301 | printf("\nWarning: %s failed to set MAC address\n", |
| 302 | dev->name); |
| 303 | } |
| 304 | |
| 305 | return ret; |
| 306 | } |
| 307 | |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 308 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
| 309 | int flags) |
| 310 | { |
| 311 | int index; |
| 312 | int retval; |
| 313 | struct udevice *dev; |
| 314 | |
| 315 | /* look for an index after "eth" */ |
| 316 | index = simple_strtoul(name + 3, NULL, 10); |
| 317 | |
| 318 | retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev); |
| 319 | if (!retval) { |
| 320 | struct eth_pdata *pdata = dev->platdata; |
| 321 | switch (op) { |
| 322 | case env_op_create: |
| 323 | case env_op_overwrite: |
| 324 | eth_parse_enetaddr(value, pdata->enetaddr); |
| 325 | break; |
| 326 | case env_op_delete: |
| 327 | memset(pdata->enetaddr, 0, 6); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); |
| 334 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 335 | int eth_init(void) |
| 336 | { |
| 337 | struct udevice *current; |
| 338 | struct udevice *old_current; |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 339 | int ret = -ENODEV; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 340 | |
| 341 | current = eth_get_dev(); |
| 342 | if (!current) { |
| 343 | printf("No ethernet found.\n"); |
| 344 | return -ENODEV; |
| 345 | } |
| 346 | |
| 347 | old_current = current; |
| 348 | do { |
| 349 | debug("Trying %s\n", current->name); |
| 350 | |
| 351 | if (device_active(current)) { |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 352 | ret = eth_get_ops(current)->start(current); |
| 353 | if (ret >= 0) { |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 354 | struct eth_device_priv *priv = |
| 355 | current->uclass_priv; |
| 356 | |
| 357 | priv->state = ETH_STATE_ACTIVE; |
| 358 | return 0; |
| 359 | } |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 360 | } else { |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 361 | ret = eth_errno; |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 362 | } |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 363 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 364 | debug("FAIL\n"); |
| 365 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 366 | /* |
| 367 | * If ethrotate is enabled, this will change "current", |
| 368 | * otherwise we will drop out of this while loop immediately |
| 369 | */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 370 | eth_try_another(0); |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 371 | /* This will ensure the new "current" attempted to probe */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 372 | current = eth_get_dev(); |
| 373 | } while (old_current != current); |
| 374 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 375 | return ret; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void eth_halt(void) |
| 379 | { |
| 380 | struct udevice *current; |
| 381 | struct eth_device_priv *priv; |
| 382 | |
| 383 | current = eth_get_dev(); |
| 384 | if (!current || !device_active(current)) |
| 385 | return; |
| 386 | |
| 387 | eth_get_ops(current)->stop(current); |
| 388 | priv = current->uclass_priv; |
| 389 | priv->state = ETH_STATE_PASSIVE; |
| 390 | } |
| 391 | |
Bernhard Nortmann | eaa8a19 | 2015-09-14 15:29:43 +0200 | [diff] [blame] | 392 | int eth_is_active(struct udevice *dev) |
| 393 | { |
| 394 | struct eth_device_priv *priv; |
| 395 | |
| 396 | if (!dev || !device_active(dev)) |
| 397 | return 0; |
| 398 | |
| 399 | priv = dev_get_uclass_priv(dev); |
| 400 | return priv->state == ETH_STATE_ACTIVE; |
| 401 | } |
| 402 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 403 | int eth_send(void *packet, int length) |
| 404 | { |
| 405 | struct udevice *current; |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 406 | int ret; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 407 | |
| 408 | current = eth_get_dev(); |
| 409 | if (!current) |
| 410 | return -ENODEV; |
| 411 | |
| 412 | if (!device_active(current)) |
| 413 | return -EINVAL; |
| 414 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 415 | ret = eth_get_ops(current)->send(current, packet, length); |
| 416 | if (ret < 0) { |
| 417 | /* We cannot completely return the error at present */ |
| 418 | debug("%s: send() returned error %d\n", __func__, ret); |
| 419 | } |
| 420 | return ret; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | int eth_rx(void) |
| 424 | { |
| 425 | struct udevice *current; |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 426 | uchar *packet; |
Simon Glass | a1ca92e | 2015-07-06 16:47:49 -0600 | [diff] [blame] | 427 | int flags; |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 428 | int ret; |
| 429 | int i; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 430 | |
| 431 | current = eth_get_dev(); |
| 432 | if (!current) |
| 433 | return -ENODEV; |
| 434 | |
| 435 | if (!device_active(current)) |
| 436 | return -EINVAL; |
| 437 | |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 438 | /* Process up to 32 packets at one time */ |
Simon Glass | a1ca92e | 2015-07-06 16:47:49 -0600 | [diff] [blame] | 439 | flags = ETH_RECV_CHECK_DEVICE; |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 440 | for (i = 0; i < 32; i++) { |
Simon Glass | a1ca92e | 2015-07-06 16:47:49 -0600 | [diff] [blame] | 441 | ret = eth_get_ops(current)->recv(current, flags, &packet); |
| 442 | flags = 0; |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 443 | if (ret > 0) |
| 444 | net_process_received_packet(packet, ret); |
Joe Hershberger | 63c9729 | 2015-04-03 20:09:46 -0500 | [diff] [blame] | 445 | if (ret >= 0 && eth_get_ops(current)->free_pkt) |
| 446 | eth_get_ops(current)->free_pkt(current, packet, ret); |
| 447 | if (ret <= 0) |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 448 | break; |
| 449 | } |
| 450 | if (ret == -EAGAIN) |
| 451 | ret = 0; |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 452 | if (ret < 0) { |
| 453 | /* We cannot completely return the error at present */ |
| 454 | debug("%s: recv() returned error %d\n", __func__, ret); |
| 455 | } |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 456 | return ret; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 459 | int eth_initialize(void) |
| 460 | { |
| 461 | int num_devices = 0; |
| 462 | struct udevice *dev; |
| 463 | |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 464 | eth_common_init(); |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 465 | |
| 466 | /* |
| 467 | * Devices need to write the hwaddr even if not started so that Linux |
| 468 | * will have access to the hwaddr that u-boot stored for the device. |
| 469 | * This is accomplished by attempting to probe each device and calling |
| 470 | * their write_hwaddr() operation. |
| 471 | */ |
| 472 | uclass_first_device(UCLASS_ETH, &dev); |
| 473 | if (!dev) { |
| 474 | printf("No ethernet found.\n"); |
| 475 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
| 476 | } else { |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 477 | char *ethprime = getenv("ethprime"); |
| 478 | struct udevice *prime_dev = NULL; |
| 479 | |
| 480 | if (ethprime) |
| 481 | prime_dev = eth_get_dev_by_name(ethprime); |
| 482 | if (prime_dev) { |
| 483 | eth_set_dev(prime_dev); |
| 484 | eth_current_changed(); |
| 485 | } else { |
| 486 | eth_set_dev(NULL); |
| 487 | } |
| 488 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 489 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
| 490 | do { |
| 491 | if (num_devices) |
| 492 | printf(", "); |
| 493 | |
| 494 | printf("eth%d: %s", dev->seq, dev->name); |
| 495 | |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 496 | if (ethprime && dev == prime_dev) |
| 497 | printf(" [PRIME]"); |
| 498 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 499 | eth_write_hwaddr(dev); |
| 500 | |
| 501 | uclass_next_device(&dev); |
| 502 | num_devices++; |
| 503 | } while (dev); |
| 504 | |
| 505 | putc('\n'); |
| 506 | } |
| 507 | |
| 508 | return num_devices; |
| 509 | } |
| 510 | |
| 511 | static int eth_post_bind(struct udevice *dev) |
| 512 | { |
| 513 | if (strchr(dev->name, ' ')) { |
| 514 | printf("\nError: eth device name \"%s\" has a space!\n", |
| 515 | dev->name); |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int eth_pre_unbind(struct udevice *dev) |
| 523 | { |
| 524 | /* Don't hang onto a pointer that is going away */ |
| 525 | if (dev == eth_get_uclass_priv()->current) |
| 526 | eth_set_dev(NULL); |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int eth_post_probe(struct udevice *dev) |
| 532 | { |
| 533 | struct eth_device_priv *priv = dev->uclass_priv; |
| 534 | struct eth_pdata *pdata = dev->platdata; |
| 535 | unsigned char env_enetaddr[6]; |
| 536 | |
| 537 | priv->state = ETH_STATE_INIT; |
| 538 | |
| 539 | /* Check if the device has a MAC address in ROM */ |
| 540 | if (eth_get_ops(dev)->read_rom_hwaddr) |
| 541 | eth_get_ops(dev)->read_rom_hwaddr(dev); |
| 542 | |
| 543 | eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 544 | if (!is_zero_ethaddr(env_enetaddr)) { |
| 545 | if (!is_zero_ethaddr(pdata->enetaddr) && |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 546 | memcmp(pdata->enetaddr, env_enetaddr, 6)) { |
| 547 | printf("\nWarning: %s MAC addresses don't match:\n", |
| 548 | dev->name); |
| 549 | printf("Address in SROM is %pM\n", |
| 550 | pdata->enetaddr); |
| 551 | printf("Address in environment is %pM\n", |
| 552 | env_enetaddr); |
| 553 | } |
| 554 | |
| 555 | /* Override the ROM MAC address */ |
| 556 | memcpy(pdata->enetaddr, env_enetaddr, 6); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 557 | } else if (is_valid_ethaddr(pdata->enetaddr)) { |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 558 | eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr); |
| 559 | printf("\nWarning: %s using MAC address from ROM\n", |
| 560 | dev->name); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 561 | } else if (is_zero_ethaddr(pdata->enetaddr)) { |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 562 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
| 563 | net_random_ethaddr(pdata->enetaddr); |
| 564 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", |
| 565 | dev->name, dev->seq, pdata->enetaddr); |
| 566 | #else |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 567 | printf("\nError: %s address not set.\n", |
| 568 | dev->name); |
| 569 | return -EINVAL; |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 570 | #endif |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int eth_pre_remove(struct udevice *dev) |
| 577 | { |
Bin Meng | a16edab | 2015-10-07 21:45:42 -0700 | [diff] [blame^] | 578 | struct eth_pdata *pdata = dev->platdata; |
| 579 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 580 | eth_get_ops(dev)->stop(dev); |
| 581 | |
Bin Meng | a16edab | 2015-10-07 21:45:42 -0700 | [diff] [blame^] | 582 | /* clear the MAC address */ |
| 583 | memset(pdata->enetaddr, 0, 6); |
| 584 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | UCLASS_DRIVER(eth) = { |
| 589 | .name = "eth", |
| 590 | .id = UCLASS_ETH, |
| 591 | .post_bind = eth_post_bind, |
| 592 | .pre_unbind = eth_pre_unbind, |
| 593 | .post_probe = eth_post_probe, |
| 594 | .pre_remove = eth_pre_remove, |
| 595 | .priv_auto_alloc_size = sizeof(struct eth_uclass_priv), |
| 596 | .per_device_auto_alloc_size = sizeof(struct eth_device_priv), |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 597 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 598 | }; |
Bernhard Nortmann | eaa8a19 | 2015-09-14 15:29:43 +0200 | [diff] [blame] | 599 | #endif /* #ifdef CONFIG_DM_ETH */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 600 | |
| 601 | #ifndef CONFIG_DM_ETH |
Ben Warren | dd35479 | 2008-06-23 22:57:27 -0700 | [diff] [blame] | 602 | |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 603 | #ifdef CONFIG_API |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 604 | static struct { |
| 605 | uchar data[PKTSIZE]; |
| 606 | int length; |
| 607 | } eth_rcv_bufs[PKTBUFSRX]; |
| 608 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 609 | static unsigned int eth_rcv_current, eth_rcv_last; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 610 | #endif |
| 611 | |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 612 | static struct eth_device *eth_devices; |
| 613 | struct eth_device *eth_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 614 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 615 | static void eth_set_current_to_next(void) |
| 616 | { |
| 617 | eth_current = eth_current->next; |
| 618 | } |
| 619 | |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 620 | static void eth_set_dev(struct eth_device *dev) |
| 621 | { |
| 622 | eth_current = dev; |
| 623 | } |
| 624 | |
Ben Warren | d7fb9bc | 2010-07-29 12:56:11 -0700 | [diff] [blame] | 625 | struct eth_device *eth_get_dev_by_name(const char *devname) |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 626 | { |
| 627 | struct eth_device *dev, *target_dev; |
| 628 | |
Helmut Raiger | 7e7f903 | 2011-08-22 00:17:17 +0000 | [diff] [blame] | 629 | BUG_ON(devname == NULL); |
| 630 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 631 | if (!eth_devices) |
| 632 | return NULL; |
| 633 | |
| 634 | dev = eth_devices; |
| 635 | target_dev = NULL; |
| 636 | do { |
| 637 | if (strcmp(devname, dev->name) == 0) { |
| 638 | target_dev = dev; |
| 639 | break; |
| 640 | } |
| 641 | dev = dev->next; |
| 642 | } while (dev != eth_devices); |
| 643 | |
| 644 | return target_dev; |
| 645 | } |
| 646 | |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 647 | struct eth_device *eth_get_dev_by_index(int index) |
| 648 | { |
| 649 | struct eth_device *dev, *target_dev; |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 650 | |
| 651 | if (!eth_devices) |
| 652 | return NULL; |
| 653 | |
| 654 | dev = eth_devices; |
| 655 | target_dev = NULL; |
| 656 | do { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 657 | if (dev->index == index) { |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 658 | target_dev = dev; |
| 659 | break; |
| 660 | } |
| 661 | dev = dev->next; |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 662 | } while (dev != eth_devices); |
| 663 | |
| 664 | return target_dev; |
| 665 | } |
| 666 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 667 | int eth_get_dev_index(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 668 | { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 669 | if (!eth_current) |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 670 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 671 | |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 672 | return eth_current->index; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 675 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
| 676 | int flags) |
| 677 | { |
| 678 | int index; |
| 679 | struct eth_device *dev; |
| 680 | |
| 681 | if (!eth_devices) |
| 682 | return 0; |
| 683 | |
| 684 | /* look for an index after "eth" */ |
| 685 | index = simple_strtoul(name + 3, NULL, 10); |
| 686 | |
| 687 | dev = eth_devices; |
| 688 | do { |
| 689 | if (dev->index == index) { |
| 690 | switch (op) { |
| 691 | case env_op_create: |
| 692 | case env_op_overwrite: |
| 693 | eth_parse_enetaddr(value, dev->enetaddr); |
| 694 | break; |
| 695 | case env_op_delete: |
| 696 | memset(dev->enetaddr, 0, 6); |
| 697 | } |
| 698 | } |
Gong Qianyu | 7aba0f2 | 2015-08-31 11:34:43 +0800 | [diff] [blame] | 699 | dev = dev->next; |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 700 | } while (dev != eth_devices); |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); |
| 705 | |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 706 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
| 707 | int eth_number) |
| 708 | { |
| 709 | unsigned char env_enetaddr[6]; |
| 710 | int ret = 0; |
| 711 | |
Eric Miao | 6937664 | 2012-01-18 22:56:33 +0000 | [diff] [blame] | 712 | eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 713 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 714 | if (!is_zero_ethaddr(env_enetaddr)) { |
| 715 | if (!is_zero_ethaddr(dev->enetaddr) && |
Joe Hershberger | 4c7c65a | 2015-03-22 17:09:01 -0500 | [diff] [blame] | 716 | memcmp(dev->enetaddr, env_enetaddr, 6)) { |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 717 | printf("\nWarning: %s MAC addresses don't match:\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 718 | dev->name); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 719 | printf("Address in SROM is %pM\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 720 | dev->enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 721 | printf("Address in environment is %pM\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 722 | env_enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | memcpy(dev->enetaddr, env_enetaddr, 6); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 726 | } else if (is_valid_ethaddr(dev->enetaddr)) { |
Rob Herring | c88ef3c | 2012-04-14 18:06:49 +0000 | [diff] [blame] | 727 | eth_setenv_enetaddr_by_index(base_name, eth_number, |
| 728 | dev->enetaddr); |
| 729 | printf("\nWarning: %s using MAC address from net device\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 730 | dev->name); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 731 | } else if (is_zero_ethaddr(dev->enetaddr)) { |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 732 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
| 733 | net_random_ethaddr(dev->enetaddr); |
| 734 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", |
| 735 | dev->name, eth_number, dev->enetaddr); |
| 736 | #else |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 737 | printf("\nError: %s address not set.\n", |
| 738 | dev->name); |
| 739 | return -EINVAL; |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 740 | #endif |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 743 | if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 744 | if (!is_valid_ethaddr(dev->enetaddr)) { |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 745 | printf("\nError: %s address %pM illegal value\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 746 | dev->name, dev->enetaddr); |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 747 | return -EINVAL; |
| 748 | } |
Benoît Thébaudeau | 460f949 | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 749 | |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 750 | ret = dev->write_hwaddr(dev); |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 751 | if (ret) |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 752 | printf("\nWarning: %s failed to set MAC address\n", |
| 753 | dev->name); |
Benoît Thébaudeau | 460f949 | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 754 | } |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 755 | |
| 756 | return ret; |
| 757 | } |
| 758 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 759 | int eth_register(struct eth_device *dev) |
| 760 | { |
| 761 | struct eth_device *d; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 762 | static int index; |
Michal Simek | 58c583b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 763 | |
Mike Frysinger | f6add13 | 2011-11-10 14:11:04 +0000 | [diff] [blame] | 764 | assert(strlen(dev->name) < sizeof(dev->name)); |
Michal Simek | 58c583b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 765 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 766 | if (!eth_devices) { |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 767 | eth_devices = dev; |
| 768 | eth_current = dev; |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 769 | eth_current_changed(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 770 | } else { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 771 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
Detlev Zundel | aba4b69 | 2010-03-31 17:56:08 +0200 | [diff] [blame] | 772 | ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 773 | d->next = dev; |
| 774 | } |
| 775 | |
| 776 | dev->state = ETH_STATE_INIT; |
| 777 | dev->next = eth_devices; |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 778 | dev->index = index++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 783 | int eth_unregister(struct eth_device *dev) |
| 784 | { |
| 785 | struct eth_device *cur; |
| 786 | |
| 787 | /* No device */ |
| 788 | if (!eth_devices) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 789 | return -ENODEV; |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 790 | |
| 791 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; |
| 792 | cur = cur->next) |
| 793 | ; |
| 794 | |
| 795 | /* Device not found */ |
| 796 | if (cur->next != dev) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 797 | return -ENODEV; |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 798 | |
| 799 | cur->next = dev->next; |
| 800 | |
| 801 | if (eth_devices == dev) |
| 802 | eth_devices = dev->next == eth_devices ? NULL : dev->next; |
| 803 | |
| 804 | if (eth_current == dev) { |
| 805 | eth_current = eth_devices; |
| 806 | eth_current_changed(); |
| 807 | } |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 812 | int eth_initialize(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 813 | { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 814 | int num_devices = 0; |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 815 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 816 | eth_devices = NULL; |
| 817 | eth_current = NULL; |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 818 | eth_common_init(); |
Marian Balakowicz | d9785c1 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 819 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 820 | if (!eth_devices) { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 821 | puts("No ethernet found.\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 822 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 823 | } else { |
| 824 | struct eth_device *dev = eth_devices; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 825 | char *ethprime = getenv("ethprime"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 826 | |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 827 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 828 | do { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 829 | if (dev->index) |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 830 | puts(", "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 831 | |
| 832 | printf("%s", dev->name); |
| 833 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 834 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 835 | eth_current = dev; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 836 | puts(" [PRIME]"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Mike Frysinger | 1384f3b | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 839 | if (strchr(dev->name, ' ')) |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 840 | puts("\nWarning: eth device name has a space!" |
| 841 | "\n"); |
Mike Frysinger | 1384f3b | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 842 | |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 843 | eth_write_hwaddr(dev, "eth", dev->index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 844 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 845 | dev = dev->next; |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 846 | num_devices++; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 847 | } while (dev != eth_devices); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 848 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 849 | eth_current_changed(); |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 850 | putc('\n'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 853 | return num_devices; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 854 | } |
| 855 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 856 | #ifdef CONFIG_MCAST_TFTP |
| 857 | /* Multicast. |
| 858 | * mcast_addr: multicast ipaddr from which multicast Mac is made |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 859 | * join: 1=join, 0=leave. |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 860 | */ |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 861 | int eth_mcast_join(struct in_addr mcast_ip, int join) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 862 | { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 863 | u8 mcast_mac[6]; |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 864 | if (!eth_current || !eth_current->mcast) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 865 | return -1; |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 866 | mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff; |
| 867 | mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff; |
| 868 | mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f; |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 869 | mcast_mac[2] = 0x5e; |
| 870 | mcast_mac[1] = 0x0; |
| 871 | mcast_mac[0] = 0x1; |
| 872 | return eth_current->mcast(eth_current, mcast_mac, join); |
| 873 | } |
| 874 | |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 875 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
| 876 | * and this is the ethernet-crc method needed for TSEC -- and perhaps |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 877 | * some other adapter -- hash tables |
| 878 | */ |
| 879 | #define CRCPOLY_LE 0xedb88320 |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 880 | u32 ether_crc(size_t len, unsigned char const *p) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 881 | { |
| 882 | int i; |
| 883 | u32 crc; |
| 884 | crc = ~0; |
| 885 | while (len--) { |
| 886 | crc ^= *p++; |
| 887 | for (i = 0; i < 8; i++) |
| 888 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); |
| 889 | } |
| 890 | /* an reverse the bits, cuz of way they arrive -- last-first */ |
| 891 | crc = (crc >> 16) | (crc << 16); |
| 892 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); |
| 893 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); |
| 894 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); |
| 895 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); |
| 896 | return crc; |
| 897 | } |
| 898 | |
| 899 | #endif |
| 900 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 901 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 902 | int eth_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 903 | { |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 904 | struct eth_device *old_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 905 | |
Stefan Roese | 6bc1138 | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 906 | if (!eth_current) { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 907 | puts("No ethernet found.\n"); |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 908 | return -ENODEV; |
Stefan Roese | 6bc1138 | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 909 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 910 | |
| 911 | old_current = eth_current; |
| 912 | do { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 913 | debug("Trying %s\n", eth_current->name); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 914 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 915 | if (eth_current->init(eth_current, gd->bd) >= 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 916 | eth_current->state = ETH_STATE_ACTIVE; |
| 917 | |
Upakul Barkakaty | 505be87 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 918 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 919 | } |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 920 | debug("FAIL\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 921 | |
| 922 | eth_try_another(0); |
| 923 | } while (old_current != eth_current); |
| 924 | |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 925 | return -ETIMEDOUT; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | void eth_halt(void) |
| 929 | { |
| 930 | if (!eth_current) |
| 931 | return; |
| 932 | |
| 933 | eth_current->halt(eth_current); |
| 934 | |
| 935 | eth_current->state = ETH_STATE_PASSIVE; |
| 936 | } |
| 937 | |
Bernhard Nortmann | eaa8a19 | 2015-09-14 15:29:43 +0200 | [diff] [blame] | 938 | int eth_is_active(struct eth_device *dev) |
| 939 | { |
| 940 | return dev && dev->state == ETH_STATE_ACTIVE; |
| 941 | } |
| 942 | |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 943 | int eth_send(void *packet, int length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 944 | { |
| 945 | if (!eth_current) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 946 | return -ENODEV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 947 | |
| 948 | return eth_current->send(eth_current, packet, length); |
| 949 | } |
| 950 | |
| 951 | int eth_rx(void) |
| 952 | { |
| 953 | if (!eth_current) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 954 | return -ENODEV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 955 | |
| 956 | return eth_current->recv(eth_current); |
| 957 | } |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 958 | #endif /* ifndef CONFIG_DM_ETH */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 959 | |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 960 | #ifdef CONFIG_API |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 961 | static void eth_save_packet(void *packet, int length) |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 962 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 963 | char *p = packet; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 964 | int i; |
| 965 | |
| 966 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) |
| 967 | return; |
| 968 | |
| 969 | if (PKTSIZE < length) |
| 970 | return; |
| 971 | |
| 972 | for (i = 0; i < length; i++) |
| 973 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; |
| 974 | |
| 975 | eth_rcv_bufs[eth_rcv_last].length = length; |
| 976 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; |
| 977 | } |
| 978 | |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 979 | int eth_receive(void *packet, int length) |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 980 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 981 | char *p = packet; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 982 | void *pp = push_packet; |
| 983 | int i; |
| 984 | |
| 985 | if (eth_rcv_current == eth_rcv_last) { |
| 986 | push_packet = eth_save_packet; |
| 987 | eth_rx(); |
| 988 | push_packet = pp; |
| 989 | |
| 990 | if (eth_rcv_current == eth_rcv_last) |
| 991 | return -1; |
| 992 | } |
| 993 | |
Michael Walle | 46c07bc | 2012-06-22 11:24:28 +0000 | [diff] [blame] | 994 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 995 | |
| 996 | for (i = 0; i < length; i++) |
| 997 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; |
| 998 | |
| 999 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; |
| 1000 | return length; |
| 1001 | } |
| 1002 | #endif /* CONFIG_API */ |
| 1003 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1004 | static void eth_current_changed(void) |
| 1005 | { |
| 1006 | char *act = getenv("ethact"); |
| 1007 | /* update current ethernet name */ |
| 1008 | if (eth_get_dev()) { |
| 1009 | if (act == NULL || strcmp(act, eth_get_name()) != 0) |
| 1010 | setenv("ethact", eth_get_name()); |
| 1011 | } |
| 1012 | /* |
| 1013 | * remove the variable completely if there is no active |
| 1014 | * interface |
| 1015 | */ |
| 1016 | else if (act != NULL) |
| 1017 | setenv("ethact", NULL); |
| 1018 | } |
| 1019 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1020 | void eth_try_another(int first_restart) |
| 1021 | { |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 1022 | static void *first_failed; |
Remy Bohmer | c650e1b | 2011-02-19 20:15:14 +0100 | [diff] [blame] | 1023 | char *ethrotate; |
Matthias Fuchs | e169257 | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 1024 | |
| 1025 | /* |
| 1026 | * Do not rotate between network interfaces when |
| 1027 | * 'ethrotate' variable is set to 'no'. |
| 1028 | */ |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 1029 | ethrotate = getenv("ethrotate"); |
| 1030 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
Matthias Fuchs | e169257 | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 1031 | return; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1032 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1033 | if (!eth_get_dev()) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1034 | return; |
| 1035 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 1036 | if (first_restart) |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1037 | first_failed = eth_get_dev(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1038 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1039 | eth_set_current_to_next(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1040 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 1041 | eth_current_changed(); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1042 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1043 | if (first_failed == eth_get_dev()) |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 1044 | net_restart_wrap = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1047 | void eth_set_current(void) |
| 1048 | { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 1049 | static char *act; |
| 1050 | static int env_changed_id; |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 1051 | int env_id; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1052 | |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 1053 | env_id = get_env_id(); |
| 1054 | if ((act == NULL) || (env_changed_id != env_id)) { |
| 1055 | act = getenv("ethact"); |
| 1056 | env_changed_id = env_id; |
| 1057 | } |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 1058 | |
| 1059 | if (act == NULL) { |
| 1060 | char *ethprime = getenv("ethprime"); |
| 1061 | void *dev = NULL; |
| 1062 | |
| 1063 | if (ethprime) |
| 1064 | dev = eth_get_dev_by_name(ethprime); |
| 1065 | if (dev) |
| 1066 | eth_set_dev(dev); |
| 1067 | else |
| 1068 | eth_set_dev(NULL); |
| 1069 | } else { |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 1070 | eth_set_dev(eth_get_dev_by_name(act)); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 1071 | } |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1072 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 1073 | eth_current_changed(); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1074 | } |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1075 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1076 | const char *eth_get_name(void) |
wdenk | 5bb226e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 1077 | { |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1078 | return eth_get_dev() ? eth_get_dev()->name : "unknown"; |
wdenk | 5bb226e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 1079 | } |