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