platform_ofw.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-06-04 GuEe-GUI the first version
  9. */
  10. #include <rtthread.h>
  11. #define DBG_TAG "drv.platform"
  12. #define DBG_LVL DBG_INFO
  13. #include <rtdbg.h>
  14. #include <drivers/ofw_io.h>
  15. #include <drivers/ofw_fdt.h>
  16. #include <drivers/platform.h>
  17. #include <drivers/core/bus.h>
  18. #include <drivers/core/dm.h>
  19. #include "../ofw/ofw_internal.h"
  20. static const struct rt_ofw_node_id platform_ofw_ids[] =
  21. {
  22. { .compatible = "simple-bus", },
  23. #ifdef RT_USING_MFD
  24. { .compatible = "simple-mfd", },
  25. #endif
  26. #ifdef RT_USING_ISA
  27. { .compatible = "isa", },
  28. #endif
  29. #ifdef RT_USING_AMBA_BUS
  30. /*
  31. * Maybe ARM has replaced it with compatible: "arm,primecell" and will not
  32. * used anymore in the future.
  33. */
  34. { .compatible = "arm,amba-bus", },
  35. #endif
  36. { /* sentinel */ }
  37. };
  38. static void ofw_device_rename(struct rt_device *dev)
  39. {
  40. rt_uint32_t mask;
  41. rt_uint64_t addr;
  42. const char *dev_name = dev->parent.name;
  43. struct rt_ofw_node *np = dev->ofw_node;
  44. #if RT_NAME_MAX > 0
  45. if (dev_name[0] == '\0')
  46. {
  47. dev_name = RT_NULL;
  48. }
  49. #endif
  50. while (np->parent)
  51. {
  52. if (!rt_ofw_get_address(np, 0, &addr, RT_NULL))
  53. {
  54. const char *node_name = rt_fdt_node_name(np->full_name);
  55. rt_size_t tag_len = strchrnul(node_name, '@') - node_name;
  56. if (!rt_ofw_prop_read_u32(np, "mask", &mask))
  57. {
  58. rt_dm_dev_set_name(dev, dev_name ? "%lx.%x.%.*s:%s" : "%lx.%x.%.*s",
  59. addr, __rt_ffs(mask) - 1, tag_len, node_name, dev_name);
  60. }
  61. else
  62. {
  63. rt_dm_dev_set_name(dev, dev_name ? "%lx.%.*s:%s" : "%lx.%.*s",
  64. addr, tag_len, node_name, dev_name);
  65. }
  66. return;
  67. }
  68. rt_dm_dev_set_name(dev, dev_name ? "%s:%s" : "%s",
  69. rt_fdt_node_name(np->full_name), dev_name);
  70. np = np->parent;
  71. }
  72. }
  73. static struct rt_platform_device *alloc_ofw_platform_device(struct rt_ofw_node *np)
  74. {
  75. struct rt_platform_device *pdev = rt_platform_device_alloc("");
  76. if (pdev)
  77. {
  78. /* inc reference of dt-node */
  79. rt_ofw_node_get(np);
  80. rt_ofw_node_set_flag(np, RT_OFW_F_PLATFORM);
  81. pdev->parent.ofw_node = np;
  82. ofw_device_rename(&pdev->parent);
  83. }
  84. else
  85. {
  86. LOG_E("Alloc device fail for %s", rt_ofw_node_full_name(np));
  87. }
  88. return pdev;
  89. }
  90. static rt_err_t platform_ofw_device_probe_once(struct rt_ofw_node *parent_np)
  91. {
  92. rt_err_t err = RT_EOK;
  93. struct rt_ofw_node *np;
  94. struct rt_platform_device *pdev;
  95. rt_ofw_foreach_available_child_node(parent_np, np)
  96. {
  97. const char *name;
  98. struct rt_ofw_node_id *id;
  99. struct rt_ofw_prop *compat_prop = RT_NULL;
  100. if (np->dev)
  101. {
  102. /* Check first */
  103. continue;
  104. }
  105. LOG_D("%s found in %s", np->full_name, parent_np->full_name);
  106. /* Is system node or have driver */
  107. if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) ||
  108. rt_ofw_node_test_flag(np, RT_OFW_F_READLY))
  109. {
  110. continue;
  111. }
  112. compat_prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  113. name = rt_ofw_node_name(np);
  114. /* Not have name and compatible */
  115. if (!compat_prop && (name == (const char *)"<NULL>" || !rt_strcmp(name, "<NULL>")))
  116. {
  117. continue;
  118. }
  119. id = rt_ofw_prop_match(compat_prop, platform_ofw_ids);
  120. if (id && np->child)
  121. {
  122. /* scan next level */
  123. err = platform_ofw_device_probe_once(np);
  124. if (err)
  125. {
  126. rt_ofw_node_put(np);
  127. LOG_E("%s bus probe fail", np->full_name);
  128. break;
  129. }
  130. }
  131. if (np->dev)
  132. {
  133. /* Maybe the childs have requested this node */
  134. continue;
  135. }
  136. pdev = alloc_ofw_platform_device(np);
  137. if (!pdev)
  138. {
  139. rt_ofw_node_put(np);
  140. err = -RT_ENOMEM;
  141. break;
  142. }
  143. pdev->dev_id = ofw_alias_node_id(np);
  144. np->dev = &pdev->parent;
  145. LOG_D("%s register to bus", np->full_name);
  146. rt_platform_device_register(pdev);
  147. }
  148. return err;
  149. }
  150. rt_err_t rt_platform_ofw_device_probe_child(struct rt_ofw_node *np)
  151. {
  152. rt_err_t err;
  153. struct rt_ofw_node *parent = rt_ofw_get_parent(np);
  154. if (parent && rt_strcmp(parent->name, "/") &&
  155. rt_ofw_get_prop(np, "compatible", RT_NULL) &&
  156. !rt_ofw_node_test_flag(np, RT_OFW_F_PLATFORM))
  157. {
  158. struct rt_platform_device *pdev = alloc_ofw_platform_device(np);
  159. if (pdev)
  160. {
  161. err = rt_platform_device_register(pdev);
  162. }
  163. else
  164. {
  165. err = -RT_ENOMEM;
  166. }
  167. }
  168. else
  169. {
  170. err = -RT_EINVAL;
  171. }
  172. rt_ofw_node_put(parent);
  173. return err;
  174. }
  175. rt_err_t rt_platform_ofw_request(struct rt_ofw_node *np)
  176. {
  177. rt_err_t err;
  178. if (np)
  179. {
  180. struct rt_device *dev = np->dev;
  181. if (dev)
  182. {
  183. /* Was create */
  184. if (dev->drv)
  185. {
  186. /* Was probe OK */
  187. err = RT_EOK;
  188. }
  189. else
  190. {
  191. err = rt_bus_reload_driver_device(dev->bus, dev);
  192. }
  193. }
  194. else
  195. {
  196. struct rt_platform_device *pdev = alloc_ofw_platform_device(np);
  197. if (pdev)
  198. {
  199. pdev->dev_id = ofw_alias_node_id(np);
  200. np->dev = &pdev->parent;
  201. LOG_D("%s register to bus", np->full_name);
  202. err = rt_platform_device_register(pdev);
  203. }
  204. else
  205. {
  206. err = -RT_ENOMEM;
  207. }
  208. }
  209. }
  210. else
  211. {
  212. err = -RT_EINVAL;
  213. }
  214. return err;
  215. }
  216. static int platform_ofw_device_probe(void)
  217. {
  218. rt_err_t err = RT_EOK;
  219. struct rt_ofw_node *node;
  220. if (ofw_node_root)
  221. {
  222. rt_ofw_node_get(ofw_node_root);
  223. err = platform_ofw_device_probe_once(ofw_node_root);
  224. rt_ofw_node_put(ofw_node_root);
  225. if ((node = rt_ofw_find_node_by_path("/firmware")))
  226. {
  227. platform_ofw_device_probe_once(node);
  228. rt_ofw_node_put(node);
  229. }
  230. if ((node = rt_ofw_find_node_by_path("/clocks")))
  231. {
  232. platform_ofw_device_probe_once(node);
  233. rt_ofw_node_put(node);
  234. }
  235. rt_ofw_node_get(ofw_node_chosen);
  236. if ((node = rt_ofw_get_child_by_compatible(ofw_node_chosen, "simple-framebuffer")))
  237. {
  238. platform_ofw_device_probe_once(node);
  239. rt_ofw_node_put(node);
  240. }
  241. rt_ofw_node_get(ofw_node_chosen);
  242. }
  243. else
  244. {
  245. err = -RT_ENOSYS;
  246. }
  247. return (int)err;
  248. }
  249. INIT_PLATFORM_EXPORT(platform_ofw_device_probe);
  250. rt_err_t rt_platform_ofw_free(struct rt_platform_device *pdev)
  251. {
  252. rt_err_t err = RT_EOK;
  253. if (pdev)
  254. {
  255. struct rt_ofw_node *np = pdev->parent.ofw_node;
  256. if (np)
  257. {
  258. rt_ofw_node_clear_flag(np, RT_OFW_F_PLATFORM);
  259. rt_ofw_node_put(np);
  260. rt_free(pdev);
  261. }
  262. }
  263. else
  264. {
  265. err = -RT_EINVAL;
  266. }
  267. return err;
  268. }