clock.c revision 1.3 1 1.3 agc /* $NetBSD: clock.c,v 1.3 2003/08/07 16:27:40 agc Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1982, 1990, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.3 agc *
7 1.3 agc * This code is derived from software contributed to Berkeley by
8 1.3 agc * the Systems Programming Group of the University of Utah Computer
9 1.3 agc * Science Department.
10 1.3 agc *
11 1.3 agc * Redistribution and use in source and binary forms, with or without
12 1.3 agc * modification, are permitted provided that the following conditions
13 1.3 agc * are met:
14 1.3 agc * 1. Redistributions of source code must retain the above copyright
15 1.3 agc * notice, this list of conditions and the following disclaimer.
16 1.3 agc * 2. Redistributions in binary form must reproduce the above copyright
17 1.3 agc * notice, this list of conditions and the following disclaimer in the
18 1.3 agc * documentation and/or other materials provided with the distribution.
19 1.3 agc * 3. Neither the name of the University nor the names of its contributors
20 1.3 agc * may be used to endorse or promote products derived from this software
21 1.3 agc * without specific prior written permission.
22 1.3 agc *
23 1.3 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.3 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.3 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.3 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.3 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.3 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.3 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.3 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.3 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.3 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.3 agc * SUCH DAMAGE.
34 1.3 agc *
35 1.3 agc * from: Utah $Hdr: clock.c 1.18 91/01/21$
36 1.3 agc *
37 1.3 agc * @(#)clock.c 8.2 (Berkeley) 1/12/94
38 1.3 agc */
39 1.3 agc /*
40 1.3 agc * Copyright (c) 1988 University of Utah.
41 1.1 thorpej *
42 1.1 thorpej * This code is derived from software contributed to Berkeley by
43 1.1 thorpej * the Systems Programming Group of the University of Utah Computer
44 1.1 thorpej * Science Department.
45 1.1 thorpej *
46 1.1 thorpej * Redistribution and use in source and binary forms, with or without
47 1.1 thorpej * modification, are permitted provided that the following conditions
48 1.1 thorpej * are met:
49 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
50 1.1 thorpej * notice, this list of conditions and the following disclaimer.
51 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
53 1.1 thorpej * documentation and/or other materials provided with the distribution.
54 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
55 1.1 thorpej * must display the following acknowledgement:
56 1.1 thorpej * This product includes software developed by the University of
57 1.1 thorpej * California, Berkeley and its contributors.
58 1.1 thorpej * 4. Neither the name of the University nor the names of its contributors
59 1.1 thorpej * may be used to endorse or promote products derived from this software
60 1.1 thorpej * without specific prior written permission.
61 1.1 thorpej *
62 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.1 thorpej * SUCH DAMAGE.
73 1.1 thorpej *
74 1.1 thorpej * from: Utah $Hdr: clock.c 1.18 91/01/21$
75 1.1 thorpej *
76 1.1 thorpej * @(#)clock.c 8.2 (Berkeley) 1/12/94
77 1.1 thorpej */
78 1.1 thorpej
79 1.1 thorpej #include <sys/param.h>
80 1.1 thorpej
81 1.1 thorpej #include <hp300/dev/hilreg.h>
82 1.2 gmcgarry
83 1.2 gmcgarry #define FEBRUARY 2
84 1.2 gmcgarry #define STARTOFTIME 1970
85 1.2 gmcgarry #define SECDAY 86400L
86 1.2 gmcgarry #define SECYR (SECDAY * 365)
87 1.2 gmcgarry
88 1.2 gmcgarry #define BBC_SET_REG 0xe0
89 1.2 gmcgarry #define BBC_WRITE_REG 0xc2
90 1.2 gmcgarry #define BBC_READ_REG 0xc3
91 1.2 gmcgarry #define NUM_BBC_REGS 12
92 1.2 gmcgarry
93 1.2 gmcgarry #define leapyear(year) ((year) % 4 == 0)
94 1.2 gmcgarry #define range_test(n, l, h) if ((n) < (l) || (n) > (h)) return(0)
95 1.2 gmcgarry #define days_in_year(a) (leapyear(a) ? 366 : 365)
96 1.2 gmcgarry #define days_in_month(a) (month_days[(a) - 1])
97 1.2 gmcgarry #define bbc_to_decimal(a,b) (bbc_registers[a] * 10 + bbc_registers[b])
98 1.1 thorpej
99 1.1 thorpej #include <hp300/stand/common/samachdep.h>
100 1.1 thorpej
101 1.1 thorpej static int month_days[12] = {
102 1.1 thorpej 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
103 1.1 thorpej };
104 1.1 thorpej
105 1.1 thorpej u_char bbc_registers[13];
106 1.1 thorpej u_char read_bbc_reg();
107 1.1 thorpej struct hil_dev *bbcaddr = BBCADDR;
108 1.1 thorpej
109 1.1 thorpej getsecs()
110 1.1 thorpej {
111 1.1 thorpej static int bbcinited = 0;
112 1.1 thorpej u_long timbuf = 0;
113 1.1 thorpej
114 1.1 thorpej if (!bbc_to_gmt(&timbuf) && !bbcinited)
115 1.1 thorpej printf("WARNING: bad date in battery clock\n");
116 1.1 thorpej bbcinited = 1;
117 1.1 thorpej
118 1.1 thorpej /* Battery clock does not store usec's, so forget about it. */
119 1.1 thorpej return(timbuf);
120 1.1 thorpej }
121 1.1 thorpej
122 1.1 thorpej
123 1.1 thorpej bbc_to_gmt(timbuf)
124 1.1 thorpej u_long *timbuf;
125 1.1 thorpej {
126 1.1 thorpej register int i;
127 1.1 thorpej register u_long tmp;
128 1.1 thorpej int year, month, day, hour, min, sec;
129 1.1 thorpej
130 1.1 thorpej read_bbc();
131 1.1 thorpej
132 1.1 thorpej sec = bbc_to_decimal(1, 0);
133 1.1 thorpej min = bbc_to_decimal(3, 2);
134 1.1 thorpej
135 1.1 thorpej /*
136 1.1 thorpej * Hours are different for some reason. Makes no sense really.
137 1.1 thorpej */
138 1.1 thorpej hour = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4];
139 1.1 thorpej day = bbc_to_decimal(8, 7);
140 1.1 thorpej month = bbc_to_decimal(10, 9);
141 1.1 thorpej year = bbc_to_decimal(12, 11) + 1900;
142 1.1 thorpej
143 1.1 thorpej range_test(hour, 0, 23);
144 1.1 thorpej range_test(day, 1, 31);
145 1.1 thorpej range_test(month, 1, 12);
146 1.1 thorpej range_test(year, STARTOFTIME, 2000);
147 1.1 thorpej
148 1.1 thorpej tmp = 0;
149 1.1 thorpej
150 1.1 thorpej for (i = STARTOFTIME; i < year; i++)
151 1.1 thorpej tmp += days_in_year(i);
152 1.1 thorpej if (leapyear(year) && month > FEBRUARY)
153 1.1 thorpej tmp++;
154 1.1 thorpej
155 1.1 thorpej for (i = 1; i < month; i++)
156 1.1 thorpej tmp += days_in_month(i);
157 1.1 thorpej
158 1.1 thorpej tmp += (day - 1);
159 1.1 thorpej tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
160 1.1 thorpej
161 1.1 thorpej *timbuf = tmp;
162 1.1 thorpej return(1);
163 1.1 thorpej }
164 1.1 thorpej
165 1.1 thorpej read_bbc()
166 1.1 thorpej {
167 1.1 thorpej register int i, read_okay;
168 1.1 thorpej
169 1.1 thorpej read_okay = 0;
170 1.1 thorpej while (!read_okay) {
171 1.1 thorpej read_okay = 1;
172 1.1 thorpej for (i = 0; i <= NUM_BBC_REGS; i++)
173 1.1 thorpej bbc_registers[i] = read_bbc_reg(i);
174 1.1 thorpej for (i = 0; i <= NUM_BBC_REGS; i++)
175 1.1 thorpej if (bbc_registers[i] != read_bbc_reg(i))
176 1.1 thorpej read_okay = 0;
177 1.1 thorpej }
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej u_char
181 1.1 thorpej read_bbc_reg(reg)
182 1.1 thorpej int reg;
183 1.1 thorpej {
184 1.1 thorpej u_char data = reg;
185 1.1 thorpej
186 1.1 thorpej if (bbcaddr) {
187 1.1 thorpej #if 0
188 1.1 thorpej send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL);
189 1.1 thorpej send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data);
190 1.1 thorpej #else
191 1.1 thorpej HILWAIT(bbcaddr);
192 1.1 thorpej bbcaddr->hil_cmd = BBC_SET_REG;
193 1.1 thorpej HILWAIT(bbcaddr);
194 1.1 thorpej bbcaddr->hil_data = data;
195 1.1 thorpej HILWAIT(bbcaddr);
196 1.1 thorpej bbcaddr->hil_cmd = BBC_READ_REG;
197 1.1 thorpej HILDATAWAIT(bbcaddr);
198 1.1 thorpej data = bbcaddr->hil_data;
199 1.1 thorpej #endif
200 1.1 thorpej }
201 1.1 thorpej return(data);
202 1.1 thorpej }
203