blob: 34dd38e8a65126d1df6fca43ea134bc811a6285e [file] [log] [blame]
Dave Airlie414c4532012-04-17 15:01:25 +01001/*
2 * Copyright 2010 Matt Turner.
3 * Copyright 2012 Red Hat
4 *
5 * This file is subject to the terms and conditions of the GNU General
6 * Public License version 2. See the file COPYING in the main
7 * directory of this archive for more details.
8 *
9 * Authors: Matthew Garrett
10 * Matt Turner
11 * Dave Airlie
12 */
13
14#include <linux/delay.h>
15
David Howells760285e2012-10-02 18:01:07 +010016#include <drm/drmP.h>
17#include <drm/drm_crtc_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010018#include <drm/drm_plane_helper.h>
Dave Airlie414c4532012-04-17 15:01:25 +010019
20#include "mgag200_drv.h"
21
22#define MGAG200_LUT_SIZE 256
23
24/*
25 * This file contains setup code for the CRTC.
26 */
27
28static void mga_crtc_load_lut(struct drm_crtc *crtc)
29{
30 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
31 struct drm_device *dev = crtc->dev;
32 struct mga_device *mdev = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070033 struct drm_framebuffer *fb = crtc->primary->fb;
Dave Airlie414c4532012-04-17 15:01:25 +010034 int i;
35
36 if (!crtc->enabled)
37 return;
38
39 WREG8(DAC_INDEX + MGA1064_INDEX, 0);
40
Egbert Eichde7500e2013-07-17 15:07:27 +020041 if (fb && fb->bits_per_pixel == 16) {
42 int inc = (fb->depth == 15) ? 8 : 4;
43 u8 r, b;
44 for (i = 0; i < MGAG200_LUT_SIZE; i += inc) {
45 if (fb->depth == 16) {
46 if (i > (MGAG200_LUT_SIZE >> 1)) {
47 r = b = 0;
48 } else {
49 r = mga_crtc->lut_r[i << 1];
50 b = mga_crtc->lut_b[i << 1];
51 }
52 } else {
53 r = mga_crtc->lut_r[i];
54 b = mga_crtc->lut_b[i];
55 }
56 /* VGA registers */
57 WREG8(DAC_INDEX + MGA1064_COL_PAL, r);
58 WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
59 WREG8(DAC_INDEX + MGA1064_COL_PAL, b);
60 }
61 return;
62 }
Dave Airlie414c4532012-04-17 15:01:25 +010063 for (i = 0; i < MGAG200_LUT_SIZE; i++) {
64 /* VGA registers */
65 WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_r[i]);
66 WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
67 WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_b[i]);
68 }
69}
70
71static inline void mga_wait_vsync(struct mga_device *mdev)
72{
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000073 unsigned long timeout = jiffies + HZ/10;
Dave Airlie414c4532012-04-17 15:01:25 +010074 unsigned int status = 0;
75
76 do {
77 status = RREG32(MGAREG_Status);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000078 } while ((status & 0x08) && time_before(jiffies, timeout));
79 timeout = jiffies + HZ/10;
Dave Airlie414c4532012-04-17 15:01:25 +010080 status = 0;
81 do {
82 status = RREG32(MGAREG_Status);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000083 } while (!(status & 0x08) && time_before(jiffies, timeout));
Dave Airlie414c4532012-04-17 15:01:25 +010084}
85
86static inline void mga_wait_busy(struct mga_device *mdev)
87{
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000088 unsigned long timeout = jiffies + HZ;
Dave Airlie414c4532012-04-17 15:01:25 +010089 unsigned int status = 0;
90 do {
91 status = RREG8(MGAREG_Status + 2);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000092 } while ((status & 0x01) && time_before(jiffies, timeout));
Dave Airlie414c4532012-04-17 15:01:25 +010093}
94
Mathieu Larouchee829d7e2015-08-21 09:24:13 -040095#define P_ARRAY_SIZE 9
96
Dave Airlie414c4532012-04-17 15:01:25 +010097static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
98{
99 unsigned int vcomax, vcomin, pllreffreq;
100 unsigned int delta, tmpdelta, permitteddelta;
101 unsigned int testp, testm, testn;
102 unsigned int p, m, n;
103 unsigned int computed;
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400104 unsigned int pvalues_e4[P_ARRAY_SIZE] = {16, 14, 12, 10, 8, 6, 4, 2, 1};
105 unsigned int fvv;
106 unsigned int i;
Dave Airlie414c4532012-04-17 15:01:25 +0100107
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400108 if (mdev->unique_rev_id <= 0x03) {
Dave Airlie414c4532012-04-17 15:01:25 +0100109
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400110 m = n = p = 0;
111 vcomax = 320000;
112 vcomin = 160000;
113 pllreffreq = 25000;
Dave Airlie414c4532012-04-17 15:01:25 +0100114
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400115 delta = 0xffffffff;
116 permitteddelta = clock * 5 / 1000;
Dave Airlie414c4532012-04-17 15:01:25 +0100117
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400118 for (testp = 8; testp > 0; testp /= 2) {
119 if (clock * testp > vcomax)
120 continue;
121 if (clock * testp < vcomin)
122 continue;
123
124 for (testn = 17; testn < 256; testn++) {
125 for (testm = 1; testm < 32; testm++) {
126 computed = (pllreffreq * testn) /
127 (testm * testp);
128 if (computed > clock)
129 tmpdelta = computed - clock;
130 else
131 tmpdelta = clock - computed;
132 if (tmpdelta < delta) {
133 delta = tmpdelta;
134 m = testm - 1;
135 n = testn - 1;
136 p = testp - 1;
137 }
Dave Airlie414c4532012-04-17 15:01:25 +0100138 }
139 }
140 }
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400141 } else {
142
143
144 m = n = p = 0;
145 vcomax = 1600000;
146 vcomin = 800000;
147 pllreffreq = 25000;
148
149 if (clock < 25000)
150 clock = 25000;
151
152 clock = clock * 2;
153
154 delta = 0xFFFFFFFF;
155 /* Permited delta is 0.5% as VESA Specification */
156 permitteddelta = clock * 5 / 1000;
157
158 for (i = 0 ; i < P_ARRAY_SIZE ; i++) {
159 testp = pvalues_e4[i];
160
161 if ((clock * testp) > vcomax)
162 continue;
163 if ((clock * testp) < vcomin)
164 continue;
165
166 for (testn = 50; testn <= 256; testn++) {
167 for (testm = 1; testm <= 32; testm++) {
168 computed = (pllreffreq * testn) /
169 (testm * testp);
170 if (computed > clock)
171 tmpdelta = computed - clock;
172 else
173 tmpdelta = clock - computed;
174
175 if (tmpdelta < delta) {
176 delta = tmpdelta;
177 m = testm - 1;
178 n = testn - 1;
179 p = testp - 1;
180 }
181 }
182 }
183 }
184
Mathieu Larouched3922b62016-05-27 15:12:50 -0400185 fvv = pllreffreq * (n + 1) / (m + 1);
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400186 fvv = (fvv - 800000) / 50000;
187
188 if (fvv > 15)
189 fvv = 15;
190
191 p |= (fvv << 4);
192 m |= 0x80;
193
194 clock = clock / 2;
Dave Airlie414c4532012-04-17 15:01:25 +0100195 }
196
197 if (delta > permitteddelta) {
198 printk(KERN_WARNING "PLL delta too large\n");
199 return 1;
200 }
201
202 WREG_DAC(MGA1064_PIX_PLLC_M, m);
203 WREG_DAC(MGA1064_PIX_PLLC_N, n);
204 WREG_DAC(MGA1064_PIX_PLLC_P, p);
Mathieu Larouched3922b62016-05-27 15:12:50 -0400205
206 if (mdev->unique_rev_id >= 0x04) {
207 WREG_DAC(0x1a, 0x09);
208 msleep(20);
209 WREG_DAC(0x1a, 0x01);
210
211 }
212
Dave Airlie414c4532012-04-17 15:01:25 +0100213 return 0;
214}
215
216static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
217{
218 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530219 unsigned int delta, tmpdelta;
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400220 unsigned int testp, testm, testn, testp2;
Dave Airlie414c4532012-04-17 15:01:25 +0100221 unsigned int p, m, n;
222 unsigned int computed;
223 int i, j, tmpcount, vcount;
224 bool pll_locked = false;
225 u8 tmp;
226
227 m = n = p = 0;
Dave Airlie414c4532012-04-17 15:01:25 +0100228
229 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100230
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400231 if (mdev->type == G200_EW3) {
Dave Airlie414c4532012-04-17 15:01:25 +0100232
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400233 vcomax = 800000;
234 vcomin = 400000;
235 pllreffreq = 25000;
236
237 for (testp = 1; testp < 8; testp++) {
238 for (testp2 = 1; testp2 < 8; testp2++) {
239 if (testp < testp2)
240 continue;
241 if ((clock * testp * testp2) > vcomax)
242 continue;
243 if ((clock * testp * testp2) < vcomin)
244 continue;
245 for (testm = 1; testm < 26; testm++) {
246 for (testn = 32; testn < 2048 ; testn++) {
247 computed = (pllreffreq * testn) /
248 (testm * testp * testp2);
249 if (computed > clock)
250 tmpdelta = computed - clock;
251 else
252 tmpdelta = clock - computed;
253 if (tmpdelta < delta) {
254 delta = tmpdelta;
255 m = ((testn & 0x100) >> 1) |
256 (testm);
257 n = (testn & 0xFF);
258 p = ((testn & 0x600) >> 3) |
259 (testp2 << 3) |
260 (testp);
261 }
262 }
263 }
264 }
265 }
266 } else {
267
268 vcomax = 550000;
269 vcomin = 150000;
270 pllreffreq = 48000;
271
272 for (testp = 1; testp < 9; testp++) {
273 if (clock * testp > vcomax)
274 continue;
275 if (clock * testp < vcomin)
276 continue;
277
278 for (testm = 1; testm < 17; testm++) {
279 for (testn = 1; testn < 151; testn++) {
280 computed = (pllreffreq * testn) /
281 (testm * testp);
282 if (computed > clock)
283 tmpdelta = computed - clock;
284 else
285 tmpdelta = clock - computed;
286 if (tmpdelta < delta) {
287 delta = tmpdelta;
288 n = testn - 1;
289 m = (testm - 1) |
290 ((n >> 1) & 0x80);
291 p = testp - 1;
292 }
Dave Airlie414c4532012-04-17 15:01:25 +0100293 }
294 }
295 }
296 }
297
298 for (i = 0; i <= 32 && pll_locked == false; i++) {
299 if (i > 0) {
300 WREG8(MGAREG_CRTC_INDEX, 0x1e);
301 tmp = RREG8(MGAREG_CRTC_DATA);
302 if (tmp < 0xff)
303 WREG8(MGAREG_CRTC_DATA, tmp+1);
304 }
305
306 /* set pixclkdis to 1 */
307 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
308 tmp = RREG8(DAC_DATA);
309 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000310 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100311
312 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
313 tmp = RREG8(DAC_DATA);
314 tmp |= MGA1064_REMHEADCTL_CLKDIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000315 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100316
317 /* select PLL Set C */
318 tmp = RREG8(MGAREG_MEM_MISC_READ);
319 tmp |= 0x3 << 2;
320 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
321
322 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
323 tmp = RREG8(DAC_DATA);
324 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000325 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100326
327 udelay(500);
328
329 /* reset the PLL */
330 WREG8(DAC_INDEX, MGA1064_VREF_CTL);
331 tmp = RREG8(DAC_DATA);
332 tmp &= ~0x04;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000333 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100334
335 udelay(50);
336
337 /* program pixel pll register */
338 WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
339 WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
340 WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
341
342 udelay(50);
343
344 /* turn pll on */
345 WREG8(DAC_INDEX, MGA1064_VREF_CTL);
346 tmp = RREG8(DAC_DATA);
347 tmp |= 0x04;
348 WREG_DAC(MGA1064_VREF_CTL, tmp);
349
350 udelay(500);
351
352 /* select the pixel pll */
353 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
354 tmp = RREG8(DAC_DATA);
355 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
356 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000357 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100358
359 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
360 tmp = RREG8(DAC_DATA);
361 tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
362 tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000363 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100364
365 /* reset dotclock rate bit */
366 WREG8(MGAREG_SEQ_INDEX, 1);
367 tmp = RREG8(MGAREG_SEQ_DATA);
368 tmp &= ~0x8;
369 WREG8(MGAREG_SEQ_DATA, tmp);
370
371 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
372 tmp = RREG8(DAC_DATA);
373 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000374 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100375
376 vcount = RREG8(MGAREG_VCOUNT);
377
378 for (j = 0; j < 30 && pll_locked == false; j++) {
379 tmpcount = RREG8(MGAREG_VCOUNT);
380 if (tmpcount < vcount)
381 vcount = 0;
382 if ((tmpcount - vcount) > 2)
383 pll_locked = true;
384 else
385 udelay(5);
386 }
387 }
388 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
389 tmp = RREG8(DAC_DATA);
390 tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
391 WREG_DAC(MGA1064_REMHEADCTL, tmp);
392 return 0;
393}
394
395static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
396{
397 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530398 unsigned int delta, tmpdelta;
Dave Airlie414c4532012-04-17 15:01:25 +0100399 unsigned int testp, testm, testn;
400 unsigned int p, m, n;
401 unsigned int computed;
402 u8 tmp;
403
404 m = n = p = 0;
405 vcomax = 550000;
406 vcomin = 150000;
407 pllreffreq = 50000;
408
409 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100410
411 for (testp = 16; testp > 0; testp--) {
412 if (clock * testp > vcomax)
413 continue;
414 if (clock * testp < vcomin)
415 continue;
416
417 for (testn = 1; testn < 257; testn++) {
418 for (testm = 1; testm < 17; testm++) {
419 computed = (pllreffreq * testn) /
420 (testm * testp);
421 if (computed > clock)
422 tmpdelta = computed - clock;
423 else
424 tmpdelta = clock - computed;
425 if (tmpdelta < delta) {
426 delta = tmpdelta;
427 n = testn - 1;
428 m = testm - 1;
429 p = testp - 1;
430 }
431 }
432 }
433 }
434
435 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
436 tmp = RREG8(DAC_DATA);
437 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000438 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100439
440 tmp = RREG8(MGAREG_MEM_MISC_READ);
441 tmp |= 0x3 << 2;
442 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
443
444 WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
445 tmp = RREG8(DAC_DATA);
Christopher Harveyfb70a662013-04-12 22:24:05 +0000446 WREG8(DAC_DATA, tmp & ~0x40);
Dave Airlie414c4532012-04-17 15:01:25 +0100447
448 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
449 tmp = RREG8(DAC_DATA);
450 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000451 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100452
453 WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
454 WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
455 WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
456
457 udelay(50);
458
459 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
460 tmp = RREG8(DAC_DATA);
461 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000462 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100463
464 udelay(500);
465
466 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
467 tmp = RREG8(DAC_DATA);
468 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
469 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000470 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100471
472 WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
473 tmp = RREG8(DAC_DATA);
Christopher Harveyfb70a662013-04-12 22:24:05 +0000474 WREG8(DAC_DATA, tmp | 0x40);
Dave Airlie414c4532012-04-17 15:01:25 +0100475
476 tmp = RREG8(MGAREG_MEM_MISC_READ);
477 tmp |= (0x3 << 2);
478 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
479
480 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
481 tmp = RREG8(DAC_DATA);
482 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000483 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100484
485 return 0;
486}
487
488static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
489{
490 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530491 unsigned int delta, tmpdelta;
Dave Airlie414c4532012-04-17 15:01:25 +0100492 unsigned int testp, testm, testn;
493 unsigned int p, m, n;
494 unsigned int computed;
495 int i, j, tmpcount, vcount;
496 u8 tmp;
497 bool pll_locked = false;
498
499 m = n = p = 0;
500 vcomax = 800000;
501 vcomin = 400000;
Julia Lemire260b3f12013-03-18 10:17:47 -0400502 pllreffreq = 33333;
Dave Airlie414c4532012-04-17 15:01:25 +0100503
504 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100505
Julia Lemire260b3f12013-03-18 10:17:47 -0400506 for (testp = 16; testp > 0; testp >>= 1) {
Dave Airlie414c4532012-04-17 15:01:25 +0100507 if (clock * testp > vcomax)
508 continue;
509 if (clock * testp < vcomin)
510 continue;
511
512 for (testm = 1; testm < 33; testm++) {
Julia Lemire260b3f12013-03-18 10:17:47 -0400513 for (testn = 17; testn < 257; testn++) {
Dave Airlie414c4532012-04-17 15:01:25 +0100514 computed = (pllreffreq * testn) /
515 (testm * testp);
516 if (computed > clock)
517 tmpdelta = computed - clock;
518 else
519 tmpdelta = clock - computed;
520 if (tmpdelta < delta) {
521 delta = tmpdelta;
522 n = testn - 1;
Julia Lemire260b3f12013-03-18 10:17:47 -0400523 m = (testm - 1);
Dave Airlie414c4532012-04-17 15:01:25 +0100524 p = testp - 1;
525 }
526 if ((clock * testp) >= 600000)
Julia Lemire260b3f12013-03-18 10:17:47 -0400527 p |= 0x80;
Dave Airlie414c4532012-04-17 15:01:25 +0100528 }
529 }
530 }
531 for (i = 0; i <= 32 && pll_locked == false; i++) {
532 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
533 tmp = RREG8(DAC_DATA);
534 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000535 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100536
537 tmp = RREG8(MGAREG_MEM_MISC_READ);
538 tmp |= 0x3 << 2;
539 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
540
541 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
542 tmp = RREG8(DAC_DATA);
543 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000544 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100545
546 udelay(500);
547
548 WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
549 WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
550 WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
551
552 udelay(500);
553
554 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
555 tmp = RREG8(DAC_DATA);
556 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
557 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000558 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100559
560 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
561 tmp = RREG8(DAC_DATA);
562 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
563 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000564 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100565
566 vcount = RREG8(MGAREG_VCOUNT);
567
568 for (j = 0; j < 30 && pll_locked == false; j++) {
569 tmpcount = RREG8(MGAREG_VCOUNT);
570 if (tmpcount < vcount)
571 vcount = 0;
572 if ((tmpcount - vcount) > 2)
573 pll_locked = true;
574 else
575 udelay(5);
576 }
577 }
578
579 return 0;
580}
581
582static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
583{
584 unsigned int vcomax, vcomin, pllreffreq;
585 unsigned int delta, tmpdelta;
Dave Airlie98306052012-08-09 15:00:15 +1000586 int testr, testn, testm, testo;
Dave Airlie414c4532012-04-17 15:01:25 +0100587 unsigned int p, m, n;
Dave Airlie98306052012-08-09 15:00:15 +1000588 unsigned int computed, vco;
Dave Airlie414c4532012-04-17 15:01:25 +0100589 int tmp;
Dave Airlie98306052012-08-09 15:00:15 +1000590 const unsigned int m_div_val[] = { 1, 2, 4, 8 };
Dave Airlie414c4532012-04-17 15:01:25 +0100591
592 m = n = p = 0;
593 vcomax = 1488000;
594 vcomin = 1056000;
595 pllreffreq = 48000;
596
597 delta = 0xffffffff;
598
599 for (testr = 0; testr < 4; testr++) {
600 if (delta == 0)
601 break;
602 for (testn = 5; testn < 129; testn++) {
603 if (delta == 0)
604 break;
605 for (testm = 3; testm >= 0; testm--) {
606 if (delta == 0)
607 break;
608 for (testo = 5; testo < 33; testo++) {
Dave Airlie98306052012-08-09 15:00:15 +1000609 vco = pllreffreq * (testn + 1) /
Dave Airlie414c4532012-04-17 15:01:25 +0100610 (testr + 1);
Dave Airlie98306052012-08-09 15:00:15 +1000611 if (vco < vcomin)
Dave Airlie414c4532012-04-17 15:01:25 +0100612 continue;
Dave Airlie98306052012-08-09 15:00:15 +1000613 if (vco > vcomax)
Dave Airlie414c4532012-04-17 15:01:25 +0100614 continue;
Dave Airlie98306052012-08-09 15:00:15 +1000615 computed = vco / (m_div_val[testm] * (testo + 1));
Dave Airlie414c4532012-04-17 15:01:25 +0100616 if (computed > clock)
617 tmpdelta = computed - clock;
618 else
619 tmpdelta = clock - computed;
620 if (tmpdelta < delta) {
621 delta = tmpdelta;
622 m = testm | (testo << 3);
623 n = testn;
624 p = testr | (testr << 3);
625 }
626 }
627 }
628 }
629 }
630
631 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
632 tmp = RREG8(DAC_DATA);
633 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000634 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100635
636 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
637 tmp = RREG8(DAC_DATA);
638 tmp |= MGA1064_REMHEADCTL_CLKDIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000639 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100640
641 tmp = RREG8(MGAREG_MEM_MISC_READ);
642 tmp |= (0x3<<2) | 0xc0;
643 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
644
645 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
646 tmp = RREG8(DAC_DATA);
647 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
648 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000649 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100650
651 udelay(500);
652
653 WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
654 WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
655 WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
656
657 udelay(50);
658
659 return 0;
660}
661
662static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
663{
664 switch(mdev->type) {
665 case G200_SE_A:
666 case G200_SE_B:
667 return mga_g200se_set_plls(mdev, clock);
668 break;
669 case G200_WB:
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400670 case G200_EW3:
Dave Airlie414c4532012-04-17 15:01:25 +0100671 return mga_g200wb_set_plls(mdev, clock);
672 break;
673 case G200_EV:
674 return mga_g200ev_set_plls(mdev, clock);
675 break;
676 case G200_EH:
677 return mga_g200eh_set_plls(mdev, clock);
678 break;
679 case G200_ER:
680 return mga_g200er_set_plls(mdev, clock);
681 break;
682 }
683 return 0;
684}
685
686static void mga_g200wb_prepare(struct drm_crtc *crtc)
687{
688 struct mga_device *mdev = crtc->dev->dev_private;
689 u8 tmp;
690 int iter_max;
691
692 /* 1- The first step is to warn the BMC of an upcoming mode change.
693 * We are putting the misc<0> to output.*/
694
695 WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
696 tmp = RREG8(DAC_DATA);
697 tmp |= 0x10;
698 WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
699
700 /* we are putting a 1 on the misc<0> line */
701 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
702 tmp = RREG8(DAC_DATA);
703 tmp |= 0x10;
704 WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
705
706 /* 2- Second step to mask and further scan request
707 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
708 */
709 WREG8(DAC_INDEX, MGA1064_SPAREREG);
710 tmp = RREG8(DAC_DATA);
711 tmp |= 0x80;
712 WREG_DAC(MGA1064_SPAREREG, tmp);
713
714 /* 3a- the third step is to verifu if there is an active scan
715 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
716 */
717 iter_max = 300;
718 while (!(tmp & 0x1) && iter_max) {
719 WREG8(DAC_INDEX, MGA1064_SPAREREG);
720 tmp = RREG8(DAC_DATA);
721 udelay(1000);
722 iter_max--;
723 }
724
725 /* 3b- this step occurs only if the remove is actually scanning
726 * we are waiting for the end of the frame which is a 1 on
727 * remvsyncsts (XSPAREREG<1>)
728 */
729 if (iter_max) {
730 iter_max = 300;
731 while ((tmp & 0x2) && iter_max) {
732 WREG8(DAC_INDEX, MGA1064_SPAREREG);
733 tmp = RREG8(DAC_DATA);
734 udelay(1000);
735 iter_max--;
736 }
737 }
738}
739
740static void mga_g200wb_commit(struct drm_crtc *crtc)
741{
742 u8 tmp;
743 struct mga_device *mdev = crtc->dev->dev_private;
744
745 /* 1- The first step is to ensure that the vrsten and hrsten are set */
746 WREG8(MGAREG_CRTCEXT_INDEX, 1);
747 tmp = RREG8(MGAREG_CRTCEXT_DATA);
748 WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
749
750 /* 2- second step is to assert the rstlvl2 */
751 WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
752 tmp = RREG8(DAC_DATA);
753 tmp |= 0x8;
754 WREG8(DAC_DATA, tmp);
755
756 /* wait 10 us */
757 udelay(10);
758
759 /* 3- deassert rstlvl2 */
760 tmp &= ~0x08;
761 WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
762 WREG8(DAC_DATA, tmp);
763
764 /* 4- remove mask of scan request */
765 WREG8(DAC_INDEX, MGA1064_SPAREREG);
766 tmp = RREG8(DAC_DATA);
767 tmp &= ~0x80;
768 WREG8(DAC_DATA, tmp);
769
770 /* 5- put back a 0 on the misc<0> line */
771 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
772 tmp = RREG8(DAC_DATA);
773 tmp &= ~0x10;
774 WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
775}
776
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000777/*
778 This is how the framebuffer base address is stored in g200 cards:
779 * Assume @offset is the gpu_addr variable of the framebuffer object
780 * Then addr is the number of _pixels_ (not bytes) from the start of
781 VRAM to the first pixel we want to display. (divided by 2 for 32bit
782 framebuffers)
783 * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
784 addr<20> -> CRTCEXT0<6>
785 addr<19-16> -> CRTCEXT0<3-0>
786 addr<15-8> -> CRTCC<7-0>
787 addr<7-0> -> CRTCD<7-0>
788 CRTCEXT0 has to be programmed last to trigger an update and make the
789 new addr variable take effect.
790 */
Rashika080fd6b2014-01-06 20:37:15 +0530791static void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
Dave Airlie414c4532012-04-17 15:01:25 +0100792{
793 struct mga_device *mdev = crtc->dev->dev_private;
794 u32 addr;
795 int count;
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000796 u8 crtcext0;
Dave Airlie414c4532012-04-17 15:01:25 +0100797
798 while (RREG8(0x1fda) & 0x08);
799 while (!(RREG8(0x1fda) & 0x08));
800
801 count = RREG8(MGAREG_VCOUNT) + 2;
802 while (RREG8(MGAREG_VCOUNT) < count);
803
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000804 WREG8(MGAREG_CRTCEXT_INDEX, 0);
805 crtcext0 = RREG8(MGAREG_CRTCEXT_DATA);
806 crtcext0 &= 0xB0;
807 addr = offset / 8;
808 /* Can't store addresses any higher than that...
809 but we also don't have more than 16MB of memory, so it should be fine. */
810 WARN_ON(addr > 0x1fffff);
811 crtcext0 |= (!!(addr & (1<<20)))<<6;
Dave Airlie414c4532012-04-17 15:01:25 +0100812 WREG_CRT(0x0d, (u8)(addr & 0xff));
813 WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000814 WREG_ECRT(0x0, ((u8)(addr >> 16) & 0xf) | crtcext0);
Dave Airlie414c4532012-04-17 15:01:25 +0100815}
816
817
818/* ast is different - we will force move buffers out of VRAM */
819static int mga_crtc_do_set_base(struct drm_crtc *crtc,
820 struct drm_framebuffer *fb,
821 int x, int y, int atomic)
822{
823 struct mga_device *mdev = crtc->dev->dev_private;
824 struct drm_gem_object *obj;
825 struct mga_framebuffer *mga_fb;
826 struct mgag200_bo *bo;
827 int ret;
828 u64 gpu_addr;
829
830 /* push the previous fb to system ram */
831 if (!atomic && fb) {
832 mga_fb = to_mga_framebuffer(fb);
833 obj = mga_fb->obj;
834 bo = gem_to_mga_bo(obj);
835 ret = mgag200_bo_reserve(bo, false);
836 if (ret)
837 return ret;
838 mgag200_bo_push_sysram(bo);
839 mgag200_bo_unreserve(bo);
840 }
841
Matt Roperf4510a22014-04-01 15:22:40 -0700842 mga_fb = to_mga_framebuffer(crtc->primary->fb);
Dave Airlie414c4532012-04-17 15:01:25 +0100843 obj = mga_fb->obj;
844 bo = gem_to_mga_bo(obj);
845
846 ret = mgag200_bo_reserve(bo, false);
847 if (ret)
848 return ret;
849
850 ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
851 if (ret) {
852 mgag200_bo_unreserve(bo);
853 return ret;
854 }
855
856 if (&mdev->mfbdev->mfb == mga_fb) {
857 /* if pushing console in kmap it */
858 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
859 if (ret)
860 DRM_ERROR("failed to kmap fbcon\n");
861
862 }
863 mgag200_bo_unreserve(bo);
864
Dave Airlie414c4532012-04-17 15:01:25 +0100865 mga_set_start_address(crtc, (u32)gpu_addr);
866
867 return 0;
868}
869
870static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
871 struct drm_framebuffer *old_fb)
872{
873 return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
874}
875
876static int mga_crtc_mode_set(struct drm_crtc *crtc,
877 struct drm_display_mode *mode,
878 struct drm_display_mode *adjusted_mode,
879 int x, int y, struct drm_framebuffer *old_fb)
880{
881 struct drm_device *dev = crtc->dev;
882 struct mga_device *mdev = dev->dev_private;
Ville Syrjälä72952752016-11-18 21:52:40 +0200883 const struct drm_framebuffer *fb = crtc->primary->fb;
Dave Airlie414c4532012-04-17 15:01:25 +0100884 int hdisplay, hsyncstart, hsyncend, htotal;
885 int vdisplay, vsyncstart, vsyncend, vtotal;
886 int pitch;
887 int option = 0, option2 = 0;
888 int i;
889 unsigned char misc = 0;
890 unsigned char ext_vga[6];
Dave Airlie414c4532012-04-17 15:01:25 +0100891 u8 bppshift;
892
893 static unsigned char dacvalue[] = {
894 /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
895 /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
896 /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
897 /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
898 /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
899 /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
900 /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
901 /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
902 /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
903 /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
904 };
905
Ville Syrjälä72952752016-11-18 21:52:40 +0200906 bppshift = mdev->bpp_shifts[(fb->bits_per_pixel >> 3) - 1];
Dave Airlie414c4532012-04-17 15:01:25 +0100907
908 switch (mdev->type) {
909 case G200_SE_A:
910 case G200_SE_B:
911 dacvalue[MGA1064_VREF_CTL] = 0x03;
912 dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
913 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
914 MGA1064_MISC_CTL_VGA8 |
915 MGA1064_MISC_CTL_DAC_RAM_CS;
916 if (mdev->has_sdram)
917 option = 0x40049120;
918 else
919 option = 0x4004d120;
920 option2 = 0x00008000;
921 break;
922 case G200_WB:
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400923 case G200_EW3:
Dave Airlie414c4532012-04-17 15:01:25 +0100924 dacvalue[MGA1064_VREF_CTL] = 0x07;
925 option = 0x41049120;
926 option2 = 0x0000b000;
927 break;
928 case G200_EV:
929 dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
930 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
931 MGA1064_MISC_CTL_DAC_RAM_CS;
932 option = 0x00000120;
933 option2 = 0x0000b000;
934 break;
935 case G200_EH:
936 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
937 MGA1064_MISC_CTL_DAC_RAM_CS;
938 option = 0x00000120;
939 option2 = 0x0000b000;
940 break;
941 case G200_ER:
Dave Airlie414c4532012-04-17 15:01:25 +0100942 break;
943 }
944
Ville Syrjälä72952752016-11-18 21:52:40 +0200945 switch (fb->bits_per_pixel) {
Dave Airlie414c4532012-04-17 15:01:25 +0100946 case 8:
947 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
948 break;
949 case 16:
Ville Syrjälä72952752016-11-18 21:52:40 +0200950 if (fb->depth == 15)
Dave Airlie414c4532012-04-17 15:01:25 +0100951 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
952 else
953 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
954 break;
955 case 24:
956 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
957 break;
958 case 32:
959 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
960 break;
961 }
962
963 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
964 misc |= 0x40;
965 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
966 misc |= 0x80;
967
968
969 for (i = 0; i < sizeof(dacvalue); i++) {
Christopher Harvey9d8aa552013-04-12 20:42:19 +0000970 if ((i <= 0x17) ||
Dave Airlie414c4532012-04-17 15:01:25 +0100971 (i == 0x1b) ||
972 (i == 0x1c) ||
973 ((i >= 0x1f) && (i <= 0x29)) ||
974 ((i >= 0x30) && (i <= 0x37)))
975 continue;
976 if (IS_G200_SE(mdev) &&
977 ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
978 continue;
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400979 if ((mdev->type == G200_EV ||
980 mdev->type == G200_WB ||
981 mdev->type == G200_EH ||
982 mdev->type == G200_EW3) &&
Dave Airlie414c4532012-04-17 15:01:25 +0100983 (i >= 0x44) && (i <= 0x4e))
984 continue;
985
986 WREG_DAC(i, dacvalue[i]);
987 }
988
Christopher Harvey1812a3d2013-04-05 10:51:15 -0400989 if (mdev->type == G200_ER)
990 WREG_DAC(0x90, 0);
Dave Airlie414c4532012-04-17 15:01:25 +0100991
992 if (option)
993 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
994 if (option2)
995 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
996
997 WREG_SEQ(2, 0xf);
998 WREG_SEQ(3, 0);
999 WREG_SEQ(4, 0xe);
1000
Ville Syrjälä72952752016-11-18 21:52:40 +02001001 pitch = fb->pitches[0] / (fb->bits_per_pixel / 8);
1002 if (fb->bits_per_pixel == 24)
Takashi Iwaida558392013-07-17 15:07:26 +02001003 pitch = (pitch * 3) >> (4 - bppshift);
Dave Airlie414c4532012-04-17 15:01:25 +01001004 else
1005 pitch = pitch >> (4 - bppshift);
1006
1007 hdisplay = mode->hdisplay / 8 - 1;
1008 hsyncstart = mode->hsync_start / 8 - 1;
1009 hsyncend = mode->hsync_end / 8 - 1;
1010 htotal = mode->htotal / 8 - 1;
1011
1012 /* Work around hardware quirk */
1013 if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
1014 htotal++;
1015
1016 vdisplay = mode->vdisplay - 1;
1017 vsyncstart = mode->vsync_start - 1;
1018 vsyncend = mode->vsync_end - 1;
1019 vtotal = mode->vtotal - 2;
1020
1021 WREG_GFX(0, 0);
1022 WREG_GFX(1, 0);
1023 WREG_GFX(2, 0);
1024 WREG_GFX(3, 0);
1025 WREG_GFX(4, 0);
1026 WREG_GFX(5, 0x40);
1027 WREG_GFX(6, 0x5);
1028 WREG_GFX(7, 0xf);
1029 WREG_GFX(8, 0xf);
1030
1031 WREG_CRT(0, htotal - 4);
1032 WREG_CRT(1, hdisplay);
1033 WREG_CRT(2, hdisplay);
1034 WREG_CRT(3, (htotal & 0x1F) | 0x80);
1035 WREG_CRT(4, hsyncstart);
1036 WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
1037 WREG_CRT(6, vtotal & 0xFF);
1038 WREG_CRT(7, ((vtotal & 0x100) >> 8) |
1039 ((vdisplay & 0x100) >> 7) |
1040 ((vsyncstart & 0x100) >> 6) |
1041 ((vdisplay & 0x100) >> 5) |
1042 ((vdisplay & 0x100) >> 4) | /* linecomp */
1043 ((vtotal & 0x200) >> 4)|
1044 ((vdisplay & 0x200) >> 3) |
1045 ((vsyncstart & 0x200) >> 2));
1046 WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
1047 ((vdisplay & 0x200) >> 3));
1048 WREG_CRT(10, 0);
1049 WREG_CRT(11, 0);
1050 WREG_CRT(12, 0);
1051 WREG_CRT(13, 0);
1052 WREG_CRT(14, 0);
1053 WREG_CRT(15, 0);
1054 WREG_CRT(16, vsyncstart & 0xFF);
1055 WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
1056 WREG_CRT(18, vdisplay & 0xFF);
1057 WREG_CRT(19, pitch & 0xFF);
1058 WREG_CRT(20, 0);
1059 WREG_CRT(21, vdisplay & 0xFF);
1060 WREG_CRT(22, (vtotal + 1) & 0xFF);
1061 WREG_CRT(23, 0xc3);
1062 WREG_CRT(24, vdisplay & 0xFF);
1063
1064 ext_vga[0] = 0;
1065 ext_vga[5] = 0;
1066
1067 /* TODO interlace */
1068
1069 ext_vga[0] |= (pitch & 0x300) >> 4;
1070 ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
1071 ((hdisplay & 0x100) >> 7) |
1072 ((hsyncstart & 0x100) >> 6) |
1073 (htotal & 0x40);
1074 ext_vga[2] = ((vtotal & 0xc00) >> 10) |
1075 ((vdisplay & 0x400) >> 8) |
1076 ((vdisplay & 0xc00) >> 7) |
1077 ((vsyncstart & 0xc00) >> 5) |
1078 ((vdisplay & 0x400) >> 3);
Ville Syrjälä72952752016-11-18 21:52:40 +02001079 if (fb->bits_per_pixel == 24)
Dave Airlie414c4532012-04-17 15:01:25 +01001080 ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
1081 else
1082 ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
1083 ext_vga[4] = 0;
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001084 if (mdev->type == G200_WB || mdev->type == G200_EW3)
Dave Airlie414c4532012-04-17 15:01:25 +01001085 ext_vga[1] |= 0x88;
1086
Dave Airlie414c4532012-04-17 15:01:25 +01001087 /* Set pixel clocks */
1088 misc = 0x2d;
1089 WREG8(MGA_MISC_OUT, misc);
1090
1091 mga_crtc_set_plls(mdev, mode->clock);
1092
1093 for (i = 0; i < 6; i++) {
1094 WREG_ECRT(i, ext_vga[i]);
1095 }
1096
1097 if (mdev->type == G200_ER)
Christopher Harvey1812a3d2013-04-05 10:51:15 -04001098 WREG_ECRT(0x24, 0x5);
Dave Airlie414c4532012-04-17 15:01:25 +01001099
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001100 if (mdev->type == G200_EW3)
1101 WREG_ECRT(0x34, 0x5);
1102
Dave Airlie414c4532012-04-17 15:01:25 +01001103 if (mdev->type == G200_EV) {
1104 WREG_ECRT(6, 0);
1105 }
1106
1107 WREG_ECRT(0, ext_vga[0]);
1108 /* Enable mga pixel clock */
1109 misc = 0x2d;
1110
1111 WREG8(MGA_MISC_OUT, misc);
1112
1113 if (adjusted_mode)
1114 memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
1115
1116 mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
1117
1118 /* reset tagfifo */
1119 if (mdev->type == G200_ER) {
1120 u32 mem_ctl = RREG32(MGAREG_MEMCTL);
1121 u8 seq1;
1122
1123 /* screen off */
1124 WREG8(MGAREG_SEQ_INDEX, 0x01);
1125 seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
1126 WREG8(MGAREG_SEQ_DATA, seq1);
1127
1128 WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
1129 udelay(1000);
1130 WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
1131
1132 WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
1133 }
1134
1135
1136 if (IS_G200_SE(mdev)) {
Julia Lemireabbee622013-06-27 13:38:59 -04001137 if (mdev->unique_rev_id >= 0x02) {
Dave Airlie414c4532012-04-17 15:01:25 +01001138 u8 hi_pri_lvl;
1139 u32 bpp;
1140 u32 mb;
1141
Ville Syrjälä72952752016-11-18 21:52:40 +02001142 if (fb->bits_per_pixel > 16)
Dave Airlie414c4532012-04-17 15:01:25 +01001143 bpp = 32;
Ville Syrjälä72952752016-11-18 21:52:40 +02001144 else if (fb->bits_per_pixel > 8)
Dave Airlie414c4532012-04-17 15:01:25 +01001145 bpp = 16;
1146 else
1147 bpp = 8;
1148
1149 mb = (mode->clock * bpp) / 1000;
1150 if (mb > 3100)
1151 hi_pri_lvl = 0;
1152 else if (mb > 2600)
1153 hi_pri_lvl = 1;
1154 else if (mb > 1900)
1155 hi_pri_lvl = 2;
1156 else if (mb > 1160)
1157 hi_pri_lvl = 3;
1158 else if (mb > 440)
1159 hi_pri_lvl = 4;
1160 else
1161 hi_pri_lvl = 5;
1162
Christopher Harvey91f8f102013-05-31 20:33:07 +00001163 WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1164 WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl);
Dave Airlie414c4532012-04-17 15:01:25 +01001165 } else {
Christopher Harvey91f8f102013-05-31 20:33:07 +00001166 WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
Julia Lemireabbee622013-06-27 13:38:59 -04001167 if (mdev->unique_rev_id >= 0x01)
Christopher Harvey91f8f102013-05-31 20:33:07 +00001168 WREG8(MGAREG_CRTCEXT_DATA, 0x03);
Dave Airlie414c4532012-04-17 15:01:25 +01001169 else
Christopher Harvey91f8f102013-05-31 20:33:07 +00001170 WREG8(MGAREG_CRTCEXT_DATA, 0x04);
Dave Airlie414c4532012-04-17 15:01:25 +01001171 }
1172 }
1173 return 0;
1174}
1175
1176#if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1177static int mga_suspend(struct drm_crtc *crtc)
1178{
1179 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1180 struct drm_device *dev = crtc->dev;
1181 struct mga_device *mdev = dev->dev_private;
1182 struct pci_dev *pdev = dev->pdev;
1183 int option;
1184
1185 if (mdev->suspended)
1186 return 0;
1187
1188 WREG_SEQ(1, 0x20);
1189 WREG_ECRT(1, 0x30);
1190 /* Disable the pixel clock */
1191 WREG_DAC(0x1a, 0x05);
1192 /* Power down the DAC */
1193 WREG_DAC(0x1e, 0x18);
1194 /* Power down the pixel PLL */
1195 WREG_DAC(0x1a, 0x0d);
1196
1197 /* Disable PLLs and clocks */
1198 pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1199 option &= ~(0x1F8024);
1200 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1201 pci_set_power_state(pdev, PCI_D3hot);
1202 pci_disable_device(pdev);
1203
1204 mdev->suspended = true;
1205
1206 return 0;
1207}
1208
1209static int mga_resume(struct drm_crtc *crtc)
1210{
1211 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1212 struct drm_device *dev = crtc->dev;
1213 struct mga_device *mdev = dev->dev_private;
1214 struct pci_dev *pdev = dev->pdev;
1215 int option;
1216
1217 if (!mdev->suspended)
1218 return 0;
1219
1220 pci_set_power_state(pdev, PCI_D0);
1221 pci_enable_device(pdev);
1222
1223 /* Disable sysclk */
1224 pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1225 option &= ~(0x4);
1226 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1227
1228 mdev->suspended = false;
1229
1230 return 0;
1231}
1232
1233#endif
1234
1235static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1236{
1237 struct drm_device *dev = crtc->dev;
1238 struct mga_device *mdev = dev->dev_private;
1239 u8 seq1 = 0, crtcext1 = 0;
1240
1241 switch (mode) {
1242 case DRM_MODE_DPMS_ON:
1243 seq1 = 0;
1244 crtcext1 = 0;
1245 mga_crtc_load_lut(crtc);
1246 break;
1247 case DRM_MODE_DPMS_STANDBY:
1248 seq1 = 0x20;
1249 crtcext1 = 0x10;
1250 break;
1251 case DRM_MODE_DPMS_SUSPEND:
1252 seq1 = 0x20;
1253 crtcext1 = 0x20;
1254 break;
1255 case DRM_MODE_DPMS_OFF:
1256 seq1 = 0x20;
1257 crtcext1 = 0x30;
1258 break;
1259 }
1260
1261#if 0
1262 if (mode == DRM_MODE_DPMS_OFF) {
1263 mga_suspend(crtc);
1264 }
1265#endif
1266 WREG8(MGAREG_SEQ_INDEX, 0x01);
1267 seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1268 mga_wait_vsync(mdev);
1269 mga_wait_busy(mdev);
1270 WREG8(MGAREG_SEQ_DATA, seq1);
1271 msleep(20);
1272 WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1273 crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1274 WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1275
1276#if 0
1277 if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1278 mga_resume(crtc);
1279 drm_helper_resume_force_mode(dev);
1280 }
1281#endif
1282}
1283
1284/*
1285 * This is called before a mode is programmed. A typical use might be to
1286 * enable DPMS during the programming to avoid seeing intermediate stages,
1287 * but that's not relevant to us
1288 */
1289static void mga_crtc_prepare(struct drm_crtc *crtc)
1290{
1291 struct drm_device *dev = crtc->dev;
1292 struct mga_device *mdev = dev->dev_private;
1293 u8 tmp;
1294
1295 /* mga_resume(crtc);*/
1296
1297 WREG8(MGAREG_CRTC_INDEX, 0x11);
1298 tmp = RREG8(MGAREG_CRTC_DATA);
1299 WREG_CRT(0x11, tmp | 0x80);
1300
1301 if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1302 WREG_SEQ(0, 1);
1303 msleep(50);
1304 WREG_SEQ(1, 0x20);
1305 msleep(20);
1306 } else {
1307 WREG8(MGAREG_SEQ_INDEX, 0x1);
1308 tmp = RREG8(MGAREG_SEQ_DATA);
1309
1310 /* start sync reset */
1311 WREG_SEQ(0, 1);
1312 WREG_SEQ(1, tmp | 0x20);
1313 }
1314
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001315 if (mdev->type == G200_WB || mdev->type == G200_EW3)
Dave Airlie414c4532012-04-17 15:01:25 +01001316 mga_g200wb_prepare(crtc);
1317
1318 WREG_CRT(17, 0);
1319}
1320
1321/*
1322 * This is called after a mode is programmed. It should reverse anything done
1323 * by the prepare function
1324 */
1325static void mga_crtc_commit(struct drm_crtc *crtc)
1326{
1327 struct drm_device *dev = crtc->dev;
1328 struct mga_device *mdev = dev->dev_private;
Jani Nikulad584ff82015-03-11 11:51:00 +02001329 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
Dave Airlie414c4532012-04-17 15:01:25 +01001330 u8 tmp;
1331
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001332 if (mdev->type == G200_WB || mdev->type == G200_EW3)
Dave Airlie414c4532012-04-17 15:01:25 +01001333 mga_g200wb_commit(crtc);
1334
1335 if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1336 msleep(50);
1337 WREG_SEQ(1, 0x0);
1338 msleep(20);
1339 WREG_SEQ(0, 0x3);
1340 } else {
1341 WREG8(MGAREG_SEQ_INDEX, 0x1);
1342 tmp = RREG8(MGAREG_SEQ_DATA);
1343
1344 tmp &= ~0x20;
1345 WREG_SEQ(0x1, tmp);
1346 WREG_SEQ(0, 3);
1347 }
1348 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1349}
1350
1351/*
1352 * The core can pass us a set of gamma values to program. We actually only
1353 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1354 * but it's a requirement that we provide the function
1355 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001356static int mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1357 u16 *blue, uint32_t size)
Dave Airlie414c4532012-04-17 15:01:25 +01001358{
1359 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
Dave Airlie414c4532012-04-17 15:01:25 +01001360 int i;
1361
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001362 for (i = 0; i < size; i++) {
Dave Airlie414c4532012-04-17 15:01:25 +01001363 mga_crtc->lut_r[i] = red[i] >> 8;
1364 mga_crtc->lut_g[i] = green[i] >> 8;
1365 mga_crtc->lut_b[i] = blue[i] >> 8;
1366 }
1367 mga_crtc_load_lut(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001368
1369 return 0;
Dave Airlie414c4532012-04-17 15:01:25 +01001370}
1371
1372/* Simple cleanup function */
1373static void mga_crtc_destroy(struct drm_crtc *crtc)
1374{
1375 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1376
1377 drm_crtc_cleanup(crtc);
1378 kfree(mga_crtc);
1379}
1380
Egbert Eich64c29072013-07-17 15:07:22 +02001381static void mga_crtc_disable(struct drm_crtc *crtc)
1382{
1383 int ret;
1384 DRM_DEBUG_KMS("\n");
1385 mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -07001386 if (crtc->primary->fb) {
1387 struct mga_framebuffer *mga_fb = to_mga_framebuffer(crtc->primary->fb);
Egbert Eich64c29072013-07-17 15:07:22 +02001388 struct drm_gem_object *obj = mga_fb->obj;
1389 struct mgag200_bo *bo = gem_to_mga_bo(obj);
1390 ret = mgag200_bo_reserve(bo, false);
1391 if (ret)
1392 return;
1393 mgag200_bo_push_sysram(bo);
1394 mgag200_bo_unreserve(bo);
1395 }
Matt Roperf4510a22014-04-01 15:22:40 -07001396 crtc->primary->fb = NULL;
Egbert Eich64c29072013-07-17 15:07:22 +02001397}
1398
Dave Airlie414c4532012-04-17 15:01:25 +01001399/* These provide the minimum set of functions required to handle a CRTC */
1400static const struct drm_crtc_funcs mga_crtc_funcs = {
Christopher Harveya080db92013-06-05 15:24:26 -04001401 .cursor_set = mga_crtc_cursor_set,
1402 .cursor_move = mga_crtc_cursor_move,
Dave Airlie414c4532012-04-17 15:01:25 +01001403 .gamma_set = mga_crtc_gamma_set,
1404 .set_config = drm_crtc_helper_set_config,
1405 .destroy = mga_crtc_destroy,
1406};
1407
1408static const struct drm_crtc_helper_funcs mga_helper_funcs = {
Egbert Eich64c29072013-07-17 15:07:22 +02001409 .disable = mga_crtc_disable,
Dave Airlie414c4532012-04-17 15:01:25 +01001410 .dpms = mga_crtc_dpms,
Dave Airlie414c4532012-04-17 15:01:25 +01001411 .mode_set = mga_crtc_mode_set,
1412 .mode_set_base = mga_crtc_mode_set_base,
1413 .prepare = mga_crtc_prepare,
1414 .commit = mga_crtc_commit,
1415 .load_lut = mga_crtc_load_lut,
1416};
1417
1418/* CRTC setup */
Christopher Harveyf1998fe2013-02-20 09:34:22 -05001419static void mga_crtc_init(struct mga_device *mdev)
Dave Airlie414c4532012-04-17 15:01:25 +01001420{
Dave Airlie414c4532012-04-17 15:01:25 +01001421 struct mga_crtc *mga_crtc;
1422 int i;
1423
1424 mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1425 (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1426 GFP_KERNEL);
1427
1428 if (mga_crtc == NULL)
1429 return;
1430
Christopher Harveyf1998fe2013-02-20 09:34:22 -05001431 drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
Dave Airlie414c4532012-04-17 15:01:25 +01001432
1433 drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1434 mdev->mode_info.crtc = mga_crtc;
1435
1436 for (i = 0; i < MGAG200_LUT_SIZE; i++) {
1437 mga_crtc->lut_r[i] = i;
1438 mga_crtc->lut_g[i] = i;
1439 mga_crtc->lut_b[i] = i;
1440 }
1441
1442 drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1443}
1444
1445/** Sets the color ramps on behalf of fbcon */
1446void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1447 u16 blue, int regno)
1448{
1449 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1450
1451 mga_crtc->lut_r[regno] = red >> 8;
1452 mga_crtc->lut_g[regno] = green >> 8;
1453 mga_crtc->lut_b[regno] = blue >> 8;
1454}
1455
1456/** Gets the color ramps on behalf of fbcon */
1457void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
1458 u16 *blue, int regno)
1459{
1460 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1461
1462 *red = (u16)mga_crtc->lut_r[regno] << 8;
1463 *green = (u16)mga_crtc->lut_g[regno] << 8;
1464 *blue = (u16)mga_crtc->lut_b[regno] << 8;
1465}
1466
1467/*
1468 * The encoder comes after the CRTC in the output pipeline, but before
1469 * the connector. It's responsible for ensuring that the digital
1470 * stream is appropriately converted into the output format. Setup is
1471 * very simple in this case - all we have to do is inform qemu of the
1472 * colour depth in order to ensure that it displays appropriately
1473 */
1474
1475/*
1476 * These functions are analagous to those in the CRTC code, but are intended
1477 * to handle any encoder-specific limitations
1478 */
Dave Airlie414c4532012-04-17 15:01:25 +01001479static void mga_encoder_mode_set(struct drm_encoder *encoder,
1480 struct drm_display_mode *mode,
1481 struct drm_display_mode *adjusted_mode)
1482{
1483
1484}
1485
1486static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
1487{
1488 return;
1489}
1490
1491static void mga_encoder_prepare(struct drm_encoder *encoder)
1492{
1493}
1494
1495static void mga_encoder_commit(struct drm_encoder *encoder)
1496{
1497}
1498
Rashika080fd6b2014-01-06 20:37:15 +05301499static void mga_encoder_destroy(struct drm_encoder *encoder)
Dave Airlie414c4532012-04-17 15:01:25 +01001500{
1501 struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
1502 drm_encoder_cleanup(encoder);
1503 kfree(mga_encoder);
1504}
1505
1506static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
1507 .dpms = mga_encoder_dpms,
Dave Airlie414c4532012-04-17 15:01:25 +01001508 .mode_set = mga_encoder_mode_set,
1509 .prepare = mga_encoder_prepare,
1510 .commit = mga_encoder_commit,
1511};
1512
1513static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
1514 .destroy = mga_encoder_destroy,
1515};
1516
1517static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
1518{
1519 struct drm_encoder *encoder;
1520 struct mga_encoder *mga_encoder;
1521
1522 mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
1523 if (!mga_encoder)
1524 return NULL;
1525
1526 encoder = &mga_encoder->base;
1527 encoder->possible_crtcs = 0x1;
1528
1529 drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001530 DRM_MODE_ENCODER_DAC, NULL);
Dave Airlie414c4532012-04-17 15:01:25 +01001531 drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
1532
1533 return encoder;
1534}
1535
1536
1537static int mga_vga_get_modes(struct drm_connector *connector)
1538{
1539 struct mga_connector *mga_connector = to_mga_connector(connector);
1540 struct edid *edid;
1541 int ret = 0;
1542
1543 edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1544 if (edid) {
1545 drm_mode_connector_update_edid_property(connector, edid);
1546 ret = drm_add_edid_modes(connector, edid);
Dave Airlie414c4532012-04-17 15:01:25 +01001547 kfree(edid);
1548 }
1549 return ret;
1550}
1551
Julia Lemireabbee622013-06-27 13:38:59 -04001552static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
1553 int bits_per_pixel)
1554{
1555 uint32_t total_area, divisor;
Nicolas Pitrec24ca5b2015-11-03 23:09:58 -05001556 uint64_t active_area, pixels_per_second, bandwidth;
Julia Lemireabbee622013-06-27 13:38:59 -04001557 uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
1558
1559 divisor = 1024;
1560
1561 if (!mode->htotal || !mode->vtotal || !mode->clock)
1562 return 0;
1563
1564 active_area = mode->hdisplay * mode->vdisplay;
1565 total_area = mode->htotal * mode->vtotal;
1566
1567 pixels_per_second = active_area * mode->clock * 1000;
1568 do_div(pixels_per_second, total_area);
1569
1570 bandwidth = pixels_per_second * bytes_per_pixel * 100;
1571 do_div(bandwidth, divisor);
1572
1573 return (uint32_t)(bandwidth);
1574}
1575
1576#define MODE_BANDWIDTH MODE_BAD
1577
Dave Airlie414c4532012-04-17 15:01:25 +01001578static int mga_vga_mode_valid(struct drm_connector *connector,
1579 struct drm_display_mode *mode)
1580{
Christopher Harvey0ba53172013-02-26 10:55:44 -05001581 struct drm_device *dev = connector->dev;
1582 struct mga_device *mdev = (struct mga_device*)dev->dev_private;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001583 int bpp = 32;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001584
Julia Lemireabbee622013-06-27 13:38:59 -04001585 if (IS_G200_SE(mdev)) {
1586 if (mdev->unique_rev_id == 0x01) {
1587 if (mode->hdisplay > 1600)
1588 return MODE_VIRTUAL_X;
1589 if (mode->vdisplay > 1200)
1590 return MODE_VIRTUAL_Y;
1591 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1592 > (24400 * 1024))
1593 return MODE_BANDWIDTH;
Mathieu Larouchee829d7e2015-08-21 09:24:13 -04001594 } else if (mdev->unique_rev_id == 0x02) {
Julia Lemireabbee622013-06-27 13:38:59 -04001595 if (mode->hdisplay > 1920)
1596 return MODE_VIRTUAL_X;
1597 if (mode->vdisplay > 1200)
1598 return MODE_VIRTUAL_Y;
1599 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1600 > (30100 * 1024))
1601 return MODE_BANDWIDTH;
1602 }
1603 } else if (mdev->type == G200_WB) {
1604 if (mode->hdisplay > 1280)
1605 return MODE_VIRTUAL_X;
1606 if (mode->vdisplay > 1024)
1607 return MODE_VIRTUAL_Y;
1608 if (mga_vga_calculate_mode_bandwidth(mode,
1609 bpp > (31877 * 1024)))
1610 return MODE_BANDWIDTH;
1611 } else if (mdev->type == G200_EV &&
1612 (mga_vga_calculate_mode_bandwidth(mode, bpp)
1613 > (32700 * 1024))) {
1614 return MODE_BANDWIDTH;
Dave Airlieec22b4a2014-02-05 14:13:56 +10001615 } else if (mdev->type == G200_EH &&
Julia Lemireabbee622013-06-27 13:38:59 -04001616 (mga_vga_calculate_mode_bandwidth(mode, bpp)
1617 > (37500 * 1024))) {
1618 return MODE_BANDWIDTH;
Dave Airlieec22b4a2014-02-05 14:13:56 +10001619 } else if (mdev->type == G200_ER &&
Julia Lemireabbee622013-06-27 13:38:59 -04001620 (mga_vga_calculate_mode_bandwidth(mode,
1621 bpp) > (55000 * 1024))) {
1622 return MODE_BANDWIDTH;
1623 }
Dave Airlie414c4532012-04-17 15:01:25 +01001624
Adam Jackson25161082015-06-15 16:16:15 -04001625 if ((mode->hdisplay % 8) != 0 || (mode->hsync_start % 8) != 0 ||
1626 (mode->hsync_end % 8) != 0 || (mode->htotal % 8) != 0) {
1627 return MODE_H_ILLEGAL;
1628 }
1629
Dave Airlie414c4532012-04-17 15:01:25 +01001630 if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1631 mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1632 mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1633 mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1634 return MODE_BAD;
1635 }
1636
Christopher Harvey0ba53172013-02-26 10:55:44 -05001637 /* Validate the mode input by the user */
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001638 if (connector->cmdline_mode.specified) {
1639 if (connector->cmdline_mode.bpp_specified)
1640 bpp = connector->cmdline_mode.bpp;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001641 }
1642
1643 if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001644 if (connector->cmdline_mode.specified)
1645 connector->cmdline_mode.specified = false;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001646 return MODE_BAD;
1647 }
1648
Dave Airlie414c4532012-04-17 15:01:25 +01001649 return MODE_OK;
1650}
1651
Rashika080fd6b2014-01-06 20:37:15 +05301652static struct drm_encoder *mga_connector_best_encoder(struct drm_connector
Dave Airlie414c4532012-04-17 15:01:25 +01001653 *connector)
1654{
1655 int enc_id = connector->encoder_ids[0];
Dave Airlie414c4532012-04-17 15:01:25 +01001656 /* pick the encoder ids */
Rob Clarkc7e95112014-07-17 23:30:00 -04001657 if (enc_id)
1658 return drm_encoder_find(connector->dev, enc_id);
Dave Airlie414c4532012-04-17 15:01:25 +01001659 return NULL;
1660}
1661
Dave Airlie414c4532012-04-17 15:01:25 +01001662static void mga_connector_destroy(struct drm_connector *connector)
1663{
1664 struct mga_connector *mga_connector = to_mga_connector(connector);
1665 mgag200_i2c_destroy(mga_connector->i2c);
1666 drm_connector_cleanup(connector);
1667 kfree(connector);
1668}
1669
Ville Syrjälä71cb7492015-12-15 12:21:10 +01001670static const struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
Dave Airlie414c4532012-04-17 15:01:25 +01001671 .get_modes = mga_vga_get_modes,
1672 .mode_valid = mga_vga_mode_valid,
1673 .best_encoder = mga_connector_best_encoder,
1674};
1675
Ville Syrjälä71cb7492015-12-15 12:21:10 +01001676static const struct drm_connector_funcs mga_vga_connector_funcs = {
Dave Airlie414c4532012-04-17 15:01:25 +01001677 .dpms = drm_helper_connector_dpms,
Dave Airlie414c4532012-04-17 15:01:25 +01001678 .fill_modes = drm_helper_probe_single_connector_modes,
1679 .destroy = mga_connector_destroy,
1680};
1681
1682static struct drm_connector *mga_vga_init(struct drm_device *dev)
1683{
1684 struct drm_connector *connector;
1685 struct mga_connector *mga_connector;
1686
1687 mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1688 if (!mga_connector)
1689 return NULL;
1690
1691 connector = &mga_connector->base;
1692
1693 drm_connector_init(dev, connector,
1694 &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1695
1696 drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1697
Thomas Wood34ea3d32014-05-29 16:57:41 +01001698 drm_connector_register(connector);
Egbert Eich3d5a1c52013-07-17 15:07:25 +02001699
Dave Airlie414c4532012-04-17 15:01:25 +01001700 mga_connector->i2c = mgag200_i2c_create(dev);
1701 if (!mga_connector->i2c)
1702 DRM_ERROR("failed to add ddc bus\n");
1703
1704 return connector;
1705}
1706
1707
1708int mgag200_modeset_init(struct mga_device *mdev)
1709{
1710 struct drm_encoder *encoder;
1711 struct drm_connector *connector;
1712 int ret;
1713
1714 mdev->mode_info.mode_config_initialized = true;
1715
1716 mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1717 mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1718
1719 mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1720
Christopher Harveyf1998fe2013-02-20 09:34:22 -05001721 mga_crtc_init(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +01001722
1723 encoder = mga_encoder_init(mdev->dev);
1724 if (!encoder) {
1725 DRM_ERROR("mga_encoder_init failed\n");
1726 return -1;
1727 }
1728
1729 connector = mga_vga_init(mdev->dev);
1730 if (!connector) {
1731 DRM_ERROR("mga_vga_init failed\n");
1732 return -1;
1733 }
1734
1735 drm_mode_connector_attach_encoder(connector, encoder);
1736
1737 ret = mgag200_fbdev_init(mdev);
1738 if (ret) {
1739 DRM_ERROR("mga_fbdev_init failed\n");
1740 return ret;
1741 }
1742
1743 return 0;
1744}
1745
1746void mgag200_modeset_fini(struct mga_device *mdev)
1747{
1748
1749}