base.c 63 KB

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