xref: /linux/drivers/gpu/drm/mgag200/mgag200_mode.c (revision da55839870263563cc70e700a7f58090a860576d)
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;
32414c4531SDave Airlie 	int i;
33414c4531SDave Airlie 
34414c4531SDave Airlie 	if (!crtc->enabled)
35414c4531SDave Airlie 		return;
36414c4531SDave Airlie 
37414c4531SDave Airlie 	WREG8(DAC_INDEX + MGA1064_INDEX, 0);
38414c4531SDave Airlie 
39414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
40414c4531SDave Airlie 		/* VGA registers */
41414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_r[i]);
42414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
43414c4531SDave Airlie 		WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_b[i]);
44414c4531SDave Airlie 	}
45414c4531SDave Airlie }
46414c4531SDave Airlie 
47414c4531SDave Airlie static inline void mga_wait_vsync(struct mga_device *mdev)
48414c4531SDave Airlie {
493cdc0e8dSChristopher Harvey 	unsigned long timeout = jiffies + HZ/10;
50414c4531SDave Airlie 	unsigned int status = 0;
51414c4531SDave Airlie 
52414c4531SDave Airlie 	do {
53414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
543cdc0e8dSChristopher Harvey 	} while ((status & 0x08) && time_before(jiffies, timeout));
553cdc0e8dSChristopher Harvey 	timeout = jiffies + HZ/10;
56414c4531SDave Airlie 	status = 0;
57414c4531SDave Airlie 	do {
58414c4531SDave Airlie 		status = RREG32(MGAREG_Status);
593cdc0e8dSChristopher Harvey 	} while (!(status & 0x08) && time_before(jiffies, timeout));
60414c4531SDave Airlie }
61414c4531SDave Airlie 
62414c4531SDave Airlie static inline void mga_wait_busy(struct mga_device *mdev)
63414c4531SDave Airlie {
643cdc0e8dSChristopher Harvey 	unsigned long timeout = jiffies + HZ;
65414c4531SDave Airlie 	unsigned int status = 0;
66414c4531SDave Airlie 	do {
67414c4531SDave Airlie 		status = RREG8(MGAREG_Status + 2);
683cdc0e8dSChristopher Harvey 	} while ((status & 0x01) && time_before(jiffies, timeout));
69414c4531SDave Airlie }
70414c4531SDave Airlie 
71414c4531SDave Airlie /*
72414c4531SDave Airlie  * The core passes the desired mode to the CRTC code to see whether any
73414c4531SDave Airlie  * CRTC-specific modifications need to be made to it. We're in a position
74414c4531SDave Airlie  * to just pass that straight through, so this does nothing
75414c4531SDave Airlie  */
76414c4531SDave Airlie static bool mga_crtc_mode_fixup(struct drm_crtc *crtc,
77e811f5aeSLaurent Pinchart 				const struct drm_display_mode *mode,
78414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
79414c4531SDave Airlie {
80414c4531SDave Airlie 	return true;
81414c4531SDave Airlie }
82414c4531SDave Airlie 
83414c4531SDave Airlie static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
84414c4531SDave Airlie {
85414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
86414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
87414c4531SDave Airlie 	unsigned int testp, testm, testn;
88414c4531SDave Airlie 	unsigned int p, m, n;
89414c4531SDave Airlie 	unsigned int computed;
90414c4531SDave Airlie 
91414c4531SDave Airlie 	m = n = p = 0;
92414c4531SDave Airlie 	vcomax = 320000;
93414c4531SDave Airlie 	vcomin = 160000;
94414c4531SDave Airlie 	pllreffreq = 25000;
95414c4531SDave Airlie 
96414c4531SDave Airlie 	delta = 0xffffffff;
97414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
98414c4531SDave Airlie 
99414c4531SDave Airlie 	for (testp = 8; testp > 0; testp /= 2) {
100414c4531SDave Airlie 		if (clock * testp > vcomax)
101414c4531SDave Airlie 			continue;
102414c4531SDave Airlie 		if (clock * testp < vcomin)
103414c4531SDave Airlie 			continue;
104414c4531SDave Airlie 
105414c4531SDave Airlie 		for (testn = 17; testn < 256; testn++) {
106414c4531SDave Airlie 			for (testm = 1; testm < 32; testm++) {
107414c4531SDave Airlie 				computed = (pllreffreq * testn) /
108414c4531SDave Airlie 					(testm * testp);
109414c4531SDave Airlie 				if (computed > clock)
110414c4531SDave Airlie 					tmpdelta = computed - clock;
111414c4531SDave Airlie 				else
112414c4531SDave Airlie 					tmpdelta = clock - computed;
113414c4531SDave Airlie 				if (tmpdelta < delta) {
114414c4531SDave Airlie 					delta = tmpdelta;
115414c4531SDave Airlie 					m = testm - 1;
116414c4531SDave Airlie 					n = testn - 1;
117414c4531SDave Airlie 					p = testp - 1;
118414c4531SDave Airlie 				}
119414c4531SDave Airlie 			}
120414c4531SDave Airlie 		}
121414c4531SDave Airlie 	}
122414c4531SDave Airlie 
123414c4531SDave Airlie 	if (delta > permitteddelta) {
124414c4531SDave Airlie 		printk(KERN_WARNING "PLL delta too large\n");
125414c4531SDave Airlie 		return 1;
126414c4531SDave Airlie 	}
127414c4531SDave Airlie 
128414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_M, m);
129414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_N, n);
130414c4531SDave Airlie 	WREG_DAC(MGA1064_PIX_PLLC_P, p);
131414c4531SDave Airlie 	return 0;
132414c4531SDave Airlie }
133414c4531SDave Airlie 
134414c4531SDave Airlie static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
135414c4531SDave Airlie {
136414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
137414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
138414c4531SDave Airlie 	unsigned int testp, testm, testn;
139414c4531SDave Airlie 	unsigned int p, m, n;
140414c4531SDave Airlie 	unsigned int computed;
141414c4531SDave Airlie 	int i, j, tmpcount, vcount;
142414c4531SDave Airlie 	bool pll_locked = false;
143414c4531SDave Airlie 	u8 tmp;
144414c4531SDave Airlie 
145414c4531SDave Airlie 	m = n = p = 0;
146414c4531SDave Airlie 	vcomax = 550000;
147414c4531SDave Airlie 	vcomin = 150000;
148414c4531SDave Airlie 	pllreffreq = 48000;
149414c4531SDave Airlie 
150414c4531SDave Airlie 	delta = 0xffffffff;
151414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
152414c4531SDave Airlie 
153414c4531SDave Airlie 	for (testp = 1; testp < 9; testp++) {
154414c4531SDave Airlie 		if (clock * testp > vcomax)
155414c4531SDave Airlie 			continue;
156414c4531SDave Airlie 		if (clock * testp < vcomin)
157414c4531SDave Airlie 			continue;
158414c4531SDave Airlie 
159414c4531SDave Airlie 		for (testm = 1; testm < 17; testm++) {
160414c4531SDave Airlie 			for (testn = 1; testn < 151; testn++) {
161414c4531SDave Airlie 				computed = (pllreffreq * testn) /
162414c4531SDave Airlie 					(testm * testp);
163414c4531SDave Airlie 				if (computed > clock)
164414c4531SDave Airlie 					tmpdelta = computed - clock;
165414c4531SDave Airlie 				else
166414c4531SDave Airlie 					tmpdelta = clock - computed;
167414c4531SDave Airlie 				if (tmpdelta < delta) {
168414c4531SDave Airlie 					delta = tmpdelta;
169414c4531SDave Airlie 					n = testn - 1;
170414c4531SDave Airlie 					m = (testm - 1) | ((n >> 1) & 0x80);
171414c4531SDave Airlie 					p = testp - 1;
172414c4531SDave Airlie 				}
173414c4531SDave Airlie 			}
174414c4531SDave Airlie 		}
175414c4531SDave Airlie 	}
176414c4531SDave Airlie 
177414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
178414c4531SDave Airlie 		if (i > 0) {
179414c4531SDave Airlie 			WREG8(MGAREG_CRTC_INDEX, 0x1e);
180414c4531SDave Airlie 			tmp = RREG8(MGAREG_CRTC_DATA);
181414c4531SDave Airlie 			if (tmp < 0xff)
182414c4531SDave Airlie 				WREG8(MGAREG_CRTC_DATA, tmp+1);
183414c4531SDave Airlie 		}
184414c4531SDave Airlie 
185414c4531SDave Airlie 		/* set pixclkdis to 1 */
186414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
187414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
188414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
189fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
190414c4531SDave Airlie 
191414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
192414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
193414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKDIS;
194fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
195414c4531SDave Airlie 
196414c4531SDave Airlie 		/* select PLL Set C */
197414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
198414c4531SDave Airlie 		tmp |= 0x3 << 2;
199414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
200414c4531SDave Airlie 
201414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
202414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
203414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
204fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
205414c4531SDave Airlie 
206414c4531SDave Airlie 		udelay(500);
207414c4531SDave Airlie 
208414c4531SDave Airlie 		/* reset the PLL */
209414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
210414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
211414c4531SDave Airlie 		tmp &= ~0x04;
212fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
213414c4531SDave Airlie 
214414c4531SDave Airlie 		udelay(50);
215414c4531SDave Airlie 
216414c4531SDave Airlie 		/* program pixel pll register */
217414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
218414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
219414c4531SDave Airlie 		WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
220414c4531SDave Airlie 
221414c4531SDave Airlie 		udelay(50);
222414c4531SDave Airlie 
223414c4531SDave Airlie 		/* turn pll on */
224414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
225414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
226414c4531SDave Airlie 		tmp |= 0x04;
227414c4531SDave Airlie 		WREG_DAC(MGA1064_VREF_CTL, tmp);
228414c4531SDave Airlie 
229414c4531SDave Airlie 		udelay(500);
230414c4531SDave Airlie 
231414c4531SDave Airlie 		/* select the pixel pll */
232414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
233414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
234414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
235414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
236fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
237414c4531SDave Airlie 
238414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
239414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
240414c4531SDave Airlie 		tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
241414c4531SDave Airlie 		tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
242fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
243414c4531SDave Airlie 
244414c4531SDave Airlie 		/* reset dotclock rate bit */
245414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 1);
246414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
247414c4531SDave Airlie 		tmp &= ~0x8;
248414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, tmp);
249414c4531SDave Airlie 
250414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
251414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
252414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
253fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
254414c4531SDave Airlie 
255414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
256414c4531SDave Airlie 
257414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
258414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
259414c4531SDave Airlie 			if (tmpcount < vcount)
260414c4531SDave Airlie 				vcount = 0;
261414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
262414c4531SDave Airlie 				pll_locked = true;
263414c4531SDave Airlie 			else
264414c4531SDave Airlie 				udelay(5);
265414c4531SDave Airlie 		}
266414c4531SDave Airlie 	}
267414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
268414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
269414c4531SDave Airlie 	tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
270414c4531SDave Airlie 	WREG_DAC(MGA1064_REMHEADCTL, tmp);
271414c4531SDave Airlie 	return 0;
272414c4531SDave Airlie }
273414c4531SDave Airlie 
274414c4531SDave Airlie static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
275414c4531SDave Airlie {
276414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
277414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
278414c4531SDave Airlie 	unsigned int testp, testm, testn;
279414c4531SDave Airlie 	unsigned int p, m, n;
280414c4531SDave Airlie 	unsigned int computed;
281414c4531SDave Airlie 	u8 tmp;
282414c4531SDave Airlie 
283414c4531SDave Airlie 	m = n = p = 0;
284414c4531SDave Airlie 	vcomax = 550000;
285414c4531SDave Airlie 	vcomin = 150000;
286414c4531SDave Airlie 	pllreffreq = 50000;
287414c4531SDave Airlie 
288414c4531SDave Airlie 	delta = 0xffffffff;
289414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
290414c4531SDave Airlie 
291414c4531SDave Airlie 	for (testp = 16; testp > 0; testp--) {
292414c4531SDave Airlie 		if (clock * testp > vcomax)
293414c4531SDave Airlie 			continue;
294414c4531SDave Airlie 		if (clock * testp < vcomin)
295414c4531SDave Airlie 			continue;
296414c4531SDave Airlie 
297414c4531SDave Airlie 		for (testn = 1; testn < 257; testn++) {
298414c4531SDave Airlie 			for (testm = 1; testm < 17; testm++) {
299414c4531SDave Airlie 				computed = (pllreffreq * testn) /
300414c4531SDave Airlie 					(testm * testp);
301414c4531SDave Airlie 				if (computed > clock)
302414c4531SDave Airlie 					tmpdelta = computed - clock;
303414c4531SDave Airlie 				else
304414c4531SDave Airlie 					tmpdelta = clock - computed;
305414c4531SDave Airlie 				if (tmpdelta < delta) {
306414c4531SDave Airlie 					delta = tmpdelta;
307414c4531SDave Airlie 					n = testn - 1;
308414c4531SDave Airlie 					m = testm - 1;
309414c4531SDave Airlie 					p = testp - 1;
310414c4531SDave Airlie 				}
311414c4531SDave Airlie 			}
312414c4531SDave Airlie 		}
313414c4531SDave Airlie 	}
314414c4531SDave Airlie 
315414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
316414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
317414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
318fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
319414c4531SDave Airlie 
320414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
321414c4531SDave Airlie 	tmp |= 0x3 << 2;
322414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
323414c4531SDave Airlie 
324414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
325414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
326fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp & ~0x40);
327414c4531SDave Airlie 
328414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
329414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
330414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
331fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
332414c4531SDave Airlie 
333414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
334414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
335414c4531SDave Airlie 	WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
336414c4531SDave Airlie 
337414c4531SDave Airlie 	udelay(50);
338414c4531SDave Airlie 
339414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
340414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
341414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
342fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
343414c4531SDave Airlie 
344414c4531SDave Airlie 	udelay(500);
345414c4531SDave Airlie 
346414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
347414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
348414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
349414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
350fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
351414c4531SDave Airlie 
352414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
353414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
354fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp | 0x40);
355414c4531SDave Airlie 
356414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
357414c4531SDave Airlie 	tmp |= (0x3 << 2);
358414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
359414c4531SDave Airlie 
360414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
361414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
362414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
363fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
364414c4531SDave Airlie 
365414c4531SDave Airlie 	return 0;
366414c4531SDave Airlie }
367414c4531SDave Airlie 
368414c4531SDave Airlie static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
369414c4531SDave Airlie {
370414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
371414c4531SDave Airlie 	unsigned int delta, tmpdelta, permitteddelta;
372414c4531SDave Airlie 	unsigned int testp, testm, testn;
373414c4531SDave Airlie 	unsigned int p, m, n;
374414c4531SDave Airlie 	unsigned int computed;
375414c4531SDave Airlie 	int i, j, tmpcount, vcount;
376414c4531SDave Airlie 	u8 tmp;
377414c4531SDave Airlie 	bool pll_locked = false;
378414c4531SDave Airlie 
379414c4531SDave Airlie 	m = n = p = 0;
380414c4531SDave Airlie 	vcomax = 800000;
381414c4531SDave Airlie 	vcomin = 400000;
382260b3f12SJulia Lemire 	pllreffreq = 33333;
383414c4531SDave Airlie 
384414c4531SDave Airlie 	delta = 0xffffffff;
385414c4531SDave Airlie 	permitteddelta = clock * 5 / 1000;
386414c4531SDave Airlie 
387260b3f12SJulia Lemire 	for (testp = 16; testp > 0; testp >>= 1) {
388414c4531SDave Airlie 		if (clock * testp > vcomax)
389414c4531SDave Airlie 			continue;
390414c4531SDave Airlie 		if (clock * testp < vcomin)
391414c4531SDave Airlie 			continue;
392414c4531SDave Airlie 
393414c4531SDave Airlie 		for (testm = 1; testm < 33; testm++) {
394260b3f12SJulia Lemire 			for (testn = 17; testn < 257; testn++) {
395414c4531SDave Airlie 				computed = (pllreffreq * testn) /
396414c4531SDave Airlie 					(testm * testp);
397414c4531SDave Airlie 				if (computed > clock)
398414c4531SDave Airlie 					tmpdelta = computed - clock;
399414c4531SDave Airlie 				else
400414c4531SDave Airlie 					tmpdelta = clock - computed;
401414c4531SDave Airlie 				if (tmpdelta < delta) {
402414c4531SDave Airlie 					delta = tmpdelta;
403414c4531SDave Airlie 					n = testn - 1;
404260b3f12SJulia Lemire 					m = (testm - 1);
405414c4531SDave Airlie 					p = testp - 1;
406414c4531SDave Airlie 				}
407414c4531SDave Airlie 				if ((clock * testp) >= 600000)
408260b3f12SJulia Lemire 					p |= 0x80;
409414c4531SDave Airlie 			}
410414c4531SDave Airlie 		}
411414c4531SDave Airlie 	}
412414c4531SDave Airlie 	for (i = 0; i <= 32 && pll_locked == false; i++) {
413414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
414414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
415414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
416fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
417414c4531SDave Airlie 
418414c4531SDave Airlie 		tmp = RREG8(MGAREG_MEM_MISC_READ);
419414c4531SDave Airlie 		tmp |= 0x3 << 2;
420414c4531SDave Airlie 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
421414c4531SDave Airlie 
422414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
423414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
424414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
425fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
426414c4531SDave Airlie 
427414c4531SDave Airlie 		udelay(500);
428414c4531SDave Airlie 
429414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
430414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
431414c4531SDave Airlie 		WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
432414c4531SDave Airlie 
433414c4531SDave Airlie 		udelay(500);
434414c4531SDave Airlie 
435414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
436414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
437414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
438414c4531SDave Airlie 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
439fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
440414c4531SDave Airlie 
441414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
442414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
443414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
444414c4531SDave Airlie 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
445fb70a669SChristopher Harvey 		WREG8(DAC_DATA, tmp);
446414c4531SDave Airlie 
447414c4531SDave Airlie 		vcount = RREG8(MGAREG_VCOUNT);
448414c4531SDave Airlie 
449414c4531SDave Airlie 		for (j = 0; j < 30 && pll_locked == false; j++) {
450414c4531SDave Airlie 			tmpcount = RREG8(MGAREG_VCOUNT);
451414c4531SDave Airlie 			if (tmpcount < vcount)
452414c4531SDave Airlie 				vcount = 0;
453414c4531SDave Airlie 			if ((tmpcount - vcount) > 2)
454414c4531SDave Airlie 				pll_locked = true;
455414c4531SDave Airlie 			else
456414c4531SDave Airlie 				udelay(5);
457414c4531SDave Airlie 		}
458414c4531SDave Airlie 	}
459414c4531SDave Airlie 
460414c4531SDave Airlie 	return 0;
461414c4531SDave Airlie }
462414c4531SDave Airlie 
463414c4531SDave Airlie static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
464414c4531SDave Airlie {
465414c4531SDave Airlie 	unsigned int vcomax, vcomin, pllreffreq;
466414c4531SDave Airlie 	unsigned int delta, tmpdelta;
4679830605dSDave Airlie 	int testr, testn, testm, testo;
468414c4531SDave Airlie 	unsigned int p, m, n;
4699830605dSDave Airlie 	unsigned int computed, vco;
470414c4531SDave Airlie 	int tmp;
4719830605dSDave Airlie 	const unsigned int m_div_val[] = { 1, 2, 4, 8 };
472414c4531SDave Airlie 
473414c4531SDave Airlie 	m = n = p = 0;
474414c4531SDave Airlie 	vcomax = 1488000;
475414c4531SDave Airlie 	vcomin = 1056000;
476414c4531SDave Airlie 	pllreffreq = 48000;
477414c4531SDave Airlie 
478414c4531SDave Airlie 	delta = 0xffffffff;
479414c4531SDave Airlie 
480414c4531SDave Airlie 	for (testr = 0; testr < 4; testr++) {
481414c4531SDave Airlie 		if (delta == 0)
482414c4531SDave Airlie 			break;
483414c4531SDave Airlie 		for (testn = 5; testn < 129; testn++) {
484414c4531SDave Airlie 			if (delta == 0)
485414c4531SDave Airlie 				break;
486414c4531SDave Airlie 			for (testm = 3; testm >= 0; testm--) {
487414c4531SDave Airlie 				if (delta == 0)
488414c4531SDave Airlie 					break;
489414c4531SDave Airlie 				for (testo = 5; testo < 33; testo++) {
4909830605dSDave Airlie 					vco = pllreffreq * (testn + 1) /
491414c4531SDave Airlie 						(testr + 1);
4929830605dSDave Airlie 					if (vco < vcomin)
493414c4531SDave Airlie 						continue;
4949830605dSDave Airlie 					if (vco > vcomax)
495414c4531SDave Airlie 						continue;
4969830605dSDave Airlie 					computed = vco / (m_div_val[testm] * (testo + 1));
497414c4531SDave Airlie 					if (computed > clock)
498414c4531SDave Airlie 						tmpdelta = computed - clock;
499414c4531SDave Airlie 					else
500414c4531SDave Airlie 						tmpdelta = clock - computed;
501414c4531SDave Airlie 					if (tmpdelta < delta) {
502414c4531SDave Airlie 						delta = tmpdelta;
503414c4531SDave Airlie 						m = testm | (testo << 3);
504414c4531SDave Airlie 						n = testn;
505414c4531SDave Airlie 						p = testr | (testr << 3);
506414c4531SDave Airlie 					}
507414c4531SDave Airlie 				}
508414c4531SDave Airlie 			}
509414c4531SDave Airlie 		}
510414c4531SDave Airlie 	}
511414c4531SDave Airlie 
512414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
513414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
514414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
515fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
516414c4531SDave Airlie 
517414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
518414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
519414c4531SDave Airlie 	tmp |= MGA1064_REMHEADCTL_CLKDIS;
520fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
521414c4531SDave Airlie 
522414c4531SDave Airlie 	tmp = RREG8(MGAREG_MEM_MISC_READ);
523414c4531SDave Airlie 	tmp |= (0x3<<2) | 0xc0;
524414c4531SDave Airlie 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
525414c4531SDave Airlie 
526414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
527414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
528414c4531SDave Airlie 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
529414c4531SDave Airlie 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
530fb70a669SChristopher Harvey 	WREG8(DAC_DATA, tmp);
531414c4531SDave Airlie 
532414c4531SDave Airlie 	udelay(500);
533414c4531SDave Airlie 
534414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
535414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
536414c4531SDave Airlie 	WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
537414c4531SDave Airlie 
538414c4531SDave Airlie 	udelay(50);
539414c4531SDave Airlie 
540414c4531SDave Airlie 	return 0;
541414c4531SDave Airlie }
542414c4531SDave Airlie 
543414c4531SDave Airlie static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
544414c4531SDave Airlie {
545414c4531SDave Airlie 	switch(mdev->type) {
546414c4531SDave Airlie 	case G200_SE_A:
547414c4531SDave Airlie 	case G200_SE_B:
548414c4531SDave Airlie 		return mga_g200se_set_plls(mdev, clock);
549414c4531SDave Airlie 		break;
550414c4531SDave Airlie 	case G200_WB:
551414c4531SDave Airlie 		return mga_g200wb_set_plls(mdev, clock);
552414c4531SDave Airlie 		break;
553414c4531SDave Airlie 	case G200_EV:
554414c4531SDave Airlie 		return mga_g200ev_set_plls(mdev, clock);
555414c4531SDave Airlie 		break;
556414c4531SDave Airlie 	case G200_EH:
557414c4531SDave Airlie 		return mga_g200eh_set_plls(mdev, clock);
558414c4531SDave Airlie 		break;
559414c4531SDave Airlie 	case G200_ER:
560414c4531SDave Airlie 		return mga_g200er_set_plls(mdev, clock);
561414c4531SDave Airlie 		break;
562414c4531SDave Airlie 	}
563414c4531SDave Airlie 	return 0;
564414c4531SDave Airlie }
565414c4531SDave Airlie 
566414c4531SDave Airlie static void mga_g200wb_prepare(struct drm_crtc *crtc)
567414c4531SDave Airlie {
568414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
569414c4531SDave Airlie 	u8 tmp;
570414c4531SDave Airlie 	int iter_max;
571414c4531SDave Airlie 
572414c4531SDave Airlie 	/* 1- The first step is to warn the BMC of an upcoming mode change.
573414c4531SDave Airlie 	 * We are putting the misc<0> to output.*/
574414c4531SDave Airlie 
575414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
576414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
577414c4531SDave Airlie 	tmp |= 0x10;
578414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
579414c4531SDave Airlie 
580414c4531SDave Airlie 	/* we are putting a 1 on the misc<0> line */
581414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
582414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
583414c4531SDave Airlie 	tmp |= 0x10;
584414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
585414c4531SDave Airlie 
586414c4531SDave Airlie 	/* 2- Second step to mask and further scan request
587414c4531SDave Airlie 	 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
588414c4531SDave Airlie 	 */
589414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
590414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
591414c4531SDave Airlie 	tmp |= 0x80;
592414c4531SDave Airlie 	WREG_DAC(MGA1064_SPAREREG, tmp);
593414c4531SDave Airlie 
594414c4531SDave Airlie 	/* 3a- the third step is to verifu if there is an active scan
595414c4531SDave Airlie 	 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
596414c4531SDave Airlie 	 */
597414c4531SDave Airlie 	iter_max = 300;
598414c4531SDave Airlie 	while (!(tmp & 0x1) && iter_max) {
599414c4531SDave Airlie 		WREG8(DAC_INDEX, MGA1064_SPAREREG);
600414c4531SDave Airlie 		tmp = RREG8(DAC_DATA);
601414c4531SDave Airlie 		udelay(1000);
602414c4531SDave Airlie 		iter_max--;
603414c4531SDave Airlie 	}
604414c4531SDave Airlie 
605414c4531SDave Airlie 	/* 3b- this step occurs only if the remove is actually scanning
606414c4531SDave Airlie 	 * we are waiting for the end of the frame which is a 1 on
607414c4531SDave Airlie 	 * remvsyncsts (XSPAREREG<1>)
608414c4531SDave Airlie 	 */
609414c4531SDave Airlie 	if (iter_max) {
610414c4531SDave Airlie 		iter_max = 300;
611414c4531SDave Airlie 		while ((tmp & 0x2) && iter_max) {
612414c4531SDave Airlie 			WREG8(DAC_INDEX, MGA1064_SPAREREG);
613414c4531SDave Airlie 			tmp = RREG8(DAC_DATA);
614414c4531SDave Airlie 			udelay(1000);
615414c4531SDave Airlie 			iter_max--;
616414c4531SDave Airlie 		}
617414c4531SDave Airlie 	}
618414c4531SDave Airlie }
619414c4531SDave Airlie 
620414c4531SDave Airlie static void mga_g200wb_commit(struct drm_crtc *crtc)
621414c4531SDave Airlie {
622414c4531SDave Airlie 	u8 tmp;
623414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
624414c4531SDave Airlie 
625414c4531SDave Airlie 	/* 1- The first step is to ensure that the vrsten and hrsten are set */
626414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 1);
627414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTCEXT_DATA);
628414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
629414c4531SDave Airlie 
630414c4531SDave Airlie 	/* 2- second step is to assert the rstlvl2 */
631414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
632414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
633414c4531SDave Airlie 	tmp |= 0x8;
634414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
635414c4531SDave Airlie 
636414c4531SDave Airlie 	/* wait 10 us */
637414c4531SDave Airlie 	udelay(10);
638414c4531SDave Airlie 
639414c4531SDave Airlie 	/* 3- deassert rstlvl2 */
640414c4531SDave Airlie 	tmp &= ~0x08;
641414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
642414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
643414c4531SDave Airlie 
644414c4531SDave Airlie 	/* 4- remove mask of scan request */
645414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
646414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
647414c4531SDave Airlie 	tmp &= ~0x80;
648414c4531SDave Airlie 	WREG8(DAC_DATA, tmp);
649414c4531SDave Airlie 
650414c4531SDave Airlie 	/* 5- put back a 0 on the misc<0> line */
651414c4531SDave Airlie 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
652414c4531SDave Airlie 	tmp = RREG8(DAC_DATA);
653414c4531SDave Airlie 	tmp &= ~0x10;
654414c4531SDave Airlie 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
655414c4531SDave Airlie }
656414c4531SDave Airlie 
6579f1d0366SChristopher Harvey /*
6589f1d0366SChristopher Harvey    This is how the framebuffer base address is stored in g200 cards:
6599f1d0366SChristopher Harvey    * Assume @offset is the gpu_addr variable of the framebuffer object
6609f1d0366SChristopher Harvey    * Then addr is the number of _pixels_ (not bytes) from the start of
6619f1d0366SChristopher Harvey      VRAM to the first pixel we want to display. (divided by 2 for 32bit
6629f1d0366SChristopher Harvey      framebuffers)
6639f1d0366SChristopher Harvey    * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
6649f1d0366SChristopher Harvey    addr<20> -> CRTCEXT0<6>
6659f1d0366SChristopher Harvey    addr<19-16> -> CRTCEXT0<3-0>
6669f1d0366SChristopher Harvey    addr<15-8> -> CRTCC<7-0>
6679f1d0366SChristopher Harvey    addr<7-0> -> CRTCD<7-0>
6689f1d0366SChristopher Harvey    CRTCEXT0 has to be programmed last to trigger an update and make the
6699f1d0366SChristopher Harvey    new addr variable take effect.
6709f1d0366SChristopher Harvey  */
671414c4531SDave Airlie void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
672414c4531SDave Airlie {
673414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
674414c4531SDave Airlie 	u32 addr;
675414c4531SDave Airlie 	int count;
6769f1d0366SChristopher Harvey 	u8 crtcext0;
677414c4531SDave Airlie 
678414c4531SDave Airlie 	while (RREG8(0x1fda) & 0x08);
679414c4531SDave Airlie 	while (!(RREG8(0x1fda) & 0x08));
680414c4531SDave Airlie 
681414c4531SDave Airlie 	count = RREG8(MGAREG_VCOUNT) + 2;
682414c4531SDave Airlie 	while (RREG8(MGAREG_VCOUNT) < count);
683414c4531SDave Airlie 
6849f1d0366SChristopher Harvey 	WREG8(MGAREG_CRTCEXT_INDEX, 0);
6859f1d0366SChristopher Harvey 	crtcext0 = RREG8(MGAREG_CRTCEXT_DATA);
6869f1d0366SChristopher Harvey 	crtcext0 &= 0xB0;
6879f1d0366SChristopher Harvey 	addr = offset / 8;
6889f1d0366SChristopher Harvey 	/* Can't store addresses any higher than that...
6899f1d0366SChristopher Harvey 	   but we also don't have more than 16MB of memory, so it should be fine. */
6909f1d0366SChristopher Harvey 	WARN_ON(addr > 0x1fffff);
6919f1d0366SChristopher Harvey 	crtcext0 |= (!!(addr & (1<<20)))<<6;
692414c4531SDave Airlie 	WREG_CRT(0x0d, (u8)(addr & 0xff));
693414c4531SDave Airlie 	WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
6949f1d0366SChristopher Harvey 	WREG_ECRT(0x0, ((u8)(addr >> 16) & 0xf) | crtcext0);
695414c4531SDave Airlie }
696414c4531SDave Airlie 
697414c4531SDave Airlie 
698414c4531SDave Airlie /* ast is different - we will force move buffers out of VRAM */
699414c4531SDave Airlie static int mga_crtc_do_set_base(struct drm_crtc *crtc,
700414c4531SDave Airlie 				struct drm_framebuffer *fb,
701414c4531SDave Airlie 				int x, int y, int atomic)
702414c4531SDave Airlie {
703414c4531SDave Airlie 	struct mga_device *mdev = crtc->dev->dev_private;
704414c4531SDave Airlie 	struct drm_gem_object *obj;
705414c4531SDave Airlie 	struct mga_framebuffer *mga_fb;
706414c4531SDave Airlie 	struct mgag200_bo *bo;
707414c4531SDave Airlie 	int ret;
708414c4531SDave Airlie 	u64 gpu_addr;
709414c4531SDave Airlie 
710414c4531SDave Airlie 	/* push the previous fb to system ram */
711414c4531SDave Airlie 	if (!atomic && fb) {
712414c4531SDave Airlie 		mga_fb = to_mga_framebuffer(fb);
713414c4531SDave Airlie 		obj = mga_fb->obj;
714414c4531SDave Airlie 		bo = gem_to_mga_bo(obj);
715414c4531SDave Airlie 		ret = mgag200_bo_reserve(bo, false);
716414c4531SDave Airlie 		if (ret)
717414c4531SDave Airlie 			return ret;
718414c4531SDave Airlie 		mgag200_bo_push_sysram(bo);
719414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
720414c4531SDave Airlie 	}
721414c4531SDave Airlie 
722414c4531SDave Airlie 	mga_fb = to_mga_framebuffer(crtc->fb);
723414c4531SDave Airlie 	obj = mga_fb->obj;
724414c4531SDave Airlie 	bo = gem_to_mga_bo(obj);
725414c4531SDave Airlie 
726414c4531SDave Airlie 	ret = mgag200_bo_reserve(bo, false);
727414c4531SDave Airlie 	if (ret)
728414c4531SDave Airlie 		return ret;
729414c4531SDave Airlie 
730414c4531SDave Airlie 	ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
731414c4531SDave Airlie 	if (ret) {
732414c4531SDave Airlie 		mgag200_bo_unreserve(bo);
733414c4531SDave Airlie 		return ret;
734414c4531SDave Airlie 	}
735414c4531SDave Airlie 
736414c4531SDave Airlie 	if (&mdev->mfbdev->mfb == mga_fb) {
737414c4531SDave Airlie 		/* if pushing console in kmap it */
738414c4531SDave Airlie 		ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
739414c4531SDave Airlie 		if (ret)
740414c4531SDave Airlie 			DRM_ERROR("failed to kmap fbcon\n");
741414c4531SDave Airlie 
742414c4531SDave Airlie 	}
743414c4531SDave Airlie 	mgag200_bo_unreserve(bo);
744414c4531SDave Airlie 
745414c4531SDave Airlie 	DRM_INFO("mga base %llx\n", gpu_addr);
746414c4531SDave Airlie 
747414c4531SDave Airlie 	mga_set_start_address(crtc, (u32)gpu_addr);
748414c4531SDave Airlie 
749414c4531SDave Airlie 	return 0;
750414c4531SDave Airlie }
751414c4531SDave Airlie 
752414c4531SDave Airlie static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
753414c4531SDave Airlie 				  struct drm_framebuffer *old_fb)
754414c4531SDave Airlie {
755414c4531SDave Airlie 	return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
756414c4531SDave Airlie }
757414c4531SDave Airlie 
758414c4531SDave Airlie static int mga_crtc_mode_set(struct drm_crtc *crtc,
759414c4531SDave Airlie 				struct drm_display_mode *mode,
760414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode,
761414c4531SDave Airlie 				int x, int y, struct drm_framebuffer *old_fb)
762414c4531SDave Airlie {
763414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
764414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
765414c4531SDave Airlie 	int hdisplay, hsyncstart, hsyncend, htotal;
766414c4531SDave Airlie 	int vdisplay, vsyncstart, vsyncend, vtotal;
767414c4531SDave Airlie 	int pitch;
768414c4531SDave Airlie 	int option = 0, option2 = 0;
769414c4531SDave Airlie 	int i;
770414c4531SDave Airlie 	unsigned char misc = 0;
771414c4531SDave Airlie 	unsigned char ext_vga[6];
772414c4531SDave Airlie 	u8 bppshift;
773414c4531SDave Airlie 
774414c4531SDave Airlie 	static unsigned char dacvalue[] = {
775414c4531SDave Airlie 		/* 0x00: */        0,    0,    0,    0,    0,    0, 0x00,    0,
776414c4531SDave Airlie 		/* 0x08: */        0,    0,    0,    0,    0,    0,    0,    0,
777414c4531SDave Airlie 		/* 0x10: */        0,    0,    0,    0,    0,    0,    0,    0,
778414c4531SDave Airlie 		/* 0x18: */     0x00,    0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
779414c4531SDave Airlie 		/* 0x20: */     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
780414c4531SDave Airlie 		/* 0x28: */     0x00, 0x00, 0x00, 0x00,    0,    0,    0, 0x40,
781414c4531SDave Airlie 		/* 0x30: */     0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
782414c4531SDave Airlie 		/* 0x38: */     0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
783414c4531SDave Airlie 		/* 0x40: */        0,    0,    0,    0,    0,    0,    0,    0,
784414c4531SDave Airlie 		/* 0x48: */        0,    0,    0,    0,    0,    0,    0,    0
785414c4531SDave Airlie 	};
786414c4531SDave Airlie 
787414c4531SDave Airlie 	bppshift = mdev->bpp_shifts[(crtc->fb->bits_per_pixel >> 3) - 1];
788414c4531SDave Airlie 
789414c4531SDave Airlie 	switch (mdev->type) {
790414c4531SDave Airlie 	case G200_SE_A:
791414c4531SDave Airlie 	case G200_SE_B:
792414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x03;
793414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
794414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
795414c4531SDave Airlie 					     MGA1064_MISC_CTL_VGA8 |
796414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
797414c4531SDave Airlie 		if (mdev->has_sdram)
798414c4531SDave Airlie 			option = 0x40049120;
799414c4531SDave Airlie 		else
800414c4531SDave Airlie 			option = 0x4004d120;
801414c4531SDave Airlie 		option2 = 0x00008000;
802414c4531SDave Airlie 		break;
803414c4531SDave Airlie 	case G200_WB:
804414c4531SDave Airlie 		dacvalue[MGA1064_VREF_CTL] = 0x07;
805414c4531SDave Airlie 		option = 0x41049120;
806414c4531SDave Airlie 		option2 = 0x0000b000;
807414c4531SDave Airlie 		break;
808414c4531SDave Airlie 	case G200_EV:
809414c4531SDave Airlie 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
810414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
811414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
812414c4531SDave Airlie 		option = 0x00000120;
813414c4531SDave Airlie 		option2 = 0x0000b000;
814414c4531SDave Airlie 		break;
815414c4531SDave Airlie 	case G200_EH:
816414c4531SDave Airlie 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
817414c4531SDave Airlie 					     MGA1064_MISC_CTL_DAC_RAM_CS;
818414c4531SDave Airlie 		option = 0x00000120;
819414c4531SDave Airlie 		option2 = 0x0000b000;
820414c4531SDave Airlie 		break;
821414c4531SDave Airlie 	case G200_ER:
822414c4531SDave Airlie 		break;
823414c4531SDave Airlie 	}
824414c4531SDave Airlie 
825414c4531SDave Airlie 	switch (crtc->fb->bits_per_pixel) {
826414c4531SDave Airlie 	case 8:
827414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
828414c4531SDave Airlie 		break;
829414c4531SDave Airlie 	case 16:
830414c4531SDave Airlie 		if (crtc->fb->depth == 15)
831414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
832414c4531SDave Airlie 		else
833414c4531SDave Airlie 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
834414c4531SDave Airlie 		break;
835414c4531SDave Airlie 	case 24:
836414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
837414c4531SDave Airlie 		break;
838414c4531SDave Airlie 	case 32:
839414c4531SDave Airlie 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
840414c4531SDave Airlie 		break;
841414c4531SDave Airlie 	}
842414c4531SDave Airlie 
843414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
844414c4531SDave Airlie 		misc |= 0x40;
845414c4531SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
846414c4531SDave Airlie 		misc |= 0x80;
847414c4531SDave Airlie 
848414c4531SDave Airlie 
849414c4531SDave Airlie 	for (i = 0; i < sizeof(dacvalue); i++) {
8509d8aa55fSChristopher Harvey 		if ((i <= 0x17) ||
851414c4531SDave Airlie 		    (i == 0x1b) ||
852414c4531SDave Airlie 		    (i == 0x1c) ||
853414c4531SDave Airlie 		    ((i >= 0x1f) && (i <= 0x29)) ||
854414c4531SDave Airlie 		    ((i >= 0x30) && (i <= 0x37)))
855414c4531SDave Airlie 			continue;
856414c4531SDave Airlie 		if (IS_G200_SE(mdev) &&
857414c4531SDave Airlie 		    ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
858414c4531SDave Airlie 			continue;
859414c4531SDave Airlie 		if ((mdev->type == G200_EV || mdev->type == G200_WB || mdev->type == G200_EH) &&
860414c4531SDave Airlie 		    (i >= 0x44) && (i <= 0x4e))
861414c4531SDave Airlie 			continue;
862414c4531SDave Airlie 
863414c4531SDave Airlie 		WREG_DAC(i, dacvalue[i]);
864414c4531SDave Airlie 	}
865414c4531SDave Airlie 
8661812a3dbSChristopher Harvey 	if (mdev->type == G200_ER)
8671812a3dbSChristopher Harvey 		WREG_DAC(0x90, 0);
868414c4531SDave Airlie 
869414c4531SDave Airlie 	if (option)
870414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
871414c4531SDave Airlie 	if (option2)
872414c4531SDave Airlie 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
873414c4531SDave Airlie 
874414c4531SDave Airlie 	WREG_SEQ(2, 0xf);
875414c4531SDave Airlie 	WREG_SEQ(3, 0);
876414c4531SDave Airlie 	WREG_SEQ(4, 0xe);
877414c4531SDave Airlie 
878414c4531SDave Airlie 	pitch = crtc->fb->pitches[0] / (crtc->fb->bits_per_pixel / 8);
879414c4531SDave Airlie 	if (crtc->fb->bits_per_pixel == 24)
880*da558398STakashi Iwai 		pitch = (pitch * 3) >> (4 - bppshift);
881414c4531SDave Airlie 	else
882414c4531SDave Airlie 		pitch = pitch >> (4 - bppshift);
883414c4531SDave Airlie 
884414c4531SDave Airlie 	hdisplay = mode->hdisplay / 8 - 1;
885414c4531SDave Airlie 	hsyncstart = mode->hsync_start / 8 - 1;
886414c4531SDave Airlie 	hsyncend = mode->hsync_end / 8 - 1;
887414c4531SDave Airlie 	htotal = mode->htotal / 8 - 1;
888414c4531SDave Airlie 
889414c4531SDave Airlie 	/* Work around hardware quirk */
890414c4531SDave Airlie 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
891414c4531SDave Airlie 		htotal++;
892414c4531SDave Airlie 
893414c4531SDave Airlie 	vdisplay = mode->vdisplay - 1;
894414c4531SDave Airlie 	vsyncstart = mode->vsync_start - 1;
895414c4531SDave Airlie 	vsyncend = mode->vsync_end - 1;
896414c4531SDave Airlie 	vtotal = mode->vtotal - 2;
897414c4531SDave Airlie 
898414c4531SDave Airlie 	WREG_GFX(0, 0);
899414c4531SDave Airlie 	WREG_GFX(1, 0);
900414c4531SDave Airlie 	WREG_GFX(2, 0);
901414c4531SDave Airlie 	WREG_GFX(3, 0);
902414c4531SDave Airlie 	WREG_GFX(4, 0);
903414c4531SDave Airlie 	WREG_GFX(5, 0x40);
904414c4531SDave Airlie 	WREG_GFX(6, 0x5);
905414c4531SDave Airlie 	WREG_GFX(7, 0xf);
906414c4531SDave Airlie 	WREG_GFX(8, 0xf);
907414c4531SDave Airlie 
908414c4531SDave Airlie 	WREG_CRT(0, htotal - 4);
909414c4531SDave Airlie 	WREG_CRT(1, hdisplay);
910414c4531SDave Airlie 	WREG_CRT(2, hdisplay);
911414c4531SDave Airlie 	WREG_CRT(3, (htotal & 0x1F) | 0x80);
912414c4531SDave Airlie 	WREG_CRT(4, hsyncstart);
913414c4531SDave Airlie 	WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
914414c4531SDave Airlie 	WREG_CRT(6, vtotal & 0xFF);
915414c4531SDave Airlie 	WREG_CRT(7, ((vtotal & 0x100) >> 8) |
916414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 7) |
917414c4531SDave Airlie 		 ((vsyncstart & 0x100) >> 6) |
918414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 5) |
919414c4531SDave Airlie 		 ((vdisplay & 0x100) >> 4) | /* linecomp */
920414c4531SDave Airlie 		 ((vtotal & 0x200) >> 4)|
921414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3) |
922414c4531SDave Airlie 		 ((vsyncstart & 0x200) >> 2));
923414c4531SDave Airlie 	WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
924414c4531SDave Airlie 		 ((vdisplay & 0x200) >> 3));
925414c4531SDave Airlie 	WREG_CRT(10, 0);
926414c4531SDave Airlie 	WREG_CRT(11, 0);
927414c4531SDave Airlie 	WREG_CRT(12, 0);
928414c4531SDave Airlie 	WREG_CRT(13, 0);
929414c4531SDave Airlie 	WREG_CRT(14, 0);
930414c4531SDave Airlie 	WREG_CRT(15, 0);
931414c4531SDave Airlie 	WREG_CRT(16, vsyncstart & 0xFF);
932414c4531SDave Airlie 	WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
933414c4531SDave Airlie 	WREG_CRT(18, vdisplay & 0xFF);
934414c4531SDave Airlie 	WREG_CRT(19, pitch & 0xFF);
935414c4531SDave Airlie 	WREG_CRT(20, 0);
936414c4531SDave Airlie 	WREG_CRT(21, vdisplay & 0xFF);
937414c4531SDave Airlie 	WREG_CRT(22, (vtotal + 1) & 0xFF);
938414c4531SDave Airlie 	WREG_CRT(23, 0xc3);
939414c4531SDave Airlie 	WREG_CRT(24, vdisplay & 0xFF);
940414c4531SDave Airlie 
941414c4531SDave Airlie 	ext_vga[0] = 0;
942414c4531SDave Airlie 	ext_vga[5] = 0;
943414c4531SDave Airlie 
944414c4531SDave Airlie 	/* TODO interlace */
945414c4531SDave Airlie 
946414c4531SDave Airlie 	ext_vga[0] |= (pitch & 0x300) >> 4;
947414c4531SDave Airlie 	ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
948414c4531SDave Airlie 		((hdisplay & 0x100) >> 7) |
949414c4531SDave Airlie 		((hsyncstart & 0x100) >> 6) |
950414c4531SDave Airlie 		(htotal & 0x40);
951414c4531SDave Airlie 	ext_vga[2] = ((vtotal & 0xc00) >> 10) |
952414c4531SDave Airlie 		((vdisplay & 0x400) >> 8) |
953414c4531SDave Airlie 		((vdisplay & 0xc00) >> 7) |
954414c4531SDave Airlie 		((vsyncstart & 0xc00) >> 5) |
955414c4531SDave Airlie 		((vdisplay & 0x400) >> 3);
956414c4531SDave Airlie 	if (crtc->fb->bits_per_pixel == 24)
957414c4531SDave Airlie 		ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
958414c4531SDave Airlie 	else
959414c4531SDave Airlie 		ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
960414c4531SDave Airlie 	ext_vga[4] = 0;
961414c4531SDave Airlie 	if (mdev->type == G200_WB)
962414c4531SDave Airlie 		ext_vga[1] |= 0x88;
963414c4531SDave Airlie 
964414c4531SDave Airlie 	/* Set pixel clocks */
965414c4531SDave Airlie 	misc = 0x2d;
966414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
967414c4531SDave Airlie 
968414c4531SDave Airlie 	mga_crtc_set_plls(mdev, mode->clock);
969414c4531SDave Airlie 
970414c4531SDave Airlie 	for (i = 0; i < 6; i++) {
971414c4531SDave Airlie 		WREG_ECRT(i, ext_vga[i]);
972414c4531SDave Airlie 	}
973414c4531SDave Airlie 
974414c4531SDave Airlie 	if (mdev->type == G200_ER)
9751812a3dbSChristopher Harvey 		WREG_ECRT(0x24, 0x5);
976414c4531SDave Airlie 
977414c4531SDave Airlie 	if (mdev->type == G200_EV) {
978414c4531SDave Airlie 		WREG_ECRT(6, 0);
979414c4531SDave Airlie 	}
980414c4531SDave Airlie 
981414c4531SDave Airlie 	WREG_ECRT(0, ext_vga[0]);
982414c4531SDave Airlie 	/* Enable mga pixel clock */
983414c4531SDave Airlie 	misc = 0x2d;
984414c4531SDave Airlie 
985414c4531SDave Airlie 	WREG8(MGA_MISC_OUT, misc);
986414c4531SDave Airlie 
987414c4531SDave Airlie 	if (adjusted_mode)
988414c4531SDave Airlie 		memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
989414c4531SDave Airlie 
990414c4531SDave Airlie 	mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
991414c4531SDave Airlie 
992414c4531SDave Airlie 	/* reset tagfifo */
993414c4531SDave Airlie 	if (mdev->type == G200_ER) {
994414c4531SDave Airlie 		u32 mem_ctl = RREG32(MGAREG_MEMCTL);
995414c4531SDave Airlie 		u8 seq1;
996414c4531SDave Airlie 
997414c4531SDave Airlie 		/* screen off */
998414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x01);
999414c4531SDave Airlie 		seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
1000414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1);
1001414c4531SDave Airlie 
1002414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
1003414c4531SDave Airlie 		udelay(1000);
1004414c4531SDave Airlie 		WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
1005414c4531SDave Airlie 
1006414c4531SDave Airlie 		WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
1007414c4531SDave Airlie 	}
1008414c4531SDave Airlie 
1009414c4531SDave Airlie 
1010414c4531SDave Airlie 	if (IS_G200_SE(mdev)) {
1011abbee623SJulia Lemire 		if (mdev->unique_rev_id >= 0x02) {
1012414c4531SDave Airlie 			u8 hi_pri_lvl;
1013414c4531SDave Airlie 			u32 bpp;
1014414c4531SDave Airlie 			u32 mb;
1015414c4531SDave Airlie 
1016414c4531SDave Airlie 			if (crtc->fb->bits_per_pixel > 16)
1017414c4531SDave Airlie 				bpp = 32;
1018414c4531SDave Airlie 			else if (crtc->fb->bits_per_pixel > 8)
1019414c4531SDave Airlie 				bpp = 16;
1020414c4531SDave Airlie 			else
1021414c4531SDave Airlie 				bpp = 8;
1022414c4531SDave Airlie 
1023414c4531SDave Airlie 			mb = (mode->clock * bpp) / 1000;
1024414c4531SDave Airlie 			if (mb > 3100)
1025414c4531SDave Airlie 				hi_pri_lvl = 0;
1026414c4531SDave Airlie 			else if (mb > 2600)
1027414c4531SDave Airlie 				hi_pri_lvl = 1;
1028414c4531SDave Airlie 			else if (mb > 1900)
1029414c4531SDave Airlie 				hi_pri_lvl = 2;
1030414c4531SDave Airlie 			else if (mb > 1160)
1031414c4531SDave Airlie 				hi_pri_lvl = 3;
1032414c4531SDave Airlie 			else if (mb > 440)
1033414c4531SDave Airlie 				hi_pri_lvl = 4;
1034414c4531SDave Airlie 			else
1035414c4531SDave Airlie 				hi_pri_lvl = 5;
1036414c4531SDave Airlie 
103791f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
103891f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl);
1039414c4531SDave Airlie 		} else {
104091f8f105SChristopher Harvey 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1041abbee623SJulia Lemire 			if (mdev->unique_rev_id >= 0x01)
104291f8f105SChristopher Harvey 				WREG8(MGAREG_CRTCEXT_DATA, 0x03);
1043414c4531SDave Airlie 			else
104491f8f105SChristopher Harvey 				WREG8(MGAREG_CRTCEXT_DATA, 0x04);
1045414c4531SDave Airlie 		}
1046414c4531SDave Airlie 	}
1047414c4531SDave Airlie 	return 0;
1048414c4531SDave Airlie }
1049414c4531SDave Airlie 
1050414c4531SDave Airlie #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1051414c4531SDave Airlie static int mga_suspend(struct drm_crtc *crtc)
1052414c4531SDave Airlie {
1053414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1054414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1055414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1056414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1057414c4531SDave Airlie 	int option;
1058414c4531SDave Airlie 
1059414c4531SDave Airlie 	if (mdev->suspended)
1060414c4531SDave Airlie 		return 0;
1061414c4531SDave Airlie 
1062414c4531SDave Airlie 	WREG_SEQ(1, 0x20);
1063414c4531SDave Airlie 	WREG_ECRT(1, 0x30);
1064414c4531SDave Airlie 	/* Disable the pixel clock */
1065414c4531SDave Airlie 	WREG_DAC(0x1a, 0x05);
1066414c4531SDave Airlie 	/* Power down the DAC */
1067414c4531SDave Airlie 	WREG_DAC(0x1e, 0x18);
1068414c4531SDave Airlie 	/* Power down the pixel PLL */
1069414c4531SDave Airlie 	WREG_DAC(0x1a, 0x0d);
1070414c4531SDave Airlie 
1071414c4531SDave Airlie 	/* Disable PLLs and clocks */
1072414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1073414c4531SDave Airlie 	option &= ~(0x1F8024);
1074414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1075414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D3hot);
1076414c4531SDave Airlie 	pci_disable_device(pdev);
1077414c4531SDave Airlie 
1078414c4531SDave Airlie 	mdev->suspended = true;
1079414c4531SDave Airlie 
1080414c4531SDave Airlie 	return 0;
1081414c4531SDave Airlie }
1082414c4531SDave Airlie 
1083414c4531SDave Airlie static int mga_resume(struct drm_crtc *crtc)
1084414c4531SDave Airlie {
1085414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1086414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1087414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1088414c4531SDave Airlie 	struct pci_dev *pdev = dev->pdev;
1089414c4531SDave Airlie 	int option;
1090414c4531SDave Airlie 
1091414c4531SDave Airlie 	if (!mdev->suspended)
1092414c4531SDave Airlie 		return 0;
1093414c4531SDave Airlie 
1094414c4531SDave Airlie 	pci_set_power_state(pdev, PCI_D0);
1095414c4531SDave Airlie 	pci_enable_device(pdev);
1096414c4531SDave Airlie 
1097414c4531SDave Airlie 	/* Disable sysclk */
1098414c4531SDave Airlie 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1099414c4531SDave Airlie 	option &= ~(0x4);
1100414c4531SDave Airlie 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1101414c4531SDave Airlie 
1102414c4531SDave Airlie 	mdev->suspended = false;
1103414c4531SDave Airlie 
1104414c4531SDave Airlie 	return 0;
1105414c4531SDave Airlie }
1106414c4531SDave Airlie 
1107414c4531SDave Airlie #endif
1108414c4531SDave Airlie 
1109414c4531SDave Airlie static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1110414c4531SDave Airlie {
1111414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1112414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1113414c4531SDave Airlie 	u8 seq1 = 0, crtcext1 = 0;
1114414c4531SDave Airlie 
1115414c4531SDave Airlie 	switch (mode) {
1116414c4531SDave Airlie 	case DRM_MODE_DPMS_ON:
1117414c4531SDave Airlie 		seq1 = 0;
1118414c4531SDave Airlie 		crtcext1 = 0;
1119414c4531SDave Airlie 		mga_crtc_load_lut(crtc);
1120414c4531SDave Airlie 		break;
1121414c4531SDave Airlie 	case DRM_MODE_DPMS_STANDBY:
1122414c4531SDave Airlie 		seq1 = 0x20;
1123414c4531SDave Airlie 		crtcext1 = 0x10;
1124414c4531SDave Airlie 		break;
1125414c4531SDave Airlie 	case DRM_MODE_DPMS_SUSPEND:
1126414c4531SDave Airlie 		seq1 = 0x20;
1127414c4531SDave Airlie 		crtcext1 = 0x20;
1128414c4531SDave Airlie 		break;
1129414c4531SDave Airlie 	case DRM_MODE_DPMS_OFF:
1130414c4531SDave Airlie 		seq1 = 0x20;
1131414c4531SDave Airlie 		crtcext1 = 0x30;
1132414c4531SDave Airlie 		break;
1133414c4531SDave Airlie 	}
1134414c4531SDave Airlie 
1135414c4531SDave Airlie #if 0
1136414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_OFF) {
1137414c4531SDave Airlie 		mga_suspend(crtc);
1138414c4531SDave Airlie 	}
1139414c4531SDave Airlie #endif
1140414c4531SDave Airlie 	WREG8(MGAREG_SEQ_INDEX, 0x01);
1141414c4531SDave Airlie 	seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1142414c4531SDave Airlie 	mga_wait_vsync(mdev);
1143414c4531SDave Airlie 	mga_wait_busy(mdev);
1144414c4531SDave Airlie 	WREG8(MGAREG_SEQ_DATA, seq1);
1145414c4531SDave Airlie 	msleep(20);
1146414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1147414c4531SDave Airlie 	crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1148414c4531SDave Airlie 	WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1149414c4531SDave Airlie 
1150414c4531SDave Airlie #if 0
1151414c4531SDave Airlie 	if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1152414c4531SDave Airlie 		mga_resume(crtc);
1153414c4531SDave Airlie 		drm_helper_resume_force_mode(dev);
1154414c4531SDave Airlie 	}
1155414c4531SDave Airlie #endif
1156414c4531SDave Airlie }
1157414c4531SDave Airlie 
1158414c4531SDave Airlie /*
1159414c4531SDave Airlie  * This is called before a mode is programmed. A typical use might be to
1160414c4531SDave Airlie  * enable DPMS during the programming to avoid seeing intermediate stages,
1161414c4531SDave Airlie  * but that's not relevant to us
1162414c4531SDave Airlie  */
1163414c4531SDave Airlie static void mga_crtc_prepare(struct drm_crtc *crtc)
1164414c4531SDave Airlie {
1165414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1166414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1167414c4531SDave Airlie 	u8 tmp;
1168414c4531SDave Airlie 
1169414c4531SDave Airlie 	/*	mga_resume(crtc);*/
1170414c4531SDave Airlie 
1171414c4531SDave Airlie 	WREG8(MGAREG_CRTC_INDEX, 0x11);
1172414c4531SDave Airlie 	tmp = RREG8(MGAREG_CRTC_DATA);
1173414c4531SDave Airlie 	WREG_CRT(0x11, tmp | 0x80);
1174414c4531SDave Airlie 
1175414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1176414c4531SDave Airlie 		WREG_SEQ(0, 1);
1177414c4531SDave Airlie 		msleep(50);
1178414c4531SDave Airlie 		WREG_SEQ(1, 0x20);
1179414c4531SDave Airlie 		msleep(20);
1180414c4531SDave Airlie 	} else {
1181414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1182414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1183414c4531SDave Airlie 
1184414c4531SDave Airlie 		/* start sync reset */
1185414c4531SDave Airlie 		WREG_SEQ(0, 1);
1186414c4531SDave Airlie 		WREG_SEQ(1, tmp | 0x20);
1187414c4531SDave Airlie 	}
1188414c4531SDave Airlie 
1189414c4531SDave Airlie 	if (mdev->type == G200_WB)
1190414c4531SDave Airlie 		mga_g200wb_prepare(crtc);
1191414c4531SDave Airlie 
1192414c4531SDave Airlie 	WREG_CRT(17, 0);
1193414c4531SDave Airlie }
1194414c4531SDave Airlie 
1195414c4531SDave Airlie /*
1196414c4531SDave Airlie  * This is called after a mode is programmed. It should reverse anything done
1197414c4531SDave Airlie  * by the prepare function
1198414c4531SDave Airlie  */
1199414c4531SDave Airlie static void mga_crtc_commit(struct drm_crtc *crtc)
1200414c4531SDave Airlie {
1201414c4531SDave Airlie 	struct drm_device *dev = crtc->dev;
1202414c4531SDave Airlie 	struct mga_device *mdev = dev->dev_private;
1203414c4531SDave Airlie 	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1204414c4531SDave Airlie 	u8 tmp;
1205414c4531SDave Airlie 
1206414c4531SDave Airlie 	if (mdev->type == G200_WB)
1207414c4531SDave Airlie 		mga_g200wb_commit(crtc);
1208414c4531SDave Airlie 
1209414c4531SDave Airlie 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1210414c4531SDave Airlie 		msleep(50);
1211414c4531SDave Airlie 		WREG_SEQ(1, 0x0);
1212414c4531SDave Airlie 		msleep(20);
1213414c4531SDave Airlie 		WREG_SEQ(0, 0x3);
1214414c4531SDave Airlie 	} else {
1215414c4531SDave Airlie 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1216414c4531SDave Airlie 		tmp = RREG8(MGAREG_SEQ_DATA);
1217414c4531SDave Airlie 
1218414c4531SDave Airlie 		tmp &= ~0x20;
1219414c4531SDave Airlie 		WREG_SEQ(0x1, tmp);
1220414c4531SDave Airlie 		WREG_SEQ(0, 3);
1221414c4531SDave Airlie 	}
1222414c4531SDave Airlie 	crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1223414c4531SDave Airlie }
1224414c4531SDave Airlie 
1225414c4531SDave Airlie /*
1226414c4531SDave Airlie  * The core can pass us a set of gamma values to program. We actually only
1227414c4531SDave Airlie  * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1228414c4531SDave Airlie  * but it's a requirement that we provide the function
1229414c4531SDave Airlie  */
1230414c4531SDave Airlie static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1231414c4531SDave Airlie 				  u16 *blue, uint32_t start, uint32_t size)
1232414c4531SDave Airlie {
1233414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1234414c4531SDave Airlie 	int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
1235414c4531SDave Airlie 	int i;
1236414c4531SDave Airlie 
1237414c4531SDave Airlie 	for (i = start; i < end; i++) {
1238414c4531SDave Airlie 		mga_crtc->lut_r[i] = red[i] >> 8;
1239414c4531SDave Airlie 		mga_crtc->lut_g[i] = green[i] >> 8;
1240414c4531SDave Airlie 		mga_crtc->lut_b[i] = blue[i] >> 8;
1241414c4531SDave Airlie 	}
1242414c4531SDave Airlie 	mga_crtc_load_lut(crtc);
1243414c4531SDave Airlie }
1244414c4531SDave Airlie 
1245414c4531SDave Airlie /* Simple cleanup function */
1246414c4531SDave Airlie static void mga_crtc_destroy(struct drm_crtc *crtc)
1247414c4531SDave Airlie {
1248414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1249414c4531SDave Airlie 
1250414c4531SDave Airlie 	drm_crtc_cleanup(crtc);
1251414c4531SDave Airlie 	kfree(mga_crtc);
1252414c4531SDave Airlie }
1253414c4531SDave Airlie 
125464c29076SEgbert Eich static void mga_crtc_disable(struct drm_crtc *crtc)
125564c29076SEgbert Eich {
125664c29076SEgbert Eich 	int ret;
125764c29076SEgbert Eich 	DRM_DEBUG_KMS("\n");
125864c29076SEgbert Eich 	mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
125964c29076SEgbert Eich 	if (crtc->fb) {
126064c29076SEgbert Eich 		struct mga_framebuffer *mga_fb = to_mga_framebuffer(crtc->fb);
126164c29076SEgbert Eich 		struct drm_gem_object *obj = mga_fb->obj;
126264c29076SEgbert Eich 		struct mgag200_bo *bo = gem_to_mga_bo(obj);
126364c29076SEgbert Eich 		ret = mgag200_bo_reserve(bo, false);
126464c29076SEgbert Eich 		if (ret)
126564c29076SEgbert Eich 			return;
126664c29076SEgbert Eich 		mgag200_bo_push_sysram(bo);
126764c29076SEgbert Eich 		mgag200_bo_unreserve(bo);
126864c29076SEgbert Eich 	}
126964c29076SEgbert Eich 	crtc->fb = NULL;
127064c29076SEgbert Eich }
127164c29076SEgbert Eich 
1272414c4531SDave Airlie /* These provide the minimum set of functions required to handle a CRTC */
1273414c4531SDave Airlie static const struct drm_crtc_funcs mga_crtc_funcs = {
1274a080db9fSChristopher Harvey 	.cursor_set = mga_crtc_cursor_set,
1275a080db9fSChristopher Harvey 	.cursor_move = mga_crtc_cursor_move,
1276414c4531SDave Airlie 	.gamma_set = mga_crtc_gamma_set,
1277414c4531SDave Airlie 	.set_config = drm_crtc_helper_set_config,
1278414c4531SDave Airlie 	.destroy = mga_crtc_destroy,
1279414c4531SDave Airlie };
1280414c4531SDave Airlie 
1281414c4531SDave Airlie static const struct drm_crtc_helper_funcs mga_helper_funcs = {
128264c29076SEgbert Eich 	.disable = mga_crtc_disable,
1283414c4531SDave Airlie 	.dpms = mga_crtc_dpms,
1284414c4531SDave Airlie 	.mode_fixup = mga_crtc_mode_fixup,
1285414c4531SDave Airlie 	.mode_set = mga_crtc_mode_set,
1286414c4531SDave Airlie 	.mode_set_base = mga_crtc_mode_set_base,
1287414c4531SDave Airlie 	.prepare = mga_crtc_prepare,
1288414c4531SDave Airlie 	.commit = mga_crtc_commit,
1289414c4531SDave Airlie 	.load_lut = mga_crtc_load_lut,
1290414c4531SDave Airlie };
1291414c4531SDave Airlie 
1292414c4531SDave Airlie /* CRTC setup */
1293f1998fe2SChristopher Harvey static void mga_crtc_init(struct mga_device *mdev)
1294414c4531SDave Airlie {
1295414c4531SDave Airlie 	struct mga_crtc *mga_crtc;
1296414c4531SDave Airlie 	int i;
1297414c4531SDave Airlie 
1298414c4531SDave Airlie 	mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1299414c4531SDave Airlie 			      (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1300414c4531SDave Airlie 			      GFP_KERNEL);
1301414c4531SDave Airlie 
1302414c4531SDave Airlie 	if (mga_crtc == NULL)
1303414c4531SDave Airlie 		return;
1304414c4531SDave Airlie 
1305f1998fe2SChristopher Harvey 	drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
1306414c4531SDave Airlie 
1307414c4531SDave Airlie 	drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1308414c4531SDave Airlie 	mdev->mode_info.crtc = mga_crtc;
1309414c4531SDave Airlie 
1310414c4531SDave Airlie 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
1311414c4531SDave Airlie 		mga_crtc->lut_r[i] = i;
1312414c4531SDave Airlie 		mga_crtc->lut_g[i] = i;
1313414c4531SDave Airlie 		mga_crtc->lut_b[i] = i;
1314414c4531SDave Airlie 	}
1315414c4531SDave Airlie 
1316414c4531SDave Airlie 	drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1317414c4531SDave Airlie }
1318414c4531SDave Airlie 
1319414c4531SDave Airlie /** Sets the color ramps on behalf of fbcon */
1320414c4531SDave Airlie void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1321414c4531SDave Airlie 			      u16 blue, int regno)
1322414c4531SDave Airlie {
1323414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1324414c4531SDave Airlie 
1325414c4531SDave Airlie 	mga_crtc->lut_r[regno] = red >> 8;
1326414c4531SDave Airlie 	mga_crtc->lut_g[regno] = green >> 8;
1327414c4531SDave Airlie 	mga_crtc->lut_b[regno] = blue >> 8;
1328414c4531SDave Airlie }
1329414c4531SDave Airlie 
1330414c4531SDave Airlie /** Gets the color ramps on behalf of fbcon */
1331414c4531SDave Airlie void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
1332414c4531SDave Airlie 			      u16 *blue, int regno)
1333414c4531SDave Airlie {
1334414c4531SDave Airlie 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1335414c4531SDave Airlie 
1336414c4531SDave Airlie 	*red = (u16)mga_crtc->lut_r[regno] << 8;
1337414c4531SDave Airlie 	*green = (u16)mga_crtc->lut_g[regno] << 8;
1338414c4531SDave Airlie 	*blue = (u16)mga_crtc->lut_b[regno] << 8;
1339414c4531SDave Airlie }
1340414c4531SDave Airlie 
1341414c4531SDave Airlie /*
1342414c4531SDave Airlie  * The encoder comes after the CRTC in the output pipeline, but before
1343414c4531SDave Airlie  * the connector. It's responsible for ensuring that the digital
1344414c4531SDave Airlie  * stream is appropriately converted into the output format. Setup is
1345414c4531SDave Airlie  * very simple in this case - all we have to do is inform qemu of the
1346414c4531SDave Airlie  * colour depth in order to ensure that it displays appropriately
1347414c4531SDave Airlie  */
1348414c4531SDave Airlie 
1349414c4531SDave Airlie /*
1350414c4531SDave Airlie  * These functions are analagous to those in the CRTC code, but are intended
1351414c4531SDave Airlie  * to handle any encoder-specific limitations
1352414c4531SDave Airlie  */
1353414c4531SDave Airlie static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
1354e811f5aeSLaurent Pinchart 				   const struct drm_display_mode *mode,
1355414c4531SDave Airlie 				   struct drm_display_mode *adjusted_mode)
1356414c4531SDave Airlie {
1357414c4531SDave Airlie 	return true;
1358414c4531SDave Airlie }
1359414c4531SDave Airlie 
1360414c4531SDave Airlie static void mga_encoder_mode_set(struct drm_encoder *encoder,
1361414c4531SDave Airlie 				struct drm_display_mode *mode,
1362414c4531SDave Airlie 				struct drm_display_mode *adjusted_mode)
1363414c4531SDave Airlie {
1364414c4531SDave Airlie 
1365414c4531SDave Airlie }
1366414c4531SDave Airlie 
1367414c4531SDave Airlie static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
1368414c4531SDave Airlie {
1369414c4531SDave Airlie 	return;
1370414c4531SDave Airlie }
1371414c4531SDave Airlie 
1372414c4531SDave Airlie static void mga_encoder_prepare(struct drm_encoder *encoder)
1373414c4531SDave Airlie {
1374414c4531SDave Airlie }
1375414c4531SDave Airlie 
1376414c4531SDave Airlie static void mga_encoder_commit(struct drm_encoder *encoder)
1377414c4531SDave Airlie {
1378414c4531SDave Airlie }
1379414c4531SDave Airlie 
1380414c4531SDave Airlie void mga_encoder_destroy(struct drm_encoder *encoder)
1381414c4531SDave Airlie {
1382414c4531SDave Airlie 	struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
1383414c4531SDave Airlie 	drm_encoder_cleanup(encoder);
1384414c4531SDave Airlie 	kfree(mga_encoder);
1385414c4531SDave Airlie }
1386414c4531SDave Airlie 
1387414c4531SDave Airlie static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
1388414c4531SDave Airlie 	.dpms = mga_encoder_dpms,
1389414c4531SDave Airlie 	.mode_fixup = mga_encoder_mode_fixup,
1390414c4531SDave Airlie 	.mode_set = mga_encoder_mode_set,
1391414c4531SDave Airlie 	.prepare = mga_encoder_prepare,
1392414c4531SDave Airlie 	.commit = mga_encoder_commit,
1393414c4531SDave Airlie };
1394414c4531SDave Airlie 
1395414c4531SDave Airlie static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
1396414c4531SDave Airlie 	.destroy = mga_encoder_destroy,
1397414c4531SDave Airlie };
1398414c4531SDave Airlie 
1399414c4531SDave Airlie static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
1400414c4531SDave Airlie {
1401414c4531SDave Airlie 	struct drm_encoder *encoder;
1402414c4531SDave Airlie 	struct mga_encoder *mga_encoder;
1403414c4531SDave Airlie 
1404414c4531SDave Airlie 	mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
1405414c4531SDave Airlie 	if (!mga_encoder)
1406414c4531SDave Airlie 		return NULL;
1407414c4531SDave Airlie 
1408414c4531SDave Airlie 	encoder = &mga_encoder->base;
1409414c4531SDave Airlie 	encoder->possible_crtcs = 0x1;
1410414c4531SDave Airlie 
1411414c4531SDave Airlie 	drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
1412414c4531SDave Airlie 			 DRM_MODE_ENCODER_DAC);
1413414c4531SDave Airlie 	drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
1414414c4531SDave Airlie 
1415414c4531SDave Airlie 	return encoder;
1416414c4531SDave Airlie }
1417414c4531SDave Airlie 
1418414c4531SDave Airlie 
1419414c4531SDave Airlie static int mga_vga_get_modes(struct drm_connector *connector)
1420414c4531SDave Airlie {
1421414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1422414c4531SDave Airlie 	struct edid *edid;
1423414c4531SDave Airlie 	int ret = 0;
1424414c4531SDave Airlie 
1425414c4531SDave Airlie 	edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1426414c4531SDave Airlie 	if (edid) {
1427414c4531SDave Airlie 		drm_mode_connector_update_edid_property(connector, edid);
1428414c4531SDave Airlie 		ret = drm_add_edid_modes(connector, edid);
1429414c4531SDave Airlie 		kfree(edid);
1430414c4531SDave Airlie 	}
1431414c4531SDave Airlie 	return ret;
1432414c4531SDave Airlie }
1433414c4531SDave Airlie 
1434abbee623SJulia Lemire static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
1435abbee623SJulia Lemire 							int bits_per_pixel)
1436abbee623SJulia Lemire {
1437abbee623SJulia Lemire 	uint32_t total_area, divisor;
1438abbee623SJulia Lemire 	int64_t active_area, pixels_per_second, bandwidth;
1439abbee623SJulia Lemire 	uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
1440abbee623SJulia Lemire 
1441abbee623SJulia Lemire 	divisor = 1024;
1442abbee623SJulia Lemire 
1443abbee623SJulia Lemire 	if (!mode->htotal || !mode->vtotal || !mode->clock)
1444abbee623SJulia Lemire 		return 0;
1445abbee623SJulia Lemire 
1446abbee623SJulia Lemire 	active_area = mode->hdisplay * mode->vdisplay;
1447abbee623SJulia Lemire 	total_area = mode->htotal * mode->vtotal;
1448abbee623SJulia Lemire 
1449abbee623SJulia Lemire 	pixels_per_second = active_area * mode->clock * 1000;
1450abbee623SJulia Lemire 	do_div(pixels_per_second, total_area);
1451abbee623SJulia Lemire 
1452abbee623SJulia Lemire 	bandwidth = pixels_per_second * bytes_per_pixel * 100;
1453abbee623SJulia Lemire 	do_div(bandwidth, divisor);
1454abbee623SJulia Lemire 
1455abbee623SJulia Lemire 	return (uint32_t)(bandwidth);
1456abbee623SJulia Lemire }
1457abbee623SJulia Lemire 
1458abbee623SJulia Lemire #define MODE_BANDWIDTH	MODE_BAD
1459abbee623SJulia Lemire 
1460414c4531SDave Airlie static int mga_vga_mode_valid(struct drm_connector *connector,
1461414c4531SDave Airlie 				 struct drm_display_mode *mode)
1462414c4531SDave Airlie {
14630ba53171SChristopher Harvey 	struct drm_device *dev = connector->dev;
14640ba53171SChristopher Harvey 	struct mga_device *mdev = (struct mga_device*)dev->dev_private;
14650ba53171SChristopher Harvey 	struct mga_fbdev *mfbdev = mdev->mfbdev;
14660ba53171SChristopher Harvey 	struct drm_fb_helper *fb_helper = &mfbdev->helper;
14670ba53171SChristopher Harvey 	struct drm_fb_helper_connector *fb_helper_conn = NULL;
14680ba53171SChristopher Harvey 	int bpp = 32;
14690ba53171SChristopher Harvey 	int i = 0;
14700ba53171SChristopher Harvey 
1471abbee623SJulia Lemire 	if (IS_G200_SE(mdev)) {
1472abbee623SJulia Lemire 		if (mdev->unique_rev_id == 0x01) {
1473abbee623SJulia Lemire 			if (mode->hdisplay > 1600)
1474abbee623SJulia Lemire 				return MODE_VIRTUAL_X;
1475abbee623SJulia Lemire 			if (mode->vdisplay > 1200)
1476abbee623SJulia Lemire 				return MODE_VIRTUAL_Y;
1477abbee623SJulia Lemire 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1478abbee623SJulia Lemire 				> (24400 * 1024))
1479abbee623SJulia Lemire 				return MODE_BANDWIDTH;
1480abbee623SJulia Lemire 		} else if (mdev->unique_rev_id >= 0x02) {
1481abbee623SJulia Lemire 			if (mode->hdisplay > 1920)
1482abbee623SJulia Lemire 				return MODE_VIRTUAL_X;
1483abbee623SJulia Lemire 			if (mode->vdisplay > 1200)
1484abbee623SJulia Lemire 				return MODE_VIRTUAL_Y;
1485abbee623SJulia Lemire 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1486abbee623SJulia Lemire 				> (30100 * 1024))
1487abbee623SJulia Lemire 				return MODE_BANDWIDTH;
1488abbee623SJulia Lemire 		}
1489abbee623SJulia Lemire 	} else if (mdev->type == G200_WB) {
1490abbee623SJulia Lemire 		if (mode->hdisplay > 1280)
1491abbee623SJulia Lemire 			return MODE_VIRTUAL_X;
1492abbee623SJulia Lemire 		if (mode->vdisplay > 1024)
1493abbee623SJulia Lemire 			return MODE_VIRTUAL_Y;
1494abbee623SJulia Lemire 		if (mga_vga_calculate_mode_bandwidth(mode,
1495abbee623SJulia Lemire 			bpp > (31877 * 1024)))
1496abbee623SJulia Lemire 			return MODE_BANDWIDTH;
1497abbee623SJulia Lemire 	} else if (mdev->type == G200_EV &&
1498abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode, bpp)
1499abbee623SJulia Lemire 			> (32700 * 1024))) {
1500abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1501abbee623SJulia Lemire 	} else if (mode->type == G200_EH &&
1502abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode, bpp)
1503abbee623SJulia Lemire 			> (37500 * 1024))) {
1504abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1505abbee623SJulia Lemire 	} else if (mode->type == G200_ER &&
1506abbee623SJulia Lemire 		(mga_vga_calculate_mode_bandwidth(mode,
1507abbee623SJulia Lemire 			bpp) > (55000 * 1024))) {
1508abbee623SJulia Lemire 		return MODE_BANDWIDTH;
1509abbee623SJulia Lemire 	}
1510414c4531SDave Airlie 
1511414c4531SDave Airlie 	if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1512414c4531SDave Airlie 	    mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1513414c4531SDave Airlie 	    mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1514414c4531SDave Airlie 	    mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1515414c4531SDave Airlie 		return MODE_BAD;
1516414c4531SDave Airlie 	}
1517414c4531SDave Airlie 
15180ba53171SChristopher Harvey 	/* Validate the mode input by the user */
15190ba53171SChristopher Harvey 	for (i = 0; i < fb_helper->connector_count; i++) {
15200ba53171SChristopher Harvey 		if (fb_helper->connector_info[i]->connector == connector) {
15210ba53171SChristopher Harvey 			/* Found the helper for this connector */
15220ba53171SChristopher Harvey 			fb_helper_conn = fb_helper->connector_info[i];
15230ba53171SChristopher Harvey 			if (fb_helper_conn->cmdline_mode.specified) {
15240ba53171SChristopher Harvey 				if (fb_helper_conn->cmdline_mode.bpp_specified) {
15250ba53171SChristopher Harvey 					bpp = fb_helper_conn->cmdline_mode.bpp;
15260ba53171SChristopher Harvey 				}
15270ba53171SChristopher Harvey 			}
15280ba53171SChristopher Harvey 		}
15290ba53171SChristopher Harvey 	}
15300ba53171SChristopher Harvey 
15310ba53171SChristopher Harvey 	if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
15320ba53171SChristopher Harvey 		if (fb_helper_conn)
15330ba53171SChristopher Harvey 			fb_helper_conn->cmdline_mode.specified = false;
15340ba53171SChristopher Harvey 		return MODE_BAD;
15350ba53171SChristopher Harvey 	}
15360ba53171SChristopher Harvey 
1537414c4531SDave Airlie 	return MODE_OK;
1538414c4531SDave Airlie }
1539414c4531SDave Airlie 
1540414c4531SDave Airlie struct drm_encoder *mga_connector_best_encoder(struct drm_connector
1541414c4531SDave Airlie 						  *connector)
1542414c4531SDave Airlie {
1543414c4531SDave Airlie 	int enc_id = connector->encoder_ids[0];
1544414c4531SDave Airlie 	struct drm_mode_object *obj;
1545414c4531SDave Airlie 	struct drm_encoder *encoder;
1546414c4531SDave Airlie 
1547414c4531SDave Airlie 	/* pick the encoder ids */
1548414c4531SDave Airlie 	if (enc_id) {
1549414c4531SDave Airlie 		obj =
1550414c4531SDave Airlie 		    drm_mode_object_find(connector->dev, enc_id,
1551414c4531SDave Airlie 					 DRM_MODE_OBJECT_ENCODER);
1552414c4531SDave Airlie 		if (!obj)
1553414c4531SDave Airlie 			return NULL;
1554414c4531SDave Airlie 		encoder = obj_to_encoder(obj);
1555414c4531SDave Airlie 		return encoder;
1556414c4531SDave Airlie 	}
1557414c4531SDave Airlie 	return NULL;
1558414c4531SDave Airlie }
1559414c4531SDave Airlie 
1560414c4531SDave Airlie static enum drm_connector_status mga_vga_detect(struct drm_connector
1561414c4531SDave Airlie 						   *connector, bool force)
1562414c4531SDave Airlie {
1563414c4531SDave Airlie 	return connector_status_connected;
1564414c4531SDave Airlie }
1565414c4531SDave Airlie 
1566414c4531SDave Airlie static void mga_connector_destroy(struct drm_connector *connector)
1567414c4531SDave Airlie {
1568414c4531SDave Airlie 	struct mga_connector *mga_connector = to_mga_connector(connector);
1569414c4531SDave Airlie 	mgag200_i2c_destroy(mga_connector->i2c);
1570414c4531SDave Airlie 	drm_connector_cleanup(connector);
1571414c4531SDave Airlie 	kfree(connector);
1572414c4531SDave Airlie }
1573414c4531SDave Airlie 
1574414c4531SDave Airlie struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
1575414c4531SDave Airlie 	.get_modes = mga_vga_get_modes,
1576414c4531SDave Airlie 	.mode_valid = mga_vga_mode_valid,
1577414c4531SDave Airlie 	.best_encoder = mga_connector_best_encoder,
1578414c4531SDave Airlie };
1579414c4531SDave Airlie 
1580414c4531SDave Airlie struct drm_connector_funcs mga_vga_connector_funcs = {
1581414c4531SDave Airlie 	.dpms = drm_helper_connector_dpms,
1582414c4531SDave Airlie 	.detect = mga_vga_detect,
1583414c4531SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
1584414c4531SDave Airlie 	.destroy = mga_connector_destroy,
1585414c4531SDave Airlie };
1586414c4531SDave Airlie 
1587414c4531SDave Airlie static struct drm_connector *mga_vga_init(struct drm_device *dev)
1588414c4531SDave Airlie {
1589414c4531SDave Airlie 	struct drm_connector *connector;
1590414c4531SDave Airlie 	struct mga_connector *mga_connector;
1591414c4531SDave Airlie 
1592414c4531SDave Airlie 	mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1593414c4531SDave Airlie 	if (!mga_connector)
1594414c4531SDave Airlie 		return NULL;
1595414c4531SDave Airlie 
1596414c4531SDave Airlie 	connector = &mga_connector->base;
1597414c4531SDave Airlie 
1598414c4531SDave Airlie 	drm_connector_init(dev, connector,
1599414c4531SDave Airlie 			   &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1600414c4531SDave Airlie 
1601414c4531SDave Airlie 	drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1602414c4531SDave Airlie 
16033d5a1c5eSEgbert Eich 	drm_sysfs_connector_add(connector);
16043d5a1c5eSEgbert Eich 
1605414c4531SDave Airlie 	mga_connector->i2c = mgag200_i2c_create(dev);
1606414c4531SDave Airlie 	if (!mga_connector->i2c)
1607414c4531SDave Airlie 		DRM_ERROR("failed to add ddc bus\n");
1608414c4531SDave Airlie 
1609414c4531SDave Airlie 	return connector;
1610414c4531SDave Airlie }
1611414c4531SDave Airlie 
1612414c4531SDave Airlie 
1613414c4531SDave Airlie int mgag200_modeset_init(struct mga_device *mdev)
1614414c4531SDave Airlie {
1615414c4531SDave Airlie 	struct drm_encoder *encoder;
1616414c4531SDave Airlie 	struct drm_connector *connector;
1617414c4531SDave Airlie 	int ret;
1618414c4531SDave Airlie 
1619414c4531SDave Airlie 	mdev->mode_info.mode_config_initialized = true;
1620414c4531SDave Airlie 
1621414c4531SDave Airlie 	mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1622414c4531SDave Airlie 	mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1623414c4531SDave Airlie 
1624414c4531SDave Airlie 	mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1625414c4531SDave Airlie 
1626f1998fe2SChristopher Harvey 	mga_crtc_init(mdev);
1627414c4531SDave Airlie 
1628414c4531SDave Airlie 	encoder = mga_encoder_init(mdev->dev);
1629414c4531SDave Airlie 	if (!encoder) {
1630414c4531SDave Airlie 		DRM_ERROR("mga_encoder_init failed\n");
1631414c4531SDave Airlie 		return -1;
1632414c4531SDave Airlie 	}
1633414c4531SDave Airlie 
1634414c4531SDave Airlie 	connector = mga_vga_init(mdev->dev);
1635414c4531SDave Airlie 	if (!connector) {
1636414c4531SDave Airlie 		DRM_ERROR("mga_vga_init failed\n");
1637414c4531SDave Airlie 		return -1;
1638414c4531SDave Airlie 	}
1639414c4531SDave Airlie 
1640414c4531SDave Airlie 	drm_mode_connector_attach_encoder(connector, encoder);
1641414c4531SDave Airlie 
1642414c4531SDave Airlie 	ret = mgag200_fbdev_init(mdev);
1643414c4531SDave Airlie 	if (ret) {
1644414c4531SDave Airlie 		DRM_ERROR("mga_fbdev_init failed\n");
1645414c4531SDave Airlie 		return ret;
1646414c4531SDave Airlie 	}
1647414c4531SDave Airlie 
1648414c4531SDave Airlie 	return 0;
1649414c4531SDave Airlie }
1650414c4531SDave Airlie 
1651414c4531SDave Airlie void mgag200_modeset_fini(struct mga_device *mdev)
1652414c4531SDave Airlie {
1653414c4531SDave Airlie 
1654414c4531SDave Airlie }
1655