1/* $NetBSD: gtmr_fdt.c,v 1.12 2021/11/12 21:59:05 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill@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 <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: gtmr_fdt.c,v 1.12 2021/11/12 21:59:05 jmcneill Exp $");
31
32#include <sys/param.h>
33#include <sys/bus.h>
34#include <sys/device.h>
35#include <sys/intr.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/kmem.h>
39
40#include <arm/cortex/gtmr_intr.h>
41#include <arm/cortex/mpcore_var.h>
42#include <arm/cortex/gtmr_var.h>
43
44#include <dev/fdt/fdtvar.h>
45#include <arm/fdt/arm_fdtvar.h>
46
47#if defined(__arm__)
48#include <arm/armreg.h>
49#endif
50
51static int	gtmr_fdt_match(device_t, cfdata_t, void *);
52static void	gtmr_fdt_attach(device_t, device_t, void *);
53
54static void	gtmr_fdt_cpu_hatch(void *, struct cpu_info *);
55
56CFATTACH_DECL_NEW(gtmr_fdt, 0, gtmr_fdt_match, gtmr_fdt_attach, NULL, NULL);
57
58/* The virtual timer list entry */
59#define GTMR_VTIMER 2
60
61static const struct device_compatible_entry compat_data[] = {
62	{ .compat = "arm,armv7-timer" },
63	{ .compat = "arm,armv8-timer" },
64	DEVICE_COMPAT_EOL
65};
66
67static int
68gtmr_fdt_match(device_t parent, cfdata_t cf, void *aux)
69{
70	struct fdt_attach_args * const faa = aux;
71
72	return of_compatible_match(faa->faa_phandle, compat_data);
73}
74
75static void
76gtmr_fdt_attach(device_t parent, device_t self, void *aux)
77{
78	aprint_naive("\n");
79	aprint_normal(": Generic Timer\n");
80
81	struct mpcore_attach_args mpcaa = {
82		.mpcaa_name = "armgtmr",
83		.mpcaa_irq = -1		/* setup handler locally */
84	};
85	struct fdt_attach_args * const faa = aux;
86	const int phandle = faa->faa_phandle;
87
88	char intrstr[128];
89	if (!fdtbus_intr_str(phandle, GTMR_VTIMER, intrstr, sizeof(intrstr))) {
90		aprint_error(": failed to decode interrupt\n");
91		return;
92	}
93
94	void *ih = fdtbus_intr_establish_xname(phandle, GTMR_VTIMER, IPL_CLOCK,
95	    FDT_INTR_MPSAFE, gtmr_intr, NULL, device_xname(self));
96	if (ih == NULL) {
97		aprint_error_dev(self, "couldn't install interrupt handler\n");
98		return;
99	}
100	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
101
102#if defined(__arm__)
103	/*
104	 * If arm,cpu-registers-not-fw-configured is present, we need
105	 * to initialize cntfrq from the clock-frequency property. Only
106	 * applicable on Armv7.
107	 */
108	if (of_hasprop(phandle, "arm,cpu-registers-not-fw-configured")) {
109		uint32_t freq;
110
111		if (of_getprop_uint32(phandle, "clock-frequency", &freq) == 0) {
112			armreg_cnt_frq_write(freq);
113			prop_dictionary_set_bool(device_properties(self),
114			    "arm,cpu-registers-not-fw-configured", true);
115		}
116	}
117#endif
118
119	config_found(self, &mpcaa, NULL, CFARGS_NONE);
120
121	arm_fdt_cpu_hatch_register(self, gtmr_fdt_cpu_hatch);
122	arm_fdt_timer_register(gtmr_cpu_initclocks);
123}
124
125static void
126gtmr_fdt_cpu_hatch(void *priv, struct cpu_info *ci)
127{
128	gtmr_init_cpu_clock(ci);
129}
130