1 /* $NetBSD: clock.c,v 1.5 2004/04/07 13:29:26 tsutsui 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 <net/if_ether.h> 82 #include <netinet/in.h> 83 #include <netinet/in_systm.h> 84 85 #include <hp300/dev/hilreg.h> 86 87 #include <lib/libsa/stand.h> 88 #include <lib/libsa/net.h> 89 #include <hp300/stand/common/samachdep.h> 90 91 #define FEBRUARY 2 92 #define STARTOFTIME 1970 93 #define SECDAY (60L * 60L * 24L) 94 #define SECYR (SECDAY * 365) 95 96 #define BBC_SET_REG 0xe0 97 #define BBC_WRITE_REG 0xc2 98 #define BBC_READ_REG 0xc3 99 #define NUM_BBC_REGS 12 100 101 #define leapyear(year) ((year) % 4 == 0) 102 #define range_test(n, l, h) if ((n) < (l) || (n) > (h)) return 0 103 #define days_in_year(a) (leapyear(a) ? 366 : 365) 104 #define days_in_month(a) (month_days[(a) - 1]) 105 #define bbc_to_decimal(a,b) (bbc_registers[a] * 10 + bbc_registers[b]) 106 107 static const int month_days[12] = { 108 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 109 }; 110 111 u_char bbc_registers[13]; 112 struct hil_dev *bbcaddr = BBCADDR; 113 114 static int bbc_to_gmt(u_long *); 115 116 time_t 117 getsecs(void) 118 { 119 static int bbcinited = 0; 120 time_t timbuf = 0; 121 122 if (!bbc_to_gmt(&timbuf) && !bbcinited) 123 printf("WARNING: bad date in battery clock\n"); 124 bbcinited = 1; 125 126 /* Battery clock does not store usec's, so forget about it. */ 127 return timbuf; 128 } 129 130 131 static int 132 bbc_to_gmt(timbuf) 133 u_long *timbuf; 134 { 135 int i; 136 u_long tmp; 137 int year, month, day, hour, min, sec; 138 139 read_bbc(); 140 141 sec = bbc_to_decimal(1, 0); 142 min = bbc_to_decimal(3, 2); 143 144 /* 145 * Hours are different for some reason. Makes no sense really. 146 */ 147 hour = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4]; 148 day = bbc_to_decimal(8, 7); 149 month = bbc_to_decimal(10, 9); 150 year = bbc_to_decimal(12, 11) + 1900; 151 if (year < STARTOFTIME) 152 year += 100; 153 154 range_test(hour, 0, 23); 155 range_test(day, 1, 31); 156 range_test(month, 1, 12); 157 158 tmp = 0; 159 160 for (i = STARTOFTIME; i < year; i++) 161 tmp += days_in_year(i); 162 if (leapyear(year) && month > FEBRUARY) 163 tmp++; 164 165 for (i = 1; i < month; i++) 166 tmp += days_in_month(i); 167 168 tmp += (day - 1); 169 tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec; 170 171 *timbuf = tmp; 172 return 1; 173 } 174 175 void 176 read_bbc() 177 { 178 int i, read_okay; 179 180 read_okay = 0; 181 while (!read_okay) { 182 read_okay = 1; 183 for (i = 0; i <= NUM_BBC_REGS; i++) 184 bbc_registers[i] = read_bbc_reg(i); 185 for (i = 0; i <= NUM_BBC_REGS; i++) 186 if (bbc_registers[i] != read_bbc_reg(i)) 187 read_okay = 0; 188 } 189 } 190 191 u_char 192 read_bbc_reg(reg) 193 int reg; 194 { 195 u_char data = reg; 196 197 if (bbcaddr) { 198 #if 0 199 send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL); 200 send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data); 201 #else 202 HILWAIT(bbcaddr); 203 bbcaddr->hil_cmd = BBC_SET_REG; 204 HILWAIT(bbcaddr); 205 bbcaddr->hil_data = data; 206 HILWAIT(bbcaddr); 207 bbcaddr->hil_cmd = BBC_READ_REG; 208 HILDATAWAIT(bbcaddr); 209 data = bbcaddr->hil_data; 210 #endif 211 } 212 return data; 213 } 214