rtc.c revision 1.2.4.4 1 1.2.4.4 jdolecek /* $NetBSD: rtc.c,v 1.2.4.4 2002/10/10 18:32:42 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.4 jdolecek __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.2.4.4 2002/10/10 18:32:42 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.4 jdolecek CFATTACH_DECL(rtc, sizeof (struct rtc_softc),
73 1.2.4.4 jdolecek rtcmatch, rtcattach, NULL, NULL);
74 1.2.4.2 thorpej
75 1.2.4.2 thorpej static int rtc_gettime(todr_chip_handle_t, struct timeval *);
76 1.2.4.2 thorpej static int rtc_settime(todr_chip_handle_t, struct timeval *);
77 1.2.4.2 thorpej static int rtc_getcal(todr_chip_handle_t, int *);
78 1.2.4.2 thorpej static int rtc_setcal(todr_chip_handle_t, int);
79 1.2.4.2 thorpej static u_int8_t rtc_readreg(struct rtc_softc *, int);
80 1.2.4.2 thorpej static u_int8_t rtc_writereg(struct rtc_softc *, int, u_int8_t);
81 1.2.4.2 thorpej
82 1.2.4.2 thorpej
83 1.2.4.2 thorpej static int
84 1.2.4.2 thorpej rtcmatch(parent, match, aux)
85 1.2.4.2 thorpej struct device *parent;
86 1.2.4.2 thorpej struct cfdata *match;
87 1.2.4.2 thorpej void *aux;
88 1.2.4.2 thorpej {
89 1.2.4.2 thorpej struct intio_attach_args *ia = aux;
90 1.2.4.2 thorpej
91 1.2.4.3 jdolecek if (strcmp("rtc", ia->ia_modname) != 0)
92 1.2.4.2 thorpej return (0);
93 1.2.4.2 thorpej
94 1.2.4.2 thorpej return (1);
95 1.2.4.2 thorpej }
96 1.2.4.2 thorpej
97 1.2.4.2 thorpej static void
98 1.2.4.2 thorpej rtcattach(parent, self, aux)
99 1.2.4.2 thorpej struct device *parent, *self;
100 1.2.4.2 thorpej void *aux;
101 1.2.4.2 thorpej {
102 1.2.4.2 thorpej struct intio_attach_args *ia = aux;
103 1.2.4.2 thorpej struct rtc_softc *sc = (struct rtc_softc *)self;
104 1.2.4.2 thorpej struct todr_chip_handle *todr_handle;
105 1.2.4.2 thorpej
106 1.2.4.2 thorpej printf("\n");
107 1.2.4.2 thorpej
108 1.2.4.2 thorpej if (bus_space_map(ia->ia_bst, ia->ia_iobase, INTIO_DEVSIZE, 0,
109 1.2.4.2 thorpej &sc->sc_bsh)) {
110 1.2.4.2 thorpej printf("%s: can't map registers\n",
111 1.2.4.2 thorpej sc->sc_dev.dv_xname);
112 1.2.4.2 thorpej return;
113 1.2.4.2 thorpej }
114 1.2.4.2 thorpej
115 1.2.4.3 jdolecek MALLOC(todr_handle, struct todr_chip_handle *,
116 1.2.4.3 jdolecek sizeof(struct todr_chip_handle), M_DEVBUF, M_NOWAIT);
117 1.2.4.2 thorpej
118 1.2.4.2 thorpej todr_handle->cookie = sc;
119 1.2.4.2 thorpej todr_handle->todr_gettime = rtc_gettime;
120 1.2.4.2 thorpej todr_handle->todr_settime = rtc_settime;
121 1.2.4.2 thorpej todr_handle->todr_getcal = rtc_getcal;
122 1.2.4.2 thorpej todr_handle->todr_setcal = rtc_setcal;
123 1.2.4.2 thorpej todr_handle->todr_setwen = NULL;
124 1.2.4.2 thorpej
125 1.2.4.2 thorpej clockattach(todr_handle);
126 1.2.4.2 thorpej }
127 1.2.4.2 thorpej
128 1.2.4.2 thorpej static int
129 1.2.4.2 thorpej rtc_gettime(todr_chip_handle_t handle, struct timeval *tv)
130 1.2.4.2 thorpej {
131 1.2.4.2 thorpej struct rtc_softc *sc = (struct rtc_softc *)handle->cookie;
132 1.2.4.2 thorpej int i, read_okay, year;
133 1.2.4.2 thorpej struct clock_ymdhms dt;
134 1.2.4.2 thorpej u_int8_t rtc_registers[NUM_RTC_REGS];
135 1.2.4.2 thorpej
136 1.2.4.2 thorpej /* read rtc registers */
137 1.2.4.2 thorpej read_okay = 0;
138 1.2.4.2 thorpej while (!read_okay) {
139 1.2.4.2 thorpej read_okay = 1;
140 1.2.4.2 thorpej for (i = 0; i < NUM_RTC_REGS; i++)
141 1.2.4.2 thorpej rtc_registers[i] = rtc_readreg(sc, i);
142 1.2.4.2 thorpej for (i = 0; i < NUM_RTC_REGS; i++)
143 1.2.4.2 thorpej if (rtc_registers[i] != rtc_readreg(sc, i))
144 1.2.4.2 thorpej read_okay = 0;
145 1.2.4.2 thorpej }
146 1.2.4.2 thorpej
147 1.2.4.2 thorpej #define rtc_to_decimal(a,b) (rtc_registers[a] * 10 + rtc_registers[b])
148 1.2.4.2 thorpej
149 1.2.4.2 thorpej dt.dt_sec = rtc_to_decimal(1, 0);
150 1.2.4.2 thorpej dt.dt_min = rtc_to_decimal(3, 2);
151 1.2.4.2 thorpej dt.dt_hour = (rtc_registers[5] & RTC_REG5_HOUR) * 10 + rtc_registers[4];
152 1.2.4.2 thorpej dt.dt_day = rtc_to_decimal(8, 7);
153 1.2.4.2 thorpej dt.dt_mon = rtc_to_decimal(10, 9);
154 1.2.4.2 thorpej
155 1.2.4.2 thorpej year = rtc_to_decimal(12, 11) + RTC_BASE_YEAR;
156 1.2.4.2 thorpej if (year < POSIX_BASE_YEAR)
157 1.2.4.2 thorpej year += 100;
158 1.2.4.2 thorpej dt.dt_year = year;
159 1.2.4.2 thorpej
160 1.2.4.2 thorpej #undef rtc_to_decimal
161 1.2.4.2 thorpej
162 1.2.4.2 thorpej /* simple sanity checks */
163 1.2.4.2 thorpej if (dt.dt_mon > 12 || dt.dt_day > 31 ||
164 1.2.4.2 thorpej dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
165 1.2.4.2 thorpej return (1);
166 1.2.4.2 thorpej
167 1.2.4.2 thorpej tv->tv_sec = clock_ymdhms_to_secs(&dt);
168 1.2.4.2 thorpej tv->tv_usec = 0;
169 1.2.4.2 thorpej return (0);
170 1.2.4.2 thorpej }
171 1.2.4.2 thorpej
172 1.2.4.2 thorpej static int
173 1.2.4.2 thorpej rtc_settime(todr_chip_handle_t handle, struct timeval *tv)
174 1.2.4.2 thorpej {
175 1.2.4.2 thorpej struct rtc_softc *sc = (struct rtc_softc *)handle->cookie;
176 1.2.4.2 thorpej int i, year;
177 1.2.4.2 thorpej struct clock_ymdhms dt;
178 1.2.4.2 thorpej u_int8_t rtc_registers[NUM_RTC_REGS];
179 1.2.4.2 thorpej
180 1.2.4.2 thorpej /* Note: we ignore `tv_usec' */
181 1.2.4.2 thorpej clock_secs_to_ymdhms(tv->tv_sec, &dt);
182 1.2.4.2 thorpej
183 1.2.4.2 thorpej year = dt.dt_year - RTC_BASE_YEAR;
184 1.2.4.2 thorpej if (year > 99)
185 1.2.4.2 thorpej year -= 100;
186 1.2.4.2 thorpej
187 1.2.4.2 thorpej #define decimal_to_rtc(a,b,n) \
188 1.2.4.2 thorpej rtc_registers[a] = (n) % 10; \
189 1.2.4.2 thorpej rtc_registers[b] = (n) / 10;
190 1.2.4.2 thorpej
191 1.2.4.2 thorpej decimal_to_rtc(0, 1, dt.dt_sec);
192 1.2.4.2 thorpej decimal_to_rtc(2, 3, dt.dt_min);
193 1.2.4.2 thorpej decimal_to_rtc(7, 8, dt.dt_day);
194 1.2.4.2 thorpej decimal_to_rtc(9, 10, dt.dt_mon);
195 1.2.4.2 thorpej decimal_to_rtc(11, 12, year);
196 1.2.4.2 thorpej
197 1.2.4.2 thorpej rtc_registers[4] = dt.dt_hour % 10;
198 1.2.4.2 thorpej rtc_registers[5] = ((dt.dt_hour / 10) & RTC_REG5_HOUR) | RTC_REG5_24HR;
199 1.2.4.2 thorpej
200 1.2.4.2 thorpej rtc_registers[6] = 0;
201 1.2.4.2 thorpej
202 1.2.4.2 thorpej #undef decimal_to_rtc
203 1.2.4.2 thorpej
204 1.2.4.2 thorpej /* write rtc registers */
205 1.2.4.2 thorpej rtc_writereg(sc, 15, 13); /* reset prescalar */
206 1.2.4.2 thorpej for (i = 0; i < NUM_RTC_REGS; i++)
207 1.2.4.2 thorpej if (rtc_registers[i] !=
208 1.2.4.2 thorpej rtc_writereg(sc, i, rtc_registers[i]))
209 1.2.4.2 thorpej return (1);
210 1.2.4.2 thorpej return (0);
211 1.2.4.2 thorpej }
212 1.2.4.2 thorpej
213 1.2.4.2 thorpej static int
214 1.2.4.2 thorpej rtc_getcal(todr_chip_handle_t handle, int *vp)
215 1.2.4.2 thorpej {
216 1.2.4.2 thorpej return (EOPNOTSUPP);
217 1.2.4.2 thorpej }
218 1.2.4.2 thorpej
219 1.2.4.2 thorpej static int
220 1.2.4.2 thorpej rtc_setcal(todr_chip_handle_t handle, int v)
221 1.2.4.2 thorpej {
222 1.2.4.2 thorpej return (EOPNOTSUPP);
223 1.2.4.2 thorpej }
224 1.2.4.2 thorpej
225 1.2.4.2 thorpej static u_int8_t
226 1.2.4.2 thorpej rtc_readreg(struct rtc_softc *sc, int reg)
227 1.2.4.2 thorpej {
228 1.2.4.2 thorpej bus_space_tag_t bst = sc->sc_bst;
229 1.2.4.2 thorpej bus_space_handle_t bsh = sc->sc_bsh;
230 1.2.4.2 thorpej u_int8_t data;
231 1.2.4.2 thorpej int s = splvm();
232 1.2.4.2 thorpej
233 1.2.4.2 thorpej data = reg;
234 1.2.4.2 thorpej intio_device_writecmd(bst, bsh, RTC_SET_REG, &data, 1);
235 1.2.4.2 thorpej intio_device_readcmd(bst, bsh, RTC_READ_REG, &data);
236 1.2.4.2 thorpej
237 1.2.4.2 thorpej splx(s);
238 1.2.4.2 thorpej return (data);
239 1.2.4.2 thorpej }
240 1.2.4.2 thorpej
241 1.2.4.2 thorpej static u_int8_t
242 1.2.4.2 thorpej rtc_writereg(struct rtc_softc *sc, int reg, u_int8_t data)
243 1.2.4.2 thorpej {
244 1.2.4.2 thorpej bus_space_tag_t bst = sc->sc_bst;
245 1.2.4.2 thorpej bus_space_handle_t bsh = sc->sc_bsh;
246 1.2.4.2 thorpej u_int8_t tmp;
247 1.2.4.2 thorpej int s = splvm();
248 1.2.4.2 thorpej
249 1.2.4.2 thorpej tmp = (data << 4) | reg;
250 1.2.4.2 thorpej intio_device_writecmd(bst, bsh, RTC_SET_REG, &tmp, 1);
251 1.2.4.2 thorpej intio_device_writecmd(bst, bsh, RTC_WRITE_REG, NULL, 0);
252 1.2.4.2 thorpej intio_device_readcmd(bst, bsh, RTC_READ_REG, &tmp);
253 1.2.4.2 thorpej
254 1.2.4.2 thorpej splx(s);
255 1.2.4.2 thorpej return (tmp);
256 1.2.4.2 thorpej }
257