1 /* 2 * Device manager 3 * 4 * Copyright (c) 2013 Google, Inc 5 * 6 * (C) Copyright 2012 7 * Pavel Herrmann <morpheus.ibis@gmail.com> 8 * 9 * SPDX-License-Identifier: GPL-2.0+ 10 */ 11 12 #include <common.h> 13 #include <asm/io.h> 14 #include <clk.h> 15 #include <fdtdec.h> 16 #include <fdt_support.h> 17 #include <malloc.h> 18 #include <dm/device.h> 19 #include <dm/device-internal.h> 20 #include <dm/lists.h> 21 #include <dm/of_access.h> 22 #include <dm/pinctrl.h> 23 #include <dm/platdata.h> 24 #include <dm/read.h> 25 #include <dm/uclass.h> 26 #include <dm/uclass-internal.h> 27 #include <dm/util.h> 28 #include <linux/err.h> 29 #include <linux/list.h> 30 31 DECLARE_GLOBAL_DATA_PTR; 32 33 static int device_bind_common(struct udevice *parent, const struct driver *drv, 34 const char *name, void *platdata, 35 ulong driver_data, ofnode node, 36 uint of_platdata_size, struct udevice **devp) 37 { 38 struct udevice *dev; 39 struct uclass *uc; 40 int size, ret = 0; 41 42 if (devp) 43 *devp = NULL; 44 if (!name) 45 return -EINVAL; 46 47 ret = uclass_get(drv->id, &uc); 48 if (ret) { 49 debug("Missing uclass for driver %s\n", drv->name); 50 return ret; 51 } 52 53 #ifdef CONFIG_USING_KERNEL_DTB 54 if (gd->flags & GD_FLG_RELOC) { 55 /* For mmc/nand/spiflash, just update from kernel dtb instead bind again*/ 56 if (drv->id == UCLASS_MMC || drv->id == UCLASS_RKNAND || 57 drv->id == UCLASS_SPI_FLASH || drv->id == UCLASS_MTD) { 58 list_for_each_entry(dev, &uc->dev_head, uclass_node) { 59 if (!strcmp(name, dev->name)) { 60 debug("%s do not bind dev already in list %s\n", 61 __func__, dev->name); 62 dev->node = node; 63 return 0; 64 } 65 } 66 } 67 68 /* Use other nodes from kernel dtb */ 69 struct udevice *n; 70 71 list_for_each_entry_safe(dev, n, &uc->dev_head, uclass_node) { 72 if (!strcmp(name, dev->name) && 73 dev_read_bool(dev, "u-boot,dm-pre-reloc")) { 74 if (drv->id == UCLASS_SERIAL || drv->id == UCLASS_CRYPTO) { 75 /* Always use serial node from U-Boot dtb */ 76 debug("%s do not delete uboot dev: %s\n", 77 __func__, dev->name); 78 return 0; 79 } else { 80 list_del(&dev->uclass_node); 81 } 82 } 83 } 84 } 85 #endif 86 dev = calloc(1, sizeof(struct udevice)); 87 if (!dev) 88 return -ENOMEM; 89 90 INIT_LIST_HEAD(&dev->sibling_node); 91 INIT_LIST_HEAD(&dev->child_head); 92 INIT_LIST_HEAD(&dev->uclass_node); 93 #ifdef CONFIG_DEVRES 94 INIT_LIST_HEAD(&dev->devres_head); 95 #endif 96 dev->platdata = platdata; 97 dev->driver_data = driver_data; 98 dev->name = name; 99 dev->node = node; 100 dev->parent = parent; 101 dev->driver = drv; 102 dev->uclass = uc; 103 104 dev->seq = -1; 105 dev->req_seq = -1; 106 if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) { 107 /* 108 * Some devices, such as a SPI bus, I2C bus and serial ports 109 * are numbered using aliases. 110 * 111 * This is just a 'requested' sequence, and will be 112 * resolved (and ->seq updated) when the device is probed. 113 */ 114 if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) { 115 if (uc->uc_drv->name && ofnode_valid(node)) { 116 dev_read_alias_seq(dev, &dev->req_seq); 117 } 118 } 119 } 120 121 if (drv->platdata_auto_alloc_size) { 122 bool alloc = !platdata; 123 124 if (CONFIG_IS_ENABLED(OF_PLATDATA)) { 125 if (of_platdata_size) { 126 dev->flags |= DM_FLAG_OF_PLATDATA; 127 if (of_platdata_size < 128 drv->platdata_auto_alloc_size) 129 alloc = true; 130 } 131 } 132 if (alloc) { 133 dev->flags |= DM_FLAG_ALLOC_PDATA; 134 dev->platdata = calloc(1, 135 drv->platdata_auto_alloc_size); 136 if (!dev->platdata) { 137 ret = -ENOMEM; 138 goto fail_alloc1; 139 } 140 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) { 141 memcpy(dev->platdata, platdata, 142 of_platdata_size); 143 } 144 } 145 } 146 147 size = uc->uc_drv->per_device_platdata_auto_alloc_size; 148 if (size) { 149 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA; 150 dev->uclass_platdata = calloc(1, size); 151 if (!dev->uclass_platdata) { 152 ret = -ENOMEM; 153 goto fail_alloc2; 154 } 155 } 156 157 if (parent) { 158 size = parent->driver->per_child_platdata_auto_alloc_size; 159 if (!size) { 160 size = parent->uclass->uc_drv-> 161 per_child_platdata_auto_alloc_size; 162 } 163 if (size) { 164 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA; 165 dev->parent_platdata = calloc(1, size); 166 if (!dev->parent_platdata) { 167 ret = -ENOMEM; 168 goto fail_alloc3; 169 } 170 } 171 } 172 173 /* put dev into parent's successor list */ 174 if (parent) 175 list_add_tail(&dev->sibling_node, &parent->child_head); 176 177 ret = uclass_bind_device(dev); 178 if (ret) 179 goto fail_uclass_bind; 180 181 /* if we fail to bind we remove device from successors and free it */ 182 if (drv->bind) { 183 ret = drv->bind(dev); 184 if (ret) 185 goto fail_bind; 186 } 187 if (parent && parent->driver->child_post_bind) { 188 ret = parent->driver->child_post_bind(dev); 189 if (ret) 190 goto fail_child_post_bind; 191 } 192 if (uc->uc_drv->post_bind) { 193 ret = uc->uc_drv->post_bind(dev); 194 if (ret) 195 goto fail_uclass_post_bind; 196 } 197 198 if (parent) 199 pr_debug("Bound device %s to %s\n", dev->name, parent->name); 200 if (devp) 201 *devp = dev; 202 203 dev->flags |= DM_FLAG_BOUND; 204 205 return 0; 206 207 fail_uclass_post_bind: 208 /* There is no child unbind() method, so no clean-up required */ 209 fail_child_post_bind: 210 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { 211 if (drv->unbind && drv->unbind(dev)) { 212 dm_warn("unbind() method failed on dev '%s' on error path\n", 213 dev->name); 214 } 215 } 216 217 fail_bind: 218 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { 219 if (uclass_unbind_device(dev)) { 220 dm_warn("Failed to unbind dev '%s' on error path\n", 221 dev->name); 222 } 223 } 224 fail_uclass_bind: 225 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { 226 list_del(&dev->sibling_node); 227 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { 228 free(dev->parent_platdata); 229 dev->parent_platdata = NULL; 230 } 231 } 232 fail_alloc3: 233 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { 234 free(dev->uclass_platdata); 235 dev->uclass_platdata = NULL; 236 } 237 fail_alloc2: 238 if (dev->flags & DM_FLAG_ALLOC_PDATA) { 239 free(dev->platdata); 240 dev->platdata = NULL; 241 } 242 fail_alloc1: 243 devres_release_all(dev); 244 245 free(dev); 246 247 return ret; 248 } 249 250 int device_bind_with_driver_data(struct udevice *parent, 251 const struct driver *drv, const char *name, 252 ulong driver_data, ofnode node, 253 struct udevice **devp) 254 { 255 return device_bind_common(parent, drv, name, NULL, driver_data, node, 256 0, devp); 257 } 258 259 int device_bind(struct udevice *parent, const struct driver *drv, 260 const char *name, void *platdata, int of_offset, 261 struct udevice **devp) 262 { 263 return device_bind_common(parent, drv, name, platdata, 0, 264 offset_to_ofnode(of_offset), 0, devp); 265 } 266 267 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only, 268 const struct driver_info *info, struct udevice **devp) 269 { 270 struct driver *drv; 271 uint platdata_size = 0; 272 273 drv = lists_driver_lookup_name(info->name); 274 if (!drv) 275 return -ENOENT; 276 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC)) 277 return -EPERM; 278 279 #if CONFIG_IS_ENABLED(OF_PLATDATA) 280 platdata_size = info->platdata_size; 281 #endif 282 return device_bind_common(parent, drv, info->name, 283 (void *)info->platdata, 0, ofnode_null(), platdata_size, 284 devp); 285 } 286 287 static void *alloc_priv(int size, uint flags) 288 { 289 void *priv; 290 291 if (flags & DM_FLAG_ALLOC_PRIV_DMA) { 292 size = ROUND(size, ARCH_DMA_MINALIGN); 293 priv = memalign(ARCH_DMA_MINALIGN, size); 294 if (priv) { 295 memset(priv, '\0', size); 296 297 /* 298 * Ensure that the zero bytes are flushed to memory. 299 * This prevents problems if the driver uses this as 300 * both an input and an output buffer: 301 * 302 * 1. Zeroes written to buffer (here) and sit in the 303 * cache 304 * 2. Driver issues a read command to DMA 305 * 3. CPU runs out of cache space and evicts some cache 306 * data in the buffer, writing zeroes to RAM from 307 * the memset() above 308 * 4. DMA completes 309 * 5. Buffer now has some DMA data and some zeroes 310 * 6. Data being read is now incorrect 311 * 312 * To prevent this, ensure that the cache is clean 313 * within this range at the start. The driver can then 314 * use normal flush-after-write, invalidate-before-read 315 * procedures. 316 * 317 * TODO(sjg@chromium.org): Drop this microblaze 318 * exception. 319 */ 320 #ifndef CONFIG_MICROBLAZE 321 flush_dcache_range((ulong)priv, (ulong)priv + size); 322 #endif 323 } 324 } else { 325 priv = calloc(1, size); 326 } 327 328 return priv; 329 } 330 331 int device_probe(struct udevice *dev) 332 { 333 const struct driver *drv; 334 int size = 0; 335 int ret; 336 int seq; 337 338 if (!dev) 339 return -EINVAL; 340 341 if (dev->flags & DM_FLAG_ACTIVATED) 342 return 0; 343 344 drv = dev->driver; 345 assert(drv); 346 347 /* Allocate private data if requested and not reentered */ 348 if (drv->priv_auto_alloc_size && !dev->priv) { 349 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags); 350 if (!dev->priv) { 351 ret = -ENOMEM; 352 goto fail; 353 } 354 } 355 /* Allocate private data if requested and not reentered */ 356 size = dev->uclass->uc_drv->per_device_auto_alloc_size; 357 if (size && !dev->uclass_priv) { 358 dev->uclass_priv = calloc(1, size); 359 if (!dev->uclass_priv) { 360 ret = -ENOMEM; 361 goto fail; 362 } 363 } 364 365 /* Ensure all parents are probed */ 366 if (dev->parent) { 367 size = dev->parent->driver->per_child_auto_alloc_size; 368 if (!size) { 369 size = dev->parent->uclass->uc_drv-> 370 per_child_auto_alloc_size; 371 } 372 if (size && !dev->parent_priv) { 373 dev->parent_priv = alloc_priv(size, drv->flags); 374 if (!dev->parent_priv) { 375 ret = -ENOMEM; 376 goto fail; 377 } 378 } 379 380 ret = device_probe(dev->parent); 381 if (ret) 382 goto fail; 383 384 /* 385 * The device might have already been probed during 386 * the call to device_probe() on its parent device 387 * (e.g. PCI bridge devices). Test the flags again 388 * so that we don't mess up the device. 389 */ 390 if (dev->flags & DM_FLAG_ACTIVATED) 391 return 0; 392 } 393 394 seq = uclass_resolve_seq(dev); 395 if (seq < 0) { 396 ret = seq; 397 goto fail; 398 } 399 dev->seq = seq; 400 401 dev->flags |= DM_FLAG_ACTIVATED; 402 403 /* 404 * Process pinctrl for everything except the root device, and 405 * continue regardless of the result of pinctrl. Don't process pinctrl 406 * settings for pinctrl devices since the device may not yet be 407 * probed. 408 */ 409 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL) 410 pinctrl_select_state(dev, "default"); 411 412 ret = uclass_pre_probe_device(dev); 413 if (ret) 414 goto fail; 415 416 if (dev->parent && dev->parent->driver->child_pre_probe) { 417 ret = dev->parent->driver->child_pre_probe(dev); 418 if (ret) 419 goto fail; 420 } 421 422 if (drv->ofdata_to_platdata && dev_has_of_node(dev)) { 423 ret = drv->ofdata_to_platdata(dev); 424 if (ret) 425 goto fail; 426 } 427 428 if (drv->probe) { 429 ret = drv->probe(dev); 430 if (ret) { 431 dev->flags &= ~DM_FLAG_ACTIVATED; 432 goto fail; 433 } 434 } 435 436 ret = uclass_post_probe_device(dev); 437 if (ret) 438 goto fail_uclass; 439 440 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) 441 pinctrl_select_state(dev, "default"); 442 443 return 0; 444 fail_uclass: 445 if (device_remove(dev, DM_REMOVE_NORMAL)) { 446 dm_warn("%s: Device '%s' failed to remove on error path\n", 447 __func__, dev->name); 448 } 449 fail: 450 dev->flags &= ~DM_FLAG_ACTIVATED; 451 452 dev->seq = -1; 453 device_free(dev); 454 455 return ret; 456 } 457 458 void *dev_get_platdata(struct udevice *dev) 459 { 460 if (!dev) { 461 dm_warn("%s: null device\n", __func__); 462 return NULL; 463 } 464 465 return dev->platdata; 466 } 467 468 void *dev_get_parent_platdata(struct udevice *dev) 469 { 470 if (!dev) { 471 dm_warn("%s: null device\n", __func__); 472 return NULL; 473 } 474 475 return dev->parent_platdata; 476 } 477 478 void *dev_get_uclass_platdata(struct udevice *dev) 479 { 480 if (!dev) { 481 dm_warn("%s: null device\n", __func__); 482 return NULL; 483 } 484 485 return dev->uclass_platdata; 486 } 487 488 void *dev_get_priv(struct udevice *dev) 489 { 490 if (!dev) { 491 dm_warn("%s: null device\n", __func__); 492 return NULL; 493 } 494 495 return dev->priv; 496 } 497 498 void *dev_get_uclass_priv(struct udevice *dev) 499 { 500 if (!dev) { 501 dm_warn("%s: null device\n", __func__); 502 return NULL; 503 } 504 505 return dev->uclass_priv; 506 } 507 508 void *dev_get_parent_priv(struct udevice *dev) 509 { 510 if (!dev) { 511 dm_warn("%s: null device\n", __func__); 512 return NULL; 513 } 514 515 return dev->parent_priv; 516 } 517 518 static int device_get_device_tail(struct udevice *dev, int ret, 519 struct udevice **devp) 520 { 521 if (ret) 522 return ret; 523 524 ret = device_probe(dev); 525 if (ret) 526 return ret; 527 528 *devp = dev; 529 530 return 0; 531 } 532 533 int device_get_child(struct udevice *parent, int index, struct udevice **devp) 534 { 535 struct udevice *dev; 536 537 list_for_each_entry(dev, &parent->child_head, sibling_node) { 538 if (!index--) 539 return device_get_device_tail(dev, 0, devp); 540 } 541 542 return -ENODEV; 543 } 544 545 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, 546 bool find_req_seq, struct udevice **devp) 547 { 548 struct udevice *dev; 549 550 *devp = NULL; 551 if (seq_or_req_seq == -1) 552 return -ENODEV; 553 554 list_for_each_entry(dev, &parent->child_head, sibling_node) { 555 if ((find_req_seq ? dev->req_seq : dev->seq) == 556 seq_or_req_seq) { 557 *devp = dev; 558 return 0; 559 } 560 } 561 562 return -ENODEV; 563 } 564 565 int device_get_child_by_seq(struct udevice *parent, int seq, 566 struct udevice **devp) 567 { 568 struct udevice *dev; 569 int ret; 570 571 *devp = NULL; 572 ret = device_find_child_by_seq(parent, seq, false, &dev); 573 if (ret == -ENODEV) { 574 /* 575 * We didn't find it in probed devices. See if there is one 576 * that will request this seq if probed. 577 */ 578 ret = device_find_child_by_seq(parent, seq, true, &dev); 579 } 580 return device_get_device_tail(dev, ret, devp); 581 } 582 583 int device_find_child_by_of_offset(struct udevice *parent, int of_offset, 584 struct udevice **devp) 585 { 586 struct udevice *dev; 587 588 *devp = NULL; 589 590 list_for_each_entry(dev, &parent->child_head, sibling_node) { 591 if (dev_of_offset(dev) == of_offset) { 592 *devp = dev; 593 return 0; 594 } 595 } 596 597 return -ENODEV; 598 } 599 600 int device_get_child_by_of_offset(struct udevice *parent, int node, 601 struct udevice **devp) 602 { 603 struct udevice *dev; 604 int ret; 605 606 *devp = NULL; 607 ret = device_find_child_by_of_offset(parent, node, &dev); 608 return device_get_device_tail(dev, ret, devp); 609 } 610 611 static struct udevice *_device_find_global_by_of_offset(struct udevice *parent, 612 int of_offset) 613 { 614 struct udevice *dev, *found; 615 616 if (dev_of_offset(parent) == of_offset) 617 return parent; 618 619 list_for_each_entry(dev, &parent->child_head, sibling_node) { 620 found = _device_find_global_by_of_offset(dev, of_offset); 621 if (found) 622 return found; 623 } 624 625 return NULL; 626 } 627 628 int device_get_global_by_of_offset(int of_offset, struct udevice **devp) 629 { 630 struct udevice *dev; 631 632 dev = _device_find_global_by_of_offset(gd->dm_root, of_offset); 633 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); 634 } 635 636 int device_find_first_child(struct udevice *parent, struct udevice **devp) 637 { 638 if (list_empty(&parent->child_head)) { 639 *devp = NULL; 640 } else { 641 *devp = list_first_entry(&parent->child_head, struct udevice, 642 sibling_node); 643 } 644 645 return 0; 646 } 647 648 int device_find_next_child(struct udevice **devp) 649 { 650 struct udevice *dev = *devp; 651 struct udevice *parent = dev->parent; 652 653 if (list_is_last(&dev->sibling_node, &parent->child_head)) { 654 *devp = NULL; 655 } else { 656 *devp = list_entry(dev->sibling_node.next, struct udevice, 657 sibling_node); 658 } 659 660 return 0; 661 } 662 663 struct udevice *dev_get_parent(struct udevice *child) 664 { 665 return child->parent; 666 } 667 668 ulong dev_get_driver_data(struct udevice *dev) 669 { 670 return dev->driver_data; 671 } 672 673 const void *dev_get_driver_ops(struct udevice *dev) 674 { 675 if (!dev || !dev->driver->ops) 676 return NULL; 677 678 return dev->driver->ops; 679 } 680 681 enum uclass_id device_get_uclass_id(struct udevice *dev) 682 { 683 return dev->uclass->uc_drv->id; 684 } 685 686 const char *dev_get_uclass_name(struct udevice *dev) 687 { 688 if (!dev) 689 return NULL; 690 691 return dev->uclass->uc_drv->name; 692 } 693 694 bool device_has_children(struct udevice *dev) 695 { 696 return !list_empty(&dev->child_head); 697 } 698 699 bool device_has_active_children(struct udevice *dev) 700 { 701 struct udevice *child; 702 703 for (device_find_first_child(dev, &child); 704 child; 705 device_find_next_child(&child)) { 706 if (device_active(child)) 707 return true; 708 } 709 710 return false; 711 } 712 713 bool device_is_last_sibling(struct udevice *dev) 714 { 715 struct udevice *parent = dev->parent; 716 717 if (!parent) 718 return false; 719 return list_is_last(&dev->sibling_node, &parent->child_head); 720 } 721 722 void device_set_name_alloced(struct udevice *dev) 723 { 724 dev->flags |= DM_FLAG_NAME_ALLOCED; 725 } 726 727 int device_set_name(struct udevice *dev, const char *name) 728 { 729 name = strdup(name); 730 if (!name) 731 return -ENOMEM; 732 dev->name = name; 733 device_set_name_alloced(dev); 734 735 return 0; 736 } 737 738 bool device_is_compatible(struct udevice *dev, const char *compat) 739 { 740 const void *fdt = gd->fdt_blob; 741 ofnode node = dev_ofnode(dev); 742 743 if (ofnode_is_np(node)) 744 return of_device_is_compatible(ofnode_to_np(node), compat, NULL, NULL); 745 else 746 return !fdt_node_check_compatible(fdt, ofnode_to_offset(node), compat); 747 } 748 749 bool of_machine_is_compatible(const char *compat) 750 { 751 const void *fdt = gd->fdt_blob; 752 753 return !fdt_node_check_compatible(fdt, 0, compat); 754 } 755