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