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