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_save_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
62 static void nv_crtc_save_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
64 static CARD8 NVReadPVIO(xf86CrtcPtr crtc, CARD32 address)
66 ScrnInfoPtr pScrn = crtc->scrn;
67 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
68 NVPtr pNv = NVPTR(pScrn);
70 /* Only NV4x have two pvio ranges */
71 if (nv_crtc->head == 1 && pNv->Architecture == NV_ARCH_40) {
72 return NV_RD08(pNv->PVIO1, address);
74 return NV_RD08(pNv->PVIO0, address);
78 static void NVWritePVIO(xf86CrtcPtr crtc, CARD32 address, CARD8 value)
80 ScrnInfoPtr pScrn = crtc->scrn;
81 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
82 NVPtr pNv = NVPTR(pScrn);
84 /* Only NV4x have two pvio ranges */
85 if (nv_crtc->head == 1 && pNv->Architecture == NV_ARCH_40) {
86 NV_WR08(pNv->PVIO1, address, value);
88 NV_WR08(pNv->PVIO0, address, value);
92 static void NVWriteMiscOut(xf86CrtcPtr crtc, CARD8 value)
94 NVWritePVIO(crtc, VGA_MISC_OUT_W, value);
97 static CARD8 NVReadMiscOut(xf86CrtcPtr crtc)
99 return NVReadPVIO(crtc, VGA_MISC_OUT_R);
102 void NVWriteVGA(NVPtr pNv, int head, CARD8 index, CARD8 value)
104 volatile CARD8 *pCRTCReg = head ? pNv->PCIO1 : pNv->PCIO0;
106 NV_WR08(pCRTCReg, CRTC_INDEX, index);
107 NV_WR08(pCRTCReg, CRTC_DATA, value);
110 CARD8 NVReadVGA(NVPtr pNv, int head, CARD8 index)
112 volatile CARD8 *pCRTCReg = head ? pNv->PCIO1 : pNv->PCIO0;
114 NV_WR08(pCRTCReg, CRTC_INDEX, index);
115 return NV_RD08(pCRTCReg, CRTC_DATA);
118 void NVWriteVgaCrtc(xf86CrtcPtr crtc, CARD8 index, CARD8 value)
120 ScrnInfoPtr pScrn = crtc->scrn;
121 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
122 NVPtr pNv = NVPTR(pScrn);
124 NVWriteVGA(pNv, nv_crtc->head, index, value);
127 CARD8 NVReadVgaCrtc(xf86CrtcPtr crtc, CARD8 index)
129 ScrnInfoPtr pScrn = crtc->scrn;
130 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
131 NVPtr pNv = NVPTR(pScrn);
133 return NVReadVGA(pNv, nv_crtc->head, index);
136 static void NVWriteVgaSeq(xf86CrtcPtr crtc, CARD8 index, CARD8 value)
138 NVWritePVIO(crtc, VGA_SEQ_INDEX, index);
139 NVWritePVIO(crtc, VGA_SEQ_DATA, value);
142 static CARD8 NVReadVgaSeq(xf86CrtcPtr crtc, CARD8 index)
144 NVWritePVIO(crtc, VGA_SEQ_INDEX, index);
145 return NVReadPVIO(crtc, VGA_SEQ_DATA);
148 static void NVWriteVgaGr(xf86CrtcPtr crtc, CARD8 index, CARD8 value)
150 NVWritePVIO(crtc, VGA_GRAPH_INDEX, index);
151 NVWritePVIO(crtc, VGA_GRAPH_DATA, value);
154 static CARD8 NVReadVgaGr(xf86CrtcPtr crtc, CARD8 index)
156 NVWritePVIO(crtc, VGA_GRAPH_INDEX, index);
157 return NVReadPVIO(crtc, VGA_GRAPH_DATA);
161 static void NVWriteVgaAttr(xf86CrtcPtr crtc, CARD8 index, CARD8 value)
163 ScrnInfoPtr pScrn = crtc->scrn;
164 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
165 NVPtr pNv = NVPTR(pScrn);
166 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
168 NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
169 if (nv_crtc->paletteEnabled)
173 NV_WR08(pCRTCReg, VGA_ATTR_INDEX, index);
174 NV_WR08(pCRTCReg, VGA_ATTR_DATA_W, value);
177 static CARD8 NVReadVgaAttr(xf86CrtcPtr crtc, CARD8 index)
179 ScrnInfoPtr pScrn = crtc->scrn;
180 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
181 NVPtr pNv = NVPTR(pScrn);
182 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
184 NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
185 if (nv_crtc->paletteEnabled)
189 NV_WR08(pCRTCReg, VGA_ATTR_INDEX, index);
190 return NV_RD08(pCRTCReg, VGA_ATTR_DATA_R);
193 void NVCrtcSetOwner(xf86CrtcPtr crtc)
195 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
196 ScrnInfoPtr pScrn = crtc->scrn;
197 NVPtr pNv = NVPTR(pScrn);
198 /* Non standard beheaviour required by NV11 */
200 uint8_t owner = NVReadVGA0(pNv, NV_VGA_CRTCX_OWNER);
201 ErrorF("pre-Owner: 0x%X\n", owner);
203 uint32_t pbus84 = nvReadMC(pNv, 0x1084);
204 ErrorF("pbus84: 0x%X\n", pbus84);
206 ErrorF("pbus84: 0x%X\n", pbus84);
207 nvWriteMC(pNv, 0x1084, pbus84);
209 /* The blob never writes owner to pcio1, so should we */
210 if (pNv->NVArch == 0x11) {
211 NVWriteVGA0(pNv, NV_VGA_CRTCX_OWNER, 0xff);
213 NVWriteVGA0(pNv, NV_VGA_CRTCX_OWNER, nv_crtc->crtc * 0x3);
214 owner = NVReadVGA0(pNv, NV_VGA_CRTCX_OWNER);
215 ErrorF("post-Owner: 0x%X\n", owner);
217 ErrorF("pNv pointer is NULL\n");
222 NVEnablePalette(xf86CrtcPtr crtc)
224 ScrnInfoPtr pScrn = crtc->scrn;
225 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
226 NVPtr pNv = NVPTR(pScrn);
227 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
229 NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
230 NV_WR08(pCRTCReg, VGA_ATTR_INDEX, 0);
231 nv_crtc->paletteEnabled = TRUE;
235 NVDisablePalette(xf86CrtcPtr crtc)
237 ScrnInfoPtr pScrn = crtc->scrn;
238 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
239 NVPtr pNv = NVPTR(pScrn);
240 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
242 NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
243 NV_WR08(pCRTCReg, VGA_ATTR_INDEX, 0x20);
244 nv_crtc->paletteEnabled = FALSE;
247 static void NVWriteVgaReg(xf86CrtcPtr crtc, CARD32 reg, CARD8 value)
249 ScrnInfoPtr pScrn = crtc->scrn;
250 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
251 NVPtr pNv = NVPTR(pScrn);
252 volatile CARD8 *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
254 NV_WR08(pCRTCReg, reg, value);
257 /* perform a sequencer reset */
258 static void NVVgaSeqReset(xf86CrtcPtr crtc, Bool start)
261 NVWriteVgaSeq(crtc, 0x00, 0x1);
263 NVWriteVgaSeq(crtc, 0x00, 0x3);
266 static void NVVgaProtect(xf86CrtcPtr crtc, Bool on)
271 tmp = NVReadVgaSeq(crtc, 0x1);
272 NVVgaSeqReset(crtc, TRUE);
273 NVWriteVgaSeq(crtc, 0x01, tmp | 0x20);
275 NVEnablePalette(crtc);
278 * Reenable sequencer, then turn on screen.
280 tmp = NVReadVgaSeq(crtc, 0x1);
281 NVWriteVgaSeq(crtc, 0x01, tmp & ~0x20); /* reenable display */
282 NVVgaSeqReset(crtc, FALSE);
284 NVDisablePalette(crtc);
288 void NVCrtcLockUnlock(xf86CrtcPtr crtc, Bool Lock)
292 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LOCK, Lock ? 0x99 : 0x57);
293 cr11 = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_VSYNCE);
294 if (Lock) cr11 |= 0x80;
296 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_VSYNCE, cr11);
300 NVGetOutputFromCRTC(xf86CrtcPtr crtc)
302 ScrnInfoPtr pScrn = crtc->scrn;
303 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
305 for (i = 0; i < xf86_config->num_output; i++) {
306 xf86OutputPtr output = xf86_config->output[i];
308 if (output->crtc == crtc) {
317 nv_find_crtc_by_index(ScrnInfoPtr pScrn, int index)
319 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
322 for (i = 0; i < xf86_config->num_crtc; i++) {
323 xf86CrtcPtr crtc = xf86_config->crtc[i];
324 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
325 if (nv_crtc->crtc == index)
333 * Calculate the Video Clock parameters for the PLL.
335 static void CalcVClock (
342 unsigned lowM, highM, highP;
343 unsigned DeltaNew, DeltaOld;
347 /* M: PLL reference frequency postscaler divider */
348 /* P: PLL VCO output postscaler divider */
349 /* N: PLL VCO postscaler setting */
351 DeltaOld = 0xFFFFFFFF;
353 VClk = (unsigned)clockIn;
355 /* Taken from Haiku, after someone with an NV28 had an issue */
356 switch(pNv->NVArch) {
362 } else if (VClk > 200000) {
364 } else if (VClk > 150000) {
375 } else if (VClk > 250000) {
383 for (P = 1; P <= highP; P++) {
385 if ((Freq >= 128000) && (Freq <= 350000)) {
386 for (M = lowM; M <= highM; M++) {
387 N = ((VClk << P) * M) / pNv->CrystalFreqKHz;
389 Freq = ((pNv->CrystalFreqKHz * N) / M) >> P;
391 DeltaNew = Freq - VClk;
393 DeltaNew = VClk - Freq;
395 if (DeltaNew < DeltaOld) {
396 *pllOut = (P << 16) | (N << 8) | M;
406 static void CalcVClock2Stage (
414 unsigned DeltaNew, DeltaOld;
417 unsigned lowM, highM, highP;
419 DeltaOld = 0xFFFFFFFF;
421 *pllBOut = 0x80000401; /* fixed at x4 for now */
423 VClk = (unsigned)clockIn;
425 /* Taken from Haiku, after someone with an NV28 had an issue */
426 switch(pNv->NVArch) {
432 } else if (VClk > 200000) {
434 } else if (VClk > 150000) {
445 } else if (VClk > 250000) {
453 for (P = 0; P <= highP; P++) {
455 if ((Freq >= 400000) && (Freq <= 1000000)) {
456 for (M = lowM; M <= highM; M++) {
457 N = ((VClk << P) * M) / (pNv->CrystalFreqKHz << 2);
458 if ((N >= 5) && (N <= 255)) {
459 Freq = (((pNv->CrystalFreqKHz << 2) * N) / M) >> P;
461 DeltaNew = Freq - VClk;
463 DeltaNew = VClk - Freq;
465 if (DeltaNew < DeltaOld) {
466 *pllOut = (P << 16) | (N << 8) | M;
476 /* Even though they are not yet used, i'm adding some notes about some of the 0x4000 regs */
477 /* They are only valid for NV4x, appearantly reordered for NV5x */
478 /* gpu pll: 0x4000 + 0x4004
479 * unknown pll: 0x4008 + 0x400c
480 * vpll1: 0x4010 + 0x4014
481 * vpll2: 0x4018 + 0x401c
482 * unknown pll: 0x4020 + 0x4024
483 * unknown pll: 0x4038 + 0x403c
484 * Some of the unknown's are probably memory pll's.
485 * The vpll's use two set's of multipliers and dividers. I refer to them as a and b.
486 * 1 and 2 refer to the registers of each pair. There is only one post divider.
487 * Logic: clock = reference_clock * ((n(a) * n(b))/(m(a) * m(b))) >> p
488 * 1) bit 0-7: familiar values, but redirected from were? (similar to PLL_SETUP_CONTROL)
489 * bit8: A switch that turns of the second divider and multiplier off.
490 * bit12: Also a switch, i haven't seen it yet.
491 * bit16-19: p-divider
492 * 2) bit0-7: m-divider (a)
493 * bit8-15: n-multiplier (a)
494 * bit16-23: m-divider (b)
495 * bit24-31: n-multiplier (b)
498 /* Modifying the gpu pll for example requires:
499 * - Disable value 0x333 (inverse AND mask) on the 0xc040 register.
500 * This is not needed for the vpll's which have their own bits.
506 uint32_t requested_clock,
507 uint32_t *given_clock,
513 uint32_t DeltaOld, DeltaNew;
515 /* We have 2 mulitpliers, 2 dividers and one post divider */
516 /* Note that p is only 4 bits */
517 uint8_t m1, m2, n1, n2, p;
518 uint8_t m1_best = 0, m2_best = 0, n1_best = 0, n2_best = 0, p_best = 0;
520 DeltaOld = 0xFFFFFFFF;
522 /* Only unset the needed stuff */
523 *pll_a &= ~((0xf << 16) | (1 << 8) | (1 << 12));
524 /* This only contains the m multipliers and n dividers */
527 if (pNv->misc_info.prefer_db1) {
530 /* Neutral settings */
535 /* Fixed at x4 for the moment */
543 /* Sticking to the limits of nv, maybe convert the other functions back as well? */
544 for (p = 0; p < 6; p++) {
545 freq = requested_clock << p;
546 /* We must restrict the frequency before the post divider somewhat */
547 if (freq > 400000 && freq < 1000000) {
548 /* We have 8 bits for each multiplier */
549 for (m1 = 1; m1 < 14; m1++) {
550 n1 = (freq * m1 * m2)/(pNv->CrystalFreqKHz * n2);
551 if (n1 > 5 && n1 < 255) {
552 freq = ((pNv->CrystalFreqKHz * n1 * n2)/(m1 * m2)) >> p;
553 if (freq > requested_clock) {
554 DeltaNew = freq - requested_clock;
556 DeltaNew = requested_clock - freq;
558 if (DeltaNew < DeltaOld) {
569 if (pNv->misc_info.prefer_db1) {
570 /* Bogus data, the same nvidia uses */
575 *pll_a |= (p_best << 16);
576 *pll_b |= ((n2_best << 24) | (m2_best << 16) | (n1_best << 8) | (m1_best << 0));
578 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);
581 static void nv40_crtc_save_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
583 state->vpll1_a = nvReadMC(pNv, NV40_VCLK1_A);
584 state->vpll1_b = nvReadMC(pNv, NV40_VCLK1_B);
585 state->vpll2_a = nvReadMC(pNv, NV40_VCLK2_A);
586 state->vpll2_b = nvReadMC(pNv, NV40_VCLK2_B);
587 state->pllsel = nvReadRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT);
588 state->sel_clk = nvReadRAMDAC0(pNv, NV_RAMDAC_SEL_CLK);
591 static void nv40_crtc_load_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
593 CARD32 fp_debug_0[2];
595 fp_debug_0[0] = nvReadRAMDAC(pNv, 0, NV_RAMDAC_FP_DEBUG_0);
596 fp_debug_0[1] = nvReadRAMDAC(pNv, 1, NV_RAMDAC_FP_DEBUG_0);
598 /* The TMDS_PLL switch is on the actual ramdac */
599 if (state->crosswired) {
602 ErrorF("Crosswired pll state load\n");
608 if (state->vpll2_b) {
609 nvWriteRAMDAC(pNv, index[1], NV_RAMDAC_FP_DEBUG_0,
610 fp_debug_0[index[1]] | NV_RAMDAC_FP_DEBUG_0_PWRDOWN_TMDS_PLL);
612 /* Wait for the situation to stabilise */
615 uint32_t reg_c040 = pNv->misc_info.reg_c040;
616 /* for vpll2 change bits 18 and 19 are disabled */
617 reg_c040 &= ~(0x3 << 18);
618 nvWriteMC(pNv, 0xc040, reg_c040);
620 ErrorF("writing vpll2_a %08X\n", state->vpll2_a);
621 ErrorF("writing vpll2_b %08X\n", state->vpll2_b);
623 nvWriteMC(pNv, NV40_VCLK2_A, state->vpll2_a);
624 nvWriteMC(pNv, NV40_VCLK2_B, state->vpll2_b);
626 /* We need to wait a while */
628 nvWriteMC(pNv, 0xc040, pNv->misc_info.reg_c040);
630 ErrorF("writing pllsel %08X\n", state->pllsel & ~NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL);
631 /* Let's keep the primary vpll off */
632 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel & ~NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL);
634 nvWriteRAMDAC(pNv, index[1], NV_RAMDAC_FP_DEBUG_0, fp_debug_0[index[1]]);
636 /* Wait for the situation to stabilise */
640 if (state->vpll1_b) {
641 nvWriteRAMDAC(pNv, index[0], NV_RAMDAC_FP_DEBUG_0,
642 fp_debug_0[index[0]] | NV_RAMDAC_FP_DEBUG_0_PWRDOWN_TMDS_PLL);
644 /* Wait for the situation to stabilise */
647 uint32_t reg_c040 = pNv->misc_info.reg_c040;
648 /* for vpll2 change bits 16 and 17 are disabled */
649 reg_c040 &= ~(0x3 << 16);
650 nvWriteMC(pNv, 0xc040, reg_c040);
652 ErrorF("writing vpll1_a %08X\n", state->vpll1_a);
653 ErrorF("writing vpll1_b %08X\n", state->vpll1_b);
655 nvWriteMC(pNv, NV40_VCLK1_A, state->vpll1_a);
656 nvWriteMC(pNv, NV40_VCLK1_B, state->vpll1_b);
658 /* We need to wait a while */
660 nvWriteMC(pNv, 0xc040, pNv->misc_info.reg_c040);
662 ErrorF("writing pllsel %08X\n", state->pllsel);
663 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel);
665 nvWriteRAMDAC(pNv, index[0], NV_RAMDAC_FP_DEBUG_0, fp_debug_0[index[0]]);
667 /* Wait for the situation to stabilise */
671 ErrorF("writing sel_clk %08X\n", state->sel_clk);
672 nvWriteRAMDAC0(pNv, NV_RAMDAC_SEL_CLK, state->sel_clk);
675 static void nv_crtc_save_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
677 state->vpll = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL);
679 state->vpll2 = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2);
681 if(pNv->twoStagePLL) {
682 state->vpllB = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL_B);
683 state->vpll2B = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2_B);
685 state->pllsel = nvReadRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT);
686 state->sel_clk = nvReadRAMDAC0(pNv, NV_RAMDAC_SEL_CLK);
690 static void nv_crtc_load_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
694 ErrorF("writing vpll2 %08X\n", state->vpll2);
695 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2, state->vpll2);
697 if(pNv->twoStagePLL) {
698 ErrorF("writing vpll2B %08X\n", state->vpll2B);
699 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2_B, state->vpll2B);
702 ErrorF("writing pllsel %08X\n", state->pllsel);
703 /* Let's keep the primary vpll off */
704 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel & ~NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL);
708 ErrorF("writing vpll %08X\n", state->vpll);
709 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL, state->vpll);
710 if(pNv->twoStagePLL) {
711 ErrorF("writing vpllB %08X\n", state->vpllB);
712 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL_B, state->vpllB);
715 ErrorF("writing pllsel %08X\n", state->pllsel);
716 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel);
719 ErrorF("writing sel_clk %08X\n", state->sel_clk);
720 nvWriteRAMDAC0(pNv, NV_RAMDAC_SEL_CLK, state->sel_clk);
724 * Calculate extended mode parameters (SVGA) and save in a
725 * mode state structure.
727 void nv_crtc_calc_state_ext(
730 int DisplayWidth, /* Does this change after setting the mode? */
737 ScrnInfoPtr pScrn = crtc->scrn;
738 uint32_t pixelDepth, VClk = 0;
740 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
741 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
743 NVPtr pNv = NVPTR(pScrn);
744 RIVA_HW_STATE *state;
745 int num_crtc_enabled, i;
747 state = &pNv->ModeReg;
749 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
751 xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
752 NVOutputPrivatePtr nv_output = output->driver_private;
755 * Extended RIVA registers.
757 pixelDepth = (bpp + 1)/8;
758 if (pNv->Architecture == NV_ARCH_40) {
759 if (nv_crtc->head == 1) {
760 /* Read our clocks if haven't got any yet */
761 if (!state->vpll2_b) {
762 state->vpll2_a = nvReadMC(pNv, NV40_VCLK2_A);
763 state->vpll2_b = nvReadMC(pNv, NV40_VCLK2_B);
765 CalculateVClkNV4x(pNv, dotClock, &VClk, &state->vpll2_a, &state->vpll2_b, &state->db1_ratio[1]);
767 /* Read our clocks if haven't got any yet */
768 if (!state->vpll1_b) {
769 state->vpll1_a = nvReadMC(pNv, NV40_VCLK1_A);
770 state->vpll1_b = nvReadMC(pNv, NV40_VCLK1_B);
772 CalculateVClkNV4x(pNv, dotClock, &VClk, &state->vpll1_a, &state->vpll1_b, &state->db1_ratio[0]);
774 } else if (pNv->twoStagePLL) {
775 CalcVClock2Stage(dotClock, &VClk, &state->pll, &state->pllB, pNv);
777 CalcVClock(dotClock, &VClk, &state->pll, pNv);
780 switch (pNv->Architecture) {
782 nv4UpdateArbitrationSettings(VClk,
784 &(state->arbitration0),
785 &(state->arbitration1),
787 regp->CRTC[NV_VGA_CRTCX_CURCTL0] = 0x00;
788 regp->CRTC[NV_VGA_CRTCX_CURCTL1] = 0xbC;
789 if (flags & V_DBLSCAN)
790 regp->CRTC[NV_VGA_CRTCX_CURCTL1] |= 2;
791 regp->CRTC[NV_VGA_CRTCX_CURCTL2] = 0x00000000;
792 state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2 | NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL;
793 state->config = 0x00001114;
794 regp->CRTC[NV_VGA_CRTCX_REPAINT1] = CrtcHDisplay < 1280 ? 0x04 : 0x00;
800 if (((pNv->Chipset & 0xfff0) == CHIPSET_C51) ||
801 ((pNv->Chipset & 0xfff0) == CHIPSET_C512)) {
802 state->arbitration0 = 128;
803 state->arbitration1 = 0x0480;
804 } else if (((pNv->Chipset & 0xffff) == CHIPSET_NFORCE) ||
805 ((pNv->Chipset & 0xffff) == CHIPSET_NFORCE2)) {
806 nForceUpdateArbitrationSettings(VClk,
808 &(state->arbitration0),
809 &(state->arbitration1),
811 } else if (pNv->Architecture < NV_ARCH_30) {
812 nv10UpdateArbitrationSettings(VClk,
814 &(state->arbitration0),
815 &(state->arbitration1),
818 nv30UpdateArbitrationSettings(pNv,
819 &(state->arbitration0),
820 &(state->arbitration1));
823 CursorStart = pNv->Cursor->offset;
825 regp->CRTC[NV_VGA_CRTCX_CURCTL0] = 0x80 | (CursorStart >> 17);
826 regp->CRTC[NV_VGA_CRTCX_CURCTL1] = (CursorStart >> 11) << 2;
827 regp->CRTC[NV_VGA_CRTCX_CURCTL2] = CursorStart >> 24;
829 if (flags & V_DBLSCAN)
830 regp->CRTC[NV_VGA_CRTCX_CURCTL1] |= 2;
832 state->config = nvReadFB(pNv, NV_PFB_CFG0);
833 regp->CRTC[NV_VGA_CRTCX_REPAINT1] = CrtcHDisplay < 1280 ? 0x04 : 0x00;
837 /* okay do we have 2 CRTCs running ? */
838 num_crtc_enabled = 0;
839 for (i = 0; i < xf86_config->num_crtc; i++) {
840 if (xf86_config->crtc[i]->enabled) {
845 ErrorF("There are %d CRTC's enabled\n", num_crtc_enabled);
847 if (pNv->Architecture < NV_ARCH_40) {
848 /* We need this before the next code */
849 if (nv_crtc->crtc == 1) {
850 state->vpll2 = state->pll;
851 state->vpll2B = state->pllB;
853 state->vpll = state->pll;
854 state->vpllB = state->pllB;
858 if (pNv->Architecture == NV_ARCH_40) {
859 /* This register is only used on the primary ramdac */
860 /* This seems to be needed to select the proper clocks, otherwise bad things happen */
861 /* Assumption CRTC1 will overwrite the CRTC0 value */
862 /* Also make sure we don't set both bits */
863 state->sel_clk = (pNv->misc_info.sel_clk & ~(0xf << 16)) | (1 << 18);
864 /* Are we a TMDS running on head 0(=ramdac 0), but native to ramdac 1? */
865 if (nv_crtc->head == 0 && nv_output->type == OUTPUT_TMDS && nv_output->valid_ramdac & RAMDAC_1) {
866 state->sel_clk = (pNv->misc_info.sel_clk & ~(0xf << 16)) | (1 << 16);
867 state->crosswired = TRUE;
868 } else if (nv_crtc->head == 0) {
869 state->crosswired = FALSE;
872 /* Some cards want this register zero, so let's hope we can catch them all */
873 if (pNv->sel_clk_override) {
874 state->sel_clk = pNv->misc_info.sel_clk;
879 /* Here even bigger guess work starts */
880 if ((n_div/m_div) > 5 * p_div) {
881 state->vpll2 |= (1 << 30);
882 } else if (p_div >= m_div) {
883 state->vpll2 |= (1 << 30);
884 state->vpll2 |= (1 << 31);
889 if (nv_crtc->head == 1) {
890 if (state->db1_ratio[1])
891 ErrorF("We are a lover of the DB1 VCLK ratio\n");
892 } else if (nv_crtc->head == 0) {
893 if (state->db1_ratio[0])
894 ErrorF("We are a lover of the DB1 VCLK ratio\n");
898 /* We've bound crtc's and ramdac's together */
899 if (nv_crtc->head == 1) {
900 if (!state->db1_ratio[1]) {
901 state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK2_RATIO_DB2;
903 state->pllsel &= ~NV_RAMDAC_PLL_SELECT_VCLK2_RATIO_DB2;
905 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL2;
907 if (pNv->Architecture < NV_ARCH_40)
908 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL;
910 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL;
911 if (!state->db1_ratio[0]) {
912 state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2;
914 state->pllsel &= ~NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2;
918 /* The purpose is unknown */
919 if (pNv->Architecture == NV_ARCH_40)
920 state->pllsel |= (1 << 2);
922 regp->CRTC[NV_VGA_CRTCX_FIFO0] = state->arbitration0;
923 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM] = state->arbitration1 & 0xff;
924 if (pNv->Architecture >= NV_ARCH_30) {
925 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30] = state->arbitration1 >> 8;
928 regp->CRTC[NV_VGA_CRTCX_REPAINT0] = (((DisplayWidth/8) * pixelDepth) & 0x700) >> 3;
929 regp->CRTC[NV_VGA_CRTCX_PIXEL] = (pixelDepth > 2) ? 3 : pixelDepth;
933 nv_crtc_dpms(xf86CrtcPtr crtc, int mode)
935 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
936 ScrnInfoPtr pScrn = crtc->scrn;
937 NVPtr pNv = NVPTR(pScrn);
938 unsigned char seq1 = 0, crtc17 = 0;
939 unsigned char crtc1A;
941 ErrorF("nv_crtc_dpms is called for CRTC %d with mode %d\n", nv_crtc->crtc, mode);
943 NVCrtcSetOwner(crtc);
945 crtc1A = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1) & ~0xC0;
947 case DPMSModeStandby:
948 /* Screen: Off; HSync: Off, VSync: On -- Not Supported */
953 case DPMSModeSuspend:
954 /* Screen: Off; HSync: On, VSync: Off -- Not Supported */
960 /* Screen: Off; HSync: Off, VSync: Off */
967 /* Screen: On; HSync: On, VSync: On */
973 NVVgaSeqReset(crtc, TRUE);
974 /* Each head has it's own sequencer, so we can turn it off when we want */
975 seq1 |= (NVReadVgaSeq(crtc, 0x01) & ~0x20);
976 NVWriteVgaSeq(crtc, 0x1, seq1);
977 crtc17 |= (NVReadVgaCrtc(crtc, NV_VGA_CRTCX_MODECTL) & ~0x80);
979 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_MODECTL, crtc17);
980 NVVgaSeqReset(crtc, FALSE);
982 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1, crtc1A);
984 /* I hope this is the right place */
985 if (crtc->enabled && mode == DPMSModeOn) {
986 pNv->crtc_active[nv_crtc->head] = TRUE;
988 pNv->crtc_active[nv_crtc->head] = FALSE;
993 nv_crtc_mode_fixup(xf86CrtcPtr crtc, DisplayModePtr mode,
994 DisplayModePtr adjusted_mode)
996 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
997 ErrorF("nv_crtc_mode_fixup is called for CRTC %d\n", nv_crtc->crtc);
1003 nv_crtc_mode_set_vga(xf86CrtcPtr crtc, DisplayModePtr mode)
1005 ScrnInfoPtr pScrn = crtc->scrn;
1006 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1008 NVPtr pNv = NVPTR(pScrn);
1009 int depth = pScrn->depth;
1012 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1015 * compute correct Hsync & Vsync polarity
1017 if ((mode->Flags & (V_PHSYNC | V_NHSYNC))
1018 && (mode->Flags & (V_PVSYNC | V_NVSYNC))) {
1020 regp->MiscOutReg = 0x23;
1021 if (mode->Flags & V_NHSYNC) regp->MiscOutReg |= 0x40;
1022 if (mode->Flags & V_NVSYNC) regp->MiscOutReg |= 0x80;
1024 int VDisplay = mode->VDisplay;
1025 if (mode->Flags & V_DBLSCAN)
1027 if (mode->VScan > 1)
1028 VDisplay *= mode->VScan;
1029 if (VDisplay < 400) {
1030 regp->MiscOutReg = 0xA3; /* +hsync -vsync */
1031 } else if (VDisplay < 480) {
1032 regp->MiscOutReg = 0x63; /* -hsync +vsync */
1033 } else if (VDisplay < 768) {
1034 regp->MiscOutReg = 0xE3; /* -hsync -vsync */
1036 regp->MiscOutReg = 0x23; /* +hsync +vsync */
1040 regp->MiscOutReg |= (mode->ClockIndex & 0x03) << 2;
1046 regp->Sequencer[0] = 0x02;
1048 regp->Sequencer[0] = 0x00;
1050 /* 0x20 disables the sequencer */
1051 if (mode->Flags & V_CLKDIV2) {
1052 regp->Sequencer[1] = 0x29;
1054 regp->Sequencer[1] = 0x21;
1057 regp->Sequencer[2] = 1 << BIT_PLANE;
1059 regp->Sequencer[2] = 0x0F;
1060 regp->Sequencer[3] = 0x00; /* Font select */
1063 regp->Sequencer[4] = 0x06; /* Misc */
1065 regp->Sequencer[4] = 0x0E; /* Misc */
1071 regp->CRTC[0] = (mode->CrtcHTotal >> 3) - 5;
1072 regp->CRTC[1] = (mode->CrtcHDisplay >> 3) - 1;
1073 regp->CRTC[2] = (mode->CrtcHBlankStart >> 3) - 1;
1074 regp->CRTC[3] = (((mode->CrtcHBlankEnd >> 3) - 1) & 0x1F) | 0x80;
1075 i = (((mode->CrtcHSkew << 2) + 0x10) & ~0x1F);
1079 regp->CRTC[4] = (mode->CrtcHSyncStart >> 3);
1080 regp->CRTC[5] = ((((mode->CrtcHBlankEnd >> 3) - 1) & 0x20) << 2)
1081 | (((mode->CrtcHSyncEnd >> 3)) & 0x1F);
1082 regp->CRTC[6] = (mode->CrtcVTotal - 2) & 0xFF;
1083 regp->CRTC[7] = (((mode->CrtcVTotal - 2) & 0x100) >> 8)
1084 | (((mode->CrtcVDisplay - 1) & 0x100) >> 7)
1085 | ((mode->CrtcVSyncStart & 0x100) >> 6)
1086 | (((mode->CrtcVBlankStart - 1) & 0x100) >> 5)
1088 | (((mode->CrtcVTotal - 2) & 0x200) >> 4)
1089 | (((mode->CrtcVDisplay - 1) & 0x200) >> 3)
1090 | ((mode->CrtcVSyncStart & 0x200) >> 2);
1091 regp->CRTC[8] = 0x00;
1092 regp->CRTC[9] = (((mode->CrtcVBlankStart - 1) & 0x200) >> 4) | 0x40;
1093 if (mode->Flags & V_DBLSCAN) {
1094 regp->CRTC[9] |= 0x80;
1096 if (mode->VScan >= 32) {
1097 regp->CRTC[9] |= 0x1F;
1098 } else if (mode->VScan > 1) {
1099 regp->CRTC[9] |= mode->VScan - 1;
1101 regp->CRTC[10] = 0x00;
1102 regp->CRTC[11] = 0x00;
1103 regp->CRTC[12] = 0x00;
1104 regp->CRTC[13] = 0x00;
1105 regp->CRTC[14] = 0x00;
1106 regp->CRTC[15] = 0x00;
1107 regp->CRTC[16] = mode->CrtcVSyncStart & 0xFF;
1108 regp->CRTC[17] = (mode->CrtcVSyncEnd & 0x0F) | 0x20;
1109 regp->CRTC[18] = (mode->CrtcVDisplay - 1) & 0xFF;
1110 regp->CRTC[19] = mode->CrtcHDisplay >> 4; /* just a guess */
1111 regp->CRTC[20] = 0x00;
1112 regp->CRTC[21] = (mode->CrtcVBlankStart - 1) & 0xFF;
1113 regp->CRTC[22] = (mode->CrtcVBlankEnd - 1) & 0xFF;
1114 /* 0x80 enables the sequencer, we don't want that */
1116 regp->CRTC[23] = 0xE3 & ~0x80;
1118 regp->CRTC[23] = 0xC3 & ~0x80;
1120 regp->CRTC[24] = 0xFF;
1123 * Theory resumes here....
1127 * Graphics Display Controller
1129 regp->Graphics[0] = 0x00;
1130 regp->Graphics[1] = 0x00;
1131 regp->Graphics[2] = 0x00;
1132 regp->Graphics[3] = 0x00;
1134 regp->Graphics[4] = BIT_PLANE;
1135 regp->Graphics[5] = 0x00;
1137 regp->Graphics[4] = 0x00;
1139 regp->Graphics[5] = 0x02;
1141 regp->Graphics[5] = 0x40;
1144 regp->Graphics[6] = 0x05; /* only map 64k VGA memory !!!! */
1145 regp->Graphics[7] = 0x0F;
1146 regp->Graphics[8] = 0xFF;
1149 /* Initialise the Mono map according to which bit-plane gets used */
1151 Bool flipPixels = xf86GetFlipPixels();
1153 for (i=0; i<16; i++) {
1154 if (((i & (1 << BIT_PLANE)) != 0) != flipPixels) {
1155 regp->Attribute[i] = WHITE_VALUE;
1157 regp->Attribute[i] = BLACK_VALUE;
1162 regp->Attribute[0] = 0x00; /* standard colormap translation */
1163 regp->Attribute[1] = 0x01;
1164 regp->Attribute[2] = 0x02;
1165 regp->Attribute[3] = 0x03;
1166 regp->Attribute[4] = 0x04;
1167 regp->Attribute[5] = 0x05;
1168 regp->Attribute[6] = 0x06;
1169 regp->Attribute[7] = 0x07;
1170 regp->Attribute[8] = 0x08;
1171 regp->Attribute[9] = 0x09;
1172 regp->Attribute[10] = 0x0A;
1173 regp->Attribute[11] = 0x0B;
1174 regp->Attribute[12] = 0x0C;
1175 regp->Attribute[13] = 0x0D;
1176 regp->Attribute[14] = 0x0E;
1177 regp->Attribute[15] = 0x0F;
1179 regp->Attribute[16] = 0x81; /* wrong for the ET4000 */
1181 regp->Attribute[16] = 0x41; /* wrong for the ET4000 */
1184 regp->Attribute[17] = 0xff;
1186 /* Attribute[17] (overscan) initialised in vgaHWGetHWRec() */
1188 regp->Attribute[18] = 0x0F;
1189 regp->Attribute[19] = 0x00;
1190 regp->Attribute[20] = 0x00;
1193 #define MAX_H_VALUE(i) ((0x1ff + i) << 3)
1194 #define MAX_V_VALUE(i) ((0xfff + i) << 0)
1197 * Sets up registers for the given mode/adjusted_mode pair.
1199 * The clocks, CRTCs and outputs attached to this CRTC must be off.
1201 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
1202 * be easily turned on/off after this.
1205 nv_crtc_mode_set_regs(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
1207 ScrnInfoPtr pScrn = crtc->scrn;
1208 NVPtr pNv = NVPTR(pScrn);
1209 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1210 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1211 NVFBLayout *pLayout = &pNv->CurrentLayout;
1212 NVCrtcRegPtr regp, savep;
1216 /* Happily borrowed from haiku driver, as an extra safety */
1218 /* Make it multiples of 8 */
1219 mode->CrtcHDisplay &= ~7;
1220 mode->CrtcHSyncStart &= ~7;
1221 mode->CrtcHSyncEnd &= ~7;
1222 mode->CrtcHTotal &= ~7;
1224 /* Horizontal stuff */
1226 /* Time for some mode mangling */
1227 /* We only have 9 bits to store most of this information (mask 0x3f) */
1228 if (mode->CrtcHDisplay > MAX_H_VALUE(-2))
1229 mode->CrtcHDisplay = MAX_H_VALUE(-2);
1231 if (mode->CrtcHSyncStart > MAX_H_VALUE(-1))
1232 mode->CrtcHSyncStart = MAX_H_VALUE(-1);
1234 if (mode->CrtcHSyncEnd > MAX_H_VALUE(0))
1235 mode->CrtcHSyncEnd = MAX_H_VALUE(0);
1237 if (mode->CrtcHTotal > MAX_H_VALUE(5))
1238 mode->CrtcHTotal = MAX_H_VALUE(5);
1240 /* Make room for a sync pulse if there is not enough room */
1241 if (mode->CrtcHTotal < mode->CrtcHSyncEnd + 0x50)
1242 mode->CrtcHTotal = mode->CrtcHSyncEnd + 0x50;
1244 /* Too large sync pulse? */
1245 if (mode->CrtcHTotal > mode->CrtcHSyncEnd + 0x3f8)
1246 mode->CrtcHTotal = mode->CrtcHSyncEnd + 0x3f8;
1248 /* Is the sync pulse outside the screen? */
1249 if (mode->CrtcHSyncEnd > mode->CrtcHTotal - 8)
1250 mode->CrtcHSyncEnd = mode->CrtcHTotal - 8;
1252 if (mode->CrtcHSyncStart < mode->CrtcHDisplay + 8)
1253 mode->CrtcHSyncStart = mode->CrtcHDisplay + 8;
1255 /* We've only got 5 bits to store the sync stuff */
1256 if (mode->CrtcHSyncEnd > mode->CrtcHSyncStart + (0x1f << 3))
1257 mode->CrtcHSyncEnd = mode->CrtcHSyncStart + (0x1f << 3);
1259 /* Vertical stuff */
1261 /* We've only got 12 bits for this stuff */
1262 if (mode->CrtcVDisplay > MAX_V_VALUE(-2))
1263 mode->CrtcVDisplay = MAX_V_VALUE(-2);
1265 if (mode->CrtcVSyncStart > MAX_V_VALUE(-1))
1266 mode->CrtcVSyncStart = MAX_V_VALUE(-1);
1268 if (mode->CrtcVSyncEnd > MAX_V_VALUE(0))
1269 mode->CrtcVSyncEnd = MAX_V_VALUE(0);
1271 if (mode->CrtcVTotal > MAX_V_VALUE(5))
1272 mode->CrtcVTotal = MAX_V_VALUE(5);
1274 /* Make room for a sync pulse if there is not enough room */
1275 if (mode->CrtcVTotal < mode->CrtcVSyncEnd + 0x3)
1276 mode->CrtcVTotal = mode->CrtcVSyncEnd + 0x3;
1278 /* Too large sync pulse? */
1279 if (mode->CrtcVTotal > mode->CrtcVSyncEnd + 0xff)
1280 mode->CrtcVTotal = mode->CrtcVSyncEnd + 0xff;
1282 /* Is the sync pulse outside the screen? */
1283 if (mode->CrtcVSyncEnd > mode->CrtcVTotal - 1)
1284 mode->CrtcVSyncEnd = mode->CrtcVTotal - 1;
1286 if (mode->CrtcVSyncStart < mode->CrtcVDisplay + 1)
1287 mode->CrtcVSyncStart = mode->CrtcVDisplay + 1;
1289 /* We've only got 4 bits to store the sync stuff */
1290 if (mode->CrtcVSyncEnd > mode->CrtcVSyncStart + (0x0f << 0))
1291 mode->CrtcVSyncEnd = mode->CrtcVSyncStart + (0x0f << 0);
1294 int horizDisplay = (mode->CrtcHDisplay >> 3) - 1;
1295 int horizStart = (mode->CrtcHSyncStart >> 3) - 1;
1296 int horizEnd = (mode->CrtcHSyncEnd >> 3) - 1;
1297 int horizTotal = (mode->CrtcHTotal >> 3) - 5;
1298 int horizBlankStart = (mode->CrtcHDisplay >> 3) - 1;
1299 int horizBlankEnd = (mode->CrtcHTotal >> 3) - 1;
1300 int vertDisplay = mode->CrtcVDisplay - 1;
1301 int vertStart = mode->CrtcVSyncStart - 1;
1302 int vertEnd = mode->CrtcVSyncEnd - 1;
1303 int vertTotal = mode->CrtcVTotal - 2;
1304 int vertBlankStart = mode->CrtcVDisplay - 1;
1305 int vertBlankEnd = mode->CrtcVTotal - 1;
1309 xf86OutputPtr output;
1310 NVOutputPrivatePtr nv_output;
1311 for (i = 0; i < xf86_config->num_output; i++) {
1312 output = xf86_config->output[i];
1313 nv_output = output->driver_private;
1315 if (output->crtc == crtc) {
1316 if ((nv_output->type == OUTPUT_LVDS) ||
1317 (nv_output->type == OUTPUT_TMDS)) {
1325 ErrorF("Mode clock: %d\n", mode->Clock);
1326 ErrorF("Adjusted mode clock: %d\n", adjusted_mode->Clock);
1328 ErrorF("crtc: Pre-sync workaround\n");
1329 /* Reverted to what nv did, because that works for all resolutions on flatpanels */
1331 vertStart = vertTotal - 3;
1332 vertEnd = vertTotal - 2;
1333 vertBlankStart = vertStart;
1334 horizStart = horizTotal - 5;
1335 horizEnd = horizTotal - 2;
1336 horizBlankEnd = horizTotal + 4;
1337 if (pNv->overlayAdaptor) {
1338 /* This reportedly works around Xv some overlay bandwidth problems*/
1342 ErrorF("crtc: Post-sync workaround\n");
1344 ErrorF("horizDisplay: 0x%X \n", horizDisplay);
1345 ErrorF("horizStart: 0x%X \n", horizStart);
1346 ErrorF("horizEnd: 0x%X \n", horizEnd);
1347 ErrorF("horizTotal: 0x%X \n", horizTotal);
1348 ErrorF("horizBlankStart: 0x%X \n", horizBlankStart);
1349 ErrorF("horizBlankEnd: 0x%X \n", horizBlankEnd);
1350 ErrorF("vertDisplay: 0x%X \n", vertDisplay);
1351 ErrorF("vertStart: 0x%X \n", vertStart);
1352 ErrorF("vertEnd: 0x%X \n", vertEnd);
1353 ErrorF("vertTotal: 0x%X \n", vertTotal);
1354 ErrorF("vertBlankStart: 0x%X \n", vertBlankStart);
1355 ErrorF("vertBlankEnd: 0x%X \n", vertBlankEnd);
1357 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1358 savep = &pNv->SavedReg.crtc_reg[nv_crtc->head];
1360 if(mode->Flags & V_INTERLACE)
1363 regp->CRTC[NV_VGA_CRTCX_HTOTAL] = Set8Bits(horizTotal);
1364 regp->CRTC[NV_VGA_CRTCX_HDISPE] = Set8Bits(horizDisplay);
1365 regp->CRTC[NV_VGA_CRTCX_HBLANKS] = Set8Bits(horizBlankStart);
1366 regp->CRTC[NV_VGA_CRTCX_HBLANKE] = SetBitField(horizBlankEnd,4:0,4:0)
1368 regp->CRTC[NV_VGA_CRTCX_HSYNCS] = Set8Bits(horizStart);
1369 regp->CRTC[NV_VGA_CRTCX_HSYNCE] = SetBitField(horizBlankEnd,5:5,7:7)
1370 | SetBitField(horizEnd,4:0,4:0);
1371 regp->CRTC[NV_VGA_CRTCX_VTOTAL] = SetBitField(vertTotal,7:0,7:0);
1372 regp->CRTC[NV_VGA_CRTCX_OVERFLOW] = SetBitField(vertTotal,8:8,0:0)
1373 | SetBitField(vertDisplay,8:8,1:1)
1374 | SetBitField(vertStart,8:8,2:2)
1375 | SetBitField(vertBlankStart,8:8,3:3)
1377 | SetBitField(vertTotal,9:9,5:5)
1378 | SetBitField(vertDisplay,9:9,6:6)
1379 | SetBitField(vertStart,9:9,7:7);
1380 regp->CRTC[NV_VGA_CRTCX_MAXSCLIN] = SetBitField(vertBlankStart,9:9,5:5)
1382 | ((mode->Flags & V_DBLSCAN) ? 0x80 : 0x00);
1383 regp->CRTC[NV_VGA_CRTCX_VSYNCS] = Set8Bits(vertStart);
1384 regp->CRTC[NV_VGA_CRTCX_VSYNCE] = SetBitField(vertEnd,3:0,3:0) | SetBit(5);
1385 regp->CRTC[NV_VGA_CRTCX_VDISPE] = Set8Bits(vertDisplay);
1386 regp->CRTC[NV_VGA_CRTCX_PITCHL] = ((pScrn->displayWidth/8)*(pLayout->bitsPerPixel/8));
1387 regp->CRTC[NV_VGA_CRTCX_VBLANKS] = Set8Bits(vertBlankStart);
1388 regp->CRTC[NV_VGA_CRTCX_VBLANKE] = Set8Bits(vertBlankEnd);
1389 /* Not an extended register */
1390 regp->CRTC[NV_VGA_CRTCX_LINECOMP] = 0xff;
1392 regp->Attribute[0x10] = 0x01;
1393 /* Blob sets this for normal monitors as well */
1394 regp->Attribute[0x11] = 0x00;
1396 regp->CRTC[NV_VGA_CRTCX_LSR] = SetBitField(horizBlankEnd,6:6,4:4)
1397 | SetBitField(vertBlankStart,10:10,3:3)
1398 | SetBitField(vertStart,10:10,2:2)
1399 | SetBitField(vertDisplay,10:10,1:1)
1400 | SetBitField(vertTotal,10:10,0:0);
1402 regp->CRTC[NV_VGA_CRTCX_HEB] = SetBitField(horizTotal,8:8,0:0)
1403 | SetBitField(horizDisplay,8:8,1:1)
1404 | SetBitField(horizBlankStart,8:8,2:2)
1405 | SetBitField(horizStart,8:8,3:3);
1407 regp->CRTC[NV_VGA_CRTCX_EXTRA] = SetBitField(vertTotal,11:11,0:0)
1408 | SetBitField(vertDisplay,11:11,2:2)
1409 | SetBitField(vertStart,11:11,4:4)
1410 | SetBitField(vertBlankStart,11:11,6:6);
1412 if(mode->Flags & V_INTERLACE) {
1413 horizTotal = (horizTotal >> 1) & ~1;
1414 regp->CRTC[NV_VGA_CRTCX_INTERLACE] = Set8Bits(horizTotal);
1415 regp->CRTC[NV_VGA_CRTCX_HEB] |= SetBitField(horizTotal,8:8,4:4);
1417 regp->CRTC[NV_VGA_CRTCX_INTERLACE] = 0xff; /* interlace off */
1420 /* bit2 = 0 -> fine pitched crtc granularity */
1421 /* The rest disables double buffering on CRTC access */
1422 regp->CRTC[NV_VGA_CRTCX_BUFFER] = 0xfa;
1424 if (savep->CRTC[NV_VGA_CRTCX_LCD] <= 0xb) {
1425 /* Common values are 0x0, 0x3, 0x8, 0xb, see logic below */
1426 if (nv_crtc->head == 0) {
1427 regp->CRTC[NV_VGA_CRTCX_LCD] = (1 << 3);
1431 regp->CRTC[NV_VGA_CRTCX_LCD] |= (1 << 0) | (1 << 1);
1434 /* Let's keep any abnormal value there may be, like 0x54 or 0x79 */
1435 regp->CRTC[NV_VGA_CRTCX_LCD] = savep->CRTC[NV_VGA_CRTCX_LCD];
1438 /* I'm trusting haiku driver on this one, they say it enables an external TDMS clock */
1440 regp->CRTC[NV_VGA_CRTCX_59] = 0x1;
1442 regp->CRTC[NV_VGA_CRTCX_59] = 0x0;
1446 * Initialize DAC palette.
1448 if(pLayout->bitsPerPixel != 8 ) {
1449 for (i = 0; i < 256; i++) {
1451 regp->DAC[(i*3)+1] = i;
1452 regp->DAC[(i*3)+2] = i;
1457 * Calculate the extended registers.
1460 if(pLayout->depth < 24) {
1466 if(pNv->Architecture >= NV_ARCH_10) {
1467 pNv->CURSOR = (CARD32 *)pNv->Cursor->map;
1470 ErrorF("crtc %d %d %d\n", nv_crtc->crtc, mode->CrtcHDisplay, pScrn->displayWidth);
1471 nv_crtc_calc_state_ext(crtc,
1473 pScrn->displayWidth,
1476 adjusted_mode->Clock,
1479 /* Enable slaved mode */
1481 regp->CRTC[NV_VGA_CRTCX_PIXEL] |= (1 << 7);
1484 /* What is the meaning of this register? */
1485 /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */
1486 regp->CRTC[NV_VGA_CRTCX_FIFO1] = savep->CRTC[NV_VGA_CRTCX_FIFO1] & ~(1<<5);
1488 /* NV40's don't set FPP units, unless in special conditions (then they set both) */
1489 /* But what are those special conditions? */
1490 if (pNv->Architecture <= NV_ARCH_30) {
1492 if(nv_crtc->head == 1) {
1493 regp->head |= NV_CRTC_FSEL_FPP1;
1494 } else if (pNv->twoHeads) {
1495 regp->head |= NV_CRTC_FSEL_FPP2;
1499 /* This is observed on some g70 cards, non-flatpanel's too */
1500 if (nv_crtc->head == 1) {
1501 regp->head |= NV_CRTC_FSEL_FPP2;
1505 /* Except for rare conditions I2C is enabled on the primary crtc */
1506 if (nv_crtc->head == 0) {
1507 if (pNv->overlayAdaptor) {
1508 regp->head |= NV_CRTC_FSEL_OVERLAY;
1510 regp->head |= NV_CRTC_FSEL_I2C;
1513 regp->cursorConfig = 0x00000100;
1514 if(mode->Flags & V_DBLSCAN)
1515 regp->cursorConfig |= (1 << 4);
1516 if(pNv->alphaCursor) {
1517 if((pNv->Chipset & 0x0ff0) != CHIPSET_NV11) {
1518 regp->cursorConfig |= 0x04011000;
1520 regp->cursorConfig |= 0x14011000;
1523 regp->cursorConfig |= 0x02000000;
1526 /* Unblock some timings */
1527 regp->CRTC[NV_VGA_CRTCX_FP_HTIMING] = 0;
1528 regp->CRTC[NV_VGA_CRTCX_FP_VTIMING] = 0;
1530 /* 0x20 seems to be enabled and 0x14 disabled */
1531 regp->CRTC[NV_VGA_CRTCX_26] = 0x20;
1533 /* 0x00 is disabled, 0x22 crt and 0x88 dfp */
1536 regp->CRTC[NV_VGA_CRTCX_3B] = 0x88;
1538 regp->CRTC[NV_VGA_CRTCX_3B] = 0x22;
1541 /* These values seem to vary */
1542 regp->CRTC[NV_VGA_CRTCX_3C] = savep->CRTC[NV_VGA_CRTCX_3C];
1544 /* 0x80 seems to be used very often, if not always */
1545 regp->CRTC[NV_VGA_CRTCX_45] = 0x80;
1547 /* Are these(0x55 and 0x56) also timing related registers, since disabling them does nothing? */
1548 regp->CRTC[NV_VGA_CRTCX_55] = 0x0;
1550 /* Common values like 0x14 and 0x04 are converted to 0x10 and 0x00 */
1551 //regp->CRTC[NV_VGA_CRTCX_56] = savep->CRTC[NV_VGA_CRTCX_56] & ~(1<<4);
1552 regp->CRTC[NV_VGA_CRTCX_56] = 0x0;
1554 regp->CRTC[NV_VGA_CRTCX_57] = 0x0;
1556 /* bit0: Seems to be mostly used on crtc1 */
1557 /* bit1: 1=crtc1, 0=crtc, but i'm unsure about this */
1558 /* 0x7E (crtc0, only seen in one dump) and 0x7F (crtc1) seem to be some kind of disable setting */
1559 /* This is likely to be incomplete */
1560 /* This is a very strange register, changed very often by the blob */
1561 regp->CRTC[NV_VGA_CRTCX_58] = 0x0;
1563 /* The blob seems to take the current value from crtc 0, add 4 to that and reuse the old value for crtc 1*/
1564 if (nv_crtc->head == 1) {
1565 regp->CRTC[NV_VGA_CRTCX_52] = pNv->misc_info.crtc_0_reg_52;
1567 regp->CRTC[NV_VGA_CRTCX_52] = pNv->misc_info.crtc_0_reg_52 + 4;
1570 /* The exact purpose of this register is unknown, but we copy value from crtc0 */
1571 regp->unk81c = nvReadCRTC0(pNv, NV_CRTC_081C);
1573 regp->unk830 = mode->CrtcVDisplay - 3;
1574 regp->unk834 = mode->CrtcVDisplay - 1;
1576 /* This is what the blob does */
1577 regp->unk850 = nvReadCRTC(pNv, 0, NV_CRTC_0850);
1579 /* Never ever modify gpio, unless you know very well what you're doing */
1580 regp->gpio = nvReadCRTC(pNv, 0, NV_CRTC_GPIO);
1584 * Sets up registers for the given mode/adjusted_mode pair.
1586 * The clocks, CRTCs and outputs attached to this CRTC must be off.
1588 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
1589 * be easily turned on/off after this.
1592 nv_crtc_mode_set(xf86CrtcPtr crtc, DisplayModePtr mode,
1593 DisplayModePtr adjusted_mode,
1596 ScrnInfoPtr pScrn = crtc->scrn;
1597 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1598 NVPtr pNv = NVPTR(pScrn);
1600 ErrorF("nv_crtc_mode_set is called for CRTC %d\n", nv_crtc->crtc);
1602 xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Mode on CRTC %d\n", nv_crtc->crtc);
1603 xf86PrintModeline(pScrn->scrnIndex, mode);
1604 NVCrtcSetOwner(crtc);
1606 nv_crtc_mode_set_vga(crtc, mode);
1607 nv_crtc_mode_set_regs(crtc, mode, adjusted_mode);
1610 NVCrtcLockUnlock(crtc, FALSE);
1612 NVVgaProtect(crtc, TRUE);
1613 if (pNv->Architecture == NV_ARCH_40) {
1614 nv40_crtc_load_state_pll(pNv, &pNv->ModeReg);
1616 nv_crtc_load_state_pll(pNv, &pNv->ModeReg);
1618 nv_crtc_load_state_ext(crtc, &pNv->ModeReg);
1619 nv_crtc_load_state_vga(crtc, &pNv->ModeReg);
1621 NVVgaProtect(crtc, FALSE);
1623 NVCrtcSetBase(crtc, x, y);
1625 #if X_BYTE_ORDER == X_BIG_ENDIAN
1626 /* turn on LFB swapping */
1630 tmp = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_SWAPPING);
1632 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_SWAPPING, tmp);
1637 void nv_crtc_save(xf86CrtcPtr crtc)
1639 ScrnInfoPtr pScrn = crtc->scrn;
1640 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1641 NVPtr pNv = NVPTR(pScrn);
1643 ErrorF("nv_crtc_save is called for CRTC %d\n", nv_crtc->crtc);
1645 /* We just came back from terminal, so unlock */
1646 NVCrtcLockUnlock(crtc, FALSE);
1648 NVCrtcSetOwner(crtc);
1649 if (pNv->Architecture == NV_ARCH_40) {
1650 nv40_crtc_save_state_pll(pNv, &pNv->SavedReg);
1652 nv_crtc_save_state_pll(pNv, &pNv->SavedReg);
1654 nv_crtc_save_state_vga(crtc, &pNv->SavedReg);
1655 nv_crtc_save_state_ext(crtc, &pNv->SavedReg);
1658 void nv_crtc_restore(xf86CrtcPtr crtc)
1660 ScrnInfoPtr pScrn = crtc->scrn;
1661 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1662 NVPtr pNv = NVPTR(pScrn);
1664 ErrorF("nv_crtc_restore is called for CRTC %d\n", nv_crtc->crtc);
1666 NVCrtcSetOwner(crtc);
1668 /* Just to be safe */
1669 NVCrtcLockUnlock(crtc, FALSE);
1671 NVVgaProtect(crtc, TRUE);
1672 nv_crtc_load_state_ext(crtc, &pNv->SavedReg);
1673 nv_crtc_load_state_vga(crtc, &pNv->SavedReg);
1674 if (pNv->Architecture == NV_ARCH_40) {
1675 nv40_crtc_load_state_pll(pNv, &pNv->SavedReg);
1677 nv_crtc_load_state_pll(pNv, &pNv->SavedReg);
1679 nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->vtOWNER);
1680 NVVgaProtect(crtc, FALSE);
1682 /* We must lock the door if we leave ;-) */
1683 NVCrtcLockUnlock(crtc, TRUE);
1686 void nv_crtc_prepare(xf86CrtcPtr crtc)
1688 ScrnInfoPtr pScrn = crtc->scrn;
1689 NVPtr pNv = NVPTR(pScrn);
1690 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1692 ErrorF("nv_crtc_prepare is called for CRTC %d\n", nv_crtc->crtc);
1694 crtc->funcs->dpms(crtc, DPMSModeOff);
1696 /* Sync the engine before adjust mode */
1697 if (pNv->EXADriverPtr) {
1698 exaMarkSync(pScrn->pScreen);
1699 exaWaitSync(pScrn->pScreen);
1703 void nv_crtc_commit(xf86CrtcPtr crtc)
1705 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1706 ErrorF("nv_crtc_commit for CRTC %d\n", nv_crtc->crtc);
1708 crtc->funcs->dpms (crtc, DPMSModeOn);
1709 if (crtc->scrn->pScreen != NULL)
1710 xf86_reload_cursors (crtc->scrn->pScreen);
1713 static Bool nv_crtc_lock(xf86CrtcPtr crtc)
1715 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1716 ErrorF("nv_crtc_lock is called for CRTC %d\n", nv_crtc->crtc);
1721 static void nv_crtc_unlock(xf86CrtcPtr crtc)
1723 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1724 ErrorF("nv_crtc_unlock is called for CRTC %d\n", nv_crtc->crtc);
1728 nv_crtc_gamma_set(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
1731 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1732 ScrnInfoPtr pScrn = crtc->scrn;
1733 NVPtr pNv = NVPTR(pScrn);
1737 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1739 switch (pNv->CurrentLayout.depth) {
1742 /* We've got 5 bit (32 values) colors and 256 registers for each color */
1743 for (i = 0; i < 32; i++) {
1744 for (j = 0; j < 8; j++) {
1745 regp->DAC[(i*8 + j) * 3 + 0] = red[i] >> 8;
1746 regp->DAC[(i*8 + j) * 3 + 1] = green[i] >> 8;
1747 regp->DAC[(i*8 + j) * 3 + 2] = blue[i] >> 8;
1753 /* First deal with the 5 bit colors */
1754 for (i = 0; i < 32; i++) {
1755 for (j = 0; j < 8; j++) {
1756 regp->DAC[(i*8 + j) * 3 + 0] = red[i] >> 8;
1757 regp->DAC[(i*8 + j) * 3 + 2] = blue[i] >> 8;
1760 /* Now deal with the 6 bit color */
1761 for (i = 0; i < 64; i++) {
1762 for (j = 0; j < 4; j++) {
1763 regp->DAC[(i*4 + j) * 3 + 1] = green[i] >> 8;
1769 for (i = 0; i < 256; i++) {
1770 regp->DAC[i * 3] = red[i] >> 8;
1771 regp->DAC[(i * 3) + 1] = green[i] >> 8;
1772 regp->DAC[(i * 3) + 2] = blue[i] >> 8;
1777 NVCrtcLoadPalette(crtc);
1780 /* NV04-NV10 doesn't support alpha cursors */
1781 static const xf86CrtcFuncsRec nv_crtc_funcs = {
1782 .dpms = nv_crtc_dpms,
1783 .save = nv_crtc_save, /* XXX */
1784 .restore = nv_crtc_restore, /* XXX */
1785 .mode_fixup = nv_crtc_mode_fixup,
1786 .mode_set = nv_crtc_mode_set,
1787 .prepare = nv_crtc_prepare,
1788 .commit = nv_crtc_commit,
1789 .destroy = NULL, /* XXX */
1790 .lock = nv_crtc_lock,
1791 .unlock = nv_crtc_unlock,
1792 .set_cursor_colors = nv_crtc_set_cursor_colors,
1793 .set_cursor_position = nv_crtc_set_cursor_position,
1794 .show_cursor = nv_crtc_show_cursor,
1795 .hide_cursor = nv_crtc_hide_cursor,
1796 .load_cursor_image = nv_crtc_load_cursor_image,
1797 .gamma_set = nv_crtc_gamma_set,
1800 /* NV11 and up has support for alpha cursors. */
1801 /* Due to different maximum sizes we cannot allow it to use normal cursors */
1802 static const xf86CrtcFuncsRec nv11_crtc_funcs = {
1803 .dpms = nv_crtc_dpms,
1804 .save = nv_crtc_save, /* XXX */
1805 .restore = nv_crtc_restore, /* XXX */
1806 .mode_fixup = nv_crtc_mode_fixup,
1807 .mode_set = nv_crtc_mode_set,
1808 .prepare = nv_crtc_prepare,
1809 .commit = nv_crtc_commit,
1810 .destroy = NULL, /* XXX */
1811 .lock = nv_crtc_lock,
1812 .unlock = nv_crtc_unlock,
1813 .set_cursor_colors = nv_crtc_set_cursor_colors,
1814 .set_cursor_position = nv_crtc_set_cursor_position,
1815 .show_cursor = nv_crtc_show_cursor,
1816 .hide_cursor = nv_crtc_hide_cursor,
1817 .load_cursor_argb = nv_crtc_load_cursor_argb,
1818 .gamma_set = nv_crtc_gamma_set,
1823 nv_crtc_init(ScrnInfoPtr pScrn, int crtc_num)
1825 NVPtr pNv = NVPTR(pScrn);
1827 NVCrtcPrivatePtr nv_crtc;
1829 if (pNv->NVArch >= 0x11) {
1830 crtc = xf86CrtcCreate (pScrn, &nv11_crtc_funcs);
1832 crtc = xf86CrtcCreate (pScrn, &nv_crtc_funcs);
1837 nv_crtc = xnfcalloc (sizeof (NVCrtcPrivateRec), 1);
1838 nv_crtc->crtc = crtc_num;
1839 nv_crtc->head = crtc_num;
1841 crtc->driver_private = nv_crtc;
1843 NVCrtcLockUnlock(crtc, FALSE);
1846 static void nv_crtc_load_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1848 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1852 regp = &state->crtc_reg[nv_crtc->head];
1854 NVWriteMiscOut(crtc, regp->MiscOutReg);
1856 for (i = 1; i < 5; i++)
1857 NVWriteVgaSeq(crtc, i, regp->Sequencer[i]);
1859 /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17] */
1860 NVWriteVgaCrtc(crtc, 17, regp->CRTC[17] & ~0x80);
1862 for (i = 0; i < 25; i++)
1863 NVWriteVgaCrtc(crtc, i, regp->CRTC[i]);
1865 for (i = 0; i < 9; i++)
1866 NVWriteVgaGr(crtc, i, regp->Graphics[i]);
1868 NVEnablePalette(crtc);
1869 for (i = 0; i < 21; i++)
1870 NVWriteVgaAttr(crtc, i, regp->Attribute[i]);
1871 NVDisablePalette(crtc);
1875 static void nv_crtc_fix_nv40_hw_cursor(xf86CrtcPtr crtc)
1877 /* TODO - implement this properly */
1878 ScrnInfoPtr pScrn = crtc->scrn;
1879 NVPtr pNv = NVPTR(pScrn);
1881 if(pNv->Architecture == NV_ARCH_40) { /* HW bug */
1882 volatile CARD32 curpos = nvReadCurRAMDAC(pNv, NV_RAMDAC_CURSOR_POS);
1883 nvWriteCurRAMDAC(pNv, NV_RAMDAC_CURSOR_POS, curpos);
1887 static void nv_crtc_load_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1889 ScrnInfoPtr pScrn = crtc->scrn;
1890 NVPtr pNv = NVPTR(pScrn);
1891 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1894 regp = &state->crtc_reg[nv_crtc->head];
1896 if(pNv->Architecture >= NV_ARCH_10) {
1898 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_FSEL, regp->head);
1900 nvWriteVIDEO(pNv, NV_PVIDEO_STOP, 1);
1901 nvWriteVIDEO(pNv, NV_PVIDEO_INTR_EN, 0);
1902 nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(0), 0);
1903 nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(1), 0);
1904 nvWriteVIDEO(pNv, NV_PVIDEO_LIMIT(0), pNv->VRAMPhysicalSize - 1);
1905 nvWriteVIDEO(pNv, NV_PVIDEO_LIMIT(1), pNv->VRAMPhysicalSize - 1);
1906 nvWriteMC(pNv, 0x1588, 0);
1908 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_BUFFER, regp->CRTC[NV_VGA_CRTCX_BUFFER]);
1909 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_CURSOR_CONFIG, regp->cursorConfig);
1910 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_GPIO, regp->gpio);
1911 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0830, regp->unk830);
1912 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0834, regp->unk834);
1913 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0850, regp->unk850);
1914 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_081C, regp->unk81c);
1916 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FP_HTIMING, regp->CRTC[NV_VGA_CRTCX_FP_HTIMING]);
1917 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FP_VTIMING, regp->CRTC[NV_VGA_CRTCX_FP_VTIMING]);
1919 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_26, regp->CRTC[NV_VGA_CRTCX_26]);
1920 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_3B, regp->CRTC[NV_VGA_CRTCX_3B]);
1921 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_3C, regp->CRTC[NV_VGA_CRTCX_3C]);
1922 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_45, regp->CRTC[NV_VGA_CRTCX_45]);
1923 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_52, regp->CRTC[NV_VGA_CRTCX_52]);
1924 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_56, regp->CRTC[NV_VGA_CRTCX_56]);
1925 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_57, regp->CRTC[NV_VGA_CRTCX_57]);
1926 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_58, regp->CRTC[NV_VGA_CRTCX_58]);
1927 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_59, regp->CRTC[NV_VGA_CRTCX_59]);
1928 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_EXTRA, regp->CRTC[NV_VGA_CRTCX_EXTRA]);
1931 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT0, regp->CRTC[NV_VGA_CRTCX_REPAINT0]);
1932 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1, regp->CRTC[NV_VGA_CRTCX_REPAINT1]);
1933 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LSR, regp->CRTC[NV_VGA_CRTCX_LSR]);
1934 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_PIXEL, regp->CRTC[NV_VGA_CRTCX_PIXEL]);
1935 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LCD, regp->CRTC[NV_VGA_CRTCX_LCD]);
1936 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_HEB, regp->CRTC[NV_VGA_CRTCX_HEB]);
1937 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO1, regp->CRTC[NV_VGA_CRTCX_FIFO1]);
1938 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO0, regp->CRTC[NV_VGA_CRTCX_FIFO0]);
1939 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM, regp->CRTC[NV_VGA_CRTCX_FIFO_LWM]);
1940 if(pNv->Architecture >= NV_ARCH_30) {
1941 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM_NV30, regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30]);
1944 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL0, regp->CRTC[NV_VGA_CRTCX_CURCTL0]);
1945 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL1, regp->CRTC[NV_VGA_CRTCX_CURCTL1]);
1946 nv_crtc_fix_nv40_hw_cursor(crtc);
1947 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL2, regp->CRTC[NV_VGA_CRTCX_CURCTL2]);
1948 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_INTERLACE, regp->CRTC[NV_VGA_CRTCX_INTERLACE]);
1950 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_INTR_EN_0, 0);
1951 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_INTR_0, NV_CRTC_INTR_VBLANK);
1953 pNv->CurrentState = state;
1956 static void nv_crtc_save_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1958 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1962 regp = &state->crtc_reg[nv_crtc->head];
1964 regp->MiscOutReg = NVReadMiscOut(crtc);
1966 for (i = 0; i < 25; i++)
1967 regp->CRTC[i] = NVReadVgaCrtc(crtc, i);
1969 NVEnablePalette(crtc);
1970 for (i = 0; i < 21; i++)
1971 regp->Attribute[i] = NVReadVgaAttr(crtc, i);
1972 NVDisablePalette(crtc);
1974 for (i = 0; i < 9; i++)
1975 regp->Graphics[i] = NVReadVgaGr(crtc, i);
1977 for (i = 1; i < 5; i++)
1978 regp->Sequencer[i] = NVReadVgaSeq(crtc, i);
1982 static void nv_crtc_save_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1984 ScrnInfoPtr pScrn = crtc->scrn;
1985 NVPtr pNv = NVPTR(pScrn);
1986 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1989 regp = &state->crtc_reg[nv_crtc->head];
1991 regp->CRTC[NV_VGA_CRTCX_LCD] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_LCD);
1992 regp->CRTC[NV_VGA_CRTCX_REPAINT0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT0);
1993 regp->CRTC[NV_VGA_CRTCX_REPAINT1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1);
1994 regp->CRTC[NV_VGA_CRTCX_LSR] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_LSR);
1995 regp->CRTC[NV_VGA_CRTCX_PIXEL] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_PIXEL);
1996 regp->CRTC[NV_VGA_CRTCX_HEB] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_HEB);
1997 regp->CRTC[NV_VGA_CRTCX_FIFO1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO1);
1999 regp->CRTC[NV_VGA_CRTCX_FIFO0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO0);
2000 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM);
2001 if(pNv->Architecture >= NV_ARCH_30) {
2002 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM_NV30);
2004 regp->CRTC[NV_VGA_CRTCX_CURCTL0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL0);
2005 regp->CRTC[NV_VGA_CRTCX_CURCTL1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL1);
2006 regp->CRTC[NV_VGA_CRTCX_CURCTL2] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL2);
2007 regp->CRTC[NV_VGA_CRTCX_INTERLACE] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_INTERLACE);
2009 regp->gpio = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_GPIO);
2010 regp->unk830 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0830);
2011 regp->unk834 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0834);
2012 regp->unk850 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0850);
2013 regp->unk81c = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_081C);
2015 if(pNv->Architecture >= NV_ARCH_10) {
2017 regp->head = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_FSEL);
2018 regp->crtcOwner = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_OWNER);
2020 regp->CRTC[NV_VGA_CRTCX_EXTRA] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_EXTRA);
2022 regp->cursorConfig = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_CURSOR_CONFIG);
2024 regp->CRTC[NV_VGA_CRTCX_26] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_26);
2025 regp->CRTC[NV_VGA_CRTCX_3B] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_3B);
2026 regp->CRTC[NV_VGA_CRTCX_3C] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_3C);
2027 regp->CRTC[NV_VGA_CRTCX_45] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_45);
2028 regp->CRTC[NV_VGA_CRTCX_52] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_52);
2029 regp->CRTC[NV_VGA_CRTCX_56] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_56);
2030 regp->CRTC[NV_VGA_CRTCX_57] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_57);
2031 regp->CRTC[NV_VGA_CRTCX_58] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_58);
2032 regp->CRTC[NV_VGA_CRTCX_59] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_59);
2033 regp->CRTC[NV_VGA_CRTCX_BUFFER] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_BUFFER);
2034 regp->CRTC[NV_VGA_CRTCX_FP_HTIMING] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FP_HTIMING);
2035 regp->CRTC[NV_VGA_CRTCX_FP_VTIMING] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FP_VTIMING);
2040 NVCrtcSetBase (xf86CrtcPtr crtc, int x, int y)
2042 ScrnInfoPtr pScrn = crtc->scrn;
2043 NVPtr pNv = NVPTR(pScrn);
2044 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2045 NVFBLayout *pLayout = &pNv->CurrentLayout;
2048 ErrorF("NVCrtcSetBase: x: %d y: %d\n", x, y);
2050 start += ((y * pScrn->displayWidth + x) * (pLayout->bitsPerPixel/8));
2051 start += pNv->FB->offset;
2053 /* 30 bits addresses in 32 bits according to haiku */
2054 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_START, start & 0xfffffffc);
2056 /* set NV4/NV10 byte adress: (bit0 - 1) */
2057 NVWriteVgaAttr(crtc, 0x13, (start & 0x3) << 1);
2063 static void NVCrtcWriteDacMask(xf86CrtcPtr crtc, CARD8 value)
2065 ScrnInfoPtr pScrn = crtc->scrn;
2066 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2067 NVPtr pNv = NVPTR(pScrn);
2068 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2070 NV_WR08(pDACReg, VGA_DAC_MASK, value);
2073 static CARD8 NVCrtcReadDacMask(xf86CrtcPtr crtc)
2075 ScrnInfoPtr pScrn = crtc->scrn;
2076 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2077 NVPtr pNv = NVPTR(pScrn);
2078 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2080 return NV_RD08(pDACReg, VGA_DAC_MASK);
2083 static void NVCrtcWriteDacReadAddr(xf86CrtcPtr crtc, CARD8 value)
2085 ScrnInfoPtr pScrn = crtc->scrn;
2086 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2087 NVPtr pNv = NVPTR(pScrn);
2088 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2090 NV_WR08(pDACReg, VGA_DAC_READ_ADDR, value);
2093 static void NVCrtcWriteDacWriteAddr(xf86CrtcPtr crtc, CARD8 value)
2095 ScrnInfoPtr pScrn = crtc->scrn;
2096 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2097 NVPtr pNv = NVPTR(pScrn);
2098 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2100 NV_WR08(pDACReg, VGA_DAC_WRITE_ADDR, value);
2103 static void NVCrtcWriteDacData(xf86CrtcPtr crtc, CARD8 value)
2105 ScrnInfoPtr pScrn = crtc->scrn;
2106 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2107 NVPtr pNv = NVPTR(pScrn);
2108 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2110 NV_WR08(pDACReg, VGA_DAC_DATA, value);
2113 static CARD8 NVCrtcReadDacData(xf86CrtcPtr crtc, CARD8 value)
2115 ScrnInfoPtr pScrn = crtc->scrn;
2116 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2117 NVPtr pNv = NVPTR(pScrn);
2118 volatile CARD8 *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2120 return NV_RD08(pDACReg, VGA_DAC_DATA);
2123 void NVCrtcLoadPalette(xf86CrtcPtr crtc)
2126 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2128 ScrnInfoPtr pScrn = crtc->scrn;
2129 NVPtr pNv = NVPTR(pScrn);
2131 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
2133 NVCrtcSetOwner(crtc);
2134 NVCrtcWriteDacMask(crtc, 0xff);
2135 NVCrtcWriteDacWriteAddr(crtc, 0x00);
2137 for (i = 0; i<768; i++) {
2138 NVCrtcWriteDacData(crtc, regp->DAC[i]);
2140 NVDisablePalette(crtc);
2143 void NVCrtcBlankScreen(xf86CrtcPtr crtc, Bool on)
2147 NVCrtcSetOwner(crtc);
2149 scrn = NVReadVgaSeq(crtc, 0x01);
2156 NVVgaSeqReset(crtc, TRUE);
2157 NVWriteVgaSeq(crtc, 0x01, scrn);
2158 NVVgaSeqReset(crtc, FALSE);
2161 /*************************************************************************** \
2163 |* Copyright 1993-2003 NVIDIA, Corporation. All rights reserved. *|
2165 |* NOTICE TO USER: The source code is copyrighted under U.S. and *|
2166 |* international laws. Users and possessors of this source code are *|
2167 |* hereby granted a nonexclusive, royalty-free copyright license to *|
2168 |* use this code in individual and commercial software. *|
2170 |* Any use of this source code must include, in the user documenta- *|
2171 |* tion and internal comments to the code, notices to the end user *|
2174 |* Copyright 1993-1999 NVIDIA, Corporation. All rights reserved. *|
2176 |* NVIDIA, CORPORATION MAKES NO REPRESENTATION ABOUT THE SUITABILITY *|
2177 |* OF THIS SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" *|
2178 |* WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. NVIDIA, CORPOR- *|
2179 |* ATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOURCE CODE, *|
2180 |* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGE- *|
2181 |* MENT, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL *|
2182 |* NVIDIA, CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT, INCI- *|
2183 |* DENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RE- *|
2184 |* SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION *|
2185 |* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF *|
2186 |* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE. *|
2188 |* U.S. Government End Users. This source code is a "commercial *|
2189 |* item," as that term is defined at 48 C.F.R. 2.101 (OCT 1995), *|
2190 |* consisting of "commercial computer software" and "commercial *|
2191 |* computer software documentation," as such terms are used in *|
2192 |* 48 C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Govern- *|
2193 |* ment only as a commercial end item. Consistent with 48 C.F.R. *|
2194 |* 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), *|
2195 |* all U.S. Government End Users acquire the source code with only *|
2196 |* those rights set forth herein. *|
2198 \***************************************************************************/