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