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