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