Subversion Repositories shark

Rev

Rev 130 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
120 giacomo 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
 
22
/*      Advanced Timer Managment
23
 *      Author: Giacomo Guidi <giacomo@gandalf.sssup.it>
24
 */
25
 
26
#include <ll/i386/stdlib.h>
27
#include <ll/i386/error.h>
28
#include <ll/i386/advtimer.h>
29
#include <ll/sys/ll/ll-data.h>
30
#include <ll/sys/ll/ll-func.h>
31
#include <ll/i386/pic.h>
32
#include <ll/sys/ll/event.h>
33
#include <ll/sys/ll/time.h>
34
 
130 giacomo 35
unsigned char use_tsc = 1; //Enable the TSC counter mode
120 giacomo 36
unsigned char use_cmos = 0; //Enable the RTC correction
37
 
126 giacomo 38
//Max single delta_clk_per_msec increment = clk_per_msec / MAX_DIV_INK;
39
#define MAX_DIV_INK 30000
120 giacomo 40
 
41
signed long long init_tsc;
42
signed long long init_nsec; //Warp around 292 years !!
43
signed long long clk_per_msec;
44
 
45
signed long last_delta_clk_per_msec;
46
signed long total_delta_clk_per_msec;
47
 
48
unsigned char save_CMOS_regA;
49
unsigned char save_CMOS_regB;
50
 
51
void HandlerIRQ8(void *p)
52
{
53
 
54
   unsigned char set;
55
 
56
   static unsigned long init_step = 0;
57
 
58
   signed long long actual_tsc;
126 giacomo 59
 
60
   signed long max_dcms = clk_per_msec / MAX_DIV_INK;
120 giacomo 61
 
62
   signed long long dt,dn;
63
   signed long delta_clk_per_msec;
126 giacomo 64
 
120 giacomo 65
   cli();
66
 
67
   CMOS_READ(0x0C,set);
68
 
69
   rdtscll(actual_tsc);
70
 
71
   //Delta TSC
72
   dt = actual_tsc - init_tsc;
73
 
74
   init_tsc = actual_tsc;
75
 
76
   UNSIGNED_TSC2NSEC(dt,&dn);
77
 
78
   //Offset
79
   init_nsec += dn;
80
 
81
   if (init_step < 5) {
82
           init_step++;
83
           return;
84
   }
85
 
86
   dn = dn % 1000000000 - 500000000;
87
 
88
   //Delta clk/msec
89
   delta_clk_per_msec = dn * clk_per_msec / (500000000 - dn);
90
 
91
   //clk_per_msec adjustment
92
   if (delta_clk_per_msec < 0) {
93
 
126 giacomo 94
        if (delta_clk_per_msec > -max_dcms)
120 giacomo 95
                clk_per_msec += delta_clk_per_msec;
96
        else
126 giacomo 97
                clk_per_msec -= max_dcms;
120 giacomo 98
   } else {
99
 
126 giacomo 100
        if (delta_clk_per_msec < max_dcms)
120 giacomo 101
                clk_per_msec += delta_clk_per_msec;
102
        else
126 giacomo 103
                clk_per_msec += max_dcms;
120 giacomo 104
   }
105
 
106
   last_delta_clk_per_msec = delta_clk_per_msec;
107
   total_delta_clk_per_msec += delta_clk_per_msec;
108
 
109
   sti();
110
 
111
}
112
 
113
#define HZ 100
114
 
115
#ifdef CONFIG_MELAN
116
#  define CLOCK_TICK_RATE 1189200 /* AMD Elan has different frequency! */
117
#else
118
#  define CLOCK_TICK_RATE 1193180 /* Underlying HZ */
119
#endif
120
 
121
#define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)
122
 
123
#define CALIBRATE_LATCH (5 * LATCH)
124
#define CALIBRATE_TIME  (5 * 1000020/HZ)
125
 
126
//TSC Calibration (idea from the linux kernel code)
127
void ll_calibrate_tsc(void)
128
{
129
 
130
        signed long long start;
131
        signed long long end;
132
        signed long long dtsc;
133
 
134
        signed long start_8253, end_8253, delta_8253;
135
 
136
        cli();
137
 
138
        /* Set the Gate high, disable speaker */
139
        outp(0x61, (inp(0x61) & ~0x02) | 0x01);
140
 
141
        outp(0x43,0xB0);                        /* binary, mode 0, LSB/MSB, Ch 2 */
142
        outp(0x42,CALIBRATE_LATCH & 0xff);      /* LSB of count */
143
        outp(0x42,CALIBRATE_LATCH >> 8);        /* MSB of count */
144
 
145
        rdtscll(start);
146
        outp(0x43,0x00);
147
        start_8253 = inp(0x42);
148
        start_8253 |= inp(0x42) << 8;          
149
 
150
        do {
151
 
152
            outp(0x43,0x00);
153
            end_8253 = inp(0x42);
154
            end_8253 |= inp(0x42) << 8;
155
 
156
        } while (end_8253 > 10);
157
 
158
        rdtscll(end);
159
        outp(0x43,0x00);
160
        end_8253 = inp(0x42);
161
        end_8253 |= inp(0x42) << 8;
162
 
163
        //Delta TSC
164
        dtsc = end - start;
165
 
166
        //Delta PIT
167
        delta_8253 = start_8253 - end_8253 + 1;
168
 
169
        if (delta_8253 > 0xFFFF) {
170
                message("Error calculating Delta PIT\n");
171
                ll_abort(10);
172
        }
173
 
174
        message("Delta TSC               = %10ld\n",(long)dtsc);
175
 
176
        message("Delta PIT               = %10ld\n",(long)delta_8253);
177
 
178
        clk_per_msec = dtsc * CALIBRATE_LATCH * 1000 / delta_8253 / CALIBRATE_TIME;
179
 
180
        message("Calibrated Clk_per_msec = %10ld\n",(long)clk_per_msec);
181
 
182
        sti();
183
 
184
}
185
 
186
//Low level time read function
131 giacomo 187
void ll_read_timespec(struct timespec *tspec)
120 giacomo 188
{
189
 
190
    signed long long actual_tsc;
191
    signed long long dt,dn;
192
 
124 giacomo 193
    if (clk_per_msec <= 0) {
194
            NULL_TIMESPEC(tspec);
195
            return;
196
    }
197
 
120 giacomo 198
    rdtscll(actual_tsc);
199
 
200
    dt = actual_tsc - init_tsc;
201
 
202
    UNSIGNED_TSC2NSEC(dt,&dn);
203
 
204
    tspec->tv_sec = (init_nsec + dn) / 1000000000;
205
    tspec->tv_nsec = (init_nsec + dn) % 1000000000;
206
 
207
}
208
 
209
void ll_init_advtimer()
210
{
211
 
212
    if (use_tsc) {
213
 
214
        ll_calibrate_tsc();
215
 
216
        last_delta_clk_per_msec = 0;
217
        total_delta_clk_per_msec = 0;
218
 
219
        rdtscll(init_tsc); // Read start TSC
220
        init_nsec = 0;
221
 
222
        if (use_cmos) {
223
 
131 giacomo 224
            message("CMOS adjustement enabled\n");
120 giacomo 225
 
226
            cli();         
227
 
228
            irq_bind(8, HandlerIRQ8, INT_FORCE);
229
 
230
            CMOS_READ(0x0A,save_CMOS_regA);
231
            CMOS_READ(0x0B,save_CMOS_regB);
232
 
233
            CMOS_WRITE(0x0A,0x2F); // Set 2 Hz Periodic Interrupt
234
            CMOS_WRITE(0x0B,0x42); // Enable Interrupt
235
 
236
            irq_unmask(8);
237
 
238
            sti();
239
 
240
        }
241
 
242
    } else {
243
 
244
        use_cmos = 0;
245
 
246
   }
247
 
248
}
249
 
131 giacomo 250
void ll_restore_CMOS()
120 giacomo 251
{
252
        if (use_cmos) {
253
                cli();
254
 
255
                irq_mask(8);
256
 
257
                CMOS_WRITE(0x0A,save_CMOS_regA);
258
                CMOS_WRITE(0x0B,save_CMOS_regB);
259
 
260
                sti();         
261
        }
262
}