rtc.c revision 1.10 1 /* $NetBSD: rtc.c,v 1.10 2003/07/15 00:04:56 lukem 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/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.10 2003/07/15 00:04:56 lukem Exp $");
38
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h>
44 #ifdef GPROF
45 #include <sys/gmon.h>
46 #endif
47
48 #include <machine/bus.h>
49 #include <machine/autoconf.h>
50
51 #include <dev/clock_subr.h>
52 #include <dev/ic/mc146818reg.h>
53
54 #include <dev/ebus/ebusreg.h>
55 #include <dev/ebus/ebusvar.h>
56
57 struct rtc_ebus_softc {
58 struct device sc_dev;
59
60 bus_space_tag_t sc_bt; /* parent bus tag */
61 bus_space_handle_t sc_bh; /* handle for registers */
62 };
63
64 static int rtcmatch_ebus(struct device *, struct cfdata *, void *);
65 static void rtcattach_ebus(struct device *, struct device *, void *);
66
67 CFATTACH_DECL(rtc_ebus, sizeof(struct rtc_ebus_softc),
68 rtcmatch_ebus, rtcattach_ebus, NULL, NULL);
69
70 /* XXX: global TOD clock handle (sparc/clock.c) */
71 extern todr_chip_handle_t todr_handle;
72
73 /* todr(9) methods */
74 int rtc_gettime(todr_chip_handle_t, struct timeval *);
75 int rtc_settime(todr_chip_handle_t, struct timeval *);
76 int rtc_getcal(todr_chip_handle_t, int *);
77 int rtc_setcal(todr_chip_handle_t, int);
78
79 int rtc_auto_century_adjust = 1; /* XXX: do we ever want not to? */
80
81
82 /*
83 * MD read/write functions declared in mc146818reg.h
84 */
85 #define RTC_ADDR 0
86 #define RTC_DATA 1
87
88 u_int
89 mc146818_read(cookie, reg)
90 void *cookie;
91 u_int reg;
92 {
93 struct rtc_ebus_softc *sc = cookie;
94
95 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg);
96 return (bus_space_read_1(sc->sc_bt, sc->sc_bh, RTC_DATA));
97 }
98
99 void
100 mc146818_write(cookie, reg, datum)
101 void *cookie;
102 u_int reg;
103 u_int datum;
104 {
105 struct rtc_ebus_softc *sc = cookie;
106
107 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg);
108 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_DATA, datum);
109 }
110
111
112 static int
113 rtcmatch_ebus(parent, cf, aux)
114 struct device *parent;
115 struct cfdata *cf;
116 void *aux;
117 {
118 struct ebus_attach_args *ea = aux;
119
120 return (strcmp(cf->cf_name, ea->ea_name) == 0);
121 }
122
123 static void
124 rtcattach_ebus(parent, self, aux)
125 struct device *parent;
126 struct device *self;
127 void *aux;
128 {
129 struct rtc_ebus_softc *sc = (void *)self;
130 struct ebus_attach_args *ea = aux;
131
132 todr_chip_handle_t handle;
133
134 sc->sc_bt = ea->ea_bustag;
135 if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
136 ea->ea_reg[0].size, 0, &sc->sc_bh) != 0)
137 {
138 printf(": unable to map registers\n");
139 return;
140 }
141
142 /* XXX: no "model" property in Krups */
143 printf(": time-of-day clock\n");
144
145 /*
146 * Turn interrupts off (clear MC_REGB_?IE bits), just in case
147 * (although they shouldn't be wired to an interrupt
148 * controller on sparcs).
149 */
150 mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
151
152 /* setup our todr_handle */
153 handle = malloc(ALIGN(sizeof(struct todr_chip_handle)),
154 M_DEVBUF, M_NOWAIT);
155
156 handle->cookie = sc;
157 handle->bus_cookie = NULL; /* unused */
158 handle->todr_gettime = rtc_gettime;
159 handle->todr_settime = rtc_settime;
160 handle->todr_getcal = rtc_getcal;
161 handle->todr_setcal = rtc_setcal;
162 handle->todr_setwen = NULL; /* not necessary, no idprom to protect */
163
164 todr_handle = handle;
165 }
166
167
168 /*
169 * Get time-of-day and convert to a `struct timeval'
170 * Return 0 on success; an error number otherwise.
171 */
172 int
173 rtc_gettime(handle, tv)
174 todr_chip_handle_t handle;
175 struct timeval *tv;
176 {
177 struct rtc_ebus_softc *sc = handle->cookie;
178 struct clock_ymdhms dt;
179 u_int year;
180
181 /* update in progress; spin loop */
182 while (mc146818_read(sc, MC_REGA) & MC_REGA_UIP)
183 continue;
184
185 /* stop updates (XXX: do we need that???) */
186 mc146818_write(sc, MC_REGB,
187 (mc146818_read(sc, MC_REGB) | MC_REGB_SET));
188
189 /* read time */
190 dt.dt_sec = mc146818_read(sc, MC_SEC);
191 dt.dt_min = mc146818_read(sc, MC_MIN);
192 dt.dt_hour = mc146818_read(sc, MC_HOUR);
193 dt.dt_day = mc146818_read(sc, MC_DOM);
194 dt.dt_mon = mc146818_read(sc, MC_MONTH);
195 year = mc146818_read(sc, MC_YEAR);
196
197 /* reenable updates */
198 mc146818_write(sc, MC_REGB,
199 (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET));
200
201 /* year in the century 0..99: adjust to AD */
202 year += 1900;
203 if (year < POSIX_BASE_YEAR && rtc_auto_century_adjust != 0)
204 year += 100;
205 dt.dt_year = year;
206
207 /* simple sanity checks */
208 if (dt.dt_mon > 12 || dt.dt_day > 31
209 || dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
210 return (ERANGE);
211
212 tv->tv_sec = clock_ymdhms_to_secs(&dt);
213 tv->tv_usec = 0;
214 return (0);
215 }
216
217 /*
218 * Set the time-of-day clock based on the value of the `struct timeval' arg.
219 * Return 0 on success; an error number otherwise.
220 */
221 int
222 rtc_settime(handle, tv)
223 todr_chip_handle_t handle;
224 struct timeval *tv;
225 {
226 struct rtc_ebus_softc *sc = handle->cookie;
227 struct clock_ymdhms dt;
228 u_int year;
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 /* stop updates */
237 mc146818_write(sc, MC_REGB,
238 (mc146818_read(sc, MC_REGB) | MC_REGB_SET));
239
240 mc146818_write(sc, MC_SEC, dt.dt_sec);
241 mc146818_write(sc, MC_MIN, dt.dt_min);
242 mc146818_write(sc, MC_HOUR, dt.dt_hour);
243 mc146818_write(sc, MC_DOW, dt.dt_wday + 1);
244 mc146818_write(sc, MC_DOM, dt.dt_day);
245 mc146818_write(sc, MC_MONTH, dt.dt_mon);
246 mc146818_write(sc, MC_YEAR, year);
247
248 /* reenable updates */
249 mc146818_write(sc, MC_REGB,
250 (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET));
251 return (0);
252 }
253
254
255 /*
256 * RTC does not support TOD clock calibration
257 */
258
259 /* ARGSUSED */
260 int
261 rtc_getcal(handle, vp)
262 todr_chip_handle_t handle;
263 int *vp;
264 {
265 return (EOPNOTSUPP);
266 }
267
268 /* ARGSUSED */
269 int
270 rtc_setcal(handle, v)
271 todr_chip_handle_t handle;
272 int v;
273 {
274 return (EOPNOTSUPP);
275 }
276