]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/pc98/cbus/clock.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / pc98 / cbus / clock.c
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz and Don Ahn.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from: @(#)clock.c       7.2 (Berkeley) 5/12/91
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 /*
39  * Routines to handle clock hardware.
40  */
41
42 /*
43  * modified for PC98 by Kakefuda
44  */
45
46 #include "opt_apic.h"
47 #include "opt_clock.h"
48 #include "opt_kdtrace.h"
49 #include "opt_isa.h"
50 #include "opt_mca.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/bus.h>
55 #include <sys/lock.h>
56 #include <sys/kdb.h>
57 #include <sys/mutex.h>
58 #include <sys/proc.h>
59 #include <sys/timetc.h>
60 #include <sys/kernel.h>
61 #include <sys/module.h>
62 #include <sys/sysctl.h>
63
64 #include <machine/clock.h>
65 #include <machine/cpu.h>
66 #include <machine/frame.h>
67 #include <machine/intr_machdep.h>
68 #include <machine/md_var.h>
69 #ifdef DEV_APIC
70 #include <machine/apicvar.h>
71 #endif
72 #include <machine/ppireg.h>
73 #include <machine/timerreg.h>
74
75 #include <pc98/pc98/pc98_machdep.h>
76 #ifdef DEV_ISA
77 #include <pc98/cbus/cbus.h>
78 #include <isa/isavar.h>
79 #endif
80
81 #ifdef KDTRACE_HOOKS
82 #include <sys/dtrace_bsd.h>
83 #endif
84
85 #define TIMER_DIV(x) ((i8254_freq + (x) / 2) / (x))
86
87 int     clkintr_pending;
88 int     statclock_disable;
89 #ifndef TIMER_FREQ
90 #define TIMER_FREQ   2457600
91 #endif
92 u_int   i8254_freq = TIMER_FREQ;
93 TUNABLE_INT("hw.i8254.freq", &i8254_freq);
94 int     i8254_max_count;
95 static int i8254_real_max_count;
96
97 static  struct mtx clock_lock;
98 static  struct intsrc *i8254_intsrc;
99 static  u_int32_t i8254_lastcount;
100 static  u_int32_t i8254_offset;
101 static  int     (*i8254_pending)(struct intsrc *);
102 static  int     i8254_ticked;
103 static  int     using_lapic_timer;
104
105 /* Values for timerX_state: */
106 #define RELEASED        0
107 #define RELEASE_PENDING 1
108 #define ACQUIRED        2
109 #define ACQUIRE_PENDING 3
110
111 static  u_char  timer1_state;
112
113 static  unsigned i8254_get_timecount(struct timecounter *tc);
114 static  unsigned i8254_simple_get_timecount(struct timecounter *tc);
115 static  void    set_i8254_freq(u_int freq, int intr_freq);
116
117 static struct timecounter i8254_timecounter = {
118         i8254_get_timecount,    /* get_timecount */
119         0,                      /* no poll_pps */
120         ~0u,                    /* counter_mask */
121         0,                      /* frequency */
122         "i8254",                /* name */
123         0                       /* quality */
124 };
125
126 static int
127 clkintr(struct trapframe *frame)
128 {
129
130         if (timecounter->tc_get_timecount == i8254_get_timecount) {
131                 mtx_lock_spin(&clock_lock);
132                 if (i8254_ticked)
133                         i8254_ticked = 0;
134                 else {
135                         i8254_offset += i8254_max_count;
136                         i8254_lastcount = 0;
137                 }
138                 clkintr_pending = 0;
139                 mtx_unlock_spin(&clock_lock);
140         }
141         KASSERT(!using_lapic_timer, ("clk interrupt enabled with lapic timer"));
142
143 #ifdef KDTRACE_HOOKS
144         /*
145          * If the DTrace hooks are configured and a callback function
146          * has been registered, then call it to process the high speed
147          * timers.
148          */
149         int cpu = PCPU_GET(cpuid);
150         if (lapic_cyclic_clock_func[cpu] != NULL)
151                 (*lapic_cyclic_clock_func[cpu])(frame);
152 #endif
153
154         hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
155         return (FILTER_HANDLED);
156 }
157
158 int
159 timer_spkr_acquire(void)
160 {
161         int mode;
162
163         mode = TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT;
164
165         if (timer1_state != RELEASED)
166                 return (-1);
167         timer1_state = ACQUIRED;
168
169         /*
170          * This access to the timer registers is as atomic as possible
171          * because it is a single instruction.  We could do better if we
172          * knew the rate.  Use of splclock() limits glitches to 10-100us,
173          * and this is probably good enough for timer2, so we aren't as
174          * careful with it as with timer0.
175          */
176         outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f));
177         ppi_spkr_on();          /* enable counter1 output to speaker */
178
179         return (0);
180 }
181
182 int
183 timer_spkr_release(void)
184 {
185
186         if (timer1_state != ACQUIRED)
187                 return (-1);
188         timer1_state = RELEASED;
189         outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT);
190         ppi_spkr_off();         /* disable counter1 output to speaker */
191         return (0);
192 }
193
194 void
195 timer_spkr_setfreq(int freq)
196 {
197
198         freq = i8254_freq / freq;
199         mtx_lock_spin(&clock_lock);
200         outb(TIMER_CNTR1, (freq) & 0xff);
201         outb(TIMER_CNTR1, (freq) >> 8);
202         mtx_unlock_spin(&clock_lock);
203 }
204
205
206 static int
207 getit(void)
208 {
209         int high, low;
210
211         mtx_lock_spin(&clock_lock);
212
213         /* Select timer0 and latch counter value. */
214         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
215
216         low = inb(TIMER_CNTR0);
217         high = inb(TIMER_CNTR0);
218
219         mtx_unlock_spin(&clock_lock);
220         return ((high << 8) | low);
221 }
222
223 /*
224  * Wait "n" microseconds.
225  * Relies on timer 1 counting down from (i8254_freq / hz)
226  * Note: timer had better have been programmed before this is first used!
227  */
228 void
229 DELAY(int n)
230 {
231         int delta, prev_tick, tick, ticks_left;
232
233 #ifdef DELAYDEBUG
234         int getit_calls = 1;
235         int n1;
236         static int state = 0;
237
238         if (state == 0) {
239                 state = 1;
240                 for (n1 = 1; n1 <= 10000000; n1 *= 10)
241                         DELAY(n1);
242                 state = 2;
243         }
244         if (state == 1)
245                 printf("DELAY(%d)...", n);
246 #endif
247         /*
248          * Read the counter first, so that the rest of the setup overhead is
249          * counted.  Guess the initial overhead is 20 usec (on most systems it
250          * takes about 1.5 usec for each of the i/o's in getit().  The loop
251          * takes about 6 usec on a 486/33 and 13 usec on a 386/20.  The
252          * multiplications and divisions to scale the count take a while).
253          *
254          * However, if ddb is active then use a fake counter since reading
255          * the i8254 counter involves acquiring a lock.  ddb must not do
256          * locking for many reasons, but it calls here for at least atkbd
257          * input.
258          */
259 #ifdef KDB
260         if (kdb_active)
261                 prev_tick = 1;
262         else
263 #endif
264                 prev_tick = getit();
265         n -= 0;                 /* XXX actually guess no initial overhead */
266         /*
267          * Calculate (n * (i8254_freq / 1e6)) without using floating point
268          * and without any avoidable overflows.
269          */
270         if (n <= 0)
271                 ticks_left = 0;
272         else if (n < 256)
273                 /*
274                  * Use fixed point to avoid a slow division by 1000000.
275                  * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
276                  * 2^15 is the first power of 2 that gives exact results
277                  * for n between 0 and 256.
278                  */
279                 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
280         else
281                 /*
282                  * Don't bother using fixed point, although gcc-2.7.2
283                  * generates particularly poor code for the long long
284                  * division, since even the slow way will complete long
285                  * before the delay is up (unless we're interrupted).
286                  */
287                 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
288                              / 1000000;
289
290         while (ticks_left > 0) {
291 #ifdef KDB
292                 if (kdb_active) {
293                         outb(0x5f, 0);
294                         tick = prev_tick - 1;
295                         if (tick <= 0)
296                                 tick = i8254_max_count;
297                 } else
298 #endif
299                         tick = getit();
300 #ifdef DELAYDEBUG
301                 ++getit_calls;
302 #endif
303                 delta = prev_tick - tick;
304                 prev_tick = tick;
305                 if (delta < 0) {
306                         delta += i8254_max_count;
307                         /*
308                          * Guard against i8254_max_count being wrong.
309                          * This shouldn't happen in normal operation,
310                          * but it may happen if set_i8254_freq() is
311                          * traced.
312                          */
313                         if (delta < 0)
314                                 delta = 0;
315                 }
316                 ticks_left -= delta;
317         }
318 #ifdef DELAYDEBUG
319         if (state == 1)
320                 printf(" %d calls to getit() at %d usec each\n",
321                        getit_calls, (n + 5) / getit_calls);
322 #endif
323 }
324
325 static void
326 set_i8254_freq(u_int freq, int intr_freq)
327 {
328         int new_i8254_real_max_count;
329
330         i8254_timecounter.tc_frequency = freq;
331         mtx_lock_spin(&clock_lock);
332         i8254_freq = freq;
333         if (using_lapic_timer)
334                 new_i8254_real_max_count = 0x10000;
335         else
336                 new_i8254_real_max_count = TIMER_DIV(intr_freq);
337         if (new_i8254_real_max_count != i8254_real_max_count) {
338                 i8254_real_max_count = new_i8254_real_max_count;
339                 if (i8254_real_max_count == 0x10000)
340                         i8254_max_count = 0xffff;
341                 else
342                         i8254_max_count = i8254_real_max_count;
343                 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
344                 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
345                 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
346         }
347         mtx_unlock_spin(&clock_lock);
348 }
349
350 static void
351 i8254_restore(void)
352 {
353
354         mtx_lock_spin(&clock_lock);
355         outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
356         outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
357         outb(TIMER_CNTR0, i8254_real_max_count >> 8);
358         mtx_unlock_spin(&clock_lock);
359 }
360
361 /*
362  * Restore all the timers non-atomically (XXX: should be atomically).
363  *
364  * This function is called from pmtimer_resume() to restore all the timers.
365  * This should not be necessary, but there are broken laptops that do not
366  * restore all the timers on resume.
367  */
368 void
369 timer_restore(void)
370 {
371
372         i8254_restore();                /* restore i8254_freq and hz */
373 }
374
375 /* This is separate from startrtclock() so that it can be called early. */
376 void
377 i8254_init(void)
378 {
379
380         mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
381
382         if (pc98_machine_type & M_8M)
383                 i8254_freq = 1996800L; /* 1.9968 MHz */
384         else
385                 i8254_freq = 2457600L; /* 2.4576 MHz */
386
387         set_i8254_freq(i8254_freq, hz);
388 }
389
390 void
391 startrtclock()
392 {
393
394         set_i8254_freq(i8254_freq, hz);
395         tc_init(&i8254_timecounter);
396
397         init_TSC();
398 }
399
400 /*
401  * Start both clocks running.
402  */
403 void
404 cpu_initclocks()
405 {
406
407 #ifdef DEV_APIC
408         using_lapic_timer = lapic_setup_clock();
409 #endif
410         /*
411          * If we aren't using the local APIC timer to drive the kernel
412          * clocks, setup the interrupt handler for the 8254 timer 0 so
413          * that it can drive hardclock().  Otherwise, change the 8254
414          * timecounter to user a simpler algorithm.
415          */
416         if (!using_lapic_timer) {
417                 intr_add_handler("clk", 0, (driver_filter_t *)clkintr, NULL,
418                     NULL, INTR_TYPE_CLK, NULL);
419                 i8254_intsrc = intr_lookup_source(0);
420                 if (i8254_intsrc != NULL)
421                         i8254_pending =
422                             i8254_intsrc->is_pic->pic_source_pending;
423         } else {
424                 i8254_timecounter.tc_get_timecount =
425                     i8254_simple_get_timecount;
426                 i8254_timecounter.tc_counter_mask = 0xffff;
427                 set_i8254_freq(i8254_freq, hz);
428         }
429
430         init_TSC_tc();
431 }
432
433 void
434 cpu_startprofclock(void)
435 {
436 }
437
438 void
439 cpu_stopprofclock(void)
440 {
441 }
442
443 static int
444 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
445 {
446         int error;
447         u_int freq;
448
449         /*
450          * Use `i8254' instead of `timer' in external names because `timer'
451          * is is too generic.  Should use it everywhere.
452          */
453         freq = i8254_freq;
454         error = sysctl_handle_int(oidp, &freq, 0, req);
455         if (error == 0 && req->newptr != NULL)
456                 set_i8254_freq(freq, hz);
457         return (error);
458 }
459
460 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
461     0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", "");
462
463 static unsigned
464 i8254_simple_get_timecount(struct timecounter *tc)
465 {
466
467         return (i8254_max_count - getit());
468 }
469
470 static unsigned
471 i8254_get_timecount(struct timecounter *tc)
472 {
473         u_int count;
474         u_int high, low;
475         u_int eflags;
476
477         eflags = read_eflags();
478         mtx_lock_spin(&clock_lock);
479
480         /* Select timer0 and latch counter value. */
481         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
482
483         low = inb(TIMER_CNTR0);
484         high = inb(TIMER_CNTR0);
485         count = i8254_max_count - ((high << 8) | low);
486         if (count < i8254_lastcount ||
487             (!i8254_ticked && (clkintr_pending ||
488             ((count < 20 || (!(eflags & PSL_I) &&
489             count < i8254_max_count / 2u)) &&
490             i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
491                 i8254_ticked = 1;
492                 i8254_offset += i8254_max_count;
493         }
494         i8254_lastcount = count;
495         count += i8254_offset;
496         mtx_unlock_spin(&clock_lock);
497         return (count);
498 }
499
500 #ifdef DEV_ISA
501 /*
502  * Attach to the ISA PnP descriptors for the timer
503  */
504 static struct isa_pnp_id attimer_ids[] = {
505         { 0x0001d041 /* PNP0100 */, "AT timer" },
506         { 0 }
507 };
508
509 static int
510 attimer_probe(device_t dev)
511 {
512         int result;
513         
514         result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
515         if (result <= 0)
516                 device_quiet(dev);
517         return(result);
518 }
519
520 static int
521 attimer_attach(device_t dev)
522 {
523         return(0);
524 }
525
526 static device_method_t attimer_methods[] = {
527         /* Device interface */
528         DEVMETHOD(device_probe,         attimer_probe),
529         DEVMETHOD(device_attach,        attimer_attach),
530         DEVMETHOD(device_detach,        bus_generic_detach),
531         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
532         DEVMETHOD(device_suspend,       bus_generic_suspend),
533         DEVMETHOD(device_resume,        bus_generic_resume),
534         { 0, 0 }
535 };
536
537 static driver_t attimer_driver = {
538         "attimer",
539         attimer_methods,
540         1,              /* no softc */
541 };
542
543 static devclass_t attimer_devclass;
544
545 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);
546
547 #endif /* DEV_ISA */