base.c 59 KB

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