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