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