base.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  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;
  628. const char *end;
  629. end = strchr(path, ':');
  630. if (!end)
  631. end = strchrnul(path, '/');
  632. len = end - path;
  633. if (!len)
  634. return NULL;
  635. __for_each_child_of_node(parent, child) {
  636. const char *name = strrchr(child->full_name, '/');
  637. if (WARN(!name, "malformed device_node %s\n", child->full_name))
  638. continue;
  639. name++;
  640. if (strncmp(path, name, len) == 0 && (strlen(name) == len))
  641. return child;
  642. }
  643. return NULL;
  644. }
  645. /**
  646. * of_find_node_opts_by_path - Find a node matching a full OF path
  647. * @path: Either the full path to match, or if the path does not
  648. * start with '/', the name of a property of the /aliases
  649. * node (an alias). In the case of an alias, the node
  650. * matching the alias' value will be returned.
  651. * @opts: Address of a pointer into which to store the start of
  652. * an options string appended to the end of the path with
  653. * a ':' separator.
  654. *
  655. * Valid paths:
  656. * /foo/bar Full path
  657. * foo Valid alias
  658. * foo/bar Valid alias + relative path
  659. *
  660. * Returns a node pointer with refcount incremented, use
  661. * of_node_put() on it when done.
  662. */
  663. struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
  664. {
  665. struct device_node *np = NULL;
  666. struct property *pp;
  667. unsigned long flags;
  668. const char *separator = strchr(path, ':');
  669. if (opts)
  670. *opts = separator ? separator + 1 : NULL;
  671. if (strcmp(path, "/") == 0)
  672. return of_node_get(of_root);
  673. /* The path could begin with an alias */
  674. if (*path != '/') {
  675. int len;
  676. const char *p = separator;
  677. if (!p)
  678. p = strchrnul(path, '/');
  679. len = p - path;
  680. /* of_aliases must not be NULL */
  681. if (!of_aliases)
  682. return NULL;
  683. for_each_property_of_node(of_aliases, pp) {
  684. if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
  685. np = of_find_node_by_path(pp->value);
  686. break;
  687. }
  688. }
  689. if (!np)
  690. return NULL;
  691. path = p;
  692. }
  693. /* Step down the tree matching path components */
  694. raw_spin_lock_irqsave(&devtree_lock, flags);
  695. if (!np)
  696. np = of_node_get(of_root);
  697. while (np && *path == '/') {
  698. path++; /* Increment past '/' delimiter */
  699. np = __of_find_node_by_path(np, path);
  700. path = strchrnul(path, '/');
  701. if (separator && separator < path)
  702. break;
  703. }
  704. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  705. return np;
  706. }
  707. EXPORT_SYMBOL(of_find_node_opts_by_path);
  708. /**
  709. * of_find_node_by_name - Find a node by its "name" property
  710. * @from: The node to start searching from or NULL, the node
  711. * you pass will not be searched, only the next one
  712. * will; typically, you pass what the previous call
  713. * returned. of_node_put() will be called on it
  714. * @name: The name string to match against
  715. *
  716. * Returns a node pointer with refcount incremented, use
  717. * of_node_put() on it when done.
  718. */
  719. struct device_node *of_find_node_by_name(struct device_node *from,
  720. const char *name)
  721. {
  722. struct device_node *np;
  723. unsigned long flags;
  724. raw_spin_lock_irqsave(&devtree_lock, flags);
  725. for_each_of_allnodes_from(from, np)
  726. if (np->name && (of_node_cmp(np->name, name) == 0)
  727. && of_node_get(np))
  728. break;
  729. of_node_put(from);
  730. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  731. return np;
  732. }
  733. EXPORT_SYMBOL(of_find_node_by_name);
  734. /**
  735. * of_find_node_by_type - Find a node by its "device_type" property
  736. * @from: The node to start searching from, or NULL to start searching
  737. * the entire device tree. The node you pass will not be
  738. * searched, only the next one will; typically, you pass
  739. * what the previous call returned. of_node_put() will be
  740. * called on from for you.
  741. * @type: The type string to match against
  742. *
  743. * Returns a node pointer with refcount incremented, use
  744. * of_node_put() on it when done.
  745. */
  746. struct device_node *of_find_node_by_type(struct device_node *from,
  747. const char *type)
  748. {
  749. struct device_node *np;
  750. unsigned long flags;
  751. raw_spin_lock_irqsave(&devtree_lock, flags);
  752. for_each_of_allnodes_from(from, np)
  753. if (np->type && (of_node_cmp(np->type, type) == 0)
  754. && of_node_get(np))
  755. break;
  756. of_node_put(from);
  757. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  758. return np;
  759. }
  760. EXPORT_SYMBOL(of_find_node_by_type);
  761. /**
  762. * of_find_compatible_node - Find a node based on type and one of the
  763. * tokens in its "compatible" property
  764. * @from: The node to start searching from or NULL, the node
  765. * you pass will not be searched, only the next one
  766. * will; typically, you pass what the previous call
  767. * returned. of_node_put() will be called on it
  768. * @type: The type string to match "device_type" or NULL to ignore
  769. * @compatible: The string to match to one of the tokens in the device
  770. * "compatible" list.
  771. *
  772. * Returns a node pointer with refcount incremented, use
  773. * of_node_put() on it when done.
  774. */
  775. struct device_node *of_find_compatible_node(struct device_node *from,
  776. const char *type, const char *compatible)
  777. {
  778. struct device_node *np;
  779. unsigned long flags;
  780. raw_spin_lock_irqsave(&devtree_lock, flags);
  781. for_each_of_allnodes_from(from, np)
  782. if (__of_device_is_compatible(np, compatible, type, NULL) &&
  783. of_node_get(np))
  784. break;
  785. of_node_put(from);
  786. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  787. return np;
  788. }
  789. EXPORT_SYMBOL(of_find_compatible_node);
  790. /**
  791. * of_find_node_with_property - Find a node which has a property with
  792. * the given name.
  793. * @from: The node to start searching from or NULL, the node
  794. * you pass will not be searched, only the next one
  795. * will; typically, you pass what the previous call
  796. * returned. of_node_put() will be called on it
  797. * @prop_name: The name of the property to look for.
  798. *
  799. * Returns a node pointer with refcount incremented, use
  800. * of_node_put() on it when done.
  801. */
  802. struct device_node *of_find_node_with_property(struct device_node *from,
  803. const char *prop_name)
  804. {
  805. struct device_node *np;
  806. struct property *pp;
  807. unsigned long flags;
  808. raw_spin_lock_irqsave(&devtree_lock, flags);
  809. for_each_of_allnodes_from(from, np) {
  810. for (pp = np->properties; pp; pp = pp->next) {
  811. if (of_prop_cmp(pp->name, prop_name) == 0) {
  812. of_node_get(np);
  813. goto out;
  814. }
  815. }
  816. }
  817. out:
  818. of_node_put(from);
  819. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  820. return np;
  821. }
  822. EXPORT_SYMBOL(of_find_node_with_property);
  823. static
  824. const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  825. const struct device_node *node)
  826. {
  827. const struct of_device_id *best_match = NULL;
  828. int score, best_score = 0;
  829. if (!matches)
  830. return NULL;
  831. for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
  832. score = __of_device_is_compatible(node, matches->compatible,
  833. matches->type, matches->name);
  834. if (score > best_score) {
  835. best_match = matches;
  836. best_score = score;
  837. }
  838. }
  839. return best_match;
  840. }
  841. /**
  842. * of_match_node - Tell if a device_node has a matching of_match structure
  843. * @matches: array of of device match structures to search in
  844. * @node: the of device structure to match against
  845. *
  846. * Low level utility function used by device matching.
  847. */
  848. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  849. const struct device_node *node)
  850. {
  851. const struct of_device_id *match;
  852. unsigned long flags;
  853. raw_spin_lock_irqsave(&devtree_lock, flags);
  854. match = __of_match_node(matches, node);
  855. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  856. return match;
  857. }
  858. EXPORT_SYMBOL(of_match_node);
  859. /**
  860. * of_find_matching_node_and_match - Find a node based on an of_device_id
  861. * match table.
  862. * @from: The node to start searching from or NULL, the node
  863. * you pass will not be searched, only the next one
  864. * will; typically, you pass what the previous call
  865. * returned. of_node_put() will be called on it
  866. * @matches: array of of device match structures to search in
  867. * @match Updated to point at the matches entry which matched
  868. *
  869. * Returns a node pointer with refcount incremented, use
  870. * of_node_put() on it when done.
  871. */
  872. struct device_node *of_find_matching_node_and_match(struct device_node *from,
  873. const struct of_device_id *matches,
  874. const struct of_device_id **match)
  875. {
  876. struct device_node *np;
  877. const struct of_device_id *m;
  878. unsigned long flags;
  879. if (match)
  880. *match = NULL;
  881. raw_spin_lock_irqsave(&devtree_lock, flags);
  882. for_each_of_allnodes_from(from, np) {
  883. m = __of_match_node(matches, np);
  884. if (m && of_node_get(np)) {
  885. if (match)
  886. *match = m;
  887. break;
  888. }
  889. }
  890. of_node_put(from);
  891. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  892. return np;
  893. }
  894. EXPORT_SYMBOL(of_find_matching_node_and_match);
  895. /**
  896. * of_modalias_node - Lookup appropriate modalias for a device node
  897. * @node: pointer to a device tree node
  898. * @modalias: Pointer to buffer that modalias value will be copied into
  899. * @len: Length of modalias value
  900. *
  901. * Based on the value of the compatible property, this routine will attempt
  902. * to choose an appropriate modalias value for a particular device tree node.
  903. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  904. * from the first entry in the compatible list property.
  905. *
  906. * This routine returns 0 on success, <0 on failure.
  907. */
  908. int of_modalias_node(struct device_node *node, char *modalias, int len)
  909. {
  910. const char *compatible, *p;
  911. int cplen;
  912. compatible = of_get_property(node, "compatible", &cplen);
  913. if (!compatible || strlen(compatible) > cplen)
  914. return -ENODEV;
  915. p = strchr(compatible, ',');
  916. strlcpy(modalias, p ? p + 1 : compatible, len);
  917. return 0;
  918. }
  919. EXPORT_SYMBOL_GPL(of_modalias_node);
  920. /**
  921. * of_find_node_by_phandle - Find a node given a phandle
  922. * @handle: phandle of the node to find
  923. *
  924. * Returns a node pointer with refcount incremented, use
  925. * of_node_put() on it when done.
  926. */
  927. struct device_node *of_find_node_by_phandle(phandle handle)
  928. {
  929. struct device_node *np;
  930. unsigned long flags;
  931. if (!handle)
  932. return NULL;
  933. raw_spin_lock_irqsave(&devtree_lock, flags);
  934. for_each_of_allnodes(np)
  935. if (np->phandle == handle)
  936. break;
  937. of_node_get(np);
  938. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  939. return np;
  940. }
  941. EXPORT_SYMBOL(of_find_node_by_phandle);
  942. /**
  943. * of_property_count_elems_of_size - Count the number of elements in a property
  944. *
  945. * @np: device node from which the property value is to be read.
  946. * @propname: name of the property to be searched.
  947. * @elem_size: size of the individual element
  948. *
  949. * Search for a property in a device node and count the number of elements of
  950. * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
  951. * property does not exist or its length does not match a multiple of elem_size
  952. * and -ENODATA if the property does not have a value.
  953. */
  954. int of_property_count_elems_of_size(const struct device_node *np,
  955. const char *propname, int elem_size)
  956. {
  957. struct property *prop = of_find_property(np, propname, NULL);
  958. if (!prop)
  959. return -EINVAL;
  960. if (!prop->value)
  961. return -ENODATA;
  962. if (prop->length % elem_size != 0) {
  963. pr_err("size of %s in node %s is not a multiple of %d\n",
  964. propname, np->full_name, elem_size);
  965. return -EINVAL;
  966. }
  967. return prop->length / elem_size;
  968. }
  969. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  970. /**
  971. * of_find_property_value_of_size
  972. *
  973. * @np: device node from which the property value is to be read.
  974. * @propname: name of the property to be searched.
  975. * @len: requested length of property value
  976. *
  977. * Search for a property in a device node and valid the requested size.
  978. * Returns the property value on success, -EINVAL if the property does not
  979. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  980. * property data isn't large enough.
  981. *
  982. */
  983. static void *of_find_property_value_of_size(const struct device_node *np,
  984. const char *propname, u32 len)
  985. {
  986. struct property *prop = of_find_property(np, propname, NULL);
  987. if (!prop)
  988. return ERR_PTR(-EINVAL);
  989. if (!prop->value)
  990. return ERR_PTR(-ENODATA);
  991. if (len > prop->length)
  992. return ERR_PTR(-EOVERFLOW);
  993. return prop->value;
  994. }
  995. /**
  996. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  997. *
  998. * @np: device node from which the property value is to be read.
  999. * @propname: name of the property to be searched.
  1000. * @index: index of the u32 in the list of values
  1001. * @out_value: pointer to return value, modified only if no error.
  1002. *
  1003. * Search for a property in a device node and read nth 32-bit value from
  1004. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1005. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1006. * property data isn't large enough.
  1007. *
  1008. * The out_value is modified only if a valid u32 value can be decoded.
  1009. */
  1010. int of_property_read_u32_index(const struct device_node *np,
  1011. const char *propname,
  1012. u32 index, u32 *out_value)
  1013. {
  1014. const u32 *val = of_find_property_value_of_size(np, propname,
  1015. ((index + 1) * sizeof(*out_value)));
  1016. if (IS_ERR(val))
  1017. return PTR_ERR(val);
  1018. *out_value = be32_to_cpup(((__be32 *)val) + index);
  1019. return 0;
  1020. }
  1021. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  1022. /**
  1023. * of_property_read_u8_array - Find and read an array of u8 from a property.
  1024. *
  1025. * @np: device node from which the property value is to be read.
  1026. * @propname: name of the property to be searched.
  1027. * @out_values: pointer to return value, modified only if return value is 0.
  1028. * @sz: number of array elements to read
  1029. *
  1030. * Search for a property in a device node and read 8-bit value(s) from
  1031. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1032. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1033. * property data isn't large enough.
  1034. *
  1035. * dts entry of array should be like:
  1036. * property = /bits/ 8 <0x50 0x60 0x70>;
  1037. *
  1038. * The out_values is modified only if a valid u8 value can be decoded.
  1039. */
  1040. int of_property_read_u8_array(const struct device_node *np,
  1041. const char *propname, u8 *out_values, size_t sz)
  1042. {
  1043. const u8 *val = of_find_property_value_of_size(np, propname,
  1044. (sz * sizeof(*out_values)));
  1045. if (IS_ERR(val))
  1046. return PTR_ERR(val);
  1047. while (sz--)
  1048. *out_values++ = *val++;
  1049. return 0;
  1050. }
  1051. EXPORT_SYMBOL_GPL(of_property_read_u8_array);
  1052. /**
  1053. * of_property_read_u16_array - Find and read an array of u16 from a property.
  1054. *
  1055. * @np: device node from which the property value is to be read.
  1056. * @propname: name of the property to be searched.
  1057. * @out_values: pointer to return value, modified only if return value is 0.
  1058. * @sz: number of array elements to read
  1059. *
  1060. * Search for a property in a device node and read 16-bit value(s) from
  1061. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1062. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1063. * property data isn't large enough.
  1064. *
  1065. * dts entry of array should be like:
  1066. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  1067. *
  1068. * The out_values is modified only if a valid u16 value can be decoded.
  1069. */
  1070. int of_property_read_u16_array(const struct device_node *np,
  1071. const char *propname, u16 *out_values, size_t sz)
  1072. {
  1073. const __be16 *val = of_find_property_value_of_size(np, propname,
  1074. (sz * sizeof(*out_values)));
  1075. if (IS_ERR(val))
  1076. return PTR_ERR(val);
  1077. while (sz--)
  1078. *out_values++ = be16_to_cpup(val++);
  1079. return 0;
  1080. }
  1081. EXPORT_SYMBOL_GPL(of_property_read_u16_array);
  1082. /**
  1083. * of_property_read_u32_array - Find and read an array of 32 bit integers
  1084. * from a property.
  1085. *
  1086. * @np: device node from which the property value is to be read.
  1087. * @propname: name of the property to be searched.
  1088. * @out_values: pointer to return value, modified only if return value is 0.
  1089. * @sz: number of array elements to read
  1090. *
  1091. * Search for a property in a device node and read 32-bit value(s) from
  1092. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1093. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1094. * property data isn't large enough.
  1095. *
  1096. * The out_values is modified only if a valid u32 value can be decoded.
  1097. */
  1098. int of_property_read_u32_array(const struct device_node *np,
  1099. const char *propname, u32 *out_values,
  1100. size_t sz)
  1101. {
  1102. const __be32 *val = of_find_property_value_of_size(np, propname,
  1103. (sz * sizeof(*out_values)));
  1104. if (IS_ERR(val))
  1105. return PTR_ERR(val);
  1106. while (sz--)
  1107. *out_values++ = be32_to_cpup(val++);
  1108. return 0;
  1109. }
  1110. EXPORT_SYMBOL_GPL(of_property_read_u32_array);
  1111. /**
  1112. * of_property_read_u64 - Find and read a 64 bit integer from a property
  1113. * @np: device node from which the property value is to be read.
  1114. * @propname: name of the property to be searched.
  1115. * @out_value: pointer to return value, modified only if return value is 0.
  1116. *
  1117. * Search for a property in a device node and read a 64-bit value from
  1118. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1119. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1120. * property data isn't large enough.
  1121. *
  1122. * The out_value is modified only if a valid u64 value can be decoded.
  1123. */
  1124. int of_property_read_u64(const struct device_node *np, const char *propname,
  1125. u64 *out_value)
  1126. {
  1127. const __be32 *val = of_find_property_value_of_size(np, propname,
  1128. sizeof(*out_value));
  1129. if (IS_ERR(val))
  1130. return PTR_ERR(val);
  1131. *out_value = of_read_number(val, 2);
  1132. return 0;
  1133. }
  1134. EXPORT_SYMBOL_GPL(of_property_read_u64);
  1135. /**
  1136. * of_property_read_u64_array - Find and read an array of 64 bit integers
  1137. * from a property.
  1138. *
  1139. * @np: device node from which the property value is to be read.
  1140. * @propname: name of the property to be searched.
  1141. * @out_values: pointer to return value, modified only if return value is 0.
  1142. * @sz: number of array elements to read
  1143. *
  1144. * Search for a property in a device node and read 64-bit value(s) from
  1145. * it. Returns 0 on success, -EINVAL if the property does not exist,
  1146. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1147. * property data isn't large enough.
  1148. *
  1149. * The out_values is modified only if a valid u64 value can be decoded.
  1150. */
  1151. int of_property_read_u64_array(const struct device_node *np,
  1152. const char *propname, u64 *out_values,
  1153. size_t sz)
  1154. {
  1155. const __be32 *val = of_find_property_value_of_size(np, propname,
  1156. (sz * sizeof(*out_values)));
  1157. if (IS_ERR(val))
  1158. return PTR_ERR(val);
  1159. while (sz--) {
  1160. *out_values++ = of_read_number(val, 2);
  1161. val += 2;
  1162. }
  1163. return 0;
  1164. }
  1165. EXPORT_SYMBOL_GPL(of_property_read_u64_array);
  1166. /**
  1167. * of_property_read_string - Find and read a string from a property
  1168. * @np: device node from which the property value is to be read.
  1169. * @propname: name of the property to be searched.
  1170. * @out_string: pointer to null terminated return string, modified only if
  1171. * return value is 0.
  1172. *
  1173. * Search for a property in a device tree node and retrieve a null
  1174. * terminated string value (pointer to data, not a copy). Returns 0 on
  1175. * success, -EINVAL if the property does not exist, -ENODATA if property
  1176. * does not have a value, and -EILSEQ if the string is not null-terminated
  1177. * within the length of the property data.
  1178. *
  1179. * The out_string pointer is modified only if a valid string can be decoded.
  1180. */
  1181. int of_property_read_string(struct device_node *np, const char *propname,
  1182. const char **out_string)
  1183. {
  1184. struct property *prop = of_find_property(np, propname, NULL);
  1185. if (!prop)
  1186. return -EINVAL;
  1187. if (!prop->value)
  1188. return -ENODATA;
  1189. if (strnlen(prop->value, prop->length) >= prop->length)
  1190. return -EILSEQ;
  1191. *out_string = prop->value;
  1192. return 0;
  1193. }
  1194. EXPORT_SYMBOL_GPL(of_property_read_string);
  1195. /**
  1196. * of_property_match_string() - Find string in a list and return index
  1197. * @np: pointer to node containing string list property
  1198. * @propname: string list property name
  1199. * @string: pointer to string to search for in string list
  1200. *
  1201. * This function searches a string list property and returns the index
  1202. * of a specific string value.
  1203. */
  1204. int of_property_match_string(struct device_node *np, const char *propname,
  1205. const char *string)
  1206. {
  1207. struct property *prop = of_find_property(np, propname, NULL);
  1208. size_t l;
  1209. int i;
  1210. const char *p, *end;
  1211. if (!prop)
  1212. return -EINVAL;
  1213. if (!prop->value)
  1214. return -ENODATA;
  1215. p = prop->value;
  1216. end = p + prop->length;
  1217. for (i = 0; p < end; i++, p += l) {
  1218. l = strnlen(p, end - p) + 1;
  1219. if (p + l > end)
  1220. return -EILSEQ;
  1221. pr_debug("comparing %s with %s\n", string, p);
  1222. if (strcmp(string, p) == 0)
  1223. return i; /* Found it; return index */
  1224. }
  1225. return -ENODATA;
  1226. }
  1227. EXPORT_SYMBOL_GPL(of_property_match_string);
  1228. /**
  1229. * of_property_read_string_helper() - Utility helper for parsing string properties
  1230. * @np: device node from which the property value is to be read.
  1231. * @propname: name of the property to be searched.
  1232. * @out_strs: output array of string pointers.
  1233. * @sz: number of array elements to read.
  1234. * @skip: Number of strings to skip over at beginning of list.
  1235. *
  1236. * Don't call this function directly. It is a utility helper for the
  1237. * of_property_read_string*() family of functions.
  1238. */
  1239. int of_property_read_string_helper(struct device_node *np, const char *propname,
  1240. const char **out_strs, size_t sz, int skip)
  1241. {
  1242. struct property *prop = of_find_property(np, propname, NULL);
  1243. int l = 0, i = 0;
  1244. const char *p, *end;
  1245. if (!prop)
  1246. return -EINVAL;
  1247. if (!prop->value)
  1248. return -ENODATA;
  1249. p = prop->value;
  1250. end = p + prop->length;
  1251. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  1252. l = strnlen(p, end - p) + 1;
  1253. if (p + l > end)
  1254. return -EILSEQ;
  1255. if (out_strs && i >= skip)
  1256. *out_strs++ = p;
  1257. }
  1258. i -= skip;
  1259. return i <= 0 ? -ENODATA : i;
  1260. }
  1261. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  1262. void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
  1263. {
  1264. int i;
  1265. printk("%s %s", msg, of_node_full_name(args->np));
  1266. for (i = 0; i < args->args_count; i++)
  1267. printk(i ? ",%08x" : ":%08x", args->args[i]);
  1268. printk("\n");
  1269. }
  1270. static int __of_parse_phandle_with_args(const struct device_node *np,
  1271. const char *list_name,
  1272. const char *cells_name,
  1273. int cell_count, int index,
  1274. struct of_phandle_args *out_args)
  1275. {
  1276. const __be32 *list, *list_end;
  1277. int rc = 0, size, cur_index = 0;
  1278. uint32_t count = 0;
  1279. struct device_node *node = NULL;
  1280. phandle phandle;
  1281. /* Retrieve the phandle list property */
  1282. list = of_get_property(np, list_name, &size);
  1283. if (!list)
  1284. return -ENOENT;
  1285. list_end = list + size / sizeof(*list);
  1286. /* Loop over the phandles until all the requested entry is found */
  1287. while (list < list_end) {
  1288. rc = -EINVAL;
  1289. count = 0;
  1290. /*
  1291. * If phandle is 0, then it is an empty entry with no
  1292. * arguments. Skip forward to the next entry.
  1293. */
  1294. phandle = be32_to_cpup(list++);
  1295. if (phandle) {
  1296. /*
  1297. * Find the provider node and parse the #*-cells
  1298. * property to determine the argument length.
  1299. *
  1300. * This is not needed if the cell count is hard-coded
  1301. * (i.e. cells_name not set, but cell_count is set),
  1302. * except when we're going to return the found node
  1303. * below.
  1304. */
  1305. if (cells_name || cur_index == index) {
  1306. node = of_find_node_by_phandle(phandle);
  1307. if (!node) {
  1308. pr_err("%s: could not find phandle\n",
  1309. np->full_name);
  1310. goto err;
  1311. }
  1312. }
  1313. if (cells_name) {
  1314. if (of_property_read_u32(node, cells_name,
  1315. &count)) {
  1316. pr_err("%s: could not get %s for %s\n",
  1317. np->full_name, cells_name,
  1318. node->full_name);
  1319. goto err;
  1320. }
  1321. } else {
  1322. count = cell_count;
  1323. }
  1324. /*
  1325. * Make sure that the arguments actually fit in the
  1326. * remaining property data length
  1327. */
  1328. if (list + count > list_end) {
  1329. pr_err("%s: arguments longer than property\n",
  1330. np->full_name);
  1331. goto err;
  1332. }
  1333. }
  1334. /*
  1335. * All of the error cases above bail out of the loop, so at
  1336. * this point, the parsing is successful. If the requested
  1337. * index matches, then fill the out_args structure and return,
  1338. * or return -ENOENT for an empty entry.
  1339. */
  1340. rc = -ENOENT;
  1341. if (cur_index == index) {
  1342. if (!phandle)
  1343. goto err;
  1344. if (out_args) {
  1345. int i;
  1346. if (WARN_ON(count > MAX_PHANDLE_ARGS))
  1347. count = MAX_PHANDLE_ARGS;
  1348. out_args->np = node;
  1349. out_args->args_count = count;
  1350. for (i = 0; i < count; i++)
  1351. out_args->args[i] = be32_to_cpup(list++);
  1352. } else {
  1353. of_node_put(node);
  1354. }
  1355. /* Found it! return success */
  1356. return 0;
  1357. }
  1358. of_node_put(node);
  1359. node = NULL;
  1360. list += count;
  1361. cur_index++;
  1362. }
  1363. /*
  1364. * Unlock node before returning result; will be one of:
  1365. * -ENOENT : index is for empty phandle
  1366. * -EINVAL : parsing error on data
  1367. * [1..n] : Number of phandle (count mode; when index = -1)
  1368. */
  1369. rc = index < 0 ? cur_index : -ENOENT;
  1370. err:
  1371. if (node)
  1372. of_node_put(node);
  1373. return rc;
  1374. }
  1375. /**
  1376. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  1377. * @np: Pointer to device node holding phandle property
  1378. * @phandle_name: Name of property holding a phandle value
  1379. * @index: For properties holding a table of phandles, this is the index into
  1380. * the table
  1381. *
  1382. * Returns the device_node pointer with refcount incremented. Use
  1383. * of_node_put() on it when done.
  1384. */
  1385. struct device_node *of_parse_phandle(const struct device_node *np,
  1386. const char *phandle_name, int index)
  1387. {
  1388. struct of_phandle_args args;
  1389. if (index < 0)
  1390. return NULL;
  1391. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  1392. index, &args))
  1393. return NULL;
  1394. return args.np;
  1395. }
  1396. EXPORT_SYMBOL(of_parse_phandle);
  1397. /**
  1398. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  1399. * @np: pointer to a device tree node containing a list
  1400. * @list_name: property name that contains a list
  1401. * @cells_name: property name that specifies phandles' arguments count
  1402. * @index: index of a phandle to parse out
  1403. * @out_args: optional pointer to output arguments structure (will be filled)
  1404. *
  1405. * This function is useful to parse lists of phandles and their arguments.
  1406. * Returns 0 on success and fills out_args, on error returns appropriate
  1407. * errno value.
  1408. *
  1409. * Caller is responsible to call of_node_put() on the returned out_args->np
  1410. * pointer.
  1411. *
  1412. * Example:
  1413. *
  1414. * phandle1: node1 {
  1415. * #list-cells = <2>;
  1416. * }
  1417. *
  1418. * phandle2: node2 {
  1419. * #list-cells = <1>;
  1420. * }
  1421. *
  1422. * node3 {
  1423. * list = <&phandle1 1 2 &phandle2 3>;
  1424. * }
  1425. *
  1426. * To get a device_node of the `node2' node you may call this:
  1427. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  1428. */
  1429. int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
  1430. const char *cells_name, int index,
  1431. struct of_phandle_args *out_args)
  1432. {
  1433. if (index < 0)
  1434. return -EINVAL;
  1435. return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
  1436. index, out_args);
  1437. }
  1438. EXPORT_SYMBOL(of_parse_phandle_with_args);
  1439. /**
  1440. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  1441. * @np: pointer to a device tree node containing a list
  1442. * @list_name: property name that contains a list
  1443. * @cell_count: number of argument cells following the phandle
  1444. * @index: index of a phandle to parse out
  1445. * @out_args: optional pointer to output arguments structure (will be filled)
  1446. *
  1447. * This function is useful to parse lists of phandles and their arguments.
  1448. * Returns 0 on success and fills out_args, on error returns appropriate
  1449. * errno value.
  1450. *
  1451. * Caller is responsible to call of_node_put() on the returned out_args->np
  1452. * pointer.
  1453. *
  1454. * Example:
  1455. *
  1456. * phandle1: node1 {
  1457. * }
  1458. *
  1459. * phandle2: node2 {
  1460. * }
  1461. *
  1462. * node3 {
  1463. * list = <&phandle1 0 2 &phandle2 2 3>;
  1464. * }
  1465. *
  1466. * To get a device_node of the `node2' node you may call this:
  1467. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  1468. */
  1469. int of_parse_phandle_with_fixed_args(const struct device_node *np,
  1470. const char *list_name, int cell_count,
  1471. int index, struct of_phandle_args *out_args)
  1472. {
  1473. if (index < 0)
  1474. return -EINVAL;
  1475. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  1476. index, out_args);
  1477. }
  1478. EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
  1479. /**
  1480. * of_count_phandle_with_args() - Find the number of phandles references in a property
  1481. * @np: pointer to a device tree node containing a list
  1482. * @list_name: property name that contains a list
  1483. * @cells_name: property name that specifies phandles' arguments count
  1484. *
  1485. * Returns the number of phandle + argument tuples within a property. It
  1486. * is a typical pattern to encode a list of phandle and variable
  1487. * arguments into a single property. The number of arguments is encoded
  1488. * by a property in the phandle-target node. For example, a gpios
  1489. * property would contain a list of GPIO specifies consisting of a
  1490. * phandle and 1 or more arguments. The number of arguments are
  1491. * determined by the #gpio-cells property in the node pointed to by the
  1492. * phandle.
  1493. */
  1494. int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
  1495. const char *cells_name)
  1496. {
  1497. return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
  1498. NULL);
  1499. }
  1500. EXPORT_SYMBOL(of_count_phandle_with_args);
  1501. /**
  1502. * __of_add_property - Add a property to a node without lock operations
  1503. */
  1504. int __of_add_property(struct device_node *np, struct property *prop)
  1505. {
  1506. struct property **next;
  1507. prop->next = NULL;
  1508. next = &np->properties;
  1509. while (*next) {
  1510. if (strcmp(prop->name, (*next)->name) == 0)
  1511. /* duplicate ! don't insert it */
  1512. return -EEXIST;
  1513. next = &(*next)->next;
  1514. }
  1515. *next = prop;
  1516. return 0;
  1517. }
  1518. /**
  1519. * of_add_property - Add a property to a node
  1520. */
  1521. int of_add_property(struct device_node *np, struct property *prop)
  1522. {
  1523. unsigned long flags;
  1524. int rc;
  1525. mutex_lock(&of_mutex);
  1526. raw_spin_lock_irqsave(&devtree_lock, flags);
  1527. rc = __of_add_property(np, prop);
  1528. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1529. if (!rc)
  1530. __of_add_property_sysfs(np, prop);
  1531. mutex_unlock(&of_mutex);
  1532. if (!rc)
  1533. of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
  1534. return rc;
  1535. }
  1536. int __of_remove_property(struct device_node *np, struct property *prop)
  1537. {
  1538. struct property **next;
  1539. for (next = &np->properties; *next; next = &(*next)->next) {
  1540. if (*next == prop)
  1541. break;
  1542. }
  1543. if (*next == NULL)
  1544. return -ENODEV;
  1545. /* found the node */
  1546. *next = prop->next;
  1547. prop->next = np->deadprops;
  1548. np->deadprops = prop;
  1549. return 0;
  1550. }
  1551. void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
  1552. {
  1553. if (!IS_ENABLED(CONFIG_SYSFS))
  1554. return;
  1555. /* at early boot, bail here and defer setup to of_init() */
  1556. if (of_kset && of_node_is_attached(np))
  1557. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  1558. }
  1559. /**
  1560. * of_remove_property - Remove a property from a node.
  1561. *
  1562. * Note that we don't actually remove it, since we have given out
  1563. * who-knows-how-many pointers to the data using get-property.
  1564. * Instead we just move the property to the "dead properties"
  1565. * list, so it won't be found any more.
  1566. */
  1567. int of_remove_property(struct device_node *np, struct property *prop)
  1568. {
  1569. unsigned long flags;
  1570. int rc;
  1571. mutex_lock(&of_mutex);
  1572. raw_spin_lock_irqsave(&devtree_lock, flags);
  1573. rc = __of_remove_property(np, prop);
  1574. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1575. if (!rc)
  1576. __of_remove_property_sysfs(np, prop);
  1577. mutex_unlock(&of_mutex);
  1578. if (!rc)
  1579. of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
  1580. return rc;
  1581. }
  1582. int __of_update_property(struct device_node *np, struct property *newprop,
  1583. struct property **oldpropp)
  1584. {
  1585. struct property **next, *oldprop;
  1586. for (next = &np->properties; *next; next = &(*next)->next) {
  1587. if (of_prop_cmp((*next)->name, newprop->name) == 0)
  1588. break;
  1589. }
  1590. *oldpropp = oldprop = *next;
  1591. if (oldprop) {
  1592. /* replace the node */
  1593. newprop->next = oldprop->next;
  1594. *next = newprop;
  1595. oldprop->next = np->deadprops;
  1596. np->deadprops = oldprop;
  1597. } else {
  1598. /* new node */
  1599. newprop->next = NULL;
  1600. *next = newprop;
  1601. }
  1602. return 0;
  1603. }
  1604. void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
  1605. struct property *oldprop)
  1606. {
  1607. if (!IS_ENABLED(CONFIG_SYSFS))
  1608. return;
  1609. /* At early boot, bail out and defer setup to of_init() */
  1610. if (!of_kset)
  1611. return;
  1612. if (oldprop)
  1613. sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
  1614. __of_add_property_sysfs(np, newprop);
  1615. }
  1616. /*
  1617. * of_update_property - Update a property in a node, if the property does
  1618. * not exist, add it.
  1619. *
  1620. * Note that we don't actually remove it, since we have given out
  1621. * who-knows-how-many pointers to the data using get-property.
  1622. * Instead we just move the property to the "dead properties" list,
  1623. * and add the new property to the property list
  1624. */
  1625. int of_update_property(struct device_node *np, struct property *newprop)
  1626. {
  1627. struct property *oldprop;
  1628. unsigned long flags;
  1629. int rc;
  1630. if (!newprop->name)
  1631. return -EINVAL;
  1632. mutex_lock(&of_mutex);
  1633. raw_spin_lock_irqsave(&devtree_lock, flags);
  1634. rc = __of_update_property(np, newprop, &oldprop);
  1635. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1636. if (!rc)
  1637. __of_update_property_sysfs(np, newprop, oldprop);
  1638. mutex_unlock(&of_mutex);
  1639. if (!rc)
  1640. of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
  1641. return rc;
  1642. }
  1643. static void of_alias_add(struct alias_prop *ap, struct device_node *np,
  1644. int id, const char *stem, int stem_len)
  1645. {
  1646. ap->np = np;
  1647. ap->id = id;
  1648. strncpy(ap->stem, stem, stem_len);
  1649. ap->stem[stem_len] = 0;
  1650. list_add_tail(&ap->link, &aliases_lookup);
  1651. pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
  1652. ap->alias, ap->stem, ap->id, of_node_full_name(np));
  1653. }
  1654. /**
  1655. * of_alias_scan - Scan all properties of the 'aliases' node
  1656. *
  1657. * The function scans all the properties of the 'aliases' node and populates
  1658. * the global lookup table with the properties. It returns the
  1659. * number of alias properties found, or an error code in case of failure.
  1660. *
  1661. * @dt_alloc: An allocator that provides a virtual address to memory
  1662. * for storing the resulting tree
  1663. */
  1664. void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
  1665. {
  1666. struct property *pp;
  1667. of_aliases = of_find_node_by_path("/aliases");
  1668. of_chosen = of_find_node_by_path("/chosen");
  1669. if (of_chosen == NULL)
  1670. of_chosen = of_find_node_by_path("/chosen@0");
  1671. if (of_chosen) {
  1672. /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
  1673. const char *name = of_get_property(of_chosen, "stdout-path", NULL);
  1674. if (!name)
  1675. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  1676. if (IS_ENABLED(CONFIG_PPC) && !name)
  1677. name = of_get_property(of_aliases, "stdout", NULL);
  1678. if (name) {
  1679. of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
  1680. add_preferred_console("stdout-path", 0, NULL);
  1681. }
  1682. }
  1683. if (!of_aliases)
  1684. return;
  1685. for_each_property_of_node(of_aliases, pp) {
  1686. const char *start = pp->name;
  1687. const char *end = start + strlen(start);
  1688. struct device_node *np;
  1689. struct alias_prop *ap;
  1690. int id, len;
  1691. /* Skip those we do not want to proceed */
  1692. if (!strcmp(pp->name, "name") ||
  1693. !strcmp(pp->name, "phandle") ||
  1694. !strcmp(pp->name, "linux,phandle"))
  1695. continue;
  1696. np = of_find_node_by_path(pp->value);
  1697. if (!np)
  1698. continue;
  1699. /* walk the alias backwards to extract the id and work out
  1700. * the 'stem' string */
  1701. while (isdigit(*(end-1)) && end > start)
  1702. end--;
  1703. len = end - start;
  1704. if (kstrtoint(end, 10, &id) < 0)
  1705. continue;
  1706. /* Allocate an alias_prop with enough space for the stem */
  1707. ap = dt_alloc(sizeof(*ap) + len + 1, 4);
  1708. if (!ap)
  1709. continue;
  1710. memset(ap, 0, sizeof(*ap) + len + 1);
  1711. ap->alias = start;
  1712. of_alias_add(ap, np, id, start, len);
  1713. }
  1714. }
  1715. /**
  1716. * of_alias_get_id - Get alias id for the given device_node
  1717. * @np: Pointer to the given device_node
  1718. * @stem: Alias stem of the given device_node
  1719. *
  1720. * The function travels the lookup table to get the alias id for the given
  1721. * device_node and alias stem. It returns the alias id if found.
  1722. */
  1723. int of_alias_get_id(struct device_node *np, const char *stem)
  1724. {
  1725. struct alias_prop *app;
  1726. int id = -ENODEV;
  1727. mutex_lock(&of_mutex);
  1728. list_for_each_entry(app, &aliases_lookup, link) {
  1729. if (strcmp(app->stem, stem) != 0)
  1730. continue;
  1731. if (np == app->np) {
  1732. id = app->id;
  1733. break;
  1734. }
  1735. }
  1736. mutex_unlock(&of_mutex);
  1737. return id;
  1738. }
  1739. EXPORT_SYMBOL_GPL(of_alias_get_id);
  1740. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  1741. u32 *pu)
  1742. {
  1743. const void *curv = cur;
  1744. if (!prop)
  1745. return NULL;
  1746. if (!cur) {
  1747. curv = prop->value;
  1748. goto out_val;
  1749. }
  1750. curv += sizeof(*cur);
  1751. if (curv >= prop->value + prop->length)
  1752. return NULL;
  1753. out_val:
  1754. *pu = be32_to_cpup(curv);
  1755. return curv;
  1756. }
  1757. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  1758. const char *of_prop_next_string(struct property *prop, const char *cur)
  1759. {
  1760. const void *curv = cur;
  1761. if (!prop)
  1762. return NULL;
  1763. if (!cur)
  1764. return prop->value;
  1765. curv += strlen(cur) + 1;
  1766. if (curv >= prop->value + prop->length)
  1767. return NULL;
  1768. return curv;
  1769. }
  1770. EXPORT_SYMBOL_GPL(of_prop_next_string);
  1771. /**
  1772. * of_console_check() - Test and setup console for DT setup
  1773. * @dn - Pointer to device node
  1774. * @name - Name to use for preferred console without index. ex. "ttyS"
  1775. * @index - Index to use for preferred console.
  1776. *
  1777. * Check if the given device node matches the stdout-path property in the
  1778. * /chosen node. If it does then register it as the preferred console and return
  1779. * TRUE. Otherwise return FALSE.
  1780. */
  1781. bool of_console_check(struct device_node *dn, char *name, int index)
  1782. {
  1783. if (!dn || dn != of_stdout || console_set_on_cmdline)
  1784. return false;
  1785. return !add_preferred_console(name, index,
  1786. kstrdup(of_stdout_options, GFP_KERNEL));
  1787. }
  1788. EXPORT_SYMBOL_GPL(of_console_check);
  1789. /**
  1790. * of_find_next_cache_node - Find a node's subsidiary cache
  1791. * @np: node of type "cpu" or "cache"
  1792. *
  1793. * Returns a node pointer with refcount incremented, use
  1794. * of_node_put() on it when done. Caller should hold a reference
  1795. * to np.
  1796. */
  1797. struct device_node *of_find_next_cache_node(const struct device_node *np)
  1798. {
  1799. struct device_node *child;
  1800. const phandle *handle;
  1801. handle = of_get_property(np, "l2-cache", NULL);
  1802. if (!handle)
  1803. handle = of_get_property(np, "next-level-cache", NULL);
  1804. if (handle)
  1805. return of_find_node_by_phandle(be32_to_cpup(handle));
  1806. /* OF on pmac has nodes instead of properties named "l2-cache"
  1807. * beneath CPU nodes.
  1808. */
  1809. if (!strcmp(np->type, "cpu"))
  1810. for_each_child_of_node(np, child)
  1811. if (!strcmp(child->type, "cache"))
  1812. return child;
  1813. return NULL;
  1814. }
  1815. /**
  1816. * of_graph_parse_endpoint() - parse common endpoint node properties
  1817. * @node: pointer to endpoint device_node
  1818. * @endpoint: pointer to the OF endpoint data structure
  1819. *
  1820. * The caller should hold a reference to @node.
  1821. */
  1822. int of_graph_parse_endpoint(const struct device_node *node,
  1823. struct of_endpoint *endpoint)
  1824. {
  1825. struct device_node *port_node = of_get_parent(node);
  1826. WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
  1827. __func__, node->full_name);
  1828. memset(endpoint, 0, sizeof(*endpoint));
  1829. endpoint->local_node = node;
  1830. /*
  1831. * It doesn't matter whether the two calls below succeed.
  1832. * If they don't then the default value 0 is used.
  1833. */
  1834. of_property_read_u32(port_node, "reg", &endpoint->port);
  1835. of_property_read_u32(node, "reg", &endpoint->id);
  1836. of_node_put(port_node);
  1837. return 0;
  1838. }
  1839. EXPORT_SYMBOL(of_graph_parse_endpoint);
  1840. /**
  1841. * of_graph_get_next_endpoint() - get next endpoint node
  1842. * @parent: pointer to the parent device node
  1843. * @prev: previous endpoint node, or NULL to get first
  1844. *
  1845. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  1846. * of the passed @prev node is not decremented, the caller have to use
  1847. * of_node_put() on it when done.
  1848. */
  1849. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  1850. struct device_node *prev)
  1851. {
  1852. struct device_node *endpoint;
  1853. struct device_node *port;
  1854. if (!parent)
  1855. return NULL;
  1856. /*
  1857. * Start by locating the port node. If no previous endpoint is specified
  1858. * search for the first port node, otherwise get the previous endpoint
  1859. * parent port node.
  1860. */
  1861. if (!prev) {
  1862. struct device_node *node;
  1863. node = of_get_child_by_name(parent, "ports");
  1864. if (node)
  1865. parent = node;
  1866. port = of_get_child_by_name(parent, "port");
  1867. of_node_put(node);
  1868. if (!port) {
  1869. pr_err("%s(): no port node found in %s\n",
  1870. __func__, parent->full_name);
  1871. return NULL;
  1872. }
  1873. } else {
  1874. port = of_get_parent(prev);
  1875. if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
  1876. __func__, prev->full_name))
  1877. return NULL;
  1878. /*
  1879. * Avoid dropping prev node refcount to 0 when getting the next
  1880. * child below.
  1881. */
  1882. of_node_get(prev);
  1883. }
  1884. while (1) {
  1885. /*
  1886. * Now that we have a port node, get the next endpoint by
  1887. * getting the next child. If the previous endpoint is NULL this
  1888. * will return the first child.
  1889. */
  1890. endpoint = of_get_next_child(port, prev);
  1891. if (endpoint) {
  1892. of_node_put(port);
  1893. return endpoint;
  1894. }
  1895. /* No more endpoints under this port, try the next one. */
  1896. prev = NULL;
  1897. do {
  1898. port = of_get_next_child(parent, port);
  1899. if (!port)
  1900. return NULL;
  1901. } while (of_node_cmp(port->name, "port"));
  1902. }
  1903. }
  1904. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  1905. /**
  1906. * of_graph_get_remote_port_parent() - get remote port's parent node
  1907. * @node: pointer to a local endpoint device_node
  1908. *
  1909. * Return: Remote device node associated with remote endpoint node linked
  1910. * to @node. Use of_node_put() on it when done.
  1911. */
  1912. struct device_node *of_graph_get_remote_port_parent(
  1913. const struct device_node *node)
  1914. {
  1915. struct device_node *np;
  1916. unsigned int depth;
  1917. /* Get remote endpoint node. */
  1918. np = of_parse_phandle(node, "remote-endpoint", 0);
  1919. /* Walk 3 levels up only if there is 'ports' node. */
  1920. for (depth = 3; depth && np; depth--) {
  1921. np = of_get_next_parent(np);
  1922. if (depth == 2 && of_node_cmp(np->name, "ports"))
  1923. break;
  1924. }
  1925. return np;
  1926. }
  1927. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  1928. /**
  1929. * of_graph_get_remote_port() - get remote port node
  1930. * @node: pointer to a local endpoint device_node
  1931. *
  1932. * Return: Remote port node associated with remote endpoint node linked
  1933. * to @node. Use of_node_put() on it when done.
  1934. */
  1935. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  1936. {
  1937. struct device_node *np;
  1938. /* Get remote endpoint node. */
  1939. np = of_parse_phandle(node, "remote-endpoint", 0);
  1940. if (!np)
  1941. return NULL;
  1942. return of_get_next_parent(np);
  1943. }
  1944. EXPORT_SYMBOL(of_graph_get_remote_port);