2 * Copyright 2006 Dave Airlie
3 * Copyright 2007 Maarten Maathuis
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * this code uses ideas taken from the NVIDIA nv driver - the nvidia license
26 * decleration is at the bottom of this file as it is rather ugly
41 #include "mipointer.h"
42 #include "windowstr.h"
44 #include <X11/extensions/render.h>
47 #include "nv_include.h"
51 #define CRTC_INDEX 0x3d4
52 #define CRTC_DATA 0x3d5
53 #define CRTC_IN_STAT_1 0x3da
55 #define WHITE_VALUE 0x3F
56 #define BLACK_VALUE 0x00
57 #define OVERSCAN_VALUE 0x01
59 static void nv_crtc_load_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
60 static void nv_crtc_load_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
61 static void nv_crtc_load_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
62 static void nv_crtc_save_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
63 static void nv_crtc_save_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
64 static void nv_crtc_save_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
66 static CARD8 NVReadPVIO(xf86CrtcPtr crtc, CARD32 address)
68 ScrnInfoPtr pScrn = crtc->scrn;
69 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
70 NVPtr pNv = NVPTR(pScrn);
72 /* Only NV4x have two pvio ranges */
73 if (nv_crtc->head == 1 && pNv->Architecture == NV_ARCH_40) {
74 return NV_RD08(pNv->PVIO1, address);
76 return NV_RD08(pNv->PVIO0, address);
80 static void NVWritePVIO(xf86CrtcPtr crtc, CARD32 address, CARD8 value)
82 ScrnInfoPtr pScrn = crtc->scrn;
83 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
84 NVPtr pNv = NVPTR(pScrn);
86 /* Only NV4x have two pvio ranges */
87 if (nv_crtc->head == 1 && pNv->Architecture == NV_ARCH_40) {
88 NV_WR08(pNv->PVIO1, address, value);
90 NV_WR08(pNv->PVIO0, address, value);
94 static void NVWriteMiscOut(xf86CrtcPtr crtc, CARD8 value)
96 NVWritePVIO(crtc, VGA_MISC_OUT_W, value);
99 static CARD8 NVReadMiscOut(xf86CrtcPtr crtc)
101 return NVReadPVIO(crtc, VGA_MISC_OUT_R);
104 void NVWriteVGA(NVPtr pNv, int head, CARD8 index, CARD8 value)
106 volatile CARD8 *pCRTCReg = head ? pNv->PCIO1 : pNv->PCIO0;
108 NV_WR08(pCRTCReg, CRTC_INDEX, index);
109 NV_WR08(pCRTCReg, CRTC_DATA, value);
112 CARD8 NVReadVGA(NVPtr pNv, int head, CARD8 index)
114 volatile CARD8 *pCRTCReg = head ? pNv->PCIO1 : pNv->PCIO0;
116 NV_WR08(pCRTCReg, CRTC_INDEX, index);
117 return NV_RD08(pCRTCReg, CRTC_DATA);
120 void NVWriteVgaCrtc(xf86CrtcPtr crtc, CARD8 index, CARD8 value)
122 ScrnInfoPtr pScrn = crtc->scrn;
123 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
124 NVPtr pNv = NVPTR(pScrn);
126 NVWriteVGA(pNv, nv_crtc->head, index, value);
129 CARD8 NVReadVgaCrtc(xf86CrtcPtr crtc, CARD8 index)
131 ScrnInfoPtr pScrn = crtc->scrn;
132 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
133 NVPtr pNv = NVPTR(pScrn);
135 return NVReadVGA(pNv, nv_crtc->head, index);
138 static void NVWriteVgaSeq(xf86CrtcPtr crtc, CARD8 index, CARD8 value)
140 NVWritePVIO(crtc, VGA_SEQ_INDEX, index);
141 NVWritePVIO(crtc, VGA_SEQ_DATA, value);
144 static CARD8 NVReadVgaSeq(xf86CrtcPtr crtc, CARD8 index)
146 NVWritePVIO(crtc, VGA_SEQ_INDEX, index);
147 return NVReadPVIO(crtc, VGA_SEQ_DATA);
150 static void NVWriteVgaGr(xf86CrtcPtr crtc, CARD8 index, CARD8 value)
152 NVWritePVIO(crtc, VGA_GRAPH_INDEX, index);
153 NVWritePVIO(crtc, VGA_GRAPH_DATA, value);
156 static CARD8 NVReadVgaGr(xf86CrtcPtr crtc, CARD8 index)
158 NVWritePVIO(crtc, VGA_GRAPH_INDEX, index);
159 return NVReadPVIO(crtc, VGA_GRAPH_DATA);
163 static void NVWriteVgaAttr(xf86CrtcPtr crtc, CARD8 index, CARD8 value)
165 ScrnInfoPtr pScrn = crtc->scrn;
166 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
167 NVPtr pNv = NVPTR(pScrn);
168 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
170 NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
171 if (nv_crtc->paletteEnabled)
175 NV_WR08(pCRTCReg, VGA_ATTR_INDEX, index);
176 NV_WR08(pCRTCReg, VGA_ATTR_DATA_W, value);
179 static CARD8 NVReadVgaAttr(xf86CrtcPtr crtc, CARD8 index)
181 ScrnInfoPtr pScrn = crtc->scrn;
182 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
183 NVPtr pNv = NVPTR(pScrn);
184 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
186 NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
187 if (nv_crtc->paletteEnabled)
191 NV_WR08(pCRTCReg, VGA_ATTR_INDEX, index);
192 return NV_RD08(pCRTCReg, VGA_ATTR_DATA_R);
195 void NVCrtcSetOwner(xf86CrtcPtr crtc)
197 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
198 ScrnInfoPtr pScrn = crtc->scrn;
199 NVPtr pNv = NVPTR(pScrn);
200 /* Non standard beheaviour required by NV11 */
202 uint8_t owner = NVReadVGA0(pNv, NV_VGA_CRTCX_OWNER);
203 ErrorF("pre-Owner: 0x%X\n", owner);
205 uint32_t pbus84 = nvReadMC(pNv, 0x1084);
206 ErrorF("pbus84: 0x%X\n", pbus84);
208 ErrorF("pbus84: 0x%X\n", pbus84);
209 nvWriteMC(pNv, 0x1084, pbus84);
211 /* The blob never writes owner to pcio1, so should we */
212 if (pNv->NVArch == 0x11) {
213 NVWriteVGA0(pNv, NV_VGA_CRTCX_OWNER, 0xff);
215 NVWriteVGA0(pNv, NV_VGA_CRTCX_OWNER, nv_crtc->crtc * 0x3);
216 owner = NVReadVGA0(pNv, NV_VGA_CRTCX_OWNER);
217 ErrorF("post-Owner: 0x%X\n", owner);
219 ErrorF("pNv pointer is NULL\n");
224 NVEnablePalette(xf86CrtcPtr crtc)
226 ScrnInfoPtr pScrn = crtc->scrn;
227 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
228 NVPtr pNv = NVPTR(pScrn);
229 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
231 NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
232 NV_WR08(pCRTCReg, VGA_ATTR_INDEX, 0);
233 nv_crtc->paletteEnabled = TRUE;
237 NVDisablePalette(xf86CrtcPtr crtc)
239 ScrnInfoPtr pScrn = crtc->scrn;
240 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
241 NVPtr pNv = NVPTR(pScrn);
242 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
244 NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
245 NV_WR08(pCRTCReg, VGA_ATTR_INDEX, 0x20);
246 nv_crtc->paletteEnabled = FALSE;
249 static void NVWriteVgaReg(xf86CrtcPtr crtc, CARD32 reg, CARD8 value)
251 ScrnInfoPtr pScrn = crtc->scrn;
252 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
253 NVPtr pNv = NVPTR(pScrn);
254 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
256 NV_WR08(pCRTCReg, reg, value);
259 /* perform a sequencer reset */
260 static void NVVgaSeqReset(xf86CrtcPtr crtc, Bool start)
263 NVWriteVgaSeq(crtc, 0x00, 0x1);
265 NVWriteVgaSeq(crtc, 0x00, 0x3);
268 static void NVVgaProtect(xf86CrtcPtr crtc, Bool on)
273 tmp = NVReadVgaSeq(crtc, 0x1);
274 NVVgaSeqReset(crtc, TRUE);
275 NVWriteVgaSeq(crtc, 0x01, tmp | 0x20);
277 NVEnablePalette(crtc);
280 * Reenable sequencer, then turn on screen.
282 tmp = NVReadVgaSeq(crtc, 0x1);
283 NVWriteVgaSeq(crtc, 0x01, tmp & ~0x20); /* reenable display */
284 NVVgaSeqReset(crtc, FALSE);
286 NVDisablePalette(crtc);
290 void NVCrtcLockUnlock(xf86CrtcPtr crtc, Bool Lock)
294 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LOCK, Lock ? 0x99 : 0x57);
295 cr11 = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_VSYNCE);
296 if (Lock) cr11 |= 0x80;
298 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_VSYNCE, cr11);
302 NVGetOutputFromCRTC(xf86CrtcPtr crtc)
304 ScrnInfoPtr pScrn = crtc->scrn;
305 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
307 for (i = 0; i < xf86_config->num_output; i++) {
308 xf86OutputPtr output = xf86_config->output[i];
310 if (output->crtc == crtc) {
319 nv_find_crtc_by_index(ScrnInfoPtr pScrn, int index)
321 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
324 for (i = 0; i < xf86_config->num_crtc; i++) {
325 xf86CrtcPtr crtc = xf86_config->crtc[i];
326 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
327 if (nv_crtc->crtc == index)
335 * Calculate the Video Clock parameters for the PLL.
337 static void CalcVClock (
344 unsigned lowM, highM, highP;
345 unsigned DeltaNew, DeltaOld;
349 /* M: PLL reference frequency postscaler divider */
350 /* P: PLL VCO output postscaler divider */
351 /* N: PLL VCO postscaler setting */
353 DeltaOld = 0xFFFFFFFF;
355 VClk = (unsigned)clockIn;
357 /* Taken from Haiku, after someone with an NV28 had an issue */
358 switch(pNv->NVArch) {
364 } else if (VClk > 200000) {
366 } else if (VClk > 150000) {
377 } else if (VClk > 250000) {
385 for (P = 1; P <= highP; P++) {
387 if ((Freq >= 128000) && (Freq <= 350000)) {
388 for (M = lowM; M <= highM; M++) {
389 N = ((VClk << P) * M) / pNv->CrystalFreqKHz;
391 Freq = ((pNv->CrystalFreqKHz * N) / M) >> P;
393 DeltaNew = Freq - VClk;
395 DeltaNew = VClk - Freq;
397 if (DeltaNew < DeltaOld) {
398 *pllOut = (P << 16) | (N << 8) | M;
408 static void CalcVClock2Stage (
416 unsigned DeltaNew, DeltaOld;
419 unsigned lowM, highM, highP;
421 DeltaOld = 0xFFFFFFFF;
423 *pllBOut = 0x80000401; /* fixed at x4 for now */
425 VClk = (unsigned)clockIn;
427 /* Taken from Haiku, after someone with an NV28 had an issue */
428 switch(pNv->NVArch) {
434 } else if (VClk > 200000) {
436 } else if (VClk > 150000) {
447 } else if (VClk > 250000) {
455 for (P = 0; P <= highP; P++) {
457 if ((Freq >= 400000) && (Freq <= 1000000)) {
458 for (M = lowM; M <= highM; M++) {
459 N = ((VClk << P) * M) / (pNv->CrystalFreqKHz << 2);
460 if ((N >= 5) && (N <= 255)) {
461 Freq = (((pNv->CrystalFreqKHz << 2) * N) / M) >> P;
463 DeltaNew = Freq - VClk;
465 DeltaNew = VClk - Freq;
467 if (DeltaNew < DeltaOld) {
468 *pllOut = (P << 16) | (N << 8) | M;
478 /* BIG NOTE: modifying vpll1 and vpll2 does not work, what bit is the switch to allow it? */
480 /* Even though they are not yet used, i'm adding some notes about some of the 0x4000 regs */
481 /* They are only valid for NV4x, appearantly reordered for NV5x */
482 /* gpu pll: 0x4000 + 0x4004
483 * unknown pll: 0x4008 + 0x400c
484 * vpll1: 0x4010 + 0x4014
485 * vpll2: 0x4018 + 0x401c
486 * unknown pll: 0x4020 + 0x4024
487 * unknown pll: 0x4038 + 0x403c
488 * Some of the unknown's are probably memory pll's.
489 * The vpll's use two set's of multipliers and dividers. I refer to them as a and b.
490 * 1 and 2 refer to the registers of each pair. There is only one post divider.
491 * Logic: clock = reference_clock * ((n(a) * n(b))/(m(a) * m(b))) >> p
492 * 1) bit 0-7: familiar values, but redirected from were? (similar to PLL_SETUP_CONTROL)
493 * bit8: A switch that turns of the second divider and multiplier off.
494 * bit12: Also a switch, i haven't seen it yet.
495 * bit16-19: p-divider
496 * but 28-31: Something related to the mode that is used (see bit8).
497 * 2) bit0-7: m-divider (a)
498 * bit8-15: n-multiplier (a)
499 * bit16-23: m-divider (b)
500 * bit24-31: n-multiplier (b)
503 /* Modifying the gpu pll for example requires:
504 * - Disable value 0x333 (inverse AND mask) on the 0xc040 register.
505 * This is not needed for the vpll's which have their own bits.
511 uint32_t requested_clock,
512 uint32_t *given_clock,
520 uint32_t DeltaOld, DeltaNew;
522 /* We have 2 mulitpliers, 2 dividers and one post divider */
523 /* Note that p is only 4 bits */
524 uint32_t m1, m2, n1, n2, p;
525 uint32_t m1_best = 0, m2_best = 0, n1_best = 0, n2_best = 0, p_best = 0;
527 DeltaOld = 0xFFFFFFFF;
529 /* This is no solid limit, but a reasonable boundary */
530 if (requested_clock < 120000) {
532 /* Turn the second set of divider and multiplier off */
533 /* Neutral settings */
538 /* Fixed at x4 for the moment */
548 temp = 0.4975 * 250000;
551 while (requested_clock <= temp) {
556 /* The minimum clock after m1 is 3 Mhz, and the clock is 27 Mhz, so m_max = 9 */
557 /* The maximum clock is 25 Mhz */
558 for (m1 = 2; m1 <= 9; m1++) {
559 n1 = ((requested_clock << p) * m1)/(pNv->CrystalFreqKHz);
560 if (n1 > 0 && n1 <= 255) {
561 freq = ((pNv->CrystalFreqKHz * n1)/m1) >> p;
562 if (freq > requested_clock) {
563 DeltaNew = freq - requested_clock;
565 DeltaNew = requested_clock - freq;
567 if (DeltaNew < DeltaOld) {
577 for (p = 0; p <= 6; p++) {
578 /* Assuming a fixed 2nd stage */
579 freq = requested_clock << p;
580 /* The maximum output frequency of stage 2 is allowed to be between 400 Mhz and 1 GHz */
581 if (freq > 400000 && freq < 1000000) {
582 /* The minimum clock after m1 is 3 Mhz, and the clock is 27 Mhz, so m_max = 9 */
583 /* The maximum clock is 25 Mhz */
584 for (m1 = 2; m1 <= 9; m1++) {
585 n1 = ((requested_clock << p) * m1 * m2)/(pNv->CrystalFreqKHz * n2);
586 if (n1 >= 5 && n1 <= 255) {
587 freq = ((pNv->CrystalFreqKHz * n1 * n2)/(m1 * m2)) >> p;
588 if (freq > requested_clock) {
589 DeltaNew = freq - requested_clock;
591 DeltaNew = requested_clock - freq;
593 if (DeltaNew < DeltaOld) {
606 /* Bogus data, the same nvidia uses */
611 /* What exactly are the purpose of bit30 (a) and bit31(b)? */
612 *pll_a = (1 << 30) | (p_best << 16) | (n1_best << 8) | (m1_best << 0);
613 *pll_b = (1 << 31) | (n2_best << 8) | (m2_best << 0);
617 *reg580 |= NV_RAMDAC_580_VPLL1_ACTIVE;
619 *reg580 |= NV_RAMDAC_580_VPLL2_ACTIVE;
623 *reg580 &= ~NV_RAMDAC_580_VPLL1_ACTIVE;
625 *reg580 &= ~NV_RAMDAC_580_VPLL2_ACTIVE;
630 ErrorF("vpll: n1 %d m1 %d p %d db1_ratio %d\n", n1_best, m1_best, p_best, *db1_ratio);
632 ErrorF("vpll: n1 %d n2 %d m1 %d m2 %d p %d db1_ratio %d\n", n1_best, n2_best, m1_best, m2_best, p_best, *db1_ratio);
636 static void nv40_crtc_save_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
638 state->vpll1_a = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL);
639 state->vpll1_b = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL_B);
640 state->vpll2_a = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2);
641 state->vpll2_b = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2_B);
642 state->pllsel = nvReadRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT);
643 state->sel_clk = nvReadRAMDAC0(pNv, NV_RAMDAC_SEL_CLK);
644 state->reg580 = nvReadRAMDAC0(pNv, NV_RAMDAC_580);
647 static void nv40_crtc_load_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
649 CARD32 fp_debug_0[2];
651 fp_debug_0[0] = nvReadRAMDAC(pNv, 0, NV_RAMDAC_FP_DEBUG_0);
652 fp_debug_0[1] = nvReadRAMDAC(pNv, 1, NV_RAMDAC_FP_DEBUG_0);
654 /* The TMDS_PLL switch is on the actual ramdac */
655 if (state->crosswired) {
658 ErrorF("Crosswired pll state load\n");
664 if (state->vpll2_b) {
665 nvWriteRAMDAC(pNv, index[1], NV_RAMDAC_FP_DEBUG_0,
666 fp_debug_0[index[1]] | NV_RAMDAC_FP_DEBUG_0_PWRDOWN_TMDS_PLL);
668 /* Wait for the situation to stabilise */
671 uint32_t reg_c040 = pNv->misc_info.reg_c040;
672 /* for vpll2 change bits 18 and 19 are disabled */
673 reg_c040 &= ~(0x3 << 18);
674 nvWriteMC(pNv, 0xc040, reg_c040);
676 ErrorF("writing vpll2_a %08X\n", state->vpll2_a);
677 ErrorF("writing vpll2_b %08X\n", state->vpll2_b);
679 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2, state->vpll2_a);
680 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2_B, state->vpll2_b);
682 ErrorF("writing pllsel %08X\n", state->pllsel & ~NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL);
683 /* Let's keep the primary vpll off */
684 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel & ~NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL);
686 nvWriteRAMDAC0(pNv, NV_RAMDAC_580, state->reg580);
687 ErrorF("writing reg580 %08X\n", state->reg580);
689 /* We need to wait a while */
691 nvWriteMC(pNv, 0xc040, pNv->misc_info.reg_c040);
693 nvWriteRAMDAC(pNv, index[1], NV_RAMDAC_FP_DEBUG_0, fp_debug_0[index[1]]);
695 /* Wait for the situation to stabilise */
699 if (state->vpll1_b) {
700 nvWriteRAMDAC(pNv, index[0], NV_RAMDAC_FP_DEBUG_0,
701 fp_debug_0[index[0]] | NV_RAMDAC_FP_DEBUG_0_PWRDOWN_TMDS_PLL);
703 /* Wait for the situation to stabilise */
706 uint32_t reg_c040 = pNv->misc_info.reg_c040;
707 /* for vpll2 change bits 16 and 17 are disabled */
708 reg_c040 &= ~(0x3 << 16);
709 nvWriteMC(pNv, 0xc040, reg_c040);
711 ErrorF("writing vpll1_a %08X\n", state->vpll1_a);
712 ErrorF("writing vpll1_b %08X\n", state->vpll1_b);
714 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL, state->vpll1_a);
715 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL_B, state->vpll1_b);
717 ErrorF("writing pllsel %08X\n", state->pllsel);
718 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel);
720 nvWriteRAMDAC0(pNv, NV_RAMDAC_580, state->reg580);
721 ErrorF("writing reg580 %08X\n", state->reg580);
723 /* We need to wait a while */
725 nvWriteMC(pNv, 0xc040, pNv->misc_info.reg_c040);
727 nvWriteRAMDAC(pNv, index[0], NV_RAMDAC_FP_DEBUG_0, fp_debug_0[index[0]]);
729 /* Wait for the situation to stabilise */
733 ErrorF("writing sel_clk %08X\n", state->sel_clk);
734 nvWriteRAMDAC0(pNv, NV_RAMDAC_SEL_CLK, state->sel_clk);
737 static void nv_crtc_save_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
739 state->vpll = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL);
741 state->vpll2 = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2);
743 if(pNv->twoStagePLL) {
744 state->vpllB = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL_B);
745 state->vpll2B = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2_B);
747 state->pllsel = nvReadRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT);
748 state->sel_clk = nvReadRAMDAC0(pNv, NV_RAMDAC_SEL_CLK);
752 static void nv_crtc_load_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
756 ErrorF("writing vpll2 %08X\n", state->vpll2);
757 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2, state->vpll2);
759 if(pNv->twoStagePLL) {
760 ErrorF("writing vpll2B %08X\n", state->vpll2B);
761 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2_B, state->vpll2B);
764 ErrorF("writing pllsel %08X\n", state->pllsel);
765 /* Let's keep the primary vpll off */
766 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel & ~NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL);
770 ErrorF("writing vpll %08X\n", state->vpll);
771 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL, state->vpll);
772 if(pNv->twoStagePLL) {
773 ErrorF("writing vpllB %08X\n", state->vpllB);
774 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL_B, state->vpllB);
777 ErrorF("writing pllsel %08X\n", state->pllsel);
778 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel);
781 ErrorF("writing sel_clk %08X\n", state->sel_clk);
782 nvWriteRAMDAC0(pNv, NV_RAMDAC_SEL_CLK, state->sel_clk);
786 * Calculate extended mode parameters (SVGA) and save in a
787 * mode state structure.
788 * State is not specific to a single crtc, but shared.
790 void nv_crtc_calc_state_ext(
793 int DisplayWidth, /* Does this change after setting the mode? */
800 ScrnInfoPtr pScrn = crtc->scrn;
801 uint32_t pixelDepth, VClk = 0;
803 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
804 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
806 NVPtr pNv = NVPTR(pScrn);
807 RIVA_HW_STATE *state;
808 int num_crtc_enabled, i;
810 state = &pNv->ModeReg;
812 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
814 xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
815 NVOutputPrivatePtr nv_output = output->driver_private;
818 * Extended RIVA registers.
820 pixelDepth = (bpp + 1)/8;
821 if (pNv->Architecture == NV_ARCH_40) {
822 /* Does register 0x580 already have a value? */
823 if (!state->reg580) {
824 state->reg580 = pNv->misc_info.ramdac_0_reg_580;
826 if (nv_output->ramdac == 1) {
827 CalculateVClkNV4x(pNv, dotClock, &VClk, &state->vpll2_a, &state->vpll2_b, &state->reg580, &state->db1_ratio[1], FALSE);
829 CalculateVClkNV4x(pNv, dotClock, &VClk, &state->vpll1_a, &state->vpll1_b, &state->reg580, &state->db1_ratio[0], TRUE);
831 } else if (pNv->twoStagePLL) {
832 CalcVClock2Stage(dotClock, &VClk, &state->pll, &state->pllB, pNv);
834 CalcVClock(dotClock, &VClk, &state->pll, pNv);
837 switch (pNv->Architecture) {
839 nv4UpdateArbitrationSettings(VClk,
841 &(state->arbitration0),
842 &(state->arbitration1),
844 regp->CRTC[NV_VGA_CRTCX_CURCTL0] = 0x00;
845 regp->CRTC[NV_VGA_CRTCX_CURCTL1] = 0xbC;
846 if (flags & V_DBLSCAN)
847 regp->CRTC[NV_VGA_CRTCX_CURCTL1] |= 2;
848 regp->CRTC[NV_VGA_CRTCX_CURCTL2] = 0x00000000;
849 state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2 | NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL;
850 state->config = 0x00001114;
851 regp->CRTC[NV_VGA_CRTCX_REPAINT1] = CrtcHDisplay < 1280 ? 0x04 : 0x00;
857 if (((pNv->Chipset & 0xfff0) == CHIPSET_C51) ||
858 ((pNv->Chipset & 0xfff0) == CHIPSET_C512)) {
859 state->arbitration0 = 128;
860 state->arbitration1 = 0x0480;
861 } else if (((pNv->Chipset & 0xffff) == CHIPSET_NFORCE) ||
862 ((pNv->Chipset & 0xffff) == CHIPSET_NFORCE2)) {
863 nForceUpdateArbitrationSettings(VClk,
865 &(state->arbitration0),
866 &(state->arbitration1),
868 } else if (pNv->Architecture < NV_ARCH_30) {
869 nv10UpdateArbitrationSettings(VClk,
871 &(state->arbitration0),
872 &(state->arbitration1),
875 nv30UpdateArbitrationSettings(pNv,
876 &(state->arbitration0),
877 &(state->arbitration1));
880 CursorStart = pNv->Cursor->offset;
882 regp->CRTC[NV_VGA_CRTCX_CURCTL0] = 0x80 | (CursorStart >> 17);
883 regp->CRTC[NV_VGA_CRTCX_CURCTL1] = (CursorStart >> 11) << 2;
884 regp->CRTC[NV_VGA_CRTCX_CURCTL2] = CursorStart >> 24;
886 if (flags & V_DBLSCAN)
887 regp->CRTC[NV_VGA_CRTCX_CURCTL1] |= 2;
889 state->config = nvReadFB(pNv, NV_PFB_CFG0);
890 regp->CRTC[NV_VGA_CRTCX_REPAINT1] = CrtcHDisplay < 1280 ? 0x04 : 0x00;
894 /* okay do we have 2 CRTCs running ? */
895 num_crtc_enabled = 0;
896 for (i = 0; i < xf86_config->num_crtc; i++) {
897 if (xf86_config->crtc[i]->enabled) {
902 ErrorF("There are %d CRTC's enabled\n", num_crtc_enabled);
904 if (pNv->Architecture < NV_ARCH_40) {
905 /* We need this before the next code */
906 if (nv_crtc->crtc == 1) {
907 state->vpll2 = state->pll;
908 state->vpll2B = state->pllB;
910 state->vpll = state->pll;
911 state->vpllB = state->pllB;
915 if (pNv->Architecture == NV_ARCH_40) {
916 /* This register is only used on the primary ramdac */
917 /* This seems to be needed to select the proper clocks, otherwise bad things happen */
918 /* Assumption CRTC1 will overwrite the CRTC0 value */
919 /* Also make sure we don't set both bits */
922 state->sel_clk = pNv->misc_info.sel_clk & ~(0xf << 16);
924 /* The rough idea is this:
925 * 0x40000: One or both dvi outputs is/are on their preferred ramdac (=clock)
926 * 0x10000: One dvi output is on not on it's preferred ramdac (=clock).
927 * 0x00000: No dvi panels present.
928 * Other bits also exist, but we leave those intact.
929 * One dvi panel must always be on it's preferred ramdac, due to "or" restrictions.
932 if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS) {
933 /* Clean out all the bits and enable another mode */
934 if (nv_output->ramdac == nv_output->preferred_ramdac) {
935 state->sel_clk &= ~(0xf << 16);
936 state->sel_clk |= (1 << 18);
938 state->sel_clk &= ~(0xf << 16);
939 state->sel_clk |= (1 << 16);
942 int other_index = (~nv_crtc->head) & 1;
943 xf86CrtcPtr crtc2 = nv_find_crtc_by_index(pScrn, other_index);
944 if (crtc2->enabled) {
945 xf86OutputPtr output2 = NVGetOutputFromCRTC(crtc2);
946 NVOutputPrivatePtr nv_output2 = output2->driver_private;
947 if (nv_output2->type == OUTPUT_TMDS || nv_output2->type == OUTPUT_LVDS) {
948 /* Clean out all the bits and enable another mode */
949 if (nv_output2->ramdac == nv_output2->preferred_ramdac) {
950 state->sel_clk &= ~(0xf << 16);
951 state->sel_clk |= (1 << 18);
953 state->sel_clk &= ~(0xf << 16);
954 state->sel_clk |= (1 << 16);
957 /* Destroy all tmds traces */
958 state->sel_clk &= ~(0xf << 16);
961 /* Destroy all tmds traces */
962 state->sel_clk &= ~(0xf << 16);
966 /* Are we crosswired? */
967 if (nv_crtc->head != nv_output->preferred_crtc &&
968 (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS)) {
969 state->crosswired = TRUE;
970 } else if (nv_crtc->head != nv_output->preferred_crtc) {
971 state->crosswired = FALSE;
974 if (nv_crtc->head == 1) {
975 if (state->db1_ratio[1])
976 ErrorF("We are a lover of the DB1 VCLK ratio\n");
977 } else if (nv_crtc->head == 0) {
978 if (state->db1_ratio[0])
979 ErrorF("We are a lover of the DB1 VCLK ratio\n");
982 /* This seems true for nv34 */
983 state->sel_clk = 0x0;
984 state->crosswired = FALSE;
987 if (nv_output->ramdac == 1) {
988 if (!state->db1_ratio[1]) {
989 state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK2_RATIO_DB2;
991 state->pllsel &= ~NV_RAMDAC_PLL_SELECT_VCLK2_RATIO_DB2;
993 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL2;
995 if (pNv->Architecture < NV_ARCH_40)
996 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL;
998 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL;
999 if (!state->db1_ratio[0]) {
1000 state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2;
1002 state->pllsel &= ~NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2;
1006 regp->CRTC[NV_VGA_CRTCX_FIFO0] = state->arbitration0;
1007 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM] = state->arbitration1 & 0xff;
1008 if (pNv->Architecture >= NV_ARCH_30) {
1009 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30] = state->arbitration1 >> 8;
1012 regp->CRTC[NV_VGA_CRTCX_REPAINT0] = (((DisplayWidth/8) * pixelDepth) & 0x700) >> 3;
1013 regp->CRTC[NV_VGA_CRTCX_PIXEL] = (pixelDepth > 2) ? 3 : pixelDepth;
1017 nv_crtc_dpms(xf86CrtcPtr crtc, int mode)
1019 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1020 ScrnInfoPtr pScrn = crtc->scrn;
1021 NVPtr pNv = NVPTR(pScrn);
1022 unsigned char seq1 = 0, crtc17 = 0;
1023 unsigned char crtc1A;
1025 ErrorF("nv_crtc_dpms is called for CRTC %d with mode %d\n", nv_crtc->crtc, mode);
1027 NVCrtcSetOwner(crtc);
1029 crtc1A = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1) & ~0xC0;
1031 case DPMSModeStandby:
1032 /* Screen: Off; HSync: Off, VSync: On -- Not Supported */
1037 case DPMSModeSuspend:
1038 /* Screen: Off; HSync: On, VSync: Off -- Not Supported */
1044 /* Screen: Off; HSync: Off, VSync: Off */
1051 /* Screen: On; HSync: On, VSync: On */
1057 NVVgaSeqReset(crtc, TRUE);
1058 /* Each head has it's own sequencer, so we can turn it off when we want */
1059 seq1 |= (NVReadVgaSeq(crtc, 0x01) & ~0x20);
1060 NVWriteVgaSeq(crtc, 0x1, seq1);
1061 crtc17 |= (NVReadVgaCrtc(crtc, NV_VGA_CRTCX_MODECTL) & ~0x80);
1063 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_MODECTL, crtc17);
1064 NVVgaSeqReset(crtc, FALSE);
1066 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1, crtc1A);
1068 /* I hope this is the right place */
1069 if (crtc->enabled && mode == DPMSModeOn) {
1070 pNv->crtc_active[nv_crtc->head] = TRUE;
1072 pNv->crtc_active[nv_crtc->head] = FALSE;
1077 nv_crtc_mode_fixup(xf86CrtcPtr crtc, DisplayModePtr mode,
1078 DisplayModePtr adjusted_mode)
1080 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1081 ScrnInfoPtr pScrn = crtc->scrn;
1082 NVPtr pNv = NVPTR(pScrn);
1083 ErrorF("nv_crtc_mode_fixup is called for CRTC %d\n", nv_crtc->crtc);
1085 xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
1086 NVOutputPrivatePtr nv_output = output->driver_private;
1088 /* For internal panels and gpu scaling on DVI we need the native mode */
1089 if ((nv_output->type == OUTPUT_LVDS) || (pNv->fpScaler && (nv_output->type == OUTPUT_TMDS))) {
1090 adjusted_mode->HDisplay = nv_output->native_mode->HDisplay;
1091 adjusted_mode->HSkew = nv_output->native_mode->HSkew;
1092 adjusted_mode->HSyncStart = nv_output->native_mode->HSyncStart;
1093 adjusted_mode->HSyncEnd = nv_output->native_mode->HSyncEnd;
1094 adjusted_mode->HTotal = nv_output->native_mode->HTotal;
1095 adjusted_mode->VDisplay = nv_output->native_mode->VDisplay;
1096 adjusted_mode->VScan = nv_output->native_mode->VScan;
1097 adjusted_mode->VSyncStart = nv_output->native_mode->VSyncStart;
1098 adjusted_mode->VSyncEnd = nv_output->native_mode->VSyncEnd;
1099 adjusted_mode->VTotal = nv_output->native_mode->VTotal;
1100 adjusted_mode->Clock = nv_output->native_mode->Clock;
1102 xf86SetModeCrtc(adjusted_mode, INTERLACE_HALVE_V);
1109 nv_crtc_mode_set_vga(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
1111 ScrnInfoPtr pScrn = crtc->scrn;
1112 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1114 NVPtr pNv = NVPTR(pScrn);
1115 NVFBLayout *pLayout = &pNv->CurrentLayout;
1116 int depth = pScrn->depth;
1118 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1120 /* Calculate our timings */
1121 int horizDisplay = (mode->CrtcHDisplay >> 3) - 1;
1122 int horizStart = (mode->CrtcHSyncStart >> 3) - 1;
1123 int horizEnd = (mode->CrtcHSyncEnd >> 3) - 1;
1124 int horizTotal = (mode->CrtcHTotal >> 3) - 5;
1125 int horizBlankStart = (mode->CrtcHDisplay >> 3) - 1;
1126 int horizBlankEnd = (mode->CrtcHTotal >> 3) - 1;
1127 int vertDisplay = mode->CrtcVDisplay - 1;
1128 int vertStart = mode->CrtcVSyncStart - 1;
1129 int vertEnd = mode->CrtcVSyncEnd - 1;
1130 int vertTotal = mode->CrtcVTotal - 2;
1131 int vertBlankStart = mode->CrtcVDisplay - 1;
1132 int vertBlankEnd = mode->CrtcVTotal - 1;
1136 xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
1137 NVOutputPrivatePtr nv_output = output->driver_private;
1139 if ((nv_output->type == OUTPUT_LVDS) || (nv_output->type == OUTPUT_TMDS))
1142 ErrorF("Mode clock: %d\n", mode->Clock);
1143 ErrorF("Adjusted mode clock: %d\n", adjusted_mode->Clock);
1145 /* Reverted to what nv did, because that works for all resolutions on flatpanels */
1147 vertStart = vertTotal - 3;
1148 vertEnd = vertTotal - 2;
1149 vertBlankStart = vertStart;
1150 horizStart = horizTotal - 5;
1151 horizEnd = horizTotal - 2;
1152 horizBlankEnd = horizTotal + 4;
1153 if (pNv->overlayAdaptor) {
1154 /* This reportedly works around Xv some overlay bandwidth problems*/
1159 if(mode->Flags & V_INTERLACE)
1162 ErrorF("horizDisplay: 0x%X \n", horizDisplay);
1163 ErrorF("horizStart: 0x%X \n", horizStart);
1164 ErrorF("horizEnd: 0x%X \n", horizEnd);
1165 ErrorF("horizTotal: 0x%X \n", horizTotal);
1166 ErrorF("horizBlankStart: 0x%X \n", horizBlankStart);
1167 ErrorF("horizBlankEnd: 0x%X \n", horizBlankEnd);
1168 ErrorF("vertDisplay: 0x%X \n", vertDisplay);
1169 ErrorF("vertStart: 0x%X \n", vertStart);
1170 ErrorF("vertEnd: 0x%X \n", vertEnd);
1171 ErrorF("vertTotal: 0x%X \n", vertTotal);
1172 ErrorF("vertBlankStart: 0x%X \n", vertBlankStart);
1173 ErrorF("vertBlankEnd: 0x%X \n", vertBlankEnd);
1176 * compute correct Hsync & Vsync polarity
1178 if ((mode->Flags & (V_PHSYNC | V_NHSYNC))
1179 && (mode->Flags & (V_PVSYNC | V_NVSYNC))) {
1181 regp->MiscOutReg = 0x23;
1182 if (mode->Flags & V_NHSYNC) regp->MiscOutReg |= 0x40;
1183 if (mode->Flags & V_NVSYNC) regp->MiscOutReg |= 0x80;
1185 int VDisplay = mode->VDisplay;
1186 if (mode->Flags & V_DBLSCAN)
1188 if (mode->VScan > 1)
1189 VDisplay *= mode->VScan;
1190 if (VDisplay < 400) {
1191 regp->MiscOutReg = 0xA3; /* +hsync -vsync */
1192 } else if (VDisplay < 480) {
1193 regp->MiscOutReg = 0x63; /* -hsync +vsync */
1194 } else if (VDisplay < 768) {
1195 regp->MiscOutReg = 0xE3; /* -hsync -vsync */
1197 regp->MiscOutReg = 0x23; /* +hsync +vsync */
1201 regp->MiscOutReg |= (mode->ClockIndex & 0x03) << 2;
1207 regp->Sequencer[0] = 0x02;
1209 regp->Sequencer[0] = 0x00;
1211 /* 0x20 disables the sequencer */
1212 if (mode->Flags & V_CLKDIV2) {
1213 regp->Sequencer[1] = 0x29;
1215 regp->Sequencer[1] = 0x21;
1218 regp->Sequencer[2] = 1 << BIT_PLANE;
1220 regp->Sequencer[2] = 0x0F;
1221 regp->Sequencer[3] = 0x00; /* Font select */
1224 regp->Sequencer[4] = 0x06; /* Misc */
1226 regp->Sequencer[4] = 0x0E; /* Misc */
1232 regp->CRTC[NV_VGA_CRTCX_HTOTAL] = Set8Bits(horizTotal);
1233 regp->CRTC[NV_VGA_CRTCX_HDISPE] = Set8Bits(horizDisplay);
1234 regp->CRTC[NV_VGA_CRTCX_HBLANKS] = Set8Bits(horizBlankStart);
1235 regp->CRTC[NV_VGA_CRTCX_HBLANKE] = SetBitField(horizBlankEnd,4:0,4:0)
1237 regp->CRTC[NV_VGA_CRTCX_HSYNCS] = Set8Bits(horizStart);
1238 regp->CRTC[NV_VGA_CRTCX_HSYNCE] = SetBitField(horizBlankEnd,5:5,7:7)
1239 | SetBitField(horizEnd,4:0,4:0);
1240 regp->CRTC[NV_VGA_CRTCX_VTOTAL] = SetBitField(vertTotal,7:0,7:0);
1241 regp->CRTC[NV_VGA_CRTCX_OVERFLOW] = SetBitField(vertTotal,8:8,0:0)
1242 | SetBitField(vertDisplay,8:8,1:1)
1243 | SetBitField(vertStart,8:8,2:2)
1244 | SetBitField(vertBlankStart,8:8,3:3)
1246 | SetBitField(vertTotal,9:9,5:5)
1247 | SetBitField(vertDisplay,9:9,6:6)
1248 | SetBitField(vertStart,9:9,7:7);
1249 regp->CRTC[NV_VGA_CRTCX_PRROWSCN] = 0x00;
1250 regp->CRTC[NV_VGA_CRTCX_MAXSCLIN] = SetBitField(vertBlankStart,9:9,5:5)
1252 | ((mode->Flags & V_DBLSCAN) ? 0x80 : 0x00);
1253 regp->CRTC[NV_VGA_CRTCX_VGACURCTRL] = 0x00;
1254 regp->CRTC[0xb] = 0x00;
1255 regp->CRTC[NV_VGA_CRTCX_FBSTADDH] = 0x00;
1256 regp->CRTC[NV_VGA_CRTCX_FBSTADDL] = 0x00;
1257 regp->CRTC[0xe] = 0x00;
1258 regp->CRTC[0xf] = 0x00;
1259 regp->CRTC[NV_VGA_CRTCX_VSYNCS] = Set8Bits(vertStart);
1260 regp->CRTC[NV_VGA_CRTCX_VSYNCE] = SetBitField(vertEnd,3:0,3:0) | SetBit(5);
1261 regp->CRTC[NV_VGA_CRTCX_VDISPE] = Set8Bits(vertDisplay);
1262 regp->CRTC[0x14] = 0x00;
1263 regp->CRTC[NV_VGA_CRTCX_PITCHL] = ((pScrn->displayWidth/8)*(pLayout->bitsPerPixel/8));
1264 regp->CRTC[NV_VGA_CRTCX_VBLANKS] = Set8Bits(vertBlankStart);
1265 regp->CRTC[NV_VGA_CRTCX_VBLANKE] = Set8Bits(vertBlankEnd);
1266 /* 0x80 enables the sequencer, we don't want that */
1268 regp->CRTC[NV_VGA_CRTCX_MODECTL] = 0xE3 & ~0x80;
1270 regp->CRTC[NV_VGA_CRTCX_MODECTL] = 0xC3 & ~0x80;
1272 regp->CRTC[NV_VGA_CRTCX_LINECOMP] = 0xff;
1275 * Some extended CRTC registers (they are not saved with the rest of the vga regs).
1278 regp->CRTC[NV_VGA_CRTCX_LSR] = SetBitField(horizBlankEnd,6:6,4:4)
1279 | SetBitField(vertBlankStart,10:10,3:3)
1280 | SetBitField(vertStart,10:10,2:2)
1281 | SetBitField(vertDisplay,10:10,1:1)
1282 | SetBitField(vertTotal,10:10,0:0);
1284 regp->CRTC[NV_VGA_CRTCX_HEB] = SetBitField(horizTotal,8:8,0:0)
1285 | SetBitField(horizDisplay,8:8,1:1)
1286 | SetBitField(horizBlankStart,8:8,2:2)
1287 | SetBitField(horizStart,8:8,3:3);
1289 regp->CRTC[NV_VGA_CRTCX_EXTRA] = SetBitField(vertTotal,11:11,0:0)
1290 | SetBitField(vertDisplay,11:11,2:2)
1291 | SetBitField(vertStart,11:11,4:4)
1292 | SetBitField(vertBlankStart,11:11,6:6);
1294 if(mode->Flags & V_INTERLACE) {
1295 horizTotal = (horizTotal >> 1) & ~1;
1296 regp->CRTC[NV_VGA_CRTCX_INTERLACE] = Set8Bits(horizTotal);
1297 regp->CRTC[NV_VGA_CRTCX_HEB] |= SetBitField(horizTotal,8:8,4:4);
1299 regp->CRTC[NV_VGA_CRTCX_INTERLACE] = 0xff; /* interlace off */
1303 * Theory resumes here....
1307 * Graphics Display Controller
1309 regp->Graphics[0] = 0x00;
1310 regp->Graphics[1] = 0x00;
1311 regp->Graphics[2] = 0x00;
1312 regp->Graphics[3] = 0x00;
1314 regp->Graphics[4] = BIT_PLANE;
1315 regp->Graphics[5] = 0x00;
1317 regp->Graphics[4] = 0x00;
1319 regp->Graphics[5] = 0x02;
1321 regp->Graphics[5] = 0x40;
1324 regp->Graphics[6] = 0x05; /* only map 64k VGA memory !!!! */
1325 regp->Graphics[7] = 0x0F;
1326 regp->Graphics[8] = 0xFF;
1328 /* I ditched the mono stuff */
1329 regp->Attribute[0] = 0x00; /* standard colormap translation */
1330 regp->Attribute[1] = 0x01;
1331 regp->Attribute[2] = 0x02;
1332 regp->Attribute[3] = 0x03;
1333 regp->Attribute[4] = 0x04;
1334 regp->Attribute[5] = 0x05;
1335 regp->Attribute[6] = 0x06;
1336 regp->Attribute[7] = 0x07;
1337 regp->Attribute[8] = 0x08;
1338 regp->Attribute[9] = 0x09;
1339 regp->Attribute[10] = 0x0A;
1340 regp->Attribute[11] = 0x0B;
1341 regp->Attribute[12] = 0x0C;
1342 regp->Attribute[13] = 0x0D;
1343 regp->Attribute[14] = 0x0E;
1344 regp->Attribute[15] = 0x0F;
1345 /* These two below are non-vga */
1346 regp->Attribute[16] = 0x01;
1347 regp->Attribute[17] = 0x00;
1348 regp->Attribute[18] = 0x0F;
1349 regp->Attribute[19] = 0x00;
1350 regp->Attribute[20] = 0x00;
1353 #define MAX_H_VALUE(i) ((0x1ff + i) << 3)
1354 #define MAX_V_VALUE(i) ((0xfff + i) << 0)
1357 * Sets up registers for the given mode/adjusted_mode pair.
1359 * The clocks, CRTCs and outputs attached to this CRTC must be off.
1361 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
1362 * be easily turned on/off after this.
1365 nv_crtc_mode_set_regs(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
1367 ScrnInfoPtr pScrn = crtc->scrn;
1368 NVPtr pNv = NVPTR(pScrn);
1369 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1370 NVFBLayout *pLayout = &pNv->CurrentLayout;
1371 NVCrtcRegPtr regp, savep;
1375 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1376 savep = &pNv->SavedReg.crtc_reg[nv_crtc->head];
1378 xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
1379 NVOutputPrivatePtr nv_output = output->driver_private;
1381 if ((nv_output->type == OUTPUT_LVDS) || (nv_output->type == OUTPUT_TMDS))
1384 /* Registers not directly related to the (s)vga mode */
1386 /* bit2 = 0 -> fine pitched crtc granularity */
1387 /* The rest disables double buffering on CRTC access */
1388 regp->CRTC[NV_VGA_CRTCX_BUFFER] = 0xfa;
1390 if (savep->CRTC[NV_VGA_CRTCX_LCD] <= 0xb) {
1391 /* Common values are 0x0, 0x3, 0x8, 0xb, see logic below */
1392 if (nv_crtc->head == 0) {
1393 regp->CRTC[NV_VGA_CRTCX_LCD] = (1 << 3);
1397 regp->CRTC[NV_VGA_CRTCX_LCD] |= (1 << 0) | (1 << 1);
1400 /* Let's keep any abnormal value there may be, like 0x54 or 0x79 */
1401 regp->CRTC[NV_VGA_CRTCX_LCD] = savep->CRTC[NV_VGA_CRTCX_LCD];
1404 /* I'm trusting haiku driver on this one, they say it enables an external TDMS clock */
1406 regp->CRTC[NV_VGA_CRTCX_59] = 0x1;
1408 regp->CRTC[NV_VGA_CRTCX_59] = 0x0;
1412 * Initialize DAC palette.
1414 if(pLayout->bitsPerPixel != 8 ) {
1415 for (i = 0; i < 256; i++) {
1417 regp->DAC[(i*3)+1] = i;
1418 regp->DAC[(i*3)+2] = i;
1423 * Calculate the extended registers.
1426 if(pLayout->depth < 24) {
1432 if(pNv->Architecture >= NV_ARCH_10) {
1433 pNv->CURSOR = (CARD32 *)pNv->Cursor->map;
1436 /* What is the meaning of this register? */
1437 /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */
1438 regp->CRTC[NV_VGA_CRTCX_FIFO1] = savep->CRTC[NV_VGA_CRTCX_FIFO1] & ~(1<<5);
1440 /* NV40's don't set FPP units, unless in special conditions (then they set both) */
1441 /* But what are those special conditions? */
1442 if (pNv->Architecture <= NV_ARCH_30) {
1444 if(nv_crtc->head == 1) {
1445 regp->head |= NV_CRTC_FSEL_FPP1;
1446 } else if (pNv->twoHeads) {
1447 regp->head |= NV_CRTC_FSEL_FPP2;
1451 /* This is observed on some g70 cards, non-flatpanel's too */
1452 if (nv_crtc->head == 1) {
1453 regp->head |= NV_CRTC_FSEL_FPP2;
1457 /* Except for rare conditions I2C is enabled on the primary crtc */
1458 if (nv_crtc->head == 0) {
1459 if (pNv->overlayAdaptor) {
1460 regp->head |= NV_CRTC_FSEL_OVERLAY;
1462 regp->head |= NV_CRTC_FSEL_I2C;
1465 /* This is not what nv does, but it is what the blob does (for nv4x at least) */
1466 /* This fixes my cursor corruption issue */
1467 regp->cursorConfig = 0x0;
1468 if(mode->Flags & V_DBLSCAN)
1469 regp->cursorConfig |= (1 << 4);
1470 if (pNv->alphaCursor) {
1471 /* bit28 means we go into alpha blend mode and not rely on the current ROP */
1472 regp->cursorConfig |= 0x14011000;
1474 regp->cursorConfig |= 0x02000000;
1477 /* Unblock some timings */
1478 regp->CRTC[NV_VGA_CRTCX_FP_HTIMING] = 0;
1479 regp->CRTC[NV_VGA_CRTCX_FP_VTIMING] = 0;
1481 /* 0x20 seems to be enabled and 0x14 disabled */
1482 regp->CRTC[NV_VGA_CRTCX_26] = 0x20;
1484 /* 0x00 is disabled, 0x22 crt and 0x88 dfp */
1487 regp->CRTC[NV_VGA_CRTCX_3B] = 0x88;
1489 regp->CRTC[NV_VGA_CRTCX_3B] = 0x22;
1492 /* These values seem to vary */
1493 regp->CRTC[NV_VGA_CRTCX_3C] = savep->CRTC[NV_VGA_CRTCX_3C];
1495 /* 0x80 seems to be used very often, if not always */
1496 regp->CRTC[NV_VGA_CRTCX_45] = 0x80;
1498 /* Are these(0x55 and 0x56) also timing related registers, since disabling them does nothing? */
1499 regp->CRTC[NV_VGA_CRTCX_55] = 0x0;
1501 /* Common values like 0x14 and 0x04 are converted to 0x10 and 0x00 */
1502 //regp->CRTC[NV_VGA_CRTCX_56] = savep->CRTC[NV_VGA_CRTCX_56] & ~(1<<4);
1503 regp->CRTC[NV_VGA_CRTCX_56] = 0x0;
1505 regp->CRTC[NV_VGA_CRTCX_57] = 0x0;
1507 /* bit0: Seems to be mostly used on crtc1 */
1508 /* bit1: 1=crtc1, 0=crtc, but i'm unsure about this */
1509 /* 0x7E (crtc0, only seen in one dump) and 0x7F (crtc1) seem to be some kind of disable setting */
1510 /* This is likely to be incomplete */
1511 /* This is a very strange register, changed very often by the blob */
1512 regp->CRTC[NV_VGA_CRTCX_58] = 0x0;
1514 /* The blob seems to take the current value from crtc 0, add 4 to that and reuse the old value for crtc 1*/
1515 if (nv_crtc->head == 1) {
1516 regp->CRTC[NV_VGA_CRTCX_52] = pNv->misc_info.crtc_0_reg_52;
1518 regp->CRTC[NV_VGA_CRTCX_52] = pNv->misc_info.crtc_0_reg_52 + 4;
1521 /* The exact purpose of this register is unknown, but we copy value from crtc0 */
1522 regp->unk81c = nvReadCRTC0(pNv, NV_CRTC_081C);
1524 regp->unk830 = mode->CrtcVDisplay - 3;
1525 regp->unk834 = mode->CrtcVDisplay - 1;
1527 /* This is what the blob does */
1528 regp->unk850 = nvReadCRTC(pNv, 0, NV_CRTC_0850);
1530 /* Never ever modify gpio, unless you know very well what you're doing */
1531 regp->gpio = nvReadCRTC(pNv, 0, NV_CRTC_GPIO);
1534 * Calculate the state that is common to all crtc's (stored in the state struct).
1536 ErrorF("crtc %d %d %d\n", nv_crtc->crtc, mode->CrtcHDisplay, pScrn->displayWidth);
1537 nv_crtc_calc_state_ext(crtc,
1539 pScrn->displayWidth,
1542 adjusted_mode->Clock,
1545 /* Enable slaved mode */
1547 regp->CRTC[NV_VGA_CRTCX_PIXEL] |= (1 << 7);
1552 nv_crtc_mode_set_ramdac_regs(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
1554 ScrnInfoPtr pScrn = crtc->scrn;
1555 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1557 NVPtr pNv = NVPTR(pScrn);
1558 NVFBLayout *pLayout = &pNv->CurrentLayout;
1560 Bool is_lvds = FALSE;
1561 float aspect_ratio, panel_ratio;
1562 uint32_t h_scale, v_scale;
1564 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1566 xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
1567 NVOutputPrivatePtr nv_output = output->driver_private;
1569 if ((nv_output->type == OUTPUT_LVDS) || (nv_output->type == OUTPUT_TMDS)) {
1572 if (nv_output->type == OUTPUT_LVDS)
1575 regp->fp_horiz_regs[REG_DISP_END] = adjusted_mode->HDisplay - 1;
1576 regp->fp_horiz_regs[REG_DISP_TOTAL] = adjusted_mode->HTotal - 1;
1577 regp->fp_horiz_regs[REG_DISP_CRTC] = adjusted_mode->HDisplay;
1578 regp->fp_horiz_regs[REG_DISP_SYNC_START] = adjusted_mode->HSyncStart - 1;
1579 regp->fp_horiz_regs[REG_DISP_SYNC_END] = adjusted_mode->HSyncEnd - 1;
1580 regp->fp_horiz_regs[REG_DISP_VALID_START] = adjusted_mode->HSkew;
1581 regp->fp_horiz_regs[REG_DISP_VALID_END] = adjusted_mode->HDisplay - 1;
1583 regp->fp_vert_regs[REG_DISP_END] = adjusted_mode->VDisplay - 1;
1584 regp->fp_vert_regs[REG_DISP_TOTAL] = adjusted_mode->VTotal - 1;
1585 regp->fp_vert_regs[REG_DISP_CRTC] = adjusted_mode->VDisplay;
1586 regp->fp_vert_regs[REG_DISP_SYNC_START] = adjusted_mode->VSyncStart - 1;
1587 regp->fp_vert_regs[REG_DISP_SYNC_END] = adjusted_mode->VSyncEnd - 1;
1588 regp->fp_vert_regs[REG_DISP_VALID_START] = 0;
1589 regp->fp_vert_regs[REG_DISP_VALID_END] = adjusted_mode->VDisplay - 1;
1591 ErrorF("Horizontal:\n");
1592 ErrorF("REG_DISP_END: 0x%X\n", regp->fp_horiz_regs[REG_DISP_END]);
1593 ErrorF("REG_DISP_TOTAL: 0x%X\n", regp->fp_horiz_regs[REG_DISP_TOTAL]);
1594 ErrorF("REG_DISP_CRTC: 0x%X\n", regp->fp_horiz_regs[REG_DISP_CRTC]);
1595 ErrorF("REG_DISP_SYNC_START: 0x%X\n", regp->fp_horiz_regs[REG_DISP_SYNC_START]);
1596 ErrorF("REG_DISP_SYNC_END: 0x%X\n", regp->fp_horiz_regs[REG_DISP_SYNC_END]);
1597 ErrorF("REG_DISP_VALID_START: 0x%X\n", regp->fp_horiz_regs[REG_DISP_VALID_START]);
1598 ErrorF("REG_DISP_VALID_END: 0x%X\n", regp->fp_horiz_regs[REG_DISP_VALID_END]);
1600 ErrorF("Vertical:\n");
1601 ErrorF("REG_DISP_END: 0x%X\n", regp->fp_vert_regs[REG_DISP_END]);
1602 ErrorF("REG_DISP_TOTAL: 0x%X\n", regp->fp_vert_regs[REG_DISP_TOTAL]);
1603 ErrorF("REG_DISP_CRTC: 0x%X\n", regp->fp_vert_regs[REG_DISP_CRTC]);
1604 ErrorF("REG_DISP_SYNC_START: 0x%X\n", regp->fp_vert_regs[REG_DISP_SYNC_START]);
1605 ErrorF("REG_DISP_SYNC_END: 0x%X\n", regp->fp_vert_regs[REG_DISP_SYNC_END]);
1606 ErrorF("REG_DISP_VALID_START: 0x%X\n", regp->fp_vert_regs[REG_DISP_VALID_START]);
1607 ErrorF("REG_DISP_VALID_END: 0x%X\n", regp->fp_vert_regs[REG_DISP_VALID_END]);
1611 * bit0: positive vsync
1612 * bit4: positive hsync
1613 * bit8: enable panel scaling
1614 * bit26: a bit sometimes seen on some g70 cards
1615 * bit31: sometimes seen on LVDS panels
1616 * This must also be set for non-flatpanels
1617 * Some bits seem shifted for vga monitors
1621 regp->fp_control = 0x11100000;
1623 regp->fp_control = 0x21100000;
1625 if (nv_output->type == OUTPUT_LVDS) {
1626 /* Let's assume LVDS to be on ramdac0, remember that in the ramdac routing is somewhat random (compared to bios setup), so don't trust it */
1627 regp->fp_control = nvReadRAMDAC0(pNv, NV_RAMDAC_FP_CONTROL) & 0xfff00000;
1629 /* If the special bit exists, it exists on both ramdac's */
1630 regp->fp_control |= nvReadRAMDAC0(pNv, NV_RAMDAC_FP_CONTROL) & (1 << 26);
1633 /* Deal with vsync/hsync polarity */
1634 /* These analog monitor offsets are guesswork */
1635 if (adjusted_mode->Flags & V_PVSYNC) {
1636 regp->fp_control |= (1 << (0 + !is_fp));
1639 if (adjusted_mode->Flags & V_PHSYNC) {
1640 regp->fp_control |= (1 << (4 + !is_fp));
1644 ErrorF("Pre-panel scaling\n");
1645 ErrorF("panel-size:%dx%d\n", nv_output->fpWidth, nv_output->fpHeight);
1646 panel_ratio = (nv_output->fpWidth)/(float)(nv_output->fpHeight);
1647 ErrorF("panel_ratio=%f\n", panel_ratio);
1648 aspect_ratio = (mode->HDisplay)/(float)(mode->VDisplay);
1649 ErrorF("aspect_ratio=%f\n", aspect_ratio);
1650 /* Scale factors is the so called 20.12 format, taken from Haiku */
1651 h_scale = ((1 << 12) * mode->HDisplay)/nv_output->fpWidth;
1652 v_scale = ((1 << 12) * mode->VDisplay)/nv_output->fpHeight;
1653 ErrorF("h_scale=%d\n", h_scale);
1654 ErrorF("v_scale=%d\n", v_scale);
1656 /* Don't limit last fetched line */
1659 /* We want automatic scaling */
1662 regp->fp_hvalid_start = 0;
1663 regp->fp_hvalid_end = (nv_output->fpWidth - 1);
1665 regp->fp_vvalid_start = 0;
1666 regp->fp_vvalid_end = (nv_output->fpHeight - 1);
1668 if (!pNv->fpScaler) {
1669 ErrorF("Flat panel is doing the scaling.\n");
1670 regp->fp_control |= (1 << 8);
1672 ErrorF("GPU is doing the scaling.\n");
1673 /* GPU scaling happens automaticly at a ratio of 1.33 */
1674 /* A 1280x1024 panel has a ratio of 1.25, we don't want to scale that at 4:3 resolutions */
1675 if (h_scale != (1 << 12) && (panel_ratio > (aspect_ratio + 0.10))) {
1678 ErrorF("Scaling resolution on a widescreen panel\n");
1680 /* Scaling in both directions needs to the same */
1683 /* Set a new horizontal scale factor and enable testmode (bit12) */
1684 regp->debug_1 = ((h_scale >> 1) & 0xfff) | (1 << 12);
1686 diff = nv_output->fpWidth - (((1 << 12) * mode->HDisplay)/h_scale);
1687 regp->fp_hvalid_start = diff/2;
1688 regp->fp_hvalid_end = nv_output->fpWidth - (diff/2) - 1;
1691 /* Same scaling, just for panels with aspect ratio's smaller than 1 */
1692 if (v_scale != (1 << 12) && (panel_ratio < (aspect_ratio - 0.10))) {
1695 ErrorF("Scaling resolution on a portrait panel\n");
1697 /* Scaling in both directions needs to the same */
1700 /* Set a new vertical scale factor and enable testmode (bit28) */
1701 regp->debug_1 = (((v_scale >> 1) & 0xfff) << 16) | (1 << (12 + 16));
1703 diff = nv_output->fpHeight - (((1 << 12) * mode->VDisplay)/v_scale);
1704 regp->fp_vvalid_start = diff/2;
1705 regp->fp_vvalid_end = nv_output->fpHeight - (diff/2) - 1;
1709 ErrorF("Post-panel scaling\n");
1712 if (pNv->Architecture >= NV_ARCH_10) {
1713 /* Bios and blob don't seem to do anything (else) */
1714 regp->nv10_cursync = (1<<25);
1717 /* These are the common blob values, minus a few fp specific bit's */
1718 /* Let's keep the TMDS pll and fpclock running in all situations */
1719 regp->debug_0 = 0x1101111;
1722 /* I am not completely certain, but seems to be set only for dfp's */
1723 regp->debug_0 |= NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED;
1726 ErrorF("output %d debug_0 %08X\n", nv_output->ramdac, regp->debug_0);
1728 /* Flatpanel support needs at least a NV10 */
1730 /* Instead of 1, several other values are also used: 2, 7, 9 */
1731 /* The purpose is unknown */
1733 regp->dither = 0x00010000;
1737 /* Kindly borrowed from haiku driver */
1738 /* bit4 and bit5 activate indirect mode trough color palette */
1739 switch (pLayout->depth) {
1742 regp->general = 0x00101130;
1746 regp->general = 0x00100130;
1750 regp->general = 0x00101100;
1754 if (pNv->alphaCursor) {
1755 regp->general |= (1<<29);
1758 /* Some values the blob sets */
1759 /* This may apply to the real ramdac that is being used (for crosswired situations) */
1760 /* Nevertheless, it's unlikely to cause many problems, since the values are equal for both */
1761 regp->unk_a20 = 0x0;
1762 regp->unk_a24 = 0xfffff;
1763 regp->unk_a34 = 0x1;
1767 * Sets up registers for the given mode/adjusted_mode pair.
1769 * The clocks, CRTCs and outputs attached to this CRTC must be off.
1771 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
1772 * be easily turned on/off after this.
1775 nv_crtc_mode_set(xf86CrtcPtr crtc, DisplayModePtr mode,
1776 DisplayModePtr adjusted_mode,
1779 ScrnInfoPtr pScrn = crtc->scrn;
1780 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1781 NVPtr pNv = NVPTR(pScrn);
1783 ErrorF("nv_crtc_mode_set is called for CRTC %d\n", nv_crtc->crtc);
1785 xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Mode on CRTC %d\n", nv_crtc->crtc);
1786 xf86PrintModeline(pScrn->scrnIndex, mode);
1787 NVCrtcSetOwner(crtc);
1789 nv_crtc_mode_set_vga(crtc, mode, adjusted_mode);
1790 nv_crtc_mode_set_regs(crtc, mode, adjusted_mode);
1791 nv_crtc_mode_set_ramdac_regs(crtc, mode, adjusted_mode);
1794 NVCrtcLockUnlock(crtc, FALSE);
1796 NVVgaProtect(crtc, TRUE);
1797 nv_crtc_load_state_ext(crtc, &pNv->ModeReg);
1798 nv_crtc_load_state_vga(crtc, &pNv->ModeReg);
1799 if (pNv->Architecture == NV_ARCH_40) {
1800 nv40_crtc_load_state_pll(pNv, &pNv->ModeReg);
1802 nv_crtc_load_state_pll(pNv, &pNv->ModeReg);
1804 nv_crtc_load_state_ramdac(crtc, &pNv->ModeReg);
1806 NVVgaProtect(crtc, FALSE);
1808 NVCrtcSetBase(crtc, x, y);
1810 #if X_BYTE_ORDER == X_BIG_ENDIAN
1811 /* turn on LFB swapping */
1815 tmp = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_SWAPPING);
1817 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_SWAPPING, tmp);
1822 void nv_crtc_save(xf86CrtcPtr crtc)
1824 ScrnInfoPtr pScrn = crtc->scrn;
1825 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1826 NVPtr pNv = NVPTR(pScrn);
1828 ErrorF("nv_crtc_save is called for CRTC %d\n", nv_crtc->crtc);
1830 /* We just came back from terminal, so unlock */
1831 NVCrtcLockUnlock(crtc, FALSE);
1833 NVCrtcSetOwner(crtc);
1834 nv_crtc_save_state_vga(crtc, &pNv->SavedReg);
1835 nv_crtc_save_state_ext(crtc, &pNv->SavedReg);
1836 if (pNv->Architecture == NV_ARCH_40) {
1837 nv40_crtc_save_state_pll(pNv, &pNv->SavedReg);
1839 nv_crtc_save_state_pll(pNv, &pNv->SavedReg);
1841 nv_crtc_save_state_ramdac(crtc, &pNv->SavedReg);
1844 void nv_crtc_restore(xf86CrtcPtr crtc)
1846 ScrnInfoPtr pScrn = crtc->scrn;
1847 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1848 NVPtr pNv = NVPTR(pScrn);
1850 ErrorF("nv_crtc_restore is called for CRTC %d\n", nv_crtc->crtc);
1852 NVCrtcSetOwner(crtc);
1854 /* Just to be safe */
1855 NVCrtcLockUnlock(crtc, FALSE);
1857 NVVgaProtect(crtc, TRUE);
1858 nv_crtc_load_state_ext(crtc, &pNv->SavedReg);
1859 nv_crtc_load_state_vga(crtc, &pNv->SavedReg);
1860 if (pNv->Architecture == NV_ARCH_40) {
1861 nv40_crtc_load_state_pll(pNv, &pNv->SavedReg);
1863 nv_crtc_load_state_pll(pNv, &pNv->SavedReg);
1865 nv_crtc_load_state_ramdac(crtc, &pNv->SavedReg);
1866 nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->vtOWNER);
1867 NVVgaProtect(crtc, FALSE);
1869 /* We must lock the door if we leave ;-) */
1870 NVCrtcLockUnlock(crtc, TRUE);
1873 void nv_crtc_prepare(xf86CrtcPtr crtc)
1875 ScrnInfoPtr pScrn = crtc->scrn;
1876 NVPtr pNv = NVPTR(pScrn);
1877 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1879 ErrorF("nv_crtc_prepare is called for CRTC %d\n", nv_crtc->crtc);
1881 crtc->funcs->dpms(crtc, DPMSModeOff);
1883 /* Sync the engine before adjust mode */
1884 if (pNv->EXADriverPtr) {
1885 exaMarkSync(pScrn->pScreen);
1886 exaWaitSync(pScrn->pScreen);
1890 void nv_crtc_commit(xf86CrtcPtr crtc)
1892 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1893 ErrorF("nv_crtc_commit for CRTC %d\n", nv_crtc->crtc);
1895 crtc->funcs->dpms (crtc, DPMSModeOn);
1896 if (crtc->scrn->pScreen != NULL)
1897 xf86_reload_cursors (crtc->scrn->pScreen);
1900 static Bool nv_crtc_lock(xf86CrtcPtr crtc)
1902 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1903 ErrorF("nv_crtc_lock is called for CRTC %d\n", nv_crtc->crtc);
1908 static void nv_crtc_unlock(xf86CrtcPtr crtc)
1910 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1911 ErrorF("nv_crtc_unlock is called for CRTC %d\n", nv_crtc->crtc);
1915 nv_crtc_gamma_set(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
1918 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1919 ScrnInfoPtr pScrn = crtc->scrn;
1920 NVPtr pNv = NVPTR(pScrn);
1924 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1926 switch (pNv->CurrentLayout.depth) {
1929 /* We've got 5 bit (32 values) colors and 256 registers for each color */
1930 for (i = 0; i < 32; i++) {
1931 for (j = 0; j < 8; j++) {
1932 regp->DAC[(i*8 + j) * 3 + 0] = red[i] >> 8;
1933 regp->DAC[(i*8 + j) * 3 + 1] = green[i] >> 8;
1934 regp->DAC[(i*8 + j) * 3 + 2] = blue[i] >> 8;
1940 /* First deal with the 5 bit colors */
1941 for (i = 0; i < 32; i++) {
1942 for (j = 0; j < 8; j++) {
1943 regp->DAC[(i*8 + j) * 3 + 0] = red[i] >> 8;
1944 regp->DAC[(i*8 + j) * 3 + 2] = blue[i] >> 8;
1947 /* Now deal with the 6 bit color */
1948 for (i = 0; i < 64; i++) {
1949 for (j = 0; j < 4; j++) {
1950 regp->DAC[(i*4 + j) * 3 + 1] = green[i] >> 8;
1956 for (i = 0; i < 256; i++) {
1957 regp->DAC[i * 3] = red[i] >> 8;
1958 regp->DAC[(i * 3) + 1] = green[i] >> 8;
1959 regp->DAC[(i * 3) + 2] = blue[i] >> 8;
1964 NVCrtcLoadPalette(crtc);
1967 /* NV04-NV10 doesn't support alpha cursors */
1968 static const xf86CrtcFuncsRec nv_crtc_funcs = {
1969 .dpms = nv_crtc_dpms,
1970 .save = nv_crtc_save, /* XXX */
1971 .restore = nv_crtc_restore, /* XXX */
1972 .mode_fixup = nv_crtc_mode_fixup,
1973 .mode_set = nv_crtc_mode_set,
1974 .prepare = nv_crtc_prepare,
1975 .commit = nv_crtc_commit,
1976 .destroy = NULL, /* XXX */
1977 .lock = nv_crtc_lock,
1978 .unlock = nv_crtc_unlock,
1979 .set_cursor_colors = nv_crtc_set_cursor_colors,
1980 .set_cursor_position = nv_crtc_set_cursor_position,
1981 .show_cursor = nv_crtc_show_cursor,
1982 .hide_cursor = nv_crtc_hide_cursor,
1983 .load_cursor_image = nv_crtc_load_cursor_image,
1984 .gamma_set = nv_crtc_gamma_set,
1987 /* NV11 and up has support for alpha cursors. */
1988 /* Due to different maximum sizes we cannot allow it to use normal cursors */
1989 static const xf86CrtcFuncsRec nv11_crtc_funcs = {
1990 .dpms = nv_crtc_dpms,
1991 .save = nv_crtc_save, /* XXX */
1992 .restore = nv_crtc_restore, /* XXX */
1993 .mode_fixup = nv_crtc_mode_fixup,
1994 .mode_set = nv_crtc_mode_set,
1995 .prepare = nv_crtc_prepare,
1996 .commit = nv_crtc_commit,
1997 .destroy = NULL, /* XXX */
1998 .lock = nv_crtc_lock,
1999 .unlock = nv_crtc_unlock,
2000 .set_cursor_colors = NULL, /* Alpha cursors do not need this */
2001 .set_cursor_position = nv_crtc_set_cursor_position,
2002 .show_cursor = nv_crtc_show_cursor,
2003 .hide_cursor = nv_crtc_hide_cursor,
2004 .load_cursor_argb = nv_crtc_load_cursor_argb,
2005 .gamma_set = nv_crtc_gamma_set,
2010 nv_crtc_init(ScrnInfoPtr pScrn, int crtc_num)
2012 NVPtr pNv = NVPTR(pScrn);
2014 NVCrtcPrivatePtr nv_crtc;
2016 if (pNv->NVArch >= 0x11) {
2017 crtc = xf86CrtcCreate (pScrn, &nv11_crtc_funcs);
2019 crtc = xf86CrtcCreate (pScrn, &nv_crtc_funcs);
2024 nv_crtc = xnfcalloc (sizeof (NVCrtcPrivateRec), 1);
2025 nv_crtc->crtc = crtc_num;
2026 nv_crtc->head = crtc_num;
2028 crtc->driver_private = nv_crtc;
2030 NVCrtcLockUnlock(crtc, FALSE);
2033 static void nv_crtc_load_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2035 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2039 regp = &state->crtc_reg[nv_crtc->head];
2041 NVWriteMiscOut(crtc, regp->MiscOutReg);
2043 for (i = 1; i < 5; i++)
2044 NVWriteVgaSeq(crtc, i, regp->Sequencer[i]);
2046 /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17] */
2047 NVWriteVgaCrtc(crtc, 17, regp->CRTC[17] & ~0x80);
2049 for (i = 0; i < 25; i++)
2050 NVWriteVgaCrtc(crtc, i, regp->CRTC[i]);
2052 for (i = 0; i < 9; i++)
2053 NVWriteVgaGr(crtc, i, regp->Graphics[i]);
2055 NVEnablePalette(crtc);
2056 for (i = 0; i < 21; i++)
2057 NVWriteVgaAttr(crtc, i, regp->Attribute[i]);
2058 NVDisablePalette(crtc);
2062 static void nv_crtc_fix_nv40_hw_cursor(xf86CrtcPtr crtc)
2064 /* TODO - implement this properly */
2065 ScrnInfoPtr pScrn = crtc->scrn;
2066 NVPtr pNv = NVPTR(pScrn);
2068 if(pNv->Architecture == NV_ARCH_40) { /* HW bug */
2069 volatile CARD32 curpos = nvReadCurRAMDAC(pNv, NV_RAMDAC_CURSOR_POS);
2070 nvWriteCurRAMDAC(pNv, NV_RAMDAC_CURSOR_POS, curpos);
2074 static void nv_crtc_load_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2076 ScrnInfoPtr pScrn = crtc->scrn;
2077 NVPtr pNv = NVPTR(pScrn);
2078 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2081 regp = &state->crtc_reg[nv_crtc->head];
2083 if(pNv->Architecture >= NV_ARCH_10) {
2085 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_FSEL, regp->head);
2087 nvWriteVIDEO(pNv, NV_PVIDEO_STOP, 1);
2088 nvWriteVIDEO(pNv, NV_PVIDEO_INTR_EN, 0);
2089 nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(0), 0);
2090 nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(1), 0);
2091 nvWriteVIDEO(pNv, NV_PVIDEO_LIMIT(0), pNv->VRAMPhysicalSize - 1);
2092 nvWriteVIDEO(pNv, NV_PVIDEO_LIMIT(1), pNv->VRAMPhysicalSize - 1);
2093 nvWriteMC(pNv, 0x1588, 0);
2095 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_BUFFER, regp->CRTC[NV_VGA_CRTCX_BUFFER]);
2096 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_CURSOR_CONFIG, regp->cursorConfig);
2097 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_GPIO, regp->gpio);
2098 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0830, regp->unk830);
2099 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0834, regp->unk834);
2100 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0850, regp->unk850);
2101 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_081C, regp->unk81c);
2103 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FP_HTIMING, regp->CRTC[NV_VGA_CRTCX_FP_HTIMING]);
2104 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FP_VTIMING, regp->CRTC[NV_VGA_CRTCX_FP_VTIMING]);
2106 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_26, regp->CRTC[NV_VGA_CRTCX_26]);
2107 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_3B, regp->CRTC[NV_VGA_CRTCX_3B]);
2108 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_3C, regp->CRTC[NV_VGA_CRTCX_3C]);
2109 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_45, regp->CRTC[NV_VGA_CRTCX_45]);
2110 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_52, regp->CRTC[NV_VGA_CRTCX_52]);
2111 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_56, regp->CRTC[NV_VGA_CRTCX_56]);
2112 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_57, regp->CRTC[NV_VGA_CRTCX_57]);
2113 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_58, regp->CRTC[NV_VGA_CRTCX_58]);
2114 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_59, regp->CRTC[NV_VGA_CRTCX_59]);
2115 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_EXTRA, regp->CRTC[NV_VGA_CRTCX_EXTRA]);
2118 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT0, regp->CRTC[NV_VGA_CRTCX_REPAINT0]);
2119 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1, regp->CRTC[NV_VGA_CRTCX_REPAINT1]);
2120 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LSR, regp->CRTC[NV_VGA_CRTCX_LSR]);
2121 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_PIXEL, regp->CRTC[NV_VGA_CRTCX_PIXEL]);
2122 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LCD, regp->CRTC[NV_VGA_CRTCX_LCD]);
2123 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_HEB, regp->CRTC[NV_VGA_CRTCX_HEB]);
2124 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO1, regp->CRTC[NV_VGA_CRTCX_FIFO1]);
2125 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO0, regp->CRTC[NV_VGA_CRTCX_FIFO0]);
2126 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM, regp->CRTC[NV_VGA_CRTCX_FIFO_LWM]);
2127 if(pNv->Architecture >= NV_ARCH_30) {
2128 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM_NV30, regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30]);
2131 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL0, regp->CRTC[NV_VGA_CRTCX_CURCTL0]);
2132 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL1, regp->CRTC[NV_VGA_CRTCX_CURCTL1]);
2133 nv_crtc_fix_nv40_hw_cursor(crtc);
2134 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL2, regp->CRTC[NV_VGA_CRTCX_CURCTL2]);
2135 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_INTERLACE, regp->CRTC[NV_VGA_CRTCX_INTERLACE]);
2137 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_INTR_EN_0, 0);
2138 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_INTR_0, NV_CRTC_INTR_VBLANK);
2140 pNv->CurrentState = state;
2143 static void nv_crtc_save_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2145 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2149 regp = &state->crtc_reg[nv_crtc->head];
2151 regp->MiscOutReg = NVReadMiscOut(crtc);
2153 for (i = 0; i < 25; i++)
2154 regp->CRTC[i] = NVReadVgaCrtc(crtc, i);
2156 NVEnablePalette(crtc);
2157 for (i = 0; i < 21; i++)
2158 regp->Attribute[i] = NVReadVgaAttr(crtc, i);
2159 NVDisablePalette(crtc);
2161 for (i = 0; i < 9; i++)
2162 regp->Graphics[i] = NVReadVgaGr(crtc, i);
2164 for (i = 1; i < 5; i++)
2165 regp->Sequencer[i] = NVReadVgaSeq(crtc, i);
2169 static void nv_crtc_save_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2171 ScrnInfoPtr pScrn = crtc->scrn;
2172 NVPtr pNv = NVPTR(pScrn);
2173 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2176 regp = &state->crtc_reg[nv_crtc->head];
2178 regp->CRTC[NV_VGA_CRTCX_LCD] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_LCD);
2179 regp->CRTC[NV_VGA_CRTCX_REPAINT0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT0);
2180 regp->CRTC[NV_VGA_CRTCX_REPAINT1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1);
2181 regp->CRTC[NV_VGA_CRTCX_LSR] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_LSR);
2182 regp->CRTC[NV_VGA_CRTCX_PIXEL] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_PIXEL);
2183 regp->CRTC[NV_VGA_CRTCX_HEB] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_HEB);
2184 regp->CRTC[NV_VGA_CRTCX_FIFO1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO1);
2186 regp->CRTC[NV_VGA_CRTCX_FIFO0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO0);
2187 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM);
2188 if(pNv->Architecture >= NV_ARCH_30) {
2189 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM_NV30);
2191 regp->CRTC[NV_VGA_CRTCX_CURCTL0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL0);
2192 regp->CRTC[NV_VGA_CRTCX_CURCTL1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL1);
2193 regp->CRTC[NV_VGA_CRTCX_CURCTL2] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL2);
2194 regp->CRTC[NV_VGA_CRTCX_INTERLACE] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_INTERLACE);
2196 regp->gpio = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_GPIO);
2197 regp->unk830 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0830);
2198 regp->unk834 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0834);
2199 regp->unk850 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0850);
2200 regp->unk81c = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_081C);
2202 if(pNv->Architecture >= NV_ARCH_10) {
2204 regp->head = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_FSEL);
2205 regp->crtcOwner = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_OWNER);
2207 regp->CRTC[NV_VGA_CRTCX_EXTRA] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_EXTRA);
2209 regp->cursorConfig = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_CURSOR_CONFIG);
2211 regp->CRTC[NV_VGA_CRTCX_26] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_26);
2212 regp->CRTC[NV_VGA_CRTCX_3B] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_3B);
2213 regp->CRTC[NV_VGA_CRTCX_3C] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_3C);
2214 regp->CRTC[NV_VGA_CRTCX_45] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_45);
2215 regp->CRTC[NV_VGA_CRTCX_52] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_52);
2216 regp->CRTC[NV_VGA_CRTCX_56] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_56);
2217 regp->CRTC[NV_VGA_CRTCX_57] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_57);
2218 regp->CRTC[NV_VGA_CRTCX_58] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_58);
2219 regp->CRTC[NV_VGA_CRTCX_59] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_59);
2220 regp->CRTC[NV_VGA_CRTCX_BUFFER] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_BUFFER);
2221 regp->CRTC[NV_VGA_CRTCX_FP_HTIMING] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FP_HTIMING);
2222 regp->CRTC[NV_VGA_CRTCX_FP_VTIMING] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FP_VTIMING);
2226 static void nv_crtc_save_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2228 ScrnInfoPtr pScrn = crtc->scrn;
2229 NVPtr pNv = NVPTR(pScrn);
2230 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2234 regp = &state->crtc_reg[nv_crtc->head];
2236 regp->general = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_GENERAL_CONTROL);
2238 regp->fp_control = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_CONTROL);
2239 regp->debug_0 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_0);
2240 regp->debug_1 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_1);
2241 regp->debug_2 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_2);
2243 regp->unk_a20 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A20);
2244 regp->unk_a24 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A24);
2245 regp->unk_a34 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A34);
2247 if (pNv->NVArch == 0x11) {
2248 regp->dither = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_DITHER_NV11);
2249 } else if (pNv->twoHeads) {
2250 regp->dither = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DITHER);
2252 regp->nv10_cursync = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_NV10_CURSYNC);
2254 /* The regs below are 0 for non-flatpanels, so you can load and save them */
2256 for (i = 0; i < 7; i++) {
2257 uint32_t ramdac_reg = NV_RAMDAC_FP_HDISP_END + (i * 4);
2258 regp->fp_horiz_regs[i] = nvReadRAMDAC(pNv, nv_crtc->head, ramdac_reg);
2261 for (i = 0; i < 7; i++) {
2262 uint32_t ramdac_reg = NV_RAMDAC_FP_VDISP_END + (i * 4);
2263 regp->fp_vert_regs[i] = nvReadRAMDAC(pNv, nv_crtc->head, ramdac_reg);
2266 regp->fp_hvalid_start = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_HVALID_START);
2267 regp->fp_hvalid_end = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_HVALID_END);
2268 regp->fp_vvalid_start = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_VVALID_START);
2269 regp->fp_vvalid_end = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_VVALID_END);
2272 static void nv_crtc_load_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2274 ScrnInfoPtr pScrn = crtc->scrn;
2275 NVPtr pNv = NVPTR(pScrn);
2276 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2280 regp = &state->crtc_reg[nv_crtc->head];
2282 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_GENERAL_CONTROL, regp->general);
2284 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_CONTROL, regp->fp_control);
2285 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_0, regp->debug_0);
2286 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_1, regp->debug_1);
2287 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_2, regp->debug_2);
2289 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A20, regp->unk_a20);
2290 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A24, regp->unk_a24);
2291 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A34, regp->unk_a34);
2293 if (pNv->NVArch == 0x11) {
2294 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_DITHER_NV11, regp->dither);
2295 } else if (pNv->twoHeads) {
2296 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DITHER, regp->dither);
2298 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_NV10_CURSYNC, regp->nv10_cursync);
2300 /* The regs below are 0 for non-flatpanels, so you can load and save them */
2302 for (i = 0; i < 7; i++) {
2303 uint32_t ramdac_reg = NV_RAMDAC_FP_HDISP_END + (i * 4);
2304 nvWriteRAMDAC(pNv, nv_crtc->head, ramdac_reg, regp->fp_horiz_regs[i]);
2307 for (i = 0; i < 7; i++) {
2308 uint32_t ramdac_reg = NV_RAMDAC_FP_VDISP_END + (i * 4);
2309 nvWriteRAMDAC(pNv, nv_crtc->head, ramdac_reg, regp->fp_vert_regs[i]);
2312 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_HVALID_START, regp->fp_hvalid_start);
2313 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_HVALID_END, regp->fp_hvalid_end);
2314 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_VVALID_START, regp->fp_vvalid_start);
2315 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_VVALID_END, regp->fp_vvalid_end);
2319 NVCrtcSetBase (xf86CrtcPtr crtc, int x, int y)
2321 ScrnInfoPtr pScrn = crtc->scrn;
2322 NVPtr pNv = NVPTR(pScrn);
2323 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2324 NVFBLayout *pLayout = &pNv->CurrentLayout;
2327 ErrorF("NVCrtcSetBase: x: %d y: %d\n", x, y);
2329 start += ((y * pScrn->displayWidth + x) * (pLayout->bitsPerPixel/8));
2330 start += pNv->FB->offset;
2332 /* 30 bits addresses in 32 bits according to haiku */
2333 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_START, start & 0xfffffffc);
2335 /* set NV4/NV10 byte adress: (bit0 - 1) */
2336 NVWriteVgaAttr(crtc, 0x13, (start & 0x3) << 1);
2342 static void NVCrtcWriteDacMask(xf86CrtcPtr crtc, CARD8 value)
2344 ScrnInfoPtr pScrn = crtc->scrn;
2345 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2346 NVPtr pNv = NVPTR(pScrn);
2347 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2349 NV_WR08(pDACReg, VGA_DAC_MASK, value);
2352 static CARD8 NVCrtcReadDacMask(xf86CrtcPtr crtc)
2354 ScrnInfoPtr pScrn = crtc->scrn;
2355 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2356 NVPtr pNv = NVPTR(pScrn);
2357 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2359 return NV_RD08(pDACReg, VGA_DAC_MASK);
2362 static void NVCrtcWriteDacReadAddr(xf86CrtcPtr crtc, CARD8 value)
2364 ScrnInfoPtr pScrn = crtc->scrn;
2365 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2366 NVPtr pNv = NVPTR(pScrn);
2367 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2369 NV_WR08(pDACReg, VGA_DAC_READ_ADDR, value);
2372 static void NVCrtcWriteDacWriteAddr(xf86CrtcPtr crtc, CARD8 value)
2374 ScrnInfoPtr pScrn = crtc->scrn;
2375 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2376 NVPtr pNv = NVPTR(pScrn);
2377 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2379 NV_WR08(pDACReg, VGA_DAC_WRITE_ADDR, value);
2382 static void NVCrtcWriteDacData(xf86CrtcPtr crtc, CARD8 value)
2384 ScrnInfoPtr pScrn = crtc->scrn;
2385 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2386 NVPtr pNv = NVPTR(pScrn);
2387 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2389 NV_WR08(pDACReg, VGA_DAC_DATA, value);
2392 static CARD8 NVCrtcReadDacData(xf86CrtcPtr crtc, CARD8 value)
2394 ScrnInfoPtr pScrn = crtc->scrn;
2395 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2396 NVPtr pNv = NVPTR(pScrn);
2397 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2399 return NV_RD08(pDACReg, VGA_DAC_DATA);
2402 void NVCrtcLoadPalette(xf86CrtcPtr crtc)
2405 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2407 ScrnInfoPtr pScrn = crtc->scrn;
2408 NVPtr pNv = NVPTR(pScrn);
2410 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
2412 NVCrtcSetOwner(crtc);
2413 NVCrtcWriteDacMask(crtc, 0xff);
2414 NVCrtcWriteDacWriteAddr(crtc, 0x00);
2416 for (i = 0; i<768; i++) {
2417 NVCrtcWriteDacData(crtc, regp->DAC[i]);
2419 NVDisablePalette(crtc);
2422 void NVCrtcBlankScreen(xf86CrtcPtr crtc, Bool on)
2426 NVCrtcSetOwner(crtc);
2428 scrn = NVReadVgaSeq(crtc, 0x01);
2435 NVVgaSeqReset(crtc, TRUE);
2436 NVWriteVgaSeq(crtc, 0x01, scrn);
2437 NVVgaSeqReset(crtc, FALSE);
2440 /*************************************************************************** \
2442 |* Copyright 1993-2003 NVIDIA, Corporation. All rights reserved. *|
2444 |* NOTICE TO USER: The source code is copyrighted under U.S. and *|
2445 |* international laws. Users and possessors of this source code are *|
2446 |* hereby granted a nonexclusive, royalty-free copyright license to *|
2447 |* use this code in individual and commercial software. *|
2449 |* Any use of this source code must include, in the user documenta- *|
2450 |* tion and internal comments to the code, notices to the end user *|
2453 |* Copyright 1993-1999 NVIDIA, Corporation. All rights reserved. *|
2455 |* NVIDIA, CORPORATION MAKES NO REPRESENTATION ABOUT THE SUITABILITY *|
2456 |* OF THIS SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" *|
2457 |* WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. NVIDIA, CORPOR- *|
2458 |* ATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOURCE CODE, *|
2459 |* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGE- *|
2460 |* MENT, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL *|
2461 |* NVIDIA, CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT, INCI- *|
2462 |* DENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RE- *|
2463 |* SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION *|
2464 |* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF *|
2465 |* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE. *|
2467 |* U.S. Government End Users. This source code is a "commercial *|
2468 |* item," as that term is defined at 48 C.F.R. 2.101 (OCT 1995), *|
2469 |* consisting of "commercial computer software" and "commercial *|
2470 |* computer software documentation," as such terms are used in *|
2471 |* 48 C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Govern- *|
2472 |* ment only as a commercial end item. Consistent with 48 C.F.R. *|
2473 |* 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), *|
2474 |* all U.S. Government End Users acquire the source code with only *|
2475 |* those rights set forth herein. *|
2477 \***************************************************************************/