base.c 50 KB

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