xref: /netbsd-src/sys/arch/arm/acpi/gtmr_acpi.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /* $NetBSD: gtmr_acpi.c,v 1.5 2021/08/07 16:18:42 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jared McNeill <jmcneill@invisible.ca>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: gtmr_acpi.c,v 1.5 2021/08/07 16:18:42 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 
40 #include <dev/acpi/acpireg.h>
41 #include <dev/acpi/acpivar.h>
42 
43 #include <arm/cortex/gtmr_intr.h>
44 #include <arm/cortex/mpcore_var.h>
45 #include <arm/cortex/gtmr_var.h>
46 
47 #include <dev/fdt/fdtvar.h>
48 #include <arm/fdt/arm_fdtvar.h>
49 
50 extern struct bus_space arm_generic_bs_tag;
51 
52 static int	gtmr_acpi_match(device_t, cfdata_t, void *);
53 static void	gtmr_acpi_attach(device_t, device_t, void *);
54 
55 static void	gtmr_acpi_cpu_hatch(void *, struct cpu_info *);
56 
57 CFATTACH_DECL_NEW(gtmr_acpi, 0, gtmr_acpi_match, gtmr_acpi_attach, NULL, NULL);
58 
59 static int
gtmr_acpi_match(device_t parent,cfdata_t cf,void * aux)60 gtmr_acpi_match(device_t parent, cfdata_t cf, void *aux)
61 {
62 	ACPI_TABLE_HEADER *hdrp = aux;
63 
64 	return memcmp(hdrp->Signature, ACPI_SIG_GTDT, ACPI_NAMESEG_SIZE) == 0;
65 }
66 
67 static void
gtmr_acpi_attach(device_t parent,device_t self,void * aux)68 gtmr_acpi_attach(device_t parent, device_t self, void *aux)
69 {
70 	ACPI_TABLE_GTDT *gtdt = aux;
71 	struct mpcore_attach_args mpcaa;
72 	void *ih;
73 
74 	const int irq = gtdt->VirtualTimerInterrupt;
75 	const int ipl = IPL_CLOCK;
76 	const int type = (gtdt->VirtualTimerFlags & ACPI_GTDT_INTERRUPT_MODE) ?
77 	    IST_EDGE : IST_LEVEL;
78 
79 	aprint_naive("\n");
80 	aprint_normal(": irq %d\n", irq);
81 
82 	ih = intr_establish_xname(irq, ipl, type | IST_MPSAFE, gtmr_intr, NULL, device_xname(self));
83 	if (ih == NULL) {
84 		aprint_error_dev(self, "couldn't install interrupt handler\n");
85 		return;
86 	}
87 
88 	memset(&mpcaa, 0, sizeof(mpcaa));
89 	mpcaa.mpcaa_name = "armgtmr";
90 	mpcaa.mpcaa_irq = -1;
91 	config_found(self, &mpcaa, NULL, CFARGS_NONE);
92 
93 	arm_fdt_cpu_hatch_register(self, gtmr_acpi_cpu_hatch);
94 	arm_fdt_timer_register(gtmr_cpu_initclocks);
95 }
96 
97 static void
gtmr_acpi_cpu_hatch(void * priv,struct cpu_info * ci)98 gtmr_acpi_cpu_hatch(void *priv, struct cpu_info *ci)
99 {
100 	gtmr_init_cpu_clock(ci);
101 }
102