xref: /linux-6.15/include/linux/of.h (revision 63d23367)
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/kref.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/spinlock.h>
24 #include <linux/topology.h>
25 
26 #include <asm/byteorder.h>
27 #include <asm/errno.h>
28 
29 typedef u32 phandle;
30 typedef u32 ihandle;
31 
32 struct property {
33 	char	*name;
34 	int	length;
35 	void	*value;
36 	struct property *next;
37 	unsigned long _flags;
38 	unsigned int unique_id;
39 };
40 
41 #if defined(CONFIG_SPARC)
42 struct of_irq_controller;
43 #endif
44 
45 struct device_node {
46 	const char *name;
47 	const char *type;
48 	phandle phandle;
49 	const char *full_name;
50 
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 	struct	device_node *next;	/* next device of same type */
57 	struct	device_node *allnext;	/* next in list of all nodes */
58 	struct	proc_dir_entry *pde;	/* this node's proc directory */
59 	struct	kref kref;
60 	unsigned long _flags;
61 	void	*data;
62 #if defined(CONFIG_SPARC)
63 	const char *path_component_name;
64 	unsigned int unique_id;
65 	struct of_irq_controller *irq_trans;
66 #endif
67 };
68 
69 #define MAX_PHANDLE_ARGS 8
70 struct of_phandle_args {
71 	struct device_node *np;
72 	int args_count;
73 	uint32_t args[MAX_PHANDLE_ARGS];
74 };
75 
76 #ifdef CONFIG_OF_DYNAMIC
77 extern struct device_node *of_node_get(struct device_node *node);
78 extern void of_node_put(struct device_node *node);
79 #else /* CONFIG_OF_DYNAMIC */
80 /* Dummy ref counting routines - to be implemented later */
81 static inline struct device_node *of_node_get(struct device_node *node)
82 {
83 	return node;
84 }
85 static inline void of_node_put(struct device_node *node) { }
86 #endif /* !CONFIG_OF_DYNAMIC */
87 
88 #ifdef CONFIG_OF
89 
90 /* Pointer for first entry in chain of all nodes. */
91 extern struct device_node *of_allnodes;
92 extern struct device_node *of_chosen;
93 extern struct device_node *of_aliases;
94 extern rwlock_t devtree_lock;
95 
96 static inline bool of_have_populated_dt(void)
97 {
98 	return of_allnodes != NULL;
99 }
100 
101 static inline bool of_node_is_root(const struct device_node *node)
102 {
103 	return node && (node->parent == NULL);
104 }
105 
106 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
107 {
108 	return test_bit(flag, &n->_flags);
109 }
110 
111 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
112 {
113 	set_bit(flag, &n->_flags);
114 }
115 
116 extern struct device_node *of_find_all_nodes(struct device_node *prev);
117 
118 /*
119  * OF address retrieval & translation
120  */
121 
122 /* Helper to read a big number; size is in cells (not bytes) */
123 static inline u64 of_read_number(const __be32 *cell, int size)
124 {
125 	u64 r = 0;
126 	while (size--)
127 		r = (r << 32) | be32_to_cpu(*(cell++));
128 	return r;
129 }
130 
131 /* Like of_read_number, but we want an unsigned long result */
132 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
133 {
134 	/* toss away upper bits if unsigned long is smaller than u64 */
135 	return of_read_number(cell, size);
136 }
137 
138 #include <asm/prom.h>
139 
140 /* Default #address and #size cells.  Allow arch asm/prom.h to override */
141 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
142 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
143 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
144 #endif
145 
146 /* Default string compare functions, Allow arch asm/prom.h to override */
147 #if !defined(of_compat_cmp)
148 #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
149 #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
150 #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
151 #endif
152 
153 /* flag descriptions */
154 #define OF_DYNAMIC	1 /* node and properties were allocated via kmalloc */
155 #define OF_DETACHED	2 /* node has been detached from the device tree */
156 
157 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
158 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
159 
160 #define OF_BAD_ADDR	((u64)-1)
161 
162 static inline const char* of_node_full_name(struct device_node *np)
163 {
164 	return np ? np->full_name : "<no-node>";
165 }
166 
167 extern struct device_node *of_find_node_by_name(struct device_node *from,
168 	const char *name);
169 #define for_each_node_by_name(dn, name) \
170 	for (dn = of_find_node_by_name(NULL, name); dn; \
171 	     dn = of_find_node_by_name(dn, name))
172 extern struct device_node *of_find_node_by_type(struct device_node *from,
173 	const char *type);
174 #define for_each_node_by_type(dn, type) \
175 	for (dn = of_find_node_by_type(NULL, type); dn; \
176 	     dn = of_find_node_by_type(dn, type))
177 extern struct device_node *of_find_compatible_node(struct device_node *from,
178 	const char *type, const char *compat);
179 #define for_each_compatible_node(dn, type, compatible) \
180 	for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
181 	     dn = of_find_compatible_node(dn, type, compatible))
182 extern struct device_node *of_find_matching_node_and_match(
183 	struct device_node *from,
184 	const struct of_device_id *matches,
185 	const struct of_device_id **match);
186 static inline struct device_node *of_find_matching_node(
187 	struct device_node *from,
188 	const struct of_device_id *matches)
189 {
190 	return of_find_matching_node_and_match(from, matches, NULL);
191 }
192 #define for_each_matching_node(dn, matches) \
193 	for (dn = of_find_matching_node(NULL, matches); dn; \
194 	     dn = of_find_matching_node(dn, matches))
195 #define for_each_matching_node_and_match(dn, matches, match) \
196 	for (dn = of_find_matching_node_and_match(NULL, matches, match); \
197 	     dn; dn = of_find_matching_node_and_match(dn, matches, match))
198 extern struct device_node *of_find_node_by_path(const char *path);
199 extern struct device_node *of_find_node_by_phandle(phandle handle);
200 extern struct device_node *of_get_parent(const struct device_node *node);
201 extern struct device_node *of_get_next_parent(struct device_node *node);
202 extern struct device_node *of_get_next_child(const struct device_node *node,
203 					     struct device_node *prev);
204 extern struct device_node *of_get_next_available_child(
205 	const struct device_node *node, struct device_node *prev);
206 
207 extern struct device_node *of_get_child_by_name(const struct device_node *node,
208 					const char *name);
209 #define for_each_child_of_node(parent, child) \
210 	for (child = of_get_next_child(parent, NULL); child != NULL; \
211 	     child = of_get_next_child(parent, child))
212 
213 #define for_each_available_child_of_node(parent, child) \
214 	for (child = of_get_next_available_child(parent, NULL); child != NULL; \
215 	     child = of_get_next_available_child(parent, child))
216 
217 static inline int of_get_child_count(const struct device_node *np)
218 {
219 	struct device_node *child;
220 	int num = 0;
221 
222 	for_each_child_of_node(np, child)
223 		num++;
224 
225 	return num;
226 }
227 
228 extern struct device_node *of_find_node_with_property(
229 	struct device_node *from, const char *prop_name);
230 #define for_each_node_with_property(dn, prop_name) \
231 	for (dn = of_find_node_with_property(NULL, prop_name); dn; \
232 	     dn = of_find_node_with_property(dn, prop_name))
233 
234 extern struct property *of_find_property(const struct device_node *np,
235 					 const char *name,
236 					 int *lenp);
237 extern int of_property_read_u8_array(const struct device_node *np,
238 			const char *propname, u8 *out_values, size_t sz);
239 extern int of_property_read_u16_array(const struct device_node *np,
240 			const char *propname, u16 *out_values, size_t sz);
241 extern int of_property_read_u32_array(const struct device_node *np,
242 				      const char *propname,
243 				      u32 *out_values,
244 				      size_t sz);
245 extern int of_property_read_u64(const struct device_node *np,
246 				const char *propname, u64 *out_value);
247 
248 extern int of_property_read_string(struct device_node *np,
249 				   const char *propname,
250 				   const char **out_string);
251 extern int of_property_read_string_index(struct device_node *np,
252 					 const char *propname,
253 					 int index, const char **output);
254 extern int of_property_match_string(struct device_node *np,
255 				    const char *propname,
256 				    const char *string);
257 extern int of_property_count_strings(struct device_node *np,
258 				     const char *propname);
259 extern int of_device_is_compatible(const struct device_node *device,
260 				   const char *);
261 extern int of_device_is_available(const struct device_node *device);
262 extern const void *of_get_property(const struct device_node *node,
263 				const char *name,
264 				int *lenp);
265 #define for_each_property_of_node(dn, pp) \
266 	for (pp = dn->properties; pp != NULL; pp = pp->next)
267 
268 extern int of_n_addr_cells(struct device_node *np);
269 extern int of_n_size_cells(struct device_node *np);
270 extern const struct of_device_id *of_match_node(
271 	const struct of_device_id *matches, const struct device_node *node);
272 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
273 extern struct device_node *of_parse_phandle(const struct device_node *np,
274 					    const char *phandle_name,
275 					    int index);
276 extern int of_parse_phandle_with_args(const struct device_node *np,
277 	const char *list_name, const char *cells_name, int index,
278 	struct of_phandle_args *out_args);
279 
280 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
281 extern int of_alias_get_id(struct device_node *np, const char *stem);
282 
283 extern int of_machine_is_compatible(const char *compat);
284 
285 extern int prom_add_property(struct device_node* np, struct property* prop);
286 extern int prom_remove_property(struct device_node *np, struct property *prop);
287 extern int prom_update_property(struct device_node *np,
288 				struct property *newprop);
289 
290 #if defined(CONFIG_OF_DYNAMIC)
291 /* For updating the device tree at runtime */
292 extern void of_attach_node(struct device_node *);
293 extern void of_detach_node(struct device_node *);
294 #endif
295 
296 #define of_match_ptr(_ptr)	(_ptr)
297 
298 /*
299  * struct property *prop;
300  * const __be32 *p;
301  * u32 u;
302  *
303  * of_property_for_each_u32(np, "propname", prop, p, u)
304  *         printk("U32 value: %x\n", u);
305  */
306 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
307 			       u32 *pu);
308 #define of_property_for_each_u32(np, propname, prop, p, u)	\
309 	for (prop = of_find_property(np, propname, NULL),	\
310 		p = of_prop_next_u32(prop, NULL, &u);		\
311 		p;						\
312 		p = of_prop_next_u32(prop, p, &u))
313 
314 /*
315  * struct property *prop;
316  * const char *s;
317  *
318  * of_property_for_each_string(np, "propname", prop, s)
319  *         printk("String value: %s\n", s);
320  */
321 const char *of_prop_next_string(struct property *prop, const char *cur);
322 #define of_property_for_each_string(np, propname, prop, s)	\
323 	for (prop = of_find_property(np, propname, NULL),	\
324 		s = of_prop_next_string(prop, NULL);		\
325 		s;						\
326 		s = of_prop_next_string(prop, s))
327 
328 #else /* CONFIG_OF */
329 
330 static inline const char* of_node_full_name(struct device_node *np)
331 {
332 	return "<no-node>";
333 }
334 
335 static inline struct device_node *of_find_node_by_name(struct device_node *from,
336 	const char *name)
337 {
338 	return NULL;
339 }
340 
341 static inline bool of_have_populated_dt(void)
342 {
343 	return false;
344 }
345 
346 #define for_each_child_of_node(parent, child) \
347 	while (0)
348 
349 static inline struct device_node *of_get_child_by_name(
350 					const struct device_node *node,
351 					const char *name)
352 {
353 	return NULL;
354 }
355 
356 static inline int of_get_child_count(const struct device_node *np)
357 {
358 	return 0;
359 }
360 
361 static inline int of_device_is_compatible(const struct device_node *device,
362 					  const char *name)
363 {
364 	return 0;
365 }
366 
367 static inline struct property *of_find_property(const struct device_node *np,
368 						const char *name,
369 						int *lenp)
370 {
371 	return NULL;
372 }
373 
374 static inline struct device_node *of_find_compatible_node(
375 						struct device_node *from,
376 						const char *type,
377 						const char *compat)
378 {
379 	return NULL;
380 }
381 
382 static inline int of_property_read_u8_array(const struct device_node *np,
383 			const char *propname, u8 *out_values, size_t sz)
384 {
385 	return -ENOSYS;
386 }
387 
388 static inline int of_property_read_u16_array(const struct device_node *np,
389 			const char *propname, u16 *out_values, size_t sz)
390 {
391 	return -ENOSYS;
392 }
393 
394 static inline int of_property_read_u32_array(const struct device_node *np,
395 					     const char *propname,
396 					     u32 *out_values, size_t sz)
397 {
398 	return -ENOSYS;
399 }
400 
401 static inline int of_property_read_string(struct device_node *np,
402 					  const char *propname,
403 					  const char **out_string)
404 {
405 	return -ENOSYS;
406 }
407 
408 static inline int of_property_read_string_index(struct device_node *np,
409 						const char *propname, int index,
410 						const char **out_string)
411 {
412 	return -ENOSYS;
413 }
414 
415 static inline int of_property_count_strings(struct device_node *np,
416 					    const char *propname)
417 {
418 	return -ENOSYS;
419 }
420 
421 static inline const void *of_get_property(const struct device_node *node,
422 				const char *name,
423 				int *lenp)
424 {
425 	return NULL;
426 }
427 
428 static inline int of_property_read_u64(const struct device_node *np,
429 				       const char *propname, u64 *out_value)
430 {
431 	return -ENOSYS;
432 }
433 
434 static inline int of_property_match_string(struct device_node *np,
435 					   const char *propname,
436 					   const char *string)
437 {
438 	return -ENOSYS;
439 }
440 
441 static inline struct device_node *of_parse_phandle(const struct device_node *np,
442 						   const char *phandle_name,
443 						   int index)
444 {
445 	return NULL;
446 }
447 
448 static inline int of_parse_phandle_with_args(struct device_node *np,
449 					     const char *list_name,
450 					     const char *cells_name,
451 					     int index,
452 					     struct of_phandle_args *out_args)
453 {
454 	return -ENOSYS;
455 }
456 
457 static inline int of_alias_get_id(struct device_node *np, const char *stem)
458 {
459 	return -ENOSYS;
460 }
461 
462 static inline int of_machine_is_compatible(const char *compat)
463 {
464 	return 0;
465 }
466 
467 #define of_match_ptr(_ptr)	NULL
468 #define of_match_node(_matches, _node)	NULL
469 #define of_property_for_each_u32(np, propname, prop, p, u) \
470 	while (0)
471 #define of_property_for_each_string(np, propname, prop, s) \
472 	while (0)
473 #endif /* CONFIG_OF */
474 
475 #ifndef of_node_to_nid
476 static inline int of_node_to_nid(struct device_node *np)
477 {
478 	return numa_node_id();
479 }
480 
481 #define of_node_to_nid of_node_to_nid
482 #endif
483 
484 /**
485  * of_property_read_bool - Findfrom a property
486  * @np:		device node from which the property value is to be read.
487  * @propname:	name of the property to be searched.
488  *
489  * Search for a property in a device node.
490  * Returns true if the property exist false otherwise.
491  */
492 static inline bool of_property_read_bool(const struct device_node *np,
493 					 const char *propname)
494 {
495 	struct property *prop = of_find_property(np, propname, NULL);
496 
497 	return prop ? true : false;
498 }
499 
500 static inline int of_property_read_u8(const struct device_node *np,
501 				       const char *propname,
502 				       u8 *out_value)
503 {
504 	return of_property_read_u8_array(np, propname, out_value, 1);
505 }
506 
507 static inline int of_property_read_u16(const struct device_node *np,
508 				       const char *propname,
509 				       u16 *out_value)
510 {
511 	return of_property_read_u16_array(np, propname, out_value, 1);
512 }
513 
514 static inline int of_property_read_u32(const struct device_node *np,
515 				       const char *propname,
516 				       u32 *out_value)
517 {
518 	return of_property_read_u32_array(np, propname, out_value, 1);
519 }
520 
521 #endif /* _LINUX_OF_H */
522