1 /* $NetBSD: rs5c313.c,v 1.7 2008/03/27 02:15:29 uwe Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: rs5c313.c,v 1.7 2008/03/27 02:15:29 uwe Exp $");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kernel.h>
37 #include <dev/clock_subr.h>
39 #include <dev/ic/rs5c313reg.h>
40 #include <dev/ic/rs5c313var.h>
44 static int rs5c313_todr_gettime_ymdhms(todr_chip_handle_t
, struct clock_ymdhms
*);
45 static int rs5c313_todr_settime_ymdhms(todr_chip_handle_t
, struct clock_ymdhms
*);
47 /* sugar for chip access */
48 #define rtc_begin(sc) ((*sc->sc_ops->rs5c313_op_begin)(sc))
49 #define rtc_ce(sc, onoff) ((*sc->sc_ops->rs5c313_op_ce)(sc, onoff))
50 #define rtc_clk(sc, onoff) ((*sc->sc_ops->rs5c313_op_clk)(sc, onoff))
51 #define rtc_dir(sc, output) ((*sc->sc_ops->rs5c313_op_dir)(sc, output))
52 #define rtc_di(sc) ((*sc->sc_ops->rs5c313_op_read)(sc))
53 #define rtc_do(sc, bit) ((*sc->sc_ops->rs5c313_op_write)(sc, bit))
55 static int rs5c313_init(struct rs5c313_softc
*);
56 static int rs5c313_read_reg(struct rs5c313_softc
*, int);
57 static void rs5c313_write_reg(struct rs5c313_softc
*, int, int);
61 rs5c313_attach(struct rs5c313_softc
*sc
)
63 device_t self
= sc
->sc_dev
;
66 aprint_normal(": real time clock\n");
68 sc
->sc_todr
.cookie
= sc
;
69 sc
->sc_todr
.todr_gettime_ymdhms
= rs5c313_todr_gettime_ymdhms
;
70 sc
->sc_todr
.todr_settime_ymdhms
= rs5c313_todr_settime_ymdhms
;
72 if (rs5c313_init(sc
) != 0) {
73 aprint_error_dev(self
, "init failed\n");
77 todr_attach(&sc
->sc_todr
);
82 rs5c313_init(struct rs5c313_softc
*sc
)
84 device_t self
= sc
->sc_dev
;
93 if ((rs5c313_read_reg(sc
, RS5C313_CTRL
) & CTRL_XSTP
) == 0) {
99 aprint_error_dev(self
, "time not valid\n");
101 rs5c313_write_reg(sc
, RS5C313_TINT
, 0);
102 rs5c313_write_reg(sc
, RS5C313_CTRL
, (CTRL_BASE
| CTRL_ADJ
));
104 for (retry
= 1000; retry
> 0; --retry
) {
105 if (rs5c313_read_reg(sc
, RS5C313_CTRL
) & CTRL_BSY
)
116 rs5c313_write_reg(sc
, RS5C313_CTRL
, CTRL_BASE
);
125 rs5c313_todr_gettime_ymdhms(todr_chip_handle_t todr
, struct clock_ymdhms
*dt
)
127 struct rs5c313_softc
*sc
= todr
->cookie
;
132 * If chip had invalid data on init, don't bother reading
133 * bogus values, let todr(9) cope.
135 if (sc
->sc_valid
== 0)
141 for (retry
= 10; retry
> 0; --retry
) {
144 rs5c313_write_reg(sc
, RS5C313_CTRL
, CTRL_BASE
);
145 if ((rs5c313_read_reg(sc
, RS5C313_CTRL
) & CTRL_BSY
) == 0)
157 #define RTCGET(x, y) \
159 int ones = rs5c313_read_reg(sc, RS5C313_ ## y ## 1); \
160 int tens = rs5c313_read_reg(sc, RS5C313_ ## y ## 10); \
161 dt->dt_ ## x = tens * 10 + ones; \
162 } while (/* CONSTCOND */0)
171 dt
->dt_wday
= rs5c313_read_reg(sc
, RS5C313_WDAY
);
177 dt
->dt_year
= (dt
->dt_year
% 100) + 1900;
178 if (dt
->dt_year
< POSIX_BASE_YEAR
) {
187 rs5c313_todr_settime_ymdhms(todr_chip_handle_t todr
, struct clock_ymdhms
*dt
)
189 struct rs5c313_softc
*sc
= todr
->cookie
;
197 for (retry
= 10; retry
> 0; --retry
) {
200 rs5c313_write_reg(sc
, RS5C313_CTRL
, CTRL_BASE
);
201 if ((rs5c313_read_reg(sc
, RS5C313_CTRL
) & CTRL_BSY
) == 0)
213 #define RTCSET(x, y) \
215 t = TOBCD(dt->dt_ ## y) & 0xff; \
216 rs5c313_write_reg(sc, RS5C313_ ## x ## 1, t & 0x0f); \
217 rs5c313_write_reg(sc, RS5C313_ ## x ## 10, (t >> 4) & 0x0f); \
218 } while (/* CONSTCOND */0)
228 t
= dt
->dt_year
% 100;
230 rs5c313_write_reg(sc
, RS5C313_YEAR1
, t
& 0x0f);
231 rs5c313_write_reg(sc
, RS5C313_YEAR10
, (t
>> 4) & 0x0f);
233 rs5c313_write_reg(sc
, RS5C313_WDAY
, dt
->dt_wday
);
244 rs5c313_read_reg(struct rs5c313_softc
*sc
, int addr
)
252 rtc_do(sc
, 1); /* ignored */
253 rtc_do(sc
, 1); /* R/#W = 1(READ) */
254 rtc_do(sc
, 1); /* AD = 1 */
255 rtc_do(sc
, 0); /* DT = 0 */
258 rtc_do(sc
, addr
& 0x8); /* A3 */
259 rtc_do(sc
, addr
& 0x4); /* A2 */
260 rtc_do(sc
, addr
& 0x2); /* A1 */
261 rtc_do(sc
, addr
& 0x1); /* A0 */
273 data
= rtc_di(sc
); /* D3 */
275 data
|= rtc_di(sc
); /* D2 */
277 data
|= rtc_di(sc
); /* D1 */
279 data
|= rtc_di(sc
); /* D0 */
286 rs5c313_write_reg(struct rs5c313_softc
*sc
, int addr
, int data
)
293 rtc_do(sc
, 1); /* ignored */
294 rtc_do(sc
, 0); /* R/#W = 0 (WRITE) */
295 rtc_do(sc
, 1); /* AD = 1 */
296 rtc_do(sc
, 0); /* DT = 0 */
299 rtc_do(sc
, addr
& 0x8); /* A3 */
300 rtc_do(sc
, addr
& 0x4); /* A2 */
301 rtc_do(sc
, addr
& 0x2); /* A1 */
302 rtc_do(sc
, addr
& 0x1); /* A0 */
305 rtc_do(sc
, 1); /* ignored */
306 rtc_do(sc
, 0); /* R/#W = 0(WRITE) */
307 rtc_do(sc
, 0); /* AD = 0 */
308 rtc_do(sc
, 1); /* DT = 1 */
311 rtc_do(sc
, data
& 0x8); /* D3 */
312 rtc_do(sc
, data
& 0x4); /* D2 */
313 rtc_do(sc
, data
& 0x2); /* D1 */
314 rtc_do(sc
, data
& 0x1); /* D0 */