platform_ofw.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/dm.h>
  18. #include "../ofw/ofw_internal.h"
  19. static const struct rt_ofw_node_id platform_ofw_ids[] =
  20. {
  21. { .compatible = "simple-bus", },
  22. #ifdef RT_USING_MFD
  23. { .compatible = "simple-mfd", },
  24. #endif
  25. #ifdef RT_USING_ISA
  26. { .compatible = "isa", },
  27. #endif
  28. #ifdef RT_USING_AMBA_BUS
  29. /*
  30. * Maybe ARM has replaced it with compatible: "arm,primecell" and will not
  31. * used anymore in the future.
  32. */
  33. { .compatible = "arm,amba-bus", },
  34. #endif
  35. { /* sentinel */ }
  36. };
  37. static void ofw_device_rename(struct rt_device *dev)
  38. {
  39. rt_uint32_t mask;
  40. rt_uint64_t addr;
  41. const char *dev_name = dev->parent.name;
  42. struct rt_ofw_node *np = dev->ofw_node;
  43. #if RT_NAME_MAX > 0
  44. if (dev_name[0] == '\0')
  45. {
  46. dev_name = RT_NULL;
  47. }
  48. #endif
  49. while (np->parent)
  50. {
  51. if (!rt_ofw_get_address(np, 0, &addr, RT_NULL))
  52. {
  53. const char *node_name = rt_fdt_node_name(np->full_name);
  54. rt_size_t tag_len = strchrnul(node_name, '@') - node_name;
  55. if (!rt_ofw_prop_read_u32(np, "mask", &mask))
  56. {
  57. rt_dm_dev_set_name(dev, dev_name ? "%lx.%x.%.*s:%s" : "%lx.%x.%.*s",
  58. addr, __rt_ffs(mask) - 1, tag_len, node_name, dev_name);
  59. }
  60. else
  61. {
  62. rt_dm_dev_set_name(dev, dev_name ? "%lx.%.*s:%s" : "%lx.%.*s",
  63. addr, tag_len, node_name, dev_name);
  64. }
  65. return;
  66. }
  67. rt_dm_dev_set_name(dev, dev_name ? "%s:%s" : "%s",
  68. rt_fdt_node_name(np->full_name), dev_name);
  69. np = np->parent;
  70. }
  71. }
  72. static struct rt_platform_device *alloc_ofw_platform_device(struct rt_ofw_node *np)
  73. {
  74. struct rt_platform_device *pdev = rt_platform_device_alloc("");
  75. if (pdev)
  76. {
  77. /* inc reference of dt-node */
  78. rt_ofw_node_get(np);
  79. rt_ofw_node_set_flag(np, RT_OFW_F_PLATFORM);
  80. #ifdef RT_USING_OFW
  81. pdev->parent.ofw_node = np;
  82. #endif
  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. LOG_D("%s found in %s", np->full_name, parent_np->full_name);
  102. /* Is system node or have driver */
  103. if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) ||
  104. rt_ofw_node_test_flag(np, RT_OFW_F_READLY))
  105. {
  106. continue;
  107. }
  108. compat_prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  109. name = rt_ofw_node_name(np);
  110. /* Not have name and compatible */
  111. if (!compat_prop && (name == (const char *)"<NULL>" || !rt_strcmp(name, "<NULL>")))
  112. {
  113. continue;
  114. }
  115. id = rt_ofw_prop_match(compat_prop, platform_ofw_ids);
  116. if (id && np->child)
  117. {
  118. /* scan next level */
  119. err = platform_ofw_device_probe_once(np);
  120. if (err)
  121. {
  122. rt_ofw_node_put(np);
  123. LOG_E("%s bus probe fail", np->full_name);
  124. break;
  125. }
  126. }
  127. pdev = alloc_ofw_platform_device(np);
  128. if (!pdev)
  129. {
  130. rt_ofw_node_put(np);
  131. err = -RT_ENOMEM;
  132. break;
  133. }
  134. pdev->dev_id = ofw_alias_node_id(np);
  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. static int platform_ofw_device_probe(void)
  166. {
  167. rt_err_t err = RT_EOK;
  168. struct rt_ofw_node *node;
  169. if (ofw_node_root)
  170. {
  171. err = platform_ofw_device_probe_once(ofw_node_root);
  172. rt_ofw_node_put(ofw_node_root);
  173. if ((node = rt_ofw_find_node_by_path("/firmware")))
  174. {
  175. platform_ofw_device_probe_once(node);
  176. rt_ofw_node_put(node);
  177. }
  178. if ((node = rt_ofw_get_child_by_compatible(ofw_node_chosen, "simple-framebuffer")))
  179. {
  180. platform_ofw_device_probe_once(node);
  181. rt_ofw_node_put(node);
  182. }
  183. }
  184. else
  185. {
  186. err = -RT_ENOSYS;
  187. }
  188. return (int)err;
  189. }
  190. INIT_PLATFORM_EXPORT(platform_ofw_device_probe);