1 // io/timer/i8254.cc -- Intel 8254 and compatible timer driver
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following conditions:
12 // * Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimers.
15 // * Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimers in the
17 // documentation and/or other materials provided with the distribution.
19 // * The names of the Software's authors and/or contributors
20 // may not be used to endorse or promote products derived from
21 // this Software without specific prior written permission.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
31 #include <kern/time.h>
33 #include <lowlevel/clock.h>
38 using Time::monotonic_clock;
42 class I8254 : public HWTimer {
49 u16 ret = ll_in_8(0x40);
50 ret |= (u16)ll_in_8(0x40) << 8;
60 } while (val < 0x8000);
64 } while (val > 0x8000);
81 s64 start_tick = ll_getclock();
84 s64 end_tick = ll_getclock();
86 s64 ticks_per_second = (end_tick - start_tick) *
87 (u64)clock_freq / 0x20000;
89 monotonic_clock.calibrate(ticks_per_second);
92 void arm(Time new_expiry)
94 // Interrupts should always be disabled when
97 Lock::AutoSpinLock autolock(lock);
99 // The 8254 is often slow to program, so don't re-program if the
100 // expiry is the same. To accomodate this, the tick timer must
101 // be set to go off often enough that the expiry is never more
102 // than 0xffff 8254 ticks (about 1/18.2 sec) in the future.
104 if (armed && expiry != new_expiry) {
106 monotonic_clock.get_time(&now);
107 Time rel_expiry = new_expiry - now;
112 assert(rel_expiry.seconds <= 0);
114 if (rel_expiry.seconds < 0)
117 // Add one extra tick to make sure we round up rather than
118 // down; otherwise, we'll just end up programming the timer
119 // for one tick and trying again.
121 ticks = rel_expiry.nanos * (u64)clock_freq /
124 assert(ticks <= 0xffff);
131 ll_out_8(0x43, 0x30);
133 ll_out_8(0x40, ticks & 0xff);
134 ll_out_8(0x40, ticks >> 8);