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