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