base.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  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 = 0;
  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;
  851. struct alias_info *info;
  852. const char *name = prop->name, *end, *id_start;
  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);
  863. while (*(end - 1) && (*(end - 1) >= '0' && *(end - 1) <= '9') && end > name)
  864. {
  865. --end;
  866. }
  867. id_start = end;
  868. while (*id_start && (*id_start >= '0' && *id_start <= '9'))
  869. {
  870. id *= 10;
  871. id += (*id_start - '0');
  872. ++id_start;
  873. }
  874. info = rt_malloc(sizeof(*info));
  875. if (!info)
  876. {
  877. err = -RT_ENOMEM;
  878. break;
  879. }
  880. rt_list_init(&info->list);
  881. info->id = id;
  882. info->tag = name;
  883. info->tag_len = end - name;
  884. info->np = tmp;
  885. rt_list_insert_after(&_aliases_nodes, &info->list);
  886. }
  887. return err;
  888. }
  889. struct rt_ofw_node *rt_ofw_get_alias_node(const char *tag, int id)
  890. {
  891. struct alias_info *info;
  892. struct rt_ofw_node *np = RT_NULL;
  893. if (tag && id >= 0)
  894. {
  895. if (!rt_list_isempty(&_aliases_nodes))
  896. {
  897. rt_list_for_each_entry(info, &_aliases_nodes, list)
  898. {
  899. if (rt_strncmp(info->tag, tag, info->tag_len))
  900. {
  901. continue;
  902. }
  903. if (info->id == id)
  904. {
  905. np = info->np;
  906. break;
  907. }
  908. }
  909. }
  910. }
  911. return np;
  912. }
  913. int ofw_alias_node_id(struct rt_ofw_node *np)
  914. {
  915. int id;
  916. struct alias_info *info = RT_NULL;
  917. if (np)
  918. {
  919. id = -1;
  920. if (!rt_list_isempty(&_aliases_nodes))
  921. {
  922. rt_list_for_each_entry(info, &_aliases_nodes, list)
  923. {
  924. if (info->np == np)
  925. {
  926. id = info->id;
  927. break;
  928. }
  929. }
  930. }
  931. }
  932. else
  933. {
  934. id = -RT_EINVAL;
  935. }
  936. return id;
  937. }
  938. int rt_ofw_get_alias_id(struct rt_ofw_node *np, const char *tag)
  939. {
  940. int id;
  941. struct alias_info *info;
  942. if (np && tag)
  943. {
  944. id = -1;
  945. if (!rt_list_isempty(&_aliases_nodes))
  946. {
  947. rt_list_for_each_entry(info, &_aliases_nodes, list)
  948. {
  949. if (rt_strncmp(info->tag, tag, info->tag_len))
  950. {
  951. continue;
  952. }
  953. if (info->np == np)
  954. {
  955. id = info->id;
  956. break;
  957. }
  958. }
  959. }
  960. }
  961. else
  962. {
  963. id = -RT_EINVAL;
  964. }
  965. return id;
  966. }
  967. int rt_ofw_get_alias_last_id(const char *tag)
  968. {
  969. int id;
  970. struct alias_info *info;
  971. if (tag)
  972. {
  973. id = -1;
  974. if (!rt_list_isempty(&_aliases_nodes))
  975. {
  976. rt_list_for_each_entry(info, &_aliases_nodes, list)
  977. {
  978. if (rt_strncmp(info->tag, tag, info->tag_len))
  979. {
  980. continue;
  981. }
  982. if (info->id > id)
  983. {
  984. id = info->id;
  985. }
  986. }
  987. }
  988. }
  989. else
  990. {
  991. id = -RT_EINVAL;
  992. }
  993. return id;
  994. }
  995. static rt_err_t ofw_map_id(struct rt_ofw_node *np, rt_uint32_t id, const char *map_name, const char *map_mask_name,
  996. const fdt32_t *map, rt_ssize_t map_len, struct rt_ofw_node **ref_np, rt_uint32_t *out_id)
  997. {
  998. rt_err_t err = RT_EOK;
  999. rt_uint32_t masked_id, map_mask;
  1000. /* Select all bits default */
  1001. map_mask = 0xffffffff;
  1002. if (map_mask_name)
  1003. {
  1004. rt_ofw_prop_read_u32(np, map_mask_name, &map_mask);
  1005. }
  1006. masked_id = map_mask & id;
  1007. for (; map_len > 0; map_len -= 4 * sizeof(*map), map += 4)
  1008. {
  1009. struct rt_ofw_node *phandle_node;
  1010. rt_uint32_t id_base = fdt32_to_cpu(*(map + 0));
  1011. rt_uint32_t phandle = fdt32_to_cpu(*(map + 1));
  1012. rt_uint32_t out_base = fdt32_to_cpu(*(map + 2));
  1013. rt_uint32_t id_len = fdt32_to_cpu(*(map + 3));
  1014. if (id_base & ~map_mask)
  1015. {
  1016. LOG_E("%s: Invalid %s translation - %s(0x%x) for id-base = 0x%x",
  1017. np->full_name, map_name, map_mask_name, map_mask, id_base);
  1018. err = -RT_ERROR;
  1019. break;
  1020. }
  1021. if (masked_id < id_base || masked_id >= id_base + id_len)
  1022. {
  1023. continue;
  1024. }
  1025. phandle_node = rt_ofw_find_node_by_phandle((rt_phandle)phandle);
  1026. if (!phandle_node)
  1027. {
  1028. err = -RT_EEMPTY;
  1029. break;
  1030. }
  1031. if (ref_np)
  1032. {
  1033. if (*ref_np)
  1034. {
  1035. rt_ofw_node_put(phandle_node);
  1036. }
  1037. else
  1038. {
  1039. *ref_np = phandle_node;
  1040. }
  1041. if (*ref_np != phandle_node)
  1042. {
  1043. continue;
  1044. }
  1045. }
  1046. if (out_id)
  1047. {
  1048. *out_id = masked_id - id_base + out_base;
  1049. }
  1050. LOG_D("%s: Get %s translation - %s(0x%x) for id-base = 0x%x, out-base = 0x%x, length = %d, id: 0x%x -> 0x%x",
  1051. np->full_name, map_name, map_mask_name, map_mask,
  1052. id_base, out_base, id_len, id, masked_id - id_base + out_base);
  1053. break;
  1054. }
  1055. if (map_len <= 0)
  1056. {
  1057. LOG_I("%s: No %s translation for id(0x%x) on %s", np->full_name, map_name,
  1058. id, ref_np && *ref_np ? *ref_np : RT_NULL);
  1059. /* Bypasses translation */
  1060. if (out_id)
  1061. {
  1062. *out_id = id;
  1063. }
  1064. }
  1065. return err;
  1066. }
  1067. rt_err_t rt_ofw_map_id(struct rt_ofw_node *np, rt_uint32_t id, const char *map_name, const char *map_mask_name,
  1068. struct rt_ofw_node **ref_np, rt_uint32_t *out_id)
  1069. {
  1070. rt_err_t err;
  1071. if (np && map_name && (ref_np || out_id))
  1072. {
  1073. rt_ssize_t map_len;
  1074. const fdt32_t *map = rt_ofw_prop_read_raw(np, map_name, &map_len);
  1075. if (!map)
  1076. {
  1077. if (ref_np)
  1078. {
  1079. err = -RT_EEMPTY;
  1080. }
  1081. else
  1082. {
  1083. *out_id = id;
  1084. }
  1085. err = RT_EOK;
  1086. }
  1087. else if (!map_len || map_len % (4 * sizeof(*map)))
  1088. {
  1089. LOG_E("%s: Invalid %s length = %u", np->full_name, map_name, map_len);
  1090. err = -RT_EINVAL;
  1091. }
  1092. else
  1093. {
  1094. err = ofw_map_id(np, id, map_name, map_mask_name, map, map_len, ref_np, out_id);
  1095. }
  1096. }
  1097. else
  1098. {
  1099. err = -RT_EINVAL;
  1100. }
  1101. return err;
  1102. }
  1103. struct rt_ofw_node *rt_ofw_append_child(struct rt_ofw_node *parent, const char *full_name)
  1104. {
  1105. rt_phandle phandle;
  1106. rt_err_t err = RT_EOK;
  1107. fdt32_t *phandle_value;
  1108. struct rt_ofw_node *np = RT_NULL, *child;
  1109. if (full_name)
  1110. {
  1111. if ((phandle = ofw_phandle_next()))
  1112. {
  1113. np = rt_calloc(1, sizeof(*np) + sizeof(*phandle_value));
  1114. }
  1115. }
  1116. if (np)
  1117. {
  1118. parent = parent ? : ofw_node_root;
  1119. np->full_name = full_name;
  1120. np->phandle = phandle;
  1121. np->parent = parent;
  1122. rt_ref_init(&np->ref);
  1123. phandle_value = (void *)np + sizeof(*np);
  1124. *phandle_value = cpu_to_fdt32(phandle);
  1125. err = rt_ofw_append_prop(np, "phandle", sizeof(*phandle_value), phandle_value);
  1126. if (!err)
  1127. {
  1128. if (parent->child)
  1129. {
  1130. rt_ofw_foreach_child_node(parent, child)
  1131. {
  1132. if (!child->sibling)
  1133. {
  1134. child->sibling = np;
  1135. rt_ofw_node_put(child);
  1136. break;
  1137. }
  1138. }
  1139. }
  1140. else
  1141. {
  1142. parent->child = np;
  1143. }
  1144. }
  1145. else
  1146. {
  1147. rt_free(np);
  1148. np = RT_NULL;
  1149. }
  1150. }
  1151. return rt_ofw_node_get(np);
  1152. }
  1153. rt_err_t rt_ofw_append_prop(struct rt_ofw_node *np, const char *name, int length, void *value)
  1154. {
  1155. rt_err_t err = RT_EOK;
  1156. if (np && name && ((length && value) || (!length && !value)))
  1157. {
  1158. struct rt_ofw_prop *prop = rt_malloc(sizeof(*prop)), *last_prop;
  1159. if (prop)
  1160. {
  1161. prop->name = name;
  1162. prop->length = length;
  1163. prop->value = value;
  1164. prop->next = RT_NULL;
  1165. if (np->props)
  1166. {
  1167. rt_ofw_foreach_prop(np, last_prop)
  1168. {
  1169. if (!last_prop->next)
  1170. {
  1171. last_prop->next = prop;
  1172. break;
  1173. }
  1174. }
  1175. }
  1176. else
  1177. {
  1178. np->props = prop;
  1179. }
  1180. }
  1181. else
  1182. {
  1183. err = -RT_ENOMEM;
  1184. }
  1185. }
  1186. else
  1187. {
  1188. err = -RT_EINVAL;
  1189. }
  1190. return err;
  1191. }
  1192. struct rt_ofw_node *rt_ofw_parse_phandle(const struct rt_ofw_node *np, const char *phandle_name, int index)
  1193. {
  1194. struct rt_ofw_cell_args args;
  1195. struct rt_ofw_node *ref_np = RT_NULL;
  1196. if (!rt_ofw_parse_phandle_cells(np, phandle_name, RT_NULL, index, &args))
  1197. {
  1198. ref_np = args.data;
  1199. }
  1200. return ref_np;
  1201. }
  1202. static rt_err_t ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  1203. int index, struct rt_ofw_cell_args *out_args)
  1204. {
  1205. rt_err_t err = -RT_EEMPTY;
  1206. rt_uint32_t value;
  1207. rt_size_t count = 0;
  1208. const fdt32_t *cell;
  1209. struct rt_ofw_prop *prop;
  1210. /*
  1211. * List:
  1212. *
  1213. * phandle1: node1 {
  1214. * #list-cells = <2>;
  1215. * };
  1216. *
  1217. * phandle2: node2 {
  1218. * #list-cells = <1>;
  1219. * };
  1220. *
  1221. * node3 {
  1222. * list = <&phandle1 0xaa 0xbb>, <&phandle2 0xcc>;
  1223. * };
  1224. *
  1225. * if call:
  1226. * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 0, &args):
  1227. *
  1228. * args.data = node1;
  1229. * args.args_count = 2;
  1230. * args.args[0] = 0xaa;
  1231. * args.args[1] = 0xbb;
  1232. *
  1233. * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 1, &args):
  1234. *
  1235. * args.data = node2;
  1236. * args.args_count = 1;
  1237. * args.args[0] = 0xcc;
  1238. */
  1239. rt_ofw_foreach_prop_u32(np, list_name, prop, cell, value)
  1240. {
  1241. rt_uint32_t cells_count = 0;
  1242. struct rt_ofw_node *phandle_np = rt_ofw_find_node_by_phandle((rt_phandle)value);
  1243. /* if phandle node is undefined, we assume that the cels_count is 0 */
  1244. if (cells_name && phandle_np)
  1245. {
  1246. rt_ofw_prop_read_u32(phandle_np, cells_name, &cells_count);
  1247. }
  1248. if (count++ == index)
  1249. {
  1250. for (int idx = 0; idx < cells_count; ++idx)
  1251. {
  1252. cell = rt_ofw_prop_next_u32(prop, cell, &value);
  1253. out_args->args[idx] = value;
  1254. }
  1255. out_args->args_count = cells_count;
  1256. out_args->data = phandle_np;
  1257. if (out_args->data)
  1258. {
  1259. err = RT_EOK;
  1260. }
  1261. break;
  1262. }
  1263. cell += cells_count;
  1264. }
  1265. return err;
  1266. }
  1267. rt_err_t rt_ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  1268. int index, struct rt_ofw_cell_args *out_args)
  1269. {
  1270. rt_err_t err;
  1271. if (np && list_name && index >= 0 && out_args)
  1272. {
  1273. err = ofw_parse_phandle_cells(np, list_name, cells_name, index, out_args);
  1274. }
  1275. else
  1276. {
  1277. err = -RT_EINVAL;
  1278. }
  1279. return err;
  1280. }
  1281. int rt_ofw_count_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name)
  1282. {
  1283. int count;
  1284. if (np && list_name)
  1285. {
  1286. count = -1;
  1287. if (!cells_name)
  1288. {
  1289. rt_ssize_t length;
  1290. if (rt_ofw_get_prop(np, list_name, &length))
  1291. {
  1292. count = length / sizeof(fdt32_t);
  1293. }
  1294. }
  1295. else
  1296. {
  1297. int index = count = 0;
  1298. struct rt_ofw_cell_args args;
  1299. while (!ofw_parse_phandle_cells(np, list_name, cells_name, index, &args))
  1300. {
  1301. ++index;
  1302. ++count;
  1303. }
  1304. }
  1305. }
  1306. else
  1307. {
  1308. count = -RT_EINVAL;
  1309. }
  1310. return count;
  1311. }
  1312. static const char *ofw_get_prop_fuzzy_name(const struct rt_ofw_node *np, const char *name)
  1313. {
  1314. char *sf, split_field[64];
  1315. rt_size_t len = 0, max_ak = 0;
  1316. const char *str, *result = RT_NULL;
  1317. RT_BITMAP_DECLARE(ak, sizeof(split_field)) = {0};
  1318. struct rt_ofw_prop *prop;
  1319. /*
  1320. * List:
  1321. *
  1322. * node {
  1323. * property;
  1324. * front-prop-rear;
  1325. * front-prop;
  1326. * prop-rear;
  1327. * };
  1328. *
  1329. * if call:
  1330. * ofw_get_prop_fuzzy_name(node, name):
  1331. * ["prop"] => property
  1332. * ["-prop"] => front-prop-rear
  1333. * ["prop-"] => front-prop-rear
  1334. * ["-prop$"] => front-prop
  1335. * ["^prop-"] => prop-rear
  1336. * ["-prop-"] => front-prop-rear
  1337. * ["front-*-rear"] => front-prop-rear
  1338. */
  1339. str = name;
  1340. sf = split_field;
  1341. if (str[0] != '^')
  1342. {
  1343. /* As '*' */
  1344. *sf++ = '\0';
  1345. rt_bitmap_set_bit(ak, len++);
  1346. }
  1347. else
  1348. {
  1349. ++str;
  1350. }
  1351. for (; *str && len < sizeof(split_field); ++str, ++sf, ++len)
  1352. {
  1353. if (*str != '*')
  1354. {
  1355. *sf = *str;
  1356. rt_bitmap_clear_bit(ak, len);
  1357. }
  1358. else
  1359. {
  1360. max_ak = len;
  1361. *sf = '\0';
  1362. rt_bitmap_set_bit(ak, len);
  1363. }
  1364. }
  1365. *sf = '\0';
  1366. if (str[-1] != '$')
  1367. {
  1368. /* As '*' */
  1369. max_ak = len;
  1370. rt_bitmap_set_bit(ak, len++);
  1371. }
  1372. else
  1373. {
  1374. sf[-1] = '\0';
  1375. --len;
  1376. }
  1377. sf = split_field;
  1378. if (len >= sizeof(split_field))
  1379. {
  1380. LOG_W("%s fuzzy name = %s len is %d out of %d", np->full_name, name, rt_strlen(name), sizeof(split_field));
  1381. }
  1382. rt_ofw_foreach_prop(np, prop)
  1383. {
  1384. int prep_ak = 0, next_ak, field;
  1385. rt_bool_t match = RT_TRUE;
  1386. const char *propname = prop->name, *fuzzy_name = sf;
  1387. if (!rt_bitmap_test_bit(ak, prep_ak))
  1388. {
  1389. next_ak = rt_bitmap_next_set_bit(ak, prep_ak + 1, max_ak) ? : len;
  1390. field = next_ak - prep_ak;
  1391. if (rt_strncmp(propname, fuzzy_name, field))
  1392. {
  1393. continue;
  1394. }
  1395. propname += field;
  1396. fuzzy_name += field;
  1397. prep_ak = next_ak;
  1398. }
  1399. rt_bitmap_for_each_set_bit_from(ak, prep_ak, next_ak, max_ak)
  1400. {
  1401. /* Skip the '*' */
  1402. if (prep_ak == next_ak)
  1403. {
  1404. ++fuzzy_name;
  1405. next_ak = rt_bitmap_next_set_bit(ak, prep_ak + 1, max_ak);
  1406. }
  1407. if (!(str = rt_strstr(propname, fuzzy_name)))
  1408. {
  1409. match = RT_FALSE;
  1410. break;
  1411. }
  1412. field = next_ak - prep_ak;
  1413. propname = str + field - 1;
  1414. fuzzy_name += field;
  1415. prep_ak = next_ak;
  1416. }
  1417. if (match)
  1418. {
  1419. if ((max_ak || !split_field[0]) && next_ak >= max_ak && len - max_ak > 1)
  1420. {
  1421. if (next_ak == max_ak)
  1422. {
  1423. /* Skip the last '*' */
  1424. ++fuzzy_name;
  1425. }
  1426. if (!(propname = rt_strstr(propname, fuzzy_name)))
  1427. {
  1428. continue;
  1429. }
  1430. /* Check end flag */
  1431. if (propname[len - max_ak - 1] != '\0')
  1432. {
  1433. continue;
  1434. }
  1435. }
  1436. result = prop->name;
  1437. break;
  1438. }
  1439. }
  1440. return result;
  1441. }
  1442. const char *rt_ofw_get_prop_fuzzy_name(const struct rt_ofw_node *np, const char *name)
  1443. {
  1444. const char *propname = RT_NULL;
  1445. if (np && name)
  1446. {
  1447. propname = ofw_get_prop_fuzzy_name(np, name);
  1448. }
  1449. return propname;
  1450. }
  1451. struct rt_ofw_prop *rt_ofw_get_prop(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length)
  1452. {
  1453. struct rt_ofw_prop *prop = RT_NULL;
  1454. if (np && name)
  1455. {
  1456. rt_ofw_foreach_prop(np, prop)
  1457. {
  1458. if (!rt_strcmp(prop->name, name))
  1459. {
  1460. if (out_length)
  1461. {
  1462. *out_length = prop->length;
  1463. }
  1464. break;
  1465. }
  1466. }
  1467. }
  1468. return prop;
  1469. }
  1470. #define OFW_PROP_READ_UXX_ARRAY_INDEX(bit) \
  1471. int rt_ofw_prop_read_u##bit##_array_index( \
  1472. const struct rt_ofw_node *np, const char *propname, \
  1473. int index, int nr, rt_uint##bit##_t *out_values) \
  1474. { \
  1475. int res, max_nr; \
  1476. if (np && propname && index >= 0 && nr >= 0 && out_values) \
  1477. { \
  1478. rt_ssize_t len; \
  1479. const fdt##bit##_t *elm; \
  1480. elm = rt_ofw_prop_read_raw(np, propname, &len); \
  1481. max_nr = len / sizeof(*elm); \
  1482. if (elm && index < max_nr) \
  1483. { \
  1484. elm += index; \
  1485. max_nr -= index; \
  1486. res = nr > max_nr ? max_nr : nr; \
  1487. for (nr = 0; nr < res; ++nr) \
  1488. { \
  1489. *out_values++ = fdt##bit##_to_cpu(*elm++); \
  1490. } \
  1491. } \
  1492. else \
  1493. { \
  1494. res = -RT_EEMPTY; \
  1495. } \
  1496. } \
  1497. else \
  1498. { \
  1499. res = -RT_EINVAL; \
  1500. } \
  1501. return res; \
  1502. }
  1503. OFW_PROP_READ_UXX_ARRAY_INDEX(8)
  1504. OFW_PROP_READ_UXX_ARRAY_INDEX(16)
  1505. OFW_PROP_READ_UXX_ARRAY_INDEX(32)
  1506. OFW_PROP_READ_UXX_ARRAY_INDEX(64)
  1507. #undef OFW_PROP_READ_UXX_ARRAY_INDEX
  1508. int rt_ofw_prop_read_string_array_index(const struct rt_ofw_node *np, const char *propname,
  1509. int index, int nr, const char **out_strings)
  1510. {
  1511. int res = 0;
  1512. if (np && propname && index >= 0 && nr >= 0 && out_strings)
  1513. {
  1514. rt_ssize_t len, slen = 0;
  1515. const char *value = rt_ofw_prop_read_raw(np, propname, &len);
  1516. if (value)
  1517. {
  1518. nr += index;
  1519. for (int idx = 0; idx < nr && len > 0; ++idx)
  1520. {
  1521. /* Add '\0' */
  1522. slen = rt_strlen(value) + 1;
  1523. if (idx >= index)
  1524. {
  1525. *out_strings++ = value;
  1526. ++res;
  1527. }
  1528. len -= slen;
  1529. value += slen;
  1530. }
  1531. }
  1532. else
  1533. {
  1534. res = -RT_EEMPTY;
  1535. }
  1536. }
  1537. else
  1538. {
  1539. res = -RT_EINVAL;
  1540. }
  1541. return res;
  1542. }
  1543. int rt_ofw_prop_count_of_size(const struct rt_ofw_node *np, const char *propname, int size)
  1544. {
  1545. int count;
  1546. if (np && propname && size > 0)
  1547. {
  1548. rt_ssize_t len;
  1549. count = -RT_EEMPTY;
  1550. if (rt_ofw_get_prop(np, propname, &len))
  1551. {
  1552. count = len / size;
  1553. }
  1554. }
  1555. else
  1556. {
  1557. count = -RT_EINVAL;
  1558. }
  1559. return count;
  1560. }
  1561. static rt_int32_t ofw_strcmp(const char *cs, const char *ct)
  1562. {
  1563. return rt_strcmp(cs, ct);
  1564. }
  1565. int rt_ofw_prop_index_of_string(const struct rt_ofw_node *np, const char *propname, const char *string)
  1566. {
  1567. int idx;
  1568. if (np && propname && string)
  1569. {
  1570. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
  1571. idx = -1;
  1572. if (prop)
  1573. {
  1574. idx = ofw_prop_index_of_string(prop, string, ofw_strcmp);
  1575. }
  1576. }
  1577. else
  1578. {
  1579. idx = -RT_EINVAL;
  1580. }
  1581. return idx;
  1582. }
  1583. const fdt32_t *rt_ofw_prop_next_u32(struct rt_ofw_prop *prop, const fdt32_t *cur, rt_uint32_t *out_value)
  1584. {
  1585. if (prop && out_value)
  1586. {
  1587. if (cur)
  1588. {
  1589. ++cur;
  1590. if ((void *)cur >= prop->value + prop->length)
  1591. {
  1592. cur = RT_NULL;
  1593. }
  1594. }
  1595. else
  1596. {
  1597. cur = prop->value;
  1598. }
  1599. if (cur)
  1600. {
  1601. *out_value = fdt32_to_cpu(*cur);
  1602. }
  1603. }
  1604. else
  1605. {
  1606. cur = RT_NULL;
  1607. }
  1608. return cur;
  1609. }
  1610. const char *rt_ofw_prop_next_string(struct rt_ofw_prop *prop, const char *cur)
  1611. {
  1612. if (prop)
  1613. {
  1614. if (cur)
  1615. {
  1616. cur += rt_strlen(cur) + 1;
  1617. if ((void *)cur >= prop->value + prop->length)
  1618. {
  1619. cur = RT_NULL;
  1620. }
  1621. }
  1622. else
  1623. {
  1624. cur = prop->value;
  1625. }
  1626. }
  1627. else
  1628. {
  1629. cur = RT_NULL;
  1630. }
  1631. return cur;
  1632. }