xref: /linux/drivers/gpu/drm/mgag200/mgag200_mode.c (revision f0493e653f9679114d1dfd54ab88b54ce95576e1)
1414c4531SDave Airlie /*
2414c4531SDave Airlie  * Copyright 2010 Matt Turner.
3414c4531SDave Airlie  * Copyright 2012 Red Hat
4414c4531SDave Airlie  *
5414c4531SDave Airlie  * This file is subject to the terms and conditions of the GNU General
6414c4531SDave Airlie  * Public License version 2. See the file COPYING in the main
7414c4531SDave Airlie  * directory of this archive for more details.
8414c4531SDave Airlie  *
9414c4531SDave Airlie  * Authors: Matthew Garrett
10414c4531SDave Airlie  *	    Matt Turner
11414c4531SDave Airlie  *	    Dave Airlie
12414c4531SDave Airlie  */
13414c4531SDave Airlie 
14414c4531SDave Airlie #include <linux/delay.h>
15414c4531SDave Airlie 
16760285e7SDavid Howells #include <drm/drmP.h>
17760285e7SDavid Howells #include <drm/drm_crtc_helper.h>
183cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
19414c4531SDave Airlie 
20414c4531SDave Airlie #include "mgag200_drv.h"
21414c4531SDave Airlie 
22414c4531SDave Airlie #define MGAG200_LUT_SIZE 256
23414c4531SDave Airlie 
24414c4531SDave Airlie /*
25414c4531SDave Airlie  * This file contains setup code for the CRTC.
26414c4531SDave Airlie  */
27414c4531SDave Airlie 
28414c4531SDave Airlie static void mga_crtc_load_lut(struct drm_crtc *crtc)
29414c4531SDave Airlie {
30414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
31414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
32414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
33f4510a27SMatt Roper 	struct drm_framebuffer *fb = crtc->primary->fb;
34414c4531SDave Airlie 	int i;
35414c4531SDave Airlie 
36414c4531SDave Airlie 	if (!crtc->enabled)
37414c4531SDave Airlie 		return;
38414c4531SDave Airlie 
39414c4531SDave Airlie 	WREG8(DAC_INDEX + MGA1064_INDEX, 0);
40414c4531SDave Airlie 
41272725c7SVille Syrjälä 	if (fb && fb->format->cpp[0] * 8 == 16) {
42b00c600eSVille Syrjälä 		int inc = (fb->format->depth == 15) ? 8 : 4;
43de7500eaSEgbert Eich 		u8 r, b;
44de7500eaSEgbert Eich 		for (i = 0; i < MGAG200_LUT_SIZE; i += inc) {
45b00c600eSVille Syrjälä 			if (fb->format->depth == 16) {
46de7500eaSEgbert Eich 				if (i > (MGAG200_LUT_SIZE >> 1)) {
47de7500eaSEgbert Eich 					r = b = 0;
48de7500eaSEgbert Eich 				} else {
49de7500eaSEgbert Eich 					r = mga_crtc->lut_r[i << 1];
50de7500eaSEgbert Eich 					b = mga_crtc->lut_b[i << 1];
51de7500eaSEgbert Eich 				}
52de7500eaSEgbert Eich 			} else {
53de7500eaSEgbert Eich 				r = mga_crtc->lut_r[i];
54de7500eaSEgbert Eich 				b = mga_crtc->lut_b[i];
55de7500eaSEgbert Eich 			}
56de7500eaSEgbert Eich 			/* VGA registers */
57de7500eaSEgbert Eich 			WREG8(DAC_INDEX + MGA1064_COL_PAL, r);
58de7500eaSEgbert Eich 			WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
59de7500eaSEgbert Eich 			WREG8(DAC_INDEX + MGA1064_COL_PAL, b);
60de7500eaSEgbert Eich 		}
61de7500eaSEgbert Eich 		return;
62de7500eaSEgbert Eich 	}
63414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
64414c4531SDave Airlie 		/* VGA registers */
65414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_r[i]);
66414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
67414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_b[i]);
68414c4531SDave Airlie 	}
69414c4531SDave Airlie }
70414c4531SDave Airlie 
71414c4531SDave Airlie static inline void mga_wait_vsync(struct mga_device *mdev)
72414c4531SDave Airlie {
733cdc0e8dSChristopher Harvey 	unsigned long timeout = jiffies + HZ/10;
74414c4531SDave Airlie 	unsigned int status = 0;
75414c4531SDave Airlie 
76414c4531SDave Airlie 	do {
77414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
783cdc0e8dSChristopher Harvey 	} while ((status & 0x08) && time_before(jiffies, timeout));
793cdc0e8dSChristopher Harvey 	timeout = jiffies + HZ/10;
80414c4531SDave Airlie 	status = 0;
81414c4531SDave Airlie 	do {
82414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
833cdc0e8dSChristopher Harvey 	} while (!(status & 0x08) && time_before(jiffies, timeout));
84414c4531SDave Airlie }
85414c4531SDave Airlie 
86414c4531SDave Airlie static inline void mga_wait_busy(struct mga_device *mdev)
87414c4531SDave Airlie {
883cdc0e8dSChristopher Harvey 	unsigned long timeout = jiffies + HZ;
89414c4531SDave Airlie 	unsigned int status = 0;
90414c4531SDave Airlie 	do {
91414c4531SDave Airlie 		status = RREG8(MGAREG_Status + 2);
923cdc0e8dSChristopher Harvey 	} while ((status & 0x01) && time_before(jiffies, timeout));
93414c4531SDave Airlie }
94414c4531SDave Airlie 
95e829d7efSMathieu Larouche #define P_ARRAY_SIZE 9
96e829d7efSMathieu Larouche 
97414c4531SDave Airlie static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
98414c4531SDave Airlie {
99414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
100414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
101414c4531SDave Airlie 	unsigned int testp, testm, testn;
102414c4531SDave Airlie 	unsigned int p, m, n;
103414c4531SDave Airlie 	unsigned int computed;
104e829d7efSMathieu Larouche 	unsigned int pvalues_e4[P_ARRAY_SIZE] = {16, 14, 12, 10, 8, 6, 4, 2, 1};
105e829d7efSMathieu Larouche 	unsigned int fvv;
106e829d7efSMathieu Larouche 	unsigned int i;
107e829d7efSMathieu Larouche 
108e829d7efSMathieu Larouche 	if (mdev->unique_rev_id <= 0x03) {
109414c4531SDave Airlie 
110414c4531SDave Airlie 		m = n = p = 0;
111414c4531SDave Airlie 		vcomax = 320000;
112414c4531SDave Airlie 		vcomin = 160000;
113414c4531SDave Airlie 		pllreffreq = 25000;
114414c4531SDave Airlie 
115414c4531SDave Airlie 		delta = 0xffffffff;
116414c4531SDave Airlie 		permitteddelta = clock * 5 / 1000;
117414c4531SDave Airlie 
118414c4531SDave Airlie 		for (testp = 8; testp > 0; testp /= 2) {
119414c4531SDave Airlie 			if (clock * testp > vcomax)
120414c4531SDave Airlie 				continue;
121414c4531SDave Airlie 			if (clock * testp < vcomin)
122414c4531SDave Airlie 				continue;
123414c4531SDave Airlie 
124414c4531SDave Airlie 			for (testn = 17; testn < 256; testn++) {
125414c4531SDave Airlie 				for (testm = 1; testm < 32; testm++) {
126414c4531SDave Airlie 					computed = (pllreffreq * testn) /
127414c4531SDave Airlie 						(testm * testp);
128414c4531SDave Airlie 					if (computed > clock)
129414c4531SDave Airlie 						tmpdelta = computed - clock;
130414c4531SDave Airlie 					else
131414c4531SDave Airlie 						tmpdelta = clock - computed;
132414c4531SDave Airlie 					if (tmpdelta < delta) {
133414c4531SDave Airlie 						delta = tmpdelta;
134414c4531SDave Airlie 						m = testm - 1;
135414c4531SDave Airlie 						n = testn - 1;
136414c4531SDave Airlie 						p = testp - 1;
137414c4531SDave Airlie 					}
138414c4531SDave Airlie 				}
139414c4531SDave Airlie 			}
140414c4531SDave Airlie 		}
141e829d7efSMathieu Larouche 	} else {
142e829d7efSMathieu Larouche 
143e829d7efSMathieu Larouche 
144e829d7efSMathieu Larouche 		m = n = p = 0;
145e829d7efSMathieu Larouche 		vcomax        = 1600000;
146e829d7efSMathieu Larouche 		vcomin        = 800000;
147e829d7efSMathieu Larouche 		pllreffreq    = 25000;
148e829d7efSMathieu Larouche 
149e829d7efSMathieu Larouche 		if (clock < 25000)
150e829d7efSMathieu Larouche 			clock = 25000;
151e829d7efSMathieu Larouche 
152e829d7efSMathieu Larouche 		clock = clock * 2;
153e829d7efSMathieu Larouche 
154e829d7efSMathieu Larouche 		delta = 0xFFFFFFFF;
155e829d7efSMathieu Larouche 		/* Permited delta is 0.5% as VESA Specification */
156e829d7efSMathieu Larouche 		permitteddelta = clock * 5 / 1000;
157e829d7efSMathieu Larouche 
158e829d7efSMathieu Larouche 		for (i = 0 ; i < P_ARRAY_SIZE ; i++) {
159e829d7efSMathieu Larouche 			testp = pvalues_e4[i];
160e829d7efSMathieu Larouche 
161e829d7efSMathieu Larouche 			if ((clock * testp) > vcomax)
162e829d7efSMathieu Larouche 				continue;
163e829d7efSMathieu Larouche 			if ((clock * testp) < vcomin)
164e829d7efSMathieu Larouche 				continue;
165e829d7efSMathieu Larouche 
166e829d7efSMathieu Larouche 			for (testn = 50; testn <= 256; testn++) {
167e829d7efSMathieu Larouche 				for (testm = 1; testm <= 32; testm++) {
168e829d7efSMathieu Larouche 					computed = (pllreffreq * testn) /
169e829d7efSMathieu Larouche 						(testm * testp);
170e829d7efSMathieu Larouche 					if (computed > clock)
171e829d7efSMathieu Larouche 						tmpdelta = computed - clock;
172e829d7efSMathieu Larouche 					else
173e829d7efSMathieu Larouche 						tmpdelta = clock - computed;
174e829d7efSMathieu Larouche 
175e829d7efSMathieu Larouche 					if (tmpdelta < delta) {
176e829d7efSMathieu Larouche 						delta = tmpdelta;
177e829d7efSMathieu Larouche 						m = testm - 1;
178e829d7efSMathieu Larouche 						n = testn - 1;
179e829d7efSMathieu Larouche 						p = testp - 1;
180e829d7efSMathieu Larouche 					}
181e829d7efSMathieu Larouche 				}
182e829d7efSMathieu Larouche 			}
183e829d7efSMathieu Larouche 		}
184e829d7efSMathieu Larouche 
185d3922b69SMathieu Larouche 		fvv = pllreffreq * (n + 1) / (m + 1);
186e829d7efSMathieu Larouche 		fvv = (fvv - 800000) / 50000;
187e829d7efSMathieu Larouche 
188e829d7efSMathieu Larouche 		if (fvv > 15)
189e829d7efSMathieu Larouche 			fvv = 15;
190e829d7efSMathieu Larouche 
191e829d7efSMathieu Larouche 		p |= (fvv << 4);
192e829d7efSMathieu Larouche 		m |= 0x80;
193e829d7efSMathieu Larouche 
194e829d7efSMathieu Larouche 		clock = clock / 2;
195e829d7efSMathieu Larouche 	}
196414c4531SDave Airlie 
197414c4531SDave Airlie 	if (delta > permitteddelta) {
198414c4531SDave Airlie 		printk(KERN_WARNING "PLL delta too large\n");
199414c4531SDave Airlie 		return 1;
200414c4531SDave Airlie 	}
201414c4531SDave Airlie 
202414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_M, m);
203414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_N, n);
204414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_P, p);
205d3922b69SMathieu Larouche 
206d3922b69SMathieu Larouche 	if (mdev->unique_rev_id >= 0x04) {
207d3922b69SMathieu Larouche 		WREG_DAC(0x1a, 0x09);
208d3922b69SMathieu Larouche 		msleep(20);
209d3922b69SMathieu Larouche 		WREG_DAC(0x1a, 0x01);
210d3922b69SMathieu Larouche 
211d3922b69SMathieu Larouche 	}
212d3922b69SMathieu Larouche 
213414c4531SDave Airlie 	return 0;
214414c4531SDave Airlie }
215414c4531SDave Airlie 
216414c4531SDave Airlie static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
217414c4531SDave Airlie {
218414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
219546aee51SSudip Mukherjee 	unsigned int delta, tmpdelta;
2206d857c18SMathieu Larouche 	unsigned int testp, testm, testn, testp2;
221414c4531SDave Airlie 	unsigned int p, m, n;
222414c4531SDave Airlie 	unsigned int computed;
223414c4531SDave Airlie 	int i, j, tmpcount, vcount;
224414c4531SDave Airlie 	bool pll_locked = false;
225414c4531SDave Airlie 	u8 tmp;
226414c4531SDave Airlie 
227414c4531SDave Airlie 	m = n = p = 0;
2286d857c18SMathieu Larouche 
2296d857c18SMathieu Larouche 	delta = 0xffffffff;
2306d857c18SMathieu Larouche 
2316d857c18SMathieu Larouche 	if (mdev->type == G200_EW3) {
2326d857c18SMathieu Larouche 
2336d857c18SMathieu Larouche 		vcomax = 800000;
2346d857c18SMathieu Larouche 		vcomin = 400000;
2356d857c18SMathieu Larouche 		pllreffreq = 25000;
2366d857c18SMathieu Larouche 
2376d857c18SMathieu Larouche 		for (testp = 1; testp < 8; testp++) {
2386d857c18SMathieu Larouche 			for (testp2 = 1; testp2 < 8; testp2++) {
2396d857c18SMathieu Larouche 				if (testp < testp2)
2406d857c18SMathieu Larouche 					continue;
2416d857c18SMathieu Larouche 				if ((clock * testp * testp2) > vcomax)
2426d857c18SMathieu Larouche 					continue;
2436d857c18SMathieu Larouche 				if ((clock * testp * testp2) < vcomin)
2446d857c18SMathieu Larouche 					continue;
2456d857c18SMathieu Larouche 				for (testm = 1; testm < 26; testm++) {
2466d857c18SMathieu Larouche 					for (testn = 32; testn < 2048 ; testn++) {
2476d857c18SMathieu Larouche 						computed = (pllreffreq * testn) /
2486d857c18SMathieu Larouche 							(testm * testp * testp2);
2496d857c18SMathieu Larouche 						if (computed > clock)
2506d857c18SMathieu Larouche 							tmpdelta = computed - clock;
2516d857c18SMathieu Larouche 						else
2526d857c18SMathieu Larouche 							tmpdelta = clock - computed;
2536d857c18SMathieu Larouche 						if (tmpdelta < delta) {
2546d857c18SMathieu Larouche 							delta = tmpdelta;
2556d857c18SMathieu Larouche 							m = ((testn & 0x100) >> 1) |
2566d857c18SMathieu Larouche 								(testm);
2576d857c18SMathieu Larouche 							n = (testn & 0xFF);
2586d857c18SMathieu Larouche 							p = ((testn & 0x600) >> 3) |
2596d857c18SMathieu Larouche 								(testp2 << 3) |
2606d857c18SMathieu Larouche 								(testp);
2616d857c18SMathieu Larouche 						}
2626d857c18SMathieu Larouche 					}
2636d857c18SMathieu Larouche 				}
2646d857c18SMathieu Larouche 			}
2656d857c18SMathieu Larouche 		}
2666d857c18SMathieu Larouche 	} else {
2676d857c18SMathieu Larouche 
268414c4531SDave Airlie 		vcomax = 550000;
269414c4531SDave Airlie 		vcomin = 150000;
270414c4531SDave Airlie 		pllreffreq = 48000;
271414c4531SDave Airlie 
272414c4531SDave Airlie 		for (testp = 1; testp < 9; testp++) {
273414c4531SDave Airlie 			if (clock * testp > vcomax)
274414c4531SDave Airlie 				continue;
275414c4531SDave Airlie 			if (clock * testp < vcomin)
276414c4531SDave Airlie 				continue;
277414c4531SDave Airlie 
278414c4531SDave Airlie 			for (testm = 1; testm < 17; testm++) {
279414c4531SDave Airlie 				for (testn = 1; testn < 151; testn++) {
280414c4531SDave Airlie 					computed = (pllreffreq * testn) /
281414c4531SDave Airlie 						(testm * testp);
282414c4531SDave Airlie 					if (computed > clock)
283414c4531SDave Airlie 						tmpdelta = computed - clock;
284414c4531SDave Airlie 					else
285414c4531SDave Airlie 						tmpdelta = clock - computed;
286414c4531SDave Airlie 					if (tmpdelta < delta) {
287414c4531SDave Airlie 						delta = tmpdelta;
288414c4531SDave Airlie 						n = testn - 1;
2896d857c18SMathieu Larouche 						m = (testm - 1) |
2906d857c18SMathieu Larouche 							((n >> 1) & 0x80);
291414c4531SDave Airlie 						p = testp - 1;
292414c4531SDave Airlie 					}
293414c4531SDave Airlie 				}
294414c4531SDave Airlie 			}
295414c4531SDave Airlie 		}
2966d857c18SMathieu Larouche 	}
297414c4531SDave Airlie 
298414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
299414c4531SDave Airlie 		if (i > 0) {
300414c4531SDave Airlie 			WREG8(MGAREG_CRTC_INDEX, 0x1e);
301414c4531SDave Airlie 			tmp = RREG8(MGAREG_CRTC_DATA);
302414c4531SDave Airlie 			if (tmp < 0xff)
303414c4531SDave Airlie 				WREG8(MGAREG_CRTC_DATA, tmp+1);
304414c4531SDave Airlie 		}
305414c4531SDave Airlie 
306414c4531SDave Airlie 		/* set pixclkdis to 1 */
307414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
308414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
309414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
310fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
311414c4531SDave Airlie 
312414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
313414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
314414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKDIS;
315fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
316414c4531SDave Airlie 
317414c4531SDave Airlie 		/* select PLL Set C */
318414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
319414c4531SDave Airlie 		tmp |= 0x3 << 2;
320414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
321414c4531SDave Airlie 
322414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
323414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
324414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
325fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
326414c4531SDave Airlie 
327414c4531SDave Airlie 		udelay(500);
328414c4531SDave Airlie 
329414c4531SDave Airlie 		/* reset the PLL */
330414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
331414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
332414c4531SDave Airlie 		tmp &= ~0x04;
333fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
334414c4531SDave Airlie 
335414c4531SDave Airlie 		udelay(50);
336414c4531SDave Airlie 
337414c4531SDave Airlie 		/* program pixel pll register */
338414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
339414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
340414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
341414c4531SDave Airlie 
342414c4531SDave Airlie 		udelay(50);
343414c4531SDave Airlie 
344414c4531SDave Airlie 		/* turn pll on */
345414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
346414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
347414c4531SDave Airlie 		tmp |= 0x04;
348414c4531SDave Airlie 		WREG_DAC(MGA1064_VREF_CTL, tmp);
349414c4531SDave Airlie 
350414c4531SDave Airlie 		udelay(500);
351414c4531SDave Airlie 
352414c4531SDave Airlie 		/* select the pixel pll */
353414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
354414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
355414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
356414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
357fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
358414c4531SDave Airlie 
359414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
360414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
361414c4531SDave Airlie 		tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
362414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
363fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
364414c4531SDave Airlie 
365414c4531SDave Airlie 		/* reset dotclock rate bit */
366414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 1);
367414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
368414c4531SDave Airlie 		tmp &= ~0x8;
369414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, tmp);
370414c4531SDave Airlie 
371414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
372414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
373414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
374fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
375414c4531SDave Airlie 
376414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
377414c4531SDave Airlie 
378414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
379414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
380414c4531SDave Airlie 			if (tmpcount < vcount)
381414c4531SDave Airlie 				vcount = 0;
382414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
383414c4531SDave Airlie 				pll_locked = true;
384414c4531SDave Airlie 			else
385414c4531SDave Airlie 				udelay(5);
386414c4531SDave Airlie 		}
387414c4531SDave Airlie 	}
388414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
389414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
390414c4531SDave Airlie 	tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
391414c4531SDave Airlie 	WREG_DAC(MGA1064_REMHEADCTL, tmp);
392414c4531SDave Airlie 	return 0;
393414c4531SDave Airlie }
394414c4531SDave Airlie 
395414c4531SDave Airlie static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
396414c4531SDave Airlie {
397414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
398546aee51SSudip Mukherjee 	unsigned int delta, tmpdelta;
399414c4531SDave Airlie 	unsigned int testp, testm, testn;
400414c4531SDave Airlie 	unsigned int p, m, n;
401414c4531SDave Airlie 	unsigned int computed;
402414c4531SDave Airlie 	u8 tmp;
403414c4531SDave Airlie 
404414c4531SDave Airlie 	m = n = p = 0;
405414c4531SDave Airlie 	vcomax = 550000;
406414c4531SDave Airlie 	vcomin = 150000;
407414c4531SDave Airlie 	pllreffreq = 50000;
408414c4531SDave Airlie 
409414c4531SDave Airlie 	delta = 0xffffffff;
410414c4531SDave Airlie 
411414c4531SDave Airlie 	for (testp = 16; testp > 0; testp--) {
412414c4531SDave Airlie 		if (clock * testp > vcomax)
413414c4531SDave Airlie 			continue;
414414c4531SDave Airlie 		if (clock * testp < vcomin)
415414c4531SDave Airlie 			continue;
416414c4531SDave Airlie 
417414c4531SDave Airlie 		for (testn = 1; testn < 257; testn++) {
418414c4531SDave Airlie 			for (testm = 1; testm < 17; testm++) {
419414c4531SDave Airlie 				computed = (pllreffreq * testn) /
420414c4531SDave Airlie 					(testm * testp);
421414c4531SDave Airlie 				if (computed > clock)
422414c4531SDave Airlie 					tmpdelta = computed - clock;
423414c4531SDave Airlie 				else
424414c4531SDave Airlie 					tmpdelta = clock - computed;
425414c4531SDave Airlie 				if (tmpdelta < delta) {
426414c4531SDave Airlie 					delta = tmpdelta;
427414c4531SDave Airlie 					n = testn - 1;
428414c4531SDave Airlie 					m = testm - 1;
429414c4531SDave Airlie 					p = testp - 1;
430414c4531SDave Airlie 				}
431414c4531SDave Airlie 			}
432414c4531SDave Airlie 		}
433414c4531SDave Airlie 	}
434414c4531SDave Airlie 
435414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
436414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
437414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
438fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
439414c4531SDave Airlie 
440414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
441414c4531SDave Airlie 	tmp |= 0x3 << 2;
442414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
443414c4531SDave Airlie 
444414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
445414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
446fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp & ~0x40);
447414c4531SDave Airlie 
448414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
449414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
450414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
451fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
452414c4531SDave Airlie 
453414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
454414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
455414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
456414c4531SDave Airlie 
457414c4531SDave Airlie 	udelay(50);
458414c4531SDave Airlie 
459414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
460414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
461414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
462fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
463414c4531SDave Airlie 
464414c4531SDave Airlie 	udelay(500);
465414c4531SDave Airlie 
466414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
467414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
468414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
469414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
470fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
471414c4531SDave Airlie 
472414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
473414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
474fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp | 0x40);
475414c4531SDave Airlie 
476414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
477414c4531SDave Airlie 	tmp |= (0x3 << 2);
478414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
479414c4531SDave Airlie 
480414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
481414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
482414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
483fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
484414c4531SDave Airlie 
485414c4531SDave Airlie 	return 0;
486414c4531SDave Airlie }
487414c4531SDave Airlie 
488414c4531SDave Airlie static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
489414c4531SDave Airlie {
490414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
491546aee51SSudip Mukherjee 	unsigned int delta, tmpdelta;
492414c4531SDave Airlie 	unsigned int testp, testm, testn;
493414c4531SDave Airlie 	unsigned int p, m, n;
494414c4531SDave Airlie 	unsigned int computed;
495414c4531SDave Airlie 	int i, j, tmpcount, vcount;
496414c4531SDave Airlie 	u8 tmp;
497414c4531SDave Airlie 	bool pll_locked = false;
498414c4531SDave Airlie 
499414c4531SDave Airlie 	m = n = p = 0;
500*f0493e65SMathieu Larouche 
501*f0493e65SMathieu Larouche 	if (mdev->type == G200_EH3) {
502*f0493e65SMathieu Larouche 		vcomax = 3000000;
503*f0493e65SMathieu Larouche 		vcomin = 1500000;
504*f0493e65SMathieu Larouche 		pllreffreq = 25000;
505*f0493e65SMathieu Larouche 
506*f0493e65SMathieu Larouche 		delta = 0xffffffff;
507*f0493e65SMathieu Larouche 
508*f0493e65SMathieu Larouche 		testp = 0;
509*f0493e65SMathieu Larouche 
510*f0493e65SMathieu Larouche 		for (testm = 150; testm >= 6; testm--) {
511*f0493e65SMathieu Larouche 			if (clock * testm > vcomax)
512*f0493e65SMathieu Larouche 				continue;
513*f0493e65SMathieu Larouche 			if (clock * testm < vcomin)
514*f0493e65SMathieu Larouche 				continue;
515*f0493e65SMathieu Larouche 			for (testn = 120; testn >= 60; testn--) {
516*f0493e65SMathieu Larouche 				computed = (pllreffreq * testn) / testm;
517*f0493e65SMathieu Larouche 				if (computed > clock)
518*f0493e65SMathieu Larouche 					tmpdelta = computed - clock;
519*f0493e65SMathieu Larouche 				else
520*f0493e65SMathieu Larouche 					tmpdelta = clock - computed;
521*f0493e65SMathieu Larouche 				if (tmpdelta < delta) {
522*f0493e65SMathieu Larouche 					delta = tmpdelta;
523*f0493e65SMathieu Larouche 					n = testn;
524*f0493e65SMathieu Larouche 					m = testm;
525*f0493e65SMathieu Larouche 					p = testp;
526*f0493e65SMathieu Larouche 				}
527*f0493e65SMathieu Larouche 				if (delta == 0)
528*f0493e65SMathieu Larouche 					break;
529*f0493e65SMathieu Larouche 			}
530*f0493e65SMathieu Larouche 			if (delta == 0)
531*f0493e65SMathieu Larouche 				break;
532*f0493e65SMathieu Larouche 		}
533*f0493e65SMathieu Larouche 	} else {
534*f0493e65SMathieu Larouche 
535414c4531SDave Airlie 		vcomax = 800000;
536414c4531SDave Airlie 		vcomin = 400000;
537260b3f12SJulia Lemire 		pllreffreq = 33333;
538414c4531SDave Airlie 
539414c4531SDave Airlie 		delta = 0xffffffff;
540414c4531SDave Airlie 
541260b3f12SJulia Lemire 		for (testp = 16; testp > 0; testp >>= 1) {
542414c4531SDave Airlie 			if (clock * testp > vcomax)
543414c4531SDave Airlie 				continue;
544414c4531SDave Airlie 			if (clock * testp < vcomin)
545414c4531SDave Airlie 				continue;
546414c4531SDave Airlie 
547414c4531SDave Airlie 			for (testm = 1; testm < 33; testm++) {
548260b3f12SJulia Lemire 				for (testn = 17; testn < 257; testn++) {
549414c4531SDave Airlie 					computed = (pllreffreq * testn) /
550414c4531SDave Airlie 						(testm * testp);
551414c4531SDave Airlie 					if (computed > clock)
552414c4531SDave Airlie 						tmpdelta = computed - clock;
553414c4531SDave Airlie 					else
554414c4531SDave Airlie 						tmpdelta = clock - computed;
555414c4531SDave Airlie 					if (tmpdelta < delta) {
556414c4531SDave Airlie 						delta = tmpdelta;
557414c4531SDave Airlie 						n = testn - 1;
558260b3f12SJulia Lemire 						m = (testm - 1);
559414c4531SDave Airlie 						p = testp - 1;
560414c4531SDave Airlie 					}
561414c4531SDave Airlie 					if ((clock * testp) >= 600000)
562260b3f12SJulia Lemire 						p |= 0x80;
563414c4531SDave Airlie 				}
564414c4531SDave Airlie 			}
565414c4531SDave Airlie 		}
566*f0493e65SMathieu Larouche 	}
567414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
568414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
569414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
570414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
571fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
572414c4531SDave Airlie 
573414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
574414c4531SDave Airlie 		tmp |= 0x3 << 2;
575414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
576414c4531SDave Airlie 
577414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
578414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
579414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
580fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
581414c4531SDave Airlie 
582414c4531SDave Airlie 		udelay(500);
583414c4531SDave Airlie 
584414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
585414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
586414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
587414c4531SDave Airlie 
588414c4531SDave Airlie 		udelay(500);
589414c4531SDave Airlie 
590414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
591414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
592414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
593414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
594fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
595414c4531SDave Airlie 
596414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
597414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
598414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
599414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
600fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
601414c4531SDave Airlie 
602414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
603414c4531SDave Airlie 
604414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
605414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
606414c4531SDave Airlie 			if (tmpcount < vcount)
607414c4531SDave Airlie 				vcount = 0;
608414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
609414c4531SDave Airlie 				pll_locked = true;
610414c4531SDave Airlie 			else
611414c4531SDave Airlie 				udelay(5);
612414c4531SDave Airlie 		}
613414c4531SDave Airlie 	}
614414c4531SDave Airlie 
615414c4531SDave Airlie 	return 0;
616414c4531SDave Airlie }
617414c4531SDave Airlie 
618414c4531SDave Airlie static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
619414c4531SDave Airlie {
620414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
621414c4531SDave Airlie 	unsigned int delta, tmpdelta;
6229830605dSDave Airlie 	int testr, testn, testm, testo;
623414c4531SDave Airlie 	unsigned int p, m, n;
6249830605dSDave Airlie 	unsigned int computed, vco;
625414c4531SDave Airlie 	int tmp;
6269830605dSDave Airlie 	const unsigned int m_div_val[] = { 1, 2, 4, 8 };
627414c4531SDave Airlie 
628414c4531SDave Airlie 	m = n = p = 0;
629414c4531SDave Airlie 	vcomax = 1488000;
630414c4531SDave Airlie 	vcomin = 1056000;
631414c4531SDave Airlie 	pllreffreq = 48000;
632414c4531SDave Airlie 
633414c4531SDave Airlie 	delta = 0xffffffff;
634414c4531SDave Airlie 
635414c4531SDave Airlie 	for (testr = 0; testr < 4; testr++) {
636414c4531SDave Airlie 		if (delta == 0)
637414c4531SDave Airlie 			break;
638414c4531SDave Airlie 		for (testn = 5; testn < 129; testn++) {
639414c4531SDave Airlie 			if (delta == 0)
640414c4531SDave Airlie 				break;
641414c4531SDave Airlie 			for (testm = 3; testm >= 0; testm--) {
642414c4531SDave Airlie 				if (delta == 0)
643414c4531SDave Airlie 					break;
644414c4531SDave Airlie 				for (testo = 5; testo < 33; testo++) {
6459830605dSDave Airlie 					vco = pllreffreq * (testn + 1) /
646414c4531SDave Airlie 						(testr + 1);
6479830605dSDave Airlie 					if (vco < vcomin)
648414c4531SDave Airlie 						continue;
6499830605dSDave Airlie 					if (vco > vcomax)
650414c4531SDave Airlie 						continue;
6519830605dSDave Airlie 					computed = vco / (m_div_val[testm] * (testo + 1));
652414c4531SDave Airlie 					if (computed > clock)
653414c4531SDave Airlie 						tmpdelta = computed - clock;
654414c4531SDave Airlie 					else
655414c4531SDave Airlie 						tmpdelta = clock - computed;
656414c4531SDave Airlie 					if (tmpdelta < delta) {
657414c4531SDave Airlie 						delta = tmpdelta;
658414c4531SDave Airlie 						m = testm | (testo << 3);
659414c4531SDave Airlie 						n = testn;
660414c4531SDave Airlie 						p = testr | (testr << 3);
661414c4531SDave Airlie 					}
662414c4531SDave Airlie 				}
663414c4531SDave Airlie 			}
664414c4531SDave Airlie 		}
665414c4531SDave Airlie 	}
666414c4531SDave Airlie 
667414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
668414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
669414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
670fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
671414c4531SDave Airlie 
672414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
673414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
674414c4531SDave Airlie 	tmp |= MGA1064_REMHEADCTL_CLKDIS;
675fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
676414c4531SDave Airlie 
677414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
678414c4531SDave Airlie 	tmp |= (0x3<<2) | 0xc0;
679414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
680414c4531SDave Airlie 
681414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
682414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
683414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
684414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
685fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
686414c4531SDave Airlie 
687414c4531SDave Airlie 	udelay(500);
688414c4531SDave Airlie 
689414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
690414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
691414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
692414c4531SDave Airlie 
693414c4531SDave Airlie 	udelay(50);
694414c4531SDave Airlie 
695414c4531SDave Airlie 	return 0;
696414c4531SDave Airlie }
697414c4531SDave Airlie 
698414c4531SDave Airlie static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
699414c4531SDave Airlie {
700414c4531SDave Airlie 	switch(mdev->type) {
701414c4531SDave Airlie 	case G200_SE_A:
702414c4531SDave Airlie 	case G200_SE_B:
703414c4531SDave Airlie 		return mga_g200se_set_plls(mdev, clock);
704414c4531SDave Airlie 		break;
705414c4531SDave Airlie 	case G200_WB:
7066d857c18SMathieu Larouche 	case G200_EW3:
707414c4531SDave Airlie 		return mga_g200wb_set_plls(mdev, clock);
708414c4531SDave Airlie 		break;
709414c4531SDave Airlie 	case G200_EV:
710414c4531SDave Airlie 		return mga_g200ev_set_plls(mdev, clock);
711414c4531SDave Airlie 		break;
712414c4531SDave Airlie 	case G200_EH:
713*f0493e65SMathieu Larouche 	case G200_EH3:
714414c4531SDave Airlie 		return mga_g200eh_set_plls(mdev, clock);
715414c4531SDave Airlie 		break;
716414c4531SDave Airlie 	case G200_ER:
717414c4531SDave Airlie 		return mga_g200er_set_plls(mdev, clock);
718414c4531SDave Airlie 		break;
719414c4531SDave Airlie 	}
720414c4531SDave Airlie 	return 0;
721414c4531SDave Airlie }
722414c4531SDave Airlie 
723414c4531SDave Airlie static void mga_g200wb_prepare(struct drm_crtc *crtc)
724414c4531SDave Airlie {
725414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
726414c4531SDave Airlie 	u8 tmp;
727414c4531SDave Airlie 	int iter_max;
728414c4531SDave Airlie 
729414c4531SDave Airlie 	/* 1- The first step is to warn the BMC of an upcoming mode change.
730414c4531SDave Airlie 	 * We are putting the misc<0> to output.*/
731414c4531SDave Airlie 
732414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
733414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
734414c4531SDave Airlie 	tmp |= 0x10;
735414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
736414c4531SDave Airlie 
737414c4531SDave Airlie 	/* we are putting a 1 on the misc<0> line */
738414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
739414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
740414c4531SDave Airlie 	tmp |= 0x10;
741414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
742414c4531SDave Airlie 
743414c4531SDave Airlie 	/* 2- Second step to mask and further scan request
744414c4531SDave Airlie 	 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
745414c4531SDave Airlie 	 */
746414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
747414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
748414c4531SDave Airlie 	tmp |= 0x80;
749414c4531SDave Airlie 	WREG_DAC(MGA1064_SPAREREG, tmp);
750414c4531SDave Airlie 
751414c4531SDave Airlie 	/* 3a- the third step is to verifu if there is an active scan
752414c4531SDave Airlie 	 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
753414c4531SDave Airlie 	 */
754414c4531SDave Airlie 	iter_max = 300;
755414c4531SDave Airlie 	while (!(tmp & 0x1) && iter_max) {
756414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_SPAREREG);
757414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
758414c4531SDave Airlie 		udelay(1000);
759414c4531SDave Airlie 		iter_max--;
760414c4531SDave Airlie 	}
761414c4531SDave Airlie 
762414c4531SDave Airlie 	/* 3b- this step occurs only if the remove is actually scanning
763414c4531SDave Airlie 	 * we are waiting for the end of the frame which is a 1 on
764414c4531SDave Airlie 	 * remvsyncsts (XSPAREREG<1>)
765414c4531SDave Airlie 	 */
766414c4531SDave Airlie 	if (iter_max) {
767414c4531SDave Airlie 		iter_max = 300;
768414c4531SDave Airlie 		while ((tmp & 0x2) && iter_max) {
769414c4531SDave Airlie 			WREG8(DAC_INDEX, MGA1064_SPAREREG);
770414c4531SDave Airlie 			tmp = RREG8(DAC_DATA);
771414c4531SDave Airlie 			udelay(1000);
772414c4531SDave Airlie 			iter_max--;
773414c4531SDave Airlie 		}
774414c4531SDave Airlie 	}
775414c4531SDave Airlie }
776414c4531SDave Airlie 
777414c4531SDave Airlie static void mga_g200wb_commit(struct drm_crtc *crtc)
778414c4531SDave Airlie {
779414c4531SDave Airlie 	u8 tmp;
780414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
781414c4531SDave Airlie 
782414c4531SDave Airlie 	/* 1- The first step is to ensure that the vrsten and hrsten are set */
783414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 1);
784414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTCEXT_DATA);
785414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
786414c4531SDave Airlie 
787414c4531SDave Airlie 	/* 2- second step is to assert the rstlvl2 */
788414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
789414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
790414c4531SDave Airlie 	tmp |= 0x8;
791414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
792414c4531SDave Airlie 
793414c4531SDave Airlie 	/* wait 10 us */
794414c4531SDave Airlie 	udelay(10);
795414c4531SDave Airlie 
796414c4531SDave Airlie 	/* 3- deassert rstlvl2 */
797414c4531SDave Airlie 	tmp &= ~0x08;
798414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
799414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
800414c4531SDave Airlie 
801414c4531SDave Airlie 	/* 4- remove mask of scan request */
802414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
803414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
804414c4531SDave Airlie 	tmp &= ~0x80;
805414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
806414c4531SDave Airlie 
807414c4531SDave Airlie 	/* 5- put back a 0 on the misc<0> line */
808414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
809414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
810414c4531SDave Airlie 	tmp &= ~0x10;
811414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
812414c4531SDave Airlie }
813414c4531SDave Airlie 
8149f1d0366SChristopher Harvey /*
8159f1d0366SChristopher Harvey    This is how the framebuffer base address is stored in g200 cards:
8169f1d0366SChristopher Harvey    * Assume @offset is the gpu_addr variable of the framebuffer object
8179f1d0366SChristopher Harvey    * Then addr is the number of _pixels_ (not bytes) from the start of
8189f1d0366SChristopher Harvey      VRAM to the first pixel we want to display. (divided by 2 for 32bit
8199f1d0366SChristopher Harvey      framebuffers)
8209f1d0366SChristopher Harvey    * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
8219f1d0366SChristopher Harvey    addr<20> -> CRTCEXT0<6>
8229f1d0366SChristopher Harvey    addr<19-16> -> CRTCEXT0<3-0>
8239f1d0366SChristopher Harvey    addr<15-8> -> CRTCC<7-0>
8249f1d0366SChristopher Harvey    addr<7-0> -> CRTCD<7-0>
8259f1d0366SChristopher Harvey    CRTCEXT0 has to be programmed last to trigger an update and make the
8269f1d0366SChristopher Harvey    new addr variable take effect.
8279f1d0366SChristopher Harvey  */
828080fd6b5SRashika static void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
829414c4531SDave Airlie {
830414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
831414c4531SDave Airlie 	u32 addr;
832414c4531SDave Airlie 	int count;
8339f1d0366SChristopher Harvey 	u8 crtcext0;
834414c4531SDave Airlie 
835414c4531SDave Airlie 	while (RREG8(0x1fda) & 0x08);
836414c4531SDave Airlie 	while (!(RREG8(0x1fda) & 0x08));
837414c4531SDave Airlie 
838414c4531SDave Airlie 	count = RREG8(MGAREG_VCOUNT) + 2;
839414c4531SDave Airlie 	while (RREG8(MGAREG_VCOUNT) < count);
840414c4531SDave Airlie 
8419f1d0366SChristopher Harvey 	WREG8(MGAREG_CRTCEXT_INDEX, 0);
8429f1d0366SChristopher Harvey 	crtcext0 = RREG8(MGAREG_CRTCEXT_DATA);
8439f1d0366SChristopher Harvey 	crtcext0 &= 0xB0;
8449f1d0366SChristopher Harvey 	addr = offset / 8;
8459f1d0366SChristopher Harvey 	/* Can't store addresses any higher than that...
8469f1d0366SChristopher Harvey 	   but we also don't have more than 16MB of memory, so it should be fine. */
8479f1d0366SChristopher Harvey 	WARN_ON(addr > 0x1fffff);
8489f1d0366SChristopher Harvey 	crtcext0 |= (!!(addr & (1<<20)))<<6;
849414c4531SDave Airlie 	WREG_CRT(0x0d, (u8)(addr & 0xff));
850414c4531SDave Airlie 	WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
8519f1d0366SChristopher Harvey 	WREG_ECRT(0x0, ((u8)(addr >> 16) & 0xf) | crtcext0);
852414c4531SDave Airlie }
853414c4531SDave Airlie 
854414c4531SDave Airlie 
855414c4531SDave Airlie /* ast is different - we will force move buffers out of VRAM */
856414c4531SDave Airlie static int mga_crtc_do_set_base(struct drm_crtc *crtc,
857414c4531SDave Airlie 				struct drm_framebuffer *fb,
858414c4531SDave Airlie 				int x, int y, int atomic)
859414c4531SDave Airlie {
860414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
861414c4531SDave Airlie 	struct drm_gem_object *obj;
862414c4531SDave Airlie 	struct mga_framebuffer *mga_fb;
863414c4531SDave Airlie 	struct mgag200_bo *bo;
864414c4531SDave Airlie 	int ret;
865414c4531SDave Airlie 	u64 gpu_addr;
866414c4531SDave Airlie 
867414c4531SDave Airlie 	/* push the previous fb to system ram */
868414c4531SDave Airlie 	if (!atomic && fb) {
869414c4531SDave Airlie 		mga_fb = to_mga_framebuffer(fb);
870414c4531SDave Airlie 		obj = mga_fb->obj;
871414c4531SDave Airlie 		bo = gem_to_mga_bo(obj);
872414c4531SDave Airlie 		ret = mgag200_bo_reserve(bo, false);
873414c4531SDave Airlie 		if (ret)
874414c4531SDave Airlie 			return ret;
875414c4531SDave Airlie 		mgag200_bo_push_sysram(bo);
876414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
877414c4531SDave Airlie 	}
878414c4531SDave Airlie 
879f4510a27SMatt Roper 	mga_fb = to_mga_framebuffer(crtc->primary->fb);
880414c4531SDave Airlie 	obj = mga_fb->obj;
881414c4531SDave Airlie 	bo = gem_to_mga_bo(obj);
882414c4531SDave Airlie 
883414c4531SDave Airlie 	ret = mgag200_bo_reserve(bo, false);
884414c4531SDave Airlie 	if (ret)
885414c4531SDave Airlie 		return ret;
886414c4531SDave Airlie 
887414c4531SDave Airlie 	ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
888414c4531SDave Airlie 	if (ret) {
889414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
890414c4531SDave Airlie 		return ret;
891414c4531SDave Airlie 	}
892414c4531SDave Airlie 
893414c4531SDave Airlie 	if (&mdev->mfbdev->mfb == mga_fb) {
894414c4531SDave Airlie 		/* if pushing console in kmap it */
895414c4531SDave Airlie 		ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
896414c4531SDave Airlie 		if (ret)
897414c4531SDave Airlie 			DRM_ERROR("failed to kmap fbcon\n");
898414c4531SDave Airlie 
899414c4531SDave Airlie 	}
900414c4531SDave Airlie 	mgag200_bo_unreserve(bo);
901414c4531SDave Airlie 
902414c4531SDave Airlie 	mga_set_start_address(crtc, (u32)gpu_addr);
903414c4531SDave Airlie 
904414c4531SDave Airlie 	return 0;
905414c4531SDave Airlie }
906414c4531SDave Airlie 
907414c4531SDave Airlie static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
908414c4531SDave Airlie 				  struct drm_framebuffer *old_fb)
909414c4531SDave Airlie {
910414c4531SDave Airlie 	return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
911414c4531SDave Airlie }
912414c4531SDave Airlie 
913414c4531SDave Airlie static int mga_crtc_mode_set(struct drm_crtc *crtc,
914414c4531SDave Airlie 				struct drm_display_mode *mode,
915414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode,
916414c4531SDave Airlie 				int x, int y, struct drm_framebuffer *old_fb)
917414c4531SDave Airlie {
918414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
919414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
92072952757SVille Syrjälä 	const struct drm_framebuffer *fb = crtc->primary->fb;
921414c4531SDave Airlie 	int hdisplay, hsyncstart, hsyncend, htotal;
922414c4531SDave Airlie 	int vdisplay, vsyncstart, vsyncend, vtotal;
923414c4531SDave Airlie 	int pitch;
924414c4531SDave Airlie 	int option = 0, option2 = 0;
925414c4531SDave Airlie 	int i;
926414c4531SDave Airlie 	unsigned char misc = 0;
927414c4531SDave Airlie 	unsigned char ext_vga[6];
928414c4531SDave Airlie 	u8 bppshift;
929414c4531SDave Airlie 
930414c4531SDave Airlie 	static unsigned char dacvalue[] = {
931414c4531SDave Airlie 		/* 0x00: */        0,    0,    0,    0,    0,    0, 0x00,    0,
932414c4531SDave Airlie 		/* 0x08: */        0,    0,    0,    0,    0,    0,    0,    0,
933414c4531SDave Airlie 		/* 0x10: */        0,    0,    0,    0,    0,    0,    0,    0,
934414c4531SDave Airlie 		/* 0x18: */     0x00,    0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
935414c4531SDave Airlie 		/* 0x20: */     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
936414c4531SDave Airlie 		/* 0x28: */     0x00, 0x00, 0x00, 0x00,    0,    0,    0, 0x40,
937414c4531SDave Airlie 		/* 0x30: */     0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
938414c4531SDave Airlie 		/* 0x38: */     0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
939414c4531SDave Airlie 		/* 0x40: */        0,    0,    0,    0,    0,    0,    0,    0,
940414c4531SDave Airlie 		/* 0x48: */        0,    0,    0,    0,    0,    0,    0,    0
941414c4531SDave Airlie 	};
942414c4531SDave Airlie 
943272725c7SVille Syrjälä 	bppshift = mdev->bpp_shifts[fb->format->cpp[0] - 1];
944414c4531SDave Airlie 
945414c4531SDave Airlie 	switch (mdev->type) {
946414c4531SDave Airlie 	case G200_SE_A:
947414c4531SDave Airlie 	case G200_SE_B:
948414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x03;
949414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
950414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
951414c4531SDave Airlie 					     MGA1064_MISC_CTL_VGA8 |
952414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
953414c4531SDave Airlie 		if (mdev->has_sdram)
954414c4531SDave Airlie 			option = 0x40049120;
955414c4531SDave Airlie 		else
956414c4531SDave Airlie 			option = 0x4004d120;
957414c4531SDave Airlie 		option2 = 0x00008000;
958414c4531SDave Airlie 		break;
959414c4531SDave Airlie 	case G200_WB:
9606d857c18SMathieu Larouche 	case G200_EW3:
961414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x07;
962414c4531SDave Airlie 		option = 0x41049120;
963414c4531SDave Airlie 		option2 = 0x0000b000;
964414c4531SDave Airlie 		break;
965414c4531SDave Airlie 	case G200_EV:
966414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
967414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
968414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
969414c4531SDave Airlie 		option = 0x00000120;
970414c4531SDave Airlie 		option2 = 0x0000b000;
971414c4531SDave Airlie 		break;
972414c4531SDave Airlie 	case G200_EH:
973*f0493e65SMathieu Larouche 	case G200_EH3:
974414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
975414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
976414c4531SDave Airlie 		option = 0x00000120;
977414c4531SDave Airlie 		option2 = 0x0000b000;
978414c4531SDave Airlie 		break;
979414c4531SDave Airlie 	case G200_ER:
980414c4531SDave Airlie 		break;
981414c4531SDave Airlie 	}
982414c4531SDave Airlie 
983272725c7SVille Syrjälä 	switch (fb->format->cpp[0] * 8) {
984414c4531SDave Airlie 	case 8:
985414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
986414c4531SDave Airlie 		break;
987414c4531SDave Airlie 	case 16:
988b00c600eSVille Syrjälä 		if (fb->format->depth == 15)
989414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
990414c4531SDave Airlie 		else
991414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
992414c4531SDave Airlie 		break;
993414c4531SDave Airlie 	case 24:
994414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
995414c4531SDave Airlie 		break;
996414c4531SDave Airlie 	case 32:
997414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
998414c4531SDave Airlie 		break;
999414c4531SDave Airlie 	}
1000414c4531SDave Airlie 
1001414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1002414c4531SDave Airlie 		misc |= 0x40;
1003414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1004414c4531SDave Airlie 		misc |= 0x80;
1005414c4531SDave Airlie 
1006414c4531SDave Airlie 
1007414c4531SDave Airlie 	for (i = 0; i < sizeof(dacvalue); i++) {
10089d8aa55fSChristopher Harvey 		if ((i <= 0x17) ||
1009414c4531SDave Airlie 		    (i == 0x1b) ||
1010414c4531SDave Airlie 		    (i == 0x1c) ||
1011414c4531SDave Airlie 		    ((i >= 0x1f) && (i <= 0x29)) ||
1012414c4531SDave Airlie 		    ((i >= 0x30) && (i <= 0x37)))
1013414c4531SDave Airlie 			continue;
1014414c4531SDave Airlie 		if (IS_G200_SE(mdev) &&
1015414c4531SDave Airlie 		    ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
1016414c4531SDave Airlie 			continue;
10176d857c18SMathieu Larouche 		if ((mdev->type == G200_EV ||
10186d857c18SMathieu Larouche 		    mdev->type == G200_WB ||
10196d857c18SMathieu Larouche 		    mdev->type == G200_EH ||
1020*f0493e65SMathieu Larouche 		    mdev->type == G200_EW3 ||
1021*f0493e65SMathieu Larouche 		    mdev->type == G200_EH3) &&
1022414c4531SDave Airlie 		    (i >= 0x44) && (i <= 0x4e))
1023414c4531SDave Airlie 			continue;
1024414c4531SDave Airlie 
1025414c4531SDave Airlie 		WREG_DAC(i, dacvalue[i]);
1026414c4531SDave Airlie 	}
1027414c4531SDave Airlie 
10281812a3dbSChristopher Harvey 	if (mdev->type == G200_ER)
10291812a3dbSChristopher Harvey 		WREG_DAC(0x90, 0);
1030414c4531SDave Airlie 
1031414c4531SDave Airlie 	if (option)
1032414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
1033414c4531SDave Airlie 	if (option2)
1034414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
1035414c4531SDave Airlie 
1036414c4531SDave Airlie 	WREG_SEQ(2, 0xf);
1037414c4531SDave Airlie 	WREG_SEQ(3, 0);
1038414c4531SDave Airlie 	WREG_SEQ(4, 0xe);
1039414c4531SDave Airlie 
1040272725c7SVille Syrjälä 	pitch = fb->pitches[0] / fb->format->cpp[0];
1041272725c7SVille Syrjälä 	if (fb->format->cpp[0] * 8 == 24)
1042da558398STakashi Iwai 		pitch = (pitch * 3) >> (4 - bppshift);
1043414c4531SDave Airlie 	else
1044414c4531SDave Airlie 		pitch = pitch >> (4 - bppshift);
1045414c4531SDave Airlie 
1046414c4531SDave Airlie 	hdisplay = mode->hdisplay / 8 - 1;
1047414c4531SDave Airlie 	hsyncstart = mode->hsync_start / 8 - 1;
1048414c4531SDave Airlie 	hsyncend = mode->hsync_end / 8 - 1;
1049414c4531SDave Airlie 	htotal = mode->htotal / 8 - 1;
1050414c4531SDave Airlie 
1051414c4531SDave Airlie 	/* Work around hardware quirk */
1052414c4531SDave Airlie 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
1053414c4531SDave Airlie 		htotal++;
1054414c4531SDave Airlie 
1055414c4531SDave Airlie 	vdisplay = mode->vdisplay - 1;
1056414c4531SDave Airlie 	vsyncstart = mode->vsync_start - 1;
1057414c4531SDave Airlie 	vsyncend = mode->vsync_end - 1;
1058414c4531SDave Airlie 	vtotal = mode->vtotal - 2;
1059414c4531SDave Airlie 
1060414c4531SDave Airlie 	WREG_GFX(0, 0);
1061414c4531SDave Airlie 	WREG_GFX(1, 0);
1062414c4531SDave Airlie 	WREG_GFX(2, 0);
1063414c4531SDave Airlie 	WREG_GFX(3, 0);
1064414c4531SDave Airlie 	WREG_GFX(4, 0);
1065414c4531SDave Airlie 	WREG_GFX(5, 0x40);
1066414c4531SDave Airlie 	WREG_GFX(6, 0x5);
1067414c4531SDave Airlie 	WREG_GFX(7, 0xf);
1068414c4531SDave Airlie 	WREG_GFX(8, 0xf);
1069414c4531SDave Airlie 
1070414c4531SDave Airlie 	WREG_CRT(0, htotal - 4);
1071414c4531SDave Airlie 	WREG_CRT(1, hdisplay);
1072414c4531SDave Airlie 	WREG_CRT(2, hdisplay);
1073414c4531SDave Airlie 	WREG_CRT(3, (htotal & 0x1F) | 0x80);
1074414c4531SDave Airlie 	WREG_CRT(4, hsyncstart);
1075414c4531SDave Airlie 	WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
1076414c4531SDave Airlie 	WREG_CRT(6, vtotal & 0xFF);
1077414c4531SDave Airlie 	WREG_CRT(7, ((vtotal & 0x100) >> 8) |
1078414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 7) |
1079414c4531SDave Airlie 		 ((vsyncstart & 0x100) >> 6) |
1080414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 5) |
1081414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 4) | /* linecomp */
1082414c4531SDave Airlie 		 ((vtotal & 0x200) >> 4)|
1083414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3) |
1084414c4531SDave Airlie 		 ((vsyncstart & 0x200) >> 2));
1085414c4531SDave Airlie 	WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
1086414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3));
1087414c4531SDave Airlie 	WREG_CRT(10, 0);
1088414c4531SDave Airlie 	WREG_CRT(11, 0);
1089414c4531SDave Airlie 	WREG_CRT(12, 0);
1090414c4531SDave Airlie 	WREG_CRT(13, 0);
1091414c4531SDave Airlie 	WREG_CRT(14, 0);
1092414c4531SDave Airlie 	WREG_CRT(15, 0);
1093414c4531SDave Airlie 	WREG_CRT(16, vsyncstart & 0xFF);
1094414c4531SDave Airlie 	WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
1095414c4531SDave Airlie 	WREG_CRT(18, vdisplay & 0xFF);
1096414c4531SDave Airlie 	WREG_CRT(19, pitch & 0xFF);
1097414c4531SDave Airlie 	WREG_CRT(20, 0);
1098414c4531SDave Airlie 	WREG_CRT(21, vdisplay & 0xFF);
1099414c4531SDave Airlie 	WREG_CRT(22, (vtotal + 1) & 0xFF);
1100414c4531SDave Airlie 	WREG_CRT(23, 0xc3);
1101414c4531SDave Airlie 	WREG_CRT(24, vdisplay & 0xFF);
1102414c4531SDave Airlie 
1103414c4531SDave Airlie 	ext_vga[0] = 0;
1104414c4531SDave Airlie 	ext_vga[5] = 0;
1105414c4531SDave Airlie 
1106414c4531SDave Airlie 	/* TODO interlace */
1107414c4531SDave Airlie 
1108414c4531SDave Airlie 	ext_vga[0] |= (pitch & 0x300) >> 4;
1109414c4531SDave Airlie 	ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
1110414c4531SDave Airlie 		((hdisplay & 0x100) >> 7) |
1111414c4531SDave Airlie 		((hsyncstart & 0x100) >> 6) |
1112414c4531SDave Airlie 		(htotal & 0x40);
1113414c4531SDave Airlie 	ext_vga[2] = ((vtotal & 0xc00) >> 10) |
1114414c4531SDave Airlie 		((vdisplay & 0x400) >> 8) |
1115414c4531SDave Airlie 		((vdisplay & 0xc00) >> 7) |
1116414c4531SDave Airlie 		((vsyncstart & 0xc00) >> 5) |
1117414c4531SDave Airlie 		((vdisplay & 0x400) >> 3);
1118272725c7SVille Syrjälä 	if (fb->format->cpp[0] * 8 == 24)
1119414c4531SDave Airlie 		ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
1120414c4531SDave Airlie 	else
1121414c4531SDave Airlie 		ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
1122414c4531SDave Airlie 	ext_vga[4] = 0;
11236d857c18SMathieu Larouche 	if (mdev->type == G200_WB || mdev->type == G200_EW3)
1124414c4531SDave Airlie 		ext_vga[1] |= 0x88;
1125414c4531SDave Airlie 
1126414c4531SDave Airlie 	/* Set pixel clocks */
1127414c4531SDave Airlie 	misc = 0x2d;
1128414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
1129414c4531SDave Airlie 
1130414c4531SDave Airlie 	mga_crtc_set_plls(mdev, mode->clock);
1131414c4531SDave Airlie 
1132414c4531SDave Airlie 	for (i = 0; i < 6; i++) {
1133414c4531SDave Airlie 		WREG_ECRT(i, ext_vga[i]);
1134414c4531SDave Airlie 	}
1135414c4531SDave Airlie 
1136414c4531SDave Airlie 	if (mdev->type == G200_ER)
11371812a3dbSChristopher Harvey 		WREG_ECRT(0x24, 0x5);
1138414c4531SDave Airlie 
11396d857c18SMathieu Larouche 	if (mdev->type == G200_EW3)
11406d857c18SMathieu Larouche 		WREG_ECRT(0x34, 0x5);
11416d857c18SMathieu Larouche 
1142414c4531SDave Airlie 	if (mdev->type == G200_EV) {
1143414c4531SDave Airlie 		WREG_ECRT(6, 0);
1144414c4531SDave Airlie 	}
1145414c4531SDave Airlie 
1146414c4531SDave Airlie 	WREG_ECRT(0, ext_vga[0]);
1147414c4531SDave Airlie 	/* Enable mga pixel clock */
1148414c4531SDave Airlie 	misc = 0x2d;
1149414c4531SDave Airlie 
1150414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
1151414c4531SDave Airlie 
1152414c4531SDave Airlie 	if (adjusted_mode)
1153414c4531SDave Airlie 		memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
1154414c4531SDave Airlie 
1155414c4531SDave Airlie 	mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
1156414c4531SDave Airlie 
1157414c4531SDave Airlie 	/* reset tagfifo */
1158414c4531SDave Airlie 	if (mdev->type == G200_ER) {
1159414c4531SDave Airlie 		u32 mem_ctl = RREG32(MGAREG_MEMCTL);
1160414c4531SDave Airlie 		u8 seq1;
1161414c4531SDave Airlie 
1162414c4531SDave Airlie 		/* screen off */
1163414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x01);
1164414c4531SDave Airlie 		seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
1165414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1);
1166414c4531SDave Airlie 
1167414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
1168414c4531SDave Airlie 		udelay(1000);
1169414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
1170414c4531SDave Airlie 
1171414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
1172414c4531SDave Airlie 	}
1173414c4531SDave Airlie 
1174414c4531SDave Airlie 
1175414c4531SDave Airlie 	if (IS_G200_SE(mdev)) {
1176abbee623SJulia Lemire 		if (mdev->unique_rev_id >= 0x02) {
1177414c4531SDave Airlie 			u8 hi_pri_lvl;
1178414c4531SDave Airlie 			u32 bpp;
1179414c4531SDave Airlie 			u32 mb;
1180414c4531SDave Airlie 
1181272725c7SVille Syrjälä 			if (fb->format->cpp[0] * 8 > 16)
1182414c4531SDave Airlie 				bpp = 32;
1183272725c7SVille Syrjälä 			else if (fb->format->cpp[0] * 8 > 8)
1184414c4531SDave Airlie 				bpp = 16;
1185414c4531SDave Airlie 			else
1186414c4531SDave Airlie 				bpp = 8;
1187414c4531SDave Airlie 
1188414c4531SDave Airlie 			mb = (mode->clock * bpp) / 1000;
1189414c4531SDave Airlie 			if (mb > 3100)
1190414c4531SDave Airlie 				hi_pri_lvl = 0;
1191414c4531SDave Airlie 			else if (mb > 2600)
1192414c4531SDave Airlie 				hi_pri_lvl = 1;
1193414c4531SDave Airlie 			else if (mb > 1900)
1194414c4531SDave Airlie 				hi_pri_lvl = 2;
1195414c4531SDave Airlie 			else if (mb > 1160)
1196414c4531SDave Airlie 				hi_pri_lvl = 3;
1197414c4531SDave Airlie 			else if (mb > 440)
1198414c4531SDave Airlie 				hi_pri_lvl = 4;
1199414c4531SDave Airlie 			else
1200414c4531SDave Airlie 				hi_pri_lvl = 5;
1201414c4531SDave Airlie 
120291f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
120391f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl);
1204414c4531SDave Airlie 		} else {
120591f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1206abbee623SJulia Lemire 			if (mdev->unique_rev_id >= 0x01)
120791f8f105SChristopher Harvey 				WREG8(MGAREG_CRTCEXT_DATA, 0x03);
1208414c4531SDave Airlie 			else
120991f8f105SChristopher Harvey 				WREG8(MGAREG_CRTCEXT_DATA, 0x04);
1210414c4531SDave Airlie 		}
1211414c4531SDave Airlie 	}
1212414c4531SDave Airlie 	return 0;
1213414c4531SDave Airlie }
1214414c4531SDave Airlie 
1215414c4531SDave Airlie #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1216414c4531SDave Airlie static int mga_suspend(struct drm_crtc *crtc)
1217414c4531SDave Airlie {
1218414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1219414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1220414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1221414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1222414c4531SDave Airlie 	int option;
1223414c4531SDave Airlie 
1224414c4531SDave Airlie 	if (mdev->suspended)
1225414c4531SDave Airlie 		return 0;
1226414c4531SDave Airlie 
1227414c4531SDave Airlie 	WREG_SEQ(1, 0x20);
1228414c4531SDave Airlie 	WREG_ECRT(1, 0x30);
1229414c4531SDave Airlie 	/* Disable the pixel clock */
1230414c4531SDave Airlie 	WREG_DAC(0x1a, 0x05);
1231414c4531SDave Airlie 	/* Power down the DAC */
1232414c4531SDave Airlie 	WREG_DAC(0x1e, 0x18);
1233414c4531SDave Airlie 	/* Power down the pixel PLL */
1234414c4531SDave Airlie 	WREG_DAC(0x1a, 0x0d);
1235414c4531SDave Airlie 
1236414c4531SDave Airlie 	/* Disable PLLs and clocks */
1237414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1238414c4531SDave Airlie 	option &= ~(0x1F8024);
1239414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1240414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D3hot);
1241414c4531SDave Airlie 	pci_disable_device(pdev);
1242414c4531SDave Airlie 
1243414c4531SDave Airlie 	mdev->suspended = true;
1244414c4531SDave Airlie 
1245414c4531SDave Airlie 	return 0;
1246414c4531SDave Airlie }
1247414c4531SDave Airlie 
1248414c4531SDave Airlie static int mga_resume(struct drm_crtc *crtc)
1249414c4531SDave Airlie {
1250414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1251414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1252414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1253414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1254414c4531SDave Airlie 	int option;
1255414c4531SDave Airlie 
1256414c4531SDave Airlie 	if (!mdev->suspended)
1257414c4531SDave Airlie 		return 0;
1258414c4531SDave Airlie 
1259414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D0);
1260414c4531SDave Airlie 	pci_enable_device(pdev);
1261414c4531SDave Airlie 
1262414c4531SDave Airlie 	/* Disable sysclk */
1263414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1264414c4531SDave Airlie 	option &= ~(0x4);
1265414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1266414c4531SDave Airlie 
1267414c4531SDave Airlie 	mdev->suspended = false;
1268414c4531SDave Airlie 
1269414c4531SDave Airlie 	return 0;
1270414c4531SDave Airlie }
1271414c4531SDave Airlie 
1272414c4531SDave Airlie #endif
1273414c4531SDave Airlie 
1274414c4531SDave Airlie static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1275414c4531SDave Airlie {
1276414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1277414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1278414c4531SDave Airlie 	u8 seq1 = 0, crtcext1 = 0;
1279414c4531SDave Airlie 
1280414c4531SDave Airlie 	switch (mode) {
1281414c4531SDave Airlie 	case DRM_MODE_DPMS_ON:
1282414c4531SDave Airlie 		seq1 = 0;
1283414c4531SDave Airlie 		crtcext1 = 0;
1284414c4531SDave Airlie 		mga_crtc_load_lut(crtc);
1285414c4531SDave Airlie 		break;
1286414c4531SDave Airlie 	case DRM_MODE_DPMS_STANDBY:
1287414c4531SDave Airlie 		seq1 = 0x20;
1288414c4531SDave Airlie 		crtcext1 = 0x10;
1289414c4531SDave Airlie 		break;
1290414c4531SDave Airlie 	case DRM_MODE_DPMS_SUSPEND:
1291414c4531SDave Airlie 		seq1 = 0x20;
1292414c4531SDave Airlie 		crtcext1 = 0x20;
1293414c4531SDave Airlie 		break;
1294414c4531SDave Airlie 	case DRM_MODE_DPMS_OFF:
1295414c4531SDave Airlie 		seq1 = 0x20;
1296414c4531SDave Airlie 		crtcext1 = 0x30;
1297414c4531SDave Airlie 		break;
1298414c4531SDave Airlie 	}
1299414c4531SDave Airlie 
1300414c4531SDave Airlie #if 0
1301414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_OFF) {
1302414c4531SDave Airlie 		mga_suspend(crtc);
1303414c4531SDave Airlie 	}
1304414c4531SDave Airlie #endif
1305414c4531SDave Airlie 	WREG8(MGAREG_SEQ_INDEX, 0x01);
1306414c4531SDave Airlie 	seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1307414c4531SDave Airlie 	mga_wait_vsync(mdev);
1308414c4531SDave Airlie 	mga_wait_busy(mdev);
1309414c4531SDave Airlie 	WREG8(MGAREG_SEQ_DATA, seq1);
1310414c4531SDave Airlie 	msleep(20);
1311414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1312414c4531SDave Airlie 	crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1313414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1314414c4531SDave Airlie 
1315414c4531SDave Airlie #if 0
1316414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1317414c4531SDave Airlie 		mga_resume(crtc);
1318414c4531SDave Airlie 		drm_helper_resume_force_mode(dev);
1319414c4531SDave Airlie 	}
1320414c4531SDave Airlie #endif
1321414c4531SDave Airlie }
1322414c4531SDave Airlie 
1323414c4531SDave Airlie /*
1324414c4531SDave Airlie  * This is called before a mode is programmed. A typical use might be to
1325414c4531SDave Airlie  * enable DPMS during the programming to avoid seeing intermediate stages,
1326414c4531SDave Airlie  * but that's not relevant to us
1327414c4531SDave Airlie  */
1328414c4531SDave Airlie static void mga_crtc_prepare(struct drm_crtc *crtc)
1329414c4531SDave Airlie {
1330414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1331414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1332414c4531SDave Airlie 	u8 tmp;
1333414c4531SDave Airlie 
1334414c4531SDave Airlie 	/*	mga_resume(crtc);*/
1335414c4531SDave Airlie 
1336414c4531SDave Airlie 	WREG8(MGAREG_CRTC_INDEX, 0x11);
1337414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTC_DATA);
1338414c4531SDave Airlie 	WREG_CRT(0x11, tmp | 0x80);
1339414c4531SDave Airlie 
1340414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1341414c4531SDave Airlie 		WREG_SEQ(0, 1);
1342414c4531SDave Airlie 		msleep(50);
1343414c4531SDave Airlie 		WREG_SEQ(1, 0x20);
1344414c4531SDave Airlie 		msleep(20);
1345414c4531SDave Airlie 	} else {
1346414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1347414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1348414c4531SDave Airlie 
1349414c4531SDave Airlie 		/* start sync reset */
1350414c4531SDave Airlie 		WREG_SEQ(0, 1);
1351414c4531SDave Airlie 		WREG_SEQ(1, tmp | 0x20);
1352414c4531SDave Airlie 	}
1353414c4531SDave Airlie 
13546d857c18SMathieu Larouche 	if (mdev->type == G200_WB || mdev->type == G200_EW3)
1355414c4531SDave Airlie 		mga_g200wb_prepare(crtc);
1356414c4531SDave Airlie 
1357414c4531SDave Airlie 	WREG_CRT(17, 0);
1358414c4531SDave Airlie }
1359414c4531SDave Airlie 
1360414c4531SDave Airlie /*
1361414c4531SDave Airlie  * This is called after a mode is programmed. It should reverse anything done
1362414c4531SDave Airlie  * by the prepare function
1363414c4531SDave Airlie  */
1364414c4531SDave Airlie static void mga_crtc_commit(struct drm_crtc *crtc)
1365414c4531SDave Airlie {
1366414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1367414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1368d584ff82SJani Nikula 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1369414c4531SDave Airlie 	u8 tmp;
1370414c4531SDave Airlie 
13716d857c18SMathieu Larouche 	if (mdev->type == G200_WB || mdev->type == G200_EW3)
1372414c4531SDave Airlie 		mga_g200wb_commit(crtc);
1373414c4531SDave Airlie 
1374414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1375414c4531SDave Airlie 		msleep(50);
1376414c4531SDave Airlie 		WREG_SEQ(1, 0x0);
1377414c4531SDave Airlie 		msleep(20);
1378414c4531SDave Airlie 		WREG_SEQ(0, 0x3);
1379414c4531SDave Airlie 	} else {
1380414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1381414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1382414c4531SDave Airlie 
1383414c4531SDave Airlie 		tmp &= ~0x20;
1384414c4531SDave Airlie 		WREG_SEQ(0x1, tmp);
1385414c4531SDave Airlie 		WREG_SEQ(0, 3);
1386414c4531SDave Airlie 	}
1387414c4531SDave Airlie 	crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1388414c4531SDave Airlie }
1389414c4531SDave Airlie 
1390414c4531SDave Airlie /*
1391414c4531SDave Airlie  * The core can pass us a set of gamma values to program. We actually only
1392414c4531SDave Airlie  * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1393414c4531SDave Airlie  * but it's a requirement that we provide the function
1394414c4531SDave Airlie  */
13957ea77283SMaarten Lankhorst static int mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
13967ea77283SMaarten Lankhorst 			      u16 *blue, uint32_t size)
1397414c4531SDave Airlie {
1398414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1399414c4531SDave Airlie 	int i;
1400414c4531SDave Airlie 
14017ea77283SMaarten Lankhorst 	for (i = 0; i < size; i++) {
1402414c4531SDave Airlie 		mga_crtc->lut_r[i] = red[i] >> 8;
1403414c4531SDave Airlie 		mga_crtc->lut_g[i] = green[i] >> 8;
1404414c4531SDave Airlie 		mga_crtc->lut_b[i] = blue[i] >> 8;
1405414c4531SDave Airlie 	}
1406414c4531SDave Airlie 	mga_crtc_load_lut(crtc);
14077ea77283SMaarten Lankhorst 
14087ea77283SMaarten Lankhorst 	return 0;
1409414c4531SDave Airlie }
1410414c4531SDave Airlie 
1411414c4531SDave Airlie /* Simple cleanup function */
1412414c4531SDave Airlie static void mga_crtc_destroy(struct drm_crtc *crtc)
1413414c4531SDave Airlie {
1414414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1415414c4531SDave Airlie 
1416414c4531SDave Airlie 	drm_crtc_cleanup(crtc);
1417414c4531SDave Airlie 	kfree(mga_crtc);
1418414c4531SDave Airlie }
1419414c4531SDave Airlie 
142064c29076SEgbert Eich static void mga_crtc_disable(struct drm_crtc *crtc)
142164c29076SEgbert Eich {
142264c29076SEgbert Eich 	int ret;
142364c29076SEgbert Eich 	DRM_DEBUG_KMS("\n");
142464c29076SEgbert Eich 	mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1425f4510a27SMatt Roper 	if (crtc->primary->fb) {
1426f4510a27SMatt Roper 		struct mga_framebuffer *mga_fb = to_mga_framebuffer(crtc->primary->fb);
142764c29076SEgbert Eich 		struct drm_gem_object *obj = mga_fb->obj;
142864c29076SEgbert Eich 		struct mgag200_bo *bo = gem_to_mga_bo(obj);
142964c29076SEgbert Eich 		ret = mgag200_bo_reserve(bo, false);
143064c29076SEgbert Eich 		if (ret)
143164c29076SEgbert Eich 			return;
143264c29076SEgbert Eich 		mgag200_bo_push_sysram(bo);
143364c29076SEgbert Eich 		mgag200_bo_unreserve(bo);
143464c29076SEgbert Eich 	}
1435f4510a27SMatt Roper 	crtc->primary->fb = NULL;
143664c29076SEgbert Eich }
143764c29076SEgbert Eich 
1438414c4531SDave Airlie /* These provide the minimum set of functions required to handle a CRTC */
1439414c4531SDave Airlie static const struct drm_crtc_funcs mga_crtc_funcs = {
1440a080db9fSChristopher Harvey 	.cursor_set = mga_crtc_cursor_set,
1441a080db9fSChristopher Harvey 	.cursor_move = mga_crtc_cursor_move,
1442414c4531SDave Airlie 	.gamma_set = mga_crtc_gamma_set,
1443414c4531SDave Airlie 	.set_config = drm_crtc_helper_set_config,
1444414c4531SDave Airlie 	.destroy = mga_crtc_destroy,
1445414c4531SDave Airlie };
1446414c4531SDave Airlie 
1447414c4531SDave Airlie static const struct drm_crtc_helper_funcs mga_helper_funcs = {
144864c29076SEgbert Eich 	.disable = mga_crtc_disable,
1449414c4531SDave Airlie 	.dpms = mga_crtc_dpms,
1450414c4531SDave Airlie 	.mode_set = mga_crtc_mode_set,
1451414c4531SDave Airlie 	.mode_set_base = mga_crtc_mode_set_base,
1452414c4531SDave Airlie 	.prepare = mga_crtc_prepare,
1453414c4531SDave Airlie 	.commit = mga_crtc_commit,
1454414c4531SDave Airlie 	.load_lut = mga_crtc_load_lut,
1455414c4531SDave Airlie };
1456414c4531SDave Airlie 
1457414c4531SDave Airlie /* CRTC setup */
1458f1998fe2SChristopher Harvey static void mga_crtc_init(struct mga_device *mdev)
1459414c4531SDave Airlie {
1460414c4531SDave Airlie 	struct mga_crtc *mga_crtc;
1461414c4531SDave Airlie 	int i;
1462414c4531SDave Airlie 
1463414c4531SDave Airlie 	mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1464414c4531SDave Airlie 			      (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1465414c4531SDave Airlie 			      GFP_KERNEL);
1466414c4531SDave Airlie 
1467414c4531SDave Airlie 	if (mga_crtc == NULL)
1468414c4531SDave Airlie 		return;
1469414c4531SDave Airlie 
1470f1998fe2SChristopher Harvey 	drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
1471414c4531SDave Airlie 
1472414c4531SDave Airlie 	drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1473414c4531SDave Airlie 	mdev->mode_info.crtc = mga_crtc;
1474414c4531SDave Airlie 
1475414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
1476414c4531SDave Airlie 		mga_crtc->lut_r[i] = i;
1477414c4531SDave Airlie 		mga_crtc->lut_g[i] = i;
1478414c4531SDave Airlie 		mga_crtc->lut_b[i] = i;
1479414c4531SDave Airlie 	}
1480414c4531SDave Airlie 
1481414c4531SDave Airlie 	drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1482414c4531SDave Airlie }
1483414c4531SDave Airlie 
1484414c4531SDave Airlie /** Sets the color ramps on behalf of fbcon */
1485414c4531SDave Airlie void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1486414c4531SDave Airlie 			      u16 blue, int regno)
1487414c4531SDave Airlie {
1488414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1489414c4531SDave Airlie 
1490414c4531SDave Airlie 	mga_crtc->lut_r[regno] = red >> 8;
1491414c4531SDave Airlie 	mga_crtc->lut_g[regno] = green >> 8;
1492414c4531SDave Airlie 	mga_crtc->lut_b[regno] = blue >> 8;
1493414c4531SDave Airlie }
1494414c4531SDave Airlie 
1495414c4531SDave Airlie /** Gets the color ramps on behalf of fbcon */
1496414c4531SDave Airlie void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
1497414c4531SDave Airlie 			      u16 *blue, int regno)
1498414c4531SDave Airlie {
1499414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1500414c4531SDave Airlie 
1501414c4531SDave Airlie 	*red = (u16)mga_crtc->lut_r[regno] << 8;
1502414c4531SDave Airlie 	*green = (u16)mga_crtc->lut_g[regno] << 8;
1503414c4531SDave Airlie 	*blue = (u16)mga_crtc->lut_b[regno] << 8;
1504414c4531SDave Airlie }
1505414c4531SDave Airlie 
1506414c4531SDave Airlie /*
1507414c4531SDave Airlie  * The encoder comes after the CRTC in the output pipeline, but before
1508414c4531SDave Airlie  * the connector. It's responsible for ensuring that the digital
1509414c4531SDave Airlie  * stream is appropriately converted into the output format. Setup is
1510414c4531SDave Airlie  * very simple in this case - all we have to do is inform qemu of the
1511414c4531SDave Airlie  * colour depth in order to ensure that it displays appropriately
1512414c4531SDave Airlie  */
1513414c4531SDave Airlie 
1514414c4531SDave Airlie /*
1515414c4531SDave Airlie  * These functions are analagous to those in the CRTC code, but are intended
1516414c4531SDave Airlie  * to handle any encoder-specific limitations
1517414c4531SDave Airlie  */
1518414c4531SDave Airlie static void mga_encoder_mode_set(struct drm_encoder *encoder,
1519414c4531SDave Airlie 				struct drm_display_mode *mode,
1520414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
1521414c4531SDave Airlie {
1522414c4531SDave Airlie 
1523414c4531SDave Airlie }
1524414c4531SDave Airlie 
1525414c4531SDave Airlie static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
1526414c4531SDave Airlie {
1527414c4531SDave Airlie 	return;
1528414c4531SDave Airlie }
1529414c4531SDave Airlie 
1530414c4531SDave Airlie static void mga_encoder_prepare(struct drm_encoder *encoder)
1531414c4531SDave Airlie {
1532414c4531SDave Airlie }
1533414c4531SDave Airlie 
1534414c4531SDave Airlie static void mga_encoder_commit(struct drm_encoder *encoder)
1535414c4531SDave Airlie {
1536414c4531SDave Airlie }
1537414c4531SDave Airlie 
1538080fd6b5SRashika static void mga_encoder_destroy(struct drm_encoder *encoder)
1539414c4531SDave Airlie {
1540414c4531SDave Airlie 	struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
1541414c4531SDave Airlie 	drm_encoder_cleanup(encoder);
1542414c4531SDave Airlie 	kfree(mga_encoder);
1543414c4531SDave Airlie }
1544414c4531SDave Airlie 
1545414c4531SDave Airlie static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
1546414c4531SDave Airlie 	.dpms = mga_encoder_dpms,
1547414c4531SDave Airlie 	.mode_set = mga_encoder_mode_set,
1548414c4531SDave Airlie 	.prepare = mga_encoder_prepare,
1549414c4531SDave Airlie 	.commit = mga_encoder_commit,
1550414c4531SDave Airlie };
1551414c4531SDave Airlie 
1552414c4531SDave Airlie static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
1553414c4531SDave Airlie 	.destroy = mga_encoder_destroy,
1554414c4531SDave Airlie };
1555414c4531SDave Airlie 
1556414c4531SDave Airlie static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
1557414c4531SDave Airlie {
1558414c4531SDave Airlie 	struct drm_encoder *encoder;
1559414c4531SDave Airlie 	struct mga_encoder *mga_encoder;
1560414c4531SDave Airlie 
1561414c4531SDave Airlie 	mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
1562414c4531SDave Airlie 	if (!mga_encoder)
1563414c4531SDave Airlie 		return NULL;
1564414c4531SDave Airlie 
1565414c4531SDave Airlie 	encoder = &mga_encoder->base;
1566414c4531SDave Airlie 	encoder->possible_crtcs = 0x1;
1567414c4531SDave Airlie 
1568414c4531SDave Airlie 	drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
156913a3d91fSVille Syrjälä 			 DRM_MODE_ENCODER_DAC, NULL);
1570414c4531SDave Airlie 	drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
1571414c4531SDave Airlie 
1572414c4531SDave Airlie 	return encoder;
1573414c4531SDave Airlie }
1574414c4531SDave Airlie 
1575414c4531SDave Airlie 
1576414c4531SDave Airlie static int mga_vga_get_modes(struct drm_connector *connector)
1577414c4531SDave Airlie {
1578414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1579414c4531SDave Airlie 	struct edid *edid;
1580414c4531SDave Airlie 	int ret = 0;
1581414c4531SDave Airlie 
1582414c4531SDave Airlie 	edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1583414c4531SDave Airlie 	if (edid) {
1584414c4531SDave Airlie 		drm_mode_connector_update_edid_property(connector, edid);
1585414c4531SDave Airlie 		ret = drm_add_edid_modes(connector, edid);
1586414c4531SDave Airlie 		kfree(edid);
1587414c4531SDave Airlie 	}
1588414c4531SDave Airlie 	return ret;
1589414c4531SDave Airlie }
1590414c4531SDave Airlie 
1591abbee623SJulia Lemire static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
1592abbee623SJulia Lemire 							int bits_per_pixel)
1593abbee623SJulia Lemire {
1594abbee623SJulia Lemire 	uint32_t total_area, divisor;
1595c24ca5beSNicolas Pitre 	uint64_t active_area, pixels_per_second, bandwidth;
1596abbee623SJulia Lemire 	uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
1597abbee623SJulia Lemire 
1598abbee623SJulia Lemire 	divisor = 1024;
1599abbee623SJulia Lemire 
1600abbee623SJulia Lemire 	if (!mode->htotal || !mode->vtotal || !mode->clock)
1601abbee623SJulia Lemire 		return 0;
1602abbee623SJulia Lemire 
1603abbee623SJulia Lemire 	active_area = mode->hdisplay * mode->vdisplay;
1604abbee623SJulia Lemire 	total_area = mode->htotal * mode->vtotal;
1605abbee623SJulia Lemire 
1606abbee623SJulia Lemire 	pixels_per_second = active_area * mode->clock * 1000;
1607abbee623SJulia Lemire 	do_div(pixels_per_second, total_area);
1608abbee623SJulia Lemire 
1609abbee623SJulia Lemire 	bandwidth = pixels_per_second * bytes_per_pixel * 100;
1610abbee623SJulia Lemire 	do_div(bandwidth, divisor);
1611abbee623SJulia Lemire 
1612abbee623SJulia Lemire 	return (uint32_t)(bandwidth);
1613abbee623SJulia Lemire }
1614abbee623SJulia Lemire 
1615abbee623SJulia Lemire #define MODE_BANDWIDTH	MODE_BAD
1616abbee623SJulia Lemire 
1617414c4531SDave Airlie static int mga_vga_mode_valid(struct drm_connector *connector,
1618414c4531SDave Airlie 				 struct drm_display_mode *mode)
1619414c4531SDave Airlie {
16200ba53171SChristopher Harvey 	struct drm_device *dev = connector->dev;
16210ba53171SChristopher Harvey 	struct mga_device *mdev = (struct mga_device*)dev->dev_private;
16220ba53171SChristopher Harvey 	int bpp = 32;
16230ba53171SChristopher Harvey 
1624abbee623SJulia Lemire 	if (IS_G200_SE(mdev)) {
1625abbee623SJulia Lemire 		if (mdev->unique_rev_id == 0x01) {
1626abbee623SJulia Lemire 			if (mode->hdisplay > 1600)
1627abbee623SJulia Lemire 				return MODE_VIRTUAL_X;
1628abbee623SJulia Lemire 			if (mode->vdisplay > 1200)
1629abbee623SJulia Lemire 				return MODE_VIRTUAL_Y;
1630abbee623SJulia Lemire 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1631abbee623SJulia Lemire 				> (24400 * 1024))
1632abbee623SJulia Lemire 				return MODE_BANDWIDTH;
1633e829d7efSMathieu Larouche 		} else if (mdev->unique_rev_id == 0x02) {
1634abbee623SJulia Lemire 			if (mode->hdisplay > 1920)
1635abbee623SJulia Lemire 				return MODE_VIRTUAL_X;
1636abbee623SJulia Lemire 			if (mode->vdisplay > 1200)
1637abbee623SJulia Lemire 				return MODE_VIRTUAL_Y;
1638abbee623SJulia Lemire 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1639abbee623SJulia Lemire 				> (30100 * 1024))
1640abbee623SJulia Lemire 				return MODE_BANDWIDTH;
1641abbee623SJulia Lemire 		}
1642abbee623SJulia Lemire 	} else if (mdev->type == G200_WB) {
1643abbee623SJulia Lemire 		if (mode->hdisplay > 1280)
1644abbee623SJulia Lemire 			return MODE_VIRTUAL_X;
1645abbee623SJulia Lemire 		if (mode->vdisplay > 1024)
1646abbee623SJulia Lemire 			return MODE_VIRTUAL_Y;
1647abbee623SJulia Lemire 		if (mga_vga_calculate_mode_bandwidth(mode,
1648abbee623SJulia Lemire 			bpp > (31877 * 1024)))
1649abbee623SJulia Lemire 			return MODE_BANDWIDTH;
1650abbee623SJulia Lemire 	} else if (mdev->type == G200_EV &&
1651abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode, bpp)
1652abbee623SJulia Lemire 			> (32700 * 1024))) {
1653abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1654ec22b4aaSDave Airlie 	} else if (mdev->type == G200_EH &&
1655abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode, bpp)
1656abbee623SJulia Lemire 			> (37500 * 1024))) {
1657abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1658ec22b4aaSDave Airlie 	} else if (mdev->type == G200_ER &&
1659abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode,
1660abbee623SJulia Lemire 			bpp) > (55000 * 1024))) {
1661abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1662abbee623SJulia Lemire 	}
1663414c4531SDave Airlie 
166425161084SAdam Jackson 	if ((mode->hdisplay % 8) != 0 || (mode->hsync_start % 8) != 0 ||
166525161084SAdam Jackson 	    (mode->hsync_end % 8) != 0 || (mode->htotal % 8) != 0) {
166625161084SAdam Jackson 		return MODE_H_ILLEGAL;
166725161084SAdam Jackson 	}
166825161084SAdam Jackson 
1669414c4531SDave Airlie 	if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1670414c4531SDave Airlie 	    mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1671414c4531SDave Airlie 	    mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1672414c4531SDave Airlie 	    mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1673414c4531SDave Airlie 		return MODE_BAD;
1674414c4531SDave Airlie 	}
1675414c4531SDave Airlie 
16760ba53171SChristopher Harvey 	/* Validate the mode input by the user */
1677eaf99c74SChris Wilson 	if (connector->cmdline_mode.specified) {
1678eaf99c74SChris Wilson 		if (connector->cmdline_mode.bpp_specified)
1679eaf99c74SChris Wilson 			bpp = connector->cmdline_mode.bpp;
16800ba53171SChristopher Harvey 	}
16810ba53171SChristopher Harvey 
16820ba53171SChristopher Harvey 	if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
1683eaf99c74SChris Wilson 		if (connector->cmdline_mode.specified)
1684eaf99c74SChris Wilson 			connector->cmdline_mode.specified = false;
16850ba53171SChristopher Harvey 		return MODE_BAD;
16860ba53171SChristopher Harvey 	}
16870ba53171SChristopher Harvey 
1688414c4531SDave Airlie 	return MODE_OK;
1689414c4531SDave Airlie }
1690414c4531SDave Airlie 
1691080fd6b5SRashika static struct drm_encoder *mga_connector_best_encoder(struct drm_connector
1692414c4531SDave Airlie 						  *connector)
1693414c4531SDave Airlie {
1694414c4531SDave Airlie 	int enc_id = connector->encoder_ids[0];
1695414c4531SDave Airlie 	/* pick the encoder ids */
1696c7e95114SRob Clark 	if (enc_id)
1697c7e95114SRob Clark 		return drm_encoder_find(connector->dev, enc_id);
1698414c4531SDave Airlie 	return NULL;
1699414c4531SDave Airlie }
1700414c4531SDave Airlie 
1701414c4531SDave Airlie static void mga_connector_destroy(struct drm_connector *connector)
1702414c4531SDave Airlie {
1703414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1704414c4531SDave Airlie 	mgag200_i2c_destroy(mga_connector->i2c);
1705414c4531SDave Airlie 	drm_connector_cleanup(connector);
1706414c4531SDave Airlie 	kfree(connector);
1707414c4531SDave Airlie }
1708414c4531SDave Airlie 
170971cb7495SVille Syrjälä static const struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
1710414c4531SDave Airlie 	.get_modes = mga_vga_get_modes,
1711414c4531SDave Airlie 	.mode_valid = mga_vga_mode_valid,
1712414c4531SDave Airlie 	.best_encoder = mga_connector_best_encoder,
1713414c4531SDave Airlie };
1714414c4531SDave Airlie 
171571cb7495SVille Syrjälä static const struct drm_connector_funcs mga_vga_connector_funcs = {
1716414c4531SDave Airlie 	.dpms = drm_helper_connector_dpms,
1717414c4531SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
1718414c4531SDave Airlie 	.destroy = mga_connector_destroy,
1719414c4531SDave Airlie };
1720414c4531SDave Airlie 
1721414c4531SDave Airlie static struct drm_connector *mga_vga_init(struct drm_device *dev)
1722414c4531SDave Airlie {
1723414c4531SDave Airlie 	struct drm_connector *connector;
1724414c4531SDave Airlie 	struct mga_connector *mga_connector;
1725414c4531SDave Airlie 
1726414c4531SDave Airlie 	mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1727414c4531SDave Airlie 	if (!mga_connector)
1728414c4531SDave Airlie 		return NULL;
1729414c4531SDave Airlie 
1730414c4531SDave Airlie 	connector = &mga_connector->base;
1731414c4531SDave Airlie 
1732414c4531SDave Airlie 	drm_connector_init(dev, connector,
1733414c4531SDave Airlie 			   &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1734414c4531SDave Airlie 
1735414c4531SDave Airlie 	drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1736414c4531SDave Airlie 
173734ea3d38SThomas Wood 	drm_connector_register(connector);
17383d5a1c5eSEgbert Eich 
1739414c4531SDave Airlie 	mga_connector->i2c = mgag200_i2c_create(dev);
1740414c4531SDave Airlie 	if (!mga_connector->i2c)
1741414c4531SDave Airlie 		DRM_ERROR("failed to add ddc bus\n");
1742414c4531SDave Airlie 
1743414c4531SDave Airlie 	return connector;
1744414c4531SDave Airlie }
1745414c4531SDave Airlie 
1746414c4531SDave Airlie 
1747414c4531SDave Airlie int mgag200_modeset_init(struct mga_device *mdev)
1748414c4531SDave Airlie {
1749414c4531SDave Airlie 	struct drm_encoder *encoder;
1750414c4531SDave Airlie 	struct drm_connector *connector;
1751414c4531SDave Airlie 	int ret;
1752414c4531SDave Airlie 
1753414c4531SDave Airlie 	mdev->mode_info.mode_config_initialized = true;
1754414c4531SDave Airlie 
1755414c4531SDave Airlie 	mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1756414c4531SDave Airlie 	mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1757414c4531SDave Airlie 
1758414c4531SDave Airlie 	mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1759414c4531SDave Airlie 
1760f1998fe2SChristopher Harvey 	mga_crtc_init(mdev);
1761414c4531SDave Airlie 
1762414c4531SDave Airlie 	encoder = mga_encoder_init(mdev->dev);
1763414c4531SDave Airlie 	if (!encoder) {
1764414c4531SDave Airlie 		DRM_ERROR("mga_encoder_init failed\n");
1765414c4531SDave Airlie 		return -1;
1766414c4531SDave Airlie 	}
1767414c4531SDave Airlie 
1768414c4531SDave Airlie 	connector = mga_vga_init(mdev->dev);
1769414c4531SDave Airlie 	if (!connector) {
1770414c4531SDave Airlie 		DRM_ERROR("mga_vga_init failed\n");
1771414c4531SDave Airlie 		return -1;
1772414c4531SDave Airlie 	}
1773414c4531SDave Airlie 
1774414c4531SDave Airlie 	drm_mode_connector_attach_encoder(connector, encoder);
1775414c4531SDave Airlie 
1776414c4531SDave Airlie 	ret = mgag200_fbdev_init(mdev);
1777414c4531SDave Airlie 	if (ret) {
1778414c4531SDave Airlie 		DRM_ERROR("mga_fbdev_init failed\n");
1779414c4531SDave Airlie 		return ret;
1780414c4531SDave Airlie 	}
1781414c4531SDave Airlie 
1782414c4531SDave Airlie 	return 0;
1783414c4531SDave Airlie }
1784414c4531SDave Airlie 
1785414c4531SDave Airlie void mgag200_modeset_fini(struct mga_device *mdev)
1786414c4531SDave Airlie {
1787414c4531SDave Airlie 
1788414c4531SDave Airlie }
1789