1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2016, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <kernel/dt.h> 8 #include <kernel/interrupt.h> 9 #include <kernel/linker.h> 10 #include <libfdt.h> 11 #include <mm/core_memprot.h> 12 #include <mm/core_mmu.h> 13 #include <string.h> 14 #include <trace.h> 15 16 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs) 17 { 18 const struct dt_device_match *dm; 19 const struct dt_driver *drv; 20 21 for_each_dt_driver(drv) { 22 for (dm = drv->match_table; dm; dm++) { 23 if (!dm->compatible) { 24 break; 25 } 26 if (!fdt_node_check_compatible(fdt, offs, 27 dm->compatible)) { 28 return drv; 29 } 30 } 31 } 32 33 return NULL; 34 } 35 36 const struct dt_driver *__dt_driver_start(void) 37 { 38 return &__rodata_dtdrv_start; 39 } 40 41 const struct dt_driver *__dt_driver_end(void) 42 { 43 return &__rodata_dtdrv_end; 44 } 45 46 bool dt_have_prop(const void *fdt, int offs, const char *propname) 47 { 48 const void *prop; 49 50 prop = fdt_getprop(fdt, offs, propname, NULL); 51 52 return prop; 53 } 54 55 int dt_disable_status(void *fdt, int node) 56 { 57 const char *prop = NULL; 58 int len = 0; 59 60 prop = fdt_getprop(fdt, node, "status", &len); 61 if (!prop) { 62 if (fdt_setprop_string(fdt, node, "status", "disabled")) 63 return -1; 64 } else { 65 /* 66 * Status is there, modify it. 67 * Ask to set "disabled" value to the property. The value 68 * will be automatically truncated with "len" size by the 69 * fdt_setprop_inplace function. 70 * Setting a value different from "ok" or "okay" will disable 71 * the property. 72 * Setting a truncated value of "disabled" with the original 73 * property "len" is preferred to not increase the DT size and 74 * losing time in recalculating the overall DT offsets. 75 * If original length of the status property is larger than 76 * "disabled", the property will start with "disabled" and be 77 * completed with the rest of the original property. 78 */ 79 if (fdt_setprop_inplace(fdt, node, "status", "disabled", len)) 80 return -1; 81 } 82 83 return 0; 84 } 85 86 int dt_enable_secure_status(void *fdt, int node) 87 { 88 if (dt_disable_status(fdt, node)) { 89 EMSG("Unable to disable Normal Status"); 90 return -1; 91 } 92 93 if (fdt_setprop_string(fdt, node, "secure-status", "okay")) 94 return -1; 95 96 return 0; 97 } 98 99 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size) 100 { 101 enum teecore_memtypes mtype; 102 paddr_t pbase; 103 vaddr_t vbase; 104 ssize_t sz; 105 int st; 106 107 assert(cpu_mmu_enabled()); 108 109 st = _fdt_get_status(fdt, offs); 110 if (st == DT_STATUS_DISABLED) 111 return -1; 112 113 pbase = _fdt_reg_base_address(fdt, offs); 114 if (pbase == DT_INFO_INVALID_REG) 115 return -1; 116 sz = _fdt_reg_size(fdt, offs); 117 if (sz < 0) 118 return -1; 119 120 if ((st & DT_STATUS_OK_SEC) && !(st & DT_STATUS_OK_NSEC)) 121 mtype = MEM_AREA_IO_SEC; 122 else 123 mtype = MEM_AREA_IO_NSEC; 124 125 /* Check if we have a mapping, create one if needed */ 126 if (!core_mmu_add_mapping(mtype, pbase, sz)) { 127 EMSG("Failed to map %zu bytes at PA 0x%"PRIxPA, 128 (size_t)sz, pbase); 129 return -1; 130 } 131 vbase = (vaddr_t)phys_to_virt(pbase, mtype); 132 if (!vbase) { 133 EMSG("Failed to get VA for PA 0x%"PRIxPA, pbase); 134 return -1; 135 } 136 137 *base = vbase; 138 *size = sz; 139 return 0; 140 } 141 142 /* Read a physical address (n=1 or 2 cells) */ 143 static paddr_t _fdt_read_paddr(const uint32_t *cell, int n) 144 { 145 paddr_t addr; 146 147 if (n < 1 || n > 2) 148 goto bad; 149 150 addr = fdt32_to_cpu(*cell); 151 cell++; 152 if (n == 2) { 153 #ifdef ARM32 154 if (addr) { 155 /* High order 32 bits can't be nonzero */ 156 goto bad; 157 } 158 addr = fdt32_to_cpu(*cell); 159 #else 160 addr = (addr << 32) | fdt32_to_cpu(*cell); 161 #endif 162 } 163 164 if (!addr) 165 goto bad; 166 167 return addr; 168 bad: 169 return DT_INFO_INVALID_REG; 170 171 } 172 173 paddr_t _fdt_reg_base_address(const void *fdt, int offs) 174 { 175 const void *reg; 176 int ncells; 177 int len; 178 int parent; 179 180 parent = fdt_parent_offset(fdt, offs); 181 if (parent < 0) 182 return DT_INFO_INVALID_REG; 183 184 reg = fdt_getprop(fdt, offs, "reg", &len); 185 if (!reg) 186 return DT_INFO_INVALID_REG; 187 188 ncells = fdt_address_cells(fdt, parent); 189 if (ncells < 0) 190 return DT_INFO_INVALID_REG; 191 192 return _fdt_read_paddr(reg, ncells); 193 } 194 195 ssize_t _fdt_reg_size(const void *fdt, int offs) 196 { 197 const uint32_t *reg; 198 uint32_t sz; 199 int n; 200 int len; 201 int parent; 202 203 parent = fdt_parent_offset(fdt, offs); 204 if (parent < 0) 205 return DT_INFO_INVALID_REG_SIZE; 206 207 reg = (const uint32_t *)fdt_getprop(fdt, offs, "reg", &len); 208 if (!reg) 209 return -1; 210 211 n = fdt_address_cells(fdt, parent); 212 if (n < 1 || n > 2) 213 return -1; 214 215 reg += n; 216 217 n = fdt_size_cells(fdt, parent); 218 if (n < 1 || n > 2) 219 return -1; 220 221 sz = fdt32_to_cpu(*reg); 222 if (n == 2) { 223 if (sz) 224 return -1; 225 reg++; 226 sz = fdt32_to_cpu(*reg); 227 } 228 229 return sz; 230 } 231 232 static bool is_okay(const char *st, int len) 233 { 234 return !strncmp(st, "ok", len) || !strncmp(st, "okay", len); 235 } 236 237 int _fdt_get_status(const void *fdt, int offs) 238 { 239 const char *prop; 240 int st = 0; 241 int len; 242 243 prop = fdt_getprop(fdt, offs, "status", &len); 244 if (!prop || is_okay(prop, len)) { 245 /* If status is not specified, it defaults to "okay" */ 246 st |= DT_STATUS_OK_NSEC; 247 } 248 249 prop = fdt_getprop(fdt, offs, "secure-status", &len); 250 if (!prop) { 251 /* 252 * When secure-status is not specified it defaults to the same 253 * value as status 254 */ 255 if (st & DT_STATUS_OK_NSEC) 256 st |= DT_STATUS_OK_SEC; 257 } else { 258 if (is_okay(prop, len)) 259 st |= DT_STATUS_OK_SEC; 260 } 261 262 return st; 263 } 264 265 void _fdt_fill_device_info(void *fdt, struct dt_node_info *info, int offs) 266 { 267 struct dt_node_info dinfo = { 268 .reg = DT_INFO_INVALID_REG, 269 .clock = DT_INFO_INVALID_CLOCK, 270 .reset = DT_INFO_INVALID_RESET, 271 .interrupt = DT_INFO_INVALID_INTERRUPT, 272 }; 273 const fdt32_t *cuint; 274 275 dinfo.reg = _fdt_reg_base_address(fdt, offs); 276 277 cuint = fdt_getprop(fdt, offs, "clocks", NULL); 278 if (cuint) { 279 cuint++; 280 dinfo.clock = (int)fdt32_to_cpu(*cuint); 281 } 282 283 cuint = fdt_getprop(fdt, offs, "resets", NULL); 284 if (cuint) { 285 cuint++; 286 dinfo.reset = (int)fdt32_to_cpu(*cuint); 287 } 288 289 dinfo.interrupt = dt_get_irq(fdt, offs); 290 291 dinfo.status = _fdt_get_status(fdt, offs); 292 293 *info = dinfo; 294 } 295