base.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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. * 2022-08-25 GuEe-GUI first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <drivers/ofw.h>
  13. #include <drivers/ofw_io.h>
  14. #include <drivers/ofw_fdt.h>
  15. #include <drivers/ofw_raw.h>
  16. #define DBG_TAG "rtdm.ofw"
  17. #define DBG_LVL DBG_INFO
  18. #include <rtdbg.h>
  19. #include "ofw_internal.h"
  20. struct rt_ofw_node *ofw_node_root = RT_NULL;
  21. struct rt_ofw_node *ofw_node_cpus = RT_NULL;
  22. struct rt_ofw_node *ofw_node_chosen = RT_NULL;
  23. struct rt_ofw_node *ofw_node_aliases = RT_NULL;
  24. struct rt_ofw_node *ofw_node_reserved_memory = RT_NULL;
  25. static rt_phandle _phandle_range[2] = { 1, 1 }, _phandle_next = 1;
  26. static struct rt_ofw_node **_phandle_hash = RT_NULL;
  27. static rt_list_t _aliases_nodes = RT_LIST_OBJECT_INIT(_aliases_nodes);
  28. rt_err_t ofw_phandle_hash_reset(rt_phandle min, rt_phandle max)
  29. {
  30. rt_err_t err = RT_EOK;
  31. rt_phandle next = max;
  32. struct rt_ofw_node **hash_ptr = RT_NULL;
  33. max = RT_ALIGN(max, OFW_NODE_MIN_HASH);
  34. if (max > _phandle_range[1])
  35. {
  36. rt_size_t size = sizeof(*_phandle_hash) * (max - min);
  37. if (!_phandle_hash)
  38. {
  39. hash_ptr = rt_calloc(1, size);
  40. }
  41. else
  42. {
  43. hash_ptr = rt_realloc(_phandle_hash, size);
  44. if (hash_ptr)
  45. {
  46. rt_size_t old_max = _phandle_range[1];
  47. rt_memset(&hash_ptr[old_max], 0, sizeof(_phandle_hash) * (max - old_max));
  48. }
  49. }
  50. }
  51. if (hash_ptr)
  52. {
  53. /* We always reset min value only once */
  54. if (min)
  55. {
  56. _phandle_range[0] = min;
  57. }
  58. _phandle_range[1] = max;
  59. _phandle_next = next + 1;
  60. _phandle_hash = hash_ptr;
  61. }
  62. else
  63. {
  64. err = -RT_ENOMEM;
  65. }
  66. return err;
  67. }
  68. static rt_phandle ofw_phandle_next(void)
  69. {
  70. rt_phandle next;
  71. static struct rt_spinlock op_lock = {};
  72. rt_hw_spin_lock(&op_lock.lock);
  73. RT_ASSERT(_phandle_next != OFW_PHANDLE_MAX);
  74. if (_phandle_next <= _phandle_range[1])
  75. {
  76. next = _phandle_next++;
  77. }
  78. else
  79. {
  80. rt_err_t err = ofw_phandle_hash_reset(_phandle_range[0], _phandle_next);
  81. if (!err)
  82. {
  83. next = _phandle_next++;
  84. }
  85. else
  86. {
  87. next = 0;
  88. LOG_E("Expanded phandle hash[%u, %u] fail error = %s",
  89. _phandle_range[0], _phandle_next + 1, rt_strerror(err));
  90. }
  91. }
  92. rt_hw_spin_unlock(&op_lock.lock);
  93. return next;
  94. }
  95. static void ofw_prop_destroy(struct rt_ofw_prop *prop)
  96. {
  97. struct rt_ofw_prop *next;
  98. while (prop)
  99. {
  100. next = prop->next;
  101. rt_free(prop);
  102. prop = next;
  103. }
  104. }
  105. static struct rt_ofw_node *ofw_get_next_node(struct rt_ofw_node *prev)
  106. {
  107. struct rt_ofw_node *np;
  108. /*
  109. * Walk:
  110. *
  111. * / { ------------------------ [0] (START) has child, goto child.
  112. *
  113. * node0 { ---------------- [1] has child, goto child.
  114. *
  115. * node0_0 { ---------- [2] no child, has sibling, goto sibling.
  116. * };
  117. *
  118. * node0_1 { ---------- [3] no sibling now.
  119. * upward while the parent has sibling.
  120. * };
  121. * };
  122. *
  123. * node1 { ---------------- [4] come from node0 who find the sibling:
  124. * node1, node1 has child, goto child.
  125. *
  126. * node1_0 { ---------- [5] has child, goto child.
  127. *
  128. * node1_0_0 { ---- [6] no sibling now.
  129. * upward while the parent has sibling.
  130. * (END) in the root.
  131. * };
  132. * };
  133. * };
  134. * };
  135. */
  136. if (!prev)
  137. {
  138. np = ofw_node_root;
  139. }
  140. else if (prev->child)
  141. {
  142. np = prev->child;
  143. }
  144. else
  145. {
  146. np = prev;
  147. while (np->parent && !np->sibling)
  148. {
  149. np = np->parent;
  150. }
  151. np = np->sibling;
  152. }
  153. return np;
  154. }
  155. static void ofw_node_destroy(struct rt_ofw_node *np)
  156. {
  157. struct rt_ofw_node *prev;
  158. if (np->parent)
  159. {
  160. /* Ask parent and prev sibling we are destroy. */
  161. prev = np->parent->child;
  162. if (prev == np)
  163. {
  164. np->parent->child = RT_NULL;
  165. }
  166. else
  167. {
  168. while (prev->sibling != np)
  169. {
  170. prev = prev->sibling;
  171. }
  172. prev->sibling = np->sibling;
  173. }
  174. }
  175. while (np)
  176. {
  177. if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) == RT_FALSE)
  178. {
  179. LOG_E("%s is system node", np->full_name);
  180. RT_ASSERT(0);
  181. }
  182. prev = np;
  183. np = ofw_get_next_node(np);
  184. ofw_prop_destroy(prev->props);
  185. rt_free(prev);
  186. }
  187. }
  188. rt_err_t rt_ofw_node_destroy(struct rt_ofw_node *np)
  189. {
  190. rt_err_t err = RT_EOK;
  191. if (np)
  192. {
  193. if (rt_ref_read(&np->ref) <= 1)
  194. {
  195. ofw_node_destroy(np);
  196. }
  197. else
  198. {
  199. err = -RT_EBUSY;
  200. }
  201. }
  202. else
  203. {
  204. err = -RT_EINVAL;
  205. }
  206. return err;
  207. }
  208. struct rt_ofw_node *rt_ofw_node_get(struct rt_ofw_node *np)
  209. {
  210. if (np)
  211. {
  212. LOG_D("%s get ref = %d", np->full_name, rt_ref_read(&np->ref));
  213. rt_ref_get(&np->ref);
  214. }
  215. return np;
  216. }
  217. static void ofw_node_release(struct rt_ref *r)
  218. {
  219. struct rt_ofw_node *np = rt_container_of(r, struct rt_ofw_node, ref);
  220. LOG_E("%s is release", np->full_name);
  221. (void)np;
  222. RT_ASSERT(0);
  223. }
  224. void rt_ofw_node_put(struct rt_ofw_node *np)
  225. {
  226. if (np)
  227. {
  228. LOG_D("%s put ref = %d", np->full_name, rt_ref_read(&np->ref));
  229. rt_ref_put(&np->ref, &ofw_node_release);
  230. }
  231. }
  232. rt_bool_t rt_ofw_node_tag_equ(const struct rt_ofw_node *np, const char *tag)
  233. {
  234. rt_bool_t ret = RT_FALSE;
  235. if (np && tag)
  236. {
  237. const char *node_name = rt_fdt_node_name(np->full_name);
  238. rt_size_t tag_len = strchrnul(node_name, '@') - node_name;
  239. ret = (rt_strlen(tag) == tag_len && !rt_strncmp(node_name, tag, tag_len));
  240. }
  241. return ret;
  242. }
  243. rt_bool_t rt_ofw_node_tag_prefix(const struct rt_ofw_node *np, const char *prefix)
  244. {
  245. rt_bool_t ret = RT_FALSE;
  246. if (np && prefix)
  247. {
  248. ret = !rt_strncmp(rt_fdt_node_name(np->full_name), prefix, rt_strlen(prefix));
  249. }
  250. return ret;
  251. }
  252. static int ofw_prop_index_of_string(struct rt_ofw_prop *prop, const char *string,
  253. rt_int32_t (*cmp)(const char *cs, const char *ct))
  254. {
  255. int index = -1;
  256. rt_size_t len = prop->length, slen = 0;
  257. const char *value = prop->value;
  258. for (int idx = 0; len > 0; ++idx)
  259. {
  260. /* Add '\0' */
  261. slen = rt_strlen(value) + 1;
  262. if (!cmp(value, string))
  263. {
  264. index = idx;
  265. break;
  266. }
  267. len -= slen;
  268. value += slen;
  269. }
  270. return index;
  271. }
  272. static rt_int32_t ofw_strcasecmp(const char *cs, const char *ct)
  273. {
  274. extern rt_int32_t strcasecmp(const char *cs, const char *ct);
  275. return rt_strcasecmp(cs, ct);
  276. }
  277. static int ofw_prop_index_of_compatible(struct rt_ofw_prop *prop, const char *compatible)
  278. {
  279. return ofw_prop_index_of_string(prop, compatible, ofw_strcasecmp);
  280. }
  281. static int ofw_node_index_of_compatible(const struct rt_ofw_node *np, const char *compatible)
  282. {
  283. int idx = -1;
  284. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  285. if (prop)
  286. {
  287. idx = ofw_prop_index_of_compatible(prop, compatible);
  288. }
  289. return idx;
  290. }
  291. rt_bool_t rt_ofw_machine_is_compatible(const char *compatible)
  292. {
  293. return ofw_node_index_of_compatible(ofw_node_root, compatible) >= 0;
  294. }
  295. /*
  296. * Property status:
  297. *
  298. * "okay" or "ok":
  299. * Indicates the device is operational.
  300. *
  301. * "disabled":
  302. * Indicates that the device is not presently operational, but it might
  303. * become operational in the future (for example, something is not
  304. * plugged in, or switched off).
  305. * Refer to the device binding for details on what disabled means for a
  306. * given device.
  307. *
  308. * "reserved":
  309. * Indicates that the device is operational, but should not be used.
  310. * Typically this is used for devices that are controlled by another
  311. * software component, such as platform firmware.
  312. *
  313. * "fail":
  314. * Indicates that the device is not operational. A serious error was
  315. * detected in the device, and it is unlikely to become operational
  316. * without repair.
  317. *
  318. * "fail-sss":
  319. * Indicates that the device is not operational. A serious error was
  320. * detected in the device and it is unlikely to become operational
  321. * without repair. The sss portion of the value is specific to the
  322. * device and indicates the error condition detected.
  323. */
  324. static rt_bool_t ofw_node_is_fail(const struct rt_ofw_node *np)
  325. {
  326. rt_bool_t res = RT_FALSE;
  327. const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
  328. if (status)
  329. {
  330. res = !rt_strcmp(status, "fail") || !rt_strncmp(status, "fail-", 5);
  331. }
  332. return res;
  333. }
  334. static rt_bool_t ofw_node_is_available(const struct rt_ofw_node *np)
  335. {
  336. rt_bool_t res = RT_TRUE;
  337. const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
  338. if (status)
  339. {
  340. res = !rt_strcmp(status, "okay") || !rt_strcmp(status, "ok");
  341. }
  342. return res;
  343. }
  344. rt_bool_t rt_ofw_node_is_available(const struct rt_ofw_node *np)
  345. {
  346. return np ? ofw_node_is_available(np) : RT_FALSE;
  347. }
  348. rt_bool_t rt_ofw_node_is_compatible(const struct rt_ofw_node *np, const char *compatible)
  349. {
  350. rt_bool_t res = RT_FALSE;
  351. if (np)
  352. {
  353. res = ofw_node_index_of_compatible(np, compatible) >= 0;
  354. }
  355. return res;
  356. }
  357. static struct rt_ofw_node_id *ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
  358. {
  359. int best_index = RT_UINT32_MAX >> 1, index;
  360. struct rt_ofw_node_id *found_id = RT_NULL, *id;
  361. for (id = (struct rt_ofw_node_id *)ids; id->compatible[0]; ++id)
  362. {
  363. index = ofw_prop_index_of_compatible(prop, id->compatible);
  364. if (index >= 0 && index < best_index)
  365. {
  366. found_id = id;
  367. best_index = index;
  368. }
  369. }
  370. return found_id;
  371. }
  372. struct rt_ofw_node_id *rt_ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
  373. {
  374. struct rt_ofw_node_id *id = RT_NULL;
  375. if (prop && ids && !rt_strcmp(prop->name, "compatible"))
  376. {
  377. id = ofw_prop_match(prop, ids);
  378. }
  379. return id;
  380. }
  381. struct rt_ofw_node_id *rt_ofw_node_match(struct rt_ofw_node *np, const struct rt_ofw_node_id *ids)
  382. {
  383. struct rt_ofw_prop *prop;
  384. struct rt_ofw_node_id *id = RT_NULL;
  385. if (np && ids && (prop = rt_ofw_get_prop(np, "compatible", RT_NULL)))
  386. {
  387. id = ofw_prop_match(prop, ids);
  388. }
  389. return id;
  390. }
  391. struct rt_ofw_node *rt_ofw_find_node_by_tag(struct rt_ofw_node *from, const char *tag)
  392. {
  393. struct rt_ofw_node *np = RT_NULL;
  394. if (tag)
  395. {
  396. rt_ofw_foreach_nodes(from, np)
  397. {
  398. if (rt_ofw_node_tag_equ(np, tag))
  399. {
  400. break;
  401. }
  402. }
  403. }
  404. return np;
  405. }
  406. struct rt_ofw_node *rt_ofw_find_node_by_prop_r(struct rt_ofw_node *from, const char *propname,
  407. const struct rt_ofw_prop **out_prop)
  408. {
  409. struct rt_ofw_node *np = RT_NULL;
  410. if (propname)
  411. {
  412. rt_ofw_foreach_nodes(from, np)
  413. {
  414. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
  415. if (prop)
  416. {
  417. if (out_prop)
  418. {
  419. *out_prop = prop;
  420. }
  421. break;
  422. }
  423. }
  424. }
  425. return np;
  426. }
  427. struct rt_ofw_node *rt_ofw_find_node_by_name(struct rt_ofw_node *from, const char *name)
  428. {
  429. struct rt_ofw_node *np = RT_NULL;
  430. if (name)
  431. {
  432. rt_ofw_foreach_nodes(from, np)
  433. {
  434. if (np->name && !rt_strcmp(np->name, name))
  435. {
  436. np = rt_ofw_node_get(np);
  437. break;
  438. }
  439. }
  440. }
  441. return np;
  442. }
  443. struct rt_ofw_node *rt_ofw_find_node_by_type(struct rt_ofw_node *from, const char *type)
  444. {
  445. struct rt_ofw_node *np = RT_NULL;
  446. if (type)
  447. {
  448. rt_ofw_foreach_nodes(from, np)
  449. {
  450. if (rt_ofw_node_is_type(np, type))
  451. {
  452. break;
  453. }
  454. }
  455. }
  456. return np;
  457. }
  458. struct rt_ofw_node *rt_ofw_find_node_by_compatible(struct rt_ofw_node *from, const char *compatible)
  459. {
  460. struct rt_ofw_node *np = RT_NULL;
  461. if (compatible)
  462. {
  463. rt_ofw_foreach_nodes(from, np)
  464. {
  465. if (ofw_node_index_of_compatible(np, compatible) >= 0)
  466. {
  467. break;
  468. }
  469. }
  470. }
  471. return np;
  472. }
  473. struct rt_ofw_node *rt_ofw_find_node_by_ids_r(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids,
  474. const struct rt_ofw_node_id **out_id)
  475. {
  476. struct rt_ofw_node *np = RT_NULL;
  477. if (ids)
  478. {
  479. rt_ofw_foreach_nodes(from, np)
  480. {
  481. struct rt_ofw_node_id *id = rt_ofw_node_match(np, ids);
  482. if (id)
  483. {
  484. if (out_id)
  485. {
  486. *out_id = id;
  487. }
  488. break;
  489. }
  490. }
  491. }
  492. return np;
  493. }
  494. struct rt_ofw_node *rt_ofw_find_node_by_path(const char *path)
  495. {
  496. struct rt_ofw_node *np = RT_NULL, *parent, *tmp = RT_NULL;
  497. if (path)
  498. {
  499. if (!rt_strcmp(path, "/"))
  500. {
  501. np = ofw_node_root;
  502. }
  503. else
  504. {
  505. ++path;
  506. parent = rt_ofw_node_get(ofw_node_root);
  507. while (*path)
  508. {
  509. const char *next = strchrnul(path, '/');
  510. rt_size_t len = next - path;
  511. tmp = RT_NULL;
  512. rt_ofw_foreach_child_node(parent, np)
  513. {
  514. if (!rt_strncmp(np->full_name, path, len))
  515. {
  516. rt_ofw_node_put(parent);
  517. parent = np;
  518. tmp = np;
  519. break;
  520. }
  521. }
  522. if (!tmp)
  523. {
  524. rt_ofw_node_put(parent);
  525. break;
  526. }
  527. path += len + !!*next;
  528. }
  529. np = tmp;
  530. }
  531. rt_ofw_node_get(np);
  532. }
  533. return np;
  534. }
  535. struct rt_ofw_node *rt_ofw_find_node_by_phandle(rt_phandle phandle)
  536. {
  537. struct rt_ofw_node *np = RT_NULL;
  538. if (phandle >= OFW_PHANDLE_MIN && phandle <= OFW_PHANDLE_MAX)
  539. {
  540. /* rebase from zero */
  541. rt_phandle poff = phandle - _phandle_range[0];
  542. np = _phandle_hash[poff];
  543. if (!np)
  544. {
  545. rt_ofw_foreach_allnodes(np)
  546. {
  547. if (np->phandle == phandle)
  548. {
  549. _phandle_hash[poff] = np;
  550. break;
  551. }
  552. }
  553. }
  554. else
  555. {
  556. rt_ofw_node_get(np);
  557. }
  558. }
  559. return np;
  560. }
  561. struct rt_ofw_node *rt_ofw_get_parent(const struct rt_ofw_node *np)
  562. {
  563. if (np)
  564. {
  565. np = rt_ofw_node_get(np->parent);
  566. }
  567. return (struct rt_ofw_node *)np;
  568. }
  569. struct rt_ofw_node *rt_ofw_get_child_by_tag(const struct rt_ofw_node *parent, const char *tag)
  570. {
  571. struct rt_ofw_node *child = RT_NULL;
  572. if (parent && tag)
  573. {
  574. rt_ofw_foreach_child_node(parent, child)
  575. {
  576. if (rt_ofw_node_tag_equ(child, tag))
  577. {
  578. break;
  579. }
  580. }
  581. }
  582. return child;
  583. }
  584. struct rt_ofw_node *rt_ofw_get_child_by_compatible(const struct rt_ofw_node *parent, const char *compatible)
  585. {
  586. struct rt_ofw_node *child = RT_NULL;
  587. if (parent && compatible)
  588. {
  589. rt_ofw_foreach_child_node(parent, child)
  590. {
  591. if (ofw_node_index_of_compatible(child, compatible) >= 0)
  592. {
  593. break;
  594. }
  595. }
  596. }
  597. return child;
  598. }
  599. int rt_ofw_get_child_count(const struct rt_ofw_node *np)
  600. {
  601. int nr;
  602. if (np)
  603. {
  604. struct rt_ofw_node *child;
  605. nr = 0;
  606. rt_ofw_foreach_child_node(np, child)
  607. {
  608. ++nr;
  609. }
  610. }
  611. else
  612. {
  613. nr = -RT_EINVAL;
  614. }
  615. return nr;
  616. }
  617. int rt_ofw_get_available_child_count(const struct rt_ofw_node *np)
  618. {
  619. int nr;
  620. if (np)
  621. {
  622. struct rt_ofw_node *child;
  623. nr = 0;
  624. rt_ofw_foreach_available_child_node(np, child)
  625. {
  626. ++nr;
  627. }
  628. }
  629. else
  630. {
  631. nr = -RT_EINVAL;
  632. }
  633. return nr;
  634. }
  635. struct rt_ofw_node *rt_ofw_get_next_node(struct rt_ofw_node *prev)
  636. {
  637. struct rt_ofw_node *np;
  638. np = rt_ofw_node_get(ofw_get_next_node(prev));
  639. rt_ofw_node_put(prev);
  640. return np;
  641. }
  642. struct rt_ofw_node *rt_ofw_get_next_parent(struct rt_ofw_node *prev)
  643. {
  644. struct rt_ofw_node *next = RT_NULL;
  645. if (prev)
  646. {
  647. next = rt_ofw_node_get(prev->parent);
  648. rt_ofw_node_put(prev);
  649. }
  650. return next;
  651. }
  652. struct rt_ofw_node *rt_ofw_get_next_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
  653. {
  654. struct rt_ofw_node *next = RT_NULL;
  655. if (parent)
  656. {
  657. next = prev ? prev->sibling : parent->child;
  658. rt_ofw_node_put(prev);
  659. rt_ofw_node_get(next);
  660. }
  661. return next;
  662. }
  663. struct rt_ofw_node *rt_ofw_get_next_available_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
  664. {
  665. struct rt_ofw_node *next = RT_NULL;
  666. if (parent)
  667. {
  668. next = prev;
  669. do {
  670. next = rt_ofw_get_next_child(parent, next);
  671. } while (next && !ofw_node_is_available(next));
  672. }
  673. return next;
  674. }
  675. struct rt_ofw_node *rt_ofw_get_cpu_node(int cpu, int *thread, rt_bool_t (*match_cpu_hwid)(int cpu, rt_uint64_t hwid))
  676. {
  677. const char *propname = "reg";
  678. struct rt_ofw_node *cpu_np = RT_NULL;
  679. /*
  680. * "reg" (some of the obsolete arch may be other names):
  681. * The value of reg is a <prop-encoded-array> that defines a unique
  682. * CPU/thread id for the CPU/threads represented by the CPU node.
  683. *
  684. * If a CPU supports more than one thread (i.e. multiple streams of
  685. * execution) the reg property is an array with 1 element per thread. The
  686. * #address-cells on the /cpus node specifies how many cells each element
  687. * of the array takes. Software can determine the number of threads by
  688. * dividing the size of reg by the parent node’s #address-cells:
  689. *
  690. * thread-number = reg-cells / address-cells
  691. *
  692. * If a CPU/thread can be the target of an external interrupt the reg
  693. * property value must be a unique CPU/thread id that is addressable by the
  694. * interrupt controller.
  695. *
  696. * If a CPU/thread cannot be the target of an external interrupt, then reg
  697. * must be unique and out of bounds of the range addressed by the interrupt
  698. * controller
  699. *
  700. * If a CPU/thread’s PIR (pending interrupt register) is modifiable, a
  701. * client program should modify PIR to match the reg property value. If PIR
  702. * cannot be modified and the PIR value is distinct from the interrupt
  703. * controller number space, the CPUs binding may define a binding-specific
  704. * representation of PIR values if desired.
  705. */
  706. rt_ofw_foreach_cpu_node(cpu_np)
  707. {
  708. rt_ssize_t prop_len;
  709. rt_bool_t is_end = RT_FALSE;
  710. int tid, addr_cells = rt_ofw_io_addr_cells(cpu_np);
  711. const fdt32_t *cell = rt_ofw_prop_read_raw(cpu_np, propname, &prop_len);
  712. if (!cell && !addr_cells)
  713. {
  714. if (match_cpu_hwid && match_cpu_hwid(cpu, 0))
  715. {
  716. break;
  717. }
  718. continue;
  719. }
  720. if (!match_cpu_hwid)
  721. {
  722. continue;
  723. }
  724. prop_len /= sizeof(*cell) * addr_cells;
  725. for (tid = 0; tid < prop_len; ++tid)
  726. {
  727. rt_uint64_t hwid = rt_fdt_read_number(cell, addr_cells);
  728. if (match_cpu_hwid(cpu, hwid))
  729. {
  730. if (thread)
  731. {
  732. *thread = tid;
  733. }
  734. is_end = RT_TRUE;
  735. break;
  736. }
  737. cell += addr_cells;
  738. }
  739. if (is_end)
  740. {
  741. break;
  742. }
  743. }
  744. return cpu_np;
  745. }
  746. struct rt_ofw_node *rt_ofw_get_next_cpu_node(struct rt_ofw_node *prev)
  747. {
  748. struct rt_ofw_node *cpu_np;
  749. if (prev)
  750. {
  751. cpu_np = prev->sibling;
  752. rt_ofw_node_put(prev);
  753. }
  754. else
  755. {
  756. cpu_np = ofw_node_cpus->child;
  757. }
  758. for (; cpu_np; cpu_np = cpu_np->sibling)
  759. {
  760. if (ofw_node_is_fail(cpu_np))
  761. {
  762. continue;
  763. }
  764. if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
  765. {
  766. continue;
  767. }
  768. if (rt_ofw_node_get(cpu_np))
  769. {
  770. break;
  771. }
  772. }
  773. return cpu_np;
  774. }
  775. struct rt_ofw_node *rt_ofw_get_cpu_state_node(struct rt_ofw_node *cpu_np, int index)
  776. {
  777. struct rt_ofw_cell_args args;
  778. struct rt_ofw_node *np = RT_NULL, *state_np;
  779. rt_err_t err = rt_ofw_parse_phandle_cells(cpu_np, "power-domains", "#power-domain-cells", 0, &args);
  780. if (!err)
  781. {
  782. state_np = rt_ofw_parse_phandle(args.data, "domain-idle-states", index);
  783. rt_ofw_node_put(args.data);
  784. if (state_np)
  785. {
  786. np = state_np;
  787. }
  788. }
  789. if (!np)
  790. {
  791. int count = 0;
  792. rt_uint32_t phandle;
  793. const fdt32_t *cell;
  794. struct rt_ofw_prop *prop;
  795. rt_ofw_foreach_prop_u32(cpu_np, "cpu-idle-states", prop, cell, phandle)
  796. {
  797. if (count == index)
  798. {
  799. np = rt_ofw_find_node_by_phandle((rt_phandle)phandle);
  800. break;
  801. }
  802. ++count;
  803. }
  804. }
  805. return np;
  806. }
  807. rt_uint64_t rt_ofw_get_cpu_id(struct rt_ofw_node *cpu_np)
  808. {
  809. rt_uint64_t cpuid = ~0ULL;
  810. if (cpu_np)
  811. {
  812. rt_uint64_t idx = 0;
  813. struct rt_ofw_node *np = ofw_node_cpus->child;
  814. for (; np; np = np->sibling)
  815. {
  816. if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
  817. {
  818. continue;
  819. }
  820. if (cpu_np == np)
  821. {
  822. cpuid = idx;
  823. break;
  824. }
  825. ++idx;
  826. }
  827. if ((rt_int64_t)cpuid < 0 && !rt_ofw_prop_read_u64(cpu_np, "rt-thread,cpuid", &idx))
  828. {
  829. cpuid = idx;
  830. }
  831. }
  832. return cpuid;
  833. }
  834. rt_uint64_t rt_ofw_get_cpu_hwid(struct rt_ofw_node *cpu_np, unsigned int thread)
  835. {
  836. rt_uint64_t thread_id, hwid = ~0ULL;
  837. if (cpu_np && thread >= 0 && !rt_ofw_get_address(cpu_np, thread, &thread_id, RT_NULL))
  838. {
  839. hwid = thread_id;
  840. }
  841. return hwid;
  842. }
  843. rt_err_t ofw_alias_scan(void)
  844. {
  845. rt_err_t err = RT_EOK;
  846. struct rt_ofw_prop *prop;
  847. struct rt_ofw_node *np = ofw_node_aliases, *tmp;
  848. rt_ofw_foreach_prop(np, prop)
  849. {
  850. int id = 0, rate = 1;
  851. struct alias_info *info;
  852. const char *name = prop->name, *end;
  853. /* Maybe the bootloader will set the name, or other nodes reference the aliases */
  854. if (!rt_strcmp(name, "name") || !rt_strcmp(name, "phandle"))
  855. {
  856. continue;
  857. }
  858. if (!(tmp = rt_ofw_find_node_by_path(prop->value)))
  859. {
  860. continue;
  861. }
  862. end = name + rt_strlen(name) - 1;
  863. while (*end && !(*end >= '0' && *end <= '9') && end > name)
  864. {
  865. --end;
  866. }
  867. while (*end && (*end >= '0' && *end <= '9'))
  868. {
  869. id += (*end - '0') * rate;
  870. rate *= 10;
  871. ++end;
  872. }
  873. info = rt_malloc(sizeof(*info));
  874. if (!info)
  875. {
  876. err = -RT_ENOMEM;
  877. break;
  878. }
  879. rt_list_init(&info->list);
  880. info->id = id;
  881. info->tag = name;
  882. info->tag_len = end - name - 1;
  883. info->np = tmp;
  884. rt_list_insert_after(&_aliases_nodes, &info->list);
  885. }
  886. return err;
  887. }
  888. struct rt_ofw_node *rt_ofw_get_alias_node(const char *tag, int id)
  889. {
  890. struct alias_info *info;
  891. struct rt_ofw_node *np = RT_NULL;
  892. if (tag && id >= 0)
  893. {
  894. rt_list_for_each_entry(info, &_aliases_nodes, list)
  895. {
  896. if (rt_strncmp(info->tag, tag, info->tag_len))
  897. {
  898. continue;
  899. }
  900. if (info->id == id)
  901. {
  902. np = info->np;
  903. break;
  904. }
  905. }
  906. }
  907. return np;
  908. }
  909. int ofw_alias_node_id(struct rt_ofw_node *np)
  910. {
  911. int id;
  912. struct alias_info *info;
  913. if (np)
  914. {
  915. id = -1;
  916. rt_list_for_each_entry(info, &_aliases_nodes, list)
  917. {
  918. if (info->np == np)
  919. {
  920. id = info->id;
  921. break;
  922. }
  923. }
  924. }
  925. else
  926. {
  927. id = -RT_EINVAL;
  928. }
  929. return id;
  930. }
  931. int rt_ofw_get_alias_id(struct rt_ofw_node *np, const char *tag)
  932. {
  933. int id;
  934. struct alias_info *info;
  935. if (np && tag)
  936. {
  937. id = -1;
  938. rt_list_for_each_entry(info, &_aliases_nodes, list)
  939. {
  940. if (rt_strncmp(info->tag, tag, info->tag_len))
  941. {
  942. continue;
  943. }
  944. if (info->np == np)
  945. {
  946. id = info->id;
  947. break;
  948. }
  949. }
  950. }
  951. else
  952. {
  953. id = -RT_EINVAL;
  954. }
  955. return id;
  956. }
  957. int rt_ofw_get_alias_last_id(const char *tag)
  958. {
  959. int id;
  960. struct alias_info *info;
  961. if (tag)
  962. {
  963. id = -1;
  964. rt_list_for_each_entry(info, &_aliases_nodes, list)
  965. {
  966. if (rt_strncmp(info->tag, tag, info->tag_len))
  967. {
  968. continue;
  969. }
  970. if (info->id > id)
  971. {
  972. id = info->id;
  973. }
  974. }
  975. }
  976. else
  977. {
  978. id = -RT_EINVAL;
  979. }
  980. return id;
  981. }
  982. struct rt_ofw_node *rt_ofw_append_child(struct rt_ofw_node *parent, const char *full_name)
  983. {
  984. rt_phandle phandle;
  985. rt_err_t err = RT_EOK;
  986. fdt32_t *phandle_value;
  987. struct rt_ofw_node *np = RT_NULL, *child;
  988. if (full_name)
  989. {
  990. if ((phandle = ofw_phandle_next()))
  991. {
  992. np = rt_calloc(1, sizeof(*np) + sizeof(*phandle_value));
  993. }
  994. }
  995. if (np)
  996. {
  997. parent = parent ? : ofw_node_root;
  998. np->full_name = full_name;
  999. np->phandle = phandle;
  1000. np->parent = parent;
  1001. rt_ref_init(&np->ref);
  1002. phandle_value = (void *)np + sizeof(*np);
  1003. *phandle_value = cpu_to_fdt32(phandle);
  1004. err = rt_ofw_append_prop(np, "phandle", sizeof(*phandle_value), phandle_value);
  1005. if (!err)
  1006. {
  1007. if (parent->child)
  1008. {
  1009. rt_ofw_foreach_child_node(parent, child)
  1010. {
  1011. if (!child->sibling)
  1012. {
  1013. child->sibling = np;
  1014. rt_ofw_node_put(child);
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. else
  1020. {
  1021. parent->child = np;
  1022. }
  1023. }
  1024. else
  1025. {
  1026. rt_free(np);
  1027. np = RT_NULL;
  1028. }
  1029. }
  1030. return np;
  1031. }
  1032. rt_err_t rt_ofw_append_prop(struct rt_ofw_node *np, const char *name, int length, void *value)
  1033. {
  1034. rt_err_t err = RT_EOK;
  1035. if (np && name && ((length && value) || (!length && !value)))
  1036. {
  1037. struct rt_ofw_prop *prop = rt_malloc(sizeof(*prop)), *last_prop;
  1038. if (prop)
  1039. {
  1040. prop->name = name;
  1041. prop->length = length;
  1042. prop->value = value;
  1043. prop->next = RT_NULL;
  1044. if (np->props)
  1045. {
  1046. rt_ofw_foreach_prop(np, last_prop)
  1047. {
  1048. if (!last_prop->next)
  1049. {
  1050. last_prop->next = prop;
  1051. break;
  1052. }
  1053. }
  1054. }
  1055. else
  1056. {
  1057. np->props = prop;
  1058. }
  1059. }
  1060. else
  1061. {
  1062. err = -RT_ENOMEM;
  1063. }
  1064. }
  1065. else
  1066. {
  1067. err = -RT_EINVAL;
  1068. }
  1069. return err;
  1070. }
  1071. struct rt_ofw_node *rt_ofw_parse_phandle(const struct rt_ofw_node *np, const char *phandle_name, int index)
  1072. {
  1073. struct rt_ofw_cell_args args;
  1074. struct rt_ofw_node *ref_np = RT_NULL;
  1075. if (!rt_ofw_parse_phandle_cells(np, phandle_name, RT_NULL, index, &args))
  1076. {
  1077. ref_np = args.data;
  1078. }
  1079. return ref_np;
  1080. }
  1081. static rt_err_t ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  1082. int index, struct rt_ofw_cell_args *out_args)
  1083. {
  1084. rt_err_t err = -RT_EEMPTY;
  1085. rt_uint32_t value;
  1086. rt_size_t count = 0;
  1087. const fdt32_t *cell;
  1088. struct rt_ofw_prop *prop;
  1089. /*
  1090. * List:
  1091. *
  1092. * phandle1: node1 {
  1093. * #list-cells = <2>;
  1094. * };
  1095. *
  1096. * phandle2: node2 {
  1097. * #list-cells = <1>;
  1098. * };
  1099. *
  1100. * node3 {
  1101. * list = <&phandle1 0xaa 0xbb>, <&phandle2 0xcc>;
  1102. * };
  1103. *
  1104. * if call:
  1105. * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 0, &args):
  1106. *
  1107. * args.data = node1;
  1108. * args.args_count = 2;
  1109. * args.args[0] = 0xaa;
  1110. * args.args[1] = 0xbb;
  1111. *
  1112. * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 1, &args):
  1113. *
  1114. * args.data = node2;
  1115. * args.args_count = 1;
  1116. * args.args[0] = 0xcc;
  1117. */
  1118. rt_ofw_foreach_prop_u32(np, list_name, prop, cell, value)
  1119. {
  1120. rt_uint32_t cells_count = 0;
  1121. struct rt_ofw_node *phandle_np = rt_ofw_find_node_by_phandle((rt_phandle)value);
  1122. /* if phandle node is undefined, we assume that the cels_count is 0 */
  1123. if (cells_name && phandle_np)
  1124. {
  1125. rt_ofw_prop_read_u32(phandle_np, cells_name, &cells_count);
  1126. }
  1127. if (count++ == index)
  1128. {
  1129. for (int idx = 0; idx < cells_count; ++idx)
  1130. {
  1131. cell = rt_ofw_prop_next_u32(prop, cell, &value);
  1132. out_args->args[idx] = value;
  1133. }
  1134. out_args->args_count = cells_count;
  1135. out_args->data = phandle_np;
  1136. if (out_args->data)
  1137. {
  1138. err = RT_EOK;
  1139. }
  1140. break;
  1141. }
  1142. cell += cells_count;
  1143. }
  1144. return err;
  1145. }
  1146. rt_err_t rt_ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  1147. int index, struct rt_ofw_cell_args *out_args)
  1148. {
  1149. rt_err_t err;
  1150. if (np && list_name && index >= 0 && out_args)
  1151. {
  1152. err = ofw_parse_phandle_cells(np, list_name, cells_name, index, out_args);
  1153. }
  1154. else
  1155. {
  1156. err = -RT_EINVAL;
  1157. }
  1158. return err;
  1159. }
  1160. int rt_ofw_count_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name)
  1161. {
  1162. int count;
  1163. if (np && list_name)
  1164. {
  1165. count = -1;
  1166. if (!cells_name)
  1167. {
  1168. rt_ssize_t length;
  1169. if (rt_ofw_get_prop(np, list_name, &length))
  1170. {
  1171. count = length / sizeof(fdt32_t);
  1172. }
  1173. }
  1174. else
  1175. {
  1176. int index = count = 0;
  1177. struct rt_ofw_cell_args args;
  1178. while (!ofw_parse_phandle_cells(np, list_name, cells_name, index, &args))
  1179. {
  1180. ++index;
  1181. ++count;
  1182. }
  1183. }
  1184. }
  1185. else
  1186. {
  1187. count = -RT_EINVAL;
  1188. }
  1189. return count;
  1190. }
  1191. static const char *ofw_get_prop_fuzzy_name(const struct rt_ofw_node *np, const char *name)
  1192. {
  1193. char *sf, split_field[64];
  1194. rt_size_t len = 0, max_ak = 0;
  1195. const char *str, *result = RT_NULL;
  1196. RT_DECLARE_BITMAP(ak, sizeof(split_field));
  1197. struct rt_ofw_prop *prop;
  1198. /*
  1199. * List:
  1200. *
  1201. * node {
  1202. * property;
  1203. * front-prop-rear;
  1204. * front-prop;
  1205. * prop-rear;
  1206. * };
  1207. *
  1208. * if call:
  1209. * ofw_get_prop_fuzzy_name(node, name):
  1210. * ["prop"] => property
  1211. * ["-prop"] => front-prop-rear
  1212. * ["prop-"] => front-prop-rear
  1213. * ["-prop$"] => front-prop
  1214. * ["^prop-"] => prop-rear
  1215. * ["-prop-"] => front-prop-rear
  1216. * ["front-*-rear"] => front-prop-rear
  1217. */
  1218. str = name;
  1219. sf = split_field;
  1220. if (str[0] != '^')
  1221. {
  1222. /* As '*' */
  1223. *sf++ = '\0';
  1224. rt_bitmap_set_bit(ak, len++);
  1225. }
  1226. else
  1227. {
  1228. ++str;
  1229. }
  1230. for (; *str && len < sizeof(split_field); ++str, ++sf, ++len)
  1231. {
  1232. if (*str != '*')
  1233. {
  1234. *sf = *str;
  1235. rt_bitmap_clear_bit(ak, len);
  1236. }
  1237. else
  1238. {
  1239. max_ak = len;
  1240. *sf = '\0';
  1241. rt_bitmap_set_bit(ak, len);
  1242. }
  1243. }
  1244. *sf = '\0';
  1245. if (str[-1] != '$')
  1246. {
  1247. /* As '*' */
  1248. max_ak = len;
  1249. rt_bitmap_set_bit(ak, len++);
  1250. }
  1251. else
  1252. {
  1253. sf[-1] = '\0';
  1254. --len;
  1255. }
  1256. sf = split_field;
  1257. if (len >= sizeof(split_field))
  1258. {
  1259. LOG_W("%s fuzzy name = %s len is %d out of %d", np->full_name, name, rt_strlen(name), sizeof(split_field));
  1260. }
  1261. rt_ofw_foreach_prop(np, prop)
  1262. {
  1263. int prep_ak = 0, next_ak, field;
  1264. rt_bool_t match = RT_TRUE;
  1265. const char *propname = prop->name, *fuzzy_name = sf;
  1266. if (!rt_bitmap_test_bit(ak, prep_ak))
  1267. {
  1268. next_ak = rt_bitmap_next_set_bit(ak, prep_ak + 1, max_ak) ? : len;
  1269. field = next_ak - prep_ak;
  1270. if (rt_strncmp(propname, fuzzy_name, field))
  1271. {
  1272. continue;
  1273. }
  1274. propname += field;
  1275. fuzzy_name += field;
  1276. prep_ak = next_ak;
  1277. }
  1278. rt_bitmap_for_each_set_bit_from(ak, prep_ak, next_ak, max_ak)
  1279. {
  1280. /* Skip the '*' */
  1281. if (prep_ak == next_ak)
  1282. {
  1283. ++fuzzy_name;
  1284. next_ak = rt_bitmap_next_set_bit(ak, prep_ak + 1, max_ak);
  1285. }
  1286. if (!(str = rt_strstr(propname, fuzzy_name)))
  1287. {
  1288. match = RT_FALSE;
  1289. break;
  1290. }
  1291. field = next_ak - prep_ak;
  1292. propname = str + field - 1;
  1293. fuzzy_name += field;
  1294. prep_ak = next_ak;
  1295. }
  1296. if (match)
  1297. {
  1298. if ((max_ak || !split_field[0]) && next_ak >= max_ak && len - max_ak > 1)
  1299. {
  1300. if (next_ak == max_ak)
  1301. {
  1302. /* Skip the last '*' */
  1303. ++fuzzy_name;
  1304. }
  1305. if (!(propname = rt_strstr(propname, fuzzy_name)))
  1306. {
  1307. continue;
  1308. }
  1309. /* Check end flag */
  1310. if (propname[len - max_ak - 1] != '\0')
  1311. {
  1312. continue;
  1313. }
  1314. }
  1315. result = prop->name;
  1316. break;
  1317. }
  1318. }
  1319. return result;
  1320. }
  1321. const char *rt_ofw_get_prop_fuzzy_name(const struct rt_ofw_node *np, const char *name)
  1322. {
  1323. const char *propname = RT_NULL;
  1324. if (np && name)
  1325. {
  1326. propname = ofw_get_prop_fuzzy_name(np, name);
  1327. }
  1328. return propname;
  1329. }
  1330. struct rt_ofw_prop *rt_ofw_get_prop(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length)
  1331. {
  1332. struct rt_ofw_prop *prop = RT_NULL;
  1333. if (np && name)
  1334. {
  1335. rt_ofw_foreach_prop(np, prop)
  1336. {
  1337. if (!rt_strcmp(prop->name, name))
  1338. {
  1339. if (out_length)
  1340. {
  1341. *out_length = prop->length;
  1342. }
  1343. break;
  1344. }
  1345. }
  1346. }
  1347. return prop;
  1348. }
  1349. #define OFW_PROP_READ_UXX_ARRAY_INDEX(bit) \
  1350. int rt_ofw_prop_read_u##bit##_array_index( \
  1351. const struct rt_ofw_node *np, const char *propname, \
  1352. int index, int nr, rt_uint##bit##_t *out_values) \
  1353. { \
  1354. int res, max_nr; \
  1355. if (np && propname && index >= 0 && nr >= 0 && out_values) \
  1356. { \
  1357. rt_ssize_t len; \
  1358. const fdt##bit##_t *elm; \
  1359. elm = rt_ofw_prop_read_raw(np, propname, &len); \
  1360. max_nr = len / sizeof(*elm); \
  1361. if (elm && index < max_nr) \
  1362. { \
  1363. elm += index; \
  1364. max_nr -= index; \
  1365. res = nr > max_nr ? max_nr : nr; \
  1366. for (nr = 0; nr < res; ++nr) \
  1367. { \
  1368. *out_values++ = fdt##bit##_to_cpu(*elm++); \
  1369. } \
  1370. } \
  1371. else \
  1372. { \
  1373. res = -RT_EEMPTY; \
  1374. } \
  1375. } \
  1376. else \
  1377. { \
  1378. res = -RT_EINVAL; \
  1379. } \
  1380. return res; \
  1381. }
  1382. OFW_PROP_READ_UXX_ARRAY_INDEX(8)
  1383. OFW_PROP_READ_UXX_ARRAY_INDEX(16)
  1384. OFW_PROP_READ_UXX_ARRAY_INDEX(32)
  1385. OFW_PROP_READ_UXX_ARRAY_INDEX(64)
  1386. #undef OFW_PROP_READ_UXX_ARRAY_INDEX
  1387. int rt_ofw_prop_read_string_array_index(const struct rt_ofw_node *np, const char *propname,
  1388. int index, int nr, const char **out_strings)
  1389. {
  1390. int res = 0;
  1391. if (np && propname && index >= 0 && nr >= 0 && out_strings)
  1392. {
  1393. rt_ssize_t len, slen = 0;
  1394. const char *value = rt_ofw_prop_read_raw(np, propname, &len);
  1395. if (value)
  1396. {
  1397. nr += index;
  1398. for (int idx = 0; idx < nr && len > 0; ++idx)
  1399. {
  1400. /* Add '\0' */
  1401. slen = rt_strlen(value) + 1;
  1402. if (idx >= index)
  1403. {
  1404. *out_strings++ = value;
  1405. ++res;
  1406. }
  1407. len -= slen;
  1408. value += slen;
  1409. }
  1410. }
  1411. else
  1412. {
  1413. res = -RT_EEMPTY;
  1414. }
  1415. }
  1416. else
  1417. {
  1418. res = -RT_EINVAL;
  1419. }
  1420. return res;
  1421. }
  1422. int rt_ofw_prop_count_of_size(const struct rt_ofw_node *np, const char *propname, int size)
  1423. {
  1424. int count;
  1425. if (np && propname && size > 0)
  1426. {
  1427. rt_ssize_t len;
  1428. count = -RT_EEMPTY;
  1429. if (rt_ofw_get_prop(np, propname, &len))
  1430. {
  1431. count = len / size;
  1432. }
  1433. }
  1434. else
  1435. {
  1436. count = -RT_EINVAL;
  1437. }
  1438. return count;
  1439. }
  1440. static rt_int32_t ofw_strcmp(const char *cs, const char *ct)
  1441. {
  1442. return rt_strcmp(cs, ct);
  1443. }
  1444. int rt_ofw_prop_index_of_string(const struct rt_ofw_node *np, const char *propname, const char *string)
  1445. {
  1446. int idx;
  1447. if (np && propname && string)
  1448. {
  1449. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
  1450. idx = -1;
  1451. if (prop)
  1452. {
  1453. idx = ofw_prop_index_of_string(prop, string, ofw_strcmp);
  1454. }
  1455. }
  1456. else
  1457. {
  1458. idx = -RT_EINVAL;
  1459. }
  1460. return idx;
  1461. }
  1462. const fdt32_t *rt_ofw_prop_next_u32(struct rt_ofw_prop *prop, const fdt32_t *cur, rt_uint32_t *out_value)
  1463. {
  1464. if (prop && out_value)
  1465. {
  1466. if (cur)
  1467. {
  1468. ++cur;
  1469. if ((void *)cur >= prop->value + prop->length)
  1470. {
  1471. cur = RT_NULL;
  1472. }
  1473. }
  1474. else
  1475. {
  1476. cur = prop->value;
  1477. }
  1478. if (cur)
  1479. {
  1480. *out_value = fdt32_to_cpu(*cur);
  1481. }
  1482. }
  1483. else
  1484. {
  1485. cur = RT_NULL;
  1486. }
  1487. return cur;
  1488. }
  1489. const char *rt_ofw_prop_next_string(struct rt_ofw_prop *prop, const char *cur)
  1490. {
  1491. if (prop)
  1492. {
  1493. if (cur)
  1494. {
  1495. cur += rt_strlen(cur) + 1;
  1496. if ((void *)cur >= prop->value + prop->length)
  1497. {
  1498. cur = RT_NULL;
  1499. }
  1500. }
  1501. else
  1502. {
  1503. cur = prop->value;
  1504. }
  1505. }
  1506. else
  1507. {
  1508. cur = RT_NULL;
  1509. }
  1510. return cur;
  1511. }