1*725c469aSjmcneill /* $NetBSD: gtmr_fdt.c,v 1.12 2021/11/12 21:59:05 jmcneill Exp $ */
25ec28b37Sjmcneill
35ec28b37Sjmcneill /*-
45ec28b37Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
55ec28b37Sjmcneill * All rights reserved.
65ec28b37Sjmcneill *
75ec28b37Sjmcneill * Redistribution and use in source and binary forms, with or without
85ec28b37Sjmcneill * modification, are permitted provided that the following conditions
95ec28b37Sjmcneill * are met:
105ec28b37Sjmcneill * 1. Redistributions of source code must retain the above copyright
115ec28b37Sjmcneill * notice, this list of conditions and the following disclaimer.
125ec28b37Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
135ec28b37Sjmcneill * notice, this list of conditions and the following disclaimer in the
145ec28b37Sjmcneill * documentation and/or other materials provided with the distribution.
155ec28b37Sjmcneill *
165ec28b37Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175ec28b37Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185ec28b37Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
195ec28b37Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
205ec28b37Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
215ec28b37Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
225ec28b37Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
235ec28b37Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
245ec28b37Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
255ec28b37Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265ec28b37Sjmcneill * SUCH DAMAGE.
275ec28b37Sjmcneill */
285ec28b37Sjmcneill
295ec28b37Sjmcneill #include <sys/cdefs.h>
30*725c469aSjmcneill __KERNEL_RCSID(0, "$NetBSD: gtmr_fdt.c,v 1.12 2021/11/12 21:59:05 jmcneill Exp $");
315ec28b37Sjmcneill
325ec28b37Sjmcneill #include <sys/param.h>
335ec28b37Sjmcneill #include <sys/bus.h>
345ec28b37Sjmcneill #include <sys/device.h>
355ec28b37Sjmcneill #include <sys/intr.h>
365ec28b37Sjmcneill #include <sys/systm.h>
375ec28b37Sjmcneill #include <sys/kernel.h>
385ec28b37Sjmcneill #include <sys/kmem.h>
395ec28b37Sjmcneill
40c6cfeef3Sjmcneill #include <arm/cortex/gtmr_intr.h>
415ec28b37Sjmcneill #include <arm/cortex/mpcore_var.h>
422fdae95fSjmcneill #include <arm/cortex/gtmr_var.h>
435ec28b37Sjmcneill
445ec28b37Sjmcneill #include <dev/fdt/fdtvar.h>
452fdae95fSjmcneill #include <arm/fdt/arm_fdtvar.h>
465ec28b37Sjmcneill
47*725c469aSjmcneill #if defined(__arm__)
48*725c469aSjmcneill #include <arm/armreg.h>
49*725c469aSjmcneill #endif
50*725c469aSjmcneill
515ec28b37Sjmcneill static int gtmr_fdt_match(device_t, cfdata_t, void *);
525ec28b37Sjmcneill static void gtmr_fdt_attach(device_t, device_t, void *);
535ec28b37Sjmcneill
542fdae95fSjmcneill static void gtmr_fdt_cpu_hatch(void *, struct cpu_info *);
552fdae95fSjmcneill
565ec28b37Sjmcneill CFATTACH_DECL_NEW(gtmr_fdt, 0, gtmr_fdt_match, gtmr_fdt_attach, NULL, NULL);
575ec28b37Sjmcneill
58d8786e96Sskrll /* The virtual timer list entry */
59f0ae4fbdSskrll #define GTMR_VTIMER 2
60f0ae4fbdSskrll
616e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
626e54367aSthorpej { .compat = "arm,armv7-timer" },
636e54367aSthorpej { .compat = "arm,armv8-timer" },
646e54367aSthorpej DEVICE_COMPAT_EOL
656e54367aSthorpej };
666e54367aSthorpej
675ec28b37Sjmcneill static int
gtmr_fdt_match(device_t parent,cfdata_t cf,void * aux)685ec28b37Sjmcneill gtmr_fdt_match(device_t parent, cfdata_t cf, void *aux)
695ec28b37Sjmcneill {
705ec28b37Sjmcneill struct fdt_attach_args * const faa = aux;
715ec28b37Sjmcneill
726e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
735ec28b37Sjmcneill }
745ec28b37Sjmcneill
755ec28b37Sjmcneill static void
gtmr_fdt_attach(device_t parent,device_t self,void * aux)765ec28b37Sjmcneill gtmr_fdt_attach(device_t parent, device_t self, void *aux)
775ec28b37Sjmcneill {
785ec28b37Sjmcneill aprint_naive("\n");
795ec28b37Sjmcneill aprint_normal(": Generic Timer\n");
805ec28b37Sjmcneill
815ec28b37Sjmcneill struct mpcore_attach_args mpcaa = {
825ec28b37Sjmcneill .mpcaa_name = "armgtmr",
83f0ae4fbdSskrll .mpcaa_irq = -1 /* setup handler locally */
845ec28b37Sjmcneill };
85f0ae4fbdSskrll struct fdt_attach_args * const faa = aux;
86f0ae4fbdSskrll const int phandle = faa->faa_phandle;
87f0ae4fbdSskrll
88f0ae4fbdSskrll char intrstr[128];
89f0ae4fbdSskrll if (!fdtbus_intr_str(phandle, GTMR_VTIMER, intrstr, sizeof(intrstr))) {
90f0ae4fbdSskrll aprint_error(": failed to decode interrupt\n");
91f0ae4fbdSskrll return;
92f0ae4fbdSskrll }
93f0ae4fbdSskrll
9464e248edSryo void *ih = fdtbus_intr_establish_xname(phandle, GTMR_VTIMER, IPL_CLOCK,
9564e248edSryo FDT_INTR_MPSAFE, gtmr_intr, NULL, device_xname(self));
96f0ae4fbdSskrll if (ih == NULL) {
97f0ae4fbdSskrll aprint_error_dev(self, "couldn't install interrupt handler\n");
98f0ae4fbdSskrll return;
99f0ae4fbdSskrll }
100f0ae4fbdSskrll aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1015ec28b37Sjmcneill
102*725c469aSjmcneill #if defined(__arm__)
103*725c469aSjmcneill /*
104*725c469aSjmcneill * If arm,cpu-registers-not-fw-configured is present, we need
105*725c469aSjmcneill * to initialize cntfrq from the clock-frequency property. Only
106*725c469aSjmcneill * applicable on Armv7.
107*725c469aSjmcneill */
108*725c469aSjmcneill if (of_hasprop(phandle, "arm,cpu-registers-not-fw-configured")) {
109*725c469aSjmcneill uint32_t freq;
110*725c469aSjmcneill
111*725c469aSjmcneill if (of_getprop_uint32(phandle, "clock-frequency", &freq) == 0) {
112*725c469aSjmcneill armreg_cnt_frq_write(freq);
113*725c469aSjmcneill prop_dictionary_set_bool(device_properties(self),
114*725c469aSjmcneill "arm,cpu-registers-not-fw-configured", true);
115*725c469aSjmcneill }
116*725c469aSjmcneill }
117*725c469aSjmcneill #endif
118*725c469aSjmcneill
119c7fb772bSthorpej config_found(self, &mpcaa, NULL, CFARGS_NONE);
1202fdae95fSjmcneill
1212fdae95fSjmcneill arm_fdt_cpu_hatch_register(self, gtmr_fdt_cpu_hatch);
1228ae98764Sjmcneill arm_fdt_timer_register(gtmr_cpu_initclocks);
1232fdae95fSjmcneill }
1242fdae95fSjmcneill
1252fdae95fSjmcneill static void
gtmr_fdt_cpu_hatch(void * priv,struct cpu_info * ci)1262fdae95fSjmcneill gtmr_fdt_cpu_hatch(void *priv, struct cpu_info *ci)
1272fdae95fSjmcneill {
1282fdae95fSjmcneill gtmr_init_cpu_clock(ci);
1295ec28b37Sjmcneill }
130