Fixed timer code bug (not ACKing after enabling)
[tpg/acess2.git] / Kernel / arch / x86 / time.c
1 /*
2  * Acess2 Kernel
3  * Timekeeping
4  * arch/x86/time.c
5  */
6 #include <acess.h>
7
8 // === MACROS ===
9 #define TIMER_QUANTUM   100
10 // 2^(15-rate), 15: 1HZ, 5: 1024Hz, 2: 8192Hz
11 #define TIMER_RATE      12      // (Max: 15, Min: 2) - 15 = 1Hz, 13 = 4Hz, 12 = 8Hz, 11 = 16Hz 10 = 32Hz, 2
12 #define TIMER_FREQ      (0x8000>>TIMER_RATE)    //Hz
13 #define MS_PER_TICK_WHOLE       (1000/(TIMER_FREQ))
14 #define MS_PER_TICK_FRACT       ((0x80000000*(1000%TIMER_FREQ))/TIMER_FREQ)
15
16 // === IMPORTS ===
17 extern Sint64   giTimestamp;
18 extern Uint64   giTicks;
19 extern Uint64   giPartMiliseconds;
20 extern void     Timer_CallTimers(void);
21
22 // === PROTOTYPES ===
23 void    Time_Interrupt(int);
24
25 // === CODE ===
26 /**
27  * \fn int Time_Setup(void)
28  * \brief Sets the system time from the Realtime-Clock
29  */
30 int Time_Setup(void)
31 {
32         Uint8   val;
33         
34         Log_Log("Timer", "RTC Timer firing at %iHz (%i divisor), %i.0x%08x",
35                 TIMER_FREQ, TIMER_RATE, MS_PER_TICK_WHOLE, MS_PER_TICK_FRACT);
36         
37         outb(0x70, inb(0x70)&0x7F);     // Disable NMIs
38         __asm__ __volatile__ ("cli");   // Disable normal interrupts
39         
40         // Set IRQ8 firing rate
41         outb(0x70, 0x0A);       // Set the index to register A
42         val = inb(0x71); // Get the current value of register A
43         outb(0x70, 0x0A); // Reset index to A
44         val &= 0xF0;
45         val |= TIMER_RATE;
46         outb(0x71, val);        // Update the timer rate
47                 
48         // Enable IRQ8
49         outb(0x70, 0x0B);       // Set the index to register B
50         val = inb(0x71);        // Read the current value of register B
51         outb(0x70, 0x0B);       // Set the index again (a read will reset the index to register D)
52         outb(0x71, val | 0x40); // Write the previous value or'd with 0x40. This turns on bit 6 of register D
53         
54         __asm__ __volatile__ ("sti");   // Disable normal interrupts
55         outb(0x70, inb(0x70)|0x80);     // Disable NMIs
56         
57         // Install IRQ Handler
58         IRQ_AddHandler(8, Time_Interrupt);
59         
60         // Make sure the RTC actually fires
61         outb(0x70, 0x0C); // Select register C
62         inb(0x71);      // Just throw away contents.
63         
64         return 0;
65 }
66
67 /**
68  * \fn void Time_Interrupt(void)
69  * \brief Called on the timekeeping IRQ
70  */
71 void Time_Interrupt(int irq)
72 {
73         giTicks ++;
74         giTimestamp += MS_PER_TICK_WHOLE;
75         giPartMiliseconds += MS_PER_TICK_FRACT;
76         if(giPartMiliseconds > 0x80000000) {
77                 giTimestamp ++;
78                 giPartMiliseconds -= 0x80000000;
79         }
80         
81         Timer_CallTimers();
82
83         // Make sure the RTC Fires again
84         outb(0x70, 0x0C); // Select register C
85         inb(0x71);      // Just throw away contents.
86 }
87
88 #if 0
89 /**
90  * \fn void Time_TimerThread(void)
91  */
92 void Time_TimerThread(void)
93 {
94         Sint64  next;
95         Threads_SetName("TIMER");
96         
97         next = giTimestamp + TIMER_QUANTUM;
98         for(;;)
99         {
100                 while(giTimestamp < next)       Threads_Yield();
101                 next = giTimestamp + TIMER_QUANTUM;     
102                 Timer_CallTimers();
103         }
104 }
105 #endif

UCC git Repository :: git.ucc.asn.au