of.h 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef _LINUX_OF_H
  3. #define _LINUX_OF_H
  4. /*
  5. * Definitions for talking to the Open Firmware PROM on
  6. * Power Macintosh and other computers.
  7. *
  8. * Copyright (C) 1996-2005 Paul Mackerras.
  9. *
  10. * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
  11. * Updates for SPARC64 by David S. Miller
  12. * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/bitops.h>
  16. #include <linux/errno.h>
  17. #include <linux/kobject.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/topology.h>
  21. #include <linux/notifier.h>
  22. #include <linux/property.h>
  23. #include <linux/list.h>
  24. #include <asm/byteorder.h>
  25. #include <asm/errno.h>
  26. typedef u32 phandle;
  27. typedef u32 ihandle;
  28. struct property {
  29. char *name;
  30. int length;
  31. void *value;
  32. struct property *next;
  33. #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
  34. unsigned long _flags;
  35. #endif
  36. #if defined(CONFIG_OF_PROMTREE)
  37. unsigned int unique_id;
  38. #endif
  39. #if defined(CONFIG_OF_KOBJ)
  40. struct bin_attribute attr;
  41. #endif
  42. };
  43. #if defined(CONFIG_SPARC)
  44. struct of_irq_controller;
  45. #endif
  46. struct device_node {
  47. const char *name;
  48. phandle phandle;
  49. const char *full_name;
  50. struct fwnode_handle fwnode;
  51. struct property *properties;
  52. struct property *deadprops; /* removed properties */
  53. struct device_node *parent;
  54. struct device_node *child;
  55. struct device_node *sibling;
  56. #if defined(CONFIG_OF_KOBJ)
  57. struct kobject kobj;
  58. #endif
  59. unsigned long _flags;
  60. void *data;
  61. #if defined(CONFIG_SPARC)
  62. unsigned int unique_id;
  63. struct of_irq_controller *irq_trans;
  64. #endif
  65. };
  66. #define MAX_PHANDLE_ARGS 16
  67. struct of_phandle_args {
  68. struct device_node *np;
  69. int args_count;
  70. uint32_t args[MAX_PHANDLE_ARGS];
  71. };
  72. struct of_phandle_iterator {
  73. /* Common iterator information */
  74. const char *cells_name;
  75. int cell_count;
  76. const struct device_node *parent;
  77. /* List size information */
  78. const __be32 *list_end;
  79. const __be32 *phandle_end;
  80. /* Current position state */
  81. const __be32 *cur;
  82. uint32_t cur_count;
  83. phandle phandle;
  84. struct device_node *node;
  85. };
  86. struct of_reconfig_data {
  87. struct device_node *dn;
  88. struct property *prop;
  89. struct property *old_prop;
  90. };
  91. /* initialize a node */
  92. extern struct kobj_type of_node_ktype;
  93. extern const struct fwnode_operations of_fwnode_ops;
  94. static inline void of_node_init(struct device_node *node)
  95. {
  96. #if defined(CONFIG_OF_KOBJ)
  97. kobject_init(&node->kobj, &of_node_ktype);
  98. #endif
  99. fwnode_init(&node->fwnode, &of_fwnode_ops);
  100. }
  101. #if defined(CONFIG_OF_KOBJ)
  102. #define of_node_kobj(n) (&(n)->kobj)
  103. #else
  104. #define of_node_kobj(n) NULL
  105. #endif
  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. /*
  124. * struct device_node flag descriptions
  125. * (need to be visible even when !CONFIG_OF)
  126. */
  127. #define OF_DYNAMIC 1 /* (and properties) allocated via kmalloc */
  128. #define OF_DETACHED 2 /* detached from the device tree */
  129. #define OF_POPULATED 3 /* device already created */
  130. #define OF_POPULATED_BUS 4 /* platform bus created for children */
  131. #define OF_OVERLAY 5 /* allocated for an overlay */
  132. #define OF_OVERLAY_FREE_CSET 6 /* in overlay cset being freed */
  133. #define OF_BAD_ADDR ((u64)-1)
  134. #ifdef CONFIG_OF
  135. void of_core_init(void);
  136. static inline bool is_of_node(const struct fwnode_handle *fwnode)
  137. {
  138. return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
  139. }
  140. #define to_of_node(__fwnode) \
  141. ({ \
  142. typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
  143. \
  144. is_of_node(__to_of_node_fwnode) ? \
  145. container_of(__to_of_node_fwnode, \
  146. struct device_node, fwnode) : \
  147. NULL; \
  148. })
  149. #define of_fwnode_handle(node) \
  150. ({ \
  151. typeof(node) __of_fwnode_handle_node = (node); \
  152. \
  153. __of_fwnode_handle_node ? \
  154. &__of_fwnode_handle_node->fwnode : NULL; \
  155. })
  156. static inline bool of_have_populated_dt(void)
  157. {
  158. return of_root != NULL;
  159. }
  160. static inline bool of_node_is_root(const struct device_node *node)
  161. {
  162. return node && (node->parent == NULL);
  163. }
  164. static inline int of_node_check_flag(const struct device_node *n, unsigned long flag)
  165. {
  166. return test_bit(flag, &n->_flags);
  167. }
  168. static inline int of_node_test_and_set_flag(struct device_node *n,
  169. unsigned long flag)
  170. {
  171. return test_and_set_bit(flag, &n->_flags);
  172. }
  173. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  174. {
  175. set_bit(flag, &n->_flags);
  176. }
  177. static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
  178. {
  179. clear_bit(flag, &n->_flags);
  180. }
  181. #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
  182. static inline int of_property_check_flag(const struct property *p, unsigned long flag)
  183. {
  184. return test_bit(flag, &p->_flags);
  185. }
  186. static inline void of_property_set_flag(struct property *p, unsigned long flag)
  187. {
  188. set_bit(flag, &p->_flags);
  189. }
  190. static inline void of_property_clear_flag(struct property *p, unsigned long flag)
  191. {
  192. clear_bit(flag, &p->_flags);
  193. }
  194. #endif
  195. extern struct device_node *__of_find_all_nodes(struct device_node *prev);
  196. extern struct device_node *of_find_all_nodes(struct device_node *prev);
  197. /*
  198. * OF address retrieval & translation
  199. */
  200. /* Helper to read a big number; size is in cells (not bytes) */
  201. static inline u64 of_read_number(const __be32 *cell, int size)
  202. {
  203. u64 r = 0;
  204. for (; size--; cell++)
  205. r = (r << 32) | be32_to_cpu(*cell);
  206. return r;
  207. }
  208. /* Like of_read_number, but we want an unsigned long result */
  209. static inline unsigned long of_read_ulong(const __be32 *cell, int size)
  210. {
  211. /* toss away upper bits if unsigned long is smaller than u64 */
  212. return of_read_number(cell, size);
  213. }
  214. #if defined(CONFIG_SPARC)
  215. #include <asm/prom.h>
  216. #endif
  217. #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
  218. #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
  219. extern bool of_node_name_eq(const struct device_node *np, const char *name);
  220. extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
  221. static inline const char *of_node_full_name(const struct device_node *np)
  222. {
  223. return np ? np->full_name : "<no-node>";
  224. }
  225. #define for_each_of_allnodes_from(from, dn) \
  226. for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
  227. #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
  228. extern struct device_node *of_find_node_by_name(struct device_node *from,
  229. const char *name);
  230. extern struct device_node *of_find_node_by_type(struct device_node *from,
  231. const char *type);
  232. extern struct device_node *of_find_compatible_node(struct device_node *from,
  233. const char *type, const char *compat);
  234. extern struct device_node *of_find_matching_node_and_match(
  235. struct device_node *from,
  236. const struct of_device_id *matches,
  237. const struct of_device_id **match);
  238. extern struct device_node *of_find_node_opts_by_path(const char *path,
  239. const char **opts);
  240. static inline struct device_node *of_find_node_by_path(const char *path)
  241. {
  242. return of_find_node_opts_by_path(path, NULL);
  243. }
  244. extern struct device_node *of_find_node_by_phandle(phandle handle);
  245. extern struct device_node *of_get_parent(const struct device_node *node);
  246. extern struct device_node *of_get_next_parent(struct device_node *node);
  247. extern struct device_node *of_get_next_child(const struct device_node *node,
  248. struct device_node *prev);
  249. extern struct device_node *of_get_next_available_child(
  250. const struct device_node *node, struct device_node *prev);
  251. extern struct device_node *of_get_compatible_child(const struct device_node *parent,
  252. const char *compatible);
  253. extern struct device_node *of_get_child_by_name(const struct device_node *node,
  254. const char *name);
  255. /* cache lookup */
  256. extern struct device_node *of_find_next_cache_node(const struct device_node *);
  257. extern int of_find_last_cache_level(unsigned int cpu);
  258. extern struct device_node *of_find_node_with_property(
  259. struct device_node *from, const char *prop_name);
  260. extern struct property *of_find_property(const struct device_node *np,
  261. const char *name,
  262. int *lenp);
  263. extern int of_property_count_elems_of_size(const struct device_node *np,
  264. const char *propname, int elem_size);
  265. extern int of_property_read_u32_index(const struct device_node *np,
  266. const char *propname,
  267. u32 index, u32 *out_value);
  268. extern int of_property_read_u64_index(const struct device_node *np,
  269. const char *propname,
  270. u32 index, u64 *out_value);
  271. extern int of_property_read_variable_u8_array(const struct device_node *np,
  272. const char *propname, u8 *out_values,
  273. size_t sz_min, size_t sz_max);
  274. extern int of_property_read_variable_u16_array(const struct device_node *np,
  275. const char *propname, u16 *out_values,
  276. size_t sz_min, size_t sz_max);
  277. extern int of_property_read_variable_u32_array(const struct device_node *np,
  278. const char *propname,
  279. u32 *out_values,
  280. size_t sz_min,
  281. size_t sz_max);
  282. extern int of_property_read_u64(const struct device_node *np,
  283. const char *propname, u64 *out_value);
  284. extern int of_property_read_variable_u64_array(const struct device_node *np,
  285. const char *propname,
  286. u64 *out_values,
  287. size_t sz_min,
  288. size_t sz_max);
  289. extern int of_property_read_string(const struct device_node *np,
  290. const char *propname,
  291. const char **out_string);
  292. extern int of_property_match_string(const struct device_node *np,
  293. const char *propname,
  294. const char *string);
  295. extern int of_property_read_string_helper(const struct device_node *np,
  296. const char *propname,
  297. const char **out_strs, size_t sz, int index);
  298. extern int of_device_is_compatible(const struct device_node *device,
  299. const char *);
  300. extern int of_device_compatible_match(const struct device_node *device,
  301. const char *const *compat);
  302. extern bool of_device_is_available(const struct device_node *device);
  303. extern bool of_device_is_big_endian(const struct device_node *device);
  304. extern const void *of_get_property(const struct device_node *node,
  305. const char *name,
  306. int *lenp);
  307. extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
  308. extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
  309. extern struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
  310. int index);
  311. extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread);
  312. #define for_each_property_of_node(dn, pp) \
  313. for (pp = dn->properties; pp != NULL; pp = pp->next)
  314. extern int of_n_addr_cells(struct device_node *np);
  315. extern int of_n_size_cells(struct device_node *np);
  316. extern const struct of_device_id *of_match_node(
  317. const struct of_device_id *matches, const struct device_node *node);
  318. extern int of_modalias_node(struct device_node *node, char *modalias, int len);
  319. extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
  320. extern int __of_parse_phandle_with_args(const struct device_node *np,
  321. const char *list_name, const char *cells_name, int cell_count,
  322. int index, struct of_phandle_args *out_args);
  323. extern int of_parse_phandle_with_args_map(const struct device_node *np,
  324. const char *list_name, const char *stem_name, int index,
  325. struct of_phandle_args *out_args);
  326. extern int of_count_phandle_with_args(const struct device_node *np,
  327. const char *list_name, const char *cells_name);
  328. /* phandle iterator functions */
  329. extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
  330. const struct device_node *np,
  331. const char *list_name,
  332. const char *cells_name,
  333. int cell_count);
  334. extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
  335. extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
  336. uint32_t *args,
  337. int size);
  338. extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
  339. extern int of_alias_get_id(struct device_node *np, const char *stem);
  340. extern int of_alias_get_highest_id(const char *stem);
  341. extern int of_machine_is_compatible(const char *compat);
  342. extern int of_add_property(struct device_node *np, struct property *prop);
  343. extern int of_remove_property(struct device_node *np, struct property *prop);
  344. extern int of_update_property(struct device_node *np, struct property *newprop);
  345. /* For updating the device tree at runtime */
  346. #define OF_RECONFIG_ATTACH_NODE 0x0001
  347. #define OF_RECONFIG_DETACH_NODE 0x0002
  348. #define OF_RECONFIG_ADD_PROPERTY 0x0003
  349. #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
  350. #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
  351. extern int of_attach_node(struct device_node *);
  352. extern int of_detach_node(struct device_node *);
  353. #define of_match_ptr(_ptr) (_ptr)
  354. /*
  355. * struct property *prop;
  356. * const __be32 *p;
  357. * u32 u;
  358. *
  359. * of_property_for_each_u32(np, "propname", prop, p, u)
  360. * printk("U32 value: %x\n", u);
  361. */
  362. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  363. u32 *pu);
  364. /*
  365. * struct property *prop;
  366. * const char *s;
  367. *
  368. * of_property_for_each_string(np, "propname", prop, s)
  369. * printk("String value: %s\n", s);
  370. */
  371. const char *of_prop_next_string(struct property *prop, const char *cur);
  372. bool of_console_check(struct device_node *dn, char *name, int index);
  373. extern int of_cpu_node_to_id(struct device_node *np);
  374. int of_map_id(struct device_node *np, u32 id,
  375. const char *map_name, const char *map_mask_name,
  376. struct device_node **target, u32 *id_out);
  377. phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
  378. struct kimage;
  379. void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
  380. unsigned long initrd_load_addr,
  381. unsigned long initrd_len,
  382. const char *cmdline, size_t extra_fdt_size);
  383. #else /* CONFIG_OF */
  384. static inline void of_core_init(void)
  385. {
  386. }
  387. static inline bool is_of_node(const struct fwnode_handle *fwnode)
  388. {
  389. return false;
  390. }
  391. static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
  392. {
  393. return NULL;
  394. }
  395. static inline bool of_node_name_eq(const struct device_node *np, const char *name)
  396. {
  397. return false;
  398. }
  399. static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
  400. {
  401. return false;
  402. }
  403. static inline const char* of_node_full_name(const struct device_node *np)
  404. {
  405. return "<no-node>";
  406. }
  407. static inline struct device_node *of_find_node_by_name(struct device_node *from,
  408. const char *name)
  409. {
  410. return NULL;
  411. }
  412. static inline struct device_node *of_find_node_by_type(struct device_node *from,
  413. const char *type)
  414. {
  415. return NULL;
  416. }
  417. static inline struct device_node *of_find_matching_node_and_match(
  418. struct device_node *from,
  419. const struct of_device_id *matches,
  420. const struct of_device_id **match)
  421. {
  422. return NULL;
  423. }
  424. static inline struct device_node *of_find_node_by_path(const char *path)
  425. {
  426. return NULL;
  427. }
  428. static inline struct device_node *of_find_node_opts_by_path(const char *path,
  429. const char **opts)
  430. {
  431. return NULL;
  432. }
  433. static inline struct device_node *of_find_node_by_phandle(phandle handle)
  434. {
  435. return NULL;
  436. }
  437. static inline struct device_node *of_get_parent(const struct device_node *node)
  438. {
  439. return NULL;
  440. }
  441. static inline struct device_node *of_get_next_parent(struct device_node *node)
  442. {
  443. return NULL;
  444. }
  445. static inline struct device_node *of_get_next_child(
  446. const struct device_node *node, struct device_node *prev)
  447. {
  448. return NULL;
  449. }
  450. static inline struct device_node *of_get_next_available_child(
  451. const struct device_node *node, struct device_node *prev)
  452. {
  453. return NULL;
  454. }
  455. static inline struct device_node *of_find_node_with_property(
  456. struct device_node *from, const char *prop_name)
  457. {
  458. return NULL;
  459. }
  460. #define of_fwnode_handle(node) NULL
  461. static inline bool of_have_populated_dt(void)
  462. {
  463. return false;
  464. }
  465. static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
  466. const char *compatible)
  467. {
  468. return NULL;
  469. }
  470. static inline struct device_node *of_get_child_by_name(
  471. const struct device_node *node,
  472. const char *name)
  473. {
  474. return NULL;
  475. }
  476. static inline int of_device_is_compatible(const struct device_node *device,
  477. const char *name)
  478. {
  479. return 0;
  480. }
  481. static inline int of_device_compatible_match(const struct device_node *device,
  482. const char *const *compat)
  483. {
  484. return 0;
  485. }
  486. static inline bool of_device_is_available(const struct device_node *device)
  487. {
  488. return false;
  489. }
  490. static inline bool of_device_is_big_endian(const struct device_node *device)
  491. {
  492. return false;
  493. }
  494. static inline struct property *of_find_property(const struct device_node *np,
  495. const char *name,
  496. int *lenp)
  497. {
  498. return NULL;
  499. }
  500. static inline struct device_node *of_find_compatible_node(
  501. struct device_node *from,
  502. const char *type,
  503. const char *compat)
  504. {
  505. return NULL;
  506. }
  507. static inline int of_property_count_elems_of_size(const struct device_node *np,
  508. const char *propname, int elem_size)
  509. {
  510. return -ENOSYS;
  511. }
  512. static inline int of_property_read_u32_index(const struct device_node *np,
  513. const char *propname, u32 index, u32 *out_value)
  514. {
  515. return -ENOSYS;
  516. }
  517. static inline int of_property_read_u64_index(const struct device_node *np,
  518. const char *propname, u32 index, u64 *out_value)
  519. {
  520. return -ENOSYS;
  521. }
  522. static inline const void *of_get_property(const struct device_node *node,
  523. const char *name,
  524. int *lenp)
  525. {
  526. return NULL;
  527. }
  528. static inline struct device_node *of_get_cpu_node(int cpu,
  529. unsigned int *thread)
  530. {
  531. return NULL;
  532. }
  533. static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
  534. {
  535. return NULL;
  536. }
  537. static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
  538. int index)
  539. {
  540. return NULL;
  541. }
  542. static inline int of_n_addr_cells(struct device_node *np)
  543. {
  544. return 0;
  545. }
  546. static inline int of_n_size_cells(struct device_node *np)
  547. {
  548. return 0;
  549. }
  550. static inline int of_property_read_variable_u8_array(const struct device_node *np,
  551. const char *propname, u8 *out_values,
  552. size_t sz_min, size_t sz_max)
  553. {
  554. return -ENOSYS;
  555. }
  556. static inline int of_property_read_variable_u16_array(const struct device_node *np,
  557. const char *propname, u16 *out_values,
  558. size_t sz_min, size_t sz_max)
  559. {
  560. return -ENOSYS;
  561. }
  562. static inline int of_property_read_variable_u32_array(const struct device_node *np,
  563. const char *propname,
  564. u32 *out_values,
  565. size_t sz_min,
  566. size_t sz_max)
  567. {
  568. return -ENOSYS;
  569. }
  570. static inline int of_property_read_u64(const struct device_node *np,
  571. const char *propname, u64 *out_value)
  572. {
  573. return -ENOSYS;
  574. }
  575. static inline int of_property_read_variable_u64_array(const struct device_node *np,
  576. const char *propname,
  577. u64 *out_values,
  578. size_t sz_min,
  579. size_t sz_max)
  580. {
  581. return -ENOSYS;
  582. }
  583. static inline int of_property_read_string(const struct device_node *np,
  584. const char *propname,
  585. const char **out_string)
  586. {
  587. return -ENOSYS;
  588. }
  589. static inline int of_property_match_string(const struct device_node *np,
  590. const char *propname,
  591. const char *string)
  592. {
  593. return -ENOSYS;
  594. }
  595. static inline int of_property_read_string_helper(const struct device_node *np,
  596. const char *propname,
  597. const char **out_strs, size_t sz, int index)
  598. {
  599. return -ENOSYS;
  600. }
  601. static inline int __of_parse_phandle_with_args(const struct device_node *np,
  602. const char *list_name,
  603. const char *cells_name,
  604. int cell_count,
  605. int index,
  606. struct of_phandle_args *out_args)
  607. {
  608. return -ENOSYS;
  609. }
  610. static inline int of_parse_phandle_with_args_map(const struct device_node *np,
  611. const char *list_name,
  612. const char *stem_name,
  613. int index,
  614. struct of_phandle_args *out_args)
  615. {
  616. return -ENOSYS;
  617. }
  618. static inline int of_count_phandle_with_args(const struct device_node *np,
  619. const char *list_name,
  620. const char *cells_name)
  621. {
  622. return -ENOSYS;
  623. }
  624. static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
  625. const struct device_node *np,
  626. const char *list_name,
  627. const char *cells_name,
  628. int cell_count)
  629. {
  630. return -ENOSYS;
  631. }
  632. static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
  633. {
  634. return -ENOSYS;
  635. }
  636. static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
  637. uint32_t *args,
  638. int size)
  639. {
  640. return 0;
  641. }
  642. static inline int of_alias_get_id(struct device_node *np, const char *stem)
  643. {
  644. return -ENOSYS;
  645. }
  646. static inline int of_alias_get_highest_id(const char *stem)
  647. {
  648. return -ENOSYS;
  649. }
  650. static inline int of_machine_is_compatible(const char *compat)
  651. {
  652. return 0;
  653. }
  654. static inline int of_add_property(struct device_node *np, struct property *prop)
  655. {
  656. return 0;
  657. }
  658. static inline int of_remove_property(struct device_node *np, struct property *prop)
  659. {
  660. return 0;
  661. }
  662. static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
  663. {
  664. return false;
  665. }
  666. static inline const __be32 *of_prop_next_u32(struct property *prop,
  667. const __be32 *cur, u32 *pu)
  668. {
  669. return NULL;
  670. }
  671. static inline const char *of_prop_next_string(struct property *prop,
  672. const char *cur)
  673. {
  674. return NULL;
  675. }
  676. static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
  677. {
  678. return 0;
  679. }
  680. static inline int of_node_test_and_set_flag(struct device_node *n,
  681. unsigned long flag)
  682. {
  683. return 0;
  684. }
  685. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  686. {
  687. }
  688. static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
  689. {
  690. }
  691. static inline int of_property_check_flag(const struct property *p,
  692. unsigned long flag)
  693. {
  694. return 0;
  695. }
  696. static inline void of_property_set_flag(struct property *p, unsigned long flag)
  697. {
  698. }
  699. static inline void of_property_clear_flag(struct property *p, unsigned long flag)
  700. {
  701. }
  702. static inline int of_cpu_node_to_id(struct device_node *np)
  703. {
  704. return -ENODEV;
  705. }
  706. static inline int of_map_id(struct device_node *np, u32 id,
  707. const char *map_name, const char *map_mask_name,
  708. struct device_node **target, u32 *id_out)
  709. {
  710. return -EINVAL;
  711. }
  712. static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
  713. {
  714. return PHYS_ADDR_MAX;
  715. }
  716. #define of_match_ptr(_ptr) NULL
  717. #define of_match_node(_matches, _node) NULL
  718. #endif /* CONFIG_OF */
  719. /* Default string compare functions, Allow arch asm/prom.h to override */
  720. #if !defined(of_compat_cmp)
  721. #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
  722. #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
  723. #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
  724. #endif
  725. static inline int of_prop_val_eq(struct property *p1, struct property *p2)
  726. {
  727. return p1->length == p2->length &&
  728. !memcmp(p1->value, p2->value, (size_t)p1->length);
  729. }
  730. #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
  731. extern int of_node_to_nid(struct device_node *np);
  732. #else
  733. static inline int of_node_to_nid(struct device_node *device)
  734. {
  735. return NUMA_NO_NODE;
  736. }
  737. #endif
  738. #ifdef CONFIG_OF_NUMA
  739. extern int of_numa_init(void);
  740. #else
  741. static inline int of_numa_init(void)
  742. {
  743. return -ENOSYS;
  744. }
  745. #endif
  746. static inline struct device_node *of_find_matching_node(
  747. struct device_node *from,
  748. const struct of_device_id *matches)
  749. {
  750. return of_find_matching_node_and_match(from, matches, NULL);
  751. }
  752. static inline const char *of_node_get_device_type(const struct device_node *np)
  753. {
  754. return of_get_property(np, "device_type", NULL);
  755. }
  756. static inline bool of_node_is_type(const struct device_node *np, const char *type)
  757. {
  758. const char *match = of_node_get_device_type(np);
  759. return np && match && type && !strcmp(match, type);
  760. }
  761. /**
  762. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  763. * @np: Pointer to device node holding phandle property
  764. * @phandle_name: Name of property holding a phandle value
  765. * @index: For properties holding a table of phandles, this is the index into
  766. * the table
  767. *
  768. * Return: The device_node pointer with refcount incremented. Use
  769. * of_node_put() on it when done.
  770. */
  771. static inline struct device_node *of_parse_phandle(const struct device_node *np,
  772. const char *phandle_name,
  773. int index)
  774. {
  775. struct of_phandle_args args;
  776. if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
  777. index, &args))
  778. return NULL;
  779. return args.np;
  780. }
  781. /**
  782. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  783. * @np: pointer to a device tree node containing a list
  784. * @list_name: property name that contains a list
  785. * @cells_name: property name that specifies phandles' arguments count
  786. * @index: index of a phandle to parse out
  787. * @out_args: optional pointer to output arguments structure (will be filled)
  788. *
  789. * This function is useful to parse lists of phandles and their arguments.
  790. * Returns 0 on success and fills out_args, on error returns appropriate
  791. * errno value.
  792. *
  793. * Caller is responsible to call of_node_put() on the returned out_args->np
  794. * pointer.
  795. *
  796. * Example::
  797. *
  798. * phandle1: node1 {
  799. * #list-cells = <2>;
  800. * };
  801. *
  802. * phandle2: node2 {
  803. * #list-cells = <1>;
  804. * };
  805. *
  806. * node3 {
  807. * list = <&phandle1 1 2 &phandle2 3>;
  808. * };
  809. *
  810. * To get a device_node of the ``node2`` node you may call this:
  811. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  812. */
  813. static inline int of_parse_phandle_with_args(const struct device_node *np,
  814. const char *list_name,
  815. const char *cells_name,
  816. int index,
  817. struct of_phandle_args *out_args)
  818. {
  819. int cell_count = -1;
  820. /* If cells_name is NULL we assume a cell count of 0 */
  821. if (!cells_name)
  822. cell_count = 0;
  823. return __of_parse_phandle_with_args(np, list_name, cells_name,
  824. cell_count, index, out_args);
  825. }
  826. /**
  827. * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
  828. * @np: pointer to a device tree node containing a list
  829. * @list_name: property name that contains a list
  830. * @cell_count: number of argument cells following the phandle
  831. * @index: index of a phandle to parse out
  832. * @out_args: optional pointer to output arguments structure (will be filled)
  833. *
  834. * This function is useful to parse lists of phandles and their arguments.
  835. * Returns 0 on success and fills out_args, on error returns appropriate
  836. * errno value.
  837. *
  838. * Caller is responsible to call of_node_put() on the returned out_args->np
  839. * pointer.
  840. *
  841. * Example::
  842. *
  843. * phandle1: node1 {
  844. * };
  845. *
  846. * phandle2: node2 {
  847. * };
  848. *
  849. * node3 {
  850. * list = <&phandle1 0 2 &phandle2 2 3>;
  851. * };
  852. *
  853. * To get a device_node of the ``node2`` node you may call this:
  854. * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
  855. */
  856. static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
  857. const char *list_name,
  858. int cell_count,
  859. int index,
  860. struct of_phandle_args *out_args)
  861. {
  862. return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
  863. index, out_args);
  864. }
  865. /**
  866. * of_property_count_u8_elems - Count the number of u8 elements in a property
  867. *
  868. * @np: device node from which the property value is to be read.
  869. * @propname: name of the property to be searched.
  870. *
  871. * Search for a property in a device node and count the number of u8 elements
  872. * in it.
  873. *
  874. * Return: The number of elements on sucess, -EINVAL if the property does
  875. * not exist or its length does not match a multiple of u8 and -ENODATA if the
  876. * property does not have a value.
  877. */
  878. static inline int of_property_count_u8_elems(const struct device_node *np,
  879. const char *propname)
  880. {
  881. return of_property_count_elems_of_size(np, propname, sizeof(u8));
  882. }
  883. /**
  884. * of_property_count_u16_elems - Count the number of u16 elements in a property
  885. *
  886. * @np: device node from which the property value is to be read.
  887. * @propname: name of the property to be searched.
  888. *
  889. * Search for a property in a device node and count the number of u16 elements
  890. * in it.
  891. *
  892. * Return: The number of elements on sucess, -EINVAL if the property does
  893. * not exist or its length does not match a multiple of u16 and -ENODATA if the
  894. * property does not have a value.
  895. */
  896. static inline int of_property_count_u16_elems(const struct device_node *np,
  897. const char *propname)
  898. {
  899. return of_property_count_elems_of_size(np, propname, sizeof(u16));
  900. }
  901. /**
  902. * of_property_count_u32_elems - Count the number of u32 elements in a property
  903. *
  904. * @np: device node from which the property value is to be read.
  905. * @propname: name of the property to be searched.
  906. *
  907. * Search for a property in a device node and count the number of u32 elements
  908. * in it.
  909. *
  910. * Return: The number of elements on sucess, -EINVAL if the property does
  911. * not exist or its length does not match a multiple of u32 and -ENODATA if the
  912. * property does not have a value.
  913. */
  914. static inline int of_property_count_u32_elems(const struct device_node *np,
  915. const char *propname)
  916. {
  917. return of_property_count_elems_of_size(np, propname, sizeof(u32));
  918. }
  919. /**
  920. * of_property_count_u64_elems - Count the number of u64 elements in a property
  921. *
  922. * @np: device node from which the property value is to be read.
  923. * @propname: name of the property to be searched.
  924. *
  925. * Search for a property in a device node and count the number of u64 elements
  926. * in it.
  927. *
  928. * Return: The number of elements on sucess, -EINVAL if the property does
  929. * not exist or its length does not match a multiple of u64 and -ENODATA if the
  930. * property does not have a value.
  931. */
  932. static inline int of_property_count_u64_elems(const struct device_node *np,
  933. const char *propname)
  934. {
  935. return of_property_count_elems_of_size(np, propname, sizeof(u64));
  936. }
  937. /**
  938. * of_property_read_string_array() - Read an array of strings from a multiple
  939. * strings property.
  940. * @np: device node from which the property value is to be read.
  941. * @propname: name of the property to be searched.
  942. * @out_strs: output array of string pointers.
  943. * @sz: number of array elements to read.
  944. *
  945. * Search for a property in a device tree node and retrieve a list of
  946. * terminated string values (pointer to data, not a copy) in that property.
  947. *
  948. * Return: If @out_strs is NULL, the number of strings in the property is returned.
  949. */
  950. static inline int of_property_read_string_array(const struct device_node *np,
  951. const char *propname, const char **out_strs,
  952. size_t sz)
  953. {
  954. return of_property_read_string_helper(np, propname, out_strs, sz, 0);
  955. }
  956. /**
  957. * of_property_count_strings() - Find and return the number of strings from a
  958. * multiple strings property.
  959. * @np: device node from which the property value is to be read.
  960. * @propname: name of the property to be searched.
  961. *
  962. * Search for a property in a device tree node and retrieve the number of null
  963. * terminated string contain in it.
  964. *
  965. * Return: The number of strings on success, -EINVAL if the property does not
  966. * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
  967. * is not null-terminated within the length of the property data.
  968. */
  969. static inline int of_property_count_strings(const struct device_node *np,
  970. const char *propname)
  971. {
  972. return of_property_read_string_helper(np, propname, NULL, 0, 0);
  973. }
  974. /**
  975. * of_property_read_string_index() - Find and read a string from a multiple
  976. * strings property.
  977. * @np: device node from which the property value is to be read.
  978. * @propname: name of the property to be searched.
  979. * @index: index of the string in the list of strings
  980. * @output: pointer to null terminated return string, modified only if
  981. * return value is 0.
  982. *
  983. * Search for a property in a device tree node and retrieve a null
  984. * terminated string value (pointer to data, not a copy) in the list of strings
  985. * contained in that property.
  986. *
  987. * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
  988. * property does not have a value, and -EILSEQ if the string is not
  989. * null-terminated within the length of the property data.
  990. *
  991. * The out_string pointer is modified only if a valid string can be decoded.
  992. */
  993. static inline int of_property_read_string_index(const struct device_node *np,
  994. const char *propname,
  995. int index, const char **output)
  996. {
  997. int rc = of_property_read_string_helper(np, propname, output, 1, index);
  998. return rc < 0 ? rc : 0;
  999. }
  1000. /**
  1001. * of_property_read_bool - Find a property
  1002. * @np: device node from which the property value is to be read.
  1003. * @propname: name of the property to be searched.
  1004. *
  1005. * Search for a property in a device node.
  1006. *
  1007. * Return: true if the property exists false otherwise.
  1008. */
  1009. static inline bool of_property_read_bool(const struct device_node *np,
  1010. const char *propname)
  1011. {
  1012. struct property *prop = of_find_property(np, propname, NULL);
  1013. return prop ? true : false;
  1014. }
  1015. /**
  1016. * of_property_read_u8_array - Find and read an array of u8 from a property.
  1017. *
  1018. * @np: device node from which the property value is to be read.
  1019. * @propname: name of the property to be searched.
  1020. * @out_values: pointer to return value, modified only if return value is 0.
  1021. * @sz: number of array elements to read
  1022. *
  1023. * Search for a property in a device node and read 8-bit value(s) from
  1024. * it.
  1025. *
  1026. * dts entry of array should be like:
  1027. * ``property = /bits/ 8 <0x50 0x60 0x70>;``
  1028. *
  1029. * Return: 0 on success, -EINVAL if the property does not exist,
  1030. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1031. * property data isn't large enough.
  1032. *
  1033. * The out_values is modified only if a valid u8 value can be decoded.
  1034. */
  1035. static inline int of_property_read_u8_array(const struct device_node *np,
  1036. const char *propname,
  1037. u8 *out_values, size_t sz)
  1038. {
  1039. int ret = of_property_read_variable_u8_array(np, propname, out_values,
  1040. sz, 0);
  1041. if (ret >= 0)
  1042. return 0;
  1043. else
  1044. return ret;
  1045. }
  1046. /**
  1047. * of_property_read_u16_array - Find and read an array of u16 from a property.
  1048. *
  1049. * @np: device node from which the property value is to be read.
  1050. * @propname: name of the property to be searched.
  1051. * @out_values: pointer to return value, modified only if return value is 0.
  1052. * @sz: number of array elements to read
  1053. *
  1054. * Search for a property in a device node and read 16-bit value(s) from
  1055. * it.
  1056. *
  1057. * dts entry of array should be like:
  1058. * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
  1059. *
  1060. * Return: 0 on success, -EINVAL if the property does not exist,
  1061. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1062. * property data isn't large enough.
  1063. *
  1064. * The out_values is modified only if a valid u16 value can be decoded.
  1065. */
  1066. static inline int of_property_read_u16_array(const struct device_node *np,
  1067. const char *propname,
  1068. u16 *out_values, size_t sz)
  1069. {
  1070. int ret = of_property_read_variable_u16_array(np, propname, out_values,
  1071. sz, 0);
  1072. if (ret >= 0)
  1073. return 0;
  1074. else
  1075. return ret;
  1076. }
  1077. /**
  1078. * of_property_read_u32_array - Find and read an array of 32 bit integers
  1079. * from a property.
  1080. *
  1081. * @np: device node from which the property value is to be read.
  1082. * @propname: name of the property to be searched.
  1083. * @out_values: pointer to return value, modified only if return value is 0.
  1084. * @sz: number of array elements to read
  1085. *
  1086. * Search for a property in a device node and read 32-bit value(s) from
  1087. * it.
  1088. *
  1089. * Return: 0 on success, -EINVAL if the property does not exist,
  1090. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1091. * property data isn't large enough.
  1092. *
  1093. * The out_values is modified only if a valid u32 value can be decoded.
  1094. */
  1095. static inline int of_property_read_u32_array(const struct device_node *np,
  1096. const char *propname,
  1097. u32 *out_values, size_t sz)
  1098. {
  1099. int ret = of_property_read_variable_u32_array(np, propname, out_values,
  1100. sz, 0);
  1101. if (ret >= 0)
  1102. return 0;
  1103. else
  1104. return ret;
  1105. }
  1106. /**
  1107. * of_property_read_u64_array - Find and read an array of 64 bit integers
  1108. * from a property.
  1109. *
  1110. * @np: device node from which the property value is to be read.
  1111. * @propname: name of the property to be searched.
  1112. * @out_values: pointer to return value, modified only if return value is 0.
  1113. * @sz: number of array elements to read
  1114. *
  1115. * Search for a property in a device node and read 64-bit value(s) from
  1116. * it.
  1117. *
  1118. * Return: 0 on success, -EINVAL if the property does not exist,
  1119. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  1120. * property data isn't large enough.
  1121. *
  1122. * The out_values is modified only if a valid u64 value can be decoded.
  1123. */
  1124. static inline int of_property_read_u64_array(const struct device_node *np,
  1125. const char *propname,
  1126. u64 *out_values, size_t sz)
  1127. {
  1128. int ret = of_property_read_variable_u64_array(np, propname, out_values,
  1129. sz, 0);
  1130. if (ret >= 0)
  1131. return 0;
  1132. else
  1133. return ret;
  1134. }
  1135. static inline int of_property_read_u8(const struct device_node *np,
  1136. const char *propname,
  1137. u8 *out_value)
  1138. {
  1139. return of_property_read_u8_array(np, propname, out_value, 1);
  1140. }
  1141. static inline int of_property_read_u16(const struct device_node *np,
  1142. const char *propname,
  1143. u16 *out_value)
  1144. {
  1145. return of_property_read_u16_array(np, propname, out_value, 1);
  1146. }
  1147. static inline int of_property_read_u32(const struct device_node *np,
  1148. const char *propname,
  1149. u32 *out_value)
  1150. {
  1151. return of_property_read_u32_array(np, propname, out_value, 1);
  1152. }
  1153. static inline int of_property_read_s32(const struct device_node *np,
  1154. const char *propname,
  1155. s32 *out_value)
  1156. {
  1157. return of_property_read_u32(np, propname, (u32*) out_value);
  1158. }
  1159. #define of_for_each_phandle(it, err, np, ln, cn, cc) \
  1160. for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
  1161. err = of_phandle_iterator_next(it); \
  1162. err == 0; \
  1163. err = of_phandle_iterator_next(it))
  1164. #define of_property_for_each_u32(np, propname, prop, p, u) \
  1165. for (prop = of_find_property(np, propname, NULL), \
  1166. p = of_prop_next_u32(prop, NULL, &u); \
  1167. p; \
  1168. p = of_prop_next_u32(prop, p, &u))
  1169. #define of_property_for_each_string(np, propname, prop, s) \
  1170. for (prop = of_find_property(np, propname, NULL), \
  1171. s = of_prop_next_string(prop, NULL); \
  1172. s; \
  1173. s = of_prop_next_string(prop, s))
  1174. #define for_each_node_by_name(dn, name) \
  1175. for (dn = of_find_node_by_name(NULL, name); dn; \
  1176. dn = of_find_node_by_name(dn, name))
  1177. #define for_each_node_by_type(dn, type) \
  1178. for (dn = of_find_node_by_type(NULL, type); dn; \
  1179. dn = of_find_node_by_type(dn, type))
  1180. #define for_each_compatible_node(dn, type, compatible) \
  1181. for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
  1182. dn = of_find_compatible_node(dn, type, compatible))
  1183. #define for_each_matching_node(dn, matches) \
  1184. for (dn = of_find_matching_node(NULL, matches); dn; \
  1185. dn = of_find_matching_node(dn, matches))
  1186. #define for_each_matching_node_and_match(dn, matches, match) \
  1187. for (dn = of_find_matching_node_and_match(NULL, matches, match); \
  1188. dn; dn = of_find_matching_node_and_match(dn, matches, match))
  1189. #define for_each_child_of_node(parent, child) \
  1190. for (child = of_get_next_child(parent, NULL); child != NULL; \
  1191. child = of_get_next_child(parent, child))
  1192. #define for_each_available_child_of_node(parent, child) \
  1193. for (child = of_get_next_available_child(parent, NULL); child != NULL; \
  1194. child = of_get_next_available_child(parent, child))
  1195. #define for_each_of_cpu_node(cpu) \
  1196. for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
  1197. cpu = of_get_next_cpu_node(cpu))
  1198. #define for_each_node_with_property(dn, prop_name) \
  1199. for (dn = of_find_node_with_property(NULL, prop_name); dn; \
  1200. dn = of_find_node_with_property(dn, prop_name))
  1201. static inline int of_get_child_count(const struct device_node *np)
  1202. {
  1203. struct device_node *child;
  1204. int num = 0;
  1205. for_each_child_of_node(np, child)
  1206. num++;
  1207. return num;
  1208. }
  1209. static inline int of_get_available_child_count(const struct device_node *np)
  1210. {
  1211. struct device_node *child;
  1212. int num = 0;
  1213. for_each_available_child_of_node(np, child)
  1214. num++;
  1215. return num;
  1216. }
  1217. #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type) \
  1218. static const struct of_device_id __of_table_##name \
  1219. __attribute__((unused)) \
  1220. = { .compatible = compat, \
  1221. .data = (fn == (fn_type)NULL) ? fn : fn }
  1222. #if defined(CONFIG_OF) && !defined(MODULE)
  1223. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  1224. static const struct of_device_id __of_table_##name \
  1225. __used __section("__" #table "_of_table") \
  1226. __aligned(__alignof__(struct of_device_id)) \
  1227. = { .compatible = compat, \
  1228. .data = (fn == (fn_type)NULL) ? fn : fn }
  1229. #else
  1230. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  1231. _OF_DECLARE_STUB(table, name, compat, fn, fn_type)
  1232. #endif
  1233. typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
  1234. typedef int (*of_init_fn_1_ret)(struct device_node *);
  1235. typedef void (*of_init_fn_1)(struct device_node *);
  1236. #define OF_DECLARE_1(table, name, compat, fn) \
  1237. _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
  1238. #define OF_DECLARE_1_RET(table, name, compat, fn) \
  1239. _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
  1240. #define OF_DECLARE_2(table, name, compat, fn) \
  1241. _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
  1242. /**
  1243. * struct of_changeset_entry - Holds a changeset entry
  1244. *
  1245. * @node: list_head for the log list
  1246. * @action: notifier action
  1247. * @np: pointer to the device node affected
  1248. * @prop: pointer to the property affected
  1249. * @old_prop: hold a pointer to the original property
  1250. *
  1251. * Every modification of the device tree during a changeset
  1252. * is held in a list of of_changeset_entry structures.
  1253. * That way we can recover from a partial application, or we can
  1254. * revert the changeset
  1255. */
  1256. struct of_changeset_entry {
  1257. struct list_head node;
  1258. unsigned long action;
  1259. struct device_node *np;
  1260. struct property *prop;
  1261. struct property *old_prop;
  1262. };
  1263. /**
  1264. * struct of_changeset - changeset tracker structure
  1265. *
  1266. * @entries: list_head for the changeset entries
  1267. *
  1268. * changesets are a convenient way to apply bulk changes to the
  1269. * live tree. In case of an error, changes are rolled-back.
  1270. * changesets live on after initial application, and if not
  1271. * destroyed after use, they can be reverted in one single call.
  1272. */
  1273. struct of_changeset {
  1274. struct list_head entries;
  1275. };
  1276. enum of_reconfig_change {
  1277. OF_RECONFIG_NO_CHANGE = 0,
  1278. OF_RECONFIG_CHANGE_ADD,
  1279. OF_RECONFIG_CHANGE_REMOVE,
  1280. };
  1281. #ifdef CONFIG_OF_DYNAMIC
  1282. extern int of_reconfig_notifier_register(struct notifier_block *);
  1283. extern int of_reconfig_notifier_unregister(struct notifier_block *);
  1284. extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
  1285. extern int of_reconfig_get_state_change(unsigned long action,
  1286. struct of_reconfig_data *arg);
  1287. extern void of_changeset_init(struct of_changeset *ocs);
  1288. extern void of_changeset_destroy(struct of_changeset *ocs);
  1289. extern int of_changeset_apply(struct of_changeset *ocs);
  1290. extern int of_changeset_revert(struct of_changeset *ocs);
  1291. extern int of_changeset_action(struct of_changeset *ocs,
  1292. unsigned long action, struct device_node *np,
  1293. struct property *prop);
  1294. static inline int of_changeset_attach_node(struct of_changeset *ocs,
  1295. struct device_node *np)
  1296. {
  1297. return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
  1298. }
  1299. static inline int of_changeset_detach_node(struct of_changeset *ocs,
  1300. struct device_node *np)
  1301. {
  1302. return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
  1303. }
  1304. static inline int of_changeset_add_property(struct of_changeset *ocs,
  1305. struct device_node *np, struct property *prop)
  1306. {
  1307. return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
  1308. }
  1309. static inline int of_changeset_remove_property(struct of_changeset *ocs,
  1310. struct device_node *np, struct property *prop)
  1311. {
  1312. return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  1313. }
  1314. static inline int of_changeset_update_property(struct of_changeset *ocs,
  1315. struct device_node *np, struct property *prop)
  1316. {
  1317. return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
  1318. }
  1319. #else /* CONFIG_OF_DYNAMIC */
  1320. static inline int of_reconfig_notifier_register(struct notifier_block *nb)
  1321. {
  1322. return -EINVAL;
  1323. }
  1324. static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
  1325. {
  1326. return -EINVAL;
  1327. }
  1328. static inline int of_reconfig_notify(unsigned long action,
  1329. struct of_reconfig_data *arg)
  1330. {
  1331. return -EINVAL;
  1332. }
  1333. static inline int of_reconfig_get_state_change(unsigned long action,
  1334. struct of_reconfig_data *arg)
  1335. {
  1336. return -EINVAL;
  1337. }
  1338. #endif /* CONFIG_OF_DYNAMIC */
  1339. /**
  1340. * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
  1341. * @np: Pointer to the given device_node
  1342. *
  1343. * Return: true if present false otherwise
  1344. */
  1345. static inline bool of_device_is_system_power_controller(const struct device_node *np)
  1346. {
  1347. return of_property_read_bool(np, "system-power-controller");
  1348. }
  1349. /*
  1350. * Overlay support
  1351. */
  1352. enum of_overlay_notify_action {
  1353. OF_OVERLAY_INIT = 0, /* kzalloc() of ovcs sets this value */
  1354. OF_OVERLAY_PRE_APPLY,
  1355. OF_OVERLAY_POST_APPLY,
  1356. OF_OVERLAY_PRE_REMOVE,
  1357. OF_OVERLAY_POST_REMOVE,
  1358. };
  1359. static inline char *of_overlay_action_name(enum of_overlay_notify_action action)
  1360. {
  1361. static char *of_overlay_action_name[] = {
  1362. "init",
  1363. "pre-apply",
  1364. "post-apply",
  1365. "pre-remove",
  1366. "post-remove",
  1367. };
  1368. return of_overlay_action_name[action];
  1369. }
  1370. struct of_overlay_notify_data {
  1371. struct device_node *overlay;
  1372. struct device_node *target;
  1373. };
  1374. #ifdef CONFIG_OF_OVERLAY
  1375. int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
  1376. int *ovcs_id);
  1377. int of_overlay_remove(int *ovcs_id);
  1378. int of_overlay_remove_all(void);
  1379. int of_overlay_notifier_register(struct notifier_block *nb);
  1380. int of_overlay_notifier_unregister(struct notifier_block *nb);
  1381. #else
  1382. static inline int of_overlay_fdt_apply(void *overlay_fdt, u32 overlay_fdt_size,
  1383. int *ovcs_id)
  1384. {
  1385. return -ENOTSUPP;
  1386. }
  1387. static inline int of_overlay_remove(int *ovcs_id)
  1388. {
  1389. return -ENOTSUPP;
  1390. }
  1391. static inline int of_overlay_remove_all(void)
  1392. {
  1393. return -ENOTSUPP;
  1394. }
  1395. static inline int of_overlay_notifier_register(struct notifier_block *nb)
  1396. {
  1397. return 0;
  1398. }
  1399. static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
  1400. {
  1401. return 0;
  1402. }
  1403. #endif
  1404. #endif /* _LINUX_OF_H */