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