base.c 58 KB

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