xref: /linux/drivers/gpu/drm/mgag200/mgag200_mode.c (revision e811f5ae19043b2ac2c28e147a4274038e655598)
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 
16414c4531SDave Airlie #include "drmP.h"
17414c4531SDave Airlie #include "drm.h"
18414c4531SDave Airlie #include "drm_crtc_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;
33414c4531SDave Airlie 	int i;
34414c4531SDave Airlie 
35414c4531SDave Airlie 	if (!crtc->enabled)
36414c4531SDave Airlie 		return;
37414c4531SDave Airlie 
38414c4531SDave Airlie 	WREG8(DAC_INDEX + MGA1064_INDEX, 0);
39414c4531SDave Airlie 
40414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
41414c4531SDave Airlie 		/* VGA registers */
42414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_r[i]);
43414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
44414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_b[i]);
45414c4531SDave Airlie 	}
46414c4531SDave Airlie }
47414c4531SDave Airlie 
48414c4531SDave Airlie static inline void mga_wait_vsync(struct mga_device *mdev)
49414c4531SDave Airlie {
50414c4531SDave Airlie 	unsigned int count = 0;
51414c4531SDave Airlie 	unsigned int status = 0;
52414c4531SDave Airlie 
53414c4531SDave Airlie 	do {
54414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
55414c4531SDave Airlie 		count++;
56414c4531SDave Airlie 	} while ((status & 0x08) && (count < 250000));
57414c4531SDave Airlie 	count = 0;
58414c4531SDave Airlie 	status = 0;
59414c4531SDave Airlie 	do {
60414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
61414c4531SDave Airlie 		count++;
62414c4531SDave Airlie 	} while (!(status & 0x08) && (count < 250000));
63414c4531SDave Airlie }
64414c4531SDave Airlie 
65414c4531SDave Airlie static inline void mga_wait_busy(struct mga_device *mdev)
66414c4531SDave Airlie {
67414c4531SDave Airlie 	unsigned int count = 0;
68414c4531SDave Airlie 	unsigned int status = 0;
69414c4531SDave Airlie 	do {
70414c4531SDave Airlie 		status = RREG8(MGAREG_Status + 2);
71414c4531SDave Airlie 		count++;
72414c4531SDave Airlie 	} while ((status & 0x01) && (count < 500000));
73414c4531SDave Airlie }
74414c4531SDave Airlie 
75414c4531SDave Airlie /*
76414c4531SDave Airlie  * The core passes the desired mode to the CRTC code to see whether any
77414c4531SDave Airlie  * CRTC-specific modifications need to be made to it. We're in a position
78414c4531SDave Airlie  * to just pass that straight through, so this does nothing
79414c4531SDave Airlie  */
80414c4531SDave Airlie static bool mga_crtc_mode_fixup(struct drm_crtc *crtc,
81*e811f5aeSLaurent Pinchart 				const struct drm_display_mode *mode,
82414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
83414c4531SDave Airlie {
84414c4531SDave Airlie 	return true;
85414c4531SDave Airlie }
86414c4531SDave Airlie 
87414c4531SDave Airlie static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
88414c4531SDave Airlie {
89414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
90414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
91414c4531SDave Airlie 	unsigned int testp, testm, testn;
92414c4531SDave Airlie 	unsigned int p, m, n;
93414c4531SDave Airlie 	unsigned int computed;
94414c4531SDave Airlie 
95414c4531SDave Airlie 	m = n = p = 0;
96414c4531SDave Airlie 	vcomax = 320000;
97414c4531SDave Airlie 	vcomin = 160000;
98414c4531SDave Airlie 	pllreffreq = 25000;
99414c4531SDave Airlie 
100414c4531SDave Airlie 	delta = 0xffffffff;
101414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
102414c4531SDave Airlie 
103414c4531SDave Airlie 	for (testp = 8; testp > 0; testp /= 2) {
104414c4531SDave Airlie 		if (clock * testp > vcomax)
105414c4531SDave Airlie 			continue;
106414c4531SDave Airlie 		if (clock * testp < vcomin)
107414c4531SDave Airlie 			continue;
108414c4531SDave Airlie 
109414c4531SDave Airlie 		for (testn = 17; testn < 256; testn++) {
110414c4531SDave Airlie 			for (testm = 1; testm < 32; testm++) {
111414c4531SDave Airlie 				computed = (pllreffreq * testn) /
112414c4531SDave Airlie 					(testm * testp);
113414c4531SDave Airlie 				if (computed > clock)
114414c4531SDave Airlie 					tmpdelta = computed - clock;
115414c4531SDave Airlie 				else
116414c4531SDave Airlie 					tmpdelta = clock - computed;
117414c4531SDave Airlie 				if (tmpdelta < delta) {
118414c4531SDave Airlie 					delta = tmpdelta;
119414c4531SDave Airlie 					m = testm - 1;
120414c4531SDave Airlie 					n = testn - 1;
121414c4531SDave Airlie 					p = testp - 1;
122414c4531SDave Airlie 				}
123414c4531SDave Airlie 			}
124414c4531SDave Airlie 		}
125414c4531SDave Airlie 	}
126414c4531SDave Airlie 
127414c4531SDave Airlie 	if (delta > permitteddelta) {
128414c4531SDave Airlie 		printk(KERN_WARNING "PLL delta too large\n");
129414c4531SDave Airlie 		return 1;
130414c4531SDave Airlie 	}
131414c4531SDave Airlie 
132414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_M, m);
133414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_N, n);
134414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_P, p);
135414c4531SDave Airlie 	return 0;
136414c4531SDave Airlie }
137414c4531SDave Airlie 
138414c4531SDave Airlie static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
139414c4531SDave Airlie {
140414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
141414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
142414c4531SDave Airlie 	unsigned int testp, testm, testn;
143414c4531SDave Airlie 	unsigned int p, m, n;
144414c4531SDave Airlie 	unsigned int computed;
145414c4531SDave Airlie 	int i, j, tmpcount, vcount;
146414c4531SDave Airlie 	bool pll_locked = false;
147414c4531SDave Airlie 	u8 tmp;
148414c4531SDave Airlie 
149414c4531SDave Airlie 	m = n = p = 0;
150414c4531SDave Airlie 	vcomax = 550000;
151414c4531SDave Airlie 	vcomin = 150000;
152414c4531SDave Airlie 	pllreffreq = 48000;
153414c4531SDave Airlie 
154414c4531SDave Airlie 	delta = 0xffffffff;
155414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
156414c4531SDave Airlie 
157414c4531SDave Airlie 	for (testp = 1; testp < 9; testp++) {
158414c4531SDave Airlie 		if (clock * testp > vcomax)
159414c4531SDave Airlie 			continue;
160414c4531SDave Airlie 		if (clock * testp < vcomin)
161414c4531SDave Airlie 			continue;
162414c4531SDave Airlie 
163414c4531SDave Airlie 		for (testm = 1; testm < 17; testm++) {
164414c4531SDave Airlie 			for (testn = 1; testn < 151; testn++) {
165414c4531SDave Airlie 				computed = (pllreffreq * testn) /
166414c4531SDave Airlie 					(testm * testp);
167414c4531SDave Airlie 				if (computed > clock)
168414c4531SDave Airlie 					tmpdelta = computed - clock;
169414c4531SDave Airlie 				else
170414c4531SDave Airlie 					tmpdelta = clock - computed;
171414c4531SDave Airlie 				if (tmpdelta < delta) {
172414c4531SDave Airlie 					delta = tmpdelta;
173414c4531SDave Airlie 					n = testn - 1;
174414c4531SDave Airlie 					m = (testm - 1) | ((n >> 1) & 0x80);
175414c4531SDave Airlie 					p = testp - 1;
176414c4531SDave Airlie 				}
177414c4531SDave Airlie 			}
178414c4531SDave Airlie 		}
179414c4531SDave Airlie 	}
180414c4531SDave Airlie 
181414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
182414c4531SDave Airlie 		if (i > 0) {
183414c4531SDave Airlie 			WREG8(MGAREG_CRTC_INDEX, 0x1e);
184414c4531SDave Airlie 			tmp = RREG8(MGAREG_CRTC_DATA);
185414c4531SDave Airlie 			if (tmp < 0xff)
186414c4531SDave Airlie 				WREG8(MGAREG_CRTC_DATA, tmp+1);
187414c4531SDave Airlie 		}
188414c4531SDave Airlie 
189414c4531SDave Airlie 		/* set pixclkdis to 1 */
190414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
191414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
192414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
193414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
194414c4531SDave Airlie 
195414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
196414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
197414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKDIS;
198414c4531SDave Airlie 		WREG_DAC(MGA1064_REMHEADCTL, tmp);
199414c4531SDave Airlie 
200414c4531SDave Airlie 		/* select PLL Set C */
201414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
202414c4531SDave Airlie 		tmp |= 0x3 << 2;
203414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
204414c4531SDave Airlie 
205414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
206414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
207414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
208414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
209414c4531SDave Airlie 
210414c4531SDave Airlie 		udelay(500);
211414c4531SDave Airlie 
212414c4531SDave Airlie 		/* reset the PLL */
213414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
214414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
215414c4531SDave Airlie 		tmp &= ~0x04;
216414c4531SDave Airlie 		WREG_DAC(MGA1064_VREF_CTL, tmp);
217414c4531SDave Airlie 
218414c4531SDave Airlie 		udelay(50);
219414c4531SDave Airlie 
220414c4531SDave Airlie 		/* program pixel pll register */
221414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
222414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
223414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
224414c4531SDave Airlie 
225414c4531SDave Airlie 		udelay(50);
226414c4531SDave Airlie 
227414c4531SDave Airlie 		/* turn pll on */
228414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
229414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
230414c4531SDave Airlie 		tmp |= 0x04;
231414c4531SDave Airlie 		WREG_DAC(MGA1064_VREF_CTL, tmp);
232414c4531SDave Airlie 
233414c4531SDave Airlie 		udelay(500);
234414c4531SDave Airlie 
235414c4531SDave Airlie 		/* select the pixel pll */
236414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
237414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
238414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
239414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
240414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
241414c4531SDave Airlie 
242414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
243414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
244414c4531SDave Airlie 		tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
245414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
246414c4531SDave Airlie 		WREG_DAC(MGA1064_REMHEADCTL, tmp);
247414c4531SDave Airlie 
248414c4531SDave Airlie 		/* reset dotclock rate bit */
249414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 1);
250414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
251414c4531SDave Airlie 		tmp &= ~0x8;
252414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, tmp);
253414c4531SDave Airlie 
254414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
255414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
256414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
257414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
258414c4531SDave Airlie 
259414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
260414c4531SDave Airlie 
261414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
262414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
263414c4531SDave Airlie 			if (tmpcount < vcount)
264414c4531SDave Airlie 				vcount = 0;
265414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
266414c4531SDave Airlie 				pll_locked = true;
267414c4531SDave Airlie 			else
268414c4531SDave Airlie 				udelay(5);
269414c4531SDave Airlie 		}
270414c4531SDave Airlie 	}
271414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
272414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
273414c4531SDave Airlie 	tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
274414c4531SDave Airlie 	WREG_DAC(MGA1064_REMHEADCTL, tmp);
275414c4531SDave Airlie 	return 0;
276414c4531SDave Airlie }
277414c4531SDave Airlie 
278414c4531SDave Airlie static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
279414c4531SDave Airlie {
280414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
281414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
282414c4531SDave Airlie 	unsigned int testp, testm, testn;
283414c4531SDave Airlie 	unsigned int p, m, n;
284414c4531SDave Airlie 	unsigned int computed;
285414c4531SDave Airlie 	u8 tmp;
286414c4531SDave Airlie 
287414c4531SDave Airlie 	m = n = p = 0;
288414c4531SDave Airlie 	vcomax = 550000;
289414c4531SDave Airlie 	vcomin = 150000;
290414c4531SDave Airlie 	pllreffreq = 50000;
291414c4531SDave Airlie 
292414c4531SDave Airlie 	delta = 0xffffffff;
293414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
294414c4531SDave Airlie 
295414c4531SDave Airlie 	for (testp = 16; testp > 0; testp--) {
296414c4531SDave Airlie 		if (clock * testp > vcomax)
297414c4531SDave Airlie 			continue;
298414c4531SDave Airlie 		if (clock * testp < vcomin)
299414c4531SDave Airlie 			continue;
300414c4531SDave Airlie 
301414c4531SDave Airlie 		for (testn = 1; testn < 257; testn++) {
302414c4531SDave Airlie 			for (testm = 1; testm < 17; testm++) {
303414c4531SDave Airlie 				computed = (pllreffreq * testn) /
304414c4531SDave Airlie 					(testm * testp);
305414c4531SDave Airlie 				if (computed > clock)
306414c4531SDave Airlie 					tmpdelta = computed - clock;
307414c4531SDave Airlie 				else
308414c4531SDave Airlie 					tmpdelta = clock - computed;
309414c4531SDave Airlie 				if (tmpdelta < delta) {
310414c4531SDave Airlie 					delta = tmpdelta;
311414c4531SDave Airlie 					n = testn - 1;
312414c4531SDave Airlie 					m = testm - 1;
313414c4531SDave Airlie 					p = testp - 1;
314414c4531SDave Airlie 				}
315414c4531SDave Airlie 			}
316414c4531SDave Airlie 		}
317414c4531SDave Airlie 	}
318414c4531SDave Airlie 
319414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
320414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
321414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
322414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
323414c4531SDave Airlie 
324414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
325414c4531SDave Airlie 	tmp |= 0x3 << 2;
326414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
327414c4531SDave Airlie 
328414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
329414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
330414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLL_STAT, tmp & ~0x40);
331414c4531SDave Airlie 
332414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
333414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
334414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
335414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
336414c4531SDave Airlie 
337414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
338414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
339414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
340414c4531SDave Airlie 
341414c4531SDave Airlie 	udelay(50);
342414c4531SDave Airlie 
343414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
344414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
345414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
346414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
347414c4531SDave Airlie 
348414c4531SDave Airlie 	udelay(500);
349414c4531SDave Airlie 
350414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
351414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
352414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
353414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
354414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
355414c4531SDave Airlie 
356414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
357414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
358414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLL_STAT, tmp | 0x40);
359414c4531SDave Airlie 
360414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
361414c4531SDave Airlie 	tmp |= (0x3 << 2);
362414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
363414c4531SDave Airlie 
364414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
365414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
366414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
367414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
368414c4531SDave Airlie 
369414c4531SDave Airlie 	return 0;
370414c4531SDave Airlie }
371414c4531SDave Airlie 
372414c4531SDave Airlie static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
373414c4531SDave Airlie {
374414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
375414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
376414c4531SDave Airlie 	unsigned int testp, testm, testn;
377414c4531SDave Airlie 	unsigned int p, m, n;
378414c4531SDave Airlie 	unsigned int computed;
379414c4531SDave Airlie 	int i, j, tmpcount, vcount;
380414c4531SDave Airlie 	u8 tmp;
381414c4531SDave Airlie 	bool pll_locked = false;
382414c4531SDave Airlie 
383414c4531SDave Airlie 	m = n = p = 0;
384414c4531SDave Airlie 	vcomax = 800000;
385414c4531SDave Airlie 	vcomin = 400000;
386414c4531SDave Airlie 	pllreffreq = 3333;
387414c4531SDave Airlie 
388414c4531SDave Airlie 	delta = 0xffffffff;
389414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
390414c4531SDave Airlie 
391414c4531SDave Airlie 	for (testp = 16; testp > 0; testp--) {
392414c4531SDave Airlie 		if (clock * testp > vcomax)
393414c4531SDave Airlie 			continue;
394414c4531SDave Airlie 		if (clock * testp < vcomin)
395414c4531SDave Airlie 			continue;
396414c4531SDave Airlie 
397414c4531SDave Airlie 		for (testm = 1; testm < 33; testm++) {
398414c4531SDave Airlie 			for (testn = 1; testn < 257; testn++) {
399414c4531SDave Airlie 				computed = (pllreffreq * testn) /
400414c4531SDave Airlie 					(testm * testp);
401414c4531SDave Airlie 				if (computed > clock)
402414c4531SDave Airlie 					tmpdelta = computed - clock;
403414c4531SDave Airlie 				else
404414c4531SDave Airlie 					tmpdelta = clock - computed;
405414c4531SDave Airlie 				if (tmpdelta < delta) {
406414c4531SDave Airlie 					delta = tmpdelta;
407414c4531SDave Airlie 					n = testn - 1;
408414c4531SDave Airlie 					m = (testm - 1) | ((n >> 1) & 0x80);
409414c4531SDave Airlie 					p = testp - 1;
410414c4531SDave Airlie 				}
411414c4531SDave Airlie 				if ((clock * testp) >= 600000)
412414c4531SDave Airlie 					p |= 80;
413414c4531SDave Airlie 			}
414414c4531SDave Airlie 		}
415414c4531SDave Airlie 	}
416414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
417414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
418414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
419414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
420414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
421414c4531SDave Airlie 
422414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
423414c4531SDave Airlie 		tmp |= 0x3 << 2;
424414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
425414c4531SDave Airlie 
426414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
427414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
428414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
429414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
430414c4531SDave Airlie 
431414c4531SDave Airlie 		udelay(500);
432414c4531SDave Airlie 
433414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
434414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
435414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
436414c4531SDave Airlie 
437414c4531SDave Airlie 		udelay(500);
438414c4531SDave Airlie 
439414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
440414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
441414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
442414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
443414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
444414c4531SDave Airlie 
445414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
446414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
447414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
448414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
449414c4531SDave Airlie 		WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
450414c4531SDave Airlie 
451414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
452414c4531SDave Airlie 
453414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
454414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
455414c4531SDave Airlie 			if (tmpcount < vcount)
456414c4531SDave Airlie 				vcount = 0;
457414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
458414c4531SDave Airlie 				pll_locked = true;
459414c4531SDave Airlie 			else
460414c4531SDave Airlie 				udelay(5);
461414c4531SDave Airlie 		}
462414c4531SDave Airlie 	}
463414c4531SDave Airlie 
464414c4531SDave Airlie 	return 0;
465414c4531SDave Airlie }
466414c4531SDave Airlie 
467414c4531SDave Airlie static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
468414c4531SDave Airlie {
469414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
470414c4531SDave Airlie 	unsigned int delta, tmpdelta;
471414c4531SDave Airlie 	unsigned int testr, testn, testm, testo;
472414c4531SDave Airlie 	unsigned int p, m, n;
473414c4531SDave Airlie 	unsigned int computed;
474414c4531SDave Airlie 	int tmp;
475414c4531SDave Airlie 
476414c4531SDave Airlie 	m = n = p = 0;
477414c4531SDave Airlie 	vcomax = 1488000;
478414c4531SDave Airlie 	vcomin = 1056000;
479414c4531SDave Airlie 	pllreffreq = 48000;
480414c4531SDave Airlie 
481414c4531SDave Airlie 	delta = 0xffffffff;
482414c4531SDave Airlie 
483414c4531SDave Airlie 	for (testr = 0; testr < 4; testr++) {
484414c4531SDave Airlie 		if (delta == 0)
485414c4531SDave Airlie 			break;
486414c4531SDave Airlie 		for (testn = 5; testn < 129; testn++) {
487414c4531SDave Airlie 			if (delta == 0)
488414c4531SDave Airlie 				break;
489414c4531SDave Airlie 			for (testm = 3; testm >= 0; testm--) {
490414c4531SDave Airlie 				if (delta == 0)
491414c4531SDave Airlie 					break;
492414c4531SDave Airlie 				for (testo = 5; testo < 33; testo++) {
493414c4531SDave Airlie 					computed = pllreffreq * (testn + 1) /
494414c4531SDave Airlie 						(testr + 1);
495414c4531SDave Airlie 					if (computed < vcomin)
496414c4531SDave Airlie 						continue;
497414c4531SDave Airlie 					if (computed > vcomax)
498414c4531SDave Airlie 						continue;
499414c4531SDave Airlie 					if (computed > clock)
500414c4531SDave Airlie 						tmpdelta = computed - clock;
501414c4531SDave Airlie 					else
502414c4531SDave Airlie 						tmpdelta = clock - computed;
503414c4531SDave Airlie 					if (tmpdelta < delta) {
504414c4531SDave Airlie 						delta = tmpdelta;
505414c4531SDave Airlie 						m = testm | (testo << 3);
506414c4531SDave Airlie 						n = testn;
507414c4531SDave Airlie 						p = testr | (testr << 3);
508414c4531SDave Airlie 					}
509414c4531SDave Airlie 				}
510414c4531SDave Airlie 			}
511414c4531SDave Airlie 		}
512414c4531SDave Airlie 	}
513414c4531SDave Airlie 
514414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
515414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
516414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
517414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
518414c4531SDave Airlie 
519414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
520414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
521414c4531SDave Airlie 	tmp |= MGA1064_REMHEADCTL_CLKDIS;
522414c4531SDave Airlie 	WREG_DAC(MGA1064_REMHEADCTL, tmp);
523414c4531SDave Airlie 
524414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
525414c4531SDave Airlie 	tmp |= (0x3<<2) | 0xc0;
526414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
527414c4531SDave Airlie 
528414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
529414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
530414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
531414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
532414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
533414c4531SDave Airlie 
534414c4531SDave Airlie 	udelay(500);
535414c4531SDave Airlie 
536414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
537414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
538414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
539414c4531SDave Airlie 
540414c4531SDave Airlie 	udelay(50);
541414c4531SDave Airlie 
542414c4531SDave Airlie 	return 0;
543414c4531SDave Airlie }
544414c4531SDave Airlie 
545414c4531SDave Airlie static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
546414c4531SDave Airlie {
547414c4531SDave Airlie 	switch(mdev->type) {
548414c4531SDave Airlie 	case G200_SE_A:
549414c4531SDave Airlie 	case G200_SE_B:
550414c4531SDave Airlie 		return mga_g200se_set_plls(mdev, clock);
551414c4531SDave Airlie 		break;
552414c4531SDave Airlie 	case G200_WB:
553414c4531SDave Airlie 		return mga_g200wb_set_plls(mdev, clock);
554414c4531SDave Airlie 		break;
555414c4531SDave Airlie 	case G200_EV:
556414c4531SDave Airlie 		return mga_g200ev_set_plls(mdev, clock);
557414c4531SDave Airlie 		break;
558414c4531SDave Airlie 	case G200_EH:
559414c4531SDave Airlie 		return mga_g200eh_set_plls(mdev, clock);
560414c4531SDave Airlie 		break;
561414c4531SDave Airlie 	case G200_ER:
562414c4531SDave Airlie 		return mga_g200er_set_plls(mdev, clock);
563414c4531SDave Airlie 		break;
564414c4531SDave Airlie 	}
565414c4531SDave Airlie 	return 0;
566414c4531SDave Airlie }
567414c4531SDave Airlie 
568414c4531SDave Airlie static void mga_g200wb_prepare(struct drm_crtc *crtc)
569414c4531SDave Airlie {
570414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
571414c4531SDave Airlie 	u8 tmp;
572414c4531SDave Airlie 	int iter_max;
573414c4531SDave Airlie 
574414c4531SDave Airlie 	/* 1- The first step is to warn the BMC of an upcoming mode change.
575414c4531SDave Airlie 	 * We are putting the misc<0> to output.*/
576414c4531SDave Airlie 
577414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
578414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
579414c4531SDave Airlie 	tmp |= 0x10;
580414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
581414c4531SDave Airlie 
582414c4531SDave Airlie 	/* we are putting a 1 on the misc<0> line */
583414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
584414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
585414c4531SDave Airlie 	tmp |= 0x10;
586414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
587414c4531SDave Airlie 
588414c4531SDave Airlie 	/* 2- Second step to mask and further scan request
589414c4531SDave Airlie 	 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
590414c4531SDave Airlie 	 */
591414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
592414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
593414c4531SDave Airlie 	tmp |= 0x80;
594414c4531SDave Airlie 	WREG_DAC(MGA1064_SPAREREG, tmp);
595414c4531SDave Airlie 
596414c4531SDave Airlie 	/* 3a- the third step is to verifu if there is an active scan
597414c4531SDave Airlie 	 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
598414c4531SDave Airlie 	 */
599414c4531SDave Airlie 	iter_max = 300;
600414c4531SDave Airlie 	while (!(tmp & 0x1) && iter_max) {
601414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_SPAREREG);
602414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
603414c4531SDave Airlie 		udelay(1000);
604414c4531SDave Airlie 		iter_max--;
605414c4531SDave Airlie 	}
606414c4531SDave Airlie 
607414c4531SDave Airlie 	/* 3b- this step occurs only if the remove is actually scanning
608414c4531SDave Airlie 	 * we are waiting for the end of the frame which is a 1 on
609414c4531SDave Airlie 	 * remvsyncsts (XSPAREREG<1>)
610414c4531SDave Airlie 	 */
611414c4531SDave Airlie 	if (iter_max) {
612414c4531SDave Airlie 		iter_max = 300;
613414c4531SDave Airlie 		while ((tmp & 0x2) && iter_max) {
614414c4531SDave Airlie 			WREG8(DAC_INDEX, MGA1064_SPAREREG);
615414c4531SDave Airlie 			tmp = RREG8(DAC_DATA);
616414c4531SDave Airlie 			udelay(1000);
617414c4531SDave Airlie 			iter_max--;
618414c4531SDave Airlie 		}
619414c4531SDave Airlie 	}
620414c4531SDave Airlie }
621414c4531SDave Airlie 
622414c4531SDave Airlie static void mga_g200wb_commit(struct drm_crtc *crtc)
623414c4531SDave Airlie {
624414c4531SDave Airlie 	u8 tmp;
625414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
626414c4531SDave Airlie 
627414c4531SDave Airlie 	/* 1- The first step is to ensure that the vrsten and hrsten are set */
628414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 1);
629414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTCEXT_DATA);
630414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
631414c4531SDave Airlie 
632414c4531SDave Airlie 	/* 2- second step is to assert the rstlvl2 */
633414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
634414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
635414c4531SDave Airlie 	tmp |= 0x8;
636414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
637414c4531SDave Airlie 
638414c4531SDave Airlie 	/* wait 10 us */
639414c4531SDave Airlie 	udelay(10);
640414c4531SDave Airlie 
641414c4531SDave Airlie 	/* 3- deassert rstlvl2 */
642414c4531SDave Airlie 	tmp &= ~0x08;
643414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
644414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
645414c4531SDave Airlie 
646414c4531SDave Airlie 	/* 4- remove mask of scan request */
647414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
648414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
649414c4531SDave Airlie 	tmp &= ~0x80;
650414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
651414c4531SDave Airlie 
652414c4531SDave Airlie 	/* 5- put back a 0 on the misc<0> line */
653414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
654414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
655414c4531SDave Airlie 	tmp &= ~0x10;
656414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
657414c4531SDave Airlie }
658414c4531SDave Airlie 
659414c4531SDave Airlie 
660414c4531SDave Airlie void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
661414c4531SDave Airlie {
662414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
663414c4531SDave Airlie 	u32 addr;
664414c4531SDave Airlie 	int count;
665414c4531SDave Airlie 
666414c4531SDave Airlie 	while (RREG8(0x1fda) & 0x08);
667414c4531SDave Airlie 	while (!(RREG8(0x1fda) & 0x08));
668414c4531SDave Airlie 
669414c4531SDave Airlie 	count = RREG8(MGAREG_VCOUNT) + 2;
670414c4531SDave Airlie 	while (RREG8(MGAREG_VCOUNT) < count);
671414c4531SDave Airlie 
672414c4531SDave Airlie 	addr = offset >> 2;
673414c4531SDave Airlie 	WREG_CRT(0x0d, (u8)(addr & 0xff));
674414c4531SDave Airlie 	WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
675414c4531SDave Airlie 	WREG_CRT(0xaf, (u8)(addr >> 16) & 0xf);
676414c4531SDave Airlie }
677414c4531SDave Airlie 
678414c4531SDave Airlie 
679414c4531SDave Airlie /* ast is different - we will force move buffers out of VRAM */
680414c4531SDave Airlie static int mga_crtc_do_set_base(struct drm_crtc *crtc,
681414c4531SDave Airlie 				struct drm_framebuffer *fb,
682414c4531SDave Airlie 				int x, int y, int atomic)
683414c4531SDave Airlie {
684414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
685414c4531SDave Airlie 	struct drm_gem_object *obj;
686414c4531SDave Airlie 	struct mga_framebuffer *mga_fb;
687414c4531SDave Airlie 	struct mgag200_bo *bo;
688414c4531SDave Airlie 	int ret;
689414c4531SDave Airlie 	u64 gpu_addr;
690414c4531SDave Airlie 
691414c4531SDave Airlie 	/* push the previous fb to system ram */
692414c4531SDave Airlie 	if (!atomic && fb) {
693414c4531SDave Airlie 		mga_fb = to_mga_framebuffer(fb);
694414c4531SDave Airlie 		obj = mga_fb->obj;
695414c4531SDave Airlie 		bo = gem_to_mga_bo(obj);
696414c4531SDave Airlie 		ret = mgag200_bo_reserve(bo, false);
697414c4531SDave Airlie 		if (ret)
698414c4531SDave Airlie 			return ret;
699414c4531SDave Airlie 		mgag200_bo_push_sysram(bo);
700414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
701414c4531SDave Airlie 	}
702414c4531SDave Airlie 
703414c4531SDave Airlie 	mga_fb = to_mga_framebuffer(crtc->fb);
704414c4531SDave Airlie 	obj = mga_fb->obj;
705414c4531SDave Airlie 	bo = gem_to_mga_bo(obj);
706414c4531SDave Airlie 
707414c4531SDave Airlie 	ret = mgag200_bo_reserve(bo, false);
708414c4531SDave Airlie 	if (ret)
709414c4531SDave Airlie 		return ret;
710414c4531SDave Airlie 
711414c4531SDave Airlie 	ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
712414c4531SDave Airlie 	if (ret) {
713414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
714414c4531SDave Airlie 		return ret;
715414c4531SDave Airlie 	}
716414c4531SDave Airlie 
717414c4531SDave Airlie 	if (&mdev->mfbdev->mfb == mga_fb) {
718414c4531SDave Airlie 		/* if pushing console in kmap it */
719414c4531SDave Airlie 		ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
720414c4531SDave Airlie 		if (ret)
721414c4531SDave Airlie 			DRM_ERROR("failed to kmap fbcon\n");
722414c4531SDave Airlie 
723414c4531SDave Airlie 	}
724414c4531SDave Airlie 	mgag200_bo_unreserve(bo);
725414c4531SDave Airlie 
726414c4531SDave Airlie 	DRM_INFO("mga base %llx\n", gpu_addr);
727414c4531SDave Airlie 
728414c4531SDave Airlie 	mga_set_start_address(crtc, (u32)gpu_addr);
729414c4531SDave Airlie 
730414c4531SDave Airlie 	return 0;
731414c4531SDave Airlie }
732414c4531SDave Airlie 
733414c4531SDave Airlie static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
734414c4531SDave Airlie 				  struct drm_framebuffer *old_fb)
735414c4531SDave Airlie {
736414c4531SDave Airlie 	return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
737414c4531SDave Airlie }
738414c4531SDave Airlie 
739414c4531SDave Airlie static int mga_crtc_mode_set(struct drm_crtc *crtc,
740414c4531SDave Airlie 				struct drm_display_mode *mode,
741414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode,
742414c4531SDave Airlie 				int x, int y, struct drm_framebuffer *old_fb)
743414c4531SDave Airlie {
744414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
745414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
746414c4531SDave Airlie 	int hdisplay, hsyncstart, hsyncend, htotal;
747414c4531SDave Airlie 	int vdisplay, vsyncstart, vsyncend, vtotal;
748414c4531SDave Airlie 	int pitch;
749414c4531SDave Airlie 	int option = 0, option2 = 0;
750414c4531SDave Airlie 	int i;
751414c4531SDave Airlie 	unsigned char misc = 0;
752414c4531SDave Airlie 	unsigned char ext_vga[6];
753414c4531SDave Airlie 	unsigned char ext_vga_index24;
754414c4531SDave Airlie 	unsigned char dac_index90 = 0;
755414c4531SDave Airlie 	u8 bppshift;
756414c4531SDave Airlie 
757414c4531SDave Airlie 	static unsigned char dacvalue[] = {
758414c4531SDave Airlie 		/* 0x00: */        0,    0,    0,    0,    0,    0, 0x00,    0,
759414c4531SDave Airlie 		/* 0x08: */        0,    0,    0,    0,    0,    0,    0,    0,
760414c4531SDave Airlie 		/* 0x10: */        0,    0,    0,    0,    0,    0,    0,    0,
761414c4531SDave Airlie 		/* 0x18: */     0x00,    0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
762414c4531SDave Airlie 		/* 0x20: */     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
763414c4531SDave Airlie 		/* 0x28: */     0x00, 0x00, 0x00, 0x00,    0,    0,    0, 0x40,
764414c4531SDave Airlie 		/* 0x30: */     0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
765414c4531SDave Airlie 		/* 0x38: */     0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
766414c4531SDave Airlie 		/* 0x40: */        0,    0,    0,    0,    0,    0,    0,    0,
767414c4531SDave Airlie 		/* 0x48: */        0,    0,    0,    0,    0,    0,    0,    0
768414c4531SDave Airlie 	};
769414c4531SDave Airlie 
770414c4531SDave Airlie 	bppshift = mdev->bpp_shifts[(crtc->fb->bits_per_pixel >> 3) - 1];
771414c4531SDave Airlie 
772414c4531SDave Airlie 	switch (mdev->type) {
773414c4531SDave Airlie 	case G200_SE_A:
774414c4531SDave Airlie 	case G200_SE_B:
775414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x03;
776414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
777414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
778414c4531SDave Airlie 					     MGA1064_MISC_CTL_VGA8 |
779414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
780414c4531SDave Airlie 		if (mdev->has_sdram)
781414c4531SDave Airlie 			option = 0x40049120;
782414c4531SDave Airlie 		else
783414c4531SDave Airlie 			option = 0x4004d120;
784414c4531SDave Airlie 		option2 = 0x00008000;
785414c4531SDave Airlie 		break;
786414c4531SDave Airlie 	case G200_WB:
787414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x07;
788414c4531SDave Airlie 		option = 0x41049120;
789414c4531SDave Airlie 		option2 = 0x0000b000;
790414c4531SDave Airlie 		break;
791414c4531SDave Airlie 	case G200_EV:
792414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
793414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
794414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
795414c4531SDave Airlie 		option = 0x00000120;
796414c4531SDave Airlie 		option2 = 0x0000b000;
797414c4531SDave Airlie 		break;
798414c4531SDave Airlie 	case G200_EH:
799414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
800414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
801414c4531SDave Airlie 		option = 0x00000120;
802414c4531SDave Airlie 		option2 = 0x0000b000;
803414c4531SDave Airlie 		break;
804414c4531SDave Airlie 	case G200_ER:
805414c4531SDave Airlie 		dac_index90 = 0;
806414c4531SDave Airlie 		break;
807414c4531SDave Airlie 	}
808414c4531SDave Airlie 
809414c4531SDave Airlie 	switch (crtc->fb->bits_per_pixel) {
810414c4531SDave Airlie 	case 8:
811414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
812414c4531SDave Airlie 		break;
813414c4531SDave Airlie 	case 16:
814414c4531SDave Airlie 		if (crtc->fb->depth == 15)
815414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
816414c4531SDave Airlie 		else
817414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
818414c4531SDave Airlie 		break;
819414c4531SDave Airlie 	case 24:
820414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
821414c4531SDave Airlie 		break;
822414c4531SDave Airlie 	case 32:
823414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
824414c4531SDave Airlie 		break;
825414c4531SDave Airlie 	}
826414c4531SDave Airlie 
827414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
828414c4531SDave Airlie 		misc |= 0x40;
829414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
830414c4531SDave Airlie 		misc |= 0x80;
831414c4531SDave Airlie 
832414c4531SDave Airlie 
833414c4531SDave Airlie 	for (i = 0; i < sizeof(dacvalue); i++) {
834414c4531SDave Airlie 		if ((i <= 0x03) ||
835414c4531SDave Airlie 		    (i == 0x07) ||
836414c4531SDave Airlie 		    (i == 0x0b) ||
837414c4531SDave Airlie 		    (i == 0x0f) ||
838414c4531SDave Airlie 		    ((i >= 0x13) && (i <= 0x17)) ||
839414c4531SDave Airlie 		    (i == 0x1b) ||
840414c4531SDave Airlie 		    (i == 0x1c) ||
841414c4531SDave Airlie 		    ((i >= 0x1f) && (i <= 0x29)) ||
842414c4531SDave Airlie 		    ((i >= 0x30) && (i <= 0x37)))
843414c4531SDave Airlie 			continue;
844414c4531SDave Airlie 		if (IS_G200_SE(mdev) &&
845414c4531SDave Airlie 		    ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
846414c4531SDave Airlie 			continue;
847414c4531SDave Airlie 		if ((mdev->type == G200_EV || mdev->type == G200_WB || mdev->type == G200_EH) &&
848414c4531SDave Airlie 		    (i >= 0x44) && (i <= 0x4e))
849414c4531SDave Airlie 			continue;
850414c4531SDave Airlie 
851414c4531SDave Airlie 		WREG_DAC(i, dacvalue[i]);
852414c4531SDave Airlie 	}
853414c4531SDave Airlie 
854414c4531SDave Airlie 	if (mdev->type == G200_ER) {
855414c4531SDave Airlie 		WREG_DAC(0x90, dac_index90);
856414c4531SDave Airlie 	}
857414c4531SDave Airlie 
858414c4531SDave Airlie 
859414c4531SDave Airlie 	if (option)
860414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
861414c4531SDave Airlie 	if (option2)
862414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
863414c4531SDave Airlie 
864414c4531SDave Airlie 	WREG_SEQ(2, 0xf);
865414c4531SDave Airlie 	WREG_SEQ(3, 0);
866414c4531SDave Airlie 	WREG_SEQ(4, 0xe);
867414c4531SDave Airlie 
868414c4531SDave Airlie 	pitch = crtc->fb->pitches[0] / (crtc->fb->bits_per_pixel / 8);
869414c4531SDave Airlie 	if (crtc->fb->bits_per_pixel == 24)
870414c4531SDave Airlie 		pitch = pitch >> (4 - bppshift);
871414c4531SDave Airlie 	else
872414c4531SDave Airlie 		pitch = pitch >> (4 - bppshift);
873414c4531SDave Airlie 
874414c4531SDave Airlie 	hdisplay = mode->hdisplay / 8 - 1;
875414c4531SDave Airlie 	hsyncstart = mode->hsync_start / 8 - 1;
876414c4531SDave Airlie 	hsyncend = mode->hsync_end / 8 - 1;
877414c4531SDave Airlie 	htotal = mode->htotal / 8 - 1;
878414c4531SDave Airlie 
879414c4531SDave Airlie 	/* Work around hardware quirk */
880414c4531SDave Airlie 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
881414c4531SDave Airlie 		htotal++;
882414c4531SDave Airlie 
883414c4531SDave Airlie 	vdisplay = mode->vdisplay - 1;
884414c4531SDave Airlie 	vsyncstart = mode->vsync_start - 1;
885414c4531SDave Airlie 	vsyncend = mode->vsync_end - 1;
886414c4531SDave Airlie 	vtotal = mode->vtotal - 2;
887414c4531SDave Airlie 
888414c4531SDave Airlie 	WREG_GFX(0, 0);
889414c4531SDave Airlie 	WREG_GFX(1, 0);
890414c4531SDave Airlie 	WREG_GFX(2, 0);
891414c4531SDave Airlie 	WREG_GFX(3, 0);
892414c4531SDave Airlie 	WREG_GFX(4, 0);
893414c4531SDave Airlie 	WREG_GFX(5, 0x40);
894414c4531SDave Airlie 	WREG_GFX(6, 0x5);
895414c4531SDave Airlie 	WREG_GFX(7, 0xf);
896414c4531SDave Airlie 	WREG_GFX(8, 0xf);
897414c4531SDave Airlie 
898414c4531SDave Airlie 	WREG_CRT(0, htotal - 4);
899414c4531SDave Airlie 	WREG_CRT(1, hdisplay);
900414c4531SDave Airlie 	WREG_CRT(2, hdisplay);
901414c4531SDave Airlie 	WREG_CRT(3, (htotal & 0x1F) | 0x80);
902414c4531SDave Airlie 	WREG_CRT(4, hsyncstart);
903414c4531SDave Airlie 	WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
904414c4531SDave Airlie 	WREG_CRT(6, vtotal & 0xFF);
905414c4531SDave Airlie 	WREG_CRT(7, ((vtotal & 0x100) >> 8) |
906414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 7) |
907414c4531SDave Airlie 		 ((vsyncstart & 0x100) >> 6) |
908414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 5) |
909414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 4) | /* linecomp */
910414c4531SDave Airlie 		 ((vtotal & 0x200) >> 4)|
911414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3) |
912414c4531SDave Airlie 		 ((vsyncstart & 0x200) >> 2));
913414c4531SDave Airlie 	WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
914414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3));
915414c4531SDave Airlie 	WREG_CRT(10, 0);
916414c4531SDave Airlie 	WREG_CRT(11, 0);
917414c4531SDave Airlie 	WREG_CRT(12, 0);
918414c4531SDave Airlie 	WREG_CRT(13, 0);
919414c4531SDave Airlie 	WREG_CRT(14, 0);
920414c4531SDave Airlie 	WREG_CRT(15, 0);
921414c4531SDave Airlie 	WREG_CRT(16, vsyncstart & 0xFF);
922414c4531SDave Airlie 	WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
923414c4531SDave Airlie 	WREG_CRT(18, vdisplay & 0xFF);
924414c4531SDave Airlie 	WREG_CRT(19, pitch & 0xFF);
925414c4531SDave Airlie 	WREG_CRT(20, 0);
926414c4531SDave Airlie 	WREG_CRT(21, vdisplay & 0xFF);
927414c4531SDave Airlie 	WREG_CRT(22, (vtotal + 1) & 0xFF);
928414c4531SDave Airlie 	WREG_CRT(23, 0xc3);
929414c4531SDave Airlie 	WREG_CRT(24, vdisplay & 0xFF);
930414c4531SDave Airlie 
931414c4531SDave Airlie 	ext_vga[0] = 0;
932414c4531SDave Airlie 	ext_vga[5] = 0;
933414c4531SDave Airlie 
934414c4531SDave Airlie 	/* TODO interlace */
935414c4531SDave Airlie 
936414c4531SDave Airlie 	ext_vga[0] |= (pitch & 0x300) >> 4;
937414c4531SDave Airlie 	ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
938414c4531SDave Airlie 		((hdisplay & 0x100) >> 7) |
939414c4531SDave Airlie 		((hsyncstart & 0x100) >> 6) |
940414c4531SDave Airlie 		(htotal & 0x40);
941414c4531SDave Airlie 	ext_vga[2] = ((vtotal & 0xc00) >> 10) |
942414c4531SDave Airlie 		((vdisplay & 0x400) >> 8) |
943414c4531SDave Airlie 		((vdisplay & 0xc00) >> 7) |
944414c4531SDave Airlie 		((vsyncstart & 0xc00) >> 5) |
945414c4531SDave Airlie 		((vdisplay & 0x400) >> 3);
946414c4531SDave Airlie 	if (crtc->fb->bits_per_pixel == 24)
947414c4531SDave Airlie 		ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
948414c4531SDave Airlie 	else
949414c4531SDave Airlie 		ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
950414c4531SDave Airlie 	ext_vga[4] = 0;
951414c4531SDave Airlie 	if (mdev->type == G200_WB)
952414c4531SDave Airlie 		ext_vga[1] |= 0x88;
953414c4531SDave Airlie 
954414c4531SDave Airlie 	ext_vga_index24 = 0x05;
955414c4531SDave Airlie 
956414c4531SDave Airlie 	/* Set pixel clocks */
957414c4531SDave Airlie 	misc = 0x2d;
958414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
959414c4531SDave Airlie 
960414c4531SDave Airlie 	mga_crtc_set_plls(mdev, mode->clock);
961414c4531SDave Airlie 
962414c4531SDave Airlie 	for (i = 0; i < 6; i++) {
963414c4531SDave Airlie 		WREG_ECRT(i, ext_vga[i]);
964414c4531SDave Airlie 	}
965414c4531SDave Airlie 
966414c4531SDave Airlie 	if (mdev->type == G200_ER)
967414c4531SDave Airlie 		WREG_ECRT(24, ext_vga_index24);
968414c4531SDave Airlie 
969414c4531SDave Airlie 	if (mdev->type == G200_EV) {
970414c4531SDave Airlie 		WREG_ECRT(6, 0);
971414c4531SDave Airlie 	}
972414c4531SDave Airlie 
973414c4531SDave Airlie 	WREG_ECRT(0, ext_vga[0]);
974414c4531SDave Airlie 	/* Enable mga pixel clock */
975414c4531SDave Airlie 	misc = 0x2d;
976414c4531SDave Airlie 
977414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
978414c4531SDave Airlie 
979414c4531SDave Airlie 	if (adjusted_mode)
980414c4531SDave Airlie 		memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
981414c4531SDave Airlie 
982414c4531SDave Airlie 	mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
983414c4531SDave Airlie 
984414c4531SDave Airlie 	/* reset tagfifo */
985414c4531SDave Airlie 	if (mdev->type == G200_ER) {
986414c4531SDave Airlie 		u32 mem_ctl = RREG32(MGAREG_MEMCTL);
987414c4531SDave Airlie 		u8 seq1;
988414c4531SDave Airlie 
989414c4531SDave Airlie 		/* screen off */
990414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x01);
991414c4531SDave Airlie 		seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
992414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1);
993414c4531SDave Airlie 
994414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
995414c4531SDave Airlie 		udelay(1000);
996414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
997414c4531SDave Airlie 
998414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
999414c4531SDave Airlie 	}
1000414c4531SDave Airlie 
1001414c4531SDave Airlie 
1002414c4531SDave Airlie 	if (IS_G200_SE(mdev)) {
1003414c4531SDave Airlie 		if (mdev->reg_1e24 >= 0x02) {
1004414c4531SDave Airlie 			u8 hi_pri_lvl;
1005414c4531SDave Airlie 			u32 bpp;
1006414c4531SDave Airlie 			u32 mb;
1007414c4531SDave Airlie 
1008414c4531SDave Airlie 			if (crtc->fb->bits_per_pixel > 16)
1009414c4531SDave Airlie 				bpp = 32;
1010414c4531SDave Airlie 			else if (crtc->fb->bits_per_pixel > 8)
1011414c4531SDave Airlie 				bpp = 16;
1012414c4531SDave Airlie 			else
1013414c4531SDave Airlie 				bpp = 8;
1014414c4531SDave Airlie 
1015414c4531SDave Airlie 			mb = (mode->clock * bpp) / 1000;
1016414c4531SDave Airlie 			if (mb > 3100)
1017414c4531SDave Airlie 				hi_pri_lvl = 0;
1018414c4531SDave Airlie 			else if (mb > 2600)
1019414c4531SDave Airlie 				hi_pri_lvl = 1;
1020414c4531SDave Airlie 			else if (mb > 1900)
1021414c4531SDave Airlie 				hi_pri_lvl = 2;
1022414c4531SDave Airlie 			else if (mb > 1160)
1023414c4531SDave Airlie 				hi_pri_lvl = 3;
1024414c4531SDave Airlie 			else if (mb > 440)
1025414c4531SDave Airlie 				hi_pri_lvl = 4;
1026414c4531SDave Airlie 			else
1027414c4531SDave Airlie 				hi_pri_lvl = 5;
1028414c4531SDave Airlie 
1029414c4531SDave Airlie 			WREG8(0x1fde, 0x06);
1030414c4531SDave Airlie 			WREG8(0x1fdf, hi_pri_lvl);
1031414c4531SDave Airlie 		} else {
1032414c4531SDave Airlie 			if (mdev->reg_1e24 >= 0x01)
1033414c4531SDave Airlie 				WREG8(0x1fdf, 0x03);
1034414c4531SDave Airlie 			else
1035414c4531SDave Airlie 				WREG8(0x1fdf, 0x04);
1036414c4531SDave Airlie 		}
1037414c4531SDave Airlie 	}
1038414c4531SDave Airlie 	return 0;
1039414c4531SDave Airlie }
1040414c4531SDave Airlie 
1041414c4531SDave Airlie #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1042414c4531SDave Airlie static int mga_suspend(struct drm_crtc *crtc)
1043414c4531SDave Airlie {
1044414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1045414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1046414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1047414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1048414c4531SDave Airlie 	int option;
1049414c4531SDave Airlie 
1050414c4531SDave Airlie 	if (mdev->suspended)
1051414c4531SDave Airlie 		return 0;
1052414c4531SDave Airlie 
1053414c4531SDave Airlie 	WREG_SEQ(1, 0x20);
1054414c4531SDave Airlie 	WREG_ECRT(1, 0x30);
1055414c4531SDave Airlie 	/* Disable the pixel clock */
1056414c4531SDave Airlie 	WREG_DAC(0x1a, 0x05);
1057414c4531SDave Airlie 	/* Power down the DAC */
1058414c4531SDave Airlie 	WREG_DAC(0x1e, 0x18);
1059414c4531SDave Airlie 	/* Power down the pixel PLL */
1060414c4531SDave Airlie 	WREG_DAC(0x1a, 0x0d);
1061414c4531SDave Airlie 
1062414c4531SDave Airlie 	/* Disable PLLs and clocks */
1063414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1064414c4531SDave Airlie 	option &= ~(0x1F8024);
1065414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1066414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D3hot);
1067414c4531SDave Airlie 	pci_disable_device(pdev);
1068414c4531SDave Airlie 
1069414c4531SDave Airlie 	mdev->suspended = true;
1070414c4531SDave Airlie 
1071414c4531SDave Airlie 	return 0;
1072414c4531SDave Airlie }
1073414c4531SDave Airlie 
1074414c4531SDave Airlie static int mga_resume(struct drm_crtc *crtc)
1075414c4531SDave Airlie {
1076414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1077414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1078414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1079414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1080414c4531SDave Airlie 	int option;
1081414c4531SDave Airlie 
1082414c4531SDave Airlie 	if (!mdev->suspended)
1083414c4531SDave Airlie 		return 0;
1084414c4531SDave Airlie 
1085414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D0);
1086414c4531SDave Airlie 	pci_enable_device(pdev);
1087414c4531SDave Airlie 
1088414c4531SDave Airlie 	/* Disable sysclk */
1089414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1090414c4531SDave Airlie 	option &= ~(0x4);
1091414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1092414c4531SDave Airlie 
1093414c4531SDave Airlie 	mdev->suspended = false;
1094414c4531SDave Airlie 
1095414c4531SDave Airlie 	return 0;
1096414c4531SDave Airlie }
1097414c4531SDave Airlie 
1098414c4531SDave Airlie #endif
1099414c4531SDave Airlie 
1100414c4531SDave Airlie static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1101414c4531SDave Airlie {
1102414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1103414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1104414c4531SDave Airlie 	u8 seq1 = 0, crtcext1 = 0;
1105414c4531SDave Airlie 
1106414c4531SDave Airlie 	switch (mode) {
1107414c4531SDave Airlie 	case DRM_MODE_DPMS_ON:
1108414c4531SDave Airlie 		seq1 = 0;
1109414c4531SDave Airlie 		crtcext1 = 0;
1110414c4531SDave Airlie 		mga_crtc_load_lut(crtc);
1111414c4531SDave Airlie 		break;
1112414c4531SDave Airlie 	case DRM_MODE_DPMS_STANDBY:
1113414c4531SDave Airlie 		seq1 = 0x20;
1114414c4531SDave Airlie 		crtcext1 = 0x10;
1115414c4531SDave Airlie 		break;
1116414c4531SDave Airlie 	case DRM_MODE_DPMS_SUSPEND:
1117414c4531SDave Airlie 		seq1 = 0x20;
1118414c4531SDave Airlie 		crtcext1 = 0x20;
1119414c4531SDave Airlie 		break;
1120414c4531SDave Airlie 	case DRM_MODE_DPMS_OFF:
1121414c4531SDave Airlie 		seq1 = 0x20;
1122414c4531SDave Airlie 		crtcext1 = 0x30;
1123414c4531SDave Airlie 		break;
1124414c4531SDave Airlie 	}
1125414c4531SDave Airlie 
1126414c4531SDave Airlie #if 0
1127414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_OFF) {
1128414c4531SDave Airlie 		mga_suspend(crtc);
1129414c4531SDave Airlie 	}
1130414c4531SDave Airlie #endif
1131414c4531SDave Airlie 	WREG8(MGAREG_SEQ_INDEX, 0x01);
1132414c4531SDave Airlie 	seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1133414c4531SDave Airlie 	mga_wait_vsync(mdev);
1134414c4531SDave Airlie 	mga_wait_busy(mdev);
1135414c4531SDave Airlie 	WREG8(MGAREG_SEQ_DATA, seq1);
1136414c4531SDave Airlie 	msleep(20);
1137414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1138414c4531SDave Airlie 	crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1139414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1140414c4531SDave Airlie 
1141414c4531SDave Airlie #if 0
1142414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1143414c4531SDave Airlie 		mga_resume(crtc);
1144414c4531SDave Airlie 		drm_helper_resume_force_mode(dev);
1145414c4531SDave Airlie 	}
1146414c4531SDave Airlie #endif
1147414c4531SDave Airlie }
1148414c4531SDave Airlie 
1149414c4531SDave Airlie /*
1150414c4531SDave Airlie  * This is called before a mode is programmed. A typical use might be to
1151414c4531SDave Airlie  * enable DPMS during the programming to avoid seeing intermediate stages,
1152414c4531SDave Airlie  * but that's not relevant to us
1153414c4531SDave Airlie  */
1154414c4531SDave Airlie static void mga_crtc_prepare(struct drm_crtc *crtc)
1155414c4531SDave Airlie {
1156414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1157414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1158414c4531SDave Airlie 	u8 tmp;
1159414c4531SDave Airlie 
1160414c4531SDave Airlie 	/*	mga_resume(crtc);*/
1161414c4531SDave Airlie 
1162414c4531SDave Airlie 	WREG8(MGAREG_CRTC_INDEX, 0x11);
1163414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTC_DATA);
1164414c4531SDave Airlie 	WREG_CRT(0x11, tmp | 0x80);
1165414c4531SDave Airlie 
1166414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1167414c4531SDave Airlie 		WREG_SEQ(0, 1);
1168414c4531SDave Airlie 		msleep(50);
1169414c4531SDave Airlie 		WREG_SEQ(1, 0x20);
1170414c4531SDave Airlie 		msleep(20);
1171414c4531SDave Airlie 	} else {
1172414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1173414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1174414c4531SDave Airlie 
1175414c4531SDave Airlie 		/* start sync reset */
1176414c4531SDave Airlie 		WREG_SEQ(0, 1);
1177414c4531SDave Airlie 		WREG_SEQ(1, tmp | 0x20);
1178414c4531SDave Airlie 	}
1179414c4531SDave Airlie 
1180414c4531SDave Airlie 	if (mdev->type == G200_WB)
1181414c4531SDave Airlie 		mga_g200wb_prepare(crtc);
1182414c4531SDave Airlie 
1183414c4531SDave Airlie 	WREG_CRT(17, 0);
1184414c4531SDave Airlie }
1185414c4531SDave Airlie 
1186414c4531SDave Airlie /*
1187414c4531SDave Airlie  * This is called after a mode is programmed. It should reverse anything done
1188414c4531SDave Airlie  * by the prepare function
1189414c4531SDave Airlie  */
1190414c4531SDave Airlie static void mga_crtc_commit(struct drm_crtc *crtc)
1191414c4531SDave Airlie {
1192414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1193414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1194414c4531SDave Airlie 	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1195414c4531SDave Airlie 	u8 tmp;
1196414c4531SDave Airlie 
1197414c4531SDave Airlie 	if (mdev->type == G200_WB)
1198414c4531SDave Airlie 		mga_g200wb_commit(crtc);
1199414c4531SDave Airlie 
1200414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1201414c4531SDave Airlie 		msleep(50);
1202414c4531SDave Airlie 		WREG_SEQ(1, 0x0);
1203414c4531SDave Airlie 		msleep(20);
1204414c4531SDave Airlie 		WREG_SEQ(0, 0x3);
1205414c4531SDave Airlie 	} else {
1206414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1207414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1208414c4531SDave Airlie 
1209414c4531SDave Airlie 		tmp &= ~0x20;
1210414c4531SDave Airlie 		WREG_SEQ(0x1, tmp);
1211414c4531SDave Airlie 		WREG_SEQ(0, 3);
1212414c4531SDave Airlie 	}
1213414c4531SDave Airlie 	crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1214414c4531SDave Airlie }
1215414c4531SDave Airlie 
1216414c4531SDave Airlie /*
1217414c4531SDave Airlie  * The core can pass us a set of gamma values to program. We actually only
1218414c4531SDave Airlie  * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1219414c4531SDave Airlie  * but it's a requirement that we provide the function
1220414c4531SDave Airlie  */
1221414c4531SDave Airlie static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1222414c4531SDave Airlie 				  u16 *blue, uint32_t start, uint32_t size)
1223414c4531SDave Airlie {
1224414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1225414c4531SDave Airlie 	int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
1226414c4531SDave Airlie 	int i;
1227414c4531SDave Airlie 
1228414c4531SDave Airlie 	for (i = start; i < end; i++) {
1229414c4531SDave Airlie 		mga_crtc->lut_r[i] = red[i] >> 8;
1230414c4531SDave Airlie 		mga_crtc->lut_g[i] = green[i] >> 8;
1231414c4531SDave Airlie 		mga_crtc->lut_b[i] = blue[i] >> 8;
1232414c4531SDave Airlie 	}
1233414c4531SDave Airlie 	mga_crtc_load_lut(crtc);
1234414c4531SDave Airlie }
1235414c4531SDave Airlie 
1236414c4531SDave Airlie /* Simple cleanup function */
1237414c4531SDave Airlie static void mga_crtc_destroy(struct drm_crtc *crtc)
1238414c4531SDave Airlie {
1239414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1240414c4531SDave Airlie 
1241414c4531SDave Airlie 	drm_crtc_cleanup(crtc);
1242414c4531SDave Airlie 	kfree(mga_crtc);
1243414c4531SDave Airlie }
1244414c4531SDave Airlie 
1245414c4531SDave Airlie /* These provide the minimum set of functions required to handle a CRTC */
1246414c4531SDave Airlie static const struct drm_crtc_funcs mga_crtc_funcs = {
1247414c4531SDave Airlie 	.gamma_set = mga_crtc_gamma_set,
1248414c4531SDave Airlie 	.set_config = drm_crtc_helper_set_config,
1249414c4531SDave Airlie 	.destroy = mga_crtc_destroy,
1250414c4531SDave Airlie };
1251414c4531SDave Airlie 
1252414c4531SDave Airlie static const struct drm_crtc_helper_funcs mga_helper_funcs = {
1253414c4531SDave Airlie 	.dpms = mga_crtc_dpms,
1254414c4531SDave Airlie 	.mode_fixup = mga_crtc_mode_fixup,
1255414c4531SDave Airlie 	.mode_set = mga_crtc_mode_set,
1256414c4531SDave Airlie 	.mode_set_base = mga_crtc_mode_set_base,
1257414c4531SDave Airlie 	.prepare = mga_crtc_prepare,
1258414c4531SDave Airlie 	.commit = mga_crtc_commit,
1259414c4531SDave Airlie 	.load_lut = mga_crtc_load_lut,
1260414c4531SDave Airlie };
1261414c4531SDave Airlie 
1262414c4531SDave Airlie /* CRTC setup */
1263414c4531SDave Airlie static void mga_crtc_init(struct drm_device *dev)
1264414c4531SDave Airlie {
1265414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1266414c4531SDave Airlie 	struct mga_crtc *mga_crtc;
1267414c4531SDave Airlie 	int i;
1268414c4531SDave Airlie 
1269414c4531SDave Airlie 	mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1270414c4531SDave Airlie 			      (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1271414c4531SDave Airlie 			      GFP_KERNEL);
1272414c4531SDave Airlie 
1273414c4531SDave Airlie 	if (mga_crtc == NULL)
1274414c4531SDave Airlie 		return;
1275414c4531SDave Airlie 
1276414c4531SDave Airlie 	drm_crtc_init(dev, &mga_crtc->base, &mga_crtc_funcs);
1277414c4531SDave Airlie 
1278414c4531SDave Airlie 	drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1279414c4531SDave Airlie 	mdev->mode_info.crtc = mga_crtc;
1280414c4531SDave Airlie 
1281414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
1282414c4531SDave Airlie 		mga_crtc->lut_r[i] = i;
1283414c4531SDave Airlie 		mga_crtc->lut_g[i] = i;
1284414c4531SDave Airlie 		mga_crtc->lut_b[i] = i;
1285414c4531SDave Airlie 	}
1286414c4531SDave Airlie 
1287414c4531SDave Airlie 	drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1288414c4531SDave Airlie }
1289414c4531SDave Airlie 
1290414c4531SDave Airlie /** Sets the color ramps on behalf of fbcon */
1291414c4531SDave Airlie void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1292414c4531SDave Airlie 			      u16 blue, int regno)
1293414c4531SDave Airlie {
1294414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1295414c4531SDave Airlie 
1296414c4531SDave Airlie 	mga_crtc->lut_r[regno] = red >> 8;
1297414c4531SDave Airlie 	mga_crtc->lut_g[regno] = green >> 8;
1298414c4531SDave Airlie 	mga_crtc->lut_b[regno] = blue >> 8;
1299414c4531SDave Airlie }
1300414c4531SDave Airlie 
1301414c4531SDave Airlie /** Gets the color ramps on behalf of fbcon */
1302414c4531SDave Airlie void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
1303414c4531SDave Airlie 			      u16 *blue, int regno)
1304414c4531SDave Airlie {
1305414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1306414c4531SDave Airlie 
1307414c4531SDave Airlie 	*red = (u16)mga_crtc->lut_r[regno] << 8;
1308414c4531SDave Airlie 	*green = (u16)mga_crtc->lut_g[regno] << 8;
1309414c4531SDave Airlie 	*blue = (u16)mga_crtc->lut_b[regno] << 8;
1310414c4531SDave Airlie }
1311414c4531SDave Airlie 
1312414c4531SDave Airlie /*
1313414c4531SDave Airlie  * The encoder comes after the CRTC in the output pipeline, but before
1314414c4531SDave Airlie  * the connector. It's responsible for ensuring that the digital
1315414c4531SDave Airlie  * stream is appropriately converted into the output format. Setup is
1316414c4531SDave Airlie  * very simple in this case - all we have to do is inform qemu of the
1317414c4531SDave Airlie  * colour depth in order to ensure that it displays appropriately
1318414c4531SDave Airlie  */
1319414c4531SDave Airlie 
1320414c4531SDave Airlie /*
1321414c4531SDave Airlie  * These functions are analagous to those in the CRTC code, but are intended
1322414c4531SDave Airlie  * to handle any encoder-specific limitations
1323414c4531SDave Airlie  */
1324414c4531SDave Airlie static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
1325*e811f5aeSLaurent Pinchart 				   const struct drm_display_mode *mode,
1326414c4531SDave Airlie 				   struct drm_display_mode *adjusted_mode)
1327414c4531SDave Airlie {
1328414c4531SDave Airlie 	return true;
1329414c4531SDave Airlie }
1330414c4531SDave Airlie 
1331414c4531SDave Airlie static void mga_encoder_mode_set(struct drm_encoder *encoder,
1332414c4531SDave Airlie 				struct drm_display_mode *mode,
1333414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
1334414c4531SDave Airlie {
1335414c4531SDave Airlie 
1336414c4531SDave Airlie }
1337414c4531SDave Airlie 
1338414c4531SDave Airlie static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
1339414c4531SDave Airlie {
1340414c4531SDave Airlie 	return;
1341414c4531SDave Airlie }
1342414c4531SDave Airlie 
1343414c4531SDave Airlie static void mga_encoder_prepare(struct drm_encoder *encoder)
1344414c4531SDave Airlie {
1345414c4531SDave Airlie }
1346414c4531SDave Airlie 
1347414c4531SDave Airlie static void mga_encoder_commit(struct drm_encoder *encoder)
1348414c4531SDave Airlie {
1349414c4531SDave Airlie }
1350414c4531SDave Airlie 
1351414c4531SDave Airlie void mga_encoder_destroy(struct drm_encoder *encoder)
1352414c4531SDave Airlie {
1353414c4531SDave Airlie 	struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
1354414c4531SDave Airlie 	drm_encoder_cleanup(encoder);
1355414c4531SDave Airlie 	kfree(mga_encoder);
1356414c4531SDave Airlie }
1357414c4531SDave Airlie 
1358414c4531SDave Airlie static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
1359414c4531SDave Airlie 	.dpms = mga_encoder_dpms,
1360414c4531SDave Airlie 	.mode_fixup = mga_encoder_mode_fixup,
1361414c4531SDave Airlie 	.mode_set = mga_encoder_mode_set,
1362414c4531SDave Airlie 	.prepare = mga_encoder_prepare,
1363414c4531SDave Airlie 	.commit = mga_encoder_commit,
1364414c4531SDave Airlie };
1365414c4531SDave Airlie 
1366414c4531SDave Airlie static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
1367414c4531SDave Airlie 	.destroy = mga_encoder_destroy,
1368414c4531SDave Airlie };
1369414c4531SDave Airlie 
1370414c4531SDave Airlie static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
1371414c4531SDave Airlie {
1372414c4531SDave Airlie 	struct drm_encoder *encoder;
1373414c4531SDave Airlie 	struct mga_encoder *mga_encoder;
1374414c4531SDave Airlie 
1375414c4531SDave Airlie 	mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
1376414c4531SDave Airlie 	if (!mga_encoder)
1377414c4531SDave Airlie 		return NULL;
1378414c4531SDave Airlie 
1379414c4531SDave Airlie 	encoder = &mga_encoder->base;
1380414c4531SDave Airlie 	encoder->possible_crtcs = 0x1;
1381414c4531SDave Airlie 
1382414c4531SDave Airlie 	drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
1383414c4531SDave Airlie 			 DRM_MODE_ENCODER_DAC);
1384414c4531SDave Airlie 	drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
1385414c4531SDave Airlie 
1386414c4531SDave Airlie 	return encoder;
1387414c4531SDave Airlie }
1388414c4531SDave Airlie 
1389414c4531SDave Airlie 
1390414c4531SDave Airlie static int mga_vga_get_modes(struct drm_connector *connector)
1391414c4531SDave Airlie {
1392414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1393414c4531SDave Airlie 	struct edid *edid;
1394414c4531SDave Airlie 	int ret = 0;
1395414c4531SDave Airlie 
1396414c4531SDave Airlie 	edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1397414c4531SDave Airlie 	if (edid) {
1398414c4531SDave Airlie 		drm_mode_connector_update_edid_property(connector, edid);
1399414c4531SDave Airlie 		ret = drm_add_edid_modes(connector, edid);
1400414c4531SDave Airlie 		connector->display_info.raw_edid = NULL;
1401414c4531SDave Airlie 		kfree(edid);
1402414c4531SDave Airlie 	}
1403414c4531SDave Airlie 	return ret;
1404414c4531SDave Airlie }
1405414c4531SDave Airlie 
1406414c4531SDave Airlie static int mga_vga_mode_valid(struct drm_connector *connector,
1407414c4531SDave Airlie 				 struct drm_display_mode *mode)
1408414c4531SDave Airlie {
1409414c4531SDave Airlie 	/* FIXME: Add bandwidth and g200se limitations */
1410414c4531SDave Airlie 
1411414c4531SDave Airlie 	if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1412414c4531SDave Airlie 	    mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1413414c4531SDave Airlie 	    mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1414414c4531SDave Airlie 	    mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1415414c4531SDave Airlie 		return MODE_BAD;
1416414c4531SDave Airlie 	}
1417414c4531SDave Airlie 
1418414c4531SDave Airlie 	return MODE_OK;
1419414c4531SDave Airlie }
1420414c4531SDave Airlie 
1421414c4531SDave Airlie struct drm_encoder *mga_connector_best_encoder(struct drm_connector
1422414c4531SDave Airlie 						  *connector)
1423414c4531SDave Airlie {
1424414c4531SDave Airlie 	int enc_id = connector->encoder_ids[0];
1425414c4531SDave Airlie 	struct drm_mode_object *obj;
1426414c4531SDave Airlie 	struct drm_encoder *encoder;
1427414c4531SDave Airlie 
1428414c4531SDave Airlie 	/* pick the encoder ids */
1429414c4531SDave Airlie 	if (enc_id) {
1430414c4531SDave Airlie 		obj =
1431414c4531SDave Airlie 		    drm_mode_object_find(connector->dev, enc_id,
1432414c4531SDave Airlie 					 DRM_MODE_OBJECT_ENCODER);
1433414c4531SDave Airlie 		if (!obj)
1434414c4531SDave Airlie 			return NULL;
1435414c4531SDave Airlie 		encoder = obj_to_encoder(obj);
1436414c4531SDave Airlie 		return encoder;
1437414c4531SDave Airlie 	}
1438414c4531SDave Airlie 	return NULL;
1439414c4531SDave Airlie }
1440414c4531SDave Airlie 
1441414c4531SDave Airlie static enum drm_connector_status mga_vga_detect(struct drm_connector
1442414c4531SDave Airlie 						   *connector, bool force)
1443414c4531SDave Airlie {
1444414c4531SDave Airlie 	return connector_status_connected;
1445414c4531SDave Airlie }
1446414c4531SDave Airlie 
1447414c4531SDave Airlie static void mga_connector_destroy(struct drm_connector *connector)
1448414c4531SDave Airlie {
1449414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1450414c4531SDave Airlie 	mgag200_i2c_destroy(mga_connector->i2c);
1451414c4531SDave Airlie 	drm_connector_cleanup(connector);
1452414c4531SDave Airlie 	kfree(connector);
1453414c4531SDave Airlie }
1454414c4531SDave Airlie 
1455414c4531SDave Airlie struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
1456414c4531SDave Airlie 	.get_modes = mga_vga_get_modes,
1457414c4531SDave Airlie 	.mode_valid = mga_vga_mode_valid,
1458414c4531SDave Airlie 	.best_encoder = mga_connector_best_encoder,
1459414c4531SDave Airlie };
1460414c4531SDave Airlie 
1461414c4531SDave Airlie struct drm_connector_funcs mga_vga_connector_funcs = {
1462414c4531SDave Airlie 	.dpms = drm_helper_connector_dpms,
1463414c4531SDave Airlie 	.detect = mga_vga_detect,
1464414c4531SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
1465414c4531SDave Airlie 	.destroy = mga_connector_destroy,
1466414c4531SDave Airlie };
1467414c4531SDave Airlie 
1468414c4531SDave Airlie static struct drm_connector *mga_vga_init(struct drm_device *dev)
1469414c4531SDave Airlie {
1470414c4531SDave Airlie 	struct drm_connector *connector;
1471414c4531SDave Airlie 	struct mga_connector *mga_connector;
1472414c4531SDave Airlie 
1473414c4531SDave Airlie 	mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1474414c4531SDave Airlie 	if (!mga_connector)
1475414c4531SDave Airlie 		return NULL;
1476414c4531SDave Airlie 
1477414c4531SDave Airlie 	connector = &mga_connector->base;
1478414c4531SDave Airlie 
1479414c4531SDave Airlie 	drm_connector_init(dev, connector,
1480414c4531SDave Airlie 			   &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1481414c4531SDave Airlie 
1482414c4531SDave Airlie 	drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1483414c4531SDave Airlie 
1484414c4531SDave Airlie 	mga_connector->i2c = mgag200_i2c_create(dev);
1485414c4531SDave Airlie 	if (!mga_connector->i2c)
1486414c4531SDave Airlie 		DRM_ERROR("failed to add ddc bus\n");
1487414c4531SDave Airlie 
1488414c4531SDave Airlie 	return connector;
1489414c4531SDave Airlie }
1490414c4531SDave Airlie 
1491414c4531SDave Airlie 
1492414c4531SDave Airlie int mgag200_modeset_init(struct mga_device *mdev)
1493414c4531SDave Airlie {
1494414c4531SDave Airlie 	struct drm_encoder *encoder;
1495414c4531SDave Airlie 	struct drm_connector *connector;
1496414c4531SDave Airlie 	int ret;
1497414c4531SDave Airlie 
1498414c4531SDave Airlie 	mdev->mode_info.mode_config_initialized = true;
1499414c4531SDave Airlie 
1500414c4531SDave Airlie 	mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1501414c4531SDave Airlie 	mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1502414c4531SDave Airlie 
1503414c4531SDave Airlie 	mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1504414c4531SDave Airlie 
1505414c4531SDave Airlie 	mga_crtc_init(mdev->dev);
1506414c4531SDave Airlie 
1507414c4531SDave Airlie 	encoder = mga_encoder_init(mdev->dev);
1508414c4531SDave Airlie 	if (!encoder) {
1509414c4531SDave Airlie 		DRM_ERROR("mga_encoder_init failed\n");
1510414c4531SDave Airlie 		return -1;
1511414c4531SDave Airlie 	}
1512414c4531SDave Airlie 
1513414c4531SDave Airlie 	connector = mga_vga_init(mdev->dev);
1514414c4531SDave Airlie 	if (!connector) {
1515414c4531SDave Airlie 		DRM_ERROR("mga_vga_init failed\n");
1516414c4531SDave Airlie 		return -1;
1517414c4531SDave Airlie 	}
1518414c4531SDave Airlie 
1519414c4531SDave Airlie 	drm_mode_connector_attach_encoder(connector, encoder);
1520414c4531SDave Airlie 
1521414c4531SDave Airlie 	ret = mgag200_fbdev_init(mdev);
1522414c4531SDave Airlie 	if (ret) {
1523414c4531SDave Airlie 		DRM_ERROR("mga_fbdev_init failed\n");
1524414c4531SDave Airlie 		return ret;
1525414c4531SDave Airlie 	}
1526414c4531SDave Airlie 
1527414c4531SDave Airlie 	return 0;
1528414c4531SDave Airlie }
1529414c4531SDave Airlie 
1530414c4531SDave Airlie void mgag200_modeset_fini(struct mga_device *mdev)
1531414c4531SDave Airlie {
1532414c4531SDave Airlie 
1533414c4531SDave Airlie }
1534