blob: 64773134633e6c777188ed569242162dcf1dd15a [file] [log] [blame]
Kevin O'Connorc6e8c072013-07-20 10:51:58 -04001// Internal timer support.
2//
3// Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
Kevin O'Connorc6e8c072013-07-20 10:51:58 -04007#include "biosvar.h" // GET_LOW
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04008#include "config.h" // CONFIG_*
9#include "ioport.h" // PORT_PIT_MODE
10#include "output.h" // dprintf
11#include "pit.h" // PM_SEL_TIMER0
Kevin O'Connor3df600b2013-09-14 19:28:55 -040012#include "stacks.h" // yield
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040013#include "util.h" // timer_setup
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040014#include "x86.h" // cpuid
Kevin O'Connorc6e8c072013-07-20 10:51:58 -040015
16// Bits for PORT_PS2_CTRLB
17#define PPCB_T2GATE (1<<0)
18#define PPCB_SPKR (1<<1)
19#define PPCB_T2OUT (1<<5)
20
Kevin O'Connorb7ab1782013-07-20 13:06:35 -040021#define PMTIMER_HZ 3579545 // Underlying Hz of the PM Timer
22#define PMTIMER_TO_PIT 3 // Ratio of pmtimer rate to pit rate
Kevin O'Connorb7ab1782013-07-20 13:06:35 -040023
Kevin O'Connor0edf1d62013-07-20 17:05:24 -040024u32 TimerKHz VARFSEG;
Kevin O'Connoreac11942013-07-20 19:09:07 -040025u16 TimerPort VARFSEG;
Kevin O'Connor23c14a12013-07-20 17:06:51 -040026u8 ShiftTSC VARFSEG;
27
Kevin O'Connor99997542013-07-20 18:39:37 -040028
29/****************************************************************
30 * Timer setup
31 ****************************************************************/
32
33#define CALIBRATE_COUNT 0x800 // Approx 1.7ms
34
35// Calibrate the CPU time-stamp-counter
36static void
37tsctimer_setup(void)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -040038{
Kevin O'Connorc6e8c072013-07-20 10:51:58 -040039 // Setup "timer2"
40 u8 orig = inb(PORT_PS2_CTRLB);
41 outb((orig & ~PPCB_SPKR) | PPCB_T2GATE, PORT_PS2_CTRLB);
42 /* binary, mode 0, LSB/MSB, Ch 2 */
43 outb(PM_SEL_TIMER2|PM_ACCESS_WORD|PM_MODE0|PM_CNT_BINARY, PORT_PIT_MODE);
44 /* LSB of ticks */
45 outb(CALIBRATE_COUNT & 0xFF, PORT_PIT_COUNTER2);
46 /* MSB of ticks */
47 outb(CALIBRATE_COUNT >> 8, PORT_PIT_COUNTER2);
48
49 u64 start = rdtscll();
50 while ((inb(PORT_PS2_CTRLB) & PPCB_T2OUT) == 0)
51 ;
52 u64 end = rdtscll();
53
54 // Restore PORT_PS2_CTRLB
55 outb(orig, PORT_PS2_CTRLB);
56
57 // Store calibrated cpu khz.
58 u64 diff = end - start;
59 dprintf(6, "tsc calibrate start=%u end=%u diff=%u\n"
60 , (u32)start, (u32)end, (u32)diff);
Kevin O'Connor23c14a12013-07-20 17:06:51 -040061 u64 t = DIV_ROUND_UP(diff * PMTIMER_HZ, CALIBRATE_COUNT);
62 while (t >= (1<<24)) {
63 ShiftTSC++;
64 t = (t + 1) >> 1;
65 }
66 TimerKHz = DIV_ROUND_UP((u32)t, 1000 * PMTIMER_TO_PIT);
Kevin O'Connorc6e8c072013-07-20 10:51:58 -040067
Kevin O'Connor23c14a12013-07-20 17:06:51 -040068 dprintf(1, "CPU Mhz=%u\n", (TimerKHz << ShiftTSC) / 1000);
Kevin O'Connorc6e8c072013-07-20 10:51:58 -040069}
70
Kevin O'Connor99997542013-07-20 18:39:37 -040071// Setup internal timers.
72void
73timer_setup(void)
74{
Kevin O'Connoreac11942013-07-20 19:09:07 -040075 if (CONFIG_PMTIMER && TimerPort) {
Kevin O'Connor99997542013-07-20 18:39:37 -040076 dprintf(3, "pmtimer already configured; will not calibrate TSC\n");
77 return;
78 }
79
80 u32 eax, ebx, ecx, edx, cpuid_features = 0;
81 cpuid(0, &eax, &ebx, &ecx, &edx);
82 if (eax > 0)
83 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
84
85 if (!(cpuid_features & CPUID_TSC)) {
Kevin O'Connoreac11942013-07-20 19:09:07 -040086 TimerPort = PORT_PIT_COUNTER0;
Kevin O'Connor99997542013-07-20 18:39:37 -040087 TimerKHz = DIV_ROUND_UP(PMTIMER_HZ, 1000 * PMTIMER_TO_PIT);
88 dprintf(3, "386/486 class CPU. Using TSC emulation\n");
89 return;
90 }
91
92 tsctimer_setup();
93}
94
95void
96pmtimer_setup(u16 ioport)
97{
98 if (!CONFIG_PMTIMER)
99 return;
100 dprintf(1, "Using pmtimer, ioport 0x%x\n", ioport);
Kevin O'Connoreac11942013-07-20 19:09:07 -0400101 TimerPort = ioport;
Kevin O'Connor99997542013-07-20 18:39:37 -0400102 TimerKHz = DIV_ROUND_UP(PMTIMER_HZ, 1000);
103}
104
105
106/****************************************************************
107 * Internal timer reading
108 ****************************************************************/
109
Kevin O'Connoreac11942013-07-20 19:09:07 -0400110u32 TimerLast VARLOW;
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400111
Kevin O'Connoreac11942013-07-20 19:09:07 -0400112// Add extra high bits to timers that have less than 32bits of precision.
Kevin O'Connor23c14a12013-07-20 17:06:51 -0400113static u32
Kevin O'Connoreac11942013-07-20 19:09:07 -0400114timer_adjust_bits(u32 value, u32 validbits)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400115{
Kevin O'Connoreac11942013-07-20 19:09:07 -0400116 u32 last = GET_LOW(TimerLast);
117 value = (last & ~validbits) | (value & validbits);
118 if (value < last)
119 value += validbits + 1;
120 SET_LOW(TimerLast, value);
121 return value;
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400122}
123
Kevin O'Connoreac11942013-07-20 19:09:07 -0400124// Sample the current timer value.
Kevin O'Connor23c14a12013-07-20 17:06:51 -0400125static u32
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400126timer_read(void)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400127{
Kevin O'Connoreac11942013-07-20 19:09:07 -0400128 u16 port = GET_GLOBAL(TimerPort);
129 if (!port)
130 // Read from CPU TSC
131 return rdtscll() >> GET_GLOBAL(ShiftTSC);
132 if (CONFIG_PMTIMER && port != PORT_PIT_COUNTER0)
133 // Read from PMTIMER
134 return timer_adjust_bits(inl(port), 0xffffff);
135 // Read from PIT.
136 outb(PM_SEL_READBACK | PM_READ_VALUE | PM_READ_COUNTER0, PORT_PIT_MODE);
137 u16 v = inb(PORT_PIT_COUNTER0) | (inb(PORT_PIT_COUNTER0) << 8);
138 return timer_adjust_bits(v, 0xffff);
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400139}
140
Kevin O'Connoreac11942013-07-20 19:09:07 -0400141// Check if the current time is past a previously calculated end time.
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400142int
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400143timer_check(u32 end)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400144{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400145 return (s32)(timer_read() - end) > 0;
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400146}
147
148static void
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400149timer_delay(u32 diff)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400150{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400151 u32 start = timer_read();
Kevin O'Connor23c14a12013-07-20 17:06:51 -0400152 u32 end = start + diff;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400153 while (!timer_check(end))
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400154 cpu_relax();
155}
156
157static void
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400158timer_sleep(u32 diff)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400159{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400160 u32 start = timer_read();
Kevin O'Connor23c14a12013-07-20 17:06:51 -0400161 u32 end = start + diff;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400162 while (!timer_check(end))
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400163 yield();
164}
165
166void ndelay(u32 count) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400167 timer_delay(DIV_ROUND_UP(count * GET_GLOBAL(TimerKHz), 1000000));
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400168}
169void udelay(u32 count) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400170 timer_delay(DIV_ROUND_UP(count * GET_GLOBAL(TimerKHz), 1000));
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400171}
172void mdelay(u32 count) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400173 timer_delay(count * GET_GLOBAL(TimerKHz));
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400174}
175
176void nsleep(u32 count) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400177 timer_sleep(DIV_ROUND_UP(count * GET_GLOBAL(TimerKHz), 1000000));
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400178}
179void usleep(u32 count) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400180 timer_sleep(DIV_ROUND_UP(count * GET_GLOBAL(TimerKHz), 1000));
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400181}
182void msleep(u32 count) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400183 timer_sleep(count * GET_GLOBAL(TimerKHz));
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400184}
185
186// Return the TSC value that is 'msecs' time in the future.
Kevin O'Connor23c14a12013-07-20 17:06:51 -0400187u32
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400188timer_calc(u32 msecs)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400189{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400190 return timer_read() + (GET_GLOBAL(TimerKHz) * msecs);
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400191}
Kevin O'Connor23c14a12013-07-20 17:06:51 -0400192u32
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400193timer_calc_usec(u32 usecs)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400194{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400195 return timer_read() + DIV_ROUND_UP(GET_GLOBAL(TimerKHz) * usecs, 1000);
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400196}
197
198
199/****************************************************************
200 * IRQ based timer
201 ****************************************************************/
202
Kevin O'Connoreac11942013-07-20 19:09:07 -0400203#define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
204
Kevin O'Connor69013372013-07-20 12:08:48 -0400205// Return the number of milliseconds in 'ticks' number of timer irqs.
206u32
207ticks_to_ms(u32 ticks)
208{
Kevin O'Connorb7ab1782013-07-20 13:06:35 -0400209 u32 t = PIT_TICK_INTERVAL * 1000 * PMTIMER_TO_PIT * ticks;
210 return DIV_ROUND_UP(t, PMTIMER_HZ);
Kevin O'Connor69013372013-07-20 12:08:48 -0400211}
212
213// Return the number of timer irqs in 'ms' number of milliseconds.
214u32
215ticks_from_ms(u32 ms)
216{
Kevin O'Connorb7ab1782013-07-20 13:06:35 -0400217 u32 t = DIV_ROUND_UP((u64)ms * PMTIMER_HZ, PIT_TICK_INTERVAL);
218 return DIV_ROUND_UP(t, 1000 * PMTIMER_TO_PIT);
Kevin O'Connor69013372013-07-20 12:08:48 -0400219}
220
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400221// Calculate the timer value at 'count' number of full timer ticks in
222// the future.
223u32
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400224irqtimer_calc_ticks(u32 count)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400225{
226 return (GET_BDA(timer_counter) + count + 1) % TICKS_PER_DAY;
227}
228
229// Return the timer value that is 'msecs' time in the future.
230u32
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400231irqtimer_calc(u32 msecs)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400232{
233 if (!msecs)
234 return GET_BDA(timer_counter);
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400235 return irqtimer_calc_ticks(ticks_from_ms(msecs));
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400236}
237
238// Check if the given timer value has passed.
239int
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400240irqtimer_check(u32 end)
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400241{
242 return (((GET_BDA(timer_counter) + TICKS_PER_DAY - end) % TICKS_PER_DAY)
243 < (TICKS_PER_DAY/2));
244}