wrap030_timer.c revision 1.2 1 1.2 thorpej /* $NetBSD: wrap030_timer.c,v 1.2 2026/08/07 23:37:46 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.2 thorpej __KERNEL_RCSID(0, "$NetBSD: wrap030_timer.c,v 1.2 2026/08/07 23:37:46 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.2 thorpej struct clock_if sc_clkif;
53 1.1 thorpej };
54 1.1 thorpej
55 1.1 thorpej #define TIMER_ARM(sc) \
56 1.1 thorpej bus_space_write_1((sc)->sc_st, (sc)->sc_sh, (sc)->sc_reload, 0)
57 1.1 thorpej #define TIMER_DISARM(sc) \
58 1.1 thorpej bus_space_write_1((sc)->sc_st, (sc)->sc_sh, 0, 0)
59 1.1 thorpej
60 1.1 thorpej static const struct device_compatible_entry compat_data[] = {
61 1.1 thorpej { .compat = "techav,wrap030-timer" },
62 1.1 thorpej DEVICE_COMPAT_EOL
63 1.1 thorpej };
64 1.1 thorpej
65 1.1 thorpej static uint32_t
66 1.1 thorpej wraptmr_us_to_ticks(struct wraptmr_softc *sc, unsigned int interval_us)
67 1.1 thorpej {
68 1.1 thorpej const uint32_t tick_ns = 1000000000 / sc->sc_freq;
69 1.1 thorpej const uint64_t interval_ns = interval_us * 1000;
70 1.1 thorpej const uint64_t ticks = interval_ns / tick_ns;
71 1.1 thorpej
72 1.1 thorpej if (ticks == 0 || ticks > 0xffff) {
73 1.1 thorpej panic("%s: impossible interval %u us (ticks=%llu)", __func__,
74 1.1 thorpej interval_us, ticks);
75 1.1 thorpej }
76 1.1 thorpej
77 1.1 thorpej return (uint32_t)ticks;
78 1.1 thorpej }
79 1.1 thorpej
80 1.2 thorpej static int
81 1.2 thorpej wraptmr_init(struct clock_if *clk, int (*intrfunc)(void *))
82 1.2 thorpej {
83 1.2 thorpej struct wraptmr_softc *sc =
84 1.2 thorpej container_of(clk, struct wraptmr_softc, sc_clkif);
85 1.2 thorpej int phandle = devhandle_to_of(device_handle(sc->sc_dev));
86 1.2 thorpej char intrstr[128];
87 1.2 thorpej
88 1.2 thorpej KASSERT(sc->sc_dev == clk->clk_dev);
89 1.2 thorpej
90 1.2 thorpej if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
91 1.2 thorpej aprint_error_dev(sc->sc_dev, "failed to decode interrupt\n");
92 1.2 thorpej return ENXIO;
93 1.2 thorpej }
94 1.2 thorpej
95 1.2 thorpej sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SCHED,
96 1.2 thorpej FDT_INTR_MPSAFE, intrfunc, NULL, device_xname(sc->sc_dev));
97 1.2 thorpej if (sc->sc_ih == NULL) {
98 1.2 thorpej aprint_error_dev(sc->sc_dev,
99 1.2 thorpej "failed to establish interrupt at %s\n", intrstr);
100 1.2 thorpej return ENXIO;
101 1.2 thorpej }
102 1.2 thorpej aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
103 1.2 thorpej
104 1.2 thorpej return 0;
105 1.2 thorpej }
106 1.2 thorpej
107 1.2 thorpej static int
108 1.2 thorpej wraptmr_arm(struct clock_if *clk, unsigned int interval_us)
109 1.1 thorpej {
110 1.2 thorpej struct wraptmr_softc *sc =
111 1.2 thorpej container_of(clk, struct wraptmr_softc, sc_clkif);
112 1.1 thorpej
113 1.1 thorpej sc->sc_reload = wraptmr_us_to_ticks(sc, interval_us);
114 1.2 thorpej TIMER_ARM(sc);
115 1.2 thorpej
116 1.2 thorpej return 0;
117 1.2 thorpej }
118 1.2 thorpej
119 1.2 thorpej static void
120 1.2 thorpej wraptmr_intr(struct clock_if *clk)
121 1.2 thorpej {
122 1.2 thorpej struct wraptmr_softc *sc =
123 1.2 thorpej container_of(clk, struct wraptmr_softc, sc_clkif);
124 1.1 thorpej
125 1.1 thorpej TIMER_ARM(sc);
126 1.1 thorpej }
127 1.1 thorpej
128 1.2 thorpej static void
129 1.2 thorpej wraptmr_disarm(struct clock_if *clk)
130 1.1 thorpej {
131 1.2 thorpej struct wraptmr_softc *sc =
132 1.2 thorpej container_of(clk, struct wraptmr_softc, sc_clkif);
133 1.1 thorpej
134 1.2 thorpej TIMER_DISARM(sc);
135 1.1 thorpej }
136 1.1 thorpej
137 1.1 thorpej static int
138 1.1 thorpej wraptmr_match(device_t parent, cfdata_t cf, void *aux)
139 1.1 thorpej {
140 1.1 thorpej struct fdt_attach_args * const faa = aux;
141 1.1 thorpej
142 1.1 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
143 1.1 thorpej }
144 1.1 thorpej
145 1.1 thorpej static void
146 1.1 thorpej wraptmr_attach(device_t parent, device_t self, void *aux)
147 1.1 thorpej {
148 1.1 thorpej struct wraptmr_softc * const sc = device_private(self);
149 1.1 thorpej struct fdt_attach_args * const faa = aux;
150 1.1 thorpej const int phandle = faa->faa_phandle;
151 1.1 thorpej bus_addr_t addr;
152 1.1 thorpej bus_size_t size;
153 1.1 thorpej struct clk *clk;
154 1.2 thorpej int error;
155 1.1 thorpej
156 1.1 thorpej sc->sc_dev = self;
157 1.1 thorpej sc->sc_st = faa->faa_bst;
158 1.1 thorpej
159 1.1 thorpej if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
160 1.1 thorpej aprint_error(": couldn't get registers\n");
161 1.1 thorpej return;
162 1.1 thorpej }
163 1.1 thorpej
164 1.1 thorpej error = bus_space_map(sc->sc_st, addr, size, 0, &sc->sc_sh);
165 1.1 thorpej if (error) {
166 1.1 thorpej aprint_error(": couldn't map registers (error=%d)\n", error);
167 1.1 thorpej return;
168 1.1 thorpej }
169 1.1 thorpej
170 1.1 thorpej /* Make sure the timer is disabled. */
171 1.1 thorpej TIMER_DISARM(sc);
172 1.1 thorpej
173 1.1 thorpej clk = fdtbus_clock_get_index(phandle, 0);
174 1.1 thorpej if (clk == NULL) {
175 1.1 thorpej aprint_error(": couldn't get clock handle\n");
176 1.1 thorpej return;
177 1.1 thorpej }
178 1.1 thorpej
179 1.1 thorpej sc->sc_freq = clk_get_rate(clk);
180 1.1 thorpej
181 1.1 thorpej aprint_naive("\n");
182 1.1 thorpej aprint_normal(": frequency %u Hz\n", sc->sc_freq);
183 1.1 thorpej
184 1.2 thorpej sc->sc_clkif.clk_dev = self;
185 1.2 thorpej sc->sc_clkif.clk_init = wraptmr_init;
186 1.2 thorpej sc->sc_clkif.clk_arm = wraptmr_arm;
187 1.2 thorpej sc->sc_clkif.clk_intr = wraptmr_intr;
188 1.2 thorpej sc->sc_clkif.clk_disarm = wraptmr_disarm;
189 1.2 thorpej clock_attach(&sc->sc_clkif);
190 1.1 thorpej }
191 1.1 thorpej
192 1.1 thorpej CFATTACH_DECL_NEW(wraptmr, sizeof(struct wraptmr_softc),
193 1.1 thorpej wraptmr_match, wraptmr_attach, NULL, NULL);
194