xref: /haiku/src/add-ons/accelerants/nvidia/engine/nv_crtc2.c (revision 8d86b84d18535654da5b693537802d6ca115efe0)
1ff50d0d1SRudolf Cornelissen /* second CTRC functionality for GeForce cards */
2ff50d0d1SRudolf Cornelissen /* Author:
319e5e222SRudolf Cornelissen    Rudolf Cornelissen 11/2002-9/2009
408705d96Sshatty */
508705d96Sshatty 
608705d96Sshatty #define MODULE_BIT 0x00020000
708705d96Sshatty 
808705d96Sshatty #include "nv_std.h"
908705d96Sshatty 
10155a2ad0SRudolf Cornelissen /*
11155a2ad0SRudolf Cornelissen 	Enable/Disable interrupts.  Just a wrapper around the
12155a2ad0SRudolf Cornelissen 	ioctl() to the kernel driver.
13155a2ad0SRudolf Cornelissen */
nv_crtc2_interrupt_enable(bool flag)14155a2ad0SRudolf Cornelissen status_t nv_crtc2_interrupt_enable(bool flag)
15155a2ad0SRudolf Cornelissen {
16155a2ad0SRudolf Cornelissen 	status_t result = B_OK;
17155a2ad0SRudolf Cornelissen 	nv_set_vblank_int svi;
18155a2ad0SRudolf Cornelissen 
19155a2ad0SRudolf Cornelissen 	if (si->ps.int_assigned)
20155a2ad0SRudolf Cornelissen 	{
21155a2ad0SRudolf Cornelissen 		/* set the magic number so the driver knows we're for real */
22155a2ad0SRudolf Cornelissen 		svi.magic = NV_PRIVATE_DATA_MAGIC;
23155a2ad0SRudolf Cornelissen 		svi.crtc = 1;
24155a2ad0SRudolf Cornelissen 		svi.do_it = flag;
25155a2ad0SRudolf Cornelissen 		/* contact driver and get a pointer to the registers and shared data */
26155a2ad0SRudolf Cornelissen 		result = ioctl(fd, NV_RUN_INTERRUPTS, &svi, sizeof(svi));
27155a2ad0SRudolf Cornelissen 	}
28155a2ad0SRudolf Cornelissen 
29155a2ad0SRudolf Cornelissen 	return result;
30155a2ad0SRudolf Cornelissen }
31155a2ad0SRudolf Cornelissen 
32a393eaf8SRudolf Cornelissen /* doing general fail-safe default setup here */
33a393eaf8SRudolf Cornelissen //fixme: this is a _very_ basic setup, and it's preliminary...
nv_crtc2_update_fifo()34a393eaf8SRudolf Cornelissen status_t nv_crtc2_update_fifo()
35a393eaf8SRudolf Cornelissen {
36a393eaf8SRudolf Cornelissen 	uint8 bytes_per_pixel = 1;
37a393eaf8SRudolf Cornelissen 	uint32 drain;
38a393eaf8SRudolf Cornelissen 
39a393eaf8SRudolf Cornelissen 	/* we are only using this on >>coldstarted<< cards which really need this */
40a393eaf8SRudolf Cornelissen 	//fixme: re-enable or remove after general user confirmation of behaviour...
41a393eaf8SRudolf Cornelissen 	if (/*(si->settings.usebios) ||*/ (si->ps.card_type != NV11)) return B_OK;
42a393eaf8SRudolf Cornelissen 
43d320dfafSRudolf Cornelissen 	/* enable access to secondary head */
44a393eaf8SRudolf Cornelissen 	set_crtc_owner(1);
45a393eaf8SRudolf Cornelissen 
46a393eaf8SRudolf Cornelissen 	/* set CRTC FIFO low watermark according to memory drain */
47a393eaf8SRudolf Cornelissen 	switch(si->dm.space)
48a393eaf8SRudolf Cornelissen 	{
49a393eaf8SRudolf Cornelissen 	case B_CMAP8:
50a393eaf8SRudolf Cornelissen 		bytes_per_pixel = 1;
51a393eaf8SRudolf Cornelissen 		break;
52a393eaf8SRudolf Cornelissen 	case B_RGB15_LITTLE:
53a393eaf8SRudolf Cornelissen 	case B_RGB16_LITTLE:
54a393eaf8SRudolf Cornelissen 		bytes_per_pixel = 2;
55a393eaf8SRudolf Cornelissen 		break;
56a393eaf8SRudolf Cornelissen 	case B_RGB24_LITTLE:
57a393eaf8SRudolf Cornelissen 		bytes_per_pixel = 3;
58a393eaf8SRudolf Cornelissen 		break;
59a393eaf8SRudolf Cornelissen 	case B_RGB32_LITTLE:
60a393eaf8SRudolf Cornelissen 		bytes_per_pixel = 4;
61a393eaf8SRudolf Cornelissen 		break;
62a393eaf8SRudolf Cornelissen 	}
63a393eaf8SRudolf Cornelissen 	/* fixme:
64a393eaf8SRudolf Cornelissen 	 * - I should probably include the refreshrate as well;
65a393eaf8SRudolf Cornelissen 	 * - and the memory clocking speed, core clocking speed, RAM buswidth.. */
66a393eaf8SRudolf Cornelissen 	drain = si->dm.timing.h_display * si->dm.timing.v_display * bytes_per_pixel;
67a393eaf8SRudolf Cornelissen 
68a393eaf8SRudolf Cornelissen 	/* Doesn't work for other than 32bit space (yet?) */
69a393eaf8SRudolf Cornelissen 	if (si->dm.space != B_RGB32_LITTLE)
70a393eaf8SRudolf Cornelissen 	{
71a393eaf8SRudolf Cornelissen 		/* BIOS defaults */
72a393eaf8SRudolf Cornelissen 		CRTC2W(FIFO, 0x03);
73a393eaf8SRudolf Cornelissen 		CRTC2W(FIFO_LWM, 0x20);
74a393eaf8SRudolf Cornelissen 		LOG(4,("CRTC2: FIFO low-watermark set to $20, burst size 256 (BIOS defaults)\n"));
75a393eaf8SRudolf Cornelissen 		return B_OK;
76a393eaf8SRudolf Cornelissen 	}
77a393eaf8SRudolf Cornelissen 
78a393eaf8SRudolf Cornelissen 	if (drain > (((uint32)1280) * 1024 * 4))
79a393eaf8SRudolf Cornelissen 	{
80a393eaf8SRudolf Cornelissen 		/* set CRTC FIFO burst size for 'smaller' bursts */
81a393eaf8SRudolf Cornelissen 		CRTC2W(FIFO, 0x01);
82a393eaf8SRudolf Cornelissen 		/* Instruct CRTC to fetch new data 'earlier' */
83a393eaf8SRudolf Cornelissen 		CRTC2W(FIFO_LWM, 0x40);
84a393eaf8SRudolf Cornelissen 		LOG(4,("CRTC2: FIFO low-watermark set to $40, burst size 64\n"));
85a393eaf8SRudolf Cornelissen 	}
86a393eaf8SRudolf Cornelissen 	else
87a393eaf8SRudolf Cornelissen 	{
88a393eaf8SRudolf Cornelissen 		if (drain > (((uint32)1024) * 768 * 4))
89a393eaf8SRudolf Cornelissen 		{
90a393eaf8SRudolf Cornelissen 			/* BIOS default */
91a393eaf8SRudolf Cornelissen 			CRTC2W(FIFO, 0x02);
92a393eaf8SRudolf Cornelissen 			/* Instruct CRTC to fetch new data 'earlier' */
93a393eaf8SRudolf Cornelissen 			CRTC2W(FIFO_LWM, 0x40);
94a393eaf8SRudolf Cornelissen 			LOG(4,("CRTC2: FIFO low-watermark set to $40, burst size 128\n"));
95a393eaf8SRudolf Cornelissen 		}
96a393eaf8SRudolf Cornelissen 		else
97a393eaf8SRudolf Cornelissen 		{
98a393eaf8SRudolf Cornelissen 			/* BIOS defaults */
99a393eaf8SRudolf Cornelissen 			CRTC2W(FIFO, 0x03);
100a393eaf8SRudolf Cornelissen 			CRTC2W(FIFO_LWM, 0x20);
101a393eaf8SRudolf Cornelissen 			LOG(4,("CRTC2: FIFO low-watermark set to $20, burst size 256 (BIOS defaults)\n"));
102a393eaf8SRudolf Cornelissen 		}
103a393eaf8SRudolf Cornelissen 	}
104a393eaf8SRudolf Cornelissen 
105a393eaf8SRudolf Cornelissen 	return B_OK;
106a393eaf8SRudolf Cornelissen }
107a393eaf8SRudolf Cornelissen 
108ff50d0d1SRudolf Cornelissen /* Adjust passed parameters to a valid mode line */
nv_crtc2_validate_timing(uint16 * hd_e,uint16 * hs_s,uint16 * hs_e,uint16 * ht,uint16 * vd_e,uint16 * vs_s,uint16 * vs_e,uint16 * vt)109ff50d0d1SRudolf Cornelissen status_t nv_crtc2_validate_timing(
110ff50d0d1SRudolf Cornelissen 	uint16 *hd_e,uint16 *hs_s,uint16 *hs_e,uint16 *ht,
111ff50d0d1SRudolf Cornelissen 	uint16 *vd_e,uint16 *vs_s,uint16 *vs_e,uint16 *vt
112ff50d0d1SRudolf Cornelissen )
11308705d96Sshatty {
114ff50d0d1SRudolf Cornelissen /* horizontal */
115ff50d0d1SRudolf Cornelissen 	/* make all parameters multiples of 8 */
116ff50d0d1SRudolf Cornelissen 	*hd_e &= 0xfff8;
117ff50d0d1SRudolf Cornelissen 	*hs_s &= 0xfff8;
118ff50d0d1SRudolf Cornelissen 	*hs_e &= 0xfff8;
119ff50d0d1SRudolf Cornelissen 	*ht   &= 0xfff8;
120ff50d0d1SRudolf Cornelissen 
121ff50d0d1SRudolf Cornelissen 	/* confine to required number of bits, taking logic into account */
122ff50d0d1SRudolf Cornelissen 	if (*hd_e > ((0x01ff - 2) << 3)) *hd_e = ((0x01ff - 2) << 3);
123ff50d0d1SRudolf Cornelissen 	if (*hs_s > ((0x01ff - 1) << 3)) *hs_s = ((0x01ff - 1) << 3);
124ff50d0d1SRudolf Cornelissen 	if (*hs_e > ( 0x01ff      << 3)) *hs_e = ( 0x01ff      << 3);
125ff50d0d1SRudolf Cornelissen 	if (*ht   > ((0x01ff + 5) << 3)) *ht   = ((0x01ff + 5) << 3);
126ff50d0d1SRudolf Cornelissen 
127ff50d0d1SRudolf Cornelissen 	/* NOTE: keep horizontal timing at multiples of 8! */
128ff50d0d1SRudolf Cornelissen 	/* confine to a reasonable width */
129ff50d0d1SRudolf Cornelissen 	if (*hd_e < 640) *hd_e = 640;
130ff50d0d1SRudolf Cornelissen 	if (*hd_e > 2048) *hd_e = 2048;
131ff50d0d1SRudolf Cornelissen 
132ff50d0d1SRudolf Cornelissen 	/* if hor. total does not leave room for a sensible sync pulse, increase it! */
133ff50d0d1SRudolf Cornelissen 	if (*ht < (*hd_e + 80)) *ht = (*hd_e + 80);
134ff50d0d1SRudolf Cornelissen 
1350ecea71bSRudolf Cornelissen 	/* if hor. total does not adhere to max. blanking pulse width, decrease it! */
1360ecea71bSRudolf Cornelissen 	if (*ht > (*hd_e + 0x3f8)) *ht = (*hd_e + 0x3f8);
1370ecea71bSRudolf Cornelissen 
138ff50d0d1SRudolf Cornelissen 	/* make sure sync pulse is not during display */
139ff50d0d1SRudolf Cornelissen 	if (*hs_e > (*ht - 8)) *hs_e = (*ht - 8);
140ff50d0d1SRudolf Cornelissen 	if (*hs_s < (*hd_e + 8)) *hs_s = (*hd_e + 8);
141ff50d0d1SRudolf Cornelissen 
142ff50d0d1SRudolf Cornelissen 	/* correct sync pulse if it is too long:
143ff50d0d1SRudolf Cornelissen 	 * there are only 5 bits available to save this in the card registers! */
144ff50d0d1SRudolf Cornelissen 	if (*hs_e > (*hs_s + 0xf8)) *hs_e = (*hs_s + 0xf8);
145ff50d0d1SRudolf Cornelissen 
146ff50d0d1SRudolf Cornelissen /*vertical*/
147ff50d0d1SRudolf Cornelissen 	/* confine to required number of bits, taking logic into account */
148ff50d0d1SRudolf Cornelissen 	//fixme if needed: on GeForce cards there are 12 instead of 11 bits...
149ff50d0d1SRudolf Cornelissen 	if (*vd_e > (0x7ff - 2)) *vd_e = (0x7ff - 2);
150ff50d0d1SRudolf Cornelissen 	if (*vs_s > (0x7ff - 1)) *vs_s = (0x7ff - 1);
151ff50d0d1SRudolf Cornelissen 	if (*vs_e >  0x7ff     ) *vs_e =  0x7ff     ;
152ff50d0d1SRudolf Cornelissen 	if (*vt   > (0x7ff + 2)) *vt   = (0x7ff + 2);
153ff50d0d1SRudolf Cornelissen 
154ff50d0d1SRudolf Cornelissen 	/* confine to a reasonable height */
155ff50d0d1SRudolf Cornelissen 	if (*vd_e < 480) *vd_e = 480;
156ff50d0d1SRudolf Cornelissen 	if (*vd_e > 1536) *vd_e = 1536;
157ff50d0d1SRudolf Cornelissen 
158ff50d0d1SRudolf Cornelissen 	/*if vertical total does not leave room for a sync pulse, increase it!*/
159ff50d0d1SRudolf Cornelissen 	if (*vt < (*vd_e + 3)) *vt = (*vd_e + 3);
160ff50d0d1SRudolf Cornelissen 
1610ecea71bSRudolf Cornelissen 	/* if vert. total does not adhere to max. blanking pulse width, decrease it! */
1620ecea71bSRudolf Cornelissen 	if (*vt > (*vd_e + 0xff)) *vt = (*vd_e + 0xff);
1630ecea71bSRudolf Cornelissen 
164ff50d0d1SRudolf Cornelissen 	/* make sure sync pulse is not during display */
165ff50d0d1SRudolf Cornelissen 	if (*vs_e > (*vt - 1)) *vs_e = (*vt - 1);
166ff50d0d1SRudolf Cornelissen 	if (*vs_s < (*vd_e + 1)) *vs_s = (*vd_e + 1);
167ff50d0d1SRudolf Cornelissen 
168ff50d0d1SRudolf Cornelissen 	/* correct sync pulse if it is too long:
169ff50d0d1SRudolf Cornelissen 	 * there are only 4 bits available to save this in the card registers! */
170ff50d0d1SRudolf Cornelissen 	if (*vs_e > (*vs_s + 0x0f)) *vs_e = (*vs_s + 0x0f);
171ff50d0d1SRudolf Cornelissen 
172ff50d0d1SRudolf Cornelissen 	return B_OK;
173ff50d0d1SRudolf Cornelissen }
174ff50d0d1SRudolf Cornelissen 
175ff50d0d1SRudolf Cornelissen /*set a mode line - inputs are in pixels*/
nv_crtc2_set_timing(display_mode target)176ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_timing(display_mode target)
177ff50d0d1SRudolf Cornelissen {
178ff50d0d1SRudolf Cornelissen 	uint8 temp;
179ff50d0d1SRudolf Cornelissen 
180ff50d0d1SRudolf Cornelissen 	uint32 htotal;		/*total horizontal total VCLKs*/
181ff50d0d1SRudolf Cornelissen 	uint32 hdisp_e;            /*end of horizontal display (begins at 0)*/
182ff50d0d1SRudolf Cornelissen 	uint32 hsync_s;            /*begin of horizontal sync pulse*/
183ff50d0d1SRudolf Cornelissen 	uint32 hsync_e;            /*end of horizontal sync pulse*/
184ff50d0d1SRudolf Cornelissen 	uint32 hblnk_s;            /*begin horizontal blanking*/
185ff50d0d1SRudolf Cornelissen 	uint32 hblnk_e;            /*end horizontal blanking*/
186ff50d0d1SRudolf Cornelissen 
187ff50d0d1SRudolf Cornelissen 	uint32 vtotal;		/*total vertical total scanlines*/
188ff50d0d1SRudolf Cornelissen 	uint32 vdisp_e;            /*end of vertical display*/
189ff50d0d1SRudolf Cornelissen 	uint32 vsync_s;            /*begin of vertical sync pulse*/
190ff50d0d1SRudolf Cornelissen 	uint32 vsync_e;            /*end of vertical sync pulse*/
191ff50d0d1SRudolf Cornelissen 	uint32 vblnk_s;            /*begin vertical blanking*/
192ff50d0d1SRudolf Cornelissen 	uint32 vblnk_e;            /*end vertical blanking*/
193ff50d0d1SRudolf Cornelissen 
194ff50d0d1SRudolf Cornelissen 	uint32 linecomp;	/*split screen and vdisp_e interrupt*/
19508705d96Sshatty 
19608705d96Sshatty 	LOG(4,("CRTC2: setting timing\n"));
19708705d96Sshatty 
198c9210b6fSRudolf Cornelissen 	/* setup tuned internal modeline for flatpanel if connected and active */
1992cb6fc9cSRudolf Cornelissen 	/* notes:
2002cb6fc9cSRudolf Cornelissen 	 * - the CRTC modeline must end earlier than the panel modeline to keep correct
2012cb6fc9cSRudolf Cornelissen 	 *   sync going;
2022cb6fc9cSRudolf Cornelissen 	 * - if the CRTC modeline ends too soon, pixelnoise will occur in 8 (or so) pixel
2032cb6fc9cSRudolf Cornelissen 	 *   wide horizontal stripes. This can be observed earliest on fullscreen overlay,
2042cb6fc9cSRudolf Cornelissen 	 *   and if it gets worse, also normal desktop output will suffer. The stripes
2052cb6fc9cSRudolf Cornelissen 	 *   are mainly visible at the left of the screen, over the entire screen height. */
2068bdea419SRudolf Cornelissen 	if (si->ps.monitors & CRTC2_TMDS)
207c567e072SRudolf Cornelissen 	{
208c567e072SRudolf Cornelissen 		LOG(2,("CRTC2: DFP active: tuning modeline\n"));
209c567e072SRudolf Cornelissen 
210c567e072SRudolf Cornelissen 		/* horizontal timing */
21116fc5a30SRudolf Cornelissen 		target.timing.h_sync_start =
212268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.h_sync_start / ((float)si->ps.p2_timing.h_display)) *
21316fc5a30SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8;
21416fc5a30SRudolf Cornelissen 
21516fc5a30SRudolf Cornelissen 		target.timing.h_sync_end =
216268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.h_sync_end / ((float)si->ps.p2_timing.h_display)) *
21716fc5a30SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8;
21816fc5a30SRudolf Cornelissen 
21916fc5a30SRudolf Cornelissen 		target.timing.h_total =
220268624c4SRudolf Cornelissen 			(((uint16)((si->ps.p2_timing.h_total / ((float)si->ps.p2_timing.h_display)) *
221bef5b86aSRudolf Cornelissen 			target.timing.h_display)) & 0xfff8) - 8;
22216fc5a30SRudolf Cornelissen 
223139d62e9SRudolf Cornelissen 		/* in native mode the CRTC needs some extra time to keep synced correctly;
224139d62e9SRudolf Cornelissen 		 * OTOH the overlay unit distorts if we reserve too much time! */
2257ae8e6dcSRudolf Cornelissen 		if (target.timing.h_display == si->ps.p2_timing.h_display)
22604e6b7ceSRudolf Cornelissen 		{
227139d62e9SRudolf Cornelissen 			/* NV11 timing has different constraints than later cards */
228139d62e9SRudolf Cornelissen 			if (si->ps.card_type == NV11)
2292cb6fc9cSRudolf Cornelissen 				target.timing.h_total -= 56;
230139d62e9SRudolf Cornelissen 			else
231139d62e9SRudolf Cornelissen 				/* confirmed NV34 with 1680x1050 panel */
232139d62e9SRudolf Cornelissen 				target.timing.h_total -= 32;
23304e6b7ceSRudolf Cornelissen 		}
23404e6b7ceSRudolf Cornelissen 
23519e5e222SRudolf Cornelissen 		/* assure sync pulse is at the correct timing position */
23616fc5a30SRudolf Cornelissen 		if (target.timing.h_sync_start == target.timing.h_display)
23716fc5a30SRudolf Cornelissen 			target.timing.h_sync_start += 8;
23816fc5a30SRudolf Cornelissen 		if (target.timing.h_sync_end == target.timing.h_total)
23916fc5a30SRudolf Cornelissen 			target.timing.h_sync_end -= 8;
24019e5e222SRudolf Cornelissen 		/* assure we (still) have a sync pulse */
24119e5e222SRudolf Cornelissen 		if (target.timing.h_sync_start == target.timing.h_sync_end) {
24219e5e222SRudolf Cornelissen 			if (target.timing.h_sync_end < (target.timing.h_total - 8)) {
24319e5e222SRudolf Cornelissen 				target.timing.h_sync_end += 8;
24419e5e222SRudolf Cornelissen 			} else {
24519e5e222SRudolf Cornelissen 				if (target.timing.h_sync_start > (target.timing.h_display + 8)) {
24619e5e222SRudolf Cornelissen 					target.timing.h_sync_start -= 8;
24719e5e222SRudolf Cornelissen 				} else {
24819e5e222SRudolf Cornelissen 					LOG(2,("CRTC2: tuning modeline, not enough room for Hsync pulse, forcing it anyway..\n"));
24919e5e222SRudolf Cornelissen 					target.timing.h_sync_start -= 8;
25019e5e222SRudolf Cornelissen 				}
25119e5e222SRudolf Cornelissen 			}
25219e5e222SRudolf Cornelissen 		}
253c567e072SRudolf Cornelissen 
254c567e072SRudolf Cornelissen 		/* vertical timing */
25516fc5a30SRudolf Cornelissen 		target.timing.v_sync_start =
256268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_sync_start / ((float)si->ps.p2_timing.v_display)) *
25716fc5a30SRudolf Cornelissen 			target.timing.v_display));
25816fc5a30SRudolf Cornelissen 
25916fc5a30SRudolf Cornelissen 		target.timing.v_sync_end =
260268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_sync_end / ((float)si->ps.p2_timing.v_display)) *
26116fc5a30SRudolf Cornelissen 			target.timing.v_display));
26216fc5a30SRudolf Cornelissen 
26316fc5a30SRudolf Cornelissen 		target.timing.v_total =
264268624c4SRudolf Cornelissen 			((uint16)((si->ps.p2_timing.v_total / ((float)si->ps.p2_timing.v_display)) *
265b97caf33SRudolf Cornelissen 			target.timing.v_display)) - 1;
26616fc5a30SRudolf Cornelissen 
26719e5e222SRudolf Cornelissen 		/* assure sync pulse is at the correct timing position */
26816fc5a30SRudolf Cornelissen 		if (target.timing.v_sync_start == target.timing.v_display)
26916fc5a30SRudolf Cornelissen 			target.timing.v_sync_start += 1;
27016fc5a30SRudolf Cornelissen 		if (target.timing.v_sync_end == target.timing.v_total)
27116fc5a30SRudolf Cornelissen 			target.timing.v_sync_end -= 1;
27219e5e222SRudolf Cornelissen 		/* assure we (still) have a sync pulse */
27319e5e222SRudolf Cornelissen 		if (target.timing.v_sync_start == target.timing.v_sync_end) {
27419e5e222SRudolf Cornelissen 			if (target.timing.v_sync_end < (target.timing.v_total - 1)) {
27519e5e222SRudolf Cornelissen 				target.timing.v_sync_end += 1;
27619e5e222SRudolf Cornelissen 			} else {
27719e5e222SRudolf Cornelissen 				if (target.timing.v_sync_start > (target.timing.v_display + 1)) {
27819e5e222SRudolf Cornelissen 					target.timing.v_sync_start -= 1;
27919e5e222SRudolf Cornelissen 				} else {
28019e5e222SRudolf Cornelissen 					LOG(2,("CRTC2: tuning modeline, not enough room for Vsync pulse, forcing it anyway..\n"));
28119e5e222SRudolf Cornelissen 					target.timing.v_sync_start -= 1;
28219e5e222SRudolf Cornelissen 				}
28319e5e222SRudolf Cornelissen 			}
28419e5e222SRudolf Cornelissen 		}
285e6708074SRudolf Cornelissen 
286e6708074SRudolf Cornelissen 		/* disable GPU scaling testmode so automatic scaling will be done */
287e6708074SRudolf Cornelissen 		DAC2W(FP_DEBUG1, 0);
288c567e072SRudolf Cornelissen 	}
289c567e072SRudolf Cornelissen 
290ff50d0d1SRudolf Cornelissen 	/* Modify parameters as required by standard VGA */
291ff50d0d1SRudolf Cornelissen 	htotal = ((target.timing.h_total >> 3) - 5);
292ff50d0d1SRudolf Cornelissen 	hdisp_e = ((target.timing.h_display >> 3) - 1);
293ff50d0d1SRudolf Cornelissen 	hblnk_s = hdisp_e;
294da3804eeSRudolf Cornelissen 	hblnk_e = (htotal + 4);
295ff50d0d1SRudolf Cornelissen 	hsync_s = (target.timing.h_sync_start >> 3);
296ff50d0d1SRudolf Cornelissen 	hsync_e = (target.timing.h_sync_end >> 3);
297ff50d0d1SRudolf Cornelissen 
298ff50d0d1SRudolf Cornelissen 	vtotal = target.timing.v_total - 2;
299ff50d0d1SRudolf Cornelissen 	vdisp_e = target.timing.v_display - 1;
300ff50d0d1SRudolf Cornelissen 	vblnk_s = vdisp_e;
301ff50d0d1SRudolf Cornelissen 	vblnk_e = (vtotal + 1);
302da3804eeSRudolf Cornelissen 	vsync_s = target.timing.v_sync_start;
303da3804eeSRudolf Cornelissen 	vsync_e = target.timing.v_sync_end;
304ff50d0d1SRudolf Cornelissen 
305ff50d0d1SRudolf Cornelissen 	/* prevent memory adress counter from being reset (linecomp may not occur) */
306ff50d0d1SRudolf Cornelissen 	linecomp = target.timing.v_display;
307ff50d0d1SRudolf Cornelissen 
30864c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
30964c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
310255e5021SRudolf Cornelissen 
311a16d55ddSRudolf Cornelissen 	/* Note for laptop and DVI flatpanels:
312a16d55ddSRudolf Cornelissen 	 * CRTC timing has a seperate set of registers from flatpanel timing.
313a16d55ddSRudolf Cornelissen 	 * The flatpanel timing registers have scaling registers that are used to match
314a16d55ddSRudolf Cornelissen 	 * these two modelines. */
31508705d96Sshatty 	{
316a16d55ddSRudolf Cornelissen 		LOG(4,("CRTC2: Setting full timing...\n"));
31708705d96Sshatty 
318ff50d0d1SRudolf Cornelissen 		/* log the mode that will be set */
319ff50d0d1SRudolf 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));
320ff50d0d1SRudolf 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));
32108705d96Sshatty 
322ff50d0d1SRudolf Cornelissen 		/* actually program the card! */
323ff50d0d1SRudolf Cornelissen 		/* unlock CRTC registers at index 0-7 */
324ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCE, (CRTC2R(VSYNCE) & 0x7f));
325ff50d0d1SRudolf Cornelissen 		/* horizontal standard VGA regs */
326ff50d0d1SRudolf Cornelissen 		CRTC2W(HTOTAL, (htotal & 0xff));
327ff50d0d1SRudolf Cornelissen 		CRTC2W(HDISPE, (hdisp_e & 0xff));
328ff50d0d1SRudolf Cornelissen 		CRTC2W(HBLANKS, (hblnk_s & 0xff));
329ff50d0d1SRudolf Cornelissen 		/* also unlock vertical retrace registers in advance */
330ff50d0d1SRudolf Cornelissen 		CRTC2W(HBLANKE, ((hblnk_e & 0x1f) | 0x80));
331ff50d0d1SRudolf Cornelissen 		CRTC2W(HSYNCS, (hsync_s & 0xff));
332ff50d0d1SRudolf Cornelissen 		CRTC2W(HSYNCE, ((hsync_e & 0x1f) | ((hblnk_e & 0x20) << 2)));
33308705d96Sshatty 
334ff50d0d1SRudolf Cornelissen 		/* vertical standard VGA regs */
335ff50d0d1SRudolf Cornelissen 		CRTC2W(VTOTAL, (vtotal & 0xff));
336ff50d0d1SRudolf Cornelissen 		CRTC2W(OVERFLOW,
337ff50d0d1SRudolf Cornelissen 		(
338ff50d0d1SRudolf Cornelissen 			((vtotal & 0x100) >> (8 - 0)) | ((vtotal & 0x200) >> (9 - 5)) |
339ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x100) >> (8 - 1)) | ((vdisp_e & 0x200) >> (9 - 6)) |
340ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x100) >> (8 - 2)) | ((vsync_s & 0x200) >> (9 - 7)) |
341ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x100) >> (8 - 3)) | ((linecomp & 0x100) >> (8 - 4))
342ff50d0d1SRudolf Cornelissen 		));
343ff50d0d1SRudolf Cornelissen 		CRTC2W(PRROWSCN, 0x00); /* not used */
344ff50d0d1SRudolf Cornelissen 		CRTC2W(MAXSCLIN, (((vblnk_s & 0x200) >> (9 - 5)) | ((linecomp & 0x200) >> (9 - 6))));
345ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCS, (vsync_s & 0xff));
346ff50d0d1SRudolf Cornelissen 		CRTC2W(VSYNCE, ((CRTC2R(VSYNCE) & 0xf0) | (vsync_e & 0x0f)));
347ff50d0d1SRudolf Cornelissen 		CRTC2W(VDISPE, (vdisp_e & 0xff));
348ff50d0d1SRudolf Cornelissen 		CRTC2W(VBLANKS, (vblnk_s & 0xff));
349ff50d0d1SRudolf Cornelissen 		CRTC2W(VBLANKE, (vblnk_e & 0xff));
350ff50d0d1SRudolf Cornelissen 		CRTC2W(LINECOMP, (linecomp & 0xff));
35108705d96Sshatty 
352ff50d0d1SRudolf Cornelissen 		/* horizontal extended regs */
353ff50d0d1SRudolf Cornelissen 		//fixme: we reset bit4. is this correct??
354ff50d0d1SRudolf Cornelissen 		CRTC2W(HEB, (CRTC2R(HEB) & 0xe0) |
355ff50d0d1SRudolf Cornelissen 			(
356ff50d0d1SRudolf Cornelissen 		 	((htotal & 0x100) >> (8 - 0)) |
357ff50d0d1SRudolf Cornelissen 			((hdisp_e & 0x100) >> (8 - 1)) |
358ff50d0d1SRudolf Cornelissen 			((hblnk_s & 0x100) >> (8 - 2)) |
359ff50d0d1SRudolf Cornelissen 			((hsync_s & 0x100) >> (8 - 3))
360ff50d0d1SRudolf Cornelissen 			));
36108705d96Sshatty 
362ff50d0d1SRudolf Cornelissen 		/* (mostly) vertical extended regs */
363ff50d0d1SRudolf Cornelissen 		CRTC2W(LSR,
364ff50d0d1SRudolf Cornelissen 			(
365ff50d0d1SRudolf Cornelissen 		 	((vtotal & 0x400) >> (10 - 0)) |
366ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x400) >> (10 - 1)) |
367ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x400) >> (10 - 2)) |
368ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x400) >> (10 - 3)) |
369ff50d0d1SRudolf Cornelissen 			((hblnk_e & 0x040) >> (6 - 4))
370ff50d0d1SRudolf Cornelissen 			//fixme: we still miss one linecomp bit!?! is this it??
371ff50d0d1SRudolf Cornelissen 			//| ((linecomp & 0x400) >> 3)
372ff50d0d1SRudolf Cornelissen 			));
37308705d96Sshatty 
374ff50d0d1SRudolf Cornelissen 		/* more vertical extended regs */
375ff50d0d1SRudolf Cornelissen 		CRTC2W(EXTRA,
376ff50d0d1SRudolf Cornelissen 			(
377ff50d0d1SRudolf Cornelissen 		 	((vtotal & 0x800) >> (11 - 0)) |
378ff50d0d1SRudolf Cornelissen 			((vdisp_e & 0x800) >> (11 - 2)) |
379ff50d0d1SRudolf Cornelissen 			((vsync_s & 0x800) >> (11 - 4)) |
380ff50d0d1SRudolf Cornelissen 			((vblnk_s & 0x800) >> (11 - 6))
381ff50d0d1SRudolf Cornelissen 			//fixme: do we miss another linecomp bit!?!
382ff50d0d1SRudolf Cornelissen 			));
38308705d96Sshatty 
384ff50d0d1SRudolf Cornelissen 		/* setup 'large screen' mode */
385ff50d0d1SRudolf Cornelissen 		if (target.timing.h_display >= 1280)
386ff50d0d1SRudolf Cornelissen 			CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xfb));
38708705d96Sshatty 		else
388ff50d0d1SRudolf Cornelissen 			CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x04));
38908705d96Sshatty 
390ff50d0d1SRudolf Cornelissen 		/* setup HSYNC & VSYNC polarity */
391ff50d0d1SRudolf Cornelissen 		LOG(2,("CRTC2: sync polarity: "));
392255e5021SRudolf Cornelissen 		temp = NV_REG8(NV8_MISCR);
393ff50d0d1SRudolf Cornelissen 		if (target.timing.flags & B_POSITIVE_HSYNC)
394ff50d0d1SRudolf Cornelissen 		{
395ff50d0d1SRudolf Cornelissen 			LOG(2,("H:pos "));
396ff50d0d1SRudolf Cornelissen 			temp &= ~0x40;
39708705d96Sshatty 		}
398ff50d0d1SRudolf Cornelissen 		else
399ff50d0d1SRudolf Cornelissen 		{
400ff50d0d1SRudolf Cornelissen 			LOG(2,("H:neg "));
401ff50d0d1SRudolf Cornelissen 			temp |= 0x40;
402ff50d0d1SRudolf Cornelissen 		}
403ff50d0d1SRudolf Cornelissen 		if (target.timing.flags & B_POSITIVE_VSYNC)
404ff50d0d1SRudolf Cornelissen 		{
405ff50d0d1SRudolf Cornelissen 			LOG(2,("V:pos "));
406ff50d0d1SRudolf Cornelissen 			temp &= ~0x80;
407ff50d0d1SRudolf Cornelissen 		}
408ff50d0d1SRudolf Cornelissen 		else
409ff50d0d1SRudolf Cornelissen 		{
410ff50d0d1SRudolf Cornelissen 			LOG(2,("V:neg "));
411ff50d0d1SRudolf Cornelissen 			temp |= 0x80;
412ff50d0d1SRudolf Cornelissen 		}
413255e5021SRudolf Cornelissen 		NV_REG8(NV8_MISCW) = temp;
414ff50d0d1SRudolf Cornelissen 
415255e5021SRudolf Cornelissen 		LOG(2,(", MISC reg readback: $%02x\n", NV_REG8(NV8_MISCR)));
416ff50d0d1SRudolf Cornelissen 	}
417ff50d0d1SRudolf Cornelissen 
418ff50d0d1SRudolf Cornelissen 	/* always disable interlaced operation */
419255e5021SRudolf Cornelissen 	/* (interlace is supported on upto and including NV10, NV15, and NV30 and up) */
420ff50d0d1SRudolf Cornelissen 	CRTC2W(INTERLACE, 0xff);
42108705d96Sshatty 
422bc9c6041SRudolf Cornelissen 	/* disable CRTC slaved mode unless a panel is in use */
423bc9c6041SRudolf Cornelissen 	// fixme: this kills TVout when it was in use...
4248bdea419SRudolf Cornelissen 	if (!(si->ps.monitors & CRTC2_TMDS)) CRTC2W(PIXEL, (CRTC2R(PIXEL) & 0x7f));
425bc9c6041SRudolf Cornelissen 
4261e37a9acSRudolf Cornelissen 	/* setup flatpanel if connected and active */
4278bdea419SRudolf Cornelissen 	if (si->ps.monitors & CRTC2_TMDS)
428a16d55ddSRudolf Cornelissen 	{
429a16d55ddSRudolf Cornelissen 		uint32 iscale_x, iscale_y;
430a16d55ddSRudolf Cornelissen 
431a973fe9eSRudolf Cornelissen 		/* calculate inverse scaling factors used by hardware in 20.12 format */
4320fccffc2SRudolf Cornelissen 		iscale_x = (((1 << 12) * target.timing.h_display) / si->ps.p2_timing.h_display);
4330fccffc2SRudolf Cornelissen 		iscale_y = (((1 << 12) * target.timing.v_display) / si->ps.p2_timing.v_display);
4341e37a9acSRudolf Cornelissen 
4351e37a9acSRudolf Cornelissen 		/* unblock flatpanel timing programming (or something like that..) */
4361e37a9acSRudolf Cornelissen 		CRTC2W(FP_HTIMING, 0);
4371e37a9acSRudolf Cornelissen 		CRTC2W(FP_VTIMING, 0);
438e6708074SRudolf Cornelissen 		LOG(2,("CRTC2: FP_HTIMING reg readback: $%02x\n", CRTC2R(FP_HTIMING)));
439e6708074SRudolf Cornelissen 		LOG(2,("CRTC2: FP_VTIMING reg readback: $%02x\n", CRTC2R(FP_VTIMING)));
4401e37a9acSRudolf Cornelissen 
441a973fe9eSRudolf Cornelissen 		/* enable full width visibility on flatpanel */
442a973fe9eSRudolf Cornelissen 		DAC2W(FP_HVALID_S, 0);
4430fccffc2SRudolf Cornelissen 		DAC2W(FP_HVALID_E, (si->ps.p2_timing.h_display - 1));
444a973fe9eSRudolf Cornelissen 		/* enable full height visibility on flatpanel */
445a973fe9eSRudolf Cornelissen 		DAC2W(FP_VVALID_S, 0);
4460fccffc2SRudolf Cornelissen 		DAC2W(FP_VVALID_E, (si->ps.p2_timing.v_display - 1));
447a973fe9eSRudolf Cornelissen 
4484709c2c8SRudolf Cornelissen 		/* nVidia cards support upscaling except on ??? */
4494709c2c8SRudolf Cornelissen 		/* NV11 cards can upscale after all! */
450e6708074SRudolf Cornelissen 		if (0)//si->ps.card_type == NV11)
4511e37a9acSRudolf Cornelissen 		{
4521e37a9acSRudolf Cornelissen 			/* disable last fetched line limiting */
4531e37a9acSRudolf Cornelissen 			DAC2W(FP_DEBUG2, 0x00000000);
454c567e072SRudolf Cornelissen 			/* inform panel to scale if needed */
455c567e072SRudolf Cornelissen 			if ((iscale_x != (1 << 12)) || (iscale_y != (1 << 12)))
456c567e072SRudolf Cornelissen 			{
457c567e072SRudolf Cornelissen 				LOG(2,("CRTC2: DFP needs to do scaling\n"));
4581e37a9acSRudolf Cornelissen 				DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) | 0x00000100));
4591e37a9acSRudolf Cornelissen 			}
4601e37a9acSRudolf Cornelissen 			else
4611e37a9acSRudolf Cornelissen 			{
462c567e072SRudolf Cornelissen 				LOG(2,("CRTC2: no scaling for DFP needed\n"));
463c567e072SRudolf Cornelissen 				DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) & 0xfffffeff));
464c567e072SRudolf Cornelissen 			}
465c567e072SRudolf Cornelissen 		}
466c567e072SRudolf Cornelissen 		else
467c567e072SRudolf Cornelissen 		{
468a973fe9eSRudolf Cornelissen 			float dm_aspect;
469a973fe9eSRudolf Cornelissen 
470c567e072SRudolf Cornelissen 			LOG(2,("CRTC2: GPU scales for DFP if needed\n"));
4711e37a9acSRudolf Cornelissen 
472a973fe9eSRudolf Cornelissen 			/* calculate display mode aspect */
473a973fe9eSRudolf Cornelissen 			dm_aspect = (target.timing.h_display / ((float)target.timing.v_display));
474a973fe9eSRudolf Cornelissen 
475a16d55ddSRudolf Cornelissen 			/* limit last fetched line if vertical scaling is done */
4761e37a9acSRudolf Cornelissen 			if (iscale_y != (1 << 12))
477a16d55ddSRudolf Cornelissen 				DAC2W(FP_DEBUG2, ((1 << 28) | ((target.timing.v_display - 1) << 16)));
478a16d55ddSRudolf Cornelissen 			else
479a16d55ddSRudolf Cornelissen 				DAC2W(FP_DEBUG2, 0x00000000);
4801e37a9acSRudolf Cornelissen 
4811e37a9acSRudolf Cornelissen 			/* inform panel not to scale */
4821e37a9acSRudolf Cornelissen 			DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) & 0xfffffeff));
483c65998faSRudolf Cornelissen 
484c65998faSRudolf Cornelissen 			/* GPU scaling is automatically setup by hardware, so only modify this
485c65998faSRudolf Cornelissen 			 * scalingfactor for non 4:3 (1.33) aspect panels;
486c65998faSRudolf Cornelissen 			 * let's consider 1280x1024 1:33 aspect (it's 1.25 aspect actually!) */
487c65998faSRudolf Cornelissen 
488a973fe9eSRudolf Cornelissen 			/* correct for widescreen panels relative to mode...
489a973fe9eSRudolf Cornelissen 			 * (so if panel is more widescreen than mode being set) */
490a973fe9eSRudolf Cornelissen 			/* BTW: known widescreen panels:
491c65998faSRudolf Cornelissen 			 * 1280 x  800 (1.60),
492c65998faSRudolf Cornelissen 			 * 1440 x  900 (1.60),
493b97caf33SRudolf Cornelissen 			 * 1680 x 1050 (1.60),
494b97caf33SRudolf Cornelissen 			 * 1920 x 1200 (1.60). */
495c65998faSRudolf Cornelissen 			/* known 4:3 aspect non-standard resolution panels:
496c65998faSRudolf Cornelissen 			 * 1400 x 1050 (1.33). */
497a973fe9eSRudolf Cornelissen 			/* NOTE:
498a973fe9eSRudolf Cornelissen 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
499c8453f43SRudolf Cornelissen 			if ((iscale_x != (1 << 12)) && (si->ps.crtc2_screen.aspect > (dm_aspect + 0.10)))
500c65998faSRudolf Cornelissen 			{
501a973fe9eSRudolf Cornelissen 				uint16 diff;
502a973fe9eSRudolf Cornelissen 
503a973fe9eSRudolf Cornelissen 				LOG(2,("CRTC2: (relative) widescreen panel: tuning horizontal scaling\n"));
504a973fe9eSRudolf Cornelissen 
505a973fe9eSRudolf Cornelissen 				/* X-scaling should be the same as Y-scaling */
506a973fe9eSRudolf Cornelissen 				iscale_x = iscale_y;
507c65998faSRudolf Cornelissen 				/* enable testmode (b12) and program new X-scaling factor */
508c65998faSRudolf Cornelissen 				DAC2W(FP_DEBUG1, (((iscale_x >> 1) & 0x00000fff) | (1 << 12)));
509a973fe9eSRudolf Cornelissen 				/* center/cut-off left and right side of screen */
5100fccffc2SRudolf Cornelissen 				diff = ((si->ps.p2_timing.h_display -
51179098812SRudolf Cornelissen 						((target.timing.h_display * (1 << 12)) / iscale_x))
512a973fe9eSRudolf Cornelissen 						/ 2);
513a973fe9eSRudolf Cornelissen 				DAC2W(FP_HVALID_S, diff);
5140fccffc2SRudolf Cornelissen 				DAC2W(FP_HVALID_E, ((si->ps.p2_timing.h_display - diff) - 1));
515c65998faSRudolf Cornelissen 			}
516c65998faSRudolf Cornelissen 			/* correct for portrait panels... */
517a973fe9eSRudolf Cornelissen 			/* NOTE:
518a973fe9eSRudolf Cornelissen 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
519c8453f43SRudolf Cornelissen 			if ((iscale_y != (1 << 12)) && (si->ps.crtc2_screen.aspect < (dm_aspect - 0.10)))
520c65998faSRudolf Cornelissen 			{
521a973fe9eSRudolf Cornelissen 				LOG(2,("CRTC2: (relative) portrait panel: should tune vertical scaling\n"));
522a973fe9eSRudolf Cornelissen 				/* fixme: implement if this kind of portrait panels exist on nVidia... */
523c65998faSRudolf Cornelissen 			}
5241e37a9acSRudolf Cornelissen 		}
5251e37a9acSRudolf Cornelissen 
5261e37a9acSRudolf Cornelissen 		/* do some logging.. */
527a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_HVALID_S reg readback: $%08x\n", DAC2R(FP_HVALID_S)));
528a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_HVALID_E reg readback: $%08x\n", DAC2R(FP_HVALID_E)));
529a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_VVALID_S reg readback: $%08x\n", DAC2R(FP_VVALID_S)));
530a973fe9eSRudolf Cornelissen 		LOG(2,("CRTC2: FP_VVALID_E reg readback: $%08x\n", DAC2R(FP_VVALID_E)));
5311e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG0 reg readback: $%08x\n", DAC2R(FP_DEBUG0)));
5321e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG1 reg readback: $%08x\n", DAC2R(FP_DEBUG1)));
5331e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG2 reg readback: $%08x\n", DAC2R(FP_DEBUG2)));
5341e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_DEBUG3 reg readback: $%08x\n", DAC2R(FP_DEBUG3)));
5351e37a9acSRudolf Cornelissen 		LOG(2,("CRTC2: FP_TG_CTRL reg readback: $%08x\n", DAC2R(FP_TG_CTRL)));
536a16d55ddSRudolf Cornelissen 	}
537a16d55ddSRudolf Cornelissen 
53808705d96Sshatty 	return B_OK;
53908705d96Sshatty }
54008705d96Sshatty 
nv_crtc2_depth(int mode)541ff50d0d1SRudolf Cornelissen status_t nv_crtc2_depth(int mode)
54208705d96Sshatty {
543ff50d0d1SRudolf Cornelissen 	uint8 viddelay = 0;
544ff50d0d1SRudolf Cornelissen 	uint32 genctrl = 0;
545ff50d0d1SRudolf Cornelissen 
546ff50d0d1SRudolf Cornelissen 	/* set VCLK scaling */
54708705d96Sshatty 	switch(mode)
54808705d96Sshatty 	{
549ff50d0d1SRudolf Cornelissen 	case BPP8:
550ff50d0d1SRudolf Cornelissen 		viddelay = 0x01;
551ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 reset: 'direct mode' */
552ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101100;
55308705d96Sshatty 		break;
554ff50d0d1SRudolf Cornelissen 	case BPP15:
555ff50d0d1SRudolf Cornelissen 		viddelay = 0x02;
556ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
557ff50d0d1SRudolf Cornelissen 		genctrl = 0x00100130;
558ff50d0d1SRudolf Cornelissen 		break;
559ff50d0d1SRudolf Cornelissen 	case BPP16:
560ff50d0d1SRudolf Cornelissen 		viddelay = 0x02;
561ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
562ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101130;
563ff50d0d1SRudolf Cornelissen 		break;
564ff50d0d1SRudolf Cornelissen 	case BPP24:
565ff50d0d1SRudolf Cornelissen 		viddelay = 0x03;
566ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
567ff50d0d1SRudolf Cornelissen 		genctrl = 0x00100130;
568ff50d0d1SRudolf Cornelissen 		break;
569ff50d0d1SRudolf Cornelissen 	case BPP32:
570ff50d0d1SRudolf Cornelissen 		viddelay = 0x03;
571ff50d0d1SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
572ff50d0d1SRudolf Cornelissen 		genctrl = 0x00101130;
57308705d96Sshatty 		break;
57408705d96Sshatty 	}
57564c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
57664c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
577255e5021SRudolf Cornelissen 
578ff50d0d1SRudolf Cornelissen 	CRTC2W(PIXEL, ((CRTC2R(PIXEL) & 0xfc) | viddelay));
579ff50d0d1SRudolf Cornelissen 	DAC2W(GENCTRL, genctrl);
58008705d96Sshatty 
58108705d96Sshatty 	return B_OK;
58208705d96Sshatty }
58308705d96Sshatty 
nv_crtc2_dpms(bool display,bool h,bool v,bool do_panel)5844022652cSRudolf Cornelissen status_t nv_crtc2_dpms(bool display, bool h, bool v, bool do_panel)
58508705d96Sshatty {
586d97178c9SRudolf Cornelissen 	uint8 temp;
5874022652cSRudolf Cornelissen 	char msg[100];
588ff50d0d1SRudolf Cornelissen 
589*8d86b84dSMurai Takashi 	strlcpy(msg, "CRTC2: setting DPMS: ", sizeof(msg));
590ff50d0d1SRudolf Cornelissen 
59164c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
59264c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
593255e5021SRudolf Cornelissen 
594ff50d0d1SRudolf Cornelissen 	/* start synchronous reset: required before turning screen off! */
595d97178c9SRudolf Cornelissen 	SEQW(RESET, 0x01);
596ff50d0d1SRudolf Cornelissen 
597d97178c9SRudolf Cornelissen 	temp = SEQR(CLKMODE);
598ff50d0d1SRudolf Cornelissen 	if (display)
59908705d96Sshatty 	{
600ecaef637SRudolf Cornelissen 		/* turn screen on */
601d97178c9SRudolf Cornelissen 		SEQW(CLKMODE, (temp & ~0x20));
602ff50d0d1SRudolf Cornelissen 
603ecaef637SRudolf Cornelissen 		/* end synchronous reset because display should be enabled */
604d97178c9SRudolf Cornelissen 		SEQW(RESET, 0x03);
605ff50d0d1SRudolf Cornelissen 
6068bdea419SRudolf Cornelissen 		if (do_panel && (si->ps.monitors & CRTC2_TMDS))
60784cbe0e3SRudolf Cornelissen 		{
60884cbe0e3SRudolf Cornelissen 			if (!si->ps.laptop)
609b4f28c26SRudolf Cornelissen 			{
610ecaef637SRudolf Cornelissen 				/* restore original panelsync and panel-enable */
611ecaef637SRudolf Cornelissen 				uint32 panelsync = 0x00000000;
612ecaef637SRudolf Cornelissen 				if(si->ps.p2_timing.flags & B_POSITIVE_VSYNC) panelsync |= 0x00000001;
613ecaef637SRudolf Cornelissen 				if(si->ps.p2_timing.flags & B_POSITIVE_HSYNC) panelsync |= 0x00000010;
614ecaef637SRudolf Cornelissen 				/* display enable polarity (not an official flag) */
615ecaef637SRudolf Cornelissen 				if(si->ps.p2_timing.flags & B_BLANK_PEDESTAL) panelsync |= 0x10000000;
616ecaef637SRudolf Cornelissen 				DAC2W(FP_TG_CTRL, ((DAC2R(FP_TG_CTRL) & 0xcfffffcc) | panelsync));
617ecaef637SRudolf Cornelissen 
618ecaef637SRudolf Cornelissen 				//fixme?: looks like we don't need this after all:
619b4f28c26SRudolf Cornelissen 				/* powerup both LVDS (laptop panellink) and TMDS (DVI panellink)
620b4f28c26SRudolf Cornelissen 				 * internal transmitters... */
6218addb7c3SRudolf Cornelissen 				/* note:
6228addb7c3SRudolf Cornelissen 				 * the powerbits in this register are hardwired to the DVI connectors,
6238addb7c3SRudolf Cornelissen 				 * instead of to the DACs! (confirmed NV34) */
6248addb7c3SRudolf Cornelissen 				//fixme...
625ecaef637SRudolf Cornelissen 				//DAC2W(FP_DEBUG0, (DAC2R(FP_DEBUG0) & 0xcfffffff));
626b4f28c26SRudolf Cornelissen 				/* ... and powerup external TMDS transmitter if it exists */
627ed391abaSRudolf Cornelissen 				/* (confirmed OK on NV28 and NV34) */
628ecaef637SRudolf Cornelissen 				//CRTC2W(0x59, (CRTC2R(0x59) | 0x01));
6294022652cSRudolf Cornelissen 
630*8d86b84dSMurai Takashi 				strlcat(msg, "(panel-)", sizeof(msg));
631b4f28c26SRudolf Cornelissen 			}
63284cbe0e3SRudolf Cornelissen 			else
63384cbe0e3SRudolf Cornelissen 			{
63484cbe0e3SRudolf Cornelissen 				//fixme: see if LVDS head can be determined with two panels there...
6358bdea419SRudolf Cornelissen 				if (!(si->ps.monitors & CRTC1_TMDS) && (si->ps.card_type != NV11))
63684cbe0e3SRudolf Cornelissen 				{
63784cbe0e3SRudolf Cornelissen 					/* b2 = 0 = enable laptop panel backlight */
63884cbe0e3SRudolf Cornelissen 					/* note: this seems to be a write-only register. */
63984cbe0e3SRudolf Cornelissen 					NV_REG32(NV32_LVDS_PWR) = 0x00000003;
64084cbe0e3SRudolf Cornelissen 
641*8d86b84dSMurai Takashi 					strlcat(msg, "(panel-)", sizeof(msg));
64284cbe0e3SRudolf Cornelissen 				}
64384cbe0e3SRudolf Cornelissen 			}
64484cbe0e3SRudolf Cornelissen 		}
6454709c2c8SRudolf Cornelissen 
646*8d86b84dSMurai Takashi 		strlcat(msg, "display on, ", sizeof(msg));
64708705d96Sshatty 	}
64808705d96Sshatty 	else
64908705d96Sshatty 	{
650ecaef637SRudolf Cornelissen 		/* turn screen off */
651d97178c9SRudolf Cornelissen 		SEQW(CLKMODE, (temp | 0x20));
652ff50d0d1SRudolf Cornelissen 
6538bdea419SRudolf Cornelissen 		if (do_panel && (si->ps.monitors & CRTC2_TMDS))
65484cbe0e3SRudolf Cornelissen 		{
65584cbe0e3SRudolf Cornelissen 			if (!si->ps.laptop)
656b4f28c26SRudolf Cornelissen 			{
657ecaef637SRudolf Cornelissen 				/* shutoff panelsync and disable panel */
658ecaef637SRudolf Cornelissen 				DAC2W(FP_TG_CTRL, ((DAC2R(FP_TG_CTRL) & 0xcfffffcc) | 0x20000022));
659ecaef637SRudolf Cornelissen 
660ecaef637SRudolf Cornelissen 				//fixme?: looks like we don't need this after all:
661b4f28c26SRudolf Cornelissen 				/* powerdown both LVDS (laptop panellink) and TMDS (DVI panellink)
662b4f28c26SRudolf Cornelissen 				 * internal transmitters... */
6638addb7c3SRudolf Cornelissen 				/* note:
6648addb7c3SRudolf Cornelissen 				 * the powerbits in this register are hardwired to the DVI connectors,
6658addb7c3SRudolf Cornelissen 				 * instead of to the DACs! (confirmed NV34) */
6668addb7c3SRudolf Cornelissen 				//fixme...
667ecaef637SRudolf Cornelissen 				//DAC2W(FP_DEBUG0, (DAC2R(FP_DEBUG0) | 0x30000000));
668b4f28c26SRudolf Cornelissen 				/* ... and powerdown external TMDS transmitter if it exists */
669ed391abaSRudolf Cornelissen 				/* (confirmed OK on NV28 and NV34) */
670ecaef637SRudolf Cornelissen 				//CRTC2W(0x59, (CRTC2R(0x59) & 0xfe));
6714022652cSRudolf Cornelissen 
672*8d86b84dSMurai Takashi 				strlcat(msg, "(panel-)", sizeof(msg));
673b4f28c26SRudolf Cornelissen 			}
67484cbe0e3SRudolf Cornelissen 			else
67584cbe0e3SRudolf Cornelissen 			{
67684cbe0e3SRudolf Cornelissen 				//fixme: see if LVDS head can be determined with two panels there...
6778bdea419SRudolf Cornelissen 				if (!(si->ps.monitors & CRTC1_TMDS) && (si->ps.card_type != NV11))
67884cbe0e3SRudolf Cornelissen 				{
67984cbe0e3SRudolf Cornelissen 					/* b2 = 1 = disable laptop panel backlight */
68084cbe0e3SRudolf Cornelissen 					/* note: this seems to be a write-only register. */
68184cbe0e3SRudolf Cornelissen 					NV_REG32(NV32_LVDS_PWR) = 0x00000007;
68284cbe0e3SRudolf Cornelissen 
683*8d86b84dSMurai Takashi 					strlcat(msg, "(panel-)", sizeof(msg));
68484cbe0e3SRudolf Cornelissen 				}
68584cbe0e3SRudolf Cornelissen 			}
68684cbe0e3SRudolf Cornelissen 		}
6874709c2c8SRudolf Cornelissen 
688*8d86b84dSMurai Takashi 		strlcat(msg, "display off, ", sizeof(msg));
68908705d96Sshatty 	}
69008705d96Sshatty 
691ff50d0d1SRudolf Cornelissen 	if (h)
69208705d96Sshatty 	{
693ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0x7f));
694*8d86b84dSMurai Takashi 		strlcat(msg, "hsync enabled, ", sizeof(msg));
695ff50d0d1SRudolf Cornelissen 	}
696ff50d0d1SRudolf Cornelissen 	else
697ff50d0d1SRudolf Cornelissen 	{
698ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x80));
699*8d86b84dSMurai Takashi 		strlcat(msg, "hsync disabled, ", sizeof(msg));
700ff50d0d1SRudolf Cornelissen 	}
701ff50d0d1SRudolf Cornelissen 	if (v)
702ff50d0d1SRudolf Cornelissen 	{
703ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xbf));
704*8d86b84dSMurai Takashi 		strlcat(msg, "vsync enabled\n", sizeof(msg));
705ff50d0d1SRudolf Cornelissen 	}
706ff50d0d1SRudolf Cornelissen 	else
707ff50d0d1SRudolf Cornelissen 	{
708ff50d0d1SRudolf Cornelissen 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x40));
709*8d86b84dSMurai Takashi 		strlcat(msg, "vsync disabled\n", sizeof(msg));
710ff50d0d1SRudolf Cornelissen 	}
71108705d96Sshatty 
7124022652cSRudolf Cornelissen 	LOG(4, (msg));
7134022652cSRudolf Cornelissen 
71408705d96Sshatty 	return B_OK;
71508705d96Sshatty }
71608705d96Sshatty 
nv_crtc2_set_display_pitch()717ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_display_pitch()
71808705d96Sshatty {
71908705d96Sshatty 	uint32 offset;
72008705d96Sshatty 
72108705d96Sshatty 	LOG(4,("CRTC2: setting card pitch (offset between lines)\n"));
72208705d96Sshatty 
72308705d96Sshatty 	/* figure out offset value hardware needs */
724ff50d0d1SRudolf Cornelissen 	offset = si->fbc.bytes_per_row / 8;
72508705d96Sshatty 
726ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: offset register set to: $%04x\n", offset));
72708705d96Sshatty 
72864c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
72964c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
730255e5021SRudolf Cornelissen 
731b4bdc2b6SRudolf Cornelissen 	/* program the card */
732ff50d0d1SRudolf Cornelissen 	CRTC2W(PITCHL, (offset & 0x00ff));
733ff50d0d1SRudolf Cornelissen 	CRTC2W(REPAINT0, ((CRTC2R(REPAINT0) & 0x1f) | ((offset & 0x0700) >> 3)));
734ff50d0d1SRudolf Cornelissen 
73508705d96Sshatty 	return B_OK;
73608705d96Sshatty }
73708705d96Sshatty 
nv_crtc2_set_display_start(uint32 startadd,uint8 bpp)738ff50d0d1SRudolf Cornelissen status_t nv_crtc2_set_display_start(uint32 startadd,uint8 bpp)
73908705d96Sshatty {
740e0dd08e8SRudolf Cornelissen 	uint32 timeout = 0;
74108705d96Sshatty 
742ff50d0d1SRudolf Cornelissen 	LOG(4,("CRTC2: setting card RAM to be displayed bpp %d\n", bpp));
74308705d96Sshatty 
744ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: startadd: $%08x\n", startadd));
745ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: frameRAM: $%08x\n", si->framebuffer));
746ff50d0d1SRudolf Cornelissen 	LOG(2,("CRTC2: framebuffer: $%08x\n", si->fbc.frame_buffer));
747ff50d0d1SRudolf Cornelissen 
748e0dd08e8SRudolf Cornelissen 	/* we might have no retraces during setmode! */
749e0dd08e8SRudolf Cornelissen 	/* wait 25mS max. for retrace to occur (refresh > 40Hz) */
750e0dd08e8SRudolf Cornelissen 	while (((NV_REG32(NV32_RASTER2) & 0x000007ff) < si->dm.timing.v_display) &&
751e0dd08e8SRudolf Cornelissen 			(timeout < (25000/10)))
752e0dd08e8SRudolf Cornelissen 	{
753e0dd08e8SRudolf Cornelissen 		/* don't snooze much longer or retrace might get missed! */
754e0dd08e8SRudolf Cornelissen 		snooze(10);
755e0dd08e8SRudolf Cornelissen 		timeout++;
756e0dd08e8SRudolf Cornelissen 	}
757ff50d0d1SRudolf Cornelissen 
75864c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
75964c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
760255e5021SRudolf Cornelissen 
761ff50d0d1SRudolf Cornelissen 	/* upto 4Gb RAM adressing: must be used on NV10 and later! */
762ff50d0d1SRudolf Cornelissen 	/* NOTE:
763ff50d0d1SRudolf Cornelissen 	 * While this register also exists on pre-NV10 cards, it will
764ff50d0d1SRudolf Cornelissen 	 * wrap-around at 16Mb boundaries!! */
765ff50d0d1SRudolf Cornelissen 
766ff50d0d1SRudolf Cornelissen 	/* 30bit adress in 32bit words */
767ff50d0d1SRudolf Cornelissen 	NV_REG32(NV32_NV10FB2STADD32) = (startadd & 0xfffffffc);
768ff50d0d1SRudolf Cornelissen 
769bc9d4aceSRudolf Cornelissen 	/* set byte adress: (b0 - 1) */
770e0dd08e8SRudolf Cornelissen 	ATB2W(HORPIXPAN, ((startadd & 0x00000003) << 1));
771ff50d0d1SRudolf Cornelissen 
772ff50d0d1SRudolf Cornelissen 	return B_OK;
773ff50d0d1SRudolf Cornelissen }
774ff50d0d1SRudolf Cornelissen 
nv_crtc2_cursor_init()775ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_init()
776ff50d0d1SRudolf Cornelissen {
777ff50d0d1SRudolf Cornelissen 	int i;
778eab3aa0cSRudolf Cornelissen 	vuint32 * fb;
779ff50d0d1SRudolf Cornelissen 	/* cursor bitmap will be stored at the start of the framebuffer */
780ff50d0d1SRudolf Cornelissen 	const uint32 curadd = 0;
781ff50d0d1SRudolf Cornelissen 
78264c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
78364c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
784255e5021SRudolf Cornelissen 
785ff50d0d1SRudolf Cornelissen 	/* set cursor bitmap adress ... */
786255e5021SRudolf Cornelissen 	if (si->ps.laptop)
787ff50d0d1SRudolf Cornelissen 	{
788ff50d0d1SRudolf Cornelissen 		/* must be used this way on pre-NV10 and on all 'Go' cards! */
789ff50d0d1SRudolf Cornelissen 
790ff50d0d1SRudolf Cornelissen 		/* cursorbitmap must start on 2Kbyte boundary: */
791ff50d0d1SRudolf Cornelissen 		/* set adress bit11-16, and set 'no doublescan' (registerbit 1 = 0) */
792ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL0, ((curadd & 0x0001f800) >> 9));
793ff50d0d1SRudolf Cornelissen 		/* set adress bit17-23, and set graphics mode cursor(?) (registerbit 7 = 1) */
794ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL1, (((curadd & 0x00fe0000) >> 17) | 0x80));
795ff50d0d1SRudolf Cornelissen 		/* set adress bit24-31 */
796ff50d0d1SRudolf Cornelissen 		CRTC2W(CURCTL2, ((curadd & 0xff000000) >> 24));
79708705d96Sshatty 	}
79808705d96Sshatty 	else
79908705d96Sshatty 	{
800ff50d0d1SRudolf Cornelissen 		/* upto 4Gb RAM adressing:
801ff50d0d1SRudolf Cornelissen 		 * can be used on NV10 and later (except for 'Go' cards)! */
802ff50d0d1SRudolf Cornelissen 		/* NOTE:
803ff50d0d1SRudolf Cornelissen 		 * This register does not exist on pre-NV10 and 'Go' cards. */
804ff50d0d1SRudolf Cornelissen 
805ff50d0d1SRudolf Cornelissen 		/* cursorbitmap must still start on 2Kbyte boundary: */
806ff50d0d1SRudolf Cornelissen 		NV_REG32(NV32_NV10CUR2ADD32) = (curadd & 0xfffff800);
80708705d96Sshatty 	}
80808705d96Sshatty 
809ff50d0d1SRudolf Cornelissen 	/* set cursor colour: not needed because of direct nature of cursor bitmap. */
810ff50d0d1SRudolf Cornelissen 
811ff50d0d1SRudolf Cornelissen 	/*clear cursor*/
812eab3aa0cSRudolf Cornelissen 	fb = (vuint32 *) si->framebuffer + curadd;
813ff50d0d1SRudolf Cornelissen 	for (i=0;i<(2048/4);i++)
814ff50d0d1SRudolf Cornelissen 	{
815ff50d0d1SRudolf Cornelissen 		fb[i]=0;
816ff50d0d1SRudolf Cornelissen 	}
817ff50d0d1SRudolf Cornelissen 
818ff50d0d1SRudolf Cornelissen 	/* select 32x32 pixel, 16bit color cursorbitmap, no doublescan */
819ff50d0d1SRudolf Cornelissen 	NV_REG32(NV32_2CURCONF) = 0x02000100;
820ff50d0d1SRudolf Cornelissen 
821df7dbd1dSRudolf Cornelissen 	/* activate hardware-sync between cursor updates and vertical retrace */
822df7dbd1dSRudolf Cornelissen 	DAC2W(NV10_CURSYNC, (DAC2R(NV10_CURSYNC) | 0x02000000));
823df7dbd1dSRudolf Cornelissen 
824ff50d0d1SRudolf Cornelissen 	/* activate hardware cursor */
825255e5021SRudolf Cornelissen 	nv_crtc2_cursor_show();
826ff50d0d1SRudolf Cornelissen 
827ff50d0d1SRudolf Cornelissen 	return B_OK;
828ff50d0d1SRudolf Cornelissen }
829ff50d0d1SRudolf Cornelissen 
nv_crtc2_cursor_show()830ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_show()
831ff50d0d1SRudolf Cornelissen {
832255e5021SRudolf Cornelissen 	LOG(4,("CRTC2: enabling cursor\n"));
833255e5021SRudolf Cornelissen 
83464c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
83564c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
836255e5021SRudolf Cornelissen 
837ff50d0d1SRudolf Cornelissen 	/* b0 = 1 enables cursor */
838ff50d0d1SRudolf Cornelissen 	CRTC2W(CURCTL0, (CRTC2R(CURCTL0) | 0x01));
839ff50d0d1SRudolf Cornelissen 
84011058c2fSRudolf Cornelissen 	/* workaround for hardware bug confirmed existing on NV43:
84111058c2fSRudolf Cornelissen 	 * Cursor visibility is not updated without a position update if its hardware
84211058c2fSRudolf Cornelissen 	 * retrace sync is enabled. */
84311058c2fSRudolf Cornelissen 	if (si->ps.card_arch == NV40A) DAC2W(CURPOS, (DAC2R(CURPOS)));
84411058c2fSRudolf Cornelissen 
845ff50d0d1SRudolf Cornelissen 	return B_OK;
846ff50d0d1SRudolf Cornelissen }
847ff50d0d1SRudolf Cornelissen 
nv_crtc2_cursor_hide()848ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_hide()
849ff50d0d1SRudolf Cornelissen {
850255e5021SRudolf Cornelissen 	LOG(4,("CRTC2: disabling cursor\n"));
851255e5021SRudolf Cornelissen 
85264c14e7eSRudolf Cornelissen 	/* enable access to secondary head */
85364c14e7eSRudolf Cornelissen 	set_crtc_owner(1);
854255e5021SRudolf Cornelissen 
855ff50d0d1SRudolf Cornelissen 	/* b0 = 0 disables cursor */
856ff50d0d1SRudolf Cornelissen 	CRTC2W(CURCTL0, (CRTC2R(CURCTL0) & 0xfe));
857ff50d0d1SRudolf Cornelissen 
85811058c2fSRudolf Cornelissen 	/* workaround for hardware bug confirmed existing on NV43:
85911058c2fSRudolf Cornelissen 	 * Cursor visibility is not updated without a position update if its hardware
86011058c2fSRudolf Cornelissen 	 * retrace sync is enabled. */
86111058c2fSRudolf Cornelissen 	if (si->ps.card_arch == NV40A) DAC2W(CURPOS, (DAC2R(CURPOS)));
86211058c2fSRudolf Cornelissen 
863ff50d0d1SRudolf Cornelissen 	return B_OK;
864ff50d0d1SRudolf Cornelissen }
865ff50d0d1SRudolf Cornelissen 
866ff50d0d1SRudolf Cornelissen /*set up cursor shape*/
nv_crtc2_cursor_define(uint8 * andMask,uint8 * xorMask)867ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_define(uint8* andMask,uint8* xorMask)
868ff50d0d1SRudolf Cornelissen {
869ff50d0d1SRudolf Cornelissen 	int x, y;
870ff50d0d1SRudolf Cornelissen 	uint8 b;
871eab3aa0cSRudolf Cornelissen 	vuint16 *cursor;
872ff50d0d1SRudolf Cornelissen 	uint16 pixel;
873ff50d0d1SRudolf Cornelissen 
874ff50d0d1SRudolf Cornelissen 	/* get a pointer to the cursor */
875eab3aa0cSRudolf Cornelissen 	cursor = (vuint16*) si->framebuffer;
876ff50d0d1SRudolf Cornelissen 
877ff50d0d1SRudolf Cornelissen 	/* draw the cursor */
878ff50d0d1SRudolf Cornelissen 	/* (Nvidia cards have a RGB15 direct color cursor bitmap, bit #16 is transparancy) */
879ff50d0d1SRudolf Cornelissen 	for (y = 0; y < 16; y++)
880ff50d0d1SRudolf Cornelissen 	{
881ff50d0d1SRudolf Cornelissen 		b = 0x80;
882ff50d0d1SRudolf Cornelissen 		for (x = 0; x < 8; x++)
883ff50d0d1SRudolf Cornelissen 		{
884ff50d0d1SRudolf Cornelissen 			/* preset transparant */
885ff50d0d1SRudolf Cornelissen 			pixel = 0x0000;
886ff50d0d1SRudolf Cornelissen 			/* set white if requested */
887ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
888ff50d0d1SRudolf Cornelissen 			/* set black if requested */
889ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
890ff50d0d1SRudolf Cornelissen 			/* set invert if requested */
891ff50d0d1SRudolf Cornelissen 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
892ff50d0d1SRudolf Cornelissen 			/* place the pixel in the bitmap */
893ff50d0d1SRudolf Cornelissen 			cursor[x + (y * 32)] = pixel;
894ff50d0d1SRudolf Cornelissen 			b >>= 1;
895ff50d0d1SRudolf Cornelissen 		}
896ff50d0d1SRudolf Cornelissen 		xorMask++;
897ff50d0d1SRudolf Cornelissen 		andMask++;
898ff50d0d1SRudolf Cornelissen 		b = 0x80;
899ff50d0d1SRudolf Cornelissen 		for (; x < 16; x++)
900ff50d0d1SRudolf Cornelissen 		{
901ff50d0d1SRudolf Cornelissen 			/* preset transparant */
902ff50d0d1SRudolf Cornelissen 			pixel = 0x0000;
903ff50d0d1SRudolf Cornelissen 			/* set white if requested */
904ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
905ff50d0d1SRudolf Cornelissen 			/* set black if requested */
906ff50d0d1SRudolf Cornelissen 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
907ff50d0d1SRudolf Cornelissen 			/* set invert if requested */
908ff50d0d1SRudolf Cornelissen 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
909ff50d0d1SRudolf Cornelissen 			/* place the pixel in the bitmap */
910ff50d0d1SRudolf Cornelissen 			cursor[x + (y * 32)] = pixel;
911ff50d0d1SRudolf Cornelissen 			b >>= 1;
912ff50d0d1SRudolf Cornelissen 		}
913ff50d0d1SRudolf Cornelissen 		xorMask++;
914ff50d0d1SRudolf Cornelissen 		andMask++;
915ff50d0d1SRudolf Cornelissen 	}
916ff50d0d1SRudolf Cornelissen 
917ff50d0d1SRudolf Cornelissen 	return B_OK;
918ff50d0d1SRudolf Cornelissen }
919ff50d0d1SRudolf Cornelissen 
920ff50d0d1SRudolf Cornelissen /* position the cursor */
nv_crtc2_cursor_position(uint16 x,uint16 y)921ff50d0d1SRudolf Cornelissen status_t nv_crtc2_cursor_position(uint16 x, uint16 y)
922ff50d0d1SRudolf Cornelissen {
9230b7b8998SRudolf Cornelissen 	/* the cursor position is updated during retrace by card hardware */
924ff50d0d1SRudolf Cornelissen 
925ff50d0d1SRudolf Cornelissen 	/* update cursorposition */
926ff50d0d1SRudolf Cornelissen 	DAC2W(CURPOS, ((x & 0x0fff) | ((y & 0x0fff) << 16)));
927ff50d0d1SRudolf Cornelissen 
92808705d96Sshatty 	return B_OK;
92908705d96Sshatty }
93091731297SRudolf Cornelissen 
nv_crtc2_stop_tvout(void)93191731297SRudolf Cornelissen status_t nv_crtc2_stop_tvout(void)
93291731297SRudolf Cornelissen {
93396cc3084SRudolf Cornelissen 	uint16 cnt;
93496cc3084SRudolf Cornelissen 
935d7dfe68dSRudolf Cornelissen 	LOG(4,("CRTC2: stopping TV output\n"));
936d7dfe68dSRudolf Cornelissen 
93791731297SRudolf Cornelissen 	/* enable access to secondary head */
93891731297SRudolf Cornelissen 	set_crtc_owner(1);
93991731297SRudolf Cornelissen 
94091731297SRudolf Cornelissen 	/* just to be sure Vsync is _really_ enabled */
94191731297SRudolf Cornelissen 	CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xbf));
94291731297SRudolf Cornelissen 
94391731297SRudolf Cornelissen 	/* wait for one image to be generated to make sure VGA has kicked in and is
94491731297SRudolf Cornelissen 	 * running OK before continuing...
94591731297SRudolf Cornelissen 	 * (Kicking in will fail often if we do not wait here) */
94691731297SRudolf Cornelissen 	/* Note:
94791731297SRudolf Cornelissen 	 * The used CRTC's Vsync is required to be enabled here. The DPMS state
94891731297SRudolf Cornelissen 	 * programming in the driver makes sure this is the case.
94991731297SRudolf Cornelissen 	 * (except for driver startup: see nv_general.c.) */
95091731297SRudolf Cornelissen 
95196cc3084SRudolf Cornelissen 	/* make sure we are 'in' active VGA picture: wait with timeout! */
95296cc3084SRudolf Cornelissen 	cnt = 1;
95396cc3084SRudolf Cornelissen 	while ((NV_REG8(NV8_INSTAT1) & 0x08) && cnt)
95496cc3084SRudolf Cornelissen 	{
95596cc3084SRudolf Cornelissen 		snooze(1);
95696cc3084SRudolf Cornelissen 		cnt++;
95796cc3084SRudolf Cornelissen 	}
95896cc3084SRudolf Cornelissen 	/* wait for next vertical retrace start on VGA: wait with timeout! */
95996cc3084SRudolf Cornelissen 	cnt = 1;
96096cc3084SRudolf Cornelissen 	while ((!(NV_REG8(NV8_INSTAT1) & 0x08)) && cnt)
96196cc3084SRudolf Cornelissen 	{
96296cc3084SRudolf Cornelissen 		snooze(1);
96396cc3084SRudolf Cornelissen 		cnt++;
96496cc3084SRudolf Cornelissen 	}
96596cc3084SRudolf Cornelissen 	/* now wait until we are 'in' active VGA picture again: wait with timeout! */
96696cc3084SRudolf Cornelissen 	cnt = 1;
96796cc3084SRudolf Cornelissen 	while ((NV_REG8(NV8_INSTAT1) & 0x08) && cnt)
96896cc3084SRudolf Cornelissen 	{
96996cc3084SRudolf Cornelissen 		snooze(1);
97096cc3084SRudolf Cornelissen 		cnt++;
97196cc3084SRudolf Cornelissen 	}
97291731297SRudolf Cornelissen 
97391731297SRudolf Cornelissen 	/* set CRTC to master mode (b7 = 0) if it wasn't slaved for a panel before */
97491731297SRudolf Cornelissen 	if (!(si->ps.slaved_tmds2))	CRTC2W(PIXEL, (CRTC2R(PIXEL) & 0x03));
97591731297SRudolf Cornelissen 
97691731297SRudolf Cornelissen 	/* CAUTION:
97791731297SRudolf Cornelissen 	 * On old cards, PLLSEL (and TV_SETUP?) cannot be read (sometimes?), but
97891731297SRudolf Cornelissen 	 * write actions do succeed ...
97991731297SRudolf Cornelissen 	 * This is confirmed for both ISA and PCI access, on NV04 and NV11. */
98091731297SRudolf Cornelissen 
98191731297SRudolf Cornelissen 	/* setup TVencoder connection */
98291731297SRudolf Cornelissen 	/* b1-0 = %00: encoder type is SLAVE;
98391731297SRudolf Cornelissen 	 * b24 = 1: VIP datapos is b0-7 */
98491731297SRudolf Cornelissen 	//fixme if needed: setup completely instead of relying on pre-init by BIOS..
98591731297SRudolf Cornelissen 	//(it seems to work OK on NV04 and NV11 although read reg. doesn't seem to work)
98691731297SRudolf Cornelissen 	DAC2W(TV_SETUP, ((DAC2R(TV_SETUP) & ~0x00000003) | 0x01000000));
98791731297SRudolf Cornelissen 
98891731297SRudolf Cornelissen 	/* tell GPU to use pixelclock from internal source instead of using TVencoder */
98991731297SRudolf Cornelissen 	DACW(PLLSEL, 0x30000f00);
99091731297SRudolf Cornelissen 
99191731297SRudolf Cornelissen 	/* HTOTAL, VTOTAL and OVERFLOW return their default CRTC use, instead of
99291731297SRudolf Cornelissen 	 * H, V-low and V-high 'shadow' counters(?)(b0, 4 and 6 = 0) (b7 use = unknown) */
99391731297SRudolf Cornelissen 	CRTC2W(TREG, 0x00);
99491731297SRudolf Cornelissen 
99591731297SRudolf Cornelissen 	/* select panel encoder, not TV encoder if needed (b0 = 1).
99691731297SRudolf Cornelissen 	 * Note:
99791731297SRudolf Cornelissen 	 * Both are devices (often) using the CRTC in slaved mode. */
99891731297SRudolf Cornelissen 	if (si->ps.slaved_tmds2) CRTC2W(LCD, (CRTC2R(LCD) | 0x01));
99991731297SRudolf Cornelissen 
100091731297SRudolf Cornelissen 	return B_OK;
100191731297SRudolf Cornelissen }
100291731297SRudolf Cornelissen 
nv_crtc2_start_tvout(void)100391731297SRudolf Cornelissen status_t nv_crtc2_start_tvout(void)
100491731297SRudolf Cornelissen {
1005d7dfe68dSRudolf Cornelissen 	LOG(4,("CRTC2: starting TV output\n"));
1006d7dfe68dSRudolf Cornelissen 
1007a658603aSRudolf Cornelissen 	/* switch TV encoder to CRTC2 */
1008a658603aSRudolf Cornelissen 	NV_REG32(NV32_FUNCSEL) &= ~0x00000100;
1009a658603aSRudolf Cornelissen 	NV_REG32(NV32_2FUNCSEL) |= 0x00000100;
1010a658603aSRudolf Cornelissen 
101191731297SRudolf Cornelissen 	/* enable access to secondary head */
101291731297SRudolf Cornelissen 	set_crtc_owner(1);
101391731297SRudolf Cornelissen 
101491731297SRudolf Cornelissen 	/* CAUTION:
101591731297SRudolf Cornelissen 	 * On old cards, PLLSEL (and TV_SETUP?) cannot be read (sometimes?), but
101691731297SRudolf Cornelissen 	 * write actions do succeed ...
101791731297SRudolf Cornelissen 	 * This is confirmed for both ISA and PCI access, on NV04 and NV11. */
101891731297SRudolf Cornelissen 
101991731297SRudolf Cornelissen 	/* setup TVencoder connection */
102091731297SRudolf Cornelissen 	/* b1-0 = %01: encoder type is MASTER;
102191731297SRudolf Cornelissen 	 * b24 = 1: VIP datapos is b0-7 */
102291731297SRudolf Cornelissen 	//fixme if needed: setup completely instead of relying on pre-init by BIOS..
102391731297SRudolf Cornelissen 	//(it seems to work OK on NV04 and NV11 although read reg. doesn't seem to work)
102491731297SRudolf Cornelissen 	DAC2W(TV_SETUP, ((DAC2R(TV_SETUP) & ~0x00000002) | 0x01000001));
102591731297SRudolf Cornelissen 
102691731297SRudolf Cornelissen 	/* tell GPU to use pixelclock from TVencoder instead of using internal source */
102791731297SRudolf Cornelissen 	/* (nessecary or display will 'shiver' on both TV and VGA.) */
102891731297SRudolf Cornelissen 	DACW(PLLSEL, 0x100c0f00);
102991731297SRudolf Cornelissen 
103091731297SRudolf Cornelissen 	/* Set overscan color to 'black' */
103191731297SRudolf Cornelissen 	/* note:
103291731297SRudolf Cornelissen 	 * Change this instruction for a visible overscan color if you're trying to
103391731297SRudolf Cornelissen 	 * center the output on TV. Use it as a guide-'line' then ;-) */
103491731297SRudolf Cornelissen 	ATB2W(OSCANCOLOR, 0x00);
103591731297SRudolf Cornelissen 
103691731297SRudolf Cornelissen 	/* set CRTC to slaved mode (b7 = 1) and clear TVadjust (b3-5 = %000) */
103791731297SRudolf Cornelissen 	CRTC2W(PIXEL, ((CRTC2R(PIXEL) & 0xc7) | 0x80));
103891731297SRudolf Cornelissen 	/* select TV encoder, not panel encoder (b0 = 0).
103991731297SRudolf Cornelissen 	 * Note:
104091731297SRudolf Cornelissen 	 * Both are devices (often) using the CRTC in slaved mode. */
104191731297SRudolf Cornelissen 	CRTC2W(LCD, (CRTC2R(LCD) & 0xfe));
104291731297SRudolf Cornelissen 
104391731297SRudolf Cornelissen 	/* HTOTAL, VTOTAL and OVERFLOW return their default CRTC use, instead of
104491731297SRudolf Cornelissen 	 * H, V-low and V-high 'shadow' counters(?)(b0, 4 and 6 = 0) (b7 use = unknown) */
104591731297SRudolf Cornelissen 	CRTC2W(TREG, 0x80);
104691731297SRudolf Cornelissen 
104791731297SRudolf Cornelissen 	return B_OK;
104891731297SRudolf Cornelissen }
1049