xref: /haiku/src/add-ons/accelerants/nvidia/engine/nv_crtc2.c (revision 17f2ecd6d429c97b27355d30a6e7cfe77a032240)
1ff50d0d1SRudolf Cornelissen /* second CTRC functionality for GeForce cards */
2ff50d0d1SRudolf Cornelissen /* Author:
3*17f2ecd6SRudolf Cornelissen    Rudolf Cornelissen 11/2002-6/2004
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 
37ff50d0d1SRudolf Cornelissen 	/* make sure sync pulse is not during display */
38ff50d0d1SRudolf Cornelissen 	if (*hs_e > (*ht - 8)) *hs_e = (*ht - 8);
39ff50d0d1SRudolf Cornelissen 	if (*hs_s < (*hd_e + 8)) *hs_s = (*hd_e + 8);
40ff50d0d1SRudolf Cornelissen 
41ff50d0d1SRudolf Cornelissen 	/* correct sync pulse if it is too long:
42ff50d0d1SRudolf Cornelissen 	 * there are only 5 bits available to save this in the card registers! */
43ff50d0d1SRudolf Cornelissen 	if (*hs_e > (*hs_s + 0xf8)) *hs_e = (*hs_s + 0xf8);
44ff50d0d1SRudolf Cornelissen 
45ff50d0d1SRudolf Cornelissen /*vertical*/
46ff50d0d1SRudolf Cornelissen 	/* confine to required number of bits, taking logic into account */
47ff50d0d1SRudolf Cornelissen 	//fixme if needed: on GeForce cards there are 12 instead of 11 bits...
48ff50d0d1SRudolf Cornelissen 	if (*vd_e > (0x7ff - 2)) *vd_e = (0x7ff - 2);
49ff50d0d1SRudolf Cornelissen 	if (*vs_s > (0x7ff - 1)) *vs_s = (0x7ff - 1);
50ff50d0d1SRudolf Cornelissen 	if (*vs_e >  0x7ff     ) *vs_e =  0x7ff     ;
51ff50d0d1SRudolf Cornelissen 	if (*vt   > (0x7ff + 2)) *vt   = (0x7ff + 2);
52ff50d0d1SRudolf Cornelissen 
53ff50d0d1SRudolf Cornelissen 	/* confine to a reasonable height */
54ff50d0d1SRudolf Cornelissen 	if (*vd_e < 480) *vd_e = 480;
55ff50d0d1SRudolf Cornelissen 	if (*vd_e > 1536) *vd_e = 1536;
56ff50d0d1SRudolf Cornelissen 
57ff50d0d1SRudolf Cornelissen 	/*if vertical total does not leave room for a sync pulse, increase it!*/
58ff50d0d1SRudolf Cornelissen 	if (*vt < (*vd_e + 3)) *vt = (*vd_e + 3);
59ff50d0d1SRudolf Cornelissen 
60ff50d0d1SRudolf Cornelissen 	/* make sure sync pulse is not during display */
61ff50d0d1SRudolf Cornelissen 	if (*vs_e > (*vt - 1)) *vs_e = (*vt - 1);
62ff50d0d1SRudolf Cornelissen 	if (*vs_s < (*vd_e + 1)) *vs_s = (*vd_e + 1);
63ff50d0d1SRudolf Cornelissen 
64ff50d0d1SRudolf Cornelissen 	/* correct sync pulse if it is too long:
65ff50d0d1SRudolf Cornelissen 	 * there are only 4 bits available to save this in the card registers! */
66ff50d0d1SRudolf Cornelissen 	if (*vs_e > (*vs_s + 0x0f)) *vs_e = (*vs_s + 0x0f);
67ff50d0d1SRudolf Cornelissen 
68ff50d0d1SRudolf Cornelissen 	return B_OK;
69ff50d0d1SRudolf Cornelissen }
70ff50d0d1SRudolf Cornelissen 
71ff50d0d1SRudolf Cornelissen /*set a mode line - inputs are in pixels*/
72ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_timing(display_mode target)
73ff50d0d1SRudolf Cornelissen {
74ff50d0d1SRudolf Cornelissen 	uint8 temp;
75ff50d0d1SRudolf Cornelissen 
76ff50d0d1SRudolf Cornelissen 	uint32 htotal;		/*total horizontal total VCLKs*/
77ff50d0d1SRudolf Cornelissen 	uint32 hdisp_e;            /*end of horizontal display (begins at 0)*/
78ff50d0d1SRudolf Cornelissen 	uint32 hsync_s;            /*begin of horizontal sync pulse*/
79ff50d0d1SRudolf Cornelissen 	uint32 hsync_e;            /*end of horizontal sync pulse*/
80ff50d0d1SRudolf Cornelissen 	uint32 hblnk_s;            /*begin horizontal blanking*/
81ff50d0d1SRudolf Cornelissen 	uint32 hblnk_e;            /*end horizontal blanking*/
82ff50d0d1SRudolf Cornelissen 
83ff50d0d1SRudolf Cornelissen 	uint32 vtotal;		/*total vertical total scanlines*/
84ff50d0d1SRudolf Cornelissen 	uint32 vdisp_e;            /*end of vertical display*/
85ff50d0d1SRudolf Cornelissen 	uint32 vsync_s;            /*begin of vertical sync pulse*/
86ff50d0d1SRudolf Cornelissen 	uint32 vsync_e;            /*end of vertical sync pulse*/
87ff50d0d1SRudolf Cornelissen 	uint32 vblnk_s;            /*begin vertical blanking*/
88ff50d0d1SRudolf Cornelissen 	uint32 vblnk_e;            /*end vertical blanking*/
89ff50d0d1SRudolf Cornelissen 
90ff50d0d1SRudolf Cornelissen 	uint32 linecomp;	/*split screen and vdisp_e interrupt*/
9108705d96Sshatty 
9208705d96Sshatty 	LOG(4,("CRTC2: setting timing\n"));
9308705d96Sshatty 
94c9210b6fSRudolf Cornelissen 	/* setup tuned internal modeline for flatpanel if connected and active */
95c567e072SRudolf Cornelissen 	if (si->ps.tmds2_active)
96c567e072SRudolf Cornelissen 	{
97c567e072SRudolf Cornelissen 		LOG(2,("CRTC2: DFP active: tuning modeline\n"));
98c567e072SRudolf Cornelissen 
99c567e072SRudolf Cornelissen 		/* horizontal timing */
100c65998faSRudolf Cornelissen 		//testing (640x480): total = 135% is too much, 120% to small...
101b6ec0fb2SRudolf Cornelissen 		//total = display + 160 equals panel modeline: but must be smaller...?
10216fc5a30SRudolf Cornelissen //		target.timing.h_total = target.timing.h_display + 152;//160;//128
10316fc5a30SRudolf Cornelissen //		target.timing.h_sync_start = target.timing.h_total - 136;//144;//112
10416fc5a30SRudolf Cornelissen //		target.timing.h_sync_end = target.timing.h_total - 40;//48;//16
10516fc5a30SRudolf Cornelissen 		//adaptive to panel: fixme: test on 4:3 and 16:10 panels!
10616fc5a30SRudolf Cornelissen 		target.timing.h_sync_start =
107268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.h_sync_start / ((float)si->ps.p2_timing.h_display)) *
10816fc5a30SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8;
10916fc5a30SRudolf Cornelissen 
11016fc5a30SRudolf Cornelissen 		target.timing.h_sync_end =
111268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.h_sync_end / ((float)si->ps.p2_timing.h_display)) *
11216fc5a30SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8;
11316fc5a30SRudolf Cornelissen 
11416fc5a30SRudolf Cornelissen 		target.timing.h_total =
115268624c4SRudolf Cornelissen 			(((uint16)((si->ps.p2_timing.h_total / ((float)si->ps.p2_timing.h_display)) *
116bef5b86aSRudolf Cornelissen 			target.timing.h_display)) & 0xfff8) - 8;
11716fc5a30SRudolf Cornelissen 
11816fc5a30SRudolf Cornelissen 		if (target.timing.h_sync_start == target.timing.h_display)
11916fc5a30SRudolf Cornelissen 			target.timing.h_sync_start += 8;
12016fc5a30SRudolf Cornelissen 		if (target.timing.h_sync_end == target.timing.h_total)
12116fc5a30SRudolf Cornelissen 			target.timing.h_sync_end -= 8;
122c567e072SRudolf Cornelissen 
123c567e072SRudolf Cornelissen 		/* vertical timing */
12416fc5a30SRudolf Cornelissen //		target.timing.v_total = target.timing.v_display + 6;
12516fc5a30SRudolf Cornelissen //		target.timing.v_sync_start = target.timing.v_total - 3;
12616fc5a30SRudolf Cornelissen //		target.timing.v_sync_end = target.timing.v_total - 2;
12716fc5a30SRudolf Cornelissen 		target.timing.v_sync_start =
128268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_sync_start / ((float)si->ps.p2_timing.v_display)) *
12916fc5a30SRudolf Cornelissen 			target.timing.v_display));
13016fc5a30SRudolf Cornelissen 
13116fc5a30SRudolf Cornelissen 		target.timing.v_sync_end =
132268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_sync_end / ((float)si->ps.p2_timing.v_display)) *
13316fc5a30SRudolf Cornelissen 			target.timing.v_display));
13416fc5a30SRudolf Cornelissen 
13516fc5a30SRudolf Cornelissen 		target.timing.v_total =
136268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_total / ((float)si->ps.p2_timing.v_display)) *
137b97caf33SRudolf Cornelissen 			target.timing.v_display)) - 1;
13816fc5a30SRudolf Cornelissen 
13916fc5a30SRudolf Cornelissen 		if (target.timing.v_sync_start == target.timing.v_display)
14016fc5a30SRudolf Cornelissen 			target.timing.v_sync_start += 1;
14116fc5a30SRudolf Cornelissen 		if (target.timing.v_sync_end == target.timing.v_total)
14216fc5a30SRudolf Cornelissen 			target.timing.v_sync_end -= 1;
143e6708074SRudolf Cornelissen 
144e6708074SRudolf Cornelissen 		/* disable GPU scaling testmode so automatic scaling will be done */
145e6708074SRudolf Cornelissen 		DAC2W(FP_DEBUG1, 0);
146c567e072SRudolf Cornelissen 	}
147c567e072SRudolf Cornelissen 
148ff50d0d1SRudolf Cornelissen 	/* Modify parameters as required by standard VGA */
149ff50d0d1SRudolf Cornelissen 	htotal = ((target.timing.h_total >> 3) - 5);
150ff50d0d1SRudolf Cornelissen 	hdisp_e = ((target.timing.h_display >> 3) - 1);
151ff50d0d1SRudolf Cornelissen 	hblnk_s = hdisp_e;
152ff50d0d1SRudolf Cornelissen 	hblnk_e = (htotal + 4);//0;
153ff50d0d1SRudolf Cornelissen 	hsync_s = (target.timing.h_sync_start >> 3);
154ff50d0d1SRudolf Cornelissen 	hsync_e = (target.timing.h_sync_end >> 3);
155ff50d0d1SRudolf Cornelissen 
156ff50d0d1SRudolf Cornelissen 	vtotal = target.timing.v_total - 2;
157ff50d0d1SRudolf Cornelissen 	vdisp_e = target.timing.v_display - 1;
158ff50d0d1SRudolf Cornelissen 	vblnk_s = vdisp_e;
159ff50d0d1SRudolf Cornelissen 	vblnk_e = (vtotal + 1);
160ff50d0d1SRudolf Cornelissen 	vsync_s = target.timing.v_sync_start;//-1;
161ff50d0d1SRudolf Cornelissen 	vsync_e = target.timing.v_sync_end;//-1;
162ff50d0d1SRudolf Cornelissen 
163ff50d0d1SRudolf Cornelissen 	/* prevent memory adress counter from being reset (linecomp may not occur) */
164ff50d0d1SRudolf Cornelissen 	linecomp = target.timing.v_display;
165ff50d0d1SRudolf Cornelissen 
16664c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
16764c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
168255e5021SRudolf Cornelissen 
169a16d55ddSRudolf Cornelissen 	/* Note for laptop and DVI flatpanels:
170a16d55ddSRudolf Cornelissen 	 * CRTC timing has a seperate set of registers from flatpanel timing.
171a16d55ddSRudolf Cornelissen 	 * The flatpanel timing registers have scaling registers that are used to match
172a16d55ddSRudolf Cornelissen 	 * these two modelines. */
17308705d96Sshatty 	{
174a16d55ddSRudolf Cornelissen 		LOG(4,("CRTC2: Setting full timing...\n"));
17508705d96Sshatty 
176ff50d0d1SRudolf Cornelissen 		/* log the mode that will be set */
177ff50d0d1SRudolf 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));
178ff50d0d1SRudolf 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));
17908705d96Sshatty 
180ff50d0d1SRudolf Cornelissen 		/* actually program the card! */
181ff50d0d1SRudolf Cornelissen 		/* unlock CRTC registers at index 0-7 */
182ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCE, (CRTC2R(VSYNCE) & 0x7f));
183ff50d0d1SRudolf Cornelissen 		/* horizontal standard VGA regs */
184ff50d0d1SRudolf Cornelissen 		CRTC2W(HTOTAL, (htotal & 0xff));
185ff50d0d1SRudolf Cornelissen 		CRTC2W(HDISPE, (hdisp_e & 0xff));
186ff50d0d1SRudolf Cornelissen 		CRTC2W(HBLANKS, (hblnk_s & 0xff));
187ff50d0d1SRudolf Cornelissen 		/* also unlock vertical retrace registers in advance */
188ff50d0d1SRudolf Cornelissen 		CRTC2W(HBLANKE, ((hblnk_e & 0x1f) | 0x80));
189ff50d0d1SRudolf Cornelissen 		CRTC2W(HSYNCS, (hsync_s & 0xff));
190ff50d0d1SRudolf Cornelissen 		CRTC2W(HSYNCE, ((hsync_e & 0x1f) | ((hblnk_e & 0x20) << 2)));
19108705d96Sshatty 
192ff50d0d1SRudolf Cornelissen 		/* vertical standard VGA regs */
193ff50d0d1SRudolf Cornelissen 		CRTC2W(VTOTAL, (vtotal & 0xff));
194ff50d0d1SRudolf Cornelissen 		CRTC2W(OVERFLOW,
195ff50d0d1SRudolf Cornelissen 		(
196ff50d0d1SRudolf Cornelissen 			((vtotal & 0x100) >> (8 - 0)) | ((vtotal & 0x200) >> (9 - 5)) |
197ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x100) >> (8 - 1)) | ((vdisp_e & 0x200) >> (9 - 6)) |
198ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x100) >> (8 - 2)) | ((vsync_s & 0x200) >> (9 - 7)) |
199ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x100) >> (8 - 3)) | ((linecomp & 0x100) >> (8 - 4))
200ff50d0d1SRudolf Cornelissen 		));
201ff50d0d1SRudolf Cornelissen 		CRTC2W(PRROWSCN, 0x00); /* not used */
202ff50d0d1SRudolf Cornelissen 		CRTC2W(MAXSCLIN, (((vblnk_s & 0x200) >> (9 - 5)) | ((linecomp & 0x200) >> (9 - 6))));
203ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCS, (vsync_s & 0xff));
204ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCE, ((CRTC2R(VSYNCE) & 0xf0) | (vsync_e & 0x0f)));
205ff50d0d1SRudolf Cornelissen 		CRTC2W(VDISPE, (vdisp_e & 0xff));
206ff50d0d1SRudolf Cornelissen 		CRTC2W(VBLANKS, (vblnk_s & 0xff));
207ff50d0d1SRudolf Cornelissen 		CRTC2W(VBLANKE, (vblnk_e & 0xff));
208ff50d0d1SRudolf Cornelissen 		CRTC2W(LINECOMP, (linecomp & 0xff));
20908705d96Sshatty 
210ff50d0d1SRudolf Cornelissen 		/* horizontal extended regs */
211ff50d0d1SRudolf Cornelissen 		//fixme: we reset bit4. is this correct??
212ff50d0d1SRudolf Cornelissen 		CRTC2W(HEB, (CRTC2R(HEB) & 0xe0) |
213ff50d0d1SRudolf Cornelissen 			(
214ff50d0d1SRudolf Cornelissen 		 	((htotal & 0x100) >> (8 - 0)) |
215ff50d0d1SRudolf Cornelissen 			((hdisp_e & 0x100) >> (8 - 1)) |
216ff50d0d1SRudolf Cornelissen 			((hblnk_s & 0x100) >> (8 - 2)) |
217ff50d0d1SRudolf Cornelissen 			((hsync_s & 0x100) >> (8 - 3))
218ff50d0d1SRudolf Cornelissen 			));
21908705d96Sshatty 
220ff50d0d1SRudolf Cornelissen 		/* (mostly) vertical extended regs */
221ff50d0d1SRudolf Cornelissen 		CRTC2W(LSR,
222ff50d0d1SRudolf Cornelissen 			(
223ff50d0d1SRudolf Cornelissen 		 	((vtotal & 0x400) >> (10 - 0)) |
224ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x400) >> (10 - 1)) |
225ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x400) >> (10 - 2)) |
226ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x400) >> (10 - 3)) |
227ff50d0d1SRudolf Cornelissen 			((hblnk_e & 0x040) >> (6 - 4))
228ff50d0d1SRudolf Cornelissen 			//fixme: we still miss one linecomp bit!?! is this it??
229ff50d0d1SRudolf Cornelissen 			//| ((linecomp & 0x400) >> 3)
230ff50d0d1SRudolf Cornelissen 			));
23108705d96Sshatty 
232ff50d0d1SRudolf Cornelissen 		/* more vertical extended regs */
233ff50d0d1SRudolf Cornelissen 		CRTC2W(EXTRA,
234ff50d0d1SRudolf Cornelissen 			(
235ff50d0d1SRudolf Cornelissen 		 	((vtotal & 0x800) >> (11 - 0)) |
236ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x800) >> (11 - 2)) |
237ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x800) >> (11 - 4)) |
238ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x800) >> (11 - 6))
239ff50d0d1SRudolf Cornelissen 			//fixme: do we miss another linecomp bit!?!
240ff50d0d1SRudolf Cornelissen 			));
24108705d96Sshatty 
242ff50d0d1SRudolf Cornelissen 		/* setup 'large screen' mode */
243ff50d0d1SRudolf Cornelissen 		if (target.timing.h_display >= 1280)
244ff50d0d1SRudolf Cornelissen 			CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xfb));
24508705d96Sshatty 		else
246ff50d0d1SRudolf Cornelissen 			CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x04));
24708705d96Sshatty 
248ff50d0d1SRudolf Cornelissen 		/* setup HSYNC & VSYNC polarity */
249ff50d0d1SRudolf Cornelissen 		LOG(2,("CRTC2: sync polarity: "));
250255e5021SRudolf Cornelissen 		temp = NV_REG8(NV8_MISCR);
251ff50d0d1SRudolf Cornelissen 		if (target.timing.flags & B_POSITIVE_HSYNC)
252ff50d0d1SRudolf Cornelissen 		{
253ff50d0d1SRudolf Cornelissen 			LOG(2,("H:pos "));
254ff50d0d1SRudolf Cornelissen 			temp &= ~0x40;
25508705d96Sshatty 		}
256ff50d0d1SRudolf Cornelissen 		else
257ff50d0d1SRudolf Cornelissen 		{
258ff50d0d1SRudolf Cornelissen 			LOG(2,("H:neg "));
259ff50d0d1SRudolf Cornelissen 			temp |= 0x40;
260ff50d0d1SRudolf Cornelissen 		}
261ff50d0d1SRudolf Cornelissen 		if (target.timing.flags & B_POSITIVE_VSYNC)
262ff50d0d1SRudolf Cornelissen 		{
263ff50d0d1SRudolf Cornelissen 			LOG(2,("V:pos "));
264ff50d0d1SRudolf Cornelissen 			temp &= ~0x80;
265ff50d0d1SRudolf Cornelissen 		}
266ff50d0d1SRudolf Cornelissen 		else
267ff50d0d1SRudolf Cornelissen 		{
268ff50d0d1SRudolf Cornelissen 			LOG(2,("V:neg "));
269ff50d0d1SRudolf Cornelissen 			temp |= 0x80;
270ff50d0d1SRudolf Cornelissen 		}
271255e5021SRudolf Cornelissen 		NV_REG8(NV8_MISCW) = temp;
272ff50d0d1SRudolf Cornelissen 
273255e5021SRudolf Cornelissen 		LOG(2,(", MISC reg readback: $%02x\n", NV_REG8(NV8_MISCR)));
274ff50d0d1SRudolf Cornelissen 	}
275ff50d0d1SRudolf Cornelissen 
276ff50d0d1SRudolf Cornelissen 	/* always disable interlaced operation */
277255e5021SRudolf Cornelissen 	/* (interlace is supported on upto and including NV10, NV15, and NV30 and up) */
278ff50d0d1SRudolf Cornelissen 	CRTC2W(INTERLACE, 0xff);
27908705d96Sshatty 
2801e37a9acSRudolf Cornelissen 	/* setup flatpanel if connected and active */
281a16d55ddSRudolf Cornelissen 	if (si->ps.tmds2_active)
282a16d55ddSRudolf Cornelissen 	{
283a16d55ddSRudolf Cornelissen 		uint32 iscale_x, iscale_y;
284a16d55ddSRudolf Cornelissen 
285a973fe9eSRudolf Cornelissen 		/* calculate inverse scaling factors used by hardware in 20.12 format */
2860fccffc2SRudolf Cornelissen 		iscale_x = (((1 << 12) * target.timing.h_display) / si->ps.p2_timing.h_display);
2870fccffc2SRudolf Cornelissen 		iscale_y = (((1 << 12) * target.timing.v_display) / si->ps.p2_timing.v_display);
2881e37a9acSRudolf Cornelissen 
2891e37a9acSRudolf Cornelissen 		/* unblock flatpanel timing programming (or something like that..) */
2901e37a9acSRudolf Cornelissen 		CRTC2W(FP_HTIMING, 0);
2911e37a9acSRudolf Cornelissen 		CRTC2W(FP_VTIMING, 0);
292e6708074SRudolf Cornelissen 		LOG(2,("CRTC2: FP_HTIMING reg readback: $%02x\n", CRTC2R(FP_HTIMING)));
293e6708074SRudolf Cornelissen 		LOG(2,("CRTC2: FP_VTIMING reg readback: $%02x\n", CRTC2R(FP_VTIMING)));
2941e37a9acSRudolf Cornelissen 
295a973fe9eSRudolf Cornelissen 		/* enable full width visibility on flatpanel */
296a973fe9eSRudolf Cornelissen 		DAC2W(FP_HVALID_S, 0);
2970fccffc2SRudolf Cornelissen 		DAC2W(FP_HVALID_E, (si->ps.p2_timing.h_display - 1));
298a973fe9eSRudolf Cornelissen 		/* enable full height visibility on flatpanel */
299a973fe9eSRudolf Cornelissen 		DAC2W(FP_VVALID_S, 0);
3000fccffc2SRudolf Cornelissen 		DAC2W(FP_VVALID_E, (si->ps.p2_timing.v_display - 1));
301a973fe9eSRudolf Cornelissen 
3024709c2c8SRudolf Cornelissen 		/* nVidia cards support upscaling except on ??? */
3034709c2c8SRudolf Cornelissen 		/* NV11 cards can upscale after all! */
304e6708074SRudolf Cornelissen 		if (0)//si->ps.card_type == NV11)
3051e37a9acSRudolf Cornelissen 		{
3061e37a9acSRudolf Cornelissen 			/* disable last fetched line limiting */
3071e37a9acSRudolf Cornelissen 			DAC2W(FP_DEBUG2, 0x00000000);
308c567e072SRudolf Cornelissen 			/* inform panel to scale if needed */
309c567e072SRudolf Cornelissen 			if ((iscale_x != (1 << 12)) || (iscale_y != (1 << 12)))
310c567e072SRudolf Cornelissen 			{
311c567e072SRudolf Cornelissen 				LOG(2,("CRTC2: DFP needs to do scaling\n"));
3121e37a9acSRudolf Cornelissen 				DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) | 0x00000100));
3131e37a9acSRudolf Cornelissen 			}
3141e37a9acSRudolf Cornelissen 			else
3151e37a9acSRudolf Cornelissen 			{
316c567e072SRudolf Cornelissen 				LOG(2,("CRTC2: no scaling for DFP needed\n"));
317c567e072SRudolf Cornelissen 				DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) & 0xfffffeff));
318c567e072SRudolf Cornelissen 			}
319c567e072SRudolf Cornelissen 		}
320c567e072SRudolf Cornelissen 		else
321c567e072SRudolf Cornelissen 		{
322a973fe9eSRudolf Cornelissen 			float dm_aspect;
323a973fe9eSRudolf Cornelissen 
324c567e072SRudolf Cornelissen 			LOG(2,("CRTC2: GPU scales for DFP if needed\n"));
3251e37a9acSRudolf Cornelissen 
326a973fe9eSRudolf Cornelissen 			/* calculate display mode aspect */
327a973fe9eSRudolf Cornelissen 			dm_aspect = (target.timing.h_display / ((float)target.timing.v_display));
328a973fe9eSRudolf Cornelissen 
329a16d55ddSRudolf Cornelissen 			/* limit last fetched line if vertical scaling is done */
3301e37a9acSRudolf Cornelissen 			if (iscale_y != (1 << 12))
331a16d55ddSRudolf Cornelissen 				DAC2W(FP_DEBUG2, ((1 << 28) | ((target.timing.v_display - 1) << 16)));
332a16d55ddSRudolf Cornelissen 			else
333a16d55ddSRudolf Cornelissen 				DAC2W(FP_DEBUG2, 0x00000000);
3341e37a9acSRudolf Cornelissen 
3351e37a9acSRudolf Cornelissen 			/* inform panel not to scale */
3361e37a9acSRudolf Cornelissen 			DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) & 0xfffffeff));
337c65998faSRudolf Cornelissen 
338c65998faSRudolf Cornelissen 			/* GPU scaling is automatically setup by hardware, so only modify this
339c65998faSRudolf Cornelissen 			 * scalingfactor for non 4:3 (1.33) aspect panels;
340c65998faSRudolf Cornelissen 			 * let's consider 1280x1024 1:33 aspect (it's 1.25 aspect actually!) */
341c65998faSRudolf Cornelissen 
342a973fe9eSRudolf Cornelissen 			/* correct for widescreen panels relative to mode...
343a973fe9eSRudolf Cornelissen 			 * (so if panel is more widescreen than mode being set) */
344a973fe9eSRudolf Cornelissen 			/* BTW: known widescreen panels:
345c65998faSRudolf Cornelissen 			 * 1280 x  800 (1.60),
346c65998faSRudolf Cornelissen 			 * 1440 x  900 (1.60),
347b97caf33SRudolf Cornelissen 			 * 1680 x 1050 (1.60),
348b97caf33SRudolf Cornelissen 			 * 1920 x 1200 (1.60). */
349c65998faSRudolf Cornelissen 			/* known 4:3 aspect non-standard resolution panels:
350c65998faSRudolf Cornelissen 			 * 1400 x 1050 (1.33). */
351a973fe9eSRudolf Cornelissen 			/* NOTE:
352a973fe9eSRudolf Cornelissen 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
353a973fe9eSRudolf Cornelissen 			if ((iscale_x != (1 << 12)) && (si->ps.panel2_aspect > (dm_aspect + 0.10)))
354c65998faSRudolf Cornelissen 			{
355a973fe9eSRudolf Cornelissen 				uint16 diff;
356a973fe9eSRudolf Cornelissen 
357a973fe9eSRudolf Cornelissen 				LOG(2,("CRTC2: (relative) widescreen panel: tuning horizontal scaling\n"));
358a973fe9eSRudolf Cornelissen 
359a973fe9eSRudolf Cornelissen 				/* X-scaling should be the same as Y-scaling */
360a973fe9eSRudolf Cornelissen 				iscale_x = iscale_y;
361c65998faSRudolf Cornelissen 				/* enable testmode (b12) and program new X-scaling factor */
362c65998faSRudolf Cornelissen 				DAC2W(FP_DEBUG1, (((iscale_x >> 1) & 0x00000fff) | (1 << 12)));
363a973fe9eSRudolf Cornelissen 				/* center/cut-off left and right side of screen */
3640fccffc2SRudolf Cornelissen 				diff = ((si->ps.p2_timing.h_display -
365a973fe9eSRudolf Cornelissen 						(target.timing.h_display * ((1 << 12) / ((float)iscale_x))))
366a973fe9eSRudolf Cornelissen 						/ 2);
367a973fe9eSRudolf Cornelissen 				DAC2W(FP_HVALID_S, diff);
3680fccffc2SRudolf Cornelissen 				DAC2W(FP_HVALID_E, ((si->ps.p2_timing.h_display - diff) - 1));
369c65998faSRudolf Cornelissen 			}
370c65998faSRudolf Cornelissen 			/* correct for portrait panels... */
371a973fe9eSRudolf Cornelissen 			/* NOTE:
372a973fe9eSRudolf Cornelissen 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
373a973fe9eSRudolf Cornelissen 			if ((iscale_y != (1 << 12)) && (si->ps.panel2_aspect < (dm_aspect - 0.10)))
374c65998faSRudolf Cornelissen 			{
375a973fe9eSRudolf Cornelissen 				LOG(2,("CRTC2: (relative) portrait panel: should tune vertical scaling\n"));
376a973fe9eSRudolf Cornelissen 				/* fixme: implement if this kind of portrait panels exist on nVidia... */
377c65998faSRudolf Cornelissen 			}
3781e37a9acSRudolf Cornelissen 		}
3791e37a9acSRudolf Cornelissen 
3801e37a9acSRudolf Cornelissen 		/* do some logging.. */
381a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_HVALID_S reg readback: $%08x\n", DAC2R(FP_HVALID_S)));
382a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_HVALID_E reg readback: $%08x\n", DAC2R(FP_HVALID_E)));
383a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_VVALID_S reg readback: $%08x\n", DAC2R(FP_VVALID_S)));
384a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_VVALID_E reg readback: $%08x\n", DAC2R(FP_VVALID_E)));
3851e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG0 reg readback: $%08x\n", DAC2R(FP_DEBUG0)));
3861e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG1 reg readback: $%08x\n", DAC2R(FP_DEBUG1)));
3871e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG2 reg readback: $%08x\n", DAC2R(FP_DEBUG2)));
3881e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG3 reg readback: $%08x\n", DAC2R(FP_DEBUG3)));
3891e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_TG_CTRL reg readback: $%08x\n", DAC2R(FP_TG_CTRL)));
390a16d55ddSRudolf Cornelissen 	}
391a16d55ddSRudolf Cornelissen 
39208705d96Sshatty 	return B_OK;
39308705d96Sshatty }
39408705d96Sshatty 
395ff50d0d1SRudolf Cornelissen status_t nv_crtc2_depth(int mode)
39608705d96Sshatty {
397ff50d0d1SRudolf Cornelissen 	uint8 viddelay = 0;
398ff50d0d1SRudolf Cornelissen 	uint32 genctrl = 0;
399ff50d0d1SRudolf Cornelissen 
400ff50d0d1SRudolf Cornelissen 	/* set VCLK scaling */
40108705d96Sshatty 	switch(mode)
40208705d96Sshatty 	{
403ff50d0d1SRudolf Cornelissen 	case BPP8:
404ff50d0d1SRudolf Cornelissen 		viddelay = 0x01;
405ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 reset: 'direct mode' */
406ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101100;
40708705d96Sshatty 		break;
408ff50d0d1SRudolf Cornelissen 	case BPP15:
409ff50d0d1SRudolf Cornelissen 		viddelay = 0x02;
410ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
411ff50d0d1SRudolf Cornelissen 		genctrl = 0x00100130;
412ff50d0d1SRudolf Cornelissen 		break;
413ff50d0d1SRudolf Cornelissen 	case BPP16:
414ff50d0d1SRudolf Cornelissen 		viddelay = 0x02;
415ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
416ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101130;
417ff50d0d1SRudolf Cornelissen 		break;
418ff50d0d1SRudolf Cornelissen 	case BPP24:
419ff50d0d1SRudolf Cornelissen 		viddelay = 0x03;
420ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
421ff50d0d1SRudolf Cornelissen 		genctrl = 0x00100130;
422ff50d0d1SRudolf Cornelissen 		break;
423ff50d0d1SRudolf Cornelissen 	case BPP32:
424ff50d0d1SRudolf Cornelissen 		viddelay = 0x03;
425ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
426ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101130;
42708705d96Sshatty 		break;
42808705d96Sshatty 	}
42964c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
43064c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
431255e5021SRudolf Cornelissen 
432ff50d0d1SRudolf Cornelissen 	CRTC2W(PIXEL, ((CRTC2R(PIXEL) & 0xfc) | viddelay));
433ff50d0d1SRudolf Cornelissen 	DAC2W(GENCTRL, genctrl);
43408705d96Sshatty 
43508705d96Sshatty 	return B_OK;
43608705d96Sshatty }
43708705d96Sshatty 
438ff50d0d1SRudolf Cornelissen status_t nv_crtc2_dpms(bool display, bool h, bool v)
43908705d96Sshatty {
440d97178c9SRudolf Cornelissen 	uint8 temp;
441ff50d0d1SRudolf Cornelissen 
442ff50d0d1SRudolf Cornelissen 	LOG(4,("CRTC2: setting DPMS: "));
443ff50d0d1SRudolf Cornelissen 
44464c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
44564c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
446255e5021SRudolf Cornelissen 
447ff50d0d1SRudolf Cornelissen 	/* start synchronous reset: required before turning screen off! */
448d97178c9SRudolf Cornelissen 	SEQW(RESET, 0x01);
449ff50d0d1SRudolf Cornelissen 
450ff50d0d1SRudolf Cornelissen 	/* turn screen off */
451d97178c9SRudolf Cornelissen 	temp = SEQR(CLKMODE);
452ff50d0d1SRudolf Cornelissen 	if (display)
45308705d96Sshatty 	{
454d97178c9SRudolf Cornelissen 		SEQW(CLKMODE, (temp & ~0x20));
455ff50d0d1SRudolf Cornelissen 
456ff50d0d1SRudolf Cornelissen 		/* end synchronous reset if display should be enabled */
457d97178c9SRudolf Cornelissen 		SEQW(RESET, 0x03);
458ff50d0d1SRudolf Cornelissen 
45922ffe8b5SRudolf Cornelissen 		//'safe mode' test! feedback needed with this 'setting'!
46022ffe8b5SRudolf Cornelissen 		if (0)//si->ps.tmds2_active)
461b4f28c26SRudolf Cornelissen 		{
462b4f28c26SRudolf Cornelissen 			/* powerup both LVDS (laptop panellink) and TMDS (DVI panellink)
463b4f28c26SRudolf Cornelissen 			 * internal transmitters... */
4648addb7c3SRudolf Cornelissen 			/* note:
4658addb7c3SRudolf Cornelissen 			 * the powerbits in this register are hardwired to the DVI connectors,
4668addb7c3SRudolf Cornelissen 			 * instead of to the DACs! (confirmed NV34) */
4678addb7c3SRudolf Cornelissen 			//fixme...
468b4f28c26SRudolf Cornelissen 			DAC2W(FP_DEBUG0, (DAC2R(FP_DEBUG0) & 0xcfffffff));
469b4f28c26SRudolf Cornelissen 			/* ... and powerup external TMDS transmitter if it exists */
470ed391abaSRudolf Cornelissen 			/* (confirmed OK on NV28 and NV34) */
471ed391abaSRudolf Cornelissen 			CRTC2W(0x59, (CRTC2R(0x59) | 0x01));
472b4f28c26SRudolf Cornelissen 		}
4734709c2c8SRudolf Cornelissen 
474ff50d0d1SRudolf Cornelissen 		LOG(4,("display on, "));
47508705d96Sshatty 	}
47608705d96Sshatty 	else
47708705d96Sshatty 	{
478d97178c9SRudolf Cornelissen 		SEQW(CLKMODE, (temp | 0x20));
479ff50d0d1SRudolf Cornelissen 
48022ffe8b5SRudolf Cornelissen 		//'safe mode' test! feedback needed with this 'setting'!
48122ffe8b5SRudolf Cornelissen 		if (0)//si->ps.tmds2_active)
482b4f28c26SRudolf Cornelissen 		{
483b4f28c26SRudolf Cornelissen 			/* powerdown both LVDS (laptop panellink) and TMDS (DVI panellink)
484b4f28c26SRudolf Cornelissen 			 * internal transmitters... */
4858addb7c3SRudolf Cornelissen 			/* note:
4868addb7c3SRudolf Cornelissen 			 * the powerbits in this register are hardwired to the DVI connectors,
4878addb7c3SRudolf Cornelissen 			 * instead of to the DACs! (confirmed NV34) */
4888addb7c3SRudolf Cornelissen 			//fixme...
489b4f28c26SRudolf Cornelissen 			DAC2W(FP_DEBUG0, (DAC2R(FP_DEBUG0) | 0x30000000));
490b4f28c26SRudolf Cornelissen 			/* ... and powerdown external TMDS transmitter if it exists */
491ed391abaSRudolf Cornelissen 			/* (confirmed OK on NV28 and NV34) */
492ed391abaSRudolf Cornelissen 			CRTC2W(0x59, (CRTC2R(0x59) & 0xfe));
493b4f28c26SRudolf Cornelissen 		}
4944709c2c8SRudolf Cornelissen 
495ff50d0d1SRudolf Cornelissen 		LOG(4,("display off, "));
49608705d96Sshatty 	}
49708705d96Sshatty 
498ff50d0d1SRudolf Cornelissen 	if (h)
49908705d96Sshatty 	{
500ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0x7f));
501ff50d0d1SRudolf Cornelissen 		LOG(4,("hsync enabled, "));
502ff50d0d1SRudolf Cornelissen 	}
503ff50d0d1SRudolf Cornelissen 	else
504ff50d0d1SRudolf Cornelissen 	{
505ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x80));
506ff50d0d1SRudolf Cornelissen 		LOG(4,("hsync disabled, "));
507ff50d0d1SRudolf Cornelissen 	}
508ff50d0d1SRudolf Cornelissen 	if (v)
509ff50d0d1SRudolf Cornelissen 	{
510ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xbf));
511ff50d0d1SRudolf Cornelissen 		LOG(4,("vsync enabled\n"));
512ff50d0d1SRudolf Cornelissen 	}
513ff50d0d1SRudolf Cornelissen 	else
514ff50d0d1SRudolf Cornelissen 	{
515ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x40));
516ff50d0d1SRudolf Cornelissen 		LOG(4,("vsync disabled\n"));
517ff50d0d1SRudolf Cornelissen 	}
51808705d96Sshatty 
51908705d96Sshatty 	return B_OK;
52008705d96Sshatty }
52108705d96Sshatty 
522ff50d0d1SRudolf Cornelissen status_t nv_crtc2_dpms_fetch(bool *display, bool *h, bool *v)
523ff50d0d1SRudolf Cornelissen {
52464c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
52564c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
526255e5021SRudolf Cornelissen 
527d97178c9SRudolf Cornelissen 	*display = !(SEQR(CLKMODE) & 0x20);
528ff50d0d1SRudolf Cornelissen 	*h = !(CRTC2R(REPAINT1) & 0x80);
529ff50d0d1SRudolf Cornelissen 	*v = !(CRTC2R(REPAINT1) & 0x40);
530ff50d0d1SRudolf Cornelissen 
531ff50d0d1SRudolf Cornelissen 	LOG(4,("CTRC2: fetched DPMS state: "));
532*17f2ecd6SRudolf Cornelissen 	if (*display) LOG(4,("display on, "));
533ff50d0d1SRudolf Cornelissen 	else LOG(4,("display off, "));
534*17f2ecd6SRudolf Cornelissen 	if (*h) LOG(4,("hsync enabled, "));
535ff50d0d1SRudolf Cornelissen 	else LOG(4,("hsync disabled, "));
536*17f2ecd6SRudolf Cornelissen 	if (*v) LOG(4,("vsync enabled\n"));
537ff50d0d1SRudolf Cornelissen 	else LOG(4,("vsync disabled\n"));
538ff50d0d1SRudolf Cornelissen 
539ff50d0d1SRudolf Cornelissen 	return B_OK;
540ff50d0d1SRudolf Cornelissen }
541ff50d0d1SRudolf Cornelissen 
542ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_display_pitch()
54308705d96Sshatty {
54408705d96Sshatty 	uint32 offset;
54508705d96Sshatty 
54608705d96Sshatty 	LOG(4,("CRTC2: setting card pitch (offset between lines)\n"));
54708705d96Sshatty 
54808705d96Sshatty 	/* figure out offset value hardware needs */
549ff50d0d1SRudolf Cornelissen 	offset = si->fbc.bytes_per_row / 8;
55008705d96Sshatty 
551ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: offset register set to: $%04x\n", offset));
55208705d96Sshatty 
55364c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
55464c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
555255e5021SRudolf Cornelissen 
556b4bdc2b6SRudolf Cornelissen 	/* program the card */
557ff50d0d1SRudolf Cornelissen 	CRTC2W(PITCHL, (offset & 0x00ff));
558ff50d0d1SRudolf Cornelissen 	CRTC2W(REPAINT0, ((CRTC2R(REPAINT0) & 0x1f) | ((offset & 0x0700) >> 3)));
559ff50d0d1SRudolf Cornelissen 
56008705d96Sshatty 	return B_OK;
56108705d96Sshatty }
56208705d96Sshatty 
563ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_display_start(uint32 startadd,uint8 bpp)
56408705d96Sshatty {
565e0dd08e8SRudolf Cornelissen 	uint32 timeout = 0;
56608705d96Sshatty 
567ff50d0d1SRudolf Cornelissen 	LOG(4,("CRTC2: setting card RAM to be displayed bpp %d\n", bpp));
56808705d96Sshatty 
569ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: startadd: $%08x\n", startadd));
570ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: frameRAM: $%08x\n", si->framebuffer));
571ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: framebuffer: $%08x\n", si->fbc.frame_buffer));
572ff50d0d1SRudolf Cornelissen 
573e0dd08e8SRudolf Cornelissen 	/* we might have no retraces during setmode! */
574e0dd08e8SRudolf Cornelissen 	/* wait 25mS max. for retrace to occur (refresh > 40Hz) */
575e0dd08e8SRudolf Cornelissen 	while (((NV_REG32(NV32_RASTER2) & 0x000007ff) < si->dm.timing.v_display) &&
576e0dd08e8SRudolf Cornelissen 			(timeout < (25000/10)))
577e0dd08e8SRudolf Cornelissen 	{
578e0dd08e8SRudolf Cornelissen 		/* don't snooze much longer or retrace might get missed! */
579e0dd08e8SRudolf Cornelissen 		snooze(10);
580e0dd08e8SRudolf Cornelissen 		timeout++;
581e0dd08e8SRudolf Cornelissen 	}
582ff50d0d1SRudolf Cornelissen 
58364c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
58464c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
585255e5021SRudolf Cornelissen 
586ff50d0d1SRudolf Cornelissen 	/* upto 4Gb RAM adressing: must be used on NV10 and later! */
587ff50d0d1SRudolf Cornelissen 	/* NOTE:
588ff50d0d1SRudolf Cornelissen 	 * While this register also exists on pre-NV10 cards, it will
589ff50d0d1SRudolf Cornelissen 	 * wrap-around at 16Mb boundaries!! */
590ff50d0d1SRudolf Cornelissen 
591ff50d0d1SRudolf Cornelissen 	/* 30bit adress in 32bit words */
592ff50d0d1SRudolf Cornelissen 	NV_REG32(NV32_NV10FB2STADD32) = (startadd & 0xfffffffc);
593ff50d0d1SRudolf Cornelissen 
594bc9d4aceSRudolf Cornelissen 	/* set byte adress: (b0 - 1) */
595e0dd08e8SRudolf Cornelissen 	ATB2W(HORPIXPAN, ((startadd & 0x00000003) << 1));
596ff50d0d1SRudolf Cornelissen 
597ff50d0d1SRudolf Cornelissen 	return B_OK;
598ff50d0d1SRudolf Cornelissen }
599ff50d0d1SRudolf Cornelissen 
600ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_init()
601ff50d0d1SRudolf Cornelissen {
602ff50d0d1SRudolf Cornelissen 	int i;
603ff50d0d1SRudolf Cornelissen 	uint32 * fb;
604ff50d0d1SRudolf Cornelissen 	/* cursor bitmap will be stored at the start of the framebuffer */
605ff50d0d1SRudolf Cornelissen 	const uint32 curadd = 0;
606ff50d0d1SRudolf Cornelissen 
60764c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
60864c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
609255e5021SRudolf Cornelissen 
610ff50d0d1SRudolf Cornelissen 	/* set cursor bitmap adress ... */
611255e5021SRudolf Cornelissen 	if (si->ps.laptop)
612ff50d0d1SRudolf Cornelissen 	{
613ff50d0d1SRudolf Cornelissen 		/* must be used this way on pre-NV10 and on all 'Go' cards! */
614ff50d0d1SRudolf Cornelissen 
615ff50d0d1SRudolf Cornelissen 		/* cursorbitmap must start on 2Kbyte boundary: */
616ff50d0d1SRudolf Cornelissen 		/* set adress bit11-16, and set 'no doublescan' (registerbit 1 = 0) */
617ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL0, ((curadd & 0x0001f800) >> 9));
618ff50d0d1SRudolf Cornelissen 		/* set adress bit17-23, and set graphics mode cursor(?) (registerbit 7 = 1) */
619ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL1, (((curadd & 0x00fe0000) >> 17) | 0x80));
620ff50d0d1SRudolf Cornelissen 		/* set adress bit24-31 */
621ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL2, ((curadd & 0xff000000) >> 24));
62208705d96Sshatty 	}
62308705d96Sshatty 	else
62408705d96Sshatty 	{
625ff50d0d1SRudolf Cornelissen 		/* upto 4Gb RAM adressing:
626ff50d0d1SRudolf Cornelissen 		 * can be used on NV10 and later (except for 'Go' cards)! */
627ff50d0d1SRudolf Cornelissen 		/* NOTE:
628ff50d0d1SRudolf Cornelissen 		 * This register does not exist on pre-NV10 and 'Go' cards. */
629ff50d0d1SRudolf Cornelissen 
630ff50d0d1SRudolf Cornelissen 		/* cursorbitmap must still start on 2Kbyte boundary: */
631ff50d0d1SRudolf Cornelissen 		NV_REG32(NV32_NV10CUR2ADD32) = (curadd & 0xfffff800);
63208705d96Sshatty 	}
63308705d96Sshatty 
634ff50d0d1SRudolf Cornelissen 	/* set cursor colour: not needed because of direct nature of cursor bitmap. */
635ff50d0d1SRudolf Cornelissen 
636ff50d0d1SRudolf Cornelissen 	/*clear cursor*/
637ff50d0d1SRudolf Cornelissen 	fb = (uint32 *) si->framebuffer + curadd;
638ff50d0d1SRudolf Cornelissen 	for (i=0;i<(2048/4);i++)
639ff50d0d1SRudolf Cornelissen 	{
640ff50d0d1SRudolf Cornelissen 		fb[i]=0;
641ff50d0d1SRudolf Cornelissen 	}
642ff50d0d1SRudolf Cornelissen 
643ff50d0d1SRudolf Cornelissen 	/* select 32x32 pixel, 16bit color cursorbitmap, no doublescan */
644ff50d0d1SRudolf Cornelissen 	NV_REG32(NV32_2CURCONF) = 0x02000100;
645ff50d0d1SRudolf Cornelissen 
646ff50d0d1SRudolf Cornelissen 	/* activate hardware cursor */
647255e5021SRudolf Cornelissen 	nv_crtc2_cursor_show();
648ff50d0d1SRudolf Cornelissen 
649ff50d0d1SRudolf Cornelissen 	return B_OK;
650ff50d0d1SRudolf Cornelissen }
651ff50d0d1SRudolf Cornelissen 
652ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_show()
653ff50d0d1SRudolf Cornelissen {
654255e5021SRudolf Cornelissen 	LOG(4,("CRTC2: enabling cursor\n"));
655255e5021SRudolf Cornelissen 
65664c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
65764c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
658255e5021SRudolf Cornelissen 
659ff50d0d1SRudolf Cornelissen 	/* b0 = 1 enables cursor */
660ff50d0d1SRudolf Cornelissen 	CRTC2W(CURCTL0, (CRTC2R(CURCTL0) | 0x01));
661ff50d0d1SRudolf Cornelissen 
662ff50d0d1SRudolf Cornelissen 	return B_OK;
663ff50d0d1SRudolf Cornelissen }
664ff50d0d1SRudolf Cornelissen 
665ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_hide()
666ff50d0d1SRudolf Cornelissen {
667255e5021SRudolf Cornelissen 	LOG(4,("CRTC2: disabling cursor\n"));
668255e5021SRudolf Cornelissen 
66964c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
67064c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
671255e5021SRudolf Cornelissen 
672ff50d0d1SRudolf Cornelissen 	/* b0 = 0 disables cursor */
673ff50d0d1SRudolf Cornelissen 	CRTC2W(CURCTL0, (CRTC2R(CURCTL0) & 0xfe));
674ff50d0d1SRudolf Cornelissen 
675ff50d0d1SRudolf Cornelissen 	return B_OK;
676ff50d0d1SRudolf Cornelissen }
677ff50d0d1SRudolf Cornelissen 
678ff50d0d1SRudolf Cornelissen /*set up cursor shape*/
679ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_define(uint8* andMask,uint8* xorMask)
680ff50d0d1SRudolf Cornelissen {
681ff50d0d1SRudolf Cornelissen 	int x, y;
682ff50d0d1SRudolf Cornelissen 	uint8 b;
683ff50d0d1SRudolf Cornelissen 	uint16 *cursor;
684ff50d0d1SRudolf Cornelissen 	uint16 pixel;
685ff50d0d1SRudolf Cornelissen 
686ff50d0d1SRudolf Cornelissen 	/* get a pointer to the cursor */
687ff50d0d1SRudolf Cornelissen 	cursor = (uint16*) si->framebuffer;
688ff50d0d1SRudolf Cornelissen 
689ff50d0d1SRudolf Cornelissen 	/* draw the cursor */
690ff50d0d1SRudolf Cornelissen 	/* (Nvidia cards have a RGB15 direct color cursor bitmap, bit #16 is transparancy) */
691ff50d0d1SRudolf Cornelissen 	for (y = 0; y < 16; y++)
692ff50d0d1SRudolf Cornelissen 	{
693ff50d0d1SRudolf Cornelissen 		b = 0x80;
694ff50d0d1SRudolf Cornelissen 		for (x = 0; x < 8; x++)
695ff50d0d1SRudolf Cornelissen 		{
696ff50d0d1SRudolf Cornelissen 			/* preset transparant */
697ff50d0d1SRudolf Cornelissen 			pixel = 0x0000;
698ff50d0d1SRudolf Cornelissen 			/* set white if requested */
699ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
700ff50d0d1SRudolf Cornelissen 			/* set black if requested */
701ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
702ff50d0d1SRudolf Cornelissen 			/* set invert if requested */
703ff50d0d1SRudolf Cornelissen 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
704ff50d0d1SRudolf Cornelissen 			/* place the pixel in the bitmap */
705ff50d0d1SRudolf Cornelissen 			cursor[x + (y * 32)] = pixel;
706ff50d0d1SRudolf Cornelissen 			b >>= 1;
707ff50d0d1SRudolf Cornelissen 		}
708ff50d0d1SRudolf Cornelissen 		xorMask++;
709ff50d0d1SRudolf Cornelissen 		andMask++;
710ff50d0d1SRudolf Cornelissen 		b = 0x80;
711ff50d0d1SRudolf Cornelissen 		for (; x < 16; x++)
712ff50d0d1SRudolf Cornelissen 		{
713ff50d0d1SRudolf Cornelissen 			/* preset transparant */
714ff50d0d1SRudolf Cornelissen 			pixel = 0x0000;
715ff50d0d1SRudolf Cornelissen 			/* set white if requested */
716ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
717ff50d0d1SRudolf Cornelissen 			/* set black if requested */
718ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
719ff50d0d1SRudolf Cornelissen 			/* set invert if requested */
720ff50d0d1SRudolf Cornelissen 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
721ff50d0d1SRudolf Cornelissen 			/* place the pixel in the bitmap */
722ff50d0d1SRudolf Cornelissen 			cursor[x + (y * 32)] = pixel;
723ff50d0d1SRudolf Cornelissen 			b >>= 1;
724ff50d0d1SRudolf Cornelissen 		}
725ff50d0d1SRudolf Cornelissen 		xorMask++;
726ff50d0d1SRudolf Cornelissen 		andMask++;
727ff50d0d1SRudolf Cornelissen 	}
728ff50d0d1SRudolf Cornelissen 
729ff50d0d1SRudolf Cornelissen 	return B_OK;
730ff50d0d1SRudolf Cornelissen }
731ff50d0d1SRudolf Cornelissen 
732ff50d0d1SRudolf Cornelissen /* position the cursor */
733ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_position(uint16 x, uint16 y)
734ff50d0d1SRudolf Cornelissen {
735ff50d0d1SRudolf Cornelissen 	uint16 yhigh;
736ff50d0d1SRudolf Cornelissen 
737ff50d0d1SRudolf Cornelissen 	/* make sure we are beyond the first line of the cursorbitmap being drawn during
738ff50d0d1SRudolf Cornelissen 	 * updating the position to prevent distortions: no double buffering feature */
739ff50d0d1SRudolf Cornelissen 	/* Note:
740ff50d0d1SRudolf Cornelissen 	 * we need to return as quick as possible or some apps will exhibit lagging.. */
741ff50d0d1SRudolf Cornelissen 
742ff50d0d1SRudolf Cornelissen 	/* read the old cursor Y position */
743ff50d0d1SRudolf Cornelissen 	yhigh = ((DAC2R(CURPOS) & 0x0fff0000) >> 16);
744ff50d0d1SRudolf Cornelissen 	/* make sure we will wait until we are below both the old and new Y position:
745ff50d0d1SRudolf Cornelissen 	 * visible cursorbitmap drawing needs to be done at least... */
746ff50d0d1SRudolf Cornelissen 	if (y > yhigh) yhigh = y;
747ff50d0d1SRudolf Cornelissen 
748ff50d0d1SRudolf Cornelissen 	if (yhigh < (si->dm.timing.v_display - 16))
749ff50d0d1SRudolf Cornelissen 	{
750ff50d0d1SRudolf Cornelissen 		/* we have vertical lines below old and new cursorposition to spare. So we
751ff50d0d1SRudolf Cornelissen 		 * update the cursor postion 'mid-screen', but below that area. */
752ff50d0d1SRudolf Cornelissen 		while (((uint16)(NV_REG32(NV32_RASTER2) & 0x000007ff)) < (yhigh + 16))
753ff50d0d1SRudolf Cornelissen 		{
754ff50d0d1SRudolf Cornelissen 			snooze(10);
755ff50d0d1SRudolf Cornelissen 		}
756ff50d0d1SRudolf Cornelissen 	}
757ff50d0d1SRudolf Cornelissen 	else
758ff50d0d1SRudolf Cornelissen 	{
759ff50d0d1SRudolf Cornelissen 		/* no room to spare, just wait for retrace (is relatively slow) */
760ff50d0d1SRudolf Cornelissen 		while ((NV_REG32(NV32_RASTER2) & 0x000007ff) < si->dm.timing.v_display)
761ff50d0d1SRudolf Cornelissen 		{
762ff50d0d1SRudolf Cornelissen 			/* don't snooze much longer or retrace might get missed! */
763ff50d0d1SRudolf Cornelissen 			snooze(10);
764ff50d0d1SRudolf Cornelissen 		}
765ff50d0d1SRudolf Cornelissen 	}
766ff50d0d1SRudolf Cornelissen 
767ff50d0d1SRudolf Cornelissen 	/* update cursorposition */
768ff50d0d1SRudolf Cornelissen 	DAC2W(CURPOS, ((x & 0x0fff) | ((y & 0x0fff) << 16)));
769ff50d0d1SRudolf Cornelissen 
77008705d96Sshatty 	return B_OK;
77108705d96Sshatty }
772