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