Home | History | Annotate | Line # | Download | only in windermere
      1 /*      $NetBSD: wmrtc.c,v 1.3 2025/09/07 21:45:12 thorpej Exp $      */
      2 /*
      3  * Copyright (c) 2012 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: wmrtc.c,v 1.3 2025/09/07 21:45:12 thorpej Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/bus.h>
     32 #include <sys/device.h>
     33 #include <sys/errno.h>
     34 
     35 #include <dev/clock_subr.h>
     36 
     37 #include <epoc32/windermere/windermerereg.h>
     38 #include <epoc32/windermere/windermerevar.h>
     39 
     40 #include "locators.h"
     41 
     42 #define RTC_SIZE	0x100
     43 
     44 struct wmrtc_softc {
     45 	device_t sc_dev;
     46 	bus_space_tag_t sc_iot;
     47 	bus_space_handle_t sc_ioh;
     48 
     49 	struct todr_chip_handle sc_todr;
     50 };
     51 
     52 static int wmrtc_match(device_t, cfdata_t, void *);
     53 static void wmrtc_attach(device_t, device_t, void *);
     54 
     55 /* todr(9) interfaces */
     56 static int wmrtc_todr_gettime(todr_chip_handle_t, struct timeval *);
     57 static int wmrtc_todr_settime(todr_chip_handle_t, struct timeval *);
     58 
     59 CFATTACH_DECL_NEW(wmrtc, sizeof(struct wmrtc_softc),
     60     wmrtc_match, wmrtc_attach, NULL, NULL);
     61 
     62 
     63 /* ARGSUSED */
     64 static int
     65 wmrtc_match(device_t parent, cfdata_t match, void *aux)
     66 {
     67 	struct windermere_attach_args *aa = aux;
     68 
     69 	/* Wildcard not accept */
     70 	if (aa->aa_offset == WINDERMERECF_OFFSET_DEFAULT)
     71 		return 0;
     72 
     73 	aa->aa_size = RTC_SIZE;
     74 	return 1;
     75 }
     76 
     77 /* ARGSUSED */
     78 static void
     79 wmrtc_attach(device_t parent, device_t self, void *aux)
     80 {
     81 	struct wmrtc_softc *sc = device_private(self);
     82 	struct windermere_attach_args *aa = aux;
     83 
     84 	aprint_naive("\n");
     85 	aprint_normal("\n");
     86 
     87 	sc->sc_dev = self;
     88 	if (windermere_bus_space_subregion(aa->aa_iot, *aa->aa_ioh,
     89 				aa->aa_offset, aa->aa_size, &sc->sc_ioh) != 0) {
     90 		aprint_error_dev(self, "can't map registers\n");
     91 		return;
     92 	}
     93 	sc->sc_iot = aa->aa_iot;
     94 
     95 	/* XXXX: reset to compare registers */
     96 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_MRL, 0);
     97 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_MRH, 0);
     98 
     99 	sc->sc_todr.todr_dev = self;
    100 	sc->sc_todr.todr_gettime = wmrtc_todr_gettime;
    101 	sc->sc_todr.todr_settime = wmrtc_todr_settime;
    102 	todr_attach(&sc->sc_todr);
    103 }
    104 
    105 static int
    106 wmrtc_todr_gettime(todr_chip_handle_t ch, struct timeval *tv)
    107 {
    108 	struct wmrtc_softc *sc = device_private(ch->todr_dev);
    109 
    110 	tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_DRL) & 0xffff;
    111 	tv->tv_sec |= (bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_DRH) << 16);
    112 	tv->tv_usec = 0;
    113 	return 0;
    114 }
    115 
    116 static int
    117 wmrtc_todr_settime(todr_chip_handle_t ch, struct timeval *tv)
    118 {
    119 	struct wmrtc_softc *sc = device_private(ch->todr_dev);
    120 
    121 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_DRL, tv->tv_sec & 0xffff);
    122 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_DRH,
    123 	    (tv->tv_sec >> 16) & 0xffff);
    124 	return 0;
    125 }
    126