1 /* $OpenBSD: clock.c,v 1.31 2023/09/17 14:50:50 cheloha Exp $ */
2 /* $NetBSD: clock.c,v 1.29 2000/06/05 21:47:10 thorpej Exp $ */
3
4 /*
5 * Copyright (c) 1988 University of Utah.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department and Ralph Campbell.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * from: Utah Hdr: clock.c 1.18 91/01/21
38 *
39 * @(#)clock.c 8.1 (Berkeley) 6/10/93
40 */
41
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/clockintr.h>
46 #include <sys/device.h>
47 #include <sys/evcount.h>
48 #include <sys/sched.h>
49 #include <sys/timetc.h>
50
51 #include <dev/clock_subr.h>
52
53 #include <machine/rpb.h>
54 #include <machine/autoconf.h>
55 #include <machine/cpuconf.h>
56
57 #include <alpha/alpha/clockvar.h>
58
59 struct device *clockdev;
60 const struct clockfns *clockfns;
61
62 struct evcount clk_count;
63 int clk_irq = 0;
64
65 u_int rpcc_get_timecount(struct timecounter *);
66 struct timecounter rpcc_timecounter = {
67 .tc_get_timecount = rpcc_get_timecount,
68 .tc_counter_mask = ~0u,
69 .tc_frequency = 0,
70 .tc_name = "rpcc",
71 .tc_quality = 0,
72 .tc_priv = NULL,
73 .tc_user = 0,
74 };
75
76 extern todr_chip_handle_t todr_handle;
77 struct todr_chip_handle rtc_todr;
78
79 int
rtc_gettime(struct todr_chip_handle * handle,struct timeval * tv)80 rtc_gettime(struct todr_chip_handle *handle, struct timeval *tv)
81 {
82 struct clock_ymdhms dt;
83 struct clocktime ct;
84 int year;
85
86 (*clockfns->cf_get)(clockdev, tv->tv_sec, &ct);
87
88 year = 1900 + ct.year;
89 if (year < 1970)
90 year += 100;
91 dt.dt_year = year;
92 dt.dt_mon = ct.mon;
93 dt.dt_day = ct.day;
94 dt.dt_hour = ct.hour;
95 dt.dt_min = ct.min;
96 dt.dt_sec = ct.sec;
97
98 tv->tv_sec = clock_ymdhms_to_secs(&dt);
99 tv->tv_usec = 0;
100 return 0;
101 }
102
103 int
rtc_settime(struct todr_chip_handle * handle,struct timeval * tv)104 rtc_settime(struct todr_chip_handle *handle, struct timeval *tv)
105 {
106 struct clock_ymdhms dt;
107 struct clocktime ct;
108
109 clock_secs_to_ymdhms(tv->tv_sec, &dt);
110
111 /* rt clock wants 2 digits */
112 ct.year = dt.dt_year % 100;
113 ct.mon = dt.dt_mon;
114 ct.day = dt.dt_day;
115 ct.hour = dt.dt_hour;
116 ct.min = dt.dt_min;
117 ct.sec = dt.dt_sec;
118 ct.dow = dt.dt_wday;
119
120 (*clockfns->cf_set)(clockdev, &ct);
121 return 0;
122 }
123
124 void
clockattach(dev,fns)125 clockattach(dev, fns)
126 struct device *dev;
127 const struct clockfns *fns;
128 {
129
130 /*
131 * Just bookkeeping.
132 */
133 printf("\n");
134
135 if (clockfns != NULL)
136 panic("clockattach: multiple clocks");
137 clockdev = dev;
138 clockfns = fns;
139 }
140
141 /*
142 * Machine-dependent clock routines.
143 */
144
145 /*
146 * Start the real-time and statistics clocks.
147 */
148 void
cpu_initclocks(void)149 cpu_initclocks(void)
150 {
151 u_int32_t cycles_per_sec;
152 struct clocktime ct;
153 u_int32_t first_rpcc, second_rpcc; /* only lower 32 bits are valid */
154 int first_sec;
155
156 if (clockfns == NULL)
157 panic("cpu_initclocks: no clock attached");
158
159 tick = 1000000 / hz; /* number of microseconds between interrupts */
160 tick_nsec = 1000000000 / hz;
161
162 evcount_attach(&clk_count, "clock", &clk_irq);
163
164 /*
165 * Get the clock started.
166 */
167 (*clockfns->cf_init)(clockdev);
168
169 /*
170 * Calibrate the cycle counter frequency.
171 */
172 (*clockfns->cf_get)(clockdev, 0, &ct);
173 first_sec = ct.sec;
174
175 /* Let the clock tick one second. */
176 do {
177 first_rpcc = alpha_rpcc();
178 (*clockfns->cf_get)(clockdev, 0, &ct);
179 } while (ct.sec == first_sec);
180 first_sec = ct.sec;
181 /* Let the clock tick one more second. */
182 do {
183 second_rpcc = alpha_rpcc();
184 (*clockfns->cf_get)(clockdev, 0, &ct);
185 } while (ct.sec == first_sec);
186
187 cycles_per_sec = second_rpcc - first_rpcc;
188
189 rpcc_timecounter.tc_frequency = cycles_per_sec;
190 tc_init(&rpcc_timecounter);
191
192 stathz = hz;
193 profhz = stathz;
194 }
195
196 void
cpu_startclock(void)197 cpu_startclock(void)
198 {
199 clockintr_cpu_init(NULL);
200
201 /*
202 * Establish the clock interrupt; it's a special case.
203 *
204 * We establish the clock interrupt this late because if
205 * we do it at clock attach time, we may have never been at
206 * spl0() since taking over the system. Some versions of
207 * PALcode save a clock interrupt, which would get delivered
208 * when we spl0() in autoconf.c. If established the clock
209 * interrupt handler earlier, that interrupt would go to
210 * hardclock, which would then fall over because the pointer
211 * to the virtual timers wasn't set at that time.
212 */
213 platform.clockintr = clockintr_dispatch;
214
215 rtc_todr.todr_gettime = rtc_gettime;
216 rtc_todr.todr_settime = rtc_settime;
217 todr_handle = &rtc_todr;
218 }
219
220 void
setstatclockrate(int newhz)221 setstatclockrate(int newhz)
222 {
223 }
224
225 u_int
rpcc_get_timecount(struct timecounter * tc)226 rpcc_get_timecount(struct timecounter *tc)
227 {
228 return alpha_rpcc();
229 }
230