xref: /netbsd-src/sys/arch/hp300/dev/rtc.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: rtc.c,v 1.19 2008/03/29 06:47:08 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.19 2008/03/29 06:47:08 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 	device_t sc_dev;
98 	bus_space_tag_t sc_bst;
99 	bus_space_handle_t sc_bsh;
100 	struct todr_chip_handle sc_handle;
101 };
102 
103 static int	rtcmatch(device_t, cfdata_t, void *);
104 static void	rtcattach(device_t, device_t, void *aux);
105 
106 CFATTACH_DECL_NEW(rtc, sizeof(struct rtc_softc),
107     rtcmatch, rtcattach, NULL, NULL);
108 
109 static int	rtc_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
110 static int	rtc_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
111 static uint8_t	rtc_readreg(struct rtc_softc *, int);
112 static uint8_t	rtc_writereg(struct rtc_softc *, int, uint8_t);
113 
114 
115 static int
116 rtcmatch(device_t parent, cfdata_t cf, void *aux)
117 {
118 	struct intio_attach_args *ia = aux;
119 
120 	if (strcmp("rtc", ia->ia_modname) != 0)
121 		return 0;
122 
123 	return 1;
124 }
125 
126 static void
127 rtcattach(device_t parent, device_t self, void *aux)
128 {
129 	struct rtc_softc *sc = device_private(self);
130 	struct intio_attach_args *ia = aux;
131 	struct todr_chip_handle *todr_handle;
132 	bus_space_tag_t bst = ia->ia_bst;
133 
134 	sc->sc_dev = self;
135 	if (bus_space_map(bst, ia->ia_iobase, INTIO_DEVSIZE, 0,
136 	    &sc->sc_bsh)) {
137 		aprint_error(": can't map registers\n");
138 		return;
139 	}
140 
141 	aprint_normal("\n");
142 
143 	sc->sc_bst = bst;
144 
145 	todr_handle = &sc->sc_handle;
146 	todr_handle->cookie = sc;
147 	todr_handle->todr_gettime = NULL;
148 	todr_handle->todr_settime = NULL;
149 	todr_handle->todr_gettime_ymdhms = rtc_gettime_ymdhms;
150 	todr_handle->todr_settime_ymdhms = rtc_settime_ymdhms;
151 	todr_handle->todr_setwen = NULL;
152 
153 	todr_attach(todr_handle);
154 }
155 
156 static int
157 rtc_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
158 {
159 	struct rtc_softc *sc;
160 	int i, year;
161 	bool read_okay;
162 	uint8_t rtc_registers[NUM_RTC_REGS];
163 
164 	sc = handle->cookie;
165 
166 	/* read rtc registers */
167 	read_okay = false;
168 	while (!read_okay) {
169 		read_okay = true;
170 		for (i = 0; i < NUM_RTC_REGS; i++)
171 			rtc_registers[i] = rtc_readreg(sc, i);
172 		for (i = 0; i < NUM_RTC_REGS; i++)
173 			if (rtc_registers[i] != rtc_readreg(sc, i))
174 				read_okay = false;
175 	}
176 
177 #define	rtc_to_decimal(a,b) 	(rtc_registers[a] * 10 + rtc_registers[b])
178 
179 	dt->dt_sec  = rtc_to_decimal(1, 0);
180 	dt->dt_min  = rtc_to_decimal(3, 2);
181 	dt->dt_hour = (rtc_registers[5] & RTC_REG5_HOUR) * 10 +
182 	    rtc_registers[4];
183 	dt->dt_day  = rtc_to_decimal(8, 7);
184 	dt->dt_mon  = rtc_to_decimal(10, 9);
185 
186 	year = rtc_to_decimal(12, 11) + RTC_BASE_YEAR;
187 	if (year < POSIX_BASE_YEAR)
188 		year += 100;
189 	dt->dt_year = year;
190 
191 #undef	rtc_to_decimal
192 
193 	return 0;
194 }
195 
196 static int
197 rtc_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
198 {
199 	struct rtc_softc *sc;
200 	int i, year;
201 	uint8_t rtc_registers[NUM_RTC_REGS];
202 
203 	sc = handle->cookie;
204 	year = dt->dt_year - RTC_BASE_YEAR;
205 	if (year > 99)
206 		year -= 100;
207 
208 #define	decimal_to_rtc(a,b,n)		\
209 	rtc_registers[a] = (n) % 10;	\
210 	rtc_registers[b] = (n) / 10;
211 
212 	decimal_to_rtc(0, 1, dt->dt_sec);
213 	decimal_to_rtc(2, 3, dt->dt_min);
214 	decimal_to_rtc(7, 8, dt->dt_day);
215 	decimal_to_rtc(9, 10, dt->dt_mon);
216 	decimal_to_rtc(11, 12, year);
217 
218 	rtc_registers[4] = dt->dt_hour % 10;
219 	rtc_registers[5] = ((dt->dt_hour / 10) & RTC_REG5_HOUR) | RTC_REG5_24HR;
220 
221 	rtc_registers[6] = 0;
222 
223 #undef	decimal_to_rtc
224 
225 	/* write rtc registers */
226 	rtc_writereg(sc, 15, 13);	/* reset prescalar */
227 	for (i = 0; i < NUM_RTC_REGS; i++)
228 		if (rtc_registers[i] !=
229 		    rtc_writereg(sc, i, rtc_registers[i]))
230 			return 1;
231 	return 0;
232 }
233 
234 static uint8_t
235 rtc_readreg(struct rtc_softc *sc, int reg)
236 {
237 	bus_space_tag_t bst = sc->sc_bst;
238 	bus_space_handle_t bsh = sc->sc_bsh;
239 	uint8_t data;
240 	int s = splvm();
241 
242 	data = reg;
243 	intio_device_writecmd(bst, bsh, RTC_SET_REG, &data, 1);
244 	intio_device_readcmd(bst, bsh, RTC_READ_REG, &data);
245 
246 	splx(s);
247 	return data;
248 }
249 
250 static uint8_t
251 rtc_writereg(struct rtc_softc *sc, int reg, uint8_t data)
252 {
253 	bus_space_tag_t bst = sc->sc_bst;
254 	bus_space_handle_t bsh = sc->sc_bsh;
255 	uint8_t tmp;
256 	int s = splvm();
257 
258 	tmp = (data << 4) | reg;
259 	intio_device_writecmd(bst, bsh, RTC_SET_REG, &tmp, 1);
260 	intio_device_writecmd(bst, bsh, RTC_WRITE_REG, NULL, 0);
261 	intio_device_readcmd(bst, bsh, RTC_READ_REG, &tmp);
262 
263 	splx(s);
264 	return tmp;
265 }
266