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