io.c 11 KB

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