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/param.h> 36 #include <sys/systm.h> 37 #include <sys/bus.h> 38 #include <sys/clock.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/module.h> 42 #include <sys/mutex.h> 43 #include <sys/types.h> 44 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 48 #include <machine/bus.h> 49 #include <machine/resource.h> 50 #include <sys/rman.h> 51 52 #include "clock_if.h" 53 54 #define GOLDFISH_RTC_TIME_LOW 0x00 55 #define GOLDFISH_RTC_TIME_HIGH 0x04 56 57 struct goldfish_rtc_softc { 58 struct resource *res; 59 int rid; 60 struct mtx mtx; 61 }; 62 63 static int 64 goldfish_rtc_probe(device_t dev) 65 { 66 67 if (!ofw_bus_status_okay(dev)) 68 return (ENXIO); 69 70 if (ofw_bus_is_compatible(dev, "google,goldfish-rtc")) { 71 device_set_desc(dev, "Goldfish RTC"); 72 return (BUS_PROBE_DEFAULT); 73 } 74 75 return (ENXIO); 76 } 77 78 static int 79 goldfish_rtc_attach(device_t dev) 80 { 81 struct goldfish_rtc_softc *sc; 82 83 sc = device_get_softc(dev); 84 85 sc->rid = 0; 86 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid, 87 RF_ACTIVE); 88 if (sc->res == NULL) { 89 device_printf(dev, "could not allocate resource\n"); 90 return (ENXIO); 91 } 92 93 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); 94 95 /* 96 * Register as a system realtime clock with 1 second resolution. 97 */ 98 clock_register_flags(dev, 1000000, CLOCKF_SETTIME_NO_ADJ); 99 clock_schedule(dev, 1); 100 101 return (0); 102 } 103 104 static int 105 goldfish_rtc_detach(device_t dev) 106 { 107 struct goldfish_rtc_softc *sc; 108 109 sc = device_get_softc(dev); 110 111 clock_unregister(dev); 112 mtx_destroy(&sc->mtx); 113 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 114 115 return (0); 116 } 117 118 static int 119 goldfish_rtc_gettime(device_t dev, struct timespec *ts) 120 { 121 struct goldfish_rtc_softc *sc; 122 uint64_t low, high, nsec; 123 124 sc = device_get_softc(dev); 125 126 /* 127 * Reading TIME_HIGH is defined in the documentation to give the high 128 * 32 bits corresponding to the last TIME_LOW read, so must be done in 129 * that order, but means we have atomicity guaranteed. 130 */ 131 mtx_lock(&sc->mtx); 132 low = bus_read_4(sc->res, GOLDFISH_RTC_TIME_LOW); 133 high = bus_read_4(sc->res, GOLDFISH_RTC_TIME_HIGH); 134 mtx_unlock(&sc->mtx); 135 136 nsec = (high << 32) | low; 137 ts->tv_sec = nsec / 1000000000; 138 ts->tv_nsec = nsec % 1000000000; 139 140 return (0); 141 } 142 143 static int 144 goldfish_rtc_settime(device_t dev, struct timespec *ts) 145 { 146 struct goldfish_rtc_softc *sc; 147 uint64_t nsec; 148 149 sc = device_get_softc(dev); 150 151 /* 152 * We request a timespec with no resolution-adjustment. That also 153 * disables utc adjustment, so apply that ourselves. 154 */ 155 ts->tv_sec -= utc_offset(); 156 nsec = (uint64_t)ts->tv_sec * 1000000000 + ts->tv_nsec; 157 158 mtx_lock(&sc->mtx); 159 bus_write_4(sc->res, GOLDFISH_RTC_TIME_HIGH, nsec >> 32); 160 bus_write_4(sc->res, GOLDFISH_RTC_TIME_LOW, nsec); 161 mtx_unlock(&sc->mtx); 162 163 return (0); 164 } 165 166 static device_method_t goldfish_rtc_methods[] = { 167 /* Device interface */ 168 DEVMETHOD(device_probe, goldfish_rtc_probe), 169 DEVMETHOD(device_attach, goldfish_rtc_attach), 170 DEVMETHOD(device_detach, goldfish_rtc_detach), 171 172 /* Clock interface */ 173 DEVMETHOD(clock_gettime, goldfish_rtc_gettime), 174 DEVMETHOD(clock_settime, goldfish_rtc_settime), 175 176 DEVMETHOD_END, 177 }; 178 179 DEFINE_CLASS_0(goldfish_rtc, goldfish_rtc_driver, goldfish_rtc_methods, 180 sizeof(struct goldfish_rtc_softc)); 181 182 DRIVER_MODULE(goldfish_rtc, simplebus, goldfish_rtc_driver, NULL, NULL); 183