gtmr_fdt.c revision 1.7.2.2 1 1.7.2.2 jdolecek /* $NetBSD: gtmr_fdt.c,v 1.7.2.2 2017/12/03 11:35:52 jdolecek Exp $ */
2 1.7.2.2 jdolecek
3 1.7.2.2 jdolecek /*-
4 1.7.2.2 jdolecek * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.7.2.2 jdolecek * All rights reserved.
6 1.7.2.2 jdolecek *
7 1.7.2.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.7.2.2 jdolecek * modification, are permitted provided that the following conditions
9 1.7.2.2 jdolecek * are met:
10 1.7.2.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.7.2.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.7.2.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.7.2.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.7.2.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.7.2.2 jdolecek *
16 1.7.2.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.7.2.2 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.7.2.2 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.7.2.2 jdolecek * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.7.2.2 jdolecek * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.7.2.2 jdolecek * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.7.2.2 jdolecek * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.7.2.2 jdolecek * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.7.2.2 jdolecek * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.7.2.2 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.7.2.2 jdolecek * SUCH DAMAGE.
27 1.7.2.2 jdolecek */
28 1.7.2.2 jdolecek
29 1.7.2.2 jdolecek #include <sys/cdefs.h>
30 1.7.2.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: gtmr_fdt.c,v 1.7.2.2 2017/12/03 11:35:52 jdolecek Exp $");
31 1.7.2.2 jdolecek
32 1.7.2.2 jdolecek #include <sys/param.h>
33 1.7.2.2 jdolecek #include <sys/bus.h>
34 1.7.2.2 jdolecek #include <sys/device.h>
35 1.7.2.2 jdolecek #include <sys/intr.h>
36 1.7.2.2 jdolecek #include <sys/systm.h>
37 1.7.2.2 jdolecek #include <sys/kernel.h>
38 1.7.2.2 jdolecek #include <sys/kmem.h>
39 1.7.2.2 jdolecek
40 1.7.2.2 jdolecek #include <arm/cortex/gtmr_intr.h>
41 1.7.2.2 jdolecek #include <arm/cortex/mpcore_var.h>
42 1.7.2.2 jdolecek #include <arm/cortex/gtmr_var.h>
43 1.7.2.2 jdolecek
44 1.7.2.2 jdolecek #include <dev/fdt/fdtvar.h>
45 1.7.2.2 jdolecek #include <arm/fdt/arm_fdtvar.h>
46 1.7.2.2 jdolecek
47 1.7.2.2 jdolecek static int gtmr_fdt_match(device_t, cfdata_t, void *);
48 1.7.2.2 jdolecek static void gtmr_fdt_attach(device_t, device_t, void *);
49 1.7.2.2 jdolecek
50 1.7.2.2 jdolecek static void gtmr_fdt_cpu_hatch(void *, struct cpu_info *);
51 1.7.2.2 jdolecek
52 1.7.2.2 jdolecek CFATTACH_DECL_NEW(gtmr_fdt, 0, gtmr_fdt_match, gtmr_fdt_attach, NULL, NULL);
53 1.7.2.2 jdolecek
54 1.7.2.2 jdolecek /* The virtual timer list entry */
55 1.7.2.2 jdolecek #define GTMR_VTIMER 2
56 1.7.2.2 jdolecek
57 1.7.2.2 jdolecek static int
58 1.7.2.2 jdolecek gtmr_fdt_match(device_t parent, cfdata_t cf, void *aux)
59 1.7.2.2 jdolecek {
60 1.7.2.2 jdolecek const char * const compatible[] = {
61 1.7.2.2 jdolecek "arm,armv7-timer",
62 1.7.2.2 jdolecek "arm,armv8-timer",
63 1.7.2.2 jdolecek NULL
64 1.7.2.2 jdolecek };
65 1.7.2.2 jdolecek struct fdt_attach_args * const faa = aux;
66 1.7.2.2 jdolecek
67 1.7.2.2 jdolecek return of_compatible(faa->faa_phandle, compatible) >= 0;
68 1.7.2.2 jdolecek }
69 1.7.2.2 jdolecek
70 1.7.2.2 jdolecek static void
71 1.7.2.2 jdolecek gtmr_fdt_attach(device_t parent, device_t self, void *aux)
72 1.7.2.2 jdolecek {
73 1.7.2.2 jdolecek aprint_naive("\n");
74 1.7.2.2 jdolecek aprint_normal(": Generic Timer\n");
75 1.7.2.2 jdolecek
76 1.7.2.2 jdolecek struct mpcore_attach_args mpcaa = {
77 1.7.2.2 jdolecek .mpcaa_name = "armgtmr",
78 1.7.2.2 jdolecek .mpcaa_irq = -1 /* setup handler locally */
79 1.7.2.2 jdolecek };
80 1.7.2.2 jdolecek struct fdt_attach_args * const faa = aux;
81 1.7.2.2 jdolecek const int phandle = faa->faa_phandle;
82 1.7.2.2 jdolecek
83 1.7.2.2 jdolecek char intrstr[128];
84 1.7.2.2 jdolecek if (!fdtbus_intr_str(phandle, GTMR_VTIMER, intrstr, sizeof(intrstr))) {
85 1.7.2.2 jdolecek aprint_error(": failed to decode interrupt\n");
86 1.7.2.2 jdolecek return;
87 1.7.2.2 jdolecek }
88 1.7.2.2 jdolecek
89 1.7.2.2 jdolecek void *ih = fdtbus_intr_establish(phandle, GTMR_VTIMER, IPL_CLOCK,
90 1.7.2.2 jdolecek FDT_INTR_MPSAFE, gtmr_intr, NULL);
91 1.7.2.2 jdolecek if (ih == NULL) {
92 1.7.2.2 jdolecek aprint_error_dev(self, "couldn't install interrupt handler\n");
93 1.7.2.2 jdolecek return;
94 1.7.2.2 jdolecek }
95 1.7.2.2 jdolecek aprint_normal_dev(self, "interrupting on %s\n", intrstr);
96 1.7.2.2 jdolecek
97 1.7.2.2 jdolecek config_found(self, &mpcaa, NULL);
98 1.7.2.2 jdolecek
99 1.7.2.2 jdolecek arm_fdt_cpu_hatch_register(self, gtmr_fdt_cpu_hatch);
100 1.7.2.2 jdolecek arm_fdt_timer_register(gtmr_cpu_initclocks);
101 1.7.2.2 jdolecek }
102 1.7.2.2 jdolecek
103 1.7.2.2 jdolecek static void
104 1.7.2.2 jdolecek gtmr_fdt_cpu_hatch(void *priv, struct cpu_info *ci)
105 1.7.2.2 jdolecek {
106 1.7.2.2 jdolecek gtmr_init_cpu_clock(ci);
107 1.7.2.2 jdolecek }
108