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