summaryrefslogtreecommitdiff
path: root/kernel/drivers/pit.c
blob: 8f9c35c031e3942a3f4f3e498e32355a8db2440d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "pit.h"

#define PIT_IO_CHANNEL_0 0x40
#define PIT_IO_MODE_COMMAND 0x43

u64 clock_num_ms_ticks = 0;
u64 pit_counter = 0;
u16 hertz;

u64 pit_num_ms(void) {
  return clock_num_ms_ticks;
}

u16 read_pit_count(void) {
  u16 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(u16 hertz) {
  u16 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(registers_t *regs) {
  process_t *p = get_current_task();
  if (p) {
    // FIXME: For some reason eflags is the esp? I have read the
    // manual multilpe times and still can't figure out why.
    if (regs->eflags <= 0x90000000 && regs->eflags) {
      p->useresp = regs->eflags;
    }
  }
  outb(0x20, 0x20);
  pit_counter++;
  if (pit_counter >= hertz / 1000) {
    pit_counter = 0;
    clock_num_ms_ticks++;
  }
  switch_task(1);
}

void pit_install(void) {
  install_handler(int_clock, INT_32_INTERRUPT_GATE(0x3), 0x20);
}