blob: a7cef78d426f54c4629f22e685d1b3f47a9e3063 [file] [log] [blame]
Thomas Gleixnerc51669e2019-05-31 01:09:37 -07001// SPDX-License-Identifier: GPL-2.0-only
Dave Airlie414c4532012-04-17 15:01:25 +01002/*
3 * Copyright 2010 Matt Turner.
4 * Copyright 2012 Red Hat
5 *
Dave Airlie414c4532012-04-17 15:01:25 +01006 * Authors: Matthew Garrett
7 * Matt Turner
8 * Dave Airlie
9 */
10
11#include <linux/delay.h>
12
David Howells760285e2012-10-02 18:01:07 +010013#include <drm/drm_crtc_helper.h>
Sam Ravnborg9f397802019-06-23 12:35:42 +020014#include <drm/drm_fourcc.h>
15#include <drm/drm_pci.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010016#include <drm/drm_plane_helper.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010017#include <drm/drm_probe_helper.h>
Dave Airlie414c4532012-04-17 15:01:25 +010018
19#include "mgag200_drv.h"
20
21#define MGAG200_LUT_SIZE 256
22
23/*
24 * This file contains setup code for the CRTC.
25 */
26
27static void mga_crtc_load_lut(struct drm_crtc *crtc)
28{
Dave Airlie414c4532012-04-17 15:01:25 +010029 struct drm_device *dev = crtc->dev;
30 struct mga_device *mdev = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070031 struct drm_framebuffer *fb = crtc->primary->fb;
Peter Rosin9ed85e12017-07-13 18:25:34 +020032 u16 *r_ptr, *g_ptr, *b_ptr;
Dave Airlie414c4532012-04-17 15:01:25 +010033 int i;
34
35 if (!crtc->enabled)
36 return;
37
Peter Rosin9ed85e12017-07-13 18:25:34 +020038 r_ptr = crtc->gamma_store;
39 g_ptr = r_ptr + crtc->gamma_size;
40 b_ptr = g_ptr + crtc->gamma_size;
41
Dave Airlie414c4532012-04-17 15:01:25 +010042 WREG8(DAC_INDEX + MGA1064_INDEX, 0);
43
Ville Syrjälä272725c2016-12-14 23:32:20 +020044 if (fb && fb->format->cpp[0] * 8 == 16) {
Ville Syrjäläb00c6002016-12-14 23:31:35 +020045 int inc = (fb->format->depth == 15) ? 8 : 4;
Egbert Eichde7500e2013-07-17 15:07:27 +020046 u8 r, b;
47 for (i = 0; i < MGAG200_LUT_SIZE; i += inc) {
Ville Syrjäläb00c6002016-12-14 23:31:35 +020048 if (fb->format->depth == 16) {
Egbert Eichde7500e2013-07-17 15:07:27 +020049 if (i > (MGAG200_LUT_SIZE >> 1)) {
50 r = b = 0;
51 } else {
Peter Rosin9ed85e12017-07-13 18:25:34 +020052 r = *r_ptr++ >> 8;
53 b = *b_ptr++ >> 8;
54 r_ptr++;
55 b_ptr++;
Egbert Eichde7500e2013-07-17 15:07:27 +020056 }
57 } else {
Peter Rosin9ed85e12017-07-13 18:25:34 +020058 r = *r_ptr++ >> 8;
59 b = *b_ptr++ >> 8;
Egbert Eichde7500e2013-07-17 15:07:27 +020060 }
61 /* VGA registers */
62 WREG8(DAC_INDEX + MGA1064_COL_PAL, r);
Peter Rosin9ed85e12017-07-13 18:25:34 +020063 WREG8(DAC_INDEX + MGA1064_COL_PAL, *g_ptr++ >> 8);
Egbert Eichde7500e2013-07-17 15:07:27 +020064 WREG8(DAC_INDEX + MGA1064_COL_PAL, b);
65 }
66 return;
67 }
Dave Airlie414c4532012-04-17 15:01:25 +010068 for (i = 0; i < MGAG200_LUT_SIZE; i++) {
69 /* VGA registers */
Peter Rosin9ed85e12017-07-13 18:25:34 +020070 WREG8(DAC_INDEX + MGA1064_COL_PAL, *r_ptr++ >> 8);
71 WREG8(DAC_INDEX + MGA1064_COL_PAL, *g_ptr++ >> 8);
72 WREG8(DAC_INDEX + MGA1064_COL_PAL, *b_ptr++ >> 8);
Dave Airlie414c4532012-04-17 15:01:25 +010073 }
74}
75
76static inline void mga_wait_vsync(struct mga_device *mdev)
77{
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000078 unsigned long timeout = jiffies + HZ/10;
Dave Airlie414c4532012-04-17 15:01:25 +010079 unsigned int status = 0;
80
81 do {
82 status = RREG32(MGAREG_Status);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000083 } while ((status & 0x08) && time_before(jiffies, timeout));
84 timeout = jiffies + HZ/10;
Dave Airlie414c4532012-04-17 15:01:25 +010085 status = 0;
86 do {
87 status = RREG32(MGAREG_Status);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000088 } while (!(status & 0x08) && time_before(jiffies, timeout));
Dave Airlie414c4532012-04-17 15:01:25 +010089}
90
91static inline void mga_wait_busy(struct mga_device *mdev)
92{
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000093 unsigned long timeout = jiffies + HZ;
Dave Airlie414c4532012-04-17 15:01:25 +010094 unsigned int status = 0;
95 do {
96 status = RREG8(MGAREG_Status + 2);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000097 } while ((status & 0x01) && time_before(jiffies, timeout));
Dave Airlie414c4532012-04-17 15:01:25 +010098}
99
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400100#define P_ARRAY_SIZE 9
101
Dave Airlie414c4532012-04-17 15:01:25 +0100102static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
103{
104 unsigned int vcomax, vcomin, pllreffreq;
105 unsigned int delta, tmpdelta, permitteddelta;
106 unsigned int testp, testm, testn;
107 unsigned int p, m, n;
108 unsigned int computed;
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400109 unsigned int pvalues_e4[P_ARRAY_SIZE] = {16, 14, 12, 10, 8, 6, 4, 2, 1};
110 unsigned int fvv;
111 unsigned int i;
Dave Airlie414c4532012-04-17 15:01:25 +0100112
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400113 if (mdev->unique_rev_id <= 0x03) {
Dave Airlie414c4532012-04-17 15:01:25 +0100114
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400115 m = n = p = 0;
116 vcomax = 320000;
117 vcomin = 160000;
118 pllreffreq = 25000;
Dave Airlie414c4532012-04-17 15:01:25 +0100119
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400120 delta = 0xffffffff;
121 permitteddelta = clock * 5 / 1000;
Dave Airlie414c4532012-04-17 15:01:25 +0100122
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400123 for (testp = 8; testp > 0; testp /= 2) {
124 if (clock * testp > vcomax)
125 continue;
126 if (clock * testp < vcomin)
127 continue;
128
129 for (testn = 17; testn < 256; testn++) {
130 for (testm = 1; testm < 32; testm++) {
131 computed = (pllreffreq * testn) /
132 (testm * testp);
133 if (computed > clock)
134 tmpdelta = computed - clock;
135 else
136 tmpdelta = clock - computed;
137 if (tmpdelta < delta) {
138 delta = tmpdelta;
139 m = testm - 1;
140 n = testn - 1;
141 p = testp - 1;
142 }
Dave Airlie414c4532012-04-17 15:01:25 +0100143 }
144 }
145 }
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400146 } else {
147
148
149 m = n = p = 0;
150 vcomax = 1600000;
151 vcomin = 800000;
152 pllreffreq = 25000;
153
154 if (clock < 25000)
155 clock = 25000;
156
157 clock = clock * 2;
158
159 delta = 0xFFFFFFFF;
160 /* Permited delta is 0.5% as VESA Specification */
161 permitteddelta = clock * 5 / 1000;
162
163 for (i = 0 ; i < P_ARRAY_SIZE ; i++) {
164 testp = pvalues_e4[i];
165
166 if ((clock * testp) > vcomax)
167 continue;
168 if ((clock * testp) < vcomin)
169 continue;
170
171 for (testn = 50; testn <= 256; testn++) {
172 for (testm = 1; testm <= 32; testm++) {
173 computed = (pllreffreq * testn) /
174 (testm * testp);
175 if (computed > clock)
176 tmpdelta = computed - clock;
177 else
178 tmpdelta = clock - computed;
179
180 if (tmpdelta < delta) {
181 delta = tmpdelta;
182 m = testm - 1;
183 n = testn - 1;
184 p = testp - 1;
185 }
186 }
187 }
188 }
189
Mathieu Larouched3922b62016-05-27 15:12:50 -0400190 fvv = pllreffreq * (n + 1) / (m + 1);
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400191 fvv = (fvv - 800000) / 50000;
192
193 if (fvv > 15)
194 fvv = 15;
195
196 p |= (fvv << 4);
197 m |= 0x80;
198
199 clock = clock / 2;
Dave Airlie414c4532012-04-17 15:01:25 +0100200 }
201
202 if (delta > permitteddelta) {
Joe Perches8dfe1622017-02-28 04:55:54 -0800203 pr_warn("PLL delta too large\n");
Dave Airlie414c4532012-04-17 15:01:25 +0100204 return 1;
205 }
206
207 WREG_DAC(MGA1064_PIX_PLLC_M, m);
208 WREG_DAC(MGA1064_PIX_PLLC_N, n);
209 WREG_DAC(MGA1064_PIX_PLLC_P, p);
Mathieu Larouched3922b62016-05-27 15:12:50 -0400210
211 if (mdev->unique_rev_id >= 0x04) {
212 WREG_DAC(0x1a, 0x09);
213 msleep(20);
214 WREG_DAC(0x1a, 0x01);
215
216 }
217
Dave Airlie414c4532012-04-17 15:01:25 +0100218 return 0;
219}
220
221static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
222{
223 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530224 unsigned int delta, tmpdelta;
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400225 unsigned int testp, testm, testn, testp2;
Dave Airlie414c4532012-04-17 15:01:25 +0100226 unsigned int p, m, n;
227 unsigned int computed;
228 int i, j, tmpcount, vcount;
229 bool pll_locked = false;
230 u8 tmp;
231
232 m = n = p = 0;
Dave Airlie414c4532012-04-17 15:01:25 +0100233
234 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100235
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400236 if (mdev->type == G200_EW3) {
Dave Airlie414c4532012-04-17 15:01:25 +0100237
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400238 vcomax = 800000;
239 vcomin = 400000;
240 pllreffreq = 25000;
241
242 for (testp = 1; testp < 8; testp++) {
243 for (testp2 = 1; testp2 < 8; testp2++) {
244 if (testp < testp2)
245 continue;
246 if ((clock * testp * testp2) > vcomax)
247 continue;
248 if ((clock * testp * testp2) < vcomin)
249 continue;
250 for (testm = 1; testm < 26; testm++) {
251 for (testn = 32; testn < 2048 ; testn++) {
252 computed = (pllreffreq * testn) /
253 (testm * testp * testp2);
254 if (computed > clock)
255 tmpdelta = computed - clock;
256 else
257 tmpdelta = clock - computed;
258 if (tmpdelta < delta) {
259 delta = tmpdelta;
260 m = ((testn & 0x100) >> 1) |
261 (testm);
262 n = (testn & 0xFF);
263 p = ((testn & 0x600) >> 3) |
264 (testp2 << 3) |
265 (testp);
266 }
267 }
268 }
269 }
270 }
271 } else {
272
273 vcomax = 550000;
274 vcomin = 150000;
275 pllreffreq = 48000;
276
277 for (testp = 1; testp < 9; testp++) {
278 if (clock * testp > vcomax)
279 continue;
280 if (clock * testp < vcomin)
281 continue;
282
283 for (testm = 1; testm < 17; testm++) {
284 for (testn = 1; testn < 151; testn++) {
285 computed = (pllreffreq * testn) /
286 (testm * testp);
287 if (computed > clock)
288 tmpdelta = computed - clock;
289 else
290 tmpdelta = clock - computed;
291 if (tmpdelta < delta) {
292 delta = tmpdelta;
293 n = testn - 1;
294 m = (testm - 1) |
295 ((n >> 1) & 0x80);
296 p = testp - 1;
297 }
Dave Airlie414c4532012-04-17 15:01:25 +0100298 }
299 }
300 }
301 }
302
303 for (i = 0; i <= 32 && pll_locked == false; i++) {
304 if (i > 0) {
305 WREG8(MGAREG_CRTC_INDEX, 0x1e);
306 tmp = RREG8(MGAREG_CRTC_DATA);
307 if (tmp < 0xff)
308 WREG8(MGAREG_CRTC_DATA, tmp+1);
309 }
310
311 /* set pixclkdis to 1 */
312 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
313 tmp = RREG8(DAC_DATA);
314 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000315 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100316
317 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
318 tmp = RREG8(DAC_DATA);
319 tmp |= MGA1064_REMHEADCTL_CLKDIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000320 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100321
322 /* select PLL Set C */
323 tmp = RREG8(MGAREG_MEM_MISC_READ);
324 tmp |= 0x3 << 2;
325 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
326
327 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
328 tmp = RREG8(DAC_DATA);
329 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000330 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100331
332 udelay(500);
333
334 /* reset the PLL */
335 WREG8(DAC_INDEX, MGA1064_VREF_CTL);
336 tmp = RREG8(DAC_DATA);
337 tmp &= ~0x04;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000338 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100339
340 udelay(50);
341
342 /* program pixel pll register */
343 WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
344 WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
345 WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
346
347 udelay(50);
348
349 /* turn pll on */
350 WREG8(DAC_INDEX, MGA1064_VREF_CTL);
351 tmp = RREG8(DAC_DATA);
352 tmp |= 0x04;
353 WREG_DAC(MGA1064_VREF_CTL, tmp);
354
355 udelay(500);
356
357 /* select the pixel pll */
358 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
359 tmp = RREG8(DAC_DATA);
360 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
361 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000362 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100363
364 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
365 tmp = RREG8(DAC_DATA);
366 tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
367 tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000368 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100369
370 /* reset dotclock rate bit */
371 WREG8(MGAREG_SEQ_INDEX, 1);
372 tmp = RREG8(MGAREG_SEQ_DATA);
373 tmp &= ~0x8;
374 WREG8(MGAREG_SEQ_DATA, tmp);
375
376 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
377 tmp = RREG8(DAC_DATA);
378 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000379 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100380
381 vcount = RREG8(MGAREG_VCOUNT);
382
383 for (j = 0; j < 30 && pll_locked == false; j++) {
384 tmpcount = RREG8(MGAREG_VCOUNT);
385 if (tmpcount < vcount)
386 vcount = 0;
387 if ((tmpcount - vcount) > 2)
388 pll_locked = true;
389 else
390 udelay(5);
391 }
392 }
393 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
394 tmp = RREG8(DAC_DATA);
395 tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
396 WREG_DAC(MGA1064_REMHEADCTL, tmp);
397 return 0;
398}
399
400static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
401{
402 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530403 unsigned int delta, tmpdelta;
Dave Airlie414c4532012-04-17 15:01:25 +0100404 unsigned int testp, testm, testn;
405 unsigned int p, m, n;
406 unsigned int computed;
407 u8 tmp;
408
409 m = n = p = 0;
410 vcomax = 550000;
411 vcomin = 150000;
412 pllreffreq = 50000;
413
414 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100415
416 for (testp = 16; testp > 0; testp--) {
417 if (clock * testp > vcomax)
418 continue;
419 if (clock * testp < vcomin)
420 continue;
421
422 for (testn = 1; testn < 257; testn++) {
423 for (testm = 1; testm < 17; testm++) {
424 computed = (pllreffreq * testn) /
425 (testm * testp);
426 if (computed > clock)
427 tmpdelta = computed - clock;
428 else
429 tmpdelta = clock - computed;
430 if (tmpdelta < delta) {
431 delta = tmpdelta;
432 n = testn - 1;
433 m = testm - 1;
434 p = testp - 1;
435 }
436 }
437 }
438 }
439
440 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
441 tmp = RREG8(DAC_DATA);
442 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000443 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100444
445 tmp = RREG8(MGAREG_MEM_MISC_READ);
446 tmp |= 0x3 << 2;
447 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
448
449 WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
450 tmp = RREG8(DAC_DATA);
Christopher Harveyfb70a662013-04-12 22:24:05 +0000451 WREG8(DAC_DATA, tmp & ~0x40);
Dave Airlie414c4532012-04-17 15:01:25 +0100452
453 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
454 tmp = RREG8(DAC_DATA);
455 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000456 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100457
458 WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
459 WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
460 WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
461
462 udelay(50);
463
464 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
465 tmp = RREG8(DAC_DATA);
466 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000467 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100468
469 udelay(500);
470
471 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
472 tmp = RREG8(DAC_DATA);
473 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
474 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000475 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100476
477 WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
478 tmp = RREG8(DAC_DATA);
Christopher Harveyfb70a662013-04-12 22:24:05 +0000479 WREG8(DAC_DATA, tmp | 0x40);
Dave Airlie414c4532012-04-17 15:01:25 +0100480
481 tmp = RREG8(MGAREG_MEM_MISC_READ);
482 tmp |= (0x3 << 2);
483 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
484
485 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
486 tmp = RREG8(DAC_DATA);
487 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000488 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100489
490 return 0;
491}
492
493static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
494{
495 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530496 unsigned int delta, tmpdelta;
Dave Airlie414c4532012-04-17 15:01:25 +0100497 unsigned int testp, testm, testn;
498 unsigned int p, m, n;
499 unsigned int computed;
500 int i, j, tmpcount, vcount;
501 u8 tmp;
502 bool pll_locked = false;
503
504 m = n = p = 0;
Dave Airlie414c4532012-04-17 15:01:25 +0100505
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400506 if (mdev->type == G200_EH3) {
507 vcomax = 3000000;
508 vcomin = 1500000;
509 pllreffreq = 25000;
Dave Airlie414c4532012-04-17 15:01:25 +0100510
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400511 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100512
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400513 testp = 0;
514
515 for (testm = 150; testm >= 6; testm--) {
516 if (clock * testm > vcomax)
517 continue;
518 if (clock * testm < vcomin)
519 continue;
520 for (testn = 120; testn >= 60; testn--) {
521 computed = (pllreffreq * testn) / testm;
Dave Airlie414c4532012-04-17 15:01:25 +0100522 if (computed > clock)
523 tmpdelta = computed - clock;
524 else
525 tmpdelta = clock - computed;
526 if (tmpdelta < delta) {
527 delta = tmpdelta;
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400528 n = testn;
529 m = testm;
530 p = testp;
Dave Airlie414c4532012-04-17 15:01:25 +0100531 }
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400532 if (delta == 0)
533 break;
534 }
535 if (delta == 0)
536 break;
537 }
538 } else {
539
540 vcomax = 800000;
541 vcomin = 400000;
542 pllreffreq = 33333;
543
544 delta = 0xffffffff;
545
546 for (testp = 16; testp > 0; testp >>= 1) {
547 if (clock * testp > vcomax)
548 continue;
549 if (clock * testp < vcomin)
550 continue;
551
552 for (testm = 1; testm < 33; testm++) {
553 for (testn = 17; testn < 257; testn++) {
554 computed = (pllreffreq * testn) /
555 (testm * testp);
556 if (computed > clock)
557 tmpdelta = computed - clock;
558 else
559 tmpdelta = clock - computed;
560 if (tmpdelta < delta) {
561 delta = tmpdelta;
562 n = testn - 1;
563 m = (testm - 1);
564 p = testp - 1;
565 }
566 if ((clock * testp) >= 600000)
567 p |= 0x80;
568 }
Dave Airlie414c4532012-04-17 15:01:25 +0100569 }
570 }
571 }
572 for (i = 0; i <= 32 && pll_locked == false; i++) {
573 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
574 tmp = RREG8(DAC_DATA);
575 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000576 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100577
578 tmp = RREG8(MGAREG_MEM_MISC_READ);
579 tmp |= 0x3 << 2;
580 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
581
582 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
583 tmp = RREG8(DAC_DATA);
584 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000585 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100586
587 udelay(500);
588
589 WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
590 WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
591 WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
592
593 udelay(500);
594
595 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
596 tmp = RREG8(DAC_DATA);
597 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
598 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000599 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100600
601 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
602 tmp = RREG8(DAC_DATA);
603 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
604 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000605 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100606
607 vcount = RREG8(MGAREG_VCOUNT);
608
609 for (j = 0; j < 30 && pll_locked == false; j++) {
610 tmpcount = RREG8(MGAREG_VCOUNT);
611 if (tmpcount < vcount)
612 vcount = 0;
613 if ((tmpcount - vcount) > 2)
614 pll_locked = true;
615 else
616 udelay(5);
617 }
618 }
619
620 return 0;
621}
622
623static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
624{
625 unsigned int vcomax, vcomin, pllreffreq;
626 unsigned int delta, tmpdelta;
Dave Airlie98306052012-08-09 15:00:15 +1000627 int testr, testn, testm, testo;
Dave Airlie414c4532012-04-17 15:01:25 +0100628 unsigned int p, m, n;
Dave Airlie98306052012-08-09 15:00:15 +1000629 unsigned int computed, vco;
Dave Airlie414c4532012-04-17 15:01:25 +0100630 int tmp;
Dave Airlie98306052012-08-09 15:00:15 +1000631 const unsigned int m_div_val[] = { 1, 2, 4, 8 };
Dave Airlie414c4532012-04-17 15:01:25 +0100632
633 m = n = p = 0;
634 vcomax = 1488000;
635 vcomin = 1056000;
636 pllreffreq = 48000;
637
638 delta = 0xffffffff;
639
640 for (testr = 0; testr < 4; testr++) {
641 if (delta == 0)
642 break;
643 for (testn = 5; testn < 129; testn++) {
644 if (delta == 0)
645 break;
646 for (testm = 3; testm >= 0; testm--) {
647 if (delta == 0)
648 break;
649 for (testo = 5; testo < 33; testo++) {
Dave Airlie98306052012-08-09 15:00:15 +1000650 vco = pllreffreq * (testn + 1) /
Dave Airlie414c4532012-04-17 15:01:25 +0100651 (testr + 1);
Dave Airlie98306052012-08-09 15:00:15 +1000652 if (vco < vcomin)
Dave Airlie414c4532012-04-17 15:01:25 +0100653 continue;
Dave Airlie98306052012-08-09 15:00:15 +1000654 if (vco > vcomax)
Dave Airlie414c4532012-04-17 15:01:25 +0100655 continue;
Dave Airlie98306052012-08-09 15:00:15 +1000656 computed = vco / (m_div_val[testm] * (testo + 1));
Dave Airlie414c4532012-04-17 15:01:25 +0100657 if (computed > clock)
658 tmpdelta = computed - clock;
659 else
660 tmpdelta = clock - computed;
661 if (tmpdelta < delta) {
662 delta = tmpdelta;
663 m = testm | (testo << 3);
664 n = testn;
665 p = testr | (testr << 3);
666 }
667 }
668 }
669 }
670 }
671
672 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
673 tmp = RREG8(DAC_DATA);
674 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000675 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100676
677 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
678 tmp = RREG8(DAC_DATA);
679 tmp |= MGA1064_REMHEADCTL_CLKDIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000680 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100681
682 tmp = RREG8(MGAREG_MEM_MISC_READ);
683 tmp |= (0x3<<2) | 0xc0;
684 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
685
686 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
687 tmp = RREG8(DAC_DATA);
688 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
689 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000690 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100691
692 udelay(500);
693
694 WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
695 WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
696 WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
697
698 udelay(50);
699
700 return 0;
701}
702
703static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
704{
705 switch(mdev->type) {
706 case G200_SE_A:
707 case G200_SE_B:
708 return mga_g200se_set_plls(mdev, clock);
709 break;
710 case G200_WB:
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400711 case G200_EW3:
Dave Airlie414c4532012-04-17 15:01:25 +0100712 return mga_g200wb_set_plls(mdev, clock);
713 break;
714 case G200_EV:
715 return mga_g200ev_set_plls(mdev, clock);
716 break;
717 case G200_EH:
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400718 case G200_EH3:
Dave Airlie414c4532012-04-17 15:01:25 +0100719 return mga_g200eh_set_plls(mdev, clock);
720 break;
721 case G200_ER:
722 return mga_g200er_set_plls(mdev, clock);
723 break;
724 }
725 return 0;
726}
727
728static void mga_g200wb_prepare(struct drm_crtc *crtc)
729{
730 struct mga_device *mdev = crtc->dev->dev_private;
731 u8 tmp;
732 int iter_max;
733
734 /* 1- The first step is to warn the BMC of an upcoming mode change.
735 * We are putting the misc<0> to output.*/
736
737 WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
738 tmp = RREG8(DAC_DATA);
739 tmp |= 0x10;
740 WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
741
742 /* we are putting a 1 on the misc<0> line */
743 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
744 tmp = RREG8(DAC_DATA);
745 tmp |= 0x10;
746 WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
747
748 /* 2- Second step to mask and further scan request
749 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
750 */
751 WREG8(DAC_INDEX, MGA1064_SPAREREG);
752 tmp = RREG8(DAC_DATA);
753 tmp |= 0x80;
754 WREG_DAC(MGA1064_SPAREREG, tmp);
755
756 /* 3a- the third step is to verifu if there is an active scan
757 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
758 */
759 iter_max = 300;
760 while (!(tmp & 0x1) && iter_max) {
761 WREG8(DAC_INDEX, MGA1064_SPAREREG);
762 tmp = RREG8(DAC_DATA);
763 udelay(1000);
764 iter_max--;
765 }
766
767 /* 3b- this step occurs only if the remove is actually scanning
768 * we are waiting for the end of the frame which is a 1 on
769 * remvsyncsts (XSPAREREG<1>)
770 */
771 if (iter_max) {
772 iter_max = 300;
773 while ((tmp & 0x2) && iter_max) {
774 WREG8(DAC_INDEX, MGA1064_SPAREREG);
775 tmp = RREG8(DAC_DATA);
776 udelay(1000);
777 iter_max--;
778 }
779 }
780}
781
782static void mga_g200wb_commit(struct drm_crtc *crtc)
783{
784 u8 tmp;
785 struct mga_device *mdev = crtc->dev->dev_private;
786
787 /* 1- The first step is to ensure that the vrsten and hrsten are set */
788 WREG8(MGAREG_CRTCEXT_INDEX, 1);
789 tmp = RREG8(MGAREG_CRTCEXT_DATA);
790 WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
791
792 /* 2- second step is to assert the rstlvl2 */
793 WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
794 tmp = RREG8(DAC_DATA);
795 tmp |= 0x8;
796 WREG8(DAC_DATA, tmp);
797
798 /* wait 10 us */
799 udelay(10);
800
801 /* 3- deassert rstlvl2 */
802 tmp &= ~0x08;
803 WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
804 WREG8(DAC_DATA, tmp);
805
806 /* 4- remove mask of scan request */
807 WREG8(DAC_INDEX, MGA1064_SPAREREG);
808 tmp = RREG8(DAC_DATA);
809 tmp &= ~0x80;
810 WREG8(DAC_DATA, tmp);
811
812 /* 5- put back a 0 on the misc<0> line */
813 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
814 tmp = RREG8(DAC_DATA);
815 tmp &= ~0x10;
816 WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
817}
818
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000819/*
820 This is how the framebuffer base address is stored in g200 cards:
821 * Assume @offset is the gpu_addr variable of the framebuffer object
822 * Then addr is the number of _pixels_ (not bytes) from the start of
823 VRAM to the first pixel we want to display. (divided by 2 for 32bit
824 framebuffers)
825 * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
826 addr<20> -> CRTCEXT0<6>
827 addr<19-16> -> CRTCEXT0<3-0>
828 addr<15-8> -> CRTCC<7-0>
829 addr<7-0> -> CRTCD<7-0>
830 CRTCEXT0 has to be programmed last to trigger an update and make the
831 new addr variable take effect.
832 */
Rashika080fd6b2014-01-06 20:37:15 +0530833static void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
Dave Airlie414c4532012-04-17 15:01:25 +0100834{
835 struct mga_device *mdev = crtc->dev->dev_private;
836 u32 addr;
837 int count;
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000838 u8 crtcext0;
Dave Airlie414c4532012-04-17 15:01:25 +0100839
840 while (RREG8(0x1fda) & 0x08);
841 while (!(RREG8(0x1fda) & 0x08));
842
843 count = RREG8(MGAREG_VCOUNT) + 2;
844 while (RREG8(MGAREG_VCOUNT) < count);
845
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000846 WREG8(MGAREG_CRTCEXT_INDEX, 0);
847 crtcext0 = RREG8(MGAREG_CRTCEXT_DATA);
848 crtcext0 &= 0xB0;
849 addr = offset / 8;
850 /* Can't store addresses any higher than that...
851 but we also don't have more than 16MB of memory, so it should be fine. */
852 WARN_ON(addr > 0x1fffff);
853 crtcext0 |= (!!(addr & (1<<20)))<<6;
Dave Airlie414c4532012-04-17 15:01:25 +0100854 WREG_CRT(0x0d, (u8)(addr & 0xff));
855 WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000856 WREG_ECRT(0x0, ((u8)(addr >> 16) & 0xf) | crtcext0);
Dave Airlie414c4532012-04-17 15:01:25 +0100857}
858
Dave Airlie414c4532012-04-17 15:01:25 +0100859static int mga_crtc_do_set_base(struct drm_crtc *crtc,
860 struct drm_framebuffer *fb,
861 int x, int y, int atomic)
862{
863 struct mga_device *mdev = crtc->dev->dev_private;
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200864 struct drm_gem_vram_object *gbo;
Dave Airlie414c4532012-04-17 15:01:25 +0100865 int ret;
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200866 s64 gpu_addr;
Thomas Zimmermanna7581342019-05-08 10:26:26 +0200867 void *base;
Dave Airlie414c4532012-04-17 15:01:25 +0100868
Dave Airlie414c4532012-04-17 15:01:25 +0100869 if (!atomic && fb) {
Thomas Zimmermann5d177182019-06-27 10:09:09 +0200870 gbo = drm_gem_vram_of_gem(fb->obj[0]);
Thomas Zimmermann81da87f2019-05-21 13:08:29 +0200871
872 /* unmap if console */
Thomas Zimmermann5d177182019-06-27 10:09:09 +0200873 if (mdev->mfbdev->helper.fb == fb)
Thomas Zimmermann81da87f2019-05-21 13:08:29 +0200874 drm_gem_vram_kunmap(gbo);
875 drm_gem_vram_unpin(gbo);
Dave Airlie414c4532012-04-17 15:01:25 +0100876 }
877
Thomas Zimmermann5d177182019-06-27 10:09:09 +0200878 gbo = drm_gem_vram_of_gem(crtc->primary->fb->obj[0]);
Dave Airlie414c4532012-04-17 15:01:25 +0100879
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200880 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
881 if (ret)
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200882 return ret;
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200883 gpu_addr = drm_gem_vram_offset(gbo);
884 if (gpu_addr < 0) {
885 ret = (int)gpu_addr;
886 goto err_drm_gem_vram_unpin;
Dave Airlie414c4532012-04-17 15:01:25 +0100887 }
888
Thomas Zimmermann5d177182019-06-27 10:09:09 +0200889 if (mdev->mfbdev->helper.fb == crtc->primary->fb) {
Dave Airlie414c4532012-04-17 15:01:25 +0100890 /* if pushing console in kmap it */
Thomas Zimmermanna7581342019-05-08 10:26:26 +0200891 base = drm_gem_vram_kmap(gbo, true, NULL);
892 if (IS_ERR(base)) {
893 ret = PTR_ERR(base);
Dave Airlie414c4532012-04-17 15:01:25 +0100894 DRM_ERROR("failed to kmap fbcon\n");
Thomas Zimmermanna7581342019-05-08 10:26:26 +0200895 }
Dave Airlie414c4532012-04-17 15:01:25 +0100896 }
Thomas Zimmermanna7581342019-05-08 10:26:26 +0200897
Dave Airlie414c4532012-04-17 15:01:25 +0100898 mga_set_start_address(crtc, (u32)gpu_addr);
899
900 return 0;
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200901
902err_drm_gem_vram_unpin:
903 drm_gem_vram_unpin(gbo);
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200904 return ret;
Dave Airlie414c4532012-04-17 15:01:25 +0100905}
906
907static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
908 struct drm_framebuffer *old_fb)
909{
910 return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
911}
912
913static int mga_crtc_mode_set(struct drm_crtc *crtc,
914 struct drm_display_mode *mode,
915 struct drm_display_mode *adjusted_mode,
916 int x, int y, struct drm_framebuffer *old_fb)
917{
918 struct drm_device *dev = crtc->dev;
919 struct mga_device *mdev = dev->dev_private;
Ville Syrjälä72952752016-11-18 21:52:40 +0200920 const struct drm_framebuffer *fb = crtc->primary->fb;
Dave Airlie414c4532012-04-17 15:01:25 +0100921 int hdisplay, hsyncstart, hsyncend, htotal;
922 int vdisplay, vsyncstart, vsyncend, vtotal;
923 int pitch;
924 int option = 0, option2 = 0;
925 int i;
926 unsigned char misc = 0;
927 unsigned char ext_vga[6];
Dave Airlie414c4532012-04-17 15:01:25 +0100928 u8 bppshift;
929
930 static unsigned char dacvalue[] = {
931 /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
932 /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
933 /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
934 /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
935 /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
936 /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
937 /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
938 /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
939 /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
940 /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
941 };
942
Ville Syrjälä272725c2016-12-14 23:32:20 +0200943 bppshift = mdev->bpp_shifts[fb->format->cpp[0] - 1];
Dave Airlie414c4532012-04-17 15:01:25 +0100944
945 switch (mdev->type) {
946 case G200_SE_A:
947 case G200_SE_B:
948 dacvalue[MGA1064_VREF_CTL] = 0x03;
949 dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
950 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
951 MGA1064_MISC_CTL_VGA8 |
952 MGA1064_MISC_CTL_DAC_RAM_CS;
953 if (mdev->has_sdram)
954 option = 0x40049120;
955 else
956 option = 0x4004d120;
957 option2 = 0x00008000;
958 break;
959 case G200_WB:
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400960 case G200_EW3:
Dave Airlie414c4532012-04-17 15:01:25 +0100961 dacvalue[MGA1064_VREF_CTL] = 0x07;
962 option = 0x41049120;
963 option2 = 0x0000b000;
964 break;
965 case G200_EV:
966 dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
967 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
968 MGA1064_MISC_CTL_DAC_RAM_CS;
969 option = 0x00000120;
970 option2 = 0x0000b000;
971 break;
972 case G200_EH:
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400973 case G200_EH3:
Dave Airlie414c4532012-04-17 15:01:25 +0100974 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
975 MGA1064_MISC_CTL_DAC_RAM_CS;
976 option = 0x00000120;
977 option2 = 0x0000b000;
978 break;
979 case G200_ER:
Dave Airlie414c4532012-04-17 15:01:25 +0100980 break;
981 }
982
Ville Syrjälä272725c2016-12-14 23:32:20 +0200983 switch (fb->format->cpp[0] * 8) {
Dave Airlie414c4532012-04-17 15:01:25 +0100984 case 8:
985 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
986 break;
987 case 16:
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200988 if (fb->format->depth == 15)
Dave Airlie414c4532012-04-17 15:01:25 +0100989 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
990 else
991 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
992 break;
993 case 24:
994 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
995 break;
996 case 32:
997 dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
998 break;
999 }
1000
1001 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1002 misc |= 0x40;
1003 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1004 misc |= 0x80;
1005
1006
1007 for (i = 0; i < sizeof(dacvalue); i++) {
Christopher Harvey9d8aa552013-04-12 20:42:19 +00001008 if ((i <= 0x17) ||
Dave Airlie414c4532012-04-17 15:01:25 +01001009 (i == 0x1b) ||
1010 (i == 0x1c) ||
1011 ((i >= 0x1f) && (i <= 0x29)) ||
1012 ((i >= 0x30) && (i <= 0x37)))
1013 continue;
1014 if (IS_G200_SE(mdev) &&
1015 ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
1016 continue;
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001017 if ((mdev->type == G200_EV ||
1018 mdev->type == G200_WB ||
1019 mdev->type == G200_EH ||
Mathieu Larouchef0493e62016-10-21 12:47:07 -04001020 mdev->type == G200_EW3 ||
1021 mdev->type == G200_EH3) &&
Dave Airlie414c4532012-04-17 15:01:25 +01001022 (i >= 0x44) && (i <= 0x4e))
1023 continue;
1024
1025 WREG_DAC(i, dacvalue[i]);
1026 }
1027
Christopher Harvey1812a3d2013-04-05 10:51:15 -04001028 if (mdev->type == G200_ER)
1029 WREG_DAC(0x90, 0);
Dave Airlie414c4532012-04-17 15:01:25 +01001030
1031 if (option)
1032 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
1033 if (option2)
1034 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
1035
1036 WREG_SEQ(2, 0xf);
1037 WREG_SEQ(3, 0);
1038 WREG_SEQ(4, 0xe);
1039
Ville Syrjälä272725c2016-12-14 23:32:20 +02001040 pitch = fb->pitches[0] / fb->format->cpp[0];
1041 if (fb->format->cpp[0] * 8 == 24)
Takashi Iwaida558392013-07-17 15:07:26 +02001042 pitch = (pitch * 3) >> (4 - bppshift);
Dave Airlie414c4532012-04-17 15:01:25 +01001043 else
1044 pitch = pitch >> (4 - bppshift);
1045
1046 hdisplay = mode->hdisplay / 8 - 1;
1047 hsyncstart = mode->hsync_start / 8 - 1;
1048 hsyncend = mode->hsync_end / 8 - 1;
1049 htotal = mode->htotal / 8 - 1;
1050
1051 /* Work around hardware quirk */
1052 if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
1053 htotal++;
1054
1055 vdisplay = mode->vdisplay - 1;
1056 vsyncstart = mode->vsync_start - 1;
1057 vsyncend = mode->vsync_end - 1;
1058 vtotal = mode->vtotal - 2;
1059
1060 WREG_GFX(0, 0);
1061 WREG_GFX(1, 0);
1062 WREG_GFX(2, 0);
1063 WREG_GFX(3, 0);
1064 WREG_GFX(4, 0);
1065 WREG_GFX(5, 0x40);
1066 WREG_GFX(6, 0x5);
1067 WREG_GFX(7, 0xf);
1068 WREG_GFX(8, 0xf);
1069
1070 WREG_CRT(0, htotal - 4);
1071 WREG_CRT(1, hdisplay);
1072 WREG_CRT(2, hdisplay);
1073 WREG_CRT(3, (htotal & 0x1F) | 0x80);
1074 WREG_CRT(4, hsyncstart);
1075 WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
1076 WREG_CRT(6, vtotal & 0xFF);
1077 WREG_CRT(7, ((vtotal & 0x100) >> 8) |
1078 ((vdisplay & 0x100) >> 7) |
1079 ((vsyncstart & 0x100) >> 6) |
1080 ((vdisplay & 0x100) >> 5) |
1081 ((vdisplay & 0x100) >> 4) | /* linecomp */
1082 ((vtotal & 0x200) >> 4)|
1083 ((vdisplay & 0x200) >> 3) |
1084 ((vsyncstart & 0x200) >> 2));
1085 WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
1086 ((vdisplay & 0x200) >> 3));
1087 WREG_CRT(10, 0);
1088 WREG_CRT(11, 0);
1089 WREG_CRT(12, 0);
1090 WREG_CRT(13, 0);
1091 WREG_CRT(14, 0);
1092 WREG_CRT(15, 0);
1093 WREG_CRT(16, vsyncstart & 0xFF);
1094 WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
1095 WREG_CRT(18, vdisplay & 0xFF);
1096 WREG_CRT(19, pitch & 0xFF);
1097 WREG_CRT(20, 0);
1098 WREG_CRT(21, vdisplay & 0xFF);
1099 WREG_CRT(22, (vtotal + 1) & 0xFF);
1100 WREG_CRT(23, 0xc3);
1101 WREG_CRT(24, vdisplay & 0xFF);
1102
1103 ext_vga[0] = 0;
1104 ext_vga[5] = 0;
1105
1106 /* TODO interlace */
1107
1108 ext_vga[0] |= (pitch & 0x300) >> 4;
1109 ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
1110 ((hdisplay & 0x100) >> 7) |
1111 ((hsyncstart & 0x100) >> 6) |
1112 (htotal & 0x40);
1113 ext_vga[2] = ((vtotal & 0xc00) >> 10) |
1114 ((vdisplay & 0x400) >> 8) |
1115 ((vdisplay & 0xc00) >> 7) |
1116 ((vsyncstart & 0xc00) >> 5) |
1117 ((vdisplay & 0x400) >> 3);
Ville Syrjälä272725c2016-12-14 23:32:20 +02001118 if (fb->format->cpp[0] * 8 == 24)
Dave Airlie414c4532012-04-17 15:01:25 +01001119 ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
1120 else
1121 ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
1122 ext_vga[4] = 0;
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001123 if (mdev->type == G200_WB || mdev->type == G200_EW3)
Dave Airlie414c4532012-04-17 15:01:25 +01001124 ext_vga[1] |= 0x88;
1125
Dave Airlie414c4532012-04-17 15:01:25 +01001126 /* Set pixel clocks */
1127 misc = 0x2d;
1128 WREG8(MGA_MISC_OUT, misc);
1129
1130 mga_crtc_set_plls(mdev, mode->clock);
1131
1132 for (i = 0; i < 6; i++) {
1133 WREG_ECRT(i, ext_vga[i]);
1134 }
1135
1136 if (mdev->type == G200_ER)
Christopher Harvey1812a3d2013-04-05 10:51:15 -04001137 WREG_ECRT(0x24, 0x5);
Dave Airlie414c4532012-04-17 15:01:25 +01001138
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001139 if (mdev->type == G200_EW3)
1140 WREG_ECRT(0x34, 0x5);
1141
Dave Airlie414c4532012-04-17 15:01:25 +01001142 if (mdev->type == G200_EV) {
1143 WREG_ECRT(6, 0);
1144 }
1145
1146 WREG_ECRT(0, ext_vga[0]);
1147 /* Enable mga pixel clock */
1148 misc = 0x2d;
1149
1150 WREG8(MGA_MISC_OUT, misc);
1151
1152 if (adjusted_mode)
1153 memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
1154
1155 mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
1156
1157 /* reset tagfifo */
1158 if (mdev->type == G200_ER) {
1159 u32 mem_ctl = RREG32(MGAREG_MEMCTL);
1160 u8 seq1;
1161
1162 /* screen off */
1163 WREG8(MGAREG_SEQ_INDEX, 0x01);
1164 seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
1165 WREG8(MGAREG_SEQ_DATA, seq1);
1166
1167 WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
1168 udelay(1000);
1169 WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
1170
1171 WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
1172 }
1173
1174
1175 if (IS_G200_SE(mdev)) {
Mathieu Larouche0cbb7382017-06-14 10:39:42 -04001176 if (mdev->unique_rev_id >= 0x04) {
1177 WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1178 WREG8(MGAREG_CRTCEXT_DATA, 0);
1179 } else if (mdev->unique_rev_id >= 0x02) {
Dave Airlie414c4532012-04-17 15:01:25 +01001180 u8 hi_pri_lvl;
1181 u32 bpp;
1182 u32 mb;
1183
Ville Syrjälä272725c2016-12-14 23:32:20 +02001184 if (fb->format->cpp[0] * 8 > 16)
Dave Airlie414c4532012-04-17 15:01:25 +01001185 bpp = 32;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001186 else if (fb->format->cpp[0] * 8 > 8)
Dave Airlie414c4532012-04-17 15:01:25 +01001187 bpp = 16;
1188 else
1189 bpp = 8;
1190
1191 mb = (mode->clock * bpp) / 1000;
1192 if (mb > 3100)
1193 hi_pri_lvl = 0;
1194 else if (mb > 2600)
1195 hi_pri_lvl = 1;
1196 else if (mb > 1900)
1197 hi_pri_lvl = 2;
1198 else if (mb > 1160)
1199 hi_pri_lvl = 3;
1200 else if (mb > 440)
1201 hi_pri_lvl = 4;
1202 else
1203 hi_pri_lvl = 5;
1204
Christopher Harvey91f8f102013-05-31 20:33:07 +00001205 WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
1206 WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl);
Dave Airlie414c4532012-04-17 15:01:25 +01001207 } else {
Christopher Harvey91f8f102013-05-31 20:33:07 +00001208 WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
Julia Lemireabbee622013-06-27 13:38:59 -04001209 if (mdev->unique_rev_id >= 0x01)
Christopher Harvey91f8f102013-05-31 20:33:07 +00001210 WREG8(MGAREG_CRTCEXT_DATA, 0x03);
Dave Airlie414c4532012-04-17 15:01:25 +01001211 else
Christopher Harvey91f8f102013-05-31 20:33:07 +00001212 WREG8(MGAREG_CRTCEXT_DATA, 0x04);
Dave Airlie414c4532012-04-17 15:01:25 +01001213 }
1214 }
1215 return 0;
1216}
1217
1218#if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1219static int mga_suspend(struct drm_crtc *crtc)
1220{
1221 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1222 struct drm_device *dev = crtc->dev;
1223 struct mga_device *mdev = dev->dev_private;
1224 struct pci_dev *pdev = dev->pdev;
1225 int option;
1226
1227 if (mdev->suspended)
1228 return 0;
1229
1230 WREG_SEQ(1, 0x20);
1231 WREG_ECRT(1, 0x30);
1232 /* Disable the pixel clock */
1233 WREG_DAC(0x1a, 0x05);
1234 /* Power down the DAC */
1235 WREG_DAC(0x1e, 0x18);
1236 /* Power down the pixel PLL */
1237 WREG_DAC(0x1a, 0x0d);
1238
1239 /* Disable PLLs and clocks */
1240 pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1241 option &= ~(0x1F8024);
1242 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1243 pci_set_power_state(pdev, PCI_D3hot);
1244 pci_disable_device(pdev);
1245
1246 mdev->suspended = true;
1247
1248 return 0;
1249}
1250
1251static int mga_resume(struct drm_crtc *crtc)
1252{
1253 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1254 struct drm_device *dev = crtc->dev;
1255 struct mga_device *mdev = dev->dev_private;
1256 struct pci_dev *pdev = dev->pdev;
1257 int option;
1258
1259 if (!mdev->suspended)
1260 return 0;
1261
1262 pci_set_power_state(pdev, PCI_D0);
1263 pci_enable_device(pdev);
1264
1265 /* Disable sysclk */
1266 pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1267 option &= ~(0x4);
1268 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1269
1270 mdev->suspended = false;
1271
1272 return 0;
1273}
1274
1275#endif
1276
1277static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1278{
1279 struct drm_device *dev = crtc->dev;
1280 struct mga_device *mdev = dev->dev_private;
1281 u8 seq1 = 0, crtcext1 = 0;
1282
1283 switch (mode) {
1284 case DRM_MODE_DPMS_ON:
1285 seq1 = 0;
1286 crtcext1 = 0;
1287 mga_crtc_load_lut(crtc);
1288 break;
1289 case DRM_MODE_DPMS_STANDBY:
1290 seq1 = 0x20;
1291 crtcext1 = 0x10;
1292 break;
1293 case DRM_MODE_DPMS_SUSPEND:
1294 seq1 = 0x20;
1295 crtcext1 = 0x20;
1296 break;
1297 case DRM_MODE_DPMS_OFF:
1298 seq1 = 0x20;
1299 crtcext1 = 0x30;
1300 break;
1301 }
1302
1303#if 0
1304 if (mode == DRM_MODE_DPMS_OFF) {
1305 mga_suspend(crtc);
1306 }
1307#endif
1308 WREG8(MGAREG_SEQ_INDEX, 0x01);
1309 seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1310 mga_wait_vsync(mdev);
1311 mga_wait_busy(mdev);
1312 WREG8(MGAREG_SEQ_DATA, seq1);
1313 msleep(20);
1314 WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1315 crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1316 WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1317
1318#if 0
1319 if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1320 mga_resume(crtc);
1321 drm_helper_resume_force_mode(dev);
1322 }
1323#endif
1324}
1325
1326/*
1327 * This is called before a mode is programmed. A typical use might be to
1328 * enable DPMS during the programming to avoid seeing intermediate stages,
1329 * but that's not relevant to us
1330 */
1331static void mga_crtc_prepare(struct drm_crtc *crtc)
1332{
1333 struct drm_device *dev = crtc->dev;
1334 struct mga_device *mdev = dev->dev_private;
1335 u8 tmp;
1336
1337 /* mga_resume(crtc);*/
1338
1339 WREG8(MGAREG_CRTC_INDEX, 0x11);
1340 tmp = RREG8(MGAREG_CRTC_DATA);
1341 WREG_CRT(0x11, tmp | 0x80);
1342
1343 if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1344 WREG_SEQ(0, 1);
1345 msleep(50);
1346 WREG_SEQ(1, 0x20);
1347 msleep(20);
1348 } else {
1349 WREG8(MGAREG_SEQ_INDEX, 0x1);
1350 tmp = RREG8(MGAREG_SEQ_DATA);
1351
1352 /* start sync reset */
1353 WREG_SEQ(0, 1);
1354 WREG_SEQ(1, tmp | 0x20);
1355 }
1356
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001357 if (mdev->type == G200_WB || mdev->type == G200_EW3)
Dave Airlie414c4532012-04-17 15:01:25 +01001358 mga_g200wb_prepare(crtc);
1359
1360 WREG_CRT(17, 0);
1361}
1362
1363/*
1364 * This is called after a mode is programmed. It should reverse anything done
1365 * by the prepare function
1366 */
1367static void mga_crtc_commit(struct drm_crtc *crtc)
1368{
1369 struct drm_device *dev = crtc->dev;
1370 struct mga_device *mdev = dev->dev_private;
Jani Nikulad584ff82015-03-11 11:51:00 +02001371 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
Dave Airlie414c4532012-04-17 15:01:25 +01001372 u8 tmp;
1373
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001374 if (mdev->type == G200_WB || mdev->type == G200_EW3)
Dave Airlie414c4532012-04-17 15:01:25 +01001375 mga_g200wb_commit(crtc);
1376
1377 if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1378 msleep(50);
1379 WREG_SEQ(1, 0x0);
1380 msleep(20);
1381 WREG_SEQ(0, 0x3);
1382 } else {
1383 WREG8(MGAREG_SEQ_INDEX, 0x1);
1384 tmp = RREG8(MGAREG_SEQ_DATA);
1385
1386 tmp &= ~0x20;
1387 WREG_SEQ(0x1, tmp);
1388 WREG_SEQ(0, 3);
1389 }
1390 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1391}
1392
1393/*
1394 * The core can pass us a set of gamma values to program. We actually only
1395 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1396 * but it's a requirement that we provide the function
1397 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001398static int mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02001399 u16 *blue, uint32_t size,
1400 struct drm_modeset_acquire_ctx *ctx)
Dave Airlie414c4532012-04-17 15:01:25 +01001401{
Dave Airlie414c4532012-04-17 15:01:25 +01001402 mga_crtc_load_lut(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001403
1404 return 0;
Dave Airlie414c4532012-04-17 15:01:25 +01001405}
1406
1407/* Simple cleanup function */
1408static void mga_crtc_destroy(struct drm_crtc *crtc)
1409{
1410 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1411
1412 drm_crtc_cleanup(crtc);
1413 kfree(mga_crtc);
1414}
1415
Egbert Eich64c29072013-07-17 15:07:22 +02001416static void mga_crtc_disable(struct drm_crtc *crtc)
1417{
Egbert Eich64c29072013-07-17 15:07:22 +02001418 DRM_DEBUG_KMS("\n");
1419 mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -07001420 if (crtc->primary->fb) {
Thomas Zimmermann81da87f2019-05-21 13:08:29 +02001421 struct mga_device *mdev = crtc->dev->dev_private;
Thomas Zimmermann5d177182019-06-27 10:09:09 +02001422 struct drm_framebuffer *fb = crtc->primary->fb;
1423 struct drm_gem_vram_object *gbo =
1424 drm_gem_vram_of_gem(fb->obj[0]);
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +02001425
Thomas Zimmermann81da87f2019-05-21 13:08:29 +02001426 /* unmap if console */
Thomas Zimmermann5d177182019-06-27 10:09:09 +02001427 if (mdev->mfbdev->helper.fb == fb)
Thomas Zimmermann81da87f2019-05-21 13:08:29 +02001428 drm_gem_vram_kunmap(gbo);
1429 drm_gem_vram_unpin(gbo);
Egbert Eich64c29072013-07-17 15:07:22 +02001430 }
Matt Roperf4510a22014-04-01 15:22:40 -07001431 crtc->primary->fb = NULL;
Egbert Eich64c29072013-07-17 15:07:22 +02001432}
1433
Dave Airlie414c4532012-04-17 15:01:25 +01001434/* These provide the minimum set of functions required to handle a CRTC */
1435static const struct drm_crtc_funcs mga_crtc_funcs = {
Christopher Harveya080db92013-06-05 15:24:26 -04001436 .cursor_set = mga_crtc_cursor_set,
1437 .cursor_move = mga_crtc_cursor_move,
Dave Airlie414c4532012-04-17 15:01:25 +01001438 .gamma_set = mga_crtc_gamma_set,
1439 .set_config = drm_crtc_helper_set_config,
1440 .destroy = mga_crtc_destroy,
1441};
1442
1443static const struct drm_crtc_helper_funcs mga_helper_funcs = {
Egbert Eich64c29072013-07-17 15:07:22 +02001444 .disable = mga_crtc_disable,
Dave Airlie414c4532012-04-17 15:01:25 +01001445 .dpms = mga_crtc_dpms,
Dave Airlie414c4532012-04-17 15:01:25 +01001446 .mode_set = mga_crtc_mode_set,
1447 .mode_set_base = mga_crtc_mode_set_base,
1448 .prepare = mga_crtc_prepare,
1449 .commit = mga_crtc_commit,
Dave Airlie414c4532012-04-17 15:01:25 +01001450};
1451
1452/* CRTC setup */
Christopher Harveyf1998fe2013-02-20 09:34:22 -05001453static void mga_crtc_init(struct mga_device *mdev)
Dave Airlie414c4532012-04-17 15:01:25 +01001454{
Dave Airlie414c4532012-04-17 15:01:25 +01001455 struct mga_crtc *mga_crtc;
Dave Airlie414c4532012-04-17 15:01:25 +01001456
1457 mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1458 (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1459 GFP_KERNEL);
1460
1461 if (mga_crtc == NULL)
1462 return;
1463
Christopher Harveyf1998fe2013-02-20 09:34:22 -05001464 drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
Dave Airlie414c4532012-04-17 15:01:25 +01001465
1466 drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
1467 mdev->mode_info.crtc = mga_crtc;
1468
Dave Airlie414c4532012-04-17 15:01:25 +01001469 drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1470}
1471
Dave Airlie414c4532012-04-17 15:01:25 +01001472/*
1473 * The encoder comes after the CRTC in the output pipeline, but before
1474 * the connector. It's responsible for ensuring that the digital
1475 * stream is appropriately converted into the output format. Setup is
1476 * very simple in this case - all we have to do is inform qemu of the
1477 * colour depth in order to ensure that it displays appropriately
1478 */
1479
1480/*
1481 * These functions are analagous to those in the CRTC code, but are intended
1482 * to handle any encoder-specific limitations
1483 */
Dave Airlie414c4532012-04-17 15:01:25 +01001484static void mga_encoder_mode_set(struct drm_encoder *encoder,
1485 struct drm_display_mode *mode,
1486 struct drm_display_mode *adjusted_mode)
1487{
1488
1489}
1490
1491static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
1492{
1493 return;
1494}
1495
1496static void mga_encoder_prepare(struct drm_encoder *encoder)
1497{
1498}
1499
1500static void mga_encoder_commit(struct drm_encoder *encoder)
1501{
1502}
1503
Rashika080fd6b2014-01-06 20:37:15 +05301504static void mga_encoder_destroy(struct drm_encoder *encoder)
Dave Airlie414c4532012-04-17 15:01:25 +01001505{
1506 struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
1507 drm_encoder_cleanup(encoder);
1508 kfree(mga_encoder);
1509}
1510
1511static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
1512 .dpms = mga_encoder_dpms,
Dave Airlie414c4532012-04-17 15:01:25 +01001513 .mode_set = mga_encoder_mode_set,
1514 .prepare = mga_encoder_prepare,
1515 .commit = mga_encoder_commit,
1516};
1517
1518static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
1519 .destroy = mga_encoder_destroy,
1520};
1521
1522static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
1523{
1524 struct drm_encoder *encoder;
1525 struct mga_encoder *mga_encoder;
1526
1527 mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
1528 if (!mga_encoder)
1529 return NULL;
1530
1531 encoder = &mga_encoder->base;
1532 encoder->possible_crtcs = 0x1;
1533
1534 drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001535 DRM_MODE_ENCODER_DAC, NULL);
Dave Airlie414c4532012-04-17 15:01:25 +01001536 drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
1537
1538 return encoder;
1539}
1540
1541
1542static int mga_vga_get_modes(struct drm_connector *connector)
1543{
1544 struct mga_connector *mga_connector = to_mga_connector(connector);
1545 struct edid *edid;
1546 int ret = 0;
1547
1548 edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1549 if (edid) {
Daniel Vetterc555f022018-07-09 10:40:06 +02001550 drm_connector_update_edid_property(connector, edid);
Dave Airlie414c4532012-04-17 15:01:25 +01001551 ret = drm_add_edid_modes(connector, edid);
Dave Airlie414c4532012-04-17 15:01:25 +01001552 kfree(edid);
1553 }
1554 return ret;
1555}
1556
Julia Lemireabbee622013-06-27 13:38:59 -04001557static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
1558 int bits_per_pixel)
1559{
1560 uint32_t total_area, divisor;
Nicolas Pitrec24ca5b2015-11-03 23:09:58 -05001561 uint64_t active_area, pixels_per_second, bandwidth;
Julia Lemireabbee622013-06-27 13:38:59 -04001562 uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
1563
1564 divisor = 1024;
1565
1566 if (!mode->htotal || !mode->vtotal || !mode->clock)
1567 return 0;
1568
1569 active_area = mode->hdisplay * mode->vdisplay;
1570 total_area = mode->htotal * mode->vtotal;
1571
1572 pixels_per_second = active_area * mode->clock * 1000;
1573 do_div(pixels_per_second, total_area);
1574
1575 bandwidth = pixels_per_second * bytes_per_pixel * 100;
1576 do_div(bandwidth, divisor);
1577
1578 return (uint32_t)(bandwidth);
1579}
1580
1581#define MODE_BANDWIDTH MODE_BAD
1582
Luc Van Oostenryckc69e52d2018-04-24 15:15:06 +02001583static enum drm_mode_status mga_vga_mode_valid(struct drm_connector *connector,
Dave Airlie414c4532012-04-17 15:01:25 +01001584 struct drm_display_mode *mode)
1585{
Christopher Harvey0ba53172013-02-26 10:55:44 -05001586 struct drm_device *dev = connector->dev;
1587 struct mga_device *mdev = (struct mga_device*)dev->dev_private;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001588 int bpp = 32;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001589
Julia Lemireabbee622013-06-27 13:38:59 -04001590 if (IS_G200_SE(mdev)) {
1591 if (mdev->unique_rev_id == 0x01) {
1592 if (mode->hdisplay > 1600)
1593 return MODE_VIRTUAL_X;
1594 if (mode->vdisplay > 1200)
1595 return MODE_VIRTUAL_Y;
1596 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1597 > (24400 * 1024))
1598 return MODE_BANDWIDTH;
Mathieu Larouchee829d7e2015-08-21 09:24:13 -04001599 } else if (mdev->unique_rev_id == 0x02) {
Julia Lemireabbee622013-06-27 13:38:59 -04001600 if (mode->hdisplay > 1920)
1601 return MODE_VIRTUAL_X;
1602 if (mode->vdisplay > 1200)
1603 return MODE_VIRTUAL_Y;
1604 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1605 > (30100 * 1024))
1606 return MODE_BANDWIDTH;
Mathieu Larouche0cbb7382017-06-14 10:39:42 -04001607 } else {
1608 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1609 > (55000 * 1024))
1610 return MODE_BANDWIDTH;
Julia Lemireabbee622013-06-27 13:38:59 -04001611 }
1612 } else if (mdev->type == G200_WB) {
1613 if (mode->hdisplay > 1280)
1614 return MODE_VIRTUAL_X;
1615 if (mode->vdisplay > 1024)
1616 return MODE_VIRTUAL_Y;
Dan Carpenter9eb8d7a2018-01-25 17:26:55 +03001617 if (mga_vga_calculate_mode_bandwidth(mode, bpp) >
1618 (31877 * 1024))
Julia Lemireabbee622013-06-27 13:38:59 -04001619 return MODE_BANDWIDTH;
1620 } else if (mdev->type == G200_EV &&
1621 (mga_vga_calculate_mode_bandwidth(mode, bpp)
1622 > (32700 * 1024))) {
1623 return MODE_BANDWIDTH;
Dave Airlieec22b4a2014-02-05 14:13:56 +10001624 } else if (mdev->type == G200_EH &&
Julia Lemireabbee622013-06-27 13:38:59 -04001625 (mga_vga_calculate_mode_bandwidth(mode, bpp)
1626 > (37500 * 1024))) {
1627 return MODE_BANDWIDTH;
Dave Airlieec22b4a2014-02-05 14:13:56 +10001628 } else if (mdev->type == G200_ER &&
Julia Lemireabbee622013-06-27 13:38:59 -04001629 (mga_vga_calculate_mode_bandwidth(mode,
1630 bpp) > (55000 * 1024))) {
1631 return MODE_BANDWIDTH;
1632 }
Dave Airlie414c4532012-04-17 15:01:25 +01001633
Adam Jackson25161082015-06-15 16:16:15 -04001634 if ((mode->hdisplay % 8) != 0 || (mode->hsync_start % 8) != 0 ||
1635 (mode->hsync_end % 8) != 0 || (mode->htotal % 8) != 0) {
1636 return MODE_H_ILLEGAL;
1637 }
1638
Dave Airlie414c4532012-04-17 15:01:25 +01001639 if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1640 mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1641 mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1642 mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1643 return MODE_BAD;
1644 }
1645
Christopher Harvey0ba53172013-02-26 10:55:44 -05001646 /* Validate the mode input by the user */
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001647 if (connector->cmdline_mode.specified) {
1648 if (connector->cmdline_mode.bpp_specified)
1649 bpp = connector->cmdline_mode.bpp;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001650 }
1651
1652 if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001653 if (connector->cmdline_mode.specified)
1654 connector->cmdline_mode.specified = false;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001655 return MODE_BAD;
1656 }
1657
Dave Airlie414c4532012-04-17 15:01:25 +01001658 return MODE_OK;
1659}
1660
Rashika080fd6b2014-01-06 20:37:15 +05301661static struct drm_encoder *mga_connector_best_encoder(struct drm_connector
Dave Airlie414c4532012-04-17 15:01:25 +01001662 *connector)
1663{
1664 int enc_id = connector->encoder_ids[0];
Dave Airlie414c4532012-04-17 15:01:25 +01001665 /* pick the encoder ids */
Rob Clarkc7e95112014-07-17 23:30:00 -04001666 if (enc_id)
Keith Packard418da172017-03-14 23:25:07 -07001667 return drm_encoder_find(connector->dev, NULL, enc_id);
Dave Airlie414c4532012-04-17 15:01:25 +01001668 return NULL;
1669}
1670
Dave Airlie414c4532012-04-17 15:01:25 +01001671static void mga_connector_destroy(struct drm_connector *connector)
1672{
1673 struct mga_connector *mga_connector = to_mga_connector(connector);
1674 mgag200_i2c_destroy(mga_connector->i2c);
1675 drm_connector_cleanup(connector);
1676 kfree(connector);
1677}
1678
Ville Syrjälä71cb7492015-12-15 12:21:10 +01001679static const struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
Dave Airlie414c4532012-04-17 15:01:25 +01001680 .get_modes = mga_vga_get_modes,
1681 .mode_valid = mga_vga_mode_valid,
1682 .best_encoder = mga_connector_best_encoder,
1683};
1684
Ville Syrjälä71cb7492015-12-15 12:21:10 +01001685static const struct drm_connector_funcs mga_vga_connector_funcs = {
Dave Airlie414c4532012-04-17 15:01:25 +01001686 .dpms = drm_helper_connector_dpms,
Dave Airlie414c4532012-04-17 15:01:25 +01001687 .fill_modes = drm_helper_probe_single_connector_modes,
1688 .destroy = mga_connector_destroy,
1689};
1690
1691static struct drm_connector *mga_vga_init(struct drm_device *dev)
1692{
1693 struct drm_connector *connector;
1694 struct mga_connector *mga_connector;
1695
1696 mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
1697 if (!mga_connector)
1698 return NULL;
1699
1700 connector = &mga_connector->base;
1701
1702 drm_connector_init(dev, connector,
1703 &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
1704
1705 drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1706
Thomas Wood34ea3d32014-05-29 16:57:41 +01001707 drm_connector_register(connector);
Egbert Eich3d5a1c52013-07-17 15:07:25 +02001708
Dave Airlie414c4532012-04-17 15:01:25 +01001709 mga_connector->i2c = mgag200_i2c_create(dev);
1710 if (!mga_connector->i2c)
1711 DRM_ERROR("failed to add ddc bus\n");
1712
1713 return connector;
1714}
1715
1716
1717int mgag200_modeset_init(struct mga_device *mdev)
1718{
1719 struct drm_encoder *encoder;
1720 struct drm_connector *connector;
1721 int ret;
1722
1723 mdev->mode_info.mode_config_initialized = true;
1724
1725 mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1726 mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
1727
1728 mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
1729
Christopher Harveyf1998fe2013-02-20 09:34:22 -05001730 mga_crtc_init(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +01001731
1732 encoder = mga_encoder_init(mdev->dev);
1733 if (!encoder) {
1734 DRM_ERROR("mga_encoder_init failed\n");
1735 return -1;
1736 }
1737
1738 connector = mga_vga_init(mdev->dev);
1739 if (!connector) {
1740 DRM_ERROR("mga_vga_init failed\n");
1741 return -1;
1742 }
1743
Daniel Vettercde4c442018-07-09 10:40:07 +02001744 drm_connector_attach_encoder(connector, encoder);
Dave Airlie414c4532012-04-17 15:01:25 +01001745
1746 ret = mgag200_fbdev_init(mdev);
1747 if (ret) {
1748 DRM_ERROR("mga_fbdev_init failed\n");
1749 return ret;
1750 }
1751
1752 return 0;
1753}
1754
1755void mgag200_modeset_fini(struct mga_device *mdev)
1756{
1757
1758}