1 /* $NetBSD: rtc.c,v 1.6 2008/12/16 22:35:25 christos Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.6 2008/12/16 22:35:25 christos Exp $");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/malloc.h>
39 #include <sys/systm.h>
44 #include <dev/clock_subr.h>
46 #include <sh3/rtcreg.h>
48 #if defined(DEBUG) && !defined(RTC_DEBUG)
57 struct todr_chip_handle sc_todr
;
60 static int rtc_match(device_t
, cfdata_t
, void *);
61 static void rtc_attach(device_t
, device_t
, void *);
63 CFATTACH_DECL_NEW(rtc
, sizeof(struct rtc_softc
),
64 rtc_match
, rtc_attach
, NULL
, NULL
);
68 static int rtc_gettime_ymdhms(todr_chip_handle_t
, struct clock_ymdhms
*);
69 static int rtc_settime_ymdhms(todr_chip_handle_t
, struct clock_ymdhms
*);
74 rtc_match(device_t parent
, cfdata_t cfp
, void *aux
)
82 rtc_attach(device_t parent
, device_t self
, void *aux
)
93 sc
= device_private(self
);
96 r
= _reg_read_1(SH_(RCR2
));
99 snprintb(bits
, sizeof(bits
), SH_RCR2_BITS
, r
);
100 aprint_debug_dev(sc
->sc_dev
, "RCR2=%s\n", bits
);
103 /* Was the clock running? */
104 if ((r
& (SH_RCR2_ENABLE
| SH_RCR2_START
)) == (SH_RCR2_ENABLE
109 aprint_error_dev(sc
->sc_dev
, "WARNING: clock was stopped\n");
112 /* Disable carry and alarm interrupts */
113 _reg_write_1(SH_(RCR1
), 0);
115 /* Clock runs, no periodic interrupts, no 30-sec adjustment */
116 _reg_write_1(SH_(RCR2
), SH_RCR2_ENABLE
| SH_RCR2_START
);
118 sc
->sc_todr
.cookie
= sc
;
119 sc
->sc_todr
.todr_gettime_ymdhms
= rtc_gettime_ymdhms
;
120 sc
->sc_todr
.todr_settime_ymdhms
= rtc_settime_ymdhms
;
122 todr_attach(&sc
->sc_todr
);
126 struct clock_ymdhms dt
;
127 rtc_gettime_ymdhms(&sc
->sc_todr
, &dt
);
131 if (!pmf_device_register(self
, NULL
, NULL
))
132 aprint_error_dev(self
, "unable to establish power handler\n");
137 rtc_gettime_ymdhms(todr_chip_handle_t h
, struct clock_ymdhms
*dt
)
139 struct rtc_softc
*sc
= h
->cookie
;
145 aprint_debug_dev(sc
->sc_dev
, "gettime: not valid\n");
146 /* but proceed and read/print it anyway */
152 /* disable carry interrupt */
153 _reg_bclr_1(SH_(RCR1
), SH_RCR1_CIE
);
156 uint8_t r
= _reg_read_1(SH_(RCR1
));
158 r
|= SH_RCR1_AF
; /* don't clear alarm flag */
159 _reg_write_1(SH_(RCR1
), r
);
162 year
= _reg_read_1(SH3_RYRCNT
);
164 year
= _reg_read_2(SH4_RYRCNT
) & 0x00ff;
165 dt
->dt_year
= FROMBCD(year
);
168 #define RTCGET(x, y) \
169 dt->dt_ ## x = FROMBCD(_reg_read_1(SH_(R ## y ## CNT)))
178 } while ((_reg_read_1(SH_(RCR1
)) & SH_RCR1_CF
) && --retry
> 0);
182 aprint_debug_dev(sc
->sc_dev
, "gettime: retry failed\n");
188 if (dt
->dt_year
< POSIX_BASE_YEAR
)
192 aprint_debug_dev(sc
->sc_dev
,
193 "gettime: %04d-%02d-%02d %02d:%02d:%02d\n",
194 dt
->dt_year
, dt
->dt_mon
, dt
->dt_day
,
195 dt
->dt_hour
, dt
->dt_min
, dt
->dt_sec
);
206 rtc_settime_ymdhms(todr_chip_handle_t h
, struct clock_ymdhms
*dt
)
208 struct rtc_softc
*sc
= h
->cookie
;
212 year
= TOBCD(dt
->dt_year
% 100);
214 r
= _reg_read_1(SH_(RCR2
));
217 _reg_write_1(SH_(RCR2
), (r
& ~SH_RCR2_START
) | SH_RCR2_RESET
);
221 _reg_write_1(SH3_RYRCNT
, year
);
223 _reg_write_2(SH4_RYRCNT
, year
);
225 #define RTCSET(x, y) \
226 _reg_write_1(SH_(R ## x ## CNT), TOBCD(dt->dt_ ## y))
238 _reg_write_1(SH_(RCR2
), r
);
242 aprint_debug_dev(sc
->sc_dev
,
243 "settime: %04d-%02d-%02d %02d:%02d:%02d\n",
244 dt
->dt_year
, dt
->dt_mon
, dt
->dt_day
,
245 dt
->dt_hour
, dt
->dt_min
, dt
->dt_sec
);