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