Home | History | Annotate | Line # | Download | only in common
clock.c revision 1.6
      1 /*	$NetBSD: clock.c,v 1.6 2005/02/20 13:59:27 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      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.
     10  *
     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.
     22  *
     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.
     34  *
     35  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     36  *
     37  *	@(#)clock.c	8.2 (Berkeley) 1/12/94
     38  */
     39 /*
     40  * Copyright (c) 1988 University of Utah.
     41  *
     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.
     45  *
     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.
     61  *
     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.
     73  *
     74  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     75  *
     76  *	@(#)clock.c	8.2 (Berkeley) 1/12/94
     77  */
     78 
     79 #include <sys/param.h>
     80 
     81 #include <net/if_ether.h>
     82 #include <netinet/in.h>
     83 #include <netinet/in_systm.h>
     84 
     85 #include <hp300/dev/hilreg.h>
     86 
     87 #include <lib/libsa/stand.h>
     88 #include <lib/libsa/net.h>
     89 #include <hp300/stand/common/samachdep.h>
     90 
     91 #define FEBRUARY        2
     92 #define STARTOFTIME     1970
     93 #define SECDAY          (60L * 60L * 24L)
     94 #define SECYR           (SECDAY * 365)
     95 
     96 #define BBC_SET_REG     0xe0
     97 #define BBC_WRITE_REG   0xc2
     98 #define BBC_READ_REG    0xc3
     99 #define NUM_BBC_REGS    12
    100 
    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])
    106 
    107 static const int month_days[12] = {
    108 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    109 };
    110 
    111 u_char bbc_registers[13];
    112 struct hil_dev *bbcaddr = BBCADDR;
    113 
    114 static int bbc_to_gmt(u_long *);
    115 
    116 time_t
    117 getsecs(void)
    118 {
    119 	static int bbcinited = 0;
    120 	time_t timbuf = 0;
    121 
    122 	if (!bbc_to_gmt(&timbuf) && !bbcinited)
    123 		printf("WARNING: bad date in battery clock\n");
    124 	bbcinited = 1;
    125 
    126 	/* Battery clock does not store usec's, so forget about it. */
    127 	return timbuf;
    128 }
    129 
    130 
    131 static int
    132 bbc_to_gmt(u_long *timbuf)
    133 {
    134 	int i;
    135 	u_long tmp;
    136 	int year, month, day, hour, min, sec;
    137 
    138 	read_bbc();
    139 
    140 	sec = bbc_to_decimal(1, 0);
    141 	min = bbc_to_decimal(3, 2);
    142 
    143 	/*
    144 	 * Hours are different for some reason. Makes no sense really.
    145 	 */
    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;
    152 
    153 	range_test(hour, 0, 23);
    154 	range_test(day, 1, 31);
    155 	range_test(month, 1, 12);
    156 
    157 	tmp = 0;
    158 
    159 	for (i = STARTOFTIME; i < year; i++)
    160 		tmp += days_in_year(i);
    161 	if (leapyear(year) && month > FEBRUARY)
    162 		tmp++;
    163 
    164 	for (i = 1; i < month; i++)
    165 	  	tmp += days_in_month(i);
    166 
    167 	tmp += (day - 1);
    168 	tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
    169 
    170 	*timbuf = tmp;
    171 	return 1;
    172 }
    173 
    174 void
    175 read_bbc(void)
    176 {
    177   	int i, read_okay;
    178 
    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;
    187 	}
    188 }
    189 
    190 u_char
    191 read_bbc_reg(int reg)
    192 {
    193 	u_char data = reg;
    194 
    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
    209 	}
    210 	return data;
    211 }
    212