1 /* $NetBSD: gfrtc_fdt.c,v 1.3 2024/01/02 07:34:27 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_fdt.c,v 1.3 2024/01/02 07:34:27 thorpej 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 #include <dev/goldfish/gfrtcvar.h>
42
43 /*
44 * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT
45 */
46
47 static const struct device_compatible_entry compat_data[] = {
48 { .compat = "google,goldfish-rtc" },
49 DEVICE_COMPAT_EOL
50 };
51
52
53 static int
gfrtc_fdt_match(device_t parent,cfdata_t cf,void * aux)54 gfrtc_fdt_match(device_t parent, cfdata_t cf, void *aux)
55 {
56 struct fdt_attach_args * const faa = aux;
57
58 return of_compatible_match(faa->faa_phandle, compat_data);
59 }
60
61 static void
gfrtc_fdt_attach(device_t parent,device_t self,void * aux)62 gfrtc_fdt_attach(device_t parent, device_t self, void *aux)
63 {
64 struct gfrtc_softc * const sc = device_private(self);
65 struct fdt_attach_args * const faa = aux;
66 const int phandle = faa->faa_phandle;
67 bus_addr_t addr;
68 bus_size_t size;
69
70 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
71 aprint_error(": couldn't get registers\n");
72 return;
73 }
74
75 sc->sc_dev = self;
76 sc->sc_bst = faa->faa_bst;
77 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
78 aprint_error(": couldn't map registers\n");
79 return;
80 }
81
82 gfrtc_attach(sc, true);
83 }
84
85 CFATTACH_DECL_NEW(gfrtc_fdt, sizeof(struct gfrtc_softc),
86 gfrtc_fdt_match, gfrtc_fdt_attach, NULL, NULL);
87