xref: /freebsd/sys/dev/goldfish/goldfish_rtc.c (revision a521f2116473fbd8c09db395518f060a27d02334)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/clock.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/types.h>
47 
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <sys/rman.h>
54 
55 #include "clock_if.h"
56 
57 #define	GOLDFISH_RTC_TIME_LOW	0x00
58 #define	GOLDFISH_RTC_TIME_HIGH	0x04
59 
60 struct goldfish_rtc_softc {
61 	struct resource	*res;
62 	int		rid;
63 	struct mtx	mtx;
64 };
65 
66 static int
67 goldfish_rtc_probe(device_t dev)
68 {
69 
70 	if (!ofw_bus_status_okay(dev))
71 		return (ENXIO);
72 
73 	if (ofw_bus_is_compatible(dev, "google,goldfish-rtc")) {
74 		device_set_desc(dev, "Goldfish RTC");
75 		return (BUS_PROBE_DEFAULT);
76 	}
77 
78 	return (ENXIO);
79 }
80 
81 static int
82 goldfish_rtc_attach(device_t dev)
83 {
84 	struct goldfish_rtc_softc *sc;
85 
86 	sc = device_get_softc(dev);
87 
88 	sc->rid = 0;
89 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
90 	    RF_ACTIVE);
91 	if (sc->res == NULL) {
92 		device_printf(dev, "could not allocate resource\n");
93 		return (ENXIO);
94 	}
95 
96 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
97 
98 	/*
99 	 * Register as a system realtime clock with 1 second resolution.
100 	 */
101 	clock_register_flags(dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
102 	clock_schedule(dev, 1);
103 
104 	return (0);
105 }
106 
107 static int
108 goldfish_rtc_detach(device_t dev)
109 {
110 	struct goldfish_rtc_softc *sc;
111 
112 	sc = device_get_softc(dev);
113 
114 	clock_unregister(dev);
115 	mtx_destroy(&sc->mtx);
116 	bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
117 
118 	return (0);
119 }
120 
121 static int
122 goldfish_rtc_gettime(device_t dev, struct timespec *ts)
123 {
124 	struct goldfish_rtc_softc *sc;
125 	uint64_t low, high, nsec;
126 
127 	sc = device_get_softc(dev);
128 
129 	/*
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.
133 	 */
134 	mtx_lock(&sc->mtx);
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);
138 
139 	nsec = (high << 32) | low;
140 	ts->tv_sec = nsec / 1000000000;
141 	ts->tv_nsec = nsec % 1000000000;
142 
143 	return (0);
144 }
145 
146 static int
147 goldfish_rtc_settime(device_t dev, struct timespec *ts)
148 {
149 	struct goldfish_rtc_softc *sc;
150 	uint64_t nsec;
151 
152 	sc = device_get_softc(dev);
153 
154 	nsec = (uint64_t)ts->tv_sec * 1000000000 + ts->tv_nsec;
155 
156 	mtx_lock(&sc->mtx);
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);
160 
161 	return (0);
162 }
163 
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),
169 
170 	/* Clock interface */
171 	DEVMETHOD(clock_gettime,	goldfish_rtc_gettime),
172 	DEVMETHOD(clock_settime,	goldfish_rtc_settime),
173 
174 	DEVMETHOD_END,
175 };
176 
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;
180 
181 DRIVER_MODULE(goldfish_rtc, simplebus, goldfish_rtc_driver,
182     goldfish_rtc_devclass, NULL, NULL);
183