2 * Copyright 2010 Matt Turner.
3 * Copyright 2012 Red Hat
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.
9 * Authors: Matthew Garrett
14 #include <linux/delay.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_plane_helper.h>
20 #include "mgag200_drv.h"
22 #define MGAG200_LUT_SIZE 256
25 * This file contains setup code for the CRTC.
28 static void mga_crtc_load_lut(struct drm_crtc *crtc)
30 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
31 struct drm_device *dev = crtc->dev;
32 struct mga_device *mdev = dev->dev_private;
33 struct drm_framebuffer *fb = crtc->primary->fb;
39 WREG8(DAC_INDEX + MGA1064_INDEX, 0);
41 if (fb && fb->bits_per_pixel == 16) {
42 int inc = (fb->depth == 15) ? 8 : 4;
44 for (i = 0; i < MGAG200_LUT_SIZE; i += inc) {
45 if (fb->depth == 16) {
46 if (i > (MGAG200_LUT_SIZE >> 1)) {
49 r = mga_crtc->lut_r[i << 1];
50 b = mga_crtc->lut_b[i << 1];
53 r = mga_crtc->lut_r[i];
54 b = mga_crtc->lut_b[i];
57 WREG8(DAC_INDEX + MGA1064_COL_PAL, r);
58 WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
59 WREG8(DAC_INDEX + MGA1064_COL_PAL, b);
63 for (i = 0; i < MGAG200_LUT_SIZE; i++) {
65 WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_r[i]);
66 WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
67 WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_b[i]);
71 static inline void mga_wait_vsync(struct mga_device *mdev)
73 unsigned long timeout = jiffies + HZ/10;
74 unsigned int status = 0;
77 status = RREG32(MGAREG_Status);
78 } while ((status & 0x08) && time_before(jiffies, timeout));
79 timeout = jiffies + HZ/10;
82 status = RREG32(MGAREG_Status);
83 } while (!(status & 0x08) && time_before(jiffies, timeout));
86 static inline void mga_wait_busy(struct mga_device *mdev)
88 unsigned long timeout = jiffies + HZ;
89 unsigned int status = 0;
91 status = RREG8(MGAREG_Status + 2);
92 } while ((status & 0x01) && time_before(jiffies, timeout));
96 * The core passes the desired mode to the CRTC code to see whether any
97 * CRTC-specific modifications need to be made to it. We're in a position
98 * to just pass that straight through, so this does nothing
100 static bool mga_crtc_mode_fixup(struct drm_crtc *crtc,
101 const struct drm_display_mode *mode,
102 struct drm_display_mode *adjusted_mode)
107 #define P_ARRAY_SIZE 9
109 static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
111 unsigned int vcomax, vcomin, pllreffreq;
112 unsigned int delta, tmpdelta, permitteddelta;
113 unsigned int testp, testm, testn;
114 unsigned int p, m, n;
115 unsigned int computed;
116 unsigned int pvalues_e4[P_ARRAY_SIZE] = {16, 14, 12, 10, 8, 6, 4, 2, 1};
120 if (mdev->unique_rev_id <= 0x03) {
128 permitteddelta = clock * 5 / 1000;
130 for (testp = 8; testp > 0; testp /= 2) {
131 if (clock * testp > vcomax)
133 if (clock * testp < vcomin)
136 for (testn = 17; testn < 256; testn++) {
137 for (testm = 1; testm < 32; testm++) {
138 computed = (pllreffreq * testn) /
140 if (computed > clock)
141 tmpdelta = computed - clock;
143 tmpdelta = clock - computed;
144 if (tmpdelta < delta) {
167 /* Permited delta is 0.5% as VESA Specification */
168 permitteddelta = clock * 5 / 1000;
170 for (i = 0 ; i < P_ARRAY_SIZE ; i++) {
171 testp = pvalues_e4[i];
173 if ((clock * testp) > vcomax)
175 if ((clock * testp) < vcomin)
178 for (testn = 50; testn <= 256; testn++) {
179 for (testm = 1; testm <= 32; testm++) {
180 computed = (pllreffreq * testn) /
182 if (computed > clock)
183 tmpdelta = computed - clock;
185 tmpdelta = clock - computed;
187 if (tmpdelta < delta) {
197 fvv = pllreffreq * testn / testm;
198 fvv = (fvv - 800000) / 50000;
209 if (delta > permitteddelta) {
210 printk(KERN_WARNING "PLL delta too large\n");
214 WREG_DAC(MGA1064_PIX_PLLC_M, m);
215 WREG_DAC(MGA1064_PIX_PLLC_N, n);
216 WREG_DAC(MGA1064_PIX_PLLC_P, p);
220 static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
222 unsigned int vcomax, vcomin, pllreffreq;
223 unsigned int delta, tmpdelta;
224 unsigned int testp, testm, testn, testp2;
225 unsigned int p, m, n;
226 unsigned int computed;
227 int i, j, tmpcount, vcount;
228 bool pll_locked = false;
235 if (mdev->type == G200_EW3) {
241 for (testp = 1; testp < 8; testp++) {
242 for (testp2 = 1; testp2 < 8; testp2++) {
245 if ((clock * testp * testp2) > vcomax)
247 if ((clock * testp * testp2) < vcomin)
249 for (testm = 1; testm < 26; testm++) {
250 for (testn = 32; testn < 2048 ; testn++) {
251 computed = (pllreffreq * testn) /
252 (testm * testp * testp2);
253 if (computed > clock)
254 tmpdelta = computed - clock;
256 tmpdelta = clock - computed;
257 if (tmpdelta < delta) {
259 m = ((testn & 0x100) >> 1) |
262 p = ((testn & 0x600) >> 3) |
276 for (testp = 1; testp < 9; testp++) {
277 if (clock * testp > vcomax)
279 if (clock * testp < vcomin)
282 for (testm = 1; testm < 17; testm++) {
283 for (testn = 1; testn < 151; testn++) {
284 computed = (pllreffreq * testn) /
286 if (computed > clock)
287 tmpdelta = computed - clock;
289 tmpdelta = clock - computed;
290 if (tmpdelta < delta) {
302 for (i = 0; i <= 32 && pll_locked == false; i++) {
304 WREG8(MGAREG_CRTC_INDEX, 0x1e);
305 tmp = RREG8(MGAREG_CRTC_DATA);
307 WREG8(MGAREG_CRTC_DATA, tmp+1);
310 /* set pixclkdis to 1 */
311 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
312 tmp = RREG8(DAC_DATA);
313 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
314 WREG8(DAC_DATA, tmp);
316 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
317 tmp = RREG8(DAC_DATA);
318 tmp |= MGA1064_REMHEADCTL_CLKDIS;
319 WREG8(DAC_DATA, tmp);
321 /* select PLL Set C */
322 tmp = RREG8(MGAREG_MEM_MISC_READ);
324 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
326 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
327 tmp = RREG8(DAC_DATA);
328 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
329 WREG8(DAC_DATA, tmp);
334 WREG8(DAC_INDEX, MGA1064_VREF_CTL);
335 tmp = RREG8(DAC_DATA);
337 WREG8(DAC_DATA, tmp);
341 /* program pixel pll register */
342 WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
343 WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
344 WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
349 WREG8(DAC_INDEX, MGA1064_VREF_CTL);
350 tmp = RREG8(DAC_DATA);
352 WREG_DAC(MGA1064_VREF_CTL, tmp);
356 /* select the pixel pll */
357 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
358 tmp = RREG8(DAC_DATA);
359 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
360 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
361 WREG8(DAC_DATA, tmp);
363 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
364 tmp = RREG8(DAC_DATA);
365 tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
366 tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
367 WREG8(DAC_DATA, tmp);
369 /* reset dotclock rate bit */
370 WREG8(MGAREG_SEQ_INDEX, 1);
371 tmp = RREG8(MGAREG_SEQ_DATA);
373 WREG8(MGAREG_SEQ_DATA, tmp);
375 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
376 tmp = RREG8(DAC_DATA);
377 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
378 WREG8(DAC_DATA, tmp);
380 vcount = RREG8(MGAREG_VCOUNT);
382 for (j = 0; j < 30 && pll_locked == false; j++) {
383 tmpcount = RREG8(MGAREG_VCOUNT);
384 if (tmpcount < vcount)
386 if ((tmpcount - vcount) > 2)
392 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
393 tmp = RREG8(DAC_DATA);
394 tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
395 WREG_DAC(MGA1064_REMHEADCTL, tmp);
399 static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
401 unsigned int vcomax, vcomin, pllreffreq;
402 unsigned int delta, tmpdelta;
403 unsigned int testp, testm, testn;
404 unsigned int p, m, n;
405 unsigned int computed;
415 for (testp = 16; testp > 0; testp--) {
416 if (clock * testp > vcomax)
418 if (clock * testp < vcomin)
421 for (testn = 1; testn < 257; testn++) {
422 for (testm = 1; testm < 17; testm++) {
423 computed = (pllreffreq * testn) /
425 if (computed > clock)
426 tmpdelta = computed - clock;
428 tmpdelta = clock - computed;
429 if (tmpdelta < delta) {
439 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
440 tmp = RREG8(DAC_DATA);
441 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
442 WREG8(DAC_DATA, tmp);
444 tmp = RREG8(MGAREG_MEM_MISC_READ);
446 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
448 WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
449 tmp = RREG8(DAC_DATA);
450 WREG8(DAC_DATA, tmp & ~0x40);
452 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
453 tmp = RREG8(DAC_DATA);
454 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
455 WREG8(DAC_DATA, tmp);
457 WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
458 WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
459 WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
463 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
464 tmp = RREG8(DAC_DATA);
465 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
466 WREG8(DAC_DATA, tmp);
470 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
471 tmp = RREG8(DAC_DATA);
472 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
473 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
474 WREG8(DAC_DATA, tmp);
476 WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
477 tmp = RREG8(DAC_DATA);
478 WREG8(DAC_DATA, tmp | 0x40);
480 tmp = RREG8(MGAREG_MEM_MISC_READ);
482 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
484 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
485 tmp = RREG8(DAC_DATA);
486 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
487 WREG8(DAC_DATA, tmp);
492 static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
494 unsigned int vcomax, vcomin, pllreffreq;
495 unsigned int delta, tmpdelta;
496 unsigned int testp, testm, testn;
497 unsigned int p, m, n;
498 unsigned int computed;
499 int i, j, tmpcount, vcount;
501 bool pll_locked = false;
510 for (testp = 16; testp > 0; testp >>= 1) {
511 if (clock * testp > vcomax)
513 if (clock * testp < vcomin)
516 for (testm = 1; testm < 33; testm++) {
517 for (testn = 17; testn < 257; testn++) {
518 computed = (pllreffreq * testn) /
520 if (computed > clock)
521 tmpdelta = computed - clock;
523 tmpdelta = clock - computed;
524 if (tmpdelta < delta) {
530 if ((clock * testp) >= 600000)
535 for (i = 0; i <= 32 && pll_locked == false; i++) {
536 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
537 tmp = RREG8(DAC_DATA);
538 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
539 WREG8(DAC_DATA, tmp);
541 tmp = RREG8(MGAREG_MEM_MISC_READ);
543 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
545 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
546 tmp = RREG8(DAC_DATA);
547 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
548 WREG8(DAC_DATA, tmp);
552 WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
553 WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
554 WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
558 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
559 tmp = RREG8(DAC_DATA);
560 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
561 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
562 WREG8(DAC_DATA, tmp);
564 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
565 tmp = RREG8(DAC_DATA);
566 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
567 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
568 WREG8(DAC_DATA, tmp);
570 vcount = RREG8(MGAREG_VCOUNT);
572 for (j = 0; j < 30 && pll_locked == false; j++) {
573 tmpcount = RREG8(MGAREG_VCOUNT);
574 if (tmpcount < vcount)
576 if ((tmpcount - vcount) > 2)
586 static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
588 unsigned int vcomax, vcomin, pllreffreq;
589 unsigned int delta, tmpdelta;
590 int testr, testn, testm, testo;
591 unsigned int p, m, n;
592 unsigned int computed, vco;
594 const unsigned int m_div_val[] = { 1, 2, 4, 8 };
603 for (testr = 0; testr < 4; testr++) {
606 for (testn = 5; testn < 129; testn++) {
609 for (testm = 3; testm >= 0; testm--) {
612 for (testo = 5; testo < 33; testo++) {
613 vco = pllreffreq * (testn + 1) /
619 computed = vco / (m_div_val[testm] * (testo + 1));
620 if (computed > clock)
621 tmpdelta = computed - clock;
623 tmpdelta = clock - computed;
624 if (tmpdelta < delta) {
626 m = testm | (testo << 3);
628 p = testr | (testr << 3);
635 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
636 tmp = RREG8(DAC_DATA);
637 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
638 WREG8(DAC_DATA, tmp);
640 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
641 tmp = RREG8(DAC_DATA);
642 tmp |= MGA1064_REMHEADCTL_CLKDIS;
643 WREG8(DAC_DATA, tmp);
645 tmp = RREG8(MGAREG_MEM_MISC_READ);
646 tmp |= (0x3<<2) | 0xc0;
647 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
649 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
650 tmp = RREG8(DAC_DATA);
651 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
652 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
653 WREG8(DAC_DATA, tmp);
657 WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
658 WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
659 WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
666 static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
671 return mga_g200se_set_plls(mdev, clock);
675 return mga_g200wb_set_plls(mdev, clock);
678 return mga_g200ev_set_plls(mdev, clock);
681 return mga_g200eh_set_plls(mdev, clock);
684 return mga_g200er_set_plls(mdev, clock);
690 static void mga_g200wb_prepare(struct drm_crtc *crtc)
692 struct mga_device *mdev = crtc->dev->dev_private;
696 /* 1- The first step is to warn the BMC of an upcoming mode change.
697 * We are putting the misc<0> to output.*/
699 WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
700 tmp = RREG8(DAC_DATA);
702 WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
704 /* we are putting a 1 on the misc<0> line */
705 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
706 tmp = RREG8(DAC_DATA);
708 WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
710 /* 2- Second step to mask and further scan request
711 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
713 WREG8(DAC_INDEX, MGA1064_SPAREREG);
714 tmp = RREG8(DAC_DATA);
716 WREG_DAC(MGA1064_SPAREREG, tmp);
718 /* 3a- the third step is to verifu if there is an active scan
719 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
722 while (!(tmp & 0x1) && iter_max) {
723 WREG8(DAC_INDEX, MGA1064_SPAREREG);
724 tmp = RREG8(DAC_DATA);
729 /* 3b- this step occurs only if the remove is actually scanning
730 * we are waiting for the end of the frame which is a 1 on
731 * remvsyncsts (XSPAREREG<1>)
735 while ((tmp & 0x2) && iter_max) {
736 WREG8(DAC_INDEX, MGA1064_SPAREREG);
737 tmp = RREG8(DAC_DATA);
744 static void mga_g200wb_commit(struct drm_crtc *crtc)
747 struct mga_device *mdev = crtc->dev->dev_private;
749 /* 1- The first step is to ensure that the vrsten and hrsten are set */
750 WREG8(MGAREG_CRTCEXT_INDEX, 1);
751 tmp = RREG8(MGAREG_CRTCEXT_DATA);
752 WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
754 /* 2- second step is to assert the rstlvl2 */
755 WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
756 tmp = RREG8(DAC_DATA);
758 WREG8(DAC_DATA, tmp);
763 /* 3- deassert rstlvl2 */
765 WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
766 WREG8(DAC_DATA, tmp);
768 /* 4- remove mask of scan request */
769 WREG8(DAC_INDEX, MGA1064_SPAREREG);
770 tmp = RREG8(DAC_DATA);
772 WREG8(DAC_DATA, tmp);
774 /* 5- put back a 0 on the misc<0> line */
775 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
776 tmp = RREG8(DAC_DATA);
778 WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
782 This is how the framebuffer base address is stored in g200 cards:
783 * Assume @offset is the gpu_addr variable of the framebuffer object
784 * Then addr is the number of _pixels_ (not bytes) from the start of
785 VRAM to the first pixel we want to display. (divided by 2 for 32bit
787 * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
788 addr<20> -> CRTCEXT0<6>
789 addr<19-16> -> CRTCEXT0<3-0>
790 addr<15-8> -> CRTCC<7-0>
791 addr<7-0> -> CRTCD<7-0>
792 CRTCEXT0 has to be programmed last to trigger an update and make the
793 new addr variable take effect.
795 static void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
797 struct mga_device *mdev = crtc->dev->dev_private;
802 while (RREG8(0x1fda) & 0x08);
803 while (!(RREG8(0x1fda) & 0x08));
805 count = RREG8(MGAREG_VCOUNT) + 2;
806 while (RREG8(MGAREG_VCOUNT) < count);
808 WREG8(MGAREG_CRTCEXT_INDEX, 0);
809 crtcext0 = RREG8(MGAREG_CRTCEXT_DATA);
812 /* Can't store addresses any higher than that...
813 but we also don't have more than 16MB of memory, so it should be fine. */
814 WARN_ON(addr > 0x1fffff);
815 crtcext0 |= (!!(addr & (1<<20)))<<6;
816 WREG_CRT(0x0d, (u8)(addr & 0xff));
817 WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
818 WREG_ECRT(0x0, ((u8)(addr >> 16) & 0xf) | crtcext0);
822 /* ast is different - we will force move buffers out of VRAM */
823 static int mga_crtc_do_set_base(struct drm_crtc *crtc,
824 struct drm_framebuffer *fb,
825 int x, int y, int atomic)
827 struct mga_device *mdev = crtc->dev->dev_private;
828 struct drm_gem_object *obj;
829 struct mga_framebuffer *mga_fb;
830 struct mgag200_bo *bo;
834 /* push the previous fb to system ram */
836 mga_fb = to_mga_framebuffer(fb);
838 bo = gem_to_mga_bo(obj);
839 ret = mgag200_bo_reserve(bo, false);
842 mgag200_bo_push_sysram(bo);
843 mgag200_bo_unreserve(bo);
846 mga_fb = to_mga_framebuffer(crtc->primary->fb);
848 bo = gem_to_mga_bo(obj);
850 ret = mgag200_bo_reserve(bo, false);
854 ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
856 mgag200_bo_unreserve(bo);
860 if (&mdev->mfbdev->mfb == mga_fb) {
861 /* if pushing console in kmap it */
862 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
864 DRM_ERROR("failed to kmap fbcon\n");
867 mgag200_bo_unreserve(bo);
869 mga_set_start_address(crtc, (u32)gpu_addr);
874 static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
875 struct drm_framebuffer *old_fb)
877 return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
880 static int mga_crtc_mode_set(struct drm_crtc *crtc,
881 struct drm_display_mode *mode,
882 struct drm_display_mode *adjusted_mode,
883 int x, int y, struct drm_framebuffer *old_fb)
885 struct drm_device *dev = crtc->dev;
886 struct mga_device *mdev = dev->dev_private;
887 int hdisplay, hsyncstart, hsyncend, htotal;
888 int vdisplay, vsyncstart, vsyncend, vtotal;
890 int option = 0, option2 = 0;
892 unsigned char misc = 0;
893 unsigned char ext_vga[6];
896 static unsigned char dacvalue[] = {
897 /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
898 /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
899 /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
900 /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
901 /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
902 /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
903 /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
904 /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
905 /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
906 /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
909 bppshift = mdev->bpp_shifts[(crtc->primary->fb->bits_per_pixel >> 3) - 1];
911 switch (mdev->type) {
914 dacvalue[MGA1064_VREF_CTL] = 0x03;
915 dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
916 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
917 MGA1064_MISC_CTL_VGA8 |
918 MGA1064_MISC_CTL_DAC_RAM_CS;
923 option2 = 0x00008000;
927 dacvalue[MGA1064_VREF_CTL] = 0x07;
929 option2 = 0x0000b000;
932 dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
933 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
934 MGA1064_MISC_CTL_DAC_RAM_CS;
936 option2 = 0x0000b000;
939 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
940 MGA1064_MISC_CTL_DAC_RAM_CS;
942 option2 = 0x0000b000;
948 switch (crtc->primary->fb->bits_per_pixel) {
950 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
953 if (crtc->primary->fb->depth == 15)
954 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
956 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
959 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
962 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
966 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
968 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
972 for (i = 0; i < sizeof(dacvalue); i++) {
976 ((i >= 0x1f) && (i <= 0x29)) ||
977 ((i >= 0x30) && (i <= 0x37)))
979 if (IS_G200_SE(mdev) &&
980 ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
982 if ((mdev->type == G200_EV ||
983 mdev->type == G200_WB ||
984 mdev->type == G200_EH ||
985 mdev->type == G200_EW3) &&
986 (i >= 0x44) && (i <= 0x4e))
989 WREG_DAC(i, dacvalue[i]);
992 if (mdev->type == G200_ER)
996 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
998 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
1004 pitch = crtc->primary->fb->pitches[0] / (crtc->primary->fb->bits_per_pixel / 8);
1005 if (crtc->primary->fb->bits_per_pixel == 24)
1006 pitch = (pitch * 3) >> (4 - bppshift);
1008 pitch = pitch >> (4 - bppshift);
1010 hdisplay = mode->hdisplay / 8 - 1;
1011 hsyncstart = mode->hsync_start / 8 - 1;
1012 hsyncend = mode->hsync_end / 8 - 1;
1013 htotal = mode->htotal / 8 - 1;
1015 /* Work around hardware quirk */
1016 if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
1019 vdisplay = mode->vdisplay - 1;
1020 vsyncstart = mode->vsync_start - 1;
1021 vsyncend = mode->vsync_end - 1;
1022 vtotal = mode->vtotal - 2;
1034 WREG_CRT(0, htotal - 4);
1035 WREG_CRT(1, hdisplay);
1036 WREG_CRT(2, hdisplay);
1037 WREG_CRT(3, (htotal & 0x1F) | 0x80);
1038 WREG_CRT(4, hsyncstart);
1039 WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
1040 WREG_CRT(6, vtotal & 0xFF);
1041 WREG_CRT(7, ((vtotal & 0x100) >> 8) |
1042 ((vdisplay & 0x100) >> 7) |
1043 ((vsyncstart & 0x100) >> 6) |
1044 ((vdisplay & 0x100) >> 5) |
1045 ((vdisplay & 0x100) >> 4) | /* linecomp */
1046 ((vtotal & 0x200) >> 4)|
1047 ((vdisplay & 0x200) >> 3) |
1048 ((vsyncstart & 0x200) >> 2));
1049 WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
1050 ((vdisplay & 0x200) >> 3));
1057 WREG_CRT(16, vsyncstart & 0xFF);
1058 WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
1059 WREG_CRT(18, vdisplay & 0xFF);
1060 WREG_CRT(19, pitch & 0xFF);
1062 WREG_CRT(21, vdisplay & 0xFF);
1063 WREG_CRT(22, (vtotal + 1) & 0xFF);
1065 WREG_CRT(24, vdisplay & 0xFF);
1070 /* TODO interlace */
1072 ext_vga[0] |= (pitch & 0x300) >> 4;
1073 ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
1074 ((hdisplay & 0x100) >> 7) |
1075 ((hsyncstart & 0x100) >> 6) |
1077 ext_vga[2] = ((vtotal & 0xc00) >> 10) |
1078 ((vdisplay & 0x400) >> 8) |
1079 ((vdisplay & 0xc00) >> 7) |
1080 ((vsyncstart & 0xc00) >> 5) |
1081 ((vdisplay & 0x400) >> 3);
1082 if (crtc->primary->fb->bits_per_pixel == 24)
1083 ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
1085 ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
1087 if (mdev->type == G200_WB || mdev->type == G200_EW3)
1090 /* Set pixel clocks */
1092 WREG8(MGA_MISC_OUT, misc);
1094 mga_crtc_set_plls(mdev, mode->clock);
1096 for (i = 0; i < 6; i++) {
1097 WREG_ECRT(i, ext_vga[i]);
1100 if (mdev->type == G200_ER)
1101 WREG_ECRT(0x24, 0x5);
1103 if (mdev->type == G200_EW3)
1104 WREG_ECRT(0x34, 0x5);
1106 if (mdev->type == G200_EV) {
1110 WREG_ECRT(0, ext_vga[0]);
1111 /* Enable mga pixel clock */
1114 WREG8(MGA_MISC_OUT, misc);
1117 memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
1119 mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
1122 if (mdev->type == G200_ER) {
1123 u32 mem_ctl = RREG32(MGAREG_MEMCTL);
1127 WREG8(MGAREG_SEQ_INDEX, 0x01);
1128 seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
1129 WREG8(MGAREG_SEQ_DATA, seq1);
1131 WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
1133 WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
1135 WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
1139 if (IS_G200_SE(mdev)) {
1140 if (mdev->unique_rev_id >= 0x02) {
1145 if (crtc->primary->fb->bits_per_pixel > 16)
1147 else if (crtc->primary->fb->bits_per_pixel > 8)
1152 mb = (mode->clock * bpp) / 1000;
1166 WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1167 WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl);
1169 WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1170 if (mdev->unique_rev_id >= 0x01)
1171 WREG8(MGAREG_CRTCEXT_DATA, 0x03);
1173 WREG8(MGAREG_CRTCEXT_DATA, 0x04);
1179 #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1180 static int mga_suspend(struct drm_crtc *crtc)
1182 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1183 struct drm_device *dev = crtc->dev;
1184 struct mga_device *mdev = dev->dev_private;
1185 struct pci_dev *pdev = dev->pdev;
1188 if (mdev->suspended)
1193 /* Disable the pixel clock */
1194 WREG_DAC(0x1a, 0x05);
1195 /* Power down the DAC */
1196 WREG_DAC(0x1e, 0x18);
1197 /* Power down the pixel PLL */
1198 WREG_DAC(0x1a, 0x0d);
1200 /* Disable PLLs and clocks */
1201 pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1202 option &= ~(0x1F8024);
1203 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1204 pci_set_power_state(pdev, PCI_D3hot);
1205 pci_disable_device(pdev);
1207 mdev->suspended = true;
1212 static int mga_resume(struct drm_crtc *crtc)
1214 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1215 struct drm_device *dev = crtc->dev;
1216 struct mga_device *mdev = dev->dev_private;
1217 struct pci_dev *pdev = dev->pdev;
1220 if (!mdev->suspended)
1223 pci_set_power_state(pdev, PCI_D0);
1224 pci_enable_device(pdev);
1226 /* Disable sysclk */
1227 pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1229 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1231 mdev->suspended = false;
1238 static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1240 struct drm_device *dev = crtc->dev;
1241 struct mga_device *mdev = dev->dev_private;
1242 u8 seq1 = 0, crtcext1 = 0;
1245 case DRM_MODE_DPMS_ON:
1248 mga_crtc_load_lut(crtc);
1250 case DRM_MODE_DPMS_STANDBY:
1254 case DRM_MODE_DPMS_SUSPEND:
1258 case DRM_MODE_DPMS_OFF:
1265 if (mode == DRM_MODE_DPMS_OFF) {
1269 WREG8(MGAREG_SEQ_INDEX, 0x01);
1270 seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1271 mga_wait_vsync(mdev);
1272 mga_wait_busy(mdev);
1273 WREG8(MGAREG_SEQ_DATA, seq1);
1275 WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1276 crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1277 WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1280 if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1282 drm_helper_resume_force_mode(dev);
1288 * This is called before a mode is programmed. A typical use might be to
1289 * enable DPMS during the programming to avoid seeing intermediate stages,
1290 * but that's not relevant to us
1292 static void mga_crtc_prepare(struct drm_crtc *crtc)
1294 struct drm_device *dev = crtc->dev;
1295 struct mga_device *mdev = dev->dev_private;
1298 /* mga_resume(crtc);*/
1300 WREG8(MGAREG_CRTC_INDEX, 0x11);
1301 tmp = RREG8(MGAREG_CRTC_DATA);
1302 WREG_CRT(0x11, tmp | 0x80);
1304 if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1310 WREG8(MGAREG_SEQ_INDEX, 0x1);
1311 tmp = RREG8(MGAREG_SEQ_DATA);
1313 /* start sync reset */
1315 WREG_SEQ(1, tmp | 0x20);
1318 if (mdev->type == G200_WB || mdev->type == G200_EW3)
1319 mga_g200wb_prepare(crtc);
1325 * This is called after a mode is programmed. It should reverse anything done
1326 * by the prepare function
1328 static void mga_crtc_commit(struct drm_crtc *crtc)
1330 struct drm_device *dev = crtc->dev;
1331 struct mga_device *mdev = dev->dev_private;
1332 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1335 if (mdev->type == G200_WB || mdev->type == G200_EW3)
1336 mga_g200wb_commit(crtc);
1338 if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1344 WREG8(MGAREG_SEQ_INDEX, 0x1);
1345 tmp = RREG8(MGAREG_SEQ_DATA);
1351 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1355 * The core can pass us a set of gamma values to program. We actually only
1356 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1357 * but it's a requirement that we provide the function
1359 static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1360 u16 *blue, uint32_t start, uint32_t size)
1362 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1363 int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
1366 for (i = start; i < end; i++) {
1367 mga_crtc->lut_r[i] = red[i] >> 8;
1368 mga_crtc->lut_g[i] = green[i] >> 8;
1369 mga_crtc->lut_b[i] = blue[i] >> 8;
1371 mga_crtc_load_lut(crtc);
1374 /* Simple cleanup function */
1375 static void mga_crtc_destroy(struct drm_crtc *crtc)
1377 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1379 drm_crtc_cleanup(crtc);
1383 static void mga_crtc_disable(struct drm_crtc *crtc)
1386 DRM_DEBUG_KMS("\n");
1387 mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1388 if (crtc->primary->fb) {
1389 struct mga_framebuffer *mga_fb = to_mga_framebuffer(crtc->primary->fb);
1390 struct drm_gem_object *obj = mga_fb->obj;
1391 struct mgag200_bo *bo = gem_to_mga_bo(obj);
1392 ret = mgag200_bo_reserve(bo, false);
1395 mgag200_bo_push_sysram(bo);
1396 mgag200_bo_unreserve(bo);
1398 crtc->primary->fb = NULL;
1401 /* These provide the minimum set of functions required to handle a CRTC */
1402 static const struct drm_crtc_funcs mga_crtc_funcs = {
1403 .cursor_set = mga_crtc_cursor_set,
1404 .cursor_move = mga_crtc_cursor_move,
1405 .gamma_set = mga_crtc_gamma_set,
1406 .set_config = drm_crtc_helper_set_config,
1407 .destroy = mga_crtc_destroy,
1410 static const struct drm_crtc_helper_funcs mga_helper_funcs = {
1411 .disable = mga_crtc_disable,
1412 .dpms = mga_crtc_dpms,
1413 .mode_fixup = mga_crtc_mode_fixup,
1414 .mode_set = mga_crtc_mode_set,
1415 .mode_set_base = mga_crtc_mode_set_base,
1416 .prepare = mga_crtc_prepare,
1417 .commit = mga_crtc_commit,
1418 .load_lut = mga_crtc_load_lut,
1422 static void mga_crtc_init(struct mga_device *mdev)
1424 struct mga_crtc *mga_crtc;
1427 mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1428 (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1431 if (mga_crtc == NULL)
1434 drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
1436 drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1437 mdev->mode_info.crtc = mga_crtc;
1439 for (i = 0; i < MGAG200_LUT_SIZE; i++) {
1440 mga_crtc->lut_r[i] = i;
1441 mga_crtc->lut_g[i] = i;
1442 mga_crtc->lut_b[i] = i;
1445 drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1448 /** Sets the color ramps on behalf of fbcon */
1449 void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1450 u16 blue, int regno)
1452 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1454 mga_crtc->lut_r[regno] = red >> 8;
1455 mga_crtc->lut_g[regno] = green >> 8;
1456 mga_crtc->lut_b[regno] = blue >> 8;
1459 /** Gets the color ramps on behalf of fbcon */
1460 void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
1461 u16 *blue, int regno)
1463 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1465 *red = (u16)mga_crtc->lut_r[regno] << 8;
1466 *green = (u16)mga_crtc->lut_g[regno] << 8;
1467 *blue = (u16)mga_crtc->lut_b[regno] << 8;
1471 * The encoder comes after the CRTC in the output pipeline, but before
1472 * the connector. It's responsible for ensuring that the digital
1473 * stream is appropriately converted into the output format. Setup is
1474 * very simple in this case - all we have to do is inform qemu of the
1475 * colour depth in order to ensure that it displays appropriately
1479 * These functions are analagous to those in the CRTC code, but are intended
1480 * to handle any encoder-specific limitations
1482 static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
1483 const struct drm_display_mode *mode,
1484 struct drm_display_mode *adjusted_mode)
1489 static void mga_encoder_mode_set(struct drm_encoder *encoder,
1490 struct drm_display_mode *mode,
1491 struct drm_display_mode *adjusted_mode)
1496 static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
1501 static void mga_encoder_prepare(struct drm_encoder *encoder)
1505 static void mga_encoder_commit(struct drm_encoder *encoder)
1509 static void mga_encoder_destroy(struct drm_encoder *encoder)
1511 struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
1512 drm_encoder_cleanup(encoder);
1516 static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
1517 .dpms = mga_encoder_dpms,
1518 .mode_fixup = mga_encoder_mode_fixup,
1519 .mode_set = mga_encoder_mode_set,
1520 .prepare = mga_encoder_prepare,
1521 .commit = mga_encoder_commit,
1524 static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
1525 .destroy = mga_encoder_destroy,
1528 static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
1530 struct drm_encoder *encoder;
1531 struct mga_encoder *mga_encoder;
1533 mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
1537 encoder = &mga_encoder->base;
1538 encoder->possible_crtcs = 0x1;
1540 drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
1541 DRM_MODE_ENCODER_DAC, NULL);
1542 drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
1548 static int mga_vga_get_modes(struct drm_connector *connector)
1550 struct mga_connector *mga_connector = to_mga_connector(connector);
1554 edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1556 drm_mode_connector_update_edid_property(connector, edid);
1557 ret = drm_add_edid_modes(connector, edid);
1563 static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
1566 uint32_t total_area, divisor;
1567 int64_t active_area, pixels_per_second, bandwidth;
1568 uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
1572 if (!mode->htotal || !mode->vtotal || !mode->clock)
1575 active_area = mode->hdisplay * mode->vdisplay;
1576 total_area = mode->htotal * mode->vtotal;
1578 pixels_per_second = active_area * mode->clock * 1000;
1579 do_div(pixels_per_second, total_area);
1581 bandwidth = pixels_per_second * bytes_per_pixel * 100;
1582 do_div(bandwidth, divisor);
1584 return (uint32_t)(bandwidth);
1587 #define MODE_BANDWIDTH MODE_BAD
1589 static int mga_vga_mode_valid(struct drm_connector *connector,
1590 struct drm_display_mode *mode)
1592 struct drm_device *dev = connector->dev;
1593 struct mga_device *mdev = (struct mga_device*)dev->dev_private;
1596 if (IS_G200_SE(mdev)) {
1597 if (mdev->unique_rev_id == 0x01) {
1598 if (mode->hdisplay > 1600)
1599 return MODE_VIRTUAL_X;
1600 if (mode->vdisplay > 1200)
1601 return MODE_VIRTUAL_Y;
1602 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1604 return MODE_BANDWIDTH;
1605 } else if (mdev->unique_rev_id == 0x02) {
1606 if (mode->hdisplay > 1920)
1607 return MODE_VIRTUAL_X;
1608 if (mode->vdisplay > 1200)
1609 return MODE_VIRTUAL_Y;
1610 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1612 return MODE_BANDWIDTH;
1614 } else if (mdev->type == G200_WB) {
1615 if (mode->hdisplay > 1280)
1616 return MODE_VIRTUAL_X;
1617 if (mode->vdisplay > 1024)
1618 return MODE_VIRTUAL_Y;
1619 if (mga_vga_calculate_mode_bandwidth(mode,
1620 bpp > (31877 * 1024)))
1621 return MODE_BANDWIDTH;
1622 } else if (mdev->type == G200_EV &&
1623 (mga_vga_calculate_mode_bandwidth(mode, bpp)
1624 > (32700 * 1024))) {
1625 return MODE_BANDWIDTH;
1626 } else if (mdev->type == G200_EH &&
1627 (mga_vga_calculate_mode_bandwidth(mode, bpp)
1628 > (37500 * 1024))) {
1629 return MODE_BANDWIDTH;
1630 } else if (mdev->type == G200_ER &&
1631 (mga_vga_calculate_mode_bandwidth(mode,
1632 bpp) > (55000 * 1024))) {
1633 return MODE_BANDWIDTH;
1636 if ((mode->hdisplay % 8) != 0 || (mode->hsync_start % 8) != 0 ||
1637 (mode->hsync_end % 8) != 0 || (mode->htotal % 8) != 0) {
1638 return MODE_H_ILLEGAL;
1641 if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1642 mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1643 mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1644 mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1648 /* Validate the mode input by the user */
1649 if (connector->cmdline_mode.specified) {
1650 if (connector->cmdline_mode.bpp_specified)
1651 bpp = connector->cmdline_mode.bpp;
1654 if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
1655 if (connector->cmdline_mode.specified)
1656 connector->cmdline_mode.specified = false;
1663 static struct drm_encoder *mga_connector_best_encoder(struct drm_connector
1666 int enc_id = connector->encoder_ids[0];
1667 /* pick the encoder ids */
1669 return drm_encoder_find(connector->dev, enc_id);
1673 static enum drm_connector_status mga_vga_detect(struct drm_connector
1674 *connector, bool force)
1676 return connector_status_connected;
1679 static void mga_connector_destroy(struct drm_connector *connector)
1681 struct mga_connector *mga_connector = to_mga_connector(connector);
1682 mgag200_i2c_destroy(mga_connector->i2c);
1683 drm_connector_cleanup(connector);
1687 static const struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
1688 .get_modes = mga_vga_get_modes,
1689 .mode_valid = mga_vga_mode_valid,
1690 .best_encoder = mga_connector_best_encoder,
1693 static const struct drm_connector_funcs mga_vga_connector_funcs = {
1694 .dpms = drm_helper_connector_dpms,
1695 .detect = mga_vga_detect,
1696 .fill_modes = drm_helper_probe_single_connector_modes,
1697 .destroy = mga_connector_destroy,
1700 static struct drm_connector *mga_vga_init(struct drm_device *dev)
1702 struct drm_connector *connector;
1703 struct mga_connector *mga_connector;
1705 mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1709 connector = &mga_connector->base;
1711 drm_connector_init(dev, connector,
1712 &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1714 drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1716 drm_connector_register(connector);
1718 mga_connector->i2c = mgag200_i2c_create(dev);
1719 if (!mga_connector->i2c)
1720 DRM_ERROR("failed to add ddc bus\n");
1726 int mgag200_modeset_init(struct mga_device *mdev)
1728 struct drm_encoder *encoder;
1729 struct drm_connector *connector;
1732 mdev->mode_info.mode_config_initialized = true;
1734 mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1735 mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1737 mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1739 mga_crtc_init(mdev);
1741 encoder = mga_encoder_init(mdev->dev);
1743 DRM_ERROR("mga_encoder_init failed\n");
1747 connector = mga_vga_init(mdev->dev);
1749 DRM_ERROR("mga_vga_init failed\n");
1753 drm_mode_connector_attach_encoder(connector, encoder);
1755 ret = mgag200_fbdev_init(mdev);
1757 DRM_ERROR("mga_fbdev_init failed\n");
1764 void mgag200_modeset_fini(struct mga_device *mdev)