1 /* $NetBSD: arm_fdt.c,v 1.24 2025/09/07 15:01:59 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include "opt_arm_timer.h" 30 #include "opt_efi.h" 31 #include "opt_modular.h" 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: arm_fdt.c,v 1.24 2025/09/07 15:01:59 thorpej Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/cpu.h> 39 #include <sys/device.h> 40 #include <sys/kmem.h> 41 #include <sys/bus.h> 42 #include <sys/module.h> 43 44 #include <uvm/uvm_extern.h> 45 46 #include <dev/fdt/fdtvar.h> 47 #include <dev/fdt/fdt_platform.h> 48 49 #include <dev/ofw/openfirm.h> 50 51 #include <arm/fdt/arm_fdtvar.h> 52 53 #include <arm/locore.h> 54 55 #ifdef EFI_RUNTIME 56 #include <arm/arm/efi_runtime.h> 57 #include <dev/clock_subr.h> 58 #endif 59 60 static int arm_fdt_match(device_t, cfdata_t, void *); 61 static void arm_fdt_attach(device_t, device_t, void *); 62 63 static void arm_fdt_irq_default_handler(void *); 64 static void arm_fdt_fiq_default_handler(void *); 65 66 #ifdef EFI_RUNTIME 67 static void arm_fdt_efi_init(device_t); 68 static int arm_fdt_efi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *); 69 static int arm_fdt_efi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *); 70 71 static struct todr_chip_handle efi_todr; 72 73 static const char * const ignore_efi_runtime_models[] = { 74 /* RTC calls do not work with current firmware. */ 75 "Radxa Computer (Shenzhen) Co., Ltd. Radxa Orion O6", 76 }; 77 #endif 78 79 CFATTACH_DECL_NEW(arm_fdt, 0, 80 arm_fdt_match, arm_fdt_attach, NULL, NULL); 81 82 struct arm_fdt_cpu_hatch_cb { 83 TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next; 84 void (*cb)(void *, struct cpu_info *); 85 void *priv; 86 }; 87 88 static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs = 89 TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs); 90 91 static void (*_arm_fdt_irq_handler)(void *) = arm_fdt_irq_default_handler; 92 static void (*_arm_fdt_fiq_handler)(void *) = arm_fdt_fiq_default_handler; 93 static void (*_arm_fdt_timer_init)(void) = NULL; 94 95 int 96 arm_fdt_match(device_t parent, cfdata_t cf, void *aux) 97 { 98 return 1; 99 } 100 101 void 102 arm_fdt_attach(device_t parent, device_t self, void *aux) 103 { 104 const struct fdt_platform *plat = fdt_platform_find(); 105 struct fdt_attach_args faa; 106 107 aprint_naive("\n"); 108 aprint_normal("\n"); 109 110 DISABLE_INTERRUPT(); 111 112 #ifdef EFI_RUNTIME 113 arm_fdt_efi_init(self); 114 #endif 115 116 plat->fp_init_attach_args(&faa); 117 faa.faa_name = ""; 118 faa.faa_phandle = OF_peer(0); 119 120 config_found(self, &faa, NULL, CFARGS_NONE); 121 } 122 void 123 arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *)) 124 { 125 struct arm_fdt_cpu_hatch_cb *c; 126 127 c = kmem_alloc(sizeof(*c), KM_SLEEP); 128 c->priv = priv; 129 c->cb = cb; 130 TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next); 131 } 132 133 void 134 arm_fdt_cpu_hatch(struct cpu_info *ci) 135 { 136 struct arm_fdt_cpu_hatch_cb *c; 137 138 TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next) 139 c->cb(c->priv, ci); 140 } 141 142 static void 143 arm_fdt_irq_default_handler(void *frame) 144 { 145 panic("No IRQ handler installed"); 146 } 147 148 static void 149 arm_fdt_fiq_default_handler(void *frame) 150 { 151 panic("No FIQ handler installed"); 152 } 153 154 void 155 arm_fdt_irq_set_handler(void (*irq_handler)(void *)) 156 { 157 KASSERT(_arm_fdt_irq_handler == arm_fdt_irq_default_handler); 158 _arm_fdt_irq_handler = irq_handler; 159 } 160 161 void 162 arm_fdt_fiq_set_handler(void (*fiq_handler)(void *)) 163 { 164 KASSERT(_arm_fdt_fiq_handler == arm_fdt_fiq_default_handler); 165 _arm_fdt_fiq_handler = fiq_handler; 166 } 167 168 void 169 arm_fdt_irq_handler(void *tf) 170 { 171 _arm_fdt_irq_handler(tf); 172 } 173 174 void 175 arm_fdt_fiq_handler(void *tf) 176 { 177 _arm_fdt_fiq_handler(tf); 178 } 179 180 void 181 arm_fdt_timer_register(void (*timerfn)(void)) 182 { 183 if (_arm_fdt_timer_init != NULL) { 184 #ifdef DIAGNOSTIC 185 aprint_verbose("%s: timer already registered\n", __func__); 186 #endif 187 return; 188 } 189 _arm_fdt_timer_init = timerfn; 190 } 191 192 #ifdef __HAVE_GENERIC_CPU_INITCLOCKS 193 void 194 cpu_initclocks(void) 195 { 196 if (_arm_fdt_timer_init == NULL) 197 panic("cpu_initclocks: no timer registered"); 198 _arm_fdt_timer_init(); 199 ENABLE_INTERRUPT(); 200 } 201 #endif 202 203 void 204 arm_fdt_module_init(void) 205 { 206 #ifdef MODULAR 207 const int chosen = OF_finddevice("/chosen"); 208 const char *module_name; 209 const uint64_t *data; 210 u_int index; 211 paddr_t pa; 212 vaddr_t va; 213 int len; 214 215 if (chosen == -1) 216 return; 217 218 data = fdtbus_get_prop(chosen, "netbsd,modules", &len); 219 if (data == NULL) 220 return; 221 222 for (index = 0; index < len / 16; index++, data += 2) { 223 module_name = fdtbus_get_string_index(chosen, 224 "netbsd,module-names", index); 225 if (module_name == NULL) 226 break; 227 228 const paddr_t startpa = (paddr_t)be64dec(data + 0); 229 const size_t size = (size_t)be64dec(data + 1); 230 const paddr_t endpa = round_page(startpa + size); 231 232 const vaddr_t startva = uvm_km_alloc(kernel_map, endpa - startpa, 233 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT); 234 if (startva == 0) { 235 printf("ERROR: Cannot allocate VA for module %s\n", 236 module_name); 237 continue; 238 } 239 240 for (pa = startpa, va = startva; 241 pa < endpa; 242 pa += PAGE_SIZE, va += PAGE_SIZE) { 243 pmap_kenter_pa(va, pa, VM_PROT_ALL, 0); 244 } 245 pmap_update(pmap_kernel()); 246 247 module_prime(module_name, (void *)(uintptr_t)startva, size); 248 } 249 #endif /* !MODULAR */ 250 } 251 252 #ifdef EFI_RUNTIME 253 static bool 254 arm_fdi_efi_ignored(void) 255 { 256 const int phandle = OF_peer(0); 257 const char *descr; 258 u_int n; 259 260 descr = fdtbus_get_string(phandle, "model"); 261 if (descr == NULL) { 262 return false; 263 } 264 265 for (n = 0; n < __arraycount(ignore_efi_runtime_models); n++) { 266 if (strcmp(descr, ignore_efi_runtime_models[n]) == 0) { 267 return true; 268 } 269 } 270 271 return false; 272 } 273 274 static void 275 arm_fdt_efi_init(device_t dev) 276 { 277 uint64_t efi_system_table; 278 struct efi_tm tm; 279 int error; 280 281 const int chosen = OF_finddevice("/chosen"); 282 if (chosen < 0) 283 return; 284 285 if (arm_fdi_efi_ignored()) { 286 aprint_debug_dev(dev, "EFI runtime services ignored on this platform\n"); 287 return; 288 } 289 290 if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0) 291 return; 292 293 error = arm_efirt_init(efi_system_table); 294 if (error) 295 return; 296 297 aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table); 298 299 if (arm_efirt_gettime(&tm, NULL) == 0) { 300 aprint_normal_dev(dev, "using EFI runtime services for RTC\n"); 301 efi_todr.todr_gettime_ymdhms = arm_fdt_efi_rtc_gettime; 302 efi_todr.todr_settime_ymdhms = arm_fdt_efi_rtc_settime; 303 todr_attach(&efi_todr); 304 } 305 } 306 307 static int 308 arm_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt) 309 { 310 struct efi_tm tm; 311 efi_status status; 312 313 status = arm_efirt_gettime(&tm, NULL); 314 if (status != 0) 315 return EIO; 316 317 dt->dt_year = tm.tm_year; 318 dt->dt_mon = tm.tm_mon; 319 dt->dt_day = tm.tm_mday; 320 dt->dt_wday = 0; 321 dt->dt_hour = tm.tm_hour; 322 dt->dt_min = tm.tm_min; 323 dt->dt_sec = tm.tm_sec; 324 325 return 0; 326 } 327 328 static int 329 arm_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt) 330 { 331 struct efi_tm tm; 332 efi_status status; 333 334 memset(&tm, 0, sizeof(tm)); 335 tm.tm_year = dt->dt_year; 336 tm.tm_mon = dt->dt_mon; 337 tm.tm_mday = dt->dt_day; 338 tm.tm_hour = dt->dt_hour; 339 tm.tm_min = dt->dt_min; 340 tm.tm_sec = dt->dt_sec; 341 342 status = arm_efirt_settime(&tm); 343 if (status != 0) 344 return EIO; 345 346 return 0; 347 } 348 #endif 349