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