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