sunxi_rtc.c revision 1.2 1 1.2 jmcneill /* $NetBSD: sunxi_rtc.c,v 1.2 2017/10/07 17:03:49 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2014-2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_rtc.c,v 1.2 2017/10/07 17:03:49 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/mutex.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/clock_subr.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/fdt/fdtvar.h>
42 1.1 jmcneill
43 1.2 jmcneill #define SUN7I_RTC_YY_MM_DD_REG 0x04
44 1.2 jmcneill #define SUN7I_RTC_LEAP __BIT(24)
45 1.2 jmcneill #define SUN7I_RTC_YEAR __BITS(23,16)
46 1.2 jmcneill #define SUN7I_RTC_MONTH __BITS(11,8)
47 1.2 jmcneill #define SUN7I_RTC_DAY __BITS(4,0)
48 1.2 jmcneill #define SUN7I_RTC_HH_MM_SS_REG 0x08
49 1.2 jmcneill #define SUN7I_RTC_WK_NO __BITS(31,29)
50 1.2 jmcneill #define SUN7I_RTC_HOUR __BITS(20,16)
51 1.2 jmcneill #define SUN7I_RTC_MINUTE __BITS(13,8)
52 1.2 jmcneill #define SUN7I_RTC_SECOND __BITS(5,0)
53 1.2 jmcneill
54 1.2 jmcneill #define SUN6I_RTC_YY_MM_DD_REG 0x10
55 1.2 jmcneill #define SUN6I_RTC_LEAP __BIT(22)
56 1.2 jmcneill #define SUN6I_RTC_YEAR __BITS(21,16)
57 1.2 jmcneill #define SUN6I_RTC_MONTH __BITS(11,8)
58 1.2 jmcneill #define SUN6I_RTC_DAY __BITS(4,0)
59 1.2 jmcneill #define SUN6I_RTC_HH_MM_SS_REG 0x14
60 1.2 jmcneill #define SUN6I_RTC_WK_NO __BITS(31,29)
61 1.2 jmcneill #define SUN6I_RTC_HOUR __BITS(20,16)
62 1.2 jmcneill #define SUN6I_RTC_MINUTE __BITS(13,8)
63 1.2 jmcneill #define SUN6I_RTC_SECOND __BITS(5,0)
64 1.1 jmcneill
65 1.1 jmcneill #define SUNXI_RTC_BASE_YEAR 2000
66 1.1 jmcneill
67 1.2 jmcneill enum sunxi_rtc_type {
68 1.2 jmcneill RTC_SUN6I = 1,
69 1.2 jmcneill RTC_SUN7I,
70 1.2 jmcneill };
71 1.2 jmcneill
72 1.2 jmcneill static const struct of_compat_data compat_data[] = {
73 1.2 jmcneill { "allwinner,sun6i-a31-rtc", RTC_SUN6I },
74 1.2 jmcneill { "allwinner,sun7i-a20-rtc", RTC_SUN7I },
75 1.2 jmcneill { NULL }
76 1.1 jmcneill };
77 1.1 jmcneill
78 1.1 jmcneill struct sunxi_rtc_softc {
79 1.1 jmcneill device_t sc_dev;
80 1.1 jmcneill bus_space_tag_t sc_bst;
81 1.1 jmcneill bus_space_handle_t sc_bsh;
82 1.1 jmcneill struct todr_chip_handle sc_todr;
83 1.1 jmcneill };
84 1.1 jmcneill
85 1.1 jmcneill #define RTC_READ(sc, reg) \
86 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
87 1.1 jmcneill #define RTC_WRITE(sc, reg, val) \
88 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
89 1.1 jmcneill
90 1.1 jmcneill static int sunxi_rtc_match(device_t, cfdata_t, void *);
91 1.1 jmcneill static void sunxi_rtc_attach(device_t, device_t, void *);
92 1.1 jmcneill
93 1.2 jmcneill static int sun6i_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
94 1.2 jmcneill static int sun6i_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
95 1.2 jmcneill
96 1.2 jmcneill static int sun7i_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
97 1.2 jmcneill static int sun7i_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
98 1.1 jmcneill
99 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_rtc, sizeof(struct sunxi_rtc_softc),
100 1.1 jmcneill sunxi_rtc_match, sunxi_rtc_attach, NULL, NULL);
101 1.1 jmcneill
102 1.1 jmcneill static int
103 1.1 jmcneill sunxi_rtc_match(device_t parent, cfdata_t cf, void *aux)
104 1.1 jmcneill {
105 1.1 jmcneill struct fdt_attach_args * const faa = aux;
106 1.1 jmcneill
107 1.2 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
108 1.1 jmcneill }
109 1.1 jmcneill
110 1.1 jmcneill static void
111 1.1 jmcneill sunxi_rtc_attach(device_t parent, device_t self, void *aux)
112 1.1 jmcneill {
113 1.1 jmcneill struct sunxi_rtc_softc * const sc = device_private(self);
114 1.1 jmcneill struct fdt_attach_args * const faa = aux;
115 1.1 jmcneill const int phandle = faa->faa_phandle;
116 1.2 jmcneill enum sunxi_rtc_type type;
117 1.1 jmcneill bus_addr_t addr;
118 1.1 jmcneill bus_size_t size;
119 1.1 jmcneill
120 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
121 1.1 jmcneill aprint_error(": couldn't get registers\n");
122 1.1 jmcneill return;
123 1.1 jmcneill }
124 1.1 jmcneill
125 1.1 jmcneill sc->sc_dev = self;
126 1.1 jmcneill sc->sc_bst = faa->faa_bst;
127 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
128 1.1 jmcneill aprint_error(": couldn't map registers\n");
129 1.1 jmcneill return;
130 1.1 jmcneill }
131 1.1 jmcneill
132 1.1 jmcneill aprint_naive("\n");
133 1.1 jmcneill aprint_normal(": RTC\n");
134 1.1 jmcneill
135 1.2 jmcneill type = of_search_compatible(phandle, compat_data)->data;
136 1.2 jmcneill
137 1.1 jmcneill sc->sc_todr.cookie = sc;
138 1.2 jmcneill switch (type) {
139 1.2 jmcneill case RTC_SUN6I:
140 1.2 jmcneill sc->sc_todr.todr_gettime_ymdhms = sun6i_rtc_gettime;
141 1.2 jmcneill sc->sc_todr.todr_settime_ymdhms = sun6i_rtc_settime;
142 1.2 jmcneill break;
143 1.2 jmcneill case RTC_SUN7I:
144 1.2 jmcneill sc->sc_todr.todr_gettime_ymdhms = sun7i_rtc_gettime;
145 1.2 jmcneill sc->sc_todr.todr_settime_ymdhms = sun7i_rtc_settime;
146 1.2 jmcneill break;
147 1.2 jmcneill }
148 1.1 jmcneill
149 1.1 jmcneill fdtbus_todr_attach(self, phandle, &sc->sc_todr);
150 1.1 jmcneill }
151 1.1 jmcneill
152 1.1 jmcneill static int
153 1.2 jmcneill sun6i_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
154 1.2 jmcneill {
155 1.2 jmcneill struct sunxi_rtc_softc *sc = tch->cookie;
156 1.2 jmcneill
157 1.2 jmcneill const uint32_t yymmdd = RTC_READ(sc, SUN6I_RTC_YY_MM_DD_REG);
158 1.2 jmcneill const uint32_t hhmmss = RTC_READ(sc, SUN6I_RTC_HH_MM_SS_REG);
159 1.2 jmcneill
160 1.2 jmcneill dt->dt_year = __SHIFTOUT(yymmdd, SUN6I_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
161 1.2 jmcneill dt->dt_mon = __SHIFTOUT(yymmdd, SUN6I_RTC_MONTH);
162 1.2 jmcneill dt->dt_day = __SHIFTOUT(yymmdd, SUN6I_RTC_DAY);
163 1.2 jmcneill dt->dt_wday = __SHIFTOUT(hhmmss, SUN6I_RTC_WK_NO);
164 1.2 jmcneill dt->dt_hour = __SHIFTOUT(hhmmss, SUN6I_RTC_HOUR);
165 1.2 jmcneill dt->dt_min = __SHIFTOUT(hhmmss, SUN6I_RTC_MINUTE);
166 1.2 jmcneill dt->dt_sec = __SHIFTOUT(hhmmss, SUN6I_RTC_SECOND);
167 1.2 jmcneill
168 1.2 jmcneill return 0;
169 1.2 jmcneill }
170 1.2 jmcneill
171 1.2 jmcneill static int
172 1.2 jmcneill sun6i_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
173 1.2 jmcneill {
174 1.2 jmcneill struct sunxi_rtc_softc *sc = tch->cookie;
175 1.2 jmcneill uint32_t yymmdd, hhmmss, maxyear;
176 1.2 jmcneill
177 1.2 jmcneill /*
178 1.2 jmcneill * Sanity check the date before writing it back
179 1.2 jmcneill */
180 1.2 jmcneill if (dt->dt_year < SUNXI_RTC_BASE_YEAR) {
181 1.2 jmcneill aprint_normal_dev(sc->sc_dev, "year pre the epoch: %llu, "
182 1.2 jmcneill "not writing back time\n", dt->dt_year);
183 1.2 jmcneill return EIO;
184 1.2 jmcneill }
185 1.2 jmcneill maxyear = __SHIFTOUT(0xffffffff, SUN6I_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
186 1.2 jmcneill if (dt->dt_year > maxyear) {
187 1.2 jmcneill aprint_normal_dev(sc->sc_dev, "year exceeds available field:"
188 1.2 jmcneill " %llu, not writing back time\n", dt->dt_year);
189 1.2 jmcneill return EIO;
190 1.2 jmcneill }
191 1.2 jmcneill
192 1.2 jmcneill yymmdd = __SHIFTIN(dt->dt_year - SUNXI_RTC_BASE_YEAR, SUN6I_RTC_YEAR);
193 1.2 jmcneill yymmdd |= __SHIFTIN(dt->dt_mon, SUN6I_RTC_MONTH);
194 1.2 jmcneill yymmdd |= __SHIFTIN(dt->dt_day, SUN6I_RTC_DAY);
195 1.2 jmcneill
196 1.2 jmcneill hhmmss = __SHIFTIN(dt->dt_wday, SUN6I_RTC_WK_NO);
197 1.2 jmcneill hhmmss |= __SHIFTIN(dt->dt_hour, SUN6I_RTC_HOUR);
198 1.2 jmcneill hhmmss |= __SHIFTIN(dt->dt_min, SUN6I_RTC_MINUTE);
199 1.2 jmcneill hhmmss |= __SHIFTIN(dt->dt_sec, SUN6I_RTC_SECOND);
200 1.2 jmcneill
201 1.2 jmcneill RTC_WRITE(sc, SUN6I_RTC_YY_MM_DD_REG, yymmdd);
202 1.2 jmcneill RTC_WRITE(sc, SUN6I_RTC_HH_MM_SS_REG, hhmmss);
203 1.2 jmcneill
204 1.2 jmcneill return 0;
205 1.2 jmcneill }
206 1.2 jmcneill
207 1.2 jmcneill static int
208 1.2 jmcneill sun7i_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
209 1.1 jmcneill {
210 1.1 jmcneill struct sunxi_rtc_softc *sc = tch->cookie;
211 1.1 jmcneill
212 1.2 jmcneill const uint32_t yymmdd = RTC_READ(sc, SUN7I_RTC_YY_MM_DD_REG);
213 1.2 jmcneill const uint32_t hhmmss = RTC_READ(sc, SUN7I_RTC_HH_MM_SS_REG);
214 1.1 jmcneill
215 1.2 jmcneill dt->dt_year = __SHIFTOUT(yymmdd, SUN7I_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
216 1.2 jmcneill dt->dt_mon = __SHIFTOUT(yymmdd, SUN7I_RTC_MONTH);
217 1.2 jmcneill dt->dt_day = __SHIFTOUT(yymmdd, SUN7I_RTC_DAY);
218 1.2 jmcneill dt->dt_wday = __SHIFTOUT(hhmmss, SUN7I_RTC_WK_NO);
219 1.2 jmcneill dt->dt_hour = __SHIFTOUT(hhmmss, SUN7I_RTC_HOUR);
220 1.2 jmcneill dt->dt_min = __SHIFTOUT(hhmmss, SUN7I_RTC_MINUTE);
221 1.2 jmcneill dt->dt_sec = __SHIFTOUT(hhmmss, SUN7I_RTC_SECOND);
222 1.1 jmcneill
223 1.1 jmcneill return 0;
224 1.1 jmcneill }
225 1.1 jmcneill
226 1.1 jmcneill static int
227 1.2 jmcneill sun7i_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
228 1.1 jmcneill {
229 1.1 jmcneill struct sunxi_rtc_softc *sc = tch->cookie;
230 1.1 jmcneill uint32_t yymmdd, hhmmss, maxyear;
231 1.1 jmcneill
232 1.1 jmcneill /*
233 1.1 jmcneill * Sanity check the date before writing it back
234 1.1 jmcneill */
235 1.1 jmcneill if (dt->dt_year < SUNXI_RTC_BASE_YEAR) {
236 1.1 jmcneill aprint_normal_dev(sc->sc_dev, "year pre the epoch: %llu, "
237 1.1 jmcneill "not writing back time\n", dt->dt_year);
238 1.1 jmcneill return EIO;
239 1.1 jmcneill }
240 1.2 jmcneill maxyear = __SHIFTOUT(0xffffffff, SUN7I_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
241 1.1 jmcneill if (dt->dt_year > maxyear) {
242 1.1 jmcneill aprint_normal_dev(sc->sc_dev, "year exceeds available field:"
243 1.1 jmcneill " %llu, not writing back time\n", dt->dt_year);
244 1.1 jmcneill return EIO;
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.2 jmcneill yymmdd = __SHIFTIN(dt->dt_year - SUNXI_RTC_BASE_YEAR, SUN7I_RTC_YEAR);
248 1.2 jmcneill yymmdd |= __SHIFTIN(dt->dt_mon, SUN7I_RTC_MONTH);
249 1.2 jmcneill yymmdd |= __SHIFTIN(dt->dt_day, SUN7I_RTC_DAY);
250 1.2 jmcneill
251 1.2 jmcneill hhmmss = __SHIFTIN(dt->dt_wday, SUN7I_RTC_WK_NO);
252 1.2 jmcneill hhmmss |= __SHIFTIN(dt->dt_hour, SUN7I_RTC_HOUR);
253 1.2 jmcneill hhmmss |= __SHIFTIN(dt->dt_min, SUN7I_RTC_MINUTE);
254 1.2 jmcneill hhmmss |= __SHIFTIN(dt->dt_sec, SUN7I_RTC_SECOND);
255 1.1 jmcneill
256 1.2 jmcneill RTC_WRITE(sc, SUN7I_RTC_YY_MM_DD_REG, yymmdd);
257 1.2 jmcneill RTC_WRITE(sc, SUN7I_RTC_HH_MM_SS_REG, hhmmss);
258 1.1 jmcneill
259 1.1 jmcneill return 0;
260 1.1 jmcneill }
261