loongson_clock.c revision 1.1 1 1.1 macallan /* $NetBSD: loongson_clock.c,v 1.1 2016/06/24 21:41:37 macallan Exp $ */
2 1.1 macallan
3 1.1 macallan /*
4 1.1 macallan * Copyright (c) 2011, 2016 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 macallan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 macallan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 macallan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 macallan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 macallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 macallan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 macallan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 macallan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 macallan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 macallan */
27 1.1 macallan
28 1.1 macallan #include <sys/cdefs.h>
29 1.1 macallan __KERNEL_RCSID(0, "$NetBSD: loongson_clock.c,v 1.1 2016/06/24 21:41:37 macallan Exp $");
30 1.1 macallan
31 1.1 macallan #include <sys/param.h>
32 1.1 macallan #include <sys/systm.h>
33 1.1 macallan #include <sys/kernel.h>
34 1.1 macallan #include <sys/device.h>
35 1.1 macallan #include <sys/cpu.h>
36 1.1 macallan #include <sys/timetc.h>
37 1.1 macallan #include <sys/sysctl.h>
38 1.1 macallan
39 1.1 macallan #include <mips/mips3_clock.h>
40 1.1 macallan #include <mips/locore.h>
41 1.1 macallan #include <mips/bonito/bonitoreg.h>
42 1.1 macallan #include <mips/bonito/bonitovar.h>
43 1.1 macallan
44 1.1 macallan #ifdef LOONGSON_CLOCK_DEBUG
45 1.1 macallan #define DPRINTF aprint_error
46 1.1 macallan #else
47 1.1 macallan #define DPRINTF while (0) printf
48 1.1 macallan #endif
49 1.1 macallan
50 1.1 macallan static uint32_t sc_last;
51 1.1 macallan static uint32_t sc_scale[8];
52 1.1 macallan static uint32_t sc_count; /* should probably be 64 bit */
53 1.1 macallan static int sc_step = 7;
54 1.1 macallan static int sc_step_wanted = 7;
55 1.1 macallan static void *sc_shutdown_cookie;
56 1.1 macallan
57 1.1 macallan /* 0, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, 1 */
58 1.1 macallan static int scale_m[] = {1, 1, 3, 1, 5, 3, 7, 1};
59 1.1 macallan static int scale_d[] = {0, 4, 8, 2, 8, 4, 8, 1};
60 1.1 macallan static int cycles[8];
61 1.1 macallan
62 1.1 macallan #define scale(x, f) (x * scale_d[f] / scale_m[f])
63 1.1 macallan #define rscale(x, f) (x * scale_m[f] / scale_d[f])
64 1.1 macallan
65 1.1 macallan static void loongson_set_speed(int);
66 1.1 macallan static int loongson_cpuspeed_temp(SYSCTLFN_ARGS);
67 1.1 macallan static int loongson_cpuspeed_cur(SYSCTLFN_ARGS);
68 1.1 macallan static int loongson_cpuspeed_available(SYSCTLFN_ARGS);
69 1.1 macallan
70 1.1 macallan static void loongson_clock_shutdown(void *);
71 1.1 macallan static u_int get_loongson_timecount(struct timecounter *);
72 1.1 macallan void loongson_delay(int);
73 1.1 macallan void loongson_setstatclockrate(int);
74 1.1 macallan void loongson_initclocks(void);
75 1.1 macallan
76 1.1 macallan static struct timecounter loongson_timecounter = {
77 1.1 macallan get_loongson_timecount, /* get_timecount */
78 1.1 macallan 0, /* no poll_pps */
79 1.1 macallan 0xffffffff, /* counter_mask */
80 1.1 macallan 0, /* frequency */
81 1.1 macallan "loongson", /* name */
82 1.1 macallan 100, /* quality */
83 1.1 macallan NULL, /* tc_priv */
84 1.1 macallan NULL /* tc_next */
85 1.1 macallan };
86 1.1 macallan
87 1.1 macallan void
88 1.1 macallan loongson_initclocks(void)
89 1.1 macallan {
90 1.1 macallan const struct sysctlnode *sysctl_node, *me, *freq;
91 1.1 macallan int clk;
92 1.1 macallan
93 1.1 macallan /*
94 1.1 macallan * Establish a hook so on shutdown we can set the CPU clock back to
95 1.1 macallan * full speed. This is necessary because PMON doesn't change the
96 1.1 macallan * clock scale register on a warm boot, the MIPS clock code gets
97 1.1 macallan * confused if we're too slow and the loongson-specific bits run
98 1.1 macallan * too late in the boot process
99 1.1 macallan */
100 1.1 macallan sc_shutdown_cookie = shutdownhook_establish(loongson_clock_shutdown, NULL);
101 1.1 macallan
102 1.1 macallan for (clk = 1; clk < 8; clk++) {
103 1.1 macallan sc_scale[clk] = rscale(curcpu()->ci_cpu_freq / 1000000, clk);
104 1.1 macallan cycles[clk] =
105 1.1 macallan (rscale(curcpu()->ci_cpu_freq, clk) + hz / 2) / (2 * hz);
106 1.1 macallan }
107 1.1 macallan #ifdef LOONGSON_CLOCK_DEBUG
108 1.1 macallan for (clk = 1; clk < 8; clk++) {
109 1.1 macallan aprint_normal("frequencies: %d/8: %d\n", clk + 1,
110 1.1 macallan sc_scale[clk]);
111 1.1 macallan }
112 1.1 macallan #endif
113 1.1 macallan
114 1.1 macallan /* now setup sysctl */
115 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
116 1.1 macallan &me,
117 1.1 macallan CTLFLAG_READWRITE, CTLTYPE_NODE, "loongson", NULL, NULL,
118 1.1 macallan 0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
119 1.1 macallan aprint_error("couldn't create 'loongson' node\n");
120 1.1 macallan
121 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
122 1.1 macallan &freq,
123 1.1 macallan CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL, 0, NULL,
124 1.1 macallan 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
125 1.1 macallan aprint_error("couldn't create 'frequency' node\n");
126 1.1 macallan
127 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
128 1.1 macallan &sysctl_node,
129 1.1 macallan CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
130 1.1 macallan CTLTYPE_INT, "target", "CPU speed", loongson_cpuspeed_temp,
131 1.1 macallan 0, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
132 1.1 macallan CTL_CREATE, CTL_EOL) == 0) {
133 1.1 macallan } else
134 1.1 macallan aprint_error("couldn't create 'target' node\n");
135 1.1 macallan
136 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
137 1.1 macallan &sysctl_node,
138 1.1 macallan CTLFLAG_READWRITE,
139 1.1 macallan CTLTYPE_INT, "current", NULL, loongson_cpuspeed_cur,
140 1.1 macallan 1, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
141 1.1 macallan CTL_CREATE, CTL_EOL) == 0) {
142 1.1 macallan } else
143 1.1 macallan aprint_error("couldn't create 'current' node\n");
144 1.1 macallan
145 1.1 macallan if (sysctl_createv(NULL, 0, NULL,
146 1.1 macallan &sysctl_node,
147 1.1 macallan CTLFLAG_READWRITE,
148 1.1 macallan CTLTYPE_STRING, "available", NULL, loongson_cpuspeed_available,
149 1.1 macallan 2, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
150 1.1 macallan CTL_CREATE, CTL_EOL) == 0) {
151 1.1 macallan } else
152 1.1 macallan aprint_error("couldn't create 'available' node\n");
153 1.1 macallan
154 1.1 macallan sc_count = 0;
155 1.1 macallan loongson_timecounter.tc_frequency = curcpu()->ci_cpu_freq / 2;
156 1.1 macallan curcpu()->ci_cctr_freq = loongson_timecounter.tc_frequency;
157 1.1 macallan
158 1.1 macallan sc_last = mips3_cp0_count_read();
159 1.1 macallan mips3_cp0_compare_write(sc_last + curcpu()->ci_cycles_per_hz);
160 1.1 macallan
161 1.1 macallan tc_init(&loongson_timecounter);
162 1.1 macallan
163 1.1 macallan /*
164 1.1 macallan * Now we can enable all interrupts including hardclock(9)
165 1.1 macallan * by CPU INT5.
166 1.1 macallan */
167 1.1 macallan spl0();
168 1.1 macallan printf("boom\n");
169 1.1 macallan }
170 1.1 macallan
171 1.1 macallan static void
172 1.1 macallan loongson_clock_shutdown(void *cookie)
173 1.1 macallan {
174 1.1 macallan
175 1.1 macallan /* just in case the interrupt handler runs again after this */
176 1.1 macallan sc_step_wanted = 7;
177 1.1 macallan /* set the clock to full speed */
178 1.1 macallan REGVAL(LS2F_CHIPCFG0) =
179 1.1 macallan (REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) | 7;
180 1.1 macallan }
181 1.1 macallan
182 1.1 macallan void
183 1.1 macallan loongson_set_speed(int speed)
184 1.1 macallan {
185 1.1 macallan
186 1.1 macallan if ((speed < 1) || (speed > 7))
187 1.1 macallan return;
188 1.1 macallan sc_step_wanted = speed;
189 1.1 macallan DPRINTF("%s: %d\n", __func__, speed);
190 1.1 macallan }
191 1.1 macallan
192 1.1 macallan /*
193 1.1 macallan * the clock interrupt handler
194 1.1 macallan * we don't have a CPU clock independent, high resolution counter so we're
195 1.1 macallan * stuck with a PWM that can't count and a CP0 counter that slows down or
196 1.1 macallan * speeds up with the actual CPU speed. In order to still get halfway
197 1.1 macallan * accurate time we do the following:
198 1.1 macallan * - only change CPU speed in the timer interrupt
199 1.1 macallan * - each timer interrupt we measure how many CP0 cycles passed since last
200 1.1 macallan * time, adjust for CPU speed since we can be sure it didn't change, use
201 1.1 macallan * that to update a separate counter
202 1.1 macallan * - when reading the time counter we take the number of CP0 ticks since
203 1.1 macallan * the last timer interrupt, scale it to CPU clock, return that plus the
204 1.1 macallan * interrupt updated counter mentioned above to get something close to
205 1.1 macallan * CP0 running at full speed
206 1.1 macallan * - when changing CPU speed do it as close to taking the time from CP0 as
207 1.1 macallan * possible to keep the period of time we spend with CP0 running at the
208 1.1 macallan * wrong frequency as short as possible - hopefully short enough to stay
209 1.1 macallan * insignificant compared to other noise since switching speeds isn't
210 1.1 macallan * going to happen all that often
211 1.1 macallan */
212 1.1 macallan
213 1.1 macallan void
214 1.1 macallan mips3_clockintr(struct clockframe *cf)
215 1.1 macallan {
216 1.1 macallan uint32_t now, diff, next, new_cnt;
217 1.1 macallan
218 1.1 macallan /*
219 1.1 macallan * this looks kinda funny but what we want here is this:
220 1.1 macallan * - reading the counter and changing the CPU clock should be as
221 1.1 macallan * close together as possible in order to remain halfway accurate
222 1.1 macallan * - we need to use the previous sc_step in order to scale the
223 1.1 macallan * interval passed since the last clock interrupt correctly, so
224 1.1 macallan * we only change sc_step after doing that
225 1.1 macallan */
226 1.1 macallan if (sc_step_wanted != sc_step) {
227 1.1 macallan
228 1.1 macallan REGVAL(LS2F_CHIPCFG0) =
229 1.1 macallan (REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) |
230 1.1 macallan sc_step_wanted;
231 1.1 macallan }
232 1.1 macallan
233 1.1 macallan now = mips3_cp0_count_read();
234 1.1 macallan diff = now - sc_last;
235 1.1 macallan sc_count += scale(diff, sc_step);
236 1.1 macallan sc_last = now;
237 1.1 macallan if (sc_step_wanted != sc_step) {
238 1.1 macallan sc_step = sc_step_wanted;
239 1.1 macallan curcpu()->ci_cycles_per_hz = cycles[sc_step];
240 1.1 macallan }
241 1.1 macallan next = now + curcpu()->ci_cycles_per_hz;
242 1.1 macallan curcpu()->ci_ev_count_compare.ev_count++;
243 1.1 macallan
244 1.1 macallan mips3_cp0_compare_write(next);
245 1.1 macallan
246 1.1 macallan /* Check for lost clock interrupts */
247 1.1 macallan new_cnt = mips3_cp0_count_read();
248 1.1 macallan
249 1.1 macallan /*
250 1.1 macallan * Missed one or more clock interrupts, so let's start
251 1.1 macallan * counting again from the current value.
252 1.1 macallan */
253 1.1 macallan if ((next - new_cnt) & 0x80000000) {
254 1.1 macallan
255 1.1 macallan next = new_cnt + curcpu()->ci_cycles_per_hz;
256 1.1 macallan mips3_cp0_compare_write(next);
257 1.1 macallan curcpu()->ci_ev_count_compare_missed.ev_count++;
258 1.1 macallan }
259 1.1 macallan
260 1.1 macallan hardclock(cf);
261 1.1 macallan }
262 1.1 macallan
263 1.1 macallan static u_int
264 1.1 macallan get_loongson_timecount(struct timecounter *tc)
265 1.1 macallan {
266 1.1 macallan uint32_t now, diff;
267 1.1 macallan
268 1.1 macallan now = mips3_cp0_count_read();
269 1.1 macallan diff = now - sc_last;
270 1.1 macallan return sc_count + scale(diff, sc_step);
271 1.1 macallan }
272 1.1 macallan
273 1.1 macallan static int
274 1.1 macallan loongson_cpuspeed_temp(SYSCTLFN_ARGS)
275 1.1 macallan {
276 1.1 macallan struct sysctlnode node = *rnode;
277 1.1 macallan int mhz, i;
278 1.1 macallan
279 1.1 macallan mhz = sc_scale[sc_step_wanted];
280 1.1 macallan
281 1.1 macallan node.sysctl_data = &mhz;
282 1.1 macallan if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
283 1.1 macallan int new_reg;
284 1.1 macallan
285 1.1 macallan new_reg = *(int *)node.sysctl_data;
286 1.1 macallan i = 1;
287 1.1 macallan while ((i < 8) && (sc_scale[i] != new_reg))
288 1.1 macallan i++;
289 1.1 macallan if (i > 7)
290 1.1 macallan return EINVAL;
291 1.1 macallan loongson_set_speed(i);
292 1.1 macallan return 0;
293 1.1 macallan }
294 1.1 macallan return EINVAL;
295 1.1 macallan }
296 1.1 macallan
297 1.1 macallan static int
298 1.1 macallan loongson_cpuspeed_cur(SYSCTLFN_ARGS)
299 1.1 macallan {
300 1.1 macallan struct sysctlnode node = *rnode;
301 1.1 macallan int mhz;
302 1.1 macallan
303 1.1 macallan mhz = sc_scale[sc_step];
304 1.1 macallan node.sysctl_data = &mhz;
305 1.1 macallan return sysctl_lookup(SYSCTLFN_CALL(&node));
306 1.1 macallan }
307 1.1 macallan
308 1.1 macallan static int
309 1.1 macallan loongson_cpuspeed_available(SYSCTLFN_ARGS)
310 1.1 macallan {
311 1.1 macallan struct sysctlnode node = *rnode;
312 1.1 macallan char buf[128];
313 1.1 macallan
314 1.1 macallan snprintf(buf, 128, "%d %d %d %d %d %d %d", sc_scale[1],
315 1.1 macallan sc_scale[2], sc_scale[3], sc_scale[4],
316 1.1 macallan sc_scale[5], sc_scale[6], sc_scale[7]);
317 1.1 macallan node.sysctl_data = buf;
318 1.1 macallan return(sysctl_lookup(SYSCTLFN_CALL(&node)));
319 1.1 macallan }
320 1.1 macallan
321 1.1 macallan /*
322 1.1 macallan * Wait for at least "n" microseconds.
323 1.1 macallan */
324 1.1 macallan void
325 1.1 macallan loongson_delay(int n)
326 1.1 macallan {
327 1.1 macallan u_long divisor_delay;
328 1.1 macallan uint32_t cur, last, delta, usecs;
329 1.1 macallan
330 1.1 macallan last = mips3_cp0_count_read();
331 1.1 macallan delta = usecs = 0;
332 1.1 macallan
333 1.1 macallan divisor_delay = rscale(curcpu()->ci_divisor_delay, sc_step);
334 1.1 macallan if (divisor_delay == 0) {
335 1.1 macallan /*
336 1.1 macallan * Frequency values in curcpu() are not initialized.
337 1.1 macallan * Assume faster frequency since longer delays are harmless.
338 1.1 macallan * Note CPU_MIPS_DOUBLE_COUNT is ignored here.
339 1.1 macallan */
340 1.1 macallan #define FAST_FREQ (300 * 1000 * 1000) /* fast enough? */
341 1.1 macallan divisor_delay = FAST_FREQ / (1000 * 1000);
342 1.1 macallan }
343 1.1 macallan
344 1.1 macallan while (n > usecs) {
345 1.1 macallan cur = mips3_cp0_count_read();
346 1.1 macallan
347 1.1 macallan /*
348 1.1 macallan * The MIPS3 CP0 counter always counts upto UINT32_MAX,
349 1.1 macallan * so no need to check wrapped around case.
350 1.1 macallan */
351 1.1 macallan delta += (cur - last);
352 1.1 macallan
353 1.1 macallan last = cur;
354 1.1 macallan
355 1.1 macallan while (delta >= divisor_delay) {
356 1.1 macallan /*
357 1.1 macallan * delta is not so larger than divisor_delay here,
358 1.1 macallan * and using DIV/DIVU ops could be much slower.
359 1.1 macallan * (though longer delay may be harmless)
360 1.1 macallan */
361 1.1 macallan usecs++;
362 1.1 macallan delta -= divisor_delay;
363 1.1 macallan }
364 1.1 macallan }
365 1.1 macallan }
366 1.1 macallan
367 1.1 macallan SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
368 1.1 macallan {
369 1.1 macallan
370 1.1 macallan sysctl_createv(NULL, 0, NULL, NULL,
371 1.1 macallan CTLFLAG_PERMANENT,
372 1.1 macallan CTLTYPE_NODE, "machdep", NULL,
373 1.1 macallan NULL, 0, NULL, 0,
374 1.1 macallan CTL_MACHDEP, CTL_EOL);
375 1.1 macallan }
376 1.1 macallan
377 1.1 macallan /*
378 1.1 macallan * We assume newhz is either stathz or profhz, and that neither will
379 1.1 macallan * change after being set up above. Could recalculate intervals here
380 1.1 macallan * but that would be a drag.
381 1.1 macallan */
382 1.1 macallan void
383 1.1 macallan loongson_setstatclockrate(int newhz)
384 1.1 macallan {
385 1.1 macallan
386 1.1 macallan /* nothing we can do */
387 1.1 macallan }
388 1.1 macallan
389 1.1 macallan __weak_alias(setstatclockrate, loongson_setstatclockrate);
390 1.1 macallan __weak_alias(cpu_initclocks, loongson_initclocks);
391 1.1 macallan __weak_alias(delay, loongson_delay);