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