clock.c revision 1.12 1 /* $NetBSD: clock.c,v 1.12 2014/04/19 06:04:58 tsutsui 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah $Hdr: clock.c 1.18 91/01/21$
37 *
38 * @(#)clock.c 8.2 (Berkeley) 1/12/94
39 */
40
41 #include <sys/param.h>
42
43 #include <net/if_ether.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46
47 #include <hp300/stand/common/hilreg.h>
48
49 #include <hp300/dev/frodoreg.h> /* for APCI offsets */
50 #include <hp300/dev/intioreg.h> /* for frodo offsets */
51 #include <dev/ic/mc146818reg.h>
52
53 #include <lib/libsa/stand.h>
54 #include <lib/libsa/net.h>
55 #include <hp300/stand/common/samachdep.h>
56
57 #define FEBRUARY 2
58 #define STARTOFTIME 1970
59 #define SECDAY (60L * 60L * 24L)
60 #define SECYR (SECDAY * 365)
61
62 #define BBC_SET_REG 0xe0
63 #define BBC_WRITE_REG 0xc2
64 #define BBC_READ_REG 0xc3
65 #define NUM_BBC_REGS 12
66
67 #define leapyear(year) ((year) % 4 == 0)
68 #define range_test(n, l, h) if ((n) < (l) || (n) > (h)) return false
69 #define days_in_year(a) (leapyear(a) ? 366 : 365)
70 #define days_in_month(a) (month_days[(a) - 1])
71 #define bbc_to_decimal(a,b) (bbc_registers[a] * 10 + bbc_registers[b])
72
73 static const int month_days[12] = {
74 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
75 };
76
77 static uint8_t bbc_registers[13];
78 static struct hil_dev *bbcaddr = BBCADDR;
79
80 static volatile uint8_t *mcclock =
81 (volatile uint8_t *)(INTIOBASE + FRODO_BASE + FRODO_CALENDAR);
82
83 static bool clock_to_gmt(satime_t *);
84
85 static void read_bbc(void);
86 static uint8_t read_bbc_reg(int);
87 static uint8_t mc_read(u_int);
88
89 satime_t
90 getsecs(void)
91 {
92 static bool clockinited = false;
93 satime_t timbuf = 0;
94
95 if (!clock_to_gmt(&timbuf) && !clockinited)
96 printf("WARNING: bad date in battery clock\n");
97 clockinited = true;
98
99 /* Battery clock does not store usec's, so forget about it. */
100 return timbuf;
101 }
102
103 static bool
104 clock_to_gmt(satime_t *timbuf)
105 {
106 int i;
107 satime_t tmp;
108 int year, month, day, hour, min, sec;
109
110 if (machineid == HP_425 && mmuid == MMUID_425_E) {
111 /* 425e uses mcclock on the frodo utility chip */
112 while ((mc_read(MC_REGA) & MC_REGA_UIP) != 0)
113 continue;
114 sec = mc_read(MC_SEC);
115 min = mc_read(MC_MIN);
116 hour = mc_read(MC_HOUR);
117 day = mc_read(MC_DOM);
118 month = mc_read(MC_MONTH);
119 year = mc_read(MC_YEAR) + 1900;
120 } else {
121 /* Use the traditional HIL bbc for all other models */
122 read_bbc();
123
124 sec = bbc_to_decimal(1, 0);
125 min = bbc_to_decimal(3, 2);
126
127 /*
128 * Hours are different for some reason. Makes no sense really.
129 */
130 hour = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4];
131 day = bbc_to_decimal(8, 7);
132 month = bbc_to_decimal(10, 9);
133 year = bbc_to_decimal(12, 11) + 1900;
134 }
135
136 if (year < STARTOFTIME)
137 year += 100;
138
139 #ifdef CLOCK_DEBUG
140 printf("clock todr: %u/%u/%u %u:%u:%u\n",
141 year, month, day, hour, min, sec);
142 #endif
143
144 range_test(hour, 0, 23);
145 range_test(day, 1, 31);
146 range_test(month, 1, 12);
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 true;
163 }
164
165 static void
166 read_bbc(void)
167 {
168 int i;
169 bool read_okay;
170
171 read_okay = false;
172 while (!read_okay) {
173 read_okay = true;
174 for (i = 0; i <= NUM_BBC_REGS; i++)
175 bbc_registers[i] = read_bbc_reg(i);
176 for (i = 0; i <= NUM_BBC_REGS; i++)
177 if (bbc_registers[i] != read_bbc_reg(i))
178 read_okay = false;
179 }
180 }
181
182 static uint8_t
183 read_bbc_reg(int reg)
184 {
185 uint8_t data = reg;
186
187 if (bbcaddr != NULL) {
188 #if 0
189 send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL);
190 send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data);
191 #else
192 HILWAIT(bbcaddr);
193 bbcaddr->hil_cmd = BBC_SET_REG;
194 HILWAIT(bbcaddr);
195 bbcaddr->hil_data = data;
196 HILWAIT(bbcaddr);
197 bbcaddr->hil_cmd = BBC_READ_REG;
198 HILDATAWAIT(bbcaddr);
199 data = bbcaddr->hil_data;
200 #endif
201 }
202 return data;
203 }
204
205 uint8_t
206 mc_read(u_int reg)
207 {
208 uint8_t datum;
209
210 mcclock[0] = (uint8_t)reg;
211 datum = mcclock[1 << 2]; /* frodo chip has 4 byte stride */
212
213 return datum;
214 }
215