[doc] Documented the module system.
[marionette.git] / kernel / timer.c
blobe9f97ffa521490c6f21e7a6543dfd8f1f79821c4
1 /*
2 * Copyright (c) 2008 Joshua Phillips. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
15 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "timer.h"
29 #include "portio.h"
30 #include "console.h"
31 #include "interrupt.h"
32 #include "thread.h"
34 #define TIMER_IRQ 0
35 #define TIMER_FREQ 1000
36 #define CTL 0x43
37 #define CNT0 0x40
38 #define CNT1 0x41
39 #define CNT2 0x42
41 static tick_t tick = 0; // current tick number
42 static unsigned long freq; // current timer frequency
44 tick_t get_tick(void)
46 return tick;
49 static void timer_interrupt(int vector, struct interrupt_stack *is)
51 tick++;
52 if (preempt_on && (next_preempt <= tick)){
53 preempt(is);
55 ack_irq(TIMER_IRQ);
58 static void set_frequency(unsigned long new_freq)
60 unsigned short divisor;
61 divisor = 1193180 / new_freq;
62 freq = (1193180LL << 32) / divisor;
63 outb(CTL, 0x36);
64 outb(CNT0, divisor);
65 outb(CNT0, divisor >> 8);
68 void timer_sys_start(void)
70 unsigned char p61h;
71 // Timer's frequency is 1,193,180 Hz
72 outb(CTL, 0x54);
73 outb(CNT1, 18); // LSB only clock divisor (?)
74 tick = 0;
75 set_interrupt(irq_to_int(TIMER_IRQ), timer_interrupt);
76 set_frequency(TIMER_FREQ);
77 unmask_irq(TIMER_IRQ);
79 outb(CTL, 0xB6);
80 outb(CNT2, 54);
81 outb(CNT2, 124);
82 p61h = inb(0x61);
83 p61h |= 3;
84 outb(0x61, p61h);