platform_ofw.c 7.5 KB

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