of.h 33 KB

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