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