of.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. #ifndef _LINUX_OF_H
  2. #define _LINUX_OF_H
  3. /*
  4. * Definitions for talking to the Open Firmware PROM on
  5. * Power Macintosh and other computers.
  6. *
  7. * Copyright (C) 1996-2005 Paul Mackerras.
  8. *
  9. * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
  10. * Updates for SPARC64 by David S. Miller
  11. * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/types.h>
  19. #include <linux/bitops.h>
  20. #include <linux/errno.h>
  21. #include <linux/kobject.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/topology.h>
  25. #include <linux/notifier.h>
  26. #include <linux/property.h>
  27. #include <linux/list.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/errno.h>
  30. typedef u32 phandle;
  31. typedef u32 ihandle;
  32. struct property {
  33. char *name;
  34. int length;
  35. void *value;
  36. struct property *next;
  37. unsigned long _flags;
  38. unsigned int unique_id;
  39. struct bin_attribute attr;
  40. };
  41. #if defined(CONFIG_SPARC)
  42. struct of_irq_controller;
  43. #endif
  44. struct device_node {
  45. const char *name;
  46. const char *type;
  47. phandle phandle;
  48. const char *full_name;
  49. struct fwnode_handle fwnode;
  50. struct property *properties;
  51. struct property *deadprops; /* removed properties */
  52. struct device_node *parent;
  53. struct device_node *child;
  54. struct device_node *sibling;
  55. struct kobject kobj;
  56. unsigned long _flags;
  57. void *data;
  58. #if defined(CONFIG_SPARC)
  59. const char *path_component_name;
  60. unsigned int unique_id;
  61. struct of_irq_controller *irq_trans;
  62. #endif
  63. };
  64. #define MAX_PHANDLE_ARGS 16
  65. struct of_phandle_args {
  66. struct device_node *np;
  67. int args_count;
  68. uint32_t args[MAX_PHANDLE_ARGS];
  69. };
  70. struct of_reconfig_data {
  71. struct device_node *dn;
  72. struct property *prop;
  73. struct property *old_prop;
  74. };
  75. /* initialize a node */
  76. extern struct kobj_type of_node_ktype;
  77. static inline void of_node_init(struct device_node *node)
  78. {
  79. kobject_init(&node->kobj, &of_node_ktype);
  80. node->fwnode.type = FWNODE_OF;
  81. }
  82. /* true when node is initialized */
  83. static inline int of_node_is_initialized(struct device_node *node)
  84. {
  85. return node && node->kobj.state_initialized;
  86. }
  87. /* true when node is attached (i.e. present on sysfs) */
  88. static inline int of_node_is_attached(struct device_node *node)
  89. {
  90. return node && node->kobj.state_in_sysfs;
  91. }
  92. #ifdef CONFIG_OF_DYNAMIC
  93. extern struct device_node *of_node_get(struct device_node *node);
  94. extern void of_node_put(struct device_node *node);
  95. #else /* CONFIG_OF_DYNAMIC */
  96. /* Dummy ref counting routines - to be implemented later */
  97. static inline struct device_node *of_node_get(struct device_node *node)
  98. {
  99. return node;
  100. }
  101. static inline void of_node_put(struct device_node *node) { }
  102. #endif /* !CONFIG_OF_DYNAMIC */
  103. /* Pointer for first entry in chain of all nodes. */
  104. extern struct device_node *of_root;
  105. extern struct device_node *of_chosen;
  106. extern struct device_node *of_aliases;
  107. extern struct device_node *of_stdout;
  108. extern raw_spinlock_t devtree_lock;
  109. /* flag descriptions (need to be visible even when !CONFIG_OF) */
  110. #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
  111. #define OF_DETACHED 2 /* node has been detached from the device tree */
  112. #define OF_POPULATED 3 /* device already created for the node */
  113. #define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
  114. #define OF_BAD_ADDR ((u64)-1)
  115. #ifdef CONFIG_OF
  116. void of_core_init(void);
  117. static inline bool is_of_node(struct fwnode_handle *fwnode)
  118. {
  119. return fwnode && fwnode->type == FWNODE_OF;
  120. }
  121. static inline struct device_node *to_of_node(struct fwnode_handle *fwnode)
  122. {
  123. return is_of_node(fwnode) ?
  124. container_of(fwnode, struct device_node, fwnode) : NULL;
  125. }
  126. static inline bool of_have_populated_dt(void)
  127. {
  128. return of_root != NULL;
  129. }
  130. static inline bool of_node_is_root(const struct device_node *node)
  131. {
  132. return node && (node->parent == NULL);
  133. }
  134. static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
  135. {
  136. return test_bit(flag, &n->_flags);
  137. }
  138. static inline int of_node_test_and_set_flag(struct device_node *n,
  139. unsigned long flag)
  140. {
  141. return test_and_set_bit(flag, &n->_flags);
  142. }
  143. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  144. {
  145. set_bit(flag, &n->_flags);
  146. }
  147. static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
  148. {
  149. clear_bit(flag, &n->_flags);
  150. }
  151. static inline int of_property_check_flag(struct property *p, unsigned long flag)
  152. {
  153. return test_bit(flag, &p->_flags);
  154. }
  155. static inline void of_property_set_flag(struct property *p, unsigned long flag)
  156. {
  157. set_bit(flag, &p->_flags);
  158. }
  159. static inline void of_property_clear_flag(struct property *p, unsigned long flag)
  160. {
  161. clear_bit(flag, &p->_flags);
  162. }
  163. extern struct device_node *__of_find_all_nodes(struct device_node *prev);
  164. extern struct device_node *of_find_all_nodes(struct device_node *prev);
  165. /*
  166. * OF address retrieval & translation
  167. */
  168. /* Helper to read a big number; size is in cells (not bytes) */
  169. static inline u64 of_read_number(const __be32 *cell, int size)
  170. {
  171. u64 r = 0;
  172. while (size--)
  173. r = (r << 32) | be32_to_cpu(*(cell++));
  174. return r;
  175. }
  176. /* Like of_read_number, but we want an unsigned long result */
  177. static inline unsigned long of_read_ulong(const __be32 *cell, int size)
  178. {
  179. /* toss away upper bits if unsigned long is smaller than u64 */
  180. return of_read_number(cell, size);
  181. }
  182. #if defined(CONFIG_SPARC)
  183. #include <asm/prom.h>
  184. #endif
  185. /* Default #address and #size cells. Allow arch asm/prom.h to override */
  186. #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
  187. #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
  188. #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
  189. #endif
  190. /* Default string compare functions, Allow arch asm/prom.h to override */
  191. #if !defined(of_compat_cmp)
  192. #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
  193. #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
  194. #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
  195. #endif
  196. #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
  197. #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
  198. static inline const char *of_node_full_name(const struct device_node *np)
  199. {
  200. return np ? np->full_name : "<no-node>";
  201. }
  202. #define for_each_of_allnodes_from(from, dn) \
  203. for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
  204. #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
  205. extern struct device_node *of_find_node_by_name(struct device_node *from,
  206. const char *name);
  207. extern struct device_node *of_find_node_by_type(struct device_node *from,
  208. const char *type);
  209. extern struct device_node *of_find_compatible_node(struct device_node *from,
  210. const char *type, const char *compat);
  211. extern struct device_node *of_find_matching_node_and_match(
  212. struct device_node *from,
  213. const struct of_device_id *matches,
  214. const struct of_device_id **match);
  215. extern struct device_node *of_find_node_opts_by_path(const char *path,
  216. const char **opts);
  217. static inline struct device_node *of_find_node_by_path(const char *path)
  218. {
  219. return of_find_node_opts_by_path(path, NULL);
  220. }
  221. extern struct device_node *of_find_node_by_phandle(phandle handle);
  222. extern struct device_node *of_get_parent(const struct device_node *node);
  223. extern struct device_node *of_get_next_parent(struct device_node *node);
  224. extern struct device_node *of_get_next_child(const struct device_node *node,
  225. struct device_node *prev);
  226. extern struct device_node *of_get_next_available_child(
  227. const struct device_node *node, struct device_node *prev);
  228. extern struct device_node *of_get_child_by_name(const struct device_node *node,
  229. const char *name);
  230. /* cache lookup */
  231. extern struct device_node *of_find_next_cache_node(const struct device_node *);
  232. extern struct device_node *of_find_node_with_property(
  233. struct device_node *from, const char *prop_name);
  234. extern struct property *of_find_property(const struct device_node *np,
  235. const char *name,
  236. int *lenp);
  237. extern int of_property_count_elems_of_size(const struct device_node *np,
  238. const char *propname, int elem_size);
  239. extern int of_property_read_u32_index(const struct device_node *np,
  240. const char *propname,
  241. u32 index, u32 *out_value);
  242. extern int of_property_read_u8_array(const struct device_node *np,
  243. const char *propname, u8 *out_values, size_t sz);
  244. extern int of_property_read_u16_array(const struct device_node *np,
  245. const char *propname, u16 *out_values, size_t sz);
  246. extern int of_property_read_u32_array(const struct device_node *np,
  247. const char *propname,
  248. u32 *out_values,
  249. size_t sz);
  250. extern int of_property_read_u64(const struct device_node *np,
  251. const char *propname, u64 *out_value);
  252. extern int of_property_read_u64_array(const struct device_node *np,
  253. const char *propname,
  254. u64 *out_values,
  255. size_t sz);
  256. extern int of_property_read_string(const struct device_node *np,
  257. const char *propname,
  258. const char **out_string);
  259. extern int of_property_match_string(const struct device_node *np,
  260. const char *propname,
  261. const char *string);
  262. extern int of_property_read_string_helper(const struct device_node *np,
  263. const char *propname,
  264. const char **out_strs, size_t sz, int index);
  265. extern int of_device_is_compatible(const struct device_node *device,
  266. const char *);
  267. extern bool of_device_is_available(const struct device_node *device);
  268. extern bool of_device_is_big_endian(const struct device_node *device);
  269. extern const void *of_get_property(const struct device_node *node,
  270. const char *name,
  271. int *lenp);
  272. extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
  273. #define for_each_property_of_node(dn, pp) \
  274. for (pp = dn->properties; pp != NULL; pp = pp->next)
  275. extern int of_n_addr_cells(struct device_node *np);
  276. extern int of_n_size_cells(struct device_node *np);
  277. extern const struct of_device_id *of_match_node(
  278. const struct of_device_id *matches, const struct device_node *node);
  279. extern int of_modalias_node(struct device_node *node, char *modalias, int len);
  280. extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
  281. extern struct device_node *of_parse_phandle(const struct device_node *np,
  282. const char *phandle_name,
  283. int index);
  284. extern int of_parse_phandle_with_args(const struct device_node *np,
  285. const char *list_name, const char *cells_name, int index,
  286. struct of_phandle_args *out_args);
  287. extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
  288. const char *list_name, int cells_count, int index,
  289. struct of_phandle_args *out_args);
  290. extern int of_count_phandle_with_args(const struct device_node *np,
  291. const char *list_name, const char *cells_name);
  292. extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
  293. extern int of_alias_get_id(struct device_node *np, const char *stem);
  294. extern int of_alias_get_highest_id(const char *stem);
  295. extern int of_machine_is_compatible(const char *compat);
  296. extern int of_add_property(struct device_node *np, struct property *prop);
  297. extern int of_remove_property(struct device_node *np, struct property *prop);
  298. extern int of_update_property(struct device_node *np, struct property *newprop);
  299. /* For updating the device tree at runtime */
  300. #define OF_RECONFIG_ATTACH_NODE 0x0001
  301. #define OF_RECONFIG_DETACH_NODE 0x0002
  302. #define OF_RECONFIG_ADD_PROPERTY 0x0003
  303. #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
  304. #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
  305. extern int of_attach_node(struct device_node *);
  306. extern int of_detach_node(struct device_node *);
  307. #define of_match_ptr(_ptr) (_ptr)
  308. /*
  309. * struct property *prop;
  310. * const __be32 *p;
  311. * u32 u;
  312. *
  313. * of_property_for_each_u32(np, "propname", prop, p, u)
  314. * printk("U32 value: %x\n", u);
  315. */
  316. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  317. u32 *pu);
  318. /*
  319. * struct property *prop;
  320. * const char *s;
  321. *
  322. * of_property_for_each_string(np, "propname", prop, s)
  323. * printk("String value: %s\n", s);
  324. */
  325. const char *of_prop_next_string(struct property *prop, const char *cur);
  326. bool of_console_check(struct device_node *dn, char *name, int index);
  327. #else /* CONFIG_OF */
  328. static inline void of_core_init(void)
  329. {
  330. }
  331. static inline bool is_of_node(struct fwnode_handle *fwnode)
  332. {
  333. return false;
  334. }
  335. static inline struct device_node *to_of_node(struct fwnode_handle *fwnode)
  336. {
  337. return NULL;
  338. }
  339. static inline const char* of_node_full_name(const struct device_node *np)
  340. {
  341. return "<no-node>";
  342. }
  343. static inline struct device_node *of_find_node_by_name(struct device_node *from,
  344. const char *name)
  345. {
  346. return NULL;
  347. }
  348. static inline struct device_node *of_find_node_by_type(struct device_node *from,
  349. const char *type)
  350. {
  351. return NULL;
  352. }
  353. static inline struct device_node *of_find_matching_node_and_match(
  354. struct device_node *from,
  355. const struct of_device_id *matches,
  356. const struct of_device_id **match)
  357. {
  358. return NULL;
  359. }
  360. static inline struct device_node *of_find_node_by_path(const char *path)
  361. {
  362. return NULL;
  363. }
  364. static inline struct device_node *of_find_node_opts_by_path(const char *path,
  365. const char **opts)
  366. {
  367. return NULL;
  368. }
  369. static inline struct device_node *of_find_node_by_phandle(phandle handle)
  370. {
  371. return NULL;
  372. }
  373. static inline struct device_node *of_get_parent(const struct device_node *node)
  374. {
  375. return NULL;
  376. }
  377. static inline struct device_node *of_get_next_child(
  378. const struct device_node *node, struct device_node *prev)
  379. {
  380. return NULL;
  381. }
  382. static inline struct device_node *of_get_next_available_child(
  383. const struct device_node *node, struct device_node *prev)
  384. {
  385. return NULL;
  386. }
  387. static inline struct device_node *of_find_node_with_property(
  388. struct device_node *from, const char *prop_name)
  389. {
  390. return NULL;
  391. }
  392. static inline bool of_have_populated_dt(void)
  393. {
  394. return false;
  395. }
  396. static inline struct device_node *of_get_child_by_name(
  397. const struct device_node *node,
  398. const char *name)
  399. {
  400. return NULL;
  401. }
  402. static inline int of_device_is_compatible(const struct device_node *device,
  403. const char *name)
  404. {
  405. return 0;
  406. }
  407. static inline bool of_device_is_available(const struct device_node *device)
  408. {
  409. return false;
  410. }
  411. static inline bool of_device_is_big_endian(const struct device_node *device)
  412. {
  413. return false;
  414. }
  415. static inline struct property *of_find_property(const struct device_node *np,
  416. const char *name,
  417. int *lenp)
  418. {
  419. return NULL;
  420. }
  421. static inline struct device_node *of_find_compatible_node(
  422. struct device_node *from,
  423. const char *type,
  424. const char *compat)
  425. {
  426. return NULL;
  427. }
  428. static inline int of_property_count_elems_of_size(const struct device_node *np,
  429. const char *propname, int elem_size)
  430. {
  431. return -ENOSYS;
  432. }
  433. static inline int of_property_read_u32_index(const struct device_node *np,
  434. const char *propname, u32 index, u32 *out_value)
  435. {
  436. return -ENOSYS;
  437. }
  438. static inline int of_property_read_u8_array(const struct device_node *np,
  439. const char *propname, u8 *out_values, size_t sz)
  440. {
  441. return -ENOSYS;
  442. }
  443. static inline int of_property_read_u16_array(const struct device_node *np,
  444. const char *propname, u16 *out_values, size_t sz)
  445. {
  446. return -ENOSYS;
  447. }
  448. static inline int of_property_read_u32_array(const struct device_node *np,
  449. const char *propname,
  450. u32 *out_values, size_t sz)
  451. {
  452. return -ENOSYS;
  453. }
  454. static inline int of_property_read_u64_array(const struct device_node *np,
  455. const char *propname,
  456. u64 *out_values, size_t sz)
  457. {
  458. return -ENOSYS;
  459. }
  460. static inline int of_property_read_string(const struct device_node *np,
  461. const char *propname,
  462. const char **out_string)
  463. {
  464. return -ENOSYS;
  465. }
  466. static inline int of_property_read_string_helper(const struct device_node *np,
  467. const char *propname,
  468. const char **out_strs, size_t sz, int index)
  469. {
  470. return -ENOSYS;
  471. }
  472. static inline const void *of_get_property(const struct device_node *node,
  473. const char *name,
  474. int *lenp)
  475. {
  476. return NULL;
  477. }
  478. static inline struct device_node *of_get_cpu_node(int cpu,
  479. unsigned int *thread)
  480. {
  481. return NULL;
  482. }
  483. static inline int of_property_read_u64(const struct device_node *np,
  484. const char *propname, u64 *out_value)
  485. {
  486. return -ENOSYS;
  487. }
  488. static inline int of_property_match_string(const struct device_node *np,
  489. const char *propname,
  490. const char *string)
  491. {
  492. return -ENOSYS;
  493. }
  494. static inline struct device_node *of_parse_phandle(const struct device_node *np,
  495. const char *phandle_name,
  496. int index)
  497. {
  498. return NULL;
  499. }
  500. static inline int of_parse_phandle_with_args(struct device_node *np,
  501. const char *list_name,
  502. const char *cells_name,
  503. int index,
  504. struct of_phandle_args *out_args)
  505. {
  506. return -ENOSYS;
  507. }
  508. static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
  509. const char *list_name, int cells_count, int index,
  510. struct of_phandle_args *out_args)
  511. {
  512. return -ENOSYS;
  513. }
  514. static inline int of_count_phandle_with_args(struct device_node *np,
  515. const char *list_name,
  516. const char *cells_name)
  517. {
  518. return -ENOSYS;
  519. }
  520. static inline int of_alias_get_id(struct device_node *np, const char *stem)
  521. {
  522. return -ENOSYS;
  523. }
  524. static inline int of_alias_get_highest_id(const char *stem)
  525. {
  526. return -ENOSYS;
  527. }
  528. static inline int of_machine_is_compatible(const char *compat)
  529. {
  530. return 0;
  531. }
  532. static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
  533. {
  534. return false;
  535. }
  536. static inline const __be32 *of_prop_next_u32(struct property *prop,
  537. const __be32 *cur, u32 *pu)
  538. {
  539. return NULL;
  540. }
  541. static inline const char *of_prop_next_string(struct property *prop,
  542. const char *cur)
  543. {
  544. return NULL;
  545. }
  546. static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
  547. {
  548. return 0;
  549. }
  550. static inline int of_node_test_and_set_flag(struct device_node *n,
  551. unsigned long flag)
  552. {
  553. return 0;
  554. }
  555. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  556. {
  557. }
  558. static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
  559. {
  560. }
  561. static inline int of_property_check_flag(struct property *p, unsigned long flag)
  562. {
  563. return 0;
  564. }
  565. static inline void of_property_set_flag(struct property *p, unsigned long flag)
  566. {
  567. }
  568. static inline void of_property_clear_flag(struct property *p, unsigned long flag)
  569. {
  570. }
  571. #define of_match_ptr(_ptr) NULL
  572. #define of_match_node(_matches, _node) NULL
  573. #endif /* CONFIG_OF */
  574. #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
  575. extern int of_node_to_nid(struct device_node *np);
  576. #else
  577. static inline int of_node_to_nid(struct device_node *device)
  578. {
  579. return NUMA_NO_NODE;
  580. }
  581. #endif
  582. static inline struct device_node *of_find_matching_node(
  583. struct device_node *from,
  584. const struct of_device_id *matches)
  585. {
  586. return of_find_matching_node_and_match(from, matches, NULL);
  587. }
  588. /**
  589. * of_property_count_u8_elems - Count the number of u8 elements in a property
  590. *
  591. * @np: device node from which the property value is to be read.
  592. * @propname: name of the property to be searched.
  593. *
  594. * Search for a property in a device node and count the number of u8 elements
  595. * in it. Returns number of elements on sucess, -EINVAL if the property does
  596. * not exist or its length does not match a multiple of u8 and -ENODATA if the
  597. * property does not have a value.
  598. */
  599. static inline int of_property_count_u8_elems(const struct device_node *np,
  600. const char *propname)
  601. {
  602. return of_property_count_elems_of_size(np, propname, sizeof(u8));
  603. }
  604. /**
  605. * of_property_count_u16_elems - Count the number of u16 elements in a property
  606. *
  607. * @np: device node from which the property value is to be read.
  608. * @propname: name of the property to be searched.
  609. *
  610. * Search for a property in a device node and count the number of u16 elements
  611. * in it. Returns number of elements on sucess, -EINVAL if the property does
  612. * not exist or its length does not match a multiple of u16 and -ENODATA if the
  613. * property does not have a value.
  614. */
  615. static inline int of_property_count_u16_elems(const struct device_node *np,
  616. const char *propname)
  617. {
  618. return of_property_count_elems_of_size(np, propname, sizeof(u16));
  619. }
  620. /**
  621. * of_property_count_u32_elems - Count the number of u32 elements in a property
  622. *
  623. * @np: device node from which the property value is to be read.
  624. * @propname: name of the property to be searched.
  625. *
  626. * Search for a property in a device node and count the number of u32 elements
  627. * in it. Returns number of elements on sucess, -EINVAL if the property does
  628. * not exist or its length does not match a multiple of u32 and -ENODATA if the
  629. * property does not have a value.
  630. */
  631. static inline int of_property_count_u32_elems(const struct device_node *np,
  632. const char *propname)
  633. {
  634. return of_property_count_elems_of_size(np, propname, sizeof(u32));
  635. }
  636. /**
  637. * of_property_count_u64_elems - Count the number of u64 elements in a property
  638. *
  639. * @np: device node from which the property value is to be read.
  640. * @propname: name of the property to be searched.
  641. *
  642. * Search for a property in a device node and count the number of u64 elements
  643. * in it. Returns number of elements on sucess, -EINVAL if the property does
  644. * not exist or its length does not match a multiple of u64 and -ENODATA if the
  645. * property does not have a value.
  646. */
  647. static inline int of_property_count_u64_elems(const struct device_node *np,
  648. const char *propname)
  649. {
  650. return of_property_count_elems_of_size(np, propname, sizeof(u64));
  651. }
  652. /**
  653. * of_property_read_string_array() - Read an array of strings from a multiple
  654. * strings property.
  655. * @np: device node from which the property value is to be read.
  656. * @propname: name of the property to be searched.
  657. * @out_strs: output array of string pointers.
  658. * @sz: number of array elements to read.
  659. *
  660. * Search for a property in a device tree node and retrieve a list of
  661. * terminated string values (pointer to data, not a copy) in that property.
  662. *
  663. * If @out_strs is NULL, the number of strings in the property is returned.
  664. */
  665. static inline int of_property_read_string_array(const struct device_node *np,
  666. const char *propname, const char **out_strs,
  667. size_t sz)
  668. {
  669. return of_property_read_string_helper(np, propname, out_strs, sz, 0);
  670. }
  671. /**
  672. * of_property_count_strings() - Find and return the number of strings from a
  673. * multiple strings property.
  674. * @np: device node from which the property value is to be read.
  675. * @propname: name of the property to be searched.
  676. *
  677. * Search for a property in a device tree node and retrieve the number of null
  678. * terminated string contain in it. Returns the number of strings on
  679. * success, -EINVAL if the property does not exist, -ENODATA if property
  680. * does not have a value, and -EILSEQ if the string is not null-terminated
  681. * within the length of the property data.
  682. */
  683. static inline int of_property_count_strings(const struct device_node *np,
  684. const char *propname)
  685. {
  686. return of_property_read_string_helper(np, propname, NULL, 0, 0);
  687. }
  688. /**
  689. * of_property_read_string_index() - Find and read a string from a multiple
  690. * strings property.
  691. * @np: device node from which the property value is to be read.
  692. * @propname: name of the property to be searched.
  693. * @index: index of the string in the list of strings
  694. * @out_string: pointer to null terminated return string, modified only if
  695. * return value is 0.
  696. *
  697. * Search for a property in a device tree node and retrieve a null
  698. * terminated string value (pointer to data, not a copy) in the list of strings
  699. * contained in that property.
  700. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  701. * property does not have a value, and -EILSEQ if the string is not
  702. * null-terminated within the length of the property data.
  703. *
  704. * The out_string pointer is modified only if a valid string can be decoded.
  705. */
  706. static inline int of_property_read_string_index(const struct device_node *np,
  707. const char *propname,
  708. int index, const char **output)
  709. {
  710. int rc = of_property_read_string_helper(np, propname, output, 1, index);
  711. return rc < 0 ? rc : 0;
  712. }
  713. /**
  714. * of_property_read_bool - Findfrom a property
  715. * @np: device node from which the property value is to be read.
  716. * @propname: name of the property to be searched.
  717. *
  718. * Search for a property in a device node.
  719. * Returns true if the property exists false otherwise.
  720. */
  721. static inline bool of_property_read_bool(const struct device_node *np,
  722. const char *propname)
  723. {
  724. struct property *prop = of_find_property(np, propname, NULL);
  725. return prop ? true : false;
  726. }
  727. static inline int of_property_read_u8(const struct device_node *np,
  728. const char *propname,
  729. u8 *out_value)
  730. {
  731. return of_property_read_u8_array(np, propname, out_value, 1);
  732. }
  733. static inline int of_property_read_u16(const struct device_node *np,
  734. const char *propname,
  735. u16 *out_value)
  736. {
  737. return of_property_read_u16_array(np, propname, out_value, 1);
  738. }
  739. static inline int of_property_read_u32(const struct device_node *np,
  740. const char *propname,
  741. u32 *out_value)
  742. {
  743. return of_property_read_u32_array(np, propname, out_value, 1);
  744. }
  745. static inline int of_property_read_s32(const struct device_node *np,
  746. const char *propname,
  747. s32 *out_value)
  748. {
  749. return of_property_read_u32(np, propname, (u32*) out_value);
  750. }
  751. #define of_property_for_each_u32(np, propname, prop, p, u) \
  752. for (prop = of_find_property(np, propname, NULL), \
  753. p = of_prop_next_u32(prop, NULL, &u); \
  754. p; \
  755. p = of_prop_next_u32(prop, p, &u))
  756. #define of_property_for_each_string(np, propname, prop, s) \
  757. for (prop = of_find_property(np, propname, NULL), \
  758. s = of_prop_next_string(prop, NULL); \
  759. s; \
  760. s = of_prop_next_string(prop, s))
  761. #define for_each_node_by_name(dn, name) \
  762. for (dn = of_find_node_by_name(NULL, name); dn; \
  763. dn = of_find_node_by_name(dn, name))
  764. #define for_each_node_by_type(dn, type) \
  765. for (dn = of_find_node_by_type(NULL, type); dn; \
  766. dn = of_find_node_by_type(dn, type))
  767. #define for_each_compatible_node(dn, type, compatible) \
  768. for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
  769. dn = of_find_compatible_node(dn, type, compatible))
  770. #define for_each_matching_node(dn, matches) \
  771. for (dn = of_find_matching_node(NULL, matches); dn; \
  772. dn = of_find_matching_node(dn, matches))
  773. #define for_each_matching_node_and_match(dn, matches, match) \
  774. for (dn = of_find_matching_node_and_match(NULL, matches, match); \
  775. dn; dn = of_find_matching_node_and_match(dn, matches, match))
  776. #define for_each_child_of_node(parent, child) \
  777. for (child = of_get_next_child(parent, NULL); child != NULL; \
  778. child = of_get_next_child(parent, child))
  779. #define for_each_available_child_of_node(parent, child) \
  780. for (child = of_get_next_available_child(parent, NULL); child != NULL; \
  781. child = of_get_next_available_child(parent, child))
  782. #define for_each_node_with_property(dn, prop_name) \
  783. for (dn = of_find_node_with_property(NULL, prop_name); dn; \
  784. dn = of_find_node_with_property(dn, prop_name))
  785. static inline int of_get_child_count(const struct device_node *np)
  786. {
  787. struct device_node *child;
  788. int num = 0;
  789. for_each_child_of_node(np, child)
  790. num++;
  791. return num;
  792. }
  793. static inline int of_get_available_child_count(const struct device_node *np)
  794. {
  795. struct device_node *child;
  796. int num = 0;
  797. for_each_available_child_of_node(np, child)
  798. num++;
  799. return num;
  800. }
  801. #ifdef CONFIG_OF
  802. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  803. static const struct of_device_id __of_table_##name \
  804. __used __section(__##table##_of_table) \
  805. = { .compatible = compat, \
  806. .data = (fn == (fn_type)NULL) ? fn : fn }
  807. #else
  808. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  809. static const struct of_device_id __of_table_##name \
  810. __attribute__((unused)) \
  811. = { .compatible = compat, \
  812. .data = (fn == (fn_type)NULL) ? fn : fn }
  813. #endif
  814. typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
  815. typedef void (*of_init_fn_1)(struct device_node *);
  816. #define OF_DECLARE_1(table, name, compat, fn) \
  817. _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
  818. #define OF_DECLARE_2(table, name, compat, fn) \
  819. _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
  820. /**
  821. * struct of_changeset_entry - Holds a changeset entry
  822. *
  823. * @node: list_head for the log list
  824. * @action: notifier action
  825. * @np: pointer to the device node affected
  826. * @prop: pointer to the property affected
  827. * @old_prop: hold a pointer to the original property
  828. *
  829. * Every modification of the device tree during a changeset
  830. * is held in a list of of_changeset_entry structures.
  831. * That way we can recover from a partial application, or we can
  832. * revert the changeset
  833. */
  834. struct of_changeset_entry {
  835. struct list_head node;
  836. unsigned long action;
  837. struct device_node *np;
  838. struct property *prop;
  839. struct property *old_prop;
  840. };
  841. /**
  842. * struct of_changeset - changeset tracker structure
  843. *
  844. * @entries: list_head for the changeset entries
  845. *
  846. * changesets are a convenient way to apply bulk changes to the
  847. * live tree. In case of an error, changes are rolled-back.
  848. * changesets live on after initial application, and if not
  849. * destroyed after use, they can be reverted in one single call.
  850. */
  851. struct of_changeset {
  852. struct list_head entries;
  853. };
  854. enum of_reconfig_change {
  855. OF_RECONFIG_NO_CHANGE = 0,
  856. OF_RECONFIG_CHANGE_ADD,
  857. OF_RECONFIG_CHANGE_REMOVE,
  858. };
  859. #ifdef CONFIG_OF_DYNAMIC
  860. extern int of_reconfig_notifier_register(struct notifier_block *);
  861. extern int of_reconfig_notifier_unregister(struct notifier_block *);
  862. extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
  863. extern int of_reconfig_get_state_change(unsigned long action,
  864. struct of_reconfig_data *arg);
  865. extern void of_changeset_init(struct of_changeset *ocs);
  866. extern void of_changeset_destroy(struct of_changeset *ocs);
  867. extern int of_changeset_apply(struct of_changeset *ocs);
  868. extern int of_changeset_revert(struct of_changeset *ocs);
  869. extern int of_changeset_action(struct of_changeset *ocs,
  870. unsigned long action, struct device_node *np,
  871. struct property *prop);
  872. static inline int of_changeset_attach_node(struct of_changeset *ocs,
  873. struct device_node *np)
  874. {
  875. return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
  876. }
  877. static inline int of_changeset_detach_node(struct of_changeset *ocs,
  878. struct device_node *np)
  879. {
  880. return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
  881. }
  882. static inline int of_changeset_add_property(struct of_changeset *ocs,
  883. struct device_node *np, struct property *prop)
  884. {
  885. return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
  886. }
  887. static inline int of_changeset_remove_property(struct of_changeset *ocs,
  888. struct device_node *np, struct property *prop)
  889. {
  890. return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  891. }
  892. static inline int of_changeset_update_property(struct of_changeset *ocs,
  893. struct device_node *np, struct property *prop)
  894. {
  895. return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
  896. }
  897. #else /* CONFIG_OF_DYNAMIC */
  898. static inline int of_reconfig_notifier_register(struct notifier_block *nb)
  899. {
  900. return -EINVAL;
  901. }
  902. static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
  903. {
  904. return -EINVAL;
  905. }
  906. static inline int of_reconfig_notify(unsigned long action,
  907. struct of_reconfig_data *arg)
  908. {
  909. return -EINVAL;
  910. }
  911. static inline int of_reconfig_get_state_change(unsigned long action,
  912. struct of_reconfig_data *arg)
  913. {
  914. return -EINVAL;
  915. }
  916. #endif /* CONFIG_OF_DYNAMIC */
  917. /* CONFIG_OF_RESOLVE api */
  918. extern int of_resolve_phandles(struct device_node *tree);
  919. /**
  920. * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
  921. * @np: Pointer to the given device_node
  922. *
  923. * return true if present false otherwise
  924. */
  925. static inline bool of_device_is_system_power_controller(const struct device_node *np)
  926. {
  927. return of_property_read_bool(np, "system-power-controller");
  928. }
  929. /**
  930. * Overlay support
  931. */
  932. #ifdef CONFIG_OF_OVERLAY
  933. /* ID based overlays; the API for external users */
  934. int of_overlay_create(struct device_node *tree);
  935. int of_overlay_destroy(int id);
  936. int of_overlay_destroy_all(void);
  937. #else
  938. static inline int of_overlay_create(struct device_node *tree)
  939. {
  940. return -ENOTSUPP;
  941. }
  942. static inline int of_overlay_destroy(int id)
  943. {
  944. return -ENOTSUPP;
  945. }
  946. static inline int of_overlay_destroy_all(void)
  947. {
  948. return -ENOTSUPP;
  949. }
  950. #endif
  951. #endif /* _LINUX_OF_H */