base.c 59 KB

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