root.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if (ret)
  64. return ret;
  65. return 0;
  66. }
  67. #ifdef CONFIG_OF_CONTROL
  68. int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
  69. bool pre_reloc_only)
  70. {
  71. int ret = 0, err;
  72. for (offset = fdt_first_subnode(blob, offset);
  73. offset > 0;
  74. offset = fdt_next_subnode(blob, offset)) {
  75. if (pre_reloc_only &&
  76. !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
  77. continue;
  78. err = lists_bind_fdt(parent, blob, offset);
  79. if (err && !ret)
  80. ret = err;
  81. }
  82. if (ret)
  83. dm_warn("Some drivers failed to bind\n");
  84. return ret;
  85. }
  86. int dm_scan_fdt(const void *blob, bool pre_reloc_only)
  87. {
  88. return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
  89. }
  90. #endif
  91. int dm_init_and_scan(bool pre_reloc_only)
  92. {
  93. int ret;
  94. ret = dm_init();
  95. if (ret) {
  96. debug("dm_init() failed: %d\n", ret);
  97. return ret;
  98. }
  99. ret = dm_scan_platdata(pre_reloc_only);
  100. if (ret) {
  101. debug("dm_scan_platdata() failed: %d\n", ret);
  102. return ret;
  103. }
  104. #ifdef CONFIG_OF_CONTROL
  105. ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
  106. if (ret) {
  107. debug("dm_scan_fdt() failed: %d\n", ret);
  108. return ret;
  109. }
  110. #endif
  111. return 0;
  112. }
  113. /* This is the root driver - all drivers are children of this */
  114. U_BOOT_DRIVER(root_driver) = {
  115. .name = "root_driver",
  116. .id = UCLASS_ROOT,
  117. };
  118. /* This is the root uclass */
  119. UCLASS_DRIVER(root) = {
  120. .name = "root",
  121. .id = UCLASS_ROOT,
  122. };