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