wrap030_timer.c revision 1.1 1 /* $NetBSD: wrap030_timer.c,v 1.1 2026/08/06 14:41:09 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2026 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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: wrap030_timer.c,v 1.1 2026/08/06 14:41:09 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/systm.h>
40
41 #include <machine/clockvar.h>
42
43 #include <dev/fdt/fdtvar.h>
44
45 struct wraptmr_softc {
46 device_t sc_dev;
47 bus_space_tag_t sc_st;
48 bus_space_handle_t sc_sh;
49 void *sc_ih;
50 uint32_t sc_freq;
51 uint32_t sc_reload;
52 struct clock_attach_args sc_clock_args;
53 void (*sc_handler)(struct clockframe *);
54 struct evcnt *sc_evcnt;
55 };
56
57 #define TIMER_ARM(sc) \
58 bus_space_write_1((sc)->sc_st, (sc)->sc_sh, (sc)->sc_reload, 0)
59 #define TIMER_DISARM(sc) \
60 bus_space_write_1((sc)->sc_st, (sc)->sc_sh, 0, 0)
61
62 static const struct device_compatible_entry compat_data[] = {
63 { .compat = "techav,wrap030-timer" },
64 DEVICE_COMPAT_EOL
65 };
66
67 static uint32_t
68 wraptmr_us_to_ticks(struct wraptmr_softc *sc, unsigned int interval_us)
69 {
70 const uint32_t tick_ns = 1000000000 / sc->sc_freq;
71 const uint64_t interval_ns = interval_us * 1000;
72 const uint64_t ticks = interval_ns / tick_ns;
73
74 if (ticks == 0 || ticks > 0xffff) {
75 panic("%s: impossible interval %u us (ticks=%llu)", __func__,
76 interval_us, ticks);
77 }
78
79 return (uint32_t)ticks;
80 }
81
82 static void
83 wraptmr_initclock(void *arg, unsigned int interval_us,
84 struct evcnt *ev, void (*func)(struct clockframe *))
85 {
86 struct wraptmr_softc *sc = arg;
87
88 sc->sc_reload = wraptmr_us_to_ticks(sc, interval_us);
89 sc->sc_handler = func;
90 sc->sc_evcnt = ev;
91
92 TIMER_ARM(sc);
93 }
94
95 #define CLOCK_HANDLER() \
96 do { \
97 TIMER_ARM(sc); \
98 \
99 /* Increment the counter and call the handler. */ \
100 sc->sc_evcnt->ev_count++; \
101 sc->sc_handler((struct clockframe *)v); \
102 } while (/*CONSTCOND*/0)
103
104 static int
105 wraptmr_hardclock(void *v)
106 {
107 struct wraptmr_softc *sc = clock_devices[CLOCK_HARDCLOCK];
108
109 CLOCK_HANDLER();
110 return 1;
111 }
112
113 static void *wraptmr_isrs[NCLOCKS] = {
114 [CLOCK_HARDCLOCK] = wraptmr_hardclock,
115 };
116
117 static int
118 wraptmr_match(device_t parent, cfdata_t cf, void *aux)
119 {
120 struct fdt_attach_args * const faa = aux;
121
122 return of_compatible_match(faa->faa_phandle, compat_data);
123 }
124
125 static void
126 wraptmr_attach(device_t parent, device_t self, void *aux)
127 {
128 struct wraptmr_softc * const sc = device_private(self);
129 struct fdt_attach_args * const faa = aux;
130 const int phandle = faa->faa_phandle;
131 bus_addr_t addr;
132 bus_size_t size;
133 char intrstr[128];
134 struct clk *clk;
135 int which, error;
136
137 sc->sc_dev = self;
138 sc->sc_st = faa->faa_bst;
139
140 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
141 aprint_error(": couldn't get registers\n");
142 return;
143 }
144
145 error = bus_space_map(sc->sc_st, addr, size, 0, &sc->sc_sh);
146 if (error) {
147 aprint_error(": couldn't map registers (error=%d)\n", error);
148 return;
149 }
150
151 /* Make sure the timer is disabled. */
152 TIMER_DISARM(sc);
153
154 clk = fdtbus_clock_get_index(phandle, 0);
155 if (clk == NULL) {
156 aprint_error(": couldn't get clock handle\n");
157 return;
158 }
159
160 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
161 aprint_error(": failed to decode interrupt\n");
162 return;
163 }
164
165 sc->sc_freq = clk_get_rate(clk);
166
167 aprint_naive("\n");
168 aprint_normal(": frequency %u Hz\n", sc->sc_freq);
169
170 which = clock_from_phandle(phandle);
171 if (which == CLOCK_NONE || wraptmr_isrs[which] == NULL) {
172 return;
173 }
174
175 sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SCHED,
176 FDT_INTR_MPSAFE, wraptmr_isrs[which], NULL, device_xname(self));
177 if (sc->sc_ih == NULL) {
178 aprint_error_dev(self, "failed to establish interrupt at %s\n",
179 intrstr);
180 return;
181 }
182 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
183
184 sc->sc_clock_args.ca_initfunc = wraptmr_initclock;
185 sc->sc_clock_args.ca_arg = sc;
186 sc->sc_clock_args.ca_which = which;
187 clock_attach(self, &sc->sc_clock_args);
188 }
189
190 CFATTACH_DECL_NEW(wraptmr, sizeof(struct wraptmr_softc),
191 wraptmr_match, wraptmr_attach, NULL, NULL);
192