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