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