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