of.h 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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. #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
  205. #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
  206. static inline const char *of_node_full_name(const struct device_node *np)
  207. {
  208. return np ? np->full_name : "<no-node>";
  209. }
  210. #define for_each_of_allnodes_from(from, dn) \
  211. for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
  212. #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
  213. extern struct device_node *of_find_node_by_name(struct device_node *from,
  214. const char *name);
  215. extern struct device_node *of_find_node_by_type(struct device_node *from,
  216. const char *type);
  217. extern struct device_node *of_find_compatible_node(struct device_node *from,
  218. const char *type, const char *compat);
  219. extern struct device_node *of_find_matching_node_and_match(
  220. struct device_node *from,
  221. const struct of_device_id *matches,
  222. const struct of_device_id **match);
  223. extern struct device_node *of_find_node_opts_by_path(const char *path,
  224. const char **opts);
  225. static inline struct device_node *of_find_node_by_path(const char *path)
  226. {
  227. return of_find_node_opts_by_path(path, NULL);
  228. }
  229. extern struct device_node *of_find_node_by_phandle(phandle handle);
  230. extern struct device_node *of_get_parent(const struct device_node *node);
  231. extern struct device_node *of_get_next_parent(struct device_node *node);
  232. extern struct device_node *of_get_next_child(const struct device_node *node,
  233. struct device_node *prev);
  234. extern struct device_node *of_get_next_available_child(
  235. const struct device_node *node, struct device_node *prev);
  236. extern struct device_node *of_get_child_by_name(const struct device_node *node,
  237. const char *name);
  238. /* cache lookup */
  239. extern struct device_node *of_find_next_cache_node(const struct device_node *);
  240. extern struct device_node *of_find_node_with_property(
  241. struct device_node *from, const char *prop_name);
  242. extern struct property *of_find_property(const struct device_node *np,
  243. const char *name,
  244. int *lenp);
  245. extern int of_property_count_elems_of_size(const struct device_node *np,
  246. const char *propname, int elem_size);
  247. extern int of_property_read_u32_index(const struct device_node *np,
  248. const char *propname,
  249. u32 index, u32 *out_value);
  250. extern int of_property_read_variable_u8_array(const struct device_node *np,
  251. const char *propname, u8 *out_values,
  252. size_t sz_min, size_t sz_max);
  253. extern int of_property_read_variable_u16_array(const struct device_node *np,
  254. const char *propname, u16 *out_values,
  255. size_t sz_min, size_t sz_max);
  256. extern int of_property_read_variable_u32_array(const struct device_node *np,
  257. const char *propname,
  258. u32 *out_values,
  259. size_t sz_min,
  260. size_t sz_max);
  261. extern int of_property_read_u64(const struct device_node *np,
  262. const char *propname, u64 *out_value);
  263. extern int of_property_read_variable_u64_array(const struct device_node *np,
  264. const char *propname,
  265. u64 *out_values,
  266. size_t sz_min,
  267. size_t sz_max);
  268. extern int of_property_read_string(const struct device_node *np,
  269. const char *propname,
  270. const char **out_string);
  271. extern int of_property_match_string(const struct device_node *np,
  272. const char *propname,
  273. const char *string);
  274. extern int of_property_read_string_helper(const struct device_node *np,
  275. const char *propname,
  276. const char **out_strs, size_t sz, int index);
  277. extern int of_device_is_compatible(const struct device_node *device,
  278. const char *);
  279. extern int of_device_compatible_match(struct device_node *device,
  280. const char *const *compat);
  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. * of_property_read_u8_array - Find and read an array of u8 from a property.
  334. *
  335. * @np: device node from which the property value is to be read.
  336. * @propname: name of the property to be searched.
  337. * @out_values: pointer to return value, modified only if return value is 0.
  338. * @sz: number of array elements to read
  339. *
  340. * Search for a property in a device node and read 8-bit value(s) from
  341. * it. Returns 0 on success, -EINVAL if the property does not exist,
  342. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  343. * property data isn't large enough.
  344. *
  345. * dts entry of array should be like:
  346. * property = /bits/ 8 <0x50 0x60 0x70>;
  347. *
  348. * The out_values is modified only if a valid u8 value can be decoded.
  349. */
  350. static inline int of_property_read_u8_array(const struct device_node *np,
  351. const char *propname,
  352. u8 *out_values, size_t sz)
  353. {
  354. int ret = of_property_read_variable_u8_array(np, propname, out_values,
  355. sz, 0);
  356. if (ret >= 0)
  357. return 0;
  358. else
  359. return ret;
  360. }
  361. /**
  362. * of_property_read_u16_array - Find and read an array of u16 from a property.
  363. *
  364. * @np: device node from which the property value is to be read.
  365. * @propname: name of the property to be searched.
  366. * @out_values: pointer to return value, modified only if return value is 0.
  367. * @sz: number of array elements to read
  368. *
  369. * Search for a property in a device node and read 16-bit value(s) from
  370. * it. Returns 0 on success, -EINVAL if the property does not exist,
  371. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  372. * property data isn't large enough.
  373. *
  374. * dts entry of array should be like:
  375. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  376. *
  377. * The out_values is modified only if a valid u16 value can be decoded.
  378. */
  379. static inline int of_property_read_u16_array(const struct device_node *np,
  380. const char *propname,
  381. u16 *out_values, size_t sz)
  382. {
  383. int ret = of_property_read_variable_u16_array(np, propname, out_values,
  384. sz, 0);
  385. if (ret >= 0)
  386. return 0;
  387. else
  388. return ret;
  389. }
  390. /**
  391. * of_property_read_u32_array - Find and read an array of 32 bit integers
  392. * from a property.
  393. *
  394. * @np: device node from which the property value is to be read.
  395. * @propname: name of the property to be searched.
  396. * @out_values: pointer to return value, modified only if return value is 0.
  397. * @sz: number of array elements to read
  398. *
  399. * Search for a property in a device node and read 32-bit value(s) from
  400. * it. Returns 0 on success, -EINVAL if the property does not exist,
  401. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  402. * property data isn't large enough.
  403. *
  404. * The out_values is modified only if a valid u32 value can be decoded.
  405. */
  406. static inline int of_property_read_u32_array(const struct device_node *np,
  407. const char *propname,
  408. u32 *out_values, size_t sz)
  409. {
  410. int ret = of_property_read_variable_u32_array(np, propname, out_values,
  411. sz, 0);
  412. if (ret >= 0)
  413. return 0;
  414. else
  415. return ret;
  416. }
  417. /**
  418. * of_property_read_u64_array - Find and read an array of 64 bit integers
  419. * from a property.
  420. *
  421. * @np: device node from which the property value is to be read.
  422. * @propname: name of the property to be searched.
  423. * @out_values: pointer to return value, modified only if return value is 0.
  424. * @sz: number of array elements to read
  425. *
  426. * Search for a property in a device node and read 64-bit value(s) from
  427. * it. Returns 0 on success, -EINVAL if the property does not exist,
  428. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  429. * property data isn't large enough.
  430. *
  431. * The out_values is modified only if a valid u64 value can be decoded.
  432. */
  433. static inline int of_property_read_u64_array(const struct device_node *np,
  434. const char *propname,
  435. u64 *out_values, size_t sz)
  436. {
  437. int ret = of_property_read_variable_u64_array(np, propname, out_values,
  438. sz, 0);
  439. if (ret >= 0)
  440. return 0;
  441. else
  442. return ret;
  443. }
  444. /*
  445. * struct property *prop;
  446. * const __be32 *p;
  447. * u32 u;
  448. *
  449. * of_property_for_each_u32(np, "propname", prop, p, u)
  450. * printk("U32 value: %x\n", u);
  451. */
  452. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  453. u32 *pu);
  454. /*
  455. * struct property *prop;
  456. * const char *s;
  457. *
  458. * of_property_for_each_string(np, "propname", prop, s)
  459. * printk("String value: %s\n", s);
  460. */
  461. const char *of_prop_next_string(struct property *prop, const char *cur);
  462. bool of_console_check(struct device_node *dn, char *name, int index);
  463. #else /* CONFIG_OF */
  464. static inline void of_core_init(void)
  465. {
  466. }
  467. static inline bool is_of_node(struct fwnode_handle *fwnode)
  468. {
  469. return false;
  470. }
  471. static inline struct device_node *to_of_node(struct fwnode_handle *fwnode)
  472. {
  473. return NULL;
  474. }
  475. static inline const char* of_node_full_name(const struct device_node *np)
  476. {
  477. return "<no-node>";
  478. }
  479. static inline struct device_node *of_find_node_by_name(struct device_node *from,
  480. const char *name)
  481. {
  482. return NULL;
  483. }
  484. static inline struct device_node *of_find_node_by_type(struct device_node *from,
  485. const char *type)
  486. {
  487. return NULL;
  488. }
  489. static inline struct device_node *of_find_matching_node_and_match(
  490. struct device_node *from,
  491. const struct of_device_id *matches,
  492. const struct of_device_id **match)
  493. {
  494. return NULL;
  495. }
  496. static inline struct device_node *of_find_node_by_path(const char *path)
  497. {
  498. return NULL;
  499. }
  500. static inline struct device_node *of_find_node_opts_by_path(const char *path,
  501. const char **opts)
  502. {
  503. return NULL;
  504. }
  505. static inline struct device_node *of_find_node_by_phandle(phandle handle)
  506. {
  507. return NULL;
  508. }
  509. static inline struct device_node *of_get_parent(const struct device_node *node)
  510. {
  511. return NULL;
  512. }
  513. static inline struct device_node *of_get_next_child(
  514. const struct device_node *node, struct device_node *prev)
  515. {
  516. return NULL;
  517. }
  518. static inline struct device_node *of_get_next_available_child(
  519. const struct device_node *node, struct device_node *prev)
  520. {
  521. return NULL;
  522. }
  523. static inline struct device_node *of_find_node_with_property(
  524. struct device_node *from, const char *prop_name)
  525. {
  526. return NULL;
  527. }
  528. static inline bool of_have_populated_dt(void)
  529. {
  530. return false;
  531. }
  532. static inline struct device_node *of_get_child_by_name(
  533. const struct device_node *node,
  534. const char *name)
  535. {
  536. return NULL;
  537. }
  538. static inline int of_device_is_compatible(const struct device_node *device,
  539. const char *name)
  540. {
  541. return 0;
  542. }
  543. static inline bool of_device_is_available(const struct device_node *device)
  544. {
  545. return false;
  546. }
  547. static inline bool of_device_is_big_endian(const struct device_node *device)
  548. {
  549. return false;
  550. }
  551. static inline struct property *of_find_property(const struct device_node *np,
  552. const char *name,
  553. int *lenp)
  554. {
  555. return NULL;
  556. }
  557. static inline struct device_node *of_find_compatible_node(
  558. struct device_node *from,
  559. const char *type,
  560. const char *compat)
  561. {
  562. return NULL;
  563. }
  564. static inline int of_property_count_elems_of_size(const struct device_node *np,
  565. const char *propname, int elem_size)
  566. {
  567. return -ENOSYS;
  568. }
  569. static inline int of_property_read_u32_index(const struct device_node *np,
  570. const char *propname, u32 index, u32 *out_value)
  571. {
  572. return -ENOSYS;
  573. }
  574. static inline int of_property_read_u8_array(const struct device_node *np,
  575. const char *propname, u8 *out_values, size_t sz)
  576. {
  577. return -ENOSYS;
  578. }
  579. static inline int of_property_read_u16_array(const struct device_node *np,
  580. const char *propname, u16 *out_values, size_t sz)
  581. {
  582. return -ENOSYS;
  583. }
  584. static inline int of_property_read_u32_array(const struct device_node *np,
  585. const char *propname,
  586. u32 *out_values, size_t sz)
  587. {
  588. return -ENOSYS;
  589. }
  590. static inline int of_property_read_u64_array(const struct device_node *np,
  591. const char *propname,
  592. u64 *out_values, size_t sz)
  593. {
  594. return -ENOSYS;
  595. }
  596. static inline int of_property_read_string(const struct device_node *np,
  597. const char *propname,
  598. const char **out_string)
  599. {
  600. return -ENOSYS;
  601. }
  602. static inline int of_property_read_string_helper(const struct device_node *np,
  603. const char *propname,
  604. const char **out_strs, size_t sz, int index)
  605. {
  606. return -ENOSYS;
  607. }
  608. static inline const void *of_get_property(const struct device_node *node,
  609. const char *name,
  610. int *lenp)
  611. {
  612. return NULL;
  613. }
  614. static inline struct device_node *of_get_cpu_node(int cpu,
  615. unsigned int *thread)
  616. {
  617. return NULL;
  618. }
  619. static inline int of_property_read_u64(const struct device_node *np,
  620. const char *propname, u64 *out_value)
  621. {
  622. return -ENOSYS;
  623. }
  624. static inline int of_property_match_string(const struct device_node *np,
  625. const char *propname,
  626. const char *string)
  627. {
  628. return -ENOSYS;
  629. }
  630. static inline struct device_node *of_parse_phandle(const struct device_node *np,
  631. const char *phandle_name,
  632. int index)
  633. {
  634. return NULL;
  635. }
  636. static inline int of_parse_phandle_with_args(const struct device_node *np,
  637. const char *list_name,
  638. const char *cells_name,
  639. int index,
  640. struct of_phandle_args *out_args)
  641. {
  642. return -ENOSYS;
  643. }
  644. static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
  645. const char *list_name, int cells_count, int index,
  646. struct of_phandle_args *out_args)
  647. {
  648. return -ENOSYS;
  649. }
  650. static inline int of_count_phandle_with_args(struct device_node *np,
  651. const char *list_name,
  652. const char *cells_name)
  653. {
  654. return -ENOSYS;
  655. }
  656. static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
  657. const struct device_node *np,
  658. const char *list_name,
  659. const char *cells_name,
  660. int cell_count)
  661. {
  662. return -ENOSYS;
  663. }
  664. static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
  665. {
  666. return -ENOSYS;
  667. }
  668. static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
  669. uint32_t *args,
  670. int size)
  671. {
  672. return 0;
  673. }
  674. static inline int of_alias_get_id(struct device_node *np, const char *stem)
  675. {
  676. return -ENOSYS;
  677. }
  678. static inline int of_alias_get_highest_id(const char *stem)
  679. {
  680. return -ENOSYS;
  681. }
  682. static inline int of_machine_is_compatible(const char *compat)
  683. {
  684. return 0;
  685. }
  686. static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
  687. {
  688. return false;
  689. }
  690. static inline const __be32 *of_prop_next_u32(struct property *prop,
  691. const __be32 *cur, u32 *pu)
  692. {
  693. return NULL;
  694. }
  695. static inline const char *of_prop_next_string(struct property *prop,
  696. const char *cur)
  697. {
  698. return NULL;
  699. }
  700. static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
  701. {
  702. return 0;
  703. }
  704. static inline int of_node_test_and_set_flag(struct device_node *n,
  705. unsigned long flag)
  706. {
  707. return 0;
  708. }
  709. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  710. {
  711. }
  712. static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
  713. {
  714. }
  715. static inline int of_property_check_flag(struct property *p, unsigned long flag)
  716. {
  717. return 0;
  718. }
  719. static inline void of_property_set_flag(struct property *p, unsigned long flag)
  720. {
  721. }
  722. static inline void of_property_clear_flag(struct property *p, unsigned long flag)
  723. {
  724. }
  725. #define of_match_ptr(_ptr) NULL
  726. #define of_match_node(_matches, _node) NULL
  727. #endif /* CONFIG_OF */
  728. /* Default string compare functions, Allow arch asm/prom.h to override */
  729. #if !defined(of_compat_cmp)
  730. #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
  731. #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
  732. #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
  733. #endif
  734. #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
  735. extern int of_node_to_nid(struct device_node *np);
  736. #else
  737. static inline int of_node_to_nid(struct device_node *device)
  738. {
  739. return NUMA_NO_NODE;
  740. }
  741. #endif
  742. #ifdef CONFIG_OF_NUMA
  743. extern int of_numa_init(void);
  744. #else
  745. static inline int of_numa_init(void)
  746. {
  747. return -ENOSYS;
  748. }
  749. #endif
  750. static inline struct device_node *of_find_matching_node(
  751. struct device_node *from,
  752. const struct of_device_id *matches)
  753. {
  754. return of_find_matching_node_and_match(from, matches, NULL);
  755. }
  756. /**
  757. * of_property_count_u8_elems - Count the number of u8 elements in a property
  758. *
  759. * @np: device node from which the property value is to be read.
  760. * @propname: name of the property to be searched.
  761. *
  762. * Search for a property in a device node and count the number of u8 elements
  763. * in it. Returns number of elements on sucess, -EINVAL if the property does
  764. * not exist or its length does not match a multiple of u8 and -ENODATA if the
  765. * property does not have a value.
  766. */
  767. static inline int of_property_count_u8_elems(const struct device_node *np,
  768. const char *propname)
  769. {
  770. return of_property_count_elems_of_size(np, propname, sizeof(u8));
  771. }
  772. /**
  773. * of_property_count_u16_elems - Count the number of u16 elements in a property
  774. *
  775. * @np: device node from which the property value is to be read.
  776. * @propname: name of the property to be searched.
  777. *
  778. * Search for a property in a device node and count the number of u16 elements
  779. * in it. Returns number of elements on sucess, -EINVAL if the property does
  780. * not exist or its length does not match a multiple of u16 and -ENODATA if the
  781. * property does not have a value.
  782. */
  783. static inline int of_property_count_u16_elems(const struct device_node *np,
  784. const char *propname)
  785. {
  786. return of_property_count_elems_of_size(np, propname, sizeof(u16));
  787. }
  788. /**
  789. * of_property_count_u32_elems - Count the number of u32 elements in a property
  790. *
  791. * @np: device node from which the property value is to be read.
  792. * @propname: name of the property to be searched.
  793. *
  794. * Search for a property in a device node and count the number of u32 elements
  795. * in it. Returns number of elements on sucess, -EINVAL if the property does
  796. * not exist or its length does not match a multiple of u32 and -ENODATA if the
  797. * property does not have a value.
  798. */
  799. static inline int of_property_count_u32_elems(const struct device_node *np,
  800. const char *propname)
  801. {
  802. return of_property_count_elems_of_size(np, propname, sizeof(u32));
  803. }
  804. /**
  805. * of_property_count_u64_elems - Count the number of u64 elements in a property
  806. *
  807. * @np: device node from which the property value is to be read.
  808. * @propname: name of the property to be searched.
  809. *
  810. * Search for a property in a device node and count the number of u64 elements
  811. * in it. Returns number of elements on sucess, -EINVAL if the property does
  812. * not exist or its length does not match a multiple of u64 and -ENODATA if the
  813. * property does not have a value.
  814. */
  815. static inline int of_property_count_u64_elems(const struct device_node *np,
  816. const char *propname)
  817. {
  818. return of_property_count_elems_of_size(np, propname, sizeof(u64));
  819. }
  820. /**
  821. * of_property_read_string_array() - Read an array of strings from a multiple
  822. * strings property.
  823. * @np: device node from which the property value is to be read.
  824. * @propname: name of the property to be searched.
  825. * @out_strs: output array of string pointers.
  826. * @sz: number of array elements to read.
  827. *
  828. * Search for a property in a device tree node and retrieve a list of
  829. * terminated string values (pointer to data, not a copy) in that property.
  830. *
  831. * If @out_strs is NULL, the number of strings in the property is returned.
  832. */
  833. static inline int of_property_read_string_array(const struct device_node *np,
  834. const char *propname, const char **out_strs,
  835. size_t sz)
  836. {
  837. return of_property_read_string_helper(np, propname, out_strs, sz, 0);
  838. }
  839. /**
  840. * of_property_count_strings() - Find and return the number of strings from a
  841. * multiple strings property.
  842. * @np: device node from which the property value is to be read.
  843. * @propname: name of the property to be searched.
  844. *
  845. * Search for a property in a device tree node and retrieve the number of null
  846. * terminated string contain in it. Returns the number of strings on
  847. * success, -EINVAL if the property does not exist, -ENODATA if property
  848. * does not have a value, and -EILSEQ if the string is not null-terminated
  849. * within the length of the property data.
  850. */
  851. static inline int of_property_count_strings(const struct device_node *np,
  852. const char *propname)
  853. {
  854. return of_property_read_string_helper(np, propname, NULL, 0, 0);
  855. }
  856. /**
  857. * of_property_read_string_index() - Find and read a string from a multiple
  858. * strings property.
  859. * @np: device node from which the property value is to be read.
  860. * @propname: name of the property to be searched.
  861. * @index: index of the string in the list of strings
  862. * @out_string: pointer to null terminated return string, modified only if
  863. * return value is 0.
  864. *
  865. * Search for a property in a device tree node and retrieve a null
  866. * terminated string value (pointer to data, not a copy) in the list of strings
  867. * contained in that property.
  868. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  869. * property does not have a value, and -EILSEQ if the string is not
  870. * null-terminated within the length of the property data.
  871. *
  872. * The out_string pointer is modified only if a valid string can be decoded.
  873. */
  874. static inline int of_property_read_string_index(const struct device_node *np,
  875. const char *propname,
  876. int index, const char **output)
  877. {
  878. int rc = of_property_read_string_helper(np, propname, output, 1, index);
  879. return rc < 0 ? rc : 0;
  880. }
  881. /**
  882. * of_property_read_bool - Findfrom a property
  883. * @np: device node from which the property value is to be read.
  884. * @propname: name of the property to be searched.
  885. *
  886. * Search for a property in a device node.
  887. * Returns true if the property exists false otherwise.
  888. */
  889. static inline bool of_property_read_bool(const struct device_node *np,
  890. const char *propname)
  891. {
  892. struct property *prop = of_find_property(np, propname, NULL);
  893. return prop ? true : false;
  894. }
  895. static inline int of_property_read_u8(const struct device_node *np,
  896. const char *propname,
  897. u8 *out_value)
  898. {
  899. return of_property_read_u8_array(np, propname, out_value, 1);
  900. }
  901. static inline int of_property_read_u16(const struct device_node *np,
  902. const char *propname,
  903. u16 *out_value)
  904. {
  905. return of_property_read_u16_array(np, propname, out_value, 1);
  906. }
  907. static inline int of_property_read_u32(const struct device_node *np,
  908. const char *propname,
  909. u32 *out_value)
  910. {
  911. return of_property_read_u32_array(np, propname, out_value, 1);
  912. }
  913. static inline int of_property_read_s32(const struct device_node *np,
  914. const char *propname,
  915. s32 *out_value)
  916. {
  917. return of_property_read_u32(np, propname, (u32*) out_value);
  918. }
  919. #define of_for_each_phandle(it, err, np, ln, cn, cc) \
  920. for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
  921. err = of_phandle_iterator_next(it); \
  922. err == 0; \
  923. err = of_phandle_iterator_next(it))
  924. #define of_property_for_each_u32(np, propname, prop, p, u) \
  925. for (prop = of_find_property(np, propname, NULL), \
  926. p = of_prop_next_u32(prop, NULL, &u); \
  927. p; \
  928. p = of_prop_next_u32(prop, p, &u))
  929. #define of_property_for_each_string(np, propname, prop, s) \
  930. for (prop = of_find_property(np, propname, NULL), \
  931. s = of_prop_next_string(prop, NULL); \
  932. s; \
  933. s = of_prop_next_string(prop, s))
  934. #define for_each_node_by_name(dn, name) \
  935. for (dn = of_find_node_by_name(NULL, name); dn; \
  936. dn = of_find_node_by_name(dn, name))
  937. #define for_each_node_by_type(dn, type) \
  938. for (dn = of_find_node_by_type(NULL, type); dn; \
  939. dn = of_find_node_by_type(dn, type))
  940. #define for_each_compatible_node(dn, type, compatible) \
  941. for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
  942. dn = of_find_compatible_node(dn, type, compatible))
  943. #define for_each_matching_node(dn, matches) \
  944. for (dn = of_find_matching_node(NULL, matches); dn; \
  945. dn = of_find_matching_node(dn, matches))
  946. #define for_each_matching_node_and_match(dn, matches, match) \
  947. for (dn = of_find_matching_node_and_match(NULL, matches, match); \
  948. dn; dn = of_find_matching_node_and_match(dn, matches, match))
  949. #define for_each_child_of_node(parent, child) \
  950. for (child = of_get_next_child(parent, NULL); child != NULL; \
  951. child = of_get_next_child(parent, child))
  952. #define for_each_available_child_of_node(parent, child) \
  953. for (child = of_get_next_available_child(parent, NULL); child != NULL; \
  954. child = of_get_next_available_child(parent, child))
  955. #define for_each_node_with_property(dn, prop_name) \
  956. for (dn = of_find_node_with_property(NULL, prop_name); dn; \
  957. dn = of_find_node_with_property(dn, prop_name))
  958. static inline int of_get_child_count(const struct device_node *np)
  959. {
  960. struct device_node *child;
  961. int num = 0;
  962. for_each_child_of_node(np, child)
  963. num++;
  964. return num;
  965. }
  966. static inline int of_get_available_child_count(const struct device_node *np)
  967. {
  968. struct device_node *child;
  969. int num = 0;
  970. for_each_available_child_of_node(np, child)
  971. num++;
  972. return num;
  973. }
  974. #if defined(CONFIG_OF) && !defined(MODULE)
  975. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  976. static const struct of_device_id __of_table_##name \
  977. __used __section(__##table##_of_table) \
  978. = { .compatible = compat, \
  979. .data = (fn == (fn_type)NULL) ? fn : fn }
  980. #else
  981. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  982. static const struct of_device_id __of_table_##name \
  983. __attribute__((unused)) \
  984. = { .compatible = compat, \
  985. .data = (fn == (fn_type)NULL) ? fn : fn }
  986. #endif
  987. typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
  988. typedef int (*of_init_fn_1_ret)(struct device_node *);
  989. typedef void (*of_init_fn_1)(struct device_node *);
  990. #define OF_DECLARE_1(table, name, compat, fn) \
  991. _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
  992. #define OF_DECLARE_1_RET(table, name, compat, fn) \
  993. _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
  994. #define OF_DECLARE_2(table, name, compat, fn) \
  995. _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
  996. /**
  997. * struct of_changeset_entry - Holds a changeset entry
  998. *
  999. * @node: list_head for the log list
  1000. * @action: notifier action
  1001. * @np: pointer to the device node affected
  1002. * @prop: pointer to the property affected
  1003. * @old_prop: hold a pointer to the original property
  1004. *
  1005. * Every modification of the device tree during a changeset
  1006. * is held in a list of of_changeset_entry structures.
  1007. * That way we can recover from a partial application, or we can
  1008. * revert the changeset
  1009. */
  1010. struct of_changeset_entry {
  1011. struct list_head node;
  1012. unsigned long action;
  1013. struct device_node *np;
  1014. struct property *prop;
  1015. struct property *old_prop;
  1016. };
  1017. /**
  1018. * struct of_changeset - changeset tracker structure
  1019. *
  1020. * @entries: list_head for the changeset entries
  1021. *
  1022. * changesets are a convenient way to apply bulk changes to the
  1023. * live tree. In case of an error, changes are rolled-back.
  1024. * changesets live on after initial application, and if not
  1025. * destroyed after use, they can be reverted in one single call.
  1026. */
  1027. struct of_changeset {
  1028. struct list_head entries;
  1029. };
  1030. enum of_reconfig_change {
  1031. OF_RECONFIG_NO_CHANGE = 0,
  1032. OF_RECONFIG_CHANGE_ADD,
  1033. OF_RECONFIG_CHANGE_REMOVE,
  1034. };
  1035. #ifdef CONFIG_OF_DYNAMIC
  1036. extern int of_reconfig_notifier_register(struct notifier_block *);
  1037. extern int of_reconfig_notifier_unregister(struct notifier_block *);
  1038. extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
  1039. extern int of_reconfig_get_state_change(unsigned long action,
  1040. struct of_reconfig_data *arg);
  1041. extern void of_changeset_init(struct of_changeset *ocs);
  1042. extern void of_changeset_destroy(struct of_changeset *ocs);
  1043. extern int of_changeset_apply(struct of_changeset *ocs);
  1044. extern int of_changeset_revert(struct of_changeset *ocs);
  1045. extern int of_changeset_action(struct of_changeset *ocs,
  1046. unsigned long action, struct device_node *np,
  1047. struct property *prop);
  1048. static inline int of_changeset_attach_node(struct of_changeset *ocs,
  1049. struct device_node *np)
  1050. {
  1051. return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
  1052. }
  1053. static inline int of_changeset_detach_node(struct of_changeset *ocs,
  1054. struct device_node *np)
  1055. {
  1056. return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
  1057. }
  1058. static inline int of_changeset_add_property(struct of_changeset *ocs,
  1059. struct device_node *np, struct property *prop)
  1060. {
  1061. return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
  1062. }
  1063. static inline int of_changeset_remove_property(struct of_changeset *ocs,
  1064. struct device_node *np, struct property *prop)
  1065. {
  1066. return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1067. }
  1068. static inline int of_changeset_update_property(struct of_changeset *ocs,
  1069. struct device_node *np, struct property *prop)
  1070. {
  1071. return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
  1072. }
  1073. #else /* CONFIG_OF_DYNAMIC */
  1074. static inline int of_reconfig_notifier_register(struct notifier_block *nb)
  1075. {
  1076. return -EINVAL;
  1077. }
  1078. static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1079. {
  1080. return -EINVAL;
  1081. }
  1082. static inline int of_reconfig_notify(unsigned long action,
  1083. struct of_reconfig_data *arg)
  1084. {
  1085. return -EINVAL;
  1086. }
  1087. static inline int of_reconfig_get_state_change(unsigned long action,
  1088. struct of_reconfig_data *arg)
  1089. {
  1090. return -EINVAL;
  1091. }
  1092. #endif /* CONFIG_OF_DYNAMIC */
  1093. /* CONFIG_OF_RESOLVE api */
  1094. extern int of_resolve_phandles(struct device_node *tree);
  1095. /**
  1096. * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
  1097. * @np: Pointer to the given device_node
  1098. *
  1099. * return true if present false otherwise
  1100. */
  1101. static inline bool of_device_is_system_power_controller(const struct device_node *np)
  1102. {
  1103. return of_property_read_bool(np, "system-power-controller");
  1104. }
  1105. /**
  1106. * Overlay support
  1107. */
  1108. #ifdef CONFIG_OF_OVERLAY
  1109. /* ID based overlays; the API for external users */
  1110. int of_overlay_create(struct device_node *tree);
  1111. int of_overlay_destroy(int id);
  1112. int of_overlay_destroy_all(void);
  1113. #else
  1114. static inline int of_overlay_create(struct device_node *tree)
  1115. {
  1116. return -ENOTSUPP;
  1117. }
  1118. static inline int of_overlay_destroy(int id)
  1119. {
  1120. return -ENOTSUPP;
  1121. }
  1122. static inline int of_overlay_destroy_all(void)
  1123. {
  1124. return -ENOTSUPP;
  1125. }
  1126. #endif
  1127. #endif /* _LINUX_OF_H */