xref: /netbsd-src/sys/arch/arm/acpi/gtmr_acpi.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /* $NetBSD: gtmr_acpi.c,v 1.5 2021/08/07 16:18:42 thorpej Exp $ */
297d57f80Sjmcneill 
397d57f80Sjmcneill /*-
497d57f80Sjmcneill  * Copyright (c) 2018 The NetBSD Foundation, Inc.
597d57f80Sjmcneill  * All rights reserved.
697d57f80Sjmcneill  *
797d57f80Sjmcneill  * This code is derived from software contributed to The NetBSD Foundation
897d57f80Sjmcneill  * by Jared McNeill <jmcneill@invisible.ca>.
997d57f80Sjmcneill  *
1097d57f80Sjmcneill  * Redistribution and use in source and binary forms, with or without
1197d57f80Sjmcneill  * modification, are permitted provided that the following conditions
1297d57f80Sjmcneill  * are met:
1397d57f80Sjmcneill  * 1. Redistributions of source code must retain the above copyright
1497d57f80Sjmcneill  *    notice, this list of conditions and the following disclaimer.
1597d57f80Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
1697d57f80Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
1797d57f80Sjmcneill  *    documentation and/or other materials provided with the distribution.
1897d57f80Sjmcneill  *
1997d57f80Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2097d57f80Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2197d57f80Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2297d57f80Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2397d57f80Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2497d57f80Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2597d57f80Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2697d57f80Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2797d57f80Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2897d57f80Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2997d57f80Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
3097d57f80Sjmcneill  */
3197d57f80Sjmcneill 
3297d57f80Sjmcneill #include <sys/cdefs.h>
33*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: gtmr_acpi.c,v 1.5 2021/08/07 16:18:42 thorpej Exp $");
3497d57f80Sjmcneill 
3597d57f80Sjmcneill #include <sys/param.h>
3697d57f80Sjmcneill #include <sys/bus.h>
3797d57f80Sjmcneill #include <sys/cpu.h>
3897d57f80Sjmcneill #include <sys/device.h>
3997d57f80Sjmcneill 
4097d57f80Sjmcneill #include <dev/acpi/acpireg.h>
4197d57f80Sjmcneill #include <dev/acpi/acpivar.h>
4297d57f80Sjmcneill 
4397d57f80Sjmcneill #include <arm/cortex/gtmr_intr.h>
4497d57f80Sjmcneill #include <arm/cortex/mpcore_var.h>
4597d57f80Sjmcneill #include <arm/cortex/gtmr_var.h>
4697d57f80Sjmcneill 
4797d57f80Sjmcneill #include <dev/fdt/fdtvar.h>
4897d57f80Sjmcneill #include <arm/fdt/arm_fdtvar.h>
4997d57f80Sjmcneill 
5097d57f80Sjmcneill extern struct bus_space arm_generic_bs_tag;
5197d57f80Sjmcneill 
5297d57f80Sjmcneill static int	gtmr_acpi_match(device_t, cfdata_t, void *);
5397d57f80Sjmcneill static void	gtmr_acpi_attach(device_t, device_t, void *);
5497d57f80Sjmcneill 
5597d57f80Sjmcneill static void	gtmr_acpi_cpu_hatch(void *, struct cpu_info *);
5697d57f80Sjmcneill 
5797d57f80Sjmcneill CFATTACH_DECL_NEW(gtmr_acpi, 0, gtmr_acpi_match, gtmr_acpi_attach, NULL, NULL);
5897d57f80Sjmcneill 
5997d57f80Sjmcneill static int
gtmr_acpi_match(device_t parent,cfdata_t cf,void * aux)6097d57f80Sjmcneill gtmr_acpi_match(device_t parent, cfdata_t cf, void *aux)
6197d57f80Sjmcneill {
6297d57f80Sjmcneill 	ACPI_TABLE_HEADER *hdrp = aux;
6397d57f80Sjmcneill 
64f1eac07bSchristos 	return memcmp(hdrp->Signature, ACPI_SIG_GTDT, ACPI_NAMESEG_SIZE) == 0;
6597d57f80Sjmcneill }
6697d57f80Sjmcneill 
6797d57f80Sjmcneill static void
gtmr_acpi_attach(device_t parent,device_t self,void * aux)6897d57f80Sjmcneill gtmr_acpi_attach(device_t parent, device_t self, void *aux)
6997d57f80Sjmcneill {
7097d57f80Sjmcneill 	ACPI_TABLE_GTDT *gtdt = aux;
7197d57f80Sjmcneill 	struct mpcore_attach_args mpcaa;
7297d57f80Sjmcneill 	void *ih;
7397d57f80Sjmcneill 
7497d57f80Sjmcneill 	const int irq = gtdt->VirtualTimerInterrupt;
7597d57f80Sjmcneill 	const int ipl = IPL_CLOCK;
7697d57f80Sjmcneill 	const int type = (gtdt->VirtualTimerFlags & ACPI_GTDT_INTERRUPT_MODE) ?
7797d57f80Sjmcneill 	    IST_EDGE : IST_LEVEL;
7897d57f80Sjmcneill 
7997d57f80Sjmcneill 	aprint_naive("\n");
8097d57f80Sjmcneill 	aprint_normal(": irq %d\n", irq);
8197d57f80Sjmcneill 
82b6a8084fSjmcneill 	ih = intr_establish_xname(irq, ipl, type | IST_MPSAFE, gtmr_intr, NULL, device_xname(self));
8397d57f80Sjmcneill 	if (ih == NULL) {
8497d57f80Sjmcneill 		aprint_error_dev(self, "couldn't install interrupt handler\n");
8597d57f80Sjmcneill 		return;
8697d57f80Sjmcneill 	}
8797d57f80Sjmcneill 
8897d57f80Sjmcneill 	memset(&mpcaa, 0, sizeof(mpcaa));
8997d57f80Sjmcneill 	mpcaa.mpcaa_name = "armgtmr";
9097d57f80Sjmcneill 	mpcaa.mpcaa_irq = -1;
91*c7fb772bSthorpej 	config_found(self, &mpcaa, NULL, CFARGS_NONE);
9297d57f80Sjmcneill 
9397d57f80Sjmcneill 	arm_fdt_cpu_hatch_register(self, gtmr_acpi_cpu_hatch);
9497d57f80Sjmcneill 	arm_fdt_timer_register(gtmr_cpu_initclocks);
9597d57f80Sjmcneill }
9697d57f80Sjmcneill 
9797d57f80Sjmcneill static void
gtmr_acpi_cpu_hatch(void * priv,struct cpu_info * ci)9897d57f80Sjmcneill gtmr_acpi_cpu_hatch(void *priv, struct cpu_info *ci)
9997d57f80Sjmcneill {
10097d57f80Sjmcneill 	gtmr_init_cpu_clock(ci);
10197d57f80Sjmcneill }
102