root.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <fdtdec.h>
  12. #include <malloc.h>
  13. #include <libfdt.h>
  14. #include <dm/device.h>
  15. #include <dm/device-internal.h>
  16. #include <dm/lists.h>
  17. #include <dm/platdata.h>
  18. #include <dm/root.h>
  19. #include <dm/uclass.h>
  20. #include <dm/util.h>
  21. #include <linux/list.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. struct root_priv {
  24. fdt_addr_t translation_offset; /* optional translation offset */
  25. };
  26. static const struct driver_info root_info = {
  27. .name = "root_driver",
  28. };
  29. struct udevice *dm_root(void)
  30. {
  31. if (!gd->dm_root) {
  32. dm_warn("Virtual root driver does not exist!\n");
  33. return NULL;
  34. }
  35. return gd->dm_root;
  36. }
  37. void dm_fixup_for_gd_move(struct global_data *new_gd)
  38. {
  39. /* The sentinel node has moved, so update things that point to it */
  40. new_gd->uclass_root.next->prev = &new_gd->uclass_root;
  41. new_gd->uclass_root.prev->next = &new_gd->uclass_root;
  42. }
  43. fdt_addr_t dm_get_translation_offset(void)
  44. {
  45. struct udevice *root = dm_root();
  46. struct root_priv *priv = dev_get_priv(root);
  47. return priv->translation_offset;
  48. }
  49. void dm_set_translation_offset(fdt_addr_t offs)
  50. {
  51. struct udevice *root = dm_root();
  52. struct root_priv *priv = dev_get_priv(root);
  53. priv->translation_offset = offs;
  54. }
  55. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  56. void fix_drivers(void)
  57. {
  58. struct driver *drv =
  59. ll_entry_start(struct driver, driver);
  60. const int n_ents = ll_entry_count(struct driver, driver);
  61. struct driver *entry;
  62. for (entry = drv; entry != drv + n_ents; entry++) {
  63. if (entry->of_match)
  64. entry->of_match = (const struct udevice_id *)
  65. ((u32)entry->of_match + gd->reloc_off);
  66. if (entry->bind)
  67. entry->bind += gd->reloc_off;
  68. if (entry->probe)
  69. entry->probe += gd->reloc_off;
  70. if (entry->remove)
  71. entry->remove += gd->reloc_off;
  72. if (entry->unbind)
  73. entry->unbind += gd->reloc_off;
  74. if (entry->ofdata_to_platdata)
  75. entry->ofdata_to_platdata += gd->reloc_off;
  76. if (entry->child_post_bind)
  77. entry->child_post_bind += gd->reloc_off;
  78. if (entry->child_pre_probe)
  79. entry->child_pre_probe += gd->reloc_off;
  80. if (entry->child_post_remove)
  81. entry->child_post_remove += gd->reloc_off;
  82. /* OPS are fixed in every uclass post_probe function */
  83. if (entry->ops)
  84. entry->ops += gd->reloc_off;
  85. }
  86. }
  87. void fix_uclass(void)
  88. {
  89. struct uclass_driver *uclass =
  90. ll_entry_start(struct uclass_driver, uclass);
  91. const int n_ents = ll_entry_count(struct uclass_driver, uclass);
  92. struct uclass_driver *entry;
  93. for (entry = uclass; entry != uclass + n_ents; entry++) {
  94. if (entry->post_bind)
  95. entry->post_bind += gd->reloc_off;
  96. if (entry->pre_unbind)
  97. entry->pre_unbind += gd->reloc_off;
  98. if (entry->pre_probe)
  99. entry->pre_probe += gd->reloc_off;
  100. if (entry->post_probe)
  101. entry->post_probe += gd->reloc_off;
  102. if (entry->pre_remove)
  103. entry->pre_remove += gd->reloc_off;
  104. if (entry->child_post_bind)
  105. entry->child_post_bind += gd->reloc_off;
  106. if (entry->child_pre_probe)
  107. entry->child_pre_probe += gd->reloc_off;
  108. if (entry->init)
  109. entry->init += gd->reloc_off;
  110. if (entry->destroy)
  111. entry->destroy += gd->reloc_off;
  112. /* FIXME maybe also need to fix these ops */
  113. if (entry->ops)
  114. entry->ops += gd->reloc_off;
  115. }
  116. }
  117. void fix_devices(void)
  118. {
  119. struct driver_info *dev =
  120. ll_entry_start(struct driver_info, driver_info);
  121. const int n_ents = ll_entry_count(struct driver_info, driver_info);
  122. struct driver_info *entry;
  123. for (entry = dev; entry != dev + n_ents; entry++) {
  124. if (entry->platdata)
  125. entry->platdata += gd->reloc_off;
  126. }
  127. }
  128. #endif
  129. int dm_init(void)
  130. {
  131. int ret;
  132. if (gd->dm_root) {
  133. dm_warn("Virtual root driver already exists!\n");
  134. return -EINVAL;
  135. }
  136. INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
  137. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  138. fix_drivers();
  139. fix_uclass();
  140. fix_devices();
  141. #endif
  142. ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
  143. if (ret)
  144. return ret;
  145. #if CONFIG_IS_ENABLED(OF_CONTROL)
  146. DM_ROOT_NON_CONST->of_offset = 0;
  147. #endif
  148. ret = device_probe(DM_ROOT_NON_CONST);
  149. if (ret)
  150. return ret;
  151. return 0;
  152. }
  153. int dm_uninit(void)
  154. {
  155. device_remove(dm_root());
  156. device_unbind(dm_root());
  157. return 0;
  158. }
  159. int dm_scan_platdata(bool pre_reloc_only)
  160. {
  161. int ret;
  162. ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
  163. if (ret == -ENOENT) {
  164. dm_warn("Some drivers were not found\n");
  165. ret = 0;
  166. }
  167. return ret;
  168. }
  169. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  170. int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
  171. bool pre_reloc_only)
  172. {
  173. int ret = 0, err;
  174. for (offset = fdt_first_subnode(blob, offset);
  175. offset > 0;
  176. offset = fdt_next_subnode(blob, offset)) {
  177. if (pre_reloc_only &&
  178. !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
  179. continue;
  180. if (!fdtdec_get_is_enabled(blob, offset)) {
  181. dm_dbg(" - ignoring disabled device\n");
  182. continue;
  183. }
  184. err = lists_bind_fdt(parent, blob, offset, NULL);
  185. if (err && !ret) {
  186. ret = err;
  187. debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
  188. ret);
  189. }
  190. }
  191. if (ret)
  192. dm_warn("Some drivers failed to bind\n");
  193. return ret;
  194. }
  195. int dm_scan_fdt_dev(struct udevice *dev)
  196. {
  197. if (dev_of_offset(dev) == -1)
  198. return 0;
  199. return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
  200. gd->flags & GD_FLG_RELOC ? false : true);
  201. }
  202. int dm_scan_fdt(const void *blob, bool pre_reloc_only)
  203. {
  204. return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
  205. }
  206. #endif
  207. __weak int dm_scan_other(bool pre_reloc_only)
  208. {
  209. return 0;
  210. }
  211. int dm_init_and_scan(bool pre_reloc_only)
  212. {
  213. int ret;
  214. ret = dm_init();
  215. if (ret) {
  216. debug("dm_init() failed: %d\n", ret);
  217. return ret;
  218. }
  219. ret = dm_scan_platdata(pre_reloc_only);
  220. if (ret) {
  221. debug("dm_scan_platdata() failed: %d\n", ret);
  222. return ret;
  223. }
  224. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  225. ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
  226. if (ret) {
  227. debug("dm_scan_fdt() failed: %d\n", ret);
  228. return ret;
  229. }
  230. }
  231. ret = dm_scan_other(pre_reloc_only);
  232. if (ret)
  233. return ret;
  234. return 0;
  235. }
  236. /* This is the root driver - all drivers are children of this */
  237. U_BOOT_DRIVER(root_driver) = {
  238. .name = "root_driver",
  239. .id = UCLASS_ROOT,
  240. .priv_auto_alloc_size = sizeof(struct root_priv),
  241. };
  242. /* This is the root uclass */
  243. UCLASS_DRIVER(root) = {
  244. .name = "root",
  245. .id = UCLASS_ROOT,
  246. };