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