Home | History | Annotate | Line # | Download | only in dev
gfrtc_mainbus.c revision 1.2
      1 /*	$NetBSD: gfrtc_mainbus.c,v 1.2 2024/01/12 06:23:20 mlelstv 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.2 2024/01/12 06:23:20 mlelstv 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 	void			(*sc_handler)(struct clockframe *);
     62 	struct evcnt *		sc_evcnt;
     63 	void *			sc_ih;
     64 };
     65 
     66 #define	CLOCK_HANDLER()							\
     67 do {									\
     68 	/* Clear the interrupt condition. */				\
     69 	gfrtc_clear_interrupt(&sc->sc_gfrtc);				\
     70 									\
     71 	/* Get the next alarm set ASAP. */				\
     72 	gfrtc_set_alarm(&sc->sc_gfrtc, sc->sc_interval_ns);		\
     73 									\
     74 	/* Increment the counter and call the handler. */		\
     75 	sc->sc_evcnt->ev_count++;					\
     76 	sc->sc_handler((struct clockframe *)v);				\
     77 } while (/*CONSTCOND*/0)
     78 
     79 static int
     80 gfrtc_mainbus_hardclock(void *v)
     81 {
     82 	struct gfrtc_mainbus_softc *sc = clock_devices[CLOCK_HARDCLOCK];
     83 
     84 	CLOCK_HANDLER();
     85 	return 1;
     86 }
     87 
     88 static void *gfrtc_isrs[] = {
     89 [CLOCK_HARDCLOCK]	=	gfrtc_mainbus_hardclock,
     90 };
     91 
     92 static void
     93 gfrtc_mainbus_initclock(void *arg, unsigned int interval_us,
     94     struct evcnt *ev, void (*func)(struct clockframe *))
     95 {
     96 	struct gfrtc_mainbus_softc *sc = arg;
     97 
     98 	sc->sc_interval_ns = (uint64_t)interval_us * 1000;
     99 	sc->sc_handler = func;
    100 	sc->sc_evcnt = ev;
    101 
    102 	/* Enable the interrupt. */
    103 	gfrtc_enable_interrupt(&sc->sc_gfrtc);
    104 
    105 	/* Start the first alarm! */
    106 	gfrtc_set_alarm(&sc->sc_gfrtc, sc->sc_interval_ns);
    107 }
    108 
    109 static int
    110 gfrtc_mainbus_match(device_t parent, cfdata_t cf, void *aux)
    111 {
    112 	struct mainbus_attach_args * const ma = aux;
    113 
    114 	return mainbus_compatible_match(ma, compat_data);
    115 }
    116 
    117 static void
    118 gfrtc_mainbus_attach(device_t parent, device_t self, void *aux)
    119 {
    120 	struct gfrtc_mainbus_softc * const sc = device_private(self);
    121 	struct mainbus_attach_args * const ma = aux;
    122 	const struct device_compatible_entry *dce;
    123 	char strbuf[INTR_STRING_BUFSIZE];
    124 
    125 	sc->sc_gfrtc.sc_dev = self;
    126 	sc->sc_gfrtc.sc_bst = ma->ma_st;
    127 	if (bus_space_map(sc->sc_gfrtc.sc_bst, ma->ma_addr, ma->ma_size, 0,
    128 			  &sc->sc_gfrtc.sc_bsh) != 0) {
    129 		aprint_error(": couldn't map registers\n");
    130 		return;
    131 	}
    132 
    133 	dce = mainbus_compatible_lookup(ma, compat_data);
    134 	KASSERT(dce != NULL);
    135 
    136 	gfrtc_attach(&sc->sc_gfrtc, (dce->value == CLOCK_NONE));
    137 
    138 	switch (dce->value) {
    139 	case CLOCK_HARDCLOCK:
    140 		/*
    141 		 * We are the one of the clock interrupts.
    142 		 */
    143 		sc->sc_ih = intr_establish(gfrtc_isrs[(int)dce->value],
    144 		    NULL, ma->ma_irq, IPL_SCHED, 0);
    145 		KASSERT(sc->sc_ih != NULL);
    146 		aprint_normal_dev(self, "%s interrupting at %s\n",
    147 		    clock_name((int)dce->value),
    148 		    intr_string(sc->sc_ih, strbuf, sizeof(strbuf)));
    149 		sc->sc_clock_args.ca_initfunc = gfrtc_mainbus_initclock;
    150 		sc->sc_clock_args.ca_arg = sc;
    151 		sc->sc_clock_args.ca_which = (int)dce->value;
    152 		clock_attach(self, &sc->sc_clock_args, gfrtc_delay);
    153 		break;
    154 
    155 	default:
    156 		break;
    157 	}
    158 }
    159 
    160 CFATTACH_DECL_NEW(gfrtc_mainbus, sizeof(struct gfrtc_mainbus_softc),
    161 	gfrtc_mainbus_match, gfrtc_mainbus_attach, NULL, NULL);
    162