No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / sh3 / dev / rtc.c
blob4563ccc754e31dcd5e7ae76949d935b4988d0913
1 /* $NetBSD: rtc.c,v 1.6 2008/12/16 22:35:25 christos Exp $ */
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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>
40 #ifdef GPROF
41 #include <sys/gmon.h>
42 #endif
44 #include <dev/clock_subr.h>
46 #include <sh3/rtcreg.h>
48 #if defined(DEBUG) && !defined(RTC_DEBUG)
49 #define RTC_DEBUG
50 #endif
53 struct rtc_softc {
54 device_t sc_dev;
56 int sc_valid;
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);
67 /* todr(9) methods */
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 *);
73 static int
74 rtc_match(device_t parent, cfdata_t cfp, void *aux)
77 return 1;
81 static void
82 rtc_attach(device_t parent, device_t self, void *aux)
84 struct rtc_softc *sc;
85 uint8_t r;
86 #ifdef RTC_DEBUG
87 char bits[128];
88 #endif
90 aprint_naive("\n");
91 aprint_normal("\n");
93 sc = device_private(self);
94 sc->sc_dev = self;
96 r = _reg_read_1(SH_(RCR2));
98 #ifdef RTC_DEBUG
99 snprintb(bits, sizeof(bits), SH_RCR2_BITS, r);
100 aprint_debug_dev(sc->sc_dev, "RCR2=%s\n", bits);
101 #endif
103 /* Was the clock running? */
104 if ((r & (SH_RCR2_ENABLE | SH_RCR2_START)) == (SH_RCR2_ENABLE
105 | SH_RCR2_START))
106 sc->sc_valid = 1;
107 else {
108 sc->sc_valid = 0;
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);
124 #ifdef RTC_DEBUG
126 struct clock_ymdhms dt;
127 rtc_gettime_ymdhms(&sc->sc_todr, &dt);
129 #endif
131 if (!pmf_device_register(self, NULL, NULL))
132 aprint_error_dev(self, "unable to establish power handler\n");
136 static int
137 rtc_gettime_ymdhms(todr_chip_handle_t h, struct clock_ymdhms *dt)
139 struct rtc_softc *sc = h->cookie;
140 unsigned int year;
141 int retry = 8;
143 if (!sc->sc_valid) {
144 #ifdef RTC_DEBUG
145 aprint_debug_dev(sc->sc_dev, "gettime: not valid\n");
146 /* but proceed and read/print it anyway */
147 #else
148 return EIO;
149 #endif
152 /* disable carry interrupt */
153 _reg_bclr_1(SH_(RCR1), SH_RCR1_CIE);
155 do {
156 uint8_t r = _reg_read_1(SH_(RCR1));
157 r &= ~SH_RCR1_CF;
158 r |= SH_RCR1_AF; /* don't clear alarm flag */
159 _reg_write_1(SH_(RCR1), r);
161 if (CPU_IS_SH3)
162 year = _reg_read_1(SH3_RYRCNT);
163 else
164 year = _reg_read_2(SH4_RYRCNT) & 0x00ff;
165 dt->dt_year = FROMBCD(year);
167 /* read counter */
168 #define RTCGET(x, y) \
169 dt->dt_ ## x = FROMBCD(_reg_read_1(SH_(R ## y ## CNT)))
171 RTCGET(mon, MON);
172 RTCGET(wday, WK);
173 RTCGET(day, DAY);
174 RTCGET(hour, HR);
175 RTCGET(min, MIN);
176 RTCGET(sec, SEC);
177 #undef RTCGET
178 } while ((_reg_read_1(SH_(RCR1)) & SH_RCR1_CF) && --retry > 0);
180 if (retry == 0) {
181 #ifdef RTC_DEBUG
182 aprint_debug_dev(sc->sc_dev, "gettime: retry failed\n");
183 #endif
184 return EIO;
187 dt->dt_year += 1900;
188 if (dt->dt_year < POSIX_BASE_YEAR)
189 dt->dt_year += 100;
191 #ifdef RTC_DEBUG
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);
197 if (!sc->sc_valid)
198 return EIO;
199 #endif
201 return 0;
205 static int
206 rtc_settime_ymdhms(todr_chip_handle_t h, struct clock_ymdhms *dt)
208 struct rtc_softc *sc = h->cookie;
209 unsigned int year;
210 uint8_t r;
212 year = TOBCD(dt->dt_year % 100);
214 r = _reg_read_1(SH_(RCR2));
216 /* stop clock */
217 _reg_write_1(SH_(RCR2), (r & ~SH_RCR2_START) | SH_RCR2_RESET);
219 /* set time */
220 if (CPU_IS_SH3)
221 _reg_write_1(SH3_RYRCNT, year);
222 else
223 _reg_write_2(SH4_RYRCNT, year);
225 #define RTCSET(x, y) \
226 _reg_write_1(SH_(R ## x ## CNT), TOBCD(dt->dt_ ## y))
228 RTCSET(MON, mon);
229 RTCSET(WK, wday);
230 RTCSET(DAY, day);
231 RTCSET(HR, hour);
232 RTCSET(MIN, min);
233 RTCSET(SEC, sec);
235 #undef RTCSET
237 /* start clock */
238 _reg_write_1(SH_(RCR2), r);
239 sc->sc_valid = 1;
241 #ifdef RTC_DEBUG
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);
246 #endif
248 return 0;