1 /* $NetBSD: gfrtc_mainbus.c,v 1.4 2026/11/30 01:34:32 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2023, 2024, 2026 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Nick Hudson and 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: gfrtc_mainbus.c,v 1.4 2026/11/30 01:34:32 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 <virt68k/dev/mainbusvar.h> 44 45 #include <dev/goldfish/gfrtcvar.h> 46 47 static const struct device_compatible_entry compat_data[] = { 48 { .compat = "google,goldfish-rtc", 49 .value = CLOCK_NONE }, 50 51 { .compat = "netbsd,goldfish-rtc-hardclock", 52 .value = CLOCK_HARDCLOCK }, 53 54 DEVICE_COMPAT_EOL 55 }; 56 57 struct gfrtc_mainbus_softc { 58 struct gfrtc_softc sc_gfrtc; 59 struct clock_if sc_clkif; 60 uint64_t sc_interval_ns; 61 uint64_t sc_alarm_time; 62 int sc_irq; 63 void * sc_ih; 64 }; 65 66 static void 67 gfrtc_mainbus_delay(struct clock_if *clk, unsigned int usec) 68 { 69 struct gfrtc_mainbus_softc *sc = 70 container_of(clk, struct gfrtc_mainbus_softc, sc_clkif); 71 72 gfrtc_delay(sc->sc_gfrtc.sc_dev, usec); 73 } 74 75 static int 76 gfrtc_mainbus_init(struct clock_if *clk, int (*intrfunc)(void *)) 77 { 78 struct gfrtc_mainbus_softc *sc = 79 container_of(clk, struct gfrtc_mainbus_softc, sc_clkif); 80 char intrstr[128]; 81 82 KASSERT(sc->sc_gfrtc.sc_dev == clk->clk_dev); 83 84 /* Nothing to do if we're just the delay timer. */ 85 if (intrfunc == NULL) { 86 return 0; 87 } 88 89 sc->sc_ih = intr_establish(intrfunc, NULL, sc->sc_irq, IPL_SCHED, 0); 90 KASSERT(sc->sc_ih != NULL); 91 aprint_normal_dev(sc->sc_gfrtc.sc_dev, "interrupting at %s\n", 92 intr_string(sc->sc_ih, intrstr, sizeof(intrstr))); 93 94 return 0; 95 } 96 97 static int 98 gfrtc_mainbus_arm(struct clock_if *clk, unsigned int interval_us) 99 { 100 struct gfrtc_mainbus_softc *sc = 101 container_of(clk, struct gfrtc_mainbus_softc, sc_clkif); 102 103 /* Nothing to do if we're just the delay timer. */ 104 if (interval_us == 0) { 105 return 0; 106 } 107 108 sc->sc_interval_ns = (uint64_t)interval_us * 1000; 109 110 /* Enable the interrupt. */ 111 gfrtc_enable_interrupt(&sc->sc_gfrtc); 112 113 /* Start the first alarm! */ 114 sc->sc_alarm_time = gfrtc_get_time(&sc->sc_gfrtc) + sc->sc_interval_ns; 115 gfrtc_set_alarm(&sc->sc_gfrtc, sc->sc_alarm_time); 116 117 return 0; 118 } 119 120 static void 121 gfrtc_mainbus_intr(struct clock_if *clk) 122 { 123 struct gfrtc_mainbus_softc *sc = 124 container_of(clk, struct gfrtc_mainbus_softc, sc_clkif); 125 126 /* Clear the interrupt condition. */ 127 gfrtc_clear_interrupt(&sc->sc_gfrtc); 128 129 /* Get the next alarm set. */ 130 sc->sc_alarm_time += sc->sc_interval_ns; 131 gfrtc_set_alarm(&sc->sc_gfrtc, sc->sc_alarm_time); 132 } 133 134 static void 135 gfrtc_mainbus_disarm(struct clock_if *clk) 136 { 137 struct gfrtc_mainbus_softc *sc = 138 container_of(clk, struct gfrtc_mainbus_softc, sc_clkif); 139 140 /* Disable the interrupt. */ 141 gfrtc_disable_interrupt(&sc->sc_gfrtc); 142 } 143 144 static int 145 gfrtc_mainbus_match(device_t parent, cfdata_t cf, void *aux) 146 { 147 struct mainbus_attach_args * const ma = aux; 148 149 return mainbus_compatible_match(ma, compat_data); 150 } 151 152 static void 153 gfrtc_mainbus_attach(device_t parent, device_t self, void *aux) 154 { 155 struct gfrtc_mainbus_softc * const sc = device_private(self); 156 struct mainbus_attach_args * const ma = aux; 157 const struct device_compatible_entry *dce; 158 159 sc->sc_gfrtc.sc_dev = self; 160 sc->sc_gfrtc.sc_bst = ma->ma_st; 161 if (bus_space_map(sc->sc_gfrtc.sc_bst, ma->ma_addr, ma->ma_size, 0, 162 &sc->sc_gfrtc.sc_bsh) != 0) { 163 aprint_error(": couldn't map registers\n"); 164 return; 165 } 166 167 dce = mainbus_compatible_lookup(ma, compat_data); 168 KASSERT(dce != NULL); 169 170 gfrtc_attach(&sc->sc_gfrtc, (dce->value == CLOCK_NONE)); 171 172 switch (dce->value) { 173 case CLOCK_HARDCLOCK: 174 /* 175 * We are the one of the clock interrupts. 176 */ 177 sc->sc_irq = ma->ma_irq; 178 sc->sc_clkif.clk_dev = self; 179 sc->sc_clkif.clk_delay = gfrtc_mainbus_delay; 180 sc->sc_clkif.clk_init = gfrtc_mainbus_init; 181 sc->sc_clkif.clk_arm = gfrtc_mainbus_arm; 182 sc->sc_clkif.clk_intr = gfrtc_mainbus_intr; 183 sc->sc_clkif.clk_disarm = gfrtc_mainbus_disarm; 184 clock_attach(&sc->sc_clkif); 185 break; 186 187 default: 188 break; 189 } 190 } 191 192 CFATTACH_DECL_NEW(gfrtc_mainbus, sizeof(struct gfrtc_mainbus_softc), 193 gfrtc_mainbus_match, gfrtc_mainbus_attach, NULL, NULL); 194