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