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