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