root.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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->platdata)
  115. entry->platdata += 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. if (CONFIG_IS_ENABLED(OF_LIVE) && of_live)
  136. DM_ROOT_NON_CONST->node = np_to_ofnode(gd_of_root());
  137. else
  138. DM_ROOT_NON_CONST->node = offset_to_ofnode(0);
  139. #endif
  140. ret = device_probe(DM_ROOT_NON_CONST);
  141. if (ret)
  142. return ret;
  143. return 0;
  144. }
  145. int dm_uninit(void)
  146. {
  147. device_remove(dm_root(), DM_REMOVE_NORMAL);
  148. device_unbind(dm_root());
  149. gd->dm_root = NULL;
  150. return 0;
  151. }
  152. #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
  153. int dm_remove_devices_flags(uint flags)
  154. {
  155. device_remove(dm_root(), flags);
  156. return 0;
  157. }
  158. #endif
  159. int dm_scan_platdata(bool pre_reloc_only)
  160. {
  161. int ret;
  162. if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
  163. struct driver_rt *dyn;
  164. int n_ents;
  165. n_ents = ll_entry_count(struct driver_info, driver_info);
  166. dyn = calloc(n_ents, sizeof(struct driver_rt));
  167. if (!dyn)
  168. return -ENOMEM;
  169. gd_set_dm_driver_rt(dyn);
  170. }
  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. static int dm_scan_fdt_live(struct udevice *parent,
  180. const struct device_node *node_parent,
  181. bool pre_reloc_only)
  182. {
  183. struct device_node *np;
  184. int ret = 0, err;
  185. for (np = node_parent->child; np; np = np->sibling) {
  186. if (!of_device_is_available(np)) {
  187. pr_debug(" - ignoring disabled device\n");
  188. continue;
  189. }
  190. err = lists_bind_fdt(parent, np_to_ofnode(np), NULL,
  191. pre_reloc_only);
  192. if (err && !ret) {
  193. ret = err;
  194. debug("%s: ret=%d\n", np->name, ret);
  195. }
  196. }
  197. if (ret)
  198. dm_warn("Some drivers failed to bind\n");
  199. return ret;
  200. }
  201. /**
  202. * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
  203. *
  204. * This scans the subnodes of a device tree node and and creates a driver
  205. * for each one.
  206. *
  207. * @parent: Parent device for the devices that will be created
  208. * @blob: Pointer to device tree blob
  209. * @offset: Offset of node to scan
  210. * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
  211. * flag. If false bind all drivers.
  212. * @return 0 if OK, -ve on error
  213. */
  214. static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
  215. int offset, bool pre_reloc_only)
  216. {
  217. int ret = 0, err;
  218. for (offset = fdt_first_subnode(blob, offset);
  219. offset > 0;
  220. offset = fdt_next_subnode(blob, offset)) {
  221. const char *node_name = fdt_get_name(blob, offset, NULL);
  222. if (!fdtdec_get_is_enabled(blob, offset)) {
  223. pr_debug(" - ignoring disabled device\n");
  224. continue;
  225. }
  226. err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL,
  227. pre_reloc_only);
  228. if (err && !ret) {
  229. ret = err;
  230. debug("%s: ret=%d\n", node_name, ret);
  231. }
  232. }
  233. if (ret)
  234. dm_warn("Some drivers failed to bind\n");
  235. return ret;
  236. }
  237. int dm_scan_fdt_dev(struct udevice *dev)
  238. {
  239. if (!dev_of_valid(dev))
  240. return 0;
  241. if (of_live_active())
  242. return dm_scan_fdt_live(dev, dev_np(dev),
  243. gd->flags & GD_FLG_RELOC ? false : true);
  244. return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
  245. gd->flags & GD_FLG_RELOC ? false : true);
  246. }
  247. int dm_scan_fdt(const void *blob, bool pre_reloc_only)
  248. {
  249. if (of_live_active())
  250. return dm_scan_fdt_live(gd->dm_root, gd_of_root(),
  251. pre_reloc_only);
  252. return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
  253. }
  254. static int dm_scan_fdt_ofnode_path(const void *blob, const char *path,
  255. bool pre_reloc_only)
  256. {
  257. ofnode node;
  258. node = ofnode_path(path);
  259. if (!ofnode_valid(node))
  260. return 0;
  261. if (of_live_active())
  262. return dm_scan_fdt_live(gd->dm_root, node.np, pre_reloc_only);
  263. return dm_scan_fdt_node(gd->dm_root, blob, node.of_offset,
  264. pre_reloc_only);
  265. }
  266. int dm_extended_scan_fdt(const void *blob, bool pre_reloc_only)
  267. {
  268. int ret, i;
  269. const char * const nodes[] = {
  270. "/chosen",
  271. "/clocks",
  272. "/firmware"
  273. };
  274. ret = dm_scan_fdt(blob, pre_reloc_only);
  275. if (ret) {
  276. debug("dm_scan_fdt() failed: %d\n", ret);
  277. return ret;
  278. }
  279. /* Some nodes aren't devices themselves but may contain some */
  280. for (i = 0; i < ARRAY_SIZE(nodes); i++) {
  281. ret = dm_scan_fdt_ofnode_path(blob, nodes[i], pre_reloc_only);
  282. if (ret) {
  283. debug("dm_scan_fdt() scan for %s failed: %d\n",
  284. nodes[i], ret);
  285. return ret;
  286. }
  287. }
  288. return ret;
  289. }
  290. #endif
  291. __weak int dm_scan_other(bool pre_reloc_only)
  292. {
  293. return 0;
  294. }
  295. int dm_init_and_scan(bool pre_reloc_only)
  296. {
  297. int ret;
  298. if (CONFIG_IS_ENABLED(OF_PLATDATA))
  299. dm_populate_phandle_data();
  300. ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
  301. if (ret) {
  302. debug("dm_init() failed: %d\n", ret);
  303. return ret;
  304. }
  305. ret = dm_scan_platdata(pre_reloc_only);
  306. if (ret) {
  307. debug("dm_scan_platdata() failed: %d\n", ret);
  308. return ret;
  309. }
  310. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  311. ret = dm_extended_scan_fdt(gd->fdt_blob, pre_reloc_only);
  312. if (ret) {
  313. debug("dm_extended_scan_dt() failed: %d\n", ret);
  314. return ret;
  315. }
  316. }
  317. ret = dm_scan_other(pre_reloc_only);
  318. if (ret)
  319. return ret;
  320. return 0;
  321. }
  322. #ifdef CONFIG_ACPIGEN
  323. static int root_acpi_get_name(const struct udevice *dev, char *out_name)
  324. {
  325. return acpi_copy_name(out_name, "\\_SB");
  326. }
  327. struct acpi_ops root_acpi_ops = {
  328. .get_name = root_acpi_get_name,
  329. };
  330. #endif
  331. /* This is the root driver - all drivers are children of this */
  332. U_BOOT_DRIVER(root_driver) = {
  333. .name = "root_driver",
  334. .id = UCLASS_ROOT,
  335. ACPI_OPS_PTR(&root_acpi_ops)
  336. };
  337. /* This is the root uclass */
  338. UCLASS_DRIVER(root) = {
  339. .name = "root",
  340. .id = UCLASS_ROOT,
  341. };