dtb_base.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "dtb_node.h"
  7. #include "libfdt.h"
  8. #include "libfdt_env.h"
  9. #include <ctype.h>
  10. static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
  11. {
  12. if (*base == 0)
  13. {
  14. if (s[0] == '0')
  15. {
  16. if (tolower(s[1]) == 'x' && isxdigit((int)s[2]))
  17. *base = 16;
  18. else
  19. *base = 8;
  20. }
  21. else
  22. *base = 10;
  23. }
  24. if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
  25. s += 2;
  26. return s;
  27. }
  28. unsigned long simple_strtoul(const char *cp, char **endp,
  29. unsigned int base)
  30. {
  31. unsigned long result = 0;
  32. unsigned long value;
  33. cp = _parse_integer_fixup_radix(cp, &base);
  34. while (isxdigit((int)*cp) && (value = isdigit((int)*cp) ? *cp-'0' : (islower((int)*cp)
  35. ? toupper(*cp) : *cp)-'A'+10) < base)
  36. {
  37. result = result*base + value;
  38. cp++;
  39. }
  40. if (endp)
  41. *endp = (char *)cp;
  42. return result;
  43. }
  44. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  45. {
  46. char *tail;
  47. unsigned long val;
  48. size_t len;
  49. *res = 0;
  50. len = strlen(cp);
  51. if (len == 0)
  52. return -EINVAL;
  53. val = simple_strtoul(cp, &tail, base);
  54. if (tail == cp)
  55. return -EINVAL;
  56. if ((*tail == '\0') ||
  57. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n')))
  58. {
  59. *res = val;
  60. return 0;
  61. }
  62. return -EINVAL;
  63. }
  64. long simple_strtol(const char *cp, char **endp, unsigned int base)
  65. {
  66. if (*cp == '-')
  67. return -simple_strtoul(cp + 1, endp, base);
  68. return simple_strtoul(cp, endp, base);
  69. }
  70. rt_bool_t dtb_node_read_bool(const struct dtb_node *node, const char *propname)
  71. {
  72. const void *prop;
  73. RT_ASSERT(dtb_node_valid(node));
  74. debug("%s: %s: ", __func__, propname);
  75. prop = dtb_node_get_property(node, propname, NULL);
  76. debug("%s\n", prop ? "true" : "false");
  77. return prop ? RT_TRUE : RT_FALSE;
  78. }
  79. const void *dtb_node_read_prop(const struct dtb_node *node, const char *propname, int *sizep)
  80. {
  81. const char *val = NULL;
  82. int len;
  83. RT_ASSERT(dtb_node_valid(node));
  84. debug("%s: %s: ", __func__, propname);
  85. struct dtb_property *prop = dtb_node_get_dtb_node_property(node, propname, &len);
  86. if (prop)
  87. {
  88. val = prop->value;
  89. len = prop->size;
  90. }
  91. if (!val)
  92. {
  93. debug("<not found>\n");
  94. if (sizep)
  95. *sizep = -FDT_ERR_NOTFOUND;
  96. return NULL;
  97. }
  98. if (sizep)
  99. *sizep = len;
  100. return val;
  101. }
  102. const char *dtb_node_read_string(const struct dtb_node *node, const char *propname)
  103. {
  104. const char *str;
  105. int len;
  106. str = dtb_node_read_prop(node, propname, &len);
  107. if (!str)
  108. return NULL;
  109. if (strnlen(str, len) >= len)
  110. {
  111. debug("<invalid>\n");
  112. return NULL;
  113. }
  114. debug("%s\n", str);
  115. return str;
  116. }
  117. const struct dtb_node *dtb_node_find_subnode(const struct dtb_node *node, const char *subnode_name)
  118. {
  119. const struct dtb_node *subnode;
  120. RT_ASSERT(dtb_node_valid(node));
  121. debug("%s: %s: ", __func__, subnode_name);
  122. for (node = node->child; node; node = node->sibling)
  123. {
  124. if (!strcmp(subnode_name, node->name))
  125. break;
  126. }
  127. subnode = node;
  128. debug("%s\n", dtb_node_valid(subnode) ?\
  129. dtb_node_get_name(subnode) : "<none>");
  130. return subnode;
  131. }
  132. struct dtb_node *dtb_node_first_subnode(const struct dtb_node *node)
  133. {
  134. RT_ASSERT(dtb_node_valid(node));
  135. return node->child;
  136. }
  137. struct dtb_node *dtb_node_next_subnode(const struct dtb_node *node)
  138. {
  139. RT_ASSERT(dtb_node_valid(node));
  140. return node->sibling;
  141. }
  142. const char *dtb_node_get_name(const struct dtb_node *node)
  143. {
  144. if (!dtb_node_valid(node))
  145. {
  146. debug("%s node not valid\n", __func__);
  147. return NULL;
  148. }
  149. return strrchr(node->path, '/') + 1;
  150. }
  151. struct dtb_node *dtb_node_get_by_phandle(uint32_t phandle)
  152. {
  153. if (dtb_node_active())
  154. return dtb_node_find_node_by_phandle(phandle);
  155. return NULL;
  156. }
  157. int dtb_node_read_size(const struct dtb_node *node, const char *propname)
  158. {
  159. struct dtb_property *prop = dtb_node_get_dtb_node_property( node, propname, NULL);
  160. if (prop)
  161. return prop->size;
  162. return -EINVAL;
  163. }
  164. int dtb_node_get_addr_and_size_by_index(const struct dtb_node *node, int index, size_t *addr, size_t *size)
  165. {
  166. const uint32_t *prop;
  167. int psize;
  168. int onesize, na, ns;
  169. na = dtb_node_n_addr_cells(node);
  170. ns = dtb_node_n_size_cells(node);
  171. prop = dtb_node_get_dtb_node_property_value(node, "reg", &psize);
  172. if (prop == NULL)
  173. {
  174. return -1;
  175. }
  176. psize /= 4;
  177. onesize = na + ns;
  178. if (psize >= (index + 1) * onesize)
  179. {
  180. prop += index * onesize;
  181. if (addr)
  182. {
  183. *addr = dtb_node_read_number(prop, na);
  184. }
  185. if (size)
  186. {
  187. *size = dtb_node_read_number(prop + na, ns);
  188. }
  189. return 0;
  190. }
  191. return -1;
  192. }
  193. size_t dtb_node_get_addr_index(const struct dtb_node *node, int index)
  194. {
  195. int na;
  196. size_t size;
  197. const uint32_t *prop_val;
  198. uint flags;
  199. prop_val = dtb_node_get_address(node, index,
  200. (uint64_t *)&size, &flags);
  201. if (!prop_val)
  202. return -1;
  203. na = dtb_node_n_addr_cells(node);
  204. return dtb_node_read_number(prop_val, na);
  205. }
  206. size_t dtb_node_get_addr(const struct dtb_node *node)
  207. {
  208. return dtb_node_get_addr_index(node, 0);
  209. }
  210. int dtb_node_stringlist_search(const struct dtb_node *node, const char *property,
  211. const char *string)
  212. {
  213. return dtb_node_property_match_string(node, property, string);
  214. }
  215. int dtb_node_read_string_index(const struct dtb_node *node, const char *property, int index,
  216. const char **outp)
  217. {
  218. return dtb_node_property_read_string_index(node, property, index, outp);
  219. }
  220. int dtb_node_read_string_count(const struct dtb_node *node, const char *property)
  221. {
  222. return dtb_node_property_count_strings(node, property);
  223. }
  224. struct dtb_node *dtb_node_path(const char *path)
  225. {
  226. if (dtb_node_active())
  227. return dtb_node_find_node_by_path(path);
  228. return NULL;
  229. }
  230. const char *dtb_node_get_chosen_prop(const char *name)
  231. {
  232. const struct dtb_node *chosen_node;
  233. chosen_node = (const struct dtb_node *)dtb_node_path("/chosen");
  234. return dtb_node_read_string(chosen_node, name);
  235. }
  236. struct dtb_node *dtb_node_get_chosen_node(const char *name)
  237. {
  238. const char *prop;
  239. prop = dtb_node_get_chosen_prop(name);
  240. if (!prop)
  241. return NULL;
  242. return dtb_node_path(prop);
  243. }
  244. const void *dtb_node_get_property(const struct dtb_node *node, const char *propname, int *lenp)
  245. {
  246. return dtb_node_get_dtb_node_property_value(node, propname, lenp);
  247. }
  248. rt_bool_t dtb_node_is_available(const struct dtb_node *node)
  249. {
  250. return dtb_node_device_is_available(node);
  251. }
  252. size_t dtb_node_get_addr_size(const struct dtb_node *node, const char *property,
  253. size_t *sizep)
  254. {
  255. int na, ns;
  256. int psize;
  257. const uint32_t *prop = dtb_node_get_dtb_node_property_value(node, property, &psize);
  258. if (!prop)
  259. return -1;
  260. na = dtb_node_n_addr_cells(node);
  261. ns = dtb_node_n_size_cells(node);
  262. *sizep = dtb_node_read_number(prop + na, ns);
  263. return dtb_node_read_number(prop, na);
  264. }
  265. const uint8_t *dtb_node_read_u8_array_ptr(const struct dtb_node *node, const char *propname,
  266. size_t sz)
  267. {
  268. int psize;
  269. const uint32_t *prop = dtb_node_get_dtb_node_property_value(node, propname, &psize);
  270. if (!prop || sz != psize)
  271. return NULL;
  272. return (uint8_t *)prop;
  273. }
  274. int dtb_node_find_all_compatible_node(const struct dtb_node *from, const char *compatible, struct dtb_node **node_table, int max_num, int *node_num)
  275. {
  276. const struct dtb_node *dn;
  277. int num = 0;
  278. for_each_of_allnodes_from(from, dn)
  279. {
  280. if (dtb_node_get_dtb_node_compatible_match(dn, compatible) &&
  281. dtb_node_get(dn))
  282. {
  283. num++;
  284. *node_table = (struct dtb_node *)dn;
  285. node_table++;
  286. if (num >= max_num)
  287. {
  288. break;
  289. }
  290. }
  291. }
  292. *node_num = num;
  293. dtb_node_put(from);
  294. return 0;
  295. }
  296. int dtb_node_write_prop(const struct dtb_node *node, const char *propname, int len,
  297. const void *value)
  298. {
  299. struct dtb_property *pp;
  300. struct dtb_property *pp_last = NULL;
  301. struct dtb_property *new;
  302. if (!dtb_node_active())
  303. return -ENOSYS;
  304. if (!node)
  305. return -EINVAL;
  306. for (pp = node->properties; pp; pp = pp->next)
  307. {
  308. if (strcmp(pp->name, propname) == 0)
  309. {
  310. /* Property exists -> change value */
  311. pp->value = (void *)value;
  312. pp->size = len;
  313. return 0;
  314. }
  315. pp_last = pp;
  316. }
  317. if (!pp_last)
  318. return -ENOENT;
  319. /* Property does not exist -> append new property */
  320. new = (struct dtb_property *)malloc(sizeof(struct dtb_property));
  321. if (!new)
  322. return -ENOMEM;
  323. new->name = strdup(propname);
  324. if (!new->name)
  325. {
  326. free(new);
  327. return -ENOMEM;
  328. }
  329. new->value = (void *)value;
  330. new->size = len;
  331. new->next = NULL;
  332. pp_last->next = new;
  333. return 0;
  334. }
  335. int dtb_node_write_string(const struct dtb_node *node, const char *propname, const char *value)
  336. {
  337. if (!dtb_node_active())
  338. return -ENOSYS;
  339. RT_ASSERT(dtb_node_valid(node));
  340. debug("%s: %s = %s", __func__, propname, value);
  341. return dtb_node_write_prop(node, propname, strlen(value) + 1, value);
  342. }
  343. int dtb_node_set_enabled(const struct dtb_node *node, rt_bool_t value)
  344. {
  345. if (!dtb_node_active())
  346. return -ENOSYS;
  347. RT_ASSERT(dtb_node_valid(node));
  348. if (value)
  349. return dtb_node_write_string(node, "status", "okay");
  350. else
  351. return dtb_node_write_string(node, "status", "disable");
  352. }
  353. /**
  354. * dtb_node_irq_find_parent - Given a device node, find its interrupt parent node
  355. * @child: pointer to device node
  356. *
  357. * Returns a pointer to the interrupt parent node, or NULL if the interrupt
  358. * parent could not be determined.
  359. */
  360. static struct dtb_node *dtb_node_irq_find_parent(struct dtb_node *child)
  361. {
  362. struct dtb_node *p;
  363. phandle parent;
  364. if (!dtb_node_get(child))
  365. return NULL;
  366. do
  367. {
  368. if (dtb_node_read_u32_array(child, "interrupt-parent", &parent, 1))
  369. {
  370. p = dtb_node_get_parent(child);
  371. }
  372. else
  373. {
  374. p = dtb_node_get_by_phandle(parent);
  375. }
  376. dtb_node_put(child);
  377. child = p;
  378. } while (p && dtb_node_get_property(p, "#interrupt-cells", NULL) == NULL);
  379. return p;
  380. }
  381. int dtb_node_irq_get(struct dtb_node *dev, int index)
  382. {
  383. int rc = 0;
  384. struct fdt_phandle_args out_irq;
  385. struct dtb_node *p;
  386. uint32_t intsize;
  387. int res, i;
  388. p = dtb_node_irq_find_parent(dev);
  389. if (p == NULL)
  390. return -EINVAL;
  391. /* Get size of interrupt specifier */
  392. if (dtb_node_read_u32_array(p, "#interrupt-cells", &intsize, 1))
  393. {
  394. res = -EINVAL;
  395. goto out;
  396. }
  397. debug(" path:%s, parent=%pOF, intsize=%d\n", p->path,p, intsize);
  398. /* Copy intspec into irq structure */
  399. out_irq.np = p;
  400. out_irq.args_count = intsize;
  401. for (i = 0; i < intsize; i++)
  402. {
  403. res = dtb_node_read_u32_index(dev, "interrupts",
  404. (index * 3 + i),
  405. out_irq.args + i);
  406. if (res)
  407. goto out;
  408. }
  409. rc = out_irq.args[1];
  410. out:
  411. dtb_node_put(p);
  412. return rc;
  413. }
  414. /**
  415. * dtb_node_irq_get_byname - Decode a node's IRQ and return it as a Linux IRQ number
  416. * @dev: pointer to device tree node
  417. * @name: IRQ name
  418. *
  419. * Returns Linux IRQ number on success, or 0 on the IRQ mapping failure, or
  420. * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
  421. * of any other failure.
  422. */
  423. int dtb_node_irq_get_byname(struct dtb_node *dev, const char *name)
  424. {
  425. int index;
  426. if (!name)
  427. return -EINVAL;
  428. index = dtb_node_stringlist_search(dev, "interrupt-names", name);
  429. if (index < 0)
  430. return index;
  431. return dtb_node_irq_get(dev, index);
  432. }
  433. /**
  434. * dtb_node_irq_count - Count the number of IRQs a node uses
  435. * @dev: pointer to device tree node
  436. */
  437. int dtb_node_irq_count(struct dtb_node *device)
  438. {
  439. struct dtb_node *p;
  440. uint32_t intsize;
  441. int nr = 0, res = 0;
  442. p = dtb_node_irq_find_parent(device);
  443. if (p == NULL)
  444. return -EINVAL;
  445. /* Get size of interrupt specifier */
  446. if (dtb_node_read_u32_array(p, "#interrupt-cells", &intsize, 1))
  447. {
  448. res = -EINVAL;
  449. goto out;
  450. }
  451. debug(" path:%s, parent=%pOF, intsize=%d\n", p->path,p, intsize);
  452. res = dtb_node_read_size(device, "interrupts");
  453. if (res < 0)
  454. {
  455. goto out;
  456. }
  457. nr = res / (intsize * 4);
  458. out:
  459. dtb_node_put(p);
  460. return nr;
  461. }