xref: /haiku/src/add-ons/accelerants/nvidia/engine/nv_crtc2.c (revision 7909881256f8dbda93a19c11f80e7beb6f164ae4)
1ff50d0d1SRudolf Cornelissen /* second CTRC functionality for GeForce cards */
2ff50d0d1SRudolf Cornelissen /* Author:
30b7b8998SRudolf Cornelissen    Rudolf Cornelissen 11/2002-1/2005
408705d96Sshatty */
508705d96Sshatty 
608705d96Sshatty #define MODULE_BIT 0x00020000
708705d96Sshatty 
808705d96Sshatty #include "nv_std.h"
908705d96Sshatty 
10ff50d0d1SRudolf Cornelissen /* Adjust passed parameters to a valid mode line */
11ff50d0d1SRudolf Cornelissen status_t nv_crtc2_validate_timing(
12ff50d0d1SRudolf Cornelissen 	uint16 *hd_e,uint16 *hs_s,uint16 *hs_e,uint16 *ht,
13ff50d0d1SRudolf Cornelissen 	uint16 *vd_e,uint16 *vs_s,uint16 *vs_e,uint16 *vt
14ff50d0d1SRudolf Cornelissen )
1508705d96Sshatty {
16ff50d0d1SRudolf Cornelissen /* horizontal */
17ff50d0d1SRudolf Cornelissen 	/* make all parameters multiples of 8 */
18ff50d0d1SRudolf Cornelissen 	*hd_e &= 0xfff8;
19ff50d0d1SRudolf Cornelissen 	*hs_s &= 0xfff8;
20ff50d0d1SRudolf Cornelissen 	*hs_e &= 0xfff8;
21ff50d0d1SRudolf Cornelissen 	*ht   &= 0xfff8;
22ff50d0d1SRudolf Cornelissen 
23ff50d0d1SRudolf Cornelissen 	/* confine to required number of bits, taking logic into account */
24ff50d0d1SRudolf Cornelissen 	if (*hd_e > ((0x01ff - 2) << 3)) *hd_e = ((0x01ff - 2) << 3);
25ff50d0d1SRudolf Cornelissen 	if (*hs_s > ((0x01ff - 1) << 3)) *hs_s = ((0x01ff - 1) << 3);
26ff50d0d1SRudolf Cornelissen 	if (*hs_e > ( 0x01ff      << 3)) *hs_e = ( 0x01ff      << 3);
27ff50d0d1SRudolf Cornelissen 	if (*ht   > ((0x01ff + 5) << 3)) *ht   = ((0x01ff + 5) << 3);
28ff50d0d1SRudolf Cornelissen 
29ff50d0d1SRudolf Cornelissen 	/* NOTE: keep horizontal timing at multiples of 8! */
30ff50d0d1SRudolf Cornelissen 	/* confine to a reasonable width */
31ff50d0d1SRudolf Cornelissen 	if (*hd_e < 640) *hd_e = 640;
32ff50d0d1SRudolf Cornelissen 	if (*hd_e > 2048) *hd_e = 2048;
33ff50d0d1SRudolf Cornelissen 
34ff50d0d1SRudolf Cornelissen 	/* if hor. total does not leave room for a sensible sync pulse, increase it! */
35ff50d0d1SRudolf Cornelissen 	if (*ht < (*hd_e + 80)) *ht = (*hd_e + 80);
36ff50d0d1SRudolf Cornelissen 
370ecea71bSRudolf Cornelissen 	/* if hor. total does not adhere to max. blanking pulse width, decrease it! */
380ecea71bSRudolf Cornelissen 	if (*ht > (*hd_e + 0x3f8)) *ht = (*hd_e + 0x3f8);
390ecea71bSRudolf Cornelissen 
40ff50d0d1SRudolf Cornelissen 	/* make sure sync pulse is not during display */
41ff50d0d1SRudolf Cornelissen 	if (*hs_e > (*ht - 8)) *hs_e = (*ht - 8);
42ff50d0d1SRudolf Cornelissen 	if (*hs_s < (*hd_e + 8)) *hs_s = (*hd_e + 8);
43ff50d0d1SRudolf Cornelissen 
44ff50d0d1SRudolf Cornelissen 	/* correct sync pulse if it is too long:
45ff50d0d1SRudolf Cornelissen 	 * there are only 5 bits available to save this in the card registers! */
46ff50d0d1SRudolf Cornelissen 	if (*hs_e > (*hs_s + 0xf8)) *hs_e = (*hs_s + 0xf8);
47ff50d0d1SRudolf Cornelissen 
48ff50d0d1SRudolf Cornelissen /*vertical*/
49ff50d0d1SRudolf Cornelissen 	/* confine to required number of bits, taking logic into account */
50ff50d0d1SRudolf Cornelissen 	//fixme if needed: on GeForce cards there are 12 instead of 11 bits...
51ff50d0d1SRudolf Cornelissen 	if (*vd_e > (0x7ff - 2)) *vd_e = (0x7ff - 2);
52ff50d0d1SRudolf Cornelissen 	if (*vs_s > (0x7ff - 1)) *vs_s = (0x7ff - 1);
53ff50d0d1SRudolf Cornelissen 	if (*vs_e >  0x7ff     ) *vs_e =  0x7ff     ;
54ff50d0d1SRudolf Cornelissen 	if (*vt   > (0x7ff + 2)) *vt   = (0x7ff + 2);
55ff50d0d1SRudolf Cornelissen 
56ff50d0d1SRudolf Cornelissen 	/* confine to a reasonable height */
57ff50d0d1SRudolf Cornelissen 	if (*vd_e < 480) *vd_e = 480;
58ff50d0d1SRudolf Cornelissen 	if (*vd_e > 1536) *vd_e = 1536;
59ff50d0d1SRudolf Cornelissen 
60ff50d0d1SRudolf Cornelissen 	/*if vertical total does not leave room for a sync pulse, increase it!*/
61ff50d0d1SRudolf Cornelissen 	if (*vt < (*vd_e + 3)) *vt = (*vd_e + 3);
62ff50d0d1SRudolf Cornelissen 
630ecea71bSRudolf Cornelissen 	/* if vert. total does not adhere to max. blanking pulse width, decrease it! */
640ecea71bSRudolf Cornelissen 	if (*vt > (*vd_e + 0xff)) *vt = (*vd_e + 0xff);
650ecea71bSRudolf Cornelissen 
66ff50d0d1SRudolf Cornelissen 	/* make sure sync pulse is not during display */
67ff50d0d1SRudolf Cornelissen 	if (*vs_e > (*vt - 1)) *vs_e = (*vt - 1);
68ff50d0d1SRudolf Cornelissen 	if (*vs_s < (*vd_e + 1)) *vs_s = (*vd_e + 1);
69ff50d0d1SRudolf Cornelissen 
70ff50d0d1SRudolf Cornelissen 	/* correct sync pulse if it is too long:
71ff50d0d1SRudolf Cornelissen 	 * there are only 4 bits available to save this in the card registers! */
72ff50d0d1SRudolf Cornelissen 	if (*vs_e > (*vs_s + 0x0f)) *vs_e = (*vs_s + 0x0f);
73ff50d0d1SRudolf Cornelissen 
74ff50d0d1SRudolf Cornelissen 	return B_OK;
75ff50d0d1SRudolf Cornelissen }
76ff50d0d1SRudolf Cornelissen 
77ff50d0d1SRudolf Cornelissen /*set a mode line - inputs are in pixels*/
78ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_timing(display_mode target)
79ff50d0d1SRudolf Cornelissen {
80ff50d0d1SRudolf Cornelissen 	uint8 temp;
81ff50d0d1SRudolf Cornelissen 
82ff50d0d1SRudolf Cornelissen 	uint32 htotal;		/*total horizontal total VCLKs*/
83ff50d0d1SRudolf Cornelissen 	uint32 hdisp_e;            /*end of horizontal display (begins at 0)*/
84ff50d0d1SRudolf Cornelissen 	uint32 hsync_s;            /*begin of horizontal sync pulse*/
85ff50d0d1SRudolf Cornelissen 	uint32 hsync_e;            /*end of horizontal sync pulse*/
86ff50d0d1SRudolf Cornelissen 	uint32 hblnk_s;            /*begin horizontal blanking*/
87ff50d0d1SRudolf Cornelissen 	uint32 hblnk_e;            /*end horizontal blanking*/
88ff50d0d1SRudolf Cornelissen 
89ff50d0d1SRudolf Cornelissen 	uint32 vtotal;		/*total vertical total scanlines*/
90ff50d0d1SRudolf Cornelissen 	uint32 vdisp_e;            /*end of vertical display*/
91ff50d0d1SRudolf Cornelissen 	uint32 vsync_s;            /*begin of vertical sync pulse*/
92ff50d0d1SRudolf Cornelissen 	uint32 vsync_e;            /*end of vertical sync pulse*/
93ff50d0d1SRudolf Cornelissen 	uint32 vblnk_s;            /*begin vertical blanking*/
94ff50d0d1SRudolf Cornelissen 	uint32 vblnk_e;            /*end vertical blanking*/
95ff50d0d1SRudolf Cornelissen 
96ff50d0d1SRudolf Cornelissen 	uint32 linecomp;	/*split screen and vdisp_e interrupt*/
9708705d96Sshatty 
9808705d96Sshatty 	LOG(4,("CRTC2: setting timing\n"));
9908705d96Sshatty 
100c9210b6fSRudolf Cornelissen 	/* setup tuned internal modeline for flatpanel if connected and active */
1012cb6fc9cSRudolf Cornelissen 	/* notes:
1022cb6fc9cSRudolf Cornelissen 	 * - the CRTC modeline must end earlier than the panel modeline to keep correct
1032cb6fc9cSRudolf Cornelissen 	 *   sync going;
1042cb6fc9cSRudolf Cornelissen 	 * - if the CRTC modeline ends too soon, pixelnoise will occur in 8 (or so) pixel
1052cb6fc9cSRudolf Cornelissen 	 *   wide horizontal stripes. This can be observed earliest on fullscreen overlay,
1062cb6fc9cSRudolf Cornelissen 	 *   and if it gets worse, also normal desktop output will suffer. The stripes
1072cb6fc9cSRudolf Cornelissen 	 *   are mainly visible at the left of the screen, over the entire screen height. */
108c567e072SRudolf Cornelissen 	if (si->ps.tmds2_active)
109c567e072SRudolf Cornelissen 	{
110c567e072SRudolf Cornelissen 		LOG(2,("CRTC2: DFP active: tuning modeline\n"));
111c567e072SRudolf Cornelissen 
112c567e072SRudolf Cornelissen 		/* horizontal timing */
11316fc5a30SRudolf Cornelissen 		target.timing.h_sync_start =
114268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.h_sync_start / ((float)si->ps.p2_timing.h_display)) *
11516fc5a30SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8;
11616fc5a30SRudolf Cornelissen 
11716fc5a30SRudolf Cornelissen 		target.timing.h_sync_end =
118268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.h_sync_end / ((float)si->ps.p2_timing.h_display)) *
11916fc5a30SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8;
12016fc5a30SRudolf Cornelissen 
12116fc5a30SRudolf Cornelissen 		target.timing.h_total =
122268624c4SRudolf Cornelissen 			(((uint16)((si->ps.p2_timing.h_total / ((float)si->ps.p2_timing.h_display)) *
123bef5b86aSRudolf Cornelissen 			target.timing.h_display)) & 0xfff8) - 8;
12416fc5a30SRudolf Cornelissen 
125139d62e9SRudolf Cornelissen 		/* in native mode the CRTC needs some extra time to keep synced correctly;
126139d62e9SRudolf Cornelissen 		 * OTOH the overlay unit distorts if we reserve too much time! */
1277ae8e6dcSRudolf Cornelissen 		if (target.timing.h_display == si->ps.p2_timing.h_display)
12804e6b7ceSRudolf Cornelissen 		{
129139d62e9SRudolf Cornelissen 			/* NV11 timing has different constraints than later cards */
130139d62e9SRudolf Cornelissen 			if (si->ps.card_type == NV11)
1312cb6fc9cSRudolf Cornelissen 				target.timing.h_total -= 56;
132139d62e9SRudolf Cornelissen 			else
133139d62e9SRudolf Cornelissen 				/* confirmed NV34 with 1680x1050 panel */
134139d62e9SRudolf Cornelissen 				target.timing.h_total -= 32;
13504e6b7ceSRudolf Cornelissen 		}
13604e6b7ceSRudolf Cornelissen 
13716fc5a30SRudolf Cornelissen 		if (target.timing.h_sync_start == target.timing.h_display)
13816fc5a30SRudolf Cornelissen 			target.timing.h_sync_start += 8;
13916fc5a30SRudolf Cornelissen 		if (target.timing.h_sync_end == target.timing.h_total)
14016fc5a30SRudolf Cornelissen 			target.timing.h_sync_end -= 8;
141c567e072SRudolf Cornelissen 
142c567e072SRudolf Cornelissen 		/* vertical timing */
14316fc5a30SRudolf Cornelissen 		target.timing.v_sync_start =
144268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_sync_start / ((float)si->ps.p2_timing.v_display)) *
14516fc5a30SRudolf Cornelissen 			target.timing.v_display));
14616fc5a30SRudolf Cornelissen 
14716fc5a30SRudolf Cornelissen 		target.timing.v_sync_end =
148268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_sync_end / ((float)si->ps.p2_timing.v_display)) *
14916fc5a30SRudolf Cornelissen 			target.timing.v_display));
15016fc5a30SRudolf Cornelissen 
15116fc5a30SRudolf Cornelissen 		target.timing.v_total =
152268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_total / ((float)si->ps.p2_timing.v_display)) *
153b97caf33SRudolf Cornelissen 			target.timing.v_display)) - 1;
15416fc5a30SRudolf Cornelissen 
15516fc5a30SRudolf Cornelissen 		if (target.timing.v_sync_start == target.timing.v_display)
15616fc5a30SRudolf Cornelissen 			target.timing.v_sync_start += 1;
15716fc5a30SRudolf Cornelissen 		if (target.timing.v_sync_end == target.timing.v_total)
15816fc5a30SRudolf Cornelissen 			target.timing.v_sync_end -= 1;
159e6708074SRudolf Cornelissen 
160e6708074SRudolf Cornelissen 		/* disable GPU scaling testmode so automatic scaling will be done */
161e6708074SRudolf Cornelissen 		DAC2W(FP_DEBUG1, 0);
162c567e072SRudolf Cornelissen 	}
163c567e072SRudolf Cornelissen 
164ff50d0d1SRudolf Cornelissen 	/* Modify parameters as required by standard VGA */
165ff50d0d1SRudolf Cornelissen 	htotal = ((target.timing.h_total >> 3) - 5);
166ff50d0d1SRudolf Cornelissen 	hdisp_e = ((target.timing.h_display >> 3) - 1);
167ff50d0d1SRudolf Cornelissen 	hblnk_s = hdisp_e;
168ff50d0d1SRudolf Cornelissen 	hblnk_e = (htotal + 4);//0;
169ff50d0d1SRudolf Cornelissen 	hsync_s = (target.timing.h_sync_start >> 3);
170ff50d0d1SRudolf Cornelissen 	hsync_e = (target.timing.h_sync_end >> 3);
171ff50d0d1SRudolf Cornelissen 
172ff50d0d1SRudolf Cornelissen 	vtotal = target.timing.v_total - 2;
173ff50d0d1SRudolf Cornelissen 	vdisp_e = target.timing.v_display - 1;
174ff50d0d1SRudolf Cornelissen 	vblnk_s = vdisp_e;
175ff50d0d1SRudolf Cornelissen 	vblnk_e = (vtotal + 1);
176ff50d0d1SRudolf Cornelissen 	vsync_s = target.timing.v_sync_start;//-1;
177ff50d0d1SRudolf Cornelissen 	vsync_e = target.timing.v_sync_end;//-1;
178ff50d0d1SRudolf Cornelissen 
179ff50d0d1SRudolf Cornelissen 	/* prevent memory adress counter from being reset (linecomp may not occur) */
180ff50d0d1SRudolf Cornelissen 	linecomp = target.timing.v_display;
181ff50d0d1SRudolf Cornelissen 
18264c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
18364c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
184255e5021SRudolf Cornelissen 
185a16d55ddSRudolf Cornelissen 	/* Note for laptop and DVI flatpanels:
186a16d55ddSRudolf Cornelissen 	 * CRTC timing has a seperate set of registers from flatpanel timing.
187a16d55ddSRudolf Cornelissen 	 * The flatpanel timing registers have scaling registers that are used to match
188a16d55ddSRudolf Cornelissen 	 * these two modelines. */
18908705d96Sshatty 	{
190a16d55ddSRudolf Cornelissen 		LOG(4,("CRTC2: Setting full timing...\n"));
19108705d96Sshatty 
192ff50d0d1SRudolf Cornelissen 		/* log the mode that will be set */
193ff50d0d1SRudolf Cornelissen 		LOG(2,("CRTC2:\n\tHTOT:%x\n\tHDISPEND:%x\n\tHBLNKS:%x\n\tHBLNKE:%x\n\tHSYNCS:%x\n\tHSYNCE:%x\n\t",htotal,hdisp_e,hblnk_s,hblnk_e,hsync_s,hsync_e));
194ff50d0d1SRudolf Cornelissen 		LOG(2,("VTOT:%x\n\tVDISPEND:%x\n\tVBLNKS:%x\n\tVBLNKE:%x\n\tVSYNCS:%x\n\tVSYNCE:%x\n",vtotal,vdisp_e,vblnk_s,vblnk_e,vsync_s,vsync_e));
19508705d96Sshatty 
196ff50d0d1SRudolf Cornelissen 		/* actually program the card! */
197ff50d0d1SRudolf Cornelissen 		/* unlock CRTC registers at index 0-7 */
198ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCE, (CRTC2R(VSYNCE) & 0x7f));
199ff50d0d1SRudolf Cornelissen 		/* horizontal standard VGA regs */
200ff50d0d1SRudolf Cornelissen 		CRTC2W(HTOTAL, (htotal & 0xff));
201ff50d0d1SRudolf Cornelissen 		CRTC2W(HDISPE, (hdisp_e & 0xff));
202ff50d0d1SRudolf Cornelissen 		CRTC2W(HBLANKS, (hblnk_s & 0xff));
203ff50d0d1SRudolf Cornelissen 		/* also unlock vertical retrace registers in advance */
204ff50d0d1SRudolf Cornelissen 		CRTC2W(HBLANKE, ((hblnk_e & 0x1f) | 0x80));
205ff50d0d1SRudolf Cornelissen 		CRTC2W(HSYNCS, (hsync_s & 0xff));
206ff50d0d1SRudolf Cornelissen 		CRTC2W(HSYNCE, ((hsync_e & 0x1f) | ((hblnk_e & 0x20) << 2)));
20708705d96Sshatty 
208ff50d0d1SRudolf Cornelissen 		/* vertical standard VGA regs */
209ff50d0d1SRudolf Cornelissen 		CRTC2W(VTOTAL, (vtotal & 0xff));
210ff50d0d1SRudolf Cornelissen 		CRTC2W(OVERFLOW,
211ff50d0d1SRudolf Cornelissen 		(
212ff50d0d1SRudolf Cornelissen 			((vtotal & 0x100) >> (8 - 0)) | ((vtotal & 0x200) >> (9 - 5)) |
213ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x100) >> (8 - 1)) | ((vdisp_e & 0x200) >> (9 - 6)) |
214ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x100) >> (8 - 2)) | ((vsync_s & 0x200) >> (9 - 7)) |
215ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x100) >> (8 - 3)) | ((linecomp & 0x100) >> (8 - 4))
216ff50d0d1SRudolf Cornelissen 		));
217ff50d0d1SRudolf Cornelissen 		CRTC2W(PRROWSCN, 0x00); /* not used */
218ff50d0d1SRudolf Cornelissen 		CRTC2W(MAXSCLIN, (((vblnk_s & 0x200) >> (9 - 5)) | ((linecomp & 0x200) >> (9 - 6))));
219ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCS, (vsync_s & 0xff));
220ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCE, ((CRTC2R(VSYNCE) & 0xf0) | (vsync_e & 0x0f)));
221ff50d0d1SRudolf Cornelissen 		CRTC2W(VDISPE, (vdisp_e & 0xff));
222ff50d0d1SRudolf Cornelissen 		CRTC2W(VBLANKS, (vblnk_s & 0xff));
223ff50d0d1SRudolf Cornelissen 		CRTC2W(VBLANKE, (vblnk_e & 0xff));
224ff50d0d1SRudolf Cornelissen 		CRTC2W(LINECOMP, (linecomp & 0xff));
22508705d96Sshatty 
226ff50d0d1SRudolf Cornelissen 		/* horizontal extended regs */
227ff50d0d1SRudolf Cornelissen 		//fixme: we reset bit4. is this correct??
228ff50d0d1SRudolf Cornelissen 		CRTC2W(HEB, (CRTC2R(HEB) & 0xe0) |
229ff50d0d1SRudolf Cornelissen 			(
230ff50d0d1SRudolf Cornelissen 		 	((htotal & 0x100) >> (8 - 0)) |
231ff50d0d1SRudolf Cornelissen 			((hdisp_e & 0x100) >> (8 - 1)) |
232ff50d0d1SRudolf Cornelissen 			((hblnk_s & 0x100) >> (8 - 2)) |
233ff50d0d1SRudolf Cornelissen 			((hsync_s & 0x100) >> (8 - 3))
234ff50d0d1SRudolf Cornelissen 			));
23508705d96Sshatty 
236ff50d0d1SRudolf Cornelissen 		/* (mostly) vertical extended regs */
237ff50d0d1SRudolf Cornelissen 		CRTC2W(LSR,
238ff50d0d1SRudolf Cornelissen 			(
239ff50d0d1SRudolf Cornelissen 		 	((vtotal & 0x400) >> (10 - 0)) |
240ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x400) >> (10 - 1)) |
241ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x400) >> (10 - 2)) |
242ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x400) >> (10 - 3)) |
243ff50d0d1SRudolf Cornelissen 			((hblnk_e & 0x040) >> (6 - 4))
244ff50d0d1SRudolf Cornelissen 			//fixme: we still miss one linecomp bit!?! is this it??
245ff50d0d1SRudolf Cornelissen 			//| ((linecomp & 0x400) >> 3)
246ff50d0d1SRudolf Cornelissen 			));
24708705d96Sshatty 
248ff50d0d1SRudolf Cornelissen 		/* more vertical extended regs */
249ff50d0d1SRudolf Cornelissen 		CRTC2W(EXTRA,
250ff50d0d1SRudolf Cornelissen 			(
251ff50d0d1SRudolf Cornelissen 		 	((vtotal & 0x800) >> (11 - 0)) |
252ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x800) >> (11 - 2)) |
253ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x800) >> (11 - 4)) |
254ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x800) >> (11 - 6))
255ff50d0d1SRudolf Cornelissen 			//fixme: do we miss another linecomp bit!?!
256ff50d0d1SRudolf Cornelissen 			));
25708705d96Sshatty 
258ff50d0d1SRudolf Cornelissen 		/* setup 'large screen' mode */
259ff50d0d1SRudolf Cornelissen 		if (target.timing.h_display >= 1280)
260ff50d0d1SRudolf Cornelissen 			CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xfb));
26108705d96Sshatty 		else
262ff50d0d1SRudolf Cornelissen 			CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x04));
26308705d96Sshatty 
264ff50d0d1SRudolf Cornelissen 		/* setup HSYNC & VSYNC polarity */
265ff50d0d1SRudolf Cornelissen 		LOG(2,("CRTC2: sync polarity: "));
266255e5021SRudolf Cornelissen 		temp = NV_REG8(NV8_MISCR);
267ff50d0d1SRudolf Cornelissen 		if (target.timing.flags & B_POSITIVE_HSYNC)
268ff50d0d1SRudolf Cornelissen 		{
269ff50d0d1SRudolf Cornelissen 			LOG(2,("H:pos "));
270ff50d0d1SRudolf Cornelissen 			temp &= ~0x40;
27108705d96Sshatty 		}
272ff50d0d1SRudolf Cornelissen 		else
273ff50d0d1SRudolf Cornelissen 		{
274ff50d0d1SRudolf Cornelissen 			LOG(2,("H:neg "));
275ff50d0d1SRudolf Cornelissen 			temp |= 0x40;
276ff50d0d1SRudolf Cornelissen 		}
277ff50d0d1SRudolf Cornelissen 		if (target.timing.flags & B_POSITIVE_VSYNC)
278ff50d0d1SRudolf Cornelissen 		{
279ff50d0d1SRudolf Cornelissen 			LOG(2,("V:pos "));
280ff50d0d1SRudolf Cornelissen 			temp &= ~0x80;
281ff50d0d1SRudolf Cornelissen 		}
282ff50d0d1SRudolf Cornelissen 		else
283ff50d0d1SRudolf Cornelissen 		{
284ff50d0d1SRudolf Cornelissen 			LOG(2,("V:neg "));
285ff50d0d1SRudolf Cornelissen 			temp |= 0x80;
286ff50d0d1SRudolf Cornelissen 		}
287255e5021SRudolf Cornelissen 		NV_REG8(NV8_MISCW) = temp;
288ff50d0d1SRudolf Cornelissen 
289255e5021SRudolf Cornelissen 		LOG(2,(", MISC reg readback: $%02x\n", NV_REG8(NV8_MISCR)));
290ff50d0d1SRudolf Cornelissen 	}
291ff50d0d1SRudolf Cornelissen 
292ff50d0d1SRudolf Cornelissen 	/* always disable interlaced operation */
293255e5021SRudolf Cornelissen 	/* (interlace is supported on upto and including NV10, NV15, and NV30 and up) */
294ff50d0d1SRudolf Cornelissen 	CRTC2W(INTERLACE, 0xff);
29508705d96Sshatty 
296bc9c6041SRudolf Cornelissen 	/* disable CRTC slaved mode unless a panel is in use */
297bc9c6041SRudolf Cornelissen 	// fixme: this kills TVout when it was in use...
298bc9c6041SRudolf Cornelissen 	if (!si->ps.tmds2_active) CRTC2W(PIXEL, (CRTC2R(PIXEL) & 0x7f));
299bc9c6041SRudolf Cornelissen 
3001e37a9acSRudolf Cornelissen 	/* setup flatpanel if connected and active */
301a16d55ddSRudolf Cornelissen 	if (si->ps.tmds2_active)
302a16d55ddSRudolf Cornelissen 	{
303a16d55ddSRudolf Cornelissen 		uint32 iscale_x, iscale_y;
304a16d55ddSRudolf Cornelissen 
305a973fe9eSRudolf Cornelissen 		/* calculate inverse scaling factors used by hardware in 20.12 format */
3060fccffc2SRudolf Cornelissen 		iscale_x = (((1 << 12) * target.timing.h_display) / si->ps.p2_timing.h_display);
3070fccffc2SRudolf Cornelissen 		iscale_y = (((1 << 12) * target.timing.v_display) / si->ps.p2_timing.v_display);
3081e37a9acSRudolf Cornelissen 
3091e37a9acSRudolf Cornelissen 		/* unblock flatpanel timing programming (or something like that..) */
3101e37a9acSRudolf Cornelissen 		CRTC2W(FP_HTIMING, 0);
3111e37a9acSRudolf Cornelissen 		CRTC2W(FP_VTIMING, 0);
312e6708074SRudolf Cornelissen 		LOG(2,("CRTC2: FP_HTIMING reg readback: $%02x\n", CRTC2R(FP_HTIMING)));
313e6708074SRudolf Cornelissen 		LOG(2,("CRTC2: FP_VTIMING reg readback: $%02x\n", CRTC2R(FP_VTIMING)));
3141e37a9acSRudolf Cornelissen 
315a973fe9eSRudolf Cornelissen 		/* enable full width visibility on flatpanel */
316a973fe9eSRudolf Cornelissen 		DAC2W(FP_HVALID_S, 0);
3170fccffc2SRudolf Cornelissen 		DAC2W(FP_HVALID_E, (si->ps.p2_timing.h_display - 1));
318a973fe9eSRudolf Cornelissen 		/* enable full height visibility on flatpanel */
319a973fe9eSRudolf Cornelissen 		DAC2W(FP_VVALID_S, 0);
3200fccffc2SRudolf Cornelissen 		DAC2W(FP_VVALID_E, (si->ps.p2_timing.v_display - 1));
321a973fe9eSRudolf Cornelissen 
3224709c2c8SRudolf Cornelissen 		/* nVidia cards support upscaling except on ??? */
3234709c2c8SRudolf Cornelissen 		/* NV11 cards can upscale after all! */
324e6708074SRudolf Cornelissen 		if (0)//si->ps.card_type == NV11)
3251e37a9acSRudolf Cornelissen 		{
3261e37a9acSRudolf Cornelissen 			/* disable last fetched line limiting */
3271e37a9acSRudolf Cornelissen 			DAC2W(FP_DEBUG2, 0x00000000);
328c567e072SRudolf Cornelissen 			/* inform panel to scale if needed */
329c567e072SRudolf Cornelissen 			if ((iscale_x != (1 << 12)) || (iscale_y != (1 << 12)))
330c567e072SRudolf Cornelissen 			{
331c567e072SRudolf Cornelissen 				LOG(2,("CRTC2: DFP needs to do scaling\n"));
3321e37a9acSRudolf Cornelissen 				DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) | 0x00000100));
3331e37a9acSRudolf Cornelissen 			}
3341e37a9acSRudolf Cornelissen 			else
3351e37a9acSRudolf Cornelissen 			{
336c567e072SRudolf Cornelissen 				LOG(2,("CRTC2: no scaling for DFP needed\n"));
337c567e072SRudolf Cornelissen 				DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) & 0xfffffeff));
338c567e072SRudolf Cornelissen 			}
339c567e072SRudolf Cornelissen 		}
340c567e072SRudolf Cornelissen 		else
341c567e072SRudolf Cornelissen 		{
342a973fe9eSRudolf Cornelissen 			float dm_aspect;
343a973fe9eSRudolf Cornelissen 
344c567e072SRudolf Cornelissen 			LOG(2,("CRTC2: GPU scales for DFP if needed\n"));
3451e37a9acSRudolf Cornelissen 
346a973fe9eSRudolf Cornelissen 			/* calculate display mode aspect */
347a973fe9eSRudolf Cornelissen 			dm_aspect = (target.timing.h_display / ((float)target.timing.v_display));
348a973fe9eSRudolf Cornelissen 
349a16d55ddSRudolf Cornelissen 			/* limit last fetched line if vertical scaling is done */
3501e37a9acSRudolf Cornelissen 			if (iscale_y != (1 << 12))
351a16d55ddSRudolf Cornelissen 				DAC2W(FP_DEBUG2, ((1 << 28) | ((target.timing.v_display - 1) << 16)));
352a16d55ddSRudolf Cornelissen 			else
353a16d55ddSRudolf Cornelissen 				DAC2W(FP_DEBUG2, 0x00000000);
3541e37a9acSRudolf Cornelissen 
3551e37a9acSRudolf Cornelissen 			/* inform panel not to scale */
3561e37a9acSRudolf Cornelissen 			DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) & 0xfffffeff));
357c65998faSRudolf Cornelissen 
358c65998faSRudolf Cornelissen 			/* GPU scaling is automatically setup by hardware, so only modify this
359c65998faSRudolf Cornelissen 			 * scalingfactor for non 4:3 (1.33) aspect panels;
360c65998faSRudolf Cornelissen 			 * let's consider 1280x1024 1:33 aspect (it's 1.25 aspect actually!) */
361c65998faSRudolf Cornelissen 
362a973fe9eSRudolf Cornelissen 			/* correct for widescreen panels relative to mode...
363a973fe9eSRudolf Cornelissen 			 * (so if panel is more widescreen than mode being set) */
364a973fe9eSRudolf Cornelissen 			/* BTW: known widescreen panels:
365c65998faSRudolf Cornelissen 			 * 1280 x  800 (1.60),
366c65998faSRudolf Cornelissen 			 * 1440 x  900 (1.60),
367b97caf33SRudolf Cornelissen 			 * 1680 x 1050 (1.60),
368b97caf33SRudolf Cornelissen 			 * 1920 x 1200 (1.60). */
369c65998faSRudolf Cornelissen 			/* known 4:3 aspect non-standard resolution panels:
370c65998faSRudolf Cornelissen 			 * 1400 x 1050 (1.33). */
371a973fe9eSRudolf Cornelissen 			/* NOTE:
372a973fe9eSRudolf Cornelissen 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
373a973fe9eSRudolf Cornelissen 			if ((iscale_x != (1 << 12)) && (si->ps.panel2_aspect > (dm_aspect + 0.10)))
374c65998faSRudolf Cornelissen 			{
375a973fe9eSRudolf Cornelissen 				uint16 diff;
376a973fe9eSRudolf Cornelissen 
377a973fe9eSRudolf Cornelissen 				LOG(2,("CRTC2: (relative) widescreen panel: tuning horizontal scaling\n"));
378a973fe9eSRudolf Cornelissen 
379a973fe9eSRudolf Cornelissen 				/* X-scaling should be the same as Y-scaling */
380a973fe9eSRudolf Cornelissen 				iscale_x = iscale_y;
381c65998faSRudolf Cornelissen 				/* enable testmode (b12) and program new X-scaling factor */
382c65998faSRudolf Cornelissen 				DAC2W(FP_DEBUG1, (((iscale_x >> 1) & 0x00000fff) | (1 << 12)));
383a973fe9eSRudolf Cornelissen 				/* center/cut-off left and right side of screen */
3840fccffc2SRudolf Cornelissen 				diff = ((si->ps.p2_timing.h_display -
385*79098812SRudolf Cornelissen 						((target.timing.h_display * (1 << 12)) / iscale_x))
386a973fe9eSRudolf Cornelissen 						/ 2);
387a973fe9eSRudolf Cornelissen 				DAC2W(FP_HVALID_S, diff);
3880fccffc2SRudolf Cornelissen 				DAC2W(FP_HVALID_E, ((si->ps.p2_timing.h_display - diff) - 1));
389c65998faSRudolf Cornelissen 			}
390c65998faSRudolf Cornelissen 			/* correct for portrait panels... */
391a973fe9eSRudolf Cornelissen 			/* NOTE:
392a973fe9eSRudolf Cornelissen 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
393a973fe9eSRudolf Cornelissen 			if ((iscale_y != (1 << 12)) && (si->ps.panel2_aspect < (dm_aspect - 0.10)))
394c65998faSRudolf Cornelissen 			{
395a973fe9eSRudolf Cornelissen 				LOG(2,("CRTC2: (relative) portrait panel: should tune vertical scaling\n"));
396a973fe9eSRudolf Cornelissen 				/* fixme: implement if this kind of portrait panels exist on nVidia... */
397c65998faSRudolf Cornelissen 			}
3981e37a9acSRudolf Cornelissen 		}
3991e37a9acSRudolf Cornelissen 
4001e37a9acSRudolf Cornelissen 		/* do some logging.. */
401a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_HVALID_S reg readback: $%08x\n", DAC2R(FP_HVALID_S)));
402a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_HVALID_E reg readback: $%08x\n", DAC2R(FP_HVALID_E)));
403a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_VVALID_S reg readback: $%08x\n", DAC2R(FP_VVALID_S)));
404a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_VVALID_E reg readback: $%08x\n", DAC2R(FP_VVALID_E)));
4051e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG0 reg readback: $%08x\n", DAC2R(FP_DEBUG0)));
4061e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG1 reg readback: $%08x\n", DAC2R(FP_DEBUG1)));
4071e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG2 reg readback: $%08x\n", DAC2R(FP_DEBUG2)));
4081e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG3 reg readback: $%08x\n", DAC2R(FP_DEBUG3)));
4091e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_TG_CTRL reg readback: $%08x\n", DAC2R(FP_TG_CTRL)));
410a16d55ddSRudolf Cornelissen 	}
411a16d55ddSRudolf Cornelissen 
41208705d96Sshatty 	return B_OK;
41308705d96Sshatty }
41408705d96Sshatty 
415ff50d0d1SRudolf Cornelissen status_t nv_crtc2_depth(int mode)
41608705d96Sshatty {
417ff50d0d1SRudolf Cornelissen 	uint8 viddelay = 0;
418ff50d0d1SRudolf Cornelissen 	uint32 genctrl = 0;
419ff50d0d1SRudolf Cornelissen 
420ff50d0d1SRudolf Cornelissen 	/* set VCLK scaling */
42108705d96Sshatty 	switch(mode)
42208705d96Sshatty 	{
423ff50d0d1SRudolf Cornelissen 	case BPP8:
424ff50d0d1SRudolf Cornelissen 		viddelay = 0x01;
425ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 reset: 'direct mode' */
426ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101100;
42708705d96Sshatty 		break;
428ff50d0d1SRudolf Cornelissen 	case BPP15:
429ff50d0d1SRudolf Cornelissen 		viddelay = 0x02;
430ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
431ff50d0d1SRudolf Cornelissen 		genctrl = 0x00100130;
432ff50d0d1SRudolf Cornelissen 		break;
433ff50d0d1SRudolf Cornelissen 	case BPP16:
434ff50d0d1SRudolf Cornelissen 		viddelay = 0x02;
435ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
436ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101130;
437ff50d0d1SRudolf Cornelissen 		break;
438ff50d0d1SRudolf Cornelissen 	case BPP24:
439ff50d0d1SRudolf Cornelissen 		viddelay = 0x03;
440ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
441ff50d0d1SRudolf Cornelissen 		genctrl = 0x00100130;
442ff50d0d1SRudolf Cornelissen 		break;
443ff50d0d1SRudolf Cornelissen 	case BPP32:
444ff50d0d1SRudolf Cornelissen 		viddelay = 0x03;
445ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
446ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101130;
44708705d96Sshatty 		break;
44808705d96Sshatty 	}
44964c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
45064c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
451255e5021SRudolf Cornelissen 
452ff50d0d1SRudolf Cornelissen 	CRTC2W(PIXEL, ((CRTC2R(PIXEL) & 0xfc) | viddelay));
453ff50d0d1SRudolf Cornelissen 	DAC2W(GENCTRL, genctrl);
45408705d96Sshatty 
45508705d96Sshatty 	return B_OK;
45608705d96Sshatty }
45708705d96Sshatty 
458ff50d0d1SRudolf Cornelissen status_t nv_crtc2_dpms(bool display, bool h, bool v)
45908705d96Sshatty {
460d97178c9SRudolf Cornelissen 	uint8 temp;
461ff50d0d1SRudolf Cornelissen 
462ff50d0d1SRudolf Cornelissen 	LOG(4,("CRTC2: setting DPMS: "));
463ff50d0d1SRudolf Cornelissen 
46464c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
46564c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
466255e5021SRudolf Cornelissen 
467ff50d0d1SRudolf Cornelissen 	/* start synchronous reset: required before turning screen off! */
468d97178c9SRudolf Cornelissen 	SEQW(RESET, 0x01);
469ff50d0d1SRudolf Cornelissen 
470ff50d0d1SRudolf Cornelissen 	/* turn screen off */
471d97178c9SRudolf Cornelissen 	temp = SEQR(CLKMODE);
472ff50d0d1SRudolf Cornelissen 	if (display)
47308705d96Sshatty 	{
474d97178c9SRudolf Cornelissen 		SEQW(CLKMODE, (temp & ~0x20));
475ff50d0d1SRudolf Cornelissen 
476ff50d0d1SRudolf Cornelissen 		/* end synchronous reset if display should be enabled */
477d97178c9SRudolf Cornelissen 		SEQW(RESET, 0x03);
478ff50d0d1SRudolf Cornelissen 
47922ffe8b5SRudolf Cornelissen 		//'safe mode' test! feedback needed with this 'setting'!
48022ffe8b5SRudolf Cornelissen 		if (0)//si->ps.tmds2_active)
481b4f28c26SRudolf Cornelissen 		{
482b4f28c26SRudolf Cornelissen 			/* powerup both LVDS (laptop panellink) and TMDS (DVI panellink)
483b4f28c26SRudolf Cornelissen 			 * internal transmitters... */
4848addb7c3SRudolf Cornelissen 			/* note:
4858addb7c3SRudolf Cornelissen 			 * the powerbits in this register are hardwired to the DVI connectors,
4868addb7c3SRudolf Cornelissen 			 * instead of to the DACs! (confirmed NV34) */
4878addb7c3SRudolf Cornelissen 			//fixme...
488b4f28c26SRudolf Cornelissen 			DAC2W(FP_DEBUG0, (DAC2R(FP_DEBUG0) & 0xcfffffff));
489b4f28c26SRudolf Cornelissen 			/* ... and powerup external TMDS transmitter if it exists */
490ed391abaSRudolf Cornelissen 			/* (confirmed OK on NV28 and NV34) */
491ed391abaSRudolf Cornelissen 			CRTC2W(0x59, (CRTC2R(0x59) | 0x01));
492b4f28c26SRudolf Cornelissen 		}
4934709c2c8SRudolf Cornelissen 
494ff50d0d1SRudolf Cornelissen 		LOG(4,("display on, "));
49508705d96Sshatty 	}
49608705d96Sshatty 	else
49708705d96Sshatty 	{
498d97178c9SRudolf Cornelissen 		SEQW(CLKMODE, (temp | 0x20));
499ff50d0d1SRudolf Cornelissen 
50022ffe8b5SRudolf Cornelissen 		//'safe mode' test! feedback needed with this 'setting'!
50122ffe8b5SRudolf Cornelissen 		if (0)//si->ps.tmds2_active)
502b4f28c26SRudolf Cornelissen 		{
503b4f28c26SRudolf Cornelissen 			/* powerdown both LVDS (laptop panellink) and TMDS (DVI panellink)
504b4f28c26SRudolf Cornelissen 			 * internal transmitters... */
5058addb7c3SRudolf Cornelissen 			/* note:
5068addb7c3SRudolf Cornelissen 			 * the powerbits in this register are hardwired to the DVI connectors,
5078addb7c3SRudolf Cornelissen 			 * instead of to the DACs! (confirmed NV34) */
5088addb7c3SRudolf Cornelissen 			//fixme...
509b4f28c26SRudolf Cornelissen 			DAC2W(FP_DEBUG0, (DAC2R(FP_DEBUG0) | 0x30000000));
510b4f28c26SRudolf Cornelissen 			/* ... and powerdown external TMDS transmitter if it exists */
511ed391abaSRudolf Cornelissen 			/* (confirmed OK on NV28 and NV34) */
512ed391abaSRudolf Cornelissen 			CRTC2W(0x59, (CRTC2R(0x59) & 0xfe));
513b4f28c26SRudolf Cornelissen 		}
5144709c2c8SRudolf Cornelissen 
515ff50d0d1SRudolf Cornelissen 		LOG(4,("display off, "));
51608705d96Sshatty 	}
51708705d96Sshatty 
518ff50d0d1SRudolf Cornelissen 	if (h)
51908705d96Sshatty 	{
520ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0x7f));
521ff50d0d1SRudolf Cornelissen 		LOG(4,("hsync enabled, "));
522ff50d0d1SRudolf Cornelissen 	}
523ff50d0d1SRudolf Cornelissen 	else
524ff50d0d1SRudolf Cornelissen 	{
525ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x80));
526ff50d0d1SRudolf Cornelissen 		LOG(4,("hsync disabled, "));
527ff50d0d1SRudolf Cornelissen 	}
528ff50d0d1SRudolf Cornelissen 	if (v)
529ff50d0d1SRudolf Cornelissen 	{
530ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xbf));
531ff50d0d1SRudolf Cornelissen 		LOG(4,("vsync enabled\n"));
532ff50d0d1SRudolf Cornelissen 	}
533ff50d0d1SRudolf Cornelissen 	else
534ff50d0d1SRudolf Cornelissen 	{
535ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x40));
536ff50d0d1SRudolf Cornelissen 		LOG(4,("vsync disabled\n"));
537ff50d0d1SRudolf Cornelissen 	}
53808705d96Sshatty 
53908705d96Sshatty 	return B_OK;
54008705d96Sshatty }
54108705d96Sshatty 
542ff50d0d1SRudolf Cornelissen status_t nv_crtc2_dpms_fetch(bool *display, bool *h, bool *v)
543ff50d0d1SRudolf Cornelissen {
54464c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
54564c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
546255e5021SRudolf Cornelissen 
547d97178c9SRudolf Cornelissen 	*display = !(SEQR(CLKMODE) & 0x20);
548ff50d0d1SRudolf Cornelissen 	*h = !(CRTC2R(REPAINT1) & 0x80);
549ff50d0d1SRudolf Cornelissen 	*v = !(CRTC2R(REPAINT1) & 0x40);
550ff50d0d1SRudolf Cornelissen 
551ff50d0d1SRudolf Cornelissen 	LOG(4,("CTRC2: fetched DPMS state: "));
55217f2ecd6SRudolf Cornelissen 	if (*display) LOG(4,("display on, "));
553ff50d0d1SRudolf Cornelissen 	else LOG(4,("display off, "));
55417f2ecd6SRudolf Cornelissen 	if (*h) LOG(4,("hsync enabled, "));
555ff50d0d1SRudolf Cornelissen 	else LOG(4,("hsync disabled, "));
55617f2ecd6SRudolf Cornelissen 	if (*v) LOG(4,("vsync enabled\n"));
557ff50d0d1SRudolf Cornelissen 	else LOG(4,("vsync disabled\n"));
558ff50d0d1SRudolf Cornelissen 
559ff50d0d1SRudolf Cornelissen 	return B_OK;
560ff50d0d1SRudolf Cornelissen }
561ff50d0d1SRudolf Cornelissen 
562ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_display_pitch()
56308705d96Sshatty {
56408705d96Sshatty 	uint32 offset;
56508705d96Sshatty 
56608705d96Sshatty 	LOG(4,("CRTC2: setting card pitch (offset between lines)\n"));
56708705d96Sshatty 
56808705d96Sshatty 	/* figure out offset value hardware needs */
569ff50d0d1SRudolf Cornelissen 	offset = si->fbc.bytes_per_row / 8;
57008705d96Sshatty 
571ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: offset register set to: $%04x\n", offset));
57208705d96Sshatty 
57364c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
57464c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
575255e5021SRudolf Cornelissen 
576b4bdc2b6SRudolf Cornelissen 	/* program the card */
577ff50d0d1SRudolf Cornelissen 	CRTC2W(PITCHL, (offset & 0x00ff));
578ff50d0d1SRudolf Cornelissen 	CRTC2W(REPAINT0, ((CRTC2R(REPAINT0) & 0x1f) | ((offset & 0x0700) >> 3)));
579ff50d0d1SRudolf Cornelissen 
58008705d96Sshatty 	return B_OK;
58108705d96Sshatty }
58208705d96Sshatty 
583ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_display_start(uint32 startadd,uint8 bpp)
58408705d96Sshatty {
585e0dd08e8SRudolf Cornelissen 	uint32 timeout = 0;
58608705d96Sshatty 
587ff50d0d1SRudolf Cornelissen 	LOG(4,("CRTC2: setting card RAM to be displayed bpp %d\n", bpp));
58808705d96Sshatty 
589ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: startadd: $%08x\n", startadd));
590ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: frameRAM: $%08x\n", si->framebuffer));
591ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: framebuffer: $%08x\n", si->fbc.frame_buffer));
592ff50d0d1SRudolf Cornelissen 
593e0dd08e8SRudolf Cornelissen 	/* we might have no retraces during setmode! */
594e0dd08e8SRudolf Cornelissen 	/* wait 25mS max. for retrace to occur (refresh > 40Hz) */
595e0dd08e8SRudolf Cornelissen 	while (((NV_REG32(NV32_RASTER2) & 0x000007ff) < si->dm.timing.v_display) &&
596e0dd08e8SRudolf Cornelissen 			(timeout < (25000/10)))
597e0dd08e8SRudolf Cornelissen 	{
598e0dd08e8SRudolf Cornelissen 		/* don't snooze much longer or retrace might get missed! */
599e0dd08e8SRudolf Cornelissen 		snooze(10);
600e0dd08e8SRudolf Cornelissen 		timeout++;
601e0dd08e8SRudolf Cornelissen 	}
602ff50d0d1SRudolf Cornelissen 
60364c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
60464c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
605255e5021SRudolf Cornelissen 
606ff50d0d1SRudolf Cornelissen 	/* upto 4Gb RAM adressing: must be used on NV10 and later! */
607ff50d0d1SRudolf Cornelissen 	/* NOTE:
608ff50d0d1SRudolf Cornelissen 	 * While this register also exists on pre-NV10 cards, it will
609ff50d0d1SRudolf Cornelissen 	 * wrap-around at 16Mb boundaries!! */
610ff50d0d1SRudolf Cornelissen 
611ff50d0d1SRudolf Cornelissen 	/* 30bit adress in 32bit words */
612ff50d0d1SRudolf Cornelissen 	NV_REG32(NV32_NV10FB2STADD32) = (startadd & 0xfffffffc);
613ff50d0d1SRudolf Cornelissen 
614bc9d4aceSRudolf Cornelissen 	/* set byte adress: (b0 - 1) */
615e0dd08e8SRudolf Cornelissen 	ATB2W(HORPIXPAN, ((startadd & 0x00000003) << 1));
616ff50d0d1SRudolf Cornelissen 
617ff50d0d1SRudolf Cornelissen 	return B_OK;
618ff50d0d1SRudolf Cornelissen }
619ff50d0d1SRudolf Cornelissen 
620ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_init()
621ff50d0d1SRudolf Cornelissen {
622ff50d0d1SRudolf Cornelissen 	int i;
623ff50d0d1SRudolf Cornelissen 	uint32 * fb;
624ff50d0d1SRudolf Cornelissen 	/* cursor bitmap will be stored at the start of the framebuffer */
625ff50d0d1SRudolf Cornelissen 	const uint32 curadd = 0;
626ff50d0d1SRudolf Cornelissen 
62764c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
62864c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
629255e5021SRudolf Cornelissen 
630ff50d0d1SRudolf Cornelissen 	/* set cursor bitmap adress ... */
631255e5021SRudolf Cornelissen 	if (si->ps.laptop)
632ff50d0d1SRudolf Cornelissen 	{
633ff50d0d1SRudolf Cornelissen 		/* must be used this way on pre-NV10 and on all 'Go' cards! */
634ff50d0d1SRudolf Cornelissen 
635ff50d0d1SRudolf Cornelissen 		/* cursorbitmap must start on 2Kbyte boundary: */
636ff50d0d1SRudolf Cornelissen 		/* set adress bit11-16, and set 'no doublescan' (registerbit 1 = 0) */
637ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL0, ((curadd & 0x0001f800) >> 9));
638ff50d0d1SRudolf Cornelissen 		/* set adress bit17-23, and set graphics mode cursor(?) (registerbit 7 = 1) */
639ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL1, (((curadd & 0x00fe0000) >> 17) | 0x80));
640ff50d0d1SRudolf Cornelissen 		/* set adress bit24-31 */
641ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL2, ((curadd & 0xff000000) >> 24));
64208705d96Sshatty 	}
64308705d96Sshatty 	else
64408705d96Sshatty 	{
645ff50d0d1SRudolf Cornelissen 		/* upto 4Gb RAM adressing:
646ff50d0d1SRudolf Cornelissen 		 * can be used on NV10 and later (except for 'Go' cards)! */
647ff50d0d1SRudolf Cornelissen 		/* NOTE:
648ff50d0d1SRudolf Cornelissen 		 * This register does not exist on pre-NV10 and 'Go' cards. */
649ff50d0d1SRudolf Cornelissen 
650ff50d0d1SRudolf Cornelissen 		/* cursorbitmap must still start on 2Kbyte boundary: */
651ff50d0d1SRudolf Cornelissen 		NV_REG32(NV32_NV10CUR2ADD32) = (curadd & 0xfffff800);
65208705d96Sshatty 	}
65308705d96Sshatty 
654ff50d0d1SRudolf Cornelissen 	/* set cursor colour: not needed because of direct nature of cursor bitmap. */
655ff50d0d1SRudolf Cornelissen 
656ff50d0d1SRudolf Cornelissen 	/*clear cursor*/
657ff50d0d1SRudolf Cornelissen 	fb = (uint32 *) si->framebuffer + curadd;
658ff50d0d1SRudolf Cornelissen 	for (i=0;i<(2048/4);i++)
659ff50d0d1SRudolf Cornelissen 	{
660ff50d0d1SRudolf Cornelissen 		fb[i]=0;
661ff50d0d1SRudolf Cornelissen 	}
662ff50d0d1SRudolf Cornelissen 
663ff50d0d1SRudolf Cornelissen 	/* select 32x32 pixel, 16bit color cursorbitmap, no doublescan */
664ff50d0d1SRudolf Cornelissen 	NV_REG32(NV32_2CURCONF) = 0x02000100;
665ff50d0d1SRudolf Cornelissen 
666df7dbd1dSRudolf Cornelissen 	/* activate hardware-sync between cursor updates and vertical retrace */
667df7dbd1dSRudolf Cornelissen 	DAC2W(NV10_CURSYNC, (DAC2R(NV10_CURSYNC) | 0x02000000));
668df7dbd1dSRudolf Cornelissen 
669ff50d0d1SRudolf Cornelissen 	/* activate hardware cursor */
670255e5021SRudolf Cornelissen 	nv_crtc2_cursor_show();
671ff50d0d1SRudolf Cornelissen 
672ff50d0d1SRudolf Cornelissen 	return B_OK;
673ff50d0d1SRudolf Cornelissen }
674ff50d0d1SRudolf Cornelissen 
675ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_show()
676ff50d0d1SRudolf Cornelissen {
677255e5021SRudolf Cornelissen 	LOG(4,("CRTC2: enabling cursor\n"));
678255e5021SRudolf Cornelissen 
67964c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
68064c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
681255e5021SRudolf Cornelissen 
682ff50d0d1SRudolf Cornelissen 	/* b0 = 1 enables cursor */
683ff50d0d1SRudolf Cornelissen 	CRTC2W(CURCTL0, (CRTC2R(CURCTL0) | 0x01));
684ff50d0d1SRudolf Cornelissen 
685ff50d0d1SRudolf Cornelissen 	return B_OK;
686ff50d0d1SRudolf Cornelissen }
687ff50d0d1SRudolf Cornelissen 
688ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_hide()
689ff50d0d1SRudolf Cornelissen {
690255e5021SRudolf Cornelissen 	LOG(4,("CRTC2: disabling cursor\n"));
691255e5021SRudolf Cornelissen 
69264c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
69364c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
694255e5021SRudolf Cornelissen 
695ff50d0d1SRudolf Cornelissen 	/* b0 = 0 disables cursor */
696ff50d0d1SRudolf Cornelissen 	CRTC2W(CURCTL0, (CRTC2R(CURCTL0) & 0xfe));
697ff50d0d1SRudolf Cornelissen 
698ff50d0d1SRudolf Cornelissen 	return B_OK;
699ff50d0d1SRudolf Cornelissen }
700ff50d0d1SRudolf Cornelissen 
701ff50d0d1SRudolf Cornelissen /*set up cursor shape*/
702ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_define(uint8* andMask,uint8* xorMask)
703ff50d0d1SRudolf Cornelissen {
704ff50d0d1SRudolf Cornelissen 	int x, y;
705ff50d0d1SRudolf Cornelissen 	uint8 b;
706ff50d0d1SRudolf Cornelissen 	uint16 *cursor;
707ff50d0d1SRudolf Cornelissen 	uint16 pixel;
708ff50d0d1SRudolf Cornelissen 
709ff50d0d1SRudolf Cornelissen 	/* get a pointer to the cursor */
710ff50d0d1SRudolf Cornelissen 	cursor = (uint16*) si->framebuffer;
711ff50d0d1SRudolf Cornelissen 
712ff50d0d1SRudolf Cornelissen 	/* draw the cursor */
713ff50d0d1SRudolf Cornelissen 	/* (Nvidia cards have a RGB15 direct color cursor bitmap, bit #16 is transparancy) */
714ff50d0d1SRudolf Cornelissen 	for (y = 0; y < 16; y++)
715ff50d0d1SRudolf Cornelissen 	{
716ff50d0d1SRudolf Cornelissen 		b = 0x80;
717ff50d0d1SRudolf Cornelissen 		for (x = 0; x < 8; x++)
718ff50d0d1SRudolf Cornelissen 		{
719ff50d0d1SRudolf Cornelissen 			/* preset transparant */
720ff50d0d1SRudolf Cornelissen 			pixel = 0x0000;
721ff50d0d1SRudolf Cornelissen 			/* set white if requested */
722ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
723ff50d0d1SRudolf Cornelissen 			/* set black if requested */
724ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
725ff50d0d1SRudolf Cornelissen 			/* set invert if requested */
726ff50d0d1SRudolf Cornelissen 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
727ff50d0d1SRudolf Cornelissen 			/* place the pixel in the bitmap */
728ff50d0d1SRudolf Cornelissen 			cursor[x + (y * 32)] = pixel;
729ff50d0d1SRudolf Cornelissen 			b >>= 1;
730ff50d0d1SRudolf Cornelissen 		}
731ff50d0d1SRudolf Cornelissen 		xorMask++;
732ff50d0d1SRudolf Cornelissen 		andMask++;
733ff50d0d1SRudolf Cornelissen 		b = 0x80;
734ff50d0d1SRudolf Cornelissen 		for (; x < 16; x++)
735ff50d0d1SRudolf Cornelissen 		{
736ff50d0d1SRudolf Cornelissen 			/* preset transparant */
737ff50d0d1SRudolf Cornelissen 			pixel = 0x0000;
738ff50d0d1SRudolf Cornelissen 			/* set white if requested */
739ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
740ff50d0d1SRudolf Cornelissen 			/* set black if requested */
741ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
742ff50d0d1SRudolf Cornelissen 			/* set invert if requested */
743ff50d0d1SRudolf Cornelissen 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
744ff50d0d1SRudolf Cornelissen 			/* place the pixel in the bitmap */
745ff50d0d1SRudolf Cornelissen 			cursor[x + (y * 32)] = pixel;
746ff50d0d1SRudolf Cornelissen 			b >>= 1;
747ff50d0d1SRudolf Cornelissen 		}
748ff50d0d1SRudolf Cornelissen 		xorMask++;
749ff50d0d1SRudolf Cornelissen 		andMask++;
750ff50d0d1SRudolf Cornelissen 	}
751ff50d0d1SRudolf Cornelissen 
752ff50d0d1SRudolf Cornelissen 	return B_OK;
753ff50d0d1SRudolf Cornelissen }
754ff50d0d1SRudolf Cornelissen 
755ff50d0d1SRudolf Cornelissen /* position the cursor */
756ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_position(uint16 x, uint16 y)
757ff50d0d1SRudolf Cornelissen {
7580b7b8998SRudolf Cornelissen 	/* the cursor position is updated during retrace by card hardware */
759ff50d0d1SRudolf Cornelissen 
760ff50d0d1SRudolf Cornelissen 	/* update cursorposition */
761ff50d0d1SRudolf Cornelissen 	DAC2W(CURPOS, ((x & 0x0fff) | ((y & 0x0fff) << 16)));
762ff50d0d1SRudolf Cornelissen 
76308705d96Sshatty 	return B_OK;
76408705d96Sshatty }
765