rtc.c revision 1.7 1 /* $NetBSD: rtc.c,v 1.7 2003/07/05 16:48:12 tsutsui Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: Utah $Hdr: clock.c 1.18 91/01/21$
41 *
42 * @(#)clock.c 8.2 (Berkeley) 1/12/94
43 */
44
45 /*
46 * attachment for HP300 real-time clock (RTC)
47 */
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.7 2003/07/05 16:48:12 tsutsui Exp $");
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/device.h>
55 #include <sys/malloc.h>
56
57 #include <hp300/dev/intiovar.h>
58 #include <hp300/dev/rtcreg.h>
59
60 #include <dev/clock_subr.h>
61 #include <hp300/hp300/clockvar.h>
62
63 struct rtc_softc {
64 struct device sc_dev;
65 bus_space_tag_t sc_bst;
66 bus_space_handle_t sc_bsh;
67 };
68
69 static int rtcmatch(struct device *, struct cfdata *, void *);
70 static void rtcattach(struct device *, struct device *, void *aux);
71
72 CFATTACH_DECL(rtc, sizeof (struct rtc_softc),
73 rtcmatch, rtcattach, NULL, NULL);
74
75 static int rtc_gettime(todr_chip_handle_t, struct timeval *);
76 static int rtc_settime(todr_chip_handle_t, struct timeval *);
77 static int rtc_getcal(todr_chip_handle_t, int *);
78 static int rtc_setcal(todr_chip_handle_t, int);
79 static u_int8_t rtc_readreg(struct rtc_softc *, int);
80 static u_int8_t rtc_writereg(struct rtc_softc *, int, u_int8_t);
81
82
83 static int
84 rtcmatch(parent, match, aux)
85 struct device *parent;
86 struct cfdata *match;
87 void *aux;
88 {
89 struct intio_attach_args *ia = aux;
90
91 if (strcmp("rtc", ia->ia_modname) != 0)
92 return (0);
93
94 return (1);
95 }
96
97 static void
98 rtcattach(parent, self, aux)
99 struct device *parent, *self;
100 void *aux;
101 {
102 struct intio_attach_args *ia = aux;
103 struct rtc_softc *sc = (struct rtc_softc *)self;
104 struct todr_chip_handle *todr_handle;
105 bus_space_tag_t bst = ia->ia_bst;
106
107 printf("\n");
108
109 if (bus_space_map(bst, ia->ia_iobase, INTIO_DEVSIZE, 0,
110 &sc->sc_bsh)) {
111 printf("%s: can't map registers\n",
112 sc->sc_dev.dv_xname);
113 return;
114 }
115
116 sc->sc_bst = bst;
117
118 MALLOC(todr_handle, struct todr_chip_handle *,
119 sizeof(struct todr_chip_handle), M_DEVBUF, M_NOWAIT);
120
121 todr_handle->cookie = sc;
122 todr_handle->todr_gettime = rtc_gettime;
123 todr_handle->todr_settime = rtc_settime;
124 todr_handle->todr_getcal = rtc_getcal;
125 todr_handle->todr_setcal = rtc_setcal;
126 todr_handle->todr_setwen = NULL;
127
128 clockattach(todr_handle);
129 }
130
131 static int
132 rtc_gettime(todr_chip_handle_t handle, struct timeval *tv)
133 {
134 struct rtc_softc *sc = (struct rtc_softc *)handle->cookie;
135 int i, read_okay, year;
136 struct clock_ymdhms dt;
137 u_int8_t rtc_registers[NUM_RTC_REGS];
138
139 /* read rtc registers */
140 read_okay = 0;
141 while (!read_okay) {
142 read_okay = 1;
143 for (i = 0; i < NUM_RTC_REGS; i++)
144 rtc_registers[i] = rtc_readreg(sc, i);
145 for (i = 0; i < NUM_RTC_REGS; i++)
146 if (rtc_registers[i] != rtc_readreg(sc, i))
147 read_okay = 0;
148 }
149
150 #define rtc_to_decimal(a,b) (rtc_registers[a] * 10 + rtc_registers[b])
151
152 dt.dt_sec = rtc_to_decimal(1, 0);
153 dt.dt_min = rtc_to_decimal(3, 2);
154 dt.dt_hour = (rtc_registers[5] & RTC_REG5_HOUR) * 10 + rtc_registers[4];
155 dt.dt_day = rtc_to_decimal(8, 7);
156 dt.dt_mon = rtc_to_decimal(10, 9);
157
158 year = rtc_to_decimal(12, 11) + RTC_BASE_YEAR;
159 if (year < POSIX_BASE_YEAR)
160 year += 100;
161 dt.dt_year = year;
162
163 #undef rtc_to_decimal
164
165 /* simple sanity checks */
166 if (dt.dt_mon > 12 || dt.dt_day > 31 ||
167 dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
168 return (1);
169
170 tv->tv_sec = clock_ymdhms_to_secs(&dt);
171 tv->tv_usec = 0;
172 return (0);
173 }
174
175 static int
176 rtc_settime(todr_chip_handle_t handle, struct timeval *tv)
177 {
178 struct rtc_softc *sc = (struct rtc_softc *)handle->cookie;
179 int i, year;
180 struct clock_ymdhms dt;
181 u_int8_t rtc_registers[NUM_RTC_REGS];
182
183 /* Note: we ignore `tv_usec' */
184 clock_secs_to_ymdhms(tv->tv_sec, &dt);
185
186 year = dt.dt_year - RTC_BASE_YEAR;
187 if (year > 99)
188 year -= 100;
189
190 #define decimal_to_rtc(a,b,n) \
191 rtc_registers[a] = (n) % 10; \
192 rtc_registers[b] = (n) / 10;
193
194 decimal_to_rtc(0, 1, dt.dt_sec);
195 decimal_to_rtc(2, 3, dt.dt_min);
196 decimal_to_rtc(7, 8, dt.dt_day);
197 decimal_to_rtc(9, 10, dt.dt_mon);
198 decimal_to_rtc(11, 12, year);
199
200 rtc_registers[4] = dt.dt_hour % 10;
201 rtc_registers[5] = ((dt.dt_hour / 10) & RTC_REG5_HOUR) | RTC_REG5_24HR;
202
203 rtc_registers[6] = 0;
204
205 #undef decimal_to_rtc
206
207 /* write rtc registers */
208 rtc_writereg(sc, 15, 13); /* reset prescalar */
209 for (i = 0; i < NUM_RTC_REGS; i++)
210 if (rtc_registers[i] !=
211 rtc_writereg(sc, i, rtc_registers[i]))
212 return (1);
213 return (0);
214 }
215
216 static int
217 rtc_getcal(todr_chip_handle_t handle, int *vp)
218 {
219 return (EOPNOTSUPP);
220 }
221
222 static int
223 rtc_setcal(todr_chip_handle_t handle, int v)
224 {
225 return (EOPNOTSUPP);
226 }
227
228 static u_int8_t
229 rtc_readreg(struct rtc_softc *sc, int reg)
230 {
231 bus_space_tag_t bst = sc->sc_bst;
232 bus_space_handle_t bsh = sc->sc_bsh;
233 u_int8_t data;
234 int s = splvm();
235
236 data = reg;
237 intio_device_writecmd(bst, bsh, RTC_SET_REG, &data, 1);
238 intio_device_readcmd(bst, bsh, RTC_READ_REG, &data);
239
240 splx(s);
241 return (data);
242 }
243
244 static u_int8_t
245 rtc_writereg(struct rtc_softc *sc, int reg, u_int8_t data)
246 {
247 bus_space_tag_t bst = sc->sc_bst;
248 bus_space_handle_t bsh = sc->sc_bsh;
249 u_int8_t tmp;
250 int s = splvm();
251
252 tmp = (data << 4) | reg;
253 intio_device_writecmd(bst, bsh, RTC_SET_REG, &tmp, 1);
254 intio_device_writecmd(bst, bsh, RTC_WRITE_REG, NULL, 0);
255 intio_device_readcmd(bst, bsh, RTC_READ_REG, &tmp);
256
257 splx(s);
258 return (tmp);
259 }
260