root.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * (C) Copyright 2012
  5. * Pavel Herrmann <morpheus.ibis@gmail.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <errno.h>
  11. #include <malloc.h>
  12. #include <libfdt.h>
  13. #include <dm/device.h>
  14. #include <dm/device-internal.h>
  15. #include <dm/lists.h>
  16. #include <dm/platdata.h>
  17. #include <dm/root.h>
  18. #include <dm/uclass.h>
  19. #include <dm/util.h>
  20. #include <linux/list.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. static const struct driver_info root_info = {
  23. .name = "root_driver",
  24. };
  25. struct udevice *dm_root(void)
  26. {
  27. if (!gd->dm_root) {
  28. dm_warn("Virtual root driver does not exist!\n");
  29. return NULL;
  30. }
  31. return gd->dm_root;
  32. }
  33. int dm_init(void)
  34. {
  35. int ret;
  36. if (gd->dm_root) {
  37. dm_warn("Virtual root driver already exists!\n");
  38. return -EINVAL;
  39. }
  40. INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
  41. ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
  42. if (ret)
  43. return ret;
  44. ret = device_probe(DM_ROOT_NON_CONST);
  45. if (ret)
  46. return ret;
  47. return 0;
  48. }
  49. int dm_uninit(void)
  50. {
  51. device_remove(dm_root());
  52. device_unbind(dm_root());
  53. return 0;
  54. }
  55. int dm_scan_platdata(bool pre_reloc_only)
  56. {
  57. int ret;
  58. ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
  59. if (ret == -ENOENT) {
  60. dm_warn("Some drivers were not found\n");
  61. ret = 0;
  62. }
  63. return ret;
  64. }
  65. #ifdef CONFIG_OF_CONTROL
  66. int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
  67. bool pre_reloc_only)
  68. {
  69. int ret = 0, err;
  70. for (offset = fdt_first_subnode(blob, offset);
  71. offset > 0;
  72. offset = fdt_next_subnode(blob, offset)) {
  73. if (pre_reloc_only &&
  74. !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
  75. continue;
  76. err = lists_bind_fdt(parent, blob, offset, NULL);
  77. if (err && !ret)
  78. ret = err;
  79. }
  80. if (ret)
  81. dm_warn("Some drivers failed to bind\n");
  82. return ret;
  83. }
  84. int dm_scan_fdt(const void *blob, bool pre_reloc_only)
  85. {
  86. return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
  87. }
  88. #endif
  89. __weak int dm_scan_other(bool pre_reloc_only)
  90. {
  91. return 0;
  92. }
  93. int dm_init_and_scan(bool pre_reloc_only)
  94. {
  95. int ret;
  96. ret = dm_init();
  97. if (ret) {
  98. debug("dm_init() failed: %d\n", ret);
  99. return ret;
  100. }
  101. ret = dm_scan_platdata(pre_reloc_only);
  102. if (ret) {
  103. debug("dm_scan_platdata() failed: %d\n", ret);
  104. return ret;
  105. }
  106. #ifdef CONFIG_OF_CONTROL
  107. ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
  108. if (ret) {
  109. debug("dm_scan_fdt() failed: %d\n", ret);
  110. return ret;
  111. }
  112. #endif
  113. ret = dm_scan_other(pre_reloc_only);
  114. if (ret)
  115. return ret;
  116. return 0;
  117. }
  118. /* This is the root driver - all drivers are children of this */
  119. U_BOOT_DRIVER(root_driver) = {
  120. .name = "root_driver",
  121. .id = UCLASS_ROOT,
  122. };
  123. /* This is the root uclass */
  124. UCLASS_DRIVER(root) = {
  125. .name = "root",
  126. .id = UCLASS_ROOT,
  127. };