kern

view src/timer.c @ 41:928b0ebfff4d

merged the timer/rtc/etc from the misc branch
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 14 Jun 2011 01:19:07 +0300
parents 2c401f69128e
children f65b348780e3
line source
1 #include <stdio.h>
2 #include <time.h>
3 #include "intr.h"
4 #include "asmops.h"
5 #include "timer.h"
6 #include "config.h"
8 /* frequency of the oscillator driving the 8254 timer */
9 #define OSC_FREQ_HZ 1193182
11 /* macro to divide and round to the nearest integer */
12 #define DIV_ROUND(a, b) ((a) / (b) + ((a) % (b)) / ((b) / 2))
14 /* I/O ports connected to the 8254 */
15 #define PORT_DATA0 0x40
16 #define PORT_DATA1 0x41
17 #define PORT_DATA2 0x42
18 #define PORT_CMD 0x43
20 /* command bits */
21 #define CMD_CHAN0 0
22 #define CMD_CHAN1 (1 << 6)
23 #define CMD_CHAN2 (2 << 6)
24 #define CMD_RDBACK (3 << 6)
26 #define CMD_LATCH 0
27 #define CMD_ACCESS_LOW (1 << 4)
28 #define CMD_ACCESS_HIGH (2 << 4)
29 #define CMD_ACCESS_BOTH (3 << 4)
31 #define CMD_OP_INT_TERM 0
32 #define CMD_OP_ONESHOT (1 << 1)
33 #define CMD_OP_RATE (2 << 1)
34 #define CMD_OP_SQWAVE (3 << 1)
35 #define CMD_OP_SOFT_STROBE (4 << 1)
36 #define CMD_OP_HW_STROBE (5 << 1)
38 #define CMD_MODE_BIN 0
39 #define CMD_MODE_BCD 1
42 static void intr_handler();
45 void init_timer(void)
46 {
47 /* calculate the reload count: round(osc / freq) */
48 int reload_count = DIV_ROUND(OSC_FREQ_HZ, TICK_FREQ_HZ);
50 /* set the mode to square wave for channel 0, both low
51 * and high reload count bytes will follow...
52 */
53 outb(CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE, PORT_CMD);
55 /* write the low and high bytes of the reload count to the
56 * port for channel 0
57 */
58 outb(reload_count & 0xff, PORT_DATA0);
59 outb((reload_count >> 8) & 0xff, PORT_DATA0);
61 /* set the timer interrupt handler */
62 interrupt(IRQ_TO_INTR(0), intr_handler);
63 }
65 /* This will be called by the interrupt dispatcher approximately
66 * every 1/250th of a second, so it must be extremely fast.
67 * For now, just increasing a tick counter will suffice.
68 */
69 static void intr_handler()
70 {
71 nticks++;
73 if(nticks % TICK_FREQ_HZ == 0) {
74 time_t t = time(0);
75 printf("%s", asctime(gmtime(&t)));
76 }
77 }