abdbb118e6f88488e8d6c7e01da876e6d69aa0de
[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 #if 0
91 /**
92  * \fn void Time_TimerThread()
93  */
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                 if(gTimers[i].Callback == NULL) continue;
131                 if(giTimestamp < gTimers[i].FiresAfter) continue;
132                 callback = gTimers[i].Callback;
133                 gTimers[i].Callback = NULL;
134                 callback(gTimers[i].Argument);
135         }
136 }
137
138 /**
139  * \fn int Time_CreateTimer(int Delta, void *Callback, void *Argument)
140  */
141 int Time_CreateTimer(int Delta, void *Callback, void *Argument)
142 {
143          int    ret;
144         
145         if(Callback == NULL)    return -1;
146         
147         for(ret = 0;
148                 ret < NUM_TIMERS;
149                 ret++)
150         {
151                 if(gTimers[ret].Callback != NULL)       continue;
152                 gTimers[ret].Callback = Callback;
153                 gTimers[ret].FiresAfter = giTimestamp + Delta;
154                 gTimers[ret].Argument = Argument;
155                 //Log("Callback = %p", Callback);
156                 //Log("Timer %i fires at %lli", ret, gTimers[ret].FiresAfter);
157                 return ret;
158         }
159         return -1;
160 }
161
162 /**
163  * \fn void Time_RemoveTimer(int ID)
164  */
165 void Time_RemoveTimer(int ID)
166 {
167         if(ID < 0 || ID >= NUM_TIMERS)  return;
168         gTimers[ID].Callback = NULL;
169 }
170
171 /**
172  * \fn void Time_Delay(int Delay)
173  * \brief Delay for a small ammount of time
174  */
175 void Time_Delay(int Delay)
176 {
177         Sint64  dest = giTimestamp + Delay;
178         while(dest < giTimestamp)       Threads_Yield();
179 }
180
181 // === EXPORTS ===
182 EXPORT(now);
183 EXPORT(Time_CreateTimer);
184 EXPORT(Time_RemoveTimer);
185 EXPORT(Time_Delay);

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