root.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013 Google, Inc
  4. *
  5. * (C) Copyright 2012
  6. * Pavel Herrmann <morpheus.ibis@gmail.com>
  7. */
  8. #include <common.h>
  9. #include <errno.h>
  10. #include <fdtdec.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <linux/libfdt.h>
  14. #include <dm/acpi.h>
  15. #include <dm/device.h>
  16. #include <dm/device-internal.h>
  17. #include <dm/lists.h>
  18. #include <dm/of.h>
  19. #include <dm/of_access.h>
  20. #include <dm/platdata.h>
  21. #include <dm/read.h>
  22. #include <dm/root.h>
  23. #include <dm/uclass.h>
  24. #include <dm/util.h>
  25. #include <linux/list.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. static struct driver_info root_info = {
  28. .name = "root_driver",
  29. };
  30. struct udevice *dm_root(void)
  31. {
  32. if (!gd->dm_root) {
  33. dm_warn("Virtual root driver does not exist!\n");
  34. return NULL;
  35. }
  36. return gd->dm_root;
  37. }
  38. void dm_fixup_for_gd_move(struct global_data *new_gd)
  39. {
  40. /* The sentinel node has moved, so update things that point to it */
  41. if (gd->dm_root) {
  42. new_gd->uclass_root.next->prev = &new_gd->uclass_root;
  43. new_gd->uclass_root.prev->next = &new_gd->uclass_root;
  44. }
  45. }
  46. void fix_drivers(void)
  47. {
  48. struct driver *drv =
  49. ll_entry_start(struct driver, driver);
  50. const int n_ents = ll_entry_count(struct driver, driver);
  51. struct driver *entry;
  52. for (entry = drv; entry != drv + n_ents; entry++) {
  53. if (entry->of_match)
  54. entry->of_match = (const struct udevice_id *)
  55. ((ulong)entry->of_match + gd->reloc_off);
  56. if (entry->bind)
  57. entry->bind += gd->reloc_off;
  58. if (entry->probe)
  59. entry->probe += gd->reloc_off;
  60. if (entry->remove)
  61. entry->remove += gd->reloc_off;
  62. if (entry->unbind)
  63. entry->unbind += gd->reloc_off;
  64. if (entry->ofdata_to_platdata)
  65. entry->ofdata_to_platdata += gd->reloc_off;
  66. if (entry->child_post_bind)
  67. entry->child_post_bind += gd->reloc_off;
  68. if (entry->child_pre_probe)
  69. entry->child_pre_probe += gd->reloc_off;
  70. if (entry->child_post_remove)
  71. entry->child_post_remove += gd->reloc_off;
  72. /* OPS are fixed in every uclass post_probe function */
  73. if (entry->ops)
  74. entry->ops += gd->reloc_off;
  75. }
  76. }
  77. void fix_uclass(void)
  78. {
  79. struct uclass_driver *uclass =
  80. ll_entry_start(struct uclass_driver, uclass);
  81. const int n_ents = ll_entry_count(struct uclass_driver, uclass);
  82. struct uclass_driver *entry;
  83. for (entry = uclass; entry != uclass + n_ents; entry++) {
  84. if (entry->post_bind)
  85. entry->post_bind += gd->reloc_off;
  86. if (entry->pre_unbind)
  87. entry->pre_unbind += gd->reloc_off;
  88. if (entry->pre_probe)
  89. entry->pre_probe += gd->reloc_off;
  90. if (entry->post_probe)
  91. entry->post_probe += gd->reloc_off;
  92. if (entry->pre_remove)
  93. entry->pre_remove += gd->reloc_off;
  94. if (entry->child_post_bind)
  95. entry->child_post_bind += gd->reloc_off;
  96. if (entry->child_pre_probe)
  97. entry->child_pre_probe += gd->reloc_off;
  98. if (entry->init)
  99. entry->init += gd->reloc_off;
  100. if (entry->destroy)
  101. entry->destroy += gd->reloc_off;
  102. /* FIXME maybe also need to fix these ops */
  103. if (entry->ops)
  104. entry->ops += gd->reloc_off;
  105. }
  106. }
  107. void fix_devices(void)
  108. {
  109. struct driver_info *dev =
  110. ll_entry_start(struct driver_info, driver_info);
  111. const int n_ents = ll_entry_count(struct driver_info, driver_info);
  112. struct driver_info *entry;
  113. for (entry = dev; entry != dev + n_ents; entry++) {
  114. if (entry->plat)
  115. entry->plat += gd->reloc_off;
  116. }
  117. }
  118. int dm_init(bool of_live)
  119. {
  120. int ret;
  121. if (gd->dm_root) {
  122. dm_warn("Virtual root driver already exists!\n");
  123. return -EINVAL;
  124. }
  125. INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
  126. if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
  127. fix_drivers();
  128. fix_uclass();
  129. fix_devices();
  130. }
  131. ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
  132. if (ret)
  133. return ret;
  134. if (CONFIG_IS_ENABLED(OF_CONTROL))
  135. DM_ROOT_NON_CONST->node = ofnode_root();
  136. ret = device_probe(DM_ROOT_NON_CONST);
  137. if (ret)
  138. return ret;
  139. return 0;
  140. }
  141. int dm_uninit(void)
  142. {
  143. device_remove(dm_root(), DM_REMOVE_NORMAL);
  144. device_unbind(dm_root());
  145. gd->dm_root = NULL;
  146. return 0;
  147. }
  148. #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
  149. int dm_remove_devices_flags(uint flags)
  150. {
  151. device_remove(dm_root(), flags);
  152. return 0;
  153. }
  154. #endif
  155. int dm_scan_platdata(bool pre_reloc_only)
  156. {
  157. int ret;
  158. if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
  159. struct driver_rt *dyn;
  160. int n_ents;
  161. n_ents = ll_entry_count(struct driver_info, driver_info);
  162. dyn = calloc(n_ents, sizeof(struct driver_rt));
  163. if (!dyn)
  164. return -ENOMEM;
  165. gd_set_dm_driver_rt(dyn);
  166. }
  167. ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
  168. if (ret == -ENOENT) {
  169. dm_warn("Some drivers were not found\n");
  170. ret = 0;
  171. }
  172. return ret;
  173. }
  174. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  175. /**
  176. * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
  177. *
  178. * This scans the subnodes of a device tree node and and creates a driver
  179. * for each one.
  180. *
  181. * @parent: Parent device for the devices that will be created
  182. * @node: Node to scan
  183. * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
  184. * flag. If false bind all drivers.
  185. * @return 0 if OK, -ve on error
  186. */
  187. static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
  188. bool pre_reloc_only)
  189. {
  190. int ret = 0, err;
  191. ofnode node;
  192. if (!ofnode_valid(parent_node))
  193. return 0;
  194. for (node = ofnode_first_subnode(parent_node);
  195. ofnode_valid(node);
  196. node = ofnode_next_subnode(node)) {
  197. const char *node_name = ofnode_get_name(node);
  198. if (!ofnode_is_enabled(node)) {
  199. pr_debug(" - ignoring disabled device\n");
  200. continue;
  201. }
  202. err = lists_bind_fdt(parent, node, NULL, pre_reloc_only);
  203. if (err && !ret) {
  204. ret = err;
  205. debug("%s: ret=%d\n", node_name, ret);
  206. }
  207. }
  208. if (ret)
  209. dm_warn("Some drivers failed to bind\n");
  210. return ret;
  211. }
  212. int dm_scan_fdt_dev(struct udevice *dev)
  213. {
  214. return dm_scan_fdt_node(dev, dev_ofnode(dev),
  215. gd->flags & GD_FLG_RELOC ? false : true);
  216. }
  217. int dm_scan_fdt(bool pre_reloc_only)
  218. {
  219. return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
  220. }
  221. static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
  222. {
  223. ofnode node;
  224. node = ofnode_path(path);
  225. return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
  226. }
  227. int dm_extended_scan(bool pre_reloc_only)
  228. {
  229. int ret, i;
  230. const char * const nodes[] = {
  231. "/chosen",
  232. "/clocks",
  233. "/firmware"
  234. };
  235. ret = dm_scan_fdt(pre_reloc_only);
  236. if (ret) {
  237. debug("dm_scan_fdt() failed: %d\n", ret);
  238. return ret;
  239. }
  240. /* Some nodes aren't devices themselves but may contain some */
  241. for (i = 0; i < ARRAY_SIZE(nodes); i++) {
  242. ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
  243. if (ret) {
  244. debug("dm_scan_fdt() scan for %s failed: %d\n",
  245. nodes[i], ret);
  246. return ret;
  247. }
  248. }
  249. return ret;
  250. }
  251. #endif
  252. __weak int dm_scan_other(bool pre_reloc_only)
  253. {
  254. return 0;
  255. }
  256. int dm_init_and_scan(bool pre_reloc_only)
  257. {
  258. int ret;
  259. if (CONFIG_IS_ENABLED(OF_PLATDATA))
  260. dm_populate_phandle_data();
  261. ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
  262. if (ret) {
  263. debug("dm_init() failed: %d\n", ret);
  264. return ret;
  265. }
  266. ret = dm_scan_platdata(pre_reloc_only);
  267. if (ret) {
  268. debug("dm_scan_platdata() failed: %d\n", ret);
  269. return ret;
  270. }
  271. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  272. ret = dm_extended_scan(pre_reloc_only);
  273. if (ret) {
  274. debug("dm_extended_scan() failed: %d\n", ret);
  275. return ret;
  276. }
  277. }
  278. ret = dm_scan_other(pre_reloc_only);
  279. if (ret)
  280. return ret;
  281. return 0;
  282. }
  283. #ifdef CONFIG_ACPIGEN
  284. static int root_acpi_get_name(const struct udevice *dev, char *out_name)
  285. {
  286. return acpi_copy_name(out_name, "\\_SB");
  287. }
  288. struct acpi_ops root_acpi_ops = {
  289. .get_name = root_acpi_get_name,
  290. };
  291. #endif
  292. /* This is the root driver - all drivers are children of this */
  293. U_BOOT_DRIVER(root_driver) = {
  294. .name = "root_driver",
  295. .id = UCLASS_ROOT,
  296. ACPI_OPS_PTR(&root_acpi_ops)
  297. };
  298. /* This is the root uclass */
  299. UCLASS_DRIVER(root) = {
  300. .name = "root",
  301. .id = UCLASS_ROOT,
  302. };