1*01be14c4Smarty /* $NetBSD: exynos_rtc.c,v 1.1 2015/12/21 00:52:50 marty Exp $ */ 2*01be14c4Smarty 3*01be14c4Smarty /*- 4*01be14c4Smarty * Copyright (c) 2015 The NetBSD Foundation, Inc. 5*01be14c4Smarty * All rights reserved. 6*01be14c4Smarty * 7*01be14c4Smarty * This code is derived from software contributed to The NetBSD Foundation 8*01be14c4Smarty * by Marty Fouts 9*01be14c4Smarty * 10*01be14c4Smarty * Redistribution and use in source and binary forms, with or without 11*01be14c4Smarty * modification, are permitted provided that the following conditions 12*01be14c4Smarty * are met: 13*01be14c4Smarty * 1. Redistributions of source code must retain the above copyright 14*01be14c4Smarty * notice, this list of conditions and the following disclaimer. 15*01be14c4Smarty * 2. Redistributions in binary form must reproduce the above copyright 16*01be14c4Smarty * notice, this list of conditions and the following disclaimer in the 17*01be14c4Smarty * documentation and/or other materials provided with the distribution. 18*01be14c4Smarty * 19*01be14c4Smarty * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*01be14c4Smarty * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*01be14c4Smarty * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*01be14c4Smarty * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*01be14c4Smarty * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*01be14c4Smarty * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*01be14c4Smarty * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*01be14c4Smarty * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*01be14c4Smarty * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*01be14c4Smarty * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*01be14c4Smarty * POSSIBILITY OF SUCH DAMAGE. 30*01be14c4Smarty */ 31*01be14c4Smarty 32*01be14c4Smarty #include "opt_exynos.h" 33*01be14c4Smarty #include "opt_arm_debug.h" 34*01be14c4Smarty #include "gpio.h" 35*01be14c4Smarty 36*01be14c4Smarty #include <sys/cdefs.h> 37*01be14c4Smarty __KERNEL_RCSID(1, "$NetBSD: exynos_rtc.c,v 1.1 2015/12/21 00:52:50 marty Exp $"); 38*01be14c4Smarty 39*01be14c4Smarty #include <sys/param.h> 40*01be14c4Smarty #include <sys/bus.h> 41*01be14c4Smarty #include <sys/device.h> 42*01be14c4Smarty #include <sys/intr.h> 43*01be14c4Smarty #include <sys/systm.h> 44*01be14c4Smarty #include <sys/kernel.h> 45*01be14c4Smarty #include <sys/kmem.h> 46*01be14c4Smarty 47*01be14c4Smarty #include <dev/clock_subr.h> 48*01be14c4Smarty 49*01be14c4Smarty #include <arm/samsung/exynos_reg.h> 50*01be14c4Smarty #include <arm/samsung/exynos_io.h> 51*01be14c4Smarty #include <arm/samsung/exynos_intr.h> 52*01be14c4Smarty 53*01be14c4Smarty #include <dev/fdt/fdtvar.h> 54*01be14c4Smarty 55*01be14c4Smarty struct exynos_rtc_softc { 56*01be14c4Smarty device_t sc_dev; 57*01be14c4Smarty bus_space_tag_t sc_bst; 58*01be14c4Smarty bus_space_handle_t sc_bsh; 59*01be14c4Smarty 60*01be14c4Smarty struct todr_chip_handle sc_todr; 61*01be14c4Smarty }; 62*01be14c4Smarty 63*01be14c4Smarty static int exynos_rtc_match(device_t, cfdata_t, void *); 64*01be14c4Smarty static void exynos_rtc_attach(device_t, device_t, void *); 65*01be14c4Smarty 66*01be14c4Smarty static int exynos_rtc_gettime(todr_chip_handle_t, struct timeval *); 67*01be14c4Smarty static int exynos_rtc_settime(todr_chip_handle_t, struct timeval *); 68*01be14c4Smarty 69*01be14c4Smarty CFATTACH_DECL_NEW(exynos_rtc, sizeof(struct exynos_rtc_softc), 70*01be14c4Smarty exynos_rtc_match, exynos_rtc_attach, NULL, NULL); 71*01be14c4Smarty 72*01be14c4Smarty #define RTC_READ(sc, reg) \ 73*01be14c4Smarty bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 74*01be14c4Smarty #define RTC_WRITE(sc, reg, val) \ 75*01be14c4Smarty bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 76*01be14c4Smarty 77*01be14c4Smarty static int 78*01be14c4Smarty exynos_rtc_match(device_t parent, cfdata_t cf, void *aux) 79*01be14c4Smarty { 80*01be14c4Smarty const char * const compatible[] = { "samsung,s3c6410-rtc", 81*01be14c4Smarty NULL }; 82*01be14c4Smarty struct fdt_attach_args * const faa = aux; 83*01be14c4Smarty return of_match_compatible(faa->faa_phandle, compatible); 84*01be14c4Smarty } 85*01be14c4Smarty 86*01be14c4Smarty static void 87*01be14c4Smarty exynos_rtc_attach(device_t parent, device_t self, void *aux) 88*01be14c4Smarty { 89*01be14c4Smarty struct exynos_rtc_softc * const sc 90*01be14c4Smarty = kmem_zalloc(sizeof(*sc), KM_SLEEP); 91*01be14c4Smarty struct fdt_attach_args * const faa = aux; 92*01be14c4Smarty bus_addr_t addr; 93*01be14c4Smarty bus_size_t size; 94*01be14c4Smarty int error; 95*01be14c4Smarty 96*01be14c4Smarty if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) { 97*01be14c4Smarty aprint_error(": couldn't get registers\n"); 98*01be14c4Smarty return; 99*01be14c4Smarty } 100*01be14c4Smarty 101*01be14c4Smarty aprint_normal(" @ 0x%08x", (uint)addr); 102*01be14c4Smarty sc->sc_dev = self; 103*01be14c4Smarty sc->sc_bst = faa->faa_bst; 104*01be14c4Smarty error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh); 105*01be14c4Smarty if (error) { 106*01be14c4Smarty aprint_error(": couldn't map %#llx: %d", 107*01be14c4Smarty (uint64_t)addr, error); 108*01be14c4Smarty return; 109*01be14c4Smarty } 110*01be14c4Smarty 111*01be14c4Smarty aprint_naive("\n"); 112*01be14c4Smarty aprint_normal(": RTC\n"); 113*01be14c4Smarty 114*01be14c4Smarty sc->sc_todr.todr_gettime = exynos_rtc_gettime; 115*01be14c4Smarty sc->sc_todr.todr_settime = exynos_rtc_settime; 116*01be14c4Smarty sc->sc_todr.cookie = sc; 117*01be14c4Smarty todr_attach(&sc->sc_todr); 118*01be14c4Smarty 119*01be14c4Smarty } 120*01be14c4Smarty 121*01be14c4Smarty static int 122*01be14c4Smarty exynos_rtc_gettime(todr_chip_handle_t tch, struct timeval *tv) 123*01be14c4Smarty { 124*01be14c4Smarty struct exynos_rtc_softc * const sc = tch->cookie; 125*01be14c4Smarty 126*01be14c4Smarty tv->tv_sec = RTC_READ(sc, EXYNOS5_RTC_OFFSET); 127*01be14c4Smarty tv->tv_usec = 0; 128*01be14c4Smarty 129*01be14c4Smarty return 0; 130*01be14c4Smarty } 131*01be14c4Smarty 132*01be14c4Smarty static int 133*01be14c4Smarty exynos_rtc_settime(todr_chip_handle_t tch, struct timeval *tv) 134*01be14c4Smarty { 135*01be14c4Smarty struct exynos_rtc_softc * const sc = tch->cookie; 136*01be14c4Smarty int retry = 500; 137*01be14c4Smarty 138*01be14c4Smarty while (--retry > 0) { 139*01be14c4Smarty if (!RTC_READ(sc, EXYNOS5_RTC_OFFSET)) 140*01be14c4Smarty break; 141*01be14c4Smarty delay(1); 142*01be14c4Smarty } 143*01be14c4Smarty if (retry == 0) { 144*01be14c4Smarty device_printf(sc->sc_dev, "RTC write failed (BUSY)\n"); 145*01be14c4Smarty return ETIMEDOUT; 146*01be14c4Smarty } 147*01be14c4Smarty 148*01be14c4Smarty RTC_WRITE(sc, EXYNOS5_RTC_OFFSET, tv->tv_sec); 149*01be14c4Smarty 150*01be14c4Smarty return 0; 151*01be14c4Smarty } 152