Home | History | Annotate | Line # | Download | only in sunxi
sunxi_rtc.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: sunxi_rtc.c,v 1.1 2017/06/29 19:38:24 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2014-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_rtc.c,v 1.1 2017/06/29 19:38:24 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/mutex.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/clock_subr.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #define	RTC_YY_MM_DD_REG	0x10
     44  1.1  jmcneill #define	 SUNXI_RTC_LEAP		__BIT(22)
     45  1.1  jmcneill #define	 SUNXI_RTC_YEAR		__BITS(21,16)
     46  1.1  jmcneill #define	 SUNXI_RTC_MONTH	__BITS(11,8)
     47  1.1  jmcneill #define	 SUNXI_RTC_DAY		__BITS(4,0)
     48  1.1  jmcneill #define	RTC_HH_MM_SS_REG	0x14
     49  1.1  jmcneill #define	 SUNXI_RTC_WK_NO	__BITS(31,29)
     50  1.1  jmcneill #define	 SUNXI_RTC_HOUR		__BITS(20,16)
     51  1.1  jmcneill #define	 SUNXI_RTC_MINUTE	__BITS(13,8)
     52  1.1  jmcneill #define	 SUNXI_RTC_SECOND	__BITS(5,0)
     53  1.1  jmcneill 
     54  1.1  jmcneill #define	SUNXI_RTC_BASE_YEAR	2000
     55  1.1  jmcneill 
     56  1.1  jmcneill static const char * const compatible[] = {
     57  1.1  jmcneill 	"allwinner,sun6i-a31-rtc",
     58  1.1  jmcneill 	NULL
     59  1.1  jmcneill };
     60  1.1  jmcneill 
     61  1.1  jmcneill struct sunxi_rtc_softc {
     62  1.1  jmcneill 	device_t sc_dev;
     63  1.1  jmcneill 	bus_space_tag_t sc_bst;
     64  1.1  jmcneill 	bus_space_handle_t sc_bsh;
     65  1.1  jmcneill 	struct todr_chip_handle sc_todr;
     66  1.1  jmcneill };
     67  1.1  jmcneill 
     68  1.1  jmcneill #define RTC_READ(sc, reg) \
     69  1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     70  1.1  jmcneill #define RTC_WRITE(sc, reg, val) \
     71  1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     72  1.1  jmcneill 
     73  1.1  jmcneill static int	sunxi_rtc_match(device_t, cfdata_t, void *);
     74  1.1  jmcneill static void	sunxi_rtc_attach(device_t, device_t, void *);
     75  1.1  jmcneill 
     76  1.1  jmcneill static int	sunxi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
     77  1.1  jmcneill static int	sunxi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
     78  1.1  jmcneill 
     79  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_rtc, sizeof(struct sunxi_rtc_softc),
     80  1.1  jmcneill 	sunxi_rtc_match, sunxi_rtc_attach, NULL, NULL);
     81  1.1  jmcneill 
     82  1.1  jmcneill static int
     83  1.1  jmcneill sunxi_rtc_match(device_t parent, cfdata_t cf, void *aux)
     84  1.1  jmcneill {
     85  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     86  1.1  jmcneill 
     87  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
     88  1.1  jmcneill }
     89  1.1  jmcneill 
     90  1.1  jmcneill static void
     91  1.1  jmcneill sunxi_rtc_attach(device_t parent, device_t self, void *aux)
     92  1.1  jmcneill {
     93  1.1  jmcneill 	struct sunxi_rtc_softc * const sc = device_private(self);
     94  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     95  1.1  jmcneill 	const int phandle = faa->faa_phandle;
     96  1.1  jmcneill 	bus_addr_t addr;
     97  1.1  jmcneill 	bus_size_t size;
     98  1.1  jmcneill 
     99  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    100  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    101  1.1  jmcneill 		return;
    102  1.1  jmcneill 	}
    103  1.1  jmcneill 
    104  1.1  jmcneill 	sc->sc_dev = self;
    105  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    106  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    107  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    108  1.1  jmcneill 		return;
    109  1.1  jmcneill 	}
    110  1.1  jmcneill 
    111  1.1  jmcneill 	aprint_naive("\n");
    112  1.1  jmcneill 	aprint_normal(": RTC\n");
    113  1.1  jmcneill 
    114  1.1  jmcneill 	sc->sc_todr.todr_gettime_ymdhms = sunxi_rtc_gettime;
    115  1.1  jmcneill 	sc->sc_todr.todr_settime_ymdhms = sunxi_rtc_settime;
    116  1.1  jmcneill 	sc->sc_todr.cookie = sc;
    117  1.1  jmcneill 
    118  1.1  jmcneill 	fdtbus_todr_attach(self, phandle, &sc->sc_todr);
    119  1.1  jmcneill }
    120  1.1  jmcneill 
    121  1.1  jmcneill static int
    122  1.1  jmcneill sunxi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    123  1.1  jmcneill {
    124  1.1  jmcneill 	struct sunxi_rtc_softc *sc = tch->cookie;
    125  1.1  jmcneill 
    126  1.1  jmcneill 	const uint32_t yymmdd = RTC_READ(sc, RTC_YY_MM_DD_REG);
    127  1.1  jmcneill 	const uint32_t hhmmss = RTC_READ(sc, RTC_HH_MM_SS_REG);
    128  1.1  jmcneill 
    129  1.1  jmcneill 	dt->dt_year = __SHIFTOUT(yymmdd, SUNXI_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
    130  1.1  jmcneill 	dt->dt_mon = __SHIFTOUT(yymmdd, SUNXI_RTC_MONTH);
    131  1.1  jmcneill 	dt->dt_day = __SHIFTOUT(yymmdd, SUNXI_RTC_DAY);
    132  1.1  jmcneill 	dt->dt_wday = __SHIFTOUT(hhmmss, SUNXI_RTC_WK_NO);
    133  1.1  jmcneill 	dt->dt_hour = __SHIFTOUT(hhmmss, SUNXI_RTC_HOUR);
    134  1.1  jmcneill 	dt->dt_min = __SHIFTOUT(hhmmss, SUNXI_RTC_MINUTE);
    135  1.1  jmcneill 	dt->dt_sec = __SHIFTOUT(hhmmss, SUNXI_RTC_SECOND);
    136  1.1  jmcneill 
    137  1.1  jmcneill 	return 0;
    138  1.1  jmcneill }
    139  1.1  jmcneill 
    140  1.1  jmcneill static int
    141  1.1  jmcneill sunxi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    142  1.1  jmcneill {
    143  1.1  jmcneill 	struct sunxi_rtc_softc *sc = tch->cookie;
    144  1.1  jmcneill 	uint32_t yymmdd, hhmmss, maxyear;
    145  1.1  jmcneill 
    146  1.1  jmcneill 	/*
    147  1.1  jmcneill 	 * Sanity check the date before writing it back
    148  1.1  jmcneill 	 */
    149  1.1  jmcneill 	if (dt->dt_year < SUNXI_RTC_BASE_YEAR) {
    150  1.1  jmcneill 		aprint_normal_dev(sc->sc_dev, "year pre the epoch: %llu, "
    151  1.1  jmcneill 		    "not writing back time\n", dt->dt_year);
    152  1.1  jmcneill 		return EIO;
    153  1.1  jmcneill 	}
    154  1.1  jmcneill 	maxyear = __SHIFTOUT(0xffffffff, SUNXI_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
    155  1.1  jmcneill 	if (dt->dt_year > maxyear) {
    156  1.1  jmcneill 		aprint_normal_dev(sc->sc_dev, "year exceeds available field:"
    157  1.1  jmcneill 		    " %llu, not writing back time\n", dt->dt_year);
    158  1.1  jmcneill 		return EIO;
    159  1.1  jmcneill 	}
    160  1.1  jmcneill 
    161  1.1  jmcneill 	yymmdd = __SHIFTIN(dt->dt_year - SUNXI_RTC_BASE_YEAR, SUNXI_RTC_YEAR);
    162  1.1  jmcneill 	yymmdd |= __SHIFTIN(dt->dt_mon, SUNXI_RTC_MONTH);
    163  1.1  jmcneill 	yymmdd |= __SHIFTIN(dt->dt_day, SUNXI_RTC_DAY);
    164  1.1  jmcneill 
    165  1.1  jmcneill 	hhmmss = __SHIFTIN(dt->dt_wday, SUNXI_RTC_WK_NO);
    166  1.1  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_hour, SUNXI_RTC_HOUR);
    167  1.1  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_min, SUNXI_RTC_MINUTE);
    168  1.1  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_sec, SUNXI_RTC_SECOND);
    169  1.1  jmcneill 
    170  1.1  jmcneill 	RTC_WRITE(sc, RTC_YY_MM_DD_REG, yymmdd);
    171  1.1  jmcneill 	RTC_WRITE(sc, RTC_HH_MM_SS_REG, hhmmss);
    172  1.1  jmcneill 
    173  1.1  jmcneill 	return 0;
    174  1.1  jmcneill }
    175