base.c 61 KB

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