base.c 50 KB

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