of_access.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 <log.h>
  23. #include <malloc.h>
  24. #include <asm/global_data.h>
  25. #include <linux/bug.h>
  26. #include <linux/libfdt.h>
  27. #include <dm/of_access.h>
  28. #include <linux/ctype.h>
  29. #include <linux/err.h>
  30. #include <linux/ioport.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. /* list of struct alias_prop aliases */
  33. static LIST_HEAD(aliases_lookup);
  34. /* "/aliaes" node */
  35. static struct device_node *of_aliases;
  36. /* "/chosen" node */
  37. static struct device_node *of_chosen;
  38. /* node pointed to by the stdout-path alias */
  39. static struct device_node *of_stdout;
  40. /* pointer to options given after the alias (separated by :) or NULL if none */
  41. static const char *of_stdout_options;
  42. /**
  43. * struct alias_prop - Alias property in 'aliases' node
  44. *
  45. * The structure represents one alias property of 'aliases' node as
  46. * an entry in aliases_lookup list.
  47. *
  48. * @link: List node to link the structure in aliases_lookup list
  49. * @alias: Alias property name
  50. * @np: Pointer to device_node that the alias stands for
  51. * @id: Index value from end of alias name
  52. * @stem: Alias string without the index
  53. */
  54. struct alias_prop {
  55. struct list_head link;
  56. const char *alias;
  57. struct device_node *np;
  58. int id;
  59. char stem[0];
  60. };
  61. int of_n_addr_cells(const struct device_node *np)
  62. {
  63. const __be32 *ip;
  64. do {
  65. if (np->parent)
  66. np = np->parent;
  67. ip = of_get_property(np, "#address-cells", NULL);
  68. if (ip)
  69. return be32_to_cpup(ip);
  70. } while (np->parent);
  71. /* No #address-cells property for the root node */
  72. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  73. }
  74. int of_n_size_cells(const struct device_node *np)
  75. {
  76. const __be32 *ip;
  77. do {
  78. if (np->parent)
  79. np = np->parent;
  80. ip = of_get_property(np, "#size-cells", NULL);
  81. if (ip)
  82. return be32_to_cpup(ip);
  83. } while (np->parent);
  84. /* No #size-cells property for the root node */
  85. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  86. }
  87. int of_simple_addr_cells(const struct device_node *np)
  88. {
  89. const __be32 *ip;
  90. ip = of_get_property(np, "#address-cells", NULL);
  91. if (ip)
  92. return be32_to_cpup(ip);
  93. /* Return a default of 2 to match fdt_address_cells()*/
  94. return 2;
  95. }
  96. int of_simple_size_cells(const struct device_node *np)
  97. {
  98. const __be32 *ip;
  99. ip = of_get_property(np, "#size-cells", NULL);
  100. if (ip)
  101. return be32_to_cpup(ip);
  102. /* Return a default of 2 to match fdt_size_cells()*/
  103. return 2;
  104. }
  105. struct property *of_find_property(const struct device_node *np,
  106. const char *name, int *lenp)
  107. {
  108. struct property *pp;
  109. if (!np)
  110. return NULL;
  111. for (pp = np->properties; pp; pp = pp->next) {
  112. if (strcmp(pp->name, name) == 0) {
  113. if (lenp)
  114. *lenp = pp->length;
  115. break;
  116. }
  117. }
  118. if (!pp && lenp)
  119. *lenp = -FDT_ERR_NOTFOUND;
  120. return pp;
  121. }
  122. struct device_node *of_find_all_nodes(struct device_node *prev)
  123. {
  124. struct device_node *np;
  125. if (!prev) {
  126. np = gd->of_root;
  127. } else if (prev->child) {
  128. np = prev->child;
  129. } else {
  130. /*
  131. * Walk back up looking for a sibling, or the end of the
  132. * structure
  133. */
  134. np = prev;
  135. while (np->parent && !np->sibling)
  136. np = np->parent;
  137. np = np->sibling; /* Might be null at the end of the tree */
  138. }
  139. return np;
  140. }
  141. const void *of_get_property(const struct device_node *np, const char *name,
  142. int *lenp)
  143. {
  144. struct property *pp = of_find_property(np, name, lenp);
  145. return pp ? pp->value : NULL;
  146. }
  147. const struct property *of_get_first_property(const struct device_node *np)
  148. {
  149. if (!np)
  150. return NULL;
  151. return np->properties;
  152. }
  153. const struct property *of_get_next_property(const struct device_node *np,
  154. const struct property *property)
  155. {
  156. if (!np)
  157. return NULL;
  158. return property->next;
  159. }
  160. const void *of_get_property_by_prop(const struct device_node *np,
  161. const struct property *property,
  162. const char **name,
  163. int *lenp)
  164. {
  165. if (!np || !property)
  166. return NULL;
  167. if (name)
  168. *name = property->name;
  169. if (lenp)
  170. *lenp = property->length;
  171. return property->value;
  172. }
  173. static const char *of_prop_next_string(struct property *prop, const char *cur)
  174. {
  175. const void *curv = cur;
  176. if (!prop)
  177. return NULL;
  178. if (!cur)
  179. return prop->value;
  180. curv += strlen(cur) + 1;
  181. if (curv >= prop->value + prop->length)
  182. return NULL;
  183. return curv;
  184. }
  185. int of_device_is_compatible(const struct device_node *device,
  186. const char *compat, const char *type,
  187. const char *name)
  188. {
  189. struct property *prop;
  190. const char *cp;
  191. int index = 0, score = 0;
  192. /* Compatible match has highest priority */
  193. if (compat && compat[0]) {
  194. prop = of_find_property(device, "compatible", NULL);
  195. for (cp = of_prop_next_string(prop, NULL); cp;
  196. cp = of_prop_next_string(prop, cp), index++) {
  197. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  198. score = INT_MAX/2 - (index << 2);
  199. break;
  200. }
  201. }
  202. if (!score)
  203. return 0;
  204. }
  205. /* Matching type is better than matching name */
  206. if (type && type[0]) {
  207. if (!device->type || of_node_cmp(type, device->type))
  208. return 0;
  209. score += 2;
  210. }
  211. /* Matching name is a bit better than not */
  212. if (name && name[0]) {
  213. if (!device->name || of_node_cmp(name, device->name))
  214. return 0;
  215. score++;
  216. }
  217. return score;
  218. }
  219. bool of_device_is_available(const struct device_node *device)
  220. {
  221. const char *status;
  222. int statlen;
  223. if (!device)
  224. return false;
  225. status = of_get_property(device, "status", &statlen);
  226. if (status == NULL)
  227. return true;
  228. if (statlen > 0) {
  229. if (!strcmp(status, "okay"))
  230. return true;
  231. }
  232. return false;
  233. }
  234. struct device_node *of_get_parent(const struct device_node *node)
  235. {
  236. const struct device_node *np;
  237. if (!node)
  238. return NULL;
  239. np = of_node_get(node->parent);
  240. return (struct device_node *)np;
  241. }
  242. static struct device_node *__of_get_next_child(const struct device_node *node,
  243. struct device_node *prev)
  244. {
  245. struct device_node *next;
  246. if (!node)
  247. return NULL;
  248. next = prev ? prev->sibling : node->child;
  249. /*
  250. * coverity[dead_error_line : FALSE]
  251. * Dead code here since our current implementation of of_node_get()
  252. * always returns NULL (Coverity CID 163245). But we leave it as is
  253. * since we may want to implement get/put later.
  254. */
  255. for (; next; next = next->sibling)
  256. if (of_node_get(next))
  257. break;
  258. of_node_put(prev);
  259. return next;
  260. }
  261. #define __for_each_child_of_node(parent, child) \
  262. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  263. child = __of_get_next_child(parent, child))
  264. static struct device_node *__of_find_node_by_path(struct device_node *parent,
  265. const char *path)
  266. {
  267. struct device_node *child;
  268. int len;
  269. len = strcspn(path, "/:");
  270. if (!len)
  271. return NULL;
  272. __for_each_child_of_node(parent, child) {
  273. const char *name = strrchr(child->full_name, '/');
  274. name++;
  275. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  276. return child;
  277. }
  278. return NULL;
  279. }
  280. #define for_each_property_of_node(dn, pp) \
  281. for (pp = dn->properties; pp != NULL; pp = pp->next)
  282. struct device_node *of_find_node_opts_by_path(struct device_node *root,
  283. const char *path,
  284. const char **opts)
  285. {
  286. struct device_node *np = NULL;
  287. struct property *pp;
  288. const char *separator = strchr(path, ':');
  289. if (!root)
  290. root = gd->of_root;
  291. if (opts)
  292. *opts = separator ? separator + 1 : NULL;
  293. if (strcmp(path, "/") == 0)
  294. return of_node_get(root);
  295. /* The path could begin with an alias */
  296. if (*path != '/') {
  297. int len;
  298. const char *p = separator;
  299. /* Only allow alias processing on the control FDT */
  300. if (root != gd->of_root)
  301. return NULL;
  302. if (!p)
  303. p = strchrnul(path, '/');
  304. len = p - path;
  305. /* of_aliases must not be NULL */
  306. if (!of_aliases)
  307. return NULL;
  308. for_each_property_of_node(of_aliases, pp) {
  309. if (strlen(pp->name) == len && !strncmp(pp->name, path,
  310. len)) {
  311. np = of_find_node_by_path(pp->value);
  312. break;
  313. }
  314. }
  315. if (!np)
  316. return NULL;
  317. path = p;
  318. }
  319. /* Step down the tree matching path components */
  320. if (!np)
  321. np = of_node_get(root);
  322. while (np && *path == '/') {
  323. struct device_node *tmp = np;
  324. path++; /* Increment past '/' delimiter */
  325. np = __of_find_node_by_path(np, path);
  326. of_node_put(tmp);
  327. path = strchrnul(path, '/');
  328. if (separator && separator < path)
  329. break;
  330. }
  331. return np;
  332. }
  333. struct device_node *of_find_compatible_node(struct device_node *from,
  334. const char *type, const char *compatible)
  335. {
  336. struct device_node *np;
  337. for_each_of_allnodes_from(from, np)
  338. if (of_device_is_compatible(np, compatible, type, NULL) &&
  339. of_node_get(np))
  340. break;
  341. of_node_put(from);
  342. return np;
  343. }
  344. static int of_device_has_prop_value(const struct device_node *device,
  345. const char *propname, const void *propval,
  346. int proplen)
  347. {
  348. struct property *prop = of_find_property(device, propname, NULL);
  349. if (!prop || !prop->value || prop->length != proplen)
  350. return 0;
  351. return !memcmp(prop->value, propval, proplen);
  352. }
  353. struct device_node *of_find_node_by_prop_value(struct device_node *from,
  354. const char *propname,
  355. const void *propval, int proplen)
  356. {
  357. struct device_node *np;
  358. for_each_of_allnodes_from(from, np) {
  359. if (of_device_has_prop_value(np, propname, propval, proplen) &&
  360. of_node_get(np))
  361. break;
  362. }
  363. of_node_put(from);
  364. return np;
  365. }
  366. struct device_node *of_find_node_by_phandle(struct device_node *root,
  367. phandle handle)
  368. {
  369. struct device_node *np;
  370. if (!handle)
  371. return NULL;
  372. for_each_of_allnodes_from(root, np)
  373. if (np->phandle == handle)
  374. break;
  375. (void)of_node_get(np);
  376. return np;
  377. }
  378. /**
  379. * of_find_property_value_of_size() - find property of given size
  380. *
  381. * Search for a property in a device node and validate the requested size.
  382. *
  383. * @np: device node from which the property value is to be read.
  384. * @propname: name of the property to be searched.
  385. * @len: requested length of property value
  386. *
  387. * Return: the property value on success, -EINVAL if the property does not
  388. * exist and -EOVERFLOW if the property data isn't large enough.
  389. */
  390. static void *of_find_property_value_of_size(const struct device_node *np,
  391. const char *propname, u32 len)
  392. {
  393. struct property *prop = of_find_property(np, propname, NULL);
  394. if (!prop)
  395. return ERR_PTR(-EINVAL);
  396. if (len > prop->length)
  397. return ERR_PTR(-EOVERFLOW);
  398. return prop->value;
  399. }
  400. int of_read_u8(const struct device_node *np, const char *propname, u8 *outp)
  401. {
  402. const u8 *val;
  403. debug("%s: %s: ", __func__, propname);
  404. if (!np)
  405. return -EINVAL;
  406. val = of_find_property_value_of_size(np, propname, sizeof(*outp));
  407. if (IS_ERR(val)) {
  408. debug("(not found)\n");
  409. return PTR_ERR(val);
  410. }
  411. *outp = *val;
  412. debug("%#x (%d)\n", *outp, *outp);
  413. return 0;
  414. }
  415. int of_read_u16(const struct device_node *np, const char *propname, u16 *outp)
  416. {
  417. const __be16 *val;
  418. debug("%s: %s: ", __func__, propname);
  419. if (!np)
  420. return -EINVAL;
  421. val = of_find_property_value_of_size(np, propname, sizeof(*outp));
  422. if (IS_ERR(val)) {
  423. debug("(not found)\n");
  424. return PTR_ERR(val);
  425. }
  426. *outp = be16_to_cpup(val);
  427. debug("%#x (%d)\n", *outp, *outp);
  428. return 0;
  429. }
  430. int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
  431. {
  432. return of_read_u32_index(np, propname, 0, outp);
  433. }
  434. int of_read_u32_array(const struct device_node *np, const char *propname,
  435. u32 *out_values, size_t sz)
  436. {
  437. const __be32 *val;
  438. debug("%s: %s: ", __func__, propname);
  439. val = of_find_property_value_of_size(np, propname,
  440. sz * sizeof(*out_values));
  441. if (IS_ERR(val))
  442. return PTR_ERR(val);
  443. debug("size %zd\n", sz);
  444. while (sz--)
  445. *out_values++ = be32_to_cpup(val++);
  446. return 0;
  447. }
  448. int of_read_u32_index(const struct device_node *np, const char *propname,
  449. int index, u32 *outp)
  450. {
  451. const __be32 *val;
  452. debug("%s: %s: ", __func__, propname);
  453. if (!np)
  454. return -EINVAL;
  455. val = of_find_property_value_of_size(np, propname,
  456. sizeof(*outp) * (index + 1));
  457. if (IS_ERR(val)) {
  458. debug("(not found)\n");
  459. return PTR_ERR(val);
  460. }
  461. *outp = be32_to_cpup(val + index);
  462. debug("%#x (%d)\n", *outp, *outp);
  463. return 0;
  464. }
  465. int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
  466. {
  467. const __be64 *val;
  468. debug("%s: %s: ", __func__, propname);
  469. if (!np)
  470. return -EINVAL;
  471. val = of_find_property_value_of_size(np, propname, sizeof(*outp));
  472. if (IS_ERR(val)) {
  473. debug("(not found)\n");
  474. return PTR_ERR(val);
  475. }
  476. *outp = be64_to_cpup(val);
  477. debug("%#llx (%lld)\n", (unsigned long long)*outp,
  478. (unsigned long long)*outp);
  479. return 0;
  480. }
  481. int of_property_match_string(const struct device_node *np, const char *propname,
  482. const char *string)
  483. {
  484. int len = 0;
  485. const struct property *prop = of_find_property(np, propname, &len);
  486. size_t l;
  487. int i;
  488. const char *p, *end;
  489. if (!prop && len == -FDT_ERR_NOTFOUND)
  490. return -ENOENT;
  491. if (!prop)
  492. return -EINVAL;
  493. if (!prop->value)
  494. return -ENODATA;
  495. p = prop->value;
  496. end = p + prop->length;
  497. for (i = 0; p < end; i++, p += l) {
  498. l = strnlen(p, end - p) + 1;
  499. if (p + l > end)
  500. return -EILSEQ;
  501. debug("comparing %s with %s\n", string, p);
  502. if (strcmp(string, p) == 0)
  503. return i; /* Found it; return index */
  504. }
  505. return -ENODATA;
  506. }
  507. /**
  508. * of_property_read_string_helper() - Utility helper for parsing string properties
  509. * @np: device node from which the property value is to be read.
  510. * @propname: name of the property to be searched.
  511. * @out_strs: output array of string pointers.
  512. * @sz: number of array elements to read.
  513. * @skip: Number of strings to skip over at beginning of list (cannot be
  514. * negative)
  515. *
  516. * Don't call this function directly. It is a utility helper for the
  517. * of_property_read_string*() family of functions.
  518. */
  519. int of_property_read_string_helper(const struct device_node *np,
  520. const char *propname, const char **out_strs,
  521. size_t sz, int skip)
  522. {
  523. const struct property *prop = of_find_property(np, propname, NULL);
  524. int l = 0, i = 0;
  525. const char *p, *end;
  526. if (!prop)
  527. return -EINVAL;
  528. if (!prop->value)
  529. return -ENODATA;
  530. p = prop->value;
  531. end = p + prop->length;
  532. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  533. l = strnlen(p, end - p) + 1;
  534. if (p + l > end)
  535. return -EILSEQ;
  536. if (out_strs && i >= skip)
  537. *out_strs++ = p;
  538. }
  539. i -= skip;
  540. return i <= 0 ? -ENODATA : i;
  541. }
  542. static int __of_parse_phandle_with_args(const struct device_node *np,
  543. const char *list_name,
  544. const char *cells_name,
  545. int cell_count, int index,
  546. struct of_phandle_args *out_args)
  547. {
  548. const __be32 *list, *list_end;
  549. int rc = 0, cur_index = 0;
  550. uint32_t count;
  551. struct device_node *node = NULL;
  552. phandle phandle;
  553. int size;
  554. /* Retrieve the phandle list property */
  555. list = of_get_property(np, list_name, &size);
  556. if (!list)
  557. return -ENOENT;
  558. list_end = list + size / sizeof(*list);
  559. /* Loop over the phandles until all the requested entry is found */
  560. while (list < list_end) {
  561. rc = -EINVAL;
  562. count = 0;
  563. /*
  564. * If phandle is 0, then it is an empty entry with no
  565. * arguments. Skip forward to the next entry.
  566. */
  567. phandle = be32_to_cpup(list++);
  568. if (phandle) {
  569. /*
  570. * Find the provider node and parse the #*-cells
  571. * property to determine the argument length.
  572. *
  573. * This is not needed if the cell count is hard-coded
  574. * (i.e. cells_name not set, but cell_count is set),
  575. * except when we're going to return the found node
  576. * below.
  577. */
  578. if (cells_name || cur_index == index) {
  579. node = of_find_node_by_phandle(NULL, phandle);
  580. if (!node) {
  581. debug("%s: could not find phandle\n",
  582. np->full_name);
  583. goto err;
  584. }
  585. }
  586. if (cells_name) {
  587. if (of_read_u32(node, cells_name, &count)) {
  588. debug("%s: could not get %s for %s\n",
  589. np->full_name, cells_name,
  590. node->full_name);
  591. goto err;
  592. }
  593. } else {
  594. count = cell_count;
  595. }
  596. /*
  597. * Make sure that the arguments actually fit in the
  598. * remaining property data length
  599. */
  600. if (list + count > list_end) {
  601. debug("%s: arguments longer than property\n",
  602. np->full_name);
  603. goto err;
  604. }
  605. }
  606. /*
  607. * All of the error cases above bail out of the loop, so at
  608. * this point, the parsing is successful. If the requested
  609. * index matches, then fill the out_args structure and return,
  610. * or return -ENOENT for an empty entry.
  611. */
  612. rc = -ENOENT;
  613. if (cur_index == index) {
  614. if (!phandle)
  615. goto err;
  616. if (out_args) {
  617. int i;
  618. if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
  619. count = OF_MAX_PHANDLE_ARGS;
  620. out_args->np = node;
  621. out_args->args_count = count;
  622. for (i = 0; i < count; i++)
  623. out_args->args[i] =
  624. be32_to_cpup(list++);
  625. } else {
  626. of_node_put(node);
  627. }
  628. /* Found it! return success */
  629. return 0;
  630. }
  631. of_node_put(node);
  632. node = NULL;
  633. list += count;
  634. cur_index++;
  635. }
  636. /*
  637. * Unlock node before returning result; will be one of:
  638. * -ENOENT : index is for empty phandle
  639. * -EINVAL : parsing error on data
  640. * [1..n] : Number of phandle (count mode; when index = -1)
  641. */
  642. rc = index < 0 ? cur_index : -ENOENT;
  643. err:
  644. if (node)
  645. of_node_put(node);
  646. return rc;
  647. }
  648. struct device_node *of_parse_phandle(const struct device_node *np,
  649. const char *phandle_name, int index)
  650. {
  651. struct of_phandle_args args;
  652. if (index < 0)
  653. return NULL;
  654. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
  655. &args))
  656. return NULL;
  657. return args.np;
  658. }
  659. int of_parse_phandle_with_args(const struct device_node *np,
  660. const char *list_name, const char *cells_name,
  661. int cell_count, int index,
  662. struct of_phandle_args *out_args)
  663. {
  664. if (index < 0)
  665. return -EINVAL;
  666. return __of_parse_phandle_with_args(np, list_name, cells_name,
  667. cell_count, index, out_args);
  668. }
  669. int of_count_phandle_with_args(const struct device_node *np,
  670. const char *list_name, const char *cells_name,
  671. int cell_count)
  672. {
  673. return __of_parse_phandle_with_args(np, list_name, cells_name,
  674. cell_count, -1, NULL);
  675. }
  676. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  677. int id, const char *stem, int stem_len)
  678. {
  679. ap->np = np;
  680. ap->id = id;
  681. strncpy(ap->stem, stem, stem_len);
  682. ap->stem[stem_len] = 0;
  683. list_add_tail(&ap->link, &aliases_lookup);
  684. debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  685. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  686. }
  687. int of_alias_scan(void)
  688. {
  689. struct property *pp;
  690. of_aliases = of_find_node_by_path("/aliases");
  691. of_chosen = of_find_node_by_path("/chosen");
  692. if (of_chosen == NULL)
  693. of_chosen = of_find_node_by_path("/chosen@0");
  694. if (of_chosen) {
  695. const char *name;
  696. name = of_get_property(of_chosen, "stdout-path", NULL);
  697. if (name)
  698. of_stdout = of_find_node_opts_by_path(NULL, name,
  699. &of_stdout_options);
  700. }
  701. if (!of_aliases)
  702. return 0;
  703. for_each_property_of_node(of_aliases, pp) {
  704. const char *start = pp->name;
  705. const char *end = start + strlen(start);
  706. struct device_node *np;
  707. struct alias_prop *ap;
  708. ulong id;
  709. int len;
  710. /* Skip those we do not want to proceed */
  711. if (!strcmp(pp->name, "name") ||
  712. !strcmp(pp->name, "phandle") ||
  713. !strcmp(pp->name, "linux,phandle"))
  714. continue;
  715. np = of_find_node_by_path(pp->value);
  716. if (!np)
  717. continue;
  718. /*
  719. * walk the alias backwards to extract the id and work out
  720. * the 'stem' string
  721. */
  722. while (isdigit(*(end-1)) && end > start)
  723. end--;
  724. len = end - start;
  725. if (strict_strtoul(end, 10, &id) < 0)
  726. continue;
  727. /* Allocate an alias_prop with enough space for the stem */
  728. ap = malloc(sizeof(*ap) + len + 1);
  729. if (!ap)
  730. return -ENOMEM;
  731. memset(ap, 0, sizeof(*ap) + len + 1);
  732. ap->alias = start;
  733. of_alias_add(ap, np, id, start, len);
  734. }
  735. return 0;
  736. }
  737. int of_alias_get_id(const struct device_node *np, const char *stem)
  738. {
  739. struct alias_prop *app;
  740. int id = -ENODEV;
  741. mutex_lock(&of_mutex);
  742. list_for_each_entry(app, &aliases_lookup, link) {
  743. if (strcmp(app->stem, stem) != 0)
  744. continue;
  745. if (np == app->np) {
  746. id = app->id;
  747. break;
  748. }
  749. }
  750. mutex_unlock(&of_mutex);
  751. return id;
  752. }
  753. int of_alias_get_highest_id(const char *stem)
  754. {
  755. struct alias_prop *app;
  756. int id = -1;
  757. mutex_lock(&of_mutex);
  758. list_for_each_entry(app, &aliases_lookup, link) {
  759. if (strcmp(app->stem, stem) != 0)
  760. continue;
  761. if (app->id > id)
  762. id = app->id;
  763. }
  764. mutex_unlock(&of_mutex);
  765. return id;
  766. }
  767. struct device_node *of_get_stdout(void)
  768. {
  769. return of_stdout;
  770. }
  771. int of_write_prop(struct device_node *np, const char *propname, int len,
  772. const void *value)
  773. {
  774. struct property *pp;
  775. struct property *pp_last = NULL;
  776. struct property *new;
  777. if (!np)
  778. return -EINVAL;
  779. for (pp = np->properties; pp; pp = pp->next) {
  780. if (strcmp(pp->name, propname) == 0) {
  781. /* Property exists -> change value */
  782. pp->value = (void *)value;
  783. pp->length = len;
  784. return 0;
  785. }
  786. pp_last = pp;
  787. }
  788. /* Property does not exist -> append new property */
  789. new = malloc(sizeof(struct property));
  790. if (!new)
  791. return -ENOMEM;
  792. new->name = strdup(propname);
  793. if (!new->name) {
  794. free(new);
  795. return -ENOMEM;
  796. }
  797. new->value = (void *)value;
  798. new->length = len;
  799. new->next = NULL;
  800. if (pp_last)
  801. pp_last->next = new;
  802. else
  803. np->properties = new;
  804. return 0;
  805. }
  806. int of_add_subnode(struct device_node *parent, const char *name, int len,
  807. struct device_node **childp)
  808. {
  809. struct device_node *child, *new, *last_sibling = NULL;
  810. char *new_name, *full_name;
  811. int parent_fnl;
  812. if (len == -1)
  813. len = strlen(name);
  814. __for_each_child_of_node(parent, child) {
  815. /*
  816. * make sure we don't use a child called "trevor" when we are
  817. * searching for "trev".
  818. */
  819. if (!strncmp(child->name, name, len) && strlen(name) == len) {
  820. *childp = child;
  821. return -EEXIST;
  822. }
  823. last_sibling = child;
  824. }
  825. /* Subnode does not exist -> append new subnode */
  826. new = calloc(1, sizeof(struct device_node));
  827. if (!new)
  828. return -ENOMEM;
  829. new_name = memdup(name, len + 1);
  830. if (!new_name) {
  831. free(new);
  832. return -ENOMEM;
  833. }
  834. new_name[len] = '\0';
  835. /*
  836. * if the parent is the root node (named "") we don't need to prepend
  837. * its full path
  838. */
  839. parent_fnl = *parent->name ? strlen(parent->full_name) : 0;
  840. full_name = calloc(1, parent_fnl + 1 + len + 1);
  841. if (!full_name) {
  842. free(new_name);
  843. free(new);
  844. return -ENOMEM;
  845. }
  846. new->name = new_name; /* assign to constant pointer */
  847. strcpy(full_name, parent->full_name); /* "" for root node */
  848. full_name[parent_fnl] = '/';
  849. strlcpy(&full_name[parent_fnl + 1], name, len + 1);
  850. new->full_name = full_name;
  851. /* Add as last sibling of the parent */
  852. if (last_sibling)
  853. last_sibling->sibling = new;
  854. if (!parent->child)
  855. parent->child = new;
  856. new->parent = parent;
  857. *childp = new;
  858. return 0;
  859. }