root.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <malloc.h>
  12. #include <linux/libfdt.h>
  13. #include <dm/device.h>
  14. #include <dm/device-internal.h>
  15. #include <dm/lists.h>
  16. #include <dm/of.h>
  17. #include <dm/of_access.h>
  18. #include <dm/platdata.h>
  19. #include <dm/read.h>
  20. #include <dm/root.h>
  21. #include <dm/uclass.h>
  22. #include <dm/util.h>
  23. #include <linux/list.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. static const struct driver_info root_info = {
  26. .name = "root_driver",
  27. };
  28. struct udevice *dm_root(void)
  29. {
  30. if (!gd->dm_root) {
  31. dm_warn("Virtual root driver does not exist!\n");
  32. return NULL;
  33. }
  34. return gd->dm_root;
  35. }
  36. void dm_fixup_for_gd_move(struct global_data *new_gd)
  37. {
  38. /* The sentinel node has moved, so update things that point to it */
  39. if (gd->dm_root) {
  40. new_gd->uclass_root.next->prev = &new_gd->uclass_root;
  41. new_gd->uclass_root.prev->next = &new_gd->uclass_root;
  42. }
  43. }
  44. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  45. void fix_drivers(void)
  46. {
  47. struct driver *drv =
  48. ll_entry_start(struct driver, driver);
  49. const int n_ents = ll_entry_count(struct driver, driver);
  50. struct driver *entry;
  51. for (entry = drv; entry != drv + n_ents; entry++) {
  52. if (entry->of_match)
  53. entry->of_match = (const struct udevice_id *)
  54. ((u32)entry->of_match + gd->reloc_off);
  55. if (entry->bind)
  56. entry->bind += gd->reloc_off;
  57. if (entry->probe)
  58. entry->probe += gd->reloc_off;
  59. if (entry->remove)
  60. entry->remove += gd->reloc_off;
  61. if (entry->unbind)
  62. entry->unbind += gd->reloc_off;
  63. if (entry->ofdata_to_platdata)
  64. entry->ofdata_to_platdata += gd->reloc_off;
  65. if (entry->child_post_bind)
  66. entry->child_post_bind += gd->reloc_off;
  67. if (entry->child_pre_probe)
  68. entry->child_pre_probe += gd->reloc_off;
  69. if (entry->child_post_remove)
  70. entry->child_post_remove += gd->reloc_off;
  71. /* OPS are fixed in every uclass post_probe function */
  72. if (entry->ops)
  73. entry->ops += gd->reloc_off;
  74. }
  75. }
  76. void fix_uclass(void)
  77. {
  78. struct uclass_driver *uclass =
  79. ll_entry_start(struct uclass_driver, uclass);
  80. const int n_ents = ll_entry_count(struct uclass_driver, uclass);
  81. struct uclass_driver *entry;
  82. for (entry = uclass; entry != uclass + n_ents; entry++) {
  83. if (entry->post_bind)
  84. entry->post_bind += gd->reloc_off;
  85. if (entry->pre_unbind)
  86. entry->pre_unbind += gd->reloc_off;
  87. if (entry->pre_probe)
  88. entry->pre_probe += gd->reloc_off;
  89. if (entry->post_probe)
  90. entry->post_probe += gd->reloc_off;
  91. if (entry->pre_remove)
  92. entry->pre_remove += gd->reloc_off;
  93. if (entry->child_post_bind)
  94. entry->child_post_bind += gd->reloc_off;
  95. if (entry->child_pre_probe)
  96. entry->child_pre_probe += gd->reloc_off;
  97. if (entry->init)
  98. entry->init += gd->reloc_off;
  99. if (entry->destroy)
  100. entry->destroy += gd->reloc_off;
  101. /* FIXME maybe also need to fix these ops */
  102. if (entry->ops)
  103. entry->ops += gd->reloc_off;
  104. }
  105. }
  106. void fix_devices(void)
  107. {
  108. struct driver_info *dev =
  109. ll_entry_start(struct driver_info, driver_info);
  110. const int n_ents = ll_entry_count(struct driver_info, driver_info);
  111. struct driver_info *entry;
  112. for (entry = dev; entry != dev + n_ents; entry++) {
  113. if (entry->platdata)
  114. entry->platdata += gd->reloc_off;
  115. }
  116. }
  117. #endif
  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 defined(CONFIG_NEEDS_MANUAL_RELOC)
  127. fix_drivers();
  128. fix_uclass();
  129. fix_devices();
  130. #endif
  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)
  136. if (of_live)
  137. DM_ROOT_NON_CONST->node = np_to_ofnode(gd->of_root);
  138. else
  139. #endif
  140. DM_ROOT_NON_CONST->node = offset_to_ofnode(0);
  141. #endif
  142. ret = device_probe(DM_ROOT_NON_CONST);
  143. if (ret)
  144. return ret;
  145. return 0;
  146. }
  147. int dm_uninit(void)
  148. {
  149. device_remove(dm_root(), DM_REMOVE_NORMAL);
  150. device_unbind(dm_root());
  151. gd->dm_root = NULL;
  152. return 0;
  153. }
  154. #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
  155. int dm_remove_devices_flags(uint flags)
  156. {
  157. device_remove(dm_root(), flags);
  158. return 0;
  159. }
  160. #endif
  161. int dm_scan_platdata(bool pre_reloc_only)
  162. {
  163. int ret;
  164. ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
  165. if (ret == -ENOENT) {
  166. dm_warn("Some drivers were not found\n");
  167. ret = 0;
  168. }
  169. return ret;
  170. }
  171. #if CONFIG_IS_ENABLED(OF_LIVE)
  172. static int dm_scan_fdt_live(struct udevice *parent,
  173. const struct device_node *node_parent,
  174. bool pre_reloc_only)
  175. {
  176. struct device_node *np;
  177. int ret = 0, err;
  178. for (np = node_parent->child; np; np = np->sibling) {
  179. if (!of_device_is_available(np)) {
  180. pr_debug(" - ignoring disabled device\n");
  181. continue;
  182. }
  183. err = lists_bind_fdt(parent, np_to_ofnode(np), NULL,
  184. pre_reloc_only);
  185. if (err && !ret) {
  186. ret = err;
  187. debug("%s: ret=%d\n", np->name, ret);
  188. }
  189. }
  190. if (ret)
  191. dm_warn("Some drivers failed to bind\n");
  192. return ret;
  193. }
  194. #endif /* CONFIG_IS_ENABLED(OF_LIVE) */
  195. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  196. /**
  197. * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
  198. *
  199. * This scans the subnodes of a device tree node and and creates a driver
  200. * for each one.
  201. *
  202. * @parent: Parent device for the devices that will be created
  203. * @blob: Pointer to device tree blob
  204. * @offset: Offset of node to scan
  205. * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
  206. * flag. If false bind all drivers.
  207. * @return 0 if OK, -ve on error
  208. */
  209. static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
  210. int offset, bool pre_reloc_only)
  211. {
  212. int ret = 0, err;
  213. for (offset = fdt_first_subnode(blob, offset);
  214. offset > 0;
  215. offset = fdt_next_subnode(blob, offset)) {
  216. const char *node_name = fdt_get_name(blob, offset, NULL);
  217. if (!fdtdec_get_is_enabled(blob, offset)) {
  218. pr_debug(" - ignoring disabled device\n");
  219. continue;
  220. }
  221. err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL,
  222. pre_reloc_only);
  223. if (err && !ret) {
  224. ret = err;
  225. debug("%s: ret=%d\n", node_name, ret);
  226. }
  227. }
  228. if (ret)
  229. dm_warn("Some drivers failed to bind\n");
  230. return ret;
  231. }
  232. int dm_scan_fdt_dev(struct udevice *dev)
  233. {
  234. if (!dev_of_valid(dev))
  235. return 0;
  236. #if CONFIG_IS_ENABLED(OF_LIVE)
  237. if (of_live_active())
  238. return dm_scan_fdt_live(dev, dev_np(dev),
  239. gd->flags & GD_FLG_RELOC ? false : true);
  240. else
  241. #endif
  242. return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
  243. gd->flags & GD_FLG_RELOC ? false : true);
  244. }
  245. int dm_scan_fdt(const void *blob, bool pre_reloc_only)
  246. {
  247. #if CONFIG_IS_ENABLED(OF_LIVE)
  248. if (of_live_active())
  249. return dm_scan_fdt_live(gd->dm_root, gd->of_root,
  250. pre_reloc_only);
  251. else
  252. #endif
  253. return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
  254. }
  255. static int dm_scan_fdt_ofnode_path(const void *blob, const char *path,
  256. bool pre_reloc_only)
  257. {
  258. ofnode node;
  259. node = ofnode_path(path);
  260. if (!ofnode_valid(node))
  261. return 0;
  262. #if CONFIG_IS_ENABLED(OF_LIVE)
  263. if (of_live_active())
  264. return dm_scan_fdt_live(gd->dm_root, node.np, pre_reloc_only);
  265. #endif
  266. return dm_scan_fdt_node(gd->dm_root, blob, node.of_offset,
  267. pre_reloc_only);
  268. }
  269. int dm_extended_scan_fdt(const void *blob, bool pre_reloc_only)
  270. {
  271. int ret, i;
  272. const char * const nodes[] = {
  273. "/chosen",
  274. "/clocks",
  275. "/firmware"
  276. };
  277. ret = dm_scan_fdt(blob, pre_reloc_only);
  278. if (ret) {
  279. debug("dm_scan_fdt() failed: %d\n", ret);
  280. return ret;
  281. }
  282. /* Some nodes aren't devices themselves but may contain some */
  283. for (i = 0; i < ARRAY_SIZE(nodes); i++) {
  284. ret = dm_scan_fdt_ofnode_path(blob, nodes[i], pre_reloc_only);
  285. if (ret) {
  286. debug("dm_scan_fdt() scan for %s failed: %d\n",
  287. nodes[i], ret);
  288. return ret;
  289. }
  290. }
  291. return ret;
  292. }
  293. #endif
  294. __weak int dm_scan_other(bool pre_reloc_only)
  295. {
  296. return 0;
  297. }
  298. int dm_init_and_scan(bool pre_reloc_only)
  299. {
  300. int ret;
  301. ret = dm_init(IS_ENABLED(CONFIG_OF_LIVE));
  302. if (ret) {
  303. debug("dm_init() failed: %d\n", ret);
  304. return ret;
  305. }
  306. ret = dm_scan_platdata(pre_reloc_only);
  307. if (ret) {
  308. debug("dm_scan_platdata() failed: %d\n", ret);
  309. return ret;
  310. }
  311. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  312. ret = dm_extended_scan_fdt(gd->fdt_blob, pre_reloc_only);
  313. if (ret) {
  314. debug("dm_extended_scan_dt() failed: %d\n", ret);
  315. return ret;
  316. }
  317. }
  318. ret = dm_scan_other(pre_reloc_only);
  319. if (ret)
  320. return ret;
  321. return 0;
  322. }
  323. /* This is the root driver - all drivers are children of this */
  324. U_BOOT_DRIVER(root_driver) = {
  325. .name = "root_driver",
  326. .id = UCLASS_ROOT,
  327. };
  328. /* This is the root uclass */
  329. UCLASS_DRIVER(root) = {
  330. .name = "root",
  331. .id = UCLASS_ROOT,
  332. };