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