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