sxirtc.c revision 1.1
1/* $OpenBSD: sxirtc.c,v 1.1 2017/01/21 08:26:49 patrick Exp $ */ 2/* 3 * Copyright (c) 2008 Mark Kettenis 4 * Copyright (c) 2013 Artturi Alm 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include <sys/param.h> 20#include <sys/device.h> 21#include <sys/malloc.h> 22#include <sys/systm.h> 23 24#include <dev/clock_subr.h> 25 26#include <machine/bus.h> 27#include <machine/fdt.h> 28 29#include <dev/fdt/sunxireg.h> 30 31#include <dev/ofw/openfirm.h> 32#include <dev/ofw/fdt.h> 33 34#define SXIRTC_YYMMDD 0x04 35#define SXIRTC_HHMMSS 0x08 36#define SXIRTC_YYMMDD_A31 0x10 37#define SXIRTC_HHMMSS_A31 0x14 38 39#define LEAPYEAR(y) \ 40 (((y) % 4 == 0 && \ 41 (y) % 100 != 0) || \ 42 (y) % 400 == 0) 43 44 45extern todr_chip_handle_t todr_handle; 46 47struct sxirtc_softc { 48 struct device sc_dev; 49 bus_space_tag_t sc_iot; 50 bus_space_handle_t sc_ioh; 51 bus_size_t sc_yymmdd; 52 bus_size_t sc_hhmmss; 53 uint32_t base_year; 54 uint32_t year_mask; 55 uint32_t leap_shift; 56}; 57 58int sxirtc_match(struct device *, void *, void *); 59void sxirtc_attach(struct device *, struct device *, void *); 60 61struct cfattach sxirtc_ca = { 62 sizeof(struct sxirtc_softc), sxirtc_match, sxirtc_attach 63}; 64 65struct cfdriver sxirtc_cd = { 66 NULL, "sxirtc", DV_DULL 67}; 68 69int sxirtc_gettime(todr_chip_handle_t, struct timeval *); 70int sxirtc_settime(todr_chip_handle_t, struct timeval *); 71 72int 73sxirtc_match(struct device *parent, void *match, void *aux) 74{ 75 struct fdt_attach_args *faa = aux; 76 77 return (OF_is_compatible(faa->fa_node, "allwinner,sun4i-a10-rtc") || 78 OF_is_compatible(faa->fa_node, "allwinner,sun7i-a20-rtc") || 79 OF_is_compatible(faa->fa_node, "allwinner,sun6i-a31-rtc")); 80} 81 82void 83sxirtc_attach(struct device *parent, struct device *self, void *aux) 84{ 85 struct sxirtc_softc *sc = (struct sxirtc_softc *)self; 86 struct fdt_attach_args *faa = aux; 87 todr_chip_handle_t handle; 88 89 if (faa->fa_nreg < 1) 90 return; 91 92 handle = malloc(sizeof(struct todr_chip_handle), M_DEVBUF, M_NOWAIT); 93 if (handle == NULL) 94 panic("sxirtc_attach: couldn't allocate todr_handle"); 95 96 sc->sc_iot = faa->fa_iot; 97 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 98 faa->fa_reg[0].size, 0, &sc->sc_ioh)) 99 panic("sxirtc_attach: bus_space_map failed!"); 100 101 if (OF_is_compatible(faa->fa_node, "allwinner,sun6i-a31-rtc")) { 102 sc->sc_yymmdd = SXIRTC_YYMMDD_A31; 103 sc->sc_hhmmss = SXIRTC_HHMMSS_A31; 104 } else { 105 sc->sc_yymmdd = SXIRTC_YYMMDD; 106 sc->sc_hhmmss = SXIRTC_HHMMSS; 107 } 108 109 if (OF_is_compatible(faa->fa_node, "allwinner,sun7i-a20-rtc")) { 110 sc->base_year = 1970; 111 sc->year_mask = 0xff; 112 sc->leap_shift = 24; 113 } else { 114 sc->base_year = 2010; 115 sc->year_mask = 0x3f; 116 sc->leap_shift = 22; 117 } 118 119 handle->cookie = self; 120 handle->todr_gettime = sxirtc_gettime; 121 handle->todr_settime = sxirtc_settime; 122 handle->todr_getcal = NULL; 123 handle->todr_setcal = NULL; 124 handle->bus_cookie = NULL; 125 handle->todr_setwen = NULL; 126 todr_handle = handle; 127 128 printf("\n"); 129} 130 131int 132sxirtc_gettime(todr_chip_handle_t handle, struct timeval *tv) 133{ 134 struct sxirtc_softc *sc = (struct sxirtc_softc *)handle->cookie; 135 struct clock_ymdhms dt; 136 uint32_t reg; 137 138 reg = SXIREAD4(sc, sc->sc_hhmmss); 139 dt.dt_sec = reg & 0x3f; 140 dt.dt_min = reg >> 8 & 0x3f; 141 dt.dt_hour = reg >> 16 & 0x1f; 142 dt.dt_wday = reg >> 29 & 0x07; 143 144 reg = SXIREAD4(sc, sc->sc_yymmdd); 145 dt.dt_day = reg & 0x1f; 146 dt.dt_mon = reg >> 8 & 0x0f; 147 dt.dt_year = (reg >> 16 & sc->year_mask) + sc->base_year; 148 149 if (dt.dt_sec > 59 || dt.dt_min > 59 || 150 dt.dt_hour > 23 || dt.dt_wday > 6 || 151 dt.dt_day > 31 || dt.dt_day == 0 || 152 dt.dt_mon > 12 || dt.dt_mon == 0) 153 return 1; 154 155 tv->tv_sec = clock_ymdhms_to_secs(&dt); 156 tv->tv_usec = 0; 157 return 0; 158} 159 160int 161sxirtc_settime(todr_chip_handle_t handle, struct timeval *tv) 162{ 163 struct sxirtc_softc *sc = (struct sxirtc_softc *)handle->cookie; 164 struct clock_ymdhms dt; 165 166 clock_secs_to_ymdhms(tv->tv_sec, &dt); 167 168 if (dt.dt_sec > 59 || dt.dt_min > 59 || 169 dt.dt_hour > 23 || dt.dt_wday > 6 || 170 dt.dt_day > 31 || dt.dt_day == 0 || 171 dt.dt_mon > 12 || dt.dt_mon == 0) 172 return 1; 173 174 SXICMS4(sc, sc->sc_hhmmss, 0xe0000000 | 0x1f0000 | 0x3f00 | 0x3f, 175 dt.dt_sec | (dt.dt_min << 8) | (dt.dt_hour << 16) | 176 (dt.dt_wday << 29)); 177 178 SXICMS4(sc, sc->sc_yymmdd, 0x00400000 | (sc->year_mask << 16) | 179 0x0f00 | 0x1f, dt.dt_day | (dt.dt_mon << 8) | 180 ((dt.dt_year - sc->base_year) << 16) | 181 (LEAPYEAR(dt.dt_year) << sc->leap_shift)); 182 183 return 0; 184} 185