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