]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/pc98/cbus/clock.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.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/smp.h>
63 #include <sys/sysctl.h>
64
65 #include <machine/clock.h>
66 #include <machine/cpu.h>
67 #include <machine/frame.h>
68 #include <machine/intr_machdep.h>
69 #include <machine/md_var.h>
70 #ifdef DEV_APIC
71 #include <machine/apicvar.h>
72 #endif
73 #include <machine/ppireg.h>
74 #include <machine/timerreg.h>
75 #include <machine/smp.h>
76
77 #include <pc98/pc98/pc98_machdep.h>
78 #ifdef DEV_ISA
79 #include <pc98/cbus/cbus.h>
80 #include <isa/isavar.h>
81 #endif
82
83 #ifdef KDTRACE_HOOKS
84 #include <sys/dtrace_bsd.h>
85 #endif
86
87 #define TIMER_DIV(x) ((i8254_freq + (x) / 2) / (x))
88
89 int     clkintr_pending;
90 #ifndef TIMER_FREQ
91 #define TIMER_FREQ   2457600
92 #endif
93 u_int   i8254_freq = TIMER_FREQ;
94 TUNABLE_INT("hw.i8254.freq", &i8254_freq);
95 int     i8254_max_count;
96 static int i8254_real_max_count;
97
98 static  struct mtx clock_lock;
99 static  struct intsrc *i8254_intsrc;
100 static  u_int32_t i8254_lastcount;
101 static  u_int32_t i8254_offset;
102 static  int     (*i8254_pending)(struct intsrc *);
103 static  int     i8254_ticked;
104 static  int     using_lapic_timer;
105
106 /* Values for timerX_state: */
107 #define RELEASED        0
108 #define RELEASE_PENDING 1
109 #define ACQUIRED        2
110 #define ACQUIRE_PENDING 3
111
112 static  u_char  timer1_state;
113
114 static  unsigned i8254_get_timecount(struct timecounter *tc);
115 static  unsigned i8254_simple_get_timecount(struct timecounter *tc);
116 static  void    set_i8254_freq(u_int freq, int intr_freq);
117
118 static struct timecounter i8254_timecounter = {
119         i8254_get_timecount,    /* get_timecount */
120         0,                      /* no poll_pps */
121         ~0u,                    /* counter_mask */
122         0,                      /* frequency */
123         "i8254",                /* name */
124         0                       /* quality */
125 };
126
127 int
128 hardclockintr(struct trapframe *frame)
129 {
130
131         if (PCPU_GET(cpuid) == 0)
132                 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
133         else
134                 hardclock_cpu(TRAPF_USERMODE(frame));
135         return (FILTER_HANDLED);
136 }
137
138 int
139 statclockintr(struct trapframe *frame)
140 {
141
142         return (FILTER_HANDLED);
143 }
144
145 int
146 profclockintr(struct trapframe *frame)
147 {
148
149         return (FILTER_HANDLED);
150 }
151
152 static int
153 clkintr(struct trapframe *frame)
154 {
155
156         if (timecounter->tc_get_timecount == i8254_get_timecount) {
157                 mtx_lock_spin(&clock_lock);
158                 if (i8254_ticked)
159                         i8254_ticked = 0;
160                 else {
161                         i8254_offset += i8254_max_count;
162                         i8254_lastcount = 0;
163                 }
164                 clkintr_pending = 0;
165                 mtx_unlock_spin(&clock_lock);
166         }
167         KASSERT(!using_lapic_timer, ("clk interrupt enabled with lapic timer"));
168
169 #ifdef KDTRACE_HOOKS
170         /*
171          * If the DTrace hooks are configured and a callback function
172          * has been registered, then call it to process the high speed
173          * timers.
174          */
175         int cpu = PCPU_GET(cpuid);
176         if (lapic_cyclic_clock_func[cpu] != NULL)
177                 (*lapic_cyclic_clock_func[cpu])(frame);
178 #endif
179
180 #ifdef SMP
181         if (smp_started)
182                 ipi_all_but_self(IPI_HARDCLOCK);
183 #endif 
184         hardclockintr(frame);
185         return (FILTER_HANDLED);
186 }
187
188 int
189 timer_spkr_acquire(void)
190 {
191         int mode;
192
193         mode = TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT;
194
195         if (timer1_state != RELEASED)
196                 return (-1);
197         timer1_state = ACQUIRED;
198
199         /*
200          * This access to the timer registers is as atomic as possible
201          * because it is a single instruction.  We could do better if we
202          * knew the rate.  Use of splclock() limits glitches to 10-100us,
203          * and this is probably good enough for timer2, so we aren't as
204          * careful with it as with timer0.
205          */
206         outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f));
207         ppi_spkr_on();          /* enable counter1 output to speaker */
208
209         return (0);
210 }
211
212 int
213 timer_spkr_release(void)
214 {
215
216         if (timer1_state != ACQUIRED)
217                 return (-1);
218         timer1_state = RELEASED;
219         outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT);
220         ppi_spkr_off();         /* disable counter1 output to speaker */
221         return (0);
222 }
223
224 void
225 timer_spkr_setfreq(int freq)
226 {
227
228         freq = i8254_freq / freq;
229         mtx_lock_spin(&clock_lock);
230         outb(TIMER_CNTR1, (freq) & 0xff);
231         outb(TIMER_CNTR1, (freq) >> 8);
232         mtx_unlock_spin(&clock_lock);
233 }
234
235
236 static int
237 getit(void)
238 {
239         int high, low;
240
241         mtx_lock_spin(&clock_lock);
242
243         /* Select timer0 and latch counter value. */
244         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
245
246         low = inb(TIMER_CNTR0);
247         high = inb(TIMER_CNTR0);
248
249         mtx_unlock_spin(&clock_lock);
250         return ((high << 8) | low);
251 }
252
253 /*
254  * Wait "n" microseconds.
255  * Relies on timer 1 counting down from (i8254_freq / hz)
256  * Note: timer had better have been programmed before this is first used!
257  */
258 void
259 DELAY(int n)
260 {
261         int delta, prev_tick, tick, ticks_left;
262
263 #ifdef DELAYDEBUG
264         int getit_calls = 1;
265         int n1;
266         static int state = 0;
267
268         if (state == 0) {
269                 state = 1;
270                 for (n1 = 1; n1 <= 10000000; n1 *= 10)
271                         DELAY(n1);
272                 state = 2;
273         }
274         if (state == 1)
275                 printf("DELAY(%d)...", n);
276 #endif
277         /*
278          * Read the counter first, so that the rest of the setup overhead is
279          * counted.  Guess the initial overhead is 20 usec (on most systems it
280          * takes about 1.5 usec for each of the i/o's in getit().  The loop
281          * takes about 6 usec on a 486/33 and 13 usec on a 386/20.  The
282          * multiplications and divisions to scale the count take a while).
283          *
284          * However, if ddb is active then use a fake counter since reading
285          * the i8254 counter involves acquiring a lock.  ddb must not do
286          * locking for many reasons, but it calls here for at least atkbd
287          * input.
288          */
289 #ifdef KDB
290         if (kdb_active)
291                 prev_tick = 1;
292         else
293 #endif
294                 prev_tick = getit();
295         n -= 0;                 /* XXX actually guess no initial overhead */
296         /*
297          * Calculate (n * (i8254_freq / 1e6)) without using floating point
298          * and without any avoidable overflows.
299          */
300         if (n <= 0)
301                 ticks_left = 0;
302         else if (n < 256)
303                 /*
304                  * Use fixed point to avoid a slow division by 1000000.
305                  * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
306                  * 2^15 is the first power of 2 that gives exact results
307                  * for n between 0 and 256.
308                  */
309                 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
310         else
311                 /*
312                  * Don't bother using fixed point, although gcc-2.7.2
313                  * generates particularly poor code for the long long
314                  * division, since even the slow way will complete long
315                  * before the delay is up (unless we're interrupted).
316                  */
317                 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
318                              / 1000000;
319
320         while (ticks_left > 0) {
321 #ifdef KDB
322                 if (kdb_active) {
323                         outb(0x5f, 0);
324                         tick = prev_tick - 1;
325                         if (tick <= 0)
326                                 tick = i8254_max_count;
327                 } else
328 #endif
329                         tick = getit();
330 #ifdef DELAYDEBUG
331                 ++getit_calls;
332 #endif
333                 delta = prev_tick - tick;
334                 prev_tick = tick;
335                 if (delta < 0) {
336                         delta += i8254_max_count;
337                         /*
338                          * Guard against i8254_max_count being wrong.
339                          * This shouldn't happen in normal operation,
340                          * but it may happen if set_i8254_freq() is
341                          * traced.
342                          */
343                         if (delta < 0)
344                                 delta = 0;
345                 }
346                 ticks_left -= delta;
347         }
348 #ifdef DELAYDEBUG
349         if (state == 1)
350                 printf(" %d calls to getit() at %d usec each\n",
351                        getit_calls, (n + 5) / getit_calls);
352 #endif
353 }
354
355 static void
356 set_i8254_freq(u_int freq, int intr_freq)
357 {
358         int new_i8254_real_max_count;
359
360         i8254_timecounter.tc_frequency = freq;
361         mtx_lock_spin(&clock_lock);
362         i8254_freq = freq;
363         if (using_lapic_timer)
364                 new_i8254_real_max_count = 0x10000;
365         else
366                 new_i8254_real_max_count = TIMER_DIV(intr_freq);
367         if (new_i8254_real_max_count != i8254_real_max_count) {
368                 i8254_real_max_count = new_i8254_real_max_count;
369                 if (i8254_real_max_count == 0x10000)
370                         i8254_max_count = 0xffff;
371                 else
372                         i8254_max_count = i8254_real_max_count;
373                 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
374                 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
375                 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
376         }
377         mtx_unlock_spin(&clock_lock);
378 }
379
380 static void
381 i8254_restore(void)
382 {
383
384         mtx_lock_spin(&clock_lock);
385         outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
386         outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
387         outb(TIMER_CNTR0, i8254_real_max_count >> 8);
388         mtx_unlock_spin(&clock_lock);
389 }
390
391 /*
392  * Restore all the timers non-atomically (XXX: should be atomically).
393  *
394  * This function is called from pmtimer_resume() to restore all the timers.
395  * This should not be necessary, but there are broken laptops that do not
396  * restore all the timers on resume.
397  */
398 void
399 timer_restore(void)
400 {
401
402         i8254_restore();                /* restore i8254_freq and hz */
403 }
404
405 /* This is separate from startrtclock() so that it can be called early. */
406 void
407 i8254_init(void)
408 {
409
410         mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
411
412         if (pc98_machine_type & M_8M)
413                 i8254_freq = 1996800L; /* 1.9968 MHz */
414         else
415                 i8254_freq = 2457600L; /* 2.4576 MHz */
416
417         set_i8254_freq(i8254_freq, hz);
418 }
419
420 void
421 startrtclock()
422 {
423
424         set_i8254_freq(i8254_freq, hz);
425         tc_init(&i8254_timecounter);
426
427         init_TSC();
428 }
429
430 /*
431  * Start both clocks running.
432  */
433 void
434 cpu_initclocks()
435 {
436
437 #ifdef DEV_APIC
438         using_lapic_timer = lapic_setup_clock();
439 #endif
440         /*
441          * If we aren't using the local APIC timer to drive the kernel
442          * clocks, setup the interrupt handler for the 8254 timer 0 so
443          * that it can drive hardclock().  Otherwise, change the 8254
444          * timecounter to user a simpler algorithm.
445          */
446         if (!using_lapic_timer) {
447                 intr_add_handler("clk", 0, (driver_filter_t *)clkintr, NULL,
448                     NULL, INTR_TYPE_CLK, NULL);
449                 i8254_intsrc = intr_lookup_source(0);
450                 if (i8254_intsrc != NULL)
451                         i8254_pending =
452                             i8254_intsrc->is_pic->pic_source_pending;
453         } else {
454                 i8254_timecounter.tc_get_timecount =
455                     i8254_simple_get_timecount;
456                 i8254_timecounter.tc_counter_mask = 0xffff;
457                 set_i8254_freq(i8254_freq, hz);
458         }
459
460         init_TSC_tc();
461 }
462
463 void
464 cpu_startprofclock(void)
465 {
466 }
467
468 void
469 cpu_stopprofclock(void)
470 {
471 }
472
473 static int
474 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
475 {
476         int error;
477         u_int freq;
478
479         /*
480          * Use `i8254' instead of `timer' in external names because `timer'
481          * is is too generic.  Should use it everywhere.
482          */
483         freq = i8254_freq;
484         error = sysctl_handle_int(oidp, &freq, 0, req);
485         if (error == 0 && req->newptr != NULL)
486                 set_i8254_freq(freq, hz);
487         return (error);
488 }
489
490 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
491     0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", "");
492
493 static unsigned
494 i8254_simple_get_timecount(struct timecounter *tc)
495 {
496
497         return (i8254_max_count - getit());
498 }
499
500 static unsigned
501 i8254_get_timecount(struct timecounter *tc)
502 {
503         u_int count;
504         u_int high, low;
505         u_int eflags;
506
507         eflags = read_eflags();
508         mtx_lock_spin(&clock_lock);
509
510         /* Select timer0 and latch counter value. */
511         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
512
513         low = inb(TIMER_CNTR0);
514         high = inb(TIMER_CNTR0);
515         count = i8254_max_count - ((high << 8) | low);
516         if (count < i8254_lastcount ||
517             (!i8254_ticked && (clkintr_pending ||
518             ((count < 20 || (!(eflags & PSL_I) &&
519             count < i8254_max_count / 2u)) &&
520             i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
521                 i8254_ticked = 1;
522                 i8254_offset += i8254_max_count;
523         }
524         i8254_lastcount = count;
525         count += i8254_offset;
526         mtx_unlock_spin(&clock_lock);
527         return (count);
528 }
529
530 #ifdef DEV_ISA
531 /*
532  * Attach to the ISA PnP descriptors for the timer
533  */
534 static struct isa_pnp_id attimer_ids[] = {
535         { 0x0001d041 /* PNP0100 */, "AT timer" },
536         { 0 }
537 };
538
539 static int
540 attimer_probe(device_t dev)
541 {
542         int result;
543         
544         result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
545         if (result <= 0)
546                 device_quiet(dev);
547         return(result);
548 }
549
550 static int
551 attimer_attach(device_t dev)
552 {
553         return(0);
554 }
555
556 static device_method_t attimer_methods[] = {
557         /* Device interface */
558         DEVMETHOD(device_probe,         attimer_probe),
559         DEVMETHOD(device_attach,        attimer_attach),
560         DEVMETHOD(device_detach,        bus_generic_detach),
561         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
562         DEVMETHOD(device_suspend,       bus_generic_suspend),
563         DEVMETHOD(device_resume,        bus_generic_resume),
564         { 0, 0 }
565 };
566
567 static driver_t attimer_driver = {
568         "attimer",
569         attimer_methods,
570         1,              /* no softc */
571 };
572
573 static devclass_t attimer_devclass;
574
575 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);
576
577 #endif /* DEV_ISA */