1 // Internal timer and Intel 8253 Programmable Interrupt Timer (PIT) support.
3 // Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "biosvar.h" // GET_LOW
8 #include "config.h" // CONFIG_*
9 #include "output.h" // dprintf
10 #include "stacks.h" // yield
11 #include "util.h" // timer_setup
12 #include "x86.h" // cpuid
14 #define PORT_PIT_COUNTER0 0x0040
15 #define PORT_PIT_COUNTER1 0x0041
16 #define PORT_PIT_COUNTER2 0x0042
17 #define PORT_PIT_MODE 0x0043
18 #define PORT_PS2_CTRLB 0x0061
20 // Bits for PORT_PIT_MODE
21 #define PM_SEL_TIMER0 (0<<6)
22 #define PM_SEL_TIMER1 (1<<6)
23 #define PM_SEL_TIMER2 (2<<6)
24 #define PM_SEL_READBACK (3<<6)
25 #define PM_ACCESS_LATCH (0<<4)
26 #define PM_ACCESS_LOBYTE (1<<4)
27 #define PM_ACCESS_HIBYTE (2<<4)
28 #define PM_ACCESS_WORD (3<<4)
29 #define PM_MODE0 (0<<1)
30 #define PM_MODE1 (1<<1)
31 #define PM_MODE2 (2<<1)
32 #define PM_MODE3 (3<<1)
33 #define PM_MODE4 (4<<1)
34 #define PM_MODE5 (5<<1)
35 #define PM_CNT_BINARY (0<<0)
36 #define PM_CNT_BCD (1<<0)
37 #define PM_READ_COUNTER0 (1<<1)
38 #define PM_READ_COUNTER1 (1<<2)
39 #define PM_READ_COUNTER2 (1<<3)
40 #define PM_READ_STATUSVALUE (0<<4)
41 #define PM_READ_VALUE (1<<4)
42 #define PM_READ_STATUS (2<<4)
44 // Bits for PORT_PS2_CTRLB
45 #define PPCB_T2GATE (1<<0)
46 #define PPCB_SPKR (1<<1)
47 #define PPCB_T2OUT (1<<5)
49 #define PMTIMER_HZ 3579545 // Underlying Hz of the PM Timer
50 #define PMTIMER_TO_PIT 3 // Ratio of pmtimer rate to pit rate
53 u16 TimerPort VARFSEG;
57 /****************************************************************
58 * Internal timer setup
59 ****************************************************************/
61 #define CALIBRATE_COUNT 0x800 // Approx 1.7ms
63 // Calibrate the CPU time-stamp-counter
68 u8 orig = inb(PORT_PS2_CTRLB);
69 outb((orig & ~PPCB_SPKR) | PPCB_T2GATE, PORT_PS2_CTRLB);
70 /* binary, mode 0, LSB/MSB, Ch 2 */
71 outb(PM_SEL_TIMER2|PM_ACCESS_WORD|PM_MODE0|PM_CNT_BINARY, PORT_PIT_MODE);
73 outb(CALIBRATE_COUNT & 0xFF, PORT_PIT_COUNTER2);
75 outb(CALIBRATE_COUNT >> 8, PORT_PIT_COUNTER2);
77 u64 start = rdtscll();
78 while ((inb(PORT_PS2_CTRLB) & PPCB_T2OUT) == 0)
82 // Restore PORT_PS2_CTRLB
83 outb(orig, PORT_PS2_CTRLB);
85 // Store calibrated cpu khz.
86 u64 diff = end - start;
87 dprintf(6, "tsc calibrate start=%u end=%u diff=%u\n"
88 , (u32)start, (u32)end, (u32)diff);
89 u64 t = DIV_ROUND_UP(diff * PMTIMER_HZ, CALIBRATE_COUNT);
90 while (t >= (1<<24)) {
94 TimerKHz = DIV_ROUND_UP((u32)t, 1000 * PMTIMER_TO_PIT);
96 dprintf(1, "CPU Mhz=%u\n", (TimerKHz << ShiftTSC) / 1000);
99 // Setup internal timers.
103 if (CONFIG_PMTIMER && TimerPort) {
104 dprintf(3, "pmtimer already configured; will not calibrate TSC\n");
108 u32 eax, ebx, ecx, edx, cpuid_features = 0;
109 cpuid(0, &eax, &ebx, &ecx, &edx);
111 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
113 if (!(cpuid_features & CPUID_TSC)) {
114 TimerPort = PORT_PIT_COUNTER0;
115 TimerKHz = DIV_ROUND_UP(PMTIMER_HZ, 1000 * PMTIMER_TO_PIT);
116 dprintf(3, "386/486 class CPU. Using TSC emulation\n");
124 pmtimer_setup(u16 ioport)
128 dprintf(1, "Using pmtimer, ioport 0x%x\n", ioport);
130 TimerKHz = DIV_ROUND_UP(PMTIMER_HZ, 1000);
134 /****************************************************************
135 * Internal timer reading
136 ****************************************************************/
138 u32 TimerLast VARLOW;
140 // Add extra high bits to timers that have less than 32bits of precision.
142 timer_adjust_bits(u32 value, u32 validbits)
144 u32 last = GET_LOW(TimerLast);
145 value = (last & ~validbits) | (value & validbits);
147 value += validbits + 1;
148 SET_LOW(TimerLast, value);
152 // Sample the current timer value.
156 u16 port = GET_GLOBAL(TimerPort);
159 return rdtscll() >> GET_GLOBAL(ShiftTSC);
160 if (CONFIG_PMTIMER && port != PORT_PIT_COUNTER0)
162 return timer_adjust_bits(inl(port), 0xffffff);
164 outb(PM_SEL_READBACK | PM_READ_VALUE | PM_READ_COUNTER0, PORT_PIT_MODE);
165 u16 v = inb(PORT_PIT_COUNTER0) | (inb(PORT_PIT_COUNTER0) << 8);
166 return timer_adjust_bits(v, 0xffff);
169 // Check if the current time is past a previously calculated end time.
173 return (s32)(timer_read() - end) > 0;
177 timer_delay(u32 diff)
179 u32 start = timer_read();
180 u32 end = start + diff;
181 while (!timer_check(end))
186 timer_sleep(u32 diff)
188 u32 start = timer_read();
189 u32 end = start + diff;
190 while (!timer_check(end))
194 void ndelay(u32 count) {
195 timer_delay(DIV_ROUND_UP(count * GET_GLOBAL(TimerKHz), 1000000));
197 void udelay(u32 count) {
198 timer_delay(DIV_ROUND_UP(count * GET_GLOBAL(TimerKHz), 1000));
200 void mdelay(u32 count) {
201 timer_delay(count * GET_GLOBAL(TimerKHz));
204 void nsleep(u32 count) {
205 timer_sleep(DIV_ROUND_UP(count * GET_GLOBAL(TimerKHz), 1000000));
207 void usleep(u32 count) {
208 timer_sleep(DIV_ROUND_UP(count * GET_GLOBAL(TimerKHz), 1000));
210 void msleep(u32 count) {
211 timer_sleep(count * GET_GLOBAL(TimerKHz));
214 // Return the TSC value that is 'msecs' time in the future.
216 timer_calc(u32 msecs)
218 return timer_read() + (GET_GLOBAL(TimerKHz) * msecs);
221 timer_calc_usec(u32 usecs)
223 return timer_read() + DIV_ROUND_UP(GET_GLOBAL(TimerKHz) * usecs, 1000);
227 /****************************************************************
229 ****************************************************************/
231 #define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
233 // Return the number of milliseconds in 'ticks' number of timer irqs.
235 ticks_to_ms(u32 ticks)
237 u32 t = PIT_TICK_INTERVAL * 1000 * PMTIMER_TO_PIT * ticks;
238 return DIV_ROUND_UP(t, PMTIMER_HZ);
241 // Return the number of timer irqs in 'ms' number of milliseconds.
243 ticks_from_ms(u32 ms)
245 u32 t = DIV_ROUND_UP((u64)ms * PMTIMER_HZ, PIT_TICK_INTERVAL);
246 return DIV_ROUND_UP(t, 1000 * PMTIMER_TO_PIT);
252 // timer0: binary count, 16bit count, mode 2
253 outb(PM_SEL_TIMER0|PM_ACCESS_WORD|PM_MODE2|PM_CNT_BINARY, PORT_PIT_MODE);
254 // maximum count of 0000H = 18.2Hz
255 outb(0x0, PORT_PIT_COUNTER0);
256 outb(0x0, PORT_PIT_COUNTER0);