1 1.7 thorpej /* $NetBSD: gfrtc.c,v 1.7 2025/09/07 21:45:15 thorpej Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 2023 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.3 thorpej * by Nick Hudson and 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.7 thorpej __KERNEL_RCSID(0, "$NetBSD: gfrtc.c,v 1.7 2025/09/07 21:45:15 thorpej Exp $"); 34 1.1 thorpej 35 1.1 thorpej #include <sys/param.h> 36 1.1 thorpej 37 1.1 thorpej #include <sys/bus.h> 38 1.1 thorpej #include <sys/device.h> 39 1.1 thorpej 40 1.1 thorpej #include <dev/goldfish/gfrtcvar.h> 41 1.1 thorpej 42 1.3 thorpej #define GOLDFISH_RTC_TIME_LOW 0x00 43 1.3 thorpej #define GOLDFISH_RTC_TIME_HIGH 0x04 44 1.3 thorpej #define GOLDFISH_RTC_ALARM_LOW 0x08 45 1.3 thorpej #define GOLDFISH_RTC_ALARM_HIGH 0x0c 46 1.3 thorpej #define GOLDFISH_RTC_IRQ_ENABLED 0x10 47 1.3 thorpej #define GOLDFISH_RTC_CLEAR_ALARM 0x14 48 1.3 thorpej #define GOLDFISH_RTC_ALARM_STATUS 0x18 49 1.3 thorpej #define GOLDFISH_RTC_CLEAR_INTERRUPT 0x1c 50 1.3 thorpej 51 1.1 thorpej /* 52 1.1 thorpej * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT 53 1.3 thorpej * 54 1.3 thorpej * (Despite what the Google docs say, the Qemu goldfish-rtc implements 55 1.3 thorpej * the timer functionality.) 56 1.1 thorpej */ 57 1.1 thorpej 58 1.1 thorpej #define GOLDFISH_RTC_READ(sc, reg) \ 59 1.1 thorpej bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 60 1.1 thorpej #define GOLDFISH_RTC_WRITE(sc, reg, val) \ 61 1.1 thorpej bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 62 1.1 thorpej 63 1.1 thorpej static int 64 1.1 thorpej gfrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) 65 1.1 thorpej { 66 1.7 thorpej struct gfrtc_softc *sc = device_private(ch->todr_dev); 67 1.3 thorpej const uint64_t nsec = gfrtc_get_time(sc); 68 1.1 thorpej 69 1.1 thorpej tv->tv_sec = nsec / 1000000000; 70 1.4 simonb tv->tv_usec = (nsec - tv->tv_sec * 1000000000) / 1000; 71 1.1 thorpej 72 1.1 thorpej return 0; 73 1.1 thorpej } 74 1.1 thorpej 75 1.1 thorpej static int 76 1.1 thorpej gfrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) 77 1.1 thorpej { 78 1.7 thorpej struct gfrtc_softc *sc = device_private(ch->todr_dev); 79 1.1 thorpej 80 1.3 thorpej const uint64_t nsec = (tv->tv_sec * 1000000 + tv->tv_usec) * 1000; 81 1.3 thorpej const uint32_t hi = (uint32_t)(nsec >> 32); 82 1.3 thorpej const uint32_t lo = (uint32_t)nsec; 83 1.1 thorpej 84 1.1 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, hi); 85 1.2 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_LOW, lo); 86 1.1 thorpej 87 1.1 thorpej return 0; 88 1.1 thorpej } 89 1.1 thorpej 90 1.1 thorpej 91 1.1 thorpej void 92 1.3 thorpej gfrtc_attach(struct gfrtc_softc * const sc, bool todr) 93 1.3 thorpej { 94 1.3 thorpej 95 1.3 thorpej aprint_naive("\n"); 96 1.3 thorpej aprint_normal(": Google Goldfish RTC + timer\n"); 97 1.3 thorpej 98 1.3 thorpej /* Cancel any alarms, make sure interrupt is disabled. */ 99 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_IRQ_ENABLED, 0); 100 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_CLEAR_ALARM, 0); 101 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_CLEAR_INTERRUPT, 0); 102 1.3 thorpej 103 1.3 thorpej if (todr) { 104 1.3 thorpej aprint_normal_dev(sc->sc_dev, 105 1.3 thorpej "using as Time of Day Register.\n"); 106 1.7 thorpej sc->sc_todr.todr_dev = sc->sc_dev; 107 1.3 thorpej sc->sc_todr.todr_gettime = gfrtc_gettime; 108 1.3 thorpej sc->sc_todr.todr_settime = gfrtc_settime; 109 1.3 thorpej todr_attach(&sc->sc_todr); 110 1.3 thorpej } 111 1.3 thorpej } 112 1.3 thorpej 113 1.3 thorpej 114 1.3 thorpej void 115 1.3 thorpej gfrtc_delay(device_t dev, unsigned int usec) 116 1.3 thorpej { 117 1.3 thorpej struct gfrtc_softc *sc = device_private(dev); 118 1.3 thorpej uint64_t start_ns, end_ns, min_ns; 119 1.3 thorpej 120 1.3 thorpej /* Get the start time now while we do the setup work. */ 121 1.3 thorpej start_ns = gfrtc_get_time(sc); 122 1.3 thorpej 123 1.3 thorpej /* Delay for this many nsec. */ 124 1.3 thorpej min_ns = (uint64_t)usec * 1000; 125 1.3 thorpej 126 1.3 thorpej do { 127 1.3 thorpej end_ns = gfrtc_get_time(sc); 128 1.3 thorpej } while ((end_ns - start_ns) < min_ns); 129 1.3 thorpej } 130 1.3 thorpej 131 1.3 thorpej 132 1.3 thorpej uint64_t 133 1.3 thorpej gfrtc_get_time(struct gfrtc_softc * const sc) 134 1.3 thorpej { 135 1.3 thorpej const uint64_t lo = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_LOW); 136 1.3 thorpej const uint64_t hi = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_HIGH); 137 1.3 thorpej 138 1.3 thorpej return (hi << 32) | lo; 139 1.3 thorpej } 140 1.3 thorpej 141 1.3 thorpej 142 1.3 thorpej void 143 1.5 isaki gfrtc_set_alarm(struct gfrtc_softc * const sc, const uint64_t next) 144 1.1 thorpej { 145 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_ALARM_HIGH, (uint32_t)(next >> 32)); 146 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_ALARM_LOW, (uint32_t)next); 147 1.3 thorpej } 148 1.3 thorpej 149 1.3 thorpej 150 1.3 thorpej void 151 1.3 thorpej gfrtc_clear_alarm(struct gfrtc_softc * const sc) 152 1.3 thorpej { 153 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_CLEAR_ALARM, 0); 154 1.3 thorpej } 155 1.3 thorpej 156 1.1 thorpej 157 1.3 thorpej void 158 1.3 thorpej gfrtc_enable_interrupt(struct gfrtc_softc * const sc) 159 1.3 thorpej { 160 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_IRQ_ENABLED, 1); 161 1.3 thorpej } 162 1.3 thorpej 163 1.3 thorpej 164 1.3 thorpej void 165 1.3 thorpej gfrtc_disable_interrupt(struct gfrtc_softc * const sc) 166 1.3 thorpej { 167 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_IRQ_ENABLED, 0); 168 1.3 thorpej } 169 1.3 thorpej 170 1.3 thorpej 171 1.3 thorpej void 172 1.3 thorpej gfrtc_clear_interrupt(struct gfrtc_softc * const sc) 173 1.3 thorpej { 174 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_CLEAR_INTERRUPT, 0); 175 1.1 thorpej } 176