Various Changes
[tpg/acess2.git] / Kernel / arch / x86 / time.c
1 /*
2  * Acess2 Kernel
3  * Timekeeping
4  * arch/x86/time.c
5  */
6 #include <common.h>
7
8 // === MACROS ===
9 #define NUM_TIMERS      8
10 #define TIMER_QUANTUM   100
11 #define TIMER_RATE      13      // (Max: 15, Min: 2) - 15 = 1Hz, 13 = 4Hz, 10 = 1024Hz
12 #define TIMER_FREQ      (32768>>TIMER_RATE)     //Hz
13 #define MS_PER_TICK_WHOLE       (1000/(TIMER_FREQ))
14 #define MS_PER_TICK_FRACT       ((Uint64)(1000*TIMER_FREQ-((Uint64)MS_PER_TICK_WHOLE)*0x80000000/TIMER_FREQ))
15
16 // === TYPEDEFS ===
17 typedef struct sTimer {
18          int    FiresAfter;
19         void    (*Callback)(void*);
20         void    *Argument;
21 } tTimer;
22
23 // === PROTOTYPES ===
24 void    Time_Interrupt();
25 void    Timer_CallTimers();
26
27 // === GLOBALS ===
28 Uint64  giTicks = 0;
29 Sint64  giTimestamp = 0;
30 Uint64  giPartMiliseconds = 0;
31 tTimer  gTimers[NUM_TIMERS];
32
33 // === CODE ===
34 /**
35  * \fn int Time_Setup()
36  * \brief Sets the system time from the Realtime-Clock
37  */
38 int Time_Setup()
39 {
40         Uint8   val;
41         
42         outb(0x70, inb(0x70)&0x7F);     // Disable NMIs
43         __asm__ __volatile__ ("cli");   // Disable normal interrupts
44         
45         // Set IRQ8 firing rate
46         outb(0x70, 0x0A);       // Set the index to register A
47         val = inb(0x71); // Get the current value of register A
48         outb(0x70, 0x0A); // Reset index to A
49         val &= 0xF0;
50         val |= TIMER_RATE;
51         outb(0x71, val);        // Update the timer rate
52                 
53         // Enable IRQ8
54         outb(0x70, 0x0B);       // Set the index to register B
55         val = inb(0x71);        // Read the current value of register B
56         outb(0x70, 0x0B);       // Set the index again (a read will reset the index to register D)
57         outb(0x71, val | 0x40); // Write the previous value or'd with 0x40. This turns on bit 6 of register D
58         
59         __asm__ __volatile__ ("sti");   // Disable normal interrupts
60         outb(0x70, inb(0x70)|0x80);     // Disable NMIs
61         
62         // Install IRQ Handler
63         IRQ_AddHandler(8, Time_Interrupt);
64         return 0;
65 }
66
67 /**
68  * \fn void Time_Interrupt()
69  * \brief Called on the timekeeping IRQ
70  */
71 void Time_Interrupt()
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         //Log("giTimestamp = %lli", giTimestamp);
82         
83         //Timer_CallTimers();
84
85         // Make sure the RTC Fires again
86         outb(0x70, 0x0C); // Select register C
87         inb(0x71);      // Just throw away contents.
88 }
89
90 /**
91  * \fn void Time_TimerThread()
92  */
93 #if 0
94 void Time_TimerThread()
95 {
96         Sint64  next;
97         Threads_SetName("TIMER");
98         
99         next = giTimestamp + TIMER_QUANTUM;
100         for(;;)
101         {
102                 while(giTimestamp < next)       Threads_Yield();
103                 next = giTimestamp + TIMER_QUANTUM;     
104                 Timer_CallTimers();
105         }
106 }
107 #endif
108
109 /**
110  * \fn Sint64 now()
111  * \brief Return the current timestamp
112  */
113 Sint64 now()
114 {
115         return giTimestamp;
116 }
117
118 /**
119  * \fn void Timer_CallTimers()
120  */
121 void Timer_CallTimers()
122 {
123          int    i;
124         void    (*callback)(void *);
125         
126         for(i = 0;
127                 i < NUM_TIMERS;
128                 i ++)
129         {
130                 //Log("Timer %i", i);
131                 if(gTimers[i].Callback == NULL) continue;
132                 Log("%i - %lli < %lli", i, giTimestamp, gTimers[i].FiresAfter);
133                 if(giTimestamp < gTimers[i].FiresAfter) continue;
134                 callback = gTimers[i].Callback;
135                 gTimers[i].Callback = NULL;
136                 callback(gTimers[i].Argument);
137         }
138 }
139
140 /**
141  * \fn int Time_CreateTimer(int Delta, void *Callback, void *Argument)
142  */
143 int Time_CreateTimer(int Delta, void *Callback, void *Argument)
144 {
145          int    ret;
146         
147         if(Callback == NULL)    return -1;
148         
149         for(ret = 0;
150                 ret < NUM_TIMERS;
151                 ret++)
152         {
153                 if(gTimers[ret].Callback != NULL)       continue;
154                 gTimers[ret].Callback = Callback;
155                 gTimers[ret].FiresAfter = giTimestamp + Delta;
156                 gTimers[ret].Argument = Argument;
157                 Log("Callback = %p", Callback);
158                 Log("Timer %i fires at %lli", ret, gTimers[ret].FiresAfter);
159                 return ret;
160         }
161         return -1;
162 }
163
164 /**
165  * \fn void Time_RemoveTimer(int ID)
166  */
167 void Time_RemoveTimer(int ID)
168 {
169         if(ID < 0 || ID >= NUM_TIMERS)  return;
170         gTimers[ID].Callback = NULL;
171 }
172
173 /**
174  * \fn void Time_Delay(int Delay)
175  * \brief Delay for a small ammount of time
176  */
177 void Time_Delay(int Delay)
178 {
179         Sint64  dest = giTimestamp + Delay;
180         while(dest < giTimestamp)       Threads_Yield();
181 }

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