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