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