rtc.c revision 1.9 1 1.9 martin /* $NetBSD: rtc.c,v 1.9 2014/09/08 10:00:18 martin Exp $ */
2 1.4 uwe
3 1.1 uwe /*-
4 1.1 uwe * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 uwe * All rights reserved.
6 1.1 uwe *
7 1.1 uwe * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uwe * by UCHIYAMA Yasushi.
9 1.1 uwe *
10 1.1 uwe * Redistribution and use in source and binary forms, with or without
11 1.1 uwe * modification, are permitted provided that the following conditions
12 1.1 uwe * are met:
13 1.1 uwe * 1. Redistributions of source code must retain the above copyright
14 1.1 uwe * notice, this list of conditions and the following disclaimer.
15 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uwe * notice, this list of conditions and the following disclaimer in the
17 1.1 uwe * documentation and/or other materials provided with the distribution.
18 1.1 uwe *
19 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 uwe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 uwe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 uwe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 uwe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 uwe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 uwe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 uwe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 uwe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 uwe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 uwe * POSSIBILITY OF SUCH DAMAGE.
30 1.1 uwe */
31 1.1 uwe
32 1.1 uwe #include <sys/cdefs.h>
33 1.9 martin __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.9 2014/09/08 10:00:18 martin Exp $");
34 1.1 uwe
35 1.1 uwe #include <sys/param.h>
36 1.1 uwe #include <sys/kernel.h>
37 1.1 uwe #include <sys/device.h>
38 1.1 uwe #include <sys/malloc.h>
39 1.1 uwe #include <sys/systm.h>
40 1.1 uwe #ifdef GPROF
41 1.1 uwe #include <sys/gmon.h>
42 1.1 uwe #endif
43 1.1 uwe
44 1.1 uwe #include <dev/clock_subr.h>
45 1.1 uwe
46 1.1 uwe #include <sh3/rtcreg.h>
47 1.1 uwe
48 1.2 uwe #if defined(DEBUG) && !defined(RTC_DEBUG)
49 1.2 uwe #define RTC_DEBUG
50 1.2 uwe #endif
51 1.2 uwe
52 1.1 uwe
53 1.1 uwe struct rtc_softc {
54 1.3 uwe device_t sc_dev;
55 1.1 uwe
56 1.2 uwe int sc_valid;
57 1.1 uwe struct todr_chip_handle sc_todr;
58 1.8 tsutsui u_int sc_year0;
59 1.1 uwe };
60 1.1 uwe
61 1.3 uwe static int rtc_match(device_t, cfdata_t, void *);
62 1.3 uwe static void rtc_attach(device_t, device_t, void *);
63 1.1 uwe
64 1.3 uwe CFATTACH_DECL_NEW(rtc, sizeof(struct rtc_softc),
65 1.1 uwe rtc_match, rtc_attach, NULL, NULL);
66 1.1 uwe
67 1.1 uwe
68 1.1 uwe /* todr(9) methods */
69 1.1 uwe static int rtc_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
70 1.1 uwe static int rtc_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
71 1.1 uwe
72 1.8 tsutsui #ifndef SH3_RTC_BASEYEAR
73 1.8 tsutsui #define SH3_RTC_BASEYEAR 1900
74 1.8 tsutsui #endif
75 1.8 tsutsui u_int sh3_rtc_baseyear = SH3_RTC_BASEYEAR;
76 1.1 uwe
77 1.1 uwe static int
78 1.3 uwe rtc_match(device_t parent, cfdata_t cfp, void *aux)
79 1.1 uwe {
80 1.1 uwe
81 1.1 uwe return 1;
82 1.1 uwe }
83 1.1 uwe
84 1.1 uwe
85 1.1 uwe static void
86 1.3 uwe rtc_attach(device_t parent, device_t self, void *aux)
87 1.1 uwe {
88 1.3 uwe struct rtc_softc *sc;
89 1.2 uwe uint8_t r;
90 1.8 tsutsui prop_number_t prop_rtc_baseyear;
91 1.2 uwe #ifdef RTC_DEBUG
92 1.2 uwe char bits[128];
93 1.2 uwe #endif
94 1.1 uwe
95 1.3 uwe aprint_naive("\n");
96 1.3 uwe aprint_normal("\n");
97 1.3 uwe
98 1.3 uwe sc = device_private(self);
99 1.3 uwe sc->sc_dev = self;
100 1.1 uwe
101 1.2 uwe r = _reg_read_1(SH_(RCR2));
102 1.1 uwe
103 1.1 uwe #ifdef RTC_DEBUG
104 1.6 christos snprintb(bits, sizeof(bits), SH_RCR2_BITS, r);
105 1.6 christos aprint_debug_dev(sc->sc_dev, "RCR2=%s\n", bits);
106 1.2 uwe #endif
107 1.1 uwe
108 1.2 uwe /* Was the clock running? */
109 1.2 uwe if ((r & (SH_RCR2_ENABLE | SH_RCR2_START)) == (SH_RCR2_ENABLE
110 1.2 uwe | SH_RCR2_START))
111 1.2 uwe sc->sc_valid = 1;
112 1.2 uwe else {
113 1.2 uwe sc->sc_valid = 0;
114 1.3 uwe aprint_error_dev(sc->sc_dev, "WARNING: clock was stopped\n");
115 1.1 uwe }
116 1.2 uwe
117 1.2 uwe /* Disable carry and alarm interrupts */
118 1.2 uwe _reg_write_1(SH_(RCR1), 0);
119 1.2 uwe
120 1.2 uwe /* Clock runs, no periodic interrupts, no 30-sec adjustment */
121 1.2 uwe _reg_write_1(SH_(RCR2), SH_RCR2_ENABLE | SH_RCR2_START);
122 1.2 uwe
123 1.1 uwe sc->sc_todr.cookie = sc;
124 1.1 uwe sc->sc_todr.todr_gettime_ymdhms = rtc_gettime_ymdhms;
125 1.1 uwe sc->sc_todr.todr_settime_ymdhms = rtc_settime_ymdhms;
126 1.1 uwe
127 1.8 tsutsui prop_rtc_baseyear = prop_dictionary_get(device_properties(self),
128 1.8 tsutsui "sh3_rtc_baseyear");
129 1.8 tsutsui if (prop_rtc_baseyear != NULL) {
130 1.8 tsutsui sh3_rtc_baseyear =
131 1.8 tsutsui (u_int)prop_number_integer_value(prop_rtc_baseyear);
132 1.8 tsutsui #ifdef RTC_DEBUG
133 1.8 tsutsui aprint_debug_dev(self,
134 1.8 tsutsui "using baseyear %u passed via device property\n",
135 1.8 tsutsui sh3_rtc_baseyear);
136 1.8 tsutsui #endif
137 1.8 tsutsui }
138 1.8 tsutsui sc->sc_year0 = sh3_rtc_baseyear;
139 1.8 tsutsui
140 1.1 uwe todr_attach(&sc->sc_todr);
141 1.2 uwe
142 1.2 uwe #ifdef RTC_DEBUG
143 1.2 uwe {
144 1.2 uwe struct clock_ymdhms dt;
145 1.2 uwe rtc_gettime_ymdhms(&sc->sc_todr, &dt);
146 1.2 uwe }
147 1.2 uwe #endif
148 1.7 uwe
149 1.7 uwe if (!pmf_device_register(self, NULL, NULL))
150 1.7 uwe aprint_error_dev(self, "unable to establish power handler\n");
151 1.1 uwe }
152 1.1 uwe
153 1.1 uwe
154 1.1 uwe static int
155 1.1 uwe rtc_gettime_ymdhms(todr_chip_handle_t h, struct clock_ymdhms *dt)
156 1.1 uwe {
157 1.2 uwe struct rtc_softc *sc = h->cookie;
158 1.1 uwe unsigned int year;
159 1.1 uwe int retry = 8;
160 1.1 uwe
161 1.2 uwe if (!sc->sc_valid) {
162 1.2 uwe #ifdef RTC_DEBUG
163 1.3 uwe aprint_debug_dev(sc->sc_dev, "gettime: not valid\n");
164 1.2 uwe /* but proceed and read/print it anyway */
165 1.2 uwe #else
166 1.2 uwe return EIO;
167 1.2 uwe #endif
168 1.2 uwe }
169 1.2 uwe
170 1.1 uwe /* disable carry interrupt */
171 1.1 uwe _reg_bclr_1(SH_(RCR1), SH_RCR1_CIE);
172 1.1 uwe
173 1.1 uwe do {
174 1.1 uwe uint8_t r = _reg_read_1(SH_(RCR1));
175 1.1 uwe r &= ~SH_RCR1_CF;
176 1.1 uwe r |= SH_RCR1_AF; /* don't clear alarm flag */
177 1.1 uwe _reg_write_1(SH_(RCR1), r);
178 1.1 uwe
179 1.1 uwe if (CPU_IS_SH3)
180 1.1 uwe year = _reg_read_1(SH3_RYRCNT);
181 1.1 uwe else
182 1.1 uwe year = _reg_read_2(SH4_RYRCNT) & 0x00ff;
183 1.1 uwe dt->dt_year = FROMBCD(year);
184 1.1 uwe
185 1.1 uwe /* read counter */
186 1.1 uwe #define RTCGET(x, y) \
187 1.1 uwe dt->dt_ ## x = FROMBCD(_reg_read_1(SH_(R ## y ## CNT)))
188 1.1 uwe
189 1.1 uwe RTCGET(mon, MON);
190 1.1 uwe RTCGET(wday, WK);
191 1.1 uwe RTCGET(day, DAY);
192 1.1 uwe RTCGET(hour, HR);
193 1.1 uwe RTCGET(min, MIN);
194 1.1 uwe RTCGET(sec, SEC);
195 1.1 uwe #undef RTCGET
196 1.1 uwe } while ((_reg_read_1(SH_(RCR1)) & SH_RCR1_CF) && --retry > 0);
197 1.1 uwe
198 1.2 uwe if (retry == 0) {
199 1.2 uwe #ifdef RTC_DEBUG
200 1.3 uwe aprint_debug_dev(sc->sc_dev, "gettime: retry failed\n");
201 1.2 uwe #endif
202 1.1 uwe return EIO;
203 1.2 uwe }
204 1.1 uwe
205 1.8 tsutsui dt->dt_year += sc->sc_year0;
206 1.1 uwe if (dt->dt_year < POSIX_BASE_YEAR)
207 1.1 uwe dt->dt_year += 100;
208 1.1 uwe
209 1.2 uwe #ifdef RTC_DEBUG
210 1.3 uwe aprint_debug_dev(sc->sc_dev,
211 1.9 martin "gettime: %04lu-%02d-%02d %02d:%02d:%02d\n",
212 1.9 martin (unsigned long)dt->dt_year, dt->dt_mon, dt->dt_day,
213 1.3 uwe dt->dt_hour, dt->dt_min, dt->dt_sec);
214 1.2 uwe
215 1.2 uwe if (!sc->sc_valid)
216 1.2 uwe return EIO;
217 1.2 uwe #endif
218 1.2 uwe
219 1.1 uwe return 0;
220 1.1 uwe }
221 1.1 uwe
222 1.1 uwe
223 1.1 uwe static int
224 1.1 uwe rtc_settime_ymdhms(todr_chip_handle_t h, struct clock_ymdhms *dt)
225 1.1 uwe {
226 1.2 uwe struct rtc_softc *sc = h->cookie;
227 1.1 uwe unsigned int year;
228 1.1 uwe uint8_t r;
229 1.1 uwe
230 1.8 tsutsui year = dt->dt_year - sc->sc_year0;
231 1.8 tsutsui if (year > 99)
232 1.8 tsutsui year -= 100;
233 1.8 tsutsui
234 1.8 tsutsui year = TOBCD(year);
235 1.1 uwe
236 1.2 uwe r = _reg_read_1(SH_(RCR2));
237 1.2 uwe
238 1.1 uwe /* stop clock */
239 1.2 uwe _reg_write_1(SH_(RCR2), (r & ~SH_RCR2_START) | SH_RCR2_RESET);
240 1.1 uwe
241 1.1 uwe /* set time */
242 1.1 uwe if (CPU_IS_SH3)
243 1.1 uwe _reg_write_1(SH3_RYRCNT, year);
244 1.1 uwe else
245 1.1 uwe _reg_write_2(SH4_RYRCNT, year);
246 1.1 uwe
247 1.1 uwe #define RTCSET(x, y) \
248 1.1 uwe _reg_write_1(SH_(R ## x ## CNT), TOBCD(dt->dt_ ## y))
249 1.1 uwe
250 1.1 uwe RTCSET(MON, mon);
251 1.1 uwe RTCSET(WK, wday);
252 1.1 uwe RTCSET(DAY, day);
253 1.1 uwe RTCSET(HR, hour);
254 1.1 uwe RTCSET(MIN, min);
255 1.1 uwe RTCSET(SEC, sec);
256 1.1 uwe
257 1.1 uwe #undef RTCSET
258 1.1 uwe
259 1.1 uwe /* start clock */
260 1.2 uwe _reg_write_1(SH_(RCR2), r);
261 1.2 uwe sc->sc_valid = 1;
262 1.2 uwe
263 1.2 uwe #ifdef RTC_DEBUG
264 1.3 uwe aprint_debug_dev(sc->sc_dev,
265 1.9 martin "settime: %04lu-%02d-%02d %02d:%02d:%02d\n",
266 1.9 martin (unsigned long)dt->dt_year, dt->dt_mon, dt->dt_day,
267 1.3 uwe dt->dt_hour, dt->dt_min, dt->dt_sec);
268 1.2 uwe #endif
269 1.1 uwe
270 1.1 uwe return 0;
271 1.1 uwe }
272