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