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