of_access.c 17 KB

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