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

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