root.c 8.8 KB

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