wrap030_timer.c revision 1.1 1 1.1 thorpej /* $NetBSD: wrap030_timer.c,v 1.1 2026/08/06 14:41:09 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2026 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h>
33 1.1 thorpej __KERNEL_RCSID(0, "$NetBSD: wrap030_timer.c,v 1.1 2026/08/06 14:41:09 thorpej Exp $");
34 1.1 thorpej
35 1.1 thorpej #include <sys/param.h>
36 1.1 thorpej #include <sys/bus.h>
37 1.1 thorpej #include <sys/device.h>
38 1.1 thorpej #include <sys/intr.h>
39 1.1 thorpej #include <sys/systm.h>
40 1.1 thorpej
41 1.1 thorpej #include <machine/clockvar.h>
42 1.1 thorpej
43 1.1 thorpej #include <dev/fdt/fdtvar.h>
44 1.1 thorpej
45 1.1 thorpej struct wraptmr_softc {
46 1.1 thorpej device_t sc_dev;
47 1.1 thorpej bus_space_tag_t sc_st;
48 1.1 thorpej bus_space_handle_t sc_sh;
49 1.1 thorpej void *sc_ih;
50 1.1 thorpej uint32_t sc_freq;
51 1.1 thorpej uint32_t sc_reload;
52 1.1 thorpej struct clock_attach_args sc_clock_args;
53 1.1 thorpej void (*sc_handler)(struct clockframe *);
54 1.1 thorpej struct evcnt *sc_evcnt;
55 1.1 thorpej };
56 1.1 thorpej
57 1.1 thorpej #define TIMER_ARM(sc) \
58 1.1 thorpej bus_space_write_1((sc)->sc_st, (sc)->sc_sh, (sc)->sc_reload, 0)
59 1.1 thorpej #define TIMER_DISARM(sc) \
60 1.1 thorpej bus_space_write_1((sc)->sc_st, (sc)->sc_sh, 0, 0)
61 1.1 thorpej
62 1.1 thorpej static const struct device_compatible_entry compat_data[] = {
63 1.1 thorpej { .compat = "techav,wrap030-timer" },
64 1.1 thorpej DEVICE_COMPAT_EOL
65 1.1 thorpej };
66 1.1 thorpej
67 1.1 thorpej static uint32_t
68 1.1 thorpej wraptmr_us_to_ticks(struct wraptmr_softc *sc, unsigned int interval_us)
69 1.1 thorpej {
70 1.1 thorpej const uint32_t tick_ns = 1000000000 / sc->sc_freq;
71 1.1 thorpej const uint64_t interval_ns = interval_us * 1000;
72 1.1 thorpej const uint64_t ticks = interval_ns / tick_ns;
73 1.1 thorpej
74 1.1 thorpej if (ticks == 0 || ticks > 0xffff) {
75 1.1 thorpej panic("%s: impossible interval %u us (ticks=%llu)", __func__,
76 1.1 thorpej interval_us, ticks);
77 1.1 thorpej }
78 1.1 thorpej
79 1.1 thorpej return (uint32_t)ticks;
80 1.1 thorpej }
81 1.1 thorpej
82 1.1 thorpej static void
83 1.1 thorpej wraptmr_initclock(void *arg, unsigned int interval_us,
84 1.1 thorpej struct evcnt *ev, void (*func)(struct clockframe *))
85 1.1 thorpej {
86 1.1 thorpej struct wraptmr_softc *sc = arg;
87 1.1 thorpej
88 1.1 thorpej sc->sc_reload = wraptmr_us_to_ticks(sc, interval_us);
89 1.1 thorpej sc->sc_handler = func;
90 1.1 thorpej sc->sc_evcnt = ev;
91 1.1 thorpej
92 1.1 thorpej TIMER_ARM(sc);
93 1.1 thorpej }
94 1.1 thorpej
95 1.1 thorpej #define CLOCK_HANDLER() \
96 1.1 thorpej do { \
97 1.1 thorpej TIMER_ARM(sc); \
98 1.1 thorpej \
99 1.1 thorpej /* Increment the counter and call the handler. */ \
100 1.1 thorpej sc->sc_evcnt->ev_count++; \
101 1.1 thorpej sc->sc_handler((struct clockframe *)v); \
102 1.1 thorpej } while (/*CONSTCOND*/0)
103 1.1 thorpej
104 1.1 thorpej static int
105 1.1 thorpej wraptmr_hardclock(void *v)
106 1.1 thorpej {
107 1.1 thorpej struct wraptmr_softc *sc = clock_devices[CLOCK_HARDCLOCK];
108 1.1 thorpej
109 1.1 thorpej CLOCK_HANDLER();
110 1.1 thorpej return 1;
111 1.1 thorpej }
112 1.1 thorpej
113 1.1 thorpej static void *wraptmr_isrs[NCLOCKS] = {
114 1.1 thorpej [CLOCK_HARDCLOCK] = wraptmr_hardclock,
115 1.1 thorpej };
116 1.1 thorpej
117 1.1 thorpej static int
118 1.1 thorpej wraptmr_match(device_t parent, cfdata_t cf, void *aux)
119 1.1 thorpej {
120 1.1 thorpej struct fdt_attach_args * const faa = aux;
121 1.1 thorpej
122 1.1 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
123 1.1 thorpej }
124 1.1 thorpej
125 1.1 thorpej static void
126 1.1 thorpej wraptmr_attach(device_t parent, device_t self, void *aux)
127 1.1 thorpej {
128 1.1 thorpej struct wraptmr_softc * const sc = device_private(self);
129 1.1 thorpej struct fdt_attach_args * const faa = aux;
130 1.1 thorpej const int phandle = faa->faa_phandle;
131 1.1 thorpej bus_addr_t addr;
132 1.1 thorpej bus_size_t size;
133 1.1 thorpej char intrstr[128];
134 1.1 thorpej struct clk *clk;
135 1.1 thorpej int which, error;
136 1.1 thorpej
137 1.1 thorpej sc->sc_dev = self;
138 1.1 thorpej sc->sc_st = faa->faa_bst;
139 1.1 thorpej
140 1.1 thorpej if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
141 1.1 thorpej aprint_error(": couldn't get registers\n");
142 1.1 thorpej return;
143 1.1 thorpej }
144 1.1 thorpej
145 1.1 thorpej error = bus_space_map(sc->sc_st, addr, size, 0, &sc->sc_sh);
146 1.1 thorpej if (error) {
147 1.1 thorpej aprint_error(": couldn't map registers (error=%d)\n", error);
148 1.1 thorpej return;
149 1.1 thorpej }
150 1.1 thorpej
151 1.1 thorpej /* Make sure the timer is disabled. */
152 1.1 thorpej TIMER_DISARM(sc);
153 1.1 thorpej
154 1.1 thorpej clk = fdtbus_clock_get_index(phandle, 0);
155 1.1 thorpej if (clk == NULL) {
156 1.1 thorpej aprint_error(": couldn't get clock handle\n");
157 1.1 thorpej return;
158 1.1 thorpej }
159 1.1 thorpej
160 1.1 thorpej if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
161 1.1 thorpej aprint_error(": failed to decode interrupt\n");
162 1.1 thorpej return;
163 1.1 thorpej }
164 1.1 thorpej
165 1.1 thorpej sc->sc_freq = clk_get_rate(clk);
166 1.1 thorpej
167 1.1 thorpej aprint_naive("\n");
168 1.1 thorpej aprint_normal(": frequency %u Hz\n", sc->sc_freq);
169 1.1 thorpej
170 1.1 thorpej which = clock_from_phandle(phandle);
171 1.1 thorpej if (which == CLOCK_NONE || wraptmr_isrs[which] == NULL) {
172 1.1 thorpej return;
173 1.1 thorpej }
174 1.1 thorpej
175 1.1 thorpej sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SCHED,
176 1.1 thorpej FDT_INTR_MPSAFE, wraptmr_isrs[which], NULL, device_xname(self));
177 1.1 thorpej if (sc->sc_ih == NULL) {
178 1.1 thorpej aprint_error_dev(self, "failed to establish interrupt at %s\n",
179 1.1 thorpej intrstr);
180 1.1 thorpej return;
181 1.1 thorpej }
182 1.1 thorpej aprint_normal_dev(self, "interrupting at %s\n", intrstr);
183 1.1 thorpej
184 1.1 thorpej sc->sc_clock_args.ca_initfunc = wraptmr_initclock;
185 1.1 thorpej sc->sc_clock_args.ca_arg = sc;
186 1.1 thorpej sc->sc_clock_args.ca_which = which;
187 1.1 thorpej clock_attach(self, &sc->sc_clock_args);
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej CFATTACH_DECL_NEW(wraptmr, sizeof(struct wraptmr_softc),
191 1.1 thorpej wraptmr_match, wraptmr_attach, NULL, NULL);
192