root.c 6.4 KB

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