root.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #define LOG_CATEGORY UCLASS_ROOT
  9. #include <common.h>
  10. #include <errno.h>
  11. #include <fdtdec.h>
  12. #include <log.h>
  13. #include <malloc.h>
  14. #include <asm-generic/sections.h>
  15. #include <asm/global_data.h>
  16. #include <linux/libfdt.h>
  17. #include <dm/acpi.h>
  18. #include <dm/device.h>
  19. #include <dm/device-internal.h>
  20. #include <dm/lists.h>
  21. #include <dm/of.h>
  22. #include <dm/of_access.h>
  23. #include <dm/platdata.h>
  24. #include <dm/read.h>
  25. #include <dm/root.h>
  26. #include <dm/uclass.h>
  27. #include <dm/util.h>
  28. #include <linux/list.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static struct driver_info root_info = {
  31. .name = "root_driver",
  32. };
  33. struct udevice *dm_root(void)
  34. {
  35. if (!gd->dm_root) {
  36. dm_warn("Virtual root driver does not exist!\n");
  37. return NULL;
  38. }
  39. return gd->dm_root;
  40. }
  41. void dm_fixup_for_gd_move(struct global_data *new_gd)
  42. {
  43. /* The sentinel node has moved, so update things that point to it */
  44. if (gd->dm_root) {
  45. new_gd->uclass_root->next->prev = new_gd->uclass_root;
  46. new_gd->uclass_root->prev->next = new_gd->uclass_root;
  47. }
  48. }
  49. void fix_drivers(void)
  50. {
  51. struct driver *drv =
  52. ll_entry_start(struct driver, driver);
  53. const int n_ents = ll_entry_count(struct driver, driver);
  54. struct driver *entry;
  55. for (entry = drv; entry != drv + n_ents; entry++) {
  56. if (entry->of_match)
  57. entry->of_match = (const struct udevice_id *)
  58. ((ulong)entry->of_match + gd->reloc_off);
  59. if (entry->bind)
  60. entry->bind += gd->reloc_off;
  61. if (entry->probe)
  62. entry->probe += gd->reloc_off;
  63. if (entry->remove)
  64. entry->remove += gd->reloc_off;
  65. if (entry->unbind)
  66. entry->unbind += gd->reloc_off;
  67. if (entry->of_to_plat)
  68. entry->of_to_plat += gd->reloc_off;
  69. if (entry->child_post_bind)
  70. entry->child_post_bind += gd->reloc_off;
  71. if (entry->child_pre_probe)
  72. entry->child_pre_probe += gd->reloc_off;
  73. if (entry->child_post_remove)
  74. entry->child_post_remove += gd->reloc_off;
  75. /* OPS are fixed in every uclass post_probe function */
  76. if (entry->ops)
  77. entry->ops += gd->reloc_off;
  78. }
  79. }
  80. void fix_uclass(void)
  81. {
  82. struct uclass_driver *uclass =
  83. ll_entry_start(struct uclass_driver, uclass_driver);
  84. const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
  85. struct uclass_driver *entry;
  86. for (entry = uclass; entry != uclass + n_ents; entry++) {
  87. if (entry->post_bind)
  88. entry->post_bind += gd->reloc_off;
  89. if (entry->pre_unbind)
  90. entry->pre_unbind += gd->reloc_off;
  91. if (entry->pre_probe)
  92. entry->pre_probe += gd->reloc_off;
  93. if (entry->post_probe)
  94. entry->post_probe += gd->reloc_off;
  95. if (entry->pre_remove)
  96. entry->pre_remove += gd->reloc_off;
  97. if (entry->child_post_bind)
  98. entry->child_post_bind += gd->reloc_off;
  99. if (entry->child_pre_probe)
  100. entry->child_pre_probe += gd->reloc_off;
  101. if (entry->init)
  102. entry->init += gd->reloc_off;
  103. if (entry->destroy)
  104. entry->destroy += 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. static int dm_setup_inst(void)
  119. {
  120. DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
  121. if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
  122. struct udevice_rt *urt;
  123. void *base;
  124. int n_ents;
  125. uint size;
  126. /* Allocate the udevice_rt table */
  127. n_ents = ll_entry_count(struct udevice, udevice);
  128. urt = calloc(n_ents, sizeof(struct udevice_rt));
  129. if (!urt)
  130. return log_msg_ret("urt", -ENOMEM);
  131. gd_set_dm_udevice_rt(urt);
  132. /* Now allocate space for the priv/plat data, and copy it in */
  133. size = __priv_data_end - __priv_data_start;
  134. base = calloc(1, size);
  135. if (!base)
  136. return log_msg_ret("priv", -ENOMEM);
  137. memcpy(base, __priv_data_start, size);
  138. gd_set_dm_priv_base(base);
  139. }
  140. return 0;
  141. }
  142. int dm_init(bool of_live)
  143. {
  144. int ret;
  145. if (gd->dm_root) {
  146. dm_warn("Virtual root driver already exists!\n");
  147. return -EINVAL;
  148. }
  149. if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
  150. gd->uclass_root = &uclass_head;
  151. } else {
  152. gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST;
  153. INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST);
  154. }
  155. if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
  156. fix_drivers();
  157. fix_uclass();
  158. fix_devices();
  159. }
  160. if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
  161. ret = dm_setup_inst();
  162. if (ret) {
  163. log_debug("dm_setup_inst() failed: %d\n", ret);
  164. return ret;
  165. }
  166. } else {
  167. ret = device_bind_by_name(NULL, false, &root_info,
  168. &DM_ROOT_NON_CONST);
  169. if (ret)
  170. return ret;
  171. if (CONFIG_IS_ENABLED(OF_CONTROL))
  172. dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root());
  173. ret = device_probe(DM_ROOT_NON_CONST);
  174. if (ret)
  175. return ret;
  176. }
  177. return 0;
  178. }
  179. int dm_uninit(void)
  180. {
  181. /* Remove non-vital devices first */
  182. device_remove(dm_root(), DM_REMOVE_NON_VITAL);
  183. device_remove(dm_root(), DM_REMOVE_NORMAL);
  184. device_unbind(dm_root());
  185. gd->dm_root = NULL;
  186. return 0;
  187. }
  188. #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
  189. int dm_remove_devices_flags(uint flags)
  190. {
  191. device_remove(dm_root(), flags);
  192. return 0;
  193. }
  194. #endif
  195. int dm_scan_plat(bool pre_reloc_only)
  196. {
  197. int ret;
  198. if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
  199. struct driver_rt *dyn;
  200. int n_ents;
  201. n_ents = ll_entry_count(struct driver_info, driver_info);
  202. dyn = calloc(n_ents, sizeof(struct driver_rt));
  203. if (!dyn)
  204. return -ENOMEM;
  205. gd_set_dm_driver_rt(dyn);
  206. }
  207. ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
  208. if (ret == -ENOENT) {
  209. dm_warn("Some drivers were not found\n");
  210. ret = 0;
  211. }
  212. return ret;
  213. }
  214. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  215. /**
  216. * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
  217. *
  218. * This scans the subnodes of a device tree node and and creates a driver
  219. * for each one.
  220. *
  221. * @parent: Parent device for the devices that will be created
  222. * @node: Node to scan
  223. * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
  224. * flag. If false bind all drivers.
  225. * @return 0 if OK, -ve on error
  226. */
  227. static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
  228. bool pre_reloc_only)
  229. {
  230. int ret = 0, err = 0;
  231. ofnode node;
  232. if (!ofnode_valid(parent_node))
  233. return 0;
  234. for (node = ofnode_first_subnode(parent_node);
  235. ofnode_valid(node);
  236. node = ofnode_next_subnode(node)) {
  237. const char *node_name = ofnode_get_name(node);
  238. if (!ofnode_is_enabled(node)) {
  239. pr_debug(" - ignoring disabled device\n");
  240. continue;
  241. }
  242. err = lists_bind_fdt(parent, node, NULL, pre_reloc_only);
  243. if (err && !ret) {
  244. ret = err;
  245. debug("%s: ret=%d\n", node_name, ret);
  246. }
  247. }
  248. if (ret)
  249. dm_warn("Some drivers failed to bind\n");
  250. return ret;
  251. }
  252. int dm_scan_fdt_dev(struct udevice *dev)
  253. {
  254. return dm_scan_fdt_node(dev, dev_ofnode(dev),
  255. gd->flags & GD_FLG_RELOC ? false : true);
  256. }
  257. int dm_scan_fdt(bool pre_reloc_only)
  258. {
  259. return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
  260. }
  261. static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
  262. {
  263. ofnode node;
  264. node = ofnode_path(path);
  265. return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
  266. }
  267. int dm_extended_scan(bool pre_reloc_only)
  268. {
  269. int ret, i;
  270. const char * const nodes[] = {
  271. "/chosen",
  272. "/clocks",
  273. "/firmware"
  274. };
  275. ret = dm_scan_fdt(pre_reloc_only);
  276. if (ret) {
  277. debug("dm_scan_fdt() failed: %d\n", ret);
  278. return ret;
  279. }
  280. /* Some nodes aren't devices themselves but may contain some */
  281. for (i = 0; i < ARRAY_SIZE(nodes); i++) {
  282. ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
  283. if (ret) {
  284. debug("dm_scan_fdt() scan for %s failed: %d\n",
  285. nodes[i], ret);
  286. return ret;
  287. }
  288. }
  289. return ret;
  290. }
  291. #endif
  292. __weak int dm_scan_other(bool pre_reloc_only)
  293. {
  294. return 0;
  295. }
  296. #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
  297. void *dm_priv_to_rw(void *priv)
  298. {
  299. long offset = priv - (void *)__priv_data_start;
  300. return gd_dm_priv_base() + offset;
  301. }
  302. #endif
  303. /**
  304. * dm_scan() - Scan tables to bind devices
  305. *
  306. * Runs through the driver_info tables and binds the devices it finds. Then runs
  307. * through the devicetree nodes. Finally calls dm_scan_other() to add any
  308. * special devices
  309. *
  310. * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
  311. * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
  312. */
  313. static int dm_scan(bool pre_reloc_only)
  314. {
  315. int ret;
  316. ret = dm_scan_plat(pre_reloc_only);
  317. if (ret) {
  318. debug("dm_scan_plat() failed: %d\n", ret);
  319. return ret;
  320. }
  321. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  322. ret = dm_extended_scan(pre_reloc_only);
  323. if (ret) {
  324. debug("dm_extended_scan() failed: %d\n", ret);
  325. return ret;
  326. }
  327. }
  328. ret = dm_scan_other(pre_reloc_only);
  329. if (ret)
  330. return ret;
  331. return 0;
  332. }
  333. int dm_init_and_scan(bool pre_reloc_only)
  334. {
  335. int ret;
  336. ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
  337. if (ret) {
  338. debug("dm_init() failed: %d\n", ret);
  339. return ret;
  340. }
  341. if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
  342. ret = dm_scan(pre_reloc_only);
  343. if (ret) {
  344. log_debug("dm_scan() failed: %d\n", ret);
  345. return ret;
  346. }
  347. }
  348. return 0;
  349. }
  350. #ifdef CONFIG_ACPIGEN
  351. static int root_acpi_get_name(const struct udevice *dev, char *out_name)
  352. {
  353. return acpi_copy_name(out_name, "\\_SB");
  354. }
  355. struct acpi_ops root_acpi_ops = {
  356. .get_name = root_acpi_get_name,
  357. };
  358. #endif
  359. /* This is the root driver - all drivers are children of this */
  360. U_BOOT_DRIVER(root_driver) = {
  361. .name = "root_driver",
  362. .id = UCLASS_ROOT,
  363. ACPI_OPS_PTR(&root_acpi_ops)
  364. };
  365. /* This is the root uclass */
  366. UCLASS_DRIVER(root) = {
  367. .name = "root",
  368. .id = UCLASS_ROOT,
  369. };