Rev 40 | Rev 131 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* Project: OSLib |
2 | * Description: The OS Construction Kit |
||
3 | * Date: 1.6.2000 |
||
4 | * Idea by: Luca Abeni & Gerardo Lamastra |
||
5 | * |
||
6 | * OSLib is an SO project aimed at developing a common, easy-to-use |
||
7 | * low-level infrastructure for developing OS kernels and Embedded |
||
8 | * Applications; it partially derives from the HARTIK project but it |
||
9 | * currently is independently developed. |
||
10 | * |
||
11 | * OSLib is distributed under GPL License, and some of its code has |
||
12 | * been derived from the Linux kernel source; also some important |
||
13 | * ideas come from studying the DJGPP go32 extender. |
||
14 | * |
||
15 | * We acknowledge the Linux Community, Free Software Foundation, |
||
16 | * D.J. Delorie and all the other developers who believe in the |
||
17 | * freedom of software and ideas. |
||
18 | * |
||
19 | * For legalese, check out the included GPL license. |
||
20 | */ |
||
21 | |||
120 | giacomo | 22 | /* Added Advanced Timer Code |
23 | * |
||
24 | * Date: 8.4.2003 |
||
25 | * Author: Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
26 | * |
||
27 | */ |
||
28 | |||
2 | pj | 29 | /* Time Event routines */ |
30 | |||
31 | #include <ll/i386/stdlib.h> |
||
32 | #include <ll/i386/mem.h> |
||
33 | #include <ll/i386/error.h> |
||
34 | #include <ll/i386/hw-arch.h> |
||
40 | pj | 35 | #include <ll/i386/pic.h> |
2 | pj | 36 | #include <ll/i386/pit.h> |
37 | #include <ll/sys/ll/ll-data.h> |
||
38 | #include <ll/sys/ll/ll-instr.h> |
||
39 | #include <ll/sys/ll/time.h> |
||
40 | #include <ll/sys/ll/event.h> |
||
41 | |||
42 | FILE(Event); |
||
43 | |||
44 | extern LL_ARCH ll_arch; |
||
45 | |||
46 | BYTE frc; |
||
47 | |||
48 | /* Timer 0 usec base tick */ |
||
49 | DWORD ticksize; |
||
50 | |||
51 | /* Timer 0 loaded time constant (= ticksize * 1.197) */ |
||
52 | WORD pit_time_const; |
||
53 | DWORD timermode; |
||
54 | |||
55 | static DWORD nts; /* System tick in nanoSeconds... */ |
||
56 | struct timespec actTime; /* Time (in nanosecs)... */ |
||
57 | extern int activeInt; |
||
58 | |||
59 | WORD lastTime; |
||
60 | struct pitspec globalCounter; |
||
61 | |||
62 | struct event eventlist[MAX_EVENT]; |
||
63 | |||
64 | struct event *freeevents; |
||
65 | struct event *firstevent; |
||
66 | |||
67 | extern void *last_handler; |
||
68 | extern void (*evt_prol) (void); |
||
69 | extern void (*evt_epil) (void); |
||
70 | |||
120 | giacomo | 71 | extern unsigned char use_tsc; |
72 | |||
40 | pj | 73 | void event_setlasthandler(void *p) |
74 | { |
||
75 | last_handler = p; |
||
76 | } |
||
2 | pj | 77 | |
78 | void event_setprologue(void *p) |
||
79 | { |
||
80 | evt_prol = p; |
||
81 | } |
||
82 | |||
83 | void event_setepilogue(void *p) |
||
84 | { |
||
85 | evt_epil = p; |
||
86 | } |
||
87 | |||
88 | /* Switched to timespec */ |
||
89 | int periodic_event_post(struct timespec time, void (*handler) (void *p), |
||
90 | void *par) |
||
91 | { |
||
92 | struct event *p; |
||
93 | struct event *p1, *t; |
||
94 | |||
95 | if (!freeevents) { |
||
96 | return -1; |
||
97 | } |
||
98 | |||
99 | /* Extract from the ``free events'' queue */ |
||
100 | p = freeevents; |
||
101 | freeevents = p->next; |
||
102 | |||
103 | /* Fill the event fields */ |
||
104 | p->handler = handler; |
||
105 | TIMESPEC_ASSIGN(&(p->time), &time); |
||
106 | p->par = par; |
||
107 | |||
108 | /* ...And insert it in the event queue!!! */ |
||
109 | |||
110 | t = NULL; |
||
111 | /* walk through list, finding spot, adjusting ticks parameter */ |
||
112 | for (p1 = firstevent; p1; p1 = t->next) { |
||
113 | /* |
||
114 | SUBTIMESPEC(&time, &(p1->time), &tmp); |
||
115 | if ((tmp.tv_sec > 0) && (tmp.tv_nsec > 0)) { |
||
116 | */ |
||
117 | if (TIMESPEC_A_GT_B(&time, &p1->time)) |
||
118 | t = p1; |
||
119 | else |
||
120 | break; |
||
121 | } |
||
122 | |||
123 | /* adjust next entry */ |
||
124 | if (t) { |
||
125 | t->next = p; |
||
126 | } else { |
||
127 | firstevent = p; |
||
128 | } |
||
129 | p->next = p1; |
||
130 | |||
131 | return p->index; |
||
132 | } |
||
133 | |||
134 | int periodic_event_delete(int index) |
||
135 | { |
||
136 | struct event *p1, *t; |
||
137 | |||
138 | t = NULL; |
||
139 | /* walk through list, finding spot, adjusting ticks parameter */ |
||
140 | for (p1 = firstevent; (p1) && (index != p1->index); p1 = t->next) { |
||
141 | t = p1; |
||
142 | } |
||
143 | |||
144 | if (p1 == NULL) { |
||
145 | return -1; |
||
146 | } |
||
147 | |||
148 | if (t == NULL) { |
||
149 | firstevent = p1->next; |
||
150 | } else { |
||
151 | t->next = p1->next; |
||
152 | } |
||
153 | p1->next = freeevents; |
||
154 | freeevents = p1; |
||
155 | |||
156 | return 1; |
||
157 | } |
||
158 | |||
159 | void periodic_wake_up(void) |
||
160 | { /* CHANGE the NAME, please... */ |
||
161 | struct event *p, *pp; |
||
162 | WORD tmp; |
||
163 | |||
120 | giacomo | 164 | if(!use_tsc) { |
165 | tmp = pit_read(frc); |
||
166 | ADDPITSPEC((WORD) (lastTime - tmp), &globalCounter); |
||
167 | lastTime = tmp; |
||
168 | } |
||
169 | |||
2 | pj | 170 | activeInt++; |
171 | if (activeInt == 1 && evt_prol != NULL) { |
||
172 | evt_prol(); |
||
173 | } |
||
174 | |||
120 | giacomo | 175 | if (!use_tsc) { |
176 | ADDNANO2TIMESPEC(nts, &actTime); |
||
177 | } else { |
||
178 | read_timespec(&actTime); |
||
179 | } |
||
180 | |||
2 | pj | 181 | for (p = firstevent; p != NULL; p = pp) { |
182 | /* |
||
183 | SUBTIMESPEC(&(p->time), &actTime, &tmp); |
||
184 | if ((tmp.tv_sec > 0) && (tmp.tv_nsec > 0)) { |
||
185 | break; |
||
186 | } */ |
||
187 | if ((p->time.tv_sec > actTime.tv_sec) || |
||
188 | ((p->time.tv_sec == actTime.tv_sec) |
||
189 | && (p->time.tv_nsec > actTime.tv_nsec))) { |
||
190 | break; |
||
191 | } |
||
192 | pp = p->next; |
||
193 | p->next = freeevents; |
||
194 | freeevents = p; |
||
195 | firstevent = pp; |
||
196 | p->handler(p->par); |
||
197 | } |
||
198 | |||
199 | if (activeInt == 1 && evt_epil != NULL) { |
||
200 | evt_epil(); |
||
201 | } |
||
202 | activeInt--; |
||
203 | } |
||
204 | |||
205 | void event_init(struct ll_initparms *l) |
||
206 | { |
||
207 | extern void ll_timer(void); |
||
208 | int i; |
||
209 | BYTE mask; |
||
210 | TIME t; |
||
211 | |||
40 | pj | 212 | IDT_place(0x40,ll_timer); |
2 | pj | 213 | |
214 | if (l->mode != LL_PERIODIC) { |
||
120 | giacomo | 215 | message("One-shot mode\n"); |
2 | pj | 216 | t = 0; |
217 | /* Mode: Binary/Mode 4/16 bit Time_const/Counter 0 */ |
||
218 | pit_init(0, TMR_MD4, 0xFFFF); /* Timer 0, Mode 4, constant 0xFFFF */ |
||
219 | } else { |
||
220 | t = l->tick; |
||
221 | /* Translate the tick value in usec into a suitable time constant */ |
||
222 | /* for 8254 timer chip; the chip is driven with a 1.19718 MHz */ |
||
223 | /* frequency; then the effective frequency is given by the base */ |
||
224 | /* frequency divided for the time constant; the tick is the inverse */ |
||
225 | /* of this effective frequency (in usec!) */ |
||
226 | /* Time-Constant = f_base (MHz) * tick (usec) */ |
||
227 | /* If T-C == 0 -> T-C = 65536 (Max available) */ |
||
228 | ticksize = t; |
||
229 | t = t * 1197; |
||
230 | t = t / 1000; |
||
231 | /* Only for security! This should cause timer overrun */ |
||
232 | /* While 0 would set maximum period on timer */ |
||
233 | if (t == 0) |
||
234 | t = 1; |
||
235 | pit_time_const = (WORD) (t & 0xFFFF); |
||
236 | /* Mode: Binary/Mode 2/16 bit Time_const/Counter 0 */ |
||
237 | pit_init(0, TMR_MD2, t); /* Timer 0, Mode 2, Time constant t */ |
||
238 | |||
239 | |||
240 | } |
||
241 | timermode = l->mode; |
||
40 | pj | 242 | |
243 | if (ll_arch.x86.cpu > 4) { |
||
2 | pj | 244 | /* Timer1: mode 0, time const 0... */ |
245 | pit_init(1, TMR_MD0, 0); |
||
246 | frc = 1; |
||
247 | } else { |
||
248 | frc = 2; |
||
249 | pit_init(2, TMR_MD0, 0); |
||
40 | pj | 250 | outp(0x61, 3); |
2 | pj | 251 | } |
252 | |||
253 | mask = ll_in(0x21); |
||
254 | mask &= 0xFE; /* 0xFE = ~0x01 */ |
||
255 | ll_out(0x21, mask); |
||
256 | |||
257 | |||
258 | /* Init the event list... */ |
||
259 | for (i = 0; i < MAX_EVENT; i++) { |
||
260 | if (i < MAX_EVENT - 1) { |
||
261 | eventlist[i].next = &(eventlist[i + 1]); |
||
262 | } |
||
263 | eventlist[i].index = i; |
||
264 | } |
||
265 | eventlist[MAX_EVENT - 1].next = NULL; |
||
266 | freeevents = &(eventlist[0]); |
||
267 | |||
268 | evt_prol = NULL; |
||
269 | evt_epil = NULL; |
||
270 | |||
271 | /* Initialization of the time variables for periodic mode */ |
||
272 | nts = ticksize * 1000; |
||
273 | NULL_TIMESPEC(&actTime); |
||
120 | giacomo | 274 | |
275 | if (use_tsc) ll_init_advtimer(); |
||
2 | pj | 276 | |
277 | /* Initialization of the general time variables */ |
||
278 | NULLPITSPEC(&globalCounter); |
||
279 | lastTime = 0; |
||
280 | |||
281 | if (timermode == LL_PERIODIC) { |
||
282 | event_post = periodic_event_post; |
||
283 | event_delete = periodic_event_delete; |
||
284 | } else { |
||
285 | event_post = oneshot_event_post; |
||
286 | event_delete = oneshot_event_delete; |
||
287 | } |
||
40 | pj | 288 | |
289 | /* Last but not least... */ |
||
290 | irq_unmask(0); |
||
2 | pj | 291 | } |