clock.c revision 1.6 1 1.6 tsutsui /* $NetBSD: clock.c,v 1.6 2005/02/20 13:59:27 tsutsui 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.4 tsutsui #include <net/if_ether.h>
82 1.4 tsutsui #include <netinet/in.h>
83 1.4 tsutsui #include <netinet/in_systm.h>
84 1.4 tsutsui
85 1.1 thorpej #include <hp300/dev/hilreg.h>
86 1.2 gmcgarry
87 1.4 tsutsui #include <lib/libsa/stand.h>
88 1.4 tsutsui #include <lib/libsa/net.h>
89 1.4 tsutsui #include <hp300/stand/common/samachdep.h>
90 1.4 tsutsui
91 1.2 gmcgarry #define FEBRUARY 2
92 1.2 gmcgarry #define STARTOFTIME 1970
93 1.4 tsutsui #define SECDAY (60L * 60L * 24L)
94 1.2 gmcgarry #define SECYR (SECDAY * 365)
95 1.2 gmcgarry
96 1.2 gmcgarry #define BBC_SET_REG 0xe0
97 1.2 gmcgarry #define BBC_WRITE_REG 0xc2
98 1.2 gmcgarry #define BBC_READ_REG 0xc3
99 1.2 gmcgarry #define NUM_BBC_REGS 12
100 1.2 gmcgarry
101 1.2 gmcgarry #define leapyear(year) ((year) % 4 == 0)
102 1.4 tsutsui #define range_test(n, l, h) if ((n) < (l) || (n) > (h)) return 0
103 1.2 gmcgarry #define days_in_year(a) (leapyear(a) ? 366 : 365)
104 1.2 gmcgarry #define days_in_month(a) (month_days[(a) - 1])
105 1.2 gmcgarry #define bbc_to_decimal(a,b) (bbc_registers[a] * 10 + bbc_registers[b])
106 1.1 thorpej
107 1.4 tsutsui static const int month_days[12] = {
108 1.1 thorpej 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
109 1.1 thorpej };
110 1.1 thorpej
111 1.1 thorpej u_char bbc_registers[13];
112 1.1 thorpej struct hil_dev *bbcaddr = BBCADDR;
113 1.1 thorpej
114 1.4 tsutsui static int bbc_to_gmt(u_long *);
115 1.4 tsutsui
116 1.4 tsutsui time_t
117 1.4 tsutsui getsecs(void)
118 1.1 thorpej {
119 1.1 thorpej static int bbcinited = 0;
120 1.4 tsutsui time_t timbuf = 0;
121 1.1 thorpej
122 1.1 thorpej if (!bbc_to_gmt(&timbuf) && !bbcinited)
123 1.1 thorpej printf("WARNING: bad date in battery clock\n");
124 1.1 thorpej bbcinited = 1;
125 1.1 thorpej
126 1.1 thorpej /* Battery clock does not store usec's, so forget about it. */
127 1.4 tsutsui return timbuf;
128 1.1 thorpej }
129 1.1 thorpej
130 1.1 thorpej
131 1.4 tsutsui static int
132 1.6 tsutsui bbc_to_gmt(u_long *timbuf)
133 1.1 thorpej {
134 1.4 tsutsui int i;
135 1.4 tsutsui u_long tmp;
136 1.1 thorpej int year, month, day, hour, min, sec;
137 1.1 thorpej
138 1.1 thorpej read_bbc();
139 1.1 thorpej
140 1.1 thorpej sec = bbc_to_decimal(1, 0);
141 1.1 thorpej min = bbc_to_decimal(3, 2);
142 1.1 thorpej
143 1.1 thorpej /*
144 1.1 thorpej * Hours are different for some reason. Makes no sense really.
145 1.1 thorpej */
146 1.1 thorpej hour = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4];
147 1.1 thorpej day = bbc_to_decimal(8, 7);
148 1.1 thorpej month = bbc_to_decimal(10, 9);
149 1.1 thorpej year = bbc_to_decimal(12, 11) + 1900;
150 1.5 tsutsui if (year < STARTOFTIME)
151 1.5 tsutsui year += 100;
152 1.1 thorpej
153 1.1 thorpej range_test(hour, 0, 23);
154 1.1 thorpej range_test(day, 1, 31);
155 1.1 thorpej range_test(month, 1, 12);
156 1.1 thorpej
157 1.1 thorpej tmp = 0;
158 1.1 thorpej
159 1.1 thorpej for (i = STARTOFTIME; i < year; i++)
160 1.1 thorpej tmp += days_in_year(i);
161 1.1 thorpej if (leapyear(year) && month > FEBRUARY)
162 1.1 thorpej tmp++;
163 1.1 thorpej
164 1.1 thorpej for (i = 1; i < month; i++)
165 1.1 thorpej tmp += days_in_month(i);
166 1.1 thorpej
167 1.1 thorpej tmp += (day - 1);
168 1.1 thorpej tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
169 1.1 thorpej
170 1.1 thorpej *timbuf = tmp;
171 1.4 tsutsui return 1;
172 1.1 thorpej }
173 1.1 thorpej
174 1.4 tsutsui void
175 1.6 tsutsui read_bbc(void)
176 1.1 thorpej {
177 1.4 tsutsui int i, read_okay;
178 1.1 thorpej
179 1.1 thorpej read_okay = 0;
180 1.1 thorpej while (!read_okay) {
181 1.1 thorpej read_okay = 1;
182 1.1 thorpej for (i = 0; i <= NUM_BBC_REGS; i++)
183 1.1 thorpej bbc_registers[i] = read_bbc_reg(i);
184 1.1 thorpej for (i = 0; i <= NUM_BBC_REGS; i++)
185 1.1 thorpej if (bbc_registers[i] != read_bbc_reg(i))
186 1.1 thorpej read_okay = 0;
187 1.1 thorpej }
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej u_char
191 1.6 tsutsui read_bbc_reg(int reg)
192 1.1 thorpej {
193 1.1 thorpej u_char data = reg;
194 1.1 thorpej
195 1.1 thorpej if (bbcaddr) {
196 1.1 thorpej #if 0
197 1.1 thorpej send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL);
198 1.1 thorpej send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data);
199 1.1 thorpej #else
200 1.1 thorpej HILWAIT(bbcaddr);
201 1.1 thorpej bbcaddr->hil_cmd = BBC_SET_REG;
202 1.1 thorpej HILWAIT(bbcaddr);
203 1.1 thorpej bbcaddr->hil_data = data;
204 1.1 thorpej HILWAIT(bbcaddr);
205 1.1 thorpej bbcaddr->hil_cmd = BBC_READ_REG;
206 1.1 thorpej HILDATAWAIT(bbcaddr);
207 1.1 thorpej data = bbcaddr->hil_data;
208 1.1 thorpej #endif
209 1.1 thorpej }
210 1.4 tsutsui return data;
211 1.1 thorpej }
212