2 * Copyright (c) 2007 NVIDIA, Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "nv_include.h"
30 #include "nv50_type.h"
31 #include "nv50_cursor.h"
32 #include "nv50_display.h"
33 #include "nv50_output.h"
35 static void NV50CrtcShowHideCursor(xf86CrtcPtr crtc, Bool show, Bool update);
38 * PLL calculation. pclk is in kHz.
41 NV50CalcPLL(float pclk, int *pNA, int *pMA, int *pNB, int *pMB, int *pP)
43 const float refclk = 27000.0f;
44 const float minVcoA = 100000;
45 const float maxVcoA = 400000;
46 const float minVcoB = 600000;
47 float maxVcoB = 1400000;
48 const float minUA = 2000;
49 const float maxUA = 400000;
50 const float minUB = 50000;
51 const float maxUB = 200000;
52 const int minNA = 1, maxNA = 255;
53 const int minNB = 1, maxNB = 31;
54 const int minMA = 1, maxMA = 255;
55 const int minMB = 1, maxMB = 31;
56 const int minP = 0, maxP = 6;
60 int na, ma, nb, mb, p;
61 float bestError = FLT_MAX;
63 *pNA = *pMA = *pNB = *pMB = *pP = 0;
65 if(maxVcoB < pclk + pclk / 200)
66 maxVcoB = pclk + pclk / 200;
67 if(minVcoB / (1 << maxP) > pclk)
68 pclk = minVcoB / (1 << maxP);
70 vcoB = maxVcoB - maxVcoB / 200;
72 vcoB /= 1 << (lowP + 1);
74 while(pclk <= vcoB && lowP < maxP)
80 vcoB = maxVcoB + maxVcoB / 200;
82 vcoB /= 1 << (highP + 1);
84 while(pclk <= vcoB && highP < maxP)
90 for(p = lowP; p <= highP; p++)
92 for(ma = minMA; ma <= maxMA; ma++)
94 if(refclk / ma < minUA)
96 else if(refclk / ma > maxUA)
99 for(na = minNA; na <= maxNA; na++)
101 if(refclk * na / ma < minVcoA || refclk * na / ma > maxVcoA)
104 for(mb = minMB; mb <= maxMB; mb++)
106 if(refclk * na / ma / mb < minUB)
108 else if(refclk * na / ma / mb > maxUB)
111 nb = rint(pclk * (1 << p) * (ma / (float)na) * mb / refclk);
119 float freq = refclk * (na / (float)ma) * (nb / (float)mb) / (1 << p);
120 float error = fabsf(pclk - freq);
121 if(error < bestError) {
136 void NV50CrtcSetPClk(xf86CrtcPtr crtc)
138 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
139 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
140 ScrnInfoPtr pScrn = crtc->scrn;
141 NVPtr pNv = NVPTR(pScrn);
142 int lo_n, lo_m, hi_n, hi_m, p, i;
143 /* These clocks are probably rerouted from the 0x4000 range to the 0x610000 range */
144 CARD32 lo = NVRead(pNv, nv_crtc->head ? NV50_CRTC_VPLL2_A : NV50_CRTC_VPLL1_A);
145 CARD32 hi = NVRead(pNv, nv_crtc->head ? NV50_CRTC_VPLL2_B : NV50_CRTC_VPLL1_B);
147 NVWrite(pNv, 0x00614100 + nv_crtc->head * 0x800, 0x10000610);
151 NV50CalcPLL(nv_crtc->pclk, &lo_n, &lo_m, &hi_n, &hi_m, &p);
153 lo |= (lo_m << 16) | lo_n;
154 hi |= (p << 28) | (hi_m << 16) | hi_n;
155 NVWrite(pNv, nv_crtc->head ? NV50_CRTC_VPLL2_A : NV50_CRTC_VPLL1_A, lo);
156 NVWrite(pNv, nv_crtc->head ? NV50_CRTC_VPLL2_B : NV50_CRTC_VPLL1_B, hi);
157 NVWrite(pNv, 0x00614200 + nv_crtc->head * 0x800, 0);
159 for(i = 0; i < xf86_config->num_output; i++) {
160 xf86OutputPtr output = xf86_config->output[i];
162 if(output->crtc != crtc)
164 NV50OutputSetPClk(output, nv_crtc->pclk);
169 NV50CrtcGetHead(xf86CrtcPtr crtc)
171 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
172 return nv_crtc->head;
176 NV50DispPreInit(ScrnInfoPtr pScrn)
178 NVPtr pNv = NVPTR(pScrn);
179 /* These labels are guesswork based on symmetry (2 SOR's and 3 DAC's exist)*/
180 NVWrite(pNv, 0x00610184, NVRead(pNv, 0x00614004));
181 NVWrite(pNv, 0x00610190 + SOR0 * 0x10, NVRead(pNv, 0x00616100 + SOR0 * 0x800));
182 NVWrite(pNv, 0x00610190 + SOR1 * 0x10, NVRead(pNv, 0x00616100 + SOR1 * 0x800));
183 NVWrite(pNv, 0x00610194 + SOR0 * 0x10, NVRead(pNv, 0x00616104 + SOR0 * 0x800));
184 NVWrite(pNv, 0x00610194 + SOR1 * 0x10, NVRead(pNv, 0x00616104 + SOR1 * 0x800));
185 NVWrite(pNv, 0x00610198 + SOR0 * 0x10, NVRead(pNv, 0x00616108 + SOR0 * 0x800));
186 NVWrite(pNv, 0x00610198 + SOR1 * 0x10, NVRead(pNv, 0x00616108 + SOR1 * 0x800));
187 NVWrite(pNv, 0x0061019c + SOR0 * 0x10, NVRead(pNv, 0x0061610c + SOR0 * 0x800));
188 NVWrite(pNv, 0x0061019c + SOR1 * 0x10, NVRead(pNv, 0x0061610c + SOR1 * 0x800));
189 NVWrite(pNv, 0x006101d0 + DAC0 * 0x4, NVRead(pNv, 0x0061a000 + DAC0 * 0x800));
190 NVWrite(pNv, 0x006101d0 + DAC1 * 0x4, NVRead(pNv, 0x0061a000 + DAC1 * 0x800));
191 NVWrite(pNv, 0x006101d0 + DAC2 * 0x4, NVRead(pNv, 0x0061a000 + DAC2 * 0x800));
192 NVWrite(pNv, 0x006101e0 + SOR0 * 0x4, NVRead(pNv, 0x0061c000 + SOR0 * 0x800));
193 NVWrite(pNv, 0x006101e0 + SOR1 * 0x4, NVRead(pNv, 0x0061c000 + SOR1 * 0x800));
194 NVWrite(pNv, 0x0061a004 + DAC0 * 0x800, 0x80550000);
195 NVWrite(pNv, 0x0061a010 + DAC0 * 0x800, 0x00000001);
196 NVWrite(pNv, 0x0061a004 + DAC1 * 0x800, 0x80550000);
197 NVWrite(pNv, 0x0061a010 + DAC1 * 0x800, 0x00000001);
198 NVWrite(pNv, 0x0061a004 + DAC2 * 0x800, 0x80550000);
199 NVWrite(pNv, 0x0061a010 + DAC2 * 0x800, 0x00000001);
205 NV50DispInit(ScrnInfoPtr pScrn)
207 NVPtr pNv = NVPTR(pScrn);
208 if (NVRead(pNv, 0x00610024) & 0x100) {
209 NVWrite(pNv, 0x00610024, 0x100);
210 NVWrite(pNv, 0x006194e8, NVRead(pNv, 0x006194e8) & ~1);
211 while (NVRead(pNv, 0x006194e8) & 2);
214 NVWrite(pNv, 0x00610200, 0x2b00);
215 /* A bugfix (#12637) from the nv driver, to unlock the driver if it's left in a poor state */
217 CARD32 val = NVRead(pNv, 0x00610200);
218 if ((val & 0x9f0000) == 0x20000)
219 NVWrite(pNv, 0x00610200, val | 0x800000);
221 if ((val & 0x3f0000) == 0x30000)
222 NVWrite(pNv, 0x00610200, val | 0x200000);
223 } while ((NVRead(pNv, 0x00610200) & 0x1e0000) != 0);
224 NVWrite(pNv, 0x00610300, 0x1);
225 NVWrite(pNv, 0x00610200, 0x1000b03);
226 while (!(NVRead(pNv, 0x00610200) & 0x40000000));
228 NV50DisplayCommand(pScrn, 0x84, 0);
229 NV50DisplayCommand(pScrn, 0x88, 0);
230 NV50DisplayCommand(pScrn, 0x874, 0);
231 NV50DisplayCommand(pScrn, 0x800, 0);
232 NV50DisplayCommand(pScrn, 0x810, 0);
233 NV50DisplayCommand(pScrn, 0x82c, 0);
239 NV50DispShutdown(ScrnInfoPtr pScrn)
241 NVPtr pNv = NVPTR(pScrn);
242 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
245 for(i = 0; i < xf86_config->num_crtc; i++) {
246 xf86CrtcPtr crtc = xf86_config->crtc[i];
248 NV50CrtcBlankScreen(crtc, TRUE);
251 NV50DisplayCommand(pScrn, 0x80, 0);
253 for(i = 0; i < xf86_config->num_crtc; i++) {
254 xf86CrtcPtr crtc = xf86_config->crtc[i];
257 const CARD32 mask = 4 << NV50CrtcGetHead(crtc);
259 NVWrite(pNv, 0x00610024, mask);
260 while(!(NVRead(pNv, 0x00610024) & mask));
264 NVWrite(pNv, 0x00610200, 0x0);
265 NVWrite(pNv, 0x00610300, 0x0);
266 while ((NVRead(pNv, 0x00610200) & 0x1e0000) != 0);
267 while ((NVRead(pNv, 0x0061c030 + SOR0 * 0x800) & 0x10000000));
268 while ((NVRead(pNv, 0x0061c030 + SOR1 * 0x800) & 0x10000000));
272 NV50CrtcDoModeFixup(DisplayModePtr dst, const DisplayModePtr src)
274 /* Magic mode timing fudge factor */
275 const int fudge = ((src->Flags & V_INTERLACE) && (src->Flags & V_DBLSCAN)) ? 2 : 1;
276 const int interlaceDiv = (src->Flags & V_INTERLACE) ? 2 : 1;
278 /* Stash the src timings in the Crtc fields in dst */
279 dst->CrtcHBlankStart = src->CrtcVTotal << 16 | src->CrtcHTotal;
280 dst->CrtcHSyncEnd = ((src->CrtcVSyncEnd - src->CrtcVSyncStart) / interlaceDiv - 1) << 16 |
281 (src->CrtcHSyncEnd - src->CrtcHSyncStart - 1);
282 dst->CrtcHBlankEnd = ((src->CrtcVBlankEnd - src->CrtcVSyncStart) / interlaceDiv - fudge) << 16 |
283 (src->CrtcHBlankEnd - src->CrtcHSyncStart - 1);
284 dst->CrtcHTotal = ((src->CrtcVTotal - src->CrtcVSyncStart + src->CrtcVBlankStart) / interlaceDiv - fudge) << 16 |
285 (src->CrtcHTotal - src->CrtcHSyncStart + src->CrtcHBlankStart - 1);
286 dst->CrtcHSkew = ((src->CrtcVTotal + src->CrtcVBlankEnd - src->CrtcVSyncStart) / 2 - 2) << 16 |
287 ((2*src->CrtcVTotal - src->CrtcVSyncStart + src->CrtcVBlankStart) / 2 - 2);
291 NV50CrtcModeFixup(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
293 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
295 if (nv_crtc->skipModeFixup)
298 NV50CrtcDoModeFixup(adjusted_mode, mode);
303 NV50CrtcModeSet(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode, int x, int y)
305 ScrnInfoPtr pScrn = crtc->scrn;
306 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
307 const int HDisplay = adjusted_mode->HDisplay;
308 const int VDisplay = adjusted_mode->VDisplay;
310 nv_crtc->pclk = adjusted_mode->Clock;
312 /* NV50CrtcCommand includes head offset */
313 NV50CrtcCommand(crtc, NV50_CRTC0_CLOCK, adjusted_mode->Clock | 0x800000);
314 NV50CrtcCommand(crtc, NV50_CRTC0_INTERLACE, (adjusted_mode->Flags & V_INTERLACE) ? 2 : 0);
315 NV50CrtcCommand(crtc, 0x810, 0);
316 NV50CrtcCommand(crtc, 0x82c, 0);
317 /* This confirms my suspicion that recent nvidia hardware does no vertical programming */
318 /* NV40 still has it as a legacy mode, and i don't know how to do the "new" way, but it definately exists */
319 NV50CrtcCommand(crtc, NV50_CRTC0_HBLANK_START, adjusted_mode->CrtcHBlankStart);
320 NV50CrtcCommand(crtc, NV50_CRTC0_HSYNC_END, adjusted_mode->CrtcHSyncEnd);
321 NV50CrtcCommand(crtc, NV50_CRTC0_HBLANK_END, adjusted_mode->CrtcHBlankEnd);
322 NV50CrtcCommand(crtc, NV50_CRTC0_HTOTAL, adjusted_mode->CrtcHTotal);
323 if(adjusted_mode->Flags & V_INTERLACE) {
324 NV50CrtcCommand(crtc, 0x824, adjusted_mode->CrtcHSkew);
326 NV50CrtcCommand(crtc, NV50_CRTC0_FB_SIZE, pScrn->virtualY << 16 | pScrn->virtualX);
327 NV50CrtcCommand(crtc, NV50_CRTC0_PITCH, pScrn->displayWidth * (pScrn->bitsPerPixel / 8) | 0x100000);
328 switch(pScrn->depth) {
330 NV50CrtcCommand(crtc, NV50_CRTC0_DEPTH, NV50_CRTC0_DEPTH_8BPP);
333 NV50CrtcCommand(crtc, NV50_CRTC0_DEPTH, NV50_CRTC0_DEPTH_15BPP);
336 NV50CrtcCommand(crtc, NV50_CRTC0_DEPTH, NV50_CRTC0_DEPTH_16BPP);
339 NV50CrtcCommand(crtc, NV50_CRTC0_DEPTH, NV50_CRTC0_DEPTH_24BPP);
342 NV50CrtcSetDither(crtc, nv_crtc->ditherEnabled, FALSE);
343 NV50CrtcCommand(crtc, 0x8a8, 0x40000);
344 NV50CrtcCommand(crtc, NV50_CRTC0_FB_POS, y << 16 | x);
345 NV50CrtcCommand(crtc, NV50_CRTC0_SCRN_SIZE, VDisplay << 16 | HDisplay);
346 NV50CrtcCommand(crtc, 0x8d4, 0);
348 NV50CrtcBlankScreen(crtc, FALSE);
352 NV50CrtcBlankScreen(xf86CrtcPtr crtc, Bool blank)
354 ScrnInfoPtr pScrn = crtc->scrn;
355 NVPtr pNv = NVPTR(pScrn);
356 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
359 NV50CrtcShowHideCursor(crtc, FALSE, FALSE);
361 NV50CrtcCommand(crtc, NV50_CRTC0_CLUT_MODE, NV50_CRTC0_CLUT_MODE_BLANK);
362 NV50CrtcCommand(crtc, NV50_CRTC0_CLUT_OFFSET, 0);
363 if(pNv->NVArch != 0x50)
364 NV50CrtcCommand(crtc, 0x85c, 0);
365 NV50CrtcCommand(crtc, 0x874, 0);
366 if(pNv->NVArch != 0x50)
367 NV50CrtcCommand(crtc, 0x89c, 0);
369 NV50CrtcCommand(crtc, NV50_CRTC0_FB_OFFSET, pNv->FB->offset >> 8);
370 NV50CrtcCommand(crtc, 0x864, 0);
371 NVWrite(pNv, 0x00610380, 0);
372 /* RAM is clamped to 256 MiB. */
373 NVWrite(pNv, NV50_CRTC0_RAM_AMOUNT, pNv->RamAmountKBytes * 1024 - 1);
374 NVWrite(pNv, 0x00610388, 0x150000);
375 NVWrite(pNv, 0x0061038C, 0);
376 NV50CrtcCommand(crtc, NV50_CRTC0_CURSOR_OFFSET, pNv->Cursor->offset >> 8);
377 if(pNv->NVArch != 0x50)
378 NV50CrtcCommand(crtc, 0x89c, 1);
379 if(nv_crtc->cursorVisible)
380 NV50CrtcShowHideCursor(crtc, TRUE, FALSE);
381 NV50CrtcCommand(crtc, NV50_CRTC0_CLUT_MODE,
382 pScrn->depth == 8 ? NV50_CRTC0_CLUT_MODE_OFF : NV50_CRTC0_CLUT_MODE_ON);
383 NV50CrtcCommand(crtc, NV50_CRTC0_CLUT_OFFSET, pNv->CLUT->offset >> 8);
384 if(pNv->NVArch != 0x50)
385 NV50CrtcCommand(crtc, 0x85c, 1);
386 NV50CrtcCommand(crtc, 0x874, 1);
390 /******************************** Cursor stuff ********************************/
391 static void NV50CrtcShowHideCursor(xf86CrtcPtr crtc, Bool show, Bool update)
393 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
395 NV50CrtcCommand(crtc, NV50_CRTC0_CURSOR0,
396 show ? NV50_CRTC0_CURSOR0_SHOW : NV50_CRTC0_CURSOR0_HIDE);
398 nv_crtc->cursorVisible = show;
399 NV50CrtcCommand(crtc, 0x80, 0);
403 void NV50CrtcShowCursor(xf86CrtcPtr crtc)
405 NV50CrtcShowHideCursor(crtc, TRUE, TRUE);
408 void NV50CrtcHideCursor(xf86CrtcPtr crtc)
410 NV50CrtcShowHideCursor(crtc, FALSE, TRUE);
413 /******************************** CRTC stuff ********************************/
416 NV50CrtcPrepare(xf86CrtcPtr crtc)
418 ScrnInfoPtr pScrn = crtc->scrn;
419 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
420 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
423 for(i = 0; i < xf86_config->num_output; i++) {
424 xf86OutputPtr output = xf86_config->output[i];
427 output->funcs->mode_set(output, NULL, NULL);
430 nv_crtc->skipModeFixup = FALSE;
434 NV50CrtcSkipModeFixup(xf86CrtcPtr crtc)
436 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
437 nv_crtc->skipModeFixup = TRUE;
441 NV50CrtcSetDither(xf86CrtcPtr crtc, Bool dither, Bool update)
443 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
445 nv_crtc->ditherEnabled = dither;
447 NV50CrtcCommand(crtc, 0x8a0, dither ? 0x11 : 0);
449 NV50CrtcCommand(crtc, 0x80, 0);
452 static void ComputeAspectScale(DisplayModePtr mode, int *outX, int *outY)
454 float scaleX, scaleY, scale;
456 scaleX = mode->CrtcHDisplay / (float)mode->HDisplay;
457 scaleY = mode->CrtcVDisplay / (float)mode->VDisplay;
464 *outX = mode->HDisplay * scale;
465 *outY = mode->VDisplay * scale;
468 void NV50CrtcSetScale(xf86CrtcPtr crtc, DisplayModePtr mode, enum NV50ScaleMode scale)
470 int outX = 0, outY = 0;
473 case NV50_SCALE_ASPECT:
474 ComputeAspectScale(mode, &outX, &outY);
477 case NV50_SCALE_FILL:
478 outX = mode->CrtcHDisplay;
479 outY = mode->CrtcVDisplay;
481 case NV50_SCALE_CENTER:
482 outX = mode->HDisplay;
483 outY = mode->VDisplay;
487 if ((mode->Flags & V_DBLSCAN) || (mode->Flags & V_INTERLACE) ||
488 mode->HDisplay != outX || mode->VDisplay != outY) {
489 NV50CrtcCommand(crtc, 0x8a4, 9);
491 NV50CrtcCommand(crtc, 0x8a4, 0);
493 NV50CrtcCommand(crtc, 0x8d8, outY << 16 | outX);
494 NV50CrtcCommand(crtc, 0x8dc, outY << 16 | outX);
498 NV50CrtcCommit(xf86CrtcPtr crtc)
500 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
501 int i, crtc_mask = 0;
503 /* If any heads are unused, blank them */
504 for(i = 0; i < xf86_config->num_output; i++) {
505 xf86OutputPtr output = xf86_config->output[i];
508 /* XXXagp: This assumes that xf86_config->crtc[i] is HEADi */
509 crtc_mask |= 1 << NV50CrtcGetHead(output->crtc);
513 for(i = 0; i < xf86_config->num_crtc; i++) {
514 if(!((1 << i) & crtc_mask)) {
515 NV50CrtcBlankScreen(xf86_config->crtc[i], TRUE);
519 NV50CrtcCommand(crtc, 0x80, 0);