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 mga_set_start_address(crtc
, (u32
)gpu_addr
);
773 static int mga_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
774 struct drm_framebuffer
*old_fb
)
776 return mga_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
779 static int mga_crtc_mode_set(struct drm_crtc
*crtc
,
780 struct drm_display_mode
*mode
,
781 struct drm_display_mode
*adjusted_mode
,
782 int x
, int y
, struct drm_framebuffer
*old_fb
)
784 struct drm_device
*dev
= crtc
->dev
;
785 struct mga_device
*mdev
= dev
->dev_private
;
786 int hdisplay
, hsyncstart
, hsyncend
, htotal
;
787 int vdisplay
, vsyncstart
, vsyncend
, vtotal
;
789 int option
= 0, option2
= 0;
791 unsigned char misc
= 0;
792 unsigned char ext_vga
[6];
795 static unsigned char dacvalue
[] = {
796 /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
797 /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
798 /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
799 /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
800 /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
801 /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
802 /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
803 /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
804 /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
805 /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
808 bppshift
= mdev
->bpp_shifts
[(crtc
->fb
->bits_per_pixel
>> 3) - 1];
810 switch (mdev
->type
) {
813 dacvalue
[MGA1064_VREF_CTL
] = 0x03;
814 dacvalue
[MGA1064_PIX_CLK_CTL
] = MGA1064_PIX_CLK_CTL_SEL_PLL
;
815 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_DAC_EN
|
816 MGA1064_MISC_CTL_VGA8
|
817 MGA1064_MISC_CTL_DAC_RAM_CS
;
822 option2
= 0x00008000;
825 dacvalue
[MGA1064_VREF_CTL
] = 0x07;
827 option2
= 0x0000b000;
830 dacvalue
[MGA1064_PIX_CLK_CTL
] = MGA1064_PIX_CLK_CTL_SEL_PLL
;
831 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_VGA8
|
832 MGA1064_MISC_CTL_DAC_RAM_CS
;
834 option2
= 0x0000b000;
837 dacvalue
[MGA1064_MISC_CTL
] = MGA1064_MISC_CTL_VGA8
|
838 MGA1064_MISC_CTL_DAC_RAM_CS
;
840 option2
= 0x0000b000;
846 switch (crtc
->fb
->bits_per_pixel
) {
848 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_8bits
;
851 if (crtc
->fb
->depth
== 15)
852 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_15bits
;
854 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_16bits
;
857 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_24bits
;
860 dacvalue
[MGA1064_MUL_CTL
] = MGA1064_MUL_CTL_32_24bits
;
864 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
866 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
870 for (i
= 0; i
< sizeof(dacvalue
); i
++) {
874 ((i
>= 0x1f) && (i
<= 0x29)) ||
875 ((i
>= 0x30) && (i
<= 0x37)))
877 if (IS_G200_SE(mdev
) &&
878 ((i
== 0x2c) || (i
== 0x2d) || (i
== 0x2e)))
880 if ((mdev
->type
== G200_EV
|| mdev
->type
== G200_WB
|| mdev
->type
== G200_EH
) &&
881 (i
>= 0x44) && (i
<= 0x4e))
884 WREG_DAC(i
, dacvalue
[i
]);
887 if (mdev
->type
== G200_ER
)
891 pci_write_config_dword(dev
->pdev
, PCI_MGA_OPTION
, option
);
893 pci_write_config_dword(dev
->pdev
, PCI_MGA_OPTION2
, option2
);
899 pitch
= crtc
->fb
->pitches
[0] / (crtc
->fb
->bits_per_pixel
/ 8);
900 if (crtc
->fb
->bits_per_pixel
== 24)
901 pitch
= (pitch
* 3) >> (4 - bppshift
);
903 pitch
= pitch
>> (4 - bppshift
);
905 hdisplay
= mode
->hdisplay
/ 8 - 1;
906 hsyncstart
= mode
->hsync_start
/ 8 - 1;
907 hsyncend
= mode
->hsync_end
/ 8 - 1;
908 htotal
= mode
->htotal
/ 8 - 1;
910 /* Work around hardware quirk */
911 if ((htotal
& 0x07) == 0x06 || (htotal
& 0x07) == 0x04)
914 vdisplay
= mode
->vdisplay
- 1;
915 vsyncstart
= mode
->vsync_start
- 1;
916 vsyncend
= mode
->vsync_end
- 1;
917 vtotal
= mode
->vtotal
- 2;
929 WREG_CRT(0, htotal
- 4);
930 WREG_CRT(1, hdisplay
);
931 WREG_CRT(2, hdisplay
);
932 WREG_CRT(3, (htotal
& 0x1F) | 0x80);
933 WREG_CRT(4, hsyncstart
);
934 WREG_CRT(5, ((htotal
& 0x20) << 2) | (hsyncend
& 0x1F));
935 WREG_CRT(6, vtotal
& 0xFF);
936 WREG_CRT(7, ((vtotal
& 0x100) >> 8) |
937 ((vdisplay
& 0x100) >> 7) |
938 ((vsyncstart
& 0x100) >> 6) |
939 ((vdisplay
& 0x100) >> 5) |
940 ((vdisplay
& 0x100) >> 4) | /* linecomp */
941 ((vtotal
& 0x200) >> 4)|
942 ((vdisplay
& 0x200) >> 3) |
943 ((vsyncstart
& 0x200) >> 2));
944 WREG_CRT(9, ((vdisplay
& 0x200) >> 4) |
945 ((vdisplay
& 0x200) >> 3));
952 WREG_CRT(16, vsyncstart
& 0xFF);
953 WREG_CRT(17, (vsyncend
& 0x0F) | 0x20);
954 WREG_CRT(18, vdisplay
& 0xFF);
955 WREG_CRT(19, pitch
& 0xFF);
957 WREG_CRT(21, vdisplay
& 0xFF);
958 WREG_CRT(22, (vtotal
+ 1) & 0xFF);
960 WREG_CRT(24, vdisplay
& 0xFF);
967 ext_vga
[0] |= (pitch
& 0x300) >> 4;
968 ext_vga
[1] = (((htotal
- 4) & 0x100) >> 8) |
969 ((hdisplay
& 0x100) >> 7) |
970 ((hsyncstart
& 0x100) >> 6) |
972 ext_vga
[2] = ((vtotal
& 0xc00) >> 10) |
973 ((vdisplay
& 0x400) >> 8) |
974 ((vdisplay
& 0xc00) >> 7) |
975 ((vsyncstart
& 0xc00) >> 5) |
976 ((vdisplay
& 0x400) >> 3);
977 if (crtc
->fb
->bits_per_pixel
== 24)
978 ext_vga
[3] = (((1 << bppshift
) * 3) - 1) | 0x80;
980 ext_vga
[3] = ((1 << bppshift
) - 1) | 0x80;
982 if (mdev
->type
== G200_WB
)
985 /* Set pixel clocks */
987 WREG8(MGA_MISC_OUT
, misc
);
989 mga_crtc_set_plls(mdev
, mode
->clock
);
991 for (i
= 0; i
< 6; i
++) {
992 WREG_ECRT(i
, ext_vga
[i
]);
995 if (mdev
->type
== G200_ER
)
996 WREG_ECRT(0x24, 0x5);
998 if (mdev
->type
== G200_EV
) {
1002 WREG_ECRT(0, ext_vga
[0]);
1003 /* Enable mga pixel clock */
1006 WREG8(MGA_MISC_OUT
, misc
);
1009 memcpy(&mdev
->mode
, mode
, sizeof(struct drm_display_mode
));
1011 mga_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
1014 if (mdev
->type
== G200_ER
) {
1015 u32 mem_ctl
= RREG32(MGAREG_MEMCTL
);
1019 WREG8(MGAREG_SEQ_INDEX
, 0x01);
1020 seq1
= RREG8(MGAREG_SEQ_DATA
) | 0x20;
1021 WREG8(MGAREG_SEQ_DATA
, seq1
);
1023 WREG32(MGAREG_MEMCTL
, mem_ctl
| 0x00200000);
1025 WREG32(MGAREG_MEMCTL
, mem_ctl
& ~0x00200000);
1027 WREG8(MGAREG_SEQ_DATA
, seq1
& ~0x20);
1031 if (IS_G200_SE(mdev
)) {
1032 if (mdev
->unique_rev_id
>= 0x02) {
1037 if (crtc
->fb
->bits_per_pixel
> 16)
1039 else if (crtc
->fb
->bits_per_pixel
> 8)
1044 mb
= (mode
->clock
* bpp
) / 1000;
1058 WREG8(MGAREG_CRTCEXT_INDEX
, 0x06);
1059 WREG8(MGAREG_CRTCEXT_DATA
, hi_pri_lvl
);
1061 WREG8(MGAREG_CRTCEXT_INDEX
, 0x06);
1062 if (mdev
->unique_rev_id
>= 0x01)
1063 WREG8(MGAREG_CRTCEXT_DATA
, 0x03);
1065 WREG8(MGAREG_CRTCEXT_DATA
, 0x04);
1071 #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1072 static int mga_suspend(struct drm_crtc
*crtc
)
1074 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1075 struct drm_device
*dev
= crtc
->dev
;
1076 struct mga_device
*mdev
= dev
->dev_private
;
1077 struct pci_dev
*pdev
= dev
->pdev
;
1080 if (mdev
->suspended
)
1085 /* Disable the pixel clock */
1086 WREG_DAC(0x1a, 0x05);
1087 /* Power down the DAC */
1088 WREG_DAC(0x1e, 0x18);
1089 /* Power down the pixel PLL */
1090 WREG_DAC(0x1a, 0x0d);
1092 /* Disable PLLs and clocks */
1093 pci_read_config_dword(pdev
, PCI_MGA_OPTION
, &option
);
1094 option
&= ~(0x1F8024);
1095 pci_write_config_dword(pdev
, PCI_MGA_OPTION
, option
);
1096 pci_set_power_state(pdev
, PCI_D3hot
);
1097 pci_disable_device(pdev
);
1099 mdev
->suspended
= true;
1104 static int mga_resume(struct drm_crtc
*crtc
)
1106 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1107 struct drm_device
*dev
= crtc
->dev
;
1108 struct mga_device
*mdev
= dev
->dev_private
;
1109 struct pci_dev
*pdev
= dev
->pdev
;
1112 if (!mdev
->suspended
)
1115 pci_set_power_state(pdev
, PCI_D0
);
1116 pci_enable_device(pdev
);
1118 /* Disable sysclk */
1119 pci_read_config_dword(pdev
, PCI_MGA_OPTION
, &option
);
1121 pci_write_config_dword(pdev
, PCI_MGA_OPTION
, option
);
1123 mdev
->suspended
= false;
1130 static void mga_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
1132 struct drm_device
*dev
= crtc
->dev
;
1133 struct mga_device
*mdev
= dev
->dev_private
;
1134 u8 seq1
= 0, crtcext1
= 0;
1137 case DRM_MODE_DPMS_ON
:
1140 mga_crtc_load_lut(crtc
);
1142 case DRM_MODE_DPMS_STANDBY
:
1146 case DRM_MODE_DPMS_SUSPEND
:
1150 case DRM_MODE_DPMS_OFF
:
1157 if (mode
== DRM_MODE_DPMS_OFF
) {
1161 WREG8(MGAREG_SEQ_INDEX
, 0x01);
1162 seq1
|= RREG8(MGAREG_SEQ_DATA
) & ~0x20;
1163 mga_wait_vsync(mdev
);
1164 mga_wait_busy(mdev
);
1165 WREG8(MGAREG_SEQ_DATA
, seq1
);
1167 WREG8(MGAREG_CRTCEXT_INDEX
, 0x01);
1168 crtcext1
|= RREG8(MGAREG_CRTCEXT_DATA
) & ~0x30;
1169 WREG8(MGAREG_CRTCEXT_DATA
, crtcext1
);
1172 if (mode
== DRM_MODE_DPMS_ON
&& mdev
->suspended
== true) {
1174 drm_helper_resume_force_mode(dev
);
1180 * This is called before a mode is programmed. A typical use might be to
1181 * enable DPMS during the programming to avoid seeing intermediate stages,
1182 * but that's not relevant to us
1184 static void mga_crtc_prepare(struct drm_crtc
*crtc
)
1186 struct drm_device
*dev
= crtc
->dev
;
1187 struct mga_device
*mdev
= dev
->dev_private
;
1190 /* mga_resume(crtc);*/
1192 WREG8(MGAREG_CRTC_INDEX
, 0x11);
1193 tmp
= RREG8(MGAREG_CRTC_DATA
);
1194 WREG_CRT(0x11, tmp
| 0x80);
1196 if (mdev
->type
== G200_SE_A
|| mdev
->type
== G200_SE_B
) {
1202 WREG8(MGAREG_SEQ_INDEX
, 0x1);
1203 tmp
= RREG8(MGAREG_SEQ_DATA
);
1205 /* start sync reset */
1207 WREG_SEQ(1, tmp
| 0x20);
1210 if (mdev
->type
== G200_WB
)
1211 mga_g200wb_prepare(crtc
);
1217 * This is called after a mode is programmed. It should reverse anything done
1218 * by the prepare function
1220 static void mga_crtc_commit(struct drm_crtc
*crtc
)
1222 struct drm_device
*dev
= crtc
->dev
;
1223 struct mga_device
*mdev
= dev
->dev_private
;
1224 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
1227 if (mdev
->type
== G200_WB
)
1228 mga_g200wb_commit(crtc
);
1230 if (mdev
->type
== G200_SE_A
|| mdev
->type
== G200_SE_B
) {
1236 WREG8(MGAREG_SEQ_INDEX
, 0x1);
1237 tmp
= RREG8(MGAREG_SEQ_DATA
);
1243 crtc_funcs
->dpms(crtc
, DRM_MODE_DPMS_ON
);
1247 * The core can pass us a set of gamma values to program. We actually only
1248 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1249 * but it's a requirement that we provide the function
1251 static void mga_crtc_gamma_set(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
1252 u16
*blue
, uint32_t start
, uint32_t size
)
1254 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1255 int end
= (start
+ size
> MGAG200_LUT_SIZE
) ? MGAG200_LUT_SIZE
: start
+ size
;
1258 for (i
= start
; i
< end
; i
++) {
1259 mga_crtc
->lut_r
[i
] = red
[i
] >> 8;
1260 mga_crtc
->lut_g
[i
] = green
[i
] >> 8;
1261 mga_crtc
->lut_b
[i
] = blue
[i
] >> 8;
1263 mga_crtc_load_lut(crtc
);
1266 /* Simple cleanup function */
1267 static void mga_crtc_destroy(struct drm_crtc
*crtc
)
1269 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1271 drm_crtc_cleanup(crtc
);
1275 static void mga_crtc_disable(struct drm_crtc
*crtc
)
1278 DRM_DEBUG_KMS("\n");
1279 mga_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
1281 struct mga_framebuffer
*mga_fb
= to_mga_framebuffer(crtc
->fb
);
1282 struct drm_gem_object
*obj
= mga_fb
->obj
;
1283 struct mgag200_bo
*bo
= gem_to_mga_bo(obj
);
1284 ret
= mgag200_bo_reserve(bo
, false);
1287 mgag200_bo_push_sysram(bo
);
1288 mgag200_bo_unreserve(bo
);
1293 /* These provide the minimum set of functions required to handle a CRTC */
1294 static const struct drm_crtc_funcs mga_crtc_funcs
= {
1295 .cursor_set
= mga_crtc_cursor_set
,
1296 .cursor_move
= mga_crtc_cursor_move
,
1297 .gamma_set
= mga_crtc_gamma_set
,
1298 .set_config
= drm_crtc_helper_set_config
,
1299 .destroy
= mga_crtc_destroy
,
1302 static const struct drm_crtc_helper_funcs mga_helper_funcs
= {
1303 .disable
= mga_crtc_disable
,
1304 .dpms
= mga_crtc_dpms
,
1305 .mode_fixup
= mga_crtc_mode_fixup
,
1306 .mode_set
= mga_crtc_mode_set
,
1307 .mode_set_base
= mga_crtc_mode_set_base
,
1308 .prepare
= mga_crtc_prepare
,
1309 .commit
= mga_crtc_commit
,
1310 .load_lut
= mga_crtc_load_lut
,
1314 static void mga_crtc_init(struct mga_device
*mdev
)
1316 struct mga_crtc
*mga_crtc
;
1319 mga_crtc
= kzalloc(sizeof(struct mga_crtc
) +
1320 (MGAG200FB_CONN_LIMIT
* sizeof(struct drm_connector
*)),
1323 if (mga_crtc
== NULL
)
1326 drm_crtc_init(mdev
->dev
, &mga_crtc
->base
, &mga_crtc_funcs
);
1328 drm_mode_crtc_set_gamma_size(&mga_crtc
->base
, MGAG200_LUT_SIZE
);
1329 mdev
->mode_info
.crtc
= mga_crtc
;
1331 for (i
= 0; i
< MGAG200_LUT_SIZE
; i
++) {
1332 mga_crtc
->lut_r
[i
] = i
;
1333 mga_crtc
->lut_g
[i
] = i
;
1334 mga_crtc
->lut_b
[i
] = i
;
1337 drm_crtc_helper_add(&mga_crtc
->base
, &mga_helper_funcs
);
1340 /** Sets the color ramps on behalf of fbcon */
1341 void mga_crtc_fb_gamma_set(struct drm_crtc
*crtc
, u16 red
, u16 green
,
1342 u16 blue
, int regno
)
1344 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1346 mga_crtc
->lut_r
[regno
] = red
>> 8;
1347 mga_crtc
->lut_g
[regno
] = green
>> 8;
1348 mga_crtc
->lut_b
[regno
] = blue
>> 8;
1351 /** Gets the color ramps on behalf of fbcon */
1352 void mga_crtc_fb_gamma_get(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
1353 u16
*blue
, int regno
)
1355 struct mga_crtc
*mga_crtc
= to_mga_crtc(crtc
);
1357 *red
= (u16
)mga_crtc
->lut_r
[regno
] << 8;
1358 *green
= (u16
)mga_crtc
->lut_g
[regno
] << 8;
1359 *blue
= (u16
)mga_crtc
->lut_b
[regno
] << 8;
1363 * The encoder comes after the CRTC in the output pipeline, but before
1364 * the connector. It's responsible for ensuring that the digital
1365 * stream is appropriately converted into the output format. Setup is
1366 * very simple in this case - all we have to do is inform qemu of the
1367 * colour depth in order to ensure that it displays appropriately
1371 * These functions are analagous to those in the CRTC code, but are intended
1372 * to handle any encoder-specific limitations
1374 static bool mga_encoder_mode_fixup(struct drm_encoder
*encoder
,
1375 const struct drm_display_mode
*mode
,
1376 struct drm_display_mode
*adjusted_mode
)
1381 static void mga_encoder_mode_set(struct drm_encoder
*encoder
,
1382 struct drm_display_mode
*mode
,
1383 struct drm_display_mode
*adjusted_mode
)
1388 static void mga_encoder_dpms(struct drm_encoder
*encoder
, int state
)
1393 static void mga_encoder_prepare(struct drm_encoder
*encoder
)
1397 static void mga_encoder_commit(struct drm_encoder
*encoder
)
1401 void mga_encoder_destroy(struct drm_encoder
*encoder
)
1403 struct mga_encoder
*mga_encoder
= to_mga_encoder(encoder
);
1404 drm_encoder_cleanup(encoder
);
1408 static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs
= {
1409 .dpms
= mga_encoder_dpms
,
1410 .mode_fixup
= mga_encoder_mode_fixup
,
1411 .mode_set
= mga_encoder_mode_set
,
1412 .prepare
= mga_encoder_prepare
,
1413 .commit
= mga_encoder_commit
,
1416 static const struct drm_encoder_funcs mga_encoder_encoder_funcs
= {
1417 .destroy
= mga_encoder_destroy
,
1420 static struct drm_encoder
*mga_encoder_init(struct drm_device
*dev
)
1422 struct drm_encoder
*encoder
;
1423 struct mga_encoder
*mga_encoder
;
1425 mga_encoder
= kzalloc(sizeof(struct mga_encoder
), GFP_KERNEL
);
1429 encoder
= &mga_encoder
->base
;
1430 encoder
->possible_crtcs
= 0x1;
1432 drm_encoder_init(dev
, encoder
, &mga_encoder_encoder_funcs
,
1433 DRM_MODE_ENCODER_DAC
);
1434 drm_encoder_helper_add(encoder
, &mga_encoder_helper_funcs
);
1440 static int mga_vga_get_modes(struct drm_connector
*connector
)
1442 struct mga_connector
*mga_connector
= to_mga_connector(connector
);
1446 edid
= drm_get_edid(connector
, &mga_connector
->i2c
->adapter
);
1448 drm_mode_connector_update_edid_property(connector
, edid
);
1449 ret
= drm_add_edid_modes(connector
, edid
);
1455 static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode
*mode
,
1458 uint32_t total_area
, divisor
;
1459 int64_t active_area
, pixels_per_second
, bandwidth
;
1460 uint64_t bytes_per_pixel
= (bits_per_pixel
+ 7) / 8;
1464 if (!mode
->htotal
|| !mode
->vtotal
|| !mode
->clock
)
1467 active_area
= mode
->hdisplay
* mode
->vdisplay
;
1468 total_area
= mode
->htotal
* mode
->vtotal
;
1470 pixels_per_second
= active_area
* mode
->clock
* 1000;
1471 do_div(pixels_per_second
, total_area
);
1473 bandwidth
= pixels_per_second
* bytes_per_pixel
* 100;
1474 do_div(bandwidth
, divisor
);
1476 return (uint32_t)(bandwidth
);
1479 #define MODE_BANDWIDTH MODE_BAD
1481 static int mga_vga_mode_valid(struct drm_connector
*connector
,
1482 struct drm_display_mode
*mode
)
1484 struct drm_device
*dev
= connector
->dev
;
1485 struct mga_device
*mdev
= (struct mga_device
*)dev
->dev_private
;
1486 struct mga_fbdev
*mfbdev
= mdev
->mfbdev
;
1487 struct drm_fb_helper
*fb_helper
= &mfbdev
->helper
;
1488 struct drm_fb_helper_connector
*fb_helper_conn
= NULL
;
1492 if (IS_G200_SE(mdev
)) {
1493 if (mdev
->unique_rev_id
== 0x01) {
1494 if (mode
->hdisplay
> 1600)
1495 return MODE_VIRTUAL_X
;
1496 if (mode
->vdisplay
> 1200)
1497 return MODE_VIRTUAL_Y
;
1498 if (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1500 return MODE_BANDWIDTH
;
1501 } else if (mdev
->unique_rev_id
>= 0x02) {
1502 if (mode
->hdisplay
> 1920)
1503 return MODE_VIRTUAL_X
;
1504 if (mode
->vdisplay
> 1200)
1505 return MODE_VIRTUAL_Y
;
1506 if (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1508 return MODE_BANDWIDTH
;
1510 } else if (mdev
->type
== G200_WB
) {
1511 if (mode
->hdisplay
> 1280)
1512 return MODE_VIRTUAL_X
;
1513 if (mode
->vdisplay
> 1024)
1514 return MODE_VIRTUAL_Y
;
1515 if (mga_vga_calculate_mode_bandwidth(mode
,
1516 bpp
> (31877 * 1024)))
1517 return MODE_BANDWIDTH
;
1518 } else if (mdev
->type
== G200_EV
&&
1519 (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1520 > (32700 * 1024))) {
1521 return MODE_BANDWIDTH
;
1522 } else if (mode
->type
== G200_EH
&&
1523 (mga_vga_calculate_mode_bandwidth(mode
, bpp
)
1524 > (37500 * 1024))) {
1525 return MODE_BANDWIDTH
;
1526 } else if (mode
->type
== G200_ER
&&
1527 (mga_vga_calculate_mode_bandwidth(mode
,
1528 bpp
) > (55000 * 1024))) {
1529 return MODE_BANDWIDTH
;
1532 if (mode
->crtc_hdisplay
> 2048 || mode
->crtc_hsync_start
> 4096 ||
1533 mode
->crtc_hsync_end
> 4096 || mode
->crtc_htotal
> 4096 ||
1534 mode
->crtc_vdisplay
> 2048 || mode
->crtc_vsync_start
> 4096 ||
1535 mode
->crtc_vsync_end
> 4096 || mode
->crtc_vtotal
> 4096) {
1539 /* Validate the mode input by the user */
1540 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1541 if (fb_helper
->connector_info
[i
]->connector
== connector
) {
1542 /* Found the helper for this connector */
1543 fb_helper_conn
= fb_helper
->connector_info
[i
];
1544 if (fb_helper_conn
->cmdline_mode
.specified
) {
1545 if (fb_helper_conn
->cmdline_mode
.bpp_specified
) {
1546 bpp
= fb_helper_conn
->cmdline_mode
.bpp
;
1552 if ((mode
->hdisplay
* mode
->vdisplay
* (bpp
/8)) > mdev
->mc
.vram_size
) {
1554 fb_helper_conn
->cmdline_mode
.specified
= false;
1561 struct drm_encoder
*mga_connector_best_encoder(struct drm_connector
1564 int enc_id
= connector
->encoder_ids
[0];
1565 struct drm_mode_object
*obj
;
1566 struct drm_encoder
*encoder
;
1568 /* pick the encoder ids */
1571 drm_mode_object_find(connector
->dev
, enc_id
,
1572 DRM_MODE_OBJECT_ENCODER
);
1575 encoder
= obj_to_encoder(obj
);
1581 static enum drm_connector_status
mga_vga_detect(struct drm_connector
1582 *connector
, bool force
)
1584 return connector_status_connected
;
1587 static void mga_connector_destroy(struct drm_connector
*connector
)
1589 struct mga_connector
*mga_connector
= to_mga_connector(connector
);
1590 mgag200_i2c_destroy(mga_connector
->i2c
);
1591 drm_connector_cleanup(connector
);
1595 struct drm_connector_helper_funcs mga_vga_connector_helper_funcs
= {
1596 .get_modes
= mga_vga_get_modes
,
1597 .mode_valid
= mga_vga_mode_valid
,
1598 .best_encoder
= mga_connector_best_encoder
,
1601 struct drm_connector_funcs mga_vga_connector_funcs
= {
1602 .dpms
= drm_helper_connector_dpms
,
1603 .detect
= mga_vga_detect
,
1604 .fill_modes
= drm_helper_probe_single_connector_modes
,
1605 .destroy
= mga_connector_destroy
,
1608 static struct drm_connector
*mga_vga_init(struct drm_device
*dev
)
1610 struct drm_connector
*connector
;
1611 struct mga_connector
*mga_connector
;
1613 mga_connector
= kzalloc(sizeof(struct mga_connector
), GFP_KERNEL
);
1617 connector
= &mga_connector
->base
;
1619 drm_connector_init(dev
, connector
,
1620 &mga_vga_connector_funcs
, DRM_MODE_CONNECTOR_VGA
);
1622 drm_connector_helper_add(connector
, &mga_vga_connector_helper_funcs
);
1624 drm_sysfs_connector_add(connector
);
1626 mga_connector
->i2c
= mgag200_i2c_create(dev
);
1627 if (!mga_connector
->i2c
)
1628 DRM_ERROR("failed to add ddc bus\n");
1634 int mgag200_modeset_init(struct mga_device
*mdev
)
1636 struct drm_encoder
*encoder
;
1637 struct drm_connector
*connector
;
1640 mdev
->mode_info
.mode_config_initialized
= true;
1642 mdev
->dev
->mode_config
.max_width
= MGAG200_MAX_FB_WIDTH
;
1643 mdev
->dev
->mode_config
.max_height
= MGAG200_MAX_FB_HEIGHT
;
1645 mdev
->dev
->mode_config
.fb_base
= mdev
->mc
.vram_base
;
1647 mga_crtc_init(mdev
);
1649 encoder
= mga_encoder_init(mdev
->dev
);
1651 DRM_ERROR("mga_encoder_init failed\n");
1655 connector
= mga_vga_init(mdev
->dev
);
1657 DRM_ERROR("mga_vga_init failed\n");
1661 drm_mode_connector_attach_encoder(connector
, encoder
);
1663 ret
= mgag200_fbdev_init(mdev
);
1665 DRM_ERROR("mga_fbdev_init failed\n");
1672 void mgag200_modeset_fini(struct mga_device
*mdev
)