xref: /linux/drivers/gpu/drm/mgag200/mgag200_mode.c (revision f4510a2752b75ad5847b7935b68c233cab497f97)
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>
18414c4531SDave Airlie 
19414c4531SDave Airlie #include "mgag200_drv.h"
20414c4531SDave Airlie 
21414c4531SDave Airlie #define MGAG200_LUT_SIZE 256
22414c4531SDave Airlie 
23414c4531SDave Airlie /*
24414c4531SDave Airlie  * This file contains setup code for the CRTC.
25414c4531SDave Airlie  */
26414c4531SDave Airlie 
27414c4531SDave Airlie static void mga_crtc_load_lut(struct drm_crtc *crtc)
28414c4531SDave Airlie {
29414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
30414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
31414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
32*f4510a27SMatt Roper 	struct drm_framebuffer *fb = crtc->primary->fb;
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 
40de7500eaSEgbert Eich 	if (fb && fb->bits_per_pixel == 16) {
41de7500eaSEgbert Eich 		int inc = (fb->depth == 15) ? 8 : 4;
42de7500eaSEgbert Eich 		u8 r, b;
43de7500eaSEgbert Eich 		for (i = 0; i < MGAG200_LUT_SIZE; i += inc) {
44de7500eaSEgbert Eich 			if (fb->depth == 16) {
45de7500eaSEgbert Eich 				if (i > (MGAG200_LUT_SIZE >> 1)) {
46de7500eaSEgbert Eich 					r = b = 0;
47de7500eaSEgbert Eich 				} else {
48de7500eaSEgbert Eich 					r = mga_crtc->lut_r[i << 1];
49de7500eaSEgbert Eich 					b = mga_crtc->lut_b[i << 1];
50de7500eaSEgbert Eich 				}
51de7500eaSEgbert Eich 			} else {
52de7500eaSEgbert Eich 				r = mga_crtc->lut_r[i];
53de7500eaSEgbert Eich 				b = mga_crtc->lut_b[i];
54de7500eaSEgbert Eich 			}
55de7500eaSEgbert Eich 			/* VGA registers */
56de7500eaSEgbert Eich 			WREG8(DAC_INDEX + MGA1064_COL_PAL, r);
57de7500eaSEgbert Eich 			WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
58de7500eaSEgbert Eich 			WREG8(DAC_INDEX + MGA1064_COL_PAL, b);
59de7500eaSEgbert Eich 		}
60de7500eaSEgbert Eich 		return;
61de7500eaSEgbert Eich 	}
62414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
63414c4531SDave Airlie 		/* VGA registers */
64414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_r[i]);
65414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
66414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_b[i]);
67414c4531SDave Airlie 	}
68414c4531SDave Airlie }
69414c4531SDave Airlie 
70414c4531SDave Airlie static inline void mga_wait_vsync(struct mga_device *mdev)
71414c4531SDave Airlie {
723cdc0e8dSChristopher Harvey 	unsigned long timeout = jiffies + HZ/10;
73414c4531SDave Airlie 	unsigned int status = 0;
74414c4531SDave Airlie 
75414c4531SDave Airlie 	do {
76414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
773cdc0e8dSChristopher Harvey 	} while ((status & 0x08) && time_before(jiffies, timeout));
783cdc0e8dSChristopher Harvey 	timeout = jiffies + HZ/10;
79414c4531SDave Airlie 	status = 0;
80414c4531SDave Airlie 	do {
81414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
823cdc0e8dSChristopher Harvey 	} while (!(status & 0x08) && time_before(jiffies, timeout));
83414c4531SDave Airlie }
84414c4531SDave Airlie 
85414c4531SDave Airlie static inline void mga_wait_busy(struct mga_device *mdev)
86414c4531SDave Airlie {
873cdc0e8dSChristopher Harvey 	unsigned long timeout = jiffies + HZ;
88414c4531SDave Airlie 	unsigned int status = 0;
89414c4531SDave Airlie 	do {
90414c4531SDave Airlie 		status = RREG8(MGAREG_Status + 2);
913cdc0e8dSChristopher Harvey 	} while ((status & 0x01) && time_before(jiffies, timeout));
92414c4531SDave Airlie }
93414c4531SDave Airlie 
94414c4531SDave Airlie /*
95414c4531SDave Airlie  * The core passes the desired mode to the CRTC code to see whether any
96414c4531SDave Airlie  * CRTC-specific modifications need to be made to it. We're in a position
97414c4531SDave Airlie  * to just pass that straight through, so this does nothing
98414c4531SDave Airlie  */
99414c4531SDave Airlie static bool mga_crtc_mode_fixup(struct drm_crtc *crtc,
100e811f5aeSLaurent Pinchart 				const struct drm_display_mode *mode,
101414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
102414c4531SDave Airlie {
103414c4531SDave Airlie 	return true;
104414c4531SDave Airlie }
105414c4531SDave Airlie 
106414c4531SDave Airlie static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
107414c4531SDave Airlie {
108414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
109414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
110414c4531SDave Airlie 	unsigned int testp, testm, testn;
111414c4531SDave Airlie 	unsigned int p, m, n;
112414c4531SDave Airlie 	unsigned int computed;
113414c4531SDave Airlie 
114414c4531SDave Airlie 	m = n = p = 0;
115414c4531SDave Airlie 	vcomax = 320000;
116414c4531SDave Airlie 	vcomin = 160000;
117414c4531SDave Airlie 	pllreffreq = 25000;
118414c4531SDave Airlie 
119414c4531SDave Airlie 	delta = 0xffffffff;
120414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
121414c4531SDave Airlie 
122414c4531SDave Airlie 	for (testp = 8; testp > 0; testp /= 2) {
123414c4531SDave Airlie 		if (clock * testp > vcomax)
124414c4531SDave Airlie 			continue;
125414c4531SDave Airlie 		if (clock * testp < vcomin)
126414c4531SDave Airlie 			continue;
127414c4531SDave Airlie 
128414c4531SDave Airlie 		for (testn = 17; testn < 256; testn++) {
129414c4531SDave Airlie 			for (testm = 1; testm < 32; testm++) {
130414c4531SDave Airlie 				computed = (pllreffreq * testn) /
131414c4531SDave Airlie 					(testm * testp);
132414c4531SDave Airlie 				if (computed > clock)
133414c4531SDave Airlie 					tmpdelta = computed - clock;
134414c4531SDave Airlie 				else
135414c4531SDave Airlie 					tmpdelta = clock - computed;
136414c4531SDave Airlie 				if (tmpdelta < delta) {
137414c4531SDave Airlie 					delta = tmpdelta;
138414c4531SDave Airlie 					m = testm - 1;
139414c4531SDave Airlie 					n = testn - 1;
140414c4531SDave Airlie 					p = testp - 1;
141414c4531SDave Airlie 				}
142414c4531SDave Airlie 			}
143414c4531SDave Airlie 		}
144414c4531SDave Airlie 	}
145414c4531SDave Airlie 
146414c4531SDave Airlie 	if (delta > permitteddelta) {
147414c4531SDave Airlie 		printk(KERN_WARNING "PLL delta too large\n");
148414c4531SDave Airlie 		return 1;
149414c4531SDave Airlie 	}
150414c4531SDave Airlie 
151414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_M, m);
152414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_N, n);
153414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_P, p);
154414c4531SDave Airlie 	return 0;
155414c4531SDave Airlie }
156414c4531SDave Airlie 
157414c4531SDave Airlie static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
158414c4531SDave Airlie {
159414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
160414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
161414c4531SDave Airlie 	unsigned int testp, testm, testn;
162414c4531SDave Airlie 	unsigned int p, m, n;
163414c4531SDave Airlie 	unsigned int computed;
164414c4531SDave Airlie 	int i, j, tmpcount, vcount;
165414c4531SDave Airlie 	bool pll_locked = false;
166414c4531SDave Airlie 	u8 tmp;
167414c4531SDave Airlie 
168414c4531SDave Airlie 	m = n = p = 0;
169414c4531SDave Airlie 	vcomax = 550000;
170414c4531SDave Airlie 	vcomin = 150000;
171414c4531SDave Airlie 	pllreffreq = 48000;
172414c4531SDave Airlie 
173414c4531SDave Airlie 	delta = 0xffffffff;
174414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
175414c4531SDave Airlie 
176414c4531SDave Airlie 	for (testp = 1; testp < 9; testp++) {
177414c4531SDave Airlie 		if (clock * testp > vcomax)
178414c4531SDave Airlie 			continue;
179414c4531SDave Airlie 		if (clock * testp < vcomin)
180414c4531SDave Airlie 			continue;
181414c4531SDave Airlie 
182414c4531SDave Airlie 		for (testm = 1; testm < 17; testm++) {
183414c4531SDave Airlie 			for (testn = 1; testn < 151; testn++) {
184414c4531SDave Airlie 				computed = (pllreffreq * testn) /
185414c4531SDave Airlie 					(testm * testp);
186414c4531SDave Airlie 				if (computed > clock)
187414c4531SDave Airlie 					tmpdelta = computed - clock;
188414c4531SDave Airlie 				else
189414c4531SDave Airlie 					tmpdelta = clock - computed;
190414c4531SDave Airlie 				if (tmpdelta < delta) {
191414c4531SDave Airlie 					delta = tmpdelta;
192414c4531SDave Airlie 					n = testn - 1;
193414c4531SDave Airlie 					m = (testm - 1) | ((n >> 1) & 0x80);
194414c4531SDave Airlie 					p = testp - 1;
195414c4531SDave Airlie 				}
196414c4531SDave Airlie 			}
197414c4531SDave Airlie 		}
198414c4531SDave Airlie 	}
199414c4531SDave Airlie 
200414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
201414c4531SDave Airlie 		if (i > 0) {
202414c4531SDave Airlie 			WREG8(MGAREG_CRTC_INDEX, 0x1e);
203414c4531SDave Airlie 			tmp = RREG8(MGAREG_CRTC_DATA);
204414c4531SDave Airlie 			if (tmp < 0xff)
205414c4531SDave Airlie 				WREG8(MGAREG_CRTC_DATA, tmp+1);
206414c4531SDave Airlie 		}
207414c4531SDave Airlie 
208414c4531SDave Airlie 		/* set pixclkdis to 1 */
209414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
210414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
211414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
212fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
213414c4531SDave Airlie 
214414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
215414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
216414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKDIS;
217fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
218414c4531SDave Airlie 
219414c4531SDave Airlie 		/* select PLL Set C */
220414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
221414c4531SDave Airlie 		tmp |= 0x3 << 2;
222414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
223414c4531SDave Airlie 
224414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
225414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
226414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
227fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
228414c4531SDave Airlie 
229414c4531SDave Airlie 		udelay(500);
230414c4531SDave Airlie 
231414c4531SDave Airlie 		/* reset the PLL */
232414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
233414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
234414c4531SDave Airlie 		tmp &= ~0x04;
235fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
236414c4531SDave Airlie 
237414c4531SDave Airlie 		udelay(50);
238414c4531SDave Airlie 
239414c4531SDave Airlie 		/* program pixel pll register */
240414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
241414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
242414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
243414c4531SDave Airlie 
244414c4531SDave Airlie 		udelay(50);
245414c4531SDave Airlie 
246414c4531SDave Airlie 		/* turn pll on */
247414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
248414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
249414c4531SDave Airlie 		tmp |= 0x04;
250414c4531SDave Airlie 		WREG_DAC(MGA1064_VREF_CTL, tmp);
251414c4531SDave Airlie 
252414c4531SDave Airlie 		udelay(500);
253414c4531SDave Airlie 
254414c4531SDave Airlie 		/* select the pixel pll */
255414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
256414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
257414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
258414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
259fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
260414c4531SDave Airlie 
261414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
262414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
263414c4531SDave Airlie 		tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
264414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
265fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
266414c4531SDave Airlie 
267414c4531SDave Airlie 		/* reset dotclock rate bit */
268414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 1);
269414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
270414c4531SDave Airlie 		tmp &= ~0x8;
271414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, tmp);
272414c4531SDave Airlie 
273414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
274414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
275414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
276fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
277414c4531SDave Airlie 
278414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
279414c4531SDave Airlie 
280414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
281414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
282414c4531SDave Airlie 			if (tmpcount < vcount)
283414c4531SDave Airlie 				vcount = 0;
284414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
285414c4531SDave Airlie 				pll_locked = true;
286414c4531SDave Airlie 			else
287414c4531SDave Airlie 				udelay(5);
288414c4531SDave Airlie 		}
289414c4531SDave Airlie 	}
290414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
291414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
292414c4531SDave Airlie 	tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
293414c4531SDave Airlie 	WREG_DAC(MGA1064_REMHEADCTL, tmp);
294414c4531SDave Airlie 	return 0;
295414c4531SDave Airlie }
296414c4531SDave Airlie 
297414c4531SDave Airlie static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
298414c4531SDave Airlie {
299414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
300414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
301414c4531SDave Airlie 	unsigned int testp, testm, testn;
302414c4531SDave Airlie 	unsigned int p, m, n;
303414c4531SDave Airlie 	unsigned int computed;
304414c4531SDave Airlie 	u8 tmp;
305414c4531SDave Airlie 
306414c4531SDave Airlie 	m = n = p = 0;
307414c4531SDave Airlie 	vcomax = 550000;
308414c4531SDave Airlie 	vcomin = 150000;
309414c4531SDave Airlie 	pllreffreq = 50000;
310414c4531SDave Airlie 
311414c4531SDave Airlie 	delta = 0xffffffff;
312414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
313414c4531SDave Airlie 
314414c4531SDave Airlie 	for (testp = 16; testp > 0; testp--) {
315414c4531SDave Airlie 		if (clock * testp > vcomax)
316414c4531SDave Airlie 			continue;
317414c4531SDave Airlie 		if (clock * testp < vcomin)
318414c4531SDave Airlie 			continue;
319414c4531SDave Airlie 
320414c4531SDave Airlie 		for (testn = 1; testn < 257; testn++) {
321414c4531SDave Airlie 			for (testm = 1; testm < 17; testm++) {
322414c4531SDave Airlie 				computed = (pllreffreq * testn) /
323414c4531SDave Airlie 					(testm * testp);
324414c4531SDave Airlie 				if (computed > clock)
325414c4531SDave Airlie 					tmpdelta = computed - clock;
326414c4531SDave Airlie 				else
327414c4531SDave Airlie 					tmpdelta = clock - computed;
328414c4531SDave Airlie 				if (tmpdelta < delta) {
329414c4531SDave Airlie 					delta = tmpdelta;
330414c4531SDave Airlie 					n = testn - 1;
331414c4531SDave Airlie 					m = testm - 1;
332414c4531SDave Airlie 					p = testp - 1;
333414c4531SDave Airlie 				}
334414c4531SDave Airlie 			}
335414c4531SDave Airlie 		}
336414c4531SDave Airlie 	}
337414c4531SDave Airlie 
338414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
339414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
340414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
341fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
342414c4531SDave Airlie 
343414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
344414c4531SDave Airlie 	tmp |= 0x3 << 2;
345414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
346414c4531SDave Airlie 
347414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
348414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
349fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp & ~0x40);
350414c4531SDave Airlie 
351414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
352414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
353414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
354fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
355414c4531SDave Airlie 
356414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
357414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
358414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
359414c4531SDave Airlie 
360414c4531SDave Airlie 	udelay(50);
361414c4531SDave Airlie 
362414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
363414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
364414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
365fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
366414c4531SDave Airlie 
367414c4531SDave Airlie 	udelay(500);
368414c4531SDave Airlie 
369414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
370414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
371414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
372414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
373fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
374414c4531SDave Airlie 
375414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
376414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
377fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp | 0x40);
378414c4531SDave Airlie 
379414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
380414c4531SDave Airlie 	tmp |= (0x3 << 2);
381414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
382414c4531SDave Airlie 
383414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
384414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
385414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
386fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
387414c4531SDave Airlie 
388414c4531SDave Airlie 	return 0;
389414c4531SDave Airlie }
390414c4531SDave Airlie 
391414c4531SDave Airlie static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
392414c4531SDave Airlie {
393414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
394414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
395414c4531SDave Airlie 	unsigned int testp, testm, testn;
396414c4531SDave Airlie 	unsigned int p, m, n;
397414c4531SDave Airlie 	unsigned int computed;
398414c4531SDave Airlie 	int i, j, tmpcount, vcount;
399414c4531SDave Airlie 	u8 tmp;
400414c4531SDave Airlie 	bool pll_locked = false;
401414c4531SDave Airlie 
402414c4531SDave Airlie 	m = n = p = 0;
403414c4531SDave Airlie 	vcomax = 800000;
404414c4531SDave Airlie 	vcomin = 400000;
405260b3f12SJulia Lemire 	pllreffreq = 33333;
406414c4531SDave Airlie 
407414c4531SDave Airlie 	delta = 0xffffffff;
408414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
409414c4531SDave Airlie 
410260b3f12SJulia Lemire 	for (testp = 16; testp > 0; testp >>= 1) {
411414c4531SDave Airlie 		if (clock * testp > vcomax)
412414c4531SDave Airlie 			continue;
413414c4531SDave Airlie 		if (clock * testp < vcomin)
414414c4531SDave Airlie 			continue;
415414c4531SDave Airlie 
416414c4531SDave Airlie 		for (testm = 1; testm < 33; testm++) {
417260b3f12SJulia Lemire 			for (testn = 17; testn < 257; testn++) {
418414c4531SDave Airlie 				computed = (pllreffreq * testn) /
419414c4531SDave Airlie 					(testm * testp);
420414c4531SDave Airlie 				if (computed > clock)
421414c4531SDave Airlie 					tmpdelta = computed - clock;
422414c4531SDave Airlie 				else
423414c4531SDave Airlie 					tmpdelta = clock - computed;
424414c4531SDave Airlie 				if (tmpdelta < delta) {
425414c4531SDave Airlie 					delta = tmpdelta;
426414c4531SDave Airlie 					n = testn - 1;
427260b3f12SJulia Lemire 					m = (testm - 1);
428414c4531SDave Airlie 					p = testp - 1;
429414c4531SDave Airlie 				}
430414c4531SDave Airlie 				if ((clock * testp) >= 600000)
431260b3f12SJulia Lemire 					p |= 0x80;
432414c4531SDave Airlie 			}
433414c4531SDave Airlie 		}
434414c4531SDave Airlie 	}
435414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
436414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
437414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
438414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
439fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
440414c4531SDave Airlie 
441414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
442414c4531SDave Airlie 		tmp |= 0x3 << 2;
443414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, 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_POW_DOWN;
448fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
449414c4531SDave Airlie 
450414c4531SDave Airlie 		udelay(500);
451414c4531SDave Airlie 
452414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
453414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
454414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
455414c4531SDave Airlie 
456414c4531SDave Airlie 		udelay(500);
457414c4531SDave Airlie 
458414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
459414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
460414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
461414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
462fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
463414c4531SDave Airlie 
464414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
465414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
466414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
467414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
468fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
469414c4531SDave Airlie 
470414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
471414c4531SDave Airlie 
472414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
473414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
474414c4531SDave Airlie 			if (tmpcount < vcount)
475414c4531SDave Airlie 				vcount = 0;
476414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
477414c4531SDave Airlie 				pll_locked = true;
478414c4531SDave Airlie 			else
479414c4531SDave Airlie 				udelay(5);
480414c4531SDave Airlie 		}
481414c4531SDave Airlie 	}
482414c4531SDave Airlie 
483414c4531SDave Airlie 	return 0;
484414c4531SDave Airlie }
485414c4531SDave Airlie 
486414c4531SDave Airlie static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
487414c4531SDave Airlie {
488414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
489414c4531SDave Airlie 	unsigned int delta, tmpdelta;
4909830605dSDave Airlie 	int testr, testn, testm, testo;
491414c4531SDave Airlie 	unsigned int p, m, n;
4929830605dSDave Airlie 	unsigned int computed, vco;
493414c4531SDave Airlie 	int tmp;
4949830605dSDave Airlie 	const unsigned int m_div_val[] = { 1, 2, 4, 8 };
495414c4531SDave Airlie 
496414c4531SDave Airlie 	m = n = p = 0;
497414c4531SDave Airlie 	vcomax = 1488000;
498414c4531SDave Airlie 	vcomin = 1056000;
499414c4531SDave Airlie 	pllreffreq = 48000;
500414c4531SDave Airlie 
501414c4531SDave Airlie 	delta = 0xffffffff;
502414c4531SDave Airlie 
503414c4531SDave Airlie 	for (testr = 0; testr < 4; testr++) {
504414c4531SDave Airlie 		if (delta == 0)
505414c4531SDave Airlie 			break;
506414c4531SDave Airlie 		for (testn = 5; testn < 129; testn++) {
507414c4531SDave Airlie 			if (delta == 0)
508414c4531SDave Airlie 				break;
509414c4531SDave Airlie 			for (testm = 3; testm >= 0; testm--) {
510414c4531SDave Airlie 				if (delta == 0)
511414c4531SDave Airlie 					break;
512414c4531SDave Airlie 				for (testo = 5; testo < 33; testo++) {
5139830605dSDave Airlie 					vco = pllreffreq * (testn + 1) /
514414c4531SDave Airlie 						(testr + 1);
5159830605dSDave Airlie 					if (vco < vcomin)
516414c4531SDave Airlie 						continue;
5179830605dSDave Airlie 					if (vco > vcomax)
518414c4531SDave Airlie 						continue;
5199830605dSDave Airlie 					computed = vco / (m_div_val[testm] * (testo + 1));
520414c4531SDave Airlie 					if (computed > clock)
521414c4531SDave Airlie 						tmpdelta = computed - clock;
522414c4531SDave Airlie 					else
523414c4531SDave Airlie 						tmpdelta = clock - computed;
524414c4531SDave Airlie 					if (tmpdelta < delta) {
525414c4531SDave Airlie 						delta = tmpdelta;
526414c4531SDave Airlie 						m = testm | (testo << 3);
527414c4531SDave Airlie 						n = testn;
528414c4531SDave Airlie 						p = testr | (testr << 3);
529414c4531SDave Airlie 					}
530414c4531SDave Airlie 				}
531414c4531SDave Airlie 			}
532414c4531SDave Airlie 		}
533414c4531SDave Airlie 	}
534414c4531SDave Airlie 
535414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
536414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
537414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
538fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
539414c4531SDave Airlie 
540414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
541414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
542414c4531SDave Airlie 	tmp |= MGA1064_REMHEADCTL_CLKDIS;
543fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
544414c4531SDave Airlie 
545414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
546414c4531SDave Airlie 	tmp |= (0x3<<2) | 0xc0;
547414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
548414c4531SDave Airlie 
549414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
550414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
551414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
552414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
553fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
554414c4531SDave Airlie 
555414c4531SDave Airlie 	udelay(500);
556414c4531SDave Airlie 
557414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
558414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
559414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
560414c4531SDave Airlie 
561414c4531SDave Airlie 	udelay(50);
562414c4531SDave Airlie 
563414c4531SDave Airlie 	return 0;
564414c4531SDave Airlie }
565414c4531SDave Airlie 
566414c4531SDave Airlie static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
567414c4531SDave Airlie {
568414c4531SDave Airlie 	switch(mdev->type) {
569414c4531SDave Airlie 	case G200_SE_A:
570414c4531SDave Airlie 	case G200_SE_B:
571414c4531SDave Airlie 		return mga_g200se_set_plls(mdev, clock);
572414c4531SDave Airlie 		break;
573414c4531SDave Airlie 	case G200_WB:
574414c4531SDave Airlie 		return mga_g200wb_set_plls(mdev, clock);
575414c4531SDave Airlie 		break;
576414c4531SDave Airlie 	case G200_EV:
577414c4531SDave Airlie 		return mga_g200ev_set_plls(mdev, clock);
578414c4531SDave Airlie 		break;
579414c4531SDave Airlie 	case G200_EH:
580414c4531SDave Airlie 		return mga_g200eh_set_plls(mdev, clock);
581414c4531SDave Airlie 		break;
582414c4531SDave Airlie 	case G200_ER:
583414c4531SDave Airlie 		return mga_g200er_set_plls(mdev, clock);
584414c4531SDave Airlie 		break;
585414c4531SDave Airlie 	}
586414c4531SDave Airlie 	return 0;
587414c4531SDave Airlie }
588414c4531SDave Airlie 
589414c4531SDave Airlie static void mga_g200wb_prepare(struct drm_crtc *crtc)
590414c4531SDave Airlie {
591414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
592414c4531SDave Airlie 	u8 tmp;
593414c4531SDave Airlie 	int iter_max;
594414c4531SDave Airlie 
595414c4531SDave Airlie 	/* 1- The first step is to warn the BMC of an upcoming mode change.
596414c4531SDave Airlie 	 * We are putting the misc<0> to output.*/
597414c4531SDave Airlie 
598414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
599414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
600414c4531SDave Airlie 	tmp |= 0x10;
601414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
602414c4531SDave Airlie 
603414c4531SDave Airlie 	/* we are putting a 1 on the misc<0> line */
604414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
605414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
606414c4531SDave Airlie 	tmp |= 0x10;
607414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
608414c4531SDave Airlie 
609414c4531SDave Airlie 	/* 2- Second step to mask and further scan request
610414c4531SDave Airlie 	 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
611414c4531SDave Airlie 	 */
612414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
613414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
614414c4531SDave Airlie 	tmp |= 0x80;
615414c4531SDave Airlie 	WREG_DAC(MGA1064_SPAREREG, tmp);
616414c4531SDave Airlie 
617414c4531SDave Airlie 	/* 3a- the third step is to verifu if there is an active scan
618414c4531SDave Airlie 	 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
619414c4531SDave Airlie 	 */
620414c4531SDave Airlie 	iter_max = 300;
621414c4531SDave Airlie 	while (!(tmp & 0x1) && iter_max) {
622414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_SPAREREG);
623414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
624414c4531SDave Airlie 		udelay(1000);
625414c4531SDave Airlie 		iter_max--;
626414c4531SDave Airlie 	}
627414c4531SDave Airlie 
628414c4531SDave Airlie 	/* 3b- this step occurs only if the remove is actually scanning
629414c4531SDave Airlie 	 * we are waiting for the end of the frame which is a 1 on
630414c4531SDave Airlie 	 * remvsyncsts (XSPAREREG<1>)
631414c4531SDave Airlie 	 */
632414c4531SDave Airlie 	if (iter_max) {
633414c4531SDave Airlie 		iter_max = 300;
634414c4531SDave Airlie 		while ((tmp & 0x2) && iter_max) {
635414c4531SDave Airlie 			WREG8(DAC_INDEX, MGA1064_SPAREREG);
636414c4531SDave Airlie 			tmp = RREG8(DAC_DATA);
637414c4531SDave Airlie 			udelay(1000);
638414c4531SDave Airlie 			iter_max--;
639414c4531SDave Airlie 		}
640414c4531SDave Airlie 	}
641414c4531SDave Airlie }
642414c4531SDave Airlie 
643414c4531SDave Airlie static void mga_g200wb_commit(struct drm_crtc *crtc)
644414c4531SDave Airlie {
645414c4531SDave Airlie 	u8 tmp;
646414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
647414c4531SDave Airlie 
648414c4531SDave Airlie 	/* 1- The first step is to ensure that the vrsten and hrsten are set */
649414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 1);
650414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTCEXT_DATA);
651414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
652414c4531SDave Airlie 
653414c4531SDave Airlie 	/* 2- second step is to assert the rstlvl2 */
654414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
655414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
656414c4531SDave Airlie 	tmp |= 0x8;
657414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
658414c4531SDave Airlie 
659414c4531SDave Airlie 	/* wait 10 us */
660414c4531SDave Airlie 	udelay(10);
661414c4531SDave Airlie 
662414c4531SDave Airlie 	/* 3- deassert rstlvl2 */
663414c4531SDave Airlie 	tmp &= ~0x08;
664414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
665414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
666414c4531SDave Airlie 
667414c4531SDave Airlie 	/* 4- remove mask of scan request */
668414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
669414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
670414c4531SDave Airlie 	tmp &= ~0x80;
671414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
672414c4531SDave Airlie 
673414c4531SDave Airlie 	/* 5- put back a 0 on the misc<0> line */
674414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
675414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
676414c4531SDave Airlie 	tmp &= ~0x10;
677414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
678414c4531SDave Airlie }
679414c4531SDave Airlie 
6809f1d0366SChristopher Harvey /*
6819f1d0366SChristopher Harvey    This is how the framebuffer base address is stored in g200 cards:
6829f1d0366SChristopher Harvey    * Assume @offset is the gpu_addr variable of the framebuffer object
6839f1d0366SChristopher Harvey    * Then addr is the number of _pixels_ (not bytes) from the start of
6849f1d0366SChristopher Harvey      VRAM to the first pixel we want to display. (divided by 2 for 32bit
6859f1d0366SChristopher Harvey      framebuffers)
6869f1d0366SChristopher Harvey    * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
6879f1d0366SChristopher Harvey    addr<20> -> CRTCEXT0<6>
6889f1d0366SChristopher Harvey    addr<19-16> -> CRTCEXT0<3-0>
6899f1d0366SChristopher Harvey    addr<15-8> -> CRTCC<7-0>
6909f1d0366SChristopher Harvey    addr<7-0> -> CRTCD<7-0>
6919f1d0366SChristopher Harvey    CRTCEXT0 has to be programmed last to trigger an update and make the
6929f1d0366SChristopher Harvey    new addr variable take effect.
6939f1d0366SChristopher Harvey  */
694080fd6b5SRashika static void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
695414c4531SDave Airlie {
696414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
697414c4531SDave Airlie 	u32 addr;
698414c4531SDave Airlie 	int count;
6999f1d0366SChristopher Harvey 	u8 crtcext0;
700414c4531SDave Airlie 
701414c4531SDave Airlie 	while (RREG8(0x1fda) & 0x08);
702414c4531SDave Airlie 	while (!(RREG8(0x1fda) & 0x08));
703414c4531SDave Airlie 
704414c4531SDave Airlie 	count = RREG8(MGAREG_VCOUNT) + 2;
705414c4531SDave Airlie 	while (RREG8(MGAREG_VCOUNT) < count);
706414c4531SDave Airlie 
7079f1d0366SChristopher Harvey 	WREG8(MGAREG_CRTCEXT_INDEX, 0);
7089f1d0366SChristopher Harvey 	crtcext0 = RREG8(MGAREG_CRTCEXT_DATA);
7099f1d0366SChristopher Harvey 	crtcext0 &= 0xB0;
7109f1d0366SChristopher Harvey 	addr = offset / 8;
7119f1d0366SChristopher Harvey 	/* Can't store addresses any higher than that...
7129f1d0366SChristopher Harvey 	   but we also don't have more than 16MB of memory, so it should be fine. */
7139f1d0366SChristopher Harvey 	WARN_ON(addr > 0x1fffff);
7149f1d0366SChristopher Harvey 	crtcext0 |= (!!(addr & (1<<20)))<<6;
715414c4531SDave Airlie 	WREG_CRT(0x0d, (u8)(addr & 0xff));
716414c4531SDave Airlie 	WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
7179f1d0366SChristopher Harvey 	WREG_ECRT(0x0, ((u8)(addr >> 16) & 0xf) | crtcext0);
718414c4531SDave Airlie }
719414c4531SDave Airlie 
720414c4531SDave Airlie 
721414c4531SDave Airlie /* ast is different - we will force move buffers out of VRAM */
722414c4531SDave Airlie static int mga_crtc_do_set_base(struct drm_crtc *crtc,
723414c4531SDave Airlie 				struct drm_framebuffer *fb,
724414c4531SDave Airlie 				int x, int y, int atomic)
725414c4531SDave Airlie {
726414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
727414c4531SDave Airlie 	struct drm_gem_object *obj;
728414c4531SDave Airlie 	struct mga_framebuffer *mga_fb;
729414c4531SDave Airlie 	struct mgag200_bo *bo;
730414c4531SDave Airlie 	int ret;
731414c4531SDave Airlie 	u64 gpu_addr;
732414c4531SDave Airlie 
733414c4531SDave Airlie 	/* push the previous fb to system ram */
734414c4531SDave Airlie 	if (!atomic && fb) {
735414c4531SDave Airlie 		mga_fb = to_mga_framebuffer(fb);
736414c4531SDave Airlie 		obj = mga_fb->obj;
737414c4531SDave Airlie 		bo = gem_to_mga_bo(obj);
738414c4531SDave Airlie 		ret = mgag200_bo_reserve(bo, false);
739414c4531SDave Airlie 		if (ret)
740414c4531SDave Airlie 			return ret;
741414c4531SDave Airlie 		mgag200_bo_push_sysram(bo);
742414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
743414c4531SDave Airlie 	}
744414c4531SDave Airlie 
745*f4510a27SMatt Roper 	mga_fb = to_mga_framebuffer(crtc->primary->fb);
746414c4531SDave Airlie 	obj = mga_fb->obj;
747414c4531SDave Airlie 	bo = gem_to_mga_bo(obj);
748414c4531SDave Airlie 
749414c4531SDave Airlie 	ret = mgag200_bo_reserve(bo, false);
750414c4531SDave Airlie 	if (ret)
751414c4531SDave Airlie 		return ret;
752414c4531SDave Airlie 
753414c4531SDave Airlie 	ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
754414c4531SDave Airlie 	if (ret) {
755414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
756414c4531SDave Airlie 		return ret;
757414c4531SDave Airlie 	}
758414c4531SDave Airlie 
759414c4531SDave Airlie 	if (&mdev->mfbdev->mfb == mga_fb) {
760414c4531SDave Airlie 		/* if pushing console in kmap it */
761414c4531SDave Airlie 		ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
762414c4531SDave Airlie 		if (ret)
763414c4531SDave Airlie 			DRM_ERROR("failed to kmap fbcon\n");
764414c4531SDave Airlie 
765414c4531SDave Airlie 	}
766414c4531SDave Airlie 	mgag200_bo_unreserve(bo);
767414c4531SDave Airlie 
768414c4531SDave Airlie 	mga_set_start_address(crtc, (u32)gpu_addr);
769414c4531SDave Airlie 
770414c4531SDave Airlie 	return 0;
771414c4531SDave Airlie }
772414c4531SDave Airlie 
773414c4531SDave Airlie static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
774414c4531SDave Airlie 				  struct drm_framebuffer *old_fb)
775414c4531SDave Airlie {
776414c4531SDave Airlie 	return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
777414c4531SDave Airlie }
778414c4531SDave Airlie 
779414c4531SDave Airlie static int mga_crtc_mode_set(struct drm_crtc *crtc,
780414c4531SDave Airlie 				struct drm_display_mode *mode,
781414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode,
782414c4531SDave Airlie 				int x, int y, struct drm_framebuffer *old_fb)
783414c4531SDave Airlie {
784414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
785414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
786414c4531SDave Airlie 	int hdisplay, hsyncstart, hsyncend, htotal;
787414c4531SDave Airlie 	int vdisplay, vsyncstart, vsyncend, vtotal;
788414c4531SDave Airlie 	int pitch;
789414c4531SDave Airlie 	int option = 0, option2 = 0;
790414c4531SDave Airlie 	int i;
791414c4531SDave Airlie 	unsigned char misc = 0;
792414c4531SDave Airlie 	unsigned char ext_vga[6];
793414c4531SDave Airlie 	u8 bppshift;
794414c4531SDave Airlie 
795414c4531SDave Airlie 	static unsigned char dacvalue[] = {
796414c4531SDave Airlie 		/* 0x00: */        0,    0,    0,    0,    0,    0, 0x00,    0,
797414c4531SDave Airlie 		/* 0x08: */        0,    0,    0,    0,    0,    0,    0,    0,
798414c4531SDave Airlie 		/* 0x10: */        0,    0,    0,    0,    0,    0,    0,    0,
799414c4531SDave Airlie 		/* 0x18: */     0x00,    0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
800414c4531SDave Airlie 		/* 0x20: */     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
801414c4531SDave Airlie 		/* 0x28: */     0x00, 0x00, 0x00, 0x00,    0,    0,    0, 0x40,
802414c4531SDave Airlie 		/* 0x30: */     0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
803414c4531SDave Airlie 		/* 0x38: */     0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
804414c4531SDave Airlie 		/* 0x40: */        0,    0,    0,    0,    0,    0,    0,    0,
805414c4531SDave Airlie 		/* 0x48: */        0,    0,    0,    0,    0,    0,    0,    0
806414c4531SDave Airlie 	};
807414c4531SDave Airlie 
808*f4510a27SMatt Roper 	bppshift = mdev->bpp_shifts[(crtc->primary->fb->bits_per_pixel >> 3) - 1];
809414c4531SDave Airlie 
810414c4531SDave Airlie 	switch (mdev->type) {
811414c4531SDave Airlie 	case G200_SE_A:
812414c4531SDave Airlie 	case G200_SE_B:
813414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x03;
814414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
815414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
816414c4531SDave Airlie 					     MGA1064_MISC_CTL_VGA8 |
817414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
818414c4531SDave Airlie 		if (mdev->has_sdram)
819414c4531SDave Airlie 			option = 0x40049120;
820414c4531SDave Airlie 		else
821414c4531SDave Airlie 			option = 0x4004d120;
822414c4531SDave Airlie 		option2 = 0x00008000;
823414c4531SDave Airlie 		break;
824414c4531SDave Airlie 	case G200_WB:
825414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x07;
826414c4531SDave Airlie 		option = 0x41049120;
827414c4531SDave Airlie 		option2 = 0x0000b000;
828414c4531SDave Airlie 		break;
829414c4531SDave Airlie 	case G200_EV:
830414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
831414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
832414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
833414c4531SDave Airlie 		option = 0x00000120;
834414c4531SDave Airlie 		option2 = 0x0000b000;
835414c4531SDave Airlie 		break;
836414c4531SDave Airlie 	case G200_EH:
837414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
838414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
839414c4531SDave Airlie 		option = 0x00000120;
840414c4531SDave Airlie 		option2 = 0x0000b000;
841414c4531SDave Airlie 		break;
842414c4531SDave Airlie 	case G200_ER:
843414c4531SDave Airlie 		break;
844414c4531SDave Airlie 	}
845414c4531SDave Airlie 
846*f4510a27SMatt Roper 	switch (crtc->primary->fb->bits_per_pixel) {
847414c4531SDave Airlie 	case 8:
848414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
849414c4531SDave Airlie 		break;
850414c4531SDave Airlie 	case 16:
851*f4510a27SMatt Roper 		if (crtc->primary->fb->depth == 15)
852414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
853414c4531SDave Airlie 		else
854414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
855414c4531SDave Airlie 		break;
856414c4531SDave Airlie 	case 24:
857414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
858414c4531SDave Airlie 		break;
859414c4531SDave Airlie 	case 32:
860414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
861414c4531SDave Airlie 		break;
862414c4531SDave Airlie 	}
863414c4531SDave Airlie 
864414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
865414c4531SDave Airlie 		misc |= 0x40;
866414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
867414c4531SDave Airlie 		misc |= 0x80;
868414c4531SDave Airlie 
869414c4531SDave Airlie 
870414c4531SDave Airlie 	for (i = 0; i < sizeof(dacvalue); i++) {
8719d8aa55fSChristopher Harvey 		if ((i <= 0x17) ||
872414c4531SDave Airlie 		    (i == 0x1b) ||
873414c4531SDave Airlie 		    (i == 0x1c) ||
874414c4531SDave Airlie 		    ((i >= 0x1f) && (i <= 0x29)) ||
875414c4531SDave Airlie 		    ((i >= 0x30) && (i <= 0x37)))
876414c4531SDave Airlie 			continue;
877414c4531SDave Airlie 		if (IS_G200_SE(mdev) &&
878414c4531SDave Airlie 		    ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
879414c4531SDave Airlie 			continue;
880414c4531SDave Airlie 		if ((mdev->type == G200_EV || mdev->type == G200_WB || mdev->type == G200_EH) &&
881414c4531SDave Airlie 		    (i >= 0x44) && (i <= 0x4e))
882414c4531SDave Airlie 			continue;
883414c4531SDave Airlie 
884414c4531SDave Airlie 		WREG_DAC(i, dacvalue[i]);
885414c4531SDave Airlie 	}
886414c4531SDave Airlie 
8871812a3dbSChristopher Harvey 	if (mdev->type == G200_ER)
8881812a3dbSChristopher Harvey 		WREG_DAC(0x90, 0);
889414c4531SDave Airlie 
890414c4531SDave Airlie 	if (option)
891414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
892414c4531SDave Airlie 	if (option2)
893414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
894414c4531SDave Airlie 
895414c4531SDave Airlie 	WREG_SEQ(2, 0xf);
896414c4531SDave Airlie 	WREG_SEQ(3, 0);
897414c4531SDave Airlie 	WREG_SEQ(4, 0xe);
898414c4531SDave Airlie 
899*f4510a27SMatt Roper 	pitch = crtc->primary->fb->pitches[0] / (crtc->primary->fb->bits_per_pixel / 8);
900*f4510a27SMatt Roper 	if (crtc->primary->fb->bits_per_pixel == 24)
901da558398STakashi Iwai 		pitch = (pitch * 3) >> (4 - bppshift);
902414c4531SDave Airlie 	else
903414c4531SDave Airlie 		pitch = pitch >> (4 - bppshift);
904414c4531SDave Airlie 
905414c4531SDave Airlie 	hdisplay = mode->hdisplay / 8 - 1;
906414c4531SDave Airlie 	hsyncstart = mode->hsync_start / 8 - 1;
907414c4531SDave Airlie 	hsyncend = mode->hsync_end / 8 - 1;
908414c4531SDave Airlie 	htotal = mode->htotal / 8 - 1;
909414c4531SDave Airlie 
910414c4531SDave Airlie 	/* Work around hardware quirk */
911414c4531SDave Airlie 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
912414c4531SDave Airlie 		htotal++;
913414c4531SDave Airlie 
914414c4531SDave Airlie 	vdisplay = mode->vdisplay - 1;
915414c4531SDave Airlie 	vsyncstart = mode->vsync_start - 1;
916414c4531SDave Airlie 	vsyncend = mode->vsync_end - 1;
917414c4531SDave Airlie 	vtotal = mode->vtotal - 2;
918414c4531SDave Airlie 
919414c4531SDave Airlie 	WREG_GFX(0, 0);
920414c4531SDave Airlie 	WREG_GFX(1, 0);
921414c4531SDave Airlie 	WREG_GFX(2, 0);
922414c4531SDave Airlie 	WREG_GFX(3, 0);
923414c4531SDave Airlie 	WREG_GFX(4, 0);
924414c4531SDave Airlie 	WREG_GFX(5, 0x40);
925414c4531SDave Airlie 	WREG_GFX(6, 0x5);
926414c4531SDave Airlie 	WREG_GFX(7, 0xf);
927414c4531SDave Airlie 	WREG_GFX(8, 0xf);
928414c4531SDave Airlie 
929414c4531SDave Airlie 	WREG_CRT(0, htotal - 4);
930414c4531SDave Airlie 	WREG_CRT(1, hdisplay);
931414c4531SDave Airlie 	WREG_CRT(2, hdisplay);
932414c4531SDave Airlie 	WREG_CRT(3, (htotal & 0x1F) | 0x80);
933414c4531SDave Airlie 	WREG_CRT(4, hsyncstart);
934414c4531SDave Airlie 	WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
935414c4531SDave Airlie 	WREG_CRT(6, vtotal & 0xFF);
936414c4531SDave Airlie 	WREG_CRT(7, ((vtotal & 0x100) >> 8) |
937414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 7) |
938414c4531SDave Airlie 		 ((vsyncstart & 0x100) >> 6) |
939414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 5) |
940414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 4) | /* linecomp */
941414c4531SDave Airlie 		 ((vtotal & 0x200) >> 4)|
942414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3) |
943414c4531SDave Airlie 		 ((vsyncstart & 0x200) >> 2));
944414c4531SDave Airlie 	WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
945414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3));
946414c4531SDave Airlie 	WREG_CRT(10, 0);
947414c4531SDave Airlie 	WREG_CRT(11, 0);
948414c4531SDave Airlie 	WREG_CRT(12, 0);
949414c4531SDave Airlie 	WREG_CRT(13, 0);
950414c4531SDave Airlie 	WREG_CRT(14, 0);
951414c4531SDave Airlie 	WREG_CRT(15, 0);
952414c4531SDave Airlie 	WREG_CRT(16, vsyncstart & 0xFF);
953414c4531SDave Airlie 	WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
954414c4531SDave Airlie 	WREG_CRT(18, vdisplay & 0xFF);
955414c4531SDave Airlie 	WREG_CRT(19, pitch & 0xFF);
956414c4531SDave Airlie 	WREG_CRT(20, 0);
957414c4531SDave Airlie 	WREG_CRT(21, vdisplay & 0xFF);
958414c4531SDave Airlie 	WREG_CRT(22, (vtotal + 1) & 0xFF);
959414c4531SDave Airlie 	WREG_CRT(23, 0xc3);
960414c4531SDave Airlie 	WREG_CRT(24, vdisplay & 0xFF);
961414c4531SDave Airlie 
962414c4531SDave Airlie 	ext_vga[0] = 0;
963414c4531SDave Airlie 	ext_vga[5] = 0;
964414c4531SDave Airlie 
965414c4531SDave Airlie 	/* TODO interlace */
966414c4531SDave Airlie 
967414c4531SDave Airlie 	ext_vga[0] |= (pitch & 0x300) >> 4;
968414c4531SDave Airlie 	ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
969414c4531SDave Airlie 		((hdisplay & 0x100) >> 7) |
970414c4531SDave Airlie 		((hsyncstart & 0x100) >> 6) |
971414c4531SDave Airlie 		(htotal & 0x40);
972414c4531SDave Airlie 	ext_vga[2] = ((vtotal & 0xc00) >> 10) |
973414c4531SDave Airlie 		((vdisplay & 0x400) >> 8) |
974414c4531SDave Airlie 		((vdisplay & 0xc00) >> 7) |
975414c4531SDave Airlie 		((vsyncstart & 0xc00) >> 5) |
976414c4531SDave Airlie 		((vdisplay & 0x400) >> 3);
977*f4510a27SMatt Roper 	if (crtc->primary->fb->bits_per_pixel == 24)
978414c4531SDave Airlie 		ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
979414c4531SDave Airlie 	else
980414c4531SDave Airlie 		ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
981414c4531SDave Airlie 	ext_vga[4] = 0;
982414c4531SDave Airlie 	if (mdev->type == G200_WB)
983414c4531SDave Airlie 		ext_vga[1] |= 0x88;
984414c4531SDave Airlie 
985414c4531SDave Airlie 	/* Set pixel clocks */
986414c4531SDave Airlie 	misc = 0x2d;
987414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
988414c4531SDave Airlie 
989414c4531SDave Airlie 	mga_crtc_set_plls(mdev, mode->clock);
990414c4531SDave Airlie 
991414c4531SDave Airlie 	for (i = 0; i < 6; i++) {
992414c4531SDave Airlie 		WREG_ECRT(i, ext_vga[i]);
993414c4531SDave Airlie 	}
994414c4531SDave Airlie 
995414c4531SDave Airlie 	if (mdev->type == G200_ER)
9961812a3dbSChristopher Harvey 		WREG_ECRT(0x24, 0x5);
997414c4531SDave Airlie 
998414c4531SDave Airlie 	if (mdev->type == G200_EV) {
999414c4531SDave Airlie 		WREG_ECRT(6, 0);
1000414c4531SDave Airlie 	}
1001414c4531SDave Airlie 
1002414c4531SDave Airlie 	WREG_ECRT(0, ext_vga[0]);
1003414c4531SDave Airlie 	/* Enable mga pixel clock */
1004414c4531SDave Airlie 	misc = 0x2d;
1005414c4531SDave Airlie 
1006414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
1007414c4531SDave Airlie 
1008414c4531SDave Airlie 	if (adjusted_mode)
1009414c4531SDave Airlie 		memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
1010414c4531SDave Airlie 
1011414c4531SDave Airlie 	mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
1012414c4531SDave Airlie 
1013414c4531SDave Airlie 	/* reset tagfifo */
1014414c4531SDave Airlie 	if (mdev->type == G200_ER) {
1015414c4531SDave Airlie 		u32 mem_ctl = RREG32(MGAREG_MEMCTL);
1016414c4531SDave Airlie 		u8 seq1;
1017414c4531SDave Airlie 
1018414c4531SDave Airlie 		/* screen off */
1019414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x01);
1020414c4531SDave Airlie 		seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
1021414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1);
1022414c4531SDave Airlie 
1023414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
1024414c4531SDave Airlie 		udelay(1000);
1025414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
1026414c4531SDave Airlie 
1027414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
1028414c4531SDave Airlie 	}
1029414c4531SDave Airlie 
1030414c4531SDave Airlie 
1031414c4531SDave Airlie 	if (IS_G200_SE(mdev)) {
1032abbee623SJulia Lemire 		if (mdev->unique_rev_id >= 0x02) {
1033414c4531SDave Airlie 			u8 hi_pri_lvl;
1034414c4531SDave Airlie 			u32 bpp;
1035414c4531SDave Airlie 			u32 mb;
1036414c4531SDave Airlie 
1037*f4510a27SMatt Roper 			if (crtc->primary->fb->bits_per_pixel > 16)
1038414c4531SDave Airlie 				bpp = 32;
1039*f4510a27SMatt Roper 			else if (crtc->primary->fb->bits_per_pixel > 8)
1040414c4531SDave Airlie 				bpp = 16;
1041414c4531SDave Airlie 			else
1042414c4531SDave Airlie 				bpp = 8;
1043414c4531SDave Airlie 
1044414c4531SDave Airlie 			mb = (mode->clock * bpp) / 1000;
1045414c4531SDave Airlie 			if (mb > 3100)
1046414c4531SDave Airlie 				hi_pri_lvl = 0;
1047414c4531SDave Airlie 			else if (mb > 2600)
1048414c4531SDave Airlie 				hi_pri_lvl = 1;
1049414c4531SDave Airlie 			else if (mb > 1900)
1050414c4531SDave Airlie 				hi_pri_lvl = 2;
1051414c4531SDave Airlie 			else if (mb > 1160)
1052414c4531SDave Airlie 				hi_pri_lvl = 3;
1053414c4531SDave Airlie 			else if (mb > 440)
1054414c4531SDave Airlie 				hi_pri_lvl = 4;
1055414c4531SDave Airlie 			else
1056414c4531SDave Airlie 				hi_pri_lvl = 5;
1057414c4531SDave Airlie 
105891f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
105991f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl);
1060414c4531SDave Airlie 		} else {
106191f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1062abbee623SJulia Lemire 			if (mdev->unique_rev_id >= 0x01)
106391f8f105SChristopher Harvey 				WREG8(MGAREG_CRTCEXT_DATA, 0x03);
1064414c4531SDave Airlie 			else
106591f8f105SChristopher Harvey 				WREG8(MGAREG_CRTCEXT_DATA, 0x04);
1066414c4531SDave Airlie 		}
1067414c4531SDave Airlie 	}
1068414c4531SDave Airlie 	return 0;
1069414c4531SDave Airlie }
1070414c4531SDave Airlie 
1071414c4531SDave Airlie #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1072414c4531SDave Airlie static int mga_suspend(struct drm_crtc *crtc)
1073414c4531SDave Airlie {
1074414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1075414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1076414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1077414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1078414c4531SDave Airlie 	int option;
1079414c4531SDave Airlie 
1080414c4531SDave Airlie 	if (mdev->suspended)
1081414c4531SDave Airlie 		return 0;
1082414c4531SDave Airlie 
1083414c4531SDave Airlie 	WREG_SEQ(1, 0x20);
1084414c4531SDave Airlie 	WREG_ECRT(1, 0x30);
1085414c4531SDave Airlie 	/* Disable the pixel clock */
1086414c4531SDave Airlie 	WREG_DAC(0x1a, 0x05);
1087414c4531SDave Airlie 	/* Power down the DAC */
1088414c4531SDave Airlie 	WREG_DAC(0x1e, 0x18);
1089414c4531SDave Airlie 	/* Power down the pixel PLL */
1090414c4531SDave Airlie 	WREG_DAC(0x1a, 0x0d);
1091414c4531SDave Airlie 
1092414c4531SDave Airlie 	/* Disable PLLs and clocks */
1093414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1094414c4531SDave Airlie 	option &= ~(0x1F8024);
1095414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1096414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D3hot);
1097414c4531SDave Airlie 	pci_disable_device(pdev);
1098414c4531SDave Airlie 
1099414c4531SDave Airlie 	mdev->suspended = true;
1100414c4531SDave Airlie 
1101414c4531SDave Airlie 	return 0;
1102414c4531SDave Airlie }
1103414c4531SDave Airlie 
1104414c4531SDave Airlie static int mga_resume(struct drm_crtc *crtc)
1105414c4531SDave Airlie {
1106414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1107414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1108414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1109414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1110414c4531SDave Airlie 	int option;
1111414c4531SDave Airlie 
1112414c4531SDave Airlie 	if (!mdev->suspended)
1113414c4531SDave Airlie 		return 0;
1114414c4531SDave Airlie 
1115414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D0);
1116414c4531SDave Airlie 	pci_enable_device(pdev);
1117414c4531SDave Airlie 
1118414c4531SDave Airlie 	/* Disable sysclk */
1119414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1120414c4531SDave Airlie 	option &= ~(0x4);
1121414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1122414c4531SDave Airlie 
1123414c4531SDave Airlie 	mdev->suspended = false;
1124414c4531SDave Airlie 
1125414c4531SDave Airlie 	return 0;
1126414c4531SDave Airlie }
1127414c4531SDave Airlie 
1128414c4531SDave Airlie #endif
1129414c4531SDave Airlie 
1130414c4531SDave Airlie static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1131414c4531SDave Airlie {
1132414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1133414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1134414c4531SDave Airlie 	u8 seq1 = 0, crtcext1 = 0;
1135414c4531SDave Airlie 
1136414c4531SDave Airlie 	switch (mode) {
1137414c4531SDave Airlie 	case DRM_MODE_DPMS_ON:
1138414c4531SDave Airlie 		seq1 = 0;
1139414c4531SDave Airlie 		crtcext1 = 0;
1140414c4531SDave Airlie 		mga_crtc_load_lut(crtc);
1141414c4531SDave Airlie 		break;
1142414c4531SDave Airlie 	case DRM_MODE_DPMS_STANDBY:
1143414c4531SDave Airlie 		seq1 = 0x20;
1144414c4531SDave Airlie 		crtcext1 = 0x10;
1145414c4531SDave Airlie 		break;
1146414c4531SDave Airlie 	case DRM_MODE_DPMS_SUSPEND:
1147414c4531SDave Airlie 		seq1 = 0x20;
1148414c4531SDave Airlie 		crtcext1 = 0x20;
1149414c4531SDave Airlie 		break;
1150414c4531SDave Airlie 	case DRM_MODE_DPMS_OFF:
1151414c4531SDave Airlie 		seq1 = 0x20;
1152414c4531SDave Airlie 		crtcext1 = 0x30;
1153414c4531SDave Airlie 		break;
1154414c4531SDave Airlie 	}
1155414c4531SDave Airlie 
1156414c4531SDave Airlie #if 0
1157414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_OFF) {
1158414c4531SDave Airlie 		mga_suspend(crtc);
1159414c4531SDave Airlie 	}
1160414c4531SDave Airlie #endif
1161414c4531SDave Airlie 	WREG8(MGAREG_SEQ_INDEX, 0x01);
1162414c4531SDave Airlie 	seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1163414c4531SDave Airlie 	mga_wait_vsync(mdev);
1164414c4531SDave Airlie 	mga_wait_busy(mdev);
1165414c4531SDave Airlie 	WREG8(MGAREG_SEQ_DATA, seq1);
1166414c4531SDave Airlie 	msleep(20);
1167414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1168414c4531SDave Airlie 	crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1169414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1170414c4531SDave Airlie 
1171414c4531SDave Airlie #if 0
1172414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1173414c4531SDave Airlie 		mga_resume(crtc);
1174414c4531SDave Airlie 		drm_helper_resume_force_mode(dev);
1175414c4531SDave Airlie 	}
1176414c4531SDave Airlie #endif
1177414c4531SDave Airlie }
1178414c4531SDave Airlie 
1179414c4531SDave Airlie /*
1180414c4531SDave Airlie  * This is called before a mode is programmed. A typical use might be to
1181414c4531SDave Airlie  * enable DPMS during the programming to avoid seeing intermediate stages,
1182414c4531SDave Airlie  * but that's not relevant to us
1183414c4531SDave Airlie  */
1184414c4531SDave Airlie static void mga_crtc_prepare(struct drm_crtc *crtc)
1185414c4531SDave Airlie {
1186414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1187414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1188414c4531SDave Airlie 	u8 tmp;
1189414c4531SDave Airlie 
1190414c4531SDave Airlie 	/*	mga_resume(crtc);*/
1191414c4531SDave Airlie 
1192414c4531SDave Airlie 	WREG8(MGAREG_CRTC_INDEX, 0x11);
1193414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTC_DATA);
1194414c4531SDave Airlie 	WREG_CRT(0x11, tmp | 0x80);
1195414c4531SDave Airlie 
1196414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1197414c4531SDave Airlie 		WREG_SEQ(0, 1);
1198414c4531SDave Airlie 		msleep(50);
1199414c4531SDave Airlie 		WREG_SEQ(1, 0x20);
1200414c4531SDave Airlie 		msleep(20);
1201414c4531SDave Airlie 	} else {
1202414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1203414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1204414c4531SDave Airlie 
1205414c4531SDave Airlie 		/* start sync reset */
1206414c4531SDave Airlie 		WREG_SEQ(0, 1);
1207414c4531SDave Airlie 		WREG_SEQ(1, tmp | 0x20);
1208414c4531SDave Airlie 	}
1209414c4531SDave Airlie 
1210414c4531SDave Airlie 	if (mdev->type == G200_WB)
1211414c4531SDave Airlie 		mga_g200wb_prepare(crtc);
1212414c4531SDave Airlie 
1213414c4531SDave Airlie 	WREG_CRT(17, 0);
1214414c4531SDave Airlie }
1215414c4531SDave Airlie 
1216414c4531SDave Airlie /*
1217414c4531SDave Airlie  * This is called after a mode is programmed. It should reverse anything done
1218414c4531SDave Airlie  * by the prepare function
1219414c4531SDave Airlie  */
1220414c4531SDave Airlie static void mga_crtc_commit(struct drm_crtc *crtc)
1221414c4531SDave Airlie {
1222414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1223414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1224414c4531SDave Airlie 	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1225414c4531SDave Airlie 	u8 tmp;
1226414c4531SDave Airlie 
1227414c4531SDave Airlie 	if (mdev->type == G200_WB)
1228414c4531SDave Airlie 		mga_g200wb_commit(crtc);
1229414c4531SDave Airlie 
1230414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1231414c4531SDave Airlie 		msleep(50);
1232414c4531SDave Airlie 		WREG_SEQ(1, 0x0);
1233414c4531SDave Airlie 		msleep(20);
1234414c4531SDave Airlie 		WREG_SEQ(0, 0x3);
1235414c4531SDave Airlie 	} else {
1236414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1237414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1238414c4531SDave Airlie 
1239414c4531SDave Airlie 		tmp &= ~0x20;
1240414c4531SDave Airlie 		WREG_SEQ(0x1, tmp);
1241414c4531SDave Airlie 		WREG_SEQ(0, 3);
1242414c4531SDave Airlie 	}
1243414c4531SDave Airlie 	crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1244414c4531SDave Airlie }
1245414c4531SDave Airlie 
1246414c4531SDave Airlie /*
1247414c4531SDave Airlie  * The core can pass us a set of gamma values to program. We actually only
1248414c4531SDave Airlie  * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1249414c4531SDave Airlie  * but it's a requirement that we provide the function
1250414c4531SDave Airlie  */
1251414c4531SDave Airlie static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1252414c4531SDave Airlie 				  u16 *blue, uint32_t start, uint32_t size)
1253414c4531SDave Airlie {
1254414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1255414c4531SDave Airlie 	int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
1256414c4531SDave Airlie 	int i;
1257414c4531SDave Airlie 
1258414c4531SDave Airlie 	for (i = start; i < end; i++) {
1259414c4531SDave Airlie 		mga_crtc->lut_r[i] = red[i] >> 8;
1260414c4531SDave Airlie 		mga_crtc->lut_g[i] = green[i] >> 8;
1261414c4531SDave Airlie 		mga_crtc->lut_b[i] = blue[i] >> 8;
1262414c4531SDave Airlie 	}
1263414c4531SDave Airlie 	mga_crtc_load_lut(crtc);
1264414c4531SDave Airlie }
1265414c4531SDave Airlie 
1266414c4531SDave Airlie /* Simple cleanup function */
1267414c4531SDave Airlie static void mga_crtc_destroy(struct drm_crtc *crtc)
1268414c4531SDave Airlie {
1269414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1270414c4531SDave Airlie 
1271414c4531SDave Airlie 	drm_crtc_cleanup(crtc);
1272414c4531SDave Airlie 	kfree(mga_crtc);
1273414c4531SDave Airlie }
1274414c4531SDave Airlie 
127564c29076SEgbert Eich static void mga_crtc_disable(struct drm_crtc *crtc)
127664c29076SEgbert Eich {
127764c29076SEgbert Eich 	int ret;
127864c29076SEgbert Eich 	DRM_DEBUG_KMS("\n");
127964c29076SEgbert Eich 	mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1280*f4510a27SMatt Roper 	if (crtc->primary->fb) {
1281*f4510a27SMatt Roper 		struct mga_framebuffer *mga_fb = to_mga_framebuffer(crtc->primary->fb);
128264c29076SEgbert Eich 		struct drm_gem_object *obj = mga_fb->obj;
128364c29076SEgbert Eich 		struct mgag200_bo *bo = gem_to_mga_bo(obj);
128464c29076SEgbert Eich 		ret = mgag200_bo_reserve(bo, false);
128564c29076SEgbert Eich 		if (ret)
128664c29076SEgbert Eich 			return;
128764c29076SEgbert Eich 		mgag200_bo_push_sysram(bo);
128864c29076SEgbert Eich 		mgag200_bo_unreserve(bo);
128964c29076SEgbert Eich 	}
1290*f4510a27SMatt Roper 	crtc->primary->fb = NULL;
129164c29076SEgbert Eich }
129264c29076SEgbert Eich 
1293414c4531SDave Airlie /* These provide the minimum set of functions required to handle a CRTC */
1294414c4531SDave Airlie static const struct drm_crtc_funcs mga_crtc_funcs = {
1295a080db9fSChristopher Harvey 	.cursor_set = mga_crtc_cursor_set,
1296a080db9fSChristopher Harvey 	.cursor_move = mga_crtc_cursor_move,
1297414c4531SDave Airlie 	.gamma_set = mga_crtc_gamma_set,
1298414c4531SDave Airlie 	.set_config = drm_crtc_helper_set_config,
1299414c4531SDave Airlie 	.destroy = mga_crtc_destroy,
1300414c4531SDave Airlie };
1301414c4531SDave Airlie 
1302414c4531SDave Airlie static const struct drm_crtc_helper_funcs mga_helper_funcs = {
130364c29076SEgbert Eich 	.disable = mga_crtc_disable,
1304414c4531SDave Airlie 	.dpms = mga_crtc_dpms,
1305414c4531SDave Airlie 	.mode_fixup = mga_crtc_mode_fixup,
1306414c4531SDave Airlie 	.mode_set = mga_crtc_mode_set,
1307414c4531SDave Airlie 	.mode_set_base = mga_crtc_mode_set_base,
1308414c4531SDave Airlie 	.prepare = mga_crtc_prepare,
1309414c4531SDave Airlie 	.commit = mga_crtc_commit,
1310414c4531SDave Airlie 	.load_lut = mga_crtc_load_lut,
1311414c4531SDave Airlie };
1312414c4531SDave Airlie 
1313414c4531SDave Airlie /* CRTC setup */
1314f1998fe2SChristopher Harvey static void mga_crtc_init(struct mga_device *mdev)
1315414c4531SDave Airlie {
1316414c4531SDave Airlie 	struct mga_crtc *mga_crtc;
1317414c4531SDave Airlie 	int i;
1318414c4531SDave Airlie 
1319414c4531SDave Airlie 	mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1320414c4531SDave Airlie 			      (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1321414c4531SDave Airlie 			      GFP_KERNEL);
1322414c4531SDave Airlie 
1323414c4531SDave Airlie 	if (mga_crtc == NULL)
1324414c4531SDave Airlie 		return;
1325414c4531SDave Airlie 
1326f1998fe2SChristopher Harvey 	drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
1327414c4531SDave Airlie 
1328414c4531SDave Airlie 	drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1329414c4531SDave Airlie 	mdev->mode_info.crtc = mga_crtc;
1330414c4531SDave Airlie 
1331414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
1332414c4531SDave Airlie 		mga_crtc->lut_r[i] = i;
1333414c4531SDave Airlie 		mga_crtc->lut_g[i] = i;
1334414c4531SDave Airlie 		mga_crtc->lut_b[i] = i;
1335414c4531SDave Airlie 	}
1336414c4531SDave Airlie 
1337414c4531SDave Airlie 	drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1338414c4531SDave Airlie }
1339414c4531SDave Airlie 
1340414c4531SDave Airlie /** Sets the color ramps on behalf of fbcon */
1341414c4531SDave Airlie void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1342414c4531SDave Airlie 			      u16 blue, int regno)
1343414c4531SDave Airlie {
1344414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1345414c4531SDave Airlie 
1346414c4531SDave Airlie 	mga_crtc->lut_r[regno] = red >> 8;
1347414c4531SDave Airlie 	mga_crtc->lut_g[regno] = green >> 8;
1348414c4531SDave Airlie 	mga_crtc->lut_b[regno] = blue >> 8;
1349414c4531SDave Airlie }
1350414c4531SDave Airlie 
1351414c4531SDave Airlie /** Gets the color ramps on behalf of fbcon */
1352414c4531SDave Airlie void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
1353414c4531SDave Airlie 			      u16 *blue, int regno)
1354414c4531SDave Airlie {
1355414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1356414c4531SDave Airlie 
1357414c4531SDave Airlie 	*red = (u16)mga_crtc->lut_r[regno] << 8;
1358414c4531SDave Airlie 	*green = (u16)mga_crtc->lut_g[regno] << 8;
1359414c4531SDave Airlie 	*blue = (u16)mga_crtc->lut_b[regno] << 8;
1360414c4531SDave Airlie }
1361414c4531SDave Airlie 
1362414c4531SDave Airlie /*
1363414c4531SDave Airlie  * The encoder comes after the CRTC in the output pipeline, but before
1364414c4531SDave Airlie  * the connector. It's responsible for ensuring that the digital
1365414c4531SDave Airlie  * stream is appropriately converted into the output format. Setup is
1366414c4531SDave Airlie  * very simple in this case - all we have to do is inform qemu of the
1367414c4531SDave Airlie  * colour depth in order to ensure that it displays appropriately
1368414c4531SDave Airlie  */
1369414c4531SDave Airlie 
1370414c4531SDave Airlie /*
1371414c4531SDave Airlie  * These functions are analagous to those in the CRTC code, but are intended
1372414c4531SDave Airlie  * to handle any encoder-specific limitations
1373414c4531SDave Airlie  */
1374414c4531SDave Airlie static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
1375e811f5aeSLaurent Pinchart 				   const struct drm_display_mode *mode,
1376414c4531SDave Airlie 				   struct drm_display_mode *adjusted_mode)
1377414c4531SDave Airlie {
1378414c4531SDave Airlie 	return true;
1379414c4531SDave Airlie }
1380414c4531SDave Airlie 
1381414c4531SDave Airlie static void mga_encoder_mode_set(struct drm_encoder *encoder,
1382414c4531SDave Airlie 				struct drm_display_mode *mode,
1383414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
1384414c4531SDave Airlie {
1385414c4531SDave Airlie 
1386414c4531SDave Airlie }
1387414c4531SDave Airlie 
1388414c4531SDave Airlie static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
1389414c4531SDave Airlie {
1390414c4531SDave Airlie 	return;
1391414c4531SDave Airlie }
1392414c4531SDave Airlie 
1393414c4531SDave Airlie static void mga_encoder_prepare(struct drm_encoder *encoder)
1394414c4531SDave Airlie {
1395414c4531SDave Airlie }
1396414c4531SDave Airlie 
1397414c4531SDave Airlie static void mga_encoder_commit(struct drm_encoder *encoder)
1398414c4531SDave Airlie {
1399414c4531SDave Airlie }
1400414c4531SDave Airlie 
1401080fd6b5SRashika static void mga_encoder_destroy(struct drm_encoder *encoder)
1402414c4531SDave Airlie {
1403414c4531SDave Airlie 	struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
1404414c4531SDave Airlie 	drm_encoder_cleanup(encoder);
1405414c4531SDave Airlie 	kfree(mga_encoder);
1406414c4531SDave Airlie }
1407414c4531SDave Airlie 
1408414c4531SDave Airlie static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
1409414c4531SDave Airlie 	.dpms = mga_encoder_dpms,
1410414c4531SDave Airlie 	.mode_fixup = mga_encoder_mode_fixup,
1411414c4531SDave Airlie 	.mode_set = mga_encoder_mode_set,
1412414c4531SDave Airlie 	.prepare = mga_encoder_prepare,
1413414c4531SDave Airlie 	.commit = mga_encoder_commit,
1414414c4531SDave Airlie };
1415414c4531SDave Airlie 
1416414c4531SDave Airlie static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
1417414c4531SDave Airlie 	.destroy = mga_encoder_destroy,
1418414c4531SDave Airlie };
1419414c4531SDave Airlie 
1420414c4531SDave Airlie static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
1421414c4531SDave Airlie {
1422414c4531SDave Airlie 	struct drm_encoder *encoder;
1423414c4531SDave Airlie 	struct mga_encoder *mga_encoder;
1424414c4531SDave Airlie 
1425414c4531SDave Airlie 	mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
1426414c4531SDave Airlie 	if (!mga_encoder)
1427414c4531SDave Airlie 		return NULL;
1428414c4531SDave Airlie 
1429414c4531SDave Airlie 	encoder = &mga_encoder->base;
1430414c4531SDave Airlie 	encoder->possible_crtcs = 0x1;
1431414c4531SDave Airlie 
1432414c4531SDave Airlie 	drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
1433414c4531SDave Airlie 			 DRM_MODE_ENCODER_DAC);
1434414c4531SDave Airlie 	drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
1435414c4531SDave Airlie 
1436414c4531SDave Airlie 	return encoder;
1437414c4531SDave Airlie }
1438414c4531SDave Airlie 
1439414c4531SDave Airlie 
1440414c4531SDave Airlie static int mga_vga_get_modes(struct drm_connector *connector)
1441414c4531SDave Airlie {
1442414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1443414c4531SDave Airlie 	struct edid *edid;
1444414c4531SDave Airlie 	int ret = 0;
1445414c4531SDave Airlie 
1446414c4531SDave Airlie 	edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1447414c4531SDave Airlie 	if (edid) {
1448414c4531SDave Airlie 		drm_mode_connector_update_edid_property(connector, edid);
1449414c4531SDave Airlie 		ret = drm_add_edid_modes(connector, edid);
1450414c4531SDave Airlie 		kfree(edid);
1451414c4531SDave Airlie 	}
1452414c4531SDave Airlie 	return ret;
1453414c4531SDave Airlie }
1454414c4531SDave Airlie 
1455abbee623SJulia Lemire static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
1456abbee623SJulia Lemire 							int bits_per_pixel)
1457abbee623SJulia Lemire {
1458abbee623SJulia Lemire 	uint32_t total_area, divisor;
1459abbee623SJulia Lemire 	int64_t active_area, pixels_per_second, bandwidth;
1460abbee623SJulia Lemire 	uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
1461abbee623SJulia Lemire 
1462abbee623SJulia Lemire 	divisor = 1024;
1463abbee623SJulia Lemire 
1464abbee623SJulia Lemire 	if (!mode->htotal || !mode->vtotal || !mode->clock)
1465abbee623SJulia Lemire 		return 0;
1466abbee623SJulia Lemire 
1467abbee623SJulia Lemire 	active_area = mode->hdisplay * mode->vdisplay;
1468abbee623SJulia Lemire 	total_area = mode->htotal * mode->vtotal;
1469abbee623SJulia Lemire 
1470abbee623SJulia Lemire 	pixels_per_second = active_area * mode->clock * 1000;
1471abbee623SJulia Lemire 	do_div(pixels_per_second, total_area);
1472abbee623SJulia Lemire 
1473abbee623SJulia Lemire 	bandwidth = pixels_per_second * bytes_per_pixel * 100;
1474abbee623SJulia Lemire 	do_div(bandwidth, divisor);
1475abbee623SJulia Lemire 
1476abbee623SJulia Lemire 	return (uint32_t)(bandwidth);
1477abbee623SJulia Lemire }
1478abbee623SJulia Lemire 
1479abbee623SJulia Lemire #define MODE_BANDWIDTH	MODE_BAD
1480abbee623SJulia Lemire 
1481414c4531SDave Airlie static int mga_vga_mode_valid(struct drm_connector *connector,
1482414c4531SDave Airlie 				 struct drm_display_mode *mode)
1483414c4531SDave Airlie {
14840ba53171SChristopher Harvey 	struct drm_device *dev = connector->dev;
14850ba53171SChristopher Harvey 	struct mga_device *mdev = (struct mga_device*)dev->dev_private;
14860ba53171SChristopher Harvey 	struct mga_fbdev *mfbdev = mdev->mfbdev;
14870ba53171SChristopher Harvey 	struct drm_fb_helper *fb_helper = &mfbdev->helper;
14880ba53171SChristopher Harvey 	struct drm_fb_helper_connector *fb_helper_conn = NULL;
14890ba53171SChristopher Harvey 	int bpp = 32;
14900ba53171SChristopher Harvey 	int i = 0;
14910ba53171SChristopher Harvey 
1492abbee623SJulia Lemire 	if (IS_G200_SE(mdev)) {
1493abbee623SJulia Lemire 		if (mdev->unique_rev_id == 0x01) {
1494abbee623SJulia Lemire 			if (mode->hdisplay > 1600)
1495abbee623SJulia Lemire 				return MODE_VIRTUAL_X;
1496abbee623SJulia Lemire 			if (mode->vdisplay > 1200)
1497abbee623SJulia Lemire 				return MODE_VIRTUAL_Y;
1498abbee623SJulia Lemire 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1499abbee623SJulia Lemire 				> (24400 * 1024))
1500abbee623SJulia Lemire 				return MODE_BANDWIDTH;
1501abbee623SJulia Lemire 		} else if (mdev->unique_rev_id >= 0x02) {
1502abbee623SJulia Lemire 			if (mode->hdisplay > 1920)
1503abbee623SJulia Lemire 				return MODE_VIRTUAL_X;
1504abbee623SJulia Lemire 			if (mode->vdisplay > 1200)
1505abbee623SJulia Lemire 				return MODE_VIRTUAL_Y;
1506abbee623SJulia Lemire 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1507abbee623SJulia Lemire 				> (30100 * 1024))
1508abbee623SJulia Lemire 				return MODE_BANDWIDTH;
1509abbee623SJulia Lemire 		}
1510abbee623SJulia Lemire 	} else if (mdev->type == G200_WB) {
1511abbee623SJulia Lemire 		if (mode->hdisplay > 1280)
1512abbee623SJulia Lemire 			return MODE_VIRTUAL_X;
1513abbee623SJulia Lemire 		if (mode->vdisplay > 1024)
1514abbee623SJulia Lemire 			return MODE_VIRTUAL_Y;
1515abbee623SJulia Lemire 		if (mga_vga_calculate_mode_bandwidth(mode,
1516abbee623SJulia Lemire 			bpp > (31877 * 1024)))
1517abbee623SJulia Lemire 			return MODE_BANDWIDTH;
1518abbee623SJulia Lemire 	} else if (mdev->type == G200_EV &&
1519abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode, bpp)
1520abbee623SJulia Lemire 			> (32700 * 1024))) {
1521abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1522ec22b4aaSDave Airlie 	} else if (mdev->type == G200_EH &&
1523abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode, bpp)
1524abbee623SJulia Lemire 			> (37500 * 1024))) {
1525abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1526ec22b4aaSDave Airlie 	} else if (mdev->type == G200_ER &&
1527abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode,
1528abbee623SJulia Lemire 			bpp) > (55000 * 1024))) {
1529abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1530abbee623SJulia Lemire 	}
1531414c4531SDave Airlie 
1532414c4531SDave Airlie 	if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1533414c4531SDave Airlie 	    mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1534414c4531SDave Airlie 	    mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1535414c4531SDave Airlie 	    mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1536414c4531SDave Airlie 		return MODE_BAD;
1537414c4531SDave Airlie 	}
1538414c4531SDave Airlie 
15390ba53171SChristopher Harvey 	/* Validate the mode input by the user */
15400ba53171SChristopher Harvey 	for (i = 0; i < fb_helper->connector_count; i++) {
15410ba53171SChristopher Harvey 		if (fb_helper->connector_info[i]->connector == connector) {
15420ba53171SChristopher Harvey 			/* Found the helper for this connector */
15430ba53171SChristopher Harvey 			fb_helper_conn = fb_helper->connector_info[i];
15440ba53171SChristopher Harvey 			if (fb_helper_conn->cmdline_mode.specified) {
15450ba53171SChristopher Harvey 				if (fb_helper_conn->cmdline_mode.bpp_specified) {
15460ba53171SChristopher Harvey 					bpp = fb_helper_conn->cmdline_mode.bpp;
15470ba53171SChristopher Harvey 				}
15480ba53171SChristopher Harvey 			}
15490ba53171SChristopher Harvey 		}
15500ba53171SChristopher Harvey 	}
15510ba53171SChristopher Harvey 
15520ba53171SChristopher Harvey 	if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
15530ba53171SChristopher Harvey 		if (fb_helper_conn)
15540ba53171SChristopher Harvey 			fb_helper_conn->cmdline_mode.specified = false;
15550ba53171SChristopher Harvey 		return MODE_BAD;
15560ba53171SChristopher Harvey 	}
15570ba53171SChristopher Harvey 
1558414c4531SDave Airlie 	return MODE_OK;
1559414c4531SDave Airlie }
1560414c4531SDave Airlie 
1561080fd6b5SRashika static struct drm_encoder *mga_connector_best_encoder(struct drm_connector
1562414c4531SDave Airlie 						  *connector)
1563414c4531SDave Airlie {
1564414c4531SDave Airlie 	int enc_id = connector->encoder_ids[0];
1565414c4531SDave Airlie 	struct drm_mode_object *obj;
1566414c4531SDave Airlie 	struct drm_encoder *encoder;
1567414c4531SDave Airlie 
1568414c4531SDave Airlie 	/* pick the encoder ids */
1569414c4531SDave Airlie 	if (enc_id) {
1570414c4531SDave Airlie 		obj =
1571414c4531SDave Airlie 		    drm_mode_object_find(connector->dev, enc_id,
1572414c4531SDave Airlie 					 DRM_MODE_OBJECT_ENCODER);
1573414c4531SDave Airlie 		if (!obj)
1574414c4531SDave Airlie 			return NULL;
1575414c4531SDave Airlie 		encoder = obj_to_encoder(obj);
1576414c4531SDave Airlie 		return encoder;
1577414c4531SDave Airlie 	}
1578414c4531SDave Airlie 	return NULL;
1579414c4531SDave Airlie }
1580414c4531SDave Airlie 
1581414c4531SDave Airlie static enum drm_connector_status mga_vga_detect(struct drm_connector
1582414c4531SDave Airlie 						   *connector, bool force)
1583414c4531SDave Airlie {
1584414c4531SDave Airlie 	return connector_status_connected;
1585414c4531SDave Airlie }
1586414c4531SDave Airlie 
1587414c4531SDave Airlie static void mga_connector_destroy(struct drm_connector *connector)
1588414c4531SDave Airlie {
1589414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1590414c4531SDave Airlie 	mgag200_i2c_destroy(mga_connector->i2c);
1591414c4531SDave Airlie 	drm_connector_cleanup(connector);
1592414c4531SDave Airlie 	kfree(connector);
1593414c4531SDave Airlie }
1594414c4531SDave Airlie 
1595414c4531SDave Airlie struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
1596414c4531SDave Airlie 	.get_modes = mga_vga_get_modes,
1597414c4531SDave Airlie 	.mode_valid = mga_vga_mode_valid,
1598414c4531SDave Airlie 	.best_encoder = mga_connector_best_encoder,
1599414c4531SDave Airlie };
1600414c4531SDave Airlie 
1601414c4531SDave Airlie struct drm_connector_funcs mga_vga_connector_funcs = {
1602414c4531SDave Airlie 	.dpms = drm_helper_connector_dpms,
1603414c4531SDave Airlie 	.detect = mga_vga_detect,
1604414c4531SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
1605414c4531SDave Airlie 	.destroy = mga_connector_destroy,
1606414c4531SDave Airlie };
1607414c4531SDave Airlie 
1608414c4531SDave Airlie static struct drm_connector *mga_vga_init(struct drm_device *dev)
1609414c4531SDave Airlie {
1610414c4531SDave Airlie 	struct drm_connector *connector;
1611414c4531SDave Airlie 	struct mga_connector *mga_connector;
1612414c4531SDave Airlie 
1613414c4531SDave Airlie 	mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1614414c4531SDave Airlie 	if (!mga_connector)
1615414c4531SDave Airlie 		return NULL;
1616414c4531SDave Airlie 
1617414c4531SDave Airlie 	connector = &mga_connector->base;
1618414c4531SDave Airlie 
1619414c4531SDave Airlie 	drm_connector_init(dev, connector,
1620414c4531SDave Airlie 			   &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1621414c4531SDave Airlie 
1622414c4531SDave Airlie 	drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1623414c4531SDave Airlie 
16243d5a1c5eSEgbert Eich 	drm_sysfs_connector_add(connector);
16253d5a1c5eSEgbert Eich 
1626414c4531SDave Airlie 	mga_connector->i2c = mgag200_i2c_create(dev);
1627414c4531SDave Airlie 	if (!mga_connector->i2c)
1628414c4531SDave Airlie 		DRM_ERROR("failed to add ddc bus\n");
1629414c4531SDave Airlie 
1630414c4531SDave Airlie 	return connector;
1631414c4531SDave Airlie }
1632414c4531SDave Airlie 
1633414c4531SDave Airlie 
1634414c4531SDave Airlie int mgag200_modeset_init(struct mga_device *mdev)
1635414c4531SDave Airlie {
1636414c4531SDave Airlie 	struct drm_encoder *encoder;
1637414c4531SDave Airlie 	struct drm_connector *connector;
1638414c4531SDave Airlie 	int ret;
1639414c4531SDave Airlie 
1640414c4531SDave Airlie 	mdev->mode_info.mode_config_initialized = true;
1641414c4531SDave Airlie 
1642414c4531SDave Airlie 	mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1643414c4531SDave Airlie 	mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1644414c4531SDave Airlie 
1645414c4531SDave Airlie 	mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1646414c4531SDave Airlie 
1647f1998fe2SChristopher Harvey 	mga_crtc_init(mdev);
1648414c4531SDave Airlie 
1649414c4531SDave Airlie 	encoder = mga_encoder_init(mdev->dev);
1650414c4531SDave Airlie 	if (!encoder) {
1651414c4531SDave Airlie 		DRM_ERROR("mga_encoder_init failed\n");
1652414c4531SDave Airlie 		return -1;
1653414c4531SDave Airlie 	}
1654414c4531SDave Airlie 
1655414c4531SDave Airlie 	connector = mga_vga_init(mdev->dev);
1656414c4531SDave Airlie 	if (!connector) {
1657414c4531SDave Airlie 		DRM_ERROR("mga_vga_init failed\n");
1658414c4531SDave Airlie 		return -1;
1659414c4531SDave Airlie 	}
1660414c4531SDave Airlie 
1661414c4531SDave Airlie 	drm_mode_connector_attach_encoder(connector, encoder);
1662414c4531SDave Airlie 
1663414c4531SDave Airlie 	ret = mgag200_fbdev_init(mdev);
1664414c4531SDave Airlie 	if (ret) {
1665414c4531SDave Airlie 		DRM_ERROR("mga_fbdev_init failed\n");
1666414c4531SDave Airlie 		return ret;
1667414c4531SDave Airlie 	}
1668414c4531SDave Airlie 
1669414c4531SDave Airlie 	return 0;
1670414c4531SDave Airlie }
1671414c4531SDave Airlie 
1672414c4531SDave Airlie void mgag200_modeset_fini(struct mga_device *mdev)
1673414c4531SDave Airlie {
1674414c4531SDave Airlie 
1675414c4531SDave Airlie }
1676