xref: /haiku/src/add-ons/accelerants/via/engine/dac2.c (revision 2beedcaece6a4533a385fd77f5025a59d13707c0)
1*2beedcaeSRudolf Cornelissen /* program the secondary DAC */
2*2beedcaeSRudolf Cornelissen /* Author:
3*2beedcaeSRudolf Cornelissen    Rudolf Cornelissen 12/2003-9/2004
4*2beedcaeSRudolf Cornelissen */
5*2beedcaeSRudolf Cornelissen 
6*2beedcaeSRudolf Cornelissen #define MODULE_BIT 0x00001000
7*2beedcaeSRudolf Cornelissen 
8*2beedcaeSRudolf Cornelissen #include "std.h"
9*2beedcaeSRudolf Cornelissen 
10*2beedcaeSRudolf Cornelissen static status_t nv10_nv20_dac2_pix_pll_find(
11*2beedcaeSRudolf Cornelissen 	display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test);
12*2beedcaeSRudolf Cornelissen 
13*2beedcaeSRudolf Cornelissen /* see if an analog VGA monitor is connected to connector #2 */
14*2beedcaeSRudolf Cornelissen bool eng_dac2_crt_connected()
15*2beedcaeSRudolf Cornelissen {
16*2beedcaeSRudolf Cornelissen 	uint32 output, dac;
17*2beedcaeSRudolf Cornelissen 	bool present;
18*2beedcaeSRudolf Cornelissen 
19*2beedcaeSRudolf Cornelissen 	/* NOTE:
20*2beedcaeSRudolf Cornelissen 	 * NV11 can't do this: It will report DAC1 status instead because it HAS no
21*2beedcaeSRudolf Cornelissen 	 * actual secondary DAC function. */
22*2beedcaeSRudolf Cornelissen 	/* (It DOES have a secondary palette RAM and pixelclock PLL though.) */
23*2beedcaeSRudolf Cornelissen 
24*2beedcaeSRudolf Cornelissen 	/* save output connector setting */
25*2beedcaeSRudolf Cornelissen 	output = DAC2R(OUTPUT);
26*2beedcaeSRudolf Cornelissen 	/* save DAC state */
27*2beedcaeSRudolf Cornelissen 	dac = DAC2R(TSTCTRL);
28*2beedcaeSRudolf Cornelissen 
29*2beedcaeSRudolf Cornelissen 	/* turn on DAC2 */
30*2beedcaeSRudolf Cornelissen 	DAC2W(TSTCTRL, (DAC2R(TSTCTRL) & 0xfffeffff));
31*2beedcaeSRudolf Cornelissen 	/* select primary head and turn off CRT (and DVI?) outputs */
32*2beedcaeSRudolf Cornelissen 	DAC2W(OUTPUT, (output & 0x0000feee));
33*2beedcaeSRudolf Cornelissen 	/* wait for signal lines to stabilize */
34*2beedcaeSRudolf Cornelissen 	snooze(1000);
35*2beedcaeSRudolf Cornelissen 	/* re-enable CRT output */
36*2beedcaeSRudolf Cornelissen 	DAC2W(OUTPUT, (DAC2R(OUTPUT) | 0x00000001));
37*2beedcaeSRudolf Cornelissen 
38*2beedcaeSRudolf Cornelissen 	/* setup RGB test signal levels to approx 30% of DAC range and enable them
39*2beedcaeSRudolf Cornelissen 	 * (NOTE: testsignal function block resides in DAC1 only (!)) */
40*2beedcaeSRudolf Cornelissen 	DACW(TSTDATA, ((0x2 << 30) | (0x140 << 20) | (0x140 << 10) | (0x140 << 0)));
41*2beedcaeSRudolf Cornelissen 	/* route test signals to output
42*2beedcaeSRudolf Cornelissen 	 * (NOTE: testsignal function block resides in DAC1 only (!)) */
43*2beedcaeSRudolf Cornelissen 	DACW(TSTCTRL, (DACR(TSTCTRL) | 0x00001000));
44*2beedcaeSRudolf Cornelissen 	/* wait for signal lines to stabilize */
45*2beedcaeSRudolf Cornelissen 	snooze(1000);
46*2beedcaeSRudolf Cornelissen 
47*2beedcaeSRudolf Cornelissen 	/* do actual detection: all signals paths high == CRT connected */
48*2beedcaeSRudolf Cornelissen 	if (DAC2R(TSTCTRL) & 0x10000000)
49*2beedcaeSRudolf Cornelissen 	{
50*2beedcaeSRudolf Cornelissen 		present = true;
51*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: CRT detected on connector #2\n"));
52*2beedcaeSRudolf Cornelissen 	}
53*2beedcaeSRudolf Cornelissen 	else
54*2beedcaeSRudolf Cornelissen 	{
55*2beedcaeSRudolf Cornelissen 		present = false;
56*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: no CRT detected on connector #2\n"));
57*2beedcaeSRudolf Cornelissen 	}
58*2beedcaeSRudolf Cornelissen 
59*2beedcaeSRudolf Cornelissen 	/* kill test signal routing
60*2beedcaeSRudolf Cornelissen 	 * (NOTE: testsignal function block resides in DAC1 only (!)) */
61*2beedcaeSRudolf Cornelissen 	DACW(TSTCTRL, (DACR(TSTCTRL) & 0xffffefff));
62*2beedcaeSRudolf Cornelissen 
63*2beedcaeSRudolf Cornelissen 	/* restore output connector setting */
64*2beedcaeSRudolf Cornelissen 	DAC2W(OUTPUT, output);
65*2beedcaeSRudolf Cornelissen 	/* restore DAC state */
66*2beedcaeSRudolf Cornelissen 	DAC2W(TSTCTRL, dac);
67*2beedcaeSRudolf Cornelissen 
68*2beedcaeSRudolf Cornelissen 	return present;
69*2beedcaeSRudolf Cornelissen }
70*2beedcaeSRudolf Cornelissen 
71*2beedcaeSRudolf Cornelissen /*set the mode, brightness is a value from 0->2 (where 1 is equivalent to direct)*/
72*2beedcaeSRudolf Cornelissen status_t eng_dac2_mode(int mode,float brightness)
73*2beedcaeSRudolf Cornelissen {
74*2beedcaeSRudolf Cornelissen 	uint8 *r,*g,*b;
75*2beedcaeSRudolf Cornelissen 	int i, ri;
76*2beedcaeSRudolf Cornelissen 
77*2beedcaeSRudolf Cornelissen 	/*set colour arrays to point to space reserved in shared info*/
78*2beedcaeSRudolf Cornelissen 	r = si->color_data;
79*2beedcaeSRudolf Cornelissen 	g = r + 256;
80*2beedcaeSRudolf Cornelissen 	b = g + 256;
81*2beedcaeSRudolf Cornelissen 
82*2beedcaeSRudolf Cornelissen 	LOG(4,("DAC2: Setting screen mode %d brightness %f\n", mode, brightness));
83*2beedcaeSRudolf Cornelissen 	/* init the palette for brightness specified */
84*2beedcaeSRudolf Cornelissen 	/* (Nvidia cards always use MSbits from screenbuffer as index for PAL) */
85*2beedcaeSRudolf Cornelissen 	for (i = 0; i < 256; i++)
86*2beedcaeSRudolf Cornelissen 	{
87*2beedcaeSRudolf Cornelissen 		ri = i * brightness;
88*2beedcaeSRudolf Cornelissen 		if (ri > 255) ri = 255;
89*2beedcaeSRudolf Cornelissen 		b[i] = g[i] = r[i] = ri;
90*2beedcaeSRudolf Cornelissen 	}
91*2beedcaeSRudolf Cornelissen 
92*2beedcaeSRudolf Cornelissen 	if (eng_dac2_palette(r,g,b) != B_OK) return B_ERROR;
93*2beedcaeSRudolf Cornelissen 
94*2beedcaeSRudolf Cornelissen 	/* disable palette RAM adressing mask */
95*2beedcaeSRudolf Cornelissen 	ENG_REG8(RG8_PAL2MASK) = 0xff;
96*2beedcaeSRudolf Cornelissen 	LOG(2,("DAC2: PAL pixrdmsk readback $%02x\n", ENG_REG8(RG8_PAL2MASK)));
97*2beedcaeSRudolf Cornelissen 
98*2beedcaeSRudolf Cornelissen 	return B_OK;
99*2beedcaeSRudolf Cornelissen }
100*2beedcaeSRudolf Cornelissen 
101*2beedcaeSRudolf Cornelissen /*program the DAC palette using the given r,g,b values*/
102*2beedcaeSRudolf Cornelissen status_t eng_dac2_palette(uint8 r[256],uint8 g[256],uint8 b[256])
103*2beedcaeSRudolf Cornelissen {
104*2beedcaeSRudolf Cornelissen 	int i;
105*2beedcaeSRudolf Cornelissen 
106*2beedcaeSRudolf Cornelissen 	LOG(4,("DAC2: setting palette\n"));
107*2beedcaeSRudolf Cornelissen 
108*2beedcaeSRudolf Cornelissen 	/* select first PAL adress before starting programming */
109*2beedcaeSRudolf Cornelissen 	ENG_REG8(RG8_PAL2INDW) = 0x00;
110*2beedcaeSRudolf Cornelissen 
111*2beedcaeSRudolf Cornelissen 	/* loop through all 256 to program DAC */
112*2beedcaeSRudolf Cornelissen 	for (i = 0; i < 256; i++)
113*2beedcaeSRudolf Cornelissen 	{
114*2beedcaeSRudolf Cornelissen 		/* the 6 implemented bits are on b0-b5 of the bus */
115*2beedcaeSRudolf Cornelissen 		ENG_REG8(RG8_PAL2DATA) = r[i];
116*2beedcaeSRudolf Cornelissen 		ENG_REG8(RG8_PAL2DATA) = g[i];
117*2beedcaeSRudolf Cornelissen 		ENG_REG8(RG8_PAL2DATA) = b[i];
118*2beedcaeSRudolf Cornelissen 	}
119*2beedcaeSRudolf Cornelissen 	if (ENG_REG8(RG8_PAL2INDW) != 0x00)
120*2beedcaeSRudolf Cornelissen 	{
121*2beedcaeSRudolf Cornelissen 		LOG(8,("DAC2: PAL write index incorrect after programming\n"));
122*2beedcaeSRudolf Cornelissen 		return B_ERROR;
123*2beedcaeSRudolf Cornelissen 	}
124*2beedcaeSRudolf Cornelissen if (1)
125*2beedcaeSRudolf Cornelissen  {//reread LUT
126*2beedcaeSRudolf Cornelissen 	uint8 R, G, B;
127*2beedcaeSRudolf Cornelissen 
128*2beedcaeSRudolf Cornelissen 	/* select first PAL adress to read (modulo 3 counter) */
129*2beedcaeSRudolf Cornelissen 	ENG_REG8(RG8_PAL2INDR) = 0x00;
130*2beedcaeSRudolf Cornelissen 	for (i = 0; i < 256; i++)
131*2beedcaeSRudolf Cornelissen 	{
132*2beedcaeSRudolf Cornelissen 		R = ENG_REG8(RG8_PAL2DATA);
133*2beedcaeSRudolf Cornelissen 		G = ENG_REG8(RG8_PAL2DATA);
134*2beedcaeSRudolf Cornelissen 		B = ENG_REG8(RG8_PAL2DATA);
135*2beedcaeSRudolf Cornelissen 		if ((r[i] != R) || (g[i] != G) || (b[i] != B))
136*2beedcaeSRudolf 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
137*2beedcaeSRudolf Cornelissen 	}
138*2beedcaeSRudolf Cornelissen  }
139*2beedcaeSRudolf Cornelissen 
140*2beedcaeSRudolf Cornelissen 	return B_OK;
141*2beedcaeSRudolf Cornelissen }
142*2beedcaeSRudolf Cornelissen 
143*2beedcaeSRudolf Cornelissen /*program the pixpll - frequency in kHz*/
144*2beedcaeSRudolf Cornelissen status_t eng_dac2_set_pix_pll(display_mode target)
145*2beedcaeSRudolf Cornelissen {
146*2beedcaeSRudolf Cornelissen 	uint8 m=0,n=0,p=0;
147*2beedcaeSRudolf Cornelissen //	uint time = 0;
148*2beedcaeSRudolf Cornelissen 
149*2beedcaeSRudolf Cornelissen 	float pix_setting, req_pclk;
150*2beedcaeSRudolf Cornelissen 	status_t result;
151*2beedcaeSRudolf Cornelissen 
152*2beedcaeSRudolf Cornelissen 	/* we offer this option because some panels have very tight restrictions,
153*2beedcaeSRudolf Cornelissen 	 * and there's no overlapping settings range that makes them all work.
154*2beedcaeSRudolf Cornelissen 	 * note:
155*2beedcaeSRudolf Cornelissen 	 * this assumes the cards BIOS correctly programmed the panel (is likely) */
156*2beedcaeSRudolf Cornelissen 	//fixme: when VESA DDC EDID stuff is implemented, this option can be deleted...
157*2beedcaeSRudolf Cornelissen 	if (si->ps.tmds2_active && !si->settings.pgm_panel)
158*2beedcaeSRudolf Cornelissen 	{
159*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: Not programming DFP refresh (specified in skel.settings)\n"));
160*2beedcaeSRudolf Cornelissen 		return B_OK;
161*2beedcaeSRudolf Cornelissen 	}
162*2beedcaeSRudolf Cornelissen 
163*2beedcaeSRudolf Cornelissen 	/* fix a DVI or laptop flatpanel to 60Hz refresh! */
164*2beedcaeSRudolf Cornelissen 	/* Note:
165*2beedcaeSRudolf Cornelissen 	 * The pixelclock drives the flatpanel modeline, not the CRTC modeline. */
166*2beedcaeSRudolf Cornelissen 	if (si->ps.tmds2_active)
167*2beedcaeSRudolf Cornelissen 	{
168*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: Fixing DFP refresh to 60Hz!\n"));
169*2beedcaeSRudolf Cornelissen 
170*2beedcaeSRudolf Cornelissen 		/* use the panel's modeline to determine the needed pixelclock */
171*2beedcaeSRudolf Cornelissen 		target.timing.pixel_clock = si->ps.p2_timing.pixel_clock;
172*2beedcaeSRudolf Cornelissen 	}
173*2beedcaeSRudolf Cornelissen 
174*2beedcaeSRudolf Cornelissen 	req_pclk = (target.timing.pixel_clock)/1000.0;
175*2beedcaeSRudolf Cornelissen 	LOG(4,("DAC2: Setting PIX PLL for pixelclock %f\n", req_pclk));
176*2beedcaeSRudolf Cornelissen 
177*2beedcaeSRudolf Cornelissen 	/* signal that we actually want to set the mode */
178*2beedcaeSRudolf Cornelissen 	result = eng_dac2_pix_pll_find(target,&pix_setting,&m,&n,&p, 1);
179*2beedcaeSRudolf Cornelissen 	if (result != B_OK)
180*2beedcaeSRudolf Cornelissen 	{
181*2beedcaeSRudolf Cornelissen 		return result;
182*2beedcaeSRudolf Cornelissen 	}
183*2beedcaeSRudolf Cornelissen 
184*2beedcaeSRudolf Cornelissen 	/*reprogram (disable,select,wait for stability,enable)*/
185*2beedcaeSRudolf Cornelissen //	DXIW(PIXCLKCTRL,(DXIR(PIXCLKCTRL)&0x0F)|0x04);  /*disable the PIXPLL*/
186*2beedcaeSRudolf Cornelissen //	DXIW(PIXCLKCTRL,(DXIR(PIXCLKCTRL)&0x0C)|0x01);  /*select the PIXPLL*/
187*2beedcaeSRudolf Cornelissen 
188*2beedcaeSRudolf Cornelissen 	/* program new frequency */
189*2beedcaeSRudolf Cornelissen 	DAC2W(PIXPLLC, ((p << 16) | (n << 8) | m));
190*2beedcaeSRudolf Cornelissen 
191*2beedcaeSRudolf Cornelissen 	/* program 2nd set N and M scalers if they exist (b31=1 enables them) */
192*2beedcaeSRudolf Cornelissen 	if (si->ps.ext_pll) DAC2W(PIXPLLC2, 0x80000401);
193*2beedcaeSRudolf Cornelissen 
194*2beedcaeSRudolf Cornelissen 	/* Wait for the PIXPLL frequency to lock until timeout occurs */
195*2beedcaeSRudolf Cornelissen //fixme: do NV cards have a LOCK indication bit??
196*2beedcaeSRudolf Cornelissen /*	while((!(DXIR(PIXPLLSTAT)&0x40)) & (time <= 2000))
197*2beedcaeSRudolf Cornelissen 	{
198*2beedcaeSRudolf Cornelissen 		time++;
199*2beedcaeSRudolf Cornelissen 		snooze(1);
200*2beedcaeSRudolf Cornelissen 	}
201*2beedcaeSRudolf Cornelissen 
202*2beedcaeSRudolf Cornelissen 	if (time > 2000)
203*2beedcaeSRudolf Cornelissen 		LOG(2,("DAC: PIX PLL frequency not locked!\n"));
204*2beedcaeSRudolf Cornelissen 	else
205*2beedcaeSRudolf Cornelissen 		LOG(2,("DAC: PIX PLL frequency locked\n"));
206*2beedcaeSRudolf Cornelissen 	DXIW(PIXCLKCTRL,DXIR(PIXCLKCTRL)&0x0B);         //enable the PIXPLL
207*2beedcaeSRudolf Cornelissen */
208*2beedcaeSRudolf Cornelissen 
209*2beedcaeSRudolf Cornelissen //for now:
210*2beedcaeSRudolf Cornelissen 	/* Give the PIXPLL frequency some time to lock... */
211*2beedcaeSRudolf Cornelissen 	snooze(1000);
212*2beedcaeSRudolf Cornelissen 	LOG(2,("DAC2: PIX PLL frequency should be locked now...\n"));
213*2beedcaeSRudolf Cornelissen 
214*2beedcaeSRudolf Cornelissen 	return B_OK;
215*2beedcaeSRudolf Cornelissen }
216*2beedcaeSRudolf Cornelissen 
217*2beedcaeSRudolf Cornelissen /* find nearest valid pix pll */
218*2beedcaeSRudolf Cornelissen status_t eng_dac2_pix_pll_find
219*2beedcaeSRudolf Cornelissen 	(display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test)
220*2beedcaeSRudolf Cornelissen {
221*2beedcaeSRudolf Cornelissen 	switch (si->ps.card_type) {
222*2beedcaeSRudolf Cornelissen 		default:   return nv10_nv20_dac2_pix_pll_find(target, calc_pclk, m_result, n_result, p_result, test);
223*2beedcaeSRudolf Cornelissen 	}
224*2beedcaeSRudolf Cornelissen 	return B_ERROR;
225*2beedcaeSRudolf Cornelissen }
226*2beedcaeSRudolf Cornelissen 
227*2beedcaeSRudolf Cornelissen /* find nearest valid pixel PLL setting */
228*2beedcaeSRudolf Cornelissen static status_t nv10_nv20_dac2_pix_pll_find(
229*2beedcaeSRudolf Cornelissen 	display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test)
230*2beedcaeSRudolf Cornelissen {
231*2beedcaeSRudolf Cornelissen 	int m = 0, n = 0, p = 0/*, m_max*/;
232*2beedcaeSRudolf Cornelissen 	float error, error_best = 999999999;
233*2beedcaeSRudolf Cornelissen 	int best[3];
234*2beedcaeSRudolf Cornelissen 	float f_vco, max_pclk;
235*2beedcaeSRudolf Cornelissen 	float req_pclk = target.timing.pixel_clock/1000.0;
236*2beedcaeSRudolf Cornelissen 
237*2beedcaeSRudolf Cornelissen 	/* determine the max. reference-frequency postscaler setting for the
238*2beedcaeSRudolf Cornelissen 	 * current card (see G100, G200 and G400 specs). */
239*2beedcaeSRudolf Cornelissen /*	switch(si->ps.card_type)
240*2beedcaeSRudolf Cornelissen 	{
241*2beedcaeSRudolf Cornelissen 	case G100:
242*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC: G100 restrictions apply\n"));
243*2beedcaeSRudolf Cornelissen 		m_max = 7;
244*2beedcaeSRudolf Cornelissen 		break;
245*2beedcaeSRudolf Cornelissen 	case G200:
246*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC: G200 restrictions apply\n"));
247*2beedcaeSRudolf Cornelissen 		m_max = 7;
248*2beedcaeSRudolf Cornelissen 		break;
249*2beedcaeSRudolf Cornelissen 	default:
250*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC: G400/G400MAX restrictions apply\n"));
251*2beedcaeSRudolf Cornelissen 		m_max = 32;
252*2beedcaeSRudolf Cornelissen 		break;
253*2beedcaeSRudolf Cornelissen 	}
254*2beedcaeSRudolf Cornelissen */
255*2beedcaeSRudolf Cornelissen 	LOG(4,("DAC2: NV10/NV20 restrictions apply\n"));
256*2beedcaeSRudolf Cornelissen 
257*2beedcaeSRudolf Cornelissen 	/* determine the max. pixelclock for the current videomode */
258*2beedcaeSRudolf Cornelissen 	switch (target.space)
259*2beedcaeSRudolf Cornelissen 	{
260*2beedcaeSRudolf Cornelissen 		case B_CMAP8:
261*2beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_8;
262*2beedcaeSRudolf Cornelissen 			break;
263*2beedcaeSRudolf Cornelissen 		case B_RGB15_LITTLE:
264*2beedcaeSRudolf Cornelissen 		case B_RGB16_LITTLE:
265*2beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_16;
266*2beedcaeSRudolf Cornelissen 			break;
267*2beedcaeSRudolf Cornelissen 		case B_RGB24_LITTLE:
268*2beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_24;
269*2beedcaeSRudolf Cornelissen 			break;
270*2beedcaeSRudolf Cornelissen 		case B_RGB32_LITTLE:
271*2beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_32;
272*2beedcaeSRudolf Cornelissen 			break;
273*2beedcaeSRudolf Cornelissen 		default:
274*2beedcaeSRudolf Cornelissen 			/* use fail-safe value */
275*2beedcaeSRudolf Cornelissen 			max_pclk = si->ps.max_dac2_clock_32;
276*2beedcaeSRudolf Cornelissen 			break;
277*2beedcaeSRudolf Cornelissen 	}
278*2beedcaeSRudolf Cornelissen 	/* if some dualhead mode is active, an extra restriction might apply */
279*2beedcaeSRudolf Cornelissen 	if ((target.flags & DUALHEAD_BITS) && (target.space == B_RGB32_LITTLE))
280*2beedcaeSRudolf Cornelissen 		max_pclk = si->ps.max_dac2_clock_32dh;
281*2beedcaeSRudolf Cornelissen 
282*2beedcaeSRudolf Cornelissen 	/* Make sure the requested pixelclock is within the PLL's operational limits */
283*2beedcaeSRudolf Cornelissen 	/* lower limit is min_pixel_vco divided by highest postscaler-factor */
284*2beedcaeSRudolf Cornelissen 	if (req_pclk < (si->ps.min_video_vco / 16.0))
285*2beedcaeSRudolf Cornelissen 	{
286*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: clamping pixclock: requested %fMHz, set to %fMHz\n",
287*2beedcaeSRudolf Cornelissen 										req_pclk, (float)(si->ps.min_video_vco / 16.0)));
288*2beedcaeSRudolf Cornelissen 		req_pclk = (si->ps.min_video_vco / 16.0);
289*2beedcaeSRudolf Cornelissen 	}
290*2beedcaeSRudolf Cornelissen 	/* upper limit is given by pins in combination with current active mode */
291*2beedcaeSRudolf Cornelissen 	if (req_pclk > max_pclk)
292*2beedcaeSRudolf Cornelissen 	{
293*2beedcaeSRudolf Cornelissen 		LOG(4,("DAC2: clamping pixclock: requested %fMHz, set to %fMHz\n",
294*2beedcaeSRudolf Cornelissen 														req_pclk, (float)max_pclk));
295*2beedcaeSRudolf Cornelissen 		req_pclk = max_pclk;
296*2beedcaeSRudolf Cornelissen 	}
297*2beedcaeSRudolf Cornelissen 
298*2beedcaeSRudolf Cornelissen 	/* iterate through all valid PLL postscaler settings */
299*2beedcaeSRudolf Cornelissen 	for (p=0x01; p < 0x20; p = p<<1)
300*2beedcaeSRudolf Cornelissen 	{
301*2beedcaeSRudolf Cornelissen 		/* calculate the needed VCO frequency for this postscaler setting */
302*2beedcaeSRudolf Cornelissen 		f_vco = req_pclk * p;
303*2beedcaeSRudolf Cornelissen 
304*2beedcaeSRudolf Cornelissen 		/* check if this is within range of the VCO specs */
305*2beedcaeSRudolf Cornelissen 		if ((f_vco >= si->ps.min_video_vco) && (f_vco <= si->ps.max_video_vco))
306*2beedcaeSRudolf Cornelissen 		{
307*2beedcaeSRudolf Cornelissen 			/* FX5600 and FX5700 tweak for 2nd set N and M scalers */
308*2beedcaeSRudolf Cornelissen 			if (si->ps.ext_pll) f_vco /= 4;
309*2beedcaeSRudolf Cornelissen 
310*2beedcaeSRudolf Cornelissen 			/* iterate trough all valid reference-frequency postscaler settings */
311*2beedcaeSRudolf Cornelissen 			for (m = 7; m <= 14; m++)
312*2beedcaeSRudolf Cornelissen 			{
313*2beedcaeSRudolf Cornelissen 				/* check if phase-discriminator will be within operational limits */
314*2beedcaeSRudolf Cornelissen 				//fixme: PLL calcs will be resetup/splitup/updated...
315*2beedcaeSRudolf Cornelissen 				if (si->ps.card_type == NV36)
316*2beedcaeSRudolf Cornelissen 				{
317*2beedcaeSRudolf Cornelissen 					if (((si->ps.f_ref / m) < 3.2) || ((si->ps.f_ref / m) > 6.4)) continue;
318*2beedcaeSRudolf Cornelissen 				}
319*2beedcaeSRudolf Cornelissen 				else
320*2beedcaeSRudolf Cornelissen 				{
321*2beedcaeSRudolf Cornelissen 					if (((si->ps.f_ref / m) < 1.0) || ((si->ps.f_ref / m) > 2.0)) continue;
322*2beedcaeSRudolf Cornelissen 				}
323*2beedcaeSRudolf Cornelissen 
324*2beedcaeSRudolf Cornelissen 				/* calculate VCO postscaler setting for current setup.. */
325*2beedcaeSRudolf Cornelissen 				n = (int)(((f_vco * m) / si->ps.f_ref) + 0.5);
326*2beedcaeSRudolf Cornelissen 				/* ..and check for validity */
327*2beedcaeSRudolf Cornelissen 				if ((n < 1) || (n > 255))	continue;
328*2beedcaeSRudolf Cornelissen 
329*2beedcaeSRudolf Cornelissen 				/* find error in frequency this setting gives */
330*2beedcaeSRudolf Cornelissen 				if (si->ps.ext_pll)
331*2beedcaeSRudolf Cornelissen 				{
332*2beedcaeSRudolf Cornelissen 					/* FX5600 and FX5700 tweak for 2nd set N and M scalers */
333*2beedcaeSRudolf Cornelissen 					error = fabs((req_pclk / 4) - (((si->ps.f_ref / m) * n) / p));
334*2beedcaeSRudolf Cornelissen 				}
335*2beedcaeSRudolf Cornelissen 				else
336*2beedcaeSRudolf Cornelissen 					error = fabs(req_pclk - (((si->ps.f_ref / m) * n) / p));
337*2beedcaeSRudolf Cornelissen 
338*2beedcaeSRudolf Cornelissen 				/* note the setting if best yet */
339*2beedcaeSRudolf Cornelissen 				if (error < error_best)
340*2beedcaeSRudolf Cornelissen 				{
341*2beedcaeSRudolf Cornelissen 					error_best = error;
342*2beedcaeSRudolf Cornelissen 					best[0]=m;
343*2beedcaeSRudolf Cornelissen 					best[1]=n;
344*2beedcaeSRudolf Cornelissen 					best[2]=p;
345*2beedcaeSRudolf Cornelissen 				}
346*2beedcaeSRudolf Cornelissen 			}
347*2beedcaeSRudolf Cornelissen 		}
348*2beedcaeSRudolf Cornelissen 	}
349*2beedcaeSRudolf Cornelissen 
350*2beedcaeSRudolf Cornelissen 	/* setup the scalers programming values for found optimum setting */
351*2beedcaeSRudolf Cornelissen 	m = best[0];
352*2beedcaeSRudolf Cornelissen 	n = best[1];
353*2beedcaeSRudolf Cornelissen 	p = best[2];
354*2beedcaeSRudolf Cornelissen 
355*2beedcaeSRudolf Cornelissen 	/* log the VCO frequency found */
356*2beedcaeSRudolf Cornelissen 	f_vco = ((si->ps.f_ref / m) * n);
357*2beedcaeSRudolf Cornelissen 	/* FX5600 and FX5700 tweak for 2nd set N and M scalers */
358*2beedcaeSRudolf Cornelissen 	if (si->ps.ext_pll) f_vco *= 4;
359*2beedcaeSRudolf Cornelissen 
360*2beedcaeSRudolf Cornelissen 	LOG(2,("DAC2: pix VCO frequency found %fMhz\n", f_vco));
361*2beedcaeSRudolf Cornelissen 
362*2beedcaeSRudolf Cornelissen 	/* return the results */
363*2beedcaeSRudolf Cornelissen 	*calc_pclk = (f_vco / p);
364*2beedcaeSRudolf Cornelissen 	*m_result = m;
365*2beedcaeSRudolf Cornelissen 	*n_result = n;
366*2beedcaeSRudolf Cornelissen 	switch(p)
367*2beedcaeSRudolf Cornelissen 	{
368*2beedcaeSRudolf Cornelissen 	case 1:
369*2beedcaeSRudolf Cornelissen 		p = 0x00;
370*2beedcaeSRudolf Cornelissen 		break;
371*2beedcaeSRudolf Cornelissen 	case 2:
372*2beedcaeSRudolf Cornelissen 		p = 0x01;
373*2beedcaeSRudolf Cornelissen 		break;
374*2beedcaeSRudolf Cornelissen 	case 4:
375*2beedcaeSRudolf Cornelissen 		p = 0x02;
376*2beedcaeSRudolf Cornelissen 		break;
377*2beedcaeSRudolf Cornelissen 	case 8:
378*2beedcaeSRudolf Cornelissen 		p = 0x03;
379*2beedcaeSRudolf Cornelissen 		break;
380*2beedcaeSRudolf Cornelissen 	case 16:
381*2beedcaeSRudolf Cornelissen 		p = 0x04;
382*2beedcaeSRudolf Cornelissen 		break;
383*2beedcaeSRudolf Cornelissen 	}
384*2beedcaeSRudolf Cornelissen 	*p_result = p;
385*2beedcaeSRudolf Cornelissen 
386*2beedcaeSRudolf Cornelissen 	/* display the found pixelclock values */
387*2beedcaeSRudolf Cornelissen 	LOG(2,("DAC2: pix PLL check: requested %fMHz got %fMHz, mnp 0x%02x 0x%02x 0x%02x\n",
388*2beedcaeSRudolf Cornelissen 		req_pclk, *calc_pclk, *m_result, *n_result, *p_result));
389*2beedcaeSRudolf Cornelissen 
390*2beedcaeSRudolf Cornelissen 	return B_OK;
391*2beedcaeSRudolf Cornelissen }
392