gfrtc.c revision 1.5 1 1.5 isaki /* $NetBSD: gfrtc.c,v 1.5 2024/03/05 11:19:30 isaki 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.5 isaki __KERNEL_RCSID(0, "$NetBSD: gfrtc.c,v 1.5 2024/03/05 11:19:30 isaki 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.1 thorpej struct gfrtc_softc *sc = ch->cookie;
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.1 thorpej struct gfrtc_softc *sc = ch->cookie;
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.3 thorpej sc->sc_todr.cookie = sc;
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 sc->sc_todr.todr_setwen = NULL;
110 1.3 thorpej todr_attach(&sc->sc_todr);
111 1.3 thorpej }
112 1.3 thorpej }
113 1.3 thorpej
114 1.3 thorpej
115 1.3 thorpej void
116 1.3 thorpej gfrtc_delay(device_t dev, unsigned int usec)
117 1.3 thorpej {
118 1.3 thorpej struct gfrtc_softc *sc = device_private(dev);
119 1.3 thorpej uint64_t start_ns, end_ns, min_ns;
120 1.3 thorpej
121 1.3 thorpej /* Get the start time now while we do the setup work. */
122 1.3 thorpej start_ns = gfrtc_get_time(sc);
123 1.3 thorpej
124 1.3 thorpej /* Delay for this many nsec. */
125 1.3 thorpej min_ns = (uint64_t)usec * 1000;
126 1.3 thorpej
127 1.3 thorpej do {
128 1.3 thorpej end_ns = gfrtc_get_time(sc);
129 1.3 thorpej } while ((end_ns - start_ns) < min_ns);
130 1.3 thorpej }
131 1.3 thorpej
132 1.3 thorpej
133 1.3 thorpej uint64_t
134 1.3 thorpej gfrtc_get_time(struct gfrtc_softc * const sc)
135 1.3 thorpej {
136 1.3 thorpej const uint64_t lo = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_LOW);
137 1.3 thorpej const uint64_t hi = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_HIGH);
138 1.3 thorpej
139 1.3 thorpej return (hi << 32) | lo;
140 1.3 thorpej }
141 1.3 thorpej
142 1.3 thorpej
143 1.3 thorpej void
144 1.5 isaki gfrtc_set_alarm(struct gfrtc_softc * const sc, const uint64_t next)
145 1.1 thorpej {
146 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_ALARM_HIGH, (uint32_t)(next >> 32));
147 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_ALARM_LOW, (uint32_t)next);
148 1.3 thorpej }
149 1.3 thorpej
150 1.3 thorpej
151 1.3 thorpej void
152 1.3 thorpej gfrtc_clear_alarm(struct gfrtc_softc * const sc)
153 1.3 thorpej {
154 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_CLEAR_ALARM, 0);
155 1.3 thorpej }
156 1.3 thorpej
157 1.1 thorpej
158 1.3 thorpej void
159 1.3 thorpej gfrtc_enable_interrupt(struct gfrtc_softc * const sc)
160 1.3 thorpej {
161 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_IRQ_ENABLED, 1);
162 1.3 thorpej }
163 1.3 thorpej
164 1.3 thorpej
165 1.3 thorpej void
166 1.3 thorpej gfrtc_disable_interrupt(struct gfrtc_softc * const sc)
167 1.3 thorpej {
168 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_IRQ_ENABLED, 0);
169 1.3 thorpej }
170 1.3 thorpej
171 1.3 thorpej
172 1.3 thorpej void
173 1.3 thorpej gfrtc_clear_interrupt(struct gfrtc_softc * const sc)
174 1.3 thorpej {
175 1.3 thorpej GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_CLEAR_INTERRUPT, 0);
176 1.1 thorpej }
177