xref: /haiku/src/add-ons/accelerants/skeleton/engine/crtc.c (revision bf1feef864e39a6776c4f85ac0f93c13a8921cc4)
168353368SRudolf Cornelissen /* CTRC functionality */
268353368SRudolf Cornelissen /* Author:
368353368SRudolf Cornelissen    Rudolf Cornelissen 11/2002-9/2004
468353368SRudolf Cornelissen */
568353368SRudolf Cornelissen 
668353368SRudolf Cornelissen #define MODULE_BIT 0x00040000
768353368SRudolf Cornelissen 
8886dbf81SRudolf Cornelissen #include "std.h"
968353368SRudolf Cornelissen 
1068353368SRudolf Cornelissen /*Adjust passed parameters to a valid mode line*/
eng_crtc_validate_timing(uint16 * hd_e,uint16 * hs_s,uint16 * hs_e,uint16 * ht,uint16 * vd_e,uint16 * vs_s,uint16 * vs_e,uint16 * vt)11886dbf81SRudolf Cornelissen status_t eng_crtc_validate_timing(
1268353368SRudolf Cornelissen 	uint16 *hd_e,uint16 *hs_s,uint16 *hs_e,uint16 *ht,
1368353368SRudolf Cornelissen 	uint16 *vd_e,uint16 *vs_s,uint16 *vs_e,uint16 *vt
1468353368SRudolf Cornelissen )
1568353368SRudolf Cornelissen {
1668353368SRudolf Cornelissen /* horizontal */
1768353368SRudolf Cornelissen 	/* make all parameters multiples of 8 */
1868353368SRudolf Cornelissen 	*hd_e &= 0xfff8;
1968353368SRudolf Cornelissen 	*hs_s &= 0xfff8;
2068353368SRudolf Cornelissen 	*hs_e &= 0xfff8;
2168353368SRudolf Cornelissen 	*ht   &= 0xfff8;
2268353368SRudolf Cornelissen 
2368353368SRudolf Cornelissen 	/* confine to required number of bits, taking logic into account */
2468353368SRudolf Cornelissen 	if (*hd_e > ((0x01ff - 2) << 3)) *hd_e = ((0x01ff - 2) << 3);
2568353368SRudolf Cornelissen 	if (*hs_s > ((0x01ff - 1) << 3)) *hs_s = ((0x01ff - 1) << 3);
2668353368SRudolf Cornelissen 	if (*hs_e > ( 0x01ff      << 3)) *hs_e = ( 0x01ff      << 3);
2768353368SRudolf Cornelissen 	if (*ht   > ((0x01ff + 5) << 3)) *ht   = ((0x01ff + 5) << 3);
2868353368SRudolf Cornelissen 
2968353368SRudolf Cornelissen 	/* NOTE: keep horizontal timing at multiples of 8! */
3068353368SRudolf Cornelissen 	/* confine to a reasonable width */
3168353368SRudolf Cornelissen 	if (*hd_e < 640) *hd_e = 640;
3268353368SRudolf Cornelissen 	if (si->ps.card_type > NV04)
3368353368SRudolf Cornelissen 	{
3468353368SRudolf Cornelissen 		if (*hd_e > 2048) *hd_e = 2048;
3568353368SRudolf Cornelissen 	}
3668353368SRudolf Cornelissen 	else
3768353368SRudolf Cornelissen 	{
3868353368SRudolf Cornelissen 		if (*hd_e > 1920) *hd_e = 1920;
3968353368SRudolf Cornelissen 	}
4068353368SRudolf Cornelissen 
4168353368SRudolf Cornelissen 	/* if hor. total does not leave room for a sensible sync pulse, increase it! */
4268353368SRudolf Cornelissen 	if (*ht < (*hd_e + 80)) *ht = (*hd_e + 80);
4368353368SRudolf Cornelissen 
4468353368SRudolf Cornelissen 	/* if hor. total does not adhere to max. blanking pulse width, decrease it! */
4568353368SRudolf Cornelissen 	if (*ht > (*hd_e + 0x3f8)) *ht = (*hd_e + 0x3f8);
4668353368SRudolf Cornelissen 
4768353368SRudolf Cornelissen 	/* make sure sync pulse is not during display */
4868353368SRudolf Cornelissen 	if (*hs_e > (*ht - 8)) *hs_e = (*ht - 8);
4968353368SRudolf Cornelissen 	if (*hs_s < (*hd_e + 8)) *hs_s = (*hd_e + 8);
5068353368SRudolf Cornelissen 
5168353368SRudolf Cornelissen 	/* correct sync pulse if it is too long:
5268353368SRudolf Cornelissen 	 * there are only 5 bits available to save this in the card registers! */
5368353368SRudolf Cornelissen 	if (*hs_e > (*hs_s + 0xf8)) *hs_e = (*hs_s + 0xf8);
5468353368SRudolf Cornelissen 
5568353368SRudolf Cornelissen /*vertical*/
5668353368SRudolf Cornelissen 	/* confine to required number of bits, taking logic into account */
5768353368SRudolf Cornelissen 	//fixme if needed: on GeForce cards there are 12 instead of 11 bits...
5868353368SRudolf Cornelissen 	if (*vd_e > (0x7ff - 2)) *vd_e = (0x7ff - 2);
5968353368SRudolf Cornelissen 	if (*vs_s > (0x7ff - 1)) *vs_s = (0x7ff - 1);
6068353368SRudolf Cornelissen 	if (*vs_e >  0x7ff     ) *vs_e =  0x7ff     ;
6168353368SRudolf Cornelissen 	if (*vt   > (0x7ff + 2)) *vt   = (0x7ff + 2);
6268353368SRudolf Cornelissen 
6368353368SRudolf Cornelissen 	/* confine to a reasonable height */
6468353368SRudolf Cornelissen 	if (*vd_e < 480) *vd_e = 480;
6568353368SRudolf Cornelissen 	if (si->ps.card_type > NV04)
6668353368SRudolf Cornelissen 	{
6768353368SRudolf Cornelissen 		if (*vd_e > 1536) *vd_e = 1536;
6868353368SRudolf Cornelissen 	}
6968353368SRudolf Cornelissen 	else
7068353368SRudolf Cornelissen 	{
7168353368SRudolf Cornelissen 		if (*vd_e > 1440) *vd_e = 1440;
7268353368SRudolf Cornelissen 	}
7368353368SRudolf Cornelissen 
7468353368SRudolf Cornelissen 	/*if vertical total does not leave room for a sync pulse, increase it!*/
7568353368SRudolf Cornelissen 	if (*vt < (*vd_e + 3)) *vt = (*vd_e + 3);
7668353368SRudolf Cornelissen 
7768353368SRudolf Cornelissen 	/* if vert. total does not adhere to max. blanking pulse width, decrease it! */
7868353368SRudolf Cornelissen 	if (*vt > (*vd_e + 0xff)) *vt = (*vd_e + 0xff);
7968353368SRudolf Cornelissen 
8068353368SRudolf Cornelissen 	/* make sure sync pulse is not during display */
8168353368SRudolf Cornelissen 	if (*vs_e > (*vt - 1)) *vs_e = (*vt - 1);
8268353368SRudolf Cornelissen 	if (*vs_s < (*vd_e + 1)) *vs_s = (*vd_e + 1);
8368353368SRudolf Cornelissen 
8468353368SRudolf Cornelissen 	/* correct sync pulse if it is too long:
8568353368SRudolf Cornelissen 	 * there are only 4 bits available to save this in the card registers! */
8668353368SRudolf Cornelissen 	if (*vs_e > (*vs_s + 0x0f)) *vs_e = (*vs_s + 0x0f);
8768353368SRudolf Cornelissen 
8868353368SRudolf Cornelissen 	return B_OK;
8968353368SRudolf Cornelissen }
9068353368SRudolf Cornelissen 
9168353368SRudolf Cornelissen /*set a mode line - inputs are in pixels*/
eng_crtc_set_timing(display_mode target)92886dbf81SRudolf Cornelissen status_t eng_crtc_set_timing(display_mode target)
9368353368SRudolf Cornelissen {
9468353368SRudolf Cornelissen 	uint8 temp;
9568353368SRudolf Cornelissen 
9668353368SRudolf Cornelissen 	uint32 htotal;		/*total horizontal total VCLKs*/
9768353368SRudolf Cornelissen 	uint32 hdisp_e;            /*end of horizontal display (begins at 0)*/
9868353368SRudolf Cornelissen 	uint32 hsync_s;            /*begin of horizontal sync pulse*/
9968353368SRudolf Cornelissen 	uint32 hsync_e;            /*end of horizontal sync pulse*/
10068353368SRudolf Cornelissen 	uint32 hblnk_s;            /*begin horizontal blanking*/
10168353368SRudolf Cornelissen 	uint32 hblnk_e;            /*end horizontal blanking*/
10268353368SRudolf Cornelissen 
10368353368SRudolf Cornelissen 	uint32 vtotal;		/*total vertical total scanlines*/
10468353368SRudolf Cornelissen 	uint32 vdisp_e;            /*end of vertical display*/
10568353368SRudolf Cornelissen 	uint32 vsync_s;            /*begin of vertical sync pulse*/
10668353368SRudolf Cornelissen 	uint32 vsync_e;            /*end of vertical sync pulse*/
10768353368SRudolf Cornelissen 	uint32 vblnk_s;            /*begin vertical blanking*/
10868353368SRudolf Cornelissen 	uint32 vblnk_e;            /*end vertical blanking*/
10968353368SRudolf Cornelissen 
11068353368SRudolf Cornelissen 	uint32 linecomp;	/*split screen and vdisp_e interrupt*/
11168353368SRudolf Cornelissen 
11268353368SRudolf Cornelissen 	LOG(4,("CRTC: setting timing\n"));
11368353368SRudolf Cornelissen 
11468353368SRudolf Cornelissen 	/* setup tuned internal modeline for flatpanel if connected and active */
11568353368SRudolf Cornelissen 	/* notes:
11668353368SRudolf Cornelissen 	 * - the CRTC modeline must end earlier than the panel modeline to keep correct
11768353368SRudolf Cornelissen 	 *   sync going;
11868353368SRudolf Cornelissen 	 * - if the CRTC modeline ends too soon, pixelnoise will occur in 8 (or so) pixel
11968353368SRudolf Cornelissen 	 *   wide horizontal stripes. This can be observed earliest on fullscreen overlay,
12068353368SRudolf Cornelissen 	 *   and if it gets worse, also normal desktop output will suffer. The stripes
12168353368SRudolf Cornelissen 	 *   are mainly visible at the left of the screen, over the entire screen height. */
12268353368SRudolf Cornelissen 	if (si->ps.tmds1_active)
12368353368SRudolf Cornelissen 	{
12468353368SRudolf Cornelissen 		LOG(2,("CRTC: DFP active: tuning modeline\n"));
12568353368SRudolf Cornelissen 
12668353368SRudolf Cornelissen 		/* horizontal timing */
12768353368SRudolf Cornelissen 		target.timing.h_sync_start =
12868353368SRudolf Cornelissen 			((uint16)((si->ps.p1_timing.h_sync_start / ((float)si->ps.p1_timing.h_display)) *
12968353368SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8;
13068353368SRudolf Cornelissen 
13168353368SRudolf Cornelissen 		target.timing.h_sync_end =
13268353368SRudolf Cornelissen 			((uint16)((si->ps.p1_timing.h_sync_end / ((float)si->ps.p1_timing.h_display)) *
13368353368SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8;
13468353368SRudolf Cornelissen 
13568353368SRudolf Cornelissen 		target.timing.h_total =
13668353368SRudolf Cornelissen 			(((uint16)((si->ps.p1_timing.h_total / ((float)si->ps.p1_timing.h_display)) *
13768353368SRudolf Cornelissen 			target.timing.h_display)) & 0xfff8) - 8;
13868353368SRudolf Cornelissen 
13968353368SRudolf Cornelissen 		/* in native mode the CRTC needs some extra time to keep synced correctly;
14068353368SRudolf Cornelissen 		 * OTOH the overlay unit distorts if we reserve too much time! */
14168353368SRudolf Cornelissen 		if (target.timing.h_display == si->ps.p1_timing.h_display)
14268353368SRudolf Cornelissen 		{
14368353368SRudolf Cornelissen 			/* NV11 timing has different constraints than later cards */
14468353368SRudolf Cornelissen 			if (si->ps.card_type == NV11)
14568353368SRudolf Cornelissen 				target.timing.h_total -= 56;
14668353368SRudolf Cornelissen 			else
14768353368SRudolf Cornelissen 				/* confirmed NV34 with 1680x1050 panel */
14868353368SRudolf Cornelissen 				target.timing.h_total -= 32;
14968353368SRudolf Cornelissen 		}
15068353368SRudolf Cornelissen 
15168353368SRudolf Cornelissen 		if (target.timing.h_sync_start == target.timing.h_display)
15268353368SRudolf Cornelissen 			target.timing.h_sync_start += 8;
15368353368SRudolf Cornelissen 		if (target.timing.h_sync_end == target.timing.h_total)
15468353368SRudolf Cornelissen 			target.timing.h_sync_end -= 8;
15568353368SRudolf Cornelissen 
15668353368SRudolf Cornelissen 		/* vertical timing */
15768353368SRudolf Cornelissen 		target.timing.v_sync_start =
15868353368SRudolf Cornelissen 			((uint16)((si->ps.p1_timing.v_sync_start / ((float)si->ps.p1_timing.v_display)) *
15968353368SRudolf Cornelissen 			target.timing.v_display));
16068353368SRudolf Cornelissen 
16168353368SRudolf Cornelissen 		target.timing.v_sync_end =
16268353368SRudolf Cornelissen 			((uint16)((si->ps.p1_timing.v_sync_end / ((float)si->ps.p1_timing.v_display)) *
16368353368SRudolf Cornelissen 			target.timing.v_display));
16468353368SRudolf Cornelissen 
16568353368SRudolf Cornelissen 		target.timing.v_total =
16668353368SRudolf Cornelissen 			((uint16)((si->ps.p1_timing.v_total / ((float)si->ps.p1_timing.v_display)) *
16768353368SRudolf Cornelissen 			target.timing.v_display)) - 1;
16868353368SRudolf Cornelissen 
16968353368SRudolf Cornelissen 		if (target.timing.v_sync_start == target.timing.v_display)
17068353368SRudolf Cornelissen 			target.timing.v_sync_start += 1;
17168353368SRudolf Cornelissen 		if (target.timing.v_sync_end == target.timing.v_total)
17268353368SRudolf Cornelissen 			target.timing.v_sync_end -= 1;
17368353368SRudolf Cornelissen 
17468353368SRudolf Cornelissen 		/* disable GPU scaling testmode so automatic scaling will be done */
17568353368SRudolf Cornelissen 		DACW(FP_DEBUG1, 0);
17668353368SRudolf Cornelissen 	}
17768353368SRudolf Cornelissen 
17868353368SRudolf Cornelissen 	/* Modify parameters as required by standard VGA */
17968353368SRudolf Cornelissen 	htotal = ((target.timing.h_total >> 3) - 5);
18068353368SRudolf Cornelissen 	hdisp_e = ((target.timing.h_display >> 3) - 1);
18168353368SRudolf Cornelissen 	hblnk_s = hdisp_e;
18268353368SRudolf Cornelissen 	hblnk_e = (htotal + 4);//0;
18368353368SRudolf Cornelissen 	hsync_s = (target.timing.h_sync_start >> 3);
18468353368SRudolf Cornelissen 	hsync_e = (target.timing.h_sync_end >> 3);
18568353368SRudolf Cornelissen 
18668353368SRudolf Cornelissen 	vtotal = target.timing.v_total - 2;
18768353368SRudolf Cornelissen 	vdisp_e = target.timing.v_display - 1;
18868353368SRudolf Cornelissen 	vblnk_s = vdisp_e;
18968353368SRudolf Cornelissen 	vblnk_e = (vtotal + 1);
19068353368SRudolf Cornelissen 	vsync_s = target.timing.v_sync_start;//-1;
19168353368SRudolf Cornelissen 	vsync_e = target.timing.v_sync_end;//-1;
19268353368SRudolf Cornelissen 
19368353368SRudolf Cornelissen 	/* prevent memory adress counter from being reset (linecomp may not occur) */
19468353368SRudolf Cornelissen 	linecomp = target.timing.v_display;
19568353368SRudolf Cornelissen 
19668353368SRudolf Cornelissen 	/* enable access to primary head */
19768353368SRudolf Cornelissen 	set_crtc_owner(0);
19868353368SRudolf Cornelissen 
19968353368SRudolf Cornelissen 	/* Note for laptop and DVI flatpanels:
20068353368SRudolf Cornelissen 	 * CRTC timing has a seperate set of registers from flatpanel timing.
20168353368SRudolf Cornelissen 	 * The flatpanel timing registers have scaling registers that are used to match
20268353368SRudolf Cornelissen 	 * these two modelines. */
20368353368SRudolf Cornelissen 	{
20468353368SRudolf Cornelissen 		LOG(4,("CRTC: Setting full timing...\n"));
20568353368SRudolf Cornelissen 
20668353368SRudolf Cornelissen 		/* log the mode that will be set */
20768353368SRudolf Cornelissen 		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));
20868353368SRudolf Cornelissen 		LOG(2,("VTOT:%x\n\tVDISPEND:%x\n\tVBLNKS:%x\n\tVBLNKE:%x\n\tVSYNCS:%x\n\tVSYNCE:%x\n",vtotal,vdisp_e,vblnk_s,vblnk_e,vsync_s,vsync_e));
20968353368SRudolf Cornelissen 
21068353368SRudolf Cornelissen 		/* actually program the card! */
21168353368SRudolf Cornelissen 		/* unlock CRTC registers at index 0-7 */
21268353368SRudolf Cornelissen 		CRTCW(VSYNCE, (CRTCR(VSYNCE) & 0x7f));
21368353368SRudolf Cornelissen 		/* horizontal standard VGA regs */
21468353368SRudolf Cornelissen 		CRTCW(HTOTAL, (htotal & 0xff));
21568353368SRudolf Cornelissen 		CRTCW(HDISPE, (hdisp_e & 0xff));
21668353368SRudolf Cornelissen 		CRTCW(HBLANKS, (hblnk_s & 0xff));
21768353368SRudolf Cornelissen 		/* also unlock vertical retrace registers in advance */
21868353368SRudolf Cornelissen 		CRTCW(HBLANKE, ((hblnk_e & 0x1f) | 0x80));
21968353368SRudolf Cornelissen 		CRTCW(HSYNCS, (hsync_s & 0xff));
22068353368SRudolf Cornelissen 		CRTCW(HSYNCE, ((hsync_e & 0x1f) | ((hblnk_e & 0x20) << 2)));
22168353368SRudolf Cornelissen 
22268353368SRudolf Cornelissen 		/* vertical standard VGA regs */
22368353368SRudolf Cornelissen 		CRTCW(VTOTAL, (vtotal & 0xff));
22468353368SRudolf Cornelissen 		CRTCW(OVERFLOW,
22568353368SRudolf Cornelissen 		(
22668353368SRudolf Cornelissen 			((vtotal & 0x100) >> (8 - 0)) | ((vtotal & 0x200) >> (9 - 5)) |
22768353368SRudolf Cornelissen 			((vdisp_e & 0x100) >> (8 - 1)) | ((vdisp_e & 0x200) >> (9 - 6)) |
22868353368SRudolf Cornelissen 			((vsync_s & 0x100) >> (8 - 2)) | ((vsync_s & 0x200) >> (9 - 7)) |
22968353368SRudolf Cornelissen 			((vblnk_s & 0x100) >> (8 - 3)) | ((linecomp & 0x100) >> (8 - 4))
23068353368SRudolf Cornelissen 		));
23168353368SRudolf Cornelissen 		CRTCW(PRROWSCN, 0x00); /* not used */
23268353368SRudolf Cornelissen 		CRTCW(MAXSCLIN, (((vblnk_s & 0x200) >> (9 - 5)) | ((linecomp & 0x200) >> (9 - 6))));
23368353368SRudolf Cornelissen 		CRTCW(VSYNCS, (vsync_s & 0xff));
23468353368SRudolf Cornelissen 		CRTCW(VSYNCE, ((CRTCR(VSYNCE) & 0xf0) | (vsync_e & 0x0f)));
23568353368SRudolf Cornelissen 		CRTCW(VDISPE, (vdisp_e & 0xff));
23668353368SRudolf Cornelissen 		CRTCW(VBLANKS, (vblnk_s & 0xff));
23768353368SRudolf Cornelissen 		CRTCW(VBLANKE, (vblnk_e & 0xff));
23868353368SRudolf Cornelissen 		CRTCW(LINECOMP, (linecomp & 0xff));
23968353368SRudolf Cornelissen 
24068353368SRudolf Cornelissen 		/* horizontal extended regs */
24168353368SRudolf Cornelissen 		//fixme: we reset bit4. is this correct??
24268353368SRudolf Cornelissen 		CRTCW(HEB, (CRTCR(HEB) & 0xe0) |
24368353368SRudolf Cornelissen 			(
24468353368SRudolf Cornelissen 		 	((htotal & 0x100) >> (8 - 0)) |
24568353368SRudolf Cornelissen 			((hdisp_e & 0x100) >> (8 - 1)) |
24668353368SRudolf Cornelissen 			((hblnk_s & 0x100) >> (8 - 2)) |
24768353368SRudolf Cornelissen 			((hsync_s & 0x100) >> (8 - 3))
24868353368SRudolf Cornelissen 			));
24968353368SRudolf Cornelissen 
25068353368SRudolf Cornelissen 		/* (mostly) vertical extended regs */
25168353368SRudolf Cornelissen 		CRTCW(LSR,
25268353368SRudolf Cornelissen 			(
25368353368SRudolf Cornelissen 		 	((vtotal & 0x400) >> (10 - 0)) |
25468353368SRudolf Cornelissen 			((vdisp_e & 0x400) >> (10 - 1)) |
25568353368SRudolf Cornelissen 			((vsync_s & 0x400) >> (10 - 2)) |
25668353368SRudolf Cornelissen 			((vblnk_s & 0x400) >> (10 - 3)) |
25768353368SRudolf Cornelissen 			((hblnk_e & 0x040) >> (6 - 4))
25868353368SRudolf Cornelissen 			//fixme: we still miss one linecomp bit!?! is this it??
25968353368SRudolf Cornelissen 			//| ((linecomp & 0x400) >> 3)
26068353368SRudolf Cornelissen 			));
26168353368SRudolf Cornelissen 
26268353368SRudolf Cornelissen 		/* more vertical extended regs (on GeForce cards only) */
26368353368SRudolf Cornelissen 		if (si->ps.card_arch >= NV10A)
26468353368SRudolf Cornelissen 		{
26568353368SRudolf Cornelissen 			CRTCW(EXTRA,
26668353368SRudolf Cornelissen 				(
26768353368SRudolf Cornelissen 			 	((vtotal & 0x800) >> (11 - 0)) |
26868353368SRudolf Cornelissen 				((vdisp_e & 0x800) >> (11 - 2)) |
26968353368SRudolf Cornelissen 				((vsync_s & 0x800) >> (11 - 4)) |
27068353368SRudolf Cornelissen 				((vblnk_s & 0x800) >> (11 - 6))
27168353368SRudolf Cornelissen 				//fixme: do we miss another linecomp bit!?!
27268353368SRudolf Cornelissen 				));
27368353368SRudolf Cornelissen 		}
27468353368SRudolf Cornelissen 
27568353368SRudolf Cornelissen 		/* setup 'large screen' mode */
27668353368SRudolf Cornelissen 		if (target.timing.h_display >= 1280)
27768353368SRudolf Cornelissen 			CRTCW(REPAINT1, (CRTCR(REPAINT1) & 0xfb));
27868353368SRudolf Cornelissen 		else
27968353368SRudolf Cornelissen 			CRTCW(REPAINT1, (CRTCR(REPAINT1) | 0x04));
28068353368SRudolf Cornelissen 
28168353368SRudolf Cornelissen 		/* setup HSYNC & VSYNC polarity */
28268353368SRudolf Cornelissen 		LOG(2,("CRTC: sync polarity: "));
283*bf1feef8SRudolf Cornelissen 		temp = ENG_REG8(RG8_MISCR);
28468353368SRudolf Cornelissen 		if (target.timing.flags & B_POSITIVE_HSYNC)
28568353368SRudolf Cornelissen 		{
28668353368SRudolf Cornelissen 			LOG(2,("H:pos "));
28768353368SRudolf Cornelissen 			temp &= ~0x40;
28868353368SRudolf Cornelissen 		}
28968353368SRudolf Cornelissen 		else
29068353368SRudolf Cornelissen 		{
29168353368SRudolf Cornelissen 			LOG(2,("H:neg "));
29268353368SRudolf Cornelissen 			temp |= 0x40;
29368353368SRudolf Cornelissen 		}
29468353368SRudolf Cornelissen 		if (target.timing.flags & B_POSITIVE_VSYNC)
29568353368SRudolf Cornelissen 		{
29668353368SRudolf Cornelissen 			LOG(2,("V:pos "));
29768353368SRudolf Cornelissen 			temp &= ~0x80;
29868353368SRudolf Cornelissen 		}
29968353368SRudolf Cornelissen 		else
30068353368SRudolf Cornelissen 		{
30168353368SRudolf Cornelissen 			LOG(2,("V:neg "));
30268353368SRudolf Cornelissen 			temp |= 0x80;
30368353368SRudolf Cornelissen 		}
304*bf1feef8SRudolf Cornelissen 		ENG_REG8(RG8_MISCW) = temp;
30568353368SRudolf Cornelissen 
306*bf1feef8SRudolf Cornelissen 		LOG(2,(", MISC reg readback: $%02x\n", ENG_REG8(RG8_MISCR)));
30768353368SRudolf Cornelissen 	}
30868353368SRudolf Cornelissen 
30968353368SRudolf Cornelissen 	/* always disable interlaced operation */
31068353368SRudolf Cornelissen 	/* (interlace is supported on upto and including NV10, NV15, and NV30 and up) */
31168353368SRudolf Cornelissen 	CRTCW(INTERLACE, 0xff);
31268353368SRudolf Cornelissen 
31368353368SRudolf Cornelissen 	/* disable CRTC slaved mode unless a panel is in use */
31468353368SRudolf Cornelissen 	// fixme: this kills TVout when it was in use...
31568353368SRudolf Cornelissen 	if (!si->ps.tmds1_active) CRTCW(PIXEL, (CRTCR(PIXEL) & 0x7f));
31668353368SRudolf Cornelissen 
31768353368SRudolf Cornelissen 	/* setup flatpanel if connected and active */
31868353368SRudolf Cornelissen 	if (si->ps.tmds1_active)
31968353368SRudolf Cornelissen 	{
32068353368SRudolf Cornelissen 		uint32 iscale_x, iscale_y;
32168353368SRudolf Cornelissen 
32268353368SRudolf Cornelissen 		/* calculate inverse scaling factors used by hardware in 20.12 format */
32368353368SRudolf Cornelissen 		iscale_x = (((1 << 12) * target.timing.h_display) / si->ps.p1_timing.h_display);
32468353368SRudolf Cornelissen 		iscale_y = (((1 << 12) * target.timing.v_display) / si->ps.p1_timing.v_display);
32568353368SRudolf Cornelissen 
32668353368SRudolf Cornelissen 		/* unblock flatpanel timing programming (or something like that..) */
32768353368SRudolf Cornelissen 		CRTCW(FP_HTIMING, 0);
32868353368SRudolf Cornelissen 		CRTCW(FP_VTIMING, 0);
32968353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_HTIMING reg readback: $%02x\n", CRTCR(FP_HTIMING)));
33068353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_VTIMING reg readback: $%02x\n", CRTCR(FP_VTIMING)));
33168353368SRudolf Cornelissen 
33268353368SRudolf Cornelissen 		/* enable full width visibility on flatpanel */
33368353368SRudolf Cornelissen 		DACW(FP_HVALID_S, 0);
33468353368SRudolf Cornelissen 		DACW(FP_HVALID_E, (si->ps.p1_timing.h_display - 1));
33568353368SRudolf Cornelissen 		/* enable full height visibility on flatpanel */
33668353368SRudolf Cornelissen 		DACW(FP_VVALID_S, 0);
33768353368SRudolf Cornelissen 		DACW(FP_VVALID_E, (si->ps.p1_timing.v_display - 1));
33868353368SRudolf Cornelissen 
33968353368SRudolf Cornelissen 		/* nVidia cards support upscaling except on ??? */
34068353368SRudolf Cornelissen 		/* NV11 cards can upscale after all! */
34168353368SRudolf Cornelissen 		if (0)//si->ps.card_type == NV11)
34268353368SRudolf Cornelissen 		{
34368353368SRudolf Cornelissen 			/* disable last fetched line limiting */
34468353368SRudolf Cornelissen 			DACW(FP_DEBUG2, 0x00000000);
34568353368SRudolf Cornelissen 			/* inform panel to scale if needed */
34668353368SRudolf Cornelissen 			if ((iscale_x != (1 << 12)) || (iscale_y != (1 << 12)))
34768353368SRudolf Cornelissen 			{
34868353368SRudolf Cornelissen 				LOG(2,("CRTC: DFP needs to do scaling\n"));
34968353368SRudolf Cornelissen 				DACW(FP_TG_CTRL, (DACR(FP_TG_CTRL) | 0x00000100));
35068353368SRudolf Cornelissen 			}
35168353368SRudolf Cornelissen 			else
35268353368SRudolf Cornelissen 			{
35368353368SRudolf Cornelissen 				LOG(2,("CRTC: no scaling for DFP needed\n"));
35468353368SRudolf Cornelissen 				DACW(FP_TG_CTRL, (DACR(FP_TG_CTRL) & 0xfffffeff));
35568353368SRudolf Cornelissen 			}
35668353368SRudolf Cornelissen 		}
35768353368SRudolf Cornelissen 		else
35868353368SRudolf Cornelissen 		{
35968353368SRudolf Cornelissen 			float dm_aspect;
36068353368SRudolf Cornelissen 
36168353368SRudolf Cornelissen 			LOG(2,("CRTC: GPU scales for DFP if needed\n"));
36268353368SRudolf Cornelissen 
36368353368SRudolf Cornelissen 			/* calculate display mode aspect */
36468353368SRudolf Cornelissen 			dm_aspect = (target.timing.h_display / ((float)target.timing.v_display));
36568353368SRudolf Cornelissen 
36668353368SRudolf Cornelissen 			/* limit last fetched line if vertical scaling is done */
36768353368SRudolf Cornelissen 			if (iscale_y != (1 << 12))
36868353368SRudolf Cornelissen 				DACW(FP_DEBUG2, ((1 << 28) | ((target.timing.v_display - 1) << 16)));
36968353368SRudolf Cornelissen 			else
37068353368SRudolf Cornelissen 				DACW(FP_DEBUG2, 0x00000000);
37168353368SRudolf Cornelissen 
37268353368SRudolf Cornelissen 			/* inform panel not to scale */
37368353368SRudolf Cornelissen 			DACW(FP_TG_CTRL, (DACR(FP_TG_CTRL) & 0xfffffeff));
37468353368SRudolf Cornelissen 
37568353368SRudolf Cornelissen 			/* GPU scaling is automatically setup by hardware, so only modify this
37668353368SRudolf Cornelissen 			 * scalingfactor for non 4:3 (1.33) aspect panels;
37768353368SRudolf Cornelissen 			 * let's consider 1280x1024 1:33 aspect (it's 1.25 aspect actually!) */
37868353368SRudolf Cornelissen 
37968353368SRudolf Cornelissen 			/* correct for widescreen panels relative to mode...
38068353368SRudolf Cornelissen 			 * (so if panel is more widescreen than mode being set) */
38168353368SRudolf Cornelissen 			/* BTW: known widescreen panels:
38268353368SRudolf Cornelissen 			 * 1280 x  800 (1.60),
38368353368SRudolf Cornelissen 			 * 1440 x  900 (1.60),
38468353368SRudolf Cornelissen 			 * 1680 x 1050 (1.60),
38568353368SRudolf Cornelissen 			 * 1920 x 1200 (1.60). */
38668353368SRudolf Cornelissen 			/* known 4:3 aspect non-standard resolution panels:
38768353368SRudolf Cornelissen 			 * 1400 x 1050 (1.33). */
38868353368SRudolf Cornelissen 			/* NOTE:
38968353368SRudolf Cornelissen 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
39068353368SRudolf Cornelissen 			if ((iscale_x != (1 << 12)) && (si->ps.panel1_aspect > (dm_aspect + 0.10)))
39168353368SRudolf Cornelissen 			{
39268353368SRudolf Cornelissen 				uint16 diff;
39368353368SRudolf Cornelissen 
39468353368SRudolf Cornelissen 				LOG(2,("CRTC: (relative) widescreen panel: tuning horizontal scaling\n"));
39568353368SRudolf Cornelissen 
39668353368SRudolf Cornelissen 				/* X-scaling should be the same as Y-scaling */
39768353368SRudolf Cornelissen 				iscale_x = iscale_y;
39868353368SRudolf Cornelissen 				/* enable testmode (b12) and program modified X-scaling factor */
39968353368SRudolf Cornelissen 				DACW(FP_DEBUG1, (((iscale_x >> 1) & 0x00000fff) | (1 << 12)));
40068353368SRudolf Cornelissen 				/* center/cut-off left and right side of screen */
40168353368SRudolf Cornelissen 				diff = ((si->ps.p1_timing.h_display -
40268353368SRudolf Cornelissen 						(target.timing.h_display * ((1 << 12) / ((float)iscale_x))))
40368353368SRudolf Cornelissen 						/ 2);
40468353368SRudolf Cornelissen 				DACW(FP_HVALID_S, diff);
40568353368SRudolf Cornelissen 				DACW(FP_HVALID_E, ((si->ps.p1_timing.h_display - diff) - 1));
40668353368SRudolf Cornelissen 			}
40768353368SRudolf Cornelissen 			/* correct for portrait panels... */
40868353368SRudolf Cornelissen 			/* NOTE:
40968353368SRudolf Cornelissen 			 * allow 0.10 difference so 1280x1024 panels will be used fullscreen! */
41068353368SRudolf Cornelissen 			if ((iscale_y != (1 << 12)) && (si->ps.panel1_aspect < (dm_aspect - 0.10)))
41168353368SRudolf Cornelissen 			{
41268353368SRudolf Cornelissen 				LOG(2,("CRTC: (relative) portrait panel: should tune vertical scaling\n"));
41368353368SRudolf Cornelissen 				/* fixme: implement if this kind of portrait panels exist on nVidia... */
41468353368SRudolf Cornelissen 			}
41568353368SRudolf Cornelissen 		}
41668353368SRudolf Cornelissen 
41768353368SRudolf Cornelissen 		/* do some logging.. */
41868353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_HVALID_S reg readback: $%08x\n", DACR(FP_HVALID_S)));
41968353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_HVALID_E reg readback: $%08x\n", DACR(FP_HVALID_E)));
42068353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_VVALID_S reg readback: $%08x\n", DACR(FP_VVALID_S)));
42168353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_VVALID_E reg readback: $%08x\n", DACR(FP_VVALID_E)));
42268353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_DEBUG0 reg readback: $%08x\n", DACR(FP_DEBUG0)));
42368353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_DEBUG1 reg readback: $%08x\n", DACR(FP_DEBUG1)));
42468353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_DEBUG2 reg readback: $%08x\n", DACR(FP_DEBUG2)));
42568353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_DEBUG3 reg readback: $%08x\n", DACR(FP_DEBUG3)));
42668353368SRudolf Cornelissen 		LOG(2,("CRTC: FP_TG_CTRL reg readback: $%08x\n", DACR(FP_TG_CTRL)));
42768353368SRudolf Cornelissen 	}
42868353368SRudolf Cornelissen 
42968353368SRudolf Cornelissen 	return B_OK;
43068353368SRudolf Cornelissen }
43168353368SRudolf Cornelissen 
eng_crtc_depth(int mode)432886dbf81SRudolf Cornelissen status_t eng_crtc_depth(int mode)
43368353368SRudolf Cornelissen {
43468353368SRudolf Cornelissen 	uint8 viddelay = 0;
43568353368SRudolf Cornelissen 	uint32 genctrl = 0;
43668353368SRudolf Cornelissen 
43768353368SRudolf Cornelissen 	/* set VCLK scaling */
43868353368SRudolf Cornelissen 	switch(mode)
43968353368SRudolf Cornelissen 	{
44068353368SRudolf Cornelissen 	case BPP8:
44168353368SRudolf Cornelissen 		viddelay = 0x01;
44268353368SRudolf Cornelissen 		/* genctrl b4 & b5 reset: 'direct mode' */
44368353368SRudolf Cornelissen 		genctrl = 0x00101100;
44468353368SRudolf Cornelissen 		break;
44568353368SRudolf Cornelissen 	case BPP15:
44668353368SRudolf Cornelissen 		viddelay = 0x02;
44768353368SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
44868353368SRudolf Cornelissen 		genctrl = 0x00100130;
44968353368SRudolf Cornelissen 		break;
45068353368SRudolf Cornelissen 	case BPP16:
45168353368SRudolf Cornelissen 		viddelay = 0x02;
45268353368SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
45368353368SRudolf Cornelissen 		genctrl = 0x00101130;
45468353368SRudolf Cornelissen 		break;
45568353368SRudolf Cornelissen 	case BPP24:
45668353368SRudolf Cornelissen 		viddelay = 0x03;
45768353368SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
45868353368SRudolf Cornelissen 		genctrl = 0x00100130;
45968353368SRudolf Cornelissen 		break;
46068353368SRudolf Cornelissen 	case BPP32:
46168353368SRudolf Cornelissen 		viddelay = 0x03;
46268353368SRudolf Cornelissen 		/* genctrl b4 & b5 set: 'indirect mode' (via colorpalette) */
46368353368SRudolf Cornelissen 		genctrl = 0x00101130;
46468353368SRudolf Cornelissen 		break;
46568353368SRudolf Cornelissen 	}
46668353368SRudolf Cornelissen 	/* enable access to primary head */
46768353368SRudolf Cornelissen 	set_crtc_owner(0);
46868353368SRudolf Cornelissen 
46968353368SRudolf Cornelissen 	CRTCW(PIXEL, ((CRTCR(PIXEL) & 0xfc) | viddelay));
47068353368SRudolf Cornelissen 	DACW(GENCTRL, genctrl);
47168353368SRudolf Cornelissen 
47268353368SRudolf Cornelissen 	return B_OK;
47368353368SRudolf Cornelissen }
47468353368SRudolf Cornelissen 
eng_crtc_dpms(bool display,bool h,bool v)475886dbf81SRudolf Cornelissen status_t eng_crtc_dpms(bool display, bool h, bool v)
47668353368SRudolf Cornelissen {
47768353368SRudolf Cornelissen 	uint8 temp;
47868353368SRudolf Cornelissen 
47968353368SRudolf Cornelissen 	LOG(4,("CRTC: setting DPMS: "));
48068353368SRudolf Cornelissen 
48168353368SRudolf Cornelissen 	/* enable access to primary head */
48268353368SRudolf Cornelissen 	set_crtc_owner(0);
48368353368SRudolf Cornelissen 
48468353368SRudolf Cornelissen 	/* start synchronous reset: required before turning screen off! */
48568353368SRudolf Cornelissen 	SEQW(RESET, 0x01);
48668353368SRudolf Cornelissen 
48768353368SRudolf Cornelissen 	/* turn screen off */
48868353368SRudolf Cornelissen 	temp = SEQR(CLKMODE);
48968353368SRudolf Cornelissen 	if (display)
49068353368SRudolf Cornelissen 	{
49168353368SRudolf Cornelissen 		SEQW(CLKMODE, (temp & ~0x20));
49268353368SRudolf Cornelissen 
49368353368SRudolf Cornelissen 		/* end synchronous reset if display should be enabled */
49468353368SRudolf Cornelissen 		SEQW(RESET, 0x03);
49568353368SRudolf Cornelissen 
49668353368SRudolf Cornelissen 		//'safe mode' test! feedback needed with this 'setting'!
49768353368SRudolf Cornelissen 		if (0)//si->ps.tmds1_active)
49868353368SRudolf Cornelissen 		{
49968353368SRudolf Cornelissen 			/* powerup both LVDS (laptop panellink) and TMDS (DVI panellink)
50068353368SRudolf Cornelissen 			 * internal transmitters... */
50168353368SRudolf Cornelissen 			/* note:
50268353368SRudolf Cornelissen 			 * the powerbits in this register are hardwired to the DVI connectors,
50368353368SRudolf Cornelissen 			 * instead of to the DACs! (confirmed NV34) */
50468353368SRudolf Cornelissen 			//fixme...
50568353368SRudolf Cornelissen 			DACW(FP_DEBUG0, (DACR(FP_DEBUG0) & 0xcfffffff));
50668353368SRudolf Cornelissen 			/* ... and powerup external TMDS transmitter if it exists */
50768353368SRudolf Cornelissen 			/* (confirmed OK on NV28 and NV34) */
50868353368SRudolf Cornelissen 			CRTCW(0x59, (CRTCR(0x59) | 0x01));
50968353368SRudolf Cornelissen 		}
51068353368SRudolf Cornelissen 
51168353368SRudolf Cornelissen 		LOG(4,("display on, "));
51268353368SRudolf Cornelissen 	}
51368353368SRudolf Cornelissen 	else
51468353368SRudolf Cornelissen 	{
51568353368SRudolf Cornelissen 		SEQW(CLKMODE, (temp | 0x20));
51668353368SRudolf Cornelissen 
51768353368SRudolf Cornelissen 		//'safe mode' test! feedback needed with this 'setting'!
51868353368SRudolf Cornelissen 		if (0)//si->ps.tmds1_active)
51968353368SRudolf Cornelissen 		{
52068353368SRudolf Cornelissen 			/* powerdown both LVDS (laptop panellink) and TMDS (DVI panellink)
52168353368SRudolf Cornelissen 			 * internal transmitters... */
52268353368SRudolf Cornelissen 			/* note:
52368353368SRudolf Cornelissen 			 * the powerbits in this register are hardwired to the DVI connectors,
52468353368SRudolf Cornelissen 			 * instead of to the DACs! (confirmed NV34) */
52568353368SRudolf Cornelissen 			//fixme...
52668353368SRudolf Cornelissen 			DACW(FP_DEBUG0, (DACR(FP_DEBUG0) | 0x30000000));
52768353368SRudolf Cornelissen 			/* ... and powerdown external TMDS transmitter if it exists */
52868353368SRudolf Cornelissen 			/* (confirmed OK on NV28 and NV34) */
52968353368SRudolf Cornelissen 			CRTCW(0x59, (CRTCR(0x59) & 0xfe));
53068353368SRudolf Cornelissen 		}
53168353368SRudolf Cornelissen 
53268353368SRudolf Cornelissen 		LOG(4,("display off, "));
53368353368SRudolf Cornelissen 	}
53468353368SRudolf Cornelissen 
53568353368SRudolf Cornelissen 	if (h)
53668353368SRudolf Cornelissen 	{
53768353368SRudolf Cornelissen 		CRTCW(REPAINT1, (CRTCR(REPAINT1) & 0x7f));
53868353368SRudolf Cornelissen 		LOG(4,("hsync enabled, "));
53968353368SRudolf Cornelissen 	}
54068353368SRudolf Cornelissen 	else
54168353368SRudolf Cornelissen 	{
54268353368SRudolf Cornelissen 		CRTCW(REPAINT1, (CRTCR(REPAINT1) | 0x80));
54368353368SRudolf Cornelissen 		LOG(4,("hsync disabled, "));
54468353368SRudolf Cornelissen 	}
54568353368SRudolf Cornelissen 	if (v)
54668353368SRudolf Cornelissen 	{
54768353368SRudolf Cornelissen 		CRTCW(REPAINT1, (CRTCR(REPAINT1) & 0xbf));
54868353368SRudolf Cornelissen 		LOG(4,("vsync enabled\n"));
54968353368SRudolf Cornelissen 	}
55068353368SRudolf Cornelissen 	else
55168353368SRudolf Cornelissen 	{
55268353368SRudolf Cornelissen 		CRTCW(REPAINT1, (CRTCR(REPAINT1) | 0x40));
55368353368SRudolf Cornelissen 		LOG(4,("vsync disabled\n"));
55468353368SRudolf Cornelissen 	}
55568353368SRudolf Cornelissen 
55668353368SRudolf Cornelissen 	return B_OK;
55768353368SRudolf Cornelissen }
55868353368SRudolf Cornelissen 
eng_crtc_dpms_fetch(bool * display,bool * h,bool * v)559886dbf81SRudolf Cornelissen status_t eng_crtc_dpms_fetch(bool *display, bool *h, bool *v)
56068353368SRudolf Cornelissen {
56168353368SRudolf Cornelissen 	/* enable access to primary head */
56268353368SRudolf Cornelissen 	set_crtc_owner(0);
56368353368SRudolf Cornelissen 
56468353368SRudolf Cornelissen 	*display = !(SEQR(CLKMODE) & 0x20);
56568353368SRudolf Cornelissen 	*h = !(CRTCR(REPAINT1) & 0x80);
56668353368SRudolf Cornelissen 	*v = !(CRTCR(REPAINT1) & 0x40);
56768353368SRudolf Cornelissen 
56868353368SRudolf Cornelissen 	LOG(4,("CTRC: fetched DPMS state: "));
56968353368SRudolf Cornelissen 	if (*display) LOG(4,("display on, "));
57068353368SRudolf Cornelissen 	else LOG(4,("display off, "));
57168353368SRudolf Cornelissen 	if (*h) LOG(4,("hsync enabled, "));
57268353368SRudolf Cornelissen 	else LOG(4,("hsync disabled, "));
57368353368SRudolf Cornelissen 	if (*v) LOG(4,("vsync enabled\n"));
57468353368SRudolf Cornelissen 	else LOG(4,("vsync disabled\n"));
57568353368SRudolf Cornelissen 
57668353368SRudolf Cornelissen 	return B_OK;
57768353368SRudolf Cornelissen }
57868353368SRudolf Cornelissen 
eng_crtc_set_display_pitch()579886dbf81SRudolf Cornelissen status_t eng_crtc_set_display_pitch()
58068353368SRudolf Cornelissen {
58168353368SRudolf Cornelissen 	uint32 offset;
58268353368SRudolf Cornelissen 
58368353368SRudolf Cornelissen 	LOG(4,("CRTC: setting card pitch (offset between lines)\n"));
58468353368SRudolf Cornelissen 
58568353368SRudolf Cornelissen 	/* figure out offset value hardware needs */
58668353368SRudolf Cornelissen 	offset = si->fbc.bytes_per_row / 8;
58768353368SRudolf Cornelissen 
58868353368SRudolf Cornelissen 	LOG(2,("CRTC: offset register set to: $%04x\n", offset));
58968353368SRudolf Cornelissen 
59068353368SRudolf Cornelissen 	/* enable access to primary head */
59168353368SRudolf Cornelissen 	set_crtc_owner(0);
59268353368SRudolf Cornelissen 
59368353368SRudolf Cornelissen 	/* program the card */
59468353368SRudolf Cornelissen 	CRTCW(PITCHL, (offset & 0x00ff));
59568353368SRudolf Cornelissen 	CRTCW(REPAINT0, ((CRTCR(REPAINT0) & 0x1f) | ((offset & 0x0700) >> 3)));
59668353368SRudolf Cornelissen 
59768353368SRudolf Cornelissen 	return B_OK;
59868353368SRudolf Cornelissen }
59968353368SRudolf Cornelissen 
eng_crtc_set_display_start(uint32 startadd,uint8 bpp)600886dbf81SRudolf Cornelissen status_t eng_crtc_set_display_start(uint32 startadd,uint8 bpp)
60168353368SRudolf Cornelissen {
60268353368SRudolf Cornelissen 	uint8 temp;
60368353368SRudolf Cornelissen 	uint32 timeout = 0;
60468353368SRudolf Cornelissen 
60568353368SRudolf Cornelissen 	LOG(4,("CRTC: setting card RAM to be displayed bpp %d\n", bpp));
60668353368SRudolf Cornelissen 
60768353368SRudolf Cornelissen 	LOG(2,("CRTC: startadd: $%08x\n", startadd));
60868353368SRudolf Cornelissen 	LOG(2,("CRTC: frameRAM: $%08x\n", si->framebuffer));
60968353368SRudolf Cornelissen 	LOG(2,("CRTC: framebuffer: $%08x\n", si->fbc.frame_buffer));
61068353368SRudolf Cornelissen 
61168353368SRudolf Cornelissen 	/* we might have no retraces during setmode! */
61268353368SRudolf Cornelissen 	/* wait 25mS max. for retrace to occur (refresh > 40Hz) */
613*bf1feef8SRudolf Cornelissen 	while (((ENG_RG32(RG32_RASTER) & 0x000007ff) < si->dm.timing.v_display) &&
61468353368SRudolf Cornelissen 			(timeout < (25000/10)))
61568353368SRudolf Cornelissen 	{
61668353368SRudolf Cornelissen 		/* don't snooze much longer or retrace might get missed! */
61768353368SRudolf Cornelissen 		snooze(10);
61868353368SRudolf Cornelissen 		timeout++;
61968353368SRudolf Cornelissen 	}
62068353368SRudolf Cornelissen 
62168353368SRudolf Cornelissen 	/* enable access to primary head */
62268353368SRudolf Cornelissen 	set_crtc_owner(0);
62368353368SRudolf Cornelissen 
62468353368SRudolf Cornelissen 	if (si->ps.card_arch == NV04A)
62568353368SRudolf Cornelissen 	{
62668353368SRudolf Cornelissen 		/* upto 32Mb RAM adressing: must be used this way on pre-NV10! */
62768353368SRudolf Cornelissen 
62868353368SRudolf Cornelissen 		/* set standard registers */
62968353368SRudolf Cornelissen 		/* (NVidia: startadress in 32bit words (b2 - b17) */
63068353368SRudolf Cornelissen 		CRTCW(FBSTADDL, ((startadd & 0x000003fc) >> 2));
63168353368SRudolf Cornelissen 		CRTCW(FBSTADDH, ((startadd & 0x0003fc00) >> 10));
63268353368SRudolf Cornelissen 
63368353368SRudolf Cornelissen 		/* set extended registers */
63468353368SRudolf Cornelissen 		/* NV4 extended bits: (b18-22) */
63568353368SRudolf Cornelissen 		temp = (CRTCR(REPAINT0) & 0xe0);
63668353368SRudolf Cornelissen 		CRTCW(REPAINT0, (temp | ((startadd & 0x007c0000) >> 18)));
63768353368SRudolf Cornelissen 		/* NV4 extended bits: (b23-24) */
63868353368SRudolf Cornelissen 		temp = (CRTCR(HEB) & 0x9f);
63968353368SRudolf Cornelissen 		CRTCW(HEB, (temp | ((startadd & 0x01800000) >> 18)));
64068353368SRudolf Cornelissen 	}
64168353368SRudolf Cornelissen 	else
64268353368SRudolf Cornelissen 	{
64368353368SRudolf Cornelissen 		/* upto 4Gb RAM adressing: must be used on NV10 and later! */
64468353368SRudolf Cornelissen 		/* NOTE:
64568353368SRudolf Cornelissen 		 * While this register also exists on pre-NV10 cards, it will
64668353368SRudolf Cornelissen 		 * wrap-around at 16Mb boundaries!! */
64768353368SRudolf Cornelissen 
64868353368SRudolf Cornelissen 		/* 30bit adress in 32bit words */
649*bf1feef8SRudolf Cornelissen 		ENG_RG32(RG32_NV10FBSTADD32) = (startadd & 0xfffffffc);
65068353368SRudolf Cornelissen 	}
65168353368SRudolf Cornelissen 
65268353368SRudolf Cornelissen 	/* set NV4/NV10 byte adress: (b0 - 1) */
65368353368SRudolf Cornelissen 	ATBW(HORPIXPAN, ((startadd & 0x00000003) << 1));
65468353368SRudolf Cornelissen 
65568353368SRudolf Cornelissen 	return B_OK;
65668353368SRudolf Cornelissen }
65768353368SRudolf Cornelissen 
eng_crtc_cursor_init()658886dbf81SRudolf Cornelissen status_t eng_crtc_cursor_init()
65968353368SRudolf Cornelissen {
66068353368SRudolf Cornelissen 	int i;
66168353368SRudolf Cornelissen 	uint32 * fb;
66268353368SRudolf Cornelissen 	/* cursor bitmap will be stored at the start of the framebuffer */
66368353368SRudolf Cornelissen 	const uint32 curadd = 0;
66468353368SRudolf Cornelissen 
66568353368SRudolf Cornelissen 	/* enable access to primary head */
66668353368SRudolf Cornelissen 	set_crtc_owner(0);
66768353368SRudolf Cornelissen 
66868353368SRudolf Cornelissen 	/* set cursor bitmap adress ... */
66968353368SRudolf Cornelissen 	if ((si->ps.card_arch == NV04A) || (si->ps.laptop))
67068353368SRudolf Cornelissen 	{
67168353368SRudolf Cornelissen 		/* must be used this way on pre-NV10 and on all 'Go' cards! */
67268353368SRudolf Cornelissen 
67368353368SRudolf Cornelissen 		/* cursorbitmap must start on 2Kbyte boundary: */
67468353368SRudolf Cornelissen 		/* set adress bit11-16, and set 'no doublescan' (registerbit 1 = 0) */
67568353368SRudolf Cornelissen 		CRTCW(CURCTL0, ((curadd & 0x0001f800) >> 9));
67668353368SRudolf Cornelissen 		/* set adress bit17-23, and set graphics mode cursor(?) (registerbit 7 = 1) */
67768353368SRudolf Cornelissen 		CRTCW(CURCTL1, (((curadd & 0x00fe0000) >> 17) | 0x80));
67868353368SRudolf Cornelissen 		/* set adress bit24-31 */
67968353368SRudolf Cornelissen 		CRTCW(CURCTL2, ((curadd & 0xff000000) >> 24));
68068353368SRudolf Cornelissen 	}
68168353368SRudolf Cornelissen 	else
68268353368SRudolf Cornelissen 	{
68368353368SRudolf Cornelissen 		/* upto 4Gb RAM adressing:
68468353368SRudolf Cornelissen 		 * can be used on NV10 and later (except for 'Go' cards)! */
68568353368SRudolf Cornelissen 		/* NOTE:
68668353368SRudolf Cornelissen 		 * This register does not exist on pre-NV10 and 'Go' cards. */
68768353368SRudolf Cornelissen 
68868353368SRudolf Cornelissen 		/* cursorbitmap must still start on 2Kbyte boundary: */
689*bf1feef8SRudolf Cornelissen 		ENG_RG32(RG32_NV10CURADD32) = (curadd & 0xfffff800);
69068353368SRudolf Cornelissen 	}
69168353368SRudolf Cornelissen 
69268353368SRudolf Cornelissen 	/* set cursor colour: not needed because of direct nature of cursor bitmap. */
69368353368SRudolf Cornelissen 
69468353368SRudolf Cornelissen 	/*clear cursor*/
69568353368SRudolf Cornelissen 	fb = (uint32 *) si->framebuffer + curadd;
69668353368SRudolf Cornelissen 	for (i=0;i<(2048/4);i++)
69768353368SRudolf Cornelissen 	{
69868353368SRudolf Cornelissen 		fb[i]=0;
69968353368SRudolf Cornelissen 	}
70068353368SRudolf Cornelissen 
70168353368SRudolf Cornelissen 	/* select 32x32 pixel, 16bit color cursorbitmap, no doublescan */
702*bf1feef8SRudolf Cornelissen 	ENG_RG32(RG32_CURCONF) = 0x02000100;
70368353368SRudolf Cornelissen 
70468353368SRudolf Cornelissen 	/* activate hardware cursor */
705886dbf81SRudolf Cornelissen 	eng_crtc_cursor_show();
70668353368SRudolf Cornelissen 
70768353368SRudolf Cornelissen 	return B_OK;
70868353368SRudolf Cornelissen }
70968353368SRudolf Cornelissen 
eng_crtc_cursor_show()710886dbf81SRudolf Cornelissen status_t eng_crtc_cursor_show()
71168353368SRudolf Cornelissen {
71268353368SRudolf Cornelissen 	LOG(4,("CRTC: enabling cursor\n"));
71368353368SRudolf Cornelissen 
71468353368SRudolf Cornelissen 	/* enable access to CRTC1 on dualhead cards */
71568353368SRudolf Cornelissen 	set_crtc_owner(0);
71668353368SRudolf Cornelissen 
71768353368SRudolf Cornelissen 	/* b0 = 1 enables cursor */
71868353368SRudolf Cornelissen 	CRTCW(CURCTL0, (CRTCR(CURCTL0) | 0x01));
71968353368SRudolf Cornelissen 
72068353368SRudolf Cornelissen 	return B_OK;
72168353368SRudolf Cornelissen }
72268353368SRudolf Cornelissen 
eng_crtc_cursor_hide()723886dbf81SRudolf Cornelissen status_t eng_crtc_cursor_hide()
72468353368SRudolf Cornelissen {
72568353368SRudolf Cornelissen 	LOG(4,("CRTC: disabling cursor\n"));
72668353368SRudolf Cornelissen 
72768353368SRudolf Cornelissen 	/* enable access to primary head */
72868353368SRudolf Cornelissen 	set_crtc_owner(0);
72968353368SRudolf Cornelissen 
73068353368SRudolf Cornelissen 	/* b0 = 0 disables cursor */
73168353368SRudolf Cornelissen 	CRTCW(CURCTL0, (CRTCR(CURCTL0) & 0xfe));
73268353368SRudolf Cornelissen 
73368353368SRudolf Cornelissen 	return B_OK;
73468353368SRudolf Cornelissen }
73568353368SRudolf Cornelissen 
73668353368SRudolf Cornelissen /*set up cursor shape*/
eng_crtc_cursor_define(uint8 * andMask,uint8 * xorMask)737886dbf81SRudolf Cornelissen status_t eng_crtc_cursor_define(uint8* andMask,uint8* xorMask)
73868353368SRudolf Cornelissen {
73968353368SRudolf Cornelissen 	int x, y;
74068353368SRudolf Cornelissen 	uint8 b;
74168353368SRudolf Cornelissen 	uint16 *cursor;
74268353368SRudolf Cornelissen 	uint16 pixel;
74368353368SRudolf Cornelissen 
74468353368SRudolf Cornelissen 	/* get a pointer to the cursor */
74568353368SRudolf Cornelissen 	cursor = (uint16*) si->framebuffer;
74668353368SRudolf Cornelissen 
74768353368SRudolf Cornelissen 	/* draw the cursor */
74868353368SRudolf Cornelissen 	/* (Nvidia cards have a RGB15 direct color cursor bitmap, bit #16 is transparancy) */
74968353368SRudolf Cornelissen 	for (y = 0; y < 16; y++)
75068353368SRudolf Cornelissen 	{
75168353368SRudolf Cornelissen 		b = 0x80;
75268353368SRudolf Cornelissen 		for (x = 0; x < 8; x++)
75368353368SRudolf Cornelissen 		{
75468353368SRudolf Cornelissen 			/* preset transparant */
75568353368SRudolf Cornelissen 			pixel = 0x0000;
75668353368SRudolf Cornelissen 			/* set white if requested */
75768353368SRudolf Cornelissen 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
75868353368SRudolf Cornelissen 			/* set black if requested */
75968353368SRudolf Cornelissen 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
76068353368SRudolf Cornelissen 			/* set invert if requested */
76168353368SRudolf Cornelissen 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
76268353368SRudolf Cornelissen 			/* place the pixel in the bitmap */
76368353368SRudolf Cornelissen 			cursor[x + (y * 32)] = pixel;
76468353368SRudolf Cornelissen 			b >>= 1;
76568353368SRudolf Cornelissen 		}
76668353368SRudolf Cornelissen 		xorMask++;
76768353368SRudolf Cornelissen 		andMask++;
76868353368SRudolf Cornelissen 		b = 0x80;
76968353368SRudolf Cornelissen 		for (; x < 16; x++)
77068353368SRudolf Cornelissen 		{
77168353368SRudolf Cornelissen 			/* preset transparant */
77268353368SRudolf Cornelissen 			pixel = 0x0000;
77368353368SRudolf Cornelissen 			/* set white if requested */
77468353368SRudolf Cornelissen 			if ((!(*andMask & b)) && (!(*xorMask & b))) pixel = 0xffff;
77568353368SRudolf Cornelissen 			/* set black if requested */
77668353368SRudolf Cornelissen 			if ((!(*andMask & b)) &&   (*xorMask & b))  pixel = 0x8000;
77768353368SRudolf Cornelissen 			/* set invert if requested */
77868353368SRudolf Cornelissen 			if (  (*andMask & b)  &&   (*xorMask & b))  pixel = 0x7fff;
77968353368SRudolf Cornelissen 			/* place the pixel in the bitmap */
78068353368SRudolf Cornelissen 			cursor[x + (y * 32)] = pixel;
78168353368SRudolf Cornelissen 			b >>= 1;
78268353368SRudolf Cornelissen 		}
78368353368SRudolf Cornelissen 		xorMask++;
78468353368SRudolf Cornelissen 		andMask++;
78568353368SRudolf Cornelissen 	}
78668353368SRudolf Cornelissen 
78768353368SRudolf Cornelissen 	return B_OK;
78868353368SRudolf Cornelissen }
78968353368SRudolf Cornelissen 
79068353368SRudolf Cornelissen /* position the cursor */
eng_crtc_cursor_position(uint16 x,uint16 y)791886dbf81SRudolf Cornelissen status_t eng_crtc_cursor_position(uint16 x, uint16 y)
79268353368SRudolf Cornelissen {
79368353368SRudolf Cornelissen 	uint16 yhigh;
79468353368SRudolf Cornelissen 
79568353368SRudolf Cornelissen 	/* make sure we are beyond the first line of the cursorbitmap being drawn during
79668353368SRudolf Cornelissen 	 * updating the position to prevent distortions: no double buffering feature */
79768353368SRudolf Cornelissen 	/* Note:
79868353368SRudolf Cornelissen 	 * we need to return as quick as possible or some apps will exhibit lagging.. */
79968353368SRudolf Cornelissen 
80068353368SRudolf Cornelissen 	/* read the old cursor Y position */
80168353368SRudolf Cornelissen 	yhigh = ((DACR(CURPOS) & 0x0fff0000) >> 16);
80268353368SRudolf Cornelissen 	/* make sure we will wait until we are below both the old and new Y position:
80368353368SRudolf Cornelissen 	 * visible cursorbitmap drawing needs to be done at least... */
80468353368SRudolf Cornelissen 	if (y > yhigh) yhigh = y;
80568353368SRudolf Cornelissen 
80668353368SRudolf Cornelissen 	if (yhigh < (si->dm.timing.v_display - 16))
80768353368SRudolf Cornelissen 	{
80868353368SRudolf Cornelissen 		/* we have vertical lines below old and new cursorposition to spare. So we
80968353368SRudolf Cornelissen 		 * update the cursor postion 'mid-screen', but below that area. */
810*bf1feef8SRudolf Cornelissen 		while (((uint16)(ENG_RG32(RG32_RASTER) & 0x000007ff)) < (yhigh + 16))
81168353368SRudolf Cornelissen 		{
81268353368SRudolf Cornelissen 			snooze(10);
81368353368SRudolf Cornelissen 		}
81468353368SRudolf Cornelissen 	}
81568353368SRudolf Cornelissen 	else
81668353368SRudolf Cornelissen 	{
81768353368SRudolf Cornelissen 		/* no room to spare, just wait for retrace (is relatively slow) */
818*bf1feef8SRudolf Cornelissen 		while ((ENG_RG32(RG32_RASTER) & 0x000007ff) < si->dm.timing.v_display)
81968353368SRudolf Cornelissen 		{
82068353368SRudolf Cornelissen 			/* don't snooze much longer or retrace might get missed! */
82168353368SRudolf Cornelissen 			snooze(10);
82268353368SRudolf Cornelissen 		}
82368353368SRudolf Cornelissen 	}
82468353368SRudolf Cornelissen 
82568353368SRudolf Cornelissen 	/* update cursorposition */
82668353368SRudolf Cornelissen 	DACW(CURPOS, ((x & 0x0fff) | ((y & 0x0fff) << 16)));
82768353368SRudolf Cornelissen 
82868353368SRudolf Cornelissen 	return B_OK;
82968353368SRudolf Cornelissen }
830