xref: /netbsd/sys/arch/sparc/dev/rtc.c (revision bf9ec67e)
1 /*	$NetBSD: rtc.c,v 1.4 2002/04/04 17:45:24 uwe Exp $ */
2 
3 /*
4  * Copyright (c) 2001 Valeriy E. Ushakov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * `rtc' is a DS1287A (== MC146818A) time-of-day clock at EBus.
32  * In Krups it's not used to store idprom so this driver doesn't
33  * support it.  Don't know about other ms-IIep systems.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #ifdef GPROF
42 #include <sys/gmon.h>
43 #endif
44 
45 #include <machine/bus.h>
46 #include <machine/autoconf.h>
47 
48 #include <dev/clock_subr.h>
49 #include <dev/ic/mc146818reg.h>
50 
51 #include <dev/ebus/ebusreg.h>
52 #include <dev/ebus/ebusvar.h>
53 
54 struct rtc_ebus_softc {
55 	struct device		sc_dev;
56 
57 	bus_space_tag_t		sc_bt;	/* parent bus tag */
58 	bus_space_handle_t	sc_bh;	/* handle for registers */
59 };
60 
61 static int	rtcmatch_ebus(struct device *, struct cfdata *, void *);
62 static void	rtcattach_ebus(struct device *, struct device *, void *);
63 
64 struct cfattach rtc_ebus_ca = {
65 	sizeof(struct rtc_ebus_softc), rtcmatch_ebus, rtcattach_ebus
66 };
67 
68 
69 /* XXX: global TOD clock handle (sparc/clock.c) */
70 extern todr_chip_handle_t todr_handle;
71 
72 /* todr(9) methods */
73 int rtc_gettime(todr_chip_handle_t, struct timeval *);
74 int rtc_settime(todr_chip_handle_t, struct timeval *);
75 int rtc_getcal(todr_chip_handle_t, int *);
76 int rtc_setcal(todr_chip_handle_t, int);
77 
78 int rtc_auto_century_adjust = 1; /* XXX: do we ever want not to? */
79 
80 
81 /*
82  * MD read/write functions declared in mc146818reg.h
83  */
84 #define	RTC_ADDR	0
85 #define	RTC_DATA	1
86 
87 u_int
88 mc146818_read(cookie, reg)
89 	void *cookie;
90 	u_int reg;
91 {
92 	struct rtc_ebus_softc *sc = cookie;
93 
94 	bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg);
95 	return (bus_space_read_1(sc->sc_bt, sc->sc_bh, RTC_DATA));
96 }
97 
98 void
99 mc146818_write(cookie, reg, datum)
100 	void *cookie;
101 	u_int reg;
102 	u_int datum;
103 {
104 	struct rtc_ebus_softc *sc = cookie;
105 
106 	bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg);
107 	bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_DATA, datum);
108 }
109 
110 
111 static int
112 rtcmatch_ebus(parent, cf, aux)
113 	struct device *parent;
114 	struct cfdata *cf;
115 	void *aux;
116 {
117 	struct ebus_attach_args *ea = aux;
118 
119 	return (strcmp(cf->cf_driver->cd_name, ea->ea_name) == 0);
120 }
121 
122 static void
123 rtcattach_ebus(parent, self, aux)
124 	struct device *parent;
125 	struct device *self;
126 	void *aux;
127 {
128 	struct rtc_ebus_softc *sc = (void *)self;
129 	struct ebus_attach_args *ea = aux;
130 
131 	todr_chip_handle_t handle;
132 
133 	sc->sc_bt = ea->ea_bustag;
134 	if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
135 			  ea->ea_reg[0].size, 0, &sc->sc_bh) != 0)
136 	{
137 		printf(": unable to map registers\n");
138 		return;
139 	}
140 
141 	/* XXX: no "model" property in Krups */
142 	printf(": time-of-day clock\n");
143 
144 	/*
145 	 * Turn interrupts off (clear MC_REGB_?IE bits), just in case
146 	 * (although they shouldn't be wired to an interrupt
147 	 * controller on sparcs).
148 	 */
149 	mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
150 
151 	/* setup our todr_handle */
152 	handle = malloc(ALIGN(sizeof(struct todr_chip_handle)),
153 			M_DEVBUF, M_NOWAIT);
154 
155 	handle->cookie = sc;
156 	handle->bus_cookie = NULL; /* unused */
157 	handle->todr_gettime = rtc_gettime;
158 	handle->todr_settime = rtc_settime;
159 	handle->todr_getcal = rtc_getcal;
160 	handle->todr_setcal = rtc_setcal;
161 	handle->todr_setwen = NULL; /* not necessary, no idprom to protect */
162 
163 	todr_handle = handle;
164 }
165 
166 
167 /*
168  * Get time-of-day and convert to a `struct timeval'
169  * Return 0 on success; an error number otherwise.
170  */
171 int
172 rtc_gettime(handle, tv)
173 	todr_chip_handle_t handle;
174 	struct timeval *tv;
175 {
176 	struct rtc_ebus_softc *sc = handle->cookie;
177 	struct clock_ymdhms dt;
178 	u_int year;
179 
180 	/* update in progress; spin loop */
181 	while (mc146818_read(sc, MC_REGA) & MC_REGA_UIP)
182 		continue;
183 
184 	/* stop updates (XXX: do we need that???) */
185 	mc146818_write(sc, MC_REGB,
186 		       (mc146818_read(sc, MC_REGB) | MC_REGB_SET));
187 
188 	/* read time */
189 	dt.dt_sec  = mc146818_read(sc, MC_SEC);
190 	dt.dt_min  = mc146818_read(sc, MC_MIN);
191 	dt.dt_hour = mc146818_read(sc, MC_HOUR);
192 	dt.dt_day  = mc146818_read(sc, MC_DOM);
193 	dt.dt_mon  = mc146818_read(sc, MC_MONTH);
194 	year       = mc146818_read(sc, MC_YEAR);
195 
196 	/* reenable updates */
197 	mc146818_write(sc, MC_REGB,
198 		       (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET));
199 
200 	/* year in the century 0..99: adjust to AD */
201 	year += 1900;
202 	if (year < POSIX_BASE_YEAR && rtc_auto_century_adjust != 0)
203 		year += 100;
204 	dt.dt_year = year;
205 
206 	/* simple sanity checks */
207 	if (dt.dt_mon > 12 || dt.dt_day > 31
208 	    || dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
209 		return (ERANGE);
210 
211 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
212 	tv->tv_usec = 0;
213 	return (0);
214 }
215 
216 /*
217  * Set the time-of-day clock based on the value of the `struct timeval' arg.
218  * Return 0 on success; an error number otherwise.
219  */
220 int
221 rtc_settime(handle, tv)
222 	todr_chip_handle_t handle;
223 	struct timeval *tv;
224 {
225 	struct rtc_ebus_softc *sc = handle->cookie;
226 	struct clock_ymdhms dt;
227 	u_int year;
228 	u_int wday;
229 
230 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
231 
232 	year = dt.dt_year - 1900;
233 	if (year >= 100 && rtc_auto_century_adjust != 0)
234 		year -= 100;
235 
236 	wday = dt.dt_wday;
237 	if (wday == 0)
238 		wday = 7;
239 
240 	/* stop updates */
241 	mc146818_write(sc, MC_REGB,
242 		       (mc146818_read(sc, MC_REGB) | MC_REGB_SET));
243 
244 	mc146818_write(sc, MC_SEC,   dt.dt_sec);
245 	mc146818_write(sc, MC_MIN,   dt.dt_min);
246 	mc146818_write(sc, MC_HOUR,  dt.dt_hour);
247 	mc146818_write(sc, MC_DOW,   wday);
248 	mc146818_write(sc, MC_DOM,   dt.dt_day);
249 	mc146818_write(sc, MC_MONTH, dt.dt_mon);
250 	mc146818_write(sc, MC_YEAR,  year);
251 
252 	/* reenable updates */
253 	mc146818_write(sc, MC_REGB,
254 		       (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET));
255 	return (0);
256 }
257 
258 
259 /*
260  * RTC does not support TOD clock calibration
261  */
262 
263 /* ARGSUSED */
264 int
265 rtc_getcal(handle, vp)
266 	todr_chip_handle_t handle;
267 	int *vp;
268 {
269 	return (EOPNOTSUPP);
270 }
271 
272 /* ARGSUSED */
273 int
274 rtc_setcal(handle, v)
275 	todr_chip_handle_t handle;
276 	int v;
277 {
278 	return (EOPNOTSUPP);
279 }
280