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