1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/*
* Copyright 1997 through 2004 by Marc Aurele La France (TSI @ UQV), tsi@xfree86.org
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of Marc Aurele La France not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. Marc Aurele La France makes no representations
* about the suitability of this software for any purpose. It is provided
* "as-is" without express or implied warranty.
*
* MARC AURELE LA FRANCE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
* EVENT SHALL MARC AURELE LA FRANCE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/*
* For all supported programmable clock generators, the driver will ignore any
* XF86Config clock line and programme, as needed, the clock number reserved by
* the BIOS for accelerated drivers. The driver's mode initialisation routine
* finds integers N, M and D such that
*
* N
* R * ------- MHz
* M * D
*
* best approximates the mode's clock frequency, where R is the crystal-
* generated reference frequency (usually 14.318 MHz). D is a power of 2
* except for those integrated controllers that also offer odd dividers.
* Different clock generators have different restrictions on the value N, M and
* D can assume. The driver contains an internal table to record these
* restrictions (among other things). The resulting values of N, M and D are
* then encoded in a generator-specific way and used to programme the clock.
* The Mach64's clock divider is not used in this case.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include "ati.h"
#include "atichip.h"
#include "atidac.h"
#include "atidsp.h"
#include "atimach64io.h"
#include "atimode.h"
#include "atiwonderio.h"
/*
* Definitions related to programmable clock generators.
*/
static CARD16 ATIPostDividers[] = {1, 2, 4, 8, 16, 32, 64, 128},
ATI264xTPostDividers[] = {1, 2, 4, 8, 3, 0, 6, 12};
ClockRec ATIClockDescriptors[] =
{
{
0, 0, 0, 1, 1,
1, 1, 0,
0, NULL,
"Non-programmable"
},
{
257, 512, 257, 1, 1,
46, 46, 0,
4, ATIPostDividers,
"ATI 18818 or ICS 2595 or similar"
},
{
2, 129, 2, 1, 1,
8, 14, 2,
8, ATIPostDividers,
"SGS-Thompson 1703 or similar"
},
{
16, 263, 8, 8, 9,
4, 12, 2,
4, ATIPostDividers,
"Chrontel 8398 or similar"
},
{
2, 255, 0, 1, 1,
45, 45, 0,
4, ATI264xTPostDividers,
"Internal"
},
{
2, 257, 2, 1, 1,
2, 32, 2,
4, ATIPostDividers,
"AT&T 20C408 or similar"
},
{
65, 128, 65, 1, 1,
2, 14, 0,
4, ATIPostDividers,
"IBM RGB 514 or similar"
}
};
/*
* ATIClockPreInit --
*
* This function is called by ATIPreInit() and handles the XF86Config clocks
* line (or lack thereof).
*/
void
ATIClockPreInit
(
ScrnInfoPtr pScreenInfo,
ATIPtr pATI
)
{
/*
* Recognise supported clock generators. This involves telling the
* rest of the server about it and (re-)initializing the XF86Config
* clocks line.
*/
pScreenInfo->progClock = TRUE;
xf86DrvMsg(pScreenInfo->scrnIndex, X_PROBED,
"%s programmable clock generator detected.\n",
pATI->ClockDescriptor.ClockName);
if (pATI->ReferenceDenominator == 1)
xf86DrvMsg(pScreenInfo->scrnIndex, X_PROBED,
"Reference clock %.3f MHz.\n",
(double)pATI->ReferenceNumerator / 1000.0);
else
xf86DrvMsg(pScreenInfo->scrnIndex, X_PROBED,
"Reference clock %.6g/%d (%.3f) MHz.\n",
(double)pATI->ReferenceNumerator / 1000.0,
pATI->ReferenceDenominator,
(double)pATI->ReferenceNumerator /
((double)pATI->ReferenceDenominator * 1000.0));
#if defined(__sparc__)
if (pATI->ReferenceNumerator != 315000 &&
pATI->ReferenceDenominator != 11)
{
xf86DrvMsg(pScreenInfo->scrnIndex, X_INFO,
"If modes do not work on Ultra 5/10 or Blade 100/150,\n"
"set option \"reference_clock\" to \"28.636 MHz\"\n");
}
#endif
if (pATI->ProgrammableClock == ATI_CLOCK_CH8398)
{ /* First two are fixed */
pScreenInfo->numClocks = 2;
pScreenInfo->clock[0] = 25175;
pScreenInfo->clock[1] = 28322;
}
else if (pATI->ProgrammableClock == ATI_CLOCK_INTERNAL)
{
/*
* The integrated PLL generates clocks as if the reference
* frequency were doubled.
*/
pATI->ReferenceNumerator <<= 1;
}
}
/*
* ATIClockCalculate --
*
* This function is called to generate, if necessary, the data needed for clock
* programming, and set clock select bits in various register values.
*/
Bool
ATIClockCalculate
(
int iScreen,
ATIPtr pATI,
ATIHWPtr pATIHW,
DisplayModePtr pMode
)
{
int N, M, D;
int ClockSelect, N1, MinimumGap;
int Frequency, Multiple; /* Used as temporaries */
/* Set default values */
pATIHW->FeedbackDivider = pATIHW->ReferenceDivider = pATIHW->PostDivider = 0;
if (((pATI->ProgrammableClock == ATI_CLOCK_CH8398) &&
(pMode->ClockIndex < 2)))
{
xf86DrvMsg(iScreen, X_ERROR,
"First two clocks of Chrontel 8398 clock generator are fixed\n");
return FALSE;
}
{
/* Generate clock programme word, using units of kHz */
MinimumGap = ((unsigned int)(-1)) >> 1;
/* Loop through reference dividers */
for (M = pATI->ClockDescriptor.MinM;
M <= pATI->ClockDescriptor.MaxM;
M++)
{
/* Loop through post-dividers */
for (D = 0; D < pATI->ClockDescriptor.NumD; D++)
{
if (!pATI->ClockDescriptor.PostDividers[D])
continue;
/* Limit undivided VCO to maxClock */
if (pATI->maxClock &&
((pATI->maxClock / pATI->ClockDescriptor.PostDividers[D]) <
pMode->Clock))
continue;
/*
* Calculate closest feedback divider and apply its
* restrictions.
*/
Multiple = M * pATI->ReferenceDenominator *
pATI->ClockDescriptor.PostDividers[D];
N = ATIDivide(pMode->Clock * Multiple,
pATI->ReferenceNumerator, 0, 0);
if (N < pATI->ClockDescriptor.MinN)
N = pATI->ClockDescriptor.MinN;
else if (N > pATI->ClockDescriptor.MaxN)
N = pATI->ClockDescriptor.MaxN;
N -= pATI->ClockDescriptor.NAdjust;
N1 = (N / pATI->ClockDescriptor.N1) * pATI->ClockDescriptor.N2;
if (N > N1)
N = ATIDivide(N1 + 1, pATI->ClockDescriptor.N1, 0, 1);
N += pATI->ClockDescriptor.NAdjust;
N1 += pATI->ClockDescriptor.NAdjust;
for (; ; N = N1)
{
/* Pick the closest setting */
Frequency = abs(ATIDivide(N * pATI->ReferenceNumerator,
Multiple, 0, 0) - pMode->Clock);
if ((Frequency < MinimumGap) ||
((Frequency == MinimumGap) &&
(pATIHW->FeedbackDivider < N)))
{
/* Save settings */
pATIHW->FeedbackDivider = N;
pATIHW->ReferenceDivider = M;
pATIHW->PostDivider = D;
MinimumGap = Frequency;
}
if (N <= N1)
break;
}
}
}
Multiple = pATIHW->ReferenceDivider * pATI->ReferenceDenominator *
pATI->ClockDescriptor.PostDividers[pATIHW->PostDivider];
Frequency = pATIHW->FeedbackDivider * pATI->ReferenceNumerator;
Frequency = ATIDivide(Frequency, Multiple, 0, 0);
if (abs(Frequency - pMode->Clock) > CLOCK_TOLERANCE)
{
xf86DrvMsg(iScreen, X_ERROR,
"Unable to programme clock %.3fMHz for mode %s.\n",
(double)(pMode->Clock) / 1000.0, pMode->name);
return FALSE;
}
pMode->SynthClock = Frequency;
ClockSelect = pATI->ClockNumberToProgramme;
xf86ErrorFVerb(4,
"\n Programming clock %d to %.3fMHz for mode %s."
" N=%d, M=%d, D=%d.\n",
ClockSelect, (double)Frequency / 1000.0, pMode->name,
pATIHW->FeedbackDivider, pATIHW->ReferenceDivider,
pATIHW->PostDivider);
if (pATI->Chip >= ATI_CHIP_264VTB)
ATIDSPCalculate(pATI, pATIHW, pMode);
}
/* Set clock select bits */
pATIHW->clock = ClockSelect;
{
pATIHW->clock_cntl = CLOCK_STROBE |
SetBits(ClockSelect, CLOCK_SELECT | CLOCK_DIVIDER);
}
return TRUE;
}
/*
* ATIClockSet --
*
* This function is called to programme a clock for the mode being set.
*/
void
ATIClockSet
(
ATIPtr pATI,
ATIHWPtr pATIHW
)
{
CARD32 crtc_gen_cntl, tmp;
CARD8 clock_cntl0;
CARD8 tmp2;
unsigned int Programme;
int N = pATIHW->FeedbackDivider - pATI->ClockDescriptor.NAdjust;
int M = pATIHW->ReferenceDivider - pATI->ClockDescriptor.MAdjust;
int D = pATIHW->PostDivider;
/* Temporarily switch to accelerator mode */
crtc_gen_cntl = inr(CRTC_GEN_CNTL);
if (!(crtc_gen_cntl & CRTC_EXT_DISP_EN))
outr(CRTC_GEN_CNTL, crtc_gen_cntl | CRTC_EXT_DISP_EN);
switch (pATI->ProgrammableClock)
{
case ATI_CLOCK_ICS2595:
clock_cntl0 = in8(CLOCK_CNTL);
Programme = (SetBits(pATIHW->clock, ICS2595_CLOCK) |
SetBits(N, ICS2595_FB_DIV) | SetBits(D, ICS2595_POST_DIV)) ^
ICS2595_TOGGLE;
ATIDelay(50000); /* 50 milliseconds */
(void)xf86DisableInterrupts();
/* Send all 20 bits of programme word */
while (Programme >= CLOCK_BIT)
{
tmp = (Programme & CLOCK_BIT) | CLOCK_STROBE;
out8(CLOCK_CNTL, tmp);
ATIDelay(26); /* 26 microseconds */
out8(CLOCK_CNTL, tmp | CLOCK_PULSE);
ATIDelay(26); /* 26 microseconds */
Programme >>= 1;
}
xf86EnableInterrupts();
/* Restore register */
out8(CLOCK_CNTL, clock_cntl0 | CLOCK_STROBE);
break;
case ATI_CLOCK_STG1703:
(void)ATIGetDACCmdReg(pATI);
(void)in8(M64_DAC_MASK);
out8(M64_DAC_MASK, (pATIHW->clock << 1) + 0x20U);
out8(M64_DAC_MASK, 0);
out8(M64_DAC_MASK, SetBits(N, 0xFFU));
out8(M64_DAC_MASK, SetBits(M, 0x1FU) | SetBits(D, 0xE0U));
break;
case ATI_CLOCK_CH8398:
tmp = inr(DAC_CNTL) | (DAC_EXT_SEL_RS2 | DAC_EXT_SEL_RS3);
outr(DAC_CNTL, tmp);
out8(M64_DAC_WRITE, pATIHW->clock);
out8(M64_DAC_DATA, SetBits(N, 0xFFU));
out8(M64_DAC_DATA, SetBits(M, 0x3FU) | SetBits(D, 0xC0U));
out8(M64_DAC_MASK, 0x04U);
outr(DAC_CNTL, tmp & ~(DAC_EXT_SEL_RS2 | DAC_EXT_SEL_RS3));
tmp2 = in8(M64_DAC_WRITE);
out8(M64_DAC_WRITE, (tmp2 & 0x70U) | 0x80U);
outr(DAC_CNTL, tmp & ~DAC_EXT_SEL_RS2);
break;
case ATI_CLOCK_INTERNAL:
/* Reset VCLK generator */
ATIMach64PutPLLReg(PLL_VCLK_CNTL, pATIHW->pll_vclk_cntl);
/* Set post-divider */
tmp2 = pATIHW->clock << 1;
tmp = ATIMach64GetPLLReg(PLL_VCLK_POST_DIV);
tmp &= ~(0x03U << tmp2);
tmp |= SetBits(D, 0x03U) << tmp2;
ATIMach64PutPLLReg(PLL_VCLK_POST_DIV, tmp);
/* Set extended post-divider */
tmp = ATIMach64GetPLLReg(PLL_XCLK_CNTL);
tmp &= ~(SetBits(1, PLL_VCLK0_XDIV) << pATIHW->clock);
tmp |= SetBits(D >> 2, PLL_VCLK0_XDIV) << pATIHW->clock;
ATIMach64PutPLLReg(PLL_XCLK_CNTL, tmp);
/* Set feedback divider */
tmp = PLL_VCLK0_FB_DIV + pATIHW->clock;
ATIMach64PutPLLReg(tmp, SetBits(N, 0xFFU));
/* End VCLK generator reset */
ATIMach64PutPLLReg(PLL_VCLK_CNTL,
pATIHW->pll_vclk_cntl & ~PLL_VCLK_RESET);
/* Reset write bit */
ATIMach64AccessPLLReg(pATI, 0, FALSE);
break;
case ATI_CLOCK_ATT20C408:
(void)ATIGetDACCmdReg(pATI);
tmp = in8(M64_DAC_MASK);
(void)ATIGetDACCmdReg(pATI);
out8(M64_DAC_MASK, tmp | 1);
out8(M64_DAC_WRITE, 1);
out8(M64_DAC_MASK, tmp | 9);
ATIDelay(400); /* 400 microseconds */
tmp2 = (pATIHW->clock << 2) + 0x40U;
out8(M64_DAC_WRITE, tmp2);
out8(M64_DAC_MASK, SetBits(N, 0xFFU));
out8(M64_DAC_WRITE, ++tmp2);
out8(M64_DAC_MASK, SetBits(M, 0x3FU) | SetBits(D, 0xC0U));
out8(M64_DAC_WRITE, ++tmp2);
out8(M64_DAC_MASK, 0x77U);
ATIDelay(400); /* 400 microseconds */
out8(M64_DAC_WRITE, 1);
out8(M64_DAC_MASK, tmp);
break;
case ATI_CLOCK_IBMRGB514:
/*
* Here, only update in-core data. It will be written out later by
* ATIRGB514Set().
*/
tmp = (pATIHW->clock << 1) + 0x20U;
pATIHW->ibmrgb514[tmp] =
(SetBits(N, 0x3FU) | SetBits(D, 0xC0U)) ^ 0xC0U;
pATIHW->ibmrgb514[tmp + 1] = SetBits(M, 0x3FU);
break;
default:
break;
}
(void)in8(M64_DAC_WRITE); /* Clear DAC counter */
/* Restore register */
if (!(crtc_gen_cntl & CRTC_EXT_DISP_EN))
outr(CRTC_GEN_CNTL, crtc_gen_cntl);
}
|