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