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