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>
19 #include "mgag200_drv.h"
21 #define MGAG200_LUT_SIZE 256
24 * This file contains setup code for the CRTC.
27 static void mga_crtc_load_lut(struct drm_crtc
*crtc
)
29 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
30 struct drm_device
*dev
= crtc
->dev
;
31 struct mga_device
*mdev
= dev
->dev_private
;
32 struct drm_framebuffer
*fb
= crtc
->fb
;
38 WREG8(DAC_INDEX
+ MGA1064_INDEX
, 0);
40 if (fb
&& fb
->bits_per_pixel
== 16) {
41 int inc
= (fb
->depth
== 15) ? 8 : 4;
43 for (i
= 0; i
< MGAG200_LUT_SIZE
; i
+= inc
) {
44 if (fb
->depth
== 16) {
45 if (i
> (MGAG200_LUT_SIZE
>> 1)) {
48 r
= mga_crtc
->lut_r
[i
<< 1];
49 b
= mga_crtc
->lut_b
[i
<< 1];
52 r
= mga_crtc
->lut_r
[i
];
53 b
= mga_crtc
->lut_b
[i
];
56 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, r
);
57 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, mga_crtc
->lut_g
[i
]);
58 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, b
);
62 for (i
= 0; i
< MGAG200_LUT_SIZE
; i
++) {
64 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, mga_crtc
->lut_r
[i
]);
65 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, mga_crtc
->lut_g
[i
]);
66 WREG8(DAC_INDEX
+ MGA1064_COL_PAL
, mga_crtc
->lut_b
[i
]);
70 static inline void mga_wait_vsync(struct mga_device
*mdev
)
72 unsigned long timeout
= jiffies
+ HZ
/10;
73 unsigned int status
= 0;
76 status
= RREG32(MGAREG_Status
);
77 } while ((status
& 0x08) && time_before(jiffies
, timeout
));
78 timeout
= jiffies
+ HZ
/10;
81 status
= RREG32(MGAREG_Status
);
82 } while (!(status
& 0x08) && time_before(jiffies
, timeout
));
85 static inline void mga_wait_busy(struct mga_device
*mdev
)
87 unsigned long timeout
= jiffies
+ HZ
;
88 unsigned int status
= 0;
90 status
= RREG8(MGAREG_Status
+ 2);
91 } while ((status
& 0x01) && time_before(jiffies
, timeout
));
95 * The core passes the desired mode to the CRTC code to see whether any
96 * CRTC-specific modifications need to be made to it. We're in a position
97 * to just pass that straight through, so this does nothing
99 static bool mga_crtc_mode_fixup(struct drm_crtc
*crtc
,
100 const struct drm_display_mode
*mode
,
101 struct drm_display_mode
*adjusted_mode
)
106 static int mga_g200se_set_plls(struct mga_device
*mdev
, long clock
)
108 unsigned int vcomax
, vcomin
, pllreffreq
;
109 unsigned int delta
, tmpdelta
, permitteddelta
;
110 unsigned int testp
, testm
, testn
;
111 unsigned int p
, m
, n
;
112 unsigned int computed
;
120 permitteddelta
= clock
* 5 / 1000;
122 for (testp
= 8; testp
> 0; testp
/= 2) {
123 if (clock
* testp
> vcomax
)
125 if (clock
* testp
< vcomin
)
128 for (testn
= 17; testn
< 256; testn
++) {
129 for (testm
= 1; testm
< 32; testm
++) {
130 computed
= (pllreffreq
* testn
) /
132 if (computed
> clock
)
133 tmpdelta
= computed
- clock
;
135 tmpdelta
= clock
- computed
;
136 if (tmpdelta
< delta
) {
146 if (delta
> permitteddelta
) {
147 printk(KERN_WARNING
"PLL delta too large\n");
151 WREG_DAC(MGA1064_PIX_PLLC_M
, m
);
152 WREG_DAC(MGA1064_PIX_PLLC_N
, n
);
153 WREG_DAC(MGA1064_PIX_PLLC_P
, p
);
157 static int mga_g200wb_set_plls(struct mga_device
*mdev
, long clock
)
159 unsigned int vcomax
, vcomin
, pllreffreq
;
160 unsigned int delta
, tmpdelta
, permitteddelta
;
161 unsigned int testp
, testm
, testn
;
162 unsigned int p
, m
, n
;
163 unsigned int computed
;
164 int i
, j
, tmpcount
, vcount
;
165 bool pll_locked
= false;
174 permitteddelta
= clock
* 5 / 1000;
176 for (testp
= 1; testp
< 9; testp
++) {
177 if (clock
* testp
> vcomax
)
179 if (clock
* testp
< vcomin
)
182 for (testm
= 1; testm
< 17; testm
++) {
183 for (testn
= 1; testn
< 151; testn
++) {
184 computed
= (pllreffreq
* testn
) /
186 if (computed
> clock
)
187 tmpdelta
= computed
- clock
;
189 tmpdelta
= clock
- computed
;
190 if (tmpdelta
< delta
) {
193 m
= (testm
- 1) | ((n
>> 1) & 0x80);
200 for (i
= 0; i
<= 32 && pll_locked
== false; i
++) {
202 WREG8(MGAREG_CRTC_INDEX
, 0x1e);
203 tmp
= RREG8(MGAREG_CRTC_DATA
);
205 WREG8(MGAREG_CRTC_DATA
, tmp
+1);
208 /* set pixclkdis to 1 */
209 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
210 tmp
= RREG8(DAC_DATA
);
211 tmp
|= MGA1064_PIX_CLK_CTL_CLK_DIS
;
212 WREG8(DAC_DATA
, tmp
);
214 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL
);
215 tmp
= RREG8(DAC_DATA
);
216 tmp
|= MGA1064_REMHEADCTL_CLKDIS
;
217 WREG8(DAC_DATA
, tmp
);
219 /* select PLL Set C */
220 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
222 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
224 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
225 tmp
= RREG8(DAC_DATA
);
226 tmp
|= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
| 0x80;
227 WREG8(DAC_DATA
, tmp
);
232 WREG8(DAC_INDEX
, MGA1064_VREF_CTL
);
233 tmp
= RREG8(DAC_DATA
);
235 WREG8(DAC_DATA
, tmp
);
239 /* program pixel pll register */
240 WREG_DAC(MGA1064_WB_PIX_PLLC_N
, n
);
241 WREG_DAC(MGA1064_WB_PIX_PLLC_M
, m
);
242 WREG_DAC(MGA1064_WB_PIX_PLLC_P
, p
);
247 WREG8(DAC_INDEX
, MGA1064_VREF_CTL
);
248 tmp
= RREG8(DAC_DATA
);
250 WREG_DAC(MGA1064_VREF_CTL
, tmp
);
254 /* select the pixel pll */
255 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
256 tmp
= RREG8(DAC_DATA
);
257 tmp
&= ~MGA1064_PIX_CLK_CTL_SEL_MSK
;
258 tmp
|= MGA1064_PIX_CLK_CTL_SEL_PLL
;
259 WREG8(DAC_DATA
, tmp
);
261 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL
);
262 tmp
= RREG8(DAC_DATA
);
263 tmp
&= ~MGA1064_REMHEADCTL_CLKSL_MSK
;
264 tmp
|= MGA1064_REMHEADCTL_CLKSL_PLL
;
265 WREG8(DAC_DATA
, tmp
);
267 /* reset dotclock rate bit */
268 WREG8(MGAREG_SEQ_INDEX
, 1);
269 tmp
= RREG8(MGAREG_SEQ_DATA
);
271 WREG8(MGAREG_SEQ_DATA
, tmp
);
273 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
274 tmp
= RREG8(DAC_DATA
);
275 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_DIS
;
276 WREG8(DAC_DATA
, tmp
);
278 vcount
= RREG8(MGAREG_VCOUNT
);
280 for (j
= 0; j
< 30 && pll_locked
== false; j
++) {
281 tmpcount
= RREG8(MGAREG_VCOUNT
);
282 if (tmpcount
< vcount
)
284 if ((tmpcount
- vcount
) > 2)
290 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL
);
291 tmp
= RREG8(DAC_DATA
);
292 tmp
&= ~MGA1064_REMHEADCTL_CLKDIS
;
293 WREG_DAC(MGA1064_REMHEADCTL
, tmp
);
297 static int mga_g200ev_set_plls(struct mga_device
*mdev
, long clock
)
299 unsigned int vcomax
, vcomin
, pllreffreq
;
300 unsigned int delta
, tmpdelta
, permitteddelta
;
301 unsigned int testp
, testm
, testn
;
302 unsigned int p
, m
, n
;
303 unsigned int computed
;
312 permitteddelta
= clock
* 5 / 1000;
314 for (testp
= 16; testp
> 0; testp
--) {
315 if (clock
* testp
> vcomax
)
317 if (clock
* testp
< vcomin
)
320 for (testn
= 1; testn
< 257; testn
++) {
321 for (testm
= 1; testm
< 17; testm
++) {
322 computed
= (pllreffreq
* testn
) /
324 if (computed
> clock
)
325 tmpdelta
= computed
- clock
;
327 tmpdelta
= clock
- computed
;
328 if (tmpdelta
< delta
) {
338 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
339 tmp
= RREG8(DAC_DATA
);
340 tmp
|= MGA1064_PIX_CLK_CTL_CLK_DIS
;
341 WREG8(DAC_DATA
, tmp
);
343 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
345 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
347 WREG8(DAC_INDEX
, MGA1064_PIX_PLL_STAT
);
348 tmp
= RREG8(DAC_DATA
);
349 WREG8(DAC_DATA
, tmp
& ~0x40);
351 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
352 tmp
= RREG8(DAC_DATA
);
353 tmp
|= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
354 WREG8(DAC_DATA
, tmp
);
356 WREG_DAC(MGA1064_EV_PIX_PLLC_M
, m
);
357 WREG_DAC(MGA1064_EV_PIX_PLLC_N
, n
);
358 WREG_DAC(MGA1064_EV_PIX_PLLC_P
, p
);
362 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
363 tmp
= RREG8(DAC_DATA
);
364 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
365 WREG8(DAC_DATA
, tmp
);
369 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
370 tmp
= RREG8(DAC_DATA
);
371 tmp
&= ~MGA1064_PIX_CLK_CTL_SEL_MSK
;
372 tmp
|= MGA1064_PIX_CLK_CTL_SEL_PLL
;
373 WREG8(DAC_DATA
, tmp
);
375 WREG8(DAC_INDEX
, MGA1064_PIX_PLL_STAT
);
376 tmp
= RREG8(DAC_DATA
);
377 WREG8(DAC_DATA
, tmp
| 0x40);
379 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
381 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
383 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
384 tmp
= RREG8(DAC_DATA
);
385 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_DIS
;
386 WREG8(DAC_DATA
, tmp
);
391 static int mga_g200eh_set_plls(struct mga_device
*mdev
, long clock
)
393 unsigned int vcomax
, vcomin
, pllreffreq
;
394 unsigned int delta
, tmpdelta
, permitteddelta
;
395 unsigned int testp
, testm
, testn
;
396 unsigned int p
, m
, n
;
397 unsigned int computed
;
398 int i
, j
, tmpcount
, vcount
;
400 bool pll_locked
= false;
408 permitteddelta
= clock
* 5 / 1000;
410 for (testp
= 16; testp
> 0; testp
>>= 1) {
411 if (clock
* testp
> vcomax
)
413 if (clock
* testp
< vcomin
)
416 for (testm
= 1; testm
< 33; testm
++) {
417 for (testn
= 17; testn
< 257; testn
++) {
418 computed
= (pllreffreq
* testn
) /
420 if (computed
> clock
)
421 tmpdelta
= computed
- clock
;
423 tmpdelta
= clock
- computed
;
424 if (tmpdelta
< delta
) {
430 if ((clock
* testp
) >= 600000)
435 for (i
= 0; i
<= 32 && pll_locked
== false; i
++) {
436 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
437 tmp
= RREG8(DAC_DATA
);
438 tmp
|= MGA1064_PIX_CLK_CTL_CLK_DIS
;
439 WREG8(DAC_DATA
, tmp
);
441 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
443 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
445 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
446 tmp
= RREG8(DAC_DATA
);
447 tmp
|= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
448 WREG8(DAC_DATA
, tmp
);
452 WREG_DAC(MGA1064_EH_PIX_PLLC_M
, m
);
453 WREG_DAC(MGA1064_EH_PIX_PLLC_N
, n
);
454 WREG_DAC(MGA1064_EH_PIX_PLLC_P
, p
);
458 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
459 tmp
= RREG8(DAC_DATA
);
460 tmp
&= ~MGA1064_PIX_CLK_CTL_SEL_MSK
;
461 tmp
|= MGA1064_PIX_CLK_CTL_SEL_PLL
;
462 WREG8(DAC_DATA
, tmp
);
464 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
465 tmp
= RREG8(DAC_DATA
);
466 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_DIS
;
467 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
468 WREG8(DAC_DATA
, tmp
);
470 vcount
= RREG8(MGAREG_VCOUNT
);
472 for (j
= 0; j
< 30 && pll_locked
== false; j
++) {
473 tmpcount
= RREG8(MGAREG_VCOUNT
);
474 if (tmpcount
< vcount
)
476 if ((tmpcount
- vcount
) > 2)
486 static int mga_g200er_set_plls(struct mga_device
*mdev
, long clock
)
488 unsigned int vcomax
, vcomin
, pllreffreq
;
489 unsigned int delta
, tmpdelta
;
490 int testr
, testn
, testm
, testo
;
491 unsigned int p
, m
, n
;
492 unsigned int computed
, vco
;
494 const unsigned int m_div_val
[] = { 1, 2, 4, 8 };
503 for (testr
= 0; testr
< 4; testr
++) {
506 for (testn
= 5; testn
< 129; testn
++) {
509 for (testm
= 3; testm
>= 0; testm
--) {
512 for (testo
= 5; testo
< 33; testo
++) {
513 vco
= pllreffreq
* (testn
+ 1) /
519 computed
= vco
/ (m_div_val
[testm
] * (testo
+ 1));
520 if (computed
> clock
)
521 tmpdelta
= computed
- clock
;
523 tmpdelta
= clock
- computed
;
524 if (tmpdelta
< delta
) {
526 m
= testm
| (testo
<< 3);
528 p
= testr
| (testr
<< 3);
535 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
536 tmp
= RREG8(DAC_DATA
);
537 tmp
|= MGA1064_PIX_CLK_CTL_CLK_DIS
;
538 WREG8(DAC_DATA
, tmp
);
540 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL
);
541 tmp
= RREG8(DAC_DATA
);
542 tmp
|= MGA1064_REMHEADCTL_CLKDIS
;
543 WREG8(DAC_DATA
, tmp
);
545 tmp
= RREG8(MGAREG_MEM_MISC_READ
);
546 tmp
|= (0x3<<2) | 0xc0;
547 WREG8(MGAREG_MEM_MISC_WRITE
, tmp
);
549 WREG8(DAC_INDEX
, MGA1064_PIX_CLK_CTL
);
550 tmp
= RREG8(DAC_DATA
);
551 tmp
&= ~MGA1064_PIX_CLK_CTL_CLK_DIS
;
552 tmp
|= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN
;
553 WREG8(DAC_DATA
, tmp
);
557 WREG_DAC(MGA1064_ER_PIX_PLLC_N
, n
);
558 WREG_DAC(MGA1064_ER_PIX_PLLC_M
, m
);
559 WREG_DAC(MGA1064_ER_PIX_PLLC_P
, p
);
566 static int mga_crtc_set_plls(struct mga_device
*mdev
, long clock
)
571 return mga_g200se_set_plls(mdev
, clock
);
574 return mga_g200wb_set_plls(mdev
, clock
);
577 return mga_g200ev_set_plls(mdev
, clock
);
580 return mga_g200eh_set_plls(mdev
, clock
);
583 return mga_g200er_set_plls(mdev
, clock
);
589 static void mga_g200wb_prepare(struct drm_crtc
*crtc
)
591 struct mga_device
*mdev
= crtc
->dev
->dev_private
;
595 /* 1- The first step is to warn the BMC of an upcoming mode change.
596 * We are putting the misc<0> to output.*/
598 WREG8(DAC_INDEX
, MGA1064_GEN_IO_CTL
);
599 tmp
= RREG8(DAC_DATA
);
601 WREG_DAC(MGA1064_GEN_IO_CTL
, tmp
);
603 /* we are putting a 1 on the misc<0> line */
604 WREG8(DAC_INDEX
, MGA1064_GEN_IO_DATA
);
605 tmp
= RREG8(DAC_DATA
);
607 WREG_DAC(MGA1064_GEN_IO_DATA
, tmp
);
609 /* 2- Second step to mask and further scan request
610 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
612 WREG8(DAC_INDEX
, MGA1064_SPAREREG
);
613 tmp
= RREG8(DAC_DATA
);
615 WREG_DAC(MGA1064_SPAREREG
, tmp
);
617 /* 3a- the third step is to verifu if there is an active scan
618 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
621 while (!(tmp
& 0x1) && iter_max
) {
622 WREG8(DAC_INDEX
, MGA1064_SPAREREG
);
623 tmp
= RREG8(DAC_DATA
);
628 /* 3b- this step occurs only if the remove is actually scanning
629 * we are waiting for the end of the frame which is a 1 on
630 * remvsyncsts (XSPAREREG<1>)
634 while ((tmp
& 0x2) && iter_max
) {
635 WREG8(DAC_INDEX
, MGA1064_SPAREREG
);
636 tmp
= RREG8(DAC_DATA
);
643 static void mga_g200wb_commit(struct drm_crtc
*crtc
)
646 struct mga_device
*mdev
= crtc
->dev
->dev_private
;
648 /* 1- The first step is to ensure that the vrsten and hrsten are set */
649 WREG8(MGAREG_CRTCEXT_INDEX
, 1);
650 tmp
= RREG8(MGAREG_CRTCEXT_DATA
);
651 WREG8(MGAREG_CRTCEXT_DATA
, tmp
| 0x88);
653 /* 2- second step is to assert the rstlvl2 */
654 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL2
);
655 tmp
= RREG8(DAC_DATA
);
657 WREG8(DAC_DATA
, tmp
);
662 /* 3- deassert rstlvl2 */
664 WREG8(DAC_INDEX
, MGA1064_REMHEADCTL2
);
665 WREG8(DAC_DATA
, tmp
);
667 /* 4- remove mask of scan request */
668 WREG8(DAC_INDEX
, MGA1064_SPAREREG
);
669 tmp
= RREG8(DAC_DATA
);
671 WREG8(DAC_DATA
, tmp
);
673 /* 5- put back a 0 on the misc<0> line */
674 WREG8(DAC_INDEX
, MGA1064_GEN_IO_DATA
);
675 tmp
= RREG8(DAC_DATA
);
677 WREG_DAC(MGA1064_GEN_IO_DATA
, tmp
);
681 This is how the framebuffer base address is stored in g200 cards:
682 * Assume @offset is the gpu_addr variable of the framebuffer object
683 * Then addr is the number of _pixels_ (not bytes) from the start of
684 VRAM to the first pixel we want to display. (divided by 2 for 32bit
686 * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
687 addr<20> -> CRTCEXT0<6>
688 addr<19-16> -> CRTCEXT0<3-0>
689 addr<15-8> -> CRTCC<7-0>
690 addr<7-0> -> CRTCD<7-0>
691 CRTCEXT0 has to be programmed last to trigger an update and make the
692 new addr variable take effect.
694 void mga_set_start_address(struct drm_crtc
*crtc
, unsigned offset
)
696 struct mga_device
*mdev
= crtc
->dev
->dev_private
;
701 while (RREG8(0x1fda) & 0x08);
702 while (!(RREG8(0x1fda) & 0x08));
704 count
= RREG8(MGAREG_VCOUNT
) + 2;
705 while (RREG8(MGAREG_VCOUNT
) < count
);
707 WREG8(MGAREG_CRTCEXT_INDEX
, 0);
708 crtcext0
= RREG8(MGAREG_CRTCEXT_DATA
);
711 /* Can't store addresses any higher than that...
712 but we also don't have more than 16MB of memory, so it should be fine. */
713 WARN_ON(addr
> 0x1fffff);
714 crtcext0
|= (!!(addr
& (1<<20)))<<6;
715 WREG_CRT(0x0d, (u8
)(addr
& 0xff));
716 WREG_CRT(0x0c, (u8
)(addr
>> 8) & 0xff);
717 WREG_ECRT(0x0, ((u8
)(addr
>> 16) & 0xf) | crtcext0
);
721 /* ast is different - we will force move buffers out of VRAM */
722 static int mga_crtc_do_set_base(struct drm_crtc
*crtc
,
723 struct drm_framebuffer
*fb
,
724 int x
, int y
, int atomic
)
726 struct mga_device
*mdev
= crtc
->dev
->dev_private
;
727 struct drm_gem_object
*obj
;
728 struct mga_framebuffer
*mga_fb
;
729 struct mgag200_bo
*bo
;
733 /* push the previous fb to system ram */
735 mga_fb
= to_mga_framebuffer(fb
);
737 bo
= gem_to_mga_bo(obj
);
738 ret
= mgag200_bo_reserve(bo
, false);
741 mgag200_bo_push_sysram(bo
);
742 mgag200_bo_unreserve(bo
);
745 mga_fb
= to_mga_framebuffer(crtc
->fb
);
747 bo
= gem_to_mga_bo(obj
);
749 ret
= mgag200_bo_reserve(bo
, false);
753 ret
= mgag200_bo_pin(bo
, TTM_PL_FLAG_VRAM
, &gpu_addr
);
755 mgag200_bo_unreserve(bo
);
759 if (&mdev
->mfbdev
->mfb
== mga_fb
) {
760 /* if pushing console in kmap it */
761 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
763 DRM_ERROR("failed to kmap fbcon\n");
766 mgag200_bo_unreserve(bo
);
768 DRM_INFO("mga base %llx\n", gpu_addr
);
770 mga_set_start_address(crtc
, (u32
)gpu_addr
);
775 static int mga_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
776 struct drm_framebuffer
*old_fb
)
778 return mga_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
781 static int mga_crtc_mode_set(struct drm_crtc
*crtc
,
782 struct drm_display_mode
*mode
,
783 struct drm_display_mode
*adjusted_mode
,
784 int x
, int y
, struct drm_framebuffer
*old_fb
)
786 struct drm_device
*dev
= crtc
->dev
;
787 struct mga_device
*mdev
= dev
->dev_private
;
788 int hdisplay
, hsyncstart
, hsyncend
, htotal
;
789 int vdisplay
, vsyncstart
, vsyncend
, vtotal
;
791 int option
= 0, option2
= 0;
793 unsigned char misc
= 0;
794 unsigned char ext_vga
[6];
797 static unsigned char dacvalue
[] = {
798 /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
799 /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
800 /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
801 /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
802 /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
803 /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
804 /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
805 /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
806 /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
807 /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
810 bppshift
= mdev
->bpp_shifts
[(crtc
->fb
->bits_per_pixel
>> 3) - 1];
812 switch (mdev
->type
) {
815 dacvalue
[MGA1064_VREF_CTL
] = 0x03;
816 dacvalue
[MGA1064_PIX_CLK_CTL
] = MGA1064_PIX_CLK_CTL_SEL_PLL
;
817 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_DAC_EN
|
818 MGA1064_MISC_CTL_VGA8
|
819 MGA1064_MISC_CTL_DAC_RAM_CS
;
824 option2
= 0x00008000;
827 dacvalue
[MGA1064_VREF_CTL
] = 0x07;
829 option2
= 0x0000b000;
832 dacvalue
[MGA1064_PIX_CLK_CTL
] = MGA1064_PIX_CLK_CTL_SEL_PLL
;
833 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_VGA8
|
834 MGA1064_MISC_CTL_DAC_RAM_CS
;
836 option2
= 0x0000b000;
839 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_VGA8
|
840 MGA1064_MISC_CTL_DAC_RAM_CS
;
842 option2
= 0x0000b000;
848 switch (crtc
->fb
->bits_per_pixel
) {
850 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_8bits
;
853 if (crtc
->fb
->depth
== 15)
854 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_15bits
;
856 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_16bits
;
859 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_24bits
;
862 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_32_24bits
;
866 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
868 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
872 for (i
= 0; i
< sizeof(dacvalue
); i
++) {
876 ((i
>= 0x1f) && (i
<= 0x29)) ||
877 ((i
>= 0x30) && (i
<= 0x37)))
879 if (IS_G200_SE(mdev
) &&
880 ((i
== 0x2c) || (i
== 0x2d) || (i
== 0x2e)))
882 if ((mdev
->type
== G200_EV
|| mdev
->type
== G200_WB
|| mdev
->type
== G200_EH
) &&
883 (i
>= 0x44) && (i
<= 0x4e))
886 WREG_DAC(i
, dacvalue
[i
]);
889 if (mdev
->type
== G200_ER
)
893 pci_write_config_dword(dev
->pdev
, PCI_MGA_OPTION
, option
);
895 pci_write_config_dword(dev
->pdev
, PCI_MGA_OPTION2
, option2
);
901 pitch
= crtc
->fb
->pitches
[0] / (crtc
->fb
->bits_per_pixel
/ 8);
902 if (crtc
->fb
->bits_per_pixel
== 24)
903 pitch
= (pitch
* 3) >> (4 - bppshift
);
905 pitch
= pitch
>> (4 - bppshift
);
907 hdisplay
= mode
->hdisplay
/ 8 - 1;
908 hsyncstart
= mode
->hsync_start
/ 8 - 1;
909 hsyncend
= mode
->hsync_end
/ 8 - 1;
910 htotal
= mode
->htotal
/ 8 - 1;
912 /* Work around hardware quirk */
913 if ((htotal
& 0x07) == 0x06 || (htotal
& 0x07) == 0x04)
916 vdisplay
= mode
->vdisplay
- 1;
917 vsyncstart
= mode
->vsync_start
- 1;
918 vsyncend
= mode
->vsync_end
- 1;
919 vtotal
= mode
->vtotal
- 2;
931 WREG_CRT(0, htotal
- 4);
932 WREG_CRT(1, hdisplay
);
933 WREG_CRT(2, hdisplay
);
934 WREG_CRT(3, (htotal
& 0x1F) | 0x80);
935 WREG_CRT(4, hsyncstart
);
936 WREG_CRT(5, ((htotal
& 0x20) << 2) | (hsyncend
& 0x1F));
937 WREG_CRT(6, vtotal
& 0xFF);
938 WREG_CRT(7, ((vtotal
& 0x100) >> 8) |
939 ((vdisplay
& 0x100) >> 7) |
940 ((vsyncstart
& 0x100) >> 6) |
941 ((vdisplay
& 0x100) >> 5) |
942 ((vdisplay
& 0x100) >> 4) | /* linecomp */
943 ((vtotal
& 0x200) >> 4)|
944 ((vdisplay
& 0x200) >> 3) |
945 ((vsyncstart
& 0x200) >> 2));
946 WREG_CRT(9, ((vdisplay
& 0x200) >> 4) |
947 ((vdisplay
& 0x200) >> 3));
954 WREG_CRT(16, vsyncstart
& 0xFF);
955 WREG_CRT(17, (vsyncend
& 0x0F) | 0x20);
956 WREG_CRT(18, vdisplay
& 0xFF);
957 WREG_CRT(19, pitch
& 0xFF);
959 WREG_CRT(21, vdisplay
& 0xFF);
960 WREG_CRT(22, (vtotal
+ 1) & 0xFF);
962 WREG_CRT(24, vdisplay
& 0xFF);
969 ext_vga
[0] |= (pitch
& 0x300) >> 4;
970 ext_vga
[1] = (((htotal
- 4) & 0x100) >> 8) |
971 ((hdisplay
& 0x100) >> 7) |
972 ((hsyncstart
& 0x100) >> 6) |
974 ext_vga
[2] = ((vtotal
& 0xc00) >> 10) |
975 ((vdisplay
& 0x400) >> 8) |
976 ((vdisplay
& 0xc00) >> 7) |
977 ((vsyncstart
& 0xc00) >> 5) |
978 ((vdisplay
& 0x400) >> 3);
979 if (crtc
->fb
->bits_per_pixel
== 24)
980 ext_vga
[3] = (((1 << bppshift
) * 3) - 1) | 0x80;
982 ext_vga
[3] = ((1 << bppshift
) - 1) | 0x80;
984 if (mdev
->type
== G200_WB
)
987 /* Set pixel clocks */
989 WREG8(MGA_MISC_OUT
, misc
);
991 mga_crtc_set_plls(mdev
, mode
->clock
);
993 for (i
= 0; i
< 6; i
++) {
994 WREG_ECRT(i
, ext_vga
[i
]);
997 if (mdev
->type
== G200_ER
)
998 WREG_ECRT(0x24, 0x5);
1000 if (mdev
->type
== G200_EV
) {
1004 WREG_ECRT(0, ext_vga
[0]);
1005 /* Enable mga pixel clock */
1008 WREG8(MGA_MISC_OUT
, misc
);
1011 memcpy(&mdev
->mode
, mode
, sizeof(struct drm_display_mode
));
1013 mga_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
1016 if (mdev
->type
== G200_ER
) {
1017 u32 mem_ctl
= RREG32(MGAREG_MEMCTL
);
1021 WREG8(MGAREG_SEQ_INDEX
, 0x01);
1022 seq1
= RREG8(MGAREG_SEQ_DATA
) | 0x20;
1023 WREG8(MGAREG_SEQ_DATA
, seq1
);
1025 WREG32(MGAREG_MEMCTL
, mem_ctl
| 0x00200000);
1027 WREG32(MGAREG_MEMCTL
, mem_ctl
& ~0x00200000);
1029 WREG8(MGAREG_SEQ_DATA
, seq1
& ~0x20);
1033 if (IS_G200_SE(mdev
)) {
1034 if (mdev
->unique_rev_id
>= 0x02) {
1039 if (crtc
->fb
->bits_per_pixel
> 16)
1041 else if (crtc
->fb
->bits_per_pixel
> 8)
1046 mb
= (mode
->clock
* bpp
) / 1000;
1060 WREG8(MGAREG_CRTCEXT_INDEX
, 0x06);
1061 WREG8(MGAREG_CRTCEXT_DATA
, hi_pri_lvl
);
1063 WREG8(MGAREG_CRTCEXT_INDEX
, 0x06);
1064 if (mdev
->unique_rev_id
>= 0x01)
1065 WREG8(MGAREG_CRTCEXT_DATA
, 0x03);
1067 WREG8(MGAREG_CRTCEXT_DATA
, 0x04);
1073 #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1074 static int mga_suspend(struct drm_crtc
*crtc
)
1076 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1077 struct drm_device
*dev
= crtc
->dev
;
1078 struct mga_device
*mdev
= dev
->dev_private
;
1079 struct pci_dev
*pdev
= dev
->pdev
;
1082 if (mdev
->suspended
)
1087 /* Disable the pixel clock */
1088 WREG_DAC(0x1a, 0x05);
1089 /* Power down the DAC */
1090 WREG_DAC(0x1e, 0x18);
1091 /* Power down the pixel PLL */
1092 WREG_DAC(0x1a, 0x0d);
1094 /* Disable PLLs and clocks */
1095 pci_read_config_dword(pdev
, PCI_MGA_OPTION
, &option
);
1096 option
&= ~(0x1F8024);
1097 pci_write_config_dword(pdev
, PCI_MGA_OPTION
, option
);
1098 pci_set_power_state(pdev
, PCI_D3hot
);
1099 pci_disable_device(pdev
);
1101 mdev
->suspended
= true;
1106 static int mga_resume(struct drm_crtc
*crtc
)
1108 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1109 struct drm_device
*dev
= crtc
->dev
;
1110 struct mga_device
*mdev
= dev
->dev_private
;
1111 struct pci_dev
*pdev
= dev
->pdev
;
1114 if (!mdev
->suspended
)
1117 pci_set_power_state(pdev
, PCI_D0
);
1118 pci_enable_device(pdev
);
1120 /* Disable sysclk */
1121 pci_read_config_dword(pdev
, PCI_MGA_OPTION
, &option
);
1123 pci_write_config_dword(pdev
, PCI_MGA_OPTION
, option
);
1125 mdev
->suspended
= false;
1132 static void mga_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
1134 struct drm_device
*dev
= crtc
->dev
;
1135 struct mga_device
*mdev
= dev
->dev_private
;
1136 u8 seq1
= 0, crtcext1
= 0;
1139 case DRM_MODE_DPMS_ON
:
1142 mga_crtc_load_lut(crtc
);
1144 case DRM_MODE_DPMS_STANDBY
:
1148 case DRM_MODE_DPMS_SUSPEND
:
1152 case DRM_MODE_DPMS_OFF
:
1159 if (mode
== DRM_MODE_DPMS_OFF
) {
1163 WREG8(MGAREG_SEQ_INDEX
, 0x01);
1164 seq1
|= RREG8(MGAREG_SEQ_DATA
) & ~0x20;
1165 mga_wait_vsync(mdev
);
1166 mga_wait_busy(mdev
);
1167 WREG8(MGAREG_SEQ_DATA
, seq1
);
1169 WREG8(MGAREG_CRTCEXT_INDEX
, 0x01);
1170 crtcext1
|= RREG8(MGAREG_CRTCEXT_DATA
) & ~0x30;
1171 WREG8(MGAREG_CRTCEXT_DATA
, crtcext1
);
1174 if (mode
== DRM_MODE_DPMS_ON
&& mdev
->suspended
== true) {
1176 drm_helper_resume_force_mode(dev
);
1182 * This is called before a mode is programmed. A typical use might be to
1183 * enable DPMS during the programming to avoid seeing intermediate stages,
1184 * but that's not relevant to us
1186 static void mga_crtc_prepare(struct drm_crtc
*crtc
)
1188 struct drm_device
*dev
= crtc
->dev
;
1189 struct mga_device
*mdev
= dev
->dev_private
;
1192 /* mga_resume(crtc);*/
1194 WREG8(MGAREG_CRTC_INDEX
, 0x11);
1195 tmp
= RREG8(MGAREG_CRTC_DATA
);
1196 WREG_CRT(0x11, tmp
| 0x80);
1198 if (mdev
->type
== G200_SE_A
|| mdev
->type
== G200_SE_B
) {
1204 WREG8(MGAREG_SEQ_INDEX
, 0x1);
1205 tmp
= RREG8(MGAREG_SEQ_DATA
);
1207 /* start sync reset */
1209 WREG_SEQ(1, tmp
| 0x20);
1212 if (mdev
->type
== G200_WB
)
1213 mga_g200wb_prepare(crtc
);
1219 * This is called after a mode is programmed. It should reverse anything done
1220 * by the prepare function
1222 static void mga_crtc_commit(struct drm_crtc
*crtc
)
1224 struct drm_device
*dev
= crtc
->dev
;
1225 struct mga_device
*mdev
= dev
->dev_private
;
1226 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
1229 if (mdev
->type
== G200_WB
)
1230 mga_g200wb_commit(crtc
);
1232 if (mdev
->type
== G200_SE_A
|| mdev
->type
== G200_SE_B
) {
1238 WREG8(MGAREG_SEQ_INDEX
, 0x1);
1239 tmp
= RREG8(MGAREG_SEQ_DATA
);
1245 crtc_funcs
->dpms(crtc
, DRM_MODE_DPMS_ON
);
1249 * The core can pass us a set of gamma values to program. We actually only
1250 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1251 * but it's a requirement that we provide the function
1253 static void mga_crtc_gamma_set(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
1254 u16
*blue
, uint32_t start
, uint32_t size
)
1256 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1257 int end
= (start
+ size
> MGAG200_LUT_SIZE
) ? MGAG200_LUT_SIZE
: start
+ size
;
1260 for (i
= start
; i
< end
; i
++) {
1261 mga_crtc
->lut_r
[i
] = red
[i
] >> 8;
1262 mga_crtc
->lut_g
[i
] = green
[i
] >> 8;
1263 mga_crtc
->lut_b
[i
] = blue
[i
] >> 8;
1265 mga_crtc_load_lut(crtc
);
1268 /* Simple cleanup function */
1269 static void mga_crtc_destroy(struct drm_crtc
*crtc
)
1271 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1273 drm_crtc_cleanup(crtc
);
1277 static void mga_crtc_disable(struct drm_crtc
*crtc
)
1280 DRM_DEBUG_KMS("\n");
1281 mga_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
1283 struct mga_framebuffer
*mga_fb
= to_mga_framebuffer(crtc
->fb
);
1284 struct drm_gem_object
*obj
= mga_fb
->obj
;
1285 struct mgag200_bo
*bo
= gem_to_mga_bo(obj
);
1286 ret
= mgag200_bo_reserve(bo
, false);
1289 mgag200_bo_push_sysram(bo
);
1290 mgag200_bo_unreserve(bo
);
1295 /* These provide the minimum set of functions required to handle a CRTC */
1296 static const struct drm_crtc_funcs mga_crtc_funcs
= {
1297 .cursor_set
= mga_crtc_cursor_set
,
1298 .cursor_move
= mga_crtc_cursor_move
,
1299 .gamma_set
= mga_crtc_gamma_set
,
1300 .set_config
= drm_crtc_helper_set_config
,
1301 .destroy
= mga_crtc_destroy
,
1304 static const struct drm_crtc_helper_funcs mga_helper_funcs
= {
1305 .disable
= mga_crtc_disable
,
1306 .dpms
= mga_crtc_dpms
,
1307 .mode_fixup
= mga_crtc_mode_fixup
,
1308 .mode_set
= mga_crtc_mode_set
,
1309 .mode_set_base
= mga_crtc_mode_set_base
,
1310 .prepare
= mga_crtc_prepare
,
1311 .commit
= mga_crtc_commit
,
1312 .load_lut
= mga_crtc_load_lut
,
1316 static void mga_crtc_init(struct mga_device
*mdev
)
1318 struct mga_crtc
*mga_crtc
;
1321 mga_crtc
= kzalloc(sizeof(struct mga_crtc
) +
1322 (MGAG200FB_CONN_LIMIT
* sizeof(struct drm_connector
*)),
1325 if (mga_crtc
== NULL
)
1328 drm_crtc_init(mdev
->dev
, &mga_crtc
->base
, &mga_crtc_funcs
);
1330 drm_mode_crtc_set_gamma_size(&mga_crtc
->base
, MGAG200_LUT_SIZE
);
1331 mdev
->mode_info
.crtc
= mga_crtc
;
1333 for (i
= 0; i
< MGAG200_LUT_SIZE
; i
++) {
1334 mga_crtc
->lut_r
[i
] = i
;
1335 mga_crtc
->lut_g
[i
] = i
;
1336 mga_crtc
->lut_b
[i
] = i
;
1339 drm_crtc_helper_add(&mga_crtc
->base
, &mga_helper_funcs
);
1342 /** Sets the color ramps on behalf of fbcon */
1343 void mga_crtc_fb_gamma_set(struct drm_crtc
*crtc
, u16 red
, u16 green
,
1344 u16 blue
, int regno
)
1346 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1348 mga_crtc
->lut_r
[regno
] = red
>> 8;
1349 mga_crtc
->lut_g
[regno
] = green
>> 8;
1350 mga_crtc
->lut_b
[regno
] = blue
>> 8;
1353 /** Gets the color ramps on behalf of fbcon */
1354 void mga_crtc_fb_gamma_get(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
1355 u16
*blue
, int regno
)
1357 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1359 *red
= (u16
)mga_crtc
->lut_r
[regno
] << 8;
1360 *green
= (u16
)mga_crtc
->lut_g
[regno
] << 8;
1361 *blue
= (u16
)mga_crtc
->lut_b
[regno
] << 8;
1365 * The encoder comes after the CRTC in the output pipeline, but before
1366 * the connector. It's responsible for ensuring that the digital
1367 * stream is appropriately converted into the output format. Setup is
1368 * very simple in this case - all we have to do is inform qemu of the
1369 * colour depth in order to ensure that it displays appropriately
1373 * These functions are analagous to those in the CRTC code, but are intended
1374 * to handle any encoder-specific limitations
1376 static bool mga_encoder_mode_fixup(struct drm_encoder
*encoder
,
1377 const struct drm_display_mode
*mode
,
1378 struct drm_display_mode
*adjusted_mode
)
1383 static void mga_encoder_mode_set(struct drm_encoder
*encoder
,
1384 struct drm_display_mode
*mode
,
1385 struct drm_display_mode
*adjusted_mode
)
1390 static void mga_encoder_dpms(struct drm_encoder
*encoder
, int state
)
1395 static void mga_encoder_prepare(struct drm_encoder
*encoder
)
1399 static void mga_encoder_commit(struct drm_encoder
*encoder
)
1403 void mga_encoder_destroy(struct drm_encoder
*encoder
)
1405 struct mga_encoder
*mga_encoder
= to_mga_encoder(encoder
);
1406 drm_encoder_cleanup(encoder
);
1410 static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs
= {
1411 .dpms
= mga_encoder_dpms
,
1412 .mode_fixup
= mga_encoder_mode_fixup
,
1413 .mode_set
= mga_encoder_mode_set
,
1414 .prepare
= mga_encoder_prepare
,
1415 .commit
= mga_encoder_commit
,
1418 static const struct drm_encoder_funcs mga_encoder_encoder_funcs
= {
1419 .destroy
= mga_encoder_destroy
,
1422 static struct drm_encoder
*mga_encoder_init(struct drm_device
*dev
)
1424 struct drm_encoder
*encoder
;
1425 struct mga_encoder
*mga_encoder
;
1427 mga_encoder
= kzalloc(sizeof(struct mga_encoder
), GFP_KERNEL
);
1431 encoder
= &mga_encoder
->base
;
1432 encoder
->possible_crtcs
= 0x1;
1434 drm_encoder_init(dev
, encoder
, &mga_encoder_encoder_funcs
,
1435 DRM_MODE_ENCODER_DAC
);
1436 drm_encoder_helper_add(encoder
, &mga_encoder_helper_funcs
);
1442 static int mga_vga_get_modes(struct drm_connector
*connector
)
1444 struct mga_connector
*mga_connector
= to_mga_connector(connector
);
1448 edid
= drm_get_edid(connector
, &mga_connector
->i2c
->adapter
);
1450 drm_mode_connector_update_edid_property(connector
, edid
);
1451 ret
= drm_add_edid_modes(connector
, edid
);
1457 static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode
*mode
,
1460 uint32_t total_area
, divisor
;
1461 int64_t active_area
, pixels_per_second
, bandwidth
;
1462 uint64_t bytes_per_pixel
= (bits_per_pixel
+ 7) / 8;
1466 if (!mode
->htotal
|| !mode
->vtotal
|| !mode
->clock
)
1469 active_area
= mode
->hdisplay
* mode
->vdisplay
;
1470 total_area
= mode
->htotal
* mode
->vtotal
;
1472 pixels_per_second
= active_area
* mode
->clock
* 1000;
1473 do_div(pixels_per_second
, total_area
);
1475 bandwidth
= pixels_per_second
* bytes_per_pixel
* 100;
1476 do_div(bandwidth
, divisor
);
1478 return (uint32_t)(bandwidth
);
1481 #define MODE_BANDWIDTH MODE_BAD
1483 static int mga_vga_mode_valid(struct drm_connector
*connector
,
1484 struct drm_display_mode
*mode
)
1486 struct drm_device
*dev
= connector
->dev
;
1487 struct mga_device
*mdev
= (struct mga_device
*)dev
->dev_private
;
1488 struct mga_fbdev
*mfbdev
= mdev
->mfbdev
;
1489 struct drm_fb_helper
*fb_helper
= &mfbdev
->helper
;
1490 struct drm_fb_helper_connector
*fb_helper_conn
= NULL
;
1494 if (IS_G200_SE(mdev
)) {
1495 if (mdev
->unique_rev_id
== 0x01) {
1496 if (mode
->hdisplay
> 1600)
1497 return MODE_VIRTUAL_X
;
1498 if (mode
->vdisplay
> 1200)
1499 return MODE_VIRTUAL_Y
;
1500 if (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1502 return MODE_BANDWIDTH
;
1503 } else if (mdev
->unique_rev_id
>= 0x02) {
1504 if (mode
->hdisplay
> 1920)
1505 return MODE_VIRTUAL_X
;
1506 if (mode
->vdisplay
> 1200)
1507 return MODE_VIRTUAL_Y
;
1508 if (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1510 return MODE_BANDWIDTH
;
1512 } else if (mdev
->type
== G200_WB
) {
1513 if (mode
->hdisplay
> 1280)
1514 return MODE_VIRTUAL_X
;
1515 if (mode
->vdisplay
> 1024)
1516 return MODE_VIRTUAL_Y
;
1517 if (mga_vga_calculate_mode_bandwidth(mode
,
1518 bpp
> (31877 * 1024)))
1519 return MODE_BANDWIDTH
;
1520 } else if (mdev
->type
== G200_EV
&&
1521 (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1522 > (32700 * 1024))) {
1523 return MODE_BANDWIDTH
;
1524 } else if (mdev
->type
== G200_EH
&&
1525 (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1526 > (37500 * 1024))) {
1527 return MODE_BANDWIDTH
;
1528 } else if (mdev
->type
== G200_ER
&&
1529 (mga_vga_calculate_mode_bandwidth(mode
,
1530 bpp
) > (55000 * 1024))) {
1531 return MODE_BANDWIDTH
;
1534 if (mode
->crtc_hdisplay
> 2048 || mode
->crtc_hsync_start
> 4096 ||
1535 mode
->crtc_hsync_end
> 4096 || mode
->crtc_htotal
> 4096 ||
1536 mode
->crtc_vdisplay
> 2048 || mode
->crtc_vsync_start
> 4096 ||
1537 mode
->crtc_vsync_end
> 4096 || mode
->crtc_vtotal
> 4096) {
1541 /* Validate the mode input by the user */
1542 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1543 if (fb_helper
->connector_info
[i
]->connector
== connector
) {
1544 /* Found the helper for this connector */
1545 fb_helper_conn
= fb_helper
->connector_info
[i
];
1546 if (fb_helper_conn
->cmdline_mode
.specified
) {
1547 if (fb_helper_conn
->cmdline_mode
.bpp_specified
) {
1548 bpp
= fb_helper_conn
->cmdline_mode
.bpp
;
1554 if ((mode
->hdisplay
* mode
->vdisplay
* (bpp
/8)) > mdev
->mc
.vram_size
) {
1556 fb_helper_conn
->cmdline_mode
.specified
= false;
1563 struct drm_encoder
*mga_connector_best_encoder(struct drm_connector
1566 int enc_id
= connector
->encoder_ids
[0];
1567 struct drm_mode_object
*obj
;
1568 struct drm_encoder
*encoder
;
1570 /* pick the encoder ids */
1573 drm_mode_object_find(connector
->dev
, enc_id
,
1574 DRM_MODE_OBJECT_ENCODER
);
1577 encoder
= obj_to_encoder(obj
);
1583 static enum drm_connector_status
mga_vga_detect(struct drm_connector
1584 *connector
, bool force
)
1586 return connector_status_connected
;
1589 static void mga_connector_destroy(struct drm_connector
*connector
)
1591 struct mga_connector
*mga_connector
= to_mga_connector(connector
);
1592 mgag200_i2c_destroy(mga_connector
->i2c
);
1593 drm_connector_cleanup(connector
);
1597 struct drm_connector_helper_funcs mga_vga_connector_helper_funcs
= {
1598 .get_modes
= mga_vga_get_modes
,
1599 .mode_valid
= mga_vga_mode_valid
,
1600 .best_encoder
= mga_connector_best_encoder
,
1603 struct drm_connector_funcs mga_vga_connector_funcs
= {
1604 .dpms
= drm_helper_connector_dpms
,
1605 .detect
= mga_vga_detect
,
1606 .fill_modes
= drm_helper_probe_single_connector_modes
,
1607 .destroy
= mga_connector_destroy
,
1610 static struct drm_connector
*mga_vga_init(struct drm_device
*dev
)
1612 struct drm_connector
*connector
;
1613 struct mga_connector
*mga_connector
;
1615 mga_connector
= kzalloc(sizeof(struct mga_connector
), GFP_KERNEL
);
1619 connector
= &mga_connector
->base
;
1621 drm_connector_init(dev
, connector
,
1622 &mga_vga_connector_funcs
, DRM_MODE_CONNECTOR_VGA
);
1624 drm_connector_helper_add(connector
, &mga_vga_connector_helper_funcs
);
1626 drm_sysfs_connector_add(connector
);
1628 mga_connector
->i2c
= mgag200_i2c_create(dev
);
1629 if (!mga_connector
->i2c
)
1630 DRM_ERROR("failed to add ddc bus\n");
1636 int mgag200_modeset_init(struct mga_device
*mdev
)
1638 struct drm_encoder
*encoder
;
1639 struct drm_connector
*connector
;
1642 mdev
->mode_info
.mode_config_initialized
= true;
1644 mdev
->dev
->mode_config
.max_width
= MGAG200_MAX_FB_WIDTH
;
1645 mdev
->dev
->mode_config
.max_height
= MGAG200_MAX_FB_HEIGHT
;
1647 mdev
->dev
->mode_config
.fb_base
= mdev
->mc
.vram_base
;
1649 mga_crtc_init(mdev
);
1651 encoder
= mga_encoder_init(mdev
->dev
);
1653 DRM_ERROR("mga_encoder_init failed\n");
1657 connector
= mga_vga_init(mdev
->dev
);
1659 DRM_ERROR("mga_vga_init failed\n");
1663 drm_mode_connector_attach_encoder(connector
, encoder
);
1665 ret
= mgag200_fbdev_init(mdev
);
1667 DRM_ERROR("mga_fbdev_init failed\n");
1674 void mgag200_modeset_fini(struct mga_device
*mdev
)