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