base.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  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 RT_DEFINE_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. return rt_strcasecmp(cs, ct);
  275. }
  276. static int ofw_prop_index_of_compatible(struct rt_ofw_prop *prop, const char *compatible)
  277. {
  278. return ofw_prop_index_of_string(prop, compatible, ofw_strcasecmp);
  279. }
  280. static int ofw_node_index_of_compatible(const struct rt_ofw_node *np, const char *compatible)
  281. {
  282. int idx = -1;
  283. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  284. if (prop)
  285. {
  286. idx = ofw_prop_index_of_compatible(prop, compatible);
  287. }
  288. return idx;
  289. }
  290. rt_bool_t rt_ofw_machine_is_compatible(const char *compatible)
  291. {
  292. return ofw_node_index_of_compatible(ofw_node_root, compatible) >= 0;
  293. }
  294. /*
  295. * Property status:
  296. *
  297. * "okay" or "ok":
  298. * Indicates the device is operational.
  299. *
  300. * "disabled":
  301. * Indicates that the device is not presently operational, but it might
  302. * become operational in the future (for example, something is not
  303. * plugged in, or switched off).
  304. * Refer to the device binding for details on what disabled means for a
  305. * given device.
  306. *
  307. * "reserved":
  308. * Indicates that the device is operational, but should not be used.
  309. * Typically this is used for devices that are controlled by another
  310. * software component, such as platform firmware.
  311. *
  312. * "fail":
  313. * Indicates that the device is not operational. A serious error was
  314. * detected in the device, and it is unlikely to become operational
  315. * without repair.
  316. *
  317. * "fail-sss":
  318. * Indicates that the device is not operational. A serious error was
  319. * detected in the device and it is unlikely to become operational
  320. * without repair. The sss portion of the value is specific to the
  321. * device and indicates the error condition detected.
  322. */
  323. static rt_bool_t ofw_node_is_fail(const struct rt_ofw_node *np)
  324. {
  325. rt_bool_t res = RT_FALSE;
  326. const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
  327. if (status)
  328. {
  329. res = !rt_strcmp(status, "fail") || !rt_strncmp(status, "fail-", 5);
  330. }
  331. return res;
  332. }
  333. static rt_bool_t ofw_node_is_available(const struct rt_ofw_node *np)
  334. {
  335. rt_bool_t res = RT_TRUE;
  336. const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
  337. if (status)
  338. {
  339. res = !rt_strcmp(status, "okay") || !rt_strcmp(status, "ok");
  340. }
  341. return res;
  342. }
  343. rt_bool_t rt_ofw_node_is_available(const struct rt_ofw_node *np)
  344. {
  345. return np ? ofw_node_is_available(np) : RT_FALSE;
  346. }
  347. rt_bool_t rt_ofw_node_is_compatible(const struct rt_ofw_node *np, const char *compatible)
  348. {
  349. rt_bool_t res = RT_FALSE;
  350. if (np)
  351. {
  352. res = ofw_node_index_of_compatible(np, compatible) >= 0;
  353. }
  354. return res;
  355. }
  356. static struct rt_ofw_node_id *ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
  357. {
  358. int best_index = RT_UINT32_MAX >> 1, index;
  359. struct rt_ofw_node_id *found_id = RT_NULL, *id;
  360. for (id = (struct rt_ofw_node_id *)ids; id->compatible[0]; ++id)
  361. {
  362. index = ofw_prop_index_of_compatible(prop, id->compatible);
  363. if (index >= 0 && index < best_index)
  364. {
  365. found_id = id;
  366. best_index = index;
  367. }
  368. }
  369. return found_id;
  370. }
  371. struct rt_ofw_node_id *rt_ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
  372. {
  373. struct rt_ofw_node_id *id = RT_NULL;
  374. if (prop && ids && !rt_strcmp(prop->name, "compatible"))
  375. {
  376. id = ofw_prop_match(prop, ids);
  377. }
  378. return id;
  379. }
  380. struct rt_ofw_node_id *rt_ofw_node_match(struct rt_ofw_node *np, const struct rt_ofw_node_id *ids)
  381. {
  382. struct rt_ofw_prop *prop;
  383. struct rt_ofw_node_id *id = RT_NULL;
  384. if (np && ids && (prop = rt_ofw_get_prop(np, "compatible", RT_NULL)))
  385. {
  386. id = ofw_prop_match(prop, ids);
  387. }
  388. return id;
  389. }
  390. struct rt_ofw_node *rt_ofw_find_node_by_tag(struct rt_ofw_node *from, const char *tag)
  391. {
  392. struct rt_ofw_node *np = RT_NULL;
  393. if (tag)
  394. {
  395. rt_ofw_foreach_nodes(from, np)
  396. {
  397. if (rt_ofw_node_tag_equ(np, tag))
  398. {
  399. break;
  400. }
  401. }
  402. }
  403. return np;
  404. }
  405. struct rt_ofw_node *rt_ofw_find_node_by_prop_r(struct rt_ofw_node *from, const char *propname,
  406. const struct rt_ofw_prop **out_prop)
  407. {
  408. struct rt_ofw_node *np = RT_NULL;
  409. if (propname)
  410. {
  411. rt_ofw_foreach_nodes(from, np)
  412. {
  413. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
  414. if (prop)
  415. {
  416. if (out_prop)
  417. {
  418. *out_prop = prop;
  419. }
  420. break;
  421. }
  422. }
  423. }
  424. return np;
  425. }
  426. struct rt_ofw_node *rt_ofw_find_node_by_name(struct rt_ofw_node *from, const char *name)
  427. {
  428. struct rt_ofw_node *np = RT_NULL;
  429. if (name)
  430. {
  431. rt_ofw_foreach_nodes(from, np)
  432. {
  433. if (np->name && !rt_strcmp(np->name, name))
  434. {
  435. np = rt_ofw_node_get(np);
  436. break;
  437. }
  438. }
  439. }
  440. return np;
  441. }
  442. struct rt_ofw_node *rt_ofw_find_node_by_type(struct rt_ofw_node *from, const char *type)
  443. {
  444. struct rt_ofw_node *np = RT_NULL;
  445. if (type)
  446. {
  447. rt_ofw_foreach_nodes(from, np)
  448. {
  449. if (rt_ofw_node_is_type(np, type))
  450. {
  451. break;
  452. }
  453. }
  454. }
  455. return np;
  456. }
  457. struct rt_ofw_node *rt_ofw_find_node_by_compatible(struct rt_ofw_node *from, const char *compatible)
  458. {
  459. struct rt_ofw_node *np = RT_NULL;
  460. if (compatible)
  461. {
  462. rt_ofw_foreach_nodes(from, np)
  463. {
  464. if (ofw_node_index_of_compatible(np, compatible) >= 0)
  465. {
  466. break;
  467. }
  468. }
  469. }
  470. return np;
  471. }
  472. struct rt_ofw_node *rt_ofw_find_node_by_ids_r(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids,
  473. const struct rt_ofw_node_id **out_id)
  474. {
  475. struct rt_ofw_node *np = RT_NULL;
  476. if (ids)
  477. {
  478. rt_ofw_foreach_nodes(from, np)
  479. {
  480. struct rt_ofw_node_id *id = rt_ofw_node_match(np, ids);
  481. if (id)
  482. {
  483. if (out_id)
  484. {
  485. *out_id = id;
  486. }
  487. break;
  488. }
  489. }
  490. }
  491. return np;
  492. }
  493. struct rt_ofw_node *rt_ofw_find_node_by_path(const char *path)
  494. {
  495. struct rt_ofw_node *np = RT_NULL, *parent, *tmp = RT_NULL;
  496. if (path)
  497. {
  498. if (!rt_strcmp(path, "/"))
  499. {
  500. np = ofw_node_root;
  501. }
  502. else
  503. {
  504. ++path;
  505. parent = rt_ofw_node_get(ofw_node_root);
  506. while (*path)
  507. {
  508. const char *next = strchrnul(path, '/');
  509. rt_size_t len = next - path;
  510. tmp = RT_NULL;
  511. rt_ofw_foreach_child_node(parent, np)
  512. {
  513. if (!rt_strncmp(np->full_name, path, len))
  514. {
  515. rt_ofw_node_put(parent);
  516. parent = np;
  517. tmp = np;
  518. break;
  519. }
  520. }
  521. if (!tmp)
  522. {
  523. rt_ofw_node_put(parent);
  524. break;
  525. }
  526. path += len + !!*next;
  527. }
  528. np = tmp;
  529. }
  530. rt_ofw_node_get(np);
  531. }
  532. return np;
  533. }
  534. struct rt_ofw_node *rt_ofw_find_node_by_phandle(rt_phandle phandle)
  535. {
  536. struct rt_ofw_node *np = RT_NULL;
  537. if (phandle >= OFW_PHANDLE_MIN && phandle <= OFW_PHANDLE_MAX)
  538. {
  539. /* rebase from zero */
  540. rt_phandle poff = phandle - _phandle_range[0];
  541. np = _phandle_hash[poff];
  542. if (!np)
  543. {
  544. rt_ofw_foreach_allnodes(np)
  545. {
  546. if (np->phandle == phandle)
  547. {
  548. _phandle_hash[poff] = np;
  549. break;
  550. }
  551. }
  552. }
  553. else
  554. {
  555. rt_ofw_node_get(np);
  556. }
  557. }
  558. return np;
  559. }
  560. struct rt_ofw_node *rt_ofw_get_parent(const struct rt_ofw_node *np)
  561. {
  562. if (np)
  563. {
  564. np = rt_ofw_node_get(np->parent);
  565. }
  566. return (struct rt_ofw_node *)np;
  567. }
  568. struct rt_ofw_node *rt_ofw_get_child_by_tag(const struct rt_ofw_node *parent, const char *tag)
  569. {
  570. struct rt_ofw_node *child = RT_NULL;
  571. if (parent && tag)
  572. {
  573. rt_ofw_foreach_child_node(parent, child)
  574. {
  575. if (rt_ofw_node_tag_equ(child, tag))
  576. {
  577. break;
  578. }
  579. }
  580. }
  581. return child;
  582. }
  583. struct rt_ofw_node *rt_ofw_get_child_by_compatible(const struct rt_ofw_node *parent, const char *compatible)
  584. {
  585. struct rt_ofw_node *child = RT_NULL;
  586. if (parent && compatible)
  587. {
  588. rt_ofw_foreach_child_node(parent, child)
  589. {
  590. if (ofw_node_index_of_compatible(child, compatible) >= 0)
  591. {
  592. break;
  593. }
  594. }
  595. }
  596. return child;
  597. }
  598. int rt_ofw_get_child_count(const struct rt_ofw_node *np)
  599. {
  600. int nr;
  601. if (np)
  602. {
  603. struct rt_ofw_node *child;
  604. nr = 0;
  605. rt_ofw_foreach_child_node(np, child)
  606. {
  607. ++nr;
  608. }
  609. }
  610. else
  611. {
  612. nr = -RT_EINVAL;
  613. }
  614. return nr;
  615. }
  616. int rt_ofw_get_available_child_count(const struct rt_ofw_node *np)
  617. {
  618. int nr;
  619. if (np)
  620. {
  621. struct rt_ofw_node *child;
  622. nr = 0;
  623. rt_ofw_foreach_available_child_node(np, child)
  624. {
  625. ++nr;
  626. }
  627. }
  628. else
  629. {
  630. nr = -RT_EINVAL;
  631. }
  632. return nr;
  633. }
  634. struct rt_ofw_node *rt_ofw_get_next_node(struct rt_ofw_node *prev)
  635. {
  636. struct rt_ofw_node *np;
  637. np = rt_ofw_node_get(ofw_get_next_node(prev));
  638. rt_ofw_node_put(prev);
  639. return np;
  640. }
  641. struct rt_ofw_node *rt_ofw_get_next_parent(struct rt_ofw_node *prev)
  642. {
  643. struct rt_ofw_node *next = RT_NULL;
  644. if (prev)
  645. {
  646. next = rt_ofw_node_get(prev->parent);
  647. rt_ofw_node_put(prev);
  648. }
  649. return next;
  650. }
  651. struct rt_ofw_node *rt_ofw_get_next_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
  652. {
  653. struct rt_ofw_node *next = RT_NULL;
  654. if (parent)
  655. {
  656. next = prev ? prev->sibling : parent->child;
  657. rt_ofw_node_put(prev);
  658. rt_ofw_node_get(next);
  659. }
  660. return next;
  661. }
  662. struct rt_ofw_node *rt_ofw_get_next_available_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
  663. {
  664. struct rt_ofw_node *next = RT_NULL;
  665. if (parent)
  666. {
  667. next = prev;
  668. do {
  669. next = rt_ofw_get_next_child(parent, next);
  670. } while (next && !ofw_node_is_available(next));
  671. }
  672. return next;
  673. }
  674. 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))
  675. {
  676. const char *propname = "reg";
  677. struct rt_ofw_node *cpu_np = RT_NULL;
  678. /*
  679. * "reg" (some of the obsolete arch may be other names):
  680. * The value of reg is a <prop-encoded-array> that defines a unique
  681. * CPU/thread id for the CPU/threads represented by the CPU node.
  682. *
  683. * If a CPU supports more than one thread (i.e. multiple streams of
  684. * execution) the reg property is an array with 1 element per thread. The
  685. * #address-cells on the /cpus node specifies how many cells each element
  686. * of the array takes. Software can determine the number of threads by
  687. * dividing the size of reg by the parent node’s #address-cells:
  688. *
  689. * thread-number = reg-cells / address-cells
  690. *
  691. * If a CPU/thread can be the target of an external interrupt the reg
  692. * property value must be a unique CPU/thread id that is addressable by the
  693. * interrupt controller.
  694. *
  695. * If a CPU/thread cannot be the target of an external interrupt, then reg
  696. * must be unique and out of bounds of the range addressed by the interrupt
  697. * controller
  698. *
  699. * If a CPU/thread’s PIR (pending interrupt register) is modifiable, a
  700. * client program should modify PIR to match the reg property value. If PIR
  701. * cannot be modified and the PIR value is distinct from the interrupt
  702. * controller number space, the CPUs binding may define a binding-specific
  703. * representation of PIR values if desired.
  704. */
  705. rt_ofw_foreach_cpu_node(cpu_np)
  706. {
  707. rt_ssize_t prop_len = 0;
  708. rt_bool_t is_end = RT_FALSE;
  709. int tid, addr_cells = rt_ofw_io_addr_cells(cpu_np);
  710. const fdt32_t *cell = rt_ofw_prop_read_raw(cpu_np, propname, &prop_len);
  711. if (!cell && !addr_cells)
  712. {
  713. if (match_cpu_hwid && match_cpu_hwid(cpu, 0))
  714. {
  715. break;
  716. }
  717. continue;
  718. }
  719. if (!match_cpu_hwid)
  720. {
  721. continue;
  722. }
  723. prop_len /= sizeof(*cell) * addr_cells;
  724. for (tid = 0; tid < prop_len; ++tid)
  725. {
  726. rt_uint64_t hwid = rt_fdt_read_number(cell, addr_cells);
  727. if (match_cpu_hwid(cpu, hwid))
  728. {
  729. if (thread)
  730. {
  731. *thread = tid;
  732. }
  733. is_end = RT_TRUE;
  734. break;
  735. }
  736. cell += addr_cells;
  737. }
  738. if (is_end)
  739. {
  740. break;
  741. }
  742. }
  743. return cpu_np;
  744. }
  745. struct rt_ofw_node *rt_ofw_get_next_cpu_node(struct rt_ofw_node *prev)
  746. {
  747. struct rt_ofw_node *cpu_np;
  748. if (prev)
  749. {
  750. cpu_np = prev->sibling;
  751. rt_ofw_node_put(prev);
  752. }
  753. else
  754. {
  755. cpu_np = ofw_node_cpus->child;
  756. }
  757. for (; cpu_np; cpu_np = cpu_np->sibling)
  758. {
  759. if (ofw_node_is_fail(cpu_np))
  760. {
  761. continue;
  762. }
  763. if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
  764. {
  765. continue;
  766. }
  767. if (rt_ofw_node_get(cpu_np))
  768. {
  769. break;
  770. }
  771. }
  772. return cpu_np;
  773. }
  774. struct rt_ofw_node *rt_ofw_get_cpu_state_node(struct rt_ofw_node *cpu_np, int index)
  775. {
  776. struct rt_ofw_cell_args args;
  777. struct rt_ofw_node *np = RT_NULL, *state_np;
  778. rt_err_t err = rt_ofw_parse_phandle_cells(cpu_np, "power-domains", "#power-domain-cells", 0, &args);
  779. if (!err)
  780. {
  781. state_np = rt_ofw_parse_phandle(args.data, "domain-idle-states", index);
  782. rt_ofw_node_put(args.data);
  783. if (state_np)
  784. {
  785. np = state_np;
  786. }
  787. }
  788. if (!np)
  789. {
  790. int count = 0;
  791. rt_uint32_t phandle;
  792. const fdt32_t *cell;
  793. struct rt_ofw_prop *prop;
  794. rt_ofw_foreach_prop_u32(cpu_np, "cpu-idle-states", prop, cell, phandle)
  795. {
  796. if (count == index)
  797. {
  798. np = rt_ofw_find_node_by_phandle((rt_phandle)phandle);
  799. break;
  800. }
  801. ++count;
  802. }
  803. }
  804. return np;
  805. }
  806. rt_uint64_t rt_ofw_get_cpu_id(struct rt_ofw_node *cpu_np)
  807. {
  808. rt_uint64_t cpuid = ~0ULL;
  809. if (cpu_np)
  810. {
  811. rt_uint64_t idx = 0;
  812. struct rt_ofw_node *np = ofw_node_cpus->child;
  813. for (; np; np = np->sibling)
  814. {
  815. if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
  816. {
  817. continue;
  818. }
  819. if (cpu_np == np)
  820. {
  821. cpuid = idx;
  822. break;
  823. }
  824. ++idx;
  825. }
  826. if ((rt_int64_t)cpuid < 0 && !rt_ofw_prop_read_u64(cpu_np, "rt-thread,cpuid", &idx))
  827. {
  828. cpuid = idx;
  829. }
  830. }
  831. return cpuid;
  832. }
  833. rt_uint64_t rt_ofw_get_cpu_hwid(struct rt_ofw_node *cpu_np, unsigned int thread)
  834. {
  835. rt_uint64_t thread_id, hwid = ~0ULL;
  836. if (cpu_np && thread >= 0 && !rt_ofw_get_address(cpu_np, thread, &thread_id, RT_NULL))
  837. {
  838. hwid = thread_id;
  839. }
  840. return hwid;
  841. }
  842. rt_err_t ofw_alias_scan(void)
  843. {
  844. rt_err_t err = RT_EOK;
  845. struct rt_ofw_prop *prop;
  846. struct rt_ofw_node *np = ofw_node_aliases, *tmp;
  847. rt_ofw_foreach_prop(np, prop)
  848. {
  849. int id = 0;
  850. struct alias_info *info;
  851. const char *name = prop->name, *end, *id_start;
  852. /* Maybe the bootloader will set the name, or other nodes reference the aliases */
  853. if (!rt_strcmp(name, "name") || !rt_strcmp(name, "phandle"))
  854. {
  855. continue;
  856. }
  857. if (!(tmp = rt_ofw_find_node_by_path(prop->value)))
  858. {
  859. continue;
  860. }
  861. end = name + rt_strlen(name);
  862. while (*(end - 1) && (*(end - 1) >= '0' && *(end - 1) <= '9') && end > name)
  863. {
  864. --end;
  865. }
  866. id_start = end;
  867. while (*id_start && (*id_start >= '0' && *id_start <= '9'))
  868. {
  869. id *= 10;
  870. id += (*id_start - '0');
  871. ++id_start;
  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;
  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. if (!rt_list_isempty(&_aliases_nodes))
  895. {
  896. rt_list_for_each_entry(info, &_aliases_nodes, list)
  897. {
  898. if (rt_strncmp(info->tag, tag, info->tag_len))
  899. {
  900. continue;
  901. }
  902. if (info->id == id)
  903. {
  904. np = info->np;
  905. break;
  906. }
  907. }
  908. }
  909. }
  910. return np;
  911. }
  912. int ofw_alias_node_id(struct rt_ofw_node *np)
  913. {
  914. int id;
  915. struct alias_info *info = RT_NULL;
  916. if (np)
  917. {
  918. id = -1;
  919. if (!rt_list_isempty(&_aliases_nodes))
  920. {
  921. rt_list_for_each_entry(info, &_aliases_nodes, list)
  922. {
  923. if (info->np == np)
  924. {
  925. id = info->id;
  926. break;
  927. }
  928. }
  929. }
  930. }
  931. else
  932. {
  933. id = -RT_EINVAL;
  934. }
  935. return id;
  936. }
  937. int rt_ofw_get_alias_id(struct rt_ofw_node *np, const char *tag)
  938. {
  939. int id;
  940. struct alias_info *info;
  941. if (np && tag)
  942. {
  943. id = -1;
  944. if (!rt_list_isempty(&_aliases_nodes))
  945. {
  946. rt_list_for_each_entry(info, &_aliases_nodes, list)
  947. {
  948. if (rt_strncmp(info->tag, tag, info->tag_len))
  949. {
  950. continue;
  951. }
  952. if (info->np == np)
  953. {
  954. id = info->id;
  955. break;
  956. }
  957. }
  958. }
  959. }
  960. else
  961. {
  962. id = -RT_EINVAL;
  963. }
  964. return id;
  965. }
  966. int rt_ofw_get_alias_last_id(const char *tag)
  967. {
  968. int id;
  969. struct alias_info *info;
  970. if (tag)
  971. {
  972. id = -1;
  973. if (!rt_list_isempty(&_aliases_nodes))
  974. {
  975. rt_list_for_each_entry(info, &_aliases_nodes, list)
  976. {
  977. if (rt_strncmp(info->tag, tag, info->tag_len))
  978. {
  979. continue;
  980. }
  981. if (info->id > id)
  982. {
  983. id = info->id;
  984. }
  985. }
  986. }
  987. }
  988. else
  989. {
  990. id = -RT_EINVAL;
  991. }
  992. return id;
  993. }
  994. 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,
  995. const fdt32_t *map, rt_ssize_t map_len, struct rt_ofw_node **ref_np, rt_uint32_t *out_id)
  996. {
  997. rt_err_t err = RT_EOK;
  998. rt_uint32_t masked_id, map_mask;
  999. /* Select all bits default */
  1000. map_mask = 0xffffffff;
  1001. if (map_mask_name)
  1002. {
  1003. rt_ofw_prop_read_u32(np, map_mask_name, &map_mask);
  1004. }
  1005. masked_id = map_mask & id;
  1006. for (; map_len > 0; map_len -= 4 * sizeof(*map), map += 4)
  1007. {
  1008. struct rt_ofw_node *phandle_node;
  1009. rt_uint32_t id_base = fdt32_to_cpu(*(map + 0));
  1010. rt_uint32_t phandle = fdt32_to_cpu(*(map + 1));
  1011. rt_uint32_t out_base = fdt32_to_cpu(*(map + 2));
  1012. rt_uint32_t id_len = fdt32_to_cpu(*(map + 3));
  1013. if (id_base & ~map_mask)
  1014. {
  1015. LOG_E("%s: Invalid %s translation - %s(0x%x) for id-base = 0x%x",
  1016. np->full_name, map_name, map_mask_name, map_mask, id_base);
  1017. err = -RT_ERROR;
  1018. break;
  1019. }
  1020. if (masked_id < id_base || masked_id >= id_base + id_len)
  1021. {
  1022. continue;
  1023. }
  1024. phandle_node = rt_ofw_find_node_by_phandle((rt_phandle)phandle);
  1025. if (!phandle_node)
  1026. {
  1027. err = -RT_EEMPTY;
  1028. break;
  1029. }
  1030. if (ref_np)
  1031. {
  1032. if (*ref_np)
  1033. {
  1034. rt_ofw_node_put(phandle_node);
  1035. }
  1036. else
  1037. {
  1038. *ref_np = phandle_node;
  1039. }
  1040. if (*ref_np != phandle_node)
  1041. {
  1042. continue;
  1043. }
  1044. }
  1045. if (out_id)
  1046. {
  1047. *out_id = masked_id - id_base + out_base;
  1048. }
  1049. 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",
  1050. np->full_name, map_name, map_mask_name, map_mask,
  1051. id_base, out_base, id_len, id, masked_id - id_base + out_base);
  1052. break;
  1053. }
  1054. if (map_len <= 0)
  1055. {
  1056. LOG_I("%s: No %s translation for id(0x%x) on %s", np->full_name, map_name,
  1057. id, ref_np && *ref_np ? *ref_np : RT_NULL);
  1058. /* Bypasses translation */
  1059. if (out_id)
  1060. {
  1061. *out_id = id;
  1062. }
  1063. }
  1064. return err;
  1065. }
  1066. 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,
  1067. struct rt_ofw_node **ref_np, rt_uint32_t *out_id)
  1068. {
  1069. rt_err_t err;
  1070. if (np && map_name && (ref_np || out_id))
  1071. {
  1072. rt_ssize_t map_len;
  1073. const fdt32_t *map = rt_ofw_prop_read_raw(np, map_name, &map_len);
  1074. if (!map)
  1075. {
  1076. if (ref_np)
  1077. {
  1078. err = -RT_EEMPTY;
  1079. }
  1080. else
  1081. {
  1082. *out_id = id;
  1083. }
  1084. err = RT_EOK;
  1085. }
  1086. else if (!map_len || map_len % (4 * sizeof(*map)))
  1087. {
  1088. LOG_E("%s: Invalid %s length = %u", np->full_name, map_name, map_len);
  1089. err = -RT_EINVAL;
  1090. }
  1091. else
  1092. {
  1093. err = ofw_map_id(np, id, map_name, map_mask_name, map, map_len, ref_np, out_id);
  1094. }
  1095. }
  1096. else
  1097. {
  1098. err = -RT_EINVAL;
  1099. }
  1100. return err;
  1101. }
  1102. struct rt_ofw_node *rt_ofw_append_child(struct rt_ofw_node *parent, const char *full_name)
  1103. {
  1104. rt_phandle phandle;
  1105. rt_err_t err = RT_EOK;
  1106. fdt32_t *phandle_value;
  1107. struct rt_ofw_node *np = RT_NULL, *child;
  1108. if (full_name)
  1109. {
  1110. if ((phandle = ofw_phandle_next()))
  1111. {
  1112. np = rt_calloc(1, sizeof(*np) + sizeof(*phandle_value));
  1113. }
  1114. }
  1115. if (np)
  1116. {
  1117. parent = parent ? : ofw_node_root;
  1118. np->full_name = full_name;
  1119. np->phandle = phandle;
  1120. np->parent = parent;
  1121. rt_ref_init(&np->ref);
  1122. phandle_value = (void *)np + sizeof(*np);
  1123. *phandle_value = cpu_to_fdt32(phandle);
  1124. err = rt_ofw_append_prop(np, "phandle", sizeof(*phandle_value), phandle_value);
  1125. if (!err)
  1126. {
  1127. if (parent->child)
  1128. {
  1129. rt_ofw_foreach_child_node(parent, child)
  1130. {
  1131. if (!child->sibling)
  1132. {
  1133. child->sibling = np;
  1134. rt_ofw_node_put(child);
  1135. break;
  1136. }
  1137. }
  1138. }
  1139. else
  1140. {
  1141. parent->child = np;
  1142. }
  1143. }
  1144. else
  1145. {
  1146. rt_free(np);
  1147. np = RT_NULL;
  1148. }
  1149. }
  1150. return rt_ofw_node_get(np);
  1151. }
  1152. rt_err_t rt_ofw_append_prop(struct rt_ofw_node *np, const char *name, int length, void *value)
  1153. {
  1154. rt_err_t err = RT_EOK;
  1155. if (np && name && ((length && value) || (!length && !value)))
  1156. {
  1157. struct rt_ofw_prop *prop = rt_malloc(sizeof(*prop)), *last_prop;
  1158. if (prop)
  1159. {
  1160. prop->name = name;
  1161. prop->length = length;
  1162. prop->value = value;
  1163. prop->next = RT_NULL;
  1164. if (np->props)
  1165. {
  1166. rt_ofw_foreach_prop(np, last_prop)
  1167. {
  1168. if (!last_prop->next)
  1169. {
  1170. last_prop->next = prop;
  1171. break;
  1172. }
  1173. }
  1174. }
  1175. else
  1176. {
  1177. np->props = prop;
  1178. }
  1179. }
  1180. else
  1181. {
  1182. err = -RT_ENOMEM;
  1183. }
  1184. }
  1185. else
  1186. {
  1187. err = -RT_EINVAL;
  1188. }
  1189. return err;
  1190. }
  1191. struct rt_ofw_node *rt_ofw_parse_phandle(const struct rt_ofw_node *np, const char *phandle_name, int index)
  1192. {
  1193. struct rt_ofw_cell_args args;
  1194. struct rt_ofw_node *ref_np = RT_NULL;
  1195. if (!rt_ofw_parse_phandle_cells(np, phandle_name, RT_NULL, index, &args))
  1196. {
  1197. ref_np = args.data;
  1198. }
  1199. return ref_np;
  1200. }
  1201. static rt_err_t ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  1202. int index, struct rt_ofw_cell_args *out_args)
  1203. {
  1204. rt_err_t err = -RT_EEMPTY;
  1205. rt_uint32_t value;
  1206. rt_size_t count = 0;
  1207. const fdt32_t *cell;
  1208. struct rt_ofw_prop *prop;
  1209. /*
  1210. * List:
  1211. *
  1212. * phandle1: node1 {
  1213. * #list-cells = <2>;
  1214. * };
  1215. *
  1216. * phandle2: node2 {
  1217. * #list-cells = <1>;
  1218. * };
  1219. *
  1220. * node3 {
  1221. * list = <&phandle1 0xaa 0xbb>, <&phandle2 0xcc>;
  1222. * };
  1223. *
  1224. * if call:
  1225. * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 0, &args):
  1226. *
  1227. * args.data = node1;
  1228. * args.args_count = 2;
  1229. * args.args[0] = 0xaa;
  1230. * args.args[1] = 0xbb;
  1231. *
  1232. * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 1, &args):
  1233. *
  1234. * args.data = node2;
  1235. * args.args_count = 1;
  1236. * args.args[0] = 0xcc;
  1237. */
  1238. rt_ofw_foreach_prop_u32(np, list_name, prop, cell, value)
  1239. {
  1240. rt_uint32_t cells_count = 0;
  1241. struct rt_ofw_node *phandle_np = rt_ofw_find_node_by_phandle((rt_phandle)value);
  1242. /* if phandle node is undefined, we assume that the cels_count is 0 */
  1243. if (cells_name && phandle_np)
  1244. {
  1245. rt_ofw_prop_read_u32(phandle_np, cells_name, &cells_count);
  1246. }
  1247. if (count++ == index)
  1248. {
  1249. for (int idx = 0; idx < cells_count; ++idx)
  1250. {
  1251. cell = rt_ofw_prop_next_u32(prop, cell, &value);
  1252. out_args->args[idx] = value;
  1253. }
  1254. out_args->args_count = cells_count;
  1255. out_args->data = phandle_np;
  1256. if (out_args->data)
  1257. {
  1258. err = RT_EOK;
  1259. }
  1260. break;
  1261. }
  1262. cell += cells_count;
  1263. }
  1264. return err;
  1265. }
  1266. rt_err_t rt_ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  1267. int index, struct rt_ofw_cell_args *out_args)
  1268. {
  1269. rt_err_t err;
  1270. if (np && list_name && index >= 0 && out_args)
  1271. {
  1272. err = ofw_parse_phandle_cells(np, list_name, cells_name, index, out_args);
  1273. }
  1274. else
  1275. {
  1276. err = -RT_EINVAL;
  1277. }
  1278. return err;
  1279. }
  1280. int rt_ofw_count_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name)
  1281. {
  1282. int count;
  1283. if (np && list_name)
  1284. {
  1285. count = -1;
  1286. if (!cells_name)
  1287. {
  1288. rt_ssize_t length;
  1289. if (rt_ofw_get_prop(np, list_name, &length))
  1290. {
  1291. count = length / sizeof(fdt32_t);
  1292. }
  1293. }
  1294. else
  1295. {
  1296. int index = count = 0;
  1297. struct rt_ofw_cell_args args;
  1298. while (!ofw_parse_phandle_cells(np, list_name, cells_name, index, &args))
  1299. {
  1300. ++index;
  1301. ++count;
  1302. }
  1303. }
  1304. }
  1305. else
  1306. {
  1307. count = -RT_EINVAL;
  1308. }
  1309. return count;
  1310. }
  1311. static const char *ofw_get_prop_fuzzy_name(const struct rt_ofw_node *np, const char *name)
  1312. {
  1313. char *sf, split_field[64];
  1314. rt_size_t len = 0, max_ak = 0;
  1315. const char *str, *result = RT_NULL;
  1316. RT_BITMAP_DECLARE(ak, sizeof(split_field)) = {0};
  1317. struct rt_ofw_prop *prop;
  1318. /*
  1319. * List:
  1320. *
  1321. * node {
  1322. * property;
  1323. * front-prop-rear;
  1324. * front-prop;
  1325. * prop-rear;
  1326. * };
  1327. *
  1328. * if call:
  1329. * ofw_get_prop_fuzzy_name(node, name):
  1330. * ["prop"] => property
  1331. * ["-prop"] => front-prop-rear
  1332. * ["prop-"] => front-prop-rear
  1333. * ["-prop$"] => front-prop
  1334. * ["^prop-"] => prop-rear
  1335. * ["-prop-"] => front-prop-rear
  1336. * ["front-*-rear"] => front-prop-rear
  1337. */
  1338. str = name;
  1339. sf = split_field;
  1340. if (str[0] != '^')
  1341. {
  1342. /* As '*' */
  1343. *sf++ = '\0';
  1344. rt_bitmap_set_bit(ak, len++);
  1345. }
  1346. else
  1347. {
  1348. ++str;
  1349. }
  1350. for (; *str && len < sizeof(split_field); ++str, ++sf, ++len)
  1351. {
  1352. if (*str != '*')
  1353. {
  1354. *sf = *str;
  1355. rt_bitmap_clear_bit(ak, len);
  1356. }
  1357. else
  1358. {
  1359. max_ak = len;
  1360. *sf = '\0';
  1361. rt_bitmap_set_bit(ak, len);
  1362. }
  1363. }
  1364. *sf = '\0';
  1365. if (str[-1] != '$')
  1366. {
  1367. /* As '*' */
  1368. max_ak = len;
  1369. rt_bitmap_set_bit(ak, len++);
  1370. }
  1371. else
  1372. {
  1373. sf[-1] = '\0';
  1374. --len;
  1375. }
  1376. sf = split_field;
  1377. if (len >= sizeof(split_field))
  1378. {
  1379. LOG_W("%s fuzzy name = %s len is %d out of %d", np->full_name, name, rt_strlen(name), sizeof(split_field));
  1380. }
  1381. rt_ofw_foreach_prop(np, prop)
  1382. {
  1383. int prep_ak = 0, next_ak, field;
  1384. rt_bool_t match = RT_TRUE;
  1385. const char *propname = prop->name, *fuzzy_name = sf;
  1386. if (!rt_bitmap_test_bit(ak, prep_ak))
  1387. {
  1388. next_ak = rt_bitmap_next_set_bit(ak, prep_ak + 1, max_ak) ? : len;
  1389. field = next_ak - prep_ak;
  1390. if (rt_strncmp(propname, fuzzy_name, field))
  1391. {
  1392. continue;
  1393. }
  1394. propname += field;
  1395. fuzzy_name += field;
  1396. prep_ak = next_ak;
  1397. }
  1398. rt_bitmap_for_each_set_bit_from(ak, prep_ak, next_ak, max_ak)
  1399. {
  1400. /* Skip the '*' */
  1401. if (prep_ak == next_ak)
  1402. {
  1403. ++fuzzy_name;
  1404. next_ak = rt_bitmap_next_set_bit(ak, prep_ak + 1, max_ak);
  1405. }
  1406. if (!(str = rt_strstr(propname, fuzzy_name)))
  1407. {
  1408. match = RT_FALSE;
  1409. break;
  1410. }
  1411. field = next_ak - prep_ak;
  1412. propname = str + field - 1;
  1413. fuzzy_name += field;
  1414. prep_ak = next_ak;
  1415. }
  1416. if (match)
  1417. {
  1418. if ((max_ak || !split_field[0]) && next_ak >= max_ak && len - max_ak > 1)
  1419. {
  1420. if (next_ak == max_ak)
  1421. {
  1422. /* Skip the last '*' */
  1423. ++fuzzy_name;
  1424. }
  1425. if (!(propname = rt_strstr(propname, fuzzy_name)))
  1426. {
  1427. continue;
  1428. }
  1429. /* Check end flag */
  1430. if (propname[len - max_ak - 1] != '\0')
  1431. {
  1432. continue;
  1433. }
  1434. }
  1435. result = prop->name;
  1436. break;
  1437. }
  1438. }
  1439. return result;
  1440. }
  1441. const char *rt_ofw_get_prop_fuzzy_name(const struct rt_ofw_node *np, const char *name)
  1442. {
  1443. const char *propname = RT_NULL;
  1444. if (np && name)
  1445. {
  1446. propname = ofw_get_prop_fuzzy_name(np, name);
  1447. }
  1448. return propname;
  1449. }
  1450. struct rt_ofw_prop *rt_ofw_get_prop(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length)
  1451. {
  1452. struct rt_ofw_prop *prop = RT_NULL;
  1453. if (np && name)
  1454. {
  1455. rt_ofw_foreach_prop(np, prop)
  1456. {
  1457. if (!rt_strcmp(prop->name, name))
  1458. {
  1459. if (out_length)
  1460. {
  1461. *out_length = prop->length;
  1462. }
  1463. break;
  1464. }
  1465. }
  1466. }
  1467. return prop;
  1468. }
  1469. #define OFW_PROP_READ_UXX_ARRAY_INDEX(bit) \
  1470. int rt_ofw_prop_read_u##bit##_array_index( \
  1471. const struct rt_ofw_node *np, const char *propname, \
  1472. int index, int nr, rt_uint##bit##_t *out_values) \
  1473. { \
  1474. int res, max_nr; \
  1475. if (np && propname && index >= 0 && nr >= 0 && out_values) \
  1476. { \
  1477. rt_ssize_t len; \
  1478. const fdt##bit##_t *elm; \
  1479. elm = rt_ofw_prop_read_raw(np, propname, &len); \
  1480. max_nr = len / sizeof(*elm); \
  1481. if (elm && index < max_nr) \
  1482. { \
  1483. elm += index; \
  1484. max_nr -= index; \
  1485. res = nr > max_nr ? max_nr : nr; \
  1486. for (nr = 0; nr < res; ++nr) \
  1487. { \
  1488. *out_values++ = fdt##bit##_to_cpu(*elm++); \
  1489. } \
  1490. } \
  1491. else \
  1492. { \
  1493. res = -RT_EEMPTY; \
  1494. } \
  1495. } \
  1496. else \
  1497. { \
  1498. res = -RT_EINVAL; \
  1499. } \
  1500. return res; \
  1501. }
  1502. OFW_PROP_READ_UXX_ARRAY_INDEX(8)
  1503. OFW_PROP_READ_UXX_ARRAY_INDEX(16)
  1504. OFW_PROP_READ_UXX_ARRAY_INDEX(32)
  1505. OFW_PROP_READ_UXX_ARRAY_INDEX(64)
  1506. #undef OFW_PROP_READ_UXX_ARRAY_INDEX
  1507. int rt_ofw_prop_read_string_array_index(const struct rt_ofw_node *np, const char *propname,
  1508. int index, int nr, const char **out_strings)
  1509. {
  1510. int res = 0;
  1511. if (np && propname && index >= 0 && nr >= 0 && out_strings)
  1512. {
  1513. rt_ssize_t len, slen = 0;
  1514. const char *value = rt_ofw_prop_read_raw(np, propname, &len);
  1515. if (value)
  1516. {
  1517. nr += index;
  1518. for (int idx = 0; idx < nr && len > 0; ++idx)
  1519. {
  1520. /* Add '\0' */
  1521. slen = rt_strlen(value) + 1;
  1522. if (idx >= index)
  1523. {
  1524. *out_strings++ = value;
  1525. ++res;
  1526. }
  1527. len -= slen;
  1528. value += slen;
  1529. }
  1530. }
  1531. else
  1532. {
  1533. res = -RT_EEMPTY;
  1534. }
  1535. }
  1536. else
  1537. {
  1538. res = -RT_EINVAL;
  1539. }
  1540. return res;
  1541. }
  1542. int rt_ofw_prop_count_of_size(const struct rt_ofw_node *np, const char *propname, int size)
  1543. {
  1544. int count;
  1545. if (np && propname && size > 0)
  1546. {
  1547. rt_ssize_t len;
  1548. count = -RT_EEMPTY;
  1549. if (rt_ofw_get_prop(np, propname, &len))
  1550. {
  1551. count = len / size;
  1552. }
  1553. }
  1554. else
  1555. {
  1556. count = -RT_EINVAL;
  1557. }
  1558. return count;
  1559. }
  1560. static rt_int32_t ofw_strcmp(const char *cs, const char *ct)
  1561. {
  1562. return rt_strcmp(cs, ct);
  1563. }
  1564. int rt_ofw_prop_index_of_string(const struct rt_ofw_node *np, const char *propname, const char *string)
  1565. {
  1566. int idx;
  1567. if (np && propname && string)
  1568. {
  1569. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
  1570. idx = -1;
  1571. if (prop)
  1572. {
  1573. idx = ofw_prop_index_of_string(prop, string, ofw_strcmp);
  1574. }
  1575. }
  1576. else
  1577. {
  1578. idx = -RT_EINVAL;
  1579. }
  1580. return idx;
  1581. }
  1582. const fdt32_t *rt_ofw_prop_next_u32(struct rt_ofw_prop *prop, const fdt32_t *cur, rt_uint32_t *out_value)
  1583. {
  1584. if (prop && out_value)
  1585. {
  1586. if (cur)
  1587. {
  1588. ++cur;
  1589. if ((void *)cur >= prop->value + prop->length)
  1590. {
  1591. cur = RT_NULL;
  1592. }
  1593. }
  1594. else
  1595. {
  1596. cur = prop->value;
  1597. }
  1598. if (cur)
  1599. {
  1600. *out_value = fdt32_to_cpu(*cur);
  1601. }
  1602. }
  1603. else
  1604. {
  1605. cur = RT_NULL;
  1606. }
  1607. return cur;
  1608. }
  1609. const char *rt_ofw_prop_next_string(struct rt_ofw_prop *prop, const char *cur)
  1610. {
  1611. if (prop)
  1612. {
  1613. if (cur)
  1614. {
  1615. cur += rt_strlen(cur) + 1;
  1616. if ((void *)cur >= prop->value + prop->length)
  1617. {
  1618. cur = RT_NULL;
  1619. }
  1620. }
  1621. else
  1622. {
  1623. cur = prop->value;
  1624. }
  1625. }
  1626. else
  1627. {
  1628. cur = RT_NULL;
  1629. }
  1630. return cur;
  1631. }