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