base.c 63 KB

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