Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001-2015 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * Joe Hershberger, National Instruments |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Patrick Delaunay | a8f2ca2 | 2021-07-20 20:15:30 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_ETH |
| 9 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 11 | #include <bootdev.h> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 12 | #include <bootstage.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 16 | #include <net.h> |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 17 | #include <nvmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
| 20 | #include <dm/uclass-internal.h> |
Ramon Fried | ac598c1 | 2019-07-18 21:43:30 +0300 | [diff] [blame] | 21 | #include <net/pcap.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 22 | #include "eth_internal.h" |
Ye Li | cd5bb77 | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 23 | #include <eth_phy.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 24 | |
Simon Glass | 004b891 | 2016-01-30 15:45:14 -0700 | [diff] [blame] | 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 27 | /** |
| 28 | * struct eth_device_priv - private structure for each Ethernet device |
| 29 | * |
| 30 | * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t) |
| 31 | */ |
| 32 | struct eth_device_priv { |
| 33 | enum eth_state_t state; |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 34 | bool running; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * struct eth_uclass_priv - The structure attached to the uclass itself |
| 39 | * |
| 40 | * @current: The Ethernet device that the network functions are using |
| 41 | */ |
| 42 | struct eth_uclass_priv { |
| 43 | struct udevice *current; |
| 44 | }; |
| 45 | |
| 46 | /* eth_errno - This stores the most recent failure code from DM functions */ |
| 47 | static int eth_errno; |
| 48 | |
| 49 | static struct eth_uclass_priv *eth_get_uclass_priv(void) |
| 50 | { |
| 51 | struct uclass *uc; |
Peng Fan | 46ec104 | 2020-05-03 22:41:13 +0800 | [diff] [blame] | 52 | int ret; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 53 | |
Peng Fan | 46ec104 | 2020-05-03 22:41:13 +0800 | [diff] [blame] | 54 | ret = uclass_get(UCLASS_ETH, &uc); |
| 55 | if (ret) |
| 56 | return NULL; |
| 57 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 58 | assert(uc); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 59 | return uclass_get_priv(uc); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void eth_set_current_to_next(void) |
| 63 | { |
| 64 | struct eth_uclass_priv *uc_priv; |
| 65 | |
| 66 | uc_priv = eth_get_uclass_priv(); |
| 67 | if (uc_priv->current) |
| 68 | uclass_next_device(&uc_priv->current); |
| 69 | if (!uc_priv->current) |
| 70 | uclass_first_device(UCLASS_ETH, &uc_priv->current); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Typically this will simply return the active device. |
| 75 | * In the case where the most recent active device was unset, this will attempt |
Michael Walle | 384023e | 2021-02-24 17:30:44 +0100 | [diff] [blame] | 76 | * to return the device with sequence id 0 (which can be configured by the |
| 77 | * device tree). If this fails, fall back to just getting the first device. |
| 78 | * The latter is non-deterministic and depends on the order of the probing. |
| 79 | * If that device doesn't exist or fails to probe, this function will return |
| 80 | * NULL. |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 81 | */ |
| 82 | struct udevice *eth_get_dev(void) |
| 83 | { |
| 84 | struct eth_uclass_priv *uc_priv; |
| 85 | |
| 86 | uc_priv = eth_get_uclass_priv(); |
Sean Anderson | 35e8298 | 2020-09-12 17:45:43 -0400 | [diff] [blame] | 87 | if (!uc_priv) |
| 88 | return NULL; |
| 89 | |
Michael Walle | 384023e | 2021-02-24 17:30:44 +0100 | [diff] [blame] | 90 | if (!uc_priv->current) { |
| 91 | eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0, |
| 92 | &uc_priv->current); |
| 93 | if (eth_errno) |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 94 | eth_errno = uclass_first_device_err(UCLASS_ETH, |
| 95 | &uc_priv->current); |
Michal Suchanek | 050aa36 | 2022-10-12 21:58:02 +0200 | [diff] [blame] | 96 | if (eth_errno) |
| 97 | uc_priv->current = NULL; |
Michael Walle | 384023e | 2021-02-24 17:30:44 +0100 | [diff] [blame] | 98 | } |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 99 | return uc_priv->current; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Typically this will just store a device pointer. |
| 104 | * In case it was not probed, we will attempt to do so. |
| 105 | * dev may be NULL to unset the active device. |
| 106 | */ |
| 107 | void eth_set_dev(struct udevice *dev) |
| 108 | { |
| 109 | if (dev && !device_active(dev)) { |
| 110 | eth_errno = device_probe(dev); |
| 111 | if (eth_errno) |
| 112 | dev = NULL; |
| 113 | } |
| 114 | |
| 115 | eth_get_uclass_priv()->current = dev; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Find the udevice that either has the name passed in as devname or has an |
| 120 | * alias named devname. |
| 121 | */ |
| 122 | struct udevice *eth_get_dev_by_name(const char *devname) |
| 123 | { |
| 124 | int seq = -1; |
| 125 | char *endp = NULL; |
| 126 | const char *startp = NULL; |
| 127 | struct udevice *it; |
| 128 | struct uclass *uc; |
| 129 | int len = strlen("eth"); |
Peng Fan | 46ec104 | 2020-05-03 22:41:13 +0800 | [diff] [blame] | 130 | int ret; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 131 | |
| 132 | /* Must be longer than 3 to be an alias */ |
| 133 | if (!strncmp(devname, "eth", len) && strlen(devname) > len) { |
| 134 | startp = devname + len; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 135 | seq = dectoul(startp, &endp); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Peng Fan | 46ec104 | 2020-05-03 22:41:13 +0800 | [diff] [blame] | 138 | ret = uclass_get(UCLASS_ETH, &uc); |
| 139 | if (ret) |
| 140 | return NULL; |
| 141 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 142 | uclass_foreach_dev(it, uc) { |
| 143 | /* |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 144 | * We don't care about errors from probe here. Either they won't |
| 145 | * match an alias or it will match a literal name and we'll pick |
| 146 | * up the error when we try to probe again in eth_set_dev(). |
| 147 | */ |
| 148 | if (device_probe(it)) |
| 149 | continue; |
| 150 | /* Check for the name or the sequence number to match */ |
| 151 | if (strcmp(it->name, devname) == 0 || |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 152 | (endp > startp && dev_seq(it) == seq)) |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 153 | return it; |
| 154 | } |
| 155 | |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | unsigned char *eth_get_ethaddr(void) |
| 160 | { |
| 161 | struct eth_pdata *pdata; |
| 162 | |
| 163 | if (eth_get_dev()) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 164 | pdata = dev_get_plat(eth_get_dev()); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 165 | return pdata->enetaddr; |
| 166 | } |
| 167 | |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | /* Set active state without calling start on the driver */ |
| 172 | int eth_init_state_only(void) |
| 173 | { |
| 174 | struct udevice *current; |
| 175 | struct eth_device_priv *priv; |
| 176 | |
| 177 | current = eth_get_dev(); |
| 178 | if (!current || !device_active(current)) |
| 179 | return -EINVAL; |
| 180 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 181 | priv = dev_get_uclass_priv(current); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 182 | priv->state = ETH_STATE_ACTIVE; |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* Set passive state without calling stop on the driver */ |
| 188 | void eth_halt_state_only(void) |
| 189 | { |
| 190 | struct udevice *current; |
| 191 | struct eth_device_priv *priv; |
| 192 | |
| 193 | current = eth_get_dev(); |
| 194 | if (!current || !device_active(current)) |
| 195 | return; |
| 196 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 197 | priv = dev_get_uclass_priv(current); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 198 | priv->state = ETH_STATE_PASSIVE; |
| 199 | } |
| 200 | |
| 201 | int eth_get_dev_index(void) |
| 202 | { |
| 203 | if (eth_get_dev()) |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 204 | return dev_seq(eth_get_dev()); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | static int eth_write_hwaddr(struct udevice *dev) |
| 209 | { |
xypron.glpk@gmx.de | 0211e39 | 2017-05-16 05:07:01 +0200 | [diff] [blame] | 210 | struct eth_pdata *pdata; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 211 | int ret = 0; |
| 212 | |
| 213 | if (!dev || !device_active(dev)) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | /* seq is valid since the device is active */ |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 217 | if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 218 | pdata = dev_get_plat(dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 219 | if (!is_valid_ethaddr(pdata->enetaddr)) { |
| 220 | printf("\nError: %s address %pM illegal value\n", |
| 221 | dev->name, pdata->enetaddr); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Drivers are allowed to decide not to implement this at |
| 227 | * run-time. E.g. Some devices may use it and some may not. |
| 228 | */ |
| 229 | ret = eth_get_ops(dev)->write_hwaddr(dev); |
| 230 | if (ret == -ENOSYS) |
| 231 | ret = 0; |
| 232 | if (ret) |
| 233 | printf("\nWarning: %s failed to set MAC address\n", |
| 234 | dev->name); |
| 235 | } |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
| 241 | int flags) |
| 242 | { |
| 243 | int index; |
| 244 | int retval; |
| 245 | struct udevice *dev; |
| 246 | |
| 247 | /* look for an index after "eth" */ |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 248 | index = dectoul(name + 3, NULL); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 249 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 250 | retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 251 | if (!retval) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 252 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 253 | switch (op) { |
| 254 | case env_op_create: |
| 255 | case env_op_overwrite: |
Joe Hershberger | 8e7545e | 2019-09-13 19:21:16 -0500 | [diff] [blame] | 256 | string_to_enetaddr(value, pdata->enetaddr); |
Hannes Schmelzer | 3071f07 | 2016-09-02 14:48:17 +0200 | [diff] [blame] | 257 | eth_write_hwaddr(dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 258 | break; |
| 259 | case env_op_delete: |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 260 | memset(pdata->enetaddr, 0, ARP_HLEN); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); |
| 267 | |
| 268 | int eth_init(void) |
| 269 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 270 | char *ethact = env_get("ethact"); |
| 271 | char *ethrotate = env_get("ethrotate"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 272 | struct udevice *current = NULL; |
| 273 | struct udevice *old_current; |
| 274 | int ret = -ENODEV; |
| 275 | |
| 276 | /* |
| 277 | * When 'ethrotate' variable is set to 'no' and 'ethact' variable |
| 278 | * is already set to an ethernet device, we should stick to 'ethact'. |
| 279 | */ |
| 280 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) { |
| 281 | if (ethact) { |
| 282 | current = eth_get_dev_by_name(ethact); |
| 283 | if (!current) |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (!current) { |
| 289 | current = eth_get_dev(); |
| 290 | if (!current) { |
Heinrich Schuchardt | 653f3ee | 2020-09-14 11:00:18 +0200 | [diff] [blame] | 291 | log_err("No ethernet found.\n"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 292 | return -ENODEV; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | old_current = current; |
| 297 | do { |
| 298 | if (current) { |
| 299 | debug("Trying %s\n", current->name); |
| 300 | |
| 301 | if (device_active(current)) { |
| 302 | ret = eth_get_ops(current)->start(current); |
| 303 | if (ret >= 0) { |
| 304 | struct eth_device_priv *priv = |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 305 | dev_get_uclass_priv(current); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 306 | |
| 307 | priv->state = ETH_STATE_ACTIVE; |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 308 | priv->running = true; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | } else { |
| 312 | ret = eth_errno; |
| 313 | } |
| 314 | |
| 315 | debug("FAIL\n"); |
| 316 | } else { |
| 317 | debug("PROBE FAIL\n"); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * If ethrotate is enabled, this will change "current", |
| 322 | * otherwise we will drop out of this while loop immediately |
| 323 | */ |
| 324 | eth_try_another(0); |
| 325 | /* This will ensure the new "current" attempted to probe */ |
| 326 | current = eth_get_dev(); |
| 327 | } while (old_current != current); |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | void eth_halt(void) |
| 333 | { |
| 334 | struct udevice *current; |
| 335 | struct eth_device_priv *priv; |
| 336 | |
| 337 | current = eth_get_dev(); |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 338 | if (!current) |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 339 | return; |
| 340 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 341 | priv = dev_get_uclass_priv(current); |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 342 | if (!priv || !priv->running) |
| 343 | return; |
| 344 | |
| 345 | eth_get_ops(current)->stop(current); |
| 346 | priv->state = ETH_STATE_PASSIVE; |
| 347 | priv->running = false; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int eth_is_active(struct udevice *dev) |
| 351 | { |
| 352 | struct eth_device_priv *priv; |
| 353 | |
| 354 | if (!dev || !device_active(dev)) |
| 355 | return 0; |
| 356 | |
| 357 | priv = dev_get_uclass_priv(dev); |
| 358 | return priv->state == ETH_STATE_ACTIVE; |
| 359 | } |
| 360 | |
| 361 | int eth_send(void *packet, int length) |
| 362 | { |
| 363 | struct udevice *current; |
| 364 | int ret; |
| 365 | |
| 366 | current = eth_get_dev(); |
| 367 | if (!current) |
| 368 | return -ENODEV; |
| 369 | |
Alexander Graf | 7151149 | 2018-03-15 15:07:09 +0100 | [diff] [blame] | 370 | if (!eth_is_active(current)) |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 371 | return -EINVAL; |
| 372 | |
| 373 | ret = eth_get_ops(current)->send(current, packet, length); |
| 374 | if (ret < 0) { |
| 375 | /* We cannot completely return the error at present */ |
| 376 | debug("%s: send() returned error %d\n", __func__, ret); |
| 377 | } |
Ramon Fried | ac598c1 | 2019-07-18 21:43:30 +0300 | [diff] [blame] | 378 | #if defined(CONFIG_CMD_PCAP) |
| 379 | if (ret >= 0) |
| 380 | pcap_post(packet, length, true); |
| 381 | #endif |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | int eth_rx(void) |
| 386 | { |
| 387 | struct udevice *current; |
| 388 | uchar *packet; |
| 389 | int flags; |
| 390 | int ret; |
| 391 | int i; |
| 392 | |
| 393 | current = eth_get_dev(); |
| 394 | if (!current) |
| 395 | return -ENODEV; |
| 396 | |
Alexander Graf | 7151149 | 2018-03-15 15:07:09 +0100 | [diff] [blame] | 397 | if (!eth_is_active(current)) |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 398 | return -EINVAL; |
| 399 | |
| 400 | /* Process up to 32 packets at one time */ |
| 401 | flags = ETH_RECV_CHECK_DEVICE; |
Patrick Wildt | d47430a | 2020-10-07 11:03:30 +0200 | [diff] [blame] | 402 | for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) { |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 403 | ret = eth_get_ops(current)->recv(current, flags, &packet); |
| 404 | flags = 0; |
| 405 | if (ret > 0) |
| 406 | net_process_received_packet(packet, ret); |
| 407 | if (ret >= 0 && eth_get_ops(current)->free_pkt) |
| 408 | eth_get_ops(current)->free_pkt(current, packet, ret); |
| 409 | if (ret <= 0) |
| 410 | break; |
| 411 | } |
| 412 | if (ret == -EAGAIN) |
| 413 | ret = 0; |
| 414 | if (ret < 0) { |
| 415 | /* We cannot completely return the error at present */ |
| 416 | debug("%s: recv() returned error %d\n", __func__, ret); |
| 417 | } |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | int eth_initialize(void) |
| 422 | { |
| 423 | int num_devices = 0; |
| 424 | struct udevice *dev; |
| 425 | |
| 426 | eth_common_init(); |
| 427 | |
| 428 | /* |
| 429 | * Devices need to write the hwaddr even if not started so that Linux |
| 430 | * will have access to the hwaddr that u-boot stored for the device. |
| 431 | * This is accomplished by attempting to probe each device and calling |
| 432 | * their write_hwaddr() operation. |
| 433 | */ |
Mario Six | 865c387 | 2018-04-27 14:52:56 +0200 | [diff] [blame] | 434 | uclass_first_device_check(UCLASS_ETH, &dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 435 | if (!dev) { |
Heinrich Schuchardt | 653f3ee | 2020-09-14 11:00:18 +0200 | [diff] [blame] | 436 | log_err("No ethernet found.\n"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 437 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
| 438 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 439 | char *ethprime = env_get("ethprime"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 440 | struct udevice *prime_dev = NULL; |
| 441 | |
| 442 | if (ethprime) |
| 443 | prime_dev = eth_get_dev_by_name(ethprime); |
| 444 | if (prime_dev) { |
| 445 | eth_set_dev(prime_dev); |
| 446 | eth_current_changed(); |
| 447 | } else { |
| 448 | eth_set_dev(NULL); |
| 449 | } |
| 450 | |
| 451 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
| 452 | do { |
Simon Glass | 3e14a22 | 2020-12-16 21:20:16 -0700 | [diff] [blame] | 453 | if (device_active(dev)) { |
Michael Walle | 902c796 | 2019-10-22 01:03:10 +0200 | [diff] [blame] | 454 | if (num_devices) |
| 455 | printf(", "); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 456 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 457 | printf("eth%d: %s", dev_seq(dev), dev->name); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 458 | |
Michael Walle | 902c796 | 2019-10-22 01:03:10 +0200 | [diff] [blame] | 459 | if (ethprime && dev == prime_dev) |
| 460 | printf(" [PRIME]"); |
| 461 | } |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 462 | |
| 463 | eth_write_hwaddr(dev); |
| 464 | |
Simon Glass | 3e14a22 | 2020-12-16 21:20:16 -0700 | [diff] [blame] | 465 | if (device_active(dev)) |
Michael Walle | 902c796 | 2019-10-22 01:03:10 +0200 | [diff] [blame] | 466 | num_devices++; |
Mario Six | 865c387 | 2018-04-27 14:52:56 +0200 | [diff] [blame] | 467 | uclass_next_device_check(&dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 468 | } while (dev); |
| 469 | |
Michael Walle | 902c796 | 2019-10-22 01:03:10 +0200 | [diff] [blame] | 470 | if (!num_devices) |
Heinrich Schuchardt | 653f3ee | 2020-09-14 11:00:18 +0200 | [diff] [blame] | 471 | log_err("No ethernet found.\n"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 472 | putc('\n'); |
| 473 | } |
| 474 | |
| 475 | return num_devices; |
| 476 | } |
| 477 | |
| 478 | static int eth_post_bind(struct udevice *dev) |
| 479 | { |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 480 | int ret; |
| 481 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 482 | if (strchr(dev->name, ' ')) { |
| 483 | printf("\nError: eth device name \"%s\" has a space!\n", |
| 484 | dev->name); |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
Ye Li | cd5bb77 | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 488 | #ifdef CONFIG_DM_ETH_PHY |
| 489 | eth_phy_binds_nodes(dev); |
| 490 | #endif |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 491 | if (CONFIG_IS_ENABLED(BOOTDEV_ETH)) { |
| 492 | ret = bootdev_setup_for_dev(dev, "eth_bootdev"); |
| 493 | if (ret) |
| 494 | return log_msg_ret("bootdev", ret); |
| 495 | } |
Ye Li | cd5bb77 | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 496 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static int eth_pre_unbind(struct udevice *dev) |
| 501 | { |
| 502 | /* Don't hang onto a pointer that is going away */ |
| 503 | if (dev == eth_get_uclass_priv()->current) |
| 504 | eth_set_dev(NULL); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 509 | static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN]) |
| 510 | { |
Simon Glass | 1eb7294 | 2021-01-13 20:29:47 -0700 | [diff] [blame] | 511 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 512 | const uint8_t *p; |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 513 | struct nvmem_cell mac_cell; |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 514 | |
| 515 | p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN); |
| 516 | if (!p) |
| 517 | p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN); |
| 518 | |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 519 | if (p) { |
| 520 | memcpy(mac, p, ARP_HLEN); |
| 521 | return true; |
| 522 | } |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 523 | |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 524 | if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell)) |
| 525 | return false; |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 526 | |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 527 | return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN); |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 528 | #else |
| 529 | return false; |
| 530 | #endif |
| 531 | } |
| 532 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 533 | static int eth_post_probe(struct udevice *dev) |
| 534 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 535 | struct eth_device_priv *priv = dev_get_uclass_priv(dev); |
| 536 | struct eth_pdata *pdata = dev_get_plat(dev); |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 537 | unsigned char env_enetaddr[ARP_HLEN]; |
Michal Simek | 3141113 | 2020-03-16 11:36:17 +0100 | [diff] [blame] | 538 | char *source = "DT"; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 539 | |
| 540 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 541 | struct eth_ops *ops = eth_get_ops(dev); |
| 542 | static int reloc_done; |
| 543 | |
| 544 | if (!reloc_done) { |
| 545 | if (ops->start) |
| 546 | ops->start += gd->reloc_off; |
| 547 | if (ops->send) |
| 548 | ops->send += gd->reloc_off; |
| 549 | if (ops->recv) |
| 550 | ops->recv += gd->reloc_off; |
| 551 | if (ops->free_pkt) |
| 552 | ops->free_pkt += gd->reloc_off; |
| 553 | if (ops->stop) |
| 554 | ops->stop += gd->reloc_off; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 555 | if (ops->mcast) |
| 556 | ops->mcast += gd->reloc_off; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 557 | if (ops->write_hwaddr) |
| 558 | ops->write_hwaddr += gd->reloc_off; |
| 559 | if (ops->read_rom_hwaddr) |
| 560 | ops->read_rom_hwaddr += gd->reloc_off; |
| 561 | |
| 562 | reloc_done++; |
| 563 | } |
| 564 | #endif |
| 565 | |
| 566 | priv->state = ETH_STATE_INIT; |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 567 | priv->running = false; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 568 | |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 569 | /* Check if the device has a valid MAC address in device tree */ |
| 570 | if (!eth_dev_get_mac_address(dev, pdata->enetaddr) || |
| 571 | !is_valid_ethaddr(pdata->enetaddr)) { |
Michal Simek | 3141113 | 2020-03-16 11:36:17 +0100 | [diff] [blame] | 572 | source = "ROM"; |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 573 | /* Check if the device has a MAC address in ROM */ |
| 574 | if (eth_get_ops(dev)->read_rom_hwaddr) |
| 575 | eth_get_ops(dev)->read_rom_hwaddr(dev); |
| 576 | } |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 577 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 578 | eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 579 | if (!is_zero_ethaddr(env_enetaddr)) { |
| 580 | if (!is_zero_ethaddr(pdata->enetaddr) && |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 581 | memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) { |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 582 | printf("\nWarning: %s MAC addresses don't match:\n", |
| 583 | dev->name); |
Michal Simek | 3141113 | 2020-03-16 11:36:17 +0100 | [diff] [blame] | 584 | printf("Address in %s is\t\t%pM\n", |
| 585 | source, pdata->enetaddr); |
| 586 | printf("Address in environment is\t%pM\n", |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 587 | env_enetaddr); |
| 588 | } |
| 589 | |
| 590 | /* Override the ROM MAC address */ |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 591 | memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 592 | } else if (is_valid_ethaddr(pdata->enetaddr)) { |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 593 | eth_env_set_enetaddr_by_index("eth", dev_seq(dev), |
| 594 | pdata->enetaddr); |
Siva Durga Prasad Paladugu | e0b7bc7 | 2016-11-02 12:52:13 +0100 | [diff] [blame] | 595 | } else if (is_zero_ethaddr(pdata->enetaddr) || |
| 596 | !is_valid_ethaddr(pdata->enetaddr)) { |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 597 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
| 598 | net_random_ethaddr(pdata->enetaddr); |
| 599 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 600 | dev->name, dev_seq(dev), pdata->enetaddr); |
Michal Simek | 2b25402 | 2022-01-11 10:28:09 +0100 | [diff] [blame] | 601 | eth_env_set_enetaddr_by_index("eth", dev_seq(dev), |
| 602 | pdata->enetaddr); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 603 | #else |
| 604 | printf("\nError: %s address not set.\n", |
| 605 | dev->name); |
| 606 | return -EINVAL; |
| 607 | #endif |
| 608 | } |
| 609 | |
Thierry Reding | 1306487 | 2019-05-20 17:59:56 +0200 | [diff] [blame] | 610 | eth_write_hwaddr(dev); |
| 611 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static int eth_pre_remove(struct udevice *dev) |
| 616 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 617 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 618 | |
| 619 | eth_get_ops(dev)->stop(dev); |
| 620 | |
| 621 | /* clear the MAC address */ |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 622 | memset(pdata->enetaddr, 0, ARP_HLEN); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
Michael Walle | 7efcdfd | 2021-02-25 16:51:11 +0100 | [diff] [blame] | 627 | UCLASS_DRIVER(ethernet) = { |
| 628 | .name = "ethernet", |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 629 | .id = UCLASS_ETH, |
| 630 | .post_bind = eth_post_bind, |
| 631 | .pre_unbind = eth_pre_unbind, |
| 632 | .post_probe = eth_post_probe, |
| 633 | .pre_remove = eth_pre_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 634 | .priv_auto = sizeof(struct eth_uclass_priv), |
| 635 | .per_device_auto = sizeof(struct eth_device_priv), |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 636 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 637 | }; |