96976934f091104feae3065ec9e01fec5d448ce3
[dragonfly.git] / usr.sbin / pcvt / set2061 / ICD2061Aalt.c
1 /*
2  * This code is derived from code available from the STB bulletin board
3  *
4  * $FreeBSD: src/usr.sbin/pcvt/set2061/ICD2061Aalt.c,v 1.5 1999/12/29 05:07:54 peter Exp $
5  * $DragonFly: src/usr.sbin/pcvt/set2061/Attic/ICD2061Aalt.c,v 1.3 2004/03/24 17:46:23 cpressey Exp $
6  */
7
8 /* $XFree86: mit/server/ddx/x386/common_hw/ICD2061Aalt.c,v 2.6 1994/04/15 05:10:30 dawes Exp $ */
9
10 #ifndef _KERNEL
11 #include "compiler.h"
12 #else
13 #define GCCUSESGAS
14 #define PCVT_STANDALONE 1
15 #endif
16
17 #define SEQREG   0x03C4
18 #define MISCREG  0x03C2
19 #define MISCREAD 0x03CC
20
21 double fref = 14.31818 * 2.0;
22 char ascclk[] = "VIDEO CLOCK ?";
23
24 unsigned short clknum;
25 unsigned short vlbus_flag;
26 unsigned short card;
27 unsigned short crtcaddr;
28 unsigned short clockreg;
29
30 static double range[15] = {50.0, 51.0, 53.2, 58.5, 60.7, 64.4, 66.8, 73.5,
31                            75.6, 80.9, 83.2, 91.5, 100.0, 120.0, 120.0};
32
33 #ifdef __STDC__
34 static double genratio(unsigned int *p, unsigned int *q, double tgt);
35 static double f(unsigned int p, unsigned int q, double basefreq);
36 #if 0
37 static void prtbinary(unsigned int size, unsigned int val);
38 #endif
39 static void wait_vb();
40 static void wrt_clk_bit(unsigned int value);
41 static void init_clock(unsigned long setup, unsigned short crtcport);
42 #else
43 static double genratio();
44 static double f();
45 #if 0
46 static void prtbinary();
47 #endif
48 static void wait_vb();
49 static void wrt_clk_bit();
50 static void init_clock();
51 #endif
52
53 void
54 AltICD2061SetClock(long frequency,      /* in Hz */
55                    int select)
56 {
57    unsigned int m, mval, ival;
58    int i;
59    long dwv;
60    double realval;
61    double freq, fvco;
62    double dev, devx;
63    double delta, deltax;
64    unsigned int p, q;
65    unsigned int bestp, bestq;
66    unsigned char tmp;
67
68    crtcaddr=(inb(0x3CC) & 0x01) ? 0x3D4 : 0x3B4;
69
70
71    outb(crtcaddr, 0x11);        /* Unlock CRTC registers */
72    tmp = inb(crtcaddr + 1);
73    outb(crtcaddr + 1, tmp & ~0x80);
74
75    outw(crtcaddr, 0x4838);      /* Unlock S3 register set */
76    outw(crtcaddr, 0xA039);
77
78    clknum = select;
79
80    freq = ((double)frequency)/1000000.0;
81    if (freq > range[14])
82         freq =range[14];
83    else if (freq <= 6.99)
84       freq = 7.0;
85
86 /*
87  *  Calculate values to load into ICD 2061A clock chip to set frequency
88  */
89    delta = 999.0;
90    dev = 999.0;
91    ival = 99;
92    mval = 99;
93
94    fvco = freq / 2;
95    for (m = 0; m < 8; m++) {
96       fvco *= 2.0;
97       for (i = 14; i >= 0; i--)
98          if (fvco >= range[i])
99             break;
100       if (i < 0)
101          continue;
102       if (i == 14)
103          break;
104       devx = (fvco - (range[i] + range[i+1])/2)/fvco;
105       if (devx < 0)
106          devx = -devx;
107       deltax = genratio(&p, &q, fvco);
108       if (delta < deltax)
109          continue;
110       if (deltax < delta || devx < dev) {
111          bestp = p;
112          bestq = q;
113          delta = deltax;
114          dev = devx;
115          ival = i;
116          mval = m;
117       }
118    }
119    fvco = fref;
120    for (m=0; m<mval; m++)
121       fvco /= 2.0;
122    realval = f(bestp, bestq, fvco);
123    dwv = ((((((long)ival << 7) | bestp) << 3) | mval) << 7) | bestq;
124
125 /*
126  * Write ICD 2061A clock chip
127  */
128    init_clock(((unsigned long)dwv) | (((long)clknum) << 21), crtcaddr);
129
130    wait_vb();
131    wait_vb();
132    wait_vb();
133    wait_vb();
134    wait_vb();
135    wait_vb();
136    wait_vb();           /* 0.10 second delay... */
137 }
138
139 static double
140 f(unsigned int p, unsigned int q, double base)
141 {
142    return(base * (p + 3)/(q + 2));
143 }
144
145 static double
146 genratio(unsigned int *p, unsigned int *q, double tgt)
147 {
148    int k, m;
149    double test, mindiff;
150    unsigned int mmax;
151
152    mindiff = 999999999.0;
153    for (k = 13; k < 69; k++) {         /* q={15..71}:Constraint 2 on page 14 */
154       m = 50.0*k/fref - 3;
155       if (m < 0)
156          m = 0;
157       mmax = 120*k/fref - 3;           /* m..mmax is constraint 3 on page 14 */
158       if (mmax > 128)
159          mmax = 128;
160       while (m < mmax) {
161          test = f(m, k, fref) - tgt;
162          if (test < 0) test = -test;
163          if (mindiff > test) {
164             mindiff = test;
165             *p = m;
166             *q = k;
167          }
168          m++;
169       }
170    }
171    return (mindiff);
172 }
173
174 #if 0
175 static void
176 prtbinary(unsigned int size, unsigned int val)
177 {
178    unsigned int mask;
179    int k;
180
181    mask = 1;
182
183    for (k=size; --k > 0 || mask <= val/2;)
184       mask <<= 1;
185
186    while (mask) {
187       fputc((mask&val)? '1': '0' , stderr);
188       mask >>= 1;
189    }
190 }
191 #endif
192
193 static void
194 wait_vb(void)
195 {
196    while ((inb(crtcaddr+6) & 0x08) == 0)
197       ;
198    while (inb(crtcaddr+6) & 0x08)
199       ;
200 }
201
202
203 static void
204 init_clock(unsigned long setup, unsigned short crtcport)
205 {
206    unsigned char nclk[2], clk[2];
207    unsigned short restore42;
208    unsigned short oldclk;
209    unsigned short bitval;
210    int i;
211    unsigned char c;
212
213 #ifndef PCVT_STANDALONE
214    (void)xf86DisableInterrupts();
215 #endif
216
217    oldclk = inb(0x3CC);
218
219    outb(crtcport, 0x42);
220    restore42 = inb(crtcport+1);
221
222    outw(0x3C4, 0x0100);
223
224    outb(0x3C4, 1);
225    c = inb(0x3C5);
226    outb(0x3C5, 0x20 | c);
227
228    outb(crtcport, 0x42);
229    outb(crtcport+1, 0x03);
230
231    outw(0x3C4, 0x0300);
232
233    nclk[0] = oldclk & 0xF3;
234    nclk[1] = nclk[0] | 0x08;
235    clk[0] = nclk[0] | 0x04;
236    clk[1] = nclk[0] | 0x0C;
237
238    outb(crtcport, 0x42);
239    i = inw(crtcport);
240
241    outw(0x3C4, 0x0100);
242
243    wrt_clk_bit(oldclk | 0x08);
244    wrt_clk_bit(oldclk | 0x0C);
245    for (i=0; i<5; i++) {
246       wrt_clk_bit(nclk[1]);
247       wrt_clk_bit(clk[1]);
248    }
249    wrt_clk_bit(nclk[1]);
250    wrt_clk_bit(nclk[0]);
251    wrt_clk_bit(clk[0]);
252    wrt_clk_bit(nclk[0]);
253    wrt_clk_bit(clk[0]);
254    for (i=0; i<24; i++) {
255       bitval = setup & 0x01;
256       setup >>= 1;
257       wrt_clk_bit(clk[1-bitval]);
258       wrt_clk_bit(nclk[1-bitval]);
259       wrt_clk_bit(nclk[bitval]);
260       wrt_clk_bit(clk[bitval]);
261    }
262    wrt_clk_bit(clk[1]);
263    wrt_clk_bit(nclk[1]);
264    wrt_clk_bit(clk[1]);
265
266    outb(0x3C4, 1);
267    c = inb(0x3C5);
268    outb(0x3C5, 0xDF & c);
269
270    outb(crtcport, 0x42);
271    outb(crtcport+1, restore42);
272
273    outb(0x3C2, oldclk);
274
275    outw(0x3C4, 0x0300);
276
277 #ifndef PCVT_STANDALONE
278    xf86EnableInterrupts();
279 #endif
280
281 }
282
283 static void wrt_clk_bit(unsigned int value)
284 {
285    int j;
286
287    outb(0x3C2, value);
288    for (j=2; --j; )
289       inb(0x200);
290 }