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