1 /* $NetBSD: clock.c,v 1.8 2006/06/25 17:35:12 tsutsui Exp $ */
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
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
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>
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
*);
119 static int bbcinited
= 0;
122 if (!bbc_to_gmt(&timbuf
) && !bbcinited
)
123 printf("WARNING: bad date in battery clock\n");
126 /* Battery clock does not store usec's, so forget about it. */
132 bbc_to_gmt(satime_t
*timbuf
)
136 int year
, month
, day
, hour
, min
, sec
;
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
)
153 range_test(hour
, 0, 23);
154 range_test(day
, 1, 31);
155 range_test(month
, 1, 12);
159 for (i
= STARTOFTIME
; i
< year
; i
++)
160 tmp
+= days_in_year(i
);
161 if (leapyear(year
) && month
> FEBRUARY
)
164 for (i
= 1; i
< month
; i
++)
165 tmp
+= days_in_month(i
);
168 tmp
= ((tmp
* 24 + hour
) * 60 + min
) * 60 + sec
;
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
))
191 read_bbc_reg(int reg
)
197 send_hil_cmd(bbcaddr
, BBC_SET_REG
, &data
, 1, NULL
);
198 send_hil_cmd(bbcaddr
, BBC_READ_REG
, NULL
, 0, &data
);
201 bbcaddr
->hil_cmd
= BBC_SET_REG
;
203 bbcaddr
->hil_data
= data
;
205 bbcaddr
->hil_cmd
= BBC_READ_REG
;
206 HILDATAWAIT(bbcaddr
);
207 data
= bbcaddr
->hil_data
;