root.c 9.6 KB

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