rtclock.c revision 1.16 1 /* $NetBSD: rtclock.c,v 1.16 2004/12/13 02:14:14 chs Exp $ */
2
3 /*
4 * Copyright 1993, 1994 Masaru Oki
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Masaru Oki.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * X680x0 internal real time clock interface
35 * alarm is not supported.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: rtclock.c,v 1.16 2004/12/13 02:14:14 chs Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/buf.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/reboot.h>
47 #include <sys/file.h>
48 #include <sys/kernel.h>
49 #include <sys/device.h>
50
51 #include <machine/bus.h>
52
53 #include <dev/clock_subr.h>
54
55 #include <arch/x68k/dev/rtclock_var.h>
56 #include <arch/x68k/dev/intiovar.h>
57
58 static time_t rtgettod __P((void));
59 static int rtsettod __P((long));
60
61 static int rtc_match __P((struct device *, struct cfdata *, void *));
62 static void rtc_attach __P((struct device *, struct device *, void *));
63
64 int rtclockinit __P((void));
65
66 CFATTACH_DECL(rtc, sizeof(struct rtc_softc),
67 rtc_match, rtc_attach, NULL, NULL);
68
69 static int rtc_attached;
70
71 static int
72 rtc_match(parent, cf, aux)
73 struct device *parent;
74 struct cfdata *cf;
75 void *aux;
76 {
77 struct intio_attach_args *ia = aux;
78
79 if (strcmp (ia->ia_name, "rtc") != 0)
80 return (0);
81 if (rtc_attached)
82 return (0);
83
84 /* fixed address */
85 if (ia->ia_addr != RTC_ADDR)
86 return (0);
87 if (ia->ia_intr != -1)
88 return (0);
89
90 return (1);
91 }
92
93
94 static struct rtc_softc *rtc; /* XXX: softc cache */
95
96 static void
97 rtc_attach(parent, self, aux)
98 struct device *parent, *self;
99 void *aux;
100 {
101 struct rtc_softc *sc = (struct rtc_softc *)self;
102 struct intio_attach_args *ia = aux;
103 int r;
104
105 rtc_attached = 1;
106
107 ia->ia_size = 0x20;
108 r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
109 #ifdef DIAGNOSTIC
110 if (r)
111 panic ("IO map for RTC corruption??");
112 #endif
113
114
115 sc->sc_bst = ia->ia_bst;
116 bus_space_map(sc->sc_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
117 rtc = sc;
118
119 rtclockinit();
120 printf (": RP5C15\n");
121 }
122
123
124
125 /*
126 * x68k/clock.c calls thru the get/set tod vector, if it is set, to read
127 * the realtime clock.
128 */
129
130 int
131 rtclockinit()
132 {
133 if (rtgettod()) {
134 gettod = rtgettod;
135 settod = rtsettod;
136 } else {
137 return 0;
138 }
139 return 1;
140 }
141
142 static time_t
143 rtgettod()
144 {
145 struct clock_ymdhms dt;
146
147 /* hold clock */
148 RTC_WRITE(RTC_MODE, RTC_HOLD_CLOCK);
149
150 /* read it */
151 dt.dt_sec = RTC_REG(RTC_SEC10) * 10 + RTC_REG(RTC_SEC);
152 dt.dt_min = RTC_REG(RTC_MIN10) * 10 + RTC_REG(RTC_MIN);
153 dt.dt_hour = RTC_REG(RTC_HOUR10) * 10 + RTC_REG(RTC_HOUR);
154 dt.dt_day = RTC_REG(RTC_DAY10) * 10 + RTC_REG(RTC_DAY);
155 dt.dt_mon = RTC_REG(RTC_MON10) * 10 + RTC_REG(RTC_MON);
156 dt.dt_year = RTC_REG(RTC_YEAR10) * 10 + RTC_REG(RTC_YEAR)
157 +RTC_BASE_YEAR;
158
159 /* let it run again.. */
160 RTC_WRITE(RTC_MODE, RTC_FREE_CLOCK);
161
162 #ifdef DIAGNOSTIC
163 range_test0(dt.dt_hour, 23);
164 range_test(dt.dt_day, 1, 31);
165 range_test(dt.dt_mon, 1, 12);
166 range_test(dt.dt_year, RTC_BASE_YEAR, RTC_BASE_YEAR+100-1);
167 #endif
168
169 return clock_ymdhms_to_secs (&dt) + rtc_offset * 60;
170 }
171
172 static int
173 rtsettod (tim)
174 time_t tim;
175 {
176 struct clock_ymdhms dt;
177 u_char sec1, sec2;
178 u_char min1, min2;
179 u_char hour1, hour2;
180 u_char day1, day2;
181 u_char mon1, mon2;
182 u_char year1, year2;
183
184 clock_secs_to_ymdhms (tim - rtc_offset * 60, &dt);
185
186 /* prepare values to be written to clock */
187 sec1 = dt.dt_sec / 10;
188 sec2 = dt.dt_sec % 10;
189 min1 = dt.dt_min / 10;
190 min2 = dt.dt_min % 10;
191 hour1 = dt.dt_hour / 10;
192 hour2 = dt.dt_hour % 10;
193
194 day1 = dt.dt_day / 10;
195 day2 = dt.dt_day % 10;
196 mon1 = dt.dt_mon / 10;
197 mon2 = dt.dt_mon % 10;
198 year1 = (dt.dt_year - RTC_BASE_YEAR) / 10;
199 year2 = dt.dt_year % 10;
200
201 RTC_WRITE(RTC_MODE, RTC_HOLD_CLOCK);
202 RTC_WRITE(RTC_SEC10, sec1);
203 RTC_WRITE(RTC_SEC, sec2);
204 RTC_WRITE(RTC_MIN10, min1);
205 RTC_WRITE(RTC_MIN, min2);
206 RTC_WRITE(RTC_HOUR10, hour1);
207 RTC_WRITE(RTC_HOUR, hour2);
208 RTC_WRITE(RTC_DAY10, day1);
209 RTC_WRITE(RTC_DAY, day2);
210 RTC_WRITE(RTC_MON10, mon1);
211 RTC_WRITE(RTC_MON, mon2);
212 RTC_WRITE(RTC_YEAR10, year1);
213 RTC_WRITE(RTC_YEAR, year2);
214 RTC_WRITE(RTC_MODE, RTC_FREE_CLOCK);
215
216 return 1;
217 }
218