of.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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 !IS_ERR_OR_NULL(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 int of_phandle_iterator_next(struct of_phandle_iterator *it);
  313. extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
  314. uint32_t *args,
  315. int size);
  316. extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
  317. extern int of_alias_get_id(struct device_node *np, const char *stem);
  318. extern int of_alias_get_highest_id(const char *stem);
  319. extern int of_machine_is_compatible(const char *compat);
  320. extern int of_add_property(struct device_node *np, struct property *prop);
  321. extern int of_remove_property(struct device_node *np, struct property *prop);
  322. extern int of_update_property(struct device_node *np, struct property *newprop);
  323. /* For updating the device tree at runtime */
  324. #define OF_RECONFIG_ATTACH_NODE 0x0001
  325. #define OF_RECONFIG_DETACH_NODE 0x0002
  326. #define OF_RECONFIG_ADD_PROPERTY 0x0003
  327. #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
  328. #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
  329. extern int of_attach_node(struct device_node *);
  330. extern int of_detach_node(struct device_node *);
  331. #define of_match_ptr(_ptr) (_ptr)
  332. /*
  333. * struct property *prop;
  334. * const __be32 *p;
  335. * u32 u;
  336. *
  337. * of_property_for_each_u32(np, "propname", prop, p, u)
  338. * printk("U32 value: %x\n", u);
  339. */
  340. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  341. u32 *pu);
  342. /*
  343. * struct property *prop;
  344. * const char *s;
  345. *
  346. * of_property_for_each_string(np, "propname", prop, s)
  347. * printk("String value: %s\n", s);
  348. */
  349. const char *of_prop_next_string(struct property *prop, const char *cur);
  350. bool of_console_check(struct device_node *dn, char *name, int index);
  351. #else /* CONFIG_OF */
  352. static inline void of_core_init(void)
  353. {
  354. }
  355. static inline bool is_of_node(struct fwnode_handle *fwnode)
  356. {
  357. return false;
  358. }
  359. static inline struct device_node *to_of_node(struct fwnode_handle *fwnode)
  360. {
  361. return NULL;
  362. }
  363. static inline const char* of_node_full_name(const struct device_node *np)
  364. {
  365. return "<no-node>";
  366. }
  367. static inline struct device_node *of_find_node_by_name(struct device_node *from,
  368. const char *name)
  369. {
  370. return NULL;
  371. }
  372. static inline struct device_node *of_find_node_by_type(struct device_node *from,
  373. const char *type)
  374. {
  375. return NULL;
  376. }
  377. static inline struct device_node *of_find_matching_node_and_match(
  378. struct device_node *from,
  379. const struct of_device_id *matches,
  380. const struct of_device_id **match)
  381. {
  382. return NULL;
  383. }
  384. static inline struct device_node *of_find_node_by_path(const char *path)
  385. {
  386. return NULL;
  387. }
  388. static inline struct device_node *of_find_node_opts_by_path(const char *path,
  389. const char **opts)
  390. {
  391. return NULL;
  392. }
  393. static inline struct device_node *of_find_node_by_phandle(phandle handle)
  394. {
  395. return NULL;
  396. }
  397. static inline struct device_node *of_get_parent(const struct device_node *node)
  398. {
  399. return NULL;
  400. }
  401. static inline struct device_node *of_get_next_child(
  402. const struct device_node *node, struct device_node *prev)
  403. {
  404. return NULL;
  405. }
  406. static inline struct device_node *of_get_next_available_child(
  407. const struct device_node *node, struct device_node *prev)
  408. {
  409. return NULL;
  410. }
  411. static inline struct device_node *of_find_node_with_property(
  412. struct device_node *from, const char *prop_name)
  413. {
  414. return NULL;
  415. }
  416. static inline bool of_have_populated_dt(void)
  417. {
  418. return false;
  419. }
  420. static inline struct device_node *of_get_child_by_name(
  421. const struct device_node *node,
  422. const char *name)
  423. {
  424. return NULL;
  425. }
  426. static inline int of_device_is_compatible(const struct device_node *device,
  427. const char *name)
  428. {
  429. return 0;
  430. }
  431. static inline bool of_device_is_available(const struct device_node *device)
  432. {
  433. return false;
  434. }
  435. static inline bool of_device_is_big_endian(const struct device_node *device)
  436. {
  437. return false;
  438. }
  439. static inline struct property *of_find_property(const struct device_node *np,
  440. const char *name,
  441. int *lenp)
  442. {
  443. return NULL;
  444. }
  445. static inline struct device_node *of_find_compatible_node(
  446. struct device_node *from,
  447. const char *type,
  448. const char *compat)
  449. {
  450. return NULL;
  451. }
  452. static inline int of_property_count_elems_of_size(const struct device_node *np,
  453. const char *propname, int elem_size)
  454. {
  455. return -ENOSYS;
  456. }
  457. static inline int of_property_read_u32_index(const struct device_node *np,
  458. const char *propname, u32 index, u32 *out_value)
  459. {
  460. return -ENOSYS;
  461. }
  462. static inline int of_property_read_u8_array(const struct device_node *np,
  463. const char *propname, u8 *out_values, size_t sz)
  464. {
  465. return -ENOSYS;
  466. }
  467. static inline int of_property_read_u16_array(const struct device_node *np,
  468. const char *propname, u16 *out_values, size_t sz)
  469. {
  470. return -ENOSYS;
  471. }
  472. static inline int of_property_read_u32_array(const struct device_node *np,
  473. const char *propname,
  474. u32 *out_values, size_t sz)
  475. {
  476. return -ENOSYS;
  477. }
  478. static inline int of_property_read_u64_array(const struct device_node *np,
  479. const char *propname,
  480. u64 *out_values, size_t sz)
  481. {
  482. return -ENOSYS;
  483. }
  484. static inline int of_property_read_string(const struct device_node *np,
  485. const char *propname,
  486. const char **out_string)
  487. {
  488. return -ENOSYS;
  489. }
  490. static inline int of_property_read_string_helper(const struct device_node *np,
  491. const char *propname,
  492. const char **out_strs, size_t sz, int index)
  493. {
  494. return -ENOSYS;
  495. }
  496. static inline const void *of_get_property(const struct device_node *node,
  497. const char *name,
  498. int *lenp)
  499. {
  500. return NULL;
  501. }
  502. static inline struct device_node *of_get_cpu_node(int cpu,
  503. unsigned int *thread)
  504. {
  505. return NULL;
  506. }
  507. static inline int of_property_read_u64(const struct device_node *np,
  508. const char *propname, u64 *out_value)
  509. {
  510. return -ENOSYS;
  511. }
  512. static inline int of_property_match_string(const struct device_node *np,
  513. const char *propname,
  514. const char *string)
  515. {
  516. return -ENOSYS;
  517. }
  518. static inline struct device_node *of_parse_phandle(const struct device_node *np,
  519. const char *phandle_name,
  520. int index)
  521. {
  522. return NULL;
  523. }
  524. static inline int of_parse_phandle_with_args(struct device_node *np,
  525. const char *list_name,
  526. const char *cells_name,
  527. int index,
  528. struct of_phandle_args *out_args)
  529. {
  530. return -ENOSYS;
  531. }
  532. static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
  533. const char *list_name, int cells_count, int index,
  534. struct of_phandle_args *out_args)
  535. {
  536. return -ENOSYS;
  537. }
  538. static inline int of_count_phandle_with_args(struct device_node *np,
  539. const char *list_name,
  540. const char *cells_name)
  541. {
  542. return -ENOSYS;
  543. }
  544. static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
  545. const struct device_node *np,
  546. const char *list_name,
  547. const char *cells_name,
  548. int cell_count)
  549. {
  550. return -ENOSYS;
  551. }
  552. static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
  553. {
  554. return -ENOSYS;
  555. }
  556. static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
  557. uint32_t *args,
  558. int size)
  559. {
  560. return 0;
  561. }
  562. static inline int of_alias_get_id(struct device_node *np, const char *stem)
  563. {
  564. return -ENOSYS;
  565. }
  566. static inline int of_alias_get_highest_id(const char *stem)
  567. {
  568. return -ENOSYS;
  569. }
  570. static inline int of_machine_is_compatible(const char *compat)
  571. {
  572. return 0;
  573. }
  574. static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
  575. {
  576. return false;
  577. }
  578. static inline const __be32 *of_prop_next_u32(struct property *prop,
  579. const __be32 *cur, u32 *pu)
  580. {
  581. return NULL;
  582. }
  583. static inline const char *of_prop_next_string(struct property *prop,
  584. const char *cur)
  585. {
  586. return NULL;
  587. }
  588. static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
  589. {
  590. return 0;
  591. }
  592. static inline int of_node_test_and_set_flag(struct device_node *n,
  593. unsigned long flag)
  594. {
  595. return 0;
  596. }
  597. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  598. {
  599. }
  600. static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
  601. {
  602. }
  603. static inline int of_property_check_flag(struct property *p, unsigned long flag)
  604. {
  605. return 0;
  606. }
  607. static inline void of_property_set_flag(struct property *p, unsigned long flag)
  608. {
  609. }
  610. static inline void of_property_clear_flag(struct property *p, unsigned long flag)
  611. {
  612. }
  613. #define of_match_ptr(_ptr) NULL
  614. #define of_match_node(_matches, _node) NULL
  615. #endif /* CONFIG_OF */
  616. #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
  617. extern int of_node_to_nid(struct device_node *np);
  618. #else
  619. static inline int of_node_to_nid(struct device_node *device)
  620. {
  621. return NUMA_NO_NODE;
  622. }
  623. #endif
  624. #ifdef CONFIG_OF_NUMA
  625. extern int of_numa_init(void);
  626. #else
  627. static inline int of_numa_init(void)
  628. {
  629. return -ENOSYS;
  630. }
  631. #endif
  632. static inline struct device_node *of_find_matching_node(
  633. struct device_node *from,
  634. const struct of_device_id *matches)
  635. {
  636. return of_find_matching_node_and_match(from, matches, NULL);
  637. }
  638. /**
  639. * of_property_count_u8_elems - Count the number of u8 elements in a property
  640. *
  641. * @np: device node from which the property value is to be read.
  642. * @propname: name of the property to be searched.
  643. *
  644. * Search for a property in a device node and count the number of u8 elements
  645. * in it. Returns number of elements on sucess, -EINVAL if the property does
  646. * not exist or its length does not match a multiple of u8 and -ENODATA if the
  647. * property does not have a value.
  648. */
  649. static inline int of_property_count_u8_elems(const struct device_node *np,
  650. const char *propname)
  651. {
  652. return of_property_count_elems_of_size(np, propname, sizeof(u8));
  653. }
  654. /**
  655. * of_property_count_u16_elems - Count the number of u16 elements in a property
  656. *
  657. * @np: device node from which the property value is to be read.
  658. * @propname: name of the property to be searched.
  659. *
  660. * Search for a property in a device node and count the number of u16 elements
  661. * in it. Returns number of elements on sucess, -EINVAL if the property does
  662. * not exist or its length does not match a multiple of u16 and -ENODATA if the
  663. * property does not have a value.
  664. */
  665. static inline int of_property_count_u16_elems(const struct device_node *np,
  666. const char *propname)
  667. {
  668. return of_property_count_elems_of_size(np, propname, sizeof(u16));
  669. }
  670. /**
  671. * of_property_count_u32_elems - Count the number of u32 elements in a property
  672. *
  673. * @np: device node from which the property value is to be read.
  674. * @propname: name of the property to be searched.
  675. *
  676. * Search for a property in a device node and count the number of u32 elements
  677. * in it. Returns number of elements on sucess, -EINVAL if the property does
  678. * not exist or its length does not match a multiple of u32 and -ENODATA if the
  679. * property does not have a value.
  680. */
  681. static inline int of_property_count_u32_elems(const struct device_node *np,
  682. const char *propname)
  683. {
  684. return of_property_count_elems_of_size(np, propname, sizeof(u32));
  685. }
  686. /**
  687. * of_property_count_u64_elems - Count the number of u64 elements in a property
  688. *
  689. * @np: device node from which the property value is to be read.
  690. * @propname: name of the property to be searched.
  691. *
  692. * Search for a property in a device node and count the number of u64 elements
  693. * in it. Returns number of elements on sucess, -EINVAL if the property does
  694. * not exist or its length does not match a multiple of u64 and -ENODATA if the
  695. * property does not have a value.
  696. */
  697. static inline int of_property_count_u64_elems(const struct device_node *np,
  698. const char *propname)
  699. {
  700. return of_property_count_elems_of_size(np, propname, sizeof(u64));
  701. }
  702. /**
  703. * of_property_read_string_array() - Read an array of strings from a multiple
  704. * strings property.
  705. * @np: device node from which the property value is to be read.
  706. * @propname: name of the property to be searched.
  707. * @out_strs: output array of string pointers.
  708. * @sz: number of array elements to read.
  709. *
  710. * Search for a property in a device tree node and retrieve a list of
  711. * terminated string values (pointer to data, not a copy) in that property.
  712. *
  713. * If @out_strs is NULL, the number of strings in the property is returned.
  714. */
  715. static inline int of_property_read_string_array(const struct device_node *np,
  716. const char *propname, const char **out_strs,
  717. size_t sz)
  718. {
  719. return of_property_read_string_helper(np, propname, out_strs, sz, 0);
  720. }
  721. /**
  722. * of_property_count_strings() - Find and return the number of strings from a
  723. * multiple strings property.
  724. * @np: device node from which the property value is to be read.
  725. * @propname: name of the property to be searched.
  726. *
  727. * Search for a property in a device tree node and retrieve the number of null
  728. * terminated string contain in it. Returns the number of strings on
  729. * success, -EINVAL if the property does not exist, -ENODATA if property
  730. * does not have a value, and -EILSEQ if the string is not null-terminated
  731. * within the length of the property data.
  732. */
  733. static inline int of_property_count_strings(const struct device_node *np,
  734. const char *propname)
  735. {
  736. return of_property_read_string_helper(np, propname, NULL, 0, 0);
  737. }
  738. /**
  739. * of_property_read_string_index() - Find and read a string from a multiple
  740. * strings property.
  741. * @np: device node from which the property value is to be read.
  742. * @propname: name of the property to be searched.
  743. * @index: index of the string in the list of strings
  744. * @out_string: pointer to null terminated return string, modified only if
  745. * return value is 0.
  746. *
  747. * Search for a property in a device tree node and retrieve a null
  748. * terminated string value (pointer to data, not a copy) in the list of strings
  749. * contained in that property.
  750. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  751. * property does not have a value, and -EILSEQ if the string is not
  752. * null-terminated within the length of the property data.
  753. *
  754. * The out_string pointer is modified only if a valid string can be decoded.
  755. */
  756. static inline int of_property_read_string_index(const struct device_node *np,
  757. const char *propname,
  758. int index, const char **output)
  759. {
  760. int rc = of_property_read_string_helper(np, propname, output, 1, index);
  761. return rc < 0 ? rc : 0;
  762. }
  763. /**
  764. * of_property_read_bool - Findfrom a property
  765. * @np: device node from which the property value is to be read.
  766. * @propname: name of the property to be searched.
  767. *
  768. * Search for a property in a device node.
  769. * Returns true if the property exists false otherwise.
  770. */
  771. static inline bool of_property_read_bool(const struct device_node *np,
  772. const char *propname)
  773. {
  774. struct property *prop = of_find_property(np, propname, NULL);
  775. return prop ? true : false;
  776. }
  777. static inline int of_property_read_u8(const struct device_node *np,
  778. const char *propname,
  779. u8 *out_value)
  780. {
  781. return of_property_read_u8_array(np, propname, out_value, 1);
  782. }
  783. static inline int of_property_read_u16(const struct device_node *np,
  784. const char *propname,
  785. u16 *out_value)
  786. {
  787. return of_property_read_u16_array(np, propname, out_value, 1);
  788. }
  789. static inline int of_property_read_u32(const struct device_node *np,
  790. const char *propname,
  791. u32 *out_value)
  792. {
  793. return of_property_read_u32_array(np, propname, out_value, 1);
  794. }
  795. static inline int of_property_read_s32(const struct device_node *np,
  796. const char *propname,
  797. s32 *out_value)
  798. {
  799. return of_property_read_u32(np, propname, (u32*) out_value);
  800. }
  801. #define of_for_each_phandle(it, err, np, ln, cn, cc) \
  802. for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
  803. err = of_phandle_iterator_next(it); \
  804. err == 0; \
  805. err = of_phandle_iterator_next(it))
  806. #define of_property_for_each_u32(np, propname, prop, p, u) \
  807. for (prop = of_find_property(np, propname, NULL), \
  808. p = of_prop_next_u32(prop, NULL, &u); \
  809. p; \
  810. p = of_prop_next_u32(prop, p, &u))
  811. #define of_property_for_each_string(np, propname, prop, s) \
  812. for (prop = of_find_property(np, propname, NULL), \
  813. s = of_prop_next_string(prop, NULL); \
  814. s; \
  815. s = of_prop_next_string(prop, s))
  816. #define for_each_node_by_name(dn, name) \
  817. for (dn = of_find_node_by_name(NULL, name); dn; \
  818. dn = of_find_node_by_name(dn, name))
  819. #define for_each_node_by_type(dn, type) \
  820. for (dn = of_find_node_by_type(NULL, type); dn; \
  821. dn = of_find_node_by_type(dn, type))
  822. #define for_each_compatible_node(dn, type, compatible) \
  823. for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
  824. dn = of_find_compatible_node(dn, type, compatible))
  825. #define for_each_matching_node(dn, matches) \
  826. for (dn = of_find_matching_node(NULL, matches); dn; \
  827. dn = of_find_matching_node(dn, matches))
  828. #define for_each_matching_node_and_match(dn, matches, match) \
  829. for (dn = of_find_matching_node_and_match(NULL, matches, match); \
  830. dn; dn = of_find_matching_node_and_match(dn, matches, match))
  831. #define for_each_child_of_node(parent, child) \
  832. for (child = of_get_next_child(parent, NULL); child != NULL; \
  833. child = of_get_next_child(parent, child))
  834. #define for_each_available_child_of_node(parent, child) \
  835. for (child = of_get_next_available_child(parent, NULL); child != NULL; \
  836. child = of_get_next_available_child(parent, child))
  837. #define for_each_node_with_property(dn, prop_name) \
  838. for (dn = of_find_node_with_property(NULL, prop_name); dn; \
  839. dn = of_find_node_with_property(dn, prop_name))
  840. static inline int of_get_child_count(const struct device_node *np)
  841. {
  842. struct device_node *child;
  843. int num = 0;
  844. for_each_child_of_node(np, child)
  845. num++;
  846. return num;
  847. }
  848. static inline int of_get_available_child_count(const struct device_node *np)
  849. {
  850. struct device_node *child;
  851. int num = 0;
  852. for_each_available_child_of_node(np, child)
  853. num++;
  854. return num;
  855. }
  856. #if defined(CONFIG_OF) && !defined(MODULE)
  857. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  858. static const struct of_device_id __of_table_##name \
  859. __used __section(__##table##_of_table) \
  860. = { .compatible = compat, \
  861. .data = (fn == (fn_type)NULL) ? fn : fn }
  862. #else
  863. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  864. static const struct of_device_id __of_table_##name \
  865. __attribute__((unused)) \
  866. = { .compatible = compat, \
  867. .data = (fn == (fn_type)NULL) ? fn : fn }
  868. #endif
  869. typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
  870. typedef int (*of_init_fn_1_ret)(struct device_node *);
  871. typedef void (*of_init_fn_1)(struct device_node *);
  872. #define OF_DECLARE_1(table, name, compat, fn) \
  873. _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
  874. #define OF_DECLARE_1_RET(table, name, compat, fn) \
  875. _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
  876. #define OF_DECLARE_2(table, name, compat, fn) \
  877. _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
  878. /**
  879. * struct of_changeset_entry - Holds a changeset entry
  880. *
  881. * @node: list_head for the log list
  882. * @action: notifier action
  883. * @np: pointer to the device node affected
  884. * @prop: pointer to the property affected
  885. * @old_prop: hold a pointer to the original property
  886. *
  887. * Every modification of the device tree during a changeset
  888. * is held in a list of of_changeset_entry structures.
  889. * That way we can recover from a partial application, or we can
  890. * revert the changeset
  891. */
  892. struct of_changeset_entry {
  893. struct list_head node;
  894. unsigned long action;
  895. struct device_node *np;
  896. struct property *prop;
  897. struct property *old_prop;
  898. };
  899. /**
  900. * struct of_changeset - changeset tracker structure
  901. *
  902. * @entries: list_head for the changeset entries
  903. *
  904. * changesets are a convenient way to apply bulk changes to the
  905. * live tree. In case of an error, changes are rolled-back.
  906. * changesets live on after initial application, and if not
  907. * destroyed after use, they can be reverted in one single call.
  908. */
  909. struct of_changeset {
  910. struct list_head entries;
  911. };
  912. enum of_reconfig_change {
  913. OF_RECONFIG_NO_CHANGE = 0,
  914. OF_RECONFIG_CHANGE_ADD,
  915. OF_RECONFIG_CHANGE_REMOVE,
  916. };
  917. #ifdef CONFIG_OF_DYNAMIC
  918. extern int of_reconfig_notifier_register(struct notifier_block *);
  919. extern int of_reconfig_notifier_unregister(struct notifier_block *);
  920. extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
  921. extern int of_reconfig_get_state_change(unsigned long action,
  922. struct of_reconfig_data *arg);
  923. extern void of_changeset_init(struct of_changeset *ocs);
  924. extern void of_changeset_destroy(struct of_changeset *ocs);
  925. extern int of_changeset_apply(struct of_changeset *ocs);
  926. extern int of_changeset_revert(struct of_changeset *ocs);
  927. extern int of_changeset_action(struct of_changeset *ocs,
  928. unsigned long action, struct device_node *np,
  929. struct property *prop);
  930. static inline int of_changeset_attach_node(struct of_changeset *ocs,
  931. struct device_node *np)
  932. {
  933. return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
  934. }
  935. static inline int of_changeset_detach_node(struct of_changeset *ocs,
  936. struct device_node *np)
  937. {
  938. return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
  939. }
  940. static inline int of_changeset_add_property(struct of_changeset *ocs,
  941. struct device_node *np, struct property *prop)
  942. {
  943. return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
  944. }
  945. static inline int of_changeset_remove_property(struct of_changeset *ocs,
  946. struct device_node *np, struct property *prop)
  947. {
  948. return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  949. }
  950. static inline int of_changeset_update_property(struct of_changeset *ocs,
  951. struct device_node *np, struct property *prop)
  952. {
  953. return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
  954. }
  955. #else /* CONFIG_OF_DYNAMIC */
  956. static inline int of_reconfig_notifier_register(struct notifier_block *nb)
  957. {
  958. return -EINVAL;
  959. }
  960. static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
  961. {
  962. return -EINVAL;
  963. }
  964. static inline int of_reconfig_notify(unsigned long action,
  965. struct of_reconfig_data *arg)
  966. {
  967. return -EINVAL;
  968. }
  969. static inline int of_reconfig_get_state_change(unsigned long action,
  970. struct of_reconfig_data *arg)
  971. {
  972. return -EINVAL;
  973. }
  974. #endif /* CONFIG_OF_DYNAMIC */
  975. /* CONFIG_OF_RESOLVE api */
  976. extern int of_resolve_phandles(struct device_node *tree);
  977. /**
  978. * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
  979. * @np: Pointer to the given device_node
  980. *
  981. * return true if present false otherwise
  982. */
  983. static inline bool of_device_is_system_power_controller(const struct device_node *np)
  984. {
  985. return of_property_read_bool(np, "system-power-controller");
  986. }
  987. /**
  988. * Overlay support
  989. */
  990. #ifdef CONFIG_OF_OVERLAY
  991. /* ID based overlays; the API for external users */
  992. int of_overlay_create(struct device_node *tree);
  993. int of_overlay_destroy(int id);
  994. int of_overlay_destroy_all(void);
  995. #else
  996. static inline int of_overlay_create(struct device_node *tree)
  997. {
  998. return -ENOTSUPP;
  999. }
  1000. static inline int of_overlay_destroy(int id)
  1001. {
  1002. return -ENOTSUPP;
  1003. }
  1004. static inline int of_overlay_destroy_all(void)
  1005. {
  1006. return -ENOTSUPP;
  1007. }
  1008. #endif
  1009. #endif /* _LINUX_OF_H */