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