#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: wmrtc.c,v 1.3 2025/09/07 21:45:12 thorpej Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <dev/clock_subr.h>
#include <epoc32/windermere/windermerereg.h>
#include <epoc32/windermere/windermerevar.h>
#include "locators.h"
#define RTC_SIZE 0x100
struct wmrtc_softc {
device_t sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
struct todr_chip_handle sc_todr;
};
static int wmrtc_match(device_t, cfdata_t, void *);
static void wmrtc_attach(device_t, device_t, void *);
static int wmrtc_todr_gettime(todr_chip_handle_t, struct timeval *);
static int wmrtc_todr_settime(todr_chip_handle_t, struct timeval *);
CFATTACH_DECL_NEW(wmrtc, sizeof(struct wmrtc_softc),
wmrtc_match, wmrtc_attach, NULL, NULL);
static int
wmrtc_match(device_t parent, cfdata_t match, void *aux)
{
struct windermere_attach_args *aa = aux;
if (aa->aa_offset == WINDERMERECF_OFFSET_DEFAULT)
return 0;
aa->aa_size = RTC_SIZE;
return 1;
}
static void
wmrtc_attach(device_t parent, device_t self, void *aux)
{
struct wmrtc_softc *sc = device_private(self);
struct windermere_attach_args *aa = aux;
aprint_naive("\n");
aprint_normal("\n");
sc->sc_dev = self;
if (windermere_bus_space_subregion(aa->aa_iot, *aa->aa_ioh,
aa->aa_offset, aa->aa_size, &sc->sc_ioh) != 0) {
aprint_error_dev(self, "can't map registers\n");
return;
}
sc->sc_iot = aa->aa_iot;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_MRL, 0);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_MRH, 0);
sc->sc_todr.todr_dev = self;
sc->sc_todr.todr_gettime = wmrtc_todr_gettime;
sc->sc_todr.todr_settime = wmrtc_todr_settime;
todr_attach(&sc->sc_todr);
}
static int
wmrtc_todr_gettime(todr_chip_handle_t ch, struct timeval *tv)
{
struct wmrtc_softc *sc = device_private(ch->todr_dev);
tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_DRL) & 0xffff;
tv->tv_sec |= (bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_DRH) << 16);
tv->tv_usec = 0;
return 0;
}
static int
wmrtc_todr_settime(todr_chip_handle_t ch, struct timeval *tv)
{
struct wmrtc_softc *sc = device_private(ch->todr_dev);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_DRL, tv->tv_sec & 0xffff);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_DRH,
(tv->tv_sec >> 16) & 0xffff);
return 0;
}