xref: /optee_os/core/include/kernel/dt.h (revision e84e1feccbdbd9deae5ad2dea921f4f624e8ad6d)
1 /*
2  * Copyright (c) 2016, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef KERNEL_DT_H
29 #define KERNEL_DT_H
30 
31 #include <compiler.h>
32 #include <stdint.h>
33 #include <types_ext.h>
34 #include <util.h>
35 
36 /*
37  * Bitfield to reflect status and secure-status values ("ok", "disabled" or not
38  * present)
39  */
40 #define DT_STATUS_DISABLED 0
41 #define DT_STATUS_OK_NSEC  BIT(0)
42 #define DT_STATUS_OK_SEC   BIT(1)
43 
44 #if defined(CFG_DT)
45 
46 /*
47  * DT-aware drivers
48  */
49 
50 struct dt_device_match {
51 	const char *compatible;
52 };
53 
54 struct dt_driver {
55 	const char *name;
56 	const struct dt_device_match *match_table; /* null-terminated */
57 	const void *driver;
58 };
59 
60 #define __dt_driver __section(".rodata.dtdrv")
61 
62 /*
63  * Find a driver that is suitable for the given DT node, that is, with
64  * a matching "compatible" property.
65  *
66  * @fdt: pointer to the device tree
67  * @offs: node offset
68  */
69 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs);
70 
71 const struct dt_driver *__dt_driver_start(void);
72 
73 const struct dt_driver *__dt_driver_end(void);
74 
75 /*
76  * Map a device into secure or non-secure memory and return the base VA and
77  * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or
78  * MEM_AREA_IO_NSEC, depending on the device status.
79  * If the mapping already exists, the function simply returns the @vbase and
80  * @size information.
81  *
82  * @offs is the offset of the node that describes the device in @fdt.
83  * @base receives the base virtual address corresponding to the base physical
84  * address of the "reg" property
85  * @size receives the size of the mapping
86  *
87  * Returns 0 on success or -1 in case of error.
88  */
89 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size);
90 
91 /*
92  * FDT manipulation functions, not provided by <libfdt.h>
93  */
94 
95 /*
96  * Return the base address for the "reg" property of the specified node or
97  * (paddr_t)-1 in case of error
98  */
99 paddr_t _fdt_reg_base_address(const void *fdt, int offs);
100 
101 /*
102  * Return the reg size for the reg property of the specified node or -1 in case
103  * of error
104  */
105 ssize_t _fdt_reg_size(const void *fdt, int offs);
106 
107 /*
108  * Read the status and secure-status properties into a bitfield.
109  * @status is set to DT_STATUS_DISABLED or a combination of DT_STATUS_OK_NSEC
110  * and DT_STATUS_OK_SEC
111  * Returns 0 on success or -1 in case of error.
112  */
113 int _fdt_get_status(const void *fdt, int offs);
114 
115 #else /* !CFG_DT */
116 
117 static inline const struct dt_driver *__dt_driver_start(void)
118 {
119 	return NULL;
120 }
121 
122 static inline const struct dt_driver *__dt_driver_end(void)
123 {
124 	return NULL;
125 }
126 
127 static inline const struct dt_driver *dt_find_compatible_driver(
128 					const void *fdt __unused,
129 					int offs __unused)
130 {
131 	return NULL;
132 }
133 
134 static inline int dt_map_dev(const void *fdt __unused, int offs __unused,
135 			     vaddr_t *vbase __unused, size_t *size __unused)
136 {
137 	return -1;
138 }
139 
140 static inline paddr_t _fdt_reg_base_address(const void *fdt __unused,
141 					    int offs __unused)
142 {
143 	return (paddr_t)-1;
144 }
145 
146 static inline ssize_t _fdt_reg_size(const void *fdt __unused,
147 				    int offs __unused)
148 {
149 	return -1;
150 }
151 
152 static inline int _fdt_get_status(const void *fdt __unused, int offs __unused)
153 {
154 	return -1;
155 }
156 
157 #endif /* !CFG_DT */
158 
159 #define for_each_dt_driver(drv) \
160 	for (drv = __dt_driver_start(); drv < __dt_driver_end(); drv++)
161 
162 #endif /* KERNEL_DT_H */
163