exynos_rtc.c revision 1.1 1 /* $NetBSD: exynos_rtc.c,v 1.1 2015/12/21 00:52: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.1 2015/12/21 00:52: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_io.h>
51 #include <arm/samsung/exynos_intr.h>
52
53 #include <dev/fdt/fdtvar.h>
54
55 struct exynos_rtc_softc {
56 device_t sc_dev;
57 bus_space_tag_t sc_bst;
58 bus_space_handle_t sc_bsh;
59
60 struct todr_chip_handle sc_todr;
61 };
62
63 static int exynos_rtc_match(device_t, cfdata_t, void *);
64 static void exynos_rtc_attach(device_t, device_t, void *);
65
66 static int exynos_rtc_gettime(todr_chip_handle_t, struct timeval *);
67 static int exynos_rtc_settime(todr_chip_handle_t, struct timeval *);
68
69 CFATTACH_DECL_NEW(exynos_rtc, sizeof(struct exynos_rtc_softc),
70 exynos_rtc_match, exynos_rtc_attach, NULL, NULL);
71
72 #define RTC_READ(sc, reg) \
73 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
74 #define RTC_WRITE(sc, reg, val) \
75 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
76
77 static int
78 exynos_rtc_match(device_t parent, cfdata_t cf, void *aux)
79 {
80 const char * const compatible[] = { "samsung,s3c6410-rtc",
81 NULL };
82 struct fdt_attach_args * const faa = aux;
83 return of_match_compatible(faa->faa_phandle, compatible);
84 }
85
86 static void
87 exynos_rtc_attach(device_t parent, device_t self, void *aux)
88 {
89 struct exynos_rtc_softc * const sc
90 = kmem_zalloc(sizeof(*sc), KM_SLEEP);
91 struct fdt_attach_args * const faa = aux;
92 bus_addr_t addr;
93 bus_size_t size;
94 int error;
95
96 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
97 aprint_error(": couldn't get registers\n");
98 return;
99 }
100
101 aprint_normal(" @ 0x%08x", (uint)addr);
102 sc->sc_dev = self;
103 sc->sc_bst = faa->faa_bst;
104 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
105 if (error) {
106 aprint_error(": couldn't map %#llx: %d",
107 (uint64_t)addr, error);
108 return;
109 }
110
111 aprint_naive("\n");
112 aprint_normal(": RTC\n");
113
114 sc->sc_todr.todr_gettime = exynos_rtc_gettime;
115 sc->sc_todr.todr_settime = exynos_rtc_settime;
116 sc->sc_todr.cookie = sc;
117 todr_attach(&sc->sc_todr);
118
119 }
120
121 static int
122 exynos_rtc_gettime(todr_chip_handle_t tch, struct timeval *tv)
123 {
124 struct exynos_rtc_softc * const sc = tch->cookie;
125
126 tv->tv_sec = RTC_READ(sc, EXYNOS5_RTC_OFFSET);
127 tv->tv_usec = 0;
128
129 return 0;
130 }
131
132 static int
133 exynos_rtc_settime(todr_chip_handle_t tch, struct timeval *tv)
134 {
135 struct exynos_rtc_softc * const sc = tch->cookie;
136 int retry = 500;
137
138 while (--retry > 0) {
139 if (!RTC_READ(sc, EXYNOS5_RTC_OFFSET))
140 break;
141 delay(1);
142 }
143 if (retry == 0) {
144 device_printf(sc->sc_dev, "RTC write failed (BUSY)\n");
145 return ETIMEDOUT;
146 }
147
148 RTC_WRITE(sc, EXYNOS5_RTC_OFFSET, tv->tv_sec);
149
150 return 0;
151 }
152