base.c 50 KB

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