1 1.1 thorpej /* $NetBSD: pgtimer.c,v 1.1 2026/07/19 01:48:24 thorpej Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 2025 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: pgtimer.c,v 1.1 2026/07/19 01:48:24 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 pgtimer_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 uint32_t sc_reg_shift; 50 1.1 thorpej void *sc_ih; 51 1.1 thorpej uint32_t sc_freq; 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_CSR 0 58 1.1 thorpej #define CSR_ENAB __BIT(0) 59 1.1 thorpej #define CSR_IPEND __BIT(1) 60 1.1 thorpej #define TIMER_VAL 1 61 1.1 thorpej 62 1.1 thorpej #define REG_OFF(sc, r) ((r) << (sc)->sc_reg_shift) 63 1.1 thorpej 64 1.1 thorpej #define REG_READ(sc, r) \ 65 1.1 thorpej bus_space_read_1((sc)->sc_st, (sc)->sc_sh, REG_OFF((sc), (r))) 66 1.1 thorpej #define REG_WRITE(sc, r, v) \ 67 1.1 thorpej bus_space_write_1((sc)->sc_st, (sc)->sc_sh, REG_OFF((sc), (r)), (v)) 68 1.1 thorpej 69 1.1 thorpej static const struct device_compatible_entry compat_data[] = { 70 1.1 thorpej { .compat = "pg68k,ioctl010-timer" }, 71 1.1 thorpej DEVICE_COMPAT_EOL 72 1.1 thorpej }; 73 1.1 thorpej 74 1.1 thorpej static uint32_t 75 1.1 thorpej pgtimer_us_to_ticks(struct pgtimer_softc *sc, unsigned int interval_us) 76 1.1 thorpej { 77 1.1 thorpej const uint32_t tick_ns = 1000000000 / sc->sc_freq; 78 1.1 thorpej const uint64_t interval_ns = interval_us * 1000; 79 1.1 thorpej const uint64_t ticks = interval_ns / tick_ns; 80 1.1 thorpej 81 1.1 thorpej if (ticks == 0 || ticks > 0xffff) { 82 1.1 thorpej panic("%s: impossible interval %u us (ticks=%llu)", __func__, 83 1.1 thorpej interval_us, ticks); 84 1.1 thorpej } 85 1.1 thorpej 86 1.1 thorpej return (uint32_t)ticks; 87 1.1 thorpej } 88 1.1 thorpej 89 1.1 thorpej static void 90 1.1 thorpej pgtimer_initclock(void *arg, unsigned int interval_us, 91 1.1 thorpej struct evcnt *ev, void (*func)(struct clockframe *)) 92 1.1 thorpej { 93 1.1 thorpej struct pgtimer_softc *sc = arg; 94 1.1 thorpej const uint32_t ticks = pgtimer_us_to_ticks(sc, interval_us); 95 1.1 thorpej 96 1.1 thorpej sc->sc_handler = func; 97 1.1 thorpej sc->sc_evcnt = ev; 98 1.1 thorpej 99 1.1 thorpej /* 100 1.1 thorpej * Changing the counter reload value implicitly disables the 101 1.1 thorpej * timer. The reload value is set by writing the high byte 102 1.1 thorpej * followed by the load byte to the Value register. The reload 103 1.1 thorpej * value is unpredictable until both bytes have been written. 104 1.1 thorpej */ 105 1.1 thorpej REG_WRITE(sc, TIMER_VAL, (uint8_t)(ticks >> 8)); 106 1.1 thorpej REG_WRITE(sc, TIMER_VAL, (uint8_t)ticks); 107 1.1 thorpej REG_WRITE(sc, TIMER_CSR, CSR_ENAB); 108 1.1 thorpej } 109 1.1 thorpej 110 1.1 thorpej #define CLOCK_HANDLER() \ 111 1.1 thorpej do { \ 112 1.1 thorpej /* Clear interrupt condition. */ \ 113 1.1 thorpej REG_READ(sc, TIMER_CSR); \ 114 1.1 thorpej \ 115 1.1 thorpej /* Timer auto-reloads. */ \ 116 1.1 thorpej \ 117 1.1 thorpej /* Increment the counter and call the handler. */ \ 118 1.1 thorpej sc->sc_evcnt->ev_count++; \ 119 1.1 thorpej sc->sc_handler((struct clockframe *)v); \ 120 1.1 thorpej } while (/*CONSTCOND*/0) 121 1.1 thorpej 122 1.1 thorpej static int 123 1.1 thorpej pgtimer_hardclock(void *v) 124 1.1 thorpej { 125 1.1 thorpej struct pgtimer_softc *sc = clock_devices[CLOCK_HARDCLOCK]; 126 1.1 thorpej 127 1.1 thorpej CLOCK_HANDLER(); 128 1.1 thorpej return 1; 129 1.1 thorpej } 130 1.1 thorpej 131 1.1 thorpej static int 132 1.1 thorpej pgtimer_statclock(void *v) 133 1.1 thorpej { 134 1.1 thorpej struct pgtimer_softc *sc = clock_devices[CLOCK_STATCLOCK]; 135 1.1 thorpej 136 1.1 thorpej CLOCK_HANDLER(); 137 1.1 thorpej return 1; 138 1.1 thorpej } 139 1.1 thorpej 140 1.1 thorpej static void *pgtimer_isrs[NCLOCKS] = { 141 1.1 thorpej [CLOCK_HARDCLOCK] = pgtimer_hardclock, 142 1.1 thorpej [CLOCK_STATCLOCK] = pgtimer_statclock, 143 1.1 thorpej }; 144 1.1 thorpej 145 1.1 thorpej static int 146 1.1 thorpej pgtimer_match(device_t parent, cfdata_t cf, void *aux) 147 1.1 thorpej { 148 1.1 thorpej struct fdt_attach_args * const faa = aux; 149 1.1 thorpej 150 1.1 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 151 1.1 thorpej } 152 1.1 thorpej 153 1.1 thorpej static void 154 1.1 thorpej pgtimer_attach(device_t parent, device_t self, void *aux) 155 1.1 thorpej { 156 1.1 thorpej struct pgtimer_softc * const sc = device_private(self); 157 1.1 thorpej struct fdt_attach_args * const faa = aux; 158 1.1 thorpej const int phandle = faa->faa_phandle; 159 1.1 thorpej bus_addr_t addr; 160 1.1 thorpej bus_size_t size; 161 1.1 thorpej char intrstr[128]; 162 1.1 thorpej struct clk *clk; 163 1.1 thorpej uint32_t clk_div; 164 1.1 thorpej int which, error; 165 1.1 thorpej 166 1.1 thorpej sc->sc_dev = self; 167 1.1 thorpej sc->sc_st = faa->faa_bst; 168 1.1 thorpej 169 1.1 thorpej if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 170 1.1 thorpej aprint_error(": couldn't get registers\n"); 171 1.1 thorpej return; 172 1.1 thorpej } 173 1.1 thorpej 174 1.1 thorpej if (of_getprop_uint32(phandle, "reg-shift", &sc->sc_reg_shift)) { 175 1.1 thorpej /* missing or bad reg-shift property, assume 0 */ 176 1.1 thorpej sc->sc_reg_shift = 0; 177 1.1 thorpej } 178 1.1 thorpej 179 1.1 thorpej error = bus_space_map(sc->sc_st, addr, size, 0, &sc->sc_sh); 180 1.1 thorpej if (error) { 181 1.1 thorpej aprint_error(": couldn't map registers (error=%d)\n", error); 182 1.1 thorpej return; 183 1.1 thorpej } 184 1.1 thorpej 185 1.1 thorpej /* Make sure the timer is disabled. */ 186 1.1 thorpej REG_WRITE(sc, TIMER_CSR, 0); 187 1.1 thorpej 188 1.1 thorpej clk = fdtbus_clock_get_index(phandle, 0); 189 1.1 thorpej if (clk == NULL) { 190 1.1 thorpej aprint_error(": couldn't get clock handle\n"); 191 1.1 thorpej return; 192 1.1 thorpej } 193 1.1 thorpej 194 1.1 thorpej if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 195 1.1 thorpej aprint_error(": failed to decode interrupt\n"); 196 1.1 thorpej return; 197 1.1 thorpej } 198 1.1 thorpej 199 1.1 thorpej /* 200 1.1 thorpej * The input to the I/O controller is the main system clock, 201 1.1 thorpej * which the timer divides itself internally. Firmware will 202 1.1 thorpej * provide this value in the device tree. 203 1.1 thorpej */ 204 1.1 thorpej if (of_getprop_uint32(phandle, "clock-div", &clk_div)) { 205 1.1 thorpej /* default to divide-by-16 */ 206 1.1 thorpej clk_div = 16; 207 1.1 thorpej } 208 1.1 thorpej 209 1.1 thorpej sc->sc_freq = clk_get_rate(clk) / clk_div; 210 1.1 thorpej 211 1.1 thorpej aprint_naive("\n"); 212 1.1 thorpej aprint_normal(": frequency %u Hz\n", sc->sc_freq); 213 1.1 thorpej 214 1.1 thorpej which = clock_from_phandle(phandle); 215 1.1 thorpej if (which == CLOCK_NONE || pgtimer_isrs[which] == NULL) { 216 1.1 thorpej return; 217 1.1 thorpej } 218 1.1 thorpej 219 1.1 thorpej sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SCHED, 220 1.1 thorpej FDT_INTR_MPSAFE, pgtimer_isrs[which], NULL, device_xname(self)); 221 1.1 thorpej if (sc->sc_ih == NULL) { 222 1.1 thorpej aprint_error_dev(self, "failed to establish interrupt at %s\n", 223 1.1 thorpej intrstr); 224 1.1 thorpej return; 225 1.1 thorpej } 226 1.1 thorpej aprint_normal_dev(self, "interrupting at %s\n", intrstr); 227 1.1 thorpej 228 1.1 thorpej sc->sc_clock_args.ca_initfunc = pgtimer_initclock; 229 1.1 thorpej sc->sc_clock_args.ca_arg = sc; 230 1.1 thorpej sc->sc_clock_args.ca_which = which; 231 1.1 thorpej clock_attach(self, &sc->sc_clock_args); 232 1.1 thorpej } 233 1.1 thorpej 234 1.1 thorpej CFATTACH_DECL_NEW(pgtimer, sizeof(struct pgtimer_softc), 235 1.1 thorpej pgtimer_match, pgtimer_attach, NULL, NULL); 236