of_access.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Originally from Linux v4.9
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
  13. * Grant Likely.
  14. *
  15. * Modified for U-Boot
  16. * Copyright (c) 2017 Google, Inc
  17. *
  18. * This file follows drivers/of/base.c with functions in the same order as the
  19. * Linux version.
  20. */
  21. #include <common.h>
  22. #include <linux/libfdt.h>
  23. #include <dm/of_access.h>
  24. #include <linux/ctype.h>
  25. #include <linux/err.h>
  26. #include <linux/ioport.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. /* list of struct alias_prop aliases */
  29. LIST_HEAD(aliases_lookup);
  30. /* "/aliaes" node */
  31. static struct device_node *of_aliases;
  32. /* "/chosen" node */
  33. static struct device_node *of_chosen;
  34. /* node pointed to by the stdout-path alias */
  35. static struct device_node *of_stdout;
  36. /* pointer to options given after the alias (separated by :) or NULL if none */
  37. static const char *of_stdout_options;
  38. /**
  39. * struct alias_prop - Alias property in 'aliases' node
  40. *
  41. * The structure represents one alias property of 'aliases' node as
  42. * an entry in aliases_lookup list.
  43. *
  44. * @link: List node to link the structure in aliases_lookup list
  45. * @alias: Alias property name
  46. * @np: Pointer to device_node that the alias stands for
  47. * @id: Index value from end of alias name
  48. * @stem: Alias string without the index
  49. */
  50. struct alias_prop {
  51. struct list_head link;
  52. const char *alias;
  53. struct device_node *np;
  54. int id;
  55. char stem[0];
  56. };
  57. int of_n_addr_cells(const struct device_node *np)
  58. {
  59. const __be32 *ip;
  60. do {
  61. if (np->parent)
  62. np = np->parent;
  63. ip = of_get_property(np, "#address-cells", NULL);
  64. if (ip)
  65. return be32_to_cpup(ip);
  66. } while (np->parent);
  67. /* No #address-cells property for the root node */
  68. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  69. }
  70. int of_n_size_cells(const struct device_node *np)
  71. {
  72. const __be32 *ip;
  73. do {
  74. if (np->parent)
  75. np = np->parent;
  76. ip = of_get_property(np, "#size-cells", NULL);
  77. if (ip)
  78. return be32_to_cpup(ip);
  79. } while (np->parent);
  80. /* No #size-cells property for the root node */
  81. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  82. }
  83. int of_simple_addr_cells(const struct device_node *np)
  84. {
  85. const __be32 *ip;
  86. ip = of_get_property(np, "#address-cells", NULL);
  87. if (ip)
  88. return be32_to_cpup(ip);
  89. /* Return a default of 2 to match fdt_address_cells()*/
  90. return 2;
  91. }
  92. int of_simple_size_cells(const struct device_node *np)
  93. {
  94. const __be32 *ip;
  95. ip = of_get_property(np, "#size-cells", NULL);
  96. if (ip)
  97. return be32_to_cpup(ip);
  98. /* Return a default of 2 to match fdt_size_cells()*/
  99. return 2;
  100. }
  101. struct property *of_find_property(const struct device_node *np,
  102. const char *name, int *lenp)
  103. {
  104. struct property *pp;
  105. if (!np)
  106. return NULL;
  107. for (pp = np->properties; pp; pp = pp->next) {
  108. if (strcmp(pp->name, name) == 0) {
  109. if (lenp)
  110. *lenp = pp->length;
  111. break;
  112. }
  113. }
  114. if (!pp && lenp)
  115. *lenp = -FDT_ERR_NOTFOUND;
  116. return pp;
  117. }
  118. struct device_node *of_find_all_nodes(struct device_node *prev)
  119. {
  120. struct device_node *np;
  121. if (!prev) {
  122. np = gd->of_root;
  123. } else if (prev->child) {
  124. np = prev->child;
  125. } else {
  126. /*
  127. * Walk back up looking for a sibling, or the end of the
  128. * structure
  129. */
  130. np = prev;
  131. while (np->parent && !np->sibling)
  132. np = np->parent;
  133. np = np->sibling; /* Might be null at the end of the tree */
  134. }
  135. return np;
  136. }
  137. const void *of_get_property(const struct device_node *np, const char *name,
  138. int *lenp)
  139. {
  140. struct property *pp = of_find_property(np, name, lenp);
  141. return pp ? pp->value : NULL;
  142. }
  143. static const char *of_prop_next_string(struct property *prop, const char *cur)
  144. {
  145. const void *curv = cur;
  146. if (!prop)
  147. return NULL;
  148. if (!cur)
  149. return prop->value;
  150. curv += strlen(cur) + 1;
  151. if (curv >= prop->value + prop->length)
  152. return NULL;
  153. return curv;
  154. }
  155. int of_device_is_compatible(const struct device_node *device,
  156. const char *compat, const char *type,
  157. const char *name)
  158. {
  159. struct property *prop;
  160. const char *cp;
  161. int index = 0, score = 0;
  162. /* Compatible match has highest priority */
  163. if (compat && compat[0]) {
  164. prop = of_find_property(device, "compatible", NULL);
  165. for (cp = of_prop_next_string(prop, NULL); cp;
  166. cp = of_prop_next_string(prop, cp), index++) {
  167. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  168. score = INT_MAX/2 - (index << 2);
  169. break;
  170. }
  171. }
  172. if (!score)
  173. return 0;
  174. }
  175. /* Matching type is better than matching name */
  176. if (type && type[0]) {
  177. if (!device->type || of_node_cmp(type, device->type))
  178. return 0;
  179. score += 2;
  180. }
  181. /* Matching name is a bit better than not */
  182. if (name && name[0]) {
  183. if (!device->name || of_node_cmp(name, device->name))
  184. return 0;
  185. score++;
  186. }
  187. return score;
  188. }
  189. bool of_device_is_available(const struct device_node *device)
  190. {
  191. const char *status;
  192. int statlen;
  193. if (!device)
  194. return false;
  195. status = of_get_property(device, "status", &statlen);
  196. if (status == NULL)
  197. return true;
  198. if (statlen > 0) {
  199. if (!strcmp(status, "okay"))
  200. return true;
  201. }
  202. return false;
  203. }
  204. struct device_node *of_get_parent(const struct device_node *node)
  205. {
  206. const struct device_node *np;
  207. if (!node)
  208. return NULL;
  209. np = of_node_get(node->parent);
  210. return (struct device_node *)np;
  211. }
  212. static struct device_node *__of_get_next_child(const struct device_node *node,
  213. struct device_node *prev)
  214. {
  215. struct device_node *next;
  216. if (!node)
  217. return NULL;
  218. next = prev ? prev->sibling : node->child;
  219. /*
  220. * coverity[dead_error_line : FALSE]
  221. * Dead code here since our current implementation of of_node_get()
  222. * always returns NULL (Coverity CID 163245). But we leave it as is
  223. * since we may want to implement get/put later.
  224. */
  225. for (; next; next = next->sibling)
  226. if (of_node_get(next))
  227. break;
  228. of_node_put(prev);
  229. return next;
  230. }
  231. #define __for_each_child_of_node(parent, child) \
  232. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  233. child = __of_get_next_child(parent, child))
  234. static struct device_node *__of_find_node_by_path(struct device_node *parent,
  235. const char *path)
  236. {
  237. struct device_node *child;
  238. int len;
  239. len = strcspn(path, "/:");
  240. if (!len)
  241. return NULL;
  242. __for_each_child_of_node(parent, child) {
  243. const char *name = strrchr(child->full_name, '/');
  244. name++;
  245. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  246. return child;
  247. }
  248. return NULL;
  249. }
  250. #define for_each_property_of_node(dn, pp) \
  251. for (pp = dn->properties; pp != NULL; pp = pp->next)
  252. struct device_node *of_find_node_opts_by_path(const char *path,
  253. const char **opts)
  254. {
  255. struct device_node *np = NULL;
  256. struct property *pp;
  257. const char *separator = strchr(path, ':');
  258. if (opts)
  259. *opts = separator ? separator + 1 : NULL;
  260. if (strcmp(path, "/") == 0)
  261. return of_node_get(gd->of_root);
  262. /* The path could begin with an alias */
  263. if (*path != '/') {
  264. int len;
  265. const char *p = separator;
  266. if (!p)
  267. p = strchrnul(path, '/');
  268. len = p - path;
  269. /* of_aliases must not be NULL */
  270. if (!of_aliases)
  271. return NULL;
  272. for_each_property_of_node(of_aliases, pp) {
  273. if (strlen(pp->name) == len && !strncmp(pp->name, path,
  274. len)) {
  275. np = of_find_node_by_path(pp->value);
  276. break;
  277. }
  278. }
  279. if (!np)
  280. return NULL;
  281. path = p;
  282. }
  283. /* Step down the tree matching path components */
  284. if (!np)
  285. np = of_node_get(gd->of_root);
  286. while (np && *path == '/') {
  287. struct device_node *tmp = np;
  288. path++; /* Increment past '/' delimiter */
  289. np = __of_find_node_by_path(np, path);
  290. of_node_put(tmp);
  291. path = strchrnul(path, '/');
  292. if (separator && separator < path)
  293. break;
  294. }
  295. return np;
  296. }
  297. struct device_node *of_find_compatible_node(struct device_node *from,
  298. const char *type, const char *compatible)
  299. {
  300. struct device_node *np;
  301. for_each_of_allnodes_from(from, np)
  302. if (of_device_is_compatible(np, compatible, type, NULL) &&
  303. of_node_get(np))
  304. break;
  305. of_node_put(from);
  306. return np;
  307. }
  308. struct device_node *of_find_node_by_phandle(phandle handle)
  309. {
  310. struct device_node *np;
  311. if (!handle)
  312. return NULL;
  313. for_each_of_allnodes(np)
  314. if (np->phandle == handle)
  315. break;
  316. (void)of_node_get(np);
  317. return np;
  318. }
  319. /**
  320. * of_find_property_value_of_size() - find property of given size
  321. *
  322. * Search for a property in a device node and validate the requested size.
  323. *
  324. * @np: device node from which the property value is to be read.
  325. * @propname: name of the property to be searched.
  326. * @len: requested length of property value
  327. *
  328. * @return the property value on success, -EINVAL if the property does not
  329. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  330. * property data isn't large enough.
  331. */
  332. static void *of_find_property_value_of_size(const struct device_node *np,
  333. const char *propname, u32 len)
  334. {
  335. struct property *prop = of_find_property(np, propname, NULL);
  336. if (!prop)
  337. return ERR_PTR(-EINVAL);
  338. if (!prop->value)
  339. return ERR_PTR(-ENODATA);
  340. if (len > prop->length)
  341. return ERR_PTR(-EOVERFLOW);
  342. return prop->value;
  343. }
  344. int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
  345. {
  346. const __be32 *val;
  347. debug("%s: %s: ", __func__, propname);
  348. if (!np)
  349. return -EINVAL;
  350. val = of_find_property_value_of_size(np, propname, sizeof(*outp));
  351. if (IS_ERR(val)) {
  352. debug("(not found)\n");
  353. return PTR_ERR(val);
  354. }
  355. *outp = be32_to_cpup(val);
  356. debug("%#x (%d)\n", *outp, *outp);
  357. return 0;
  358. }
  359. int of_read_u32_array(const struct device_node *np, const char *propname,
  360. u32 *out_values, size_t sz)
  361. {
  362. const __be32 *val;
  363. debug("%s: %s: ", __func__, propname);
  364. val = of_find_property_value_of_size(np, propname,
  365. sz * sizeof(*out_values));
  366. if (IS_ERR(val))
  367. return PTR_ERR(val);
  368. debug("size %zd\n", sz);
  369. while (sz--)
  370. *out_values++ = be32_to_cpup(val++);
  371. return 0;
  372. }
  373. int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
  374. {
  375. const __be64 *val;
  376. debug("%s: %s: ", __func__, propname);
  377. if (!np)
  378. return -EINVAL;
  379. val = of_find_property_value_of_size(np, propname, sizeof(*outp));
  380. if (IS_ERR(val)) {
  381. debug("(not found)\n");
  382. return PTR_ERR(val);
  383. }
  384. *outp = be64_to_cpup(val);
  385. debug("%#llx (%lld)\n", (unsigned long long)*outp,
  386. (unsigned long long)*outp);
  387. return 0;
  388. }
  389. int of_property_match_string(const struct device_node *np, const char *propname,
  390. const char *string)
  391. {
  392. const struct property *prop = of_find_property(np, propname, NULL);
  393. size_t l;
  394. int i;
  395. const char *p, *end;
  396. if (!prop)
  397. return -EINVAL;
  398. if (!prop->value)
  399. return -ENODATA;
  400. p = prop->value;
  401. end = p + prop->length;
  402. for (i = 0; p < end; i++, p += l) {
  403. l = strnlen(p, end - p) + 1;
  404. if (p + l > end)
  405. return -EILSEQ;
  406. debug("comparing %s with %s\n", string, p);
  407. if (strcmp(string, p) == 0)
  408. return i; /* Found it; return index */
  409. }
  410. return -ENODATA;
  411. }
  412. /**
  413. * of_property_read_string_helper() - Utility helper for parsing string properties
  414. * @np: device node from which the property value is to be read.
  415. * @propname: name of the property to be searched.
  416. * @out_strs: output array of string pointers.
  417. * @sz: number of array elements to read.
  418. * @skip: Number of strings to skip over at beginning of list.
  419. *
  420. * Don't call this function directly. It is a utility helper for the
  421. * of_property_read_string*() family of functions.
  422. */
  423. int of_property_read_string_helper(const struct device_node *np,
  424. const char *propname, const char **out_strs,
  425. size_t sz, int skip)
  426. {
  427. const struct property *prop = of_find_property(np, propname, NULL);
  428. int l = 0, i = 0;
  429. const char *p, *end;
  430. if (!prop)
  431. return -EINVAL;
  432. if (!prop->value)
  433. return -ENODATA;
  434. p = prop->value;
  435. end = p + prop->length;
  436. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  437. l = strnlen(p, end - p) + 1;
  438. if (p + l > end)
  439. return -EILSEQ;
  440. if (out_strs && i >= skip)
  441. *out_strs++ = p;
  442. }
  443. i -= skip;
  444. return i <= 0 ? -ENODATA : i;
  445. }
  446. static int __of_parse_phandle_with_args(const struct device_node *np,
  447. const char *list_name,
  448. const char *cells_name,
  449. int cell_count, int index,
  450. struct of_phandle_args *out_args)
  451. {
  452. const __be32 *list, *list_end;
  453. int rc = 0, cur_index = 0;
  454. uint32_t count = 0;
  455. struct device_node *node = NULL;
  456. phandle phandle;
  457. int size;
  458. /* Retrieve the phandle list property */
  459. list = of_get_property(np, list_name, &size);
  460. if (!list)
  461. return -ENOENT;
  462. list_end = list + size / sizeof(*list);
  463. /* Loop over the phandles until all the requested entry is found */
  464. while (list < list_end) {
  465. rc = -EINVAL;
  466. count = 0;
  467. /*
  468. * If phandle is 0, then it is an empty entry with no
  469. * arguments. Skip forward to the next entry.
  470. */
  471. phandle = be32_to_cpup(list++);
  472. if (phandle) {
  473. /*
  474. * Find the provider node and parse the #*-cells
  475. * property to determine the argument length.
  476. *
  477. * This is not needed if the cell count is hard-coded
  478. * (i.e. cells_name not set, but cell_count is set),
  479. * except when we're going to return the found node
  480. * below.
  481. */
  482. if (cells_name || cur_index == index) {
  483. node = of_find_node_by_phandle(phandle);
  484. if (!node) {
  485. debug("%s: could not find phandle\n",
  486. np->full_name);
  487. goto err;
  488. }
  489. }
  490. if (cells_name) {
  491. if (of_read_u32(node, cells_name, &count)) {
  492. debug("%s: could not get %s for %s\n",
  493. np->full_name, cells_name,
  494. node->full_name);
  495. goto err;
  496. }
  497. } else {
  498. count = cell_count;
  499. }
  500. /*
  501. * Make sure that the arguments actually fit in the
  502. * remaining property data length
  503. */
  504. if (list + count > list_end) {
  505. debug("%s: arguments longer than property\n",
  506. np->full_name);
  507. goto err;
  508. }
  509. }
  510. /*
  511. * All of the error cases above bail out of the loop, so at
  512. * this point, the parsing is successful. If the requested
  513. * index matches, then fill the out_args structure and return,
  514. * or return -ENOENT for an empty entry.
  515. */
  516. rc = -ENOENT;
  517. if (cur_index == index) {
  518. if (!phandle)
  519. goto err;
  520. if (out_args) {
  521. int i;
  522. if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
  523. count = OF_MAX_PHANDLE_ARGS;
  524. out_args->np = node;
  525. out_args->args_count = count;
  526. for (i = 0; i < count; i++)
  527. out_args->args[i] =
  528. be32_to_cpup(list++);
  529. } else {
  530. of_node_put(node);
  531. }
  532. /* Found it! return success */
  533. return 0;
  534. }
  535. of_node_put(node);
  536. node = NULL;
  537. list += count;
  538. cur_index++;
  539. }
  540. /*
  541. * Unlock node before returning result; will be one of:
  542. * -ENOENT : index is for empty phandle
  543. * -EINVAL : parsing error on data
  544. * [1..n] : Number of phandle (count mode; when index = -1)
  545. */
  546. rc = index < 0 ? cur_index : -ENOENT;
  547. err:
  548. if (node)
  549. of_node_put(node);
  550. return rc;
  551. }
  552. struct device_node *of_parse_phandle(const struct device_node *np,
  553. const char *phandle_name, int index)
  554. {
  555. struct of_phandle_args args;
  556. if (index < 0)
  557. return NULL;
  558. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
  559. &args))
  560. return NULL;
  561. return args.np;
  562. }
  563. int of_parse_phandle_with_args(const struct device_node *np,
  564. const char *list_name, const char *cells_name,
  565. int index, struct of_phandle_args *out_args)
  566. {
  567. if (index < 0)
  568. return -EINVAL;
  569. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  570. index, out_args);
  571. }
  572. int of_count_phandle_with_args(const struct device_node *np,
  573. const char *list_name, const char *cells_name)
  574. {
  575. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  576. -1, NULL);
  577. }
  578. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  579. int id, const char *stem, int stem_len)
  580. {
  581. ap->np = np;
  582. ap->id = id;
  583. strncpy(ap->stem, stem, stem_len);
  584. ap->stem[stem_len] = 0;
  585. list_add_tail(&ap->link, &aliases_lookup);
  586. debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  587. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  588. }
  589. int of_alias_scan(void)
  590. {
  591. struct property *pp;
  592. of_aliases = of_find_node_by_path("/aliases");
  593. of_chosen = of_find_node_by_path("/chosen");
  594. if (of_chosen == NULL)
  595. of_chosen = of_find_node_by_path("/chosen@0");
  596. if (of_chosen) {
  597. const char *name;
  598. name = of_get_property(of_chosen, "stdout-path", NULL);
  599. if (name)
  600. of_stdout = of_find_node_opts_by_path(name,
  601. &of_stdout_options);
  602. }
  603. if (!of_aliases)
  604. return 0;
  605. for_each_property_of_node(of_aliases, pp) {
  606. const char *start = pp->name;
  607. const char *end = start + strlen(start);
  608. struct device_node *np;
  609. struct alias_prop *ap;
  610. ulong id;
  611. int len;
  612. /* Skip those we do not want to proceed */
  613. if (!strcmp(pp->name, "name") ||
  614. !strcmp(pp->name, "phandle") ||
  615. !strcmp(pp->name, "linux,phandle"))
  616. continue;
  617. np = of_find_node_by_path(pp->value);
  618. if (!np)
  619. continue;
  620. /*
  621. * walk the alias backwards to extract the id and work out
  622. * the 'stem' string
  623. */
  624. while (isdigit(*(end-1)) && end > start)
  625. end--;
  626. len = end - start;
  627. if (strict_strtoul(end, 10, &id) < 0)
  628. continue;
  629. /* Allocate an alias_prop with enough space for the stem */
  630. ap = malloc(sizeof(*ap) + len + 1);
  631. if (!ap)
  632. return -ENOMEM;
  633. memset(ap, 0, sizeof(*ap) + len + 1);
  634. ap->alias = start;
  635. of_alias_add(ap, np, id, start, len);
  636. }
  637. return 0;
  638. }
  639. int of_alias_get_id(const struct device_node *np, const char *stem)
  640. {
  641. struct alias_prop *app;
  642. int id = -ENODEV;
  643. mutex_lock(&of_mutex);
  644. list_for_each_entry(app, &aliases_lookup, link) {
  645. if (strcmp(app->stem, stem) != 0)
  646. continue;
  647. if (np == app->np) {
  648. id = app->id;
  649. break;
  650. }
  651. }
  652. mutex_unlock(&of_mutex);
  653. return id;
  654. }
  655. struct device_node *of_get_stdout(void)
  656. {
  657. return of_stdout;
  658. }