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