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