rtc.c revision 1.12.4.1 1 /* $NetBSD: rtc.c,v 1.12.4.1 2006/09/09 02:43:09 rpaulo 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.12.4.1 2006/09/09 02:43:09 rpaulo 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 static int rtc_gettime(todr_chip_handle_t, volatile struct timeval *);
75 static int rtc_settime(todr_chip_handle_t, volatile struct timeval *);
76
77 int rtc_auto_century_adjust = 1; /* XXX: do we ever want not to? */
78
79
80 /*
81 * MD read/write functions declared in mc146818reg.h
82 */
83 #define RTC_ADDR 0
84 #define RTC_DATA 1
85
86 u_int
87 mc146818_read(void *cookie, u_int reg)
88 {
89 struct rtc_ebus_softc *sc = cookie;
90
91 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg);
92 return (bus_space_read_1(sc->sc_bt, sc->sc_bh, RTC_DATA));
93 }
94
95 void
96 mc146818_write(void *cookie, u_int reg, u_int datum)
97 {
98 struct rtc_ebus_softc *sc = cookie;
99
100 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg);
101 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_DATA, datum);
102 }
103
104
105 static int
106 rtcmatch_ebus(struct device *parent, struct cfdata *cf, void *aux)
107 {
108 struct ebus_attach_args *ea = aux;
109
110 return (strcmp(cf->cf_name, ea->ea_name) == 0);
111 }
112
113 static void
114 rtcattach_ebus(struct device *parent, struct device *self, void *aux)
115 {
116 struct rtc_ebus_softc *sc = (void *)self;
117 struct ebus_attach_args *ea = aux;
118
119 todr_chip_handle_t handle;
120
121 sc->sc_bt = ea->ea_bustag;
122 if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
123 ea->ea_reg[0].size, 0, &sc->sc_bh) != 0)
124 {
125 printf(": unable to map registers\n");
126 return;
127 }
128
129 /* XXX: no "model" property in Krups */
130 printf(": time-of-day clock\n");
131
132 /*
133 * Turn interrupts off (clear MC_REGB_?IE bits), just in case
134 * (although they shouldn't be wired to an interrupt
135 * controller on sparcs).
136 */
137 mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
138
139 /* setup our todr_handle */
140 handle = malloc(ALIGN(sizeof(struct todr_chip_handle)),
141 M_DEVBUF, M_NOWAIT);
142
143 handle->cookie = sc;
144 handle->bus_cookie = NULL; /* unused */
145 handle->todr_gettime = rtc_gettime;
146 handle->todr_settime = rtc_settime;
147 handle->todr_setwen = NULL; /* not necessary, no idprom to protect */
148
149 todr_attach(handle);
150 }
151
152
153 /*
154 * Get time-of-day and convert to a `struct timeval'
155 * Return 0 on success; an error number otherwise.
156 */
157 static int
158 rtc_gettime(todr_chip_handle_t handle, volatile struct timeval *tv)
159 {
160 struct rtc_ebus_softc *sc = handle->cookie;
161 struct clock_ymdhms dt;
162 u_int year;
163
164 /* update in progress; spin loop */
165 while (mc146818_read(sc, MC_REGA) & MC_REGA_UIP)
166 continue;
167
168 /* stop updates (XXX: do we need that???) */
169 mc146818_write(sc, MC_REGB,
170 (mc146818_read(sc, MC_REGB) | MC_REGB_SET));
171
172 /* read time */
173 dt.dt_sec = mc146818_read(sc, MC_SEC);
174 dt.dt_min = mc146818_read(sc, MC_MIN);
175 dt.dt_hour = mc146818_read(sc, MC_HOUR);
176 dt.dt_day = mc146818_read(sc, MC_DOM);
177 dt.dt_mon = mc146818_read(sc, MC_MONTH);
178 year = mc146818_read(sc, MC_YEAR);
179
180 /* reenable updates */
181 mc146818_write(sc, MC_REGB,
182 (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET));
183
184 /* year in the century 0..99: adjust to AD */
185 year += 1900;
186 if (year < POSIX_BASE_YEAR && rtc_auto_century_adjust != 0)
187 year += 100;
188 dt.dt_year = year;
189
190 /* simple sanity checks */
191 if (dt.dt_mon > 12 || dt.dt_day > 31
192 || dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
193 return (ERANGE);
194
195 tv->tv_sec = clock_ymdhms_to_secs(&dt);
196 tv->tv_usec = 0;
197 return (0);
198 }
199
200 /*
201 * Set the time-of-day clock based on the value of the `struct timeval' arg.
202 * Return 0 on success; an error number otherwise.
203 */
204 static int
205 rtc_settime(todr_chip_handle_t handle, volatile struct timeval *tv)
206 {
207 struct rtc_ebus_softc *sc = handle->cookie;
208 struct clock_ymdhms dt;
209 u_int year;
210
211 clock_secs_to_ymdhms(tv->tv_sec, &dt);
212
213 year = dt.dt_year - 1900;
214 if (year >= 100 && rtc_auto_century_adjust != 0)
215 year -= 100;
216
217 /* stop updates */
218 mc146818_write(sc, MC_REGB,
219 (mc146818_read(sc, MC_REGB) | MC_REGB_SET));
220
221 mc146818_write(sc, MC_SEC, dt.dt_sec);
222 mc146818_write(sc, MC_MIN, dt.dt_min);
223 mc146818_write(sc, MC_HOUR, dt.dt_hour);
224 mc146818_write(sc, MC_DOW, dt.dt_wday + 1);
225 mc146818_write(sc, MC_DOM, dt.dt_day);
226 mc146818_write(sc, MC_MONTH, dt.dt_mon);
227 mc146818_write(sc, MC_YEAR, year);
228
229 /* reenable updates */
230 mc146818_write(sc, MC_REGB,
231 (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET));
232 return (0);
233 }
234