2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Jessica Clarke <jrtc27@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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.
32 * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/clock.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/types.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
57 #define GOLDFISH_RTC_TIME_LOW 0x00
58 #define GOLDFISH_RTC_TIME_HIGH 0x04
60 struct goldfish_rtc_softc {
67 goldfish_rtc_probe(device_t dev)
70 if (!ofw_bus_status_okay(dev))
73 if (ofw_bus_is_compatible(dev, "google,goldfish-rtc")) {
74 device_set_desc(dev, "Goldfish RTC");
75 return (BUS_PROBE_DEFAULT);
82 goldfish_rtc_attach(device_t dev)
84 struct goldfish_rtc_softc *sc;
86 sc = device_get_softc(dev);
89 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
91 if (sc->res == NULL) {
92 device_printf(dev, "could not allocate resource\n");
96 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
99 * Register as a system realtime clock with 1 second resolution.
101 clock_register_flags(dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
102 clock_schedule(dev, 1);
108 goldfish_rtc_detach(device_t dev)
110 struct goldfish_rtc_softc *sc;
112 sc = device_get_softc(dev);
114 clock_unregister(dev);
115 mtx_destroy(&sc->mtx);
116 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
122 goldfish_rtc_gettime(device_t dev, struct timespec *ts)
124 struct goldfish_rtc_softc *sc;
125 uint64_t low, high, nsec;
127 sc = device_get_softc(dev);
130 * Reading TIME_HIGH is defined in the documentation to give the high
131 * 32 bits corresponding to the last TIME_LOW read, so must be done in
132 * that order, but means we have atomicity guaranteed.
135 low = bus_read_4(sc->res, GOLDFISH_RTC_TIME_LOW);
136 high = bus_read_4(sc->res, GOLDFISH_RTC_TIME_HIGH);
137 mtx_unlock(&sc->mtx);
139 nsec = (high << 32) | low;
140 ts->tv_sec = nsec / 1000000000;
141 ts->tv_nsec = nsec % 1000000000;
147 goldfish_rtc_settime(device_t dev, struct timespec *ts)
149 struct goldfish_rtc_softc *sc;
152 sc = device_get_softc(dev);
154 nsec = (uint64_t)ts->tv_sec * 1000000000 + ts->tv_nsec;
157 bus_write_4(sc->res, GOLDFISH_RTC_TIME_HIGH, nsec >> 32);
158 bus_write_4(sc->res, GOLDFISH_RTC_TIME_LOW, nsec);
159 mtx_unlock(&sc->mtx);
164 static device_method_t goldfish_rtc_methods[] = {
165 /* Device interface */
166 DEVMETHOD(device_probe, goldfish_rtc_probe),
167 DEVMETHOD(device_attach, goldfish_rtc_attach),
168 DEVMETHOD(device_detach, goldfish_rtc_detach),
170 /* Clock interface */
171 DEVMETHOD(clock_gettime, goldfish_rtc_gettime),
172 DEVMETHOD(clock_settime, goldfish_rtc_settime),
177 DEFINE_CLASS_0(goldfish_rtc, goldfish_rtc_driver, goldfish_rtc_methods,
178 sizeof(struct goldfish_rtc_softc));
179 static devclass_t goldfish_rtc_devclass;
181 DRIVER_MODULE(goldfish_rtc, simplebus, goldfish_rtc_driver,
182 goldfish_rtc_devclass, NULL, NULL);