toaruos/kernel/devices/timer.c

78 lines
1.6 KiB
C
Raw Normal View History

/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* Programmable Interrupt Timer
*/
2011-01-16 19:56:44 +03:00
#include <system.h>
#include <logging.h>
2012-12-11 08:28:31 +04:00
#include <process.h>
2011-01-16 19:56:44 +03:00
#define PIT_A 0x40
#define PIT_B 0x41
#define PIT_C 0x42
#define PIT_CONTROL 0x43
#define PIT_MASK 0xFF
#define PIT_SCALE 1193180
#define PIT_SET 0x36
#define TIMER_IRQ 0
2012-12-08 07:06:43 +04:00
#define SUBTICKS_PER_TICK 100
/*
* Set the phase (in hertz) for the Programmable
* Interrupt Timer (PIT).
*/
2011-01-16 19:56:44 +03:00
void
timer_phase(
int hz
) {
int divisor = PIT_SCALE / hz;
outportb(PIT_CONTROL, PIT_SET);
outportb(PIT_A, divisor & PIT_MASK);
outportb(PIT_A, (divisor >> 8) & PIT_MASK);
2011-01-16 19:56:44 +03:00
}
/*
* Internal timer counters
*/
2012-12-08 07:06:43 +04:00
unsigned long timer_ticks = 0;
unsigned char timer_subticks = 0;
2011-01-16 19:56:44 +03:00
/*
* IRQ handler for when the timer fires
*/
2011-01-16 19:56:44 +03:00
void
timer_handler(
struct regs *r
) {
2012-12-08 07:06:43 +04:00
if (++timer_subticks == SUBTICKS_PER_TICK) {
timer_ticks++;
timer_subticks = 0;
}
irq_ack(TIMER_IRQ);
2012-12-11 08:28:31 +04:00
wakeup_sleepers(timer_ticks, timer_subticks);
switch_task(1);
2011-01-16 19:56:44 +03:00
}
2012-12-11 08:28:31 +04:00
void relative_time(unsigned long seconds, unsigned long subseconds, unsigned long * out_seconds, unsigned long * out_subseconds) {
if (subseconds + timer_subticks > SUBTICKS_PER_TICK) {
*out_seconds = timer_ticks + seconds + 1;
*out_subseconds = (subseconds + timer_subticks) - SUBTICKS_PER_TICK;
} else {
*out_seconds = timer_ticks + seconds;
*out_subseconds = timer_subticks + subseconds;
}
}
/*
* Device installer for the PIT
*/
2013-06-06 10:10:36 +04:00
void timer_install(void) {
2012-12-08 06:33:07 +04:00
debug_print(NOTICE,"Initializing interval timer");
irq_install_handler(TIMER_IRQ, timer_handler);
timer_phase(100); /* 100Hz */
2011-01-16 19:56:44 +03:00
}