of.h 33 KB

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