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