gfrtc.c revision 1.2 1 /* $NetBSD: gfrtc.c,v 1.2 2023/12/31 22:06:41 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: gfrtc.c,v 1.2 2023/12/31 22:06:41 thorpej Exp $");
34
35 #include <sys/param.h>
36
37 #include <sys/bus.h>
38 #include <sys/device.h>
39
40 #include <dev/goldfish/gfrtcvar.h>
41
42 /*
43 * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT
44 */
45
46 #define GOLDFISH_RTC_READ(sc, reg) \
47 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
48 #define GOLDFISH_RTC_WRITE(sc, reg, val) \
49 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
50
51
52 #define GOLDFISH_RTC_TIME_LOW 0x00
53 #define GOLDFISH_RTC_TIME_HIGH 0x04
54
55
56 static int
57 gfrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
58 {
59 struct gfrtc_softc *sc = ch->cookie;
60 const uint64_t lo = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_LOW);
61 const uint64_t hi = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_HIGH);
62
63 uint64_t nsec = (hi << 32) | lo;
64
65 tv->tv_sec = nsec / 1000000000;
66 tv->tv_usec = (nsec - tv->tv_sec) / 1000; // Always 0?
67
68 return 0;
69 }
70
71 static int
72 gfrtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
73 {
74 struct gfrtc_softc *sc = ch->cookie;
75
76 uint64_t nsec = (tv->tv_sec * 1000000 + tv->tv_usec) * 1000;
77 uint32_t hi = nsec >> 32;
78 uint32_t lo = nsec;
79
80 GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, hi);
81 GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_LOW, lo);
82
83 return 0;
84 }
85
86
87 void
88 gfrtc_attach(struct gfrtc_softc * const sc)
89 {
90 sc->sc_todr.cookie = sc;
91 sc->sc_todr.todr_gettime = gfrtc_gettime;
92 sc->sc_todr.todr_settime = gfrtc_settime;
93 sc->sc_todr.todr_setwen = NULL;
94
95 todr_attach(&sc->sc_todr);
96 }
97