base.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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/console.h>
  21. #include <linux/ctype.h>
  22. #include <linux/cpu.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_graph.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/slab.h>
  28. #include <linux/string.h>
  29. #include <linux/proc_fs.h>
  30. #include "of_private.h"
  31. LIST_HEAD(aliases_lookup);
  32. struct device_node *of_root;
  33. EXPORT_SYMBOL(of_root);
  34. struct device_node *of_chosen;
  35. struct device_node *of_aliases;
  36. struct device_node *of_stdout;
  37. static const char *of_stdout_options;
  38. struct kset *of_kset;
  39. /*
  40. * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
  41. * This mutex must be held whenever modifications are being made to the
  42. * device tree. The of_{attach,detach}_node() and
  43. * of_{add,remove,update}_property() helpers make sure this happens.
  44. */
  45. DEFINE_MUTEX(of_mutex);
  46. /* use when traversing tree through the child, sibling,
  47. * or parent members of struct device_node.
  48. */
  49. DEFINE_RAW_SPINLOCK(devtree_lock);
  50. int of_n_addr_cells(struct device_node *np)
  51. {
  52. const __be32 *ip;
  53. do {
  54. if (np->parent)
  55. np = np->parent;
  56. ip = of_get_property(np, "#address-cells", NULL);
  57. if (ip)
  58. return be32_to_cpup(ip);
  59. } while (np->parent);
  60. /* No #address-cells property for the root node */
  61. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  62. }
  63. EXPORT_SYMBOL(of_n_addr_cells);
  64. int of_n_size_cells(struct device_node *np)
  65. {
  66. const __be32 *ip;
  67. do {
  68. if (np->parent)
  69. np = np->parent;
  70. ip = of_get_property(np, "#size-cells", NULL);
  71. if (ip)
  72. return be32_to_cpup(ip);
  73. } while (np->parent);
  74. /* No #size-cells property for the root node */
  75. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  76. }
  77. EXPORT_SYMBOL(of_n_size_cells);
  78. #ifdef CONFIG_NUMA
  79. int __weak of_node_to_nid(struct device_node *np)
  80. {
  81. return numa_node_id();
  82. }
  83. #endif
  84. #ifndef CONFIG_OF_DYNAMIC
  85. static void of_node_release(struct kobject *kobj)
  86. {
  87. /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
  88. }
  89. #endif /* CONFIG_OF_DYNAMIC */
  90. struct kobj_type of_node_ktype = {
  91. .release = of_node_release,
  92. };
  93. static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
  94. struct bin_attribute *bin_attr, char *buf,
  95. loff_t offset, size_t count)
  96. {
  97. struct property *pp = container_of(bin_attr, struct property, attr);
  98. return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
  99. }
  100. static const char *safe_name(struct kobject *kobj, const char *orig_name)
  101. {
  102. const char *name = orig_name;
  103. struct kernfs_node *kn;
  104. int i = 0;
  105. /* don't be a hero. After 16 tries give up */
  106. while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
  107. sysfs_put(kn);
  108. if (name != orig_name)
  109. kfree(name);
  110. name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
  111. }
  112. if (name != orig_name)
  113. pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
  114. kobject_name(kobj), name);
  115. return name;
  116. }
  117. int __of_add_property_sysfs(struct device_node *np, struct property *pp)
  118. {
  119. int rc;
  120. /* Important: Don't leak passwords */
  121. bool secure = strncmp(pp->name, "security-", 9) == 0;
  122. if (!IS_ENABLED(CONFIG_SYSFS))
  123. return 0;
  124. if (!of_kset || !of_node_is_attached(np))
  125. return 0;
  126. sysfs_bin_attr_init(&pp->attr);
  127. pp->attr.attr.name = safe_name(&np->kobj, pp->name);
  128. pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
  129. pp->attr.size = secure ? 0 : pp->length;
  130. pp->attr.read = of_node_property_read;
  131. rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
  132. WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
  133. return rc;
  134. }
  135. int __of_attach_node_sysfs(struct device_node *np)
  136. {
  137. const char *name;
  138. struct property *pp;
  139. int rc;
  140. if (!IS_ENABLED(CONFIG_SYSFS))
  141. return 0;
  142. if (!of_kset)
  143. return 0;
  144. np->kobj.kset = of_kset;
  145. if (!np->parent) {
  146. /* Nodes without parents are new top level trees */
  147. rc = kobject_add(&np->kobj, NULL, "%s",
  148. safe_name(&of_kset->kobj, "base"));
  149. } else {
  150. name = safe_name(&np->parent->kobj, kbasename(np->full_name));
  151. if (!name || !name[0])
  152. return -EINVAL;
  153. rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
  154. }
  155. if (rc)
  156. return rc;
  157. for_each_property_of_node(np, pp)
  158. __of_add_property_sysfs(np, pp);
  159. return 0;
  160. }
  161. static int __init of_init(void)
  162. {
  163. struct device_node *np;
  164. /* Create the kset, and register existing nodes */
  165. mutex_lock(&of_mutex);
  166. of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
  167. if (!of_kset) {
  168. mutex_unlock(&of_mutex);
  169. return -ENOMEM;
  170. }
  171. for_each_of_allnodes(np)
  172. __of_attach_node_sysfs(np);
  173. mutex_unlock(&of_mutex);
  174. /* Symlink in /proc as required by userspace ABI */
  175. if (of_root)
  176. proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
  177. return 0;
  178. }
  179. core_initcall(of_init);
  180. static struct property *__of_find_property(const struct device_node *np,
  181. const char *name, int *lenp)
  182. {
  183. struct property *pp;
  184. if (!np)
  185. return NULL;
  186. for (pp = np->properties; pp; pp = pp->next) {
  187. if (of_prop_cmp(pp->name, name) == 0) {
  188. if (lenp)
  189. *lenp = pp->length;
  190. break;
  191. }
  192. }
  193. return pp;
  194. }
  195. struct property *of_find_property(const struct device_node *np,
  196. const char *name,
  197. int *lenp)
  198. {
  199. struct property *pp;
  200. unsigned long flags;
  201. raw_spin_lock_irqsave(&devtree_lock, flags);
  202. pp = __of_find_property(np, name, lenp);
  203. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  204. return pp;
  205. }
  206. EXPORT_SYMBOL(of_find_property);
  207. struct device_node *__of_find_all_nodes(struct device_node *prev)
  208. {
  209. struct device_node *np;
  210. if (!prev) {
  211. np = of_root;
  212. } else if (prev->child) {
  213. np = prev->child;
  214. } else {
  215. /* Walk back up looking for a sibling, or the end of the structure */
  216. np = prev;
  217. while (np->parent && !np->sibling)
  218. np = np->parent;
  219. np = np->sibling; /* Might be null at the end of the tree */
  220. }
  221. return np;
  222. }
  223. /**
  224. * of_find_all_nodes - Get next node in global list
  225. * @prev: Previous node or NULL to start iteration
  226. * of_node_put() will be called on it
  227. *
  228. * Returns a node pointer with refcount incremented, use
  229. * of_node_put() on it when done.
  230. */
  231. struct device_node *of_find_all_nodes(struct device_node *prev)
  232. {
  233. struct device_node *np;
  234. unsigned long flags;
  235. raw_spin_lock_irqsave(&devtree_lock, flags);
  236. np = __of_find_all_nodes(prev);
  237. of_node_get(np);
  238. of_node_put(prev);
  239. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  240. return np;
  241. }
  242. EXPORT_SYMBOL(of_find_all_nodes);
  243. /*
  244. * Find a property with a given name for a given node
  245. * and return the value.
  246. */
  247. const void *__of_get_property(const struct device_node *np,
  248. const char *name, int *lenp)
  249. {
  250. struct property *pp = __of_find_property(np, name, lenp);
  251. return pp ? pp->value : NULL;
  252. }
  253. /*
  254. * Find a property with a given name for a given node
  255. * and return the value.
  256. */
  257. const void *of_get_property(const struct device_node *np, const char *name,
  258. int *lenp)
  259. {
  260. struct property *pp = of_find_property(np, name, lenp);
  261. return pp ? pp->value : NULL;
  262. }
  263. EXPORT_SYMBOL(of_get_property);
  264. /*
  265. * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  266. *
  267. * @cpu: logical cpu index of a core/thread
  268. * @phys_id: physical identifier of a core/thread
  269. *
  270. * CPU logical to physical index mapping is architecture specific.
  271. * However this __weak function provides a default match of physical
  272. * id to logical cpu index. phys_id provided here is usually values read
  273. * from the device tree which must match the hardware internal registers.
  274. *
  275. * Returns true if the physical identifier and the logical cpu index
  276. * correspond to the same core/thread, false otherwise.
  277. */
  278. bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
  279. {
  280. return (u32)phys_id == cpu;
  281. }
  282. /**
  283. * Checks if the given "prop_name" property holds the physical id of the
  284. * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
  285. * NULL, local thread number within the core is returned in it.
  286. */
  287. static bool __of_find_n_match_cpu_property(struct device_node *cpun,
  288. const char *prop_name, int cpu, unsigned int *thread)
  289. {
  290. const __be32 *cell;
  291. int ac, prop_len, tid;
  292. u64 hwid;
  293. ac = of_n_addr_cells(cpun);
  294. cell = of_get_property(cpun, prop_name, &prop_len);
  295. if (!cell || !ac)
  296. return false;
  297. prop_len /= sizeof(*cell) * ac;
  298. for (tid = 0; tid < prop_len; tid++) {
  299. hwid = of_read_number(cell, ac);
  300. if (arch_match_cpu_phys_id(cpu, hwid)) {
  301. if (thread)
  302. *thread = tid;
  303. return true;
  304. }
  305. cell += ac;
  306. }
  307. return false;
  308. }
  309. /*
  310. * arch_find_n_match_cpu_physical_id - See if the given device node is
  311. * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
  312. * else false. If 'thread' is non-NULL, the local thread number within the
  313. * core is returned in it.
  314. */
  315. bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  316. int cpu, unsigned int *thread)
  317. {
  318. /* Check for non-standard "ibm,ppc-interrupt-server#s" property
  319. * for thread ids on PowerPC. If it doesn't exist fallback to
  320. * standard "reg" property.
  321. */
  322. if (IS_ENABLED(CONFIG_PPC) &&
  323. __of_find_n_match_cpu_property(cpun,
  324. "ibm,ppc-interrupt-server#s",
  325. cpu, thread))
  326. return true;
  327. if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
  328. return true;
  329. return false;
  330. }
  331. /**
  332. * of_get_cpu_node - Get device node associated with the given logical CPU
  333. *
  334. * @cpu: CPU number(logical index) for which device node is required
  335. * @thread: if not NULL, local thread number within the physical core is
  336. * returned
  337. *
  338. * The main purpose of this function is to retrieve the device node for the
  339. * given logical CPU index. It should be used to initialize the of_node in
  340. * cpu device. Once of_node in cpu device is populated, all the further
  341. * references can use that instead.
  342. *
  343. * CPU logical to physical index mapping is architecture specific and is built
  344. * before booting secondary cores. This function uses arch_match_cpu_phys_id
  345. * which can be overridden by architecture specific implementation.
  346. *
  347. * Returns a node pointer for the logical cpu if found, else NULL.
  348. */
  349. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  350. {
  351. struct device_node *cpun;
  352. for_each_node_by_type(cpun, "cpu") {
  353. if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
  354. return cpun;
  355. }
  356. return NULL;
  357. }
  358. EXPORT_SYMBOL(of_get_cpu_node);
  359. /**
  360. * __of_device_is_compatible() - Check if the node matches given constraints
  361. * @device: pointer to node
  362. * @compat: required compatible string, NULL or "" for any match
  363. * @type: required device_type value, NULL or "" for any match
  364. * @name: required node name, NULL or "" for any match
  365. *
  366. * Checks if the given @compat, @type and @name strings match the
  367. * properties of the given @device. A constraints can be skipped by
  368. * passing NULL or an empty string as the constraint.
  369. *
  370. * Returns 0 for no match, and a positive integer on match. The return
  371. * value is a relative score with larger values indicating better
  372. * matches. The score is weighted for the most specific compatible value
  373. * to get the highest score. Matching type is next, followed by matching
  374. * name. Practically speaking, this results in the following priority
  375. * order for matches:
  376. *
  377. * 1. specific compatible && type && name
  378. * 2. specific compatible && type
  379. * 3. specific compatible && name
  380. * 4. specific compatible
  381. * 5. general compatible && type && name
  382. * 6. general compatible && type
  383. * 7. general compatible && name
  384. * 8. general compatible
  385. * 9. type && name
  386. * 10. type
  387. * 11. name
  388. */
  389. static int __of_device_is_compatible(const struct device_node *device,
  390. const char *compat, const char *type, const char *name)
  391. {
  392. struct property *prop;
  393. const char *cp;
  394. int index = 0, score = 0;
  395. /* Compatible match has highest priority */
  396. if (compat && compat[0]) {
  397. prop = __of_find_property(device, "compatible", NULL);
  398. for (cp = of_prop_next_string(prop, NULL); cp;
  399. cp = of_prop_next_string(prop, cp), index++) {
  400. if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
  401. score = INT_MAX/2 - (index << 2);
  402. break;
  403. }
  404. }
  405. if (!score)
  406. return 0;
  407. }
  408. /* Matching type is better than matching name */
  409. if (type && type[0]) {
  410. if (!device->type || of_node_cmp(type, device->type))
  411. return 0;
  412. score += 2;
  413. }
  414. /* Matching name is a bit better than not */
  415. if (name && name[0]) {
  416. if (!device->name || of_node_cmp(name, device->name))
  417. return 0;
  418. score++;
  419. }
  420. return score;
  421. }
  422. /** Checks if the given "compat" string matches one of the strings in
  423. * the device's "compatible" property
  424. */
  425. int of_device_is_compatible(const struct device_node *device,
  426. const char *compat)
  427. {
  428. unsigned long flags;
  429. int res;
  430. raw_spin_lock_irqsave(&devtree_lock, flags);
  431. res = __of_device_is_compatible(device, compat, NULL, NULL);
  432. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  433. return res;
  434. }
  435. EXPORT_SYMBOL(of_device_is_compatible);
  436. /**
  437. * of_machine_is_compatible - Test root of device tree for a given compatible value
  438. * @compat: compatible string to look for in root node's compatible property.
  439. *
  440. * Returns a positive integer if the root node has the given value in its
  441. * compatible property.
  442. */
  443. int of_machine_is_compatible(const char *compat)
  444. {
  445. struct device_node *root;
  446. int rc = 0;
  447. root = of_find_node_by_path("/");
  448. if (root) {
  449. rc = of_device_is_compatible(root, compat);
  450. of_node_put(root);
  451. }
  452. return rc;
  453. }
  454. EXPORT_SYMBOL(of_machine_is_compatible);
  455. /**
  456. * __of_device_is_available - check if a device is available for use
  457. *
  458. * @device: Node to check for availability, with locks already held
  459. *
  460. * Returns true if the status property is absent or set to "okay" or "ok",
  461. * false otherwise
  462. */
  463. static bool __of_device_is_available(const struct device_node *device)
  464. {
  465. const char *status;
  466. int statlen;
  467. if (!device)
  468. return false;
  469. status = __of_get_property(device, "status", &statlen);
  470. if (status == NULL)
  471. return true;
  472. if (statlen > 0) {
  473. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  474. return true;
  475. }
  476. return false;
  477. }
  478. /**
  479. * of_device_is_available - check if a device is available for use
  480. *
  481. * @device: Node to check for availability
  482. *
  483. * Returns true if the status property is absent or set to "okay" or "ok",
  484. * false otherwise
  485. */
  486. bool of_device_is_available(const struct device_node *device)
  487. {
  488. unsigned long flags;
  489. bool res;
  490. raw_spin_lock_irqsave(&devtree_lock, flags);
  491. res = __of_device_is_available(device);
  492. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  493. return res;
  494. }
  495. EXPORT_SYMBOL(of_device_is_available);
  496. /**
  497. * of_get_parent - Get a node's parent if any
  498. * @node: Node to get parent
  499. *
  500. * Returns a node pointer with refcount incremented, use
  501. * of_node_put() on it when done.
  502. */
  503. struct device_node *of_get_parent(const struct device_node *node)
  504. {
  505. struct device_node *np;
  506. unsigned long flags;
  507. if (!node)
  508. return NULL;
  509. raw_spin_lock_irqsave(&devtree_lock, flags);
  510. np = of_node_get(node->parent);
  511. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  512. return np;
  513. }
  514. EXPORT_SYMBOL(of_get_parent);
  515. /**
  516. * of_get_next_parent - Iterate to a node's parent
  517. * @node: Node to get parent of
  518. *
  519. * This is like of_get_parent() except that it drops the
  520. * refcount on the passed node, making it suitable for iterating
  521. * through a node's parents.
  522. *
  523. * Returns a node pointer with refcount incremented, use
  524. * of_node_put() on it when done.
  525. */
  526. struct device_node *of_get_next_parent(struct device_node *node)
  527. {
  528. struct device_node *parent;
  529. unsigned long flags;
  530. if (!node)
  531. return NULL;
  532. raw_spin_lock_irqsave(&devtree_lock, flags);
  533. parent = of_node_get(node->parent);
  534. of_node_put(node);
  535. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  536. return parent;
  537. }
  538. EXPORT_SYMBOL(of_get_next_parent);
  539. static struct device_node *__of_get_next_child(const struct device_node *node,
  540. struct device_node *prev)
  541. {
  542. struct device_node *next;
  543. if (!node)
  544. return NULL;
  545. next = prev ? prev->sibling : node->child;
  546. for (; next; next = next->sibling)
  547. if (of_node_get(next))
  548. break;
  549. of_node_put(prev);
  550. return next;
  551. }
  552. #define __for_each_child_of_node(parent, child) \
  553. for (child = __of_get_next_child(parent, NULL); child != NULL; \
  554. child = __of_get_next_child(parent, child))
  555. /**
  556. * of_get_next_child - Iterate a node childs
  557. * @node: parent node
  558. * @prev: previous child of the parent node, or NULL to get first
  559. *
  560. * Returns a node pointer with refcount incremented, use
  561. * of_node_put() on it when done.
  562. */
  563. struct device_node *of_get_next_child(const struct device_node *node,
  564. struct device_node *prev)
  565. {
  566. struct device_node *next;
  567. unsigned long flags;
  568. raw_spin_lock_irqsave(&devtree_lock, flags);
  569. next = __of_get_next_child(node, prev);
  570. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  571. return next;
  572. }
  573. EXPORT_SYMBOL(of_get_next_child);
  574. /**
  575. * of_get_next_available_child - Find the next available child node
  576. * @node: parent node
  577. * @prev: previous child of the parent node, or NULL to get first
  578. *
  579. * This function is like of_get_next_child(), except that it
  580. * automatically skips any disabled nodes (i.e. status = "disabled").
  581. */
  582. struct device_node *of_get_next_available_child(const struct device_node *node,
  583. struct device_node *prev)
  584. {
  585. struct device_node *next;
  586. unsigned long flags;
  587. if (!node)
  588. return NULL;
  589. raw_spin_lock_irqsave(&devtree_lock, flags);
  590. next = prev ? prev->sibling : node->child;
  591. for (; next; next = next->sibling) {
  592. if (!__of_device_is_available(next))
  593. continue;
  594. if (of_node_get(next))
  595. break;
  596. }
  597. of_node_put(prev);
  598. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  599. return next;
  600. }
  601. EXPORT_SYMBOL(of_get_next_available_child);
  602. /**
  603. * of_get_child_by_name - Find the child node by name for a given parent
  604. * @node: parent node
  605. * @name: child name to look for.
  606. *
  607. * This function looks for child node for given matching name
  608. *
  609. * Returns a node pointer if found, with refcount incremented, use
  610. * of_node_put() on it when done.
  611. * Returns NULL if node is not found.
  612. */
  613. struct device_node *of_get_child_by_name(const struct device_node *node,
  614. const char *name)
  615. {
  616. struct device_node *child;
  617. for_each_child_of_node(node, child)
  618. if (child->name && (of_node_cmp(child->name, name) == 0))
  619. break;
  620. return child;
  621. }
  622. EXPORT_SYMBOL(of_get_child_by_name);
  623. static struct device_node *__of_find_node_by_path(struct device_node *parent,
  624. const char *path)
  625. {
  626. struct device_node *child;
  627. int len = strchrnul(path, '/') - path;
  628. int term;
  629. if (!len)
  630. return NULL;
  631. term = strchrnul(path, ':') - path;
  632. if (term < len)
  633. len = term;
  634. __for_each_child_of_node(parent, child) {
  635. const char *name = strrchr(child->full_name, '/');
  636. if (WARN(!name, "malformed device_node %s\n", child->full_name))
  637. continue;
  638. name++;
  639. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  640. return child;
  641. }
  642. return NULL;
  643. }
  644. /**
  645. * of_find_node_opts_by_path - Find a node matching a full OF path
  646. * @path: Either the full path to match, or if the path does not
  647. * start with '/', the name of a property of the /aliases
  648. * node (an alias). In the case of an alias, the node
  649. * matching the alias' value will be returned.
  650. * @opts: Address of a pointer into which to store the start of
  651. * an options string appended to the end of the path with
  652. * a ':' separator.
  653. *
  654. * Valid paths:
  655. * /foo/bar Full path
  656. * foo Valid alias
  657. * foo/bar Valid alias + relative path
  658. *
  659. * Returns a node pointer with refcount incremented, use
  660. * of_node_put() on it when done.
  661. */
  662. struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
  663. {
  664. struct device_node *np = NULL;
  665. struct property *pp;
  666. unsigned long flags;
  667. const char *separator = strchr(path, ':');
  668. if (opts)
  669. *opts = separator ? separator + 1 : NULL;
  670. if (strcmp(path, "/") == 0)
  671. return of_node_get(of_root);
  672. /* The path could begin with an alias */
  673. if (*path != '/') {
  674. char *p = strchrnul(path, '/');
  675. int len = separator ? separator - path : p - path;
  676. /* of_aliases must not be NULL */
  677. if (!of_aliases)
  678. return NULL;
  679. for_each_property_of_node(of_aliases, pp) {
  680. if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
  681. np = of_find_node_by_path(pp->value);
  682. break;
  683. }
  684. }
  685. if (!np)
  686. return NULL;
  687. path = p;
  688. }
  689. /* Step down the tree matching path components */
  690. raw_spin_lock_irqsave(&devtree_lock, flags);
  691. if (!np)
  692. np = of_node_get(of_root);
  693. while (np && *path == '/') {
  694. path++; /* Increment past '/' delimiter */
  695. np = __of_find_node_by_path(np, path);
  696. path = strchrnul(path, '/');
  697. }
  698. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  699. return np;
  700. }
  701. EXPORT_SYMBOL(of_find_node_opts_by_path);
  702. /**
  703. * of_find_node_by_name - Find a node by its "name" property
  704. * @from: The node to start searching from or NULL, the node
  705. * you pass will not be searched, only the next one
  706. * will; typically, you pass what the previous call
  707. * returned. of_node_put() will be called on it
  708. * @name: The name string to match against
  709. *
  710. * Returns a node pointer with refcount incremented, use
  711. * of_node_put() on it when done.
  712. */
  713. struct device_node *of_find_node_by_name(struct device_node *from,
  714. const char *name)
  715. {
  716. struct device_node *np;
  717. unsigned long flags;
  718. raw_spin_lock_irqsave(&devtree_lock, flags);
  719. for_each_of_allnodes_from(from, np)
  720. if (np->name && (of_node_cmp(np->name, name) == 0)
  721. && of_node_get(np))
  722. break;
  723. of_node_put(from);
  724. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  725. return np;
  726. }
  727. EXPORT_SYMBOL(of_find_node_by_name);
  728. /**
  729. * of_find_node_by_type - Find a node by its "device_type" property
  730. * @from: The node to start searching from, or NULL to start searching
  731. * the entire device tree. The node you pass will not be
  732. * searched, only the next one will; typically, you pass
  733. * what the previous call returned. of_node_put() will be
  734. * called on from for you.
  735. * @type: The type string to match against
  736. *
  737. * Returns a node pointer with refcount incremented, use
  738. * of_node_put() on it when done.
  739. */
  740. struct device_node *of_find_node_by_type(struct device_node *from,
  741. const char *type)
  742. {
  743. struct device_node *np;
  744. unsigned long flags;
  745. raw_spin_lock_irqsave(&devtree_lock, flags);
  746. for_each_of_allnodes_from(from, np)
  747. if (np->type && (of_node_cmp(np->type, type) == 0)
  748. && of_node_get(np))
  749. break;
  750. of_node_put(from);
  751. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  752. return np;
  753. }
  754. EXPORT_SYMBOL(of_find_node_by_type);
  755. /**
  756. * of_find_compatible_node - Find a node based on type and one of the
  757. * tokens in its "compatible" property
  758. * @from: The node to start searching from or NULL, the node
  759. * you pass will not be searched, only the next one
  760. * will; typically, you pass what the previous call
  761. * returned. of_node_put() will be called on it
  762. * @type: The type string to match "device_type" or NULL to ignore
  763. * @compatible: The string to match to one of the tokens in the device
  764. * "compatible" list.
  765. *
  766. * Returns a node pointer with refcount incremented, use
  767. * of_node_put() on it when done.
  768. */
  769. struct device_node *of_find_compatible_node(struct device_node *from,
  770. const char *type, const char *compatible)
  771. {
  772. struct device_node *np;
  773. unsigned long flags;
  774. raw_spin_lock_irqsave(&devtree_lock, flags);
  775. for_each_of_allnodes_from(from, np)
  776. if (__of_device_is_compatible(np, compatible, type, NULL) &&
  777. of_node_get(np))
  778. break;
  779. of_node_put(from);
  780. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  781. return np;
  782. }
  783. EXPORT_SYMBOL(of_find_compatible_node);
  784. /**
  785. * of_find_node_with_property - Find a node which has a property with
  786. * the given name.
  787. * @from: The node to start searching from or NULL, the node
  788. * you pass will not be searched, only the next one
  789. * will; typically, you pass what the previous call
  790. * returned. of_node_put() will be called on it
  791. * @prop_name: The name of the property to look for.
  792. *
  793. * Returns a node pointer with refcount incremented, use
  794. * of_node_put() on it when done.
  795. */
  796. struct device_node *of_find_node_with_property(struct device_node *from,
  797. const char *prop_name)
  798. {
  799. struct device_node *np;
  800. struct property *pp;
  801. unsigned long flags;
  802. raw_spin_lock_irqsave(&devtree_lock, flags);
  803. for_each_of_allnodes_from(from, np) {
  804. for (pp = np->properties; pp; pp = pp->next) {
  805. if (of_prop_cmp(pp->name, prop_name) == 0) {
  806. of_node_get(np);
  807. goto out;
  808. }
  809. }
  810. }
  811. out:
  812. of_node_put(from);
  813. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  814. return np;
  815. }
  816. EXPORT_SYMBOL(of_find_node_with_property);
  817. static
  818. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  819. const struct device_node *node)
  820. {
  821. const struct of_device_id *best_match = NULL;
  822. int score, best_score = 0;
  823. if (!matches)
  824. return NULL;
  825. for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
  826. score = __of_device_is_compatible(node, matches->compatible,
  827. matches->type, matches->name);
  828. if (score > best_score) {
  829. best_match = matches;
  830. best_score = score;
  831. }
  832. }
  833. return best_match;
  834. }
  835. /**
  836. * of_match_node - Tell if a device_node has a matching of_match structure
  837. * @matches: array of of device match structures to search in
  838. * @node: the of device structure to match against
  839. *
  840. * Low level utility function used by device matching.
  841. */
  842. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  843. const struct device_node *node)
  844. {
  845. const struct of_device_id *match;
  846. unsigned long flags;
  847. raw_spin_lock_irqsave(&devtree_lock, flags);
  848. match = __of_match_node(matches, node);
  849. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  850. return match;
  851. }
  852. EXPORT_SYMBOL(of_match_node);
  853. /**
  854. * of_find_matching_node_and_match - Find a node based on an of_device_id
  855. * match table.
  856. * @from: The node to start searching from or NULL, the node
  857. * you pass will not be searched, only the next one
  858. * will; typically, you pass what the previous call
  859. * returned. of_node_put() will be called on it
  860. * @matches: array of of device match structures to search in
  861. * @match Updated to point at the matches entry which matched
  862. *
  863. * Returns a node pointer with refcount incremented, use
  864. * of_node_put() on it when done.
  865. */
  866. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  867. const struct of_device_id *matches,
  868. const struct of_device_id **match)
  869. {
  870. struct device_node *np;
  871. const struct of_device_id *m;
  872. unsigned long flags;
  873. if (match)
  874. *match = NULL;
  875. raw_spin_lock_irqsave(&devtree_lock, flags);
  876. for_each_of_allnodes_from(from, np) {
  877. m = __of_match_node(matches, np);
  878. if (m && of_node_get(np)) {
  879. if (match)
  880. *match = m;
  881. break;
  882. }
  883. }
  884. of_node_put(from);
  885. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  886. return np;
  887. }
  888. EXPORT_SYMBOL(of_find_matching_node_and_match);
  889. /**
  890. * of_modalias_node - Lookup appropriate modalias for a device node
  891. * @node: pointer to a device tree node
  892. * @modalias: Pointer to buffer that modalias value will be copied into
  893. * @len: Length of modalias value
  894. *
  895. * Based on the value of the compatible property, this routine will attempt
  896. * to choose an appropriate modalias value for a particular device tree node.
  897. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  898. * from the first entry in the compatible list property.
  899. *
  900. * This routine returns 0 on success, <0 on failure.
  901. */
  902. int of_modalias_node(struct device_node *node, char *modalias, int len)
  903. {
  904. const char *compatible, *p;
  905. int cplen;
  906. compatible = of_get_property(node, "compatible", &cplen);
  907. if (!compatible || strlen(compatible) > cplen)
  908. return -ENODEV;
  909. p = strchr(compatible, ',');
  910. strlcpy(modalias, p ? p + 1 : compatible, len);
  911. return 0;
  912. }
  913. EXPORT_SYMBOL_GPL(of_modalias_node);
  914. /**
  915. * of_find_node_by_phandle - Find a node given a phandle
  916. * @handle: phandle of the node to find
  917. *
  918. * Returns a node pointer with refcount incremented, use
  919. * of_node_put() on it when done.
  920. */
  921. struct device_node *of_find_node_by_phandle(phandle handle)
  922. {
  923. struct device_node *np;
  924. unsigned long flags;
  925. if (!handle)
  926. return NULL;
  927. raw_spin_lock_irqsave(&devtree_lock, flags);
  928. for_each_of_allnodes(np)
  929. if (np->phandle == handle)
  930. break;
  931. of_node_get(np);
  932. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  933. return np;
  934. }
  935. EXPORT_SYMBOL(of_find_node_by_phandle);
  936. /**
  937. * of_property_count_elems_of_size - Count the number of elements in a property
  938. *
  939. * @np: device node from which the property value is to be read.
  940. * @propname: name of the property to be searched.
  941. * @elem_size: size of the individual element
  942. *
  943. * Search for a property in a device node and count the number of elements of
  944. * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
  945. * property does not exist or its length does not match a multiple of elem_size
  946. * and -ENODATA if the property does not have a value.
  947. */
  948. int of_property_count_elems_of_size(const struct device_node *np,
  949. const char *propname, int elem_size)
  950. {
  951. struct property *prop = of_find_property(np, propname, NULL);
  952. if (!prop)
  953. return -EINVAL;
  954. if (!prop->value)
  955. return -ENODATA;
  956. if (prop->length % elem_size != 0) {
  957. pr_err("size of %s in node %s is not a multiple of %d\n",
  958. propname, np->full_name, elem_size);
  959. return -EINVAL;
  960. }
  961. return prop->length / elem_size;
  962. }
  963. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  964. /**
  965. * of_find_property_value_of_size
  966. *
  967. * @np: device node from which the property value is to be read.
  968. * @propname: name of the property to be searched.
  969. * @len: requested length of property value
  970. *
  971. * Search for a property in a device node and valid the requested size.
  972. * Returns the property value on success, -EINVAL if the property does not
  973. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  974. * property data isn't large enough.
  975. *
  976. */
  977. static void *of_find_property_value_of_size(const struct device_node *np,
  978. const char *propname, u32 len)
  979. {
  980. struct property *prop = of_find_property(np, propname, NULL);
  981. if (!prop)
  982. return ERR_PTR(-EINVAL);
  983. if (!prop->value)
  984. return ERR_PTR(-ENODATA);
  985. if (len > prop->length)
  986. return ERR_PTR(-EOVERFLOW);
  987. return prop->value;
  988. }
  989. /**
  990. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  991. *
  992. * @np: device node from which the property value is to be read.
  993. * @propname: name of the property to be searched.
  994. * @index: index of the u32 in the list of values
  995. * @out_value: pointer to return value, modified only if no error.
  996. *
  997. * Search for a property in a device node and read nth 32-bit value from
  998. * it. Returns 0 on success, -EINVAL if the property does not exist,
  999. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1000. * property data isn't large enough.
  1001. *
  1002. * The out_value is modified only if a valid u32 value can be decoded.
  1003. */
  1004. int of_property_read_u32_index(const struct device_node *np,
  1005. const char *propname,
  1006. u32 index, u32 *out_value)
  1007. {
  1008. const u32 *val = of_find_property_value_of_size(np, propname,
  1009. ((index + 1) * sizeof(*out_value)));
  1010. if (IS_ERR(val))
  1011. return PTR_ERR(val);
  1012. *out_value = be32_to_cpup(((__be32 *)val) + index);
  1013. return 0;
  1014. }
  1015. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  1016. /**
  1017. * of_property_read_u8_array - Find and read an array of u8 from a property.
  1018. *
  1019. * @np: device node from which the property value is to be read.
  1020. * @propname: name of the property to be searched.
  1021. * @out_values: pointer to return value, modified only if return value is 0.
  1022. * @sz: number of array elements to read
  1023. *
  1024. * Search for a property in a device node and read 8-bit value(s) from
  1025. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1026. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1027. * property data isn't large enough.
  1028. *
  1029. * dts entry of array should be like:
  1030. * property = /bits/ 8 <0x50 0x60 0x70>;
  1031. *
  1032. * The out_values is modified only if a valid u8 value can be decoded.
  1033. */
  1034. int of_property_read_u8_array(const struct device_node *np,
  1035. const char *propname, u8 *out_values, size_t sz)
  1036. {
  1037. const u8 *val = of_find_property_value_of_size(np, propname,
  1038. (sz * sizeof(*out_values)));
  1039. if (IS_ERR(val))
  1040. return PTR_ERR(val);
  1041. while (sz--)
  1042. *out_values++ = *val++;
  1043. return 0;
  1044. }
  1045. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  1046. /**
  1047. * of_property_read_u16_array - Find and read an array of u16 from a property.
  1048. *
  1049. * @np: device node from which the property value is to be read.
  1050. * @propname: name of the property to be searched.
  1051. * @out_values: pointer to return value, modified only if return value is 0.
  1052. * @sz: number of array elements to read
  1053. *
  1054. * Search for a property in a device node and read 16-bit value(s) from
  1055. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1056. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1057. * property data isn't large enough.
  1058. *
  1059. * dts entry of array should be like:
  1060. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  1061. *
  1062. * The out_values is modified only if a valid u16 value can be decoded.
  1063. */
  1064. int of_property_read_u16_array(const struct device_node *np,
  1065. const char *propname, u16 *out_values, size_t sz)
  1066. {
  1067. const __be16 *val = of_find_property_value_of_size(np, propname,
  1068. (sz * sizeof(*out_values)));
  1069. if (IS_ERR(val))
  1070. return PTR_ERR(val);
  1071. while (sz--)
  1072. *out_values++ = be16_to_cpup(val++);
  1073. return 0;
  1074. }
  1075. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  1076. /**
  1077. * of_property_read_u32_array - Find and read an array of 32 bit integers
  1078. * from a property.
  1079. *
  1080. * @np: device node from which the property value is to be read.
  1081. * @propname: name of the property to be searched.
  1082. * @out_values: pointer to return value, modified only if return value is 0.
  1083. * @sz: number of array elements to read
  1084. *
  1085. * Search for a property in a device node and read 32-bit value(s) from
  1086. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1087. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1088. * property data isn't large enough.
  1089. *
  1090. * The out_values is modified only if a valid u32 value can be decoded.
  1091. */
  1092. int of_property_read_u32_array(const struct device_node *np,
  1093. const char *propname, u32 *out_values,
  1094. size_t sz)
  1095. {
  1096. const __be32 *val = of_find_property_value_of_size(np, propname,
  1097. (sz * sizeof(*out_values)));
  1098. if (IS_ERR(val))
  1099. return PTR_ERR(val);
  1100. while (sz--)
  1101. *out_values++ = be32_to_cpup(val++);
  1102. return 0;
  1103. }
  1104. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  1105. /**
  1106. * of_property_read_u64 - Find and read a 64 bit integer from a property
  1107. * @np: device node from which the property value is to be read.
  1108. * @propname: name of the property to be searched.
  1109. * @out_value: pointer to return value, modified only if return value is 0.
  1110. *
  1111. * Search for a property in a device node and read a 64-bit value from
  1112. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1113. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1114. * property data isn't large enough.
  1115. *
  1116. * The out_value is modified only if a valid u64 value can be decoded.
  1117. */
  1118. int of_property_read_u64(const struct device_node *np, const char *propname,
  1119. u64 *out_value)
  1120. {
  1121. const __be32 *val = of_find_property_value_of_size(np, propname,
  1122. sizeof(*out_value));
  1123. if (IS_ERR(val))
  1124. return PTR_ERR(val);
  1125. *out_value = of_read_number(val, 2);
  1126. return 0;
  1127. }
  1128. EXPORT_SYMBOL_GPL(of_property_read_u64);
  1129. /**
  1130. * of_property_read_u64_array - Find and read an array of 64 bit integers
  1131. * from a property.
  1132. *
  1133. * @np: device node from which the property value is to be read.
  1134. * @propname: name of the property to be searched.
  1135. * @out_values: pointer to return value, modified only if return value is 0.
  1136. * @sz: number of array elements to read
  1137. *
  1138. * Search for a property in a device node and read 64-bit value(s) from
  1139. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1140. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1141. * property data isn't large enough.
  1142. *
  1143. * The out_values is modified only if a valid u64 value can be decoded.
  1144. */
  1145. int of_property_read_u64_array(const struct device_node *np,
  1146. const char *propname, u64 *out_values,
  1147. size_t sz)
  1148. {
  1149. const __be32 *val = of_find_property_value_of_size(np, propname,
  1150. (sz * sizeof(*out_values)));
  1151. if (IS_ERR(val))
  1152. return PTR_ERR(val);
  1153. while (sz--) {
  1154. *out_values++ = of_read_number(val, 2);
  1155. val += 2;
  1156. }
  1157. return 0;
  1158. }
  1159. EXPORT_SYMBOL_GPL(of_property_read_u64_array);
  1160. /**
  1161. * of_property_read_string - Find and read a string from a property
  1162. * @np: device node from which the property value is to be read.
  1163. * @propname: name of the property to be searched.
  1164. * @out_string: pointer to null terminated return string, modified only if
  1165. * return value is 0.
  1166. *
  1167. * Search for a property in a device tree node and retrieve a null
  1168. * terminated string value (pointer to data, not a copy). Returns 0 on
  1169. * success, -EINVAL if the property does not exist, -ENODATA if property
  1170. * does not have a value, and -EILSEQ if the string is not null-terminated
  1171. * within the length of the property data.
  1172. *
  1173. * The out_string pointer is modified only if a valid string can be decoded.
  1174. */
  1175. int of_property_read_string(struct device_node *np, const char *propname,
  1176. const char **out_string)
  1177. {
  1178. struct property *prop = of_find_property(np, propname, NULL);
  1179. if (!prop)
  1180. return -EINVAL;
  1181. if (!prop->value)
  1182. return -ENODATA;
  1183. if (strnlen(prop->value, prop->length) >= prop->length)
  1184. return -EILSEQ;
  1185. *out_string = prop->value;
  1186. return 0;
  1187. }
  1188. EXPORT_SYMBOL_GPL(of_property_read_string);
  1189. /**
  1190. * of_property_match_string() - Find string in a list and return index
  1191. * @np: pointer to node containing string list property
  1192. * @propname: string list property name
  1193. * @string: pointer to string to search for in string list
  1194. *
  1195. * This function searches a string list property and returns the index
  1196. * of a specific string value.
  1197. */
  1198. int of_property_match_string(struct device_node *np, const char *propname,
  1199. const char *string)
  1200. {
  1201. struct property *prop = of_find_property(np, propname, NULL);
  1202. size_t l;
  1203. int i;
  1204. const char *p, *end;
  1205. if (!prop)
  1206. return -EINVAL;
  1207. if (!prop->value)
  1208. return -ENODATA;
  1209. p = prop->value;
  1210. end = p + prop->length;
  1211. for (i = 0; p < end; i++, p += l) {
  1212. l = strnlen(p, end - p) + 1;
  1213. if (p + l > end)
  1214. return -EILSEQ;
  1215. pr_debug("comparing %s with %s\n", string, p);
  1216. if (strcmp(string, p) == 0)
  1217. return i; /* Found it; return index */
  1218. }
  1219. return -ENODATA;
  1220. }
  1221. EXPORT_SYMBOL_GPL(of_property_match_string);
  1222. /**
  1223. * of_property_read_string_helper() - Utility helper for parsing string properties
  1224. * @np: device node from which the property value is to be read.
  1225. * @propname: name of the property to be searched.
  1226. * @out_strs: output array of string pointers.
  1227. * @sz: number of array elements to read.
  1228. * @skip: Number of strings to skip over at beginning of list.
  1229. *
  1230. * Don't call this function directly. It is a utility helper for the
  1231. * of_property_read_string*() family of functions.
  1232. */
  1233. int of_property_read_string_helper(struct device_node *np, const char *propname,
  1234. const char **out_strs, size_t sz, int skip)
  1235. {
  1236. struct property *prop = of_find_property(np, propname, NULL);
  1237. int l = 0, i = 0;
  1238. const char *p, *end;
  1239. if (!prop)
  1240. return -EINVAL;
  1241. if (!prop->value)
  1242. return -ENODATA;
  1243. p = prop->value;
  1244. end = p + prop->length;
  1245. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  1246. l = strnlen(p, end - p) + 1;
  1247. if (p + l > end)
  1248. return -EILSEQ;
  1249. if (out_strs && i >= skip)
  1250. *out_strs++ = p;
  1251. }
  1252. i -= skip;
  1253. return i <= 0 ? -ENODATA : i;
  1254. }
  1255. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  1256. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1257. {
  1258. int i;
  1259. printk("%s %s", msg, of_node_full_name(args->np));
  1260. for (i = 0; i < args->args_count; i++)
  1261. printk(i ? ",%08x" : ":%08x", args->args[i]);
  1262. printk("\n");
  1263. }
  1264. static int __of_parse_phandle_with_args(const struct device_node *np,
  1265. const char *list_name,
  1266. const char *cells_name,
  1267. int cell_count, int index,
  1268. struct of_phandle_args *out_args)
  1269. {
  1270. const __be32 *list, *list_end;
  1271. int rc = 0, size, cur_index = 0;
  1272. uint32_t count = 0;
  1273. struct device_node *node = NULL;
  1274. phandle phandle;
  1275. /* Retrieve the phandle list property */
  1276. list = of_get_property(np, list_name, &size);
  1277. if (!list)
  1278. return -ENOENT;
  1279. list_end = list + size / sizeof(*list);
  1280. /* Loop over the phandles until all the requested entry is found */
  1281. while (list < list_end) {
  1282. rc = -EINVAL;
  1283. count = 0;
  1284. /*
  1285. * If phandle is 0, then it is an empty entry with no
  1286. * arguments. Skip forward to the next entry.
  1287. */
  1288. phandle = be32_to_cpup(list++);
  1289. if (phandle) {
  1290. /*
  1291. * Find the provider node and parse the #*-cells
  1292. * property to determine the argument length.
  1293. *
  1294. * This is not needed if the cell count is hard-coded
  1295. * (i.e. cells_name not set, but cell_count is set),
  1296. * except when we're going to return the found node
  1297. * below.
  1298. */
  1299. if (cells_name || cur_index == index) {
  1300. node = of_find_node_by_phandle(phandle);
  1301. if (!node) {
  1302. pr_err("%s: could not find phandle\n",
  1303. np->full_name);
  1304. goto err;
  1305. }
  1306. }
  1307. if (cells_name) {
  1308. if (of_property_read_u32(node, cells_name,
  1309. &count)) {
  1310. pr_err("%s: could not get %s for %s\n",
  1311. np->full_name, cells_name,
  1312. node->full_name);
  1313. goto err;
  1314. }
  1315. } else {
  1316. count = cell_count;
  1317. }
  1318. /*
  1319. * Make sure that the arguments actually fit in the
  1320. * remaining property data length
  1321. */
  1322. if (list + count > list_end) {
  1323. pr_err("%s: arguments longer than property\n",
  1324. np->full_name);
  1325. goto err;
  1326. }
  1327. }
  1328. /*
  1329. * All of the error cases above bail out of the loop, so at
  1330. * this point, the parsing is successful. If the requested
  1331. * index matches, then fill the out_args structure and return,
  1332. * or return -ENOENT for an empty entry.
  1333. */
  1334. rc = -ENOENT;
  1335. if (cur_index == index) {
  1336. if (!phandle)
  1337. goto err;
  1338. if (out_args) {
  1339. int i;
  1340. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1341. count = MAX_PHANDLE_ARGS;
  1342. out_args->np = node;
  1343. out_args->args_count = count;
  1344. for (i = 0; i < count; i++)
  1345. out_args->args[i] = be32_to_cpup(list++);
  1346. } else {
  1347. of_node_put(node);
  1348. }
  1349. /* Found it! return success */
  1350. return 0;
  1351. }
  1352. of_node_put(node);
  1353. node = NULL;
  1354. list += count;
  1355. cur_index++;
  1356. }
  1357. /*
  1358. * Unlock node before returning result; will be one of:
  1359. * -ENOENT : index is for empty phandle
  1360. * -EINVAL : parsing error on data
  1361. * [1..n] : Number of phandle (count mode; when index = -1)
  1362. */
  1363. rc = index < 0 ? cur_index : -ENOENT;
  1364. err:
  1365. if (node)
  1366. of_node_put(node);
  1367. return rc;
  1368. }
  1369. /**
  1370. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1371. * @np: Pointer to device node holding phandle property
  1372. * @phandle_name: Name of property holding a phandle value
  1373. * @index: For properties holding a table of phandles, this is the index into
  1374. * the table
  1375. *
  1376. * Returns the device_node pointer with refcount incremented. Use
  1377. * of_node_put() on it when done.
  1378. */
  1379. struct device_node *of_parse_phandle(const struct device_node *np,
  1380. const char *phandle_name, int index)
  1381. {
  1382. struct of_phandle_args args;
  1383. if (index < 0)
  1384. return NULL;
  1385. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1386. index, &args))
  1387. return NULL;
  1388. return args.np;
  1389. }
  1390. EXPORT_SYMBOL(of_parse_phandle);
  1391. /**
  1392. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1393. * @np: pointer to a device tree node containing a list
  1394. * @list_name: property name that contains a list
  1395. * @cells_name: property name that specifies phandles' arguments count
  1396. * @index: index of a phandle to parse out
  1397. * @out_args: optional pointer to output arguments structure (will be filled)
  1398. *
  1399. * This function is useful to parse lists of phandles and their arguments.
  1400. * Returns 0 on success and fills out_args, on error returns appropriate
  1401. * errno value.
  1402. *
  1403. * Caller is responsible to call of_node_put() on the returned out_args->np
  1404. * pointer.
  1405. *
  1406. * Example:
  1407. *
  1408. * phandle1: node1 {
  1409. * #list-cells = <2>;
  1410. * }
  1411. *
  1412. * phandle2: node2 {
  1413. * #list-cells = <1>;
  1414. * }
  1415. *
  1416. * node3 {
  1417. * list = <&phandle1 1 2 &phandle2 3>;
  1418. * }
  1419. *
  1420. * To get a device_node of the `node2' node you may call this:
  1421. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1422. */
  1423. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1424. const char *cells_name, int index,
  1425. struct of_phandle_args *out_args)
  1426. {
  1427. if (index < 0)
  1428. return -EINVAL;
  1429. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1430. index, out_args);
  1431. }
  1432. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1433. /**
  1434. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1435. * @np: pointer to a device tree node containing a list
  1436. * @list_name: property name that contains a list
  1437. * @cell_count: number of argument cells following the phandle
  1438. * @index: index of a phandle to parse out
  1439. * @out_args: optional pointer to output arguments structure (will be filled)
  1440. *
  1441. * This function is useful to parse lists of phandles and their arguments.
  1442. * Returns 0 on success and fills out_args, on error returns appropriate
  1443. * errno value.
  1444. *
  1445. * Caller is responsible to call of_node_put() on the returned out_args->np
  1446. * pointer.
  1447. *
  1448. * Example:
  1449. *
  1450. * phandle1: node1 {
  1451. * }
  1452. *
  1453. * phandle2: node2 {
  1454. * }
  1455. *
  1456. * node3 {
  1457. * list = <&phandle1 0 2 &phandle2 2 3>;
  1458. * }
  1459. *
  1460. * To get a device_node of the `node2' node you may call this:
  1461. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1462. */
  1463. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1464. const char *list_name, int cell_count,
  1465. int index, struct of_phandle_args *out_args)
  1466. {
  1467. if (index < 0)
  1468. return -EINVAL;
  1469. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1470. index, out_args);
  1471. }
  1472. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1473. /**
  1474. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1475. * @np: pointer to a device tree node containing a list
  1476. * @list_name: property name that contains a list
  1477. * @cells_name: property name that specifies phandles' arguments count
  1478. *
  1479. * Returns the number of phandle + argument tuples within a property. It
  1480. * is a typical pattern to encode a list of phandle and variable
  1481. * arguments into a single property. The number of arguments is encoded
  1482. * by a property in the phandle-target node. For example, a gpios
  1483. * property would contain a list of GPIO specifies consisting of a
  1484. * phandle and 1 or more arguments. The number of arguments are
  1485. * determined by the #gpio-cells property in the node pointed to by the
  1486. * phandle.
  1487. */
  1488. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1489. const char *cells_name)
  1490. {
  1491. return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
  1492. NULL);
  1493. }
  1494. EXPORT_SYMBOL(of_count_phandle_with_args);
  1495. /**
  1496. * __of_add_property - Add a property to a node without lock operations
  1497. */
  1498. int __of_add_property(struct device_node *np, struct property *prop)
  1499. {
  1500. struct property **next;
  1501. prop->next = NULL;
  1502. next = &np->properties;
  1503. while (*next) {
  1504. if (strcmp(prop->name, (*next)->name) == 0)
  1505. /* duplicate ! don't insert it */
  1506. return -EEXIST;
  1507. next = &(*next)->next;
  1508. }
  1509. *next = prop;
  1510. return 0;
  1511. }
  1512. /**
  1513. * of_add_property - Add a property to a node
  1514. */
  1515. int of_add_property(struct device_node *np, struct property *prop)
  1516. {
  1517. unsigned long flags;
  1518. int rc;
  1519. mutex_lock(&of_mutex);
  1520. raw_spin_lock_irqsave(&devtree_lock, flags);
  1521. rc = __of_add_property(np, prop);
  1522. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1523. if (!rc)
  1524. __of_add_property_sysfs(np, prop);
  1525. mutex_unlock(&of_mutex);
  1526. if (!rc)
  1527. of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
  1528. return rc;
  1529. }
  1530. int __of_remove_property(struct device_node *np, struct property *prop)
  1531. {
  1532. struct property **next;
  1533. for (next = &np->properties; *next; next = &(*next)->next) {
  1534. if (*next == prop)
  1535. break;
  1536. }
  1537. if (*next == NULL)
  1538. return -ENODEV;
  1539. /* found the node */
  1540. *next = prop->next;
  1541. prop->next = np->deadprops;
  1542. np->deadprops = prop;
  1543. return 0;
  1544. }
  1545. void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
  1546. {
  1547. if (!IS_ENABLED(CONFIG_SYSFS))
  1548. return;
  1549. /* at early boot, bail here and defer setup to of_init() */
  1550. if (of_kset && of_node_is_attached(np))
  1551. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  1552. }
  1553. /**
  1554. * of_remove_property - Remove a property from a node.
  1555. *
  1556. * Note that we don't actually remove it, since we have given out
  1557. * who-knows-how-many pointers to the data using get-property.
  1558. * Instead we just move the property to the "dead properties"
  1559. * list, so it won't be found any more.
  1560. */
  1561. int of_remove_property(struct device_node *np, struct property *prop)
  1562. {
  1563. unsigned long flags;
  1564. int rc;
  1565. mutex_lock(&of_mutex);
  1566. raw_spin_lock_irqsave(&devtree_lock, flags);
  1567. rc = __of_remove_property(np, prop);
  1568. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1569. if (!rc)
  1570. __of_remove_property_sysfs(np, prop);
  1571. mutex_unlock(&of_mutex);
  1572. if (!rc)
  1573. of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
  1574. return rc;
  1575. }
  1576. int __of_update_property(struct device_node *np, struct property *newprop,
  1577. struct property **oldpropp)
  1578. {
  1579. struct property **next, *oldprop;
  1580. for (next = &np->properties; *next; next = &(*next)->next) {
  1581. if (of_prop_cmp((*next)->name, newprop->name) == 0)
  1582. break;
  1583. }
  1584. *oldpropp = oldprop = *next;
  1585. if (oldprop) {
  1586. /* replace the node */
  1587. newprop->next = oldprop->next;
  1588. *next = newprop;
  1589. oldprop->next = np->deadprops;
  1590. np->deadprops = oldprop;
  1591. } else {
  1592. /* new node */
  1593. newprop->next = NULL;
  1594. *next = newprop;
  1595. }
  1596. return 0;
  1597. }
  1598. void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
  1599. struct property *oldprop)
  1600. {
  1601. if (!IS_ENABLED(CONFIG_SYSFS))
  1602. return;
  1603. /* At early boot, bail out and defer setup to of_init() */
  1604. if (!of_kset)
  1605. return;
  1606. if (oldprop)
  1607. sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
  1608. __of_add_property_sysfs(np, newprop);
  1609. }
  1610. /*
  1611. * of_update_property - Update a property in a node, if the property does
  1612. * not exist, add it.
  1613. *
  1614. * Note that we don't actually remove it, since we have given out
  1615. * who-knows-how-many pointers to the data using get-property.
  1616. * Instead we just move the property to the "dead properties" list,
  1617. * and add the new property to the property list
  1618. */
  1619. int of_update_property(struct device_node *np, struct property *newprop)
  1620. {
  1621. struct property *oldprop;
  1622. unsigned long flags;
  1623. int rc;
  1624. if (!newprop->name)
  1625. return -EINVAL;
  1626. mutex_lock(&of_mutex);
  1627. raw_spin_lock_irqsave(&devtree_lock, flags);
  1628. rc = __of_update_property(np, newprop, &oldprop);
  1629. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1630. if (!rc)
  1631. __of_update_property_sysfs(np, newprop, oldprop);
  1632. mutex_unlock(&of_mutex);
  1633. if (!rc)
  1634. of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
  1635. return rc;
  1636. }
  1637. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1638. int id, const char *stem, int stem_len)
  1639. {
  1640. ap->np = np;
  1641. ap->id = id;
  1642. strncpy(ap->stem, stem, stem_len);
  1643. ap->stem[stem_len] = 0;
  1644. list_add_tail(&ap->link, &aliases_lookup);
  1645. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1646. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1647. }
  1648. /**
  1649. * of_alias_scan - Scan all properties of the 'aliases' node
  1650. *
  1651. * The function scans all the properties of the 'aliases' node and populates
  1652. * the global lookup table with the properties. It returns the
  1653. * number of alias properties found, or an error code in case of failure.
  1654. *
  1655. * @dt_alloc: An allocator that provides a virtual address to memory
  1656. * for storing the resulting tree
  1657. */
  1658. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1659. {
  1660. struct property *pp;
  1661. of_aliases = of_find_node_by_path("/aliases");
  1662. of_chosen = of_find_node_by_path("/chosen");
  1663. if (of_chosen == NULL)
  1664. of_chosen = of_find_node_by_path("/chosen@0");
  1665. if (of_chosen) {
  1666. /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
  1667. const char *name = of_get_property(of_chosen, "stdout-path", NULL);
  1668. if (!name)
  1669. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1670. if (IS_ENABLED(CONFIG_PPC) && !name)
  1671. name = of_get_property(of_aliases, "stdout", NULL);
  1672. if (name)
  1673. of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
  1674. }
  1675. if (!of_aliases)
  1676. return;
  1677. for_each_property_of_node(of_aliases, pp) {
  1678. const char *start = pp->name;
  1679. const char *end = start + strlen(start);
  1680. struct device_node *np;
  1681. struct alias_prop *ap;
  1682. int id, len;
  1683. /* Skip those we do not want to proceed */
  1684. if (!strcmp(pp->name, "name") ||
  1685. !strcmp(pp->name, "phandle") ||
  1686. !strcmp(pp->name, "linux,phandle"))
  1687. continue;
  1688. np = of_find_node_by_path(pp->value);
  1689. if (!np)
  1690. continue;
  1691. /* walk the alias backwards to extract the id and work out
  1692. * the 'stem' string */
  1693. while (isdigit(*(end-1)) && end > start)
  1694. end--;
  1695. len = end - start;
  1696. if (kstrtoint(end, 10, &id) < 0)
  1697. continue;
  1698. /* Allocate an alias_prop with enough space for the stem */
  1699. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1700. if (!ap)
  1701. continue;
  1702. memset(ap, 0, sizeof(*ap) + len + 1);
  1703. ap->alias = start;
  1704. of_alias_add(ap, np, id, start, len);
  1705. }
  1706. }
  1707. /**
  1708. * of_alias_get_id - Get alias id for the given device_node
  1709. * @np: Pointer to the given device_node
  1710. * @stem: Alias stem of the given device_node
  1711. *
  1712. * The function travels the lookup table to get the alias id for the given
  1713. * device_node and alias stem. It returns the alias id if found.
  1714. */
  1715. int of_alias_get_id(struct device_node *np, const char *stem)
  1716. {
  1717. struct alias_prop *app;
  1718. int id = -ENODEV;
  1719. mutex_lock(&of_mutex);
  1720. list_for_each_entry(app, &aliases_lookup, link) {
  1721. if (strcmp(app->stem, stem) != 0)
  1722. continue;
  1723. if (np == app->np) {
  1724. id = app->id;
  1725. break;
  1726. }
  1727. }
  1728. mutex_unlock(&of_mutex);
  1729. return id;
  1730. }
  1731. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1732. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1733. u32 *pu)
  1734. {
  1735. const void *curv = cur;
  1736. if (!prop)
  1737. return NULL;
  1738. if (!cur) {
  1739. curv = prop->value;
  1740. goto out_val;
  1741. }
  1742. curv += sizeof(*cur);
  1743. if (curv >= prop->value + prop->length)
  1744. return NULL;
  1745. out_val:
  1746. *pu = be32_to_cpup(curv);
  1747. return curv;
  1748. }
  1749. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1750. const char *of_prop_next_string(struct property *prop, const char *cur)
  1751. {
  1752. const void *curv = cur;
  1753. if (!prop)
  1754. return NULL;
  1755. if (!cur)
  1756. return prop->value;
  1757. curv += strlen(cur) + 1;
  1758. if (curv >= prop->value + prop->length)
  1759. return NULL;
  1760. return curv;
  1761. }
  1762. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1763. /**
  1764. * of_console_check() - Test and setup console for DT setup
  1765. * @dn - Pointer to device node
  1766. * @name - Name to use for preferred console without index. ex. "ttyS"
  1767. * @index - Index to use for preferred console.
  1768. *
  1769. * Check if the given device node matches the stdout-path property in the
  1770. * /chosen node. If it does then register it as the preferred console and return
  1771. * TRUE. Otherwise return FALSE.
  1772. */
  1773. bool of_console_check(struct device_node *dn, char *name, int index)
  1774. {
  1775. if (!dn || dn != of_stdout || console_set_on_cmdline)
  1776. return false;
  1777. return !add_preferred_console(name, index,
  1778. kstrdup(of_stdout_options, GFP_KERNEL));
  1779. }
  1780. EXPORT_SYMBOL_GPL(of_console_check);
  1781. /**
  1782. * of_find_next_cache_node - Find a node's subsidiary cache
  1783. * @np: node of type "cpu" or "cache"
  1784. *
  1785. * Returns a node pointer with refcount incremented, use
  1786. * of_node_put() on it when done. Caller should hold a reference
  1787. * to np.
  1788. */
  1789. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1790. {
  1791. struct device_node *child;
  1792. const phandle *handle;
  1793. handle = of_get_property(np, "l2-cache", NULL);
  1794. if (!handle)
  1795. handle = of_get_property(np, "next-level-cache", NULL);
  1796. if (handle)
  1797. return of_find_node_by_phandle(be32_to_cpup(handle));
  1798. /* OF on pmac has nodes instead of properties named "l2-cache"
  1799. * beneath CPU nodes.
  1800. */
  1801. if (!strcmp(np->type, "cpu"))
  1802. for_each_child_of_node(np, child)
  1803. if (!strcmp(child->type, "cache"))
  1804. return child;
  1805. return NULL;
  1806. }
  1807. /**
  1808. * of_graph_parse_endpoint() - parse common endpoint node properties
  1809. * @node: pointer to endpoint device_node
  1810. * @endpoint: pointer to the OF endpoint data structure
  1811. *
  1812. * The caller should hold a reference to @node.
  1813. */
  1814. int of_graph_parse_endpoint(const struct device_node *node,
  1815. struct of_endpoint *endpoint)
  1816. {
  1817. struct device_node *port_node = of_get_parent(node);
  1818. WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
  1819. __func__, node->full_name);
  1820. memset(endpoint, 0, sizeof(*endpoint));
  1821. endpoint->local_node = node;
  1822. /*
  1823. * It doesn't matter whether the two calls below succeed.
  1824. * If they don't then the default value 0 is used.
  1825. */
  1826. of_property_read_u32(port_node, "reg", &endpoint->port);
  1827. of_property_read_u32(node, "reg", &endpoint->id);
  1828. of_node_put(port_node);
  1829. return 0;
  1830. }
  1831. EXPORT_SYMBOL(of_graph_parse_endpoint);
  1832. /**
  1833. * of_graph_get_next_endpoint() - get next endpoint node
  1834. * @parent: pointer to the parent device node
  1835. * @prev: previous endpoint node, or NULL to get first
  1836. *
  1837. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  1838. * of the passed @prev node is not decremented, the caller have to use
  1839. * of_node_put() on it when done.
  1840. */
  1841. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  1842. struct device_node *prev)
  1843. {
  1844. struct device_node *endpoint;
  1845. struct device_node *port;
  1846. if (!parent)
  1847. return NULL;
  1848. /*
  1849. * Start by locating the port node. If no previous endpoint is specified
  1850. * search for the first port node, otherwise get the previous endpoint
  1851. * parent port node.
  1852. */
  1853. if (!prev) {
  1854. struct device_node *node;
  1855. node = of_get_child_by_name(parent, "ports");
  1856. if (node)
  1857. parent = node;
  1858. port = of_get_child_by_name(parent, "port");
  1859. of_node_put(node);
  1860. if (!port) {
  1861. pr_err("%s(): no port node found in %s\n",
  1862. __func__, parent->full_name);
  1863. return NULL;
  1864. }
  1865. } else {
  1866. port = of_get_parent(prev);
  1867. if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
  1868. __func__, prev->full_name))
  1869. return NULL;
  1870. /*
  1871. * Avoid dropping prev node refcount to 0 when getting the next
  1872. * child below.
  1873. */
  1874. of_node_get(prev);
  1875. }
  1876. while (1) {
  1877. /*
  1878. * Now that we have a port node, get the next endpoint by
  1879. * getting the next child. If the previous endpoint is NULL this
  1880. * will return the first child.
  1881. */
  1882. endpoint = of_get_next_child(port, prev);
  1883. if (endpoint) {
  1884. of_node_put(port);
  1885. return endpoint;
  1886. }
  1887. /* No more endpoints under this port, try the next one. */
  1888. prev = NULL;
  1889. do {
  1890. port = of_get_next_child(parent, port);
  1891. if (!port)
  1892. return NULL;
  1893. } while (of_node_cmp(port->name, "port"));
  1894. }
  1895. }
  1896. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  1897. /**
  1898. * of_graph_get_remote_port_parent() - get remote port's parent node
  1899. * @node: pointer to a local endpoint device_node
  1900. *
  1901. * Return: Remote device node associated with remote endpoint node linked
  1902. * to @node. Use of_node_put() on it when done.
  1903. */
  1904. struct device_node *of_graph_get_remote_port_parent(
  1905. const struct device_node *node)
  1906. {
  1907. struct device_node *np;
  1908. unsigned int depth;
  1909. /* Get remote endpoint node. */
  1910. np = of_parse_phandle(node, "remote-endpoint", 0);
  1911. /* Walk 3 levels up only if there is 'ports' node. */
  1912. for (depth = 3; depth && np; depth--) {
  1913. np = of_get_next_parent(np);
  1914. if (depth == 2 && of_node_cmp(np->name, "ports"))
  1915. break;
  1916. }
  1917. return np;
  1918. }
  1919. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  1920. /**
  1921. * of_graph_get_remote_port() - get remote port node
  1922. * @node: pointer to a local endpoint device_node
  1923. *
  1924. * Return: Remote port node associated with remote endpoint node linked
  1925. * to @node. Use of_node_put() on it when done.
  1926. */
  1927. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  1928. {
  1929. struct device_node *np;
  1930. /* Get remote endpoint node. */
  1931. np = of_parse_phandle(node, "remote-endpoint", 0);
  1932. if (!np)
  1933. return NULL;
  1934. return of_get_next_parent(np);
  1935. }
  1936. EXPORT_SYMBOL(of_graph_get_remote_port);