clock.c revision 1.12 1 1.12 tsutsui /* $NetBSD: clock.c,v 1.12 2014/04/19 06:04:58 tsutsui Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.11 rmind * 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.10 tsutsui #include <hp300/stand/common/hilreg.h>
48 1.2 gmcgarry
49 1.12 tsutsui #include <hp300/dev/frodoreg.h> /* for APCI offsets */
50 1.12 tsutsui #include <hp300/dev/intioreg.h> /* for frodo offsets */
51 1.12 tsutsui #include <dev/ic/mc146818reg.h>
52 1.12 tsutsui
53 1.4 tsutsui #include <lib/libsa/stand.h>
54 1.4 tsutsui #include <lib/libsa/net.h>
55 1.4 tsutsui #include <hp300/stand/common/samachdep.h>
56 1.4 tsutsui
57 1.2 gmcgarry #define FEBRUARY 2
58 1.2 gmcgarry #define STARTOFTIME 1970
59 1.4 tsutsui #define SECDAY (60L * 60L * 24L)
60 1.2 gmcgarry #define SECYR (SECDAY * 365)
61 1.2 gmcgarry
62 1.2 gmcgarry #define BBC_SET_REG 0xe0
63 1.2 gmcgarry #define BBC_WRITE_REG 0xc2
64 1.2 gmcgarry #define BBC_READ_REG 0xc3
65 1.2 gmcgarry #define NUM_BBC_REGS 12
66 1.2 gmcgarry
67 1.2 gmcgarry #define leapyear(year) ((year) % 4 == 0)
68 1.12 tsutsui #define range_test(n, l, h) if ((n) < (l) || (n) > (h)) return false
69 1.2 gmcgarry #define days_in_year(a) (leapyear(a) ? 366 : 365)
70 1.2 gmcgarry #define days_in_month(a) (month_days[(a) - 1])
71 1.2 gmcgarry #define bbc_to_decimal(a,b) (bbc_registers[a] * 10 + bbc_registers[b])
72 1.1 thorpej
73 1.4 tsutsui static const int month_days[12] = {
74 1.1 thorpej 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
75 1.1 thorpej };
76 1.1 thorpej
77 1.12 tsutsui static uint8_t bbc_registers[13];
78 1.12 tsutsui static struct hil_dev *bbcaddr = BBCADDR;
79 1.12 tsutsui
80 1.12 tsutsui static volatile uint8_t *mcclock =
81 1.12 tsutsui (volatile uint8_t *)(INTIOBASE + FRODO_BASE + FRODO_CALENDAR);
82 1.1 thorpej
83 1.12 tsutsui static bool clock_to_gmt(satime_t *);
84 1.12 tsutsui
85 1.12 tsutsui static void read_bbc(void);
86 1.12 tsutsui static uint8_t read_bbc_reg(int);
87 1.12 tsutsui static uint8_t mc_read(u_int);
88 1.4 tsutsui
89 1.9 tsutsui satime_t
90 1.4 tsutsui getsecs(void)
91 1.1 thorpej {
92 1.12 tsutsui static bool clockinited = false;
93 1.9 tsutsui satime_t timbuf = 0;
94 1.1 thorpej
95 1.12 tsutsui if (!clock_to_gmt(&timbuf) && !clockinited)
96 1.1 thorpej printf("WARNING: bad date in battery clock\n");
97 1.12 tsutsui clockinited = true;
98 1.1 thorpej
99 1.1 thorpej /* Battery clock does not store usec's, so forget about it. */
100 1.4 tsutsui return timbuf;
101 1.1 thorpej }
102 1.1 thorpej
103 1.12 tsutsui static bool
104 1.12 tsutsui clock_to_gmt(satime_t *timbuf)
105 1.1 thorpej {
106 1.4 tsutsui int i;
107 1.12 tsutsui satime_t tmp;
108 1.1 thorpej int year, month, day, hour, min, sec;
109 1.1 thorpej
110 1.12 tsutsui if (machineid == HP_425 && mmuid == MMUID_425_E) {
111 1.12 tsutsui /* 425e uses mcclock on the frodo utility chip */
112 1.12 tsutsui while ((mc_read(MC_REGA) & MC_REGA_UIP) != 0)
113 1.12 tsutsui continue;
114 1.12 tsutsui sec = mc_read(MC_SEC);
115 1.12 tsutsui min = mc_read(MC_MIN);
116 1.12 tsutsui hour = mc_read(MC_HOUR);
117 1.12 tsutsui day = mc_read(MC_DOM);
118 1.12 tsutsui month = mc_read(MC_MONTH);
119 1.12 tsutsui year = mc_read(MC_YEAR) + 1900;
120 1.12 tsutsui } else {
121 1.12 tsutsui /* Use the traditional HIL bbc for all other models */
122 1.12 tsutsui read_bbc();
123 1.12 tsutsui
124 1.12 tsutsui sec = bbc_to_decimal(1, 0);
125 1.12 tsutsui min = bbc_to_decimal(3, 2);
126 1.12 tsutsui
127 1.12 tsutsui /*
128 1.12 tsutsui * Hours are different for some reason. Makes no sense really.
129 1.12 tsutsui */
130 1.12 tsutsui hour = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4];
131 1.12 tsutsui day = bbc_to_decimal(8, 7);
132 1.12 tsutsui month = bbc_to_decimal(10, 9);
133 1.12 tsutsui year = bbc_to_decimal(12, 11) + 1900;
134 1.12 tsutsui }
135 1.1 thorpej
136 1.5 tsutsui if (year < STARTOFTIME)
137 1.5 tsutsui year += 100;
138 1.1 thorpej
139 1.12 tsutsui #ifdef CLOCK_DEBUG
140 1.12 tsutsui printf("clock todr: %u/%u/%u %u:%u:%u\n",
141 1.12 tsutsui year, month, day, hour, min, sec);
142 1.12 tsutsui #endif
143 1.12 tsutsui
144 1.1 thorpej range_test(hour, 0, 23);
145 1.1 thorpej range_test(day, 1, 31);
146 1.1 thorpej range_test(month, 1, 12);
147 1.1 thorpej
148 1.1 thorpej tmp = 0;
149 1.1 thorpej
150 1.1 thorpej for (i = STARTOFTIME; i < year; i++)
151 1.1 thorpej tmp += days_in_year(i);
152 1.1 thorpej if (leapyear(year) && month > FEBRUARY)
153 1.1 thorpej tmp++;
154 1.1 thorpej
155 1.1 thorpej for (i = 1; i < month; i++)
156 1.1 thorpej tmp += days_in_month(i);
157 1.1 thorpej
158 1.1 thorpej tmp += (day - 1);
159 1.1 thorpej tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
160 1.1 thorpej
161 1.1 thorpej *timbuf = tmp;
162 1.12 tsutsui return true;
163 1.1 thorpej }
164 1.1 thorpej
165 1.12 tsutsui static void
166 1.6 tsutsui read_bbc(void)
167 1.1 thorpej {
168 1.12 tsutsui int i;
169 1.12 tsutsui bool read_okay;
170 1.1 thorpej
171 1.12 tsutsui read_okay = false;
172 1.1 thorpej while (!read_okay) {
173 1.12 tsutsui read_okay = true;
174 1.1 thorpej for (i = 0; i <= NUM_BBC_REGS; i++)
175 1.1 thorpej bbc_registers[i] = read_bbc_reg(i);
176 1.1 thorpej for (i = 0; i <= NUM_BBC_REGS; i++)
177 1.1 thorpej if (bbc_registers[i] != read_bbc_reg(i))
178 1.12 tsutsui read_okay = false;
179 1.1 thorpej }
180 1.1 thorpej }
181 1.1 thorpej
182 1.12 tsutsui static uint8_t
183 1.6 tsutsui read_bbc_reg(int reg)
184 1.1 thorpej {
185 1.12 tsutsui uint8_t data = reg;
186 1.1 thorpej
187 1.12 tsutsui if (bbcaddr != NULL) {
188 1.1 thorpej #if 0
189 1.1 thorpej send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL);
190 1.1 thorpej send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data);
191 1.1 thorpej #else
192 1.1 thorpej HILWAIT(bbcaddr);
193 1.1 thorpej bbcaddr->hil_cmd = BBC_SET_REG;
194 1.1 thorpej HILWAIT(bbcaddr);
195 1.1 thorpej bbcaddr->hil_data = data;
196 1.1 thorpej HILWAIT(bbcaddr);
197 1.1 thorpej bbcaddr->hil_cmd = BBC_READ_REG;
198 1.1 thorpej HILDATAWAIT(bbcaddr);
199 1.1 thorpej data = bbcaddr->hil_data;
200 1.1 thorpej #endif
201 1.1 thorpej }
202 1.4 tsutsui return data;
203 1.1 thorpej }
204 1.12 tsutsui
205 1.12 tsutsui uint8_t
206 1.12 tsutsui mc_read(u_int reg)
207 1.12 tsutsui {
208 1.12 tsutsui uint8_t datum;
209 1.12 tsutsui
210 1.12 tsutsui mcclock[0] = (uint8_t)reg;
211 1.12 tsutsui datum = mcclock[1 << 2]; /* frodo chip has 4 byte stride */
212 1.12 tsutsui
213 1.12 tsutsui return datum;
214 1.12 tsutsui }
215