]> git.karo-electronics.de Git - linux-beck.git/blob - include/linux/of.h
257994a420f3cfd4d25c476847dc0661b44be909
[linux-beck.git] / include / linux / of.h
1 #ifndef _LINUX_OF_H
2 #define _LINUX_OF_H
3 /*
4  * Definitions for talking to the Open Firmware PROM on
5  * Power Macintosh and other computers.
6  *
7  * Copyright (C) 1996-2005 Paul Mackerras.
8  *
9  * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
10  * Updates for SPARC64 by David S. Miller
11  * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version
16  * 2 of the License, or (at your option) any later version.
17  */
18 #include <linux/types.h>
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/kobject.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/spinlock.h>
24 #include <linux/topology.h>
25 #include <linux/notifier.h>
26
27 #include <asm/byteorder.h>
28 #include <asm/errno.h>
29
30 typedef u32 phandle;
31 typedef u32 ihandle;
32
33 struct property {
34         char    *name;
35         int     length;
36         void    *value;
37         struct property *next;
38         unsigned long _flags;
39         unsigned int unique_id;
40         struct bin_attribute attr;
41 };
42
43 #if defined(CONFIG_SPARC)
44 struct of_irq_controller;
45 #endif
46
47 struct device_node {
48         const char *name;
49         const char *type;
50         phandle phandle;
51         const char *full_name;
52
53         struct  property *properties;
54         struct  property *deadprops;    /* removed properties */
55         struct  device_node *parent;
56         struct  device_node *child;
57         struct  device_node *sibling;
58         struct  device_node *next;      /* next device of same type */
59         struct  device_node *allnext;   /* next in list of all nodes */
60         struct  kobject kobj;
61         unsigned long _flags;
62         void    *data;
63 #if defined(CONFIG_SPARC)
64         const char *path_component_name;
65         unsigned int unique_id;
66         struct of_irq_controller *irq_trans;
67 #endif
68 };
69
70 #define MAX_PHANDLE_ARGS 16
71 struct of_phandle_args {
72         struct device_node *np;
73         int args_count;
74         uint32_t args[MAX_PHANDLE_ARGS];
75 };
76
77 extern int of_node_add(struct device_node *node);
78
79 #ifdef CONFIG_OF_DYNAMIC
80 extern struct device_node *of_node_get(struct device_node *node);
81 extern void of_node_put(struct device_node *node);
82 #else /* CONFIG_OF_DYNAMIC */
83 /* Dummy ref counting routines - to be implemented later */
84 static inline struct device_node *of_node_get(struct device_node *node)
85 {
86         return node;
87 }
88 static inline void of_node_put(struct device_node *node) { }
89 #endif /* !CONFIG_OF_DYNAMIC */
90
91 #ifdef CONFIG_OF
92
93 /* Pointer for first entry in chain of all nodes. */
94 extern struct device_node *of_allnodes;
95 extern struct device_node *of_chosen;
96 extern struct device_node *of_aliases;
97 extern raw_spinlock_t devtree_lock;
98
99 static inline bool of_have_populated_dt(void)
100 {
101         return of_allnodes != NULL;
102 }
103
104 static inline bool of_node_is_root(const struct device_node *node)
105 {
106         return node && (node->parent == NULL);
107 }
108
109 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
110 {
111         return test_bit(flag, &n->_flags);
112 }
113
114 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
115 {
116         set_bit(flag, &n->_flags);
117 }
118
119 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
120 {
121         clear_bit(flag, &n->_flags);
122 }
123
124 static inline int of_property_check_flag(struct property *p, unsigned long flag)
125 {
126         return test_bit(flag, &p->_flags);
127 }
128
129 static inline void of_property_set_flag(struct property *p, unsigned long flag)
130 {
131         set_bit(flag, &p->_flags);
132 }
133
134 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
135 {
136         clear_bit(flag, &p->_flags);
137 }
138
139 extern struct device_node *of_find_all_nodes(struct device_node *prev);
140
141 /*
142  * OF address retrieval & translation
143  */
144
145 /* Helper to read a big number; size is in cells (not bytes) */
146 static inline u64 of_read_number(const __be32 *cell, int size)
147 {
148         u64 r = 0;
149         while (size--)
150                 r = (r << 32) | be32_to_cpu(*(cell++));
151         return r;
152 }
153
154 /* Like of_read_number, but we want an unsigned long result */
155 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
156 {
157         /* toss away upper bits if unsigned long is smaller than u64 */
158         return of_read_number(cell, size);
159 }
160
161 #if defined(CONFIG_SPARC)
162 #include <asm/prom.h>
163 #endif
164
165 /* Default #address and #size cells.  Allow arch asm/prom.h to override */
166 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
167 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
168 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
169 #endif
170
171 /* Default string compare functions, Allow arch asm/prom.h to override */
172 #if !defined(of_compat_cmp)
173 #define of_compat_cmp(s1, s2, l)        strcasecmp((s1), (s2))
174 #define of_prop_cmp(s1, s2)             strcmp((s1), (s2))
175 #define of_node_cmp(s1, s2)             strcasecmp((s1), (s2))
176 #endif
177
178 /* flag descriptions */
179 #define OF_DYNAMIC      1 /* node and properties were allocated via kmalloc */
180 #define OF_DETACHED     2 /* node has been detached from the device tree */
181
182 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
183 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
184
185 #define OF_BAD_ADDR     ((u64)-1)
186
187 static inline const char *of_node_full_name(const struct device_node *np)
188 {
189         return np ? np->full_name : "<no-node>";
190 }
191
192 #define for_each_of_allnodes(dn) \
193         for (dn = of_allnodes; dn; dn = dn->allnext)
194 extern struct device_node *of_find_node_by_name(struct device_node *from,
195         const char *name);
196 extern struct device_node *of_find_node_by_type(struct device_node *from,
197         const char *type);
198 extern struct device_node *of_find_compatible_node(struct device_node *from,
199         const char *type, const char *compat);
200 extern struct device_node *of_find_matching_node_and_match(
201         struct device_node *from,
202         const struct of_device_id *matches,
203         const struct of_device_id **match);
204
205 extern struct device_node *of_find_node_by_path(const char *path);
206 extern struct device_node *of_find_node_by_phandle(phandle handle);
207 extern struct device_node *of_get_parent(const struct device_node *node);
208 extern struct device_node *of_get_next_parent(struct device_node *node);
209 extern struct device_node *of_get_next_child(const struct device_node *node,
210                                              struct device_node *prev);
211 extern struct device_node *of_get_next_available_child(
212         const struct device_node *node, struct device_node *prev);
213
214 extern struct device_node *of_get_child_by_name(const struct device_node *node,
215                                         const char *name);
216
217 /* cache lookup */
218 extern struct device_node *of_find_next_cache_node(const struct device_node *);
219 extern struct device_node *of_find_node_with_property(
220         struct device_node *from, const char *prop_name);
221
222 extern struct property *of_find_property(const struct device_node *np,
223                                          const char *name,
224                                          int *lenp);
225 extern int of_property_read_u32_index(const struct device_node *np,
226                                        const char *propname,
227                                        u32 index, u32 *out_value);
228 extern int of_property_read_u8_array(const struct device_node *np,
229                         const char *propname, u8 *out_values, size_t sz);
230 extern int of_property_read_u16_array(const struct device_node *np,
231                         const char *propname, u16 *out_values, size_t sz);
232 extern int of_property_read_u32_array(const struct device_node *np,
233                                       const char *propname,
234                                       u32 *out_values,
235                                       size_t sz);
236 extern int of_property_read_u64(const struct device_node *np,
237                                 const char *propname, u64 *out_value);
238
239 extern int of_property_read_string(struct device_node *np,
240                                    const char *propname,
241                                    const char **out_string);
242 extern int of_property_read_string_index(struct device_node *np,
243                                          const char *propname,
244                                          int index, const char **output);
245 extern int of_property_match_string(struct device_node *np,
246                                     const char *propname,
247                                     const char *string);
248 extern int of_property_count_strings(struct device_node *np,
249                                      const char *propname);
250 extern int of_device_is_compatible(const struct device_node *device,
251                                    const char *);
252 extern int of_device_is_available(const struct device_node *device);
253 extern const void *of_get_property(const struct device_node *node,
254                                 const char *name,
255                                 int *lenp);
256 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
257 #define for_each_property_of_node(dn, pp) \
258         for (pp = dn->properties; pp != NULL; pp = pp->next)
259
260 extern int of_n_addr_cells(struct device_node *np);
261 extern int of_n_size_cells(struct device_node *np);
262 extern const struct of_device_id *of_match_node(
263         const struct of_device_id *matches, const struct device_node *node);
264 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
265 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
266 extern struct device_node *of_parse_phandle(const struct device_node *np,
267                                             const char *phandle_name,
268                                             int index);
269 extern int of_parse_phandle_with_args(const struct device_node *np,
270         const char *list_name, const char *cells_name, int index,
271         struct of_phandle_args *out_args);
272 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
273         const char *list_name, int cells_count, int index,
274         struct of_phandle_args *out_args);
275 extern int of_count_phandle_with_args(const struct device_node *np,
276         const char *list_name, const char *cells_name);
277
278 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
279 extern int of_alias_get_id(struct device_node *np, const char *stem);
280
281 extern int of_machine_is_compatible(const char *compat);
282
283 extern int of_add_property(struct device_node *np, struct property *prop);
284 extern int of_remove_property(struct device_node *np, struct property *prop);
285 extern int of_update_property(struct device_node *np, struct property *newprop);
286
287 /* For updating the device tree at runtime */
288 #define OF_RECONFIG_ATTACH_NODE         0x0001
289 #define OF_RECONFIG_DETACH_NODE         0x0002
290 #define OF_RECONFIG_ADD_PROPERTY        0x0003
291 #define OF_RECONFIG_REMOVE_PROPERTY     0x0004
292 #define OF_RECONFIG_UPDATE_PROPERTY     0x0005
293
294 struct of_prop_reconfig {
295         struct device_node      *dn;
296         struct property         *prop;
297 };
298
299 extern int of_reconfig_notifier_register(struct notifier_block *);
300 extern int of_reconfig_notifier_unregister(struct notifier_block *);
301 extern int of_reconfig_notify(unsigned long, void *);
302
303 extern int of_attach_node(struct device_node *);
304 extern int of_detach_node(struct device_node *);
305
306 #define of_match_ptr(_ptr)      (_ptr)
307
308 /*
309  * struct property *prop;
310  * const __be32 *p;
311  * u32 u;
312  *
313  * of_property_for_each_u32(np, "propname", prop, p, u)
314  *         printk("U32 value: %x\n", u);
315  */
316 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
317                                u32 *pu);
318 /*
319  * struct property *prop;
320  * const char *s;
321  *
322  * of_property_for_each_string(np, "propname", prop, s)
323  *         printk("String value: %s\n", s);
324  */
325 const char *of_prop_next_string(struct property *prop, const char *cur);
326
327 int of_device_is_stdout_path(struct device_node *dn);
328
329 #else /* CONFIG_OF */
330
331 static inline const char* of_node_full_name(struct device_node *np)
332 {
333         return "<no-node>";
334 }
335
336 static inline struct device_node *of_find_node_by_name(struct device_node *from,
337         const char *name)
338 {
339         return NULL;
340 }
341
342 static inline struct device_node *of_find_node_by_type(struct device_node *from,
343         const char *type)
344 {
345         return NULL;
346 }
347
348 static inline struct device_node *of_find_matching_node_and_match(
349         struct device_node *from,
350         const struct of_device_id *matches,
351         const struct of_device_id **match)
352 {
353         return NULL;
354 }
355
356 static inline struct device_node *of_get_parent(const struct device_node *node)
357 {
358         return NULL;
359 }
360
361 static inline struct device_node *of_get_next_child(
362         const struct device_node *node, struct device_node *prev)
363 {
364         return NULL;
365 }
366
367 static inline struct device_node *of_get_next_available_child(
368         const struct device_node *node, struct device_node *prev)
369 {
370         return NULL;
371 }
372
373 static inline struct device_node *of_find_node_with_property(
374         struct device_node *from, const char *prop_name)
375 {
376         return NULL;
377 }
378
379 static inline bool of_have_populated_dt(void)
380 {
381         return false;
382 }
383
384 static inline struct device_node *of_get_child_by_name(
385                                         const struct device_node *node,
386                                         const char *name)
387 {
388         return NULL;
389 }
390
391 static inline int of_device_is_compatible(const struct device_node *device,
392                                           const char *name)
393 {
394         return 0;
395 }
396
397 static inline int of_device_is_available(const struct device_node *device)
398 {
399         return 0;
400 }
401
402 static inline struct property *of_find_property(const struct device_node *np,
403                                                 const char *name,
404                                                 int *lenp)
405 {
406         return NULL;
407 }
408
409 static inline struct device_node *of_find_compatible_node(
410                                                 struct device_node *from,
411                                                 const char *type,
412                                                 const char *compat)
413 {
414         return NULL;
415 }
416
417 static inline int of_property_read_u32_index(const struct device_node *np,
418                         const char *propname, u32 index, u32 *out_value)
419 {
420         return -ENOSYS;
421 }
422
423 static inline int of_property_read_u8_array(const struct device_node *np,
424                         const char *propname, u8 *out_values, size_t sz)
425 {
426         return -ENOSYS;
427 }
428
429 static inline int of_property_read_u16_array(const struct device_node *np,
430                         const char *propname, u16 *out_values, size_t sz)
431 {
432         return -ENOSYS;
433 }
434
435 static inline int of_property_read_u32_array(const struct device_node *np,
436                                              const char *propname,
437                                              u32 *out_values, size_t sz)
438 {
439         return -ENOSYS;
440 }
441
442 static inline int of_property_read_string(struct device_node *np,
443                                           const char *propname,
444                                           const char **out_string)
445 {
446         return -ENOSYS;
447 }
448
449 static inline int of_property_read_string_index(struct device_node *np,
450                                                 const char *propname, int index,
451                                                 const char **out_string)
452 {
453         return -ENOSYS;
454 }
455
456 static inline int of_property_count_strings(struct device_node *np,
457                                             const char *propname)
458 {
459         return -ENOSYS;
460 }
461
462 static inline const void *of_get_property(const struct device_node *node,
463                                 const char *name,
464                                 int *lenp)
465 {
466         return NULL;
467 }
468
469 static inline struct device_node *of_get_cpu_node(int cpu,
470                                         unsigned int *thread)
471 {
472         return NULL;
473 }
474
475 static inline int of_property_read_u64(const struct device_node *np,
476                                        const char *propname, u64 *out_value)
477 {
478         return -ENOSYS;
479 }
480
481 static inline int of_property_match_string(struct device_node *np,
482                                            const char *propname,
483                                            const char *string)
484 {
485         return -ENOSYS;
486 }
487
488 static inline struct device_node *of_parse_phandle(const struct device_node *np,
489                                                    const char *phandle_name,
490                                                    int index)
491 {
492         return NULL;
493 }
494
495 static inline int of_parse_phandle_with_args(struct device_node *np,
496                                              const char *list_name,
497                                              const char *cells_name,
498                                              int index,
499                                              struct of_phandle_args *out_args)
500 {
501         return -ENOSYS;
502 }
503
504 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
505         const char *list_name, int cells_count, int index,
506         struct of_phandle_args *out_args)
507 {
508         return -ENOSYS;
509 }
510
511 static inline int of_count_phandle_with_args(struct device_node *np,
512                                              const char *list_name,
513                                              const char *cells_name)
514 {
515         return -ENOSYS;
516 }
517
518 static inline int of_alias_get_id(struct device_node *np, const char *stem)
519 {
520         return -ENOSYS;
521 }
522
523 static inline int of_machine_is_compatible(const char *compat)
524 {
525         return 0;
526 }
527
528 static inline int of_device_is_stdout_path(struct device_node *dn)
529 {
530         return 0;
531 }
532
533 static inline const __be32 *of_prop_next_u32(struct property *prop,
534                 const __be32 *cur, u32 *pu)
535 {
536         return NULL;
537 }
538
539 static inline const char *of_prop_next_string(struct property *prop,
540                 const char *cur)
541 {
542         return NULL;
543 }
544
545 #define of_match_ptr(_ptr)      NULL
546 #define of_match_node(_matches, _node)  NULL
547 #endif /* CONFIG_OF */
548
549 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
550 extern int of_node_to_nid(struct device_node *np);
551 #else
552 static inline int of_node_to_nid(struct device_node *device) { return 0; }
553 #endif
554
555 static inline struct device_node *of_find_matching_node(
556         struct device_node *from,
557         const struct of_device_id *matches)
558 {
559         return of_find_matching_node_and_match(from, matches, NULL);
560 }
561
562 /**
563  * of_property_read_bool - Findfrom a property
564  * @np:         device node from which the property value is to be read.
565  * @propname:   name of the property to be searched.
566  *
567  * Search for a property in a device node.
568  * Returns true if the property exist false otherwise.
569  */
570 static inline bool of_property_read_bool(const struct device_node *np,
571                                          const char *propname)
572 {
573         struct property *prop = of_find_property(np, propname, NULL);
574
575         return prop ? true : false;
576 }
577
578 static inline int of_property_read_u8(const struct device_node *np,
579                                        const char *propname,
580                                        u8 *out_value)
581 {
582         return of_property_read_u8_array(np, propname, out_value, 1);
583 }
584
585 static inline int of_property_read_u16(const struct device_node *np,
586                                        const char *propname,
587                                        u16 *out_value)
588 {
589         return of_property_read_u16_array(np, propname, out_value, 1);
590 }
591
592 static inline int of_property_read_u32(const struct device_node *np,
593                                        const char *propname,
594                                        u32 *out_value)
595 {
596         return of_property_read_u32_array(np, propname, out_value, 1);
597 }
598
599 #define of_property_for_each_u32(np, propname, prop, p, u)      \
600         for (prop = of_find_property(np, propname, NULL),       \
601                 p = of_prop_next_u32(prop, NULL, &u);           \
602                 p;                                              \
603                 p = of_prop_next_u32(prop, p, &u))
604
605 #define of_property_for_each_string(np, propname, prop, s)      \
606         for (prop = of_find_property(np, propname, NULL),       \
607                 s = of_prop_next_string(prop, NULL);            \
608                 s;                                              \
609                 s = of_prop_next_string(prop, s))
610
611 #define for_each_node_by_name(dn, name) \
612         for (dn = of_find_node_by_name(NULL, name); dn; \
613              dn = of_find_node_by_name(dn, name))
614 #define for_each_node_by_type(dn, type) \
615         for (dn = of_find_node_by_type(NULL, type); dn; \
616              dn = of_find_node_by_type(dn, type))
617 #define for_each_compatible_node(dn, type, compatible) \
618         for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
619              dn = of_find_compatible_node(dn, type, compatible))
620 #define for_each_matching_node(dn, matches) \
621         for (dn = of_find_matching_node(NULL, matches); dn; \
622              dn = of_find_matching_node(dn, matches))
623 #define for_each_matching_node_and_match(dn, matches, match) \
624         for (dn = of_find_matching_node_and_match(NULL, matches, match); \
625              dn; dn = of_find_matching_node_and_match(dn, matches, match))
626
627 #define for_each_child_of_node(parent, child) \
628         for (child = of_get_next_child(parent, NULL); child != NULL; \
629              child = of_get_next_child(parent, child))
630 #define for_each_available_child_of_node(parent, child) \
631         for (child = of_get_next_available_child(parent, NULL); child != NULL; \
632              child = of_get_next_available_child(parent, child))
633
634 #define for_each_node_with_property(dn, prop_name) \
635         for (dn = of_find_node_with_property(NULL, prop_name); dn; \
636              dn = of_find_node_with_property(dn, prop_name))
637
638 static inline int of_get_child_count(const struct device_node *np)
639 {
640         struct device_node *child;
641         int num = 0;
642
643         for_each_child_of_node(np, child)
644                 num++;
645
646         return num;
647 }
648
649 static inline int of_get_available_child_count(const struct device_node *np)
650 {
651         struct device_node *child;
652         int num = 0;
653
654         for_each_available_child_of_node(np, child)
655                 num++;
656
657         return num;
658 }
659
660 #endif /* _LINUX_OF_H */