rtclock.c revision 1.1 1 1.1 oki /* $NetBSD: rtclock.c,v 1.1 1996/05/05 12:17:08 oki Exp $ */
2 1.1 oki
3 1.1 oki /*
4 1.1 oki * Copyright 1993, 1994 Masaru Oki
5 1.1 oki * All rights reserved.
6 1.1 oki *
7 1.1 oki * Redistribution and use in source and binary forms, with or without
8 1.1 oki * modification, are permitted provided that the following conditions
9 1.1 oki * are met:
10 1.1 oki * 1. Redistributions of source code must retain the above copyright
11 1.1 oki * notice, this list of conditions and the following disclaimer.
12 1.1 oki * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 oki * notice, this list of conditions and the following disclaimer in the
14 1.1 oki * documentation and/or other materials provided with the distribution.
15 1.1 oki * 3. All advertising materials mentioning features or use of this software
16 1.1 oki * must display the following acknowledgement:
17 1.1 oki * This product includes software developed by Masaru Oki.
18 1.1 oki * 4. The name of the author may not be used to endorse or promote products
19 1.1 oki * derived from this software without specific prior written permission
20 1.1 oki *
21 1.1 oki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 oki * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 oki * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 oki * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 oki * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 oki * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 oki * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 oki * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 oki * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 oki * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 oki */
32 1.1 oki
33 1.1 oki /*
34 1.1 oki * X680x0 internal real time clock interface
35 1.1 oki * alarm is not supported.
36 1.1 oki */
37 1.1 oki
38 1.1 oki #include <sys/param.h>
39 1.1 oki #include <sys/systm.h>
40 1.1 oki #include <sys/buf.h>
41 1.1 oki #include <sys/malloc.h>
42 1.1 oki #include <sys/proc.h>
43 1.1 oki #include <sys/reboot.h>
44 1.1 oki #include <sys/file.h>
45 1.1 oki
46 1.1 oki #include <x68k/dev/rtclock_var.h>
47 1.1 oki #include <x68k/x68k/iodevice.h>
48 1.1 oki
49 1.1 oki static u_long rtgettod __P((void));
50 1.1 oki static int rtsettod __P((long));
51 1.1 oki
52 1.1 oki /*
53 1.1 oki * x68k/clock.c calls thru this vector, if it is set, to read
54 1.1 oki * the realtime clock.
55 1.1 oki */
56 1.1 oki u_long (*gettod) __P((void));
57 1.1 oki int (*settod)();
58 1.1 oki
59 1.1 oki static volatile union rtc *rtc_addr = 0;
60 1.1 oki
61 1.1 oki int
62 1.1 oki rtclockinit()
63 1.1 oki {
64 1.1 oki rtc_addr = &IODEVbase->io_rtc;
65 1.1 oki
66 1.1 oki if (rtgettod()) {
67 1.1 oki gettod = rtgettod;
68 1.1 oki settod = rtsettod;
69 1.1 oki } else {
70 1.1 oki return 0;
71 1.1 oki }
72 1.1 oki return 1;
73 1.1 oki }
74 1.1 oki
75 1.1 oki static int month_days[12] = {
76 1.1 oki 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
77 1.1 oki };
78 1.1 oki
79 1.1 oki static u_long
80 1.1 oki rtgettod()
81 1.1 oki {
82 1.1 oki register int i;
83 1.1 oki register u_long tmp;
84 1.1 oki int year, month, day, hour, min, sec;
85 1.1 oki
86 1.1 oki /* hold clock */
87 1.1 oki RTC_WRITE(rtc_addr, mode, RTC_HOLD_CLOCK);
88 1.1 oki
89 1.1 oki /* read it */
90 1.1 oki sec = RTC_REG(sec10) * 10 + RTC_REG(sec);
91 1.1 oki min = RTC_REG(min10) * 10 + RTC_REG(min);
92 1.1 oki hour = RTC_REG(hour10) * 10 + RTC_REG(hour);
93 1.1 oki day = RTC_REG(day10) * 10 + RTC_REG(day);
94 1.1 oki month = RTC_REG(mon10) * 10 + RTC_REG(mon);
95 1.1 oki year = RTC_REG(year10) * 10 + RTC_REG(year) + 1980;
96 1.1 oki
97 1.1 oki /* let it run again.. */
98 1.1 oki RTC_WRITE(rtc_addr, mode, RTC_FREE_CLOCK);
99 1.1 oki
100 1.1 oki range_test(hour, 0, 23);
101 1.1 oki range_test(day, 1, 31);
102 1.1 oki range_test(month, 1, 12);
103 1.1 oki range_test(year, STARTOFTIME, 2000);
104 1.1 oki
105 1.1 oki tmp = 0;
106 1.1 oki
107 1.1 oki for (i = STARTOFTIME; i < year; i++)
108 1.1 oki tmp += days_in_year(i);
109 1.1 oki if (leapyear(year) && month > FEBRUARY)
110 1.1 oki tmp++;
111 1.1 oki
112 1.1 oki for (i = 1; i < month; i++)
113 1.1 oki tmp += days_in_month(i);
114 1.1 oki
115 1.1 oki tmp += (day - 1);
116 1.1 oki
117 1.1 oki tmp = ((tmp * 24 + hour) * 60 + min + TIMEZONE) * 60 + sec;
118 1.1 oki
119 1.1 oki return tmp;
120 1.1 oki }
121 1.1 oki
122 1.1 oki static int
123 1.1 oki rtsettod (tim)
124 1.1 oki long tim;
125 1.1 oki {
126 1.1 oki /*
127 1.1 oki * I don't know if setting the clock is analogous
128 1.1 oki * to reading it, I don't have demo-code for setting.
129 1.1 oki * just give it a try..
130 1.1 oki */
131 1.1 oki register int i;
132 1.1 oki register long hms, day;
133 1.1 oki u_char sec1, sec2;
134 1.1 oki u_char min1, min2;
135 1.1 oki u_char hour1, hour2;
136 1.1 oki u_char day1, day2;
137 1.1 oki u_char mon1, mon2;
138 1.1 oki u_char year1, year2;
139 1.1 oki
140 1.1 oki /*
141 1.1 oki * there seem to be problems with the bitfield addressing
142 1.1 oki * currently used..
143 1.1 oki */
144 1.1 oki if (!rtc_addr)
145 1.1 oki return 0;
146 1.1 oki
147 1.1 oki tim -= (TIMEZONE * 60);
148 1.1 oki
149 1.1 oki /* prepare values to be written to clock */
150 1.1 oki day = tim / SECDAY;
151 1.1 oki hms = tim % SECDAY;
152 1.1 oki
153 1.1 oki hour2 = hms / 3600;
154 1.1 oki hour1 = hour2 / 10;
155 1.1 oki hour2 %= 10;
156 1.1 oki
157 1.1 oki min2 = (hms % 3600) / 60;
158 1.1 oki min1 = min2 / 10;
159 1.1 oki min2 %= 10;
160 1.1 oki
161 1.1 oki sec2 = (hms % 3600) % 60;
162 1.1 oki sec1 = sec2 / 10;
163 1.1 oki sec2 %= 10;
164 1.1 oki
165 1.1 oki /* Number of years in days */
166 1.1 oki for (i = STARTOFTIME - 1980; day >= days_in_year(i); i++)
167 1.1 oki day -= days_in_year(i);
168 1.1 oki year1 = i / 10;
169 1.1 oki year2 = i % 10;
170 1.1 oki
171 1.1 oki /* Number of months in days left */
172 1.1 oki if (leapyear(i))
173 1.1 oki days_in_month(FEBRUARY) = 29;
174 1.1 oki for (i = 1; day >= days_in_month(i); i++)
175 1.1 oki day -= days_in_month(i);
176 1.1 oki days_in_month(FEBRUARY) = 28;
177 1.1 oki
178 1.1 oki mon1 = i / 10;
179 1.1 oki mon2 = i % 10;
180 1.1 oki
181 1.1 oki /* Days are what is left over (+1) from all that. */
182 1.1 oki day ++;
183 1.1 oki day1 = day / 10;
184 1.1 oki day2 = day % 10;
185 1.1 oki
186 1.1 oki RTC_WRITE(rtc_addr, mode, RTC_HOLD_CLOCK);
187 1.1 oki RTC_WRITE(rtc_addr, sec10, sec1);
188 1.1 oki RTC_WRITE(rtc_addr, sec, sec2);
189 1.1 oki RTC_WRITE(rtc_addr, min10, min1);
190 1.1 oki RTC_WRITE(rtc_addr, min, min2);
191 1.1 oki RTC_WRITE(rtc_addr, hour10, hour1);
192 1.1 oki RTC_WRITE(rtc_addr, hour, hour2);
193 1.1 oki RTC_WRITE(rtc_addr, day10, day1);
194 1.1 oki RTC_WRITE(rtc_addr, day, day2);
195 1.1 oki RTC_WRITE(rtc_addr, mon10, mon1);
196 1.1 oki RTC_WRITE(rtc_addr, mon, mon2);
197 1.1 oki RTC_WRITE(rtc_addr, year10, year1);
198 1.1 oki RTC_WRITE(rtc_addr, year, year2);
199 1.1 oki RTC_WRITE(rtc_addr, mode, RTC_FREE_CLOCK);
200 1.1 oki
201 1.1 oki return 1;
202 1.1 oki }
203