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 drm_device
*dev
= crtc
->dev
;
31 struct mga_device
*mdev
= dev
->dev_private
;
32 struct drm_framebuffer
*fb
= crtc
->primary
->fb
;
33 u16
*r_ptr
, *g_ptr
, *b_ptr
;
39 r_ptr
= crtc
->gamma_store
;
40 g_ptr
= r_ptr
+ crtc
->gamma_size
;
41 b_ptr
= g_ptr
+ crtc
->gamma_size
;
43 WREG8(DAC_INDEX
+ MGA1064_INDEX
, 0);
45 if (fb
&& fb
->format
->cpp
[0] * 8 == 16) {
46 int inc
= (fb
->format
->depth
== 15) ? 8 : 4;
48 for (i
= 0; i
< MGAG200_LUT_SIZE
; i
+= inc
) {
49 if (fb
->format
->depth
== 16) {
50 if (i
> (MGAG200_LUT_SIZE
>> 1)) {
63 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, r
);
64 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, *g_ptr
++ >> 8);
65 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, b
);
69 for (i
= 0; i
< MGAG200_LUT_SIZE
; i
++) {
71 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, *r_ptr
++ >> 8);
72 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, *g_ptr
++ >> 8);
73 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, *b_ptr
++ >> 8);
77 static inline void mga_wait_vsync(struct mga_device
*mdev
)
79 unsigned long timeout
= jiffies
+ HZ
/10;
80 unsigned int status
= 0;
83 status
= RREG32(MGAREG_Status
);
84 } while ((status
& 0x08) && time_before(jiffies
, timeout
));
85 timeout
= jiffies
+ HZ
/10;
88 status
= RREG32(MGAREG_Status
);
89 } while (!(status
& 0x08) && time_before(jiffies
, timeout
));
92 static inline void mga_wait_busy(struct mga_device
*mdev
)
94 unsigned long timeout
= jiffies
+ HZ
;
95 unsigned int status
= 0;
97 status
= RREG8(MGAREG_Status
+ 2);
98 } while ((status
& 0x01) && time_before(jiffies
, timeout
));
101 #define P_ARRAY_SIZE 9
103 static int mga_g200se_set_plls(struct mga_device
*mdev
, long clock
)
105 unsigned int vcomax
, vcomin
, pllreffreq
;
106 unsigned int delta
, tmpdelta
, permitteddelta
;
107 unsigned int testp
, testm
, testn
;
108 unsigned int p
, m
, n
;
109 unsigned int computed
;
110 unsigned int pvalues_e4
[P_ARRAY_SIZE
] = {16, 14, 12, 10, 8, 6, 4, 2, 1};
114 if (mdev
->unique_rev_id
<= 0x03) {
122 permitteddelta
= clock
* 5 / 1000;
124 for (testp
= 8; testp
> 0; testp
/= 2) {
125 if (clock
* testp
> vcomax
)
127 if (clock
* testp
< vcomin
)
130 for (testn
= 17; testn
< 256; testn
++) {
131 for (testm
= 1; testm
< 32; testm
++) {
132 computed
= (pllreffreq
* testn
) /
134 if (computed
> clock
)
135 tmpdelta
= computed
- clock
;
137 tmpdelta
= clock
- computed
;
138 if (tmpdelta
< delta
) {
161 /* Permited delta is 0.5% as VESA Specification */
162 permitteddelta
= clock
* 5 / 1000;
164 for (i
= 0 ; i
< P_ARRAY_SIZE
; i
++) {
165 testp
= pvalues_e4
[i
];
167 if ((clock
* testp
) > vcomax
)
169 if ((clock
* testp
) < vcomin
)
172 for (testn
= 50; testn
<= 256; testn
++) {
173 for (testm
= 1; testm
<= 32; testm
++) {
174 computed
= (pllreffreq
* testn
) /
176 if (computed
> clock
)
177 tmpdelta
= computed
- clock
;
179 tmpdelta
= clock
- computed
;
181 if (tmpdelta
< delta
) {
191 fvv
= pllreffreq
* (n
+ 1) / (m
+ 1);
192 fvv
= (fvv
- 800000) / 50000;
203 if (delta
> permitteddelta
) {
204 pr_warn("PLL delta too large\n");
208 WREG_DAC(MGA1064_PIX_PLLC_M
, m
);
209 WREG_DAC(MGA1064_PIX_PLLC_N
, n
);
210 WREG_DAC(MGA1064_PIX_PLLC_P
, p
);
212 if (mdev
->unique_rev_id
>= 0x04) {
213 WREG_DAC(0x1a, 0x09);
215 WREG_DAC(0x1a, 0x01);
222 static int mga_g200wb_set_plls(struct mga_device
*mdev
, long clock
)
224 unsigned int vcomax
, vcomin
, pllreffreq
;
225 unsigned int delta
, tmpdelta
;
226 unsigned int testp
, testm
, testn
, testp2
;
227 unsigned int p
, m
, n
;
228 unsigned int computed
;
229 int i
, j
, tmpcount
, vcount
;
230 bool pll_locked
= false;
237 if (mdev
->type
== G200_EW3
) {
243 for (testp
= 1; testp
< 8; testp
++) {
244 for (testp2
= 1; testp2
< 8; testp2
++) {
247 if ((clock
* testp
* testp2
) > vcomax
)
249 if ((clock
* testp
* testp2
) < vcomin
)
251 for (testm
= 1; testm
< 26; testm
++) {
252 for (testn
= 32; testn
< 2048 ; testn
++) {
253 computed
= (pllreffreq
* testn
) /
254 (testm
* testp
* testp2
);
255 if (computed
> clock
)
256 tmpdelta
= computed
- clock
;
258 tmpdelta
= clock
- computed
;
259 if (tmpdelta
< delta
) {
261 m
= ((testn
& 0x100) >> 1) |
264 p
= ((testn
& 0x600) >> 3) |
278 for (testp
= 1; testp
< 9; testp
++) {
279 if (clock
* testp
> vcomax
)
281 if (clock
* testp
< vcomin
)
284 for (testm
= 1; testm
< 17; testm
++) {
285 for (testn
= 1; testn
< 151; testn
++) {
286 computed
= (pllreffreq
* testn
) /
288 if (computed
> clock
)
289 tmpdelta
= computed
- clock
;
291 tmpdelta
= clock
- computed
;
292 if (tmpdelta
< delta
) {
304 for (i
= 0; i
<= 32 && pll_locked
== false; i
++) {
306 WREG8(MGAREG_CRTC_INDEX
, 0x1e);
307 tmp
= RREG8(MGAREG_CRTC_DATA
);
309 WREG8(MGAREG_CRTC_DATA
, tmp
+1);
312 /* set pixclkdis to 1 */
313 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
314 tmp
= RREG8(DAC_DATA
);
315 tmp
|= MGA1064_PIX_CLK_CTL_CLK_DIS
;
316 WREG8(DAC_DATA
, tmp
);
318 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL
);
319 tmp
= RREG8(DAC_DATA
);
320 tmp
|= MGA1064_REMHEADCTL_CLKDIS
;
321 WREG8(DAC_DATA
, tmp
);
323 /* select PLL Set C */
324 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
326 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
328 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
329 tmp
= RREG8(DAC_DATA
);
330 tmp
|= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
| 0x80;
331 WREG8(DAC_DATA
, tmp
);
336 WREG8(DAC_INDEX
, MGA1064_VREF_CTL
);
337 tmp
= RREG8(DAC_DATA
);
339 WREG8(DAC_DATA
, tmp
);
343 /* program pixel pll register */
344 WREG_DAC(MGA1064_WB_PIX_PLLC_N
, n
);
345 WREG_DAC(MGA1064_WB_PIX_PLLC_M
, m
);
346 WREG_DAC(MGA1064_WB_PIX_PLLC_P
, p
);
351 WREG8(DAC_INDEX
, MGA1064_VREF_CTL
);
352 tmp
= RREG8(DAC_DATA
);
354 WREG_DAC(MGA1064_VREF_CTL
, tmp
);
358 /* select the pixel pll */
359 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
360 tmp
= RREG8(DAC_DATA
);
361 tmp
&= ~MGA1064_PIX_CLK_CTL_SEL_MSK
;
362 tmp
|= MGA1064_PIX_CLK_CTL_SEL_PLL
;
363 WREG8(DAC_DATA
, tmp
);
365 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL
);
366 tmp
= RREG8(DAC_DATA
);
367 tmp
&= ~MGA1064_REMHEADCTL_CLKSL_MSK
;
368 tmp
|= MGA1064_REMHEADCTL_CLKSL_PLL
;
369 WREG8(DAC_DATA
, tmp
);
371 /* reset dotclock rate bit */
372 WREG8(MGAREG_SEQ_INDEX
, 1);
373 tmp
= RREG8(MGAREG_SEQ_DATA
);
375 WREG8(MGAREG_SEQ_DATA
, tmp
);
377 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
378 tmp
= RREG8(DAC_DATA
);
379 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_DIS
;
380 WREG8(DAC_DATA
, tmp
);
382 vcount
= RREG8(MGAREG_VCOUNT
);
384 for (j
= 0; j
< 30 && pll_locked
== false; j
++) {
385 tmpcount
= RREG8(MGAREG_VCOUNT
);
386 if (tmpcount
< vcount
)
388 if ((tmpcount
- vcount
) > 2)
394 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL
);
395 tmp
= RREG8(DAC_DATA
);
396 tmp
&= ~MGA1064_REMHEADCTL_CLKDIS
;
397 WREG_DAC(MGA1064_REMHEADCTL
, tmp
);
401 static int mga_g200ev_set_plls(struct mga_device
*mdev
, long clock
)
403 unsigned int vcomax
, vcomin
, pllreffreq
;
404 unsigned int delta
, tmpdelta
;
405 unsigned int testp
, testm
, testn
;
406 unsigned int p
, m
, n
;
407 unsigned int computed
;
417 for (testp
= 16; testp
> 0; testp
--) {
418 if (clock
* testp
> vcomax
)
420 if (clock
* testp
< vcomin
)
423 for (testn
= 1; testn
< 257; testn
++) {
424 for (testm
= 1; testm
< 17; testm
++) {
425 computed
= (pllreffreq
* testn
) /
427 if (computed
> clock
)
428 tmpdelta
= computed
- clock
;
430 tmpdelta
= clock
- computed
;
431 if (tmpdelta
< delta
) {
441 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
442 tmp
= RREG8(DAC_DATA
);
443 tmp
|= MGA1064_PIX_CLK_CTL_CLK_DIS
;
444 WREG8(DAC_DATA
, tmp
);
446 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
448 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
450 WREG8(DAC_INDEX
, MGA1064_PIX_PLL_STAT
);
451 tmp
= RREG8(DAC_DATA
);
452 WREG8(DAC_DATA
, tmp
& ~0x40);
454 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
455 tmp
= RREG8(DAC_DATA
);
456 tmp
|= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
457 WREG8(DAC_DATA
, tmp
);
459 WREG_DAC(MGA1064_EV_PIX_PLLC_M
, m
);
460 WREG_DAC(MGA1064_EV_PIX_PLLC_N
, n
);
461 WREG_DAC(MGA1064_EV_PIX_PLLC_P
, p
);
465 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
466 tmp
= RREG8(DAC_DATA
);
467 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
468 WREG8(DAC_DATA
, tmp
);
472 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
473 tmp
= RREG8(DAC_DATA
);
474 tmp
&= ~MGA1064_PIX_CLK_CTL_SEL_MSK
;
475 tmp
|= MGA1064_PIX_CLK_CTL_SEL_PLL
;
476 WREG8(DAC_DATA
, tmp
);
478 WREG8(DAC_INDEX
, MGA1064_PIX_PLL_STAT
);
479 tmp
= RREG8(DAC_DATA
);
480 WREG8(DAC_DATA
, tmp
| 0x40);
482 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
484 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
486 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
487 tmp
= RREG8(DAC_DATA
);
488 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_DIS
;
489 WREG8(DAC_DATA
, tmp
);
494 static int mga_g200eh_set_plls(struct mga_device
*mdev
, long clock
)
496 unsigned int vcomax
, vcomin
, pllreffreq
;
497 unsigned int delta
, tmpdelta
;
498 unsigned int testp
, testm
, testn
;
499 unsigned int p
, m
, n
;
500 unsigned int computed
;
501 int i
, j
, tmpcount
, vcount
;
503 bool pll_locked
= false;
507 if (mdev
->type
== G200_EH3
) {
516 for (testm
= 150; testm
>= 6; testm
--) {
517 if (clock
* testm
> vcomax
)
519 if (clock
* testm
< vcomin
)
521 for (testn
= 120; testn
>= 60; testn
--) {
522 computed
= (pllreffreq
* testn
) / testm
;
523 if (computed
> clock
)
524 tmpdelta
= computed
- clock
;
526 tmpdelta
= clock
- computed
;
527 if (tmpdelta
< delta
) {
547 for (testp
= 16; testp
> 0; testp
>>= 1) {
548 if (clock
* testp
> vcomax
)
550 if (clock
* testp
< vcomin
)
553 for (testm
= 1; testm
< 33; testm
++) {
554 for (testn
= 17; testn
< 257; testn
++) {
555 computed
= (pllreffreq
* testn
) /
557 if (computed
> clock
)
558 tmpdelta
= computed
- clock
;
560 tmpdelta
= clock
- computed
;
561 if (tmpdelta
< delta
) {
567 if ((clock
* testp
) >= 600000)
573 for (i
= 0; i
<= 32 && pll_locked
== false; i
++) {
574 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
575 tmp
= RREG8(DAC_DATA
);
576 tmp
|= MGA1064_PIX_CLK_CTL_CLK_DIS
;
577 WREG8(DAC_DATA
, tmp
);
579 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
581 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
583 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
584 tmp
= RREG8(DAC_DATA
);
585 tmp
|= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
586 WREG8(DAC_DATA
, tmp
);
590 WREG_DAC(MGA1064_EH_PIX_PLLC_M
, m
);
591 WREG_DAC(MGA1064_EH_PIX_PLLC_N
, n
);
592 WREG_DAC(MGA1064_EH_PIX_PLLC_P
, p
);
596 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
597 tmp
= RREG8(DAC_DATA
);
598 tmp
&= ~MGA1064_PIX_CLK_CTL_SEL_MSK
;
599 tmp
|= MGA1064_PIX_CLK_CTL_SEL_PLL
;
600 WREG8(DAC_DATA
, tmp
);
602 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
603 tmp
= RREG8(DAC_DATA
);
604 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_DIS
;
605 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
606 WREG8(DAC_DATA
, tmp
);
608 vcount
= RREG8(MGAREG_VCOUNT
);
610 for (j
= 0; j
< 30 && pll_locked
== false; j
++) {
611 tmpcount
= RREG8(MGAREG_VCOUNT
);
612 if (tmpcount
< vcount
)
614 if ((tmpcount
- vcount
) > 2)
624 static int mga_g200er_set_plls(struct mga_device
*mdev
, long clock
)
626 unsigned int vcomax
, vcomin
, pllreffreq
;
627 unsigned int delta
, tmpdelta
;
628 int testr
, testn
, testm
, testo
;
629 unsigned int p
, m
, n
;
630 unsigned int computed
, vco
;
632 const unsigned int m_div_val
[] = { 1, 2, 4, 8 };
641 for (testr
= 0; testr
< 4; testr
++) {
644 for (testn
= 5; testn
< 129; testn
++) {
647 for (testm
= 3; testm
>= 0; testm
--) {
650 for (testo
= 5; testo
< 33; testo
++) {
651 vco
= pllreffreq
* (testn
+ 1) /
657 computed
= vco
/ (m_div_val
[testm
] * (testo
+ 1));
658 if (computed
> clock
)
659 tmpdelta
= computed
- clock
;
661 tmpdelta
= clock
- computed
;
662 if (tmpdelta
< delta
) {
664 m
= testm
| (testo
<< 3);
666 p
= testr
| (testr
<< 3);
673 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
674 tmp
= RREG8(DAC_DATA
);
675 tmp
|= MGA1064_PIX_CLK_CTL_CLK_DIS
;
676 WREG8(DAC_DATA
, tmp
);
678 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL
);
679 tmp
= RREG8(DAC_DATA
);
680 tmp
|= MGA1064_REMHEADCTL_CLKDIS
;
681 WREG8(DAC_DATA
, tmp
);
683 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
684 tmp
|= (0x3<<2) | 0xc0;
685 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
687 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
688 tmp
= RREG8(DAC_DATA
);
689 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_DIS
;
690 tmp
|= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
691 WREG8(DAC_DATA
, tmp
);
695 WREG_DAC(MGA1064_ER_PIX_PLLC_N
, n
);
696 WREG_DAC(MGA1064_ER_PIX_PLLC_M
, m
);
697 WREG_DAC(MGA1064_ER_PIX_PLLC_P
, p
);
704 static int mga_crtc_set_plls(struct mga_device
*mdev
, long clock
)
709 return mga_g200se_set_plls(mdev
, clock
);
713 return mga_g200wb_set_plls(mdev
, clock
);
716 return mga_g200ev_set_plls(mdev
, clock
);
720 return mga_g200eh_set_plls(mdev
, clock
);
723 return mga_g200er_set_plls(mdev
, clock
);
729 static void mga_g200wb_prepare(struct drm_crtc
*crtc
)
731 struct mga_device
*mdev
= crtc
->dev
->dev_private
;
735 /* 1- The first step is to warn the BMC of an upcoming mode change.
736 * We are putting the misc<0> to output.*/
738 WREG8(DAC_INDEX
, MGA1064_GEN_IO_CTL
);
739 tmp
= RREG8(DAC_DATA
);
741 WREG_DAC(MGA1064_GEN_IO_CTL
, tmp
);
743 /* we are putting a 1 on the misc<0> line */
744 WREG8(DAC_INDEX
, MGA1064_GEN_IO_DATA
);
745 tmp
= RREG8(DAC_DATA
);
747 WREG_DAC(MGA1064_GEN_IO_DATA
, tmp
);
749 /* 2- Second step to mask and further scan request
750 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
752 WREG8(DAC_INDEX
, MGA1064_SPAREREG
);
753 tmp
= RREG8(DAC_DATA
);
755 WREG_DAC(MGA1064_SPAREREG
, tmp
);
757 /* 3a- the third step is to verifu if there is an active scan
758 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
761 while (!(tmp
& 0x1) && iter_max
) {
762 WREG8(DAC_INDEX
, MGA1064_SPAREREG
);
763 tmp
= RREG8(DAC_DATA
);
768 /* 3b- this step occurs only if the remove is actually scanning
769 * we are waiting for the end of the frame which is a 1 on
770 * remvsyncsts (XSPAREREG<1>)
774 while ((tmp
& 0x2) && iter_max
) {
775 WREG8(DAC_INDEX
, MGA1064_SPAREREG
);
776 tmp
= RREG8(DAC_DATA
);
783 static void mga_g200wb_commit(struct drm_crtc
*crtc
)
786 struct mga_device
*mdev
= crtc
->dev
->dev_private
;
788 /* 1- The first step is to ensure that the vrsten and hrsten are set */
789 WREG8(MGAREG_CRTCEXT_INDEX
, 1);
790 tmp
= RREG8(MGAREG_CRTCEXT_DATA
);
791 WREG8(MGAREG_CRTCEXT_DATA
, tmp
| 0x88);
793 /* 2- second step is to assert the rstlvl2 */
794 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL2
);
795 tmp
= RREG8(DAC_DATA
);
797 WREG8(DAC_DATA
, tmp
);
802 /* 3- deassert rstlvl2 */
804 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL2
);
805 WREG8(DAC_DATA
, tmp
);
807 /* 4- remove mask of scan request */
808 WREG8(DAC_INDEX
, MGA1064_SPAREREG
);
809 tmp
= RREG8(DAC_DATA
);
811 WREG8(DAC_DATA
, tmp
);
813 /* 5- put back a 0 on the misc<0> line */
814 WREG8(DAC_INDEX
, MGA1064_GEN_IO_DATA
);
815 tmp
= RREG8(DAC_DATA
);
817 WREG_DAC(MGA1064_GEN_IO_DATA
, tmp
);
821 This is how the framebuffer base address is stored in g200 cards:
822 * Assume @offset is the gpu_addr variable of the framebuffer object
823 * Then addr is the number of _pixels_ (not bytes) from the start of
824 VRAM to the first pixel we want to display. (divided by 2 for 32bit
826 * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
827 addr<20> -> CRTCEXT0<6>
828 addr<19-16> -> CRTCEXT0<3-0>
829 addr<15-8> -> CRTCC<7-0>
830 addr<7-0> -> CRTCD<7-0>
831 CRTCEXT0 has to be programmed last to trigger an update and make the
832 new addr variable take effect.
834 static void mga_set_start_address(struct drm_crtc
*crtc
, unsigned offset
)
836 struct mga_device
*mdev
= crtc
->dev
->dev_private
;
841 while (RREG8(0x1fda) & 0x08);
842 while (!(RREG8(0x1fda) & 0x08));
844 count
= RREG8(MGAREG_VCOUNT
) + 2;
845 while (RREG8(MGAREG_VCOUNT
) < count
);
847 WREG8(MGAREG_CRTCEXT_INDEX
, 0);
848 crtcext0
= RREG8(MGAREG_CRTCEXT_DATA
);
851 /* Can't store addresses any higher than that...
852 but we also don't have more than 16MB of memory, so it should be fine. */
853 WARN_ON(addr
> 0x1fffff);
854 crtcext0
|= (!!(addr
& (1<<20)))<<6;
855 WREG_CRT(0x0d, (u8
)(addr
& 0xff));
856 WREG_CRT(0x0c, (u8
)(addr
>> 8) & 0xff);
857 WREG_ECRT(0x0, ((u8
)(addr
>> 16) & 0xf) | crtcext0
);
861 /* ast is different - we will force move buffers out of VRAM */
862 static int mga_crtc_do_set_base(struct drm_crtc
*crtc
,
863 struct drm_framebuffer
*fb
,
864 int x
, int y
, int atomic
)
866 struct mga_device
*mdev
= crtc
->dev
->dev_private
;
867 struct drm_gem_object
*obj
;
868 struct mga_framebuffer
*mga_fb
;
869 struct mgag200_bo
*bo
;
873 /* push the previous fb to system ram */
875 mga_fb
= to_mga_framebuffer(fb
);
877 bo
= gem_to_mga_bo(obj
);
878 ret
= mgag200_bo_reserve(bo
, false);
881 mgag200_bo_push_sysram(bo
);
882 mgag200_bo_unreserve(bo
);
885 mga_fb
= to_mga_framebuffer(crtc
->primary
->fb
);
887 bo
= gem_to_mga_bo(obj
);
889 ret
= mgag200_bo_reserve(bo
, false);
893 ret
= mgag200_bo_pin(bo
, TTM_PL_FLAG_VRAM
, &gpu_addr
);
895 mgag200_bo_unreserve(bo
);
899 if (&mdev
->mfbdev
->mfb
== mga_fb
) {
900 /* if pushing console in kmap it */
901 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
903 DRM_ERROR("failed to kmap fbcon\n");
906 mgag200_bo_unreserve(bo
);
908 mga_set_start_address(crtc
, (u32
)gpu_addr
);
913 static int mga_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
914 struct drm_framebuffer
*old_fb
)
916 return mga_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
919 static int mga_crtc_mode_set(struct drm_crtc
*crtc
,
920 struct drm_display_mode
*mode
,
921 struct drm_display_mode
*adjusted_mode
,
922 int x
, int y
, struct drm_framebuffer
*old_fb
)
924 struct drm_device
*dev
= crtc
->dev
;
925 struct mga_device
*mdev
= dev
->dev_private
;
926 const struct drm_framebuffer
*fb
= crtc
->primary
->fb
;
927 int hdisplay
, hsyncstart
, hsyncend
, htotal
;
928 int vdisplay
, vsyncstart
, vsyncend
, vtotal
;
930 int option
= 0, option2
= 0;
932 unsigned char misc
= 0;
933 unsigned char ext_vga
[6];
936 static unsigned char dacvalue
[] = {
937 /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
938 /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
939 /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
940 /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
941 /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
942 /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
943 /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
944 /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
945 /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
946 /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
949 bppshift
= mdev
->bpp_shifts
[fb
->format
->cpp
[0] - 1];
951 switch (mdev
->type
) {
954 dacvalue
[MGA1064_VREF_CTL
] = 0x03;
955 dacvalue
[MGA1064_PIX_CLK_CTL
] = MGA1064_PIX_CLK_CTL_SEL_PLL
;
956 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_DAC_EN
|
957 MGA1064_MISC_CTL_VGA8
|
958 MGA1064_MISC_CTL_DAC_RAM_CS
;
963 option2
= 0x00008000;
967 dacvalue
[MGA1064_VREF_CTL
] = 0x07;
969 option2
= 0x0000b000;
972 dacvalue
[MGA1064_PIX_CLK_CTL
] = MGA1064_PIX_CLK_CTL_SEL_PLL
;
973 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_VGA8
|
974 MGA1064_MISC_CTL_DAC_RAM_CS
;
976 option2
= 0x0000b000;
980 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_VGA8
|
981 MGA1064_MISC_CTL_DAC_RAM_CS
;
983 option2
= 0x0000b000;
989 switch (fb
->format
->cpp
[0] * 8) {
991 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_8bits
;
994 if (fb
->format
->depth
== 15)
995 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_15bits
;
997 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_16bits
;
1000 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_24bits
;
1003 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_32_24bits
;
1007 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
1009 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
1013 for (i
= 0; i
< sizeof(dacvalue
); i
++) {
1017 ((i
>= 0x1f) && (i
<= 0x29)) ||
1018 ((i
>= 0x30) && (i
<= 0x37)))
1020 if (IS_G200_SE(mdev
) &&
1021 ((i
== 0x2c) || (i
== 0x2d) || (i
== 0x2e)))
1023 if ((mdev
->type
== G200_EV
||
1024 mdev
->type
== G200_WB
||
1025 mdev
->type
== G200_EH
||
1026 mdev
->type
== G200_EW3
||
1027 mdev
->type
== G200_EH3
) &&
1028 (i
>= 0x44) && (i
<= 0x4e))
1031 WREG_DAC(i
, dacvalue
[i
]);
1034 if (mdev
->type
== G200_ER
)
1038 pci_write_config_dword(dev
->pdev
, PCI_MGA_OPTION
, option
);
1040 pci_write_config_dword(dev
->pdev
, PCI_MGA_OPTION2
, option2
);
1046 pitch
= fb
->pitches
[0] / fb
->format
->cpp
[0];
1047 if (fb
->format
->cpp
[0] * 8 == 24)
1048 pitch
= (pitch
* 3) >> (4 - bppshift
);
1050 pitch
= pitch
>> (4 - bppshift
);
1052 hdisplay
= mode
->hdisplay
/ 8 - 1;
1053 hsyncstart
= mode
->hsync_start
/ 8 - 1;
1054 hsyncend
= mode
->hsync_end
/ 8 - 1;
1055 htotal
= mode
->htotal
/ 8 - 1;
1057 /* Work around hardware quirk */
1058 if ((htotal
& 0x07) == 0x06 || (htotal
& 0x07) == 0x04)
1061 vdisplay
= mode
->vdisplay
- 1;
1062 vsyncstart
= mode
->vsync_start
- 1;
1063 vsyncend
= mode
->vsync_end
- 1;
1064 vtotal
= mode
->vtotal
- 2;
1076 WREG_CRT(0, htotal
- 4);
1077 WREG_CRT(1, hdisplay
);
1078 WREG_CRT(2, hdisplay
);
1079 WREG_CRT(3, (htotal
& 0x1F) | 0x80);
1080 WREG_CRT(4, hsyncstart
);
1081 WREG_CRT(5, ((htotal
& 0x20) << 2) | (hsyncend
& 0x1F));
1082 WREG_CRT(6, vtotal
& 0xFF);
1083 WREG_CRT(7, ((vtotal
& 0x100) >> 8) |
1084 ((vdisplay
& 0x100) >> 7) |
1085 ((vsyncstart
& 0x100) >> 6) |
1086 ((vdisplay
& 0x100) >> 5) |
1087 ((vdisplay
& 0x100) >> 4) | /* linecomp */
1088 ((vtotal
& 0x200) >> 4)|
1089 ((vdisplay
& 0x200) >> 3) |
1090 ((vsyncstart
& 0x200) >> 2));
1091 WREG_CRT(9, ((vdisplay
& 0x200) >> 4) |
1092 ((vdisplay
& 0x200) >> 3));
1099 WREG_CRT(16, vsyncstart
& 0xFF);
1100 WREG_CRT(17, (vsyncend
& 0x0F) | 0x20);
1101 WREG_CRT(18, vdisplay
& 0xFF);
1102 WREG_CRT(19, pitch
& 0xFF);
1104 WREG_CRT(21, vdisplay
& 0xFF);
1105 WREG_CRT(22, (vtotal
+ 1) & 0xFF);
1107 WREG_CRT(24, vdisplay
& 0xFF);
1112 /* TODO interlace */
1114 ext_vga
[0] |= (pitch
& 0x300) >> 4;
1115 ext_vga
[1] = (((htotal
- 4) & 0x100) >> 8) |
1116 ((hdisplay
& 0x100) >> 7) |
1117 ((hsyncstart
& 0x100) >> 6) |
1119 ext_vga
[2] = ((vtotal
& 0xc00) >> 10) |
1120 ((vdisplay
& 0x400) >> 8) |
1121 ((vdisplay
& 0xc00) >> 7) |
1122 ((vsyncstart
& 0xc00) >> 5) |
1123 ((vdisplay
& 0x400) >> 3);
1124 if (fb
->format
->cpp
[0] * 8 == 24)
1125 ext_vga
[3] = (((1 << bppshift
) * 3) - 1) | 0x80;
1127 ext_vga
[3] = ((1 << bppshift
) - 1) | 0x80;
1129 if (mdev
->type
== G200_WB
|| mdev
->type
== G200_EW3
)
1132 /* Set pixel clocks */
1134 WREG8(MGA_MISC_OUT
, misc
);
1136 mga_crtc_set_plls(mdev
, mode
->clock
);
1138 for (i
= 0; i
< 6; i
++) {
1139 WREG_ECRT(i
, ext_vga
[i
]);
1142 if (mdev
->type
== G200_ER
)
1143 WREG_ECRT(0x24, 0x5);
1145 if (mdev
->type
== G200_EW3
)
1146 WREG_ECRT(0x34, 0x5);
1148 if (mdev
->type
== G200_EV
) {
1152 WREG_ECRT(0, ext_vga
[0]);
1153 /* Enable mga pixel clock */
1156 WREG8(MGA_MISC_OUT
, misc
);
1159 memcpy(&mdev
->mode
, mode
, sizeof(struct drm_display_mode
));
1161 mga_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
1164 if (mdev
->type
== G200_ER
) {
1165 u32 mem_ctl
= RREG32(MGAREG_MEMCTL
);
1169 WREG8(MGAREG_SEQ_INDEX
, 0x01);
1170 seq1
= RREG8(MGAREG_SEQ_DATA
) | 0x20;
1171 WREG8(MGAREG_SEQ_DATA
, seq1
);
1173 WREG32(MGAREG_MEMCTL
, mem_ctl
| 0x00200000);
1175 WREG32(MGAREG_MEMCTL
, mem_ctl
& ~0x00200000);
1177 WREG8(MGAREG_SEQ_DATA
, seq1
& ~0x20);
1181 if (IS_G200_SE(mdev
)) {
1182 if (mdev
->unique_rev_id
>= 0x04) {
1183 WREG8(MGAREG_CRTCEXT_INDEX
, 0x06);
1184 WREG8(MGAREG_CRTCEXT_DATA
, 0);
1185 } else if (mdev
->unique_rev_id
>= 0x02) {
1190 if (fb
->format
->cpp
[0] * 8 > 16)
1192 else if (fb
->format
->cpp
[0] * 8 > 8)
1197 mb
= (mode
->clock
* bpp
) / 1000;
1211 WREG8(MGAREG_CRTCEXT_INDEX
, 0x06);
1212 WREG8(MGAREG_CRTCEXT_DATA
, hi_pri_lvl
);
1214 WREG8(MGAREG_CRTCEXT_INDEX
, 0x06);
1215 if (mdev
->unique_rev_id
>= 0x01)
1216 WREG8(MGAREG_CRTCEXT_DATA
, 0x03);
1218 WREG8(MGAREG_CRTCEXT_DATA
, 0x04);
1224 #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1225 static int mga_suspend(struct drm_crtc
*crtc
)
1227 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1228 struct drm_device
*dev
= crtc
->dev
;
1229 struct mga_device
*mdev
= dev
->dev_private
;
1230 struct pci_dev
*pdev
= dev
->pdev
;
1233 if (mdev
->suspended
)
1238 /* Disable the pixel clock */
1239 WREG_DAC(0x1a, 0x05);
1240 /* Power down the DAC */
1241 WREG_DAC(0x1e, 0x18);
1242 /* Power down the pixel PLL */
1243 WREG_DAC(0x1a, 0x0d);
1245 /* Disable PLLs and clocks */
1246 pci_read_config_dword(pdev
, PCI_MGA_OPTION
, &option
);
1247 option
&= ~(0x1F8024);
1248 pci_write_config_dword(pdev
, PCI_MGA_OPTION
, option
);
1249 pci_set_power_state(pdev
, PCI_D3hot
);
1250 pci_disable_device(pdev
);
1252 mdev
->suspended
= true;
1257 static int mga_resume(struct drm_crtc
*crtc
)
1259 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1260 struct drm_device
*dev
= crtc
->dev
;
1261 struct mga_device
*mdev
= dev
->dev_private
;
1262 struct pci_dev
*pdev
= dev
->pdev
;
1265 if (!mdev
->suspended
)
1268 pci_set_power_state(pdev
, PCI_D0
);
1269 pci_enable_device(pdev
);
1271 /* Disable sysclk */
1272 pci_read_config_dword(pdev
, PCI_MGA_OPTION
, &option
);
1274 pci_write_config_dword(pdev
, PCI_MGA_OPTION
, option
);
1276 mdev
->suspended
= false;
1283 static void mga_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
1285 struct drm_device
*dev
= crtc
->dev
;
1286 struct mga_device
*mdev
= dev
->dev_private
;
1287 u8 seq1
= 0, crtcext1
= 0;
1290 case DRM_MODE_DPMS_ON
:
1293 mga_crtc_load_lut(crtc
);
1295 case DRM_MODE_DPMS_STANDBY
:
1299 case DRM_MODE_DPMS_SUSPEND
:
1303 case DRM_MODE_DPMS_OFF
:
1310 if (mode
== DRM_MODE_DPMS_OFF
) {
1314 WREG8(MGAREG_SEQ_INDEX
, 0x01);
1315 seq1
|= RREG8(MGAREG_SEQ_DATA
) & ~0x20;
1316 mga_wait_vsync(mdev
);
1317 mga_wait_busy(mdev
);
1318 WREG8(MGAREG_SEQ_DATA
, seq1
);
1320 WREG8(MGAREG_CRTCEXT_INDEX
, 0x01);
1321 crtcext1
|= RREG8(MGAREG_CRTCEXT_DATA
) & ~0x30;
1322 WREG8(MGAREG_CRTCEXT_DATA
, crtcext1
);
1325 if (mode
== DRM_MODE_DPMS_ON
&& mdev
->suspended
== true) {
1327 drm_helper_resume_force_mode(dev
);
1333 * This is called before a mode is programmed. A typical use might be to
1334 * enable DPMS during the programming to avoid seeing intermediate stages,
1335 * but that's not relevant to us
1337 static void mga_crtc_prepare(struct drm_crtc
*crtc
)
1339 struct drm_device
*dev
= crtc
->dev
;
1340 struct mga_device
*mdev
= dev
->dev_private
;
1343 /* mga_resume(crtc);*/
1345 WREG8(MGAREG_CRTC_INDEX
, 0x11);
1346 tmp
= RREG8(MGAREG_CRTC_DATA
);
1347 WREG_CRT(0x11, tmp
| 0x80);
1349 if (mdev
->type
== G200_SE_A
|| mdev
->type
== G200_SE_B
) {
1355 WREG8(MGAREG_SEQ_INDEX
, 0x1);
1356 tmp
= RREG8(MGAREG_SEQ_DATA
);
1358 /* start sync reset */
1360 WREG_SEQ(1, tmp
| 0x20);
1363 if (mdev
->type
== G200_WB
|| mdev
->type
== G200_EW3
)
1364 mga_g200wb_prepare(crtc
);
1370 * This is called after a mode is programmed. It should reverse anything done
1371 * by the prepare function
1373 static void mga_crtc_commit(struct drm_crtc
*crtc
)
1375 struct drm_device
*dev
= crtc
->dev
;
1376 struct mga_device
*mdev
= dev
->dev_private
;
1377 const struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
1380 if (mdev
->type
== G200_WB
|| mdev
->type
== G200_EW3
)
1381 mga_g200wb_commit(crtc
);
1383 if (mdev
->type
== G200_SE_A
|| mdev
->type
== G200_SE_B
) {
1389 WREG8(MGAREG_SEQ_INDEX
, 0x1);
1390 tmp
= RREG8(MGAREG_SEQ_DATA
);
1396 crtc_funcs
->dpms(crtc
, DRM_MODE_DPMS_ON
);
1400 * The core can pass us a set of gamma values to program. We actually only
1401 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1402 * but it's a requirement that we provide the function
1404 static int mga_crtc_gamma_set(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
1405 u16
*blue
, uint32_t size
,
1406 struct drm_modeset_acquire_ctx
*ctx
)
1408 mga_crtc_load_lut(crtc
);
1413 /* Simple cleanup function */
1414 static void mga_crtc_destroy(struct drm_crtc
*crtc
)
1416 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1418 drm_crtc_cleanup(crtc
);
1422 static void mga_crtc_disable(struct drm_crtc
*crtc
)
1425 DRM_DEBUG_KMS("\n");
1426 mga_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
1427 if (crtc
->primary
->fb
) {
1428 struct mga_framebuffer
*mga_fb
= to_mga_framebuffer(crtc
->primary
->fb
);
1429 struct drm_gem_object
*obj
= mga_fb
->obj
;
1430 struct mgag200_bo
*bo
= gem_to_mga_bo(obj
);
1431 ret
= mgag200_bo_reserve(bo
, false);
1434 mgag200_bo_push_sysram(bo
);
1435 mgag200_bo_unreserve(bo
);
1437 crtc
->primary
->fb
= NULL
;
1440 /* These provide the minimum set of functions required to handle a CRTC */
1441 static const struct drm_crtc_funcs mga_crtc_funcs
= {
1442 .cursor_set
= mga_crtc_cursor_set
,
1443 .cursor_move
= mga_crtc_cursor_move
,
1444 .gamma_set
= mga_crtc_gamma_set
,
1445 .set_config
= drm_crtc_helper_set_config
,
1446 .destroy
= mga_crtc_destroy
,
1449 static const struct drm_crtc_helper_funcs mga_helper_funcs
= {
1450 .disable
= mga_crtc_disable
,
1451 .dpms
= mga_crtc_dpms
,
1452 .mode_set
= mga_crtc_mode_set
,
1453 .mode_set_base
= mga_crtc_mode_set_base
,
1454 .prepare
= mga_crtc_prepare
,
1455 .commit
= mga_crtc_commit
,
1459 static void mga_crtc_init(struct mga_device
*mdev
)
1461 struct mga_crtc
*mga_crtc
;
1463 mga_crtc
= kzalloc(sizeof(struct mga_crtc
) +
1464 (MGAG200FB_CONN_LIMIT
* sizeof(struct drm_connector
*)),
1467 if (mga_crtc
== NULL
)
1470 drm_crtc_init(mdev
->dev
, &mga_crtc
->base
, &mga_crtc_funcs
);
1472 drm_mode_crtc_set_gamma_size(&mga_crtc
->base
, MGAG200_LUT_SIZE
);
1473 mdev
->mode_info
.crtc
= mga_crtc
;
1475 drm_crtc_helper_add(&mga_crtc
->base
, &mga_helper_funcs
);
1479 * The encoder comes after the CRTC in the output pipeline, but before
1480 * the connector. It's responsible for ensuring that the digital
1481 * stream is appropriately converted into the output format. Setup is
1482 * very simple in this case - all we have to do is inform qemu of the
1483 * colour depth in order to ensure that it displays appropriately
1487 * These functions are analagous to those in the CRTC code, but are intended
1488 * to handle any encoder-specific limitations
1490 static void mga_encoder_mode_set(struct drm_encoder
*encoder
,
1491 struct drm_display_mode
*mode
,
1492 struct drm_display_mode
*adjusted_mode
)
1497 static void mga_encoder_dpms(struct drm_encoder
*encoder
, int state
)
1502 static void mga_encoder_prepare(struct drm_encoder
*encoder
)
1506 static void mga_encoder_commit(struct drm_encoder
*encoder
)
1510 static void mga_encoder_destroy(struct drm_encoder
*encoder
)
1512 struct mga_encoder
*mga_encoder
= to_mga_encoder(encoder
);
1513 drm_encoder_cleanup(encoder
);
1517 static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs
= {
1518 .dpms
= mga_encoder_dpms
,
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_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 uint64_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 enum drm_mode_status
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 if (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1616 return MODE_BANDWIDTH
;
1618 } else if (mdev
->type
== G200_WB
) {
1619 if (mode
->hdisplay
> 1280)
1620 return MODE_VIRTUAL_X
;
1621 if (mode
->vdisplay
> 1024)
1622 return MODE_VIRTUAL_Y
;
1623 if (mga_vga_calculate_mode_bandwidth(mode
, bpp
) >
1625 return MODE_BANDWIDTH
;
1626 } else if (mdev
->type
== G200_EV
&&
1627 (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1628 > (32700 * 1024))) {
1629 return MODE_BANDWIDTH
;
1630 } else if (mdev
->type
== G200_EH
&&
1631 (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1632 > (37500 * 1024))) {
1633 return MODE_BANDWIDTH
;
1634 } else if (mdev
->type
== G200_ER
&&
1635 (mga_vga_calculate_mode_bandwidth(mode
,
1636 bpp
) > (55000 * 1024))) {
1637 return MODE_BANDWIDTH
;
1640 if ((mode
->hdisplay
% 8) != 0 || (mode
->hsync_start
% 8) != 0 ||
1641 (mode
->hsync_end
% 8) != 0 || (mode
->htotal
% 8) != 0) {
1642 return MODE_H_ILLEGAL
;
1645 if (mode
->crtc_hdisplay
> 2048 || mode
->crtc_hsync_start
> 4096 ||
1646 mode
->crtc_hsync_end
> 4096 || mode
->crtc_htotal
> 4096 ||
1647 mode
->crtc_vdisplay
> 2048 || mode
->crtc_vsync_start
> 4096 ||
1648 mode
->crtc_vsync_end
> 4096 || mode
->crtc_vtotal
> 4096) {
1652 /* Validate the mode input by the user */
1653 if (connector
->cmdline_mode
.specified
) {
1654 if (connector
->cmdline_mode
.bpp_specified
)
1655 bpp
= connector
->cmdline_mode
.bpp
;
1658 if ((mode
->hdisplay
* mode
->vdisplay
* (bpp
/8)) > mdev
->mc
.vram_size
) {
1659 if (connector
->cmdline_mode
.specified
)
1660 connector
->cmdline_mode
.specified
= false;
1667 static struct drm_encoder
*mga_connector_best_encoder(struct drm_connector
1670 int enc_id
= connector
->encoder_ids
[0];
1671 /* pick the encoder ids */
1673 return drm_encoder_find(connector
->dev
, NULL
, enc_id
);
1677 static void mga_connector_destroy(struct drm_connector
*connector
)
1679 struct mga_connector
*mga_connector
= to_mga_connector(connector
);
1680 mgag200_i2c_destroy(mga_connector
->i2c
);
1681 drm_connector_cleanup(connector
);
1685 static const struct drm_connector_helper_funcs mga_vga_connector_helper_funcs
= {
1686 .get_modes
= mga_vga_get_modes
,
1687 .mode_valid
= mga_vga_mode_valid
,
1688 .best_encoder
= mga_connector_best_encoder
,
1691 static const struct drm_connector_funcs mga_vga_connector_funcs
= {
1692 .dpms
= drm_helper_connector_dpms
,
1693 .fill_modes
= drm_helper_probe_single_connector_modes
,
1694 .destroy
= mga_connector_destroy
,
1697 static struct drm_connector
*mga_vga_init(struct drm_device
*dev
)
1699 struct drm_connector
*connector
;
1700 struct mga_connector
*mga_connector
;
1702 mga_connector
= kzalloc(sizeof(struct mga_connector
), GFP_KERNEL
);
1706 connector
= &mga_connector
->base
;
1708 drm_connector_init(dev
, connector
,
1709 &mga_vga_connector_funcs
, DRM_MODE_CONNECTOR_VGA
);
1711 drm_connector_helper_add(connector
, &mga_vga_connector_helper_funcs
);
1713 drm_connector_register(connector
);
1715 mga_connector
->i2c
= mgag200_i2c_create(dev
);
1716 if (!mga_connector
->i2c
)
1717 DRM_ERROR("failed to add ddc bus\n");
1723 int mgag200_modeset_init(struct mga_device
*mdev
)
1725 struct drm_encoder
*encoder
;
1726 struct drm_connector
*connector
;
1729 mdev
->mode_info
.mode_config_initialized
= true;
1731 mdev
->dev
->mode_config
.max_width
= MGAG200_MAX_FB_WIDTH
;
1732 mdev
->dev
->mode_config
.max_height
= MGAG200_MAX_FB_HEIGHT
;
1734 mdev
->dev
->mode_config
.fb_base
= mdev
->mc
.vram_base
;
1736 mga_crtc_init(mdev
);
1738 encoder
= mga_encoder_init(mdev
->dev
);
1740 DRM_ERROR("mga_encoder_init failed\n");
1744 connector
= mga_vga_init(mdev
->dev
);
1746 DRM_ERROR("mga_vga_init failed\n");
1750 drm_connector_attach_encoder(connector
, encoder
);
1752 ret
= mgag200_fbdev_init(mdev
);
1754 DRM_ERROR("mga_fbdev_init failed\n");
1761 void mgag200_modeset_fini(struct mga_device
*mdev
)