2023-03-19 20:36:36 +00:00
|
|
|
#include <socks/printk.h>
|
2023-03-20 20:21:44 +00:00
|
|
|
#include <socks/cpu.h>
|
2023-03-19 20:36:36 +00:00
|
|
|
#include <arch/ports.h>
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
2023-03-20 20:21:44 +00:00
|
|
|
static unsigned long long tick_counter = 0;
|
|
|
|
|
|
2023-03-19 20:36:36 +00:00
|
|
|
static int pit_callback(void)
|
|
|
|
|
{
|
2023-03-20 20:21:44 +00:00
|
|
|
tick_counter++;
|
2023-03-19 20:36:36 +00:00
|
|
|
printk("tick");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static irq_hook_t pit_irq_hook = {
|
|
|
|
|
.irq_callback = pit_callback
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-20 20:21:44 +00:00
|
|
|
void pit_start(unsigned int hz)
|
2023-03-19 20:36:36 +00:00
|
|
|
{
|
|
|
|
|
unsigned int divisor = 1193180 / hz;
|
|
|
|
|
|
|
|
|
|
outportb(0x43, 0x36);
|
|
|
|
|
uint8_t lo = (uint8_t)(divisor & 0xFF);
|
|
|
|
|
uint8_t hi = (uint8_t)((divisor >> 8) & 0xFF);
|
|
|
|
|
|
|
|
|
|
outportb(0x40, lo);
|
|
|
|
|
outportb(0x40, hi);
|
|
|
|
|
|
|
|
|
|
hook_irq(IRQ0, &pit_irq_hook);
|
2023-03-20 20:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pit_stop(void)
|
|
|
|
|
{
|
|
|
|
|
/* lowest possible frequency */
|
|
|
|
|
unsigned int divisor = 1193180;
|
|
|
|
|
|
|
|
|
|
outportb(0x43, 0x36);
|
|
|
|
|
uint8_t lo = (uint8_t)(divisor & 0xFF);
|
|
|
|
|
uint8_t hi = (uint8_t)((divisor >> 8) & 0xFF);
|
|
|
|
|
|
|
|
|
|
outportb(0x40, lo);
|
|
|
|
|
outportb(0x40, hi);
|
|
|
|
|
|
|
|
|
|
unhook_irq(IRQ0, &pit_irq_hook);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pit_wait(unsigned int ticks)
|
|
|
|
|
{
|
|
|
|
|
unsigned long long end = tick_counter + ticks;
|
2023-03-19 20:36:36 +00:00
|
|
|
|
2023-03-20 20:21:44 +00:00
|
|
|
while (tick_counter < end) {
|
|
|
|
|
ml_cpu_pause();
|
|
|
|
|
}
|
2023-03-19 20:36:36 +00:00
|
|
|
}
|