xref: /haiku/src/add-ons/accelerants/via/engine/dac2.c (revision a81f65eae5f9a0577ae33a0293c3a04acf36aa6d)
12beedcaeSRudolf Cornelissen /* program the secondary DAC */
22beedcaeSRudolf Cornelissen /* Author:
32beedcaeSRudolf Cornelissen    Rudolf Cornelissen 12/2003-9/2004
42beedcaeSRudolf Cornelissen */
52beedcaeSRudolf Cornelissen 
62beedcaeSRudolf Cornelissen #define MODULE_BIT 0x00001000
72beedcaeSRudolf Cornelissen 
82beedcaeSRudolf Cornelissen #include "std.h"
92beedcaeSRudolf Cornelissen 
102beedcaeSRudolf Cornelissen static status_t nv10_nv20_dac2_pix_pll_find(
112beedcaeSRudolf Cornelissen 	display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test);
122beedcaeSRudolf Cornelissen 
132beedcaeSRudolf Cornelissen /* see if an analog VGA monitor is connected to connector #2 */
eng_dac2_crt_connected()142beedcaeSRudolf Cornelissen bool eng_dac2_crt_connected()
152beedcaeSRudolf Cornelissen {
162beedcaeSRudolf Cornelissen 	uint32 output, dac;
172beedcaeSRudolf Cornelissen 	bool present;
182beedcaeSRudolf Cornelissen 
192beedcaeSRudolf Cornelissen 	/* NOTE:
202beedcaeSRudolf Cornelissen 	 * NV11 can't do this: It will report DAC1 status instead because it HAS no
212beedcaeSRudolf Cornelissen 	 * actual secondary DAC function. */
222beedcaeSRudolf Cornelissen 	/* (It DOES have a secondary palette RAM and pixelclock PLL though.) */
232beedcaeSRudolf Cornelissen 
242beedcaeSRudolf Cornelissen 	/* save output connector setting */
252beedcaeSRudolf Cornelissen 	output = DAC2R(OUTPUT);
262beedcaeSRudolf Cornelissen 	/* save DAC state */
272beedcaeSRudolf Cornelissen 	dac = DAC2R(TSTCTRL);
282beedcaeSRudolf Cornelissen 
292beedcaeSRudolf Cornelissen 	/* turn on DAC2 */
302beedcaeSRudolf Cornelissen 	DAC2W(TSTCTRL, (DAC2R(TSTCTRL) & 0xfffeffff));
312beedcaeSRudolf Cornelissen 	/* select primary head and turn off CRT (and DVI?) outputs */
322beedcaeSRudolf Cornelissen 	DAC2W(OUTPUT, (output & 0x0000feee));
332beedcaeSRudolf Cornelissen 	/* wait for signal lines to stabilize */
342beedcaeSRudolf Cornelissen 	snooze(1000);
352beedcaeSRudolf Cornelissen 	/* re-enable CRT output */
362beedcaeSRudolf Cornelissen 	DAC2W(OUTPUT, (DAC2R(OUTPUT) | 0x00000001));
372beedcaeSRudolf Cornelissen 
382beedcaeSRudolf Cornelissen 	/* setup RGB test signal levels to approx 30% of DAC range and enable them
392beedcaeSRudolf Cornelissen 	 * (NOTE: testsignal function block resides in DAC1 only (!)) */
402beedcaeSRudolf Cornelissen 	DACW(TSTDATA, ((0x2 << 30) | (0x140 << 20) | (0x140 << 10) | (0x140 << 0)));
412beedcaeSRudolf Cornelissen 	/* route test signals to output
422beedcaeSRudolf Cornelissen 	 * (NOTE: testsignal function block resides in DAC1 only (!)) */
432beedcaeSRudolf Cornelissen 	DACW(TSTCTRL, (DACR(TSTCTRL) | 0x00001000));
442beedcaeSRudolf Cornelissen 	/* wait for signal lines to stabilize */
452beedcaeSRudolf Cornelissen 	snooze(1000);
462beedcaeSRudolf Cornelissen 
472beedcaeSRudolf Cornelissen 	/* do actual detection: all signals paths high == CRT connected */
482beedcaeSRudolf Cornelissen 	if (DAC2R(TSTCTRL) & 0x10000000)
492beedcaeSRudolf Cornelissen 	{
502beedcaeSRudolf Cornelissen 		present = true;
512beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: CRT detected on connector #2\n"));
522beedcaeSRudolf Cornelissen 	}
532beedcaeSRudolf Cornelissen 	else
542beedcaeSRudolf Cornelissen 	{
552beedcaeSRudolf Cornelissen 		present = false;
562beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: no CRT detected on connector #2\n"));
572beedcaeSRudolf Cornelissen 	}
582beedcaeSRudolf Cornelissen 
592beedcaeSRudolf Cornelissen 	/* kill test signal routing
602beedcaeSRudolf Cornelissen 	 * (NOTE: testsignal function block resides in DAC1 only (!)) */
612beedcaeSRudolf Cornelissen 	DACW(TSTCTRL, (DACR(TSTCTRL) & 0xffffefff));
622beedcaeSRudolf Cornelissen 
632beedcaeSRudolf Cornelissen 	/* restore output connector setting */
642beedcaeSRudolf Cornelissen 	DAC2W(OUTPUT, output);
652beedcaeSRudolf Cornelissen 	/* restore DAC state */
662beedcaeSRudolf Cornelissen 	DAC2W(TSTCTRL, dac);
672beedcaeSRudolf Cornelissen 
682beedcaeSRudolf Cornelissen 	return present;
692beedcaeSRudolf Cornelissen }
702beedcaeSRudolf Cornelissen 
712beedcaeSRudolf Cornelissen /*set the mode, brightness is a value from 0->2 (where 1 is equivalent to direct)*/
eng_dac2_mode(int mode,float brightness)722beedcaeSRudolf Cornelissen status_t eng_dac2_mode(int mode,float brightness)
732beedcaeSRudolf Cornelissen {
742beedcaeSRudolf Cornelissen 	uint8 *r,*g,*b;
752beedcaeSRudolf Cornelissen 	int i, ri;
762beedcaeSRudolf Cornelissen 
772beedcaeSRudolf Cornelissen 	/*set colour arrays to point to space reserved in shared info*/
782beedcaeSRudolf Cornelissen 	r = si->color_data;
792beedcaeSRudolf Cornelissen 	g = r + 256;
802beedcaeSRudolf Cornelissen 	b = g + 256;
812beedcaeSRudolf Cornelissen 
822beedcaeSRudolf Cornelissen 	LOG(4,("DAC2: Setting screen mode %d brightness %f\n", mode, brightness));
832beedcaeSRudolf Cornelissen 	/* init the palette for brightness specified */
842beedcaeSRudolf Cornelissen 	/* (Nvidia cards always use MSbits from screenbuffer as index for PAL) */
852beedcaeSRudolf Cornelissen 	for (i = 0; i < 256; i++)
862beedcaeSRudolf Cornelissen 	{
872beedcaeSRudolf Cornelissen 		ri = i * brightness;
882beedcaeSRudolf Cornelissen 		if (ri > 255) ri = 255;
892beedcaeSRudolf Cornelissen 		b[i] = g[i] = r[i] = ri;
902beedcaeSRudolf Cornelissen 	}
912beedcaeSRudolf Cornelissen 
922beedcaeSRudolf Cornelissen 	if (eng_dac2_palette(r,g,b) != B_OK) return B_ERROR;
932beedcaeSRudolf Cornelissen 
942beedcaeSRudolf Cornelissen 	/* disable palette RAM adressing mask */
952beedcaeSRudolf Cornelissen 	ENG_REG8(RG8_PAL2MASK) = 0xff;
962beedcaeSRudolf Cornelissen 	LOG(2,("DAC2: PAL pixrdmsk readback $%02x\n", ENG_REG8(RG8_PAL2MASK)));
972beedcaeSRudolf Cornelissen 
982beedcaeSRudolf Cornelissen 	return B_OK;
992beedcaeSRudolf Cornelissen }
1002beedcaeSRudolf Cornelissen 
1012beedcaeSRudolf Cornelissen /*program the DAC palette using the given r,g,b values*/
eng_dac2_palette(uint8 r[256],uint8 g[256],uint8 b[256])1022beedcaeSRudolf Cornelissen status_t eng_dac2_palette(uint8 r[256],uint8 g[256],uint8 b[256])
1032beedcaeSRudolf Cornelissen {
1042beedcaeSRudolf Cornelissen 	int i;
1052beedcaeSRudolf Cornelissen 
1062beedcaeSRudolf Cornelissen 	LOG(4,("DAC2: setting palette\n"));
1072beedcaeSRudolf Cornelissen 
1082beedcaeSRudolf Cornelissen 	/* select first PAL adress before starting programming */
1092beedcaeSRudolf Cornelissen 	ENG_REG8(RG8_PAL2INDW) = 0x00;
1102beedcaeSRudolf Cornelissen 
1112beedcaeSRudolf Cornelissen 	/* loop through all 256 to program DAC */
1122beedcaeSRudolf Cornelissen 	for (i = 0; i < 256; i++)
1132beedcaeSRudolf Cornelissen 	{
1142beedcaeSRudolf Cornelissen 		/* the 6 implemented bits are on b0-b5 of the bus */
1152beedcaeSRudolf Cornelissen 		ENG_REG8(RG8_PAL2DATA) = r[i];
1162beedcaeSRudolf Cornelissen 		ENG_REG8(RG8_PAL2DATA) = g[i];
1172beedcaeSRudolf Cornelissen 		ENG_REG8(RG8_PAL2DATA) = b[i];
1182beedcaeSRudolf Cornelissen 	}
1192beedcaeSRudolf Cornelissen 	if (ENG_REG8(RG8_PAL2INDW) != 0x00)
1202beedcaeSRudolf Cornelissen 	{
1212beedcaeSRudolf Cornelissen 		LOG(8,("DAC2: PAL write index incorrect after programming\n"));
1222beedcaeSRudolf Cornelissen 		return B_ERROR;
1232beedcaeSRudolf Cornelissen 	}
1242beedcaeSRudolf Cornelissen if (1)
1252beedcaeSRudolf Cornelissen  {//reread LUT
1262beedcaeSRudolf Cornelissen 	uint8 R, G, B;
1272beedcaeSRudolf Cornelissen 
1282beedcaeSRudolf Cornelissen 	/* select first PAL adress to read (modulo 3 counter) */
1292beedcaeSRudolf Cornelissen 	ENG_REG8(RG8_PAL2INDR) = 0x00;
1302beedcaeSRudolf Cornelissen 	for (i = 0; i < 256; i++)
1312beedcaeSRudolf Cornelissen 	{
1322beedcaeSRudolf Cornelissen 		R = ENG_REG8(RG8_PAL2DATA);
1332beedcaeSRudolf Cornelissen 		G = ENG_REG8(RG8_PAL2DATA);
1342beedcaeSRudolf Cornelissen 		B = ENG_REG8(RG8_PAL2DATA);
1352beedcaeSRudolf Cornelissen 		if ((r[i] != R) || (g[i] != G) || (b[i] != B))
1362beedcaeSRudolf Cornelissen 			LOG(1,("DAC2 palette %d: w %x %x %x, r %x %x %x\n", i, r[i], g[i], b[i], R, G, B)); // apsed
1372beedcaeSRudolf Cornelissen 	}
1382beedcaeSRudolf Cornelissen  }
1392beedcaeSRudolf Cornelissen 
1402beedcaeSRudolf Cornelissen 	return B_OK;
1412beedcaeSRudolf Cornelissen }
1422beedcaeSRudolf Cornelissen 
1432beedcaeSRudolf Cornelissen /*program the pixpll - frequency in kHz*/
eng_dac2_set_pix_pll(display_mode target)1442beedcaeSRudolf Cornelissen status_t eng_dac2_set_pix_pll(display_mode target)
1452beedcaeSRudolf Cornelissen {
1462beedcaeSRudolf Cornelissen 	uint8 m=0,n=0,p=0;
1472beedcaeSRudolf Cornelissen //	uint time = 0;
1482beedcaeSRudolf Cornelissen 
1492beedcaeSRudolf Cornelissen 	float pix_setting, req_pclk;
1502beedcaeSRudolf Cornelissen 	status_t result;
1512beedcaeSRudolf Cornelissen 
1522beedcaeSRudolf Cornelissen 	/* we offer this option because some panels have very tight restrictions,
1532beedcaeSRudolf Cornelissen 	 * and there's no overlapping settings range that makes them all work.
1542beedcaeSRudolf Cornelissen 	 * note:
1552beedcaeSRudolf Cornelissen 	 * this assumes the cards BIOS correctly programmed the panel (is likely) */
1562beedcaeSRudolf Cornelissen 	//fixme: when VESA DDC EDID stuff is implemented, this option can be deleted...
1572beedcaeSRudolf Cornelissen 	if (si->ps.tmds2_active && !si->settings.pgm_panel)
1582beedcaeSRudolf Cornelissen 	{
1592beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: Not programming DFP refresh (specified in skel.settings)\n"));
1602beedcaeSRudolf Cornelissen 		return B_OK;
1612beedcaeSRudolf Cornelissen 	}
1622beedcaeSRudolf Cornelissen 
1632beedcaeSRudolf Cornelissen 	/* fix a DVI or laptop flatpanel to 60Hz refresh! */
1642beedcaeSRudolf Cornelissen 	/* Note:
1652beedcaeSRudolf Cornelissen 	 * The pixelclock drives the flatpanel modeline, not the CRTC modeline. */
1662beedcaeSRudolf Cornelissen 	if (si->ps.tmds2_active)
1672beedcaeSRudolf Cornelissen 	{
1682beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: Fixing DFP refresh to 60Hz!\n"));
1692beedcaeSRudolf Cornelissen 
1702beedcaeSRudolf Cornelissen 		/* use the panel's modeline to determine the needed pixelclock */
1712beedcaeSRudolf Cornelissen 		target.timing.pixel_clock = si->ps.p2_timing.pixel_clock;
1722beedcaeSRudolf Cornelissen 	}
1732beedcaeSRudolf Cornelissen 
1742beedcaeSRudolf Cornelissen 	req_pclk = (target.timing.pixel_clock)/1000.0;
1752beedcaeSRudolf Cornelissen 	LOG(4,("DAC2: Setting PIX PLL for pixelclock %f\n", req_pclk));
1762beedcaeSRudolf Cornelissen 
1772beedcaeSRudolf Cornelissen 	/* signal that we actually want to set the mode */
1782beedcaeSRudolf Cornelissen 	result = eng_dac2_pix_pll_find(target,&pix_setting,&m,&n,&p, 1);
1792beedcaeSRudolf Cornelissen 	if (result != B_OK)
1802beedcaeSRudolf Cornelissen 	{
1812beedcaeSRudolf Cornelissen 		return result;
1822beedcaeSRudolf Cornelissen 	}
1832beedcaeSRudolf Cornelissen 
1842beedcaeSRudolf Cornelissen 	/*reprogram (disable,select,wait for stability,enable)*/
1852beedcaeSRudolf Cornelissen //	DXIW(PIXCLKCTRL,(DXIR(PIXCLKCTRL)&0x0F)|0x04);  /*disable the PIXPLL*/
1862beedcaeSRudolf Cornelissen //	DXIW(PIXCLKCTRL,(DXIR(PIXCLKCTRL)&0x0C)|0x01);  /*select the PIXPLL*/
1872beedcaeSRudolf Cornelissen 
1882beedcaeSRudolf Cornelissen 	/* program new frequency */
1892beedcaeSRudolf Cornelissen 	DAC2W(PIXPLLC, ((p << 16) | (n << 8) | m));
1902beedcaeSRudolf Cornelissen 
1912beedcaeSRudolf Cornelissen 	/* program 2nd set N and M scalers if they exist (b31=1 enables them) */
1922beedcaeSRudolf Cornelissen 	if (si->ps.ext_pll) DAC2W(PIXPLLC2, 0x80000401);
1932beedcaeSRudolf Cornelissen 
1942beedcaeSRudolf Cornelissen 	/* Wait for the PIXPLL frequency to lock until timeout occurs */
1952beedcaeSRudolf Cornelissen //fixme: do NV cards have a LOCK indication bit??
1962beedcaeSRudolf Cornelissen /*	while((!(DXIR(PIXPLLSTAT)&0x40)) & (time <= 2000))
1972beedcaeSRudolf Cornelissen 	{
1982beedcaeSRudolf Cornelissen 		time++;
1992beedcaeSRudolf Cornelissen 		snooze(1);
2002beedcaeSRudolf Cornelissen 	}
2012beedcaeSRudolf Cornelissen 
2022beedcaeSRudolf Cornelissen 	if (time > 2000)
2032beedcaeSRudolf Cornelissen 		LOG(2,("DAC: PIX PLL frequency not locked!\n"));
2042beedcaeSRudolf Cornelissen 	else
2052beedcaeSRudolf Cornelissen 		LOG(2,("DAC: PIX PLL frequency locked\n"));
2062beedcaeSRudolf Cornelissen 	DXIW(PIXCLKCTRL,DXIR(PIXCLKCTRL)&0x0B);         //enable the PIXPLL
2072beedcaeSRudolf Cornelissen */
2082beedcaeSRudolf Cornelissen 
2092beedcaeSRudolf Cornelissen //for now:
2102beedcaeSRudolf Cornelissen 	/* Give the PIXPLL frequency some time to lock... */
2112beedcaeSRudolf Cornelissen 	snooze(1000);
2122beedcaeSRudolf Cornelissen 	LOG(2,("DAC2: PIX PLL frequency should be locked now...\n"));
2132beedcaeSRudolf Cornelissen 
2142beedcaeSRudolf Cornelissen 	return B_OK;
2152beedcaeSRudolf Cornelissen }
2162beedcaeSRudolf Cornelissen 
2172beedcaeSRudolf Cornelissen /* find nearest valid pix pll */
eng_dac2_pix_pll_find(display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result,uint8 test)2182beedcaeSRudolf Cornelissen status_t eng_dac2_pix_pll_find
2192beedcaeSRudolf Cornelissen 	(display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test)
2202beedcaeSRudolf Cornelissen {
2212beedcaeSRudolf Cornelissen 	switch (si->ps.card_type) {
2222beedcaeSRudolf Cornelissen 		default:   return nv10_nv20_dac2_pix_pll_find(target, calc_pclk, m_result, n_result, p_result, test);
2232beedcaeSRudolf Cornelissen 	}
2242beedcaeSRudolf Cornelissen 	return B_ERROR;
2252beedcaeSRudolf Cornelissen }
2262beedcaeSRudolf Cornelissen 
2272beedcaeSRudolf Cornelissen /* find nearest valid pixel PLL setting */
nv10_nv20_dac2_pix_pll_find(display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result,uint8 test)2282beedcaeSRudolf Cornelissen static status_t nv10_nv20_dac2_pix_pll_find(
2292beedcaeSRudolf Cornelissen 	display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test)
2302beedcaeSRudolf Cornelissen {
2312beedcaeSRudolf Cornelissen 	int m = 0, n = 0, p = 0/*, m_max*/;
2322beedcaeSRudolf Cornelissen 	float error, error_best = 999999999;
233*0d6b5d26SJérôme Duval 	int best[3] = {0};
2342beedcaeSRudolf Cornelissen 	float f_vco, max_pclk;
2352beedcaeSRudolf Cornelissen 	float req_pclk = target.timing.pixel_clock/1000.0;
2362beedcaeSRudolf Cornelissen 
2372beedcaeSRudolf Cornelissen 	/* determine the max. reference-frequency postscaler setting for the
2382beedcaeSRudolf Cornelissen 	 * current card (see G100, G200 and G400 specs). */
2392beedcaeSRudolf Cornelissen /*	switch(si->ps.card_type)
2402beedcaeSRudolf Cornelissen 	{
2412beedcaeSRudolf Cornelissen 	case G100:
2422beedcaeSRudolf Cornelissen 		LOG(4,("DAC: G100 restrictions apply\n"));
2432beedcaeSRudolf Cornelissen 		m_max = 7;
2442beedcaeSRudolf Cornelissen 		break;
2452beedcaeSRudolf Cornelissen 	case G200:
2462beedcaeSRudolf Cornelissen 		LOG(4,("DAC: G200 restrictions apply\n"));
2472beedcaeSRudolf Cornelissen 		m_max = 7;
2482beedcaeSRudolf Cornelissen 		break;
2492beedcaeSRudolf Cornelissen 	default:
2502beedcaeSRudolf Cornelissen 		LOG(4,("DAC: G400/G400MAX restrictions apply\n"));
2512beedcaeSRudolf Cornelissen 		m_max = 32;
2522beedcaeSRudolf Cornelissen 		break;
2532beedcaeSRudolf Cornelissen 	}
2542beedcaeSRudolf Cornelissen */
2552beedcaeSRudolf Cornelissen 	LOG(4,("DAC2: NV10/NV20 restrictions apply\n"));
2562beedcaeSRudolf Cornelissen 
2572beedcaeSRudolf Cornelissen 	/* determine the max. pixelclock for the current videomode */
2582beedcaeSRudolf Cornelissen 	switch (target.space)
2592beedcaeSRudolf Cornelissen 	{
2602beedcaeSRudolf Cornelissen 		case B_CMAP8:
2612beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_8;
2622beedcaeSRudolf Cornelissen 			break;
2632beedcaeSRudolf Cornelissen 		case B_RGB15_LITTLE:
2642beedcaeSRudolf Cornelissen 		case B_RGB16_LITTLE:
2652beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_16;
2662beedcaeSRudolf Cornelissen 			break;
2672beedcaeSRudolf Cornelissen 		case B_RGB24_LITTLE:
2682beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_24;
2692beedcaeSRudolf Cornelissen 			break;
2702beedcaeSRudolf Cornelissen 		case B_RGB32_LITTLE:
2712beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_32;
2722beedcaeSRudolf Cornelissen 			break;
2732beedcaeSRudolf Cornelissen 		default:
2742beedcaeSRudolf Cornelissen 			/* use fail-safe value */
2752beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_32;
2762beedcaeSRudolf Cornelissen 			break;
2772beedcaeSRudolf Cornelissen 	}
2782beedcaeSRudolf Cornelissen 	/* if some dualhead mode is active, an extra restriction might apply */
2792beedcaeSRudolf Cornelissen 	if ((target.flags & DUALHEAD_BITS) && (target.space == B_RGB32_LITTLE))
2802beedcaeSRudolf Cornelissen 		max_pclk = si->ps.max_dac2_clock_32dh;
2812beedcaeSRudolf Cornelissen 
2822beedcaeSRudolf Cornelissen 	/* Make sure the requested pixelclock is within the PLL's operational limits */
2832beedcaeSRudolf Cornelissen 	/* lower limit is min_pixel_vco divided by highest postscaler-factor */
2842beedcaeSRudolf Cornelissen 	if (req_pclk < (si->ps.min_video_vco / 16.0))
2852beedcaeSRudolf Cornelissen 	{
2862beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: clamping pixclock: requested %fMHz, set to %fMHz\n",
2872beedcaeSRudolf Cornelissen 										req_pclk, (float)(si->ps.min_video_vco / 16.0)));
2882beedcaeSRudolf Cornelissen 		req_pclk = (si->ps.min_video_vco / 16.0);
2892beedcaeSRudolf Cornelissen 	}
2902beedcaeSRudolf Cornelissen 	/* upper limit is given by pins in combination with current active mode */
2912beedcaeSRudolf Cornelissen 	if (req_pclk > max_pclk)
2922beedcaeSRudolf Cornelissen 	{
2932beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: clamping pixclock: requested %fMHz, set to %fMHz\n",
2942beedcaeSRudolf Cornelissen 														req_pclk, (float)max_pclk));
2952beedcaeSRudolf Cornelissen 		req_pclk = max_pclk;
2962beedcaeSRudolf Cornelissen 	}
2972beedcaeSRudolf Cornelissen 
2982beedcaeSRudolf Cornelissen 	/* iterate through all valid PLL postscaler settings */
2992beedcaeSRudolf Cornelissen 	for (p=0x01; p < 0x20; p = p<<1)
3002beedcaeSRudolf Cornelissen 	{
3012beedcaeSRudolf Cornelissen 		/* calculate the needed VCO frequency for this postscaler setting */
3022beedcaeSRudolf Cornelissen 		f_vco = req_pclk * p;
3032beedcaeSRudolf Cornelissen 
3042beedcaeSRudolf Cornelissen 		/* check if this is within range of the VCO specs */
3052beedcaeSRudolf Cornelissen 		if ((f_vco >= si->ps.min_video_vco) && (f_vco <= si->ps.max_video_vco))
3062beedcaeSRudolf Cornelissen 		{
3072beedcaeSRudolf Cornelissen 			/* FX5600 and FX5700 tweak for 2nd set N and M scalers */
3082beedcaeSRudolf Cornelissen 			if (si->ps.ext_pll) f_vco /= 4;
3092beedcaeSRudolf Cornelissen 
3102beedcaeSRudolf Cornelissen 			/* iterate trough all valid reference-frequency postscaler settings */
3112beedcaeSRudolf Cornelissen 			for (m = 7; m <= 14; m++)
3122beedcaeSRudolf Cornelissen 			{
3132beedcaeSRudolf Cornelissen 				/* check if phase-discriminator will be within operational limits */
3142beedcaeSRudolf Cornelissen 				//fixme: PLL calcs will be resetup/splitup/updated...
3152beedcaeSRudolf Cornelissen 				if (si->ps.card_type == NV36)
3162beedcaeSRudolf Cornelissen 				{
3172beedcaeSRudolf Cornelissen 					if (((si->ps.f_ref / m) < 3.2) || ((si->ps.f_ref / m) > 6.4)) continue;
3182beedcaeSRudolf Cornelissen 				}
3192beedcaeSRudolf Cornelissen 				else
3202beedcaeSRudolf Cornelissen 				{
3212beedcaeSRudolf Cornelissen 					if (((si->ps.f_ref / m) < 1.0) || ((si->ps.f_ref / m) > 2.0)) continue;
3222beedcaeSRudolf Cornelissen 				}
3232beedcaeSRudolf Cornelissen 
3242beedcaeSRudolf Cornelissen 				/* calculate VCO postscaler setting for current setup.. */
3252beedcaeSRudolf Cornelissen 				n = (int)(((f_vco * m) / si->ps.f_ref) + 0.5);
3262beedcaeSRudolf Cornelissen 				/* ..and check for validity */
3272beedcaeSRudolf Cornelissen 				if ((n < 1) || (n > 255))	continue;
3282beedcaeSRudolf Cornelissen 
3292beedcaeSRudolf Cornelissen 				/* find error in frequency this setting gives */
3302beedcaeSRudolf Cornelissen 				if (si->ps.ext_pll)
3312beedcaeSRudolf Cornelissen 				{
3322beedcaeSRudolf Cornelissen 					/* FX5600 and FX5700 tweak for 2nd set N and M scalers */
3332beedcaeSRudolf Cornelissen 					error = fabs((req_pclk / 4) - (((si->ps.f_ref / m) * n) / p));
3342beedcaeSRudolf Cornelissen 				}
3352beedcaeSRudolf Cornelissen 				else
3362beedcaeSRudolf Cornelissen 					error = fabs(req_pclk - (((si->ps.f_ref / m) * n) / p));
3372beedcaeSRudolf Cornelissen 
3382beedcaeSRudolf Cornelissen 				/* note the setting if best yet */
3392beedcaeSRudolf Cornelissen 				if (error < error_best)
3402beedcaeSRudolf Cornelissen 				{
3412beedcaeSRudolf Cornelissen 					error_best = error;
3422beedcaeSRudolf Cornelissen 					best[0]=m;
3432beedcaeSRudolf Cornelissen 					best[1]=n;
3442beedcaeSRudolf Cornelissen 					best[2]=p;
3452beedcaeSRudolf Cornelissen 				}
3462beedcaeSRudolf Cornelissen 			}
3472beedcaeSRudolf Cornelissen 		}
3482beedcaeSRudolf Cornelissen 	}
3492beedcaeSRudolf Cornelissen 
3502beedcaeSRudolf Cornelissen 	/* setup the scalers programming values for found optimum setting */
3512beedcaeSRudolf Cornelissen 	m = best[0];
3522beedcaeSRudolf Cornelissen 	n = best[1];
3532beedcaeSRudolf Cornelissen 	p = best[2];
3542beedcaeSRudolf Cornelissen 
3552beedcaeSRudolf Cornelissen 	/* log the VCO frequency found */
3562beedcaeSRudolf Cornelissen 	f_vco = ((si->ps.f_ref / m) * n);
3572beedcaeSRudolf Cornelissen 	/* FX5600 and FX5700 tweak for 2nd set N and M scalers */
3582beedcaeSRudolf Cornelissen 	if (si->ps.ext_pll) f_vco *= 4;
3592beedcaeSRudolf Cornelissen 
3602beedcaeSRudolf Cornelissen 	LOG(2,("DAC2: pix VCO frequency found %fMhz\n", f_vco));
3612beedcaeSRudolf Cornelissen 
3622beedcaeSRudolf Cornelissen 	/* return the results */
3632beedcaeSRudolf Cornelissen 	*calc_pclk = (f_vco / p);
3642beedcaeSRudolf Cornelissen 	*m_result = m;
3652beedcaeSRudolf Cornelissen 	*n_result = n;
3662beedcaeSRudolf Cornelissen 	switch(p)
3672beedcaeSRudolf Cornelissen 	{
3682beedcaeSRudolf Cornelissen 	case 1:
3692beedcaeSRudolf Cornelissen 		p = 0x00;
3702beedcaeSRudolf Cornelissen 		break;
3712beedcaeSRudolf Cornelissen 	case 2:
3722beedcaeSRudolf Cornelissen 		p = 0x01;
3732beedcaeSRudolf Cornelissen 		break;
3742beedcaeSRudolf Cornelissen 	case 4:
3752beedcaeSRudolf Cornelissen 		p = 0x02;
3762beedcaeSRudolf Cornelissen 		break;
3772beedcaeSRudolf Cornelissen 	case 8:
3782beedcaeSRudolf Cornelissen 		p = 0x03;
3792beedcaeSRudolf Cornelissen 		break;
3802beedcaeSRudolf Cornelissen 	case 16:
3812beedcaeSRudolf Cornelissen 		p = 0x04;
3822beedcaeSRudolf Cornelissen 		break;
3832beedcaeSRudolf Cornelissen 	}
3842beedcaeSRudolf Cornelissen 	*p_result = p;
3852beedcaeSRudolf Cornelissen 
3862beedcaeSRudolf Cornelissen 	/* display the found pixelclock values */
3872beedcaeSRudolf Cornelissen 	LOG(2,("DAC2: pix PLL check: requested %fMHz got %fMHz, mnp 0x%02x 0x%02x 0x%02x\n",
3882beedcaeSRudolf Cornelissen 		req_pclk, *calc_pclk, *m_result, *n_result, *p_result));
3892beedcaeSRudolf Cornelissen 
3902beedcaeSRudolf Cornelissen 	return B_OK;
3912beedcaeSRudolf Cornelissen }
392