xref: /netbsd/sys/arch/arm/samsung/exynos_rtc.c (revision 8e90f9ed)
1*8e90f9edSthorpej /*	$NetBSD: exynos_rtc.c,v 1.4 2021/01/27 03:10:19 thorpej Exp $ */
201be14c4Smarty 
301be14c4Smarty /*-
401be14c4Smarty * Copyright (c) 2015 The NetBSD Foundation, Inc.
501be14c4Smarty * All rights reserved.
601be14c4Smarty *
701be14c4Smarty * This code is derived from software contributed to The NetBSD Foundation
801be14c4Smarty * by Marty Fouts
901be14c4Smarty *
1001be14c4Smarty * Redistribution and use in source and binary forms, with or without
1101be14c4Smarty * modification, are permitted provided that the following conditions
1201be14c4Smarty * are met:
1301be14c4Smarty * 1. Redistributions of source code must retain the above copyright
1401be14c4Smarty *    notice, this list of conditions and the following disclaimer.
1501be14c4Smarty * 2. Redistributions in binary form must reproduce the above copyright
1601be14c4Smarty *    notice, this list of conditions and the following disclaimer in the
1701be14c4Smarty *    documentation and/or other materials provided with the distribution.
1801be14c4Smarty *
1901be14c4Smarty * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2001be14c4Smarty * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2101be14c4Smarty * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2201be14c4Smarty * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2301be14c4Smarty * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2401be14c4Smarty * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2501be14c4Smarty * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2601be14c4Smarty * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2701be14c4Smarty * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2801be14c4Smarty * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2901be14c4Smarty * POSSIBILITY OF SUCH DAMAGE.
3001be14c4Smarty */
3101be14c4Smarty 
3201be14c4Smarty #include "opt_exynos.h"
3301be14c4Smarty #include "opt_arm_debug.h"
3401be14c4Smarty #include "gpio.h"
3501be14c4Smarty 
3601be14c4Smarty #include <sys/cdefs.h>
37*8e90f9edSthorpej __KERNEL_RCSID(1, "$NetBSD: exynos_rtc.c,v 1.4 2021/01/27 03:10:19 thorpej Exp $");
3801be14c4Smarty 
3901be14c4Smarty #include <sys/param.h>
4001be14c4Smarty #include <sys/bus.h>
4101be14c4Smarty #include <sys/device.h>
4201be14c4Smarty #include <sys/intr.h>
4301be14c4Smarty #include <sys/systm.h>
4401be14c4Smarty #include <sys/kernel.h>
4501be14c4Smarty #include <sys/kmem.h>
4601be14c4Smarty 
4701be14c4Smarty #include <dev/clock_subr.h>
4801be14c4Smarty 
4901be14c4Smarty #include <arm/samsung/exynos_reg.h>
5001be14c4Smarty #include <arm/samsung/exynos_intr.h>
5101be14c4Smarty 
5201be14c4Smarty #include <dev/fdt/fdtvar.h>
5301be14c4Smarty 
5401be14c4Smarty struct exynos_rtc_softc {
5501be14c4Smarty 	device_t		sc_dev;
5601be14c4Smarty 	bus_space_tag_t		sc_bst;
5701be14c4Smarty 	bus_space_handle_t	sc_bsh;
5801be14c4Smarty 
5901be14c4Smarty 	struct todr_chip_handle sc_todr;
6001be14c4Smarty };
6101be14c4Smarty 
6201be14c4Smarty static int exynos_rtc_match(device_t, cfdata_t, void *);
6301be14c4Smarty static void exynos_rtc_attach(device_t, device_t, void *);
6401be14c4Smarty 
6501be14c4Smarty static int	exynos_rtc_gettime(todr_chip_handle_t, struct timeval *);
6601be14c4Smarty static int	exynos_rtc_settime(todr_chip_handle_t, struct timeval *);
6701be14c4Smarty 
6801be14c4Smarty CFATTACH_DECL_NEW(exynos_rtc, sizeof(struct exynos_rtc_softc),
6901be14c4Smarty 	exynos_rtc_match, exynos_rtc_attach, NULL, NULL);
7001be14c4Smarty 
7101be14c4Smarty #define RTC_READ(sc, reg)	\
7201be14c4Smarty 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
7301be14c4Smarty #define RTC_WRITE(sc, reg, val)	\
7401be14c4Smarty 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
7501be14c4Smarty 
76*8e90f9edSthorpej static const struct device_compatible_entry compat_data[] = {
77*8e90f9edSthorpej 	{ .compat = "samsung,s3c6410-rtc" },
78*8e90f9edSthorpej 	DEVICE_COMPAT_EOL
79*8e90f9edSthorpej };
80*8e90f9edSthorpej 
8101be14c4Smarty static int
exynos_rtc_match(device_t parent,cfdata_t cf,void * aux)8201be14c4Smarty exynos_rtc_match(device_t parent, cfdata_t cf, void *aux)
8301be14c4Smarty {
8401be14c4Smarty 	struct fdt_attach_args * const faa = aux;
85*8e90f9edSthorpej 
86*8e90f9edSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
8701be14c4Smarty }
8801be14c4Smarty 
8901be14c4Smarty static void
exynos_rtc_attach(device_t parent,device_t self,void * aux)9001be14c4Smarty exynos_rtc_attach(device_t parent, device_t self, void *aux)
9101be14c4Smarty {
9201be14c4Smarty 	struct exynos_rtc_softc * const sc
9301be14c4Smarty 		= kmem_zalloc(sizeof(*sc), KM_SLEEP);
9401be14c4Smarty 	struct fdt_attach_args * const faa = aux;
9501be14c4Smarty 	bus_addr_t addr;
9601be14c4Smarty 	bus_size_t size;
9701be14c4Smarty 	int error;
9801be14c4Smarty 
9901be14c4Smarty 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
10001be14c4Smarty 		aprint_error(": couldn't get registers\n");
10101be14c4Smarty 		return;
10201be14c4Smarty 	}
10301be14c4Smarty 
10401be14c4Smarty 	aprint_normal(" @ 0x%08x", (uint)addr);
10501be14c4Smarty 	sc->sc_dev = self;
10601be14c4Smarty 	sc->sc_bst = faa->faa_bst;
10701be14c4Smarty 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
10801be14c4Smarty 	if (error) {
109dee6b79eSskrll 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
110dee6b79eSskrll 			     addr, error);
11101be14c4Smarty 		return;
11201be14c4Smarty 	}
11301be14c4Smarty 
11401be14c4Smarty 	aprint_naive("\n");
11501be14c4Smarty 	aprint_normal(": RTC\n");
11601be14c4Smarty 
11701be14c4Smarty 	sc->sc_todr.todr_gettime = exynos_rtc_gettime;
11801be14c4Smarty 	sc->sc_todr.todr_settime = exynos_rtc_settime;
11901be14c4Smarty 	sc->sc_todr.cookie = sc;
12001be14c4Smarty 	todr_attach(&sc->sc_todr);
12101be14c4Smarty 
12201be14c4Smarty }
12301be14c4Smarty 
12401be14c4Smarty static int
exynos_rtc_gettime(todr_chip_handle_t tch,struct timeval * tv)12501be14c4Smarty exynos_rtc_gettime(todr_chip_handle_t tch, struct timeval *tv)
12601be14c4Smarty {
12701be14c4Smarty 	struct exynos_rtc_softc * const sc = tch->cookie;
12801be14c4Smarty 
12901be14c4Smarty 	tv->tv_sec = RTC_READ(sc, EXYNOS5_RTC_OFFSET);
13001be14c4Smarty 	tv->tv_usec = 0;
13101be14c4Smarty 
13201be14c4Smarty 	return 0;
13301be14c4Smarty }
13401be14c4Smarty 
13501be14c4Smarty static int
exynos_rtc_settime(todr_chip_handle_t tch,struct timeval * tv)13601be14c4Smarty exynos_rtc_settime(todr_chip_handle_t tch, struct timeval *tv)
13701be14c4Smarty {
13801be14c4Smarty 	struct exynos_rtc_softc * const sc = tch->cookie;
13901be14c4Smarty 	int retry = 500;
14001be14c4Smarty 
14101be14c4Smarty 	while (--retry > 0) {
14201be14c4Smarty 		if (!RTC_READ(sc, EXYNOS5_RTC_OFFSET))
14301be14c4Smarty 			break;
14401be14c4Smarty 		delay(1);
14501be14c4Smarty 	}
14601be14c4Smarty 	if (retry == 0) {
14701be14c4Smarty 		device_printf(sc->sc_dev, "RTC write failed (BUSY)\n");
14801be14c4Smarty 		return ETIMEDOUT;
14901be14c4Smarty 	}
15001be14c4Smarty 
15101be14c4Smarty 	RTC_WRITE(sc, EXYNOS5_RTC_OFFSET, tv->tv_sec);
15201be14c4Smarty 
15301be14c4Smarty 	return 0;
15401be14c4Smarty }
155