base.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  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. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/ctype.h>
  21. #include <linux/cpu.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/slab.h>
  26. #include <linux/proc_fs.h>
  27. #include "of_private.h"
  28. LIST_HEAD(aliases_lookup);
  29. struct device_node *of_allnodes;
  30. EXPORT_SYMBOL(of_allnodes);
  31. struct device_node *of_chosen;
  32. struct device_node *of_aliases;
  33. DEFINE_MUTEX(of_aliases_mutex);
  34. /* use when traversing tree through the allnext, child, sibling,
  35. * or parent members of struct device_node.
  36. */
  37. DEFINE_RAW_SPINLOCK(devtree_lock);
  38. int of_n_addr_cells(struct device_node *np)
  39. {
  40. const __be32 *ip;
  41. do {
  42. if (np->parent)
  43. np = np->parent;
  44. ip = of_get_property(np, "#address-cells", NULL);
  45. if (ip)
  46. return be32_to_cpup(ip);
  47. } while (np->parent);
  48. /* No #address-cells property for the root node */
  49. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  50. }
  51. EXPORT_SYMBOL(of_n_addr_cells);
  52. int of_n_size_cells(struct device_node *np)
  53. {
  54. const __be32 *ip;
  55. do {
  56. if (np->parent)
  57. np = np->parent;
  58. ip = of_get_property(np, "#size-cells", NULL);
  59. if (ip)
  60. return be32_to_cpup(ip);
  61. } while (np->parent);
  62. /* No #size-cells property for the root node */
  63. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  64. }
  65. EXPORT_SYMBOL(of_n_size_cells);
  66. #if defined(CONFIG_OF_DYNAMIC)
  67. /**
  68. * of_node_get - Increment refcount of a node
  69. * @node: Node to inc refcount, NULL is supported to
  70. * simplify writing of callers
  71. *
  72. * Returns node.
  73. */
  74. struct device_node *of_node_get(struct device_node *node)
  75. {
  76. if (node)
  77. kref_get(&node->kref);
  78. return node;
  79. }
  80. EXPORT_SYMBOL(of_node_get);
  81. static inline struct device_node *kref_to_device_node(struct kref *kref)
  82. {
  83. return container_of(kref, struct device_node, kref);
  84. }
  85. /**
  86. * of_node_release - release a dynamically allocated node
  87. * @kref: kref element of the node to be released
  88. *
  89. * In of_node_put() this function is passed to kref_put()
  90. * as the destructor.
  91. */
  92. static void of_node_release(struct kref *kref)
  93. {
  94. struct device_node *node = kref_to_device_node(kref);
  95. struct property *prop = node->properties;
  96. /* We should never be releasing nodes that haven't been detached. */
  97. if (!of_node_check_flag(node, OF_DETACHED)) {
  98. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  99. dump_stack();
  100. kref_init(&node->kref);
  101. return;
  102. }
  103. if (!of_node_check_flag(node, OF_DYNAMIC))
  104. return;
  105. while (prop) {
  106. struct property *next = prop->next;
  107. kfree(prop->name);
  108. kfree(prop->value);
  109. kfree(prop);
  110. prop = next;
  111. if (!prop) {
  112. prop = node->deadprops;
  113. node->deadprops = NULL;
  114. }
  115. }
  116. kfree(node->full_name);
  117. kfree(node->data);
  118. kfree(node);
  119. }
  120. /**
  121. * of_node_put - Decrement refcount of a node
  122. * @node: Node to dec refcount, NULL is supported to
  123. * simplify writing of callers
  124. *
  125. */
  126. void of_node_put(struct device_node *node)
  127. {
  128. if (node)
  129. kref_put(&node->kref, of_node_release);
  130. }
  131. EXPORT_SYMBOL(of_node_put);
  132. #endif /* CONFIG_OF_DYNAMIC */
  133. static struct property *__of_find_property(const struct device_node *np,
  134. const char *name, int *lenp)
  135. {
  136. struct property *pp;
  137. if (!np)
  138. return NULL;
  139. for (pp = np->properties; pp; pp = pp->next) {
  140. if (of_prop_cmp(pp->name, name) == 0) {
  141. if (lenp)
  142. *lenp = pp->length;
  143. break;
  144. }
  145. }
  146. return pp;
  147. }
  148. struct property *of_find_property(const struct device_node *np,
  149. const char *name,
  150. int *lenp)
  151. {
  152. struct property *pp;
  153. unsigned long flags;
  154. raw_spin_lock_irqsave(&devtree_lock, flags);
  155. pp = __of_find_property(np, name, lenp);
  156. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  157. return pp;
  158. }
  159. EXPORT_SYMBOL(of_find_property);
  160. /**
  161. * of_find_all_nodes - Get next node in global list
  162. * @prev: Previous node or NULL to start iteration
  163. * of_node_put() will be called on it
  164. *
  165. * Returns a node pointer with refcount incremented, use
  166. * of_node_put() on it when done.
  167. */
  168. struct device_node *of_find_all_nodes(struct device_node *prev)
  169. {
  170. struct device_node *np;
  171. unsigned long flags;
  172. raw_spin_lock_irqsave(&devtree_lock, flags);
  173. np = prev ? prev->allnext : of_allnodes;
  174. for (; np != NULL; np = np->allnext)
  175. if (of_node_get(np))
  176. break;
  177. of_node_put(prev);
  178. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  179. return np;
  180. }
  181. EXPORT_SYMBOL(of_find_all_nodes);
  182. /*
  183. * Find a property with a given name for a given node
  184. * and return the value.
  185. */
  186. static const void *__of_get_property(const struct device_node *np,
  187. const char *name, int *lenp)
  188. {
  189. struct property *pp = __of_find_property(np, name, lenp);
  190. return pp ? pp->value : NULL;
  191. }
  192. /*
  193. * Find a property with a given name for a given node
  194. * and return the value.
  195. */
  196. const void *of_get_property(const struct device_node *np, const char *name,
  197. int *lenp)
  198. {
  199. struct property *pp = of_find_property(np, name, lenp);
  200. return pp ? pp->value : NULL;
  201. }
  202. EXPORT_SYMBOL(of_get_property);
  203. /*
  204. * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  205. *
  206. * @cpu: logical cpu index of a core/thread
  207. * @phys_id: physical identifier of a core/thread
  208. *
  209. * CPU logical to physical index mapping is architecture specific.
  210. * However this __weak function provides a default match of physical
  211. * id to logical cpu index. phys_id provided here is usually values read
  212. * from the device tree which must match the hardware internal registers.
  213. *
  214. * Returns true if the physical identifier and the logical cpu index
  215. * correspond to the same core/thread, false otherwise.
  216. */
  217. bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
  218. {
  219. return (u32)phys_id == cpu;
  220. }
  221. /**
  222. * Checks if the given "prop_name" property holds the physical id of the
  223. * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
  224. * NULL, local thread number within the core is returned in it.
  225. */
  226. static bool __of_find_n_match_cpu_property(struct device_node *cpun,
  227. const char *prop_name, int cpu, unsigned int *thread)
  228. {
  229. const __be32 *cell;
  230. int ac, prop_len, tid;
  231. u64 hwid;
  232. ac = of_n_addr_cells(cpun);
  233. cell = of_get_property(cpun, prop_name, &prop_len);
  234. if (!cell)
  235. return false;
  236. prop_len /= sizeof(*cell);
  237. for (tid = 0; tid < prop_len; tid++) {
  238. hwid = of_read_number(cell, ac);
  239. if (arch_match_cpu_phys_id(cpu, hwid)) {
  240. if (thread)
  241. *thread = tid;
  242. return true;
  243. }
  244. cell += ac;
  245. }
  246. return false;
  247. }
  248. /**
  249. * of_get_cpu_node - Get device node associated with the given logical CPU
  250. *
  251. * @cpu: CPU number(logical index) for which device node is required
  252. * @thread: if not NULL, local thread number within the physical core is
  253. * returned
  254. *
  255. * The main purpose of this function is to retrieve the device node for the
  256. * given logical CPU index. It should be used to initialize the of_node in
  257. * cpu device. Once of_node in cpu device is populated, all the further
  258. * references can use that instead.
  259. *
  260. * CPU logical to physical index mapping is architecture specific and is built
  261. * before booting secondary cores. This function uses arch_match_cpu_phys_id
  262. * which can be overridden by architecture specific implementation.
  263. *
  264. * Returns a node pointer for the logical cpu if found, else NULL.
  265. */
  266. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  267. {
  268. struct device_node *cpun, *cpus;
  269. cpus = of_find_node_by_path("/cpus");
  270. if (!cpus) {
  271. pr_warn("Missing cpus node, bailing out\n");
  272. return NULL;
  273. }
  274. for_each_child_of_node(cpus, cpun) {
  275. if (of_node_cmp(cpun->type, "cpu"))
  276. continue;
  277. /* Check for non-standard "ibm,ppc-interrupt-server#s" property
  278. * for thread ids on PowerPC. If it doesn't exist fallback to
  279. * standard "reg" property.
  280. */
  281. if (IS_ENABLED(CONFIG_PPC) &&
  282. __of_find_n_match_cpu_property(cpun,
  283. "ibm,ppc-interrupt-server#s", cpu, thread))
  284. return cpun;
  285. if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
  286. return cpun;
  287. }
  288. return NULL;
  289. }
  290. EXPORT_SYMBOL(of_get_cpu_node);
  291. /** Checks if the given "compat" string matches one of the strings in
  292. * the device's "compatible" property
  293. */
  294. static int __of_device_is_compatible(const struct device_node *device,
  295. const char *compat)
  296. {
  297. const char* cp;
  298. int cplen, l;
  299. cp = __of_get_property(device, "compatible", &cplen);
  300. if (cp == NULL)
  301. return 0;
  302. while (cplen > 0) {
  303. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  304. return 1;
  305. l = strlen(cp) + 1;
  306. cp += l;
  307. cplen -= l;
  308. }
  309. return 0;
  310. }
  311. /** Checks if the given "compat" string matches one of the strings in
  312. * the device's "compatible" property
  313. */
  314. int of_device_is_compatible(const struct device_node *device,
  315. const char *compat)
  316. {
  317. unsigned long flags;
  318. int res;
  319. raw_spin_lock_irqsave(&devtree_lock, flags);
  320. res = __of_device_is_compatible(device, compat);
  321. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  322. return res;
  323. }
  324. EXPORT_SYMBOL(of_device_is_compatible);
  325. /**
  326. * of_machine_is_compatible - Test root of device tree for a given compatible value
  327. * @compat: compatible string to look for in root node's compatible property.
  328. *
  329. * Returns true if the root node has the given value in its
  330. * compatible property.
  331. */
  332. int of_machine_is_compatible(const char *compat)
  333. {
  334. struct device_node *root;
  335. int rc = 0;
  336. root = of_find_node_by_path("/");
  337. if (root) {
  338. rc = of_device_is_compatible(root, compat);
  339. of_node_put(root);
  340. }
  341. return rc;
  342. }
  343. EXPORT_SYMBOL(of_machine_is_compatible);
  344. /**
  345. * __of_device_is_available - check if a device is available for use
  346. *
  347. * @device: Node to check for availability, with locks already held
  348. *
  349. * Returns 1 if the status property is absent or set to "okay" or "ok",
  350. * 0 otherwise
  351. */
  352. static int __of_device_is_available(const struct device_node *device)
  353. {
  354. const char *status;
  355. int statlen;
  356. status = __of_get_property(device, "status", &statlen);
  357. if (status == NULL)
  358. return 1;
  359. if (statlen > 0) {
  360. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. /**
  366. * of_device_is_available - check if a device is available for use
  367. *
  368. * @device: Node to check for availability
  369. *
  370. * Returns 1 if the status property is absent or set to "okay" or "ok",
  371. * 0 otherwise
  372. */
  373. int of_device_is_available(const struct device_node *device)
  374. {
  375. unsigned long flags;
  376. int res;
  377. raw_spin_lock_irqsave(&devtree_lock, flags);
  378. res = __of_device_is_available(device);
  379. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  380. return res;
  381. }
  382. EXPORT_SYMBOL(of_device_is_available);
  383. /**
  384. * of_get_parent - Get a node's parent if any
  385. * @node: Node to get parent
  386. *
  387. * Returns a node pointer with refcount incremented, use
  388. * of_node_put() on it when done.
  389. */
  390. struct device_node *of_get_parent(const struct device_node *node)
  391. {
  392. struct device_node *np;
  393. unsigned long flags;
  394. if (!node)
  395. return NULL;
  396. raw_spin_lock_irqsave(&devtree_lock, flags);
  397. np = of_node_get(node->parent);
  398. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  399. return np;
  400. }
  401. EXPORT_SYMBOL(of_get_parent);
  402. /**
  403. * of_get_next_parent - Iterate to a node's parent
  404. * @node: Node to get parent of
  405. *
  406. * This is like of_get_parent() except that it drops the
  407. * refcount on the passed node, making it suitable for iterating
  408. * through a node's parents.
  409. *
  410. * Returns a node pointer with refcount incremented, use
  411. * of_node_put() on it when done.
  412. */
  413. struct device_node *of_get_next_parent(struct device_node *node)
  414. {
  415. struct device_node *parent;
  416. unsigned long flags;
  417. if (!node)
  418. return NULL;
  419. raw_spin_lock_irqsave(&devtree_lock, flags);
  420. parent = of_node_get(node->parent);
  421. of_node_put(node);
  422. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  423. return parent;
  424. }
  425. EXPORT_SYMBOL(of_get_next_parent);
  426. /**
  427. * of_get_next_child - Iterate a node childs
  428. * @node: parent node
  429. * @prev: previous child of the parent node, or NULL to get first
  430. *
  431. * Returns a node pointer with refcount incremented, use
  432. * of_node_put() on it when done.
  433. */
  434. struct device_node *of_get_next_child(const struct device_node *node,
  435. struct device_node *prev)
  436. {
  437. struct device_node *next;
  438. unsigned long flags;
  439. raw_spin_lock_irqsave(&devtree_lock, flags);
  440. next = prev ? prev->sibling : node->child;
  441. for (; next; next = next->sibling)
  442. if (of_node_get(next))
  443. break;
  444. of_node_put(prev);
  445. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  446. return next;
  447. }
  448. EXPORT_SYMBOL(of_get_next_child);
  449. /**
  450. * of_get_next_available_child - Find the next available child node
  451. * @node: parent node
  452. * @prev: previous child of the parent node, or NULL to get first
  453. *
  454. * This function is like of_get_next_child(), except that it
  455. * automatically skips any disabled nodes (i.e. status = "disabled").
  456. */
  457. struct device_node *of_get_next_available_child(const struct device_node *node,
  458. struct device_node *prev)
  459. {
  460. struct device_node *next;
  461. unsigned long flags;
  462. raw_spin_lock_irqsave(&devtree_lock, flags);
  463. next = prev ? prev->sibling : node->child;
  464. for (; next; next = next->sibling) {
  465. if (!__of_device_is_available(next))
  466. continue;
  467. if (of_node_get(next))
  468. break;
  469. }
  470. of_node_put(prev);
  471. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  472. return next;
  473. }
  474. EXPORT_SYMBOL(of_get_next_available_child);
  475. /**
  476. * of_get_child_by_name - Find the child node by name for a given parent
  477. * @node: parent node
  478. * @name: child name to look for.
  479. *
  480. * This function looks for child node for given matching name
  481. *
  482. * Returns a node pointer if found, with refcount incremented, use
  483. * of_node_put() on it when done.
  484. * Returns NULL if node is not found.
  485. */
  486. struct device_node *of_get_child_by_name(const struct device_node *node,
  487. const char *name)
  488. {
  489. struct device_node *child;
  490. for_each_child_of_node(node, child)
  491. if (child->name && (of_node_cmp(child->name, name) == 0))
  492. break;
  493. return child;
  494. }
  495. EXPORT_SYMBOL(of_get_child_by_name);
  496. /**
  497. * of_find_node_by_path - Find a node matching a full OF path
  498. * @path: The full path to match
  499. *
  500. * Returns a node pointer with refcount incremented, use
  501. * of_node_put() on it when done.
  502. */
  503. struct device_node *of_find_node_by_path(const char *path)
  504. {
  505. struct device_node *np = of_allnodes;
  506. unsigned long flags;
  507. raw_spin_lock_irqsave(&devtree_lock, flags);
  508. for (; np; np = np->allnext) {
  509. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  510. && of_node_get(np))
  511. break;
  512. }
  513. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  514. return np;
  515. }
  516. EXPORT_SYMBOL(of_find_node_by_path);
  517. /**
  518. * of_find_node_by_name - Find a node by its "name" property
  519. * @from: The node to start searching from or NULL, the node
  520. * you pass will not be searched, only the next one
  521. * will; typically, you pass what the previous call
  522. * returned. of_node_put() will be called on it
  523. * @name: The name string to match against
  524. *
  525. * Returns a node pointer with refcount incremented, use
  526. * of_node_put() on it when done.
  527. */
  528. struct device_node *of_find_node_by_name(struct device_node *from,
  529. const char *name)
  530. {
  531. struct device_node *np;
  532. unsigned long flags;
  533. raw_spin_lock_irqsave(&devtree_lock, flags);
  534. np = from ? from->allnext : of_allnodes;
  535. for (; np; np = np->allnext)
  536. if (np->name && (of_node_cmp(np->name, name) == 0)
  537. && of_node_get(np))
  538. break;
  539. of_node_put(from);
  540. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  541. return np;
  542. }
  543. EXPORT_SYMBOL(of_find_node_by_name);
  544. /**
  545. * of_find_node_by_type - Find a node by its "device_type" property
  546. * @from: The node to start searching from, or NULL to start searching
  547. * the entire device tree. The node you pass will not be
  548. * searched, only the next one will; typically, you pass
  549. * what the previous call returned. of_node_put() will be
  550. * called on from for you.
  551. * @type: The type string to match against
  552. *
  553. * Returns a node pointer with refcount incremented, use
  554. * of_node_put() on it when done.
  555. */
  556. struct device_node *of_find_node_by_type(struct device_node *from,
  557. const char *type)
  558. {
  559. struct device_node *np;
  560. unsigned long flags;
  561. raw_spin_lock_irqsave(&devtree_lock, flags);
  562. np = from ? from->allnext : of_allnodes;
  563. for (; np; np = np->allnext)
  564. if (np->type && (of_node_cmp(np->type, type) == 0)
  565. && of_node_get(np))
  566. break;
  567. of_node_put(from);
  568. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  569. return np;
  570. }
  571. EXPORT_SYMBOL(of_find_node_by_type);
  572. /**
  573. * of_find_compatible_node - Find a node based on type and one of the
  574. * tokens in its "compatible" property
  575. * @from: The node to start searching from or NULL, the node
  576. * you pass will not be searched, only the next one
  577. * will; typically, you pass what the previous call
  578. * returned. of_node_put() will be called on it
  579. * @type: The type string to match "device_type" or NULL to ignore
  580. * @compatible: The string to match to one of the tokens in the device
  581. * "compatible" list.
  582. *
  583. * Returns a node pointer with refcount incremented, use
  584. * of_node_put() on it when done.
  585. */
  586. struct device_node *of_find_compatible_node(struct device_node *from,
  587. const char *type, const char *compatible)
  588. {
  589. struct device_node *np;
  590. unsigned long flags;
  591. raw_spin_lock_irqsave(&devtree_lock, flags);
  592. np = from ? from->allnext : of_allnodes;
  593. for (; np; np = np->allnext) {
  594. if (type
  595. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  596. continue;
  597. if (__of_device_is_compatible(np, compatible) &&
  598. of_node_get(np))
  599. break;
  600. }
  601. of_node_put(from);
  602. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  603. return np;
  604. }
  605. EXPORT_SYMBOL(of_find_compatible_node);
  606. /**
  607. * of_find_node_with_property - Find a node which has a property with
  608. * the given name.
  609. * @from: The node to start searching from or NULL, the node
  610. * you pass will not be searched, only the next one
  611. * will; typically, you pass what the previous call
  612. * returned. of_node_put() will be called on it
  613. * @prop_name: The name of the property to look for.
  614. *
  615. * Returns a node pointer with refcount incremented, use
  616. * of_node_put() on it when done.
  617. */
  618. struct device_node *of_find_node_with_property(struct device_node *from,
  619. const char *prop_name)
  620. {
  621. struct device_node *np;
  622. struct property *pp;
  623. unsigned long flags;
  624. raw_spin_lock_irqsave(&devtree_lock, flags);
  625. np = from ? from->allnext : of_allnodes;
  626. for (; np; np = np->allnext) {
  627. for (pp = np->properties; pp; pp = pp->next) {
  628. if (of_prop_cmp(pp->name, prop_name) == 0) {
  629. of_node_get(np);
  630. goto out;
  631. }
  632. }
  633. }
  634. out:
  635. of_node_put(from);
  636. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  637. return np;
  638. }
  639. EXPORT_SYMBOL(of_find_node_with_property);
  640. static
  641. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  642. const struct device_node *node)
  643. {
  644. if (!matches)
  645. return NULL;
  646. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  647. int match = 1;
  648. if (matches->name[0])
  649. match &= node->name
  650. && !strcmp(matches->name, node->name);
  651. if (matches->type[0])
  652. match &= node->type
  653. && !strcmp(matches->type, node->type);
  654. if (matches->compatible[0])
  655. match &= __of_device_is_compatible(node,
  656. matches->compatible);
  657. if (match)
  658. return matches;
  659. matches++;
  660. }
  661. return NULL;
  662. }
  663. /**
  664. * of_match_node - Tell if an device_node has a matching of_match structure
  665. * @matches: array of of device match structures to search in
  666. * @node: the of device structure to match against
  667. *
  668. * Low level utility function used by device matching.
  669. */
  670. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  671. const struct device_node *node)
  672. {
  673. const struct of_device_id *match;
  674. unsigned long flags;
  675. raw_spin_lock_irqsave(&devtree_lock, flags);
  676. match = __of_match_node(matches, node);
  677. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  678. return match;
  679. }
  680. EXPORT_SYMBOL(of_match_node);
  681. /**
  682. * of_find_matching_node_and_match - Find a node based on an of_device_id
  683. * match table.
  684. * @from: The node to start searching from or NULL, the node
  685. * you pass will not be searched, only the next one
  686. * will; typically, you pass what the previous call
  687. * returned. of_node_put() will be called on it
  688. * @matches: array of of device match structures to search in
  689. * @match Updated to point at the matches entry which matched
  690. *
  691. * Returns a node pointer with refcount incremented, use
  692. * of_node_put() on it when done.
  693. */
  694. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  695. const struct of_device_id *matches,
  696. const struct of_device_id **match)
  697. {
  698. struct device_node *np;
  699. const struct of_device_id *m;
  700. unsigned long flags;
  701. if (match)
  702. *match = NULL;
  703. raw_spin_lock_irqsave(&devtree_lock, flags);
  704. np = from ? from->allnext : of_allnodes;
  705. for (; np; np = np->allnext) {
  706. m = __of_match_node(matches, np);
  707. if (m && of_node_get(np)) {
  708. if (match)
  709. *match = m;
  710. break;
  711. }
  712. }
  713. of_node_put(from);
  714. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  715. return np;
  716. }
  717. EXPORT_SYMBOL(of_find_matching_node_and_match);
  718. /**
  719. * of_modalias_node - Lookup appropriate modalias for a device node
  720. * @node: pointer to a device tree node
  721. * @modalias: Pointer to buffer that modalias value will be copied into
  722. * @len: Length of modalias value
  723. *
  724. * Based on the value of the compatible property, this routine will attempt
  725. * to choose an appropriate modalias value for a particular device tree node.
  726. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  727. * from the first entry in the compatible list property.
  728. *
  729. * This routine returns 0 on success, <0 on failure.
  730. */
  731. int of_modalias_node(struct device_node *node, char *modalias, int len)
  732. {
  733. const char *compatible, *p;
  734. int cplen;
  735. compatible = of_get_property(node, "compatible", &cplen);
  736. if (!compatible || strlen(compatible) > cplen)
  737. return -ENODEV;
  738. p = strchr(compatible, ',');
  739. strlcpy(modalias, p ? p + 1 : compatible, len);
  740. return 0;
  741. }
  742. EXPORT_SYMBOL_GPL(of_modalias_node);
  743. /**
  744. * of_find_node_by_phandle - Find a node given a phandle
  745. * @handle: phandle of the node to find
  746. *
  747. * Returns a node pointer with refcount incremented, use
  748. * of_node_put() on it when done.
  749. */
  750. struct device_node *of_find_node_by_phandle(phandle handle)
  751. {
  752. struct device_node *np;
  753. unsigned long flags;
  754. raw_spin_lock_irqsave(&devtree_lock, flags);
  755. for (np = of_allnodes; np; np = np->allnext)
  756. if (np->phandle == handle)
  757. break;
  758. of_node_get(np);
  759. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  760. return np;
  761. }
  762. EXPORT_SYMBOL(of_find_node_by_phandle);
  763. /**
  764. * of_find_property_value_of_size
  765. *
  766. * @np: device node from which the property value is to be read.
  767. * @propname: name of the property to be searched.
  768. * @len: requested length of property value
  769. *
  770. * Search for a property in a device node and valid the requested size.
  771. * Returns the property value on success, -EINVAL if the property does not
  772. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  773. * property data isn't large enough.
  774. *
  775. */
  776. static void *of_find_property_value_of_size(const struct device_node *np,
  777. const char *propname, u32 len)
  778. {
  779. struct property *prop = of_find_property(np, propname, NULL);
  780. if (!prop)
  781. return ERR_PTR(-EINVAL);
  782. if (!prop->value)
  783. return ERR_PTR(-ENODATA);
  784. if (len > prop->length)
  785. return ERR_PTR(-EOVERFLOW);
  786. return prop->value;
  787. }
  788. /**
  789. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  790. *
  791. * @np: device node from which the property value is to be read.
  792. * @propname: name of the property to be searched.
  793. * @index: index of the u32 in the list of values
  794. * @out_value: pointer to return value, modified only if no error.
  795. *
  796. * Search for a property in a device node and read nth 32-bit value from
  797. * it. Returns 0 on success, -EINVAL if the property does not exist,
  798. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  799. * property data isn't large enough.
  800. *
  801. * The out_value is modified only if a valid u32 value can be decoded.
  802. */
  803. int of_property_read_u32_index(const struct device_node *np,
  804. const char *propname,
  805. u32 index, u32 *out_value)
  806. {
  807. const u32 *val = of_find_property_value_of_size(np, propname,
  808. ((index + 1) * sizeof(*out_value)));
  809. if (IS_ERR(val))
  810. return PTR_ERR(val);
  811. *out_value = be32_to_cpup(((__be32 *)val) + index);
  812. return 0;
  813. }
  814. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  815. /**
  816. * of_property_read_u8_array - Find and read an array of u8 from a property.
  817. *
  818. * @np: device node from which the property value is to be read.
  819. * @propname: name of the property to be searched.
  820. * @out_values: pointer to return value, modified only if return value is 0.
  821. * @sz: number of array elements to read
  822. *
  823. * Search for a property in a device node and read 8-bit value(s) from
  824. * it. Returns 0 on success, -EINVAL if the property does not exist,
  825. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  826. * property data isn't large enough.
  827. *
  828. * dts entry of array should be like:
  829. * property = /bits/ 8 <0x50 0x60 0x70>;
  830. *
  831. * The out_values is modified only if a valid u8 value can be decoded.
  832. */
  833. int of_property_read_u8_array(const struct device_node *np,
  834. const char *propname, u8 *out_values, size_t sz)
  835. {
  836. const u8 *val = of_find_property_value_of_size(np, propname,
  837. (sz * sizeof(*out_values)));
  838. if (IS_ERR(val))
  839. return PTR_ERR(val);
  840. while (sz--)
  841. *out_values++ = *val++;
  842. return 0;
  843. }
  844. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  845. /**
  846. * of_property_read_u16_array - Find and read an array of u16 from a property.
  847. *
  848. * @np: device node from which the property value is to be read.
  849. * @propname: name of the property to be searched.
  850. * @out_values: pointer to return value, modified only if return value is 0.
  851. * @sz: number of array elements to read
  852. *
  853. * Search for a property in a device node and read 16-bit value(s) from
  854. * it. Returns 0 on success, -EINVAL if the property does not exist,
  855. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  856. * property data isn't large enough.
  857. *
  858. * dts entry of array should be like:
  859. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  860. *
  861. * The out_values is modified only if a valid u16 value can be decoded.
  862. */
  863. int of_property_read_u16_array(const struct device_node *np,
  864. const char *propname, u16 *out_values, size_t sz)
  865. {
  866. const __be16 *val = of_find_property_value_of_size(np, propname,
  867. (sz * sizeof(*out_values)));
  868. if (IS_ERR(val))
  869. return PTR_ERR(val);
  870. while (sz--)
  871. *out_values++ = be16_to_cpup(val++);
  872. return 0;
  873. }
  874. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  875. /**
  876. * of_property_read_u32_array - Find and read an array of 32 bit integers
  877. * from a property.
  878. *
  879. * @np: device node from which the property value is to be read.
  880. * @propname: name of the property to be searched.
  881. * @out_values: pointer to return value, modified only if return value is 0.
  882. * @sz: number of array elements to read
  883. *
  884. * Search for a property in a device node and read 32-bit value(s) from
  885. * it. Returns 0 on success, -EINVAL if the property does not exist,
  886. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  887. * property data isn't large enough.
  888. *
  889. * The out_values is modified only if a valid u32 value can be decoded.
  890. */
  891. int of_property_read_u32_array(const struct device_node *np,
  892. const char *propname, u32 *out_values,
  893. size_t sz)
  894. {
  895. const __be32 *val = of_find_property_value_of_size(np, propname,
  896. (sz * sizeof(*out_values)));
  897. if (IS_ERR(val))
  898. return PTR_ERR(val);
  899. while (sz--)
  900. *out_values++ = be32_to_cpup(val++);
  901. return 0;
  902. }
  903. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  904. /**
  905. * of_property_read_u64 - Find and read a 64 bit integer from a property
  906. * @np: device node from which the property value is to be read.
  907. * @propname: name of the property to be searched.
  908. * @out_value: pointer to return value, modified only if return value is 0.
  909. *
  910. * Search for a property in a device node and read a 64-bit value from
  911. * it. Returns 0 on success, -EINVAL if the property does not exist,
  912. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  913. * property data isn't large enough.
  914. *
  915. * The out_value is modified only if a valid u64 value can be decoded.
  916. */
  917. int of_property_read_u64(const struct device_node *np, const char *propname,
  918. u64 *out_value)
  919. {
  920. const __be32 *val = of_find_property_value_of_size(np, propname,
  921. sizeof(*out_value));
  922. if (IS_ERR(val))
  923. return PTR_ERR(val);
  924. *out_value = of_read_number(val, 2);
  925. return 0;
  926. }
  927. EXPORT_SYMBOL_GPL(of_property_read_u64);
  928. /**
  929. * of_property_read_string - Find and read a string from a property
  930. * @np: device node from which the property value is to be read.
  931. * @propname: name of the property to be searched.
  932. * @out_string: pointer to null terminated return string, modified only if
  933. * return value is 0.
  934. *
  935. * Search for a property in a device tree node and retrieve a null
  936. * terminated string value (pointer to data, not a copy). Returns 0 on
  937. * success, -EINVAL if the property does not exist, -ENODATA if property
  938. * does not have a value, and -EILSEQ if the string is not null-terminated
  939. * within the length of the property data.
  940. *
  941. * The out_string pointer is modified only if a valid string can be decoded.
  942. */
  943. int of_property_read_string(struct device_node *np, const char *propname,
  944. const char **out_string)
  945. {
  946. struct property *prop = of_find_property(np, propname, NULL);
  947. if (!prop)
  948. return -EINVAL;
  949. if (!prop->value)
  950. return -ENODATA;
  951. if (strnlen(prop->value, prop->length) >= prop->length)
  952. return -EILSEQ;
  953. *out_string = prop->value;
  954. return 0;
  955. }
  956. EXPORT_SYMBOL_GPL(of_property_read_string);
  957. /**
  958. * of_property_read_string_index - Find and read a string from a multiple
  959. * strings property.
  960. * @np: device node from which the property value is to be read.
  961. * @propname: name of the property to be searched.
  962. * @index: index of the string in the list of strings
  963. * @out_string: pointer to null terminated return string, modified only if
  964. * return value is 0.
  965. *
  966. * Search for a property in a device tree node and retrieve a null
  967. * terminated string value (pointer to data, not a copy) in the list of strings
  968. * contained in that property.
  969. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  970. * property does not have a value, and -EILSEQ if the string is not
  971. * null-terminated within the length of the property data.
  972. *
  973. * The out_string pointer is modified only if a valid string can be decoded.
  974. */
  975. int of_property_read_string_index(struct device_node *np, const char *propname,
  976. int index, const char **output)
  977. {
  978. struct property *prop = of_find_property(np, propname, NULL);
  979. int i = 0;
  980. size_t l = 0, total = 0;
  981. const char *p;
  982. if (!prop)
  983. return -EINVAL;
  984. if (!prop->value)
  985. return -ENODATA;
  986. if (strnlen(prop->value, prop->length) >= prop->length)
  987. return -EILSEQ;
  988. p = prop->value;
  989. for (i = 0; total < prop->length; total += l, p += l) {
  990. l = strlen(p) + 1;
  991. if (i++ == index) {
  992. *output = p;
  993. return 0;
  994. }
  995. }
  996. return -ENODATA;
  997. }
  998. EXPORT_SYMBOL_GPL(of_property_read_string_index);
  999. /**
  1000. * of_property_match_string() - Find string in a list and return index
  1001. * @np: pointer to node containing string list property
  1002. * @propname: string list property name
  1003. * @string: pointer to string to search for in string list
  1004. *
  1005. * This function searches a string list property and returns the index
  1006. * of a specific string value.
  1007. */
  1008. int of_property_match_string(struct device_node *np, const char *propname,
  1009. const char *string)
  1010. {
  1011. struct property *prop = of_find_property(np, propname, NULL);
  1012. size_t l;
  1013. int i;
  1014. const char *p, *end;
  1015. if (!prop)
  1016. return -EINVAL;
  1017. if (!prop->value)
  1018. return -ENODATA;
  1019. p = prop->value;
  1020. end = p + prop->length;
  1021. for (i = 0; p < end; i++, p += l) {
  1022. l = strlen(p) + 1;
  1023. if (p + l > end)
  1024. return -EILSEQ;
  1025. pr_debug("comparing %s with %s\n", string, p);
  1026. if (strcmp(string, p) == 0)
  1027. return i; /* Found it; return index */
  1028. }
  1029. return -ENODATA;
  1030. }
  1031. EXPORT_SYMBOL_GPL(of_property_match_string);
  1032. /**
  1033. * of_property_count_strings - Find and return the number of strings from a
  1034. * multiple strings property.
  1035. * @np: device node from which the property value is to be read.
  1036. * @propname: name of the property to be searched.
  1037. *
  1038. * Search for a property in a device tree node and retrieve the number of null
  1039. * terminated string contain in it. Returns the number of strings on
  1040. * success, -EINVAL if the property does not exist, -ENODATA if property
  1041. * does not have a value, and -EILSEQ if the string is not null-terminated
  1042. * within the length of the property data.
  1043. */
  1044. int of_property_count_strings(struct device_node *np, const char *propname)
  1045. {
  1046. struct property *prop = of_find_property(np, propname, NULL);
  1047. int i = 0;
  1048. size_t l = 0, total = 0;
  1049. const char *p;
  1050. if (!prop)
  1051. return -EINVAL;
  1052. if (!prop->value)
  1053. return -ENODATA;
  1054. if (strnlen(prop->value, prop->length) >= prop->length)
  1055. return -EILSEQ;
  1056. p = prop->value;
  1057. for (i = 0; total < prop->length; total += l, p += l, i++)
  1058. l = strlen(p) + 1;
  1059. return i;
  1060. }
  1061. EXPORT_SYMBOL_GPL(of_property_count_strings);
  1062. /**
  1063. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1064. * @np: Pointer to device node holding phandle property
  1065. * @phandle_name: Name of property holding a phandle value
  1066. * @index: For properties holding a table of phandles, this is the index into
  1067. * the table
  1068. *
  1069. * Returns the device_node pointer with refcount incremented. Use
  1070. * of_node_put() on it when done.
  1071. */
  1072. struct device_node *of_parse_phandle(const struct device_node *np,
  1073. const char *phandle_name, int index)
  1074. {
  1075. const __be32 *phandle;
  1076. int size;
  1077. phandle = of_get_property(np, phandle_name, &size);
  1078. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  1079. return NULL;
  1080. return of_find_node_by_phandle(be32_to_cpup(phandle + index));
  1081. }
  1082. EXPORT_SYMBOL(of_parse_phandle);
  1083. /**
  1084. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1085. * @np: pointer to a device tree node containing a list
  1086. * @list_name: property name that contains a list
  1087. * @cells_name: property name that specifies phandles' arguments count
  1088. * @index: index of a phandle to parse out
  1089. * @out_args: optional pointer to output arguments structure (will be filled)
  1090. *
  1091. * This function is useful to parse lists of phandles and their arguments.
  1092. * Returns 0 on success and fills out_args, on error returns appropriate
  1093. * errno value.
  1094. *
  1095. * Caller is responsible to call of_node_put() on the returned out_args->node
  1096. * pointer.
  1097. *
  1098. * Example:
  1099. *
  1100. * phandle1: node1 {
  1101. * #list-cells = <2>;
  1102. * }
  1103. *
  1104. * phandle2: node2 {
  1105. * #list-cells = <1>;
  1106. * }
  1107. *
  1108. * node3 {
  1109. * list = <&phandle1 1 2 &phandle2 3>;
  1110. * }
  1111. *
  1112. * To get a device_node of the `node2' node you may call this:
  1113. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1114. */
  1115. static int __of_parse_phandle_with_args(const struct device_node *np,
  1116. const char *list_name,
  1117. const char *cells_name, int index,
  1118. struct of_phandle_args *out_args)
  1119. {
  1120. const __be32 *list, *list_end;
  1121. int rc = 0, size, cur_index = 0;
  1122. uint32_t count = 0;
  1123. struct device_node *node = NULL;
  1124. phandle phandle;
  1125. /* Retrieve the phandle list property */
  1126. list = of_get_property(np, list_name, &size);
  1127. if (!list)
  1128. return -ENOENT;
  1129. list_end = list + size / sizeof(*list);
  1130. /* Loop over the phandles until all the requested entry is found */
  1131. while (list < list_end) {
  1132. rc = -EINVAL;
  1133. count = 0;
  1134. /*
  1135. * If phandle is 0, then it is an empty entry with no
  1136. * arguments. Skip forward to the next entry.
  1137. */
  1138. phandle = be32_to_cpup(list++);
  1139. if (phandle) {
  1140. /*
  1141. * Find the provider node and parse the #*-cells
  1142. * property to determine the argument length
  1143. */
  1144. node = of_find_node_by_phandle(phandle);
  1145. if (!node) {
  1146. pr_err("%s: could not find phandle\n",
  1147. np->full_name);
  1148. goto err;
  1149. }
  1150. if (of_property_read_u32(node, cells_name, &count)) {
  1151. pr_err("%s: could not get %s for %s\n",
  1152. np->full_name, cells_name,
  1153. node->full_name);
  1154. goto err;
  1155. }
  1156. /*
  1157. * Make sure that the arguments actually fit in the
  1158. * remaining property data length
  1159. */
  1160. if (list + count > list_end) {
  1161. pr_err("%s: arguments longer than property\n",
  1162. np->full_name);
  1163. goto err;
  1164. }
  1165. }
  1166. /*
  1167. * All of the error cases above bail out of the loop, so at
  1168. * this point, the parsing is successful. If the requested
  1169. * index matches, then fill the out_args structure and return,
  1170. * or return -ENOENT for an empty entry.
  1171. */
  1172. rc = -ENOENT;
  1173. if (cur_index == index) {
  1174. if (!phandle)
  1175. goto err;
  1176. if (out_args) {
  1177. int i;
  1178. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1179. count = MAX_PHANDLE_ARGS;
  1180. out_args->np = node;
  1181. out_args->args_count = count;
  1182. for (i = 0; i < count; i++)
  1183. out_args->args[i] = be32_to_cpup(list++);
  1184. } else {
  1185. of_node_put(node);
  1186. }
  1187. /* Found it! return success */
  1188. return 0;
  1189. }
  1190. of_node_put(node);
  1191. node = NULL;
  1192. list += count;
  1193. cur_index++;
  1194. }
  1195. /*
  1196. * Unlock node before returning result; will be one of:
  1197. * -ENOENT : index is for empty phandle
  1198. * -EINVAL : parsing error on data
  1199. * [1..n] : Number of phandle (count mode; when index = -1)
  1200. */
  1201. rc = index < 0 ? cur_index : -ENOENT;
  1202. err:
  1203. if (node)
  1204. of_node_put(node);
  1205. return rc;
  1206. }
  1207. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1208. const char *cells_name, int index,
  1209. struct of_phandle_args *out_args)
  1210. {
  1211. if (index < 0)
  1212. return -EINVAL;
  1213. return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
  1214. }
  1215. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1216. /**
  1217. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1218. * @np: pointer to a device tree node containing a list
  1219. * @list_name: property name that contains a list
  1220. * @cells_name: property name that specifies phandles' arguments count
  1221. *
  1222. * Returns the number of phandle + argument tuples within a property. It
  1223. * is a typical pattern to encode a list of phandle and variable
  1224. * arguments into a single property. The number of arguments is encoded
  1225. * by a property in the phandle-target node. For example, a gpios
  1226. * property would contain a list of GPIO specifies consisting of a
  1227. * phandle and 1 or more arguments. The number of arguments are
  1228. * determined by the #gpio-cells property in the node pointed to by the
  1229. * phandle.
  1230. */
  1231. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1232. const char *cells_name)
  1233. {
  1234. return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
  1235. }
  1236. EXPORT_SYMBOL(of_count_phandle_with_args);
  1237. #if defined(CONFIG_OF_DYNAMIC)
  1238. static int of_property_notify(int action, struct device_node *np,
  1239. struct property *prop)
  1240. {
  1241. struct of_prop_reconfig pr;
  1242. pr.dn = np;
  1243. pr.prop = prop;
  1244. return of_reconfig_notify(action, &pr);
  1245. }
  1246. #else
  1247. static int of_property_notify(int action, struct device_node *np,
  1248. struct property *prop)
  1249. {
  1250. return 0;
  1251. }
  1252. #endif
  1253. /**
  1254. * of_add_property - Add a property to a node
  1255. */
  1256. int of_add_property(struct device_node *np, struct property *prop)
  1257. {
  1258. struct property **next;
  1259. unsigned long flags;
  1260. int rc;
  1261. rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
  1262. if (rc)
  1263. return rc;
  1264. prop->next = NULL;
  1265. raw_spin_lock_irqsave(&devtree_lock, flags);
  1266. next = &np->properties;
  1267. while (*next) {
  1268. if (strcmp(prop->name, (*next)->name) == 0) {
  1269. /* duplicate ! don't insert it */
  1270. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1271. return -1;
  1272. }
  1273. next = &(*next)->next;
  1274. }
  1275. *next = prop;
  1276. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1277. #ifdef CONFIG_PROC_DEVICETREE
  1278. /* try to add to proc as well if it was initialized */
  1279. if (np->pde)
  1280. proc_device_tree_add_prop(np->pde, prop);
  1281. #endif /* CONFIG_PROC_DEVICETREE */
  1282. return 0;
  1283. }
  1284. /**
  1285. * of_remove_property - Remove a property from a node.
  1286. *
  1287. * Note that we don't actually remove it, since we have given out
  1288. * who-knows-how-many pointers to the data using get-property.
  1289. * Instead we just move the property to the "dead properties"
  1290. * list, so it won't be found any more.
  1291. */
  1292. int of_remove_property(struct device_node *np, struct property *prop)
  1293. {
  1294. struct property **next;
  1295. unsigned long flags;
  1296. int found = 0;
  1297. int rc;
  1298. rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1299. if (rc)
  1300. return rc;
  1301. raw_spin_lock_irqsave(&devtree_lock, flags);
  1302. next = &np->properties;
  1303. while (*next) {
  1304. if (*next == prop) {
  1305. /* found the node */
  1306. *next = prop->next;
  1307. prop->next = np->deadprops;
  1308. np->deadprops = prop;
  1309. found = 1;
  1310. break;
  1311. }
  1312. next = &(*next)->next;
  1313. }
  1314. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1315. if (!found)
  1316. return -ENODEV;
  1317. #ifdef CONFIG_PROC_DEVICETREE
  1318. /* try to remove the proc node as well */
  1319. if (np->pde)
  1320. proc_device_tree_remove_prop(np->pde, prop);
  1321. #endif /* CONFIG_PROC_DEVICETREE */
  1322. return 0;
  1323. }
  1324. /*
  1325. * of_update_property - Update a property in a node, if the property does
  1326. * not exist, add it.
  1327. *
  1328. * Note that we don't actually remove it, since we have given out
  1329. * who-knows-how-many pointers to the data using get-property.
  1330. * Instead we just move the property to the "dead properties" list,
  1331. * and add the new property to the property list
  1332. */
  1333. int of_update_property(struct device_node *np, struct property *newprop)
  1334. {
  1335. struct property **next, *oldprop;
  1336. unsigned long flags;
  1337. int rc, found = 0;
  1338. rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
  1339. if (rc)
  1340. return rc;
  1341. if (!newprop->name)
  1342. return -EINVAL;
  1343. oldprop = of_find_property(np, newprop->name, NULL);
  1344. if (!oldprop)
  1345. return of_add_property(np, newprop);
  1346. raw_spin_lock_irqsave(&devtree_lock, flags);
  1347. next = &np->properties;
  1348. while (*next) {
  1349. if (*next == oldprop) {
  1350. /* found the node */
  1351. newprop->next = oldprop->next;
  1352. *next = newprop;
  1353. oldprop->next = np->deadprops;
  1354. np->deadprops = oldprop;
  1355. found = 1;
  1356. break;
  1357. }
  1358. next = &(*next)->next;
  1359. }
  1360. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1361. if (!found)
  1362. return -ENODEV;
  1363. #ifdef CONFIG_PROC_DEVICETREE
  1364. /* try to add to proc as well if it was initialized */
  1365. if (np->pde)
  1366. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1367. #endif /* CONFIG_PROC_DEVICETREE */
  1368. return 0;
  1369. }
  1370. #if defined(CONFIG_OF_DYNAMIC)
  1371. /*
  1372. * Support for dynamic device trees.
  1373. *
  1374. * On some platforms, the device tree can be manipulated at runtime.
  1375. * The routines in this section support adding, removing and changing
  1376. * device tree nodes.
  1377. */
  1378. static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
  1379. int of_reconfig_notifier_register(struct notifier_block *nb)
  1380. {
  1381. return blocking_notifier_chain_register(&of_reconfig_chain, nb);
  1382. }
  1383. EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
  1384. int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1385. {
  1386. return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
  1387. }
  1388. EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
  1389. int of_reconfig_notify(unsigned long action, void *p)
  1390. {
  1391. int rc;
  1392. rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
  1393. return notifier_to_errno(rc);
  1394. }
  1395. #ifdef CONFIG_PROC_DEVICETREE
  1396. static void of_add_proc_dt_entry(struct device_node *dn)
  1397. {
  1398. struct proc_dir_entry *ent;
  1399. ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
  1400. if (ent)
  1401. proc_device_tree_add_node(dn, ent);
  1402. }
  1403. #else
  1404. static void of_add_proc_dt_entry(struct device_node *dn)
  1405. {
  1406. return;
  1407. }
  1408. #endif
  1409. /**
  1410. * of_attach_node - Plug a device node into the tree and global list.
  1411. */
  1412. int of_attach_node(struct device_node *np)
  1413. {
  1414. unsigned long flags;
  1415. int rc;
  1416. rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
  1417. if (rc)
  1418. return rc;
  1419. raw_spin_lock_irqsave(&devtree_lock, flags);
  1420. np->sibling = np->parent->child;
  1421. np->allnext = of_allnodes;
  1422. np->parent->child = np;
  1423. of_allnodes = np;
  1424. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1425. of_add_proc_dt_entry(np);
  1426. return 0;
  1427. }
  1428. #ifdef CONFIG_PROC_DEVICETREE
  1429. static void of_remove_proc_dt_entry(struct device_node *dn)
  1430. {
  1431. proc_remove(dn->pde);
  1432. }
  1433. #else
  1434. static void of_remove_proc_dt_entry(struct device_node *dn)
  1435. {
  1436. return;
  1437. }
  1438. #endif
  1439. /**
  1440. * of_detach_node - "Unplug" a node from the device tree.
  1441. *
  1442. * The caller must hold a reference to the node. The memory associated with
  1443. * the node is not freed until its refcount goes to zero.
  1444. */
  1445. int of_detach_node(struct device_node *np)
  1446. {
  1447. struct device_node *parent;
  1448. unsigned long flags;
  1449. int rc = 0;
  1450. rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
  1451. if (rc)
  1452. return rc;
  1453. raw_spin_lock_irqsave(&devtree_lock, flags);
  1454. if (of_node_check_flag(np, OF_DETACHED)) {
  1455. /* someone already detached it */
  1456. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1457. return rc;
  1458. }
  1459. parent = np->parent;
  1460. if (!parent) {
  1461. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1462. return rc;
  1463. }
  1464. if (of_allnodes == np)
  1465. of_allnodes = np->allnext;
  1466. else {
  1467. struct device_node *prev;
  1468. for (prev = of_allnodes;
  1469. prev->allnext != np;
  1470. prev = prev->allnext)
  1471. ;
  1472. prev->allnext = np->allnext;
  1473. }
  1474. if (parent->child == np)
  1475. parent->child = np->sibling;
  1476. else {
  1477. struct device_node *prevsib;
  1478. for (prevsib = np->parent->child;
  1479. prevsib->sibling != np;
  1480. prevsib = prevsib->sibling)
  1481. ;
  1482. prevsib->sibling = np->sibling;
  1483. }
  1484. of_node_set_flag(np, OF_DETACHED);
  1485. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1486. of_remove_proc_dt_entry(np);
  1487. return rc;
  1488. }
  1489. #endif /* defined(CONFIG_OF_DYNAMIC) */
  1490. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1491. int id, const char *stem, int stem_len)
  1492. {
  1493. ap->np = np;
  1494. ap->id = id;
  1495. strncpy(ap->stem, stem, stem_len);
  1496. ap->stem[stem_len] = 0;
  1497. list_add_tail(&ap->link, &aliases_lookup);
  1498. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1499. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1500. }
  1501. /**
  1502. * of_alias_scan - Scan all properties of 'aliases' node
  1503. *
  1504. * The function scans all the properties of 'aliases' node and populate
  1505. * the the global lookup table with the properties. It returns the
  1506. * number of alias_prop found, or error code in error case.
  1507. *
  1508. * @dt_alloc: An allocator that provides a virtual address to memory
  1509. * for the resulting tree
  1510. */
  1511. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1512. {
  1513. struct property *pp;
  1514. of_chosen = of_find_node_by_path("/chosen");
  1515. if (of_chosen == NULL)
  1516. of_chosen = of_find_node_by_path("/chosen@0");
  1517. of_aliases = of_find_node_by_path("/aliases");
  1518. if (!of_aliases)
  1519. return;
  1520. for_each_property_of_node(of_aliases, pp) {
  1521. const char *start = pp->name;
  1522. const char *end = start + strlen(start);
  1523. struct device_node *np;
  1524. struct alias_prop *ap;
  1525. int id, len;
  1526. /* Skip those we do not want to proceed */
  1527. if (!strcmp(pp->name, "name") ||
  1528. !strcmp(pp->name, "phandle") ||
  1529. !strcmp(pp->name, "linux,phandle"))
  1530. continue;
  1531. np = of_find_node_by_path(pp->value);
  1532. if (!np)
  1533. continue;
  1534. /* walk the alias backwards to extract the id and work out
  1535. * the 'stem' string */
  1536. while (isdigit(*(end-1)) && end > start)
  1537. end--;
  1538. len = end - start;
  1539. if (kstrtoint(end, 10, &id) < 0)
  1540. continue;
  1541. /* Allocate an alias_prop with enough space for the stem */
  1542. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1543. if (!ap)
  1544. continue;
  1545. ap->alias = start;
  1546. of_alias_add(ap, np, id, start, len);
  1547. }
  1548. }
  1549. /**
  1550. * of_alias_get_id - Get alias id for the given device_node
  1551. * @np: Pointer to the given device_node
  1552. * @stem: Alias stem of the given device_node
  1553. *
  1554. * The function travels the lookup table to get alias id for the given
  1555. * device_node and alias stem. It returns the alias id if find it.
  1556. */
  1557. int of_alias_get_id(struct device_node *np, const char *stem)
  1558. {
  1559. struct alias_prop *app;
  1560. int id = -ENODEV;
  1561. mutex_lock(&of_aliases_mutex);
  1562. list_for_each_entry(app, &aliases_lookup, link) {
  1563. if (strcmp(app->stem, stem) != 0)
  1564. continue;
  1565. if (np == app->np) {
  1566. id = app->id;
  1567. break;
  1568. }
  1569. }
  1570. mutex_unlock(&of_aliases_mutex);
  1571. return id;
  1572. }
  1573. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1574. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1575. u32 *pu)
  1576. {
  1577. const void *curv = cur;
  1578. if (!prop)
  1579. return NULL;
  1580. if (!cur) {
  1581. curv = prop->value;
  1582. goto out_val;
  1583. }
  1584. curv += sizeof(*cur);
  1585. if (curv >= prop->value + prop->length)
  1586. return NULL;
  1587. out_val:
  1588. *pu = be32_to_cpup(curv);
  1589. return curv;
  1590. }
  1591. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1592. const char *of_prop_next_string(struct property *prop, const char *cur)
  1593. {
  1594. const void *curv = cur;
  1595. if (!prop)
  1596. return NULL;
  1597. if (!cur)
  1598. return prop->value;
  1599. curv += strlen(cur) + 1;
  1600. if (curv >= prop->value + prop->length)
  1601. return NULL;
  1602. return curv;
  1603. }
  1604. EXPORT_SYMBOL_GPL(of_prop_next_string);