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