xref: /haiku/src/add-ons/accelerants/nvidia/engine/nv_crtc2.c (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 /* second CTRC functionality for GeForce cards */
2 /* Author:
3    Rudolf Cornelissen 11/2002-11/2005
4 */
5 
6 #define MODULE_BIT 0x00020000
7 
8 #include "nv_std.h"
9 
10 /* doing general fail-safe default setup here */
11 //fixme: this is a _very_ basic setup, and it's preliminary...
12 status_t nv_crtc2_update_fifo()
13 {
14 	uint8 bytes_per_pixel = 1;
15 	uint32 drain;
16 
17 	/* we are only using this on >>coldstarted<< cards which really need this */
18 	//fixme: re-enable or remove after general user confirmation of behaviour...
19 	if (/*(si->settings.usebios) ||*/ (si->ps.card_type != NV11)) return B_OK;
20 
21 	/* enable access to secondary head */
22 	set_crtc_owner(1);
23 
24 	/* set CRTC FIFO low watermark according to memory drain */
25 	switch(si->dm.space)
26 	{
27 	case B_CMAP8:
28 		bytes_per_pixel = 1;
29 		break;
30 	case B_RGB15_LITTLE:
31 	case B_RGB16_LITTLE:
32 		bytes_per_pixel = 2;
33 		break;
34 	case B_RGB24_LITTLE:
35 		bytes_per_pixel = 3;
36 		break;
37 	case B_RGB32_LITTLE:
38 		bytes_per_pixel = 4;
39 		break;
40 	}
41 	/* fixme:
42 	 * - I should probably include the refreshrate as well;
43 	 * - and the memory clocking speed, core clocking speed, RAM buswidth.. */
44 	drain = si->dm.timing.h_display * si->dm.timing.v_display * bytes_per_pixel;
45 
46 	/* Doesn't work for other than 32bit space (yet?) */
47 	if (si->dm.space != B_RGB32_LITTLE)
48 	{
49 		/* BIOS defaults */
50 		CRTC2W(FIFO, 0x03);
51 		CRTC2W(FIFO_LWM, 0x20);
52 		LOG(4,("CRTC2: FIFO low-watermark set to $20, burst size 256 (BIOS defaults)\n"));
53 		return B_OK;
54 	}
55 
56 	if (drain > (((uint32)1280) * 1024 * 4))
57 	{
58 		/* set CRTC FIFO burst size for 'smaller' bursts */
59 		CRTC2W(FIFO, 0x01);
60 		/* Instruct CRTC to fetch new data 'earlier' */
61 		CRTC2W(FIFO_LWM, 0x40);
62 		LOG(4,("CRTC2: FIFO low-watermark set to $40, burst size 64\n"));
63 	}
64 	else
65 	{
66 		if (drain > (((uint32)1024) * 768 * 4))
67 		{
68 			/* BIOS default */
69 			CRTC2W(FIFO, 0x02);
70 			/* Instruct CRTC to fetch new data 'earlier' */
71 			CRTC2W(FIFO_LWM, 0x40);
72 			LOG(4,("CRTC2: FIFO low-watermark set to $40, burst size 128\n"));
73 		}
74 		else
75 		{
76 			/* BIOS defaults */
77 			CRTC2W(FIFO, 0x03);
78 			CRTC2W(FIFO_LWM, 0x20);
79 			LOG(4,("CRTC2: FIFO low-watermark set to $20, burst size 256 (BIOS defaults)\n"));
80 		}
81 	}
82 
83 	return B_OK;
84 }
85 
86 /* Adjust passed parameters to a valid mode line */
87 status_t nv_crtc2_validate_timing(
88 	uint16 *hd_e,uint16 *hs_s,uint16 *hs_e,uint16 *ht,
89 	uint16 *vd_e,uint16 *vs_s,uint16 *vs_e,uint16 *vt
90 )
91 {
92 /* horizontal */
93 	/* make all parameters multiples of 8 */
94 	*hd_e &= 0xfff8;
95 	*hs_s &= 0xfff8;
96 	*hs_e &= 0xfff8;
97 	*ht   &= 0xfff8;
98 
99 	/* confine to required number of bits, taking logic into account */
100 	if (*hd_e > ((0x01ff - 2) << 3)) *hd_e = ((0x01ff - 2) << 3);
101 	if (*hs_s > ((0x01ff - 1) << 3)) *hs_s = ((0x01ff - 1) << 3);
102 	if (*hs_e > ( 0x01ff      << 3)) *hs_e = ( 0x01ff      << 3);
103 	if (*ht   > ((0x01ff + 5) << 3)) *ht   = ((0x01ff + 5) << 3);
104 
105 	/* NOTE: keep horizontal timing at multiples of 8! */
106 	/* confine to a reasonable width */
107 	if (*hd_e < 640) *hd_e = 640;
108 	if (*hd_e > 2048) *hd_e = 2048;
109 
110 	/* if hor. total does not leave room for a sensible sync pulse, increase it! */
111 	if (*ht < (*hd_e + 80)) *ht = (*hd_e + 80);
112 
113 	/* if hor. total does not adhere to max. blanking pulse width, decrease it! */
114 	if (*ht > (*hd_e + 0x3f8)) *ht = (*hd_e + 0x3f8);
115 
116 	/* make sure sync pulse is not during display */
117 	if (*hs_e > (*ht - 8)) *hs_e = (*ht - 8);
118 	if (*hs_s < (*hd_e + 8)) *hs_s = (*hd_e + 8);
119 
120 	/* correct sync pulse if it is too long:
121 	 * there are only 5 bits available to save this in the card registers! */
122 	if (*hs_e > (*hs_s + 0xf8)) *hs_e = (*hs_s + 0xf8);
123 
124 /*vertical*/
125 	/* confine to required number of bits, taking logic into account */
126 	//fixme if needed: on GeForce cards there are 12 instead of 11 bits...
127 	if (*vd_e > (0x7ff - 2)) *vd_e = (0x7ff - 2);
128 	if (*vs_s > (0x7ff - 1)) *vs_s = (0x7ff - 1);
129 	if (*vs_e >  0x7ff     ) *vs_e =  0x7ff     ;
130 	if (*vt   > (0x7ff + 2)) *vt   = (0x7ff + 2);
131 
132 	/* confine to a reasonable height */
133 	if (*vd_e < 480) *vd_e = 480;
134 	if (*vd_e > 1536) *vd_e = 1536;
135 
136 	/*if vertical total does not leave room for a sync pulse, increase it!*/
137 	if (*vt < (*vd_e + 3)) *vt = (*vd_e + 3);
138 
139 	/* if vert. total does not adhere to max. blanking pulse width, decrease it! */
140 	if (*vt > (*vd_e + 0xff)) *vt = (*vd_e + 0xff);
141 
142 	/* make sure sync pulse is not during display */
143 	if (*vs_e > (*vt - 1)) *vs_e = (*vt - 1);
144 	if (*vs_s < (*vd_e + 1)) *vs_s = (*vd_e + 1);
145 
146 	/* correct sync pulse if it is too long:
147 	 * there are only 4 bits available to save this in the card registers! */
148 	if (*vs_e > (*vs_s + 0x0f)) *vs_e = (*vs_s + 0x0f);
149 
150 	return B_OK;
151 }
152 
153 /*set a mode line - inputs are in pixels*/
154 status_t nv_crtc2_set_timing(display_mode target)
155 {
156 	uint8 temp;
157 
158 	uint32 htotal;		/*total horizontal total VCLKs*/
159 	uint32 hdisp_e;            /*end of horizontal display (begins at 0)*/
160 	uint32 hsync_s;            /*begin of horizontal sync pulse*/
161 	uint32 hsync_e;            /*end of horizontal sync pulse*/
162 	uint32 hblnk_s;            /*begin horizontal blanking*/
163 	uint32 hblnk_e;            /*end horizontal blanking*/
164 
165 	uint32 vtotal;		/*total vertical total scanlines*/
166 	uint32 vdisp_e;            /*end of vertical display*/
167 	uint32 vsync_s;            /*begin of vertical sync pulse*/
168 	uint32 vsync_e;            /*end of vertical sync pulse*/
169 	uint32 vblnk_s;            /*begin vertical blanking*/
170 	uint32 vblnk_e;            /*end vertical blanking*/
171 
172 	uint32 linecomp;	/*split screen and vdisp_e interrupt*/
173 
174 	LOG(4,("CRTC2: setting timing\n"));
175 
176 	/* setup tuned internal modeline for flatpanel if connected and active */
177 	/* notes:
178 	 * - the CRTC modeline must end earlier than the panel modeline to keep correct
179 	 *   sync going;
180 	 * - if the CRTC modeline ends too soon, pixelnoise will occur in 8 (or so) pixel
181 	 *   wide horizontal stripes. This can be observed earliest on fullscreen overlay,
182 	 *   and if it gets worse, also normal desktop output will suffer. The stripes
183 	 *   are mainly visible at the left of the screen, over the entire screen height. */
184 	if (si->ps.tmds2_active)
185 	{
186 		LOG(2,("CRTC2: DFP active: tuning modeline\n"));
187 
188 		/* horizontal timing */
189 		target.timing.h_sync_start =
190 			((uint16)((si->ps.p2_timing.h_sync_start / ((float)si->ps.p2_timing.h_display)) *
191 			target.timing.h_display)) & 0xfff8;
192 
193 		target.timing.h_sync_end =
194 			((uint16)((si->ps.p2_timing.h_sync_end / ((float)si->ps.p2_timing.h_display)) *
195 			target.timing.h_display)) & 0xfff8;
196 
197 		target.timing.h_total =
198 			(((uint16)((si->ps.p2_timing.h_total / ((float)si->ps.p2_timing.h_display)) *
199 			target.timing.h_display)) & 0xfff8) - 8;
200 
201 		/* in native mode the CRTC needs some extra time to keep synced correctly;
202 		 * OTOH the overlay unit distorts if we reserve too much time! */
203 		if (target.timing.h_display == si->ps.p2_timing.h_display)
204 		{
205 			/* NV11 timing has different constraints than later cards */
206 			if (si->ps.card_type == NV11)
207 				target.timing.h_total -= 56;
208 			else
209 				/* confirmed NV34 with 1680x1050 panel */
210 				target.timing.h_total -= 32;
211 		}
212 
213 		if (target.timing.h_sync_start == target.timing.h_display)
214 			target.timing.h_sync_start += 8;
215 		if (target.timing.h_sync_end == target.timing.h_total)
216 			target.timing.h_sync_end -= 8;
217 
218 		/* vertical timing */
219 		target.timing.v_sync_start =
220 			((uint16)((si->ps.p2_timing.v_sync_start / ((float)si->ps.p2_timing.v_display)) *
221 			target.timing.v_display));
222 
223 		target.timing.v_sync_end =
224 			((uint16)((si->ps.p2_timing.v_sync_end / ((float)si->ps.p2_timing.v_display)) *
225 			target.timing.v_display));
226 
227 		target.timing.v_total =
228 			((uint16)((si->ps.p2_timing.v_total / ((float)si->ps.p2_timing.v_display)) *
229 			target.timing.v_display)) - 1;
230 
231 		if (target.timing.v_sync_start == target.timing.v_display)
232 			target.timing.v_sync_start += 1;
233 		if (target.timing.v_sync_end == target.timing.v_total)
234 			target.timing.v_sync_end -= 1;
235 
236 		/* disable GPU scaling testmode so automatic scaling will be done */
237 		DAC2W(FP_DEBUG1, 0);
238 	}
239 
240 	/* Modify parameters as required by standard VGA */
241 	htotal = ((target.timing.h_total >> 3) - 5);
242 	hdisp_e = ((target.timing.h_display >> 3) - 1);
243 	hblnk_s = hdisp_e;
244 	hblnk_e = (htotal + 4);
245 	hsync_s = (target.timing.h_sync_start >> 3);
246 	hsync_e = (target.timing.h_sync_end >> 3);
247 
248 	vtotal = target.timing.v_total - 2;
249 	vdisp_e = target.timing.v_display - 1;
250 	vblnk_s = vdisp_e;
251 	vblnk_e = (vtotal + 1);
252 	vsync_s = target.timing.v_sync_start;
253 	vsync_e = target.timing.v_sync_end;
254 
255 	/* prevent memory adress counter from being reset (linecomp may not occur) */
256 	linecomp = target.timing.v_display;
257 
258 	/* enable access to secondary head */
259 	set_crtc_owner(1);
260 
261 	/* Note for laptop and DVI flatpanels:
262 	 * CRTC timing has a seperate set of registers from flatpanel timing.
263 	 * The flatpanel timing registers have scaling registers that are used to match
264 	 * these two modelines. */
265 	{
266 		LOG(4,("CRTC2: Setting full timing...\n"));
267 
268 		/* log the mode that will be set */
269 		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));
270 		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));
271 
272 		/* actually program the card! */
273 		/* unlock CRTC registers at index 0-7 */
274 		CRTC2W(VSYNCE, (CRTC2R(VSYNCE) & 0x7f));
275 		/* horizontal standard VGA regs */
276 		CRTC2W(HTOTAL, (htotal & 0xff));
277 		CRTC2W(HDISPE, (hdisp_e & 0xff));
278 		CRTC2W(HBLANKS, (hblnk_s & 0xff));
279 		/* also unlock vertical retrace registers in advance */
280 		CRTC2W(HBLANKE, ((hblnk_e & 0x1f) | 0x80));
281 		CRTC2W(HSYNCS, (hsync_s & 0xff));
282 		CRTC2W(HSYNCE, ((hsync_e & 0x1f) | ((hblnk_e & 0x20) << 2)));
283 
284 		/* vertical standard VGA regs */
285 		CRTC2W(VTOTAL, (vtotal & 0xff));
286 		CRTC2W(OVERFLOW,
287 		(
288 			((vtotal & 0x100) >> (8 - 0)) | ((vtotal & 0x200) >> (9 - 5)) |
289 			((vdisp_e & 0x100) >> (8 - 1)) | ((vdisp_e & 0x200) >> (9 - 6)) |
290 			((vsync_s & 0x100) >> (8 - 2)) | ((vsync_s & 0x200) >> (9 - 7)) |
291 			((vblnk_s & 0x100) >> (8 - 3)) | ((linecomp & 0x100) >> (8 - 4))
292 		));
293 		CRTC2W(PRROWSCN, 0x00); /* not used */
294 		CRTC2W(MAXSCLIN, (((vblnk_s & 0x200) >> (9 - 5)) | ((linecomp & 0x200) >> (9 - 6))));
295 		CRTC2W(VSYNCS, (vsync_s & 0xff));
296 		CRTC2W(VSYNCE, ((CRTC2R(VSYNCE) & 0xf0) | (vsync_e & 0x0f)));
297 		CRTC2W(VDISPE, (vdisp_e & 0xff));
298 		CRTC2W(VBLANKS, (vblnk_s & 0xff));
299 		CRTC2W(VBLANKE, (vblnk_e & 0xff));
300 		CRTC2W(LINECOMP, (linecomp & 0xff));
301 
302 		/* horizontal extended regs */
303 		//fixme: we reset bit4. is this correct??
304 		CRTC2W(HEB, (CRTC2R(HEB) & 0xe0) |
305 			(
306 		 	((htotal & 0x100) >> (8 - 0)) |
307 			((hdisp_e & 0x100) >> (8 - 1)) |
308 			((hblnk_s & 0x100) >> (8 - 2)) |
309 			((hsync_s & 0x100) >> (8 - 3))
310 			));
311 
312 		/* (mostly) vertical extended regs */
313 		CRTC2W(LSR,
314 			(
315 		 	((vtotal & 0x400) >> (10 - 0)) |
316 			((vdisp_e & 0x400) >> (10 - 1)) |
317 			((vsync_s & 0x400) >> (10 - 2)) |
318 			((vblnk_s & 0x400) >> (10 - 3)) |
319 			((hblnk_e & 0x040) >> (6 - 4))
320 			//fixme: we still miss one linecomp bit!?! is this it??
321 			//| ((linecomp & 0x400) >> 3)
322 			));
323 
324 		/* more vertical extended regs */
325 		CRTC2W(EXTRA,
326 			(
327 		 	((vtotal & 0x800) >> (11 - 0)) |
328 			((vdisp_e & 0x800) >> (11 - 2)) |
329 			((vsync_s & 0x800) >> (11 - 4)) |
330 			((vblnk_s & 0x800) >> (11 - 6))
331 			//fixme: do we miss another linecomp bit!?!
332 			));
333 
334 		/* setup 'large screen' mode */
335 		if (target.timing.h_display >= 1280)
336 			CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xfb));
337 		else
338 			CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x04));
339 
340 		/* setup HSYNC & VSYNC polarity */
341 		LOG(2,("CRTC2: sync polarity: "));
342 		temp = NV_REG8(NV8_MISCR);
343 		if (target.timing.flags & B_POSITIVE_HSYNC)
344 		{
345 			LOG(2,("H:pos "));
346 			temp &= ~0x40;
347 		}
348 		else
349 		{
350 			LOG(2,("H:neg "));
351 			temp |= 0x40;
352 		}
353 		if (target.timing.flags & B_POSITIVE_VSYNC)
354 		{
355 			LOG(2,("V:pos "));
356 			temp &= ~0x80;
357 		}
358 		else
359 		{
360 			LOG(2,("V:neg "));
361 			temp |= 0x80;
362 		}
363 		NV_REG8(NV8_MISCW) = temp;
364 
365 		LOG(2,(", MISC reg readback: $%02x\n", NV_REG8(NV8_MISCR)));
366 	}
367 
368 	/* always disable interlaced operation */
369 	/* (interlace is supported on upto and including NV10, NV15, and NV30 and up) */
370 	CRTC2W(INTERLACE, 0xff);
371 
372 	/* disable CRTC slaved mode unless a panel is in use */
373 	// fixme: this kills TVout when it was in use...
374 	if (!si->ps.tmds2_active) CRTC2W(PIXEL, (CRTC2R(PIXEL) & 0x7f));
375 
376 	/* setup flatpanel if connected and active */
377 	if (si->ps.tmds2_active)
378 	{
379 		uint32 iscale_x, iscale_y;
380 
381 		/* calculate inverse scaling factors used by hardware in 20.12 format */
382 		iscale_x = (((1 << 12) * target.timing.h_display) / si->ps.p2_timing.h_display);
383 		iscale_y = (((1 << 12) * target.timing.v_display) / si->ps.p2_timing.v_display);
384 
385 		/* unblock flatpanel timing programming (or something like that..) */
386 		CRTC2W(FP_HTIMING, 0);
387 		CRTC2W(FP_VTIMING, 0);
388 		LOG(2,("CRTC2: FP_HTIMING reg readback: $%02x\n", CRTC2R(FP_HTIMING)));
389 		LOG(2,("CRTC2: FP_VTIMING reg readback: $%02x\n", CRTC2R(FP_VTIMING)));
390 
391 		/* enable full width visibility on flatpanel */
392 		DAC2W(FP_HVALID_S, 0);
393 		DAC2W(FP_HVALID_E, (si->ps.p2_timing.h_display - 1));
394 		/* enable full height visibility on flatpanel */
395 		DAC2W(FP_VVALID_S, 0);
396 		DAC2W(FP_VVALID_E, (si->ps.p2_timing.v_display - 1));
397 
398 		/* nVidia cards support upscaling except on ??? */
399 		/* NV11 cards can upscale after all! */
400 		if (0)//si->ps.card_type == NV11)
401 		{
402 			/* disable last fetched line limiting */
403 			DAC2W(FP_DEBUG2, 0x00000000);
404 			/* inform panel to scale if needed */
405 			if ((iscale_x != (1 << 12)) || (iscale_y != (1 << 12)))
406 			{
407 				LOG(2,("CRTC2: DFP needs to do scaling\n"));
408 				DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) | 0x00000100));
409 			}
410 			else
411 			{
412 				LOG(2,("CRTC2: no scaling for DFP needed\n"));
413 				DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) & 0xfffffeff));
414 			}
415 		}
416 		else
417 		{
418 			float dm_aspect;
419 
420 			LOG(2,("CRTC2: GPU scales for DFP if needed\n"));
421 
422 			/* calculate display mode aspect */
423 			dm_aspect = (target.timing.h_display / ((float)target.timing.v_display));
424 
425 			/* limit last fetched line if vertical scaling is done */
426 			if (iscale_y != (1 << 12))
427 				DAC2W(FP_DEBUG2, ((1 << 28) | ((target.timing.v_display - 1) << 16)));
428 			else
429 				DAC2W(FP_DEBUG2, 0x00000000);
430 
431 			/* inform panel not to scale */
432 			DAC2W(FP_TG_CTRL, (DAC2R(FP_TG_CTRL) & 0xfffffeff));
433 
434 			/* GPU scaling is automatically setup by hardware, so only modify this
435 			 * scalingfactor for non 4:3 (1.33) aspect panels;
436 			 * let's consider 1280x1024 1:33 aspect (it's 1.25 aspect actually!) */
437 
438 			/* correct for widescreen panels relative to mode...
439 			 * (so if panel is more widescreen than mode being set) */
440 			/* BTW: known widescreen panels:
441 			 * 1280 x  800 (1.60),
442 			 * 1440 x  900 (1.60),
443 			 * 1680 x 1050 (1.60),
444 			 * 1920 x 1200 (1.60). */
445 			/* known 4:3 aspect non-standard resolution panels:
446 			 * 1400 x 1050 (1.33). */
447 			/* NOTE:
448 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
449 			if ((iscale_x != (1 << 12)) && (si->ps.panel2_aspect > (dm_aspect + 0.10)))
450 			{
451 				uint16 diff;
452 
453 				LOG(2,("CRTC2: (relative) widescreen panel: tuning horizontal scaling\n"));
454 
455 				/* X-scaling should be the same as Y-scaling */
456 				iscale_x = iscale_y;
457 				/* enable testmode (b12) and program new X-scaling factor */
458 				DAC2W(FP_DEBUG1, (((iscale_x >> 1) & 0x00000fff) | (1 << 12)));
459 				/* center/cut-off left and right side of screen */
460 				diff = ((si->ps.p2_timing.h_display -
461 						((target.timing.h_display * (1 << 12)) / iscale_x))
462 						/ 2);
463 				DAC2W(FP_HVALID_S, diff);
464 				DAC2W(FP_HVALID_E, ((si->ps.p2_timing.h_display - diff) - 1));
465 			}
466 			/* correct for portrait panels... */
467 			/* NOTE:
468 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
469 			if ((iscale_y != (1 << 12)) && (si->ps.panel2_aspect < (dm_aspect - 0.10)))
470 			{
471 				LOG(2,("CRTC2: (relative) portrait panel: should tune vertical scaling\n"));
472 				/* fixme: implement if this kind of portrait panels exist on nVidia... */
473 			}
474 		}
475 
476 		/* do some logging.. */
477 		LOG(2,("CRTC2: FP_HVALID_S reg readback: $%08x\n", DAC2R(FP_HVALID_S)));
478 		LOG(2,("CRTC2: FP_HVALID_E reg readback: $%08x\n", DAC2R(FP_HVALID_E)));
479 		LOG(2,("CRTC2: FP_VVALID_S reg readback: $%08x\n", DAC2R(FP_VVALID_S)));
480 		LOG(2,("CRTC2: FP_VVALID_E reg readback: $%08x\n", DAC2R(FP_VVALID_E)));
481 		LOG(2,("CRTC2: FP_DEBUG0 reg readback: $%08x\n", DAC2R(FP_DEBUG0)));
482 		LOG(2,("CRTC2: FP_DEBUG1 reg readback: $%08x\n", DAC2R(FP_DEBUG1)));
483 		LOG(2,("CRTC2: FP_DEBUG2 reg readback: $%08x\n", DAC2R(FP_DEBUG2)));
484 		LOG(2,("CRTC2: FP_DEBUG3 reg readback: $%08x\n", DAC2R(FP_DEBUG3)));
485 		LOG(2,("CRTC2: FP_TG_CTRL reg readback: $%08x\n", DAC2R(FP_TG_CTRL)));
486 	}
487 
488 	return B_OK;
489 }
490 
491 status_t nv_crtc2_depth(int mode)
492 {
493 	uint8 viddelay = 0;
494 	uint32 genctrl = 0;
495 
496 	/* set VCLK scaling */
497 	switch(mode)
498 	{
499 	case BPP8:
500 		viddelay = 0x01;
501 		/* genctrl b4 & b5 reset: 'direct mode' */
502 		genctrl = 0x00101100;
503 		break;
504 	case BPP15:
505 		viddelay = 0x02;
506 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
507 		genctrl = 0x00100130;
508 		break;
509 	case BPP16:
510 		viddelay = 0x02;
511 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
512 		genctrl = 0x00101130;
513 		break;
514 	case BPP24:
515 		viddelay = 0x03;
516 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
517 		genctrl = 0x00100130;
518 		break;
519 	case BPP32:
520 		viddelay = 0x03;
521 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
522 		genctrl = 0x00101130;
523 		break;
524 	}
525 	/* enable access to secondary head */
526 	set_crtc_owner(1);
527 
528 	CRTC2W(PIXEL, ((CRTC2R(PIXEL) & 0xfc) | viddelay));
529 	DAC2W(GENCTRL, genctrl);
530 
531 	return B_OK;
532 }
533 
534 status_t nv_crtc2_dpms(bool display, bool h, bool v, bool do_panel)
535 {
536 	uint8 temp;
537 	char msg[100];
538 
539 	sprintf(msg, "CRTC2: setting DPMS: ");
540 
541 	/* enable access to secondary head */
542 	set_crtc_owner(1);
543 
544 	/* start synchronous reset: required before turning screen off! */
545 	SEQW(RESET, 0x01);
546 
547 	temp = SEQR(CLKMODE);
548 	if (display)
549 	{
550 		/* turn screen on */
551 		SEQW(CLKMODE, (temp & ~0x20));
552 
553 		/* end synchronous reset because display should be enabled */
554 		SEQW(RESET, 0x03);
555 
556 		if (do_panel && si->ps.tmds2_active && !si->ps.laptop)
557 		{
558 			/* restore original panelsync and panel-enable */
559 			uint32 panelsync = 0x00000000;
560 			if(si->ps.p2_timing.flags & B_POSITIVE_VSYNC) panelsync |= 0x00000001;
561 			if(si->ps.p2_timing.flags & B_POSITIVE_HSYNC) panelsync |= 0x00000010;
562 			/* display enable polarity (not an official flag) */
563 			if(si->ps.p2_timing.flags & B_BLANK_PEDESTAL) panelsync |= 0x10000000;
564 			DAC2W(FP_TG_CTRL, ((DAC2R(FP_TG_CTRL) & 0xcfffffcc) | panelsync));
565 
566 			//fixme?: looks like we don't need this after all:
567 			/* powerup both LVDS (laptop panellink) and TMDS (DVI panellink)
568 			 * internal transmitters... */
569 			/* note:
570 			 * the powerbits in this register are hardwired to the DVI connectors,
571 			 * instead of to the DACs! (confirmed NV34) */
572 			//fixme...
573 			//DAC2W(FP_DEBUG0, (DAC2R(FP_DEBUG0) & 0xcfffffff));
574 			/* ... and powerup external TMDS transmitter if it exists */
575 			/* (confirmed OK on NV28 and NV34) */
576 			//CRTC2W(0x59, (CRTC2R(0x59) | 0x01));
577 
578 			sprintf(msg, "%s(panel-)", msg);
579 		}
580 
581 		sprintf(msg, "%sdisplay on, ", msg);
582 	}
583 	else
584 	{
585 		/* turn screen off */
586 		SEQW(CLKMODE, (temp | 0x20));
587 
588 		if (do_panel && si->ps.tmds2_active && !si->ps.laptop)
589 		{
590 			/* shutoff panelsync and disable panel */
591 			DAC2W(FP_TG_CTRL, ((DAC2R(FP_TG_CTRL) & 0xcfffffcc) | 0x20000022));
592 
593 			//fixme?: looks like we don't need this after all:
594 			/* powerdown both LVDS (laptop panellink) and TMDS (DVI panellink)
595 			 * internal transmitters... */
596 			/* note:
597 			 * the powerbits in this register are hardwired to the DVI connectors,
598 			 * instead of to the DACs! (confirmed NV34) */
599 			//fixme...
600 			//DAC2W(FP_DEBUG0, (DAC2R(FP_DEBUG0) | 0x30000000));
601 			/* ... and powerdown external TMDS transmitter if it exists */
602 			/* (confirmed OK on NV28 and NV34) */
603 			//CRTC2W(0x59, (CRTC2R(0x59) & 0xfe));
604 
605 			sprintf(msg, "%s(panel-)", msg);
606 		}
607 
608 		sprintf(msg, "%sdisplay off, ", msg);
609 	}
610 
611 	if (h)
612 	{
613 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0x7f));
614 		sprintf(msg, "%shsync enabled, ", msg);
615 	}
616 	else
617 	{
618 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x80));
619 		sprintf(msg, "%shsync disabled, ", msg);
620 	}
621 	if (v)
622 	{
623 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xbf));
624 		sprintf(msg, "%svsync enabled\n", msg);
625 	}
626 	else
627 	{
628 		CRTC2W(REPAINT1, (CRTC2R(REPAINT1) | 0x40));
629 		sprintf(msg, "%svsync disabled\n", msg);
630 	}
631 
632 	LOG(4, (msg));
633 
634 	return B_OK;
635 }
636 
637 status_t nv_crtc2_set_display_pitch()
638 {
639 	uint32 offset;
640 
641 	LOG(4,("CRTC2: setting card pitch (offset between lines)\n"));
642 
643 	/* figure out offset value hardware needs */
644 	offset = si->fbc.bytes_per_row / 8;
645 
646 	LOG(2,("CRTC2: offset register set to: $%04x\n", offset));
647 
648 	/* enable access to secondary head */
649 	set_crtc_owner(1);
650 
651 	/* program the card */
652 	CRTC2W(PITCHL, (offset & 0x00ff));
653 	CRTC2W(REPAINT0, ((CRTC2R(REPAINT0) & 0x1f) | ((offset & 0x0700) >> 3)));
654 
655 	return B_OK;
656 }
657 
658 status_t nv_crtc2_set_display_start(uint32 startadd,uint8 bpp)
659 {
660 	uint32 timeout = 0;
661 
662 	LOG(4,("CRTC2: setting card RAM to be displayed bpp %d\n", bpp));
663 
664 	LOG(2,("CRTC2: startadd: $%08x\n", startadd));
665 	LOG(2,("CRTC2: frameRAM: $%08x\n", si->framebuffer));
666 	LOG(2,("CRTC2: framebuffer: $%08x\n", si->fbc.frame_buffer));
667 
668 	/* we might have no retraces during setmode! */
669 	/* wait 25mS max. for retrace to occur (refresh > 40Hz) */
670 	while (((NV_REG32(NV32_RASTER2) & 0x000007ff) < si->dm.timing.v_display) &&
671 			(timeout < (25000/10)))
672 	{
673 		/* don't snooze much longer or retrace might get missed! */
674 		snooze(10);
675 		timeout++;
676 	}
677 
678 	/* enable access to secondary head */
679 	set_crtc_owner(1);
680 
681 	/* upto 4Gb RAM adressing: must be used on NV10 and later! */
682 	/* NOTE:
683 	 * While this register also exists on pre-NV10 cards, it will
684 	 * wrap-around at 16Mb boundaries!! */
685 
686 	/* 30bit adress in 32bit words */
687 	NV_REG32(NV32_NV10FB2STADD32) = (startadd & 0xfffffffc);
688 
689 	/* set byte adress: (b0 - 1) */
690 	ATB2W(HORPIXPAN, ((startadd & 0x00000003) << 1));
691 
692 	return B_OK;
693 }
694 
695 status_t nv_crtc2_cursor_init()
696 {
697 	int i;
698 	uint32 * fb;
699 	/* cursor bitmap will be stored at the start of the framebuffer */
700 	const uint32 curadd = 0;
701 
702 	/* enable access to secondary head */
703 	set_crtc_owner(1);
704 
705 	/* set cursor bitmap adress ... */
706 	if (si->ps.laptop)
707 	{
708 		/* must be used this way on pre-NV10 and on all 'Go' cards! */
709 
710 		/* cursorbitmap must start on 2Kbyte boundary: */
711 		/* set adress bit11-16, and set 'no doublescan' (registerbit 1 = 0) */
712 		CRTC2W(CURCTL0, ((curadd & 0x0001f800) >> 9));
713 		/* set adress bit17-23, and set graphics mode cursor(?) (registerbit 7 = 1) */
714 		CRTC2W(CURCTL1, (((curadd & 0x00fe0000) >> 17) | 0x80));
715 		/* set adress bit24-31 */
716 		CRTC2W(CURCTL2, ((curadd & 0xff000000) >> 24));
717 	}
718 	else
719 	{
720 		/* upto 4Gb RAM adressing:
721 		 * can be used on NV10 and later (except for 'Go' cards)! */
722 		/* NOTE:
723 		 * This register does not exist on pre-NV10 and 'Go' cards. */
724 
725 		/* cursorbitmap must still start on 2Kbyte boundary: */
726 		NV_REG32(NV32_NV10CUR2ADD32) = (curadd & 0xfffff800);
727 	}
728 
729 	/* set cursor colour: not needed because of direct nature of cursor bitmap. */
730 
731 	/*clear cursor*/
732 	fb = (uint32 *) si->framebuffer + curadd;
733 	for (i=0;i<(2048/4);i++)
734 	{
735 		fb[i]=0;
736 	}
737 
738 	/* select 32x32 pixel, 16bit color cursorbitmap, no doublescan */
739 	NV_REG32(NV32_2CURCONF) = 0x02000100;
740 
741 	/* activate hardware-sync between cursor updates and vertical retrace */
742 	DAC2W(NV10_CURSYNC, (DAC2R(NV10_CURSYNC) | 0x02000000));
743 
744 	/* activate hardware cursor */
745 	nv_crtc2_cursor_show();
746 
747 	return B_OK;
748 }
749 
750 status_t nv_crtc2_cursor_show()
751 {
752 	LOG(4,("CRTC2: enabling cursor\n"));
753 
754 	/* enable access to secondary head */
755 	set_crtc_owner(1);
756 
757 	/* b0 = 1 enables cursor */
758 	CRTC2W(CURCTL0, (CRTC2R(CURCTL0) | 0x01));
759 
760 	/* workaround for hardware bug confirmed existing on NV43:
761 	 * Cursor visibility is not updated without a position update if its hardware
762 	 * retrace sync is enabled. */
763 	if (si->ps.card_arch == NV40A) DAC2W(CURPOS, (DAC2R(CURPOS)));
764 
765 	return B_OK;
766 }
767 
768 status_t nv_crtc2_cursor_hide()
769 {
770 	LOG(4,("CRTC2: disabling cursor\n"));
771 
772 	/* enable access to secondary head */
773 	set_crtc_owner(1);
774 
775 	/* b0 = 0 disables cursor */
776 	CRTC2W(CURCTL0, (CRTC2R(CURCTL0) & 0xfe));
777 
778 	/* workaround for hardware bug confirmed existing on NV43:
779 	 * Cursor visibility is not updated without a position update if its hardware
780 	 * retrace sync is enabled. */
781 	if (si->ps.card_arch == NV40A) DAC2W(CURPOS, (DAC2R(CURPOS)));
782 
783 	return B_OK;
784 }
785 
786 /*set up cursor shape*/
787 status_t nv_crtc2_cursor_define(uint8* andMask,uint8* xorMask)
788 {
789 	int x, y;
790 	uint8 b;
791 	uint16 *cursor;
792 	uint16 pixel;
793 
794 	/* get a pointer to the cursor */
795 	cursor = (uint16*) si->framebuffer;
796 
797 	/* draw the cursor */
798 	/* (Nvidia cards have a RGB15 direct color cursor bitmap, bit #16 is transparancy) */
799 	for (y = 0; y < 16; y++)
800 	{
801 		b = 0x80;
802 		for (x = 0; x < 8; x++)
803 		{
804 			/* preset transparant */
805 			pixel = 0x0000;
806 			/* set white if requested */
807 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
808 			/* set black if requested */
809 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
810 			/* set invert if requested */
811 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
812 			/* place the pixel in the bitmap */
813 			cursor[x + (y * 32)] = pixel;
814 			b >>= 1;
815 		}
816 		xorMask++;
817 		andMask++;
818 		b = 0x80;
819 		for (; x < 16; x++)
820 		{
821 			/* preset transparant */
822 			pixel = 0x0000;
823 			/* set white if requested */
824 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
825 			/* set black if requested */
826 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
827 			/* set invert if requested */
828 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
829 			/* place the pixel in the bitmap */
830 			cursor[x + (y * 32)] = pixel;
831 			b >>= 1;
832 		}
833 		xorMask++;
834 		andMask++;
835 	}
836 
837 	return B_OK;
838 }
839 
840 /* position the cursor */
841 status_t nv_crtc2_cursor_position(uint16 x, uint16 y)
842 {
843 	/* the cursor position is updated during retrace by card hardware */
844 
845 	/* update cursorposition */
846 	DAC2W(CURPOS, ((x & 0x0fff) | ((y & 0x0fff) << 16)));
847 
848 	return B_OK;
849 }
850 
851 status_t nv_crtc2_stop_tvout(void)
852 {
853 	LOG(4,("CRTC2: stopping TV output\n"));
854 
855 	/* enable access to secondary head */
856 	set_crtc_owner(1);
857 
858 	/* just to be sure Vsync is _really_ enabled */
859 	CRTC2W(REPAINT1, (CRTC2R(REPAINT1) & 0xbf));
860 
861 	/* wait for one image to be generated to make sure VGA has kicked in and is
862 	 * running OK before continuing...
863 	 * (Kicking in will fail often if we do not wait here) */
864 	/* Note:
865 	 * The used CRTC's Vsync is required to be enabled here. The DPMS state
866 	 * programming in the driver makes sure this is the case.
867 	 * (except for driver startup: see nv_general.c.) */
868 
869 	/* make sure we are 'in' active VGA picture */
870 	while (NV_REG8(NV8_INSTAT1) & 0x08) snooze(1);
871 	/* wait for next vertical retrace start on VGA */
872 	while (!(NV_REG8(NV8_INSTAT1) & 0x08)) snooze(1);
873 	/* now wait until we are 'in' active VGA picture again */
874 	while (NV_REG8(NV8_INSTAT1) & 0x08) snooze(1);
875 
876 
877 	/* set CRTC to master mode (b7 = 0) if it wasn't slaved for a panel before */
878 	if (!(si->ps.slaved_tmds2))	CRTC2W(PIXEL, (CRTC2R(PIXEL) & 0x03));
879 
880 	/* CAUTION:
881 	 * On old cards, PLLSEL (and TV_SETUP?) cannot be read (sometimes?), but
882 	 * write actions do succeed ...
883 	 * This is confirmed for both ISA and PCI access, on NV04 and NV11. */
884 
885 	/* setup TVencoder connection */
886 	/* b1-0 = %00: encoder type is SLAVE;
887 	 * b24 = 1: VIP datapos is b0-7 */
888 	//fixme if needed: setup completely instead of relying on pre-init by BIOS..
889 	//(it seems to work OK on NV04 and NV11 although read reg. doesn't seem to work)
890 	DAC2W(TV_SETUP, ((DAC2R(TV_SETUP) & ~0x00000003) | 0x01000000));
891 
892 	/* tell GPU to use pixelclock from internal source instead of using TVencoder */
893 	DACW(PLLSEL, 0x30000f00);
894 
895 	/* HTOTAL, VTOTAL and OVERFLOW return their default CRTC use, instead of
896 	 * H, V-low and V-high 'shadow' counters(?)(b0, 4 and 6 = 0) (b7 use = unknown) */
897 	CRTC2W(TREG, 0x00);
898 
899 	/* select panel encoder, not TV encoder if needed (b0 = 1).
900 	 * Note:
901 	 * Both are devices (often) using the CRTC in slaved mode. */
902 	if (si->ps.slaved_tmds2) CRTC2W(LCD, (CRTC2R(LCD) | 0x01));
903 
904 	return B_OK;
905 }
906 
907 status_t nv_crtc2_start_tvout(void)
908 {
909 	LOG(4,("CRTC2: starting TV output\n"));
910 
911 	/* switch TV encoder to CRTC2 */
912 	NV_REG32(NV32_FUNCSEL) &= ~0x00000100;
913 	NV_REG32(NV32_2FUNCSEL) |= 0x00000100;
914 
915 	/* enable access to secondary head */
916 	set_crtc_owner(1);
917 
918 	/* CAUTION:
919 	 * On old cards, PLLSEL (and TV_SETUP?) cannot be read (sometimes?), but
920 	 * write actions do succeed ...
921 	 * This is confirmed for both ISA and PCI access, on NV04 and NV11. */
922 
923 	/* setup TVencoder connection */
924 	/* b1-0 = %01: encoder type is MASTER;
925 	 * b24 = 1: VIP datapos is b0-7 */
926 	//fixme if needed: setup completely instead of relying on pre-init by BIOS..
927 	//(it seems to work OK on NV04 and NV11 although read reg. doesn't seem to work)
928 	DAC2W(TV_SETUP, ((DAC2R(TV_SETUP) & ~0x00000002) | 0x01000001));
929 
930 	/* tell GPU to use pixelclock from TVencoder instead of using internal source */
931 	/* (nessecary or display will 'shiver' on both TV and VGA.) */
932 	DACW(PLLSEL, 0x100c0f00);
933 
934 	/* Set overscan color to 'black' */
935 	/* note:
936 	 * Change this instruction for a visible overscan color if you're trying to
937 	 * center the output on TV. Use it as a guide-'line' then ;-) */
938 	ATB2W(OSCANCOLOR, 0x00);
939 
940 	/* set CRTC to slaved mode (b7 = 1) and clear TVadjust (b3-5 = %000) */
941 	CRTC2W(PIXEL, ((CRTC2R(PIXEL) & 0xc7) | 0x80));
942 	/* select TV encoder, not panel encoder (b0 = 0).
943 	 * Note:
944 	 * Both are devices (often) using the CRTC in slaved mode. */
945 	CRTC2W(LCD, (CRTC2R(LCD) & 0xfe));
946 
947 	/* HTOTAL, VTOTAL and OVERFLOW return their default CRTC use, instead of
948 	 * H, V-low and V-high 'shadow' counters(?)(b0, 4 and 6 = 0) (b7 use = unknown) */
949 	CRTC2W(TREG, 0x80);
950 
951 	return B_OK;
952 }
953