platform_ofw.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. LOG_D("%s found in %s", np->full_name, parent_np->full_name);
  101. /* Is system node or have driver */
  102. if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) ||
  103. rt_ofw_node_test_flag(np, RT_OFW_F_READLY))
  104. {
  105. continue;
  106. }
  107. compat_prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  108. name = rt_ofw_node_name(np);
  109. /* Not have name and compatible */
  110. if (!compat_prop && (name == (const char *)"<NULL>" || !rt_strcmp(name, "<NULL>")))
  111. {
  112. continue;
  113. }
  114. id = rt_ofw_prop_match(compat_prop, platform_ofw_ids);
  115. if (id && np->child)
  116. {
  117. /* scan next level */
  118. err = platform_ofw_device_probe_once(np);
  119. if (err)
  120. {
  121. rt_ofw_node_put(np);
  122. LOG_E("%s bus probe fail", np->full_name);
  123. break;
  124. }
  125. }
  126. pdev = alloc_ofw_platform_device(np);
  127. if (!pdev)
  128. {
  129. rt_ofw_node_put(np);
  130. err = -RT_ENOMEM;
  131. break;
  132. }
  133. pdev->dev_id = ofw_alias_node_id(np);
  134. np->dev = &pdev->parent;
  135. LOG_D("%s register to bus", np->full_name);
  136. rt_platform_device_register(pdev);
  137. }
  138. return err;
  139. }
  140. rt_err_t rt_platform_ofw_device_probe_child(struct rt_ofw_node *np)
  141. {
  142. rt_err_t err;
  143. struct rt_ofw_node *parent = rt_ofw_get_parent(np);
  144. if (parent && rt_strcmp(parent->name, "/") &&
  145. rt_ofw_get_prop(np, "compatible", RT_NULL) &&
  146. !rt_ofw_node_test_flag(np, RT_OFW_F_PLATFORM))
  147. {
  148. struct rt_platform_device *pdev = alloc_ofw_platform_device(np);
  149. if (pdev)
  150. {
  151. err = rt_platform_device_register(pdev);
  152. }
  153. else
  154. {
  155. err = -RT_ENOMEM;
  156. }
  157. }
  158. else
  159. {
  160. err = -RT_EINVAL;
  161. }
  162. rt_ofw_node_put(parent);
  163. return err;
  164. }
  165. rt_err_t rt_platform_ofw_request(struct rt_ofw_node *np)
  166. {
  167. rt_err_t err;
  168. if (np)
  169. {
  170. struct rt_device *dev = np->dev;
  171. if (dev)
  172. {
  173. /* Was create */
  174. if (dev->drv)
  175. {
  176. /* Was probe OK */
  177. err = RT_EOK;
  178. }
  179. else
  180. {
  181. err = rt_bus_reload_driver_device(dev->bus, dev);
  182. }
  183. }
  184. else
  185. {
  186. struct rt_platform_device *pdev = alloc_ofw_platform_device(np);
  187. if (pdev)
  188. {
  189. pdev->dev_id = ofw_alias_node_id(np);
  190. np->dev = &pdev->parent;
  191. LOG_D("%s register to bus", np->full_name);
  192. err = rt_platform_device_register(pdev);
  193. }
  194. else
  195. {
  196. err = -RT_ENOMEM;
  197. }
  198. }
  199. }
  200. else
  201. {
  202. err = -RT_EINVAL;
  203. }
  204. return err;
  205. }
  206. static int platform_ofw_device_probe(void)
  207. {
  208. rt_err_t err = RT_EOK;
  209. struct rt_ofw_node *node;
  210. if (ofw_node_root)
  211. {
  212. rt_ofw_node_get(ofw_node_root);
  213. err = platform_ofw_device_probe_once(ofw_node_root);
  214. rt_ofw_node_put(ofw_node_root);
  215. if ((node = rt_ofw_find_node_by_path("/firmware")))
  216. {
  217. platform_ofw_device_probe_once(node);
  218. rt_ofw_node_put(node);
  219. }
  220. rt_ofw_node_get(ofw_node_chosen);
  221. if ((node = rt_ofw_get_child_by_compatible(ofw_node_chosen, "simple-framebuffer")))
  222. {
  223. platform_ofw_device_probe_once(node);
  224. rt_ofw_node_put(node);
  225. }
  226. rt_ofw_node_get(ofw_node_chosen);
  227. }
  228. else
  229. {
  230. err = -RT_ENOSYS;
  231. }
  232. return (int)err;
  233. }
  234. INIT_PLATFORM_EXPORT(platform_ofw_device_probe);
  235. rt_err_t rt_platform_ofw_free(struct rt_platform_device *pdev)
  236. {
  237. rt_err_t err = RT_EOK;
  238. if (pdev)
  239. {
  240. struct rt_ofw_node *np = pdev->parent.ofw_node;
  241. if (np)
  242. {
  243. rt_ofw_node_clear_flag(np, RT_OFW_F_PLATFORM);
  244. rt_ofw_node_put(np);
  245. rt_free(pdev);
  246. }
  247. }
  248. else
  249. {
  250. err = -RT_EINVAL;
  251. }
  252. return err;
  253. }