clock.c revision 1.15 1 1.15 tsutsui /* $NetBSD: clock.c,v 1.15 2025/05/17 17:27:21 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.13 christos #include <dev/clock_subr.h>
53 1.12 tsutsui
54 1.4 tsutsui #include <lib/libsa/stand.h>
55 1.4 tsutsui #include <lib/libsa/net.h>
56 1.4 tsutsui #include <hp300/stand/common/samachdep.h>
57 1.4 tsutsui
58 1.2 gmcgarry #define FEBRUARY 2
59 1.2 gmcgarry
60 1.2 gmcgarry #define BBC_SET_REG 0xe0
61 1.2 gmcgarry #define BBC_WRITE_REG 0xc2
62 1.2 gmcgarry #define BBC_READ_REG 0xc3
63 1.2 gmcgarry #define NUM_BBC_REGS 12
64 1.2 gmcgarry
65 1.15 tsutsui #ifdef BBC_SLOW_GETSECS
66 1.15 tsutsui #define NSKIP_READ_BBC 100
67 1.15 tsutsui #endif
68 1.15 tsutsui
69 1.12 tsutsui #define range_test(n, l, h) if ((n) < (l) || (n) > (h)) return false
70 1.2 gmcgarry #define bbc_to_decimal(a,b) (bbc_registers[a] * 10 + bbc_registers[b])
71 1.1 thorpej
72 1.12 tsutsui static uint8_t bbc_registers[13];
73 1.12 tsutsui static struct hil_dev *bbcaddr = BBCADDR;
74 1.12 tsutsui
75 1.12 tsutsui static volatile uint8_t *mcclock =
76 1.12 tsutsui (volatile uint8_t *)(INTIOBASE + FRODO_BASE + FRODO_CALENDAR);
77 1.1 thorpej
78 1.12 tsutsui static bool clock_to_gmt(satime_t *);
79 1.12 tsutsui
80 1.12 tsutsui static void read_bbc(void);
81 1.12 tsutsui static uint8_t read_bbc_reg(int);
82 1.12 tsutsui static uint8_t mc_read(u_int);
83 1.4 tsutsui
84 1.9 tsutsui satime_t
85 1.4 tsutsui getsecs(void)
86 1.1 thorpej {
87 1.12 tsutsui static bool clockinited = false;
88 1.9 tsutsui satime_t timbuf = 0;
89 1.1 thorpej
90 1.12 tsutsui if (!clock_to_gmt(&timbuf) && !clockinited)
91 1.1 thorpej printf("WARNING: bad date in battery clock\n");
92 1.12 tsutsui clockinited = true;
93 1.1 thorpej
94 1.1 thorpej /* Battery clock does not store usec's, so forget about it. */
95 1.4 tsutsui return timbuf;
96 1.1 thorpej }
97 1.1 thorpej
98 1.12 tsutsui static bool
99 1.12 tsutsui clock_to_gmt(satime_t *timbuf)
100 1.1 thorpej {
101 1.4 tsutsui int i;
102 1.12 tsutsui satime_t tmp;
103 1.1 thorpej int year, month, day, hour, min, sec;
104 1.15 tsutsui #ifdef BBC_SLOW_GETSECS
105 1.15 tsutsui static satime_t tim_prev = 0;
106 1.15 tsutsui #endif
107 1.1 thorpej
108 1.12 tsutsui if (machineid == HP_425 && mmuid == MMUID_425_E) {
109 1.12 tsutsui /* 425e uses mcclock on the frodo utility chip */
110 1.12 tsutsui while ((mc_read(MC_REGA) & MC_REGA_UIP) != 0)
111 1.12 tsutsui continue;
112 1.12 tsutsui sec = mc_read(MC_SEC);
113 1.12 tsutsui min = mc_read(MC_MIN);
114 1.12 tsutsui hour = mc_read(MC_HOUR);
115 1.12 tsutsui day = mc_read(MC_DOM);
116 1.12 tsutsui month = mc_read(MC_MONTH);
117 1.12 tsutsui year = mc_read(MC_YEAR) + 1900;
118 1.12 tsutsui } else {
119 1.15 tsutsui #ifdef BBC_SLOW_GETSECS
120 1.15 tsutsui /*
121 1.15 tsutsui * XXX:
122 1.15 tsutsui * The read_bbc() function against HIL seems extremely slow.
123 1.15 tsutsui * It was being called on every timeout check via getsecs()
124 1.15 tsutsui * so it significantly slowed down loading a kernel via NFS.
125 1.15 tsutsui *
126 1.15 tsutsui * In most cases (e.g., in common/if_le.c and libsa/net.c),
127 1.15 tsutsui * timeout checks are performed using getsecs() in busy loops
128 1.15 tsutsui * without any actual waiting. In such cases, it's not
129 1.15 tsutsui * necessary to read the precise time from the RTC on
130 1.15 tsutsui * every call.
131 1.15 tsutsui */
132 1.15 tsutsui static int nskip = NSKIP_READ_BBC;
133 1.15 tsutsui
134 1.15 tsutsui if (tim_prev != 0 && nskip-- > 0) {
135 1.15 tsutsui *timbuf = tim_prev;
136 1.15 tsutsui return true;
137 1.15 tsutsui }
138 1.15 tsutsui nskip = NSKIP_READ_BBC;
139 1.15 tsutsui #endif
140 1.15 tsutsui
141 1.12 tsutsui /* Use the traditional HIL bbc for all other models */
142 1.12 tsutsui read_bbc();
143 1.12 tsutsui
144 1.12 tsutsui sec = bbc_to_decimal(1, 0);
145 1.12 tsutsui min = bbc_to_decimal(3, 2);
146 1.12 tsutsui
147 1.12 tsutsui /*
148 1.12 tsutsui * Hours are different for some reason. Makes no sense really.
149 1.12 tsutsui */
150 1.12 tsutsui hour = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4];
151 1.12 tsutsui day = bbc_to_decimal(8, 7);
152 1.12 tsutsui month = bbc_to_decimal(10, 9);
153 1.12 tsutsui year = bbc_to_decimal(12, 11) + 1900;
154 1.12 tsutsui }
155 1.1 thorpej
156 1.13 christos if (year < POSIX_BASE_YEAR)
157 1.5 tsutsui year += 100;
158 1.1 thorpej
159 1.12 tsutsui #ifdef CLOCK_DEBUG
160 1.12 tsutsui printf("clock todr: %u/%u/%u %u:%u:%u\n",
161 1.12 tsutsui year, month, day, hour, min, sec);
162 1.12 tsutsui #endif
163 1.12 tsutsui
164 1.1 thorpej range_test(hour, 0, 23);
165 1.1 thorpej range_test(day, 1, 31);
166 1.1 thorpej range_test(month, 1, 12);
167 1.1 thorpej
168 1.1 thorpej tmp = 0;
169 1.1 thorpej
170 1.13 christos for (i = POSIX_BASE_YEAR; i < year; i++)
171 1.13 christos tmp += days_per_year(i);
172 1.13 christos if (is_leap_year(year) && month > FEBRUARY)
173 1.1 thorpej tmp++;
174 1.1 thorpej
175 1.1 thorpej for (i = 1; i < month; i++)
176 1.1 thorpej tmp += days_in_month(i);
177 1.14 tsutsui
178 1.1 thorpej tmp += (day - 1);
179 1.1 thorpej tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
180 1.1 thorpej
181 1.15 tsutsui #ifdef BBC_SLOW_GETSECS
182 1.15 tsutsui tim_prev = tmp;
183 1.15 tsutsui #endif
184 1.1 thorpej *timbuf = tmp;
185 1.12 tsutsui return true;
186 1.1 thorpej }
187 1.1 thorpej
188 1.12 tsutsui static void
189 1.6 tsutsui read_bbc(void)
190 1.1 thorpej {
191 1.12 tsutsui int i;
192 1.12 tsutsui bool read_okay;
193 1.1 thorpej
194 1.12 tsutsui read_okay = false;
195 1.1 thorpej while (!read_okay) {
196 1.12 tsutsui read_okay = true;
197 1.1 thorpej for (i = 0; i <= NUM_BBC_REGS; i++)
198 1.1 thorpej bbc_registers[i] = read_bbc_reg(i);
199 1.1 thorpej for (i = 0; i <= NUM_BBC_REGS; i++)
200 1.1 thorpej if (bbc_registers[i] != read_bbc_reg(i))
201 1.12 tsutsui read_okay = false;
202 1.1 thorpej }
203 1.1 thorpej }
204 1.1 thorpej
205 1.12 tsutsui static uint8_t
206 1.6 tsutsui read_bbc_reg(int reg)
207 1.1 thorpej {
208 1.12 tsutsui uint8_t data = reg;
209 1.1 thorpej
210 1.12 tsutsui if (bbcaddr != NULL) {
211 1.1 thorpej #if 0
212 1.1 thorpej send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL);
213 1.1 thorpej send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data);
214 1.1 thorpej #else
215 1.1 thorpej HILWAIT(bbcaddr);
216 1.1 thorpej bbcaddr->hil_cmd = BBC_SET_REG;
217 1.1 thorpej HILWAIT(bbcaddr);
218 1.1 thorpej bbcaddr->hil_data = data;
219 1.1 thorpej HILWAIT(bbcaddr);
220 1.1 thorpej bbcaddr->hil_cmd = BBC_READ_REG;
221 1.1 thorpej HILDATAWAIT(bbcaddr);
222 1.1 thorpej data = bbcaddr->hil_data;
223 1.1 thorpej #endif
224 1.1 thorpej }
225 1.4 tsutsui return data;
226 1.1 thorpej }
227 1.12 tsutsui
228 1.12 tsutsui uint8_t
229 1.12 tsutsui mc_read(u_int reg)
230 1.12 tsutsui {
231 1.12 tsutsui uint8_t datum;
232 1.12 tsutsui
233 1.12 tsutsui mcclock[0] = (uint8_t)reg;
234 1.12 tsutsui datum = mcclock[1 << 2]; /* frodo chip has 4 byte stride */
235 1.12 tsutsui
236 1.12 tsutsui return datum;
237 1.12 tsutsui }
238