1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2010 Matt Turner.
4  * Copyright 2012 Red Hat
5  *
6  * Authors: Matthew Garrett
7  *	    Matt Turner
8  *	    Dave Airlie
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/pci.h>
13 
14 #include <drm/drm_crtc_helper.h>
15 #include <drm/drm_fourcc.h>
16 #include <drm/drm_plane_helper.h>
17 #include <drm/drm_probe_helper.h>
18 #include <drm/drm_simple_kms_helper.h>
19 
20 #include "mgag200_drv.h"
21 
22 #define MGAG200_LUT_SIZE 256
23 
24 /*
25  * This file contains setup code for the CRTC.
26  */
27 
28 static void mga_crtc_load_lut(struct drm_crtc *crtc)
29 {
30 	struct drm_device *dev = crtc->dev;
31 	struct mga_device *mdev = dev->dev_private;
32 	struct drm_framebuffer *fb = crtc->primary->fb;
33 	u16 *r_ptr, *g_ptr, *b_ptr;
34 	int i;
35 
36 	if (!crtc->enabled)
37 		return;
38 
39 	r_ptr = crtc->gamma_store;
40 	g_ptr = r_ptr + crtc->gamma_size;
41 	b_ptr = g_ptr + crtc->gamma_size;
42 
43 	WREG8(DAC_INDEX + MGA1064_INDEX, 0);
44 
45 	if (fb && fb->format->cpp[0] * 8 == 16) {
46 		int inc = (fb->format->depth == 15) ? 8 : 4;
47 		u8 r, b;
48 		for (i = 0; i < MGAG200_LUT_SIZE; i += inc) {
49 			if (fb->format->depth == 16) {
50 				if (i > (MGAG200_LUT_SIZE >> 1)) {
51 					r = b = 0;
52 				} else {
53 					r = *r_ptr++ >> 8;
54 					b = *b_ptr++ >> 8;
55 					r_ptr++;
56 					b_ptr++;
57 				}
58 			} else {
59 				r = *r_ptr++ >> 8;
60 				b = *b_ptr++ >> 8;
61 			}
62 			/* VGA registers */
63 			WREG8(DAC_INDEX + MGA1064_COL_PAL, r);
64 			WREG8(DAC_INDEX + MGA1064_COL_PAL, *g_ptr++ >> 8);
65 			WREG8(DAC_INDEX + MGA1064_COL_PAL, b);
66 		}
67 		return;
68 	}
69 	for (i = 0; i < MGAG200_LUT_SIZE; i++) {
70 		/* VGA registers */
71 		WREG8(DAC_INDEX + MGA1064_COL_PAL, *r_ptr++ >> 8);
72 		WREG8(DAC_INDEX + MGA1064_COL_PAL, *g_ptr++ >> 8);
73 		WREG8(DAC_INDEX + MGA1064_COL_PAL, *b_ptr++ >> 8);
74 	}
75 }
76 
77 static inline void mga_wait_vsync(struct mga_device *mdev)
78 {
79 	unsigned long timeout = jiffies + HZ/10;
80 	unsigned int status = 0;
81 
82 	do {
83 		status = RREG32(MGAREG_Status);
84 	} while ((status & 0x08) && time_before(jiffies, timeout));
85 	timeout = jiffies + HZ/10;
86 	status = 0;
87 	do {
88 		status = RREG32(MGAREG_Status);
89 	} while (!(status & 0x08) && time_before(jiffies, timeout));
90 }
91 
92 static inline void mga_wait_busy(struct mga_device *mdev)
93 {
94 	unsigned long timeout = jiffies + HZ;
95 	unsigned int status = 0;
96 	do {
97 		status = RREG8(MGAREG_Status + 2);
98 	} while ((status & 0x01) && time_before(jiffies, timeout));
99 }
100 
101 #define P_ARRAY_SIZE 9
102 
103 static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
104 {
105 	unsigned int vcomax, vcomin, pllreffreq;
106 	unsigned int delta, tmpdelta, permitteddelta;
107 	unsigned int testp, testm, testn;
108 	unsigned int p, m, n;
109 	unsigned int computed;
110 	unsigned int pvalues_e4[P_ARRAY_SIZE] = {16, 14, 12, 10, 8, 6, 4, 2, 1};
111 	unsigned int fvv;
112 	unsigned int i;
113 
114 	if (mdev->unique_rev_id <= 0x03) {
115 
116 		m = n = p = 0;
117 		vcomax = 320000;
118 		vcomin = 160000;
119 		pllreffreq = 25000;
120 
121 		delta = 0xffffffff;
122 		permitteddelta = clock * 5 / 1000;
123 
124 		for (testp = 8; testp > 0; testp /= 2) {
125 			if (clock * testp > vcomax)
126 				continue;
127 			if (clock * testp < vcomin)
128 				continue;
129 
130 			for (testn = 17; testn < 256; testn++) {
131 				for (testm = 1; testm < 32; testm++) {
132 					computed = (pllreffreq * testn) /
133 						(testm * testp);
134 					if (computed > clock)
135 						tmpdelta = computed - clock;
136 					else
137 						tmpdelta = clock - computed;
138 					if (tmpdelta < delta) {
139 						delta = tmpdelta;
140 						m = testm - 1;
141 						n = testn - 1;
142 						p = testp - 1;
143 					}
144 				}
145 			}
146 		}
147 	} else {
148 
149 
150 		m = n = p = 0;
151 		vcomax        = 1600000;
152 		vcomin        = 800000;
153 		pllreffreq    = 25000;
154 
155 		if (clock < 25000)
156 			clock = 25000;
157 
158 		clock = clock * 2;
159 
160 		delta = 0xFFFFFFFF;
161 		/* Permited delta is 0.5% as VESA Specification */
162 		permitteddelta = clock * 5 / 1000;
163 
164 		for (i = 0 ; i < P_ARRAY_SIZE ; i++) {
165 			testp = pvalues_e4[i];
166 
167 			if ((clock * testp) > vcomax)
168 				continue;
169 			if ((clock * testp) < vcomin)
170 				continue;
171 
172 			for (testn = 50; testn <= 256; testn++) {
173 				for (testm = 1; testm <= 32; testm++) {
174 					computed = (pllreffreq * testn) /
175 						(testm * testp);
176 					if (computed > clock)
177 						tmpdelta = computed - clock;
178 					else
179 						tmpdelta = clock - computed;
180 
181 					if (tmpdelta < delta) {
182 						delta = tmpdelta;
183 						m = testm - 1;
184 						n = testn - 1;
185 						p = testp - 1;
186 					}
187 				}
188 			}
189 		}
190 
191 		fvv = pllreffreq * (n + 1) / (m + 1);
192 		fvv = (fvv - 800000) / 50000;
193 
194 		if (fvv > 15)
195 			fvv = 15;
196 
197 		p |= (fvv << 4);
198 		m |= 0x80;
199 
200 		clock = clock / 2;
201 	}
202 
203 	if (delta > permitteddelta) {
204 		pr_warn("PLL delta too large\n");
205 		return 1;
206 	}
207 
208 	WREG_DAC(MGA1064_PIX_PLLC_M, m);
209 	WREG_DAC(MGA1064_PIX_PLLC_N, n);
210 	WREG_DAC(MGA1064_PIX_PLLC_P, p);
211 
212 	if (mdev->unique_rev_id >= 0x04) {
213 		WREG_DAC(0x1a, 0x09);
214 		msleep(20);
215 		WREG_DAC(0x1a, 0x01);
216 
217 	}
218 
219 	return 0;
220 }
221 
222 static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
223 {
224 	unsigned int vcomax, vcomin, pllreffreq;
225 	unsigned int delta, tmpdelta;
226 	unsigned int testp, testm, testn, testp2;
227 	unsigned int p, m, n;
228 	unsigned int computed;
229 	int i, j, tmpcount, vcount;
230 	bool pll_locked = false;
231 	u8 tmp;
232 
233 	m = n = p = 0;
234 
235 	delta = 0xffffffff;
236 
237 	if (mdev->type == G200_EW3) {
238 
239 		vcomax = 800000;
240 		vcomin = 400000;
241 		pllreffreq = 25000;
242 
243 		for (testp = 1; testp < 8; testp++) {
244 			for (testp2 = 1; testp2 < 8; testp2++) {
245 				if (testp < testp2)
246 					continue;
247 				if ((clock * testp * testp2) > vcomax)
248 					continue;
249 				if ((clock * testp * testp2) < vcomin)
250 					continue;
251 				for (testm = 1; testm < 26; testm++) {
252 					for (testn = 32; testn < 2048 ; testn++) {
253 						computed = (pllreffreq * testn) /
254 							(testm * testp * testp2);
255 						if (computed > clock)
256 							tmpdelta = computed - clock;
257 						else
258 							tmpdelta = clock - computed;
259 						if (tmpdelta < delta) {
260 							delta = tmpdelta;
261 							m = ((testn & 0x100) >> 1) |
262 								(testm);
263 							n = (testn & 0xFF);
264 							p = ((testn & 0x600) >> 3) |
265 								(testp2 << 3) |
266 								(testp);
267 						}
268 					}
269 				}
270 			}
271 		}
272 	} else {
273 
274 		vcomax = 550000;
275 		vcomin = 150000;
276 		pllreffreq = 48000;
277 
278 		for (testp = 1; testp < 9; testp++) {
279 			if (clock * testp > vcomax)
280 				continue;
281 			if (clock * testp < vcomin)
282 				continue;
283 
284 			for (testm = 1; testm < 17; testm++) {
285 				for (testn = 1; testn < 151; testn++) {
286 					computed = (pllreffreq * testn) /
287 						(testm * testp);
288 					if (computed > clock)
289 						tmpdelta = computed - clock;
290 					else
291 						tmpdelta = clock - computed;
292 					if (tmpdelta < delta) {
293 						delta = tmpdelta;
294 						n = testn - 1;
295 						m = (testm - 1) |
296 							((n >> 1) & 0x80);
297 						p = testp - 1;
298 					}
299 				}
300 			}
301 		}
302 	}
303 
304 	for (i = 0; i <= 32 && pll_locked == false; i++) {
305 		if (i > 0) {
306 			WREG8(MGAREG_CRTC_INDEX, 0x1e);
307 			tmp = RREG8(MGAREG_CRTC_DATA);
308 			if (tmp < 0xff)
309 				WREG8(MGAREG_CRTC_DATA, tmp+1);
310 		}
311 
312 		/* set pixclkdis to 1 */
313 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
314 		tmp = RREG8(DAC_DATA);
315 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
316 		WREG8(DAC_DATA, tmp);
317 
318 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
319 		tmp = RREG8(DAC_DATA);
320 		tmp |= MGA1064_REMHEADCTL_CLKDIS;
321 		WREG8(DAC_DATA, tmp);
322 
323 		/* select PLL Set C */
324 		tmp = RREG8(MGAREG_MEM_MISC_READ);
325 		tmp |= 0x3 << 2;
326 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
327 
328 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
329 		tmp = RREG8(DAC_DATA);
330 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
331 		WREG8(DAC_DATA, tmp);
332 
333 		udelay(500);
334 
335 		/* reset the PLL */
336 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
337 		tmp = RREG8(DAC_DATA);
338 		tmp &= ~0x04;
339 		WREG8(DAC_DATA, tmp);
340 
341 		udelay(50);
342 
343 		/* program pixel pll register */
344 		WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
345 		WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
346 		WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
347 
348 		udelay(50);
349 
350 		/* turn pll on */
351 		WREG8(DAC_INDEX, MGA1064_VREF_CTL);
352 		tmp = RREG8(DAC_DATA);
353 		tmp |= 0x04;
354 		WREG_DAC(MGA1064_VREF_CTL, tmp);
355 
356 		udelay(500);
357 
358 		/* select the pixel pll */
359 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
360 		tmp = RREG8(DAC_DATA);
361 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
362 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
363 		WREG8(DAC_DATA, tmp);
364 
365 		WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
366 		tmp = RREG8(DAC_DATA);
367 		tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
368 		tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
369 		WREG8(DAC_DATA, tmp);
370 
371 		/* reset dotclock rate bit */
372 		WREG8(MGAREG_SEQ_INDEX, 1);
373 		tmp = RREG8(MGAREG_SEQ_DATA);
374 		tmp &= ~0x8;
375 		WREG8(MGAREG_SEQ_DATA, tmp);
376 
377 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
378 		tmp = RREG8(DAC_DATA);
379 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
380 		WREG8(DAC_DATA, tmp);
381 
382 		vcount = RREG8(MGAREG_VCOUNT);
383 
384 		for (j = 0; j < 30 && pll_locked == false; j++) {
385 			tmpcount = RREG8(MGAREG_VCOUNT);
386 			if (tmpcount < vcount)
387 				vcount = 0;
388 			if ((tmpcount - vcount) > 2)
389 				pll_locked = true;
390 			else
391 				udelay(5);
392 		}
393 	}
394 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
395 	tmp = RREG8(DAC_DATA);
396 	tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
397 	WREG_DAC(MGA1064_REMHEADCTL, tmp);
398 	return 0;
399 }
400 
401 static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
402 {
403 	unsigned int vcomax, vcomin, pllreffreq;
404 	unsigned int delta, tmpdelta;
405 	unsigned int testp, testm, testn;
406 	unsigned int p, m, n;
407 	unsigned int computed;
408 	u8 tmp;
409 
410 	m = n = p = 0;
411 	vcomax = 550000;
412 	vcomin = 150000;
413 	pllreffreq = 50000;
414 
415 	delta = 0xffffffff;
416 
417 	for (testp = 16; testp > 0; testp--) {
418 		if (clock * testp > vcomax)
419 			continue;
420 		if (clock * testp < vcomin)
421 			continue;
422 
423 		for (testn = 1; testn < 257; testn++) {
424 			for (testm = 1; testm < 17; testm++) {
425 				computed = (pllreffreq * testn) /
426 					(testm * testp);
427 				if (computed > clock)
428 					tmpdelta = computed - clock;
429 				else
430 					tmpdelta = clock - computed;
431 				if (tmpdelta < delta) {
432 					delta = tmpdelta;
433 					n = testn - 1;
434 					m = testm - 1;
435 					p = testp - 1;
436 				}
437 			}
438 		}
439 	}
440 
441 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
442 	tmp = RREG8(DAC_DATA);
443 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
444 	WREG8(DAC_DATA, tmp);
445 
446 	tmp = RREG8(MGAREG_MEM_MISC_READ);
447 	tmp |= 0x3 << 2;
448 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
449 
450 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
451 	tmp = RREG8(DAC_DATA);
452 	WREG8(DAC_DATA, tmp & ~0x40);
453 
454 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
455 	tmp = RREG8(DAC_DATA);
456 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
457 	WREG8(DAC_DATA, tmp);
458 
459 	WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
460 	WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
461 	WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
462 
463 	udelay(50);
464 
465 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
466 	tmp = RREG8(DAC_DATA);
467 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
468 	WREG8(DAC_DATA, tmp);
469 
470 	udelay(500);
471 
472 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
473 	tmp = RREG8(DAC_DATA);
474 	tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
475 	tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
476 	WREG8(DAC_DATA, tmp);
477 
478 	WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
479 	tmp = RREG8(DAC_DATA);
480 	WREG8(DAC_DATA, tmp | 0x40);
481 
482 	tmp = RREG8(MGAREG_MEM_MISC_READ);
483 	tmp |= (0x3 << 2);
484 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
485 
486 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
487 	tmp = RREG8(DAC_DATA);
488 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
489 	WREG8(DAC_DATA, tmp);
490 
491 	return 0;
492 }
493 
494 static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
495 {
496 	unsigned int vcomax, vcomin, pllreffreq;
497 	unsigned int delta, tmpdelta;
498 	unsigned int testp, testm, testn;
499 	unsigned int p, m, n;
500 	unsigned int computed;
501 	int i, j, tmpcount, vcount;
502 	u8 tmp;
503 	bool pll_locked = false;
504 
505 	m = n = p = 0;
506 
507 	if (mdev->type == G200_EH3) {
508 		vcomax = 3000000;
509 		vcomin = 1500000;
510 		pllreffreq = 25000;
511 
512 		delta = 0xffffffff;
513 
514 		testp = 0;
515 
516 		for (testm = 150; testm >= 6; testm--) {
517 			if (clock * testm > vcomax)
518 				continue;
519 			if (clock * testm < vcomin)
520 				continue;
521 			for (testn = 120; testn >= 60; testn--) {
522 				computed = (pllreffreq * testn) / testm;
523 				if (computed > clock)
524 					tmpdelta = computed - clock;
525 				else
526 					tmpdelta = clock - computed;
527 				if (tmpdelta < delta) {
528 					delta = tmpdelta;
529 					n = testn;
530 					m = testm;
531 					p = testp;
532 				}
533 				if (delta == 0)
534 					break;
535 			}
536 			if (delta == 0)
537 				break;
538 		}
539 	} else {
540 
541 		vcomax = 800000;
542 		vcomin = 400000;
543 		pllreffreq = 33333;
544 
545 		delta = 0xffffffff;
546 
547 		for (testp = 16; testp > 0; testp >>= 1) {
548 			if (clock * testp > vcomax)
549 				continue;
550 			if (clock * testp < vcomin)
551 				continue;
552 
553 			for (testm = 1; testm < 33; testm++) {
554 				for (testn = 17; testn < 257; testn++) {
555 					computed = (pllreffreq * testn) /
556 						(testm * testp);
557 					if (computed > clock)
558 						tmpdelta = computed - clock;
559 					else
560 						tmpdelta = clock - computed;
561 					if (tmpdelta < delta) {
562 						delta = tmpdelta;
563 						n = testn - 1;
564 						m = (testm - 1);
565 						p = testp - 1;
566 					}
567 					if ((clock * testp) >= 600000)
568 						p |= 0x80;
569 				}
570 			}
571 		}
572 	}
573 	for (i = 0; i <= 32 && pll_locked == false; i++) {
574 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
575 		tmp = RREG8(DAC_DATA);
576 		tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
577 		WREG8(DAC_DATA, tmp);
578 
579 		tmp = RREG8(MGAREG_MEM_MISC_READ);
580 		tmp |= 0x3 << 2;
581 		WREG8(MGAREG_MEM_MISC_WRITE, tmp);
582 
583 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
584 		tmp = RREG8(DAC_DATA);
585 		tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
586 		WREG8(DAC_DATA, tmp);
587 
588 		udelay(500);
589 
590 		WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
591 		WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
592 		WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
593 
594 		udelay(500);
595 
596 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
597 		tmp = RREG8(DAC_DATA);
598 		tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
599 		tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
600 		WREG8(DAC_DATA, tmp);
601 
602 		WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
603 		tmp = RREG8(DAC_DATA);
604 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
605 		tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
606 		WREG8(DAC_DATA, tmp);
607 
608 		vcount = RREG8(MGAREG_VCOUNT);
609 
610 		for (j = 0; j < 30 && pll_locked == false; j++) {
611 			tmpcount = RREG8(MGAREG_VCOUNT);
612 			if (tmpcount < vcount)
613 				vcount = 0;
614 			if ((tmpcount - vcount) > 2)
615 				pll_locked = true;
616 			else
617 				udelay(5);
618 		}
619 	}
620 
621 	return 0;
622 }
623 
624 static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
625 {
626 	unsigned int vcomax, vcomin, pllreffreq;
627 	unsigned int delta, tmpdelta;
628 	int testr, testn, testm, testo;
629 	unsigned int p, m, n;
630 	unsigned int computed, vco;
631 	int tmp;
632 	const unsigned int m_div_val[] = { 1, 2, 4, 8 };
633 
634 	m = n = p = 0;
635 	vcomax = 1488000;
636 	vcomin = 1056000;
637 	pllreffreq = 48000;
638 
639 	delta = 0xffffffff;
640 
641 	for (testr = 0; testr < 4; testr++) {
642 		if (delta == 0)
643 			break;
644 		for (testn = 5; testn < 129; testn++) {
645 			if (delta == 0)
646 				break;
647 			for (testm = 3; testm >= 0; testm--) {
648 				if (delta == 0)
649 					break;
650 				for (testo = 5; testo < 33; testo++) {
651 					vco = pllreffreq * (testn + 1) /
652 						(testr + 1);
653 					if (vco < vcomin)
654 						continue;
655 					if (vco > vcomax)
656 						continue;
657 					computed = vco / (m_div_val[testm] * (testo + 1));
658 					if (computed > clock)
659 						tmpdelta = computed - clock;
660 					else
661 						tmpdelta = clock - computed;
662 					if (tmpdelta < delta) {
663 						delta = tmpdelta;
664 						m = testm | (testo << 3);
665 						n = testn;
666 						p = testr | (testr << 3);
667 					}
668 				}
669 			}
670 		}
671 	}
672 
673 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
674 	tmp = RREG8(DAC_DATA);
675 	tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
676 	WREG8(DAC_DATA, tmp);
677 
678 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
679 	tmp = RREG8(DAC_DATA);
680 	tmp |= MGA1064_REMHEADCTL_CLKDIS;
681 	WREG8(DAC_DATA, tmp);
682 
683 	tmp = RREG8(MGAREG_MEM_MISC_READ);
684 	tmp |= (0x3<<2) | 0xc0;
685 	WREG8(MGAREG_MEM_MISC_WRITE, tmp);
686 
687 	WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
688 	tmp = RREG8(DAC_DATA);
689 	tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
690 	tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
691 	WREG8(DAC_DATA, tmp);
692 
693 	udelay(500);
694 
695 	WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
696 	WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
697 	WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
698 
699 	udelay(50);
700 
701 	return 0;
702 }
703 
704 static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
705 {
706 	switch(mdev->type) {
707 	case G200_SE_A:
708 	case G200_SE_B:
709 		return mga_g200se_set_plls(mdev, clock);
710 		break;
711 	case G200_WB:
712 	case G200_EW3:
713 		return mga_g200wb_set_plls(mdev, clock);
714 		break;
715 	case G200_EV:
716 		return mga_g200ev_set_plls(mdev, clock);
717 		break;
718 	case G200_EH:
719 	case G200_EH3:
720 		return mga_g200eh_set_plls(mdev, clock);
721 		break;
722 	case G200_ER:
723 		return mga_g200er_set_plls(mdev, clock);
724 		break;
725 	}
726 	return 0;
727 }
728 
729 static void mga_g200wb_prepare(struct drm_crtc *crtc)
730 {
731 	struct mga_device *mdev = crtc->dev->dev_private;
732 	u8 tmp;
733 	int iter_max;
734 
735 	/* 1- The first step is to warn the BMC of an upcoming mode change.
736 	 * We are putting the misc<0> to output.*/
737 
738 	WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
739 	tmp = RREG8(DAC_DATA);
740 	tmp |= 0x10;
741 	WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
742 
743 	/* we are putting a 1 on the misc<0> line */
744 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
745 	tmp = RREG8(DAC_DATA);
746 	tmp |= 0x10;
747 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
748 
749 	/* 2- Second step to mask and further scan request
750 	 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
751 	 */
752 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
753 	tmp = RREG8(DAC_DATA);
754 	tmp |= 0x80;
755 	WREG_DAC(MGA1064_SPAREREG, tmp);
756 
757 	/* 3a- the third step is to verifu if there is an active scan
758 	 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
759 	 */
760 	iter_max = 300;
761 	while (!(tmp & 0x1) && iter_max) {
762 		WREG8(DAC_INDEX, MGA1064_SPAREREG);
763 		tmp = RREG8(DAC_DATA);
764 		udelay(1000);
765 		iter_max--;
766 	}
767 
768 	/* 3b- this step occurs only if the remove is actually scanning
769 	 * we are waiting for the end of the frame which is a 1 on
770 	 * remvsyncsts (XSPAREREG<1>)
771 	 */
772 	if (iter_max) {
773 		iter_max = 300;
774 		while ((tmp & 0x2) && iter_max) {
775 			WREG8(DAC_INDEX, MGA1064_SPAREREG);
776 			tmp = RREG8(DAC_DATA);
777 			udelay(1000);
778 			iter_max--;
779 		}
780 	}
781 }
782 
783 static void mga_g200wb_commit(struct drm_crtc *crtc)
784 {
785 	u8 tmp;
786 	struct mga_device *mdev = crtc->dev->dev_private;
787 
788 	/* 1- The first step is to ensure that the vrsten and hrsten are set */
789 	WREG8(MGAREG_CRTCEXT_INDEX, 1);
790 	tmp = RREG8(MGAREG_CRTCEXT_DATA);
791 	WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
792 
793 	/* 2- second step is to assert the rstlvl2 */
794 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
795 	tmp = RREG8(DAC_DATA);
796 	tmp |= 0x8;
797 	WREG8(DAC_DATA, tmp);
798 
799 	/* wait 10 us */
800 	udelay(10);
801 
802 	/* 3- deassert rstlvl2 */
803 	tmp &= ~0x08;
804 	WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
805 	WREG8(DAC_DATA, tmp);
806 
807 	/* 4- remove mask of scan request */
808 	WREG8(DAC_INDEX, MGA1064_SPAREREG);
809 	tmp = RREG8(DAC_DATA);
810 	tmp &= ~0x80;
811 	WREG8(DAC_DATA, tmp);
812 
813 	/* 5- put back a 0 on the misc<0> line */
814 	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
815 	tmp = RREG8(DAC_DATA);
816 	tmp &= ~0x10;
817 	WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
818 }
819 
820 /*
821    This is how the framebuffer base address is stored in g200 cards:
822    * Assume @offset is the gpu_addr variable of the framebuffer object
823    * Then addr is the number of _pixels_ (not bytes) from the start of
824      VRAM to the first pixel we want to display. (divided by 2 for 32bit
825      framebuffers)
826    * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
827    addr<20> -> CRTCEXT0<6>
828    addr<19-16> -> CRTCEXT0<3-0>
829    addr<15-8> -> CRTCC<7-0>
830    addr<7-0> -> CRTCD<7-0>
831    CRTCEXT0 has to be programmed last to trigger an update and make the
832    new addr variable take effect.
833  */
834 static void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
835 {
836 	struct mga_device *mdev = crtc->dev->dev_private;
837 	u32 addr;
838 	int count;
839 	u8 crtcext0;
840 
841 	while (RREG8(0x1fda) & 0x08);
842 	while (!(RREG8(0x1fda) & 0x08));
843 
844 	count = RREG8(MGAREG_VCOUNT) + 2;
845 	while (RREG8(MGAREG_VCOUNT) < count);
846 
847 	WREG8(MGAREG_CRTCEXT_INDEX, 0);
848 	crtcext0 = RREG8(MGAREG_CRTCEXT_DATA);
849 	crtcext0 &= 0xB0;
850 	addr = offset / 8;
851 	/* Can't store addresses any higher than that...
852 	   but we also don't have more than 16MB of memory, so it should be fine. */
853 	WARN_ON(addr > 0x1fffff);
854 	crtcext0 |= (!!(addr & (1<<20)))<<6;
855 	WREG_CRT(0x0d, (u8)(addr & 0xff));
856 	WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
857 	WREG_ECRT(0x0, ((u8)(addr >> 16) & 0xf) | crtcext0);
858 }
859 
860 static int mga_crtc_do_set_base(struct drm_crtc *crtc,
861 				struct drm_framebuffer *fb,
862 				int x, int y, int atomic)
863 {
864 	struct drm_gem_vram_object *gbo;
865 	int ret;
866 	s64 gpu_addr;
867 
868 	if (!atomic && fb) {
869 		gbo = drm_gem_vram_of_gem(fb->obj[0]);
870 		drm_gem_vram_unpin(gbo);
871 	}
872 
873 	gbo = drm_gem_vram_of_gem(crtc->primary->fb->obj[0]);
874 
875 	ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
876 	if (ret)
877 		return ret;
878 	gpu_addr = drm_gem_vram_offset(gbo);
879 	if (gpu_addr < 0) {
880 		ret = (int)gpu_addr;
881 		goto err_drm_gem_vram_unpin;
882 	}
883 
884 	mga_set_start_address(crtc, (u32)gpu_addr);
885 
886 	return 0;
887 
888 err_drm_gem_vram_unpin:
889 	drm_gem_vram_unpin(gbo);
890 	return ret;
891 }
892 
893 static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
894 				  struct drm_framebuffer *old_fb)
895 {
896 	return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
897 }
898 
899 static int mga_crtc_mode_set(struct drm_crtc *crtc,
900 				struct drm_display_mode *mode,
901 				struct drm_display_mode *adjusted_mode,
902 				int x, int y, struct drm_framebuffer *old_fb)
903 {
904 	struct drm_device *dev = crtc->dev;
905 	struct mga_device *mdev = dev->dev_private;
906 	const struct drm_framebuffer *fb = crtc->primary->fb;
907 	int hdisplay, hsyncstart, hsyncend, htotal;
908 	int vdisplay, vsyncstart, vsyncend, vtotal;
909 	int pitch;
910 	int option = 0, option2 = 0;
911 	int i;
912 	unsigned char misc = 0;
913 	unsigned char ext_vga[6];
914 	u8 bppshift;
915 
916 	static unsigned char dacvalue[] = {
917 		/* 0x00: */        0,    0,    0,    0,    0,    0, 0x00,    0,
918 		/* 0x08: */        0,    0,    0,    0,    0,    0,    0,    0,
919 		/* 0x10: */        0,    0,    0,    0,    0,    0,    0,    0,
920 		/* 0x18: */     0x00,    0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
921 		/* 0x20: */     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
922 		/* 0x28: */     0x00, 0x00, 0x00, 0x00,    0,    0,    0, 0x40,
923 		/* 0x30: */     0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
924 		/* 0x38: */     0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
925 		/* 0x40: */        0,    0,    0,    0,    0,    0,    0,    0,
926 		/* 0x48: */        0,    0,    0,    0,    0,    0,    0,    0
927 	};
928 
929 	bppshift = mdev->bpp_shifts[fb->format->cpp[0] - 1];
930 
931 	switch (mdev->type) {
932 	case G200_SE_A:
933 	case G200_SE_B:
934 		dacvalue[MGA1064_VREF_CTL] = 0x03;
935 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
936 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
937 					     MGA1064_MISC_CTL_VGA8 |
938 					     MGA1064_MISC_CTL_DAC_RAM_CS;
939 		if (mdev->has_sdram)
940 			option = 0x40049120;
941 		else
942 			option = 0x4004d120;
943 		option2 = 0x00008000;
944 		break;
945 	case G200_WB:
946 	case G200_EW3:
947 		dacvalue[MGA1064_VREF_CTL] = 0x07;
948 		option = 0x41049120;
949 		option2 = 0x0000b000;
950 		break;
951 	case G200_EV:
952 		dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
953 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
954 					     MGA1064_MISC_CTL_DAC_RAM_CS;
955 		option = 0x00000120;
956 		option2 = 0x0000b000;
957 		break;
958 	case G200_EH:
959 	case G200_EH3:
960 		dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
961 					     MGA1064_MISC_CTL_DAC_RAM_CS;
962 		option = 0x00000120;
963 		option2 = 0x0000b000;
964 		break;
965 	case G200_ER:
966 		break;
967 	}
968 
969 	switch (fb->format->cpp[0] * 8) {
970 	case 8:
971 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
972 		break;
973 	case 16:
974 		if (fb->format->depth == 15)
975 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
976 		else
977 			dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
978 		break;
979 	case 24:
980 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
981 		break;
982 	case 32:
983 		dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
984 		break;
985 	}
986 
987 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
988 		misc |= 0x40;
989 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
990 		misc |= 0x80;
991 
992 
993 	for (i = 0; i < sizeof(dacvalue); i++) {
994 		if ((i <= 0x17) ||
995 		    (i == 0x1b) ||
996 		    (i == 0x1c) ||
997 		    ((i >= 0x1f) && (i <= 0x29)) ||
998 		    ((i >= 0x30) && (i <= 0x37)))
999 			continue;
1000 		if (IS_G200_SE(mdev) &&
1001 		    ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
1002 			continue;
1003 		if ((mdev->type == G200_EV ||
1004 		    mdev->type == G200_WB ||
1005 		    mdev->type == G200_EH ||
1006 		    mdev->type == G200_EW3 ||
1007 		    mdev->type == G200_EH3) &&
1008 		    (i >= 0x44) && (i <= 0x4e))
1009 			continue;
1010 
1011 		WREG_DAC(i, dacvalue[i]);
1012 	}
1013 
1014 	if (mdev->type == G200_ER)
1015 		WREG_DAC(0x90, 0);
1016 
1017 	if (option)
1018 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
1019 	if (option2)
1020 		pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
1021 
1022 	WREG_SEQ(2, 0xf);
1023 	WREG_SEQ(3, 0);
1024 	WREG_SEQ(4, 0xe);
1025 
1026 	pitch = fb->pitches[0] / fb->format->cpp[0];
1027 	if (fb->format->cpp[0] * 8 == 24)
1028 		pitch = (pitch * 3) >> (4 - bppshift);
1029 	else
1030 		pitch = pitch >> (4 - bppshift);
1031 
1032 	hdisplay = mode->hdisplay / 8 - 1;
1033 	hsyncstart = mode->hsync_start / 8 - 1;
1034 	hsyncend = mode->hsync_end / 8 - 1;
1035 	htotal = mode->htotal / 8 - 1;
1036 
1037 	/* Work around hardware quirk */
1038 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
1039 		htotal++;
1040 
1041 	vdisplay = mode->vdisplay - 1;
1042 	vsyncstart = mode->vsync_start - 1;
1043 	vsyncend = mode->vsync_end - 1;
1044 	vtotal = mode->vtotal - 2;
1045 
1046 	WREG_GFX(0, 0);
1047 	WREG_GFX(1, 0);
1048 	WREG_GFX(2, 0);
1049 	WREG_GFX(3, 0);
1050 	WREG_GFX(4, 0);
1051 	WREG_GFX(5, 0x40);
1052 	WREG_GFX(6, 0x5);
1053 	WREG_GFX(7, 0xf);
1054 	WREG_GFX(8, 0xf);
1055 
1056 	WREG_CRT(0, htotal - 4);
1057 	WREG_CRT(1, hdisplay);
1058 	WREG_CRT(2, hdisplay);
1059 	WREG_CRT(3, (htotal & 0x1F) | 0x80);
1060 	WREG_CRT(4, hsyncstart);
1061 	WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
1062 	WREG_CRT(6, vtotal & 0xFF);
1063 	WREG_CRT(7, ((vtotal & 0x100) >> 8) |
1064 		 ((vdisplay & 0x100) >> 7) |
1065 		 ((vsyncstart & 0x100) >> 6) |
1066 		 ((vdisplay & 0x100) >> 5) |
1067 		 ((vdisplay & 0x100) >> 4) | /* linecomp */
1068 		 ((vtotal & 0x200) >> 4)|
1069 		 ((vdisplay & 0x200) >> 3) |
1070 		 ((vsyncstart & 0x200) >> 2));
1071 	WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
1072 		 ((vdisplay & 0x200) >> 3));
1073 	WREG_CRT(10, 0);
1074 	WREG_CRT(11, 0);
1075 	WREG_CRT(12, 0);
1076 	WREG_CRT(13, 0);
1077 	WREG_CRT(14, 0);
1078 	WREG_CRT(15, 0);
1079 	WREG_CRT(16, vsyncstart & 0xFF);
1080 	WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
1081 	WREG_CRT(18, vdisplay & 0xFF);
1082 	WREG_CRT(19, pitch & 0xFF);
1083 	WREG_CRT(20, 0);
1084 	WREG_CRT(21, vdisplay & 0xFF);
1085 	WREG_CRT(22, (vtotal + 1) & 0xFF);
1086 	WREG_CRT(23, 0xc3);
1087 	WREG_CRT(24, vdisplay & 0xFF);
1088 
1089 	ext_vga[0] = 0;
1090 	ext_vga[5] = 0;
1091 
1092 	/* TODO interlace */
1093 
1094 	ext_vga[0] |= (pitch & 0x300) >> 4;
1095 	ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
1096 		((hdisplay & 0x100) >> 7) |
1097 		((hsyncstart & 0x100) >> 6) |
1098 		(htotal & 0x40);
1099 	ext_vga[2] = ((vtotal & 0xc00) >> 10) |
1100 		((vdisplay & 0x400) >> 8) |
1101 		((vdisplay & 0xc00) >> 7) |
1102 		((vsyncstart & 0xc00) >> 5) |
1103 		((vdisplay & 0x400) >> 3);
1104 	if (fb->format->cpp[0] * 8 == 24)
1105 		ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
1106 	else
1107 		ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
1108 	ext_vga[4] = 0;
1109 	if (mdev->type == G200_WB || mdev->type == G200_EW3)
1110 		ext_vga[1] |= 0x88;
1111 
1112 	/* Set pixel clocks */
1113 	misc = 0x2d;
1114 	WREG8(MGA_MISC_OUT, misc);
1115 
1116 	mga_crtc_set_plls(mdev, mode->clock);
1117 
1118 	for (i = 0; i < 6; i++) {
1119 		WREG_ECRT(i, ext_vga[i]);
1120 	}
1121 
1122 	if (mdev->type == G200_ER)
1123 		WREG_ECRT(0x24, 0x5);
1124 
1125 	if (mdev->type == G200_EW3)
1126 		WREG_ECRT(0x34, 0x5);
1127 
1128 	if (mdev->type == G200_EV) {
1129 		WREG_ECRT(6, 0);
1130 	}
1131 
1132 	WREG_ECRT(0, ext_vga[0]);
1133 	/* Enable mga pixel clock */
1134 	misc = 0x2d;
1135 
1136 	WREG8(MGA_MISC_OUT, misc);
1137 
1138 	if (adjusted_mode)
1139 		memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
1140 
1141 	mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
1142 
1143 	/* reset tagfifo */
1144 	if (mdev->type == G200_ER) {
1145 		u32 mem_ctl = RREG32(MGAREG_MEMCTL);
1146 		u8 seq1;
1147 
1148 		/* screen off */
1149 		WREG8(MGAREG_SEQ_INDEX, 0x01);
1150 		seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
1151 		WREG8(MGAREG_SEQ_DATA, seq1);
1152 
1153 		WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
1154 		udelay(1000);
1155 		WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
1156 
1157 		WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
1158 	}
1159 
1160 
1161 	if (IS_G200_SE(mdev)) {
1162 		if  (mdev->unique_rev_id >= 0x04) {
1163 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1164 			WREG8(MGAREG_CRTCEXT_DATA, 0);
1165 		} else if (mdev->unique_rev_id >= 0x02) {
1166 			u8 hi_pri_lvl;
1167 			u32 bpp;
1168 			u32 mb;
1169 
1170 			if (fb->format->cpp[0] * 8 > 16)
1171 				bpp = 32;
1172 			else if (fb->format->cpp[0] * 8 > 8)
1173 				bpp = 16;
1174 			else
1175 				bpp = 8;
1176 
1177 			mb = (mode->clock * bpp) / 1000;
1178 			if (mb > 3100)
1179 				hi_pri_lvl = 0;
1180 			else if (mb > 2600)
1181 				hi_pri_lvl = 1;
1182 			else if (mb > 1900)
1183 				hi_pri_lvl = 2;
1184 			else if (mb > 1160)
1185 				hi_pri_lvl = 3;
1186 			else if (mb > 440)
1187 				hi_pri_lvl = 4;
1188 			else
1189 				hi_pri_lvl = 5;
1190 
1191 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1192 			WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl);
1193 		} else {
1194 			WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1195 			if (mdev->unique_rev_id >= 0x01)
1196 				WREG8(MGAREG_CRTCEXT_DATA, 0x03);
1197 			else
1198 				WREG8(MGAREG_CRTCEXT_DATA, 0x04);
1199 		}
1200 	}
1201 	return 0;
1202 }
1203 
1204 #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1205 static int mga_suspend(struct drm_crtc *crtc)
1206 {
1207 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1208 	struct drm_device *dev = crtc->dev;
1209 	struct mga_device *mdev = dev->dev_private;
1210 	struct pci_dev *pdev = dev->pdev;
1211 	int option;
1212 
1213 	if (mdev->suspended)
1214 		return 0;
1215 
1216 	WREG_SEQ(1, 0x20);
1217 	WREG_ECRT(1, 0x30);
1218 	/* Disable the pixel clock */
1219 	WREG_DAC(0x1a, 0x05);
1220 	/* Power down the DAC */
1221 	WREG_DAC(0x1e, 0x18);
1222 	/* Power down the pixel PLL */
1223 	WREG_DAC(0x1a, 0x0d);
1224 
1225 	/* Disable PLLs and clocks */
1226 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1227 	option &= ~(0x1F8024);
1228 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1229 	pci_set_power_state(pdev, PCI_D3hot);
1230 	pci_disable_device(pdev);
1231 
1232 	mdev->suspended = true;
1233 
1234 	return 0;
1235 }
1236 
1237 static int mga_resume(struct drm_crtc *crtc)
1238 {
1239 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1240 	struct drm_device *dev = crtc->dev;
1241 	struct mga_device *mdev = dev->dev_private;
1242 	struct pci_dev *pdev = dev->pdev;
1243 	int option;
1244 
1245 	if (!mdev->suspended)
1246 		return 0;
1247 
1248 	pci_set_power_state(pdev, PCI_D0);
1249 	pci_enable_device(pdev);
1250 
1251 	/* Disable sysclk */
1252 	pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1253 	option &= ~(0x4);
1254 	pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1255 
1256 	mdev->suspended = false;
1257 
1258 	return 0;
1259 }
1260 
1261 #endif
1262 
1263 static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1264 {
1265 	struct drm_device *dev = crtc->dev;
1266 	struct mga_device *mdev = dev->dev_private;
1267 	u8 seq1 = 0, crtcext1 = 0;
1268 
1269 	switch (mode) {
1270 	case DRM_MODE_DPMS_ON:
1271 		seq1 = 0;
1272 		crtcext1 = 0;
1273 		mga_crtc_load_lut(crtc);
1274 		break;
1275 	case DRM_MODE_DPMS_STANDBY:
1276 		seq1 = 0x20;
1277 		crtcext1 = 0x10;
1278 		break;
1279 	case DRM_MODE_DPMS_SUSPEND:
1280 		seq1 = 0x20;
1281 		crtcext1 = 0x20;
1282 		break;
1283 	case DRM_MODE_DPMS_OFF:
1284 		seq1 = 0x20;
1285 		crtcext1 = 0x30;
1286 		break;
1287 	}
1288 
1289 #if 0
1290 	if (mode == DRM_MODE_DPMS_OFF) {
1291 		mga_suspend(crtc);
1292 	}
1293 #endif
1294 	WREG8(MGAREG_SEQ_INDEX, 0x01);
1295 	seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1296 	mga_wait_vsync(mdev);
1297 	mga_wait_busy(mdev);
1298 	WREG8(MGAREG_SEQ_DATA, seq1);
1299 	msleep(20);
1300 	WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1301 	crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1302 	WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1303 
1304 #if 0
1305 	if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1306 		mga_resume(crtc);
1307 		drm_helper_resume_force_mode(dev);
1308 	}
1309 #endif
1310 }
1311 
1312 /*
1313  * This is called before a mode is programmed. A typical use might be to
1314  * enable DPMS during the programming to avoid seeing intermediate stages,
1315  * but that's not relevant to us
1316  */
1317 static void mga_crtc_prepare(struct drm_crtc *crtc)
1318 {
1319 	struct drm_device *dev = crtc->dev;
1320 	struct mga_device *mdev = dev->dev_private;
1321 	u8 tmp;
1322 
1323 	/*	mga_resume(crtc);*/
1324 
1325 	WREG8(MGAREG_CRTC_INDEX, 0x11);
1326 	tmp = RREG8(MGAREG_CRTC_DATA);
1327 	WREG_CRT(0x11, tmp | 0x80);
1328 
1329 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1330 		WREG_SEQ(0, 1);
1331 		msleep(50);
1332 		WREG_SEQ(1, 0x20);
1333 		msleep(20);
1334 	} else {
1335 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1336 		tmp = RREG8(MGAREG_SEQ_DATA);
1337 
1338 		/* start sync reset */
1339 		WREG_SEQ(0, 1);
1340 		WREG_SEQ(1, tmp | 0x20);
1341 	}
1342 
1343 	if (mdev->type == G200_WB || mdev->type == G200_EW3)
1344 		mga_g200wb_prepare(crtc);
1345 
1346 	WREG_CRT(17, 0);
1347 }
1348 
1349 /*
1350  * This is called after a mode is programmed. It should reverse anything done
1351  * by the prepare function
1352  */
1353 static void mga_crtc_commit(struct drm_crtc *crtc)
1354 {
1355 	struct drm_device *dev = crtc->dev;
1356 	struct mga_device *mdev = dev->dev_private;
1357 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1358 	u8 tmp;
1359 
1360 	if (mdev->type == G200_WB || mdev->type == G200_EW3)
1361 		mga_g200wb_commit(crtc);
1362 
1363 	if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1364 		msleep(50);
1365 		WREG_SEQ(1, 0x0);
1366 		msleep(20);
1367 		WREG_SEQ(0, 0x3);
1368 	} else {
1369 		WREG8(MGAREG_SEQ_INDEX, 0x1);
1370 		tmp = RREG8(MGAREG_SEQ_DATA);
1371 
1372 		tmp &= ~0x20;
1373 		WREG_SEQ(0x1, tmp);
1374 		WREG_SEQ(0, 3);
1375 	}
1376 	crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1377 }
1378 
1379 /*
1380  * The core can pass us a set of gamma values to program. We actually only
1381  * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1382  * but it's a requirement that we provide the function
1383  */
1384 static int mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1385 			      u16 *blue, uint32_t size,
1386 			      struct drm_modeset_acquire_ctx *ctx)
1387 {
1388 	mga_crtc_load_lut(crtc);
1389 
1390 	return 0;
1391 }
1392 
1393 /* Simple cleanup function */
1394 static void mga_crtc_destroy(struct drm_crtc *crtc)
1395 {
1396 	struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1397 
1398 	drm_crtc_cleanup(crtc);
1399 	kfree(mga_crtc);
1400 }
1401 
1402 static void mga_crtc_disable(struct drm_crtc *crtc)
1403 {
1404 	DRM_DEBUG_KMS("\n");
1405 	mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1406 	if (crtc->primary->fb) {
1407 		struct drm_framebuffer *fb = crtc->primary->fb;
1408 		struct drm_gem_vram_object *gbo =
1409 			drm_gem_vram_of_gem(fb->obj[0]);
1410 		drm_gem_vram_unpin(gbo);
1411 	}
1412 	crtc->primary->fb = NULL;
1413 }
1414 
1415 /* These provide the minimum set of functions required to handle a CRTC */
1416 static const struct drm_crtc_funcs mga_crtc_funcs = {
1417 	.cursor_set = mgag200_crtc_cursor_set,
1418 	.cursor_move = mgag200_crtc_cursor_move,
1419 	.gamma_set = mga_crtc_gamma_set,
1420 	.set_config = drm_crtc_helper_set_config,
1421 	.destroy = mga_crtc_destroy,
1422 };
1423 
1424 static const struct drm_crtc_helper_funcs mga_helper_funcs = {
1425 	.disable = mga_crtc_disable,
1426 	.dpms = mga_crtc_dpms,
1427 	.mode_set = mga_crtc_mode_set,
1428 	.mode_set_base = mga_crtc_mode_set_base,
1429 	.prepare = mga_crtc_prepare,
1430 	.commit = mga_crtc_commit,
1431 };
1432 
1433 /* CRTC setup */
1434 static void mga_crtc_init(struct mga_device *mdev)
1435 {
1436 	struct mga_crtc *mga_crtc;
1437 
1438 	mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1439 			      (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1440 			      GFP_KERNEL);
1441 
1442 	if (mga_crtc == NULL)
1443 		return;
1444 
1445 	drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
1446 
1447 	drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1448 	mdev->mode_info.crtc = mga_crtc;
1449 
1450 	drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1451 }
1452 
1453 static int mga_vga_get_modes(struct drm_connector *connector)
1454 {
1455 	struct mga_connector *mga_connector = to_mga_connector(connector);
1456 	struct edid *edid;
1457 	int ret = 0;
1458 
1459 	edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1460 	if (edid) {
1461 		drm_connector_update_edid_property(connector, edid);
1462 		ret = drm_add_edid_modes(connector, edid);
1463 		kfree(edid);
1464 	}
1465 	return ret;
1466 }
1467 
1468 static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
1469 							int bits_per_pixel)
1470 {
1471 	uint32_t total_area, divisor;
1472 	uint64_t active_area, pixels_per_second, bandwidth;
1473 	uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
1474 
1475 	divisor = 1024;
1476 
1477 	if (!mode->htotal || !mode->vtotal || !mode->clock)
1478 		return 0;
1479 
1480 	active_area = mode->hdisplay * mode->vdisplay;
1481 	total_area = mode->htotal * mode->vtotal;
1482 
1483 	pixels_per_second = active_area * mode->clock * 1000;
1484 	do_div(pixels_per_second, total_area);
1485 
1486 	bandwidth = pixels_per_second * bytes_per_pixel * 100;
1487 	do_div(bandwidth, divisor);
1488 
1489 	return (uint32_t)(bandwidth);
1490 }
1491 
1492 #define MODE_BANDWIDTH	MODE_BAD
1493 
1494 static enum drm_mode_status mga_vga_mode_valid(struct drm_connector *connector,
1495 				 struct drm_display_mode *mode)
1496 {
1497 	struct drm_device *dev = connector->dev;
1498 	struct mga_device *mdev = (struct mga_device*)dev->dev_private;
1499 	int bpp = 32;
1500 
1501 	if (IS_G200_SE(mdev)) {
1502 		if (mdev->unique_rev_id == 0x01) {
1503 			if (mode->hdisplay > 1600)
1504 				return MODE_VIRTUAL_X;
1505 			if (mode->vdisplay > 1200)
1506 				return MODE_VIRTUAL_Y;
1507 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1508 				> (24400 * 1024))
1509 				return MODE_BANDWIDTH;
1510 		} else if (mdev->unique_rev_id == 0x02) {
1511 			if (mode->hdisplay > 1920)
1512 				return MODE_VIRTUAL_X;
1513 			if (mode->vdisplay > 1200)
1514 				return MODE_VIRTUAL_Y;
1515 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1516 				> (30100 * 1024))
1517 				return MODE_BANDWIDTH;
1518 		} else {
1519 			if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1520 				> (55000 * 1024))
1521 				return MODE_BANDWIDTH;
1522 		}
1523 	} else if (mdev->type == G200_WB) {
1524 		if (mode->hdisplay > 1280)
1525 			return MODE_VIRTUAL_X;
1526 		if (mode->vdisplay > 1024)
1527 			return MODE_VIRTUAL_Y;
1528 		if (mga_vga_calculate_mode_bandwidth(mode, bpp) >
1529 		    (31877 * 1024))
1530 			return MODE_BANDWIDTH;
1531 	} else if (mdev->type == G200_EV &&
1532 		(mga_vga_calculate_mode_bandwidth(mode, bpp)
1533 			> (32700 * 1024))) {
1534 		return MODE_BANDWIDTH;
1535 	} else if (mdev->type == G200_EH &&
1536 		(mga_vga_calculate_mode_bandwidth(mode, bpp)
1537 			> (37500 * 1024))) {
1538 		return MODE_BANDWIDTH;
1539 	} else if (mdev->type == G200_ER &&
1540 		(mga_vga_calculate_mode_bandwidth(mode,
1541 			bpp) > (55000 * 1024))) {
1542 		return MODE_BANDWIDTH;
1543 	}
1544 
1545 	if ((mode->hdisplay % 8) != 0 || (mode->hsync_start % 8) != 0 ||
1546 	    (mode->hsync_end % 8) != 0 || (mode->htotal % 8) != 0) {
1547 		return MODE_H_ILLEGAL;
1548 	}
1549 
1550 	if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1551 	    mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1552 	    mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1553 	    mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1554 		return MODE_BAD;
1555 	}
1556 
1557 	/* Validate the mode input by the user */
1558 	if (connector->cmdline_mode.specified) {
1559 		if (connector->cmdline_mode.bpp_specified)
1560 			bpp = connector->cmdline_mode.bpp;
1561 	}
1562 
1563 	if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->vram_fb_available) {
1564 		if (connector->cmdline_mode.specified)
1565 			connector->cmdline_mode.specified = false;
1566 		return MODE_BAD;
1567 	}
1568 
1569 	return MODE_OK;
1570 }
1571 
1572 static void mga_connector_destroy(struct drm_connector *connector)
1573 {
1574 	struct mga_connector *mga_connector = to_mga_connector(connector);
1575 	mgag200_i2c_destroy(mga_connector->i2c);
1576 	drm_connector_cleanup(connector);
1577 	kfree(connector);
1578 }
1579 
1580 static const struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
1581 	.get_modes = mga_vga_get_modes,
1582 	.mode_valid = mga_vga_mode_valid,
1583 };
1584 
1585 static const struct drm_connector_funcs mga_vga_connector_funcs = {
1586 	.dpms = drm_helper_connector_dpms,
1587 	.fill_modes = drm_helper_probe_single_connector_modes,
1588 	.destroy = mga_connector_destroy,
1589 };
1590 
1591 static struct drm_connector *mga_vga_init(struct drm_device *dev)
1592 {
1593 	struct drm_connector *connector;
1594 	struct mga_connector *mga_connector;
1595 
1596 	mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1597 	if (!mga_connector)
1598 		return NULL;
1599 
1600 	connector = &mga_connector->base;
1601 	mga_connector->i2c = mgag200_i2c_create(dev);
1602 	if (!mga_connector->i2c)
1603 		DRM_ERROR("failed to add ddc bus\n");
1604 
1605 	drm_connector_init_with_ddc(dev, connector,
1606 				    &mga_vga_connector_funcs,
1607 				    DRM_MODE_CONNECTOR_VGA,
1608 				    &mga_connector->i2c->adapter);
1609 
1610 	drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1611 
1612 	drm_connector_register(connector);
1613 
1614 	return connector;
1615 }
1616 
1617 
1618 int mgag200_modeset_init(struct mga_device *mdev)
1619 {
1620 	struct drm_encoder *encoder = &mdev->encoder;
1621 	struct drm_connector *connector;
1622 	int ret;
1623 
1624 	mdev->mode_info.mode_config_initialized = true;
1625 
1626 	mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1627 	mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1628 
1629 	mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1630 
1631 	mga_crtc_init(mdev);
1632 
1633 	ret = drm_simple_encoder_init(mdev->dev, encoder,
1634 				      DRM_MODE_ENCODER_DAC);
1635 	if (ret) {
1636 		drm_err(mdev->dev,
1637 			"drm_simple_encoder_init() failed, error %d\n",
1638 			ret);
1639 		return ret;
1640 	}
1641 	encoder->possible_crtcs = 0x1;
1642 
1643 	connector = mga_vga_init(mdev->dev);
1644 	if (!connector) {
1645 		DRM_ERROR("mga_vga_init failed\n");
1646 		return -1;
1647 	}
1648 
1649 	drm_connector_attach_encoder(connector, encoder);
1650 
1651 	return 0;
1652 }
1653 
1654 void mgag200_modeset_fini(struct mga_device *mdev)
1655 {
1656 
1657 }
1658