1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2016-2021, Linaro Limited 4 */ 5 6 #ifndef KERNEL_DT_H 7 #define KERNEL_DT_H 8 9 #include <compiler.h> 10 #include <kernel/interrupt.h> 11 #include <kernel/panic.h> 12 #include <stdint.h> 13 #include <tee_api_types.h> 14 #include <types_ext.h> 15 #include <util.h> 16 17 /* 18 * Bitfield to reflect status and secure-status values ("okay", "disabled" 19 * or not present) 20 */ 21 #define DT_STATUS_DISABLED U(0) 22 #define DT_STATUS_OK_NSEC BIT(0) 23 #define DT_STATUS_OK_SEC BIT(1) 24 25 #define DT_INFO_INVALID_REG ((paddr_t)-1) 26 #define DT_INFO_INVALID_REG_SIZE ((size_t)-1) 27 #define DT_INFO_INVALID_CLOCK -1 28 #define DT_INFO_INVALID_RESET -1 29 #define DT_INFO_INVALID_INTERRUPT -1 30 31 /* 32 * @status: Bit mask for DT_STATUS_* 33 * @reg: Device register physical base address or DT_INFO_INVALID_REG 34 * @reg_size: Device register size or DT_INFO_INVALID_REG_SIZE 35 * @clock: Device identifier (positive value) or DT_INFO_INVALID_CLOCK 36 * @reset: Device reset identifier (positive value) or DT_INFO_INVALID_CLOCK 37 * @interrupt: Device interrupt identifier (positive value) or 38 * DT_INFO_INVALID_INTERRUPT 39 * @type: IRQ_TYPE_* value parsed from interrupts properties or IRQ_TYPE_NONE if 40 * not present 41 * @prio: interrupt priority parsed from interrupts properties or 0 if not 42 * present 43 */ 44 struct dt_node_info { 45 unsigned int status; 46 paddr_t reg; 47 size_t reg_size; 48 int clock; 49 int reset; 50 int interrupt; 51 uint32_t type; 52 uint32_t prio; 53 }; 54 55 #if defined(CFG_DT) 56 57 /* 58 * DT-aware drivers 59 */ 60 61 struct dt_device_match { 62 const char *compatible; 63 const void *compat_data; 64 }; 65 66 enum dt_driver_type { 67 DT_DRIVER_NOTYPE, 68 DT_DRIVER_UART, 69 DT_DRIVER_CLK, 70 }; 71 72 /* 73 * dt_driver_probe_func - Callback probe function for a driver. 74 * 75 * @fdt: FDT base address 76 * @nodeoffset: Offset of the node in the FDT 77 * @compat_data: Data registered for the compatible that probed the device 78 * 79 * Return TEE_SUCCESS on successful probe, 80 * TEE_ERROR_DEFER_DRIVER_INIT if probe must be deferred 81 * TEE_ERROR_ITEM_NOT_FOUND when no driver matched node's compatible string 82 * Any other TEE_ERROR_* compliant code. 83 */ 84 typedef TEE_Result (*dt_driver_probe_func)(const void *fdt, int nodeoffset, 85 const void *compat_data); 86 87 /* 88 * Driver instance registered to be probed on compatible node found in the DT. 89 * 90 * @name: Driver name 91 * @type: Drive type 92 * @match_table: Compatible matching identifiers, null terminated 93 * @driver: Driver private reference or NULL 94 * @probe: Probe callback (see dt_driver_probe_func) or NULL 95 */ 96 struct dt_driver { 97 const char *name; 98 enum dt_driver_type type; 99 const struct dt_device_match *match_table; /* null-terminated */ 100 const void *driver; 101 TEE_Result (*probe)(const void *fdt, int node, const void *compat_data); 102 }; 103 104 #define __dt_driver __section(".rodata.dtdrv" __SECTION_FLAGS_RODATA) 105 106 /* 107 * Find a driver that is suitable for the given DT node, that is, with 108 * a matching "compatible" property. 109 * 110 * @fdt: pointer to the device tree 111 * @offs: node offset 112 */ 113 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs); 114 115 const struct dt_driver *__dt_driver_start(void); 116 117 const struct dt_driver *__dt_driver_end(void); 118 119 /* 120 * Map a device into secure or non-secure memory and return the base VA and 121 * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or 122 * MEM_AREA_IO_NSEC, depending on the device status. 123 * If the mapping already exists, the function simply returns the @vbase and 124 * @size information. 125 * 126 * @offs is the offset of the node that describes the device in @fdt. 127 * @base receives the base virtual address corresponding to the base physical 128 * address of the "reg" property 129 * @size receives the size of the mapping 130 * 131 * Returns 0 on success or -1 in case of error. 132 */ 133 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size); 134 135 /* 136 * Check whether the node at @offs contains the property with propname or not. 137 * 138 * @offs is the offset of the node that describes the device in @fdt. 139 * @propname is the property that need to check 140 * 141 * Returns true on success or false if no propname. 142 */ 143 bool dt_have_prop(const void *fdt, int offs, const char *propname); 144 145 /* 146 * Modify or add "status" property to "disabled" 147 * 148 * @fdt reference to the Device Tree 149 * @node is the node offset to modify 150 * 151 * Returns 0 on success or -1 on failure 152 */ 153 int dt_disable_status(void *fdt, int node); 154 155 /* 156 * Force secure-status = "okay" and status="disabled" for the target node. 157 * 158 * @fdt reference to the Device Tree 159 * @node is the node offset to modify 160 * 161 * Returns 0 on success or -1 on failure 162 */ 163 int dt_enable_secure_status(void *fdt, int node); 164 165 /* 166 * FDT manipulation functions, not provided by <libfdt.h> 167 */ 168 169 /* 170 * Return the base address for the "reg" property of the specified node or 171 * (paddr_t)-1 in case of error 172 */ 173 paddr_t _fdt_reg_base_address(const void *fdt, int offs); 174 175 /* 176 * Return the reg size for the reg property of the specified node or -1 in case 177 * of error 178 */ 179 size_t _fdt_reg_size(const void *fdt, int offs); 180 181 /* 182 * Read the status and secure-status properties into a bitfield. 183 * @status is set to DT_STATUS_DISABLED or a combination of DT_STATUS_OK_NSEC 184 * and DT_STATUS_OK_SEC. 185 */ 186 int _fdt_get_status(const void *fdt, int offs); 187 188 /* 189 * fdt_fill_device_info - Get generic device info from a node 190 * 191 * This function fills the generic information from a given node. 192 * Currently supports a single base register, a single clock, 193 * a single reset ID line and a single interrupt ID. 194 * Default DT_INFO_* macros are used when the relate property is not found. 195 */ 196 void _fdt_fill_device_info(const void *fdt, struct dt_node_info *info, 197 int node); 198 199 #else /* !CFG_DT */ 200 201 static inline const struct dt_driver *__dt_driver_start(void) 202 { 203 return NULL; 204 } 205 206 static inline const struct dt_driver *__dt_driver_end(void) 207 { 208 return NULL; 209 } 210 211 static inline const struct dt_driver *dt_find_compatible_driver( 212 const void *fdt __unused, 213 int offs __unused) 214 { 215 return NULL; 216 } 217 218 static inline int dt_map_dev(const void *fdt __unused, int offs __unused, 219 vaddr_t *vbase __unused, size_t *size __unused) 220 { 221 return -1; 222 } 223 224 static inline paddr_t _fdt_reg_base_address(const void *fdt __unused, 225 int offs __unused) 226 { 227 return (paddr_t)-1; 228 } 229 230 static inline size_t _fdt_reg_size(const void *fdt __unused, 231 int offs __unused) 232 { 233 return (size_t)-1; 234 } 235 236 static inline int _fdt_get_status(const void *fdt __unused, int offs __unused) 237 { 238 return -1; 239 } 240 241 __noreturn 242 static inline void _fdt_fill_device_info(const void *fdt __unused, 243 struct dt_node_info *info __unused, 244 int node __unused) 245 { 246 panic(); 247 } 248 #endif /* !CFG_DT */ 249 250 #define for_each_dt_driver(drv) \ 251 for (drv = __dt_driver_start(); drv < __dt_driver_end(); drv++) 252 253 #endif /* KERNEL_DT_H */ 254