summaryrefslogtreecommitdiff
path: root/kernel/drivers/pit.c
blob: 05b6a3a0e254a3a9315823c6a46fab2dafebf8cb (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
65
#include "lib/klib.h"
#include <comus/asm.h>
#include <comus/drivers/pit.h>
#include <comus/drivers/spkr.h>
#include <lib.h>

#define CMD 0x43
#define SPKR 0x61

#define BASE 1193180

volatile uint64_t ticks = 0;

uint32_t pit_read_freq(uint8_t chan)
{
	uint16_t div = 0;
	cli();
	outb(CMD, 0x36); // clear bits
	div = inb(chan); // low byte
	div |= inb(chan) << 8; // highbyte
	sti();
	return div * BASE;
}

void pit_set_freq(uint8_t chan, uint32_t hz)
{
	uint16_t div = BASE / hz;
	cli();
	outb(CMD, 0x36);
	outb(chan, div & 0xFF); // low byte
	outb(chan, (div & 0xFF00) >> 8); // high byte
	sti();
}

void spkr_play_tone(uint32_t hz)
{
	uint8_t reg;

	if (hz == 0) {
		spkr_quiet();
		return;
	}

	// set spkr freq
	pit_set_freq(CHAN_SPKR, hz);

	// enable spkr gate
	reg = inb(SPKR);
	if (reg != (reg | 0x3))
		outb(SPKR, reg | 0x3);
}

void spkr_quiet(void)
{
	uint8_t reg;
	reg = inb(SPKR) & 0xFC;
	outb(SPKR, reg);
}

void spkr_beep(void)
{
	spkr_play_tone(1000);
	kspin_milliseconds(100);
	spkr_quiet();
}