xref: /netbsd-src/sys/dev/fdt/gfrtc_fdt.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1 /*	$NetBSD: gfrtc_fdt.c,v 1.1 2023/05/08 07:51:44 skrll 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_fdt.c,v 1.1 2023/05/08 07:51:44 skrll Exp $");
34 
35 #include <sys/param.h>
36 
37 #include <sys/bus.h>
38 #include <sys/device.h>
39 
40 #include <dev/fdt/fdtvar.h>
41 
42 /*
43  * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT
44  */
45 
46 static const struct device_compatible_entry compat_data[] = {
47 	{ .compat = "google,goldfish-rtc" },
48 	DEVICE_COMPAT_EOL
49 };
50 
51 struct gfrtc_fdt_softc {
52 	device_t sc_dev;
53 	bus_space_tag_t sc_bst;
54 	bus_space_handle_t sc_bsh;
55 
56 	struct todr_chip_handle	sc_todr;
57 };
58 
59 #define	GOLDFISH_RTC_READ(sc, reg) \
60 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
61 #define	GOLDFISH_RTC_WRITE(sc, reg, val) \
62 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
63 
64 
65 #define	GOLDFISH_RTC_TIME_LOW	0x00
66 #define	GOLDFISH_RTC_TIME_HIGH	0x04
67 
68 
69 static int
70 gfrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
71 {
72 	struct gfrtc_fdt_softc *sc = ch->cookie;
73 	const uint64_t lo = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_LOW);
74 	const uint64_t hi = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_HIGH);
75 
76 	uint64_t nsec = (hi << 32) | lo;
77 
78 	tv->tv_sec = nsec / 1000000000;
79 	tv->tv_usec = (nsec - tv->tv_sec) / 1000;	// Always 0?
80 
81 	return 0;
82 }
83 
84 static int
85 gfrtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
86 {
87 	struct gfrtc_fdt_softc *sc = ch->cookie;
88 
89 	uint64_t nsec = (tv->tv_sec * 1000000 + tv->tv_usec) * 1000;
90 	uint32_t hi = nsec >> 32;
91 	uint32_t lo = nsec;
92 
93 	GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, hi);
94 	GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, lo);
95 
96 	return 0;
97 }
98 
99 
100 static int
101 gfrtc_fdt_match(device_t parent, cfdata_t cf, void *aux)
102 {
103 	struct fdt_attach_args * const faa = aux;
104 
105 	return of_compatible_match(faa->faa_phandle, compat_data);
106 }
107 
108 static void
109 gfrtc_fdt_attach(device_t parent, device_t self, void *aux)
110 {
111 	struct gfrtc_fdt_softc * const sc = device_private(self);
112 	struct fdt_attach_args * const faa = aux;
113 	const int phandle = faa->faa_phandle;
114 	bus_addr_t addr;
115 	bus_size_t size;
116 
117 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
118 		aprint_error(": couldn't get registers\n");
119 		return;
120 	}
121 
122 	sc->sc_dev = self;
123 	sc->sc_bst = faa->faa_bst;
124 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
125 		aprint_error(": couldn't map registers\n");
126 		return;
127 	}
128 
129 	aprint_naive("\n");
130 	aprint_normal(": Google Goldfish RTC\n");
131 
132 	sc->sc_todr.cookie = sc;
133 	sc->sc_todr.todr_gettime = gfrtc_gettime;
134 	sc->sc_todr.todr_settime = gfrtc_settime;
135 	sc->sc_todr.todr_setwen = NULL;
136 
137 	todr_attach(&sc->sc_todr);
138 }
139 
140 CFATTACH_DECL_NEW(gfrtc_fdt, sizeof(struct gfrtc_fdt_softc),
141 	gfrtc_fdt_match, gfrtc_fdt_attach, NULL, NULL);
142