1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020 Jessica Clarke <jrtc27@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * RTC for the goldfish virtual hardware platform implemented in QEMU, 30 * initially for Android but now also used for RISC-V's virt machine. 31 * 32 * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT 33 */ 34 35 #include <sys/cdefs.h> 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/bus.h> 39 #include <sys/clock.h> 40 #include <sys/kernel.h> 41 #include <sys/lock.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/types.h> 45 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 49 #include <machine/bus.h> 50 #include <machine/resource.h> 51 #include <sys/rman.h> 52 53 #include "clock_if.h" 54 55 #define GOLDFISH_RTC_TIME_LOW 0x00 56 #define GOLDFISH_RTC_TIME_HIGH 0x04 57 58 struct goldfish_rtc_softc { 59 struct resource *res; 60 int rid; 61 struct mtx mtx; 62 }; 63 64 static int 65 goldfish_rtc_probe(device_t dev) 66 { 67 68 if (!ofw_bus_status_okay(dev)) 69 return (ENXIO); 70 71 if (ofw_bus_is_compatible(dev, "google,goldfish-rtc")) { 72 device_set_desc(dev, "Goldfish RTC"); 73 return (BUS_PROBE_DEFAULT); 74 } 75 76 return (ENXIO); 77 } 78 79 static int 80 goldfish_rtc_attach(device_t dev) 81 { 82 struct goldfish_rtc_softc *sc; 83 84 sc = device_get_softc(dev); 85 86 sc->rid = 0; 87 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid, 88 RF_ACTIVE); 89 if (sc->res == NULL) { 90 device_printf(dev, "could not allocate resource\n"); 91 return (ENXIO); 92 } 93 94 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); 95 96 /* 97 * Register as a system realtime clock with 1 second resolution. 98 */ 99 clock_register_flags(dev, 1000000, CLOCKF_SETTIME_NO_ADJ); 100 clock_schedule(dev, 1); 101 102 return (0); 103 } 104 105 static int 106 goldfish_rtc_detach(device_t dev) 107 { 108 struct goldfish_rtc_softc *sc; 109 110 sc = device_get_softc(dev); 111 112 clock_unregister(dev); 113 mtx_destroy(&sc->mtx); 114 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 115 116 return (0); 117 } 118 119 static int 120 goldfish_rtc_gettime(device_t dev, struct timespec *ts) 121 { 122 struct goldfish_rtc_softc *sc; 123 uint64_t low, high, nsec; 124 125 sc = device_get_softc(dev); 126 127 /* 128 * Reading TIME_HIGH is defined in the documentation to give the high 129 * 32 bits corresponding to the last TIME_LOW read, so must be done in 130 * that order, but means we have atomicity guaranteed. 131 */ 132 mtx_lock(&sc->mtx); 133 low = bus_read_4(sc->res, GOLDFISH_RTC_TIME_LOW); 134 high = bus_read_4(sc->res, GOLDFISH_RTC_TIME_HIGH); 135 mtx_unlock(&sc->mtx); 136 137 nsec = (high << 32) | low; 138 ts->tv_sec = nsec / 1000000000; 139 ts->tv_nsec = nsec % 1000000000; 140 141 return (0); 142 } 143 144 static int 145 goldfish_rtc_settime(device_t dev, struct timespec *ts) 146 { 147 struct goldfish_rtc_softc *sc; 148 uint64_t nsec; 149 150 sc = device_get_softc(dev); 151 152 /* 153 * We request a timespec with no resolution-adjustment. That also 154 * disables utc adjustment, so apply that ourselves. 155 */ 156 ts->tv_sec -= utc_offset(); 157 nsec = (uint64_t)ts->tv_sec * 1000000000 + ts->tv_nsec; 158 159 mtx_lock(&sc->mtx); 160 bus_write_4(sc->res, GOLDFISH_RTC_TIME_HIGH, nsec >> 32); 161 bus_write_4(sc->res, GOLDFISH_RTC_TIME_LOW, nsec); 162 mtx_unlock(&sc->mtx); 163 164 return (0); 165 } 166 167 static device_method_t goldfish_rtc_methods[] = { 168 /* Device interface */ 169 DEVMETHOD(device_probe, goldfish_rtc_probe), 170 DEVMETHOD(device_attach, goldfish_rtc_attach), 171 DEVMETHOD(device_detach, goldfish_rtc_detach), 172 173 /* Clock interface */ 174 DEVMETHOD(clock_gettime, goldfish_rtc_gettime), 175 DEVMETHOD(clock_settime, goldfish_rtc_settime), 176 177 DEVMETHOD_END, 178 }; 179 180 DEFINE_CLASS_0(goldfish_rtc, goldfish_rtc_driver, goldfish_rtc_methods, 181 sizeof(struct goldfish_rtc_softc)); 182 183 DRIVER_MODULE(goldfish_rtc, simplebus, goldfish_rtc_driver, NULL, NULL); 184