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