base.c 59 KB

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