io.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-08-25 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <ioremap.h>
  12. #include <drivers/ofw.h>
  13. #include <drivers/ofw_io.h>
  14. #include <drivers/ofw_fdt.h>
  15. #define DBG_TAG "rtdm.ofw"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include "ofw_internal.h"
  19. static int ofw_bus_addr_cells(struct rt_ofw_node *np)
  20. {
  21. int res = OFW_ROOT_NODE_ADDR_CELLS_DEFAULT;
  22. for (rt_uint32_t cells; np; np = np->parent)
  23. {
  24. if (!rt_ofw_prop_read_u32(np, "#address-cells", &cells))
  25. {
  26. res = cells;
  27. break;
  28. }
  29. }
  30. return res;
  31. }
  32. static int ofw_bus_size_cells(struct rt_ofw_node *np)
  33. {
  34. int res = OFW_ROOT_NODE_SIZE_CELLS_DEFAULT;
  35. for (rt_uint32_t cells; np; np = np->parent)
  36. {
  37. if (!rt_ofw_prop_read_u32(np, "#size-cells", &cells))
  38. {
  39. res = cells;
  40. break;
  41. }
  42. }
  43. return res;
  44. }
  45. int rt_ofw_bus_addr_cells(struct rt_ofw_node *np)
  46. {
  47. return np ? ofw_bus_addr_cells(np) : -RT_EINVAL;
  48. }
  49. int rt_ofw_bus_size_cells(struct rt_ofw_node *np)
  50. {
  51. return np ? ofw_bus_size_cells(np) : -RT_EINVAL;
  52. }
  53. int rt_ofw_io_addr_cells(struct rt_ofw_node *np)
  54. {
  55. return np ? ofw_bus_addr_cells(np->parent ? np->parent : np) : -RT_EINVAL;
  56. }
  57. int rt_ofw_io_size_cells(struct rt_ofw_node *np)
  58. {
  59. return np ? ofw_bus_size_cells(np->parent ? np->parent : np) : -RT_EINVAL;
  60. }
  61. int rt_ofw_get_address_count(struct rt_ofw_node *np)
  62. {
  63. int count;
  64. if (np)
  65. {
  66. rt_ssize_t len;
  67. count = 0;
  68. if (rt_ofw_get_prop(np, "reg", &len))
  69. {
  70. count = len / (sizeof(fdt32_t) * (rt_ofw_io_addr_cells(np) + rt_ofw_io_size_cells(np)));
  71. }
  72. }
  73. else
  74. {
  75. count = -RT_EINVAL;
  76. }
  77. return count;
  78. }
  79. static rt_err_t ofw_get_address(struct rt_ofw_node *np, int index, rt_uint64_t *out_address, rt_uint64_t *out_size)
  80. {
  81. rt_ssize_t len;
  82. rt_err_t err = RT_EOK;
  83. int addr_cells = rt_ofw_io_addr_cells(np);
  84. int size_cells = rt_ofw_io_size_cells(np);
  85. int skip_cells = (addr_cells + size_cells) * index;
  86. const fdt32_t *cell = rt_ofw_prop_read_raw(np, "reg", &len);
  87. if (cell && skip_cells < (len / sizeof(*cell)))
  88. {
  89. cell += skip_cells;
  90. *out_address = rt_fdt_next_cell(&cell, addr_cells);
  91. *out_address = rt_ofw_translate_address(np, RT_NULL, *out_address);
  92. *out_size = rt_fdt_read_number(cell, size_cells);
  93. }
  94. else
  95. {
  96. err = -RT_EINVAL;
  97. }
  98. return err;
  99. }
  100. rt_err_t rt_ofw_get_address(struct rt_ofw_node *np, int index, rt_uint64_t *out_address, rt_uint64_t *out_size)
  101. {
  102. rt_err_t err;
  103. if (np && index >= 0 && (out_address || out_size))
  104. {
  105. rt_uint64_t address, size;
  106. err = ofw_get_address(np, index, &address, &size);
  107. if (!err)
  108. {
  109. if (out_address)
  110. {
  111. *out_address = address;
  112. }
  113. if (out_size)
  114. {
  115. *out_size = size;
  116. }
  117. }
  118. }
  119. else
  120. {
  121. err = -RT_EINVAL;
  122. }
  123. return err;
  124. }
  125. static rt_err_t ofw_get_address_by_name(struct rt_ofw_node *np, const char *name,
  126. rt_uint64_t *out_address, rt_uint64_t *out_size)
  127. {
  128. int index = 0;
  129. rt_err_t err = RT_EOK;
  130. const char *reg_name;
  131. struct rt_ofw_prop *prop;
  132. rt_ofw_foreach_prop_string(np, "reg-names", prop, reg_name)
  133. {
  134. if (!rt_strcmp(name, reg_name))
  135. {
  136. err = rt_ofw_get_address(np, index, out_address, out_size);
  137. break;
  138. }
  139. ++index;
  140. }
  141. return err;
  142. }
  143. rt_err_t rt_ofw_get_address_by_name(struct rt_ofw_node *np, const char *name,
  144. rt_uint64_t *out_address, rt_uint64_t *out_size)
  145. {
  146. rt_err_t err;
  147. if (np && name && (out_address || out_size))
  148. {
  149. rt_uint64_t address, size;
  150. err = ofw_get_address_by_name(np, name, &address, &size);
  151. if (!err)
  152. {
  153. if (out_address)
  154. {
  155. *out_address = address;
  156. }
  157. if (out_size)
  158. {
  159. *out_size = size;
  160. }
  161. }
  162. }
  163. else
  164. {
  165. err = -RT_EINVAL;
  166. }
  167. return err;
  168. }
  169. int rt_ofw_get_address_array(struct rt_ofw_node *np, int nr, rt_uint64_t *out_regs)
  170. {
  171. int count;
  172. if (np && nr > 0 && out_regs)
  173. {
  174. rt_ssize_t len;
  175. int max_nr;
  176. int addr_cells = rt_ofw_io_addr_cells(np);
  177. int size_cells = rt_ofw_io_size_cells(np);
  178. const fdt32_t *cell = rt_ofw_prop_read_raw(np, "reg", &len);
  179. max_nr = len / (sizeof(*cell) * (addr_cells + size_cells));
  180. if (nr > max_nr)
  181. {
  182. nr = max_nr;
  183. }
  184. count = nr;
  185. while (nr --> 0)
  186. {
  187. *out_regs = rt_fdt_next_cell(&cell, addr_cells);
  188. *out_regs = rt_ofw_translate_address(np, RT_NULL, *out_regs);
  189. ++out_regs;
  190. *out_regs = rt_fdt_next_cell(&cell, size_cells);
  191. ++out_regs;
  192. }
  193. }
  194. else
  195. {
  196. count = -RT_EINVAL;
  197. }
  198. return count;
  199. }
  200. static struct bus_ranges *ofw_bus_ranges(struct rt_ofw_node *np, struct rt_ofw_prop *prop)
  201. {
  202. const fdt32_t *cell;
  203. struct bus_ranges *ranges = RT_NULL;
  204. int child_address_cells, child_size_cells, parent_address_cells, groups;
  205. rt_uint64_t *child_addr, *parent_addr, *child_size;
  206. /*
  207. * Address Translation Example:
  208. *
  209. * / {
  210. * #address-cells = <1>;
  211. * #size-cells = <1>;
  212. *
  213. * soc {
  214. * compatible = "simple-bus";
  215. * #address-cells = <1>;
  216. * #size-cells = <1>;
  217. * ranges = <0x0 0xe0000000 0x00100000>;
  218. *
  219. * serial@4600 {
  220. * device_type = "serial";
  221. * reg = <0x4600 0x100>;
  222. * clock-frequency = <0>;
  223. * };
  224. * };
  225. * }
  226. *
  227. * The soc node specifies a ranges property of <0x0 0xe0000000 0x00100000>;
  228. * This property value specifies that for a 1024 KB range of address space, a
  229. * child node addressed at physical 0x0 maps to a parent address of physical
  230. * 0xe0000000. With this mapping, the serial device node can be addressed by a
  231. * load or store at address 0xe0004600, an offset of 0x4600 (specified in reg)
  232. * plus the 0xe0000000 mapping specified in ranges:
  233. *
  234. * bus-address = parent-bus-address + (reg-address - child-bus-address)
  235. */
  236. do {
  237. child_address_cells = rt_ofw_bus_addr_cells(np);
  238. child_size_cells = rt_ofw_bus_size_cells(np);
  239. parent_address_cells = rt_ofw_io_addr_cells(np);
  240. if (child_address_cells < 0 || child_size_cells < 0 || parent_address_cells < 0)
  241. {
  242. LOG_D("%s read address/size cells fail: child[%d, %d] parent[%d]",
  243. np->full_name, child_address_cells, child_size_cells, parent_address_cells);
  244. break;
  245. }
  246. groups = prop->length / sizeof(*cell);
  247. groups /= child_address_cells + child_size_cells + parent_address_cells;
  248. ranges = rt_malloc(sizeof(*ranges) + sizeof(rt_uint64_t) * 3 * groups);
  249. if (!ranges)
  250. {
  251. break;
  252. }
  253. ranges->nr = groups;
  254. ranges->child_addr = (void *)ranges + sizeof(*ranges);
  255. ranges->parent_addr = &ranges->child_addr[groups];
  256. ranges->child_size = &ranges->parent_addr[groups];
  257. cell = prop->value;
  258. child_addr = ranges->child_addr;
  259. parent_addr = ranges->parent_addr;
  260. child_size = ranges->child_size;
  261. while (groups --> 0)
  262. {
  263. *child_addr++ = rt_fdt_next_cell(&cell, child_address_cells);
  264. *parent_addr++ = rt_fdt_next_cell(&cell, parent_address_cells);
  265. *child_size++ = rt_fdt_next_cell(&cell, child_size_cells);
  266. }
  267. rt_ofw_data(np) = ranges;
  268. } while (0);
  269. return ranges;
  270. }
  271. rt_uint64_t rt_ofw_translate_address(struct rt_ofw_node *np, const char *range_type, rt_uint64_t address)
  272. {
  273. rt_uint64_t cpu_addr = address;
  274. if (!range_type)
  275. {
  276. range_type = "ranges";
  277. }
  278. rt_ofw_foreach_parent_node(np)
  279. {
  280. rt_ssize_t len;
  281. struct rt_ofw_prop *prop;
  282. struct bus_ranges *ranges;
  283. prop = rt_ofw_get_prop(np, range_type, &len);
  284. if (!prop || !len)
  285. {
  286. continue;
  287. }
  288. ranges = rt_ofw_data(np);
  289. if (!ranges)
  290. {
  291. ranges = ofw_bus_ranges(np, prop);
  292. }
  293. if (ranges)
  294. {
  295. for (int i = 0; i < ranges->nr; ++i)
  296. {
  297. rt_uint64_t child_addr = ranges->child_addr[i];
  298. rt_uint64_t child_size = ranges->child_size[i];
  299. if (address >= child_addr && address < child_addr + child_size)
  300. {
  301. cpu_addr = address + (ranges->parent_addr[i] - child_addr);
  302. break;
  303. }
  304. }
  305. }
  306. else
  307. {
  308. cpu_addr = ~0ULL;
  309. }
  310. rt_ofw_node_put(np);
  311. break;
  312. }
  313. return cpu_addr;
  314. }
  315. void *rt_ofw_iomap(struct rt_ofw_node *np, int index)
  316. {
  317. void *iomem = RT_NULL;
  318. if (np)
  319. {
  320. rt_uint64_t regs[2];
  321. if (!ofw_get_address(np, index, &regs[0], &regs[1]))
  322. {
  323. iomem = rt_ioremap((void *)regs[0], (size_t)regs[1]);
  324. }
  325. }
  326. return iomem;
  327. }
  328. void *rt_ofw_iomap_by_name(struct rt_ofw_node *np, const char *name)
  329. {
  330. void *iomem = RT_NULL;
  331. if (np)
  332. {
  333. rt_uint64_t regs[2];
  334. if (!ofw_get_address_by_name(np, name, &regs[0], &regs[1]))
  335. {
  336. iomem = rt_ioremap((void *)regs[0], (size_t)regs[1]);
  337. }
  338. }
  339. return iomem;
  340. }