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

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