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

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