2018-07-20 15:41:58 +02:00
|
|
|
#include "pit.h"
|
|
|
|
#include "io.h"
|
2018-11-08 21:37:38 +01:00
|
|
|
#include "irq.h"
|
2020-05-03 23:14:40 +02:00
|
|
|
#include "klibc.h"
|
2021-10-30 14:18:21 +02:00
|
|
|
#include "thread.h"
|
2020-04-27 23:08:36 +02:00
|
|
|
#include "time.h"
|
2018-07-20 15:41:58 +02:00
|
|
|
|
2018-11-08 22:08:27 +01:00
|
|
|
int pitSetup(unsigned int freq)
|
2018-07-20 15:41:58 +02:00
|
|
|
{
|
2020-04-27 00:14:37 +02:00
|
|
|
unsigned int divisor = PIT_FREQ / freq;
|
|
|
|
if (divisor > 65535)
|
|
|
|
divisor = 0; // Used to represent 35536
|
|
|
|
outb(PIT_CMD, 0x34); // chan 0; low then high; mode 2
|
|
|
|
outb(PIT_CHAN_0, divisor & 0xFF);
|
|
|
|
outb(PIT_CHAN_0, divisor >> 8u);
|
2018-07-20 15:41:58 +02:00
|
|
|
|
2020-04-27 00:14:37 +02:00
|
|
|
return 0;
|
2018-07-20 15:41:58 +02:00
|
|
|
}
|
2020-04-27 23:08:36 +02:00
|
|
|
|
|
|
|
struct cpu_state *pitIrqHandler(struct cpu_state *prevCpu)
|
|
|
|
{
|
|
|
|
__atomic_add_fetch(&jiffies, 1, __ATOMIC_RELAXED);
|
2021-10-30 14:18:21 +02:00
|
|
|
threadOnJieffiesTick();
|
2020-05-03 23:14:40 +02:00
|
|
|
// Uncomment for non-preemptible kernel
|
2021-11-05 23:02:23 +01:00
|
|
|
if (cpu_context_is_in_user_mode(prevCpu)) {
|
|
|
|
return threadSwitch(prevCpu);
|
|
|
|
} else {
|
|
|
|
return prevCpu;
|
|
|
|
}
|
2020-04-27 23:08:36 +02:00
|
|
|
}
|