summaryrefslogtreecommitdiff
path: root/kernel/drivers/pit.c
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-30 22:12:14 +0100
committerAnton Kling <anton@kling.gg>2023-10-31 00:18:38 +0100
commit8a9208612eec8ddae4c418485d848ecfa0613699 (patch)
tree2f4b29200c2f0c19ae52f45bdb9b38a41b356e30 /kernel/drivers/pit.c
parentca76600acc8bf7a02346efa5bd8f17072210ec01 (diff)
Meta: Move kernel and userland to their own folders.
This is to allow both the kernel and the userland to share certain header files and to make the folder structure a bit more clear.
Diffstat (limited to 'kernel/drivers/pit.c')
-rw-r--r--kernel/drivers/pit.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/kernel/drivers/pit.c b/kernel/drivers/pit.c
new file mode 100644
index 0000000..711883c
--- /dev/null
+++ b/kernel/drivers/pit.c
@@ -0,0 +1,55 @@
+#include "pit.h"
+
+#define PIT_IO_CHANNEL_0 0x40
+#define PIT_IO_MODE_COMMAND 0x43
+
+uint64_t clock_num_ms_ticks = 0;
+uint64_t pit_counter = 0;
+uint16_t hertz;
+
+uint64_t pit_num_ms(void) { return clock_num_ms_ticks; }
+
+uint16_t read_pit_count(void) {
+ uint16_t count = 0;
+
+ outb(PIT_IO_MODE_COMMAND, 0x0 /*0b00000000*/);
+
+ count = inb(PIT_IO_CHANNEL_0);
+ count |= inb(PIT_IO_CHANNEL_0) << 8;
+
+ return count;
+}
+
+void set_pit_count(uint16_t hertz) {
+ uint16_t divisor = 1193180 / hertz;
+
+ /*
+ * 0b00110110
+ * ^^
+ * channel - 0
+ * ^^
+ * r/w mode - LSB then MSB
+ * ^^^
+ * mode - 3 Square Wave Mode
+ * ^
+ * BCD - no
+ */
+ outb(PIT_IO_MODE_COMMAND, 0x36 /*0b00110110*/);
+ outb(PIT_IO_CHANNEL_0, divisor & 0xFF);
+ outb(PIT_IO_CHANNEL_0, (divisor & 0xFF00) >> 8);
+}
+
+__attribute__((interrupt)) void
+int_clock(__attribute__((unused)) struct interrupt_frame *frame) {
+ outb(0x20, 0x20);
+ pit_counter++;
+ if (pit_counter >= hertz / 1000) {
+ pit_counter = 0;
+ clock_num_ms_ticks++;
+ }
+ switch_task();
+}
+
+void pit_install(void) {
+ install_handler(int_clock, INT_32_INTERRUPT_GATE(0x0), 0x20);
+}