of.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. #ifdef CONFIG_OF_NUMA
  583. extern int of_numa_init(void);
  584. #else
  585. static inline int of_numa_init(void)
  586. {
  587. return -ENOSYS;
  588. }
  589. #endif
  590. static inline struct device_node *of_find_matching_node(
  591. struct device_node *from,
  592. const struct of_device_id *matches)
  593. {
  594. return of_find_matching_node_and_match(from, matches, NULL);
  595. }
  596. /**
  597. * of_property_count_u8_elems - Count the number of u8 elements in a property
  598. *
  599. * @np: device node from which the property value is to be read.
  600. * @propname: name of the property to be searched.
  601. *
  602. * Search for a property in a device node and count the number of u8 elements
  603. * in it. Returns number of elements on sucess, -EINVAL if the property does
  604. * not exist or its length does not match a multiple of u8 and -ENODATA if the
  605. * property does not have a value.
  606. */
  607. static inline int of_property_count_u8_elems(const struct device_node *np,
  608. const char *propname)
  609. {
  610. return of_property_count_elems_of_size(np, propname, sizeof(u8));
  611. }
  612. /**
  613. * of_property_count_u16_elems - Count the number of u16 elements in a property
  614. *
  615. * @np: device node from which the property value is to be read.
  616. * @propname: name of the property to be searched.
  617. *
  618. * Search for a property in a device node and count the number of u16 elements
  619. * in it. Returns number of elements on sucess, -EINVAL if the property does
  620. * not exist or its length does not match a multiple of u16 and -ENODATA if the
  621. * property does not have a value.
  622. */
  623. static inline int of_property_count_u16_elems(const struct device_node *np,
  624. const char *propname)
  625. {
  626. return of_property_count_elems_of_size(np, propname, sizeof(u16));
  627. }
  628. /**
  629. * of_property_count_u32_elems - Count the number of u32 elements in a property
  630. *
  631. * @np: device node from which the property value is to be read.
  632. * @propname: name of the property to be searched.
  633. *
  634. * Search for a property in a device node and count the number of u32 elements
  635. * in it. Returns number of elements on sucess, -EINVAL if the property does
  636. * not exist or its length does not match a multiple of u32 and -ENODATA if the
  637. * property does not have a value.
  638. */
  639. static inline int of_property_count_u32_elems(const struct device_node *np,
  640. const char *propname)
  641. {
  642. return of_property_count_elems_of_size(np, propname, sizeof(u32));
  643. }
  644. /**
  645. * of_property_count_u64_elems - Count the number of u64 elements in a property
  646. *
  647. * @np: device node from which the property value is to be read.
  648. * @propname: name of the property to be searched.
  649. *
  650. * Search for a property in a device node and count the number of u64 elements
  651. * in it. Returns number of elements on sucess, -EINVAL if the property does
  652. * not exist or its length does not match a multiple of u64 and -ENODATA if the
  653. * property does not have a value.
  654. */
  655. static inline int of_property_count_u64_elems(const struct device_node *np,
  656. const char *propname)
  657. {
  658. return of_property_count_elems_of_size(np, propname, sizeof(u64));
  659. }
  660. /**
  661. * of_property_read_string_array() - Read an array of strings from a multiple
  662. * strings property.
  663. * @np: device node from which the property value is to be read.
  664. * @propname: name of the property to be searched.
  665. * @out_strs: output array of string pointers.
  666. * @sz: number of array elements to read.
  667. *
  668. * Search for a property in a device tree node and retrieve a list of
  669. * terminated string values (pointer to data, not a copy) in that property.
  670. *
  671. * If @out_strs is NULL, the number of strings in the property is returned.
  672. */
  673. static inline int of_property_read_string_array(const struct device_node *np,
  674. const char *propname, const char **out_strs,
  675. size_t sz)
  676. {
  677. return of_property_read_string_helper(np, propname, out_strs, sz, 0);
  678. }
  679. /**
  680. * of_property_count_strings() - Find and return the number of strings from a
  681. * multiple strings property.
  682. * @np: device node from which the property value is to be read.
  683. * @propname: name of the property to be searched.
  684. *
  685. * Search for a property in a device tree node and retrieve the number of null
  686. * terminated string contain in it. Returns the number of strings on
  687. * success, -EINVAL if the property does not exist, -ENODATA if property
  688. * does not have a value, and -EILSEQ if the string is not null-terminated
  689. * within the length of the property data.
  690. */
  691. static inline int of_property_count_strings(const struct device_node *np,
  692. const char *propname)
  693. {
  694. return of_property_read_string_helper(np, propname, NULL, 0, 0);
  695. }
  696. /**
  697. * of_property_read_string_index() - Find and read a string from a multiple
  698. * strings property.
  699. * @np: device node from which the property value is to be read.
  700. * @propname: name of the property to be searched.
  701. * @index: index of the string in the list of strings
  702. * @out_string: pointer to null terminated return string, modified only if
  703. * return value is 0.
  704. *
  705. * Search for a property in a device tree node and retrieve a null
  706. * terminated string value (pointer to data, not a copy) in the list of strings
  707. * contained in that property.
  708. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  709. * property does not have a value, and -EILSEQ if the string is not
  710. * null-terminated within the length of the property data.
  711. *
  712. * The out_string pointer is modified only if a valid string can be decoded.
  713. */
  714. static inline int of_property_read_string_index(const struct device_node *np,
  715. const char *propname,
  716. int index, const char **output)
  717. {
  718. int rc = of_property_read_string_helper(np, propname, output, 1, index);
  719. return rc < 0 ? rc : 0;
  720. }
  721. /**
  722. * of_property_read_bool - Findfrom a property
  723. * @np: device node from which the property value is to be read.
  724. * @propname: name of the property to be searched.
  725. *
  726. * Search for a property in a device node.
  727. * Returns true if the property exists false otherwise.
  728. */
  729. static inline bool of_property_read_bool(const struct device_node *np,
  730. const char *propname)
  731. {
  732. struct property *prop = of_find_property(np, propname, NULL);
  733. return prop ? true : false;
  734. }
  735. static inline int of_property_read_u8(const struct device_node *np,
  736. const char *propname,
  737. u8 *out_value)
  738. {
  739. return of_property_read_u8_array(np, propname, out_value, 1);
  740. }
  741. static inline int of_property_read_u16(const struct device_node *np,
  742. const char *propname,
  743. u16 *out_value)
  744. {
  745. return of_property_read_u16_array(np, propname, out_value, 1);
  746. }
  747. static inline int of_property_read_u32(const struct device_node *np,
  748. const char *propname,
  749. u32 *out_value)
  750. {
  751. return of_property_read_u32_array(np, propname, out_value, 1);
  752. }
  753. static inline int of_property_read_s32(const struct device_node *np,
  754. const char *propname,
  755. s32 *out_value)
  756. {
  757. return of_property_read_u32(np, propname, (u32*) out_value);
  758. }
  759. #define of_property_for_each_u32(np, propname, prop, p, u) \
  760. for (prop = of_find_property(np, propname, NULL), \
  761. p = of_prop_next_u32(prop, NULL, &u); \
  762. p; \
  763. p = of_prop_next_u32(prop, p, &u))
  764. #define of_property_for_each_string(np, propname, prop, s) \
  765. for (prop = of_find_property(np, propname, NULL), \
  766. s = of_prop_next_string(prop, NULL); \
  767. s; \
  768. s = of_prop_next_string(prop, s))
  769. #define for_each_node_by_name(dn, name) \
  770. for (dn = of_find_node_by_name(NULL, name); dn; \
  771. dn = of_find_node_by_name(dn, name))
  772. #define for_each_node_by_type(dn, type) \
  773. for (dn = of_find_node_by_type(NULL, type); dn; \
  774. dn = of_find_node_by_type(dn, type))
  775. #define for_each_compatible_node(dn, type, compatible) \
  776. for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
  777. dn = of_find_compatible_node(dn, type, compatible))
  778. #define for_each_matching_node(dn, matches) \
  779. for (dn = of_find_matching_node(NULL, matches); dn; \
  780. dn = of_find_matching_node(dn, matches))
  781. #define for_each_matching_node_and_match(dn, matches, match) \
  782. for (dn = of_find_matching_node_and_match(NULL, matches, match); \
  783. dn; dn = of_find_matching_node_and_match(dn, matches, match))
  784. #define for_each_child_of_node(parent, child) \
  785. for (child = of_get_next_child(parent, NULL); child != NULL; \
  786. child = of_get_next_child(parent, child))
  787. #define for_each_available_child_of_node(parent, child) \
  788. for (child = of_get_next_available_child(parent, NULL); child != NULL; \
  789. child = of_get_next_available_child(parent, child))
  790. #define for_each_node_with_property(dn, prop_name) \
  791. for (dn = of_find_node_with_property(NULL, prop_name); dn; \
  792. dn = of_find_node_with_property(dn, prop_name))
  793. static inline int of_get_child_count(const struct device_node *np)
  794. {
  795. struct device_node *child;
  796. int num = 0;
  797. for_each_child_of_node(np, child)
  798. num++;
  799. return num;
  800. }
  801. static inline int of_get_available_child_count(const struct device_node *np)
  802. {
  803. struct device_node *child;
  804. int num = 0;
  805. for_each_available_child_of_node(np, child)
  806. num++;
  807. return num;
  808. }
  809. #if defined(CONFIG_OF) && !defined(MODULE)
  810. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  811. static const struct of_device_id __of_table_##name \
  812. __used __section(__##table##_of_table) \
  813. = { .compatible = compat, \
  814. .data = (fn == (fn_type)NULL) ? fn : fn }
  815. #else
  816. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  817. static const struct of_device_id __of_table_##name \
  818. __attribute__((unused)) \
  819. = { .compatible = compat, \
  820. .data = (fn == (fn_type)NULL) ? fn : fn }
  821. #endif
  822. typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
  823. typedef void (*of_init_fn_1)(struct device_node *);
  824. #define OF_DECLARE_1(table, name, compat, fn) \
  825. _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
  826. #define OF_DECLARE_2(table, name, compat, fn) \
  827. _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
  828. /**
  829. * struct of_changeset_entry - Holds a changeset entry
  830. *
  831. * @node: list_head for the log list
  832. * @action: notifier action
  833. * @np: pointer to the device node affected
  834. * @prop: pointer to the property affected
  835. * @old_prop: hold a pointer to the original property
  836. *
  837. * Every modification of the device tree during a changeset
  838. * is held in a list of of_changeset_entry structures.
  839. * That way we can recover from a partial application, or we can
  840. * revert the changeset
  841. */
  842. struct of_changeset_entry {
  843. struct list_head node;
  844. unsigned long action;
  845. struct device_node *np;
  846. struct property *prop;
  847. struct property *old_prop;
  848. };
  849. /**
  850. * struct of_changeset - changeset tracker structure
  851. *
  852. * @entries: list_head for the changeset entries
  853. *
  854. * changesets are a convenient way to apply bulk changes to the
  855. * live tree. In case of an error, changes are rolled-back.
  856. * changesets live on after initial application, and if not
  857. * destroyed after use, they can be reverted in one single call.
  858. */
  859. struct of_changeset {
  860. struct list_head entries;
  861. };
  862. enum of_reconfig_change {
  863. OF_RECONFIG_NO_CHANGE = 0,
  864. OF_RECONFIG_CHANGE_ADD,
  865. OF_RECONFIG_CHANGE_REMOVE,
  866. };
  867. #ifdef CONFIG_OF_DYNAMIC
  868. extern int of_reconfig_notifier_register(struct notifier_block *);
  869. extern int of_reconfig_notifier_unregister(struct notifier_block *);
  870. extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
  871. extern int of_reconfig_get_state_change(unsigned long action,
  872. struct of_reconfig_data *arg);
  873. extern void of_changeset_init(struct of_changeset *ocs);
  874. extern void of_changeset_destroy(struct of_changeset *ocs);
  875. extern int of_changeset_apply(struct of_changeset *ocs);
  876. extern int of_changeset_revert(struct of_changeset *ocs);
  877. extern int of_changeset_action(struct of_changeset *ocs,
  878. unsigned long action, struct device_node *np,
  879. struct property *prop);
  880. static inline int of_changeset_attach_node(struct of_changeset *ocs,
  881. struct device_node *np)
  882. {
  883. return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
  884. }
  885. static inline int of_changeset_detach_node(struct of_changeset *ocs,
  886. struct device_node *np)
  887. {
  888. return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
  889. }
  890. static inline int of_changeset_add_property(struct of_changeset *ocs,
  891. struct device_node *np, struct property *prop)
  892. {
  893. return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
  894. }
  895. static inline int of_changeset_remove_property(struct of_changeset *ocs,
  896. struct device_node *np, struct property *prop)
  897. {
  898. return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  899. }
  900. static inline int of_changeset_update_property(struct of_changeset *ocs,
  901. struct device_node *np, struct property *prop)
  902. {
  903. return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
  904. }
  905. #else /* CONFIG_OF_DYNAMIC */
  906. static inline int of_reconfig_notifier_register(struct notifier_block *nb)
  907. {
  908. return -EINVAL;
  909. }
  910. static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
  911. {
  912. return -EINVAL;
  913. }
  914. static inline int of_reconfig_notify(unsigned long action,
  915. struct of_reconfig_data *arg)
  916. {
  917. return -EINVAL;
  918. }
  919. static inline int of_reconfig_get_state_change(unsigned long action,
  920. struct of_reconfig_data *arg)
  921. {
  922. return -EINVAL;
  923. }
  924. #endif /* CONFIG_OF_DYNAMIC */
  925. /* CONFIG_OF_RESOLVE api */
  926. extern int of_resolve_phandles(struct device_node *tree);
  927. /**
  928. * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
  929. * @np: Pointer to the given device_node
  930. *
  931. * return true if present false otherwise
  932. */
  933. static inline bool of_device_is_system_power_controller(const struct device_node *np)
  934. {
  935. return of_property_read_bool(np, "system-power-controller");
  936. }
  937. /**
  938. * Overlay support
  939. */
  940. #ifdef CONFIG_OF_OVERLAY
  941. /* ID based overlays; the API for external users */
  942. int of_overlay_create(struct device_node *tree);
  943. int of_overlay_destroy(int id);
  944. int of_overlay_destroy_all(void);
  945. #else
  946. static inline int of_overlay_create(struct device_node *tree)
  947. {
  948. return -ENOTSUPP;
  949. }
  950. static inline int of_overlay_destroy(int id)
  951. {
  952. return -ENOTSUPP;
  953. }
  954. static inline int of_overlay_destroy_all(void)
  955. {
  956. return -ENOTSUPP;
  957. }
  958. #endif
  959. #endif /* _LINUX_OF_H */