1 /* $NetBSD: gfrtc_mainbus.c,v 1.3 2024/03/05 11:19:30 isaki Exp $ */
2
3 /*-
4 * Copyright (c) 2023, 2024 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 and by Jason R. Thorpe.
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_mainbus.c,v 1.3 2024/03/05 11:19:30 isaki Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/systm.h>
40
41 #include <machine/clockvar.h>
42
43 #include <virt68k/dev/mainbusvar.h>
44
45 #include <dev/goldfish/gfrtcvar.h>
46
47 static const struct device_compatible_entry compat_data[] = {
48 { .compat = "google,goldfish-rtc",
49 .value = CLOCK_NONE },
50
51 { .compat = "netbsd,goldfish-rtc-hardclock",
52 .value = CLOCK_HARDCLOCK },
53
54 DEVICE_COMPAT_EOL
55 };
56
57 struct gfrtc_mainbus_softc {
58 struct gfrtc_softc sc_gfrtc;
59 struct clock_attach_args sc_clock_args;
60 uint64_t sc_interval_ns;
61 uint64_t sc_alarm_time;
62 void (*sc_handler)(struct clockframe *);
63 struct evcnt * sc_evcnt;
64 void * sc_ih;
65 };
66
67 #define CLOCK_HANDLER() \
68 do { \
69 /* Clear the interrupt condition. */ \
70 gfrtc_clear_interrupt(&sc->sc_gfrtc); \
71 \
72 /* Get the next alarm set. */ \
73 sc->sc_alarm_time += sc->sc_interval_ns; \
74 gfrtc_set_alarm(&sc->sc_gfrtc, sc->sc_alarm_time); \
75 \
76 /* Increment the counter and call the handler. */ \
77 sc->sc_evcnt->ev_count++; \
78 sc->sc_handler((struct clockframe *)v); \
79 } while (/*CONSTCOND*/0)
80
81 static int
gfrtc_mainbus_hardclock(void * v)82 gfrtc_mainbus_hardclock(void *v)
83 {
84 struct gfrtc_mainbus_softc *sc = clock_devices[CLOCK_HARDCLOCK];
85
86 CLOCK_HANDLER();
87 return 1;
88 }
89
90 static void *gfrtc_isrs[] = {
91 [CLOCK_HARDCLOCK] = gfrtc_mainbus_hardclock,
92 };
93
94 static void
gfrtc_mainbus_initclock(void * arg,unsigned int interval_us,struct evcnt * ev,void (* func)(struct clockframe *))95 gfrtc_mainbus_initclock(void *arg, unsigned int interval_us,
96 struct evcnt *ev, void (*func)(struct clockframe *))
97 {
98 struct gfrtc_mainbus_softc *sc = arg;
99
100 sc->sc_interval_ns = (uint64_t)interval_us * 1000;
101 sc->sc_handler = func;
102 sc->sc_evcnt = ev;
103
104 /* Enable the interrupt. */
105 gfrtc_enable_interrupt(&sc->sc_gfrtc);
106
107 /* Start the first alarm! */
108 sc->sc_alarm_time = gfrtc_get_time(&sc->sc_gfrtc) + sc->sc_interval_ns;
109 gfrtc_set_alarm(&sc->sc_gfrtc, sc->sc_alarm_time);
110 }
111
112 static int
gfrtc_mainbus_match(device_t parent,cfdata_t cf,void * aux)113 gfrtc_mainbus_match(device_t parent, cfdata_t cf, void *aux)
114 {
115 struct mainbus_attach_args * const ma = aux;
116
117 return mainbus_compatible_match(ma, compat_data);
118 }
119
120 static void
gfrtc_mainbus_attach(device_t parent,device_t self,void * aux)121 gfrtc_mainbus_attach(device_t parent, device_t self, void *aux)
122 {
123 struct gfrtc_mainbus_softc * const sc = device_private(self);
124 struct mainbus_attach_args * const ma = aux;
125 const struct device_compatible_entry *dce;
126 char strbuf[INTR_STRING_BUFSIZE];
127
128 sc->sc_gfrtc.sc_dev = self;
129 sc->sc_gfrtc.sc_bst = ma->ma_st;
130 if (bus_space_map(sc->sc_gfrtc.sc_bst, ma->ma_addr, ma->ma_size, 0,
131 &sc->sc_gfrtc.sc_bsh) != 0) {
132 aprint_error(": couldn't map registers\n");
133 return;
134 }
135
136 dce = mainbus_compatible_lookup(ma, compat_data);
137 KASSERT(dce != NULL);
138
139 gfrtc_attach(&sc->sc_gfrtc, (dce->value == CLOCK_NONE));
140
141 switch (dce->value) {
142 case CLOCK_HARDCLOCK:
143 /*
144 * We are the one of the clock interrupts.
145 */
146 sc->sc_ih = intr_establish(gfrtc_isrs[(int)dce->value],
147 NULL, ma->ma_irq, IPL_SCHED, 0);
148 KASSERT(sc->sc_ih != NULL);
149 aprint_normal_dev(self, "%s interrupting at %s\n",
150 clock_name((int)dce->value),
151 intr_string(sc->sc_ih, strbuf, sizeof(strbuf)));
152 sc->sc_clock_args.ca_initfunc = gfrtc_mainbus_initclock;
153 sc->sc_clock_args.ca_arg = sc;
154 sc->sc_clock_args.ca_which = (int)dce->value;
155 clock_attach(self, &sc->sc_clock_args, gfrtc_delay);
156 break;
157
158 default:
159 break;
160 }
161 }
162
163 CFATTACH_DECL_NEW(gfrtc_mainbus, sizeof(struct gfrtc_mainbus_softc),
164 gfrtc_mainbus_match, gfrtc_mainbus_attach, NULL, NULL);
165