Home | History | Annotate | Line # | Download | only in common
clock.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: clock.c,v 1.1 1997/02/04 03:52:15 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1988 University of Utah.
      5  1.1  thorpej  * Copyright (c) 1982, 1990, 1993
      6  1.1  thorpej  *	The Regents of the University of California.  All rights reserved.
      7  1.1  thorpej  *
      8  1.1  thorpej  * This code is derived from software contributed to Berkeley by
      9  1.1  thorpej  * the Systems Programming Group of the University of Utah Computer
     10  1.1  thorpej  * Science Department.
     11  1.1  thorpej  *
     12  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     13  1.1  thorpej  * modification, are permitted provided that the following conditions
     14  1.1  thorpej  * are met:
     15  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     17  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     19  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     20  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     21  1.1  thorpej  *    must display the following acknowledgement:
     22  1.1  thorpej  *	This product includes software developed by the University of
     23  1.1  thorpej  *	California, Berkeley and its contributors.
     24  1.1  thorpej  * 4. Neither the name of the University nor the names of its contributors
     25  1.1  thorpej  *    may be used to endorse or promote products derived from this software
     26  1.1  thorpej  *    without specific prior written permission.
     27  1.1  thorpej  *
     28  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.1  thorpej  * SUCH DAMAGE.
     39  1.1  thorpej  *
     40  1.1  thorpej  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     41  1.1  thorpej  *
     42  1.1  thorpej  *	@(#)clock.c	8.2 (Berkeley) 1/12/94
     43  1.1  thorpej  */
     44  1.1  thorpej 
     45  1.1  thorpej #include <sys/param.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <hp300/dev/hilreg.h>
     48  1.1  thorpej #include <hp300/hp300/clockreg.h>
     49  1.1  thorpej 
     50  1.1  thorpej #include <hp300/stand/common/samachdep.h>
     51  1.1  thorpej 
     52  1.1  thorpej static int month_days[12] = {
     53  1.1  thorpej 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
     54  1.1  thorpej };
     55  1.1  thorpej 
     56  1.1  thorpej u_char bbc_registers[13];
     57  1.1  thorpej u_char read_bbc_reg();
     58  1.1  thorpej struct hil_dev *bbcaddr = BBCADDR;
     59  1.1  thorpej 
     60  1.1  thorpej getsecs()
     61  1.1  thorpej {
     62  1.1  thorpej 	static int bbcinited = 0;
     63  1.1  thorpej 	u_long timbuf = 0;
     64  1.1  thorpej 
     65  1.1  thorpej 	if (!bbc_to_gmt(&timbuf) && !bbcinited)
     66  1.1  thorpej 		printf("WARNING: bad date in battery clock\n");
     67  1.1  thorpej 	bbcinited = 1;
     68  1.1  thorpej 
     69  1.1  thorpej 	/* Battery clock does not store usec's, so forget about it. */
     70  1.1  thorpej 	return(timbuf);
     71  1.1  thorpej }
     72  1.1  thorpej 
     73  1.1  thorpej 
     74  1.1  thorpej bbc_to_gmt(timbuf)
     75  1.1  thorpej 	u_long *timbuf;
     76  1.1  thorpej {
     77  1.1  thorpej 	register int i;
     78  1.1  thorpej 	register u_long tmp;
     79  1.1  thorpej 	int year, month, day, hour, min, sec;
     80  1.1  thorpej 
     81  1.1  thorpej 	read_bbc();
     82  1.1  thorpej 
     83  1.1  thorpej 	sec = bbc_to_decimal(1, 0);
     84  1.1  thorpej 	min = bbc_to_decimal(3, 2);
     85  1.1  thorpej 
     86  1.1  thorpej 	/*
     87  1.1  thorpej 	 * Hours are different for some reason. Makes no sense really.
     88  1.1  thorpej 	 */
     89  1.1  thorpej 	hour  = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4];
     90  1.1  thorpej 	day   = bbc_to_decimal(8, 7);
     91  1.1  thorpej 	month = bbc_to_decimal(10, 9);
     92  1.1  thorpej 	year  = bbc_to_decimal(12, 11) + 1900;
     93  1.1  thorpej 
     94  1.1  thorpej 	range_test(hour, 0, 23);
     95  1.1  thorpej 	range_test(day, 1, 31);
     96  1.1  thorpej 	range_test(month, 1, 12);
     97  1.1  thorpej 	range_test(year, STARTOFTIME, 2000);
     98  1.1  thorpej 
     99  1.1  thorpej 	tmp = 0;
    100  1.1  thorpej 
    101  1.1  thorpej 	for (i = STARTOFTIME; i < year; i++)
    102  1.1  thorpej 		tmp += days_in_year(i);
    103  1.1  thorpej 	if (leapyear(year) && month > FEBRUARY)
    104  1.1  thorpej 		tmp++;
    105  1.1  thorpej 
    106  1.1  thorpej 	for (i = 1; i < month; i++)
    107  1.1  thorpej 	  	tmp += days_in_month(i);
    108  1.1  thorpej 
    109  1.1  thorpej 	tmp += (day - 1);
    110  1.1  thorpej 	tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
    111  1.1  thorpej 
    112  1.1  thorpej 	*timbuf = tmp;
    113  1.1  thorpej 	return(1);
    114  1.1  thorpej }
    115  1.1  thorpej 
    116  1.1  thorpej read_bbc()
    117  1.1  thorpej {
    118  1.1  thorpej   	register int i, read_okay;
    119  1.1  thorpej 
    120  1.1  thorpej 	read_okay = 0;
    121  1.1  thorpej 	while (!read_okay) {
    122  1.1  thorpej 		read_okay = 1;
    123  1.1  thorpej 		for (i = 0; i <= NUM_BBC_REGS; i++)
    124  1.1  thorpej 			bbc_registers[i] = read_bbc_reg(i);
    125  1.1  thorpej 		for (i = 0; i <= NUM_BBC_REGS; i++)
    126  1.1  thorpej 			if (bbc_registers[i] != read_bbc_reg(i))
    127  1.1  thorpej 				read_okay = 0;
    128  1.1  thorpej 	}
    129  1.1  thorpej }
    130  1.1  thorpej 
    131  1.1  thorpej u_char
    132  1.1  thorpej read_bbc_reg(reg)
    133  1.1  thorpej 	int reg;
    134  1.1  thorpej {
    135  1.1  thorpej 	u_char data = reg;
    136  1.1  thorpej 
    137  1.1  thorpej 	if (bbcaddr) {
    138  1.1  thorpej #if 0
    139  1.1  thorpej 		send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL);
    140  1.1  thorpej 		send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data);
    141  1.1  thorpej #else
    142  1.1  thorpej 		HILWAIT(bbcaddr);
    143  1.1  thorpej 		bbcaddr->hil_cmd = BBC_SET_REG;
    144  1.1  thorpej 		HILWAIT(bbcaddr);
    145  1.1  thorpej 		bbcaddr->hil_data = data;
    146  1.1  thorpej 		HILWAIT(bbcaddr);
    147  1.1  thorpej 		bbcaddr->hil_cmd = BBC_READ_REG;
    148  1.1  thorpej 		HILDATAWAIT(bbcaddr);
    149  1.1  thorpej 		data = bbcaddr->hil_data;
    150  1.1  thorpej #endif
    151  1.1  thorpej 	}
    152  1.1  thorpej 	return(data);
    153  1.1  thorpej }
    154