meson_rtc.c revision 1.2.6.2 1 1.2.6.2 christos /* $NetBSD: meson_rtc.c,v 1.2.6.2 2019/06/10 22:05:51 christos Exp $ */
2 1.2.6.2 christos
3 1.2.6.2 christos /*-
4 1.2.6.2 christos * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.2.6.2 christos * All rights reserved.
6 1.2.6.2 christos *
7 1.2.6.2 christos * Redistribution and use in source and binary forms, with or without
8 1.2.6.2 christos * modification, are permitted provided that the following conditions
9 1.2.6.2 christos * are met:
10 1.2.6.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.2.6.2 christos * notice, this list of conditions and the following disclaimer.
12 1.2.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.6.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.2.6.2 christos * documentation and/or other materials provided with the distribution.
15 1.2.6.2 christos *
16 1.2.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.2.6.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2.6.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2.6.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.2.6.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2.6.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2.6.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2.6.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2.6.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2.6.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.6.2 christos * POSSIBILITY OF SUCH DAMAGE.
27 1.2.6.2 christos */
28 1.2.6.2 christos
29 1.2.6.2 christos #include <sys/cdefs.h>
30 1.2.6.2 christos __KERNEL_RCSID(0, "$NetBSD: meson_rtc.c,v 1.2.6.2 2019/06/10 22:05:51 christos Exp $");
31 1.2.6.2 christos
32 1.2.6.2 christos #include <sys/param.h>
33 1.2.6.2 christos #include <sys/atomic.h>
34 1.2.6.2 christos #include <sys/device.h>
35 1.2.6.2 christos #include <sys/kernel.h>
36 1.2.6.2 christos #include <sys/systm.h>
37 1.2.6.2 christos
38 1.2.6.2 christos #include <dev/clock_subr.h>
39 1.2.6.2 christos
40 1.2.6.2 christos #include <arm/amlogic/meson_rtcreg.h>
41 1.2.6.2 christos
42 1.2.6.2 christos #include <dev/fdt/fdtvar.h>
43 1.2.6.2 christos
44 1.2.6.2 christos #define RESET_RETRY_TIMES 3
45 1.2.6.2 christos #define RTC_COMM_DELAY 5
46 1.2.6.2 christos #define RTC_RESET_DELAY 100
47 1.2.6.2 christos #define RTC_STATIC_VALUE_INIT 0x180a /* XXX: MAGIC? */
48 1.2.6.2 christos
49 1.2.6.2 christos struct meson_rtc_softc {
50 1.2.6.2 christos device_t sc_dev;
51 1.2.6.2 christos bus_space_tag_t sc_bst;
52 1.2.6.2 christos bus_space_handle_t sc_bsh;
53 1.2.6.2 christos struct todr_chip_handle sc_todr;
54 1.2.6.2 christos int sc_osc_failed;
55 1.2.6.2 christos unsigned int sc_busy;
56 1.2.6.2 christos };
57 1.2.6.2 christos
58 1.2.6.2 christos static const char * const compatible[] = {
59 1.2.6.2 christos "amlogic,meson8b-rtc",
60 1.2.6.2 christos NULL
61 1.2.6.2 christos };
62 1.2.6.2 christos
63 1.2.6.2 christos static int meson_rtc_match(device_t, cfdata_t, void *);
64 1.2.6.2 christos static void meson_rtc_attach(device_t, device_t, void *);
65 1.2.6.2 christos static int meson_rtc_todr_gettime(todr_chip_handle_t, struct timeval *);
66 1.2.6.2 christos static int meson_rtc_todr_settime(todr_chip_handle_t, struct timeval *);
67 1.2.6.2 christos
68 1.2.6.2 christos CFATTACH_DECL_NEW(meson_rtc, sizeof(struct meson_rtc_softc),
69 1.2.6.2 christos meson_rtc_match, meson_rtc_attach, NULL, NULL);
70 1.2.6.2 christos
71 1.2.6.2 christos #define RTC_WRITE(sc, reg, val) \
72 1.2.6.2 christos bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
73 1.2.6.2 christos #define RTC_READ(sc, reg) \
74 1.2.6.2 christos bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
75 1.2.6.2 christos
76 1.2.6.2 christos static inline void
77 1.2.6.2 christos setbits(struct meson_rtc_softc *sc, uint32_t reg, uint32_t bits)
78 1.2.6.2 christos {
79 1.2.6.2 christos
80 1.2.6.2 christos RTC_WRITE(sc, reg, RTC_READ(sc, reg) | bits);
81 1.2.6.2 christos }
82 1.2.6.2 christos
83 1.2.6.2 christos static inline void
84 1.2.6.2 christos clrbits(struct meson_rtc_softc *sc, uint32_t reg, uint32_t bits)
85 1.2.6.2 christos {
86 1.2.6.2 christos
87 1.2.6.2 christos RTC_WRITE(sc, reg, RTC_READ(sc, reg) & ~bits);
88 1.2.6.2 christos }
89 1.2.6.2 christos
90 1.2.6.2 christos static int
91 1.2.6.2 christos meson_rtc_check_osc_clk(struct meson_rtc_softc *sc)
92 1.2.6.2 christos {
93 1.2.6.2 christos uint32_t cnt1, cnt2;
94 1.2.6.2 christos
95 1.2.6.2 christos setbits(sc, AO_RTC_REG3, AO_RTC_REG3_COUNT_ALWAYS);
96 1.2.6.2 christos
97 1.2.6.2 christos /*
98 1.2.6.2 christos * Wait for 50uS. 32.768khz is 30.5uS. This should be long
99 1.2.6.2 christos * enough for one full cycle of 32.768 khz.
100 1.2.6.2 christos */
101 1.2.6.2 christos cnt1 = RTC_READ(sc, AO_RTC_REG2);
102 1.2.6.2 christos delay(50);
103 1.2.6.2 christos cnt2 = RTC_READ(sc, AO_RTC_REG2);
104 1.2.6.2 christos
105 1.2.6.2 christos clrbits(sc, AO_RTC_REG3, AO_RTC_REG3_COUNT_ALWAYS);
106 1.2.6.2 christos
107 1.2.6.2 christos return cnt1 == cnt2;
108 1.2.6.2 christos }
109 1.2.6.2 christos
110 1.2.6.2 christos static int
111 1.2.6.2 christos meson_rtc_match(device_t parent, cfdata_t cf, void *aux)
112 1.2.6.2 christos {
113 1.2.6.2 christos struct fdt_attach_args * const faa = aux;
114 1.2.6.2 christos
115 1.2.6.2 christos return of_match_compatible(faa->faa_phandle, compatible);
116 1.2.6.2 christos }
117 1.2.6.2 christos
118 1.2.6.2 christos static void
119 1.2.6.2 christos meson_rtc_attach(device_t parent, device_t self, void *aux)
120 1.2.6.2 christos {
121 1.2.6.2 christos struct meson_rtc_softc * const sc = device_private(self);
122 1.2.6.2 christos struct fdt_attach_args * const faa = aux;
123 1.2.6.2 christos const int phandle = faa->faa_phandle;
124 1.2.6.2 christos bus_addr_t addr;
125 1.2.6.2 christos bus_size_t size;
126 1.2.6.2 christos
127 1.2.6.2 christos if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
128 1.2.6.2 christos aprint_error(": couldn't map registers\n");
129 1.2.6.2 christos return;
130 1.2.6.2 christos }
131 1.2.6.2 christos
132 1.2.6.2 christos sc->sc_dev = self;
133 1.2.6.2 christos sc->sc_bst = faa->faa_bst;
134 1.2.6.2 christos if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
135 1.2.6.2 christos aprint_error(": couldn't map registers\n");
136 1.2.6.2 christos return;
137 1.2.6.2 christos }
138 1.2.6.2 christos
139 1.2.6.2 christos sc->sc_osc_failed = meson_rtc_check_osc_clk(sc);
140 1.2.6.2 christos
141 1.2.6.2 christos memset(&sc->sc_todr, 0, sizeof(sc->sc_todr));
142 1.2.6.2 christos sc->sc_todr.cookie = sc;
143 1.2.6.2 christos sc->sc_todr.todr_gettime = meson_rtc_todr_gettime;
144 1.2.6.2 christos sc->sc_todr.todr_settime = meson_rtc_todr_settime;
145 1.2.6.2 christos
146 1.2.6.2 christos aprint_naive("\n");
147 1.2.6.2 christos aprint_normal(": RTC");
148 1.2.6.2 christos if (sc->sc_osc_failed) {
149 1.2.6.2 christos aprint_normal(" battery not present or discharged\n");
150 1.2.6.2 christos } else {
151 1.2.6.2 christos aprint_normal("\n");
152 1.2.6.2 christos fdtbus_todr_attach(self, phandle, &sc->sc_todr);
153 1.2.6.2 christos }
154 1.2.6.2 christos }
155 1.2.6.2 christos
156 1.2.6.2 christos static void
157 1.2.6.2 christos meson_rtc_sclk_pulse(struct meson_rtc_softc *sc)
158 1.2.6.2 christos {
159 1.2.6.2 christos
160 1.2.6.2 christos delay(RTC_COMM_DELAY);
161 1.2.6.2 christos setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SCLK);
162 1.2.6.2 christos
163 1.2.6.2 christos delay(RTC_COMM_DELAY);
164 1.2.6.2 christos clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SCLK);
165 1.2.6.2 christos }
166 1.2.6.2 christos
167 1.2.6.2 christos static void
168 1.2.6.2 christos meson_rtc_send_bit(struct meson_rtc_softc *sc, uint32_t bitset)
169 1.2.6.2 christos {
170 1.2.6.2 christos
171 1.2.6.2 christos if (bitset)
172 1.2.6.2 christos setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
173 1.2.6.2 christos else
174 1.2.6.2 christos clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
175 1.2.6.2 christos
176 1.2.6.2 christos meson_rtc_sclk_pulse(sc);
177 1.2.6.2 christos }
178 1.2.6.2 christos
179 1.2.6.2 christos #define SERIAL_ADDR_BITS 3
180 1.2.6.2 christos #define SERIAL_DATA_BITS 32
181 1.2.6.2 christos #define SERIAL_TYPE_ADDR (1 << (SERIAL_ADDR_BITS - 1))
182 1.2.6.2 christos #define SERIAL_TYPE_DATA (1 << (SERIAL_DATA_BITS - 1))
183 1.2.6.2 christos
184 1.2.6.2 christos static void
185 1.2.6.2 christos meson_rtc_send_data(struct meson_rtc_softc *sc,
186 1.2.6.2 christos uint32_t nextbit, uint32_t data)
187 1.2.6.2 christos {
188 1.2.6.2 christos
189 1.2.6.2 christos KASSERT(nextbit == SERIAL_TYPE_ADDR || nextbit == SERIAL_TYPE_DATA);
190 1.2.6.2 christos
191 1.2.6.2 christos while (nextbit) {
192 1.2.6.2 christos meson_rtc_send_bit(sc, data & nextbit);
193 1.2.6.2 christos nextbit >>= 1;
194 1.2.6.2 christos }
195 1.2.6.2 christos }
196 1.2.6.2 christos
197 1.2.6.2 christos static uint32_t
198 1.2.6.2 christos meson_rtc_get_data(struct meson_rtc_softc *sc)
199 1.2.6.2 christos {
200 1.2.6.2 christos uint32_t data;
201 1.2.6.2 christos size_t i;
202 1.2.6.2 christos
203 1.2.6.2 christos data = 0;
204 1.2.6.2 christos for (i = 0; i < SERIAL_DATA_BITS; i++) {
205 1.2.6.2 christos meson_rtc_sclk_pulse(sc);
206 1.2.6.2 christos data <<= 1;
207 1.2.6.2 christos data |= __SHIFTOUT(RTC_READ(sc, AO_RTC_REG1), AO_RTC_REG1_SDO);
208 1.2.6.2 christos }
209 1.2.6.2 christos return data;
210 1.2.6.2 christos }
211 1.2.6.2 christos
212 1.2.6.2 christos enum serial_mode {
213 1.2.6.2 christos SERIAL_MODE_READ,
214 1.2.6.2 christos SERIAL_MODE_WRITE,
215 1.2.6.2 christos };
216 1.2.6.2 christos
217 1.2.6.2 christos static void
218 1.2.6.2 christos meson_rtc_set_mode(struct meson_rtc_softc *sc, enum serial_mode mode)
219 1.2.6.2 christos {
220 1.2.6.2 christos
221 1.2.6.2 christos clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SEN);
222 1.2.6.2 christos
223 1.2.6.2 christos switch(mode) {
224 1.2.6.2 christos case SERIAL_MODE_READ:
225 1.2.6.2 christos clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
226 1.2.6.2 christos break;
227 1.2.6.2 christos case SERIAL_MODE_WRITE:
228 1.2.6.2 christos setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
229 1.2.6.2 christos break;
230 1.2.6.2 christos default:
231 1.2.6.2 christos KASSERT(1);
232 1.2.6.2 christos return;
233 1.2.6.2 christos }
234 1.2.6.2 christos meson_rtc_sclk_pulse(sc);
235 1.2.6.2 christos
236 1.2.6.2 christos clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
237 1.2.6.2 christos }
238 1.2.6.2 christos
239 1.2.6.2 christos static int
240 1.2.6.2 christos meson_rtc_wait_s_ready(struct meson_rtc_softc *sc)
241 1.2.6.2 christos {
242 1.2.6.2 christos size_t s_nrdy_cnt, retry_cnt;
243 1.2.6.2 christos
244 1.2.6.2 christos s_nrdy_cnt = 40000;
245 1.2.6.2 christos retry_cnt = 0;
246 1.2.6.2 christos while (!(RTC_READ(sc, AO_RTC_REG1) & AO_RTC_REG1_S_READY)) {
247 1.2.6.2 christos if (s_nrdy_cnt-- == 0) {
248 1.2.6.2 christos s_nrdy_cnt = 40000;
249 1.2.6.2 christos if (retry_cnt++ == RESET_RETRY_TIMES)
250 1.2.6.2 christos return 0;
251 1.2.6.2 christos /* XXX: reset_s_ready? Linux does not. */
252 1.2.6.2 christos setbits(sc, AO_RTC_REG1, AO_RTC_REG1_S_READY);
253 1.2.6.2 christos delay(RTC_RESET_DELAY);
254 1.2.6.2 christos }
255 1.2.6.2 christos }
256 1.2.6.2 christos return 1;
257 1.2.6.2 christos }
258 1.2.6.2 christos
259 1.2.6.2 christos static int
260 1.2.6.2 christos meson_rtc_comm_init(struct meson_rtc_softc *sc)
261 1.2.6.2 christos {
262 1.2.6.2 christos
263 1.2.6.2 christos clrbits(sc, AO_RTC_REG0,
264 1.2.6.2 christos AO_RTC_REG0_SEN | AO_RTC_REG0_SCLK | AO_RTC_REG0_SDI);
265 1.2.6.2 christos
266 1.2.6.2 christos if (meson_rtc_wait_s_ready(sc)) {
267 1.2.6.2 christos setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SEN);
268 1.2.6.2 christos return 0;
269 1.2.6.2 christos }
270 1.2.6.2 christos return -1;
271 1.2.6.2 christos }
272 1.2.6.2 christos
273 1.2.6.2 christos static void
274 1.2.6.2 christos meson_rtc_static_register_write(struct meson_rtc_softc *sc, uint32_t data)
275 1.2.6.2 christos {
276 1.2.6.2 christos uint32_t u;
277 1.2.6.2 christos
278 1.2.6.2 christos /* Program MSB 15-8 */
279 1.2.6.2 christos u = RTC_READ(sc, AO_RTC_REG4);
280 1.2.6.2 christos u &= AO_RTC_REG4_STATIC_REG_MSB;
281 1.2.6.2 christos u |= __SHIFTIN(data, AO_RTC_REG4_STATIC_REG_MSB);
282 1.2.6.2 christos RTC_WRITE(sc, AO_RTC_REG4, u);
283 1.2.6.2 christos
284 1.2.6.2 christos /* Program LSB 7-0, and start serializing */
285 1.2.6.2 christos u = RTC_READ(sc, AO_RTC_REG0);
286 1.2.6.2 christos u &= ~AO_RTC_REG0_STATIC_REG_LSB;
287 1.2.6.2 christos u |= __SHIFTIN(data, AO_RTC_REG0_STATIC_REG_LSB);
288 1.2.6.2 christos u |= AO_RTC_REG0_SERIAL_START;
289 1.2.6.2 christos RTC_WRITE(sc, AO_RTC_REG0, u);
290 1.2.6.2 christos
291 1.2.6.2 christos /* Poll auto_serializer_busy bit until it's low (IDLE) */
292 1.2.6.2 christos while ((RTC_READ(sc, AO_RTC_REG0) & AO_RTC_REG0_SERIAL_BUSY) != 0)
293 1.2.6.2 christos continue;
294 1.2.6.2 christos }
295 1.2.6.2 christos
296 1.2.6.2 christos static void
297 1.2.6.2 christos meson_rtc_reset(struct meson_rtc_softc *sc)
298 1.2.6.2 christos {
299 1.2.6.2 christos
300 1.2.6.2 christos meson_rtc_static_register_write(sc, RTC_STATIC_VALUE_INIT);
301 1.2.6.2 christos }
302 1.2.6.2 christos
303 1.2.6.2 christos static int
304 1.2.6.2 christos meson_rtc_serial_init(struct meson_rtc_softc *sc)
305 1.2.6.2 christos {
306 1.2.6.2 christos size_t init_cnt, retry_cnt;
307 1.2.6.2 christos
308 1.2.6.2 christos init_cnt = 0;
309 1.2.6.2 christos retry_cnt = 0;
310 1.2.6.2 christos while (meson_rtc_comm_init(sc) == -1) {
311 1.2.6.2 christos if (init_cnt++ == RESET_RETRY_TIMES) {
312 1.2.6.2 christos init_cnt = 0;
313 1.2.6.2 christos if (retry_cnt++ == RESET_RETRY_TIMES) {
314 1.2.6.2 christos aprint_error_dev(sc->sc_dev,
315 1.2.6.2 christos "cannot init rtc\n");
316 1.2.6.2 christos return -1;
317 1.2.6.2 christos }
318 1.2.6.2 christos meson_rtc_reset(sc);
319 1.2.6.2 christos }
320 1.2.6.2 christos delay(RTC_RESET_DELAY);
321 1.2.6.2 christos }
322 1.2.6.2 christos return 0;
323 1.2.6.2 christos }
324 1.2.6.2 christos
325 1.2.6.2 christos static int
326 1.2.6.2 christos meson_rtc_serial_read(struct meson_rtc_softc *sc, uint32_t addr,
327 1.2.6.2 christos uint32_t *sec)
328 1.2.6.2 christos {
329 1.2.6.2 christos
330 1.2.6.2 christos if (meson_rtc_serial_init(sc) == -1)
331 1.2.6.2 christos return EIO;
332 1.2.6.2 christos
333 1.2.6.2 christos meson_rtc_send_data(sc, SERIAL_TYPE_ADDR, addr);
334 1.2.6.2 christos meson_rtc_set_mode(sc, SERIAL_MODE_READ);
335 1.2.6.2 christos *sec = meson_rtc_get_data(sc);
336 1.2.6.2 christos return 0;
337 1.2.6.2 christos }
338 1.2.6.2 christos
339 1.2.6.2 christos static int
340 1.2.6.2 christos meson_rtc_serial_write(struct meson_rtc_softc *sc, uint32_t addr,
341 1.2.6.2 christos uint32_t data)
342 1.2.6.2 christos {
343 1.2.6.2 christos
344 1.2.6.2 christos if (meson_rtc_serial_init(sc) == -1)
345 1.2.6.2 christos return EIO;
346 1.2.6.2 christos
347 1.2.6.2 christos meson_rtc_send_data(sc, SERIAL_TYPE_DATA, data);
348 1.2.6.2 christos meson_rtc_send_data(sc, SERIAL_TYPE_ADDR, addr);
349 1.2.6.2 christos meson_rtc_set_mode(sc, SERIAL_MODE_WRITE);
350 1.2.6.2 christos return 0;
351 1.2.6.2 christos }
352 1.2.6.2 christos
353 1.2.6.2 christos static int
354 1.2.6.2 christos meson_rtc_todr_gettime(todr_chip_handle_t ch, struct timeval *tv)
355 1.2.6.2 christos {
356 1.2.6.2 christos struct meson_rtc_softc * const sc = ch->cookie;
357 1.2.6.2 christos uint32_t sec;
358 1.2.6.2 christos int rv;
359 1.2.6.2 christos
360 1.2.6.2 christos if (atomic_swap_uint(&sc->sc_busy, 1))
361 1.2.6.2 christos return EBUSY; /* XXX: EAGAIN? */
362 1.2.6.2 christos
363 1.2.6.2 christos rv = meson_rtc_serial_read(sc, RTC_COUNTER_ADDR, &sec);
364 1.2.6.2 christos sc->sc_busy = 0;
365 1.2.6.2 christos
366 1.2.6.2 christos if (rv == 0) {
367 1.2.6.2 christos tv->tv_sec = sec;
368 1.2.6.2 christos tv->tv_usec = 0;
369 1.2.6.2 christos }
370 1.2.6.2 christos return rv;
371 1.2.6.2 christos }
372 1.2.6.2 christos
373 1.2.6.2 christos static int
374 1.2.6.2 christos meson_rtc_todr_settime(todr_chip_handle_t ch, struct timeval *tv)
375 1.2.6.2 christos {
376 1.2.6.2 christos struct meson_rtc_softc * const sc = ch->cookie;
377 1.2.6.2 christos int rv;
378 1.2.6.2 christos
379 1.2.6.2 christos if (atomic_swap_uint(&sc->sc_busy, 1))
380 1.2.6.2 christos return EBUSY; /* XXX: EAGAIN? */
381 1.2.6.2 christos
382 1.2.6.2 christos rv = meson_rtc_serial_write(sc, RTC_COUNTER_ADDR, tv->tv_sec);
383 1.2.6.2 christos sc->sc_busy = 0;
384 1.2.6.2 christos
385 1.2.6.2 christos return rv;
386 1.2.6.2 christos }
387