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