Home | History | Annotate | Line # | Download | only in common
clock.c revision 1.1
      1 /*	$NetBSD: clock.c,v 1.1 1997/02/04 03:52:15 thorpej 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 #include <hp300/hp300/clockreg.h>
     49 
     50 #include <hp300/stand/common/samachdep.h>
     51 
     52 static int month_days[12] = {
     53 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
     54 };
     55 
     56 u_char bbc_registers[13];
     57 u_char read_bbc_reg();
     58 struct hil_dev *bbcaddr = BBCADDR;
     59 
     60 getsecs()
     61 {
     62 	static int bbcinited = 0;
     63 	u_long timbuf = 0;
     64 
     65 	if (!bbc_to_gmt(&timbuf) && !bbcinited)
     66 		printf("WARNING: bad date in battery clock\n");
     67 	bbcinited = 1;
     68 
     69 	/* Battery clock does not store usec's, so forget about it. */
     70 	return(timbuf);
     71 }
     72 
     73 
     74 bbc_to_gmt(timbuf)
     75 	u_long *timbuf;
     76 {
     77 	register int i;
     78 	register u_long tmp;
     79 	int year, month, day, hour, min, sec;
     80 
     81 	read_bbc();
     82 
     83 	sec = bbc_to_decimal(1, 0);
     84 	min = bbc_to_decimal(3, 2);
     85 
     86 	/*
     87 	 * Hours are different for some reason. Makes no sense really.
     88 	 */
     89 	hour  = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4];
     90 	day   = bbc_to_decimal(8, 7);
     91 	month = bbc_to_decimal(10, 9);
     92 	year  = bbc_to_decimal(12, 11) + 1900;
     93 
     94 	range_test(hour, 0, 23);
     95 	range_test(day, 1, 31);
     96 	range_test(month, 1, 12);
     97 	range_test(year, STARTOFTIME, 2000);
     98 
     99 	tmp = 0;
    100 
    101 	for (i = STARTOFTIME; i < year; i++)
    102 		tmp += days_in_year(i);
    103 	if (leapyear(year) && month > FEBRUARY)
    104 		tmp++;
    105 
    106 	for (i = 1; i < month; i++)
    107 	  	tmp += days_in_month(i);
    108 
    109 	tmp += (day - 1);
    110 	tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
    111 
    112 	*timbuf = tmp;
    113 	return(1);
    114 }
    115 
    116 read_bbc()
    117 {
    118   	register int i, read_okay;
    119 
    120 	read_okay = 0;
    121 	while (!read_okay) {
    122 		read_okay = 1;
    123 		for (i = 0; i <= NUM_BBC_REGS; i++)
    124 			bbc_registers[i] = read_bbc_reg(i);
    125 		for (i = 0; i <= NUM_BBC_REGS; i++)
    126 			if (bbc_registers[i] != read_bbc_reg(i))
    127 				read_okay = 0;
    128 	}
    129 }
    130 
    131 u_char
    132 read_bbc_reg(reg)
    133 	int reg;
    134 {
    135 	u_char data = reg;
    136 
    137 	if (bbcaddr) {
    138 #if 0
    139 		send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL);
    140 		send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data);
    141 #else
    142 		HILWAIT(bbcaddr);
    143 		bbcaddr->hil_cmd = BBC_SET_REG;
    144 		HILWAIT(bbcaddr);
    145 		bbcaddr->hil_data = data;
    146 		HILWAIT(bbcaddr);
    147 		bbcaddr->hil_cmd = BBC_READ_REG;
    148 		HILDATAWAIT(bbcaddr);
    149 		data = bbcaddr->hil_data;
    150 #endif
    151 	}
    152 	return(data);
    153 }
    154