blob: 46122fa3198891f0c11147e759ff4bc0e3789e6f [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>
Thomas Zimmermann09daa2e2019-12-03 11:04:00 +010012#include <linux/pci.h>
Dave Airlie414c4532012-04-17 15:01:25 +010013
David Howells760285e2012-10-02 18:01:07 +010014#include <drm/drm_crtc_helper.h>
Sam Ravnborg9f397802019-06-23 12:35:42 +020015#include <drm/drm_fourcc.h>
Thomas Zimmermann5635b7c2020-05-07 11:03:13 +020016#include <drm/drm_gem_framebuffer_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010017#include <drm/drm_plane_helper.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010018#include <drm/drm_probe_helper.h>
Thomas Zimmermann03e44ad2020-02-28 09:18:27 +010019#include <drm/drm_simple_kms_helper.h>
Dave Airlie414c4532012-04-17 15:01:25 +010020
21#include "mgag200_drv.h"
22
23#define MGAG200_LUT_SIZE 256
24
25/*
26 * This file contains setup code for the CRTC.
27 */
28
29static void mga_crtc_load_lut(struct drm_crtc *crtc)
30{
Dave Airlie414c4532012-04-17 15:01:25 +010031 struct drm_device *dev = crtc->dev;
Thomas Zimmermann8d8ff2a2020-05-07 11:03:10 +020032 struct mga_device *mdev = to_mga_device(dev);
Matt Roperf4510a22014-04-01 15:22:40 -070033 struct drm_framebuffer *fb = crtc->primary->fb;
Peter Rosin9ed85e12017-07-13 18:25:34 +020034 u16 *r_ptr, *g_ptr, *b_ptr;
Dave Airlie414c4532012-04-17 15:01:25 +010035 int i;
36
37 if (!crtc->enabled)
38 return;
39
Peter Rosin9ed85e12017-07-13 18:25:34 +020040 r_ptr = crtc->gamma_store;
41 g_ptr = r_ptr + crtc->gamma_size;
42 b_ptr = g_ptr + crtc->gamma_size;
43
Dave Airlie414c4532012-04-17 15:01:25 +010044 WREG8(DAC_INDEX + MGA1064_INDEX, 0);
45
Ville Syrjälä272725c2016-12-14 23:32:20 +020046 if (fb && fb->format->cpp[0] * 8 == 16) {
Ville Syrjäläb00c6002016-12-14 23:31:35 +020047 int inc = (fb->format->depth == 15) ? 8 : 4;
Egbert Eichde7500e2013-07-17 15:07:27 +020048 u8 r, b;
49 for (i = 0; i < MGAG200_LUT_SIZE; i += inc) {
Ville Syrjäläb00c6002016-12-14 23:31:35 +020050 if (fb->format->depth == 16) {
Egbert Eichde7500e2013-07-17 15:07:27 +020051 if (i > (MGAG200_LUT_SIZE >> 1)) {
52 r = b = 0;
53 } else {
Peter Rosin9ed85e12017-07-13 18:25:34 +020054 r = *r_ptr++ >> 8;
55 b = *b_ptr++ >> 8;
56 r_ptr++;
57 b_ptr++;
Egbert Eichde7500e2013-07-17 15:07:27 +020058 }
59 } else {
Peter Rosin9ed85e12017-07-13 18:25:34 +020060 r = *r_ptr++ >> 8;
61 b = *b_ptr++ >> 8;
Egbert Eichde7500e2013-07-17 15:07:27 +020062 }
63 /* VGA registers */
64 WREG8(DAC_INDEX + MGA1064_COL_PAL, r);
Peter Rosin9ed85e12017-07-13 18:25:34 +020065 WREG8(DAC_INDEX + MGA1064_COL_PAL, *g_ptr++ >> 8);
Egbert Eichde7500e2013-07-17 15:07:27 +020066 WREG8(DAC_INDEX + MGA1064_COL_PAL, b);
67 }
68 return;
69 }
Dave Airlie414c4532012-04-17 15:01:25 +010070 for (i = 0; i < MGAG200_LUT_SIZE; i++) {
71 /* VGA registers */
Peter Rosin9ed85e12017-07-13 18:25:34 +020072 WREG8(DAC_INDEX + MGA1064_COL_PAL, *r_ptr++ >> 8);
73 WREG8(DAC_INDEX + MGA1064_COL_PAL, *g_ptr++ >> 8);
74 WREG8(DAC_INDEX + MGA1064_COL_PAL, *b_ptr++ >> 8);
Dave Airlie414c4532012-04-17 15:01:25 +010075 }
76}
77
78static inline void mga_wait_vsync(struct mga_device *mdev)
79{
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000080 unsigned long timeout = jiffies + HZ/10;
Dave Airlie414c4532012-04-17 15:01:25 +010081 unsigned int status = 0;
82
83 do {
84 status = RREG32(MGAREG_Status);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000085 } while ((status & 0x08) && time_before(jiffies, timeout));
86 timeout = jiffies + HZ/10;
Dave Airlie414c4532012-04-17 15:01:25 +010087 status = 0;
88 do {
89 status = RREG32(MGAREG_Status);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000090 } while (!(status & 0x08) && time_before(jiffies, timeout));
Dave Airlie414c4532012-04-17 15:01:25 +010091}
92
93static inline void mga_wait_busy(struct mga_device *mdev)
94{
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000095 unsigned long timeout = jiffies + HZ;
Dave Airlie414c4532012-04-17 15:01:25 +010096 unsigned int status = 0;
97 do {
98 status = RREG8(MGAREG_Status + 2);
Christopher Harvey3cdc0e82013-05-06 15:56:17 +000099 } while ((status & 0x01) && time_before(jiffies, timeout));
Dave Airlie414c4532012-04-17 15:01:25 +0100100}
101
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400102#define P_ARRAY_SIZE 9
103
Dave Airlie414c4532012-04-17 15:01:25 +0100104static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
105{
106 unsigned int vcomax, vcomin, pllreffreq;
107 unsigned int delta, tmpdelta, permitteddelta;
108 unsigned int testp, testm, testn;
109 unsigned int p, m, n;
110 unsigned int computed;
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400111 unsigned int pvalues_e4[P_ARRAY_SIZE] = {16, 14, 12, 10, 8, 6, 4, 2, 1};
112 unsigned int fvv;
113 unsigned int i;
Dave Airlie414c4532012-04-17 15:01:25 +0100114
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400115 if (mdev->unique_rev_id <= 0x03) {
Dave Airlie414c4532012-04-17 15:01:25 +0100116
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400117 m = n = p = 0;
118 vcomax = 320000;
119 vcomin = 160000;
120 pllreffreq = 25000;
Dave Airlie414c4532012-04-17 15:01:25 +0100121
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400122 delta = 0xffffffff;
123 permitteddelta = clock * 5 / 1000;
Dave Airlie414c4532012-04-17 15:01:25 +0100124
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400125 for (testp = 8; testp > 0; testp /= 2) {
126 if (clock * testp > vcomax)
127 continue;
128 if (clock * testp < vcomin)
129 continue;
130
131 for (testn = 17; testn < 256; testn++) {
132 for (testm = 1; testm < 32; testm++) {
133 computed = (pllreffreq * testn) /
134 (testm * testp);
135 if (computed > clock)
136 tmpdelta = computed - clock;
137 else
138 tmpdelta = clock - computed;
139 if (tmpdelta < delta) {
140 delta = tmpdelta;
141 m = testm - 1;
142 n = testn - 1;
143 p = testp - 1;
144 }
Dave Airlie414c4532012-04-17 15:01:25 +0100145 }
146 }
147 }
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400148 } else {
149
150
151 m = n = p = 0;
152 vcomax = 1600000;
153 vcomin = 800000;
154 pllreffreq = 25000;
155
156 if (clock < 25000)
157 clock = 25000;
158
159 clock = clock * 2;
160
161 delta = 0xFFFFFFFF;
162 /* Permited delta is 0.5% as VESA Specification */
163 permitteddelta = clock * 5 / 1000;
164
165 for (i = 0 ; i < P_ARRAY_SIZE ; i++) {
166 testp = pvalues_e4[i];
167
168 if ((clock * testp) > vcomax)
169 continue;
170 if ((clock * testp) < vcomin)
171 continue;
172
173 for (testn = 50; testn <= 256; testn++) {
174 for (testm = 1; testm <= 32; testm++) {
175 computed = (pllreffreq * testn) /
176 (testm * testp);
177 if (computed > clock)
178 tmpdelta = computed - clock;
179 else
180 tmpdelta = clock - computed;
181
182 if (tmpdelta < delta) {
183 delta = tmpdelta;
184 m = testm - 1;
185 n = testn - 1;
186 p = testp - 1;
187 }
188 }
189 }
190 }
191
Mathieu Larouched3922b62016-05-27 15:12:50 -0400192 fvv = pllreffreq * (n + 1) / (m + 1);
Mathieu Larouchee829d7e2015-08-21 09:24:13 -0400193 fvv = (fvv - 800000) / 50000;
194
195 if (fvv > 15)
196 fvv = 15;
197
198 p |= (fvv << 4);
199 m |= 0x80;
200
201 clock = clock / 2;
Dave Airlie414c4532012-04-17 15:01:25 +0100202 }
203
204 if (delta > permitteddelta) {
Joe Perches8dfe1622017-02-28 04:55:54 -0800205 pr_warn("PLL delta too large\n");
Dave Airlie414c4532012-04-17 15:01:25 +0100206 return 1;
207 }
208
209 WREG_DAC(MGA1064_PIX_PLLC_M, m);
210 WREG_DAC(MGA1064_PIX_PLLC_N, n);
211 WREG_DAC(MGA1064_PIX_PLLC_P, p);
Mathieu Larouched3922b62016-05-27 15:12:50 -0400212
213 if (mdev->unique_rev_id >= 0x04) {
214 WREG_DAC(0x1a, 0x09);
215 msleep(20);
216 WREG_DAC(0x1a, 0x01);
217
218 }
219
Dave Airlie414c4532012-04-17 15:01:25 +0100220 return 0;
221}
222
223static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
224{
225 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530226 unsigned int delta, tmpdelta;
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400227 unsigned int testp, testm, testn, testp2;
Dave Airlie414c4532012-04-17 15:01:25 +0100228 unsigned int p, m, n;
229 unsigned int computed;
230 int i, j, tmpcount, vcount;
231 bool pll_locked = false;
232 u8 tmp;
233
234 m = n = p = 0;
Dave Airlie414c4532012-04-17 15:01:25 +0100235
236 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100237
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400238 if (mdev->type == G200_EW3) {
Dave Airlie414c4532012-04-17 15:01:25 +0100239
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400240 vcomax = 800000;
241 vcomin = 400000;
242 pllreffreq = 25000;
243
244 for (testp = 1; testp < 8; testp++) {
245 for (testp2 = 1; testp2 < 8; testp2++) {
246 if (testp < testp2)
247 continue;
248 if ((clock * testp * testp2) > vcomax)
249 continue;
250 if ((clock * testp * testp2) < vcomin)
251 continue;
252 for (testm = 1; testm < 26; testm++) {
253 for (testn = 32; testn < 2048 ; testn++) {
254 computed = (pllreffreq * testn) /
255 (testm * testp * testp2);
256 if (computed > clock)
257 tmpdelta = computed - clock;
258 else
259 tmpdelta = clock - computed;
260 if (tmpdelta < delta) {
261 delta = tmpdelta;
262 m = ((testn & 0x100) >> 1) |
263 (testm);
264 n = (testn & 0xFF);
265 p = ((testn & 0x600) >> 3) |
266 (testp2 << 3) |
267 (testp);
268 }
269 }
270 }
271 }
272 }
273 } else {
274
275 vcomax = 550000;
276 vcomin = 150000;
277 pllreffreq = 48000;
278
279 for (testp = 1; testp < 9; testp++) {
280 if (clock * testp > vcomax)
281 continue;
282 if (clock * testp < vcomin)
283 continue;
284
285 for (testm = 1; testm < 17; testm++) {
286 for (testn = 1; testn < 151; testn++) {
287 computed = (pllreffreq * testn) /
288 (testm * testp);
289 if (computed > clock)
290 tmpdelta = computed - clock;
291 else
292 tmpdelta = clock - computed;
293 if (tmpdelta < delta) {
294 delta = tmpdelta;
295 n = testn - 1;
296 m = (testm - 1) |
297 ((n >> 1) & 0x80);
298 p = testp - 1;
299 }
Dave Airlie414c4532012-04-17 15:01:25 +0100300 }
301 }
302 }
303 }
304
305 for (i = 0; i <= 32 && pll_locked == false; i++) {
306 if (i > 0) {
307 WREG8(MGAREG_CRTC_INDEX, 0x1e);
308 tmp = RREG8(MGAREG_CRTC_DATA);
309 if (tmp < 0xff)
310 WREG8(MGAREG_CRTC_DATA, tmp+1);
311 }
312
313 /* set pixclkdis to 1 */
314 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
315 tmp = RREG8(DAC_DATA);
316 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000317 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100318
319 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
320 tmp = RREG8(DAC_DATA);
321 tmp |= MGA1064_REMHEADCTL_CLKDIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000322 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100323
324 /* select PLL Set C */
325 tmp = RREG8(MGAREG_MEM_MISC_READ);
326 tmp |= 0x3 << 2;
327 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
328
329 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
330 tmp = RREG8(DAC_DATA);
331 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000332 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100333
334 udelay(500);
335
336 /* reset the PLL */
337 WREG8(DAC_INDEX, MGA1064_VREF_CTL);
338 tmp = RREG8(DAC_DATA);
339 tmp &= ~0x04;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000340 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100341
342 udelay(50);
343
344 /* program pixel pll register */
345 WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
346 WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
347 WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
348
349 udelay(50);
350
351 /* turn pll on */
352 WREG8(DAC_INDEX, MGA1064_VREF_CTL);
353 tmp = RREG8(DAC_DATA);
354 tmp |= 0x04;
355 WREG_DAC(MGA1064_VREF_CTL, tmp);
356
357 udelay(500);
358
359 /* select the pixel pll */
360 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
361 tmp = RREG8(DAC_DATA);
362 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
363 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000364 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100365
366 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
367 tmp = RREG8(DAC_DATA);
368 tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
369 tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000370 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100371
372 /* reset dotclock rate bit */
373 WREG8(MGAREG_SEQ_INDEX, 1);
374 tmp = RREG8(MGAREG_SEQ_DATA);
375 tmp &= ~0x8;
376 WREG8(MGAREG_SEQ_DATA, tmp);
377
378 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
379 tmp = RREG8(DAC_DATA);
380 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000381 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100382
383 vcount = RREG8(MGAREG_VCOUNT);
384
385 for (j = 0; j < 30 && pll_locked == false; j++) {
386 tmpcount = RREG8(MGAREG_VCOUNT);
387 if (tmpcount < vcount)
388 vcount = 0;
389 if ((tmpcount - vcount) > 2)
390 pll_locked = true;
391 else
392 udelay(5);
393 }
394 }
395 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
396 tmp = RREG8(DAC_DATA);
397 tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
398 WREG_DAC(MGA1064_REMHEADCTL, tmp);
399 return 0;
400}
401
402static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
403{
404 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530405 unsigned int delta, tmpdelta;
Dave Airlie414c4532012-04-17 15:01:25 +0100406 unsigned int testp, testm, testn;
407 unsigned int p, m, n;
408 unsigned int computed;
409 u8 tmp;
410
411 m = n = p = 0;
412 vcomax = 550000;
413 vcomin = 150000;
414 pllreffreq = 50000;
415
416 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100417
418 for (testp = 16; testp > 0; testp--) {
419 if (clock * testp > vcomax)
420 continue;
421 if (clock * testp < vcomin)
422 continue;
423
424 for (testn = 1; testn < 257; testn++) {
425 for (testm = 1; testm < 17; testm++) {
426 computed = (pllreffreq * testn) /
427 (testm * testp);
428 if (computed > clock)
429 tmpdelta = computed - clock;
430 else
431 tmpdelta = clock - computed;
432 if (tmpdelta < delta) {
433 delta = tmpdelta;
434 n = testn - 1;
435 m = testm - 1;
436 p = testp - 1;
437 }
438 }
439 }
440 }
441
442 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
443 tmp = RREG8(DAC_DATA);
444 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000445 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100446
447 tmp = RREG8(MGAREG_MEM_MISC_READ);
448 tmp |= 0x3 << 2;
449 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
450
451 WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
452 tmp = RREG8(DAC_DATA);
Christopher Harveyfb70a662013-04-12 22:24:05 +0000453 WREG8(DAC_DATA, tmp & ~0x40);
Dave Airlie414c4532012-04-17 15:01:25 +0100454
455 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
456 tmp = RREG8(DAC_DATA);
457 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000458 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100459
460 WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
461 WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
462 WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
463
464 udelay(50);
465
466 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
467 tmp = RREG8(DAC_DATA);
468 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000469 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100470
471 udelay(500);
472
473 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
474 tmp = RREG8(DAC_DATA);
475 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
476 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000477 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100478
479 WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
480 tmp = RREG8(DAC_DATA);
Christopher Harveyfb70a662013-04-12 22:24:05 +0000481 WREG8(DAC_DATA, tmp | 0x40);
Dave Airlie414c4532012-04-17 15:01:25 +0100482
483 tmp = RREG8(MGAREG_MEM_MISC_READ);
484 tmp |= (0x3 << 2);
485 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
486
487 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
488 tmp = RREG8(DAC_DATA);
489 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000490 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100491
492 return 0;
493}
494
495static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
496{
497 unsigned int vcomax, vcomin, pllreffreq;
Sudip Mukherjee546aee52015-07-01 17:12:45 +0530498 unsigned int delta, tmpdelta;
Dave Airlie414c4532012-04-17 15:01:25 +0100499 unsigned int testp, testm, testn;
500 unsigned int p, m, n;
501 unsigned int computed;
502 int i, j, tmpcount, vcount;
503 u8 tmp;
504 bool pll_locked = false;
505
506 m = n = p = 0;
Dave Airlie414c4532012-04-17 15:01:25 +0100507
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400508 if (mdev->type == G200_EH3) {
509 vcomax = 3000000;
510 vcomin = 1500000;
511 pllreffreq = 25000;
Dave Airlie414c4532012-04-17 15:01:25 +0100512
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400513 delta = 0xffffffff;
Dave Airlie414c4532012-04-17 15:01:25 +0100514
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400515 testp = 0;
516
517 for (testm = 150; testm >= 6; testm--) {
518 if (clock * testm > vcomax)
519 continue;
520 if (clock * testm < vcomin)
521 continue;
522 for (testn = 120; testn >= 60; testn--) {
523 computed = (pllreffreq * testn) / testm;
Dave Airlie414c4532012-04-17 15:01:25 +0100524 if (computed > clock)
525 tmpdelta = computed - clock;
526 else
527 tmpdelta = clock - computed;
528 if (tmpdelta < delta) {
529 delta = tmpdelta;
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400530 n = testn;
531 m = testm;
532 p = testp;
Dave Airlie414c4532012-04-17 15:01:25 +0100533 }
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400534 if (delta == 0)
535 break;
536 }
537 if (delta == 0)
538 break;
539 }
540 } else {
541
542 vcomax = 800000;
543 vcomin = 400000;
544 pllreffreq = 33333;
545
546 delta = 0xffffffff;
547
548 for (testp = 16; testp > 0; testp >>= 1) {
549 if (clock * testp > vcomax)
550 continue;
551 if (clock * testp < vcomin)
552 continue;
553
554 for (testm = 1; testm < 33; testm++) {
555 for (testn = 17; testn < 257; testn++) {
556 computed = (pllreffreq * testn) /
557 (testm * testp);
558 if (computed > clock)
559 tmpdelta = computed - clock;
560 else
561 tmpdelta = clock - computed;
562 if (tmpdelta < delta) {
563 delta = tmpdelta;
564 n = testn - 1;
565 m = (testm - 1);
566 p = testp - 1;
567 }
568 if ((clock * testp) >= 600000)
569 p |= 0x80;
570 }
Dave Airlie414c4532012-04-17 15:01:25 +0100571 }
572 }
573 }
574 for (i = 0; i <= 32 && pll_locked == false; i++) {
575 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
576 tmp = RREG8(DAC_DATA);
577 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000578 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100579
580 tmp = RREG8(MGAREG_MEM_MISC_READ);
581 tmp |= 0x3 << 2;
582 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
583
584 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
585 tmp = RREG8(DAC_DATA);
586 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000587 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100588
589 udelay(500);
590
591 WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
592 WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
593 WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
594
595 udelay(500);
596
597 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
598 tmp = RREG8(DAC_DATA);
599 tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
600 tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000601 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100602
603 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
604 tmp = RREG8(DAC_DATA);
605 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
606 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000607 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100608
609 vcount = RREG8(MGAREG_VCOUNT);
610
611 for (j = 0; j < 30 && pll_locked == false; j++) {
612 tmpcount = RREG8(MGAREG_VCOUNT);
613 if (tmpcount < vcount)
614 vcount = 0;
615 if ((tmpcount - vcount) > 2)
616 pll_locked = true;
617 else
618 udelay(5);
619 }
620 }
621
622 return 0;
623}
624
625static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
626{
627 unsigned int vcomax, vcomin, pllreffreq;
628 unsigned int delta, tmpdelta;
Dave Airlie98306052012-08-09 15:00:15 +1000629 int testr, testn, testm, testo;
Dave Airlie414c4532012-04-17 15:01:25 +0100630 unsigned int p, m, n;
Dave Airlie98306052012-08-09 15:00:15 +1000631 unsigned int computed, vco;
Dave Airlie414c4532012-04-17 15:01:25 +0100632 int tmp;
Dave Airlie98306052012-08-09 15:00:15 +1000633 const unsigned int m_div_val[] = { 1, 2, 4, 8 };
Dave Airlie414c4532012-04-17 15:01:25 +0100634
635 m = n = p = 0;
636 vcomax = 1488000;
637 vcomin = 1056000;
638 pllreffreq = 48000;
639
640 delta = 0xffffffff;
641
642 for (testr = 0; testr < 4; testr++) {
643 if (delta == 0)
644 break;
645 for (testn = 5; testn < 129; testn++) {
646 if (delta == 0)
647 break;
648 for (testm = 3; testm >= 0; testm--) {
649 if (delta == 0)
650 break;
651 for (testo = 5; testo < 33; testo++) {
Dave Airlie98306052012-08-09 15:00:15 +1000652 vco = pllreffreq * (testn + 1) /
Dave Airlie414c4532012-04-17 15:01:25 +0100653 (testr + 1);
Dave Airlie98306052012-08-09 15:00:15 +1000654 if (vco < vcomin)
Dave Airlie414c4532012-04-17 15:01:25 +0100655 continue;
Dave Airlie98306052012-08-09 15:00:15 +1000656 if (vco > vcomax)
Dave Airlie414c4532012-04-17 15:01:25 +0100657 continue;
Dave Airlie98306052012-08-09 15:00:15 +1000658 computed = vco / (m_div_val[testm] * (testo + 1));
Dave Airlie414c4532012-04-17 15:01:25 +0100659 if (computed > clock)
660 tmpdelta = computed - clock;
661 else
662 tmpdelta = clock - computed;
663 if (tmpdelta < delta) {
664 delta = tmpdelta;
665 m = testm | (testo << 3);
666 n = testn;
667 p = testr | (testr << 3);
668 }
669 }
670 }
671 }
672 }
673
674 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
675 tmp = RREG8(DAC_DATA);
676 tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000677 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100678
679 WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
680 tmp = RREG8(DAC_DATA);
681 tmp |= MGA1064_REMHEADCTL_CLKDIS;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000682 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100683
684 tmp = RREG8(MGAREG_MEM_MISC_READ);
685 tmp |= (0x3<<2) | 0xc0;
686 WREG8(MGAREG_MEM_MISC_WRITE, tmp);
687
688 WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
689 tmp = RREG8(DAC_DATA);
690 tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
691 tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
Christopher Harveyfb70a662013-04-12 22:24:05 +0000692 WREG8(DAC_DATA, tmp);
Dave Airlie414c4532012-04-17 15:01:25 +0100693
694 udelay(500);
695
696 WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
697 WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
698 WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
699
700 udelay(50);
701
702 return 0;
703}
704
705static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
706{
Thomas Zimmermanndb05f8d2020-05-15 10:32:23 +0200707 u8 misc;
708
Dave Airlie414c4532012-04-17 15:01:25 +0100709 switch(mdev->type) {
710 case G200_SE_A:
711 case G200_SE_B:
712 return mga_g200se_set_plls(mdev, clock);
713 break;
714 case G200_WB:
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400715 case G200_EW3:
Dave Airlie414c4532012-04-17 15:01:25 +0100716 return mga_g200wb_set_plls(mdev, clock);
717 break;
718 case G200_EV:
719 return mga_g200ev_set_plls(mdev, clock);
720 break;
721 case G200_EH:
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400722 case G200_EH3:
Dave Airlie414c4532012-04-17 15:01:25 +0100723 return mga_g200eh_set_plls(mdev, clock);
724 break;
725 case G200_ER:
726 return mga_g200er_set_plls(mdev, clock);
727 break;
728 }
Thomas Zimmermanndb05f8d2020-05-15 10:32:23 +0200729
730 misc = RREG8(MGA_MISC_IN);
731 misc &= ~MGAREG_MISC_CLK_SEL_MASK;
732 misc |= MGAREG_MISC_CLK_SEL_MGA_MSK;
733 WREG8(MGA_MISC_OUT, misc);
734
Dave Airlie414c4532012-04-17 15:01:25 +0100735 return 0;
736}
737
738static void mga_g200wb_prepare(struct drm_crtc *crtc)
739{
Thomas Zimmermann8d8ff2a2020-05-07 11:03:10 +0200740 struct mga_device *mdev = to_mga_device(crtc->dev);
Dave Airlie414c4532012-04-17 15:01:25 +0100741 u8 tmp;
742 int iter_max;
743
744 /* 1- The first step is to warn the BMC of an upcoming mode change.
745 * We are putting the misc<0> to output.*/
746
747 WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
748 tmp = RREG8(DAC_DATA);
749 tmp |= 0x10;
750 WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
751
752 /* we are putting a 1 on the misc<0> line */
753 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
754 tmp = RREG8(DAC_DATA);
755 tmp |= 0x10;
756 WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
757
758 /* 2- Second step to mask and further scan request
759 * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
760 */
761 WREG8(DAC_INDEX, MGA1064_SPAREREG);
762 tmp = RREG8(DAC_DATA);
763 tmp |= 0x80;
764 WREG_DAC(MGA1064_SPAREREG, tmp);
765
766 /* 3a- the third step is to verifu if there is an active scan
767 * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
768 */
769 iter_max = 300;
770 while (!(tmp & 0x1) && iter_max) {
771 WREG8(DAC_INDEX, MGA1064_SPAREREG);
772 tmp = RREG8(DAC_DATA);
773 udelay(1000);
774 iter_max--;
775 }
776
777 /* 3b- this step occurs only if the remove is actually scanning
778 * we are waiting for the end of the frame which is a 1 on
779 * remvsyncsts (XSPAREREG<1>)
780 */
781 if (iter_max) {
782 iter_max = 300;
783 while ((tmp & 0x2) && iter_max) {
784 WREG8(DAC_INDEX, MGA1064_SPAREREG);
785 tmp = RREG8(DAC_DATA);
786 udelay(1000);
787 iter_max--;
788 }
789 }
790}
791
792static void mga_g200wb_commit(struct drm_crtc *crtc)
793{
794 u8 tmp;
Thomas Zimmermann8d8ff2a2020-05-07 11:03:10 +0200795 struct mga_device *mdev = to_mga_device(crtc->dev);
Dave Airlie414c4532012-04-17 15:01:25 +0100796
797 /* 1- The first step is to ensure that the vrsten and hrsten are set */
798 WREG8(MGAREG_CRTCEXT_INDEX, 1);
799 tmp = RREG8(MGAREG_CRTCEXT_DATA);
800 WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
801
802 /* 2- second step is to assert the rstlvl2 */
803 WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
804 tmp = RREG8(DAC_DATA);
805 tmp |= 0x8;
806 WREG8(DAC_DATA, tmp);
807
808 /* wait 10 us */
809 udelay(10);
810
811 /* 3- deassert rstlvl2 */
812 tmp &= ~0x08;
813 WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
814 WREG8(DAC_DATA, tmp);
815
816 /* 4- remove mask of scan request */
817 WREG8(DAC_INDEX, MGA1064_SPAREREG);
818 tmp = RREG8(DAC_DATA);
819 tmp &= ~0x80;
820 WREG8(DAC_DATA, tmp);
821
822 /* 5- put back a 0 on the misc<0> line */
823 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
824 tmp = RREG8(DAC_DATA);
825 tmp &= ~0x10;
826 WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
827}
828
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000829/*
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200830 * This is how the framebuffer base address is stored in g200 cards:
831 * * Assume @offset is the gpu_addr variable of the framebuffer object
832 * * Then addr is the number of _pixels_ (not bytes) from the start of
833 * VRAM to the first pixel we want to display. (divided by 2 for 32bit
834 * framebuffers)
835 * * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers
836 * addr<20> -> CRTCEXT0<6>
837 * addr<19-16> -> CRTCEXT0<3-0>
838 * addr<15-8> -> CRTCC<7-0>
839 * addr<7-0> -> CRTCD<7-0>
840 *
841 * CRTCEXT0 has to be programmed last to trigger an update and make the
842 * new addr variable take effect.
Christopher Harvey9f1d0362013-05-08 19:10:38 +0000843 */
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200844static void mgag200_set_startadd(struct mga_device *mdev,
845 unsigned long offset)
Dave Airlie414c4532012-04-17 15:01:25 +0100846{
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200847 struct drm_device *dev = mdev->dev;
848 u32 startadd;
849 u8 crtcc, crtcd, crtcext0;
Dave Airlie414c4532012-04-17 15:01:25 +0100850
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200851 startadd = offset / 8;
Dave Airlie414c4532012-04-17 15:01:25 +0100852
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200853 /*
854 * Can't store addresses any higher than that, but we also
855 * don't have more than 16 MiB of memory, so it should be fine.
856 */
857 drm_WARN_ON(dev, startadd > 0x1fffff);
Dave Airlie414c4532012-04-17 15:01:25 +0100858
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200859 RREG_ECRT(0x00, crtcext0);
860
861 crtcc = (startadd >> 8) & 0xff;
862 crtcd = startadd & 0xff;
863 crtcext0 &= 0xb0;
864 crtcext0 |= ((startadd >> 14) & BIT(6)) |
865 ((startadd >> 16) & 0x0f);
866
867 WREG_CRT(0x0c, crtcc);
868 WREG_CRT(0x0d, crtcd);
869 WREG_ECRT(0x00, crtcext0);
Dave Airlie414c4532012-04-17 15:01:25 +0100870}
871
Thomas Zimmermannfb724f12020-05-15 10:32:21 +0200872static int mga_crtc_do_set_base(struct mga_device *mdev,
873 const struct drm_framebuffer *fb,
874 const struct drm_framebuffer *old_fb)
Dave Airlie414c4532012-04-17 15:01:25 +0100875{
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200876 struct drm_gem_vram_object *gbo;
Dave Airlie414c4532012-04-17 15:01:25 +0100877 int ret;
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200878 s64 gpu_addr;
Dave Airlie414c4532012-04-17 15:01:25 +0100879
Thomas Zimmermannfb724f12020-05-15 10:32:21 +0200880 if (old_fb) {
881 gbo = drm_gem_vram_of_gem(old_fb->obj[0]);
Thomas Zimmermann81da87f2019-05-21 13:08:29 +0200882 drm_gem_vram_unpin(gbo);
Dave Airlie414c4532012-04-17 15:01:25 +0100883 }
884
Thomas Zimmermannfb724f12020-05-15 10:32:21 +0200885 gbo = drm_gem_vram_of_gem(fb->obj[0]);
Dave Airlie414c4532012-04-17 15:01:25 +0100886
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200887 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
888 if (ret)
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200889 return ret;
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200890 gpu_addr = drm_gem_vram_offset(gbo);
891 if (gpu_addr < 0) {
892 ret = (int)gpu_addr;
893 goto err_drm_gem_vram_unpin;
Dave Airlie414c4532012-04-17 15:01:25 +0100894 }
895
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200896 mgag200_set_startadd(mdev, (unsigned long)gpu_addr);
Dave Airlie414c4532012-04-17 15:01:25 +0100897
898 return 0;
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200899
900err_drm_gem_vram_unpin:
901 drm_gem_vram_unpin(gbo);
Thomas Zimmermannebb04eb2019-05-08 10:26:24 +0200902 return ret;
Dave Airlie414c4532012-04-17 15:01:25 +0100903}
904
905static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
906 struct drm_framebuffer *old_fb)
907{
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200908 struct drm_device *dev = crtc->dev;
909 struct mga_device *mdev = dev->dev_private;
Thomas Zimmermannfb724f12020-05-15 10:32:21 +0200910 struct drm_framebuffer *fb = crtc->primary->fb;
Thomas Zimmermannd6237682020-05-15 10:32:20 +0200911 unsigned int count;
912
913 do { } while (RREG8(0x1fda) & 0x08);
914 do { } while (!(RREG8(0x1fda) & 0x08));
915
916 count = RREG8(MGAREG_VCOUNT) + 2;
917 do { } while (RREG8(MGAREG_VCOUNT) < count);
918
Thomas Zimmermannfb724f12020-05-15 10:32:21 +0200919 return mga_crtc_do_set_base(mdev, fb, old_fb);
Dave Airlie414c4532012-04-17 15:01:25 +0100920}
921
Thomas Zimmermanna6edae02020-05-15 10:32:22 +0200922static void mgag200_set_mode_regs(struct mga_device *mdev,
923 const struct drm_display_mode *mode)
924{
925 unsigned int hdisplay, hsyncstart, hsyncend, htotal;
926 unsigned int vdisplay, vsyncstart, vsyncend, vtotal;
Thomas Zimmermanndb05f8d2020-05-15 10:32:23 +0200927 u8 misc, crtcext1, crtcext2, crtcext5;
Thomas Zimmermanna6edae02020-05-15 10:32:22 +0200928
929 hdisplay = mode->hdisplay / 8 - 1;
930 hsyncstart = mode->hsync_start / 8 - 1;
931 hsyncend = mode->hsync_end / 8 - 1;
932 htotal = mode->htotal / 8 - 1;
933
934 /* Work around hardware quirk */
935 if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
936 htotal++;
937
938 vdisplay = mode->vdisplay - 1;
939 vsyncstart = mode->vsync_start - 1;
940 vsyncend = mode->vsync_end - 1;
941 vtotal = mode->vtotal - 2;
942
Thomas Zimmermanndb05f8d2020-05-15 10:32:23 +0200943 misc = RREG8(MGA_MISC_IN);
944
Thomas Zimmermanna6edae02020-05-15 10:32:22 +0200945 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
Thomas Zimmermanndb05f8d2020-05-15 10:32:23 +0200946 misc |= MGAREG_MISC_HSYNCPOL;
947 else
948 misc &= ~MGAREG_MISC_HSYNCPOL;
949
Thomas Zimmermanna6edae02020-05-15 10:32:22 +0200950 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
Thomas Zimmermanndb05f8d2020-05-15 10:32:23 +0200951 misc |= MGAREG_MISC_VSYNCPOL;
952 else
953 misc &= ~MGAREG_MISC_VSYNCPOL;
Thomas Zimmermanna6edae02020-05-15 10:32:22 +0200954
955 crtcext1 = (((htotal - 4) & 0x100) >> 8) |
956 ((hdisplay & 0x100) >> 7) |
957 ((hsyncstart & 0x100) >> 6) |
958 (htotal & 0x40);
959 if (mdev->type == G200_WB || mdev->type == G200_EW3)
960 crtcext1 |= BIT(7) | /* vrsten */
961 BIT(3); /* hrsten */
962
963 crtcext2 = ((vtotal & 0xc00) >> 10) |
964 ((vdisplay & 0x400) >> 8) |
965 ((vdisplay & 0xc00) >> 7) |
966 ((vsyncstart & 0xc00) >> 5) |
967 ((vdisplay & 0x400) >> 3);
968 crtcext5 = 0x00;
969
970 WREG_CRT(0, htotal - 4);
971 WREG_CRT(1, hdisplay);
972 WREG_CRT(2, hdisplay);
973 WREG_CRT(3, (htotal & 0x1F) | 0x80);
974 WREG_CRT(4, hsyncstart);
975 WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
976 WREG_CRT(6, vtotal & 0xFF);
977 WREG_CRT(7, ((vtotal & 0x100) >> 8) |
978 ((vdisplay & 0x100) >> 7) |
979 ((vsyncstart & 0x100) >> 6) |
980 ((vdisplay & 0x100) >> 5) |
981 ((vdisplay & 0x100) >> 4) | /* linecomp */
982 ((vtotal & 0x200) >> 4) |
983 ((vdisplay & 0x200) >> 3) |
984 ((vsyncstart & 0x200) >> 2));
985 WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
986 ((vdisplay & 0x200) >> 3));
987 WREG_CRT(16, vsyncstart & 0xFF);
988 WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
989 WREG_CRT(18, vdisplay & 0xFF);
990 WREG_CRT(20, 0);
991 WREG_CRT(21, vdisplay & 0xFF);
992 WREG_CRT(22, (vtotal + 1) & 0xFF);
993 WREG_CRT(23, 0xc3);
994 WREG_CRT(24, vdisplay & 0xFF);
995
996 WREG_ECRT(0x01, crtcext1);
997 WREG_ECRT(0x02, crtcext2);
998 WREG_ECRT(0x05, crtcext5);
Thomas Zimmermanndb05f8d2020-05-15 10:32:23 +0200999
1000 WREG8(MGA_MISC_OUT, misc);
1001
1002 mga_crtc_set_plls(mdev, mode->clock);
Thomas Zimmermanna6edae02020-05-15 10:32:22 +02001003}
1004
Thomas Zimmermann72a03a32020-05-15 10:32:25 +02001005static u8 mgag200_get_bpp_shift(struct mga_device *mdev,
1006 const struct drm_format_info *format)
1007{
1008 return mdev->bpp_shifts[format->cpp[0] - 1];
1009}
1010
1011/*
1012 * Calculates the HW offset value from the framebuffer's pitch. The
1013 * offset is a multiple of the pixel size and depends on the display
1014 * format.
1015 */
1016static u32 mgag200_calculate_offset(struct mga_device *mdev,
1017 const struct drm_framebuffer *fb)
1018{
1019 u32 offset = fb->pitches[0] / fb->format->cpp[0];
1020 u8 bppshift = mgag200_get_bpp_shift(mdev, fb->format);
1021
1022 if (fb->format->cpp[0] * 8 == 24)
1023 offset = (offset * 3) >> (4 - bppshift);
1024 else
1025 offset = offset >> (4 - bppshift);
1026
1027 return offset;
1028}
1029
1030static void mgag200_set_offset(struct mga_device *mdev,
1031 const struct drm_framebuffer *fb)
1032{
1033 u8 crtc13, crtcext0;
1034 u32 offset = mgag200_calculate_offset(mdev, fb);
1035
1036 RREG_ECRT(0, crtcext0);
1037
1038 crtc13 = offset & 0xff;
1039
1040 crtcext0 &= ~MGAREG_CRTCEXT0_OFFSET_MASK;
1041 crtcext0 |= (offset >> 4) & MGAREG_CRTCEXT0_OFFSET_MASK;
1042
1043 WREG_CRT(0x13, crtc13);
1044 WREG_ECRT(0x00, crtcext0);
1045}
1046
Thomas Zimmermann836d5362020-05-15 10:32:26 +02001047static void mgag200_set_format_regs(struct mga_device *mdev,
1048 const struct drm_framebuffer *fb)
1049{
1050 struct drm_device *dev = mdev->dev;
1051 const struct drm_format_info *format = fb->format;
1052 unsigned int bpp, bppshift, scale;
1053 u8 crtcext3, xmulctrl;
1054
1055 bpp = format->cpp[0] * 8;
1056
1057 bppshift = mgag200_get_bpp_shift(mdev, format);
1058 switch (bpp) {
1059 case 24:
1060 scale = ((1 << bppshift) * 3) - 1;
1061 break;
1062 default:
1063 scale = (1 << bppshift) - 1;
1064 break;
1065 }
1066
1067 RREG_ECRT(3, crtcext3);
1068
1069 switch (bpp) {
1070 case 8:
1071 xmulctrl = MGA1064_MUL_CTL_8bits;
1072 break;
1073 case 16:
1074 if (format->depth == 15)
1075 xmulctrl = MGA1064_MUL_CTL_15bits;
1076 else
1077 xmulctrl = MGA1064_MUL_CTL_16bits;
1078 break;
1079 case 24:
1080 xmulctrl = MGA1064_MUL_CTL_24bits;
1081 break;
1082 case 32:
1083 xmulctrl = MGA1064_MUL_CTL_32_24bits;
1084 break;
1085 default:
1086 /* BUG: We should have caught this problem already. */
1087 drm_WARN_ON(dev, "invalid format depth\n");
1088 return;
1089 }
1090
1091 crtcext3 &= ~GENMASK(2, 0);
1092 crtcext3 |= scale;
1093
1094 WREG_DAC(MGA1064_MUL_CTL, xmulctrl);
1095
1096 WREG_GFX(0, 0x00);
1097 WREG_GFX(1, 0x00);
1098 WREG_GFX(2, 0x00);
1099 WREG_GFX(3, 0x00);
1100 WREG_GFX(4, 0x00);
1101 WREG_GFX(5, 0x40);
1102 WREG_GFX(6, 0x05);
1103 WREG_GFX(7, 0x0f);
1104 WREG_GFX(8, 0x0f);
1105
1106 WREG_ECRT(3, crtcext3);
1107}
1108
Thomas Zimmermann2e5ccbb2020-05-15 10:32:27 +02001109static void mgag200_g200er_reset_tagfifo(struct mga_device *mdev)
1110{
1111 static uint32_t RESET_FLAG = 0x00200000; /* undocumented magic value */
1112 u8 seq1;
1113 u32 memctl;
1114
1115 /* screen off */
1116 RREG_SEQ(0x01, seq1);
1117 seq1 |= MGAREG_SEQ1_SCROFF;
1118 WREG_SEQ(0x01, seq1);
1119
1120 memctl = RREG32(MGAREG_MEMCTL);
1121
1122 memctl |= RESET_FLAG;
1123 WREG32(MGAREG_MEMCTL, memctl);
1124
1125 udelay(1000);
1126
1127 memctl &= ~RESET_FLAG;
1128 WREG32(MGAREG_MEMCTL, memctl);
1129
1130 /* screen on */
1131 RREG_SEQ(0x01, seq1);
1132 seq1 &= ~MGAREG_SEQ1_SCROFF;
1133 WREG_SEQ(0x01, seq1);
1134}
1135
Thomas Zimmermann7fc1ae52020-05-15 10:32:28 +02001136static void mgag200_g200se_set_hiprilvl(struct mga_device *mdev,
1137 const struct drm_display_mode *mode,
1138 const struct drm_framebuffer *fb)
1139{
1140 unsigned int hiprilvl;
1141 u8 crtcext6;
1142
1143 if (mdev->unique_rev_id >= 0x04) {
1144 hiprilvl = 0;
1145 } else if (mdev->unique_rev_id >= 0x02) {
1146 unsigned int bpp;
1147 unsigned long mb;
1148
1149 if (fb->format->cpp[0] * 8 > 16)
1150 bpp = 32;
1151 else if (fb->format->cpp[0] * 8 > 8)
1152 bpp = 16;
1153 else
1154 bpp = 8;
1155
1156 mb = (mode->clock * bpp) / 1000;
1157 if (mb > 3100)
1158 hiprilvl = 0;
1159 else if (mb > 2600)
1160 hiprilvl = 1;
1161 else if (mb > 1900)
1162 hiprilvl = 2;
1163 else if (mb > 1160)
1164 hiprilvl = 3;
1165 else if (mb > 440)
1166 hiprilvl = 4;
1167 else
1168 hiprilvl = 5;
1169
1170 } else if (mdev->unique_rev_id >= 0x01) {
1171 hiprilvl = 3;
1172 } else {
1173 hiprilvl = 4;
1174 }
1175
1176 crtcext6 = hiprilvl; /* implicitly sets maxhipri to 0 */
1177
1178 WREG_ECRT(0x06, crtcext6);
1179}
1180
1181static void mgag200_g200ev_set_hiprilvl(struct mga_device *mdev)
1182{
1183 WREG_ECRT(0x06, 0x00);
1184}
1185
Dave Airlie414c4532012-04-17 15:01:25 +01001186static int mga_crtc_mode_set(struct drm_crtc *crtc,
1187 struct drm_display_mode *mode,
1188 struct drm_display_mode *adjusted_mode,
1189 int x, int y, struct drm_framebuffer *old_fb)
1190{
1191 struct drm_device *dev = crtc->dev;
Thomas Zimmermann8d8ff2a2020-05-07 11:03:10 +02001192 struct mga_device *mdev = to_mga_device(dev);
Ville Syrjälä72952752016-11-18 21:52:40 +02001193 const struct drm_framebuffer *fb = crtc->primary->fb;
Dave Airlie414c4532012-04-17 15:01:25 +01001194 int option = 0, option2 = 0;
1195 int i;
1196 unsigned char misc = 0;
Thomas Zimmermann836d5362020-05-15 10:32:26 +02001197 u8 crtcext3, crtcext4;
Dave Airlie414c4532012-04-17 15:01:25 +01001198
1199 static unsigned char dacvalue[] = {
1200 /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
1201 /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
1202 /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
1203 /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
1204 /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1205 /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
1206 /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
1207 /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
1208 /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
1209 /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
1210 };
1211
Dave Airlie414c4532012-04-17 15:01:25 +01001212 switch (mdev->type) {
1213 case G200_SE_A:
1214 case G200_SE_B:
1215 dacvalue[MGA1064_VREF_CTL] = 0x03;
1216 dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
1217 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
1218 MGA1064_MISC_CTL_VGA8 |
1219 MGA1064_MISC_CTL_DAC_RAM_CS;
1220 if (mdev->has_sdram)
1221 option = 0x40049120;
1222 else
1223 option = 0x4004d120;
1224 option2 = 0x00008000;
1225 break;
1226 case G200_WB:
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001227 case G200_EW3:
Dave Airlie414c4532012-04-17 15:01:25 +01001228 dacvalue[MGA1064_VREF_CTL] = 0x07;
1229 option = 0x41049120;
1230 option2 = 0x0000b000;
1231 break;
1232 case G200_EV:
1233 dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
1234 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
1235 MGA1064_MISC_CTL_DAC_RAM_CS;
1236 option = 0x00000120;
1237 option2 = 0x0000b000;
1238 break;
1239 case G200_EH:
Mathieu Larouchef0493e62016-10-21 12:47:07 -04001240 case G200_EH3:
Dave Airlie414c4532012-04-17 15:01:25 +01001241 dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
1242 MGA1064_MISC_CTL_DAC_RAM_CS;
1243 option = 0x00000120;
1244 option2 = 0x0000b000;
1245 break;
1246 case G200_ER:
Dave Airlie414c4532012-04-17 15:01:25 +01001247 break;
1248 }
1249
Dave Airlie414c4532012-04-17 15:01:25 +01001250 for (i = 0; i < sizeof(dacvalue); i++) {
Christopher Harvey9d8aa552013-04-12 20:42:19 +00001251 if ((i <= 0x17) ||
Dave Airlie414c4532012-04-17 15:01:25 +01001252 (i == 0x1b) ||
1253 (i == 0x1c) ||
1254 ((i >= 0x1f) && (i <= 0x29)) ||
1255 ((i >= 0x30) && (i <= 0x37)))
1256 continue;
1257 if (IS_G200_SE(mdev) &&
1258 ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
1259 continue;
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001260 if ((mdev->type == G200_EV ||
1261 mdev->type == G200_WB ||
1262 mdev->type == G200_EH ||
Mathieu Larouchef0493e62016-10-21 12:47:07 -04001263 mdev->type == G200_EW3 ||
1264 mdev->type == G200_EH3) &&
Dave Airlie414c4532012-04-17 15:01:25 +01001265 (i >= 0x44) && (i <= 0x4e))
1266 continue;
1267
1268 WREG_DAC(i, dacvalue[i]);
1269 }
1270
Christopher Harvey1812a3d2013-04-05 10:51:15 -04001271 if (mdev->type == G200_ER)
1272 WREG_DAC(0x90, 0);
Dave Airlie414c4532012-04-17 15:01:25 +01001273
1274 if (option)
1275 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
1276 if (option2)
1277 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
1278
1279 WREG_SEQ(2, 0xf);
1280 WREG_SEQ(3, 0);
1281 WREG_SEQ(4, 0xe);
1282
Dave Airlie414c4532012-04-17 15:01:25 +01001283 WREG_CRT(10, 0);
1284 WREG_CRT(11, 0);
1285 WREG_CRT(12, 0);
1286 WREG_CRT(13, 0);
1287 WREG_CRT(14, 0);
1288 WREG_CRT(15, 0);
Dave Airlie414c4532012-04-17 15:01:25 +01001289
Thomas Zimmermann836d5362020-05-15 10:32:26 +02001290 RREG_ECRT(0x03, crtcext3);
Dave Airlie414c4532012-04-17 15:01:25 +01001291
Thomas Zimmermann836d5362020-05-15 10:32:26 +02001292 crtcext3 |= BIT(7); /* enable MGA mode */
1293 crtcext4 = 0x00;
Dave Airlie414c4532012-04-17 15:01:25 +01001294
Thomas Zimmermann836d5362020-05-15 10:32:26 +02001295 WREG_ECRT(0x03, crtcext3);
1296 WREG_ECRT(0x04, crtcext4);
Dave Airlie414c4532012-04-17 15:01:25 +01001297
1298 if (mdev->type == G200_ER)
Christopher Harvey1812a3d2013-04-05 10:51:15 -04001299 WREG_ECRT(0x24, 0x5);
Dave Airlie414c4532012-04-17 15:01:25 +01001300
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001301 if (mdev->type == G200_EW3)
1302 WREG_ECRT(0x34, 0x5);
1303
Thomas Zimmermanndb05f8d2020-05-15 10:32:23 +02001304 misc = RREG8(MGA_MISC_IN);
1305 misc |= MGAREG_MISC_IOADSEL |
1306 MGAREG_MISC_RAMMAPEN |
1307 MGAREG_MISC_HIGH_PG_SEL;
Dave Airlie414c4532012-04-17 15:01:25 +01001308 WREG8(MGA_MISC_OUT, misc);
1309
Thomas Zimmermann836d5362020-05-15 10:32:26 +02001310 mgag200_set_format_regs(mdev, fb);
Thomas Zimmermannfb724f12020-05-15 10:32:21 +02001311 mga_crtc_do_set_base(mdev, fb, old_fb);
Thomas Zimmermann72a03a32020-05-15 10:32:25 +02001312 mgag200_set_offset(mdev, fb);
Dave Airlie414c4532012-04-17 15:01:25 +01001313
Thomas Zimmermannddb8d052020-05-15 10:32:24 +02001314 mgag200_set_mode_regs(mdev, mode);
1315
Thomas Zimmermann2e5ccbb2020-05-15 10:32:27 +02001316 if (mdev->type == G200_ER)
1317 mgag200_g200er_reset_tagfifo(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +01001318
Thomas Zimmermann7fc1ae52020-05-15 10:32:28 +02001319 if (IS_G200_SE(mdev))
1320 mgag200_g200se_set_hiprilvl(mdev, mode, fb);
1321 else if (mdev->type == G200_EV)
1322 mgag200_g200ev_set_hiprilvl(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +01001323
Dave Airlie414c4532012-04-17 15:01:25 +01001324 return 0;
1325}
1326
1327#if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
1328static int mga_suspend(struct drm_crtc *crtc)
1329{
1330 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1331 struct drm_device *dev = crtc->dev;
1332 struct mga_device *mdev = dev->dev_private;
1333 struct pci_dev *pdev = dev->pdev;
1334 int option;
1335
1336 if (mdev->suspended)
1337 return 0;
1338
1339 WREG_SEQ(1, 0x20);
1340 WREG_ECRT(1, 0x30);
1341 /* Disable the pixel clock */
1342 WREG_DAC(0x1a, 0x05);
1343 /* Power down the DAC */
1344 WREG_DAC(0x1e, 0x18);
1345 /* Power down the pixel PLL */
1346 WREG_DAC(0x1a, 0x0d);
1347
1348 /* Disable PLLs and clocks */
1349 pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1350 option &= ~(0x1F8024);
1351 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1352 pci_set_power_state(pdev, PCI_D3hot);
1353 pci_disable_device(pdev);
1354
1355 mdev->suspended = true;
1356
1357 return 0;
1358}
1359
1360static int mga_resume(struct drm_crtc *crtc)
1361{
1362 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1363 struct drm_device *dev = crtc->dev;
1364 struct mga_device *mdev = dev->dev_private;
1365 struct pci_dev *pdev = dev->pdev;
1366 int option;
1367
1368 if (!mdev->suspended)
1369 return 0;
1370
1371 pci_set_power_state(pdev, PCI_D0);
1372 pci_enable_device(pdev);
1373
1374 /* Disable sysclk */
1375 pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
1376 option &= ~(0x4);
1377 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
1378
1379 mdev->suspended = false;
1380
1381 return 0;
1382}
1383
1384#endif
1385
1386static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
1387{
1388 struct drm_device *dev = crtc->dev;
Thomas Zimmermann8d8ff2a2020-05-07 11:03:10 +02001389 struct mga_device *mdev = to_mga_device(dev);
Dave Airlie414c4532012-04-17 15:01:25 +01001390 u8 seq1 = 0, crtcext1 = 0;
1391
1392 switch (mode) {
1393 case DRM_MODE_DPMS_ON:
1394 seq1 = 0;
1395 crtcext1 = 0;
1396 mga_crtc_load_lut(crtc);
1397 break;
1398 case DRM_MODE_DPMS_STANDBY:
1399 seq1 = 0x20;
1400 crtcext1 = 0x10;
1401 break;
1402 case DRM_MODE_DPMS_SUSPEND:
1403 seq1 = 0x20;
1404 crtcext1 = 0x20;
1405 break;
1406 case DRM_MODE_DPMS_OFF:
1407 seq1 = 0x20;
1408 crtcext1 = 0x30;
1409 break;
1410 }
1411
1412#if 0
1413 if (mode == DRM_MODE_DPMS_OFF) {
1414 mga_suspend(crtc);
1415 }
1416#endif
1417 WREG8(MGAREG_SEQ_INDEX, 0x01);
1418 seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
1419 mga_wait_vsync(mdev);
1420 mga_wait_busy(mdev);
1421 WREG8(MGAREG_SEQ_DATA, seq1);
1422 msleep(20);
1423 WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
1424 crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
1425 WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
1426
1427#if 0
1428 if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
1429 mga_resume(crtc);
1430 drm_helper_resume_force_mode(dev);
1431 }
1432#endif
1433}
1434
1435/*
1436 * This is called before a mode is programmed. A typical use might be to
1437 * enable DPMS during the programming to avoid seeing intermediate stages,
1438 * but that's not relevant to us
1439 */
1440static void mga_crtc_prepare(struct drm_crtc *crtc)
1441{
1442 struct drm_device *dev = crtc->dev;
Thomas Zimmermann8d8ff2a2020-05-07 11:03:10 +02001443 struct mga_device *mdev = to_mga_device(dev);
Dave Airlie414c4532012-04-17 15:01:25 +01001444 u8 tmp;
1445
1446 /* mga_resume(crtc);*/
1447
1448 WREG8(MGAREG_CRTC_INDEX, 0x11);
1449 tmp = RREG8(MGAREG_CRTC_DATA);
1450 WREG_CRT(0x11, tmp | 0x80);
1451
1452 if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1453 WREG_SEQ(0, 1);
1454 msleep(50);
1455 WREG_SEQ(1, 0x20);
1456 msleep(20);
1457 } else {
1458 WREG8(MGAREG_SEQ_INDEX, 0x1);
1459 tmp = RREG8(MGAREG_SEQ_DATA);
1460
1461 /* start sync reset */
1462 WREG_SEQ(0, 1);
1463 WREG_SEQ(1, tmp | 0x20);
1464 }
1465
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001466 if (mdev->type == G200_WB || mdev->type == G200_EW3)
Dave Airlie414c4532012-04-17 15:01:25 +01001467 mga_g200wb_prepare(crtc);
1468
1469 WREG_CRT(17, 0);
1470}
1471
1472/*
1473 * This is called after a mode is programmed. It should reverse anything done
1474 * by the prepare function
1475 */
1476static void mga_crtc_commit(struct drm_crtc *crtc)
1477{
1478 struct drm_device *dev = crtc->dev;
Thomas Zimmermann8d8ff2a2020-05-07 11:03:10 +02001479 struct mga_device *mdev = to_mga_device(dev);
Jani Nikulad584ff82015-03-11 11:51:00 +02001480 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
Dave Airlie414c4532012-04-17 15:01:25 +01001481 u8 tmp;
1482
Mathieu Larouche6d857c12015-08-21 09:24:05 -04001483 if (mdev->type == G200_WB || mdev->type == G200_EW3)
Dave Airlie414c4532012-04-17 15:01:25 +01001484 mga_g200wb_commit(crtc);
1485
1486 if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
1487 msleep(50);
1488 WREG_SEQ(1, 0x0);
1489 msleep(20);
1490 WREG_SEQ(0, 0x3);
1491 } else {
1492 WREG8(MGAREG_SEQ_INDEX, 0x1);
1493 tmp = RREG8(MGAREG_SEQ_DATA);
1494
1495 tmp &= ~0x20;
1496 WREG_SEQ(0x1, tmp);
1497 WREG_SEQ(0, 3);
1498 }
1499 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1500}
1501
1502/*
1503 * The core can pass us a set of gamma values to program. We actually only
1504 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
1505 * but it's a requirement that we provide the function
1506 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001507static int mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02001508 u16 *blue, uint32_t size,
1509 struct drm_modeset_acquire_ctx *ctx)
Dave Airlie414c4532012-04-17 15:01:25 +01001510{
Dave Airlie414c4532012-04-17 15:01:25 +01001511 mga_crtc_load_lut(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001512
1513 return 0;
Dave Airlie414c4532012-04-17 15:01:25 +01001514}
1515
1516/* Simple cleanup function */
1517static void mga_crtc_destroy(struct drm_crtc *crtc)
1518{
1519 struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
1520
1521 drm_crtc_cleanup(crtc);
1522 kfree(mga_crtc);
1523}
1524
Egbert Eich64c29072013-07-17 15:07:22 +02001525static void mga_crtc_disable(struct drm_crtc *crtc)
1526{
Egbert Eich64c29072013-07-17 15:07:22 +02001527 DRM_DEBUG_KMS("\n");
1528 mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -07001529 if (crtc->primary->fb) {
Thomas Zimmermann5d177182019-06-27 10:09:09 +02001530 struct drm_framebuffer *fb = crtc->primary->fb;
1531 struct drm_gem_vram_object *gbo =
1532 drm_gem_vram_of_gem(fb->obj[0]);
Thomas Zimmermann81da87f2019-05-21 13:08:29 +02001533 drm_gem_vram_unpin(gbo);
Egbert Eich64c29072013-07-17 15:07:22 +02001534 }
Matt Roperf4510a22014-04-01 15:22:40 -07001535 crtc->primary->fb = NULL;
Egbert Eich64c29072013-07-17 15:07:22 +02001536}
1537
Dave Airlie414c4532012-04-17 15:01:25 +01001538/* These provide the minimum set of functions required to handle a CRTC */
1539static const struct drm_crtc_funcs mga_crtc_funcs = {
1540 .gamma_set = mga_crtc_gamma_set,
1541 .set_config = drm_crtc_helper_set_config,
1542 .destroy = mga_crtc_destroy,
1543};
1544
1545static const struct drm_crtc_helper_funcs mga_helper_funcs = {
Egbert Eich64c29072013-07-17 15:07:22 +02001546 .disable = mga_crtc_disable,
Dave Airlie414c4532012-04-17 15:01:25 +01001547 .dpms = mga_crtc_dpms,
Dave Airlie414c4532012-04-17 15:01:25 +01001548 .mode_set = mga_crtc_mode_set,
1549 .mode_set_base = mga_crtc_mode_set_base,
1550 .prepare = mga_crtc_prepare,
1551 .commit = mga_crtc_commit,
Dave Airlie414c4532012-04-17 15:01:25 +01001552};
1553
1554/* CRTC setup */
Christopher Harveyf1998fe2013-02-20 09:34:22 -05001555static void mga_crtc_init(struct mga_device *mdev)
Dave Airlie414c4532012-04-17 15:01:25 +01001556{
Thomas Zimmermanned5877b2020-05-07 11:03:11 +02001557 struct drm_device *dev = mdev->dev;
Dave Airlie414c4532012-04-17 15:01:25 +01001558 struct mga_crtc *mga_crtc;
Dave Airlie414c4532012-04-17 15:01:25 +01001559
1560 mga_crtc = kzalloc(sizeof(struct mga_crtc) +
1561 (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
1562 GFP_KERNEL);
1563
1564 if (mga_crtc == NULL)
1565 return;
1566
Thomas Zimmermanned5877b2020-05-07 11:03:11 +02001567 drm_crtc_init(dev, &mga_crtc->base, &mga_crtc_funcs);
Dave Airlie414c4532012-04-17 15:01:25 +01001568
1569 drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
Dave Airlie414c4532012-04-17 15:01:25 +01001570
Dave Airlie414c4532012-04-17 15:01:25 +01001571 drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
1572}
1573
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001574/*
1575 * Connector
1576 */
1577
Dave Airlie414c4532012-04-17 15:01:25 +01001578static int mga_vga_get_modes(struct drm_connector *connector)
1579{
1580 struct mga_connector *mga_connector = to_mga_connector(connector);
1581 struct edid *edid;
1582 int ret = 0;
1583
1584 edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
1585 if (edid) {
Daniel Vetterc555f022018-07-09 10:40:06 +02001586 drm_connector_update_edid_property(connector, edid);
Dave Airlie414c4532012-04-17 15:01:25 +01001587 ret = drm_add_edid_modes(connector, edid);
Dave Airlie414c4532012-04-17 15:01:25 +01001588 kfree(edid);
1589 }
1590 return ret;
1591}
1592
Julia Lemireabbee622013-06-27 13:38:59 -04001593static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
1594 int bits_per_pixel)
1595{
1596 uint32_t total_area, divisor;
Nicolas Pitrec24ca5b2015-11-03 23:09:58 -05001597 uint64_t active_area, pixels_per_second, bandwidth;
Julia Lemireabbee622013-06-27 13:38:59 -04001598 uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
1599
1600 divisor = 1024;
1601
1602 if (!mode->htotal || !mode->vtotal || !mode->clock)
1603 return 0;
1604
1605 active_area = mode->hdisplay * mode->vdisplay;
1606 total_area = mode->htotal * mode->vtotal;
1607
1608 pixels_per_second = active_area * mode->clock * 1000;
1609 do_div(pixels_per_second, total_area);
1610
1611 bandwidth = pixels_per_second * bytes_per_pixel * 100;
1612 do_div(bandwidth, divisor);
1613
1614 return (uint32_t)(bandwidth);
1615}
1616
1617#define MODE_BANDWIDTH MODE_BAD
1618
Luc Van Oostenryckc69e52d2018-04-24 15:15:06 +02001619static enum drm_mode_status mga_vga_mode_valid(struct drm_connector *connector,
Dave Airlie414c4532012-04-17 15:01:25 +01001620 struct drm_display_mode *mode)
1621{
Christopher Harvey0ba53172013-02-26 10:55:44 -05001622 struct drm_device *dev = connector->dev;
Thomas Zimmermann8d8ff2a2020-05-07 11:03:10 +02001623 struct mga_device *mdev = to_mga_device(dev);
Christopher Harvey0ba53172013-02-26 10:55:44 -05001624 int bpp = 32;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001625
Julia Lemireabbee622013-06-27 13:38:59 -04001626 if (IS_G200_SE(mdev)) {
1627 if (mdev->unique_rev_id == 0x01) {
1628 if (mode->hdisplay > 1600)
1629 return MODE_VIRTUAL_X;
1630 if (mode->vdisplay > 1200)
1631 return MODE_VIRTUAL_Y;
1632 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1633 > (24400 * 1024))
1634 return MODE_BANDWIDTH;
Mathieu Larouchee829d7e2015-08-21 09:24:13 -04001635 } else if (mdev->unique_rev_id == 0x02) {
Julia Lemireabbee622013-06-27 13:38:59 -04001636 if (mode->hdisplay > 1920)
1637 return MODE_VIRTUAL_X;
1638 if (mode->vdisplay > 1200)
1639 return MODE_VIRTUAL_Y;
1640 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1641 > (30100 * 1024))
1642 return MODE_BANDWIDTH;
Mathieu Larouche0cbb7382017-06-14 10:39:42 -04001643 } else {
1644 if (mga_vga_calculate_mode_bandwidth(mode, bpp)
1645 > (55000 * 1024))
1646 return MODE_BANDWIDTH;
Julia Lemireabbee622013-06-27 13:38:59 -04001647 }
1648 } else if (mdev->type == G200_WB) {
1649 if (mode->hdisplay > 1280)
1650 return MODE_VIRTUAL_X;
1651 if (mode->vdisplay > 1024)
1652 return MODE_VIRTUAL_Y;
Dan Carpenter9eb8d7a2018-01-25 17:26:55 +03001653 if (mga_vga_calculate_mode_bandwidth(mode, bpp) >
1654 (31877 * 1024))
Julia Lemireabbee622013-06-27 13:38:59 -04001655 return MODE_BANDWIDTH;
1656 } else if (mdev->type == G200_EV &&
1657 (mga_vga_calculate_mode_bandwidth(mode, bpp)
1658 > (32700 * 1024))) {
1659 return MODE_BANDWIDTH;
Dave Airlieec22b4a2014-02-05 14:13:56 +10001660 } else if (mdev->type == G200_EH &&
Julia Lemireabbee622013-06-27 13:38:59 -04001661 (mga_vga_calculate_mode_bandwidth(mode, bpp)
1662 > (37500 * 1024))) {
1663 return MODE_BANDWIDTH;
Dave Airlieec22b4a2014-02-05 14:13:56 +10001664 } else if (mdev->type == G200_ER &&
Julia Lemireabbee622013-06-27 13:38:59 -04001665 (mga_vga_calculate_mode_bandwidth(mode,
1666 bpp) > (55000 * 1024))) {
1667 return MODE_BANDWIDTH;
1668 }
Dave Airlie414c4532012-04-17 15:01:25 +01001669
Adam Jackson25161082015-06-15 16:16:15 -04001670 if ((mode->hdisplay % 8) != 0 || (mode->hsync_start % 8) != 0 ||
1671 (mode->hsync_end % 8) != 0 || (mode->htotal % 8) != 0) {
1672 return MODE_H_ILLEGAL;
1673 }
1674
Dave Airlie414c4532012-04-17 15:01:25 +01001675 if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
1676 mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
1677 mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
1678 mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
1679 return MODE_BAD;
1680 }
1681
Christopher Harvey0ba53172013-02-26 10:55:44 -05001682 /* Validate the mode input by the user */
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001683 if (connector->cmdline_mode.specified) {
1684 if (connector->cmdline_mode.bpp_specified)
1685 bpp = connector->cmdline_mode.bpp;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001686 }
1687
Thomas Zimmermann2c51a662019-09-27 11:13:00 +02001688 if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->vram_fb_available) {
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001689 if (connector->cmdline_mode.specified)
1690 connector->cmdline_mode.specified = false;
Christopher Harvey0ba53172013-02-26 10:55:44 -05001691 return MODE_BAD;
1692 }
1693
Dave Airlie414c4532012-04-17 15:01:25 +01001694 return MODE_OK;
1695}
1696
Dave Airlie414c4532012-04-17 15:01:25 +01001697static void mga_connector_destroy(struct drm_connector *connector)
1698{
1699 struct mga_connector *mga_connector = to_mga_connector(connector);
1700 mgag200_i2c_destroy(mga_connector->i2c);
1701 drm_connector_cleanup(connector);
Dave Airlie414c4532012-04-17 15:01:25 +01001702}
1703
Ville Syrjälä71cb7492015-12-15 12:21:10 +01001704static const struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
Dave Airlie414c4532012-04-17 15:01:25 +01001705 .get_modes = mga_vga_get_modes,
1706 .mode_valid = mga_vga_mode_valid,
Dave Airlie414c4532012-04-17 15:01:25 +01001707};
1708
Ville Syrjälä71cb7492015-12-15 12:21:10 +01001709static const struct drm_connector_funcs mga_vga_connector_funcs = {
Dave Airlie414c4532012-04-17 15:01:25 +01001710 .dpms = drm_helper_connector_dpms,
Dave Airlie414c4532012-04-17 15:01:25 +01001711 .fill_modes = drm_helper_probe_single_connector_modes,
1712 .destroy = mga_connector_destroy,
1713};
1714
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001715static int mgag200_vga_connector_init(struct mga_device *mdev)
Dave Airlie414c4532012-04-17 15:01:25 +01001716{
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001717 struct drm_device *dev = mdev->dev;
1718 struct mga_connector *mconnector = &mdev->connector;
1719 struct drm_connector *connector = &mconnector->base;
1720 struct mga_i2c_chan *i2c;
1721 int ret;
Dave Airlie414c4532012-04-17 15:01:25 +01001722
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001723 i2c = mgag200_i2c_create(dev);
1724 if (!i2c)
1725 drm_warn(dev, "failed to add DDC bus\n");
Dave Airlie414c4532012-04-17 15:01:25 +01001726
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001727 ret = drm_connector_init_with_ddc(dev, connector,
1728 &mga_vga_connector_funcs,
1729 DRM_MODE_CONNECTOR_VGA,
1730 &i2c->adapter);
1731 if (ret)
1732 goto err_mgag200_i2c_destroy;
Dave Airlie414c4532012-04-17 15:01:25 +01001733 drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
1734
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001735 mconnector->i2c = i2c;
Egbert Eich3d5a1c52013-07-17 15:07:25 +02001736
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001737 return 0;
1738
1739err_mgag200_i2c_destroy:
1740 mgag200_i2c_destroy(i2c);
1741 return ret;
Dave Airlie414c4532012-04-17 15:01:25 +01001742}
1743
Thomas Zimmermann5635b7c2020-05-07 11:03:13 +02001744static const struct drm_mode_config_funcs mgag200_mode_config_funcs = {
1745 .fb_create = drm_gem_fb_create
1746};
1747
1748static unsigned int mgag200_preferred_depth(struct mga_device *mdev)
1749{
1750 if (IS_G200_SE(mdev) && mdev->vram_fb_available < (2048*1024))
1751 return 16;
1752 else
1753 return 32;
1754}
Dave Airlie414c4532012-04-17 15:01:25 +01001755
1756int mgag200_modeset_init(struct mga_device *mdev)
1757{
Thomas Zimmermanned5877b2020-05-07 11:03:11 +02001758 struct drm_device *dev = mdev->dev;
Thomas Zimmermann03e44ad2020-02-28 09:18:27 +01001759 struct drm_encoder *encoder = &mdev->encoder;
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001760 struct drm_connector *connector = &mdev->connector.base;
Thomas Zimmermann03e44ad2020-02-28 09:18:27 +01001761 int ret;
Dave Airlie414c4532012-04-17 15:01:25 +01001762
Thomas Zimmermann5635b7c2020-05-07 11:03:13 +02001763 mdev->bpp_shifts[0] = 0;
1764 mdev->bpp_shifts[1] = 1;
1765 mdev->bpp_shifts[2] = 0;
1766 mdev->bpp_shifts[3] = 2;
1767
1768 ret = drmm_mode_config_init(dev);
1769 if (ret) {
1770 drm_err(dev, "drmm_mode_config_init() failed, error %d\n",
1771 ret);
1772 return ret;
1773 }
1774
Thomas Zimmermanned5877b2020-05-07 11:03:11 +02001775 dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
1776 dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
Dave Airlie414c4532012-04-17 15:01:25 +01001777
Thomas Zimmermann5635b7c2020-05-07 11:03:13 +02001778 dev->mode_config.preferred_depth = mgag200_preferred_depth(mdev);
1779 dev->mode_config.prefer_shadow = 1;
1780
Thomas Zimmermanned5877b2020-05-07 11:03:11 +02001781 dev->mode_config.fb_base = mdev->mc.vram_base;
Dave Airlie414c4532012-04-17 15:01:25 +01001782
Thomas Zimmermann5635b7c2020-05-07 11:03:13 +02001783 dev->mode_config.funcs = &mgag200_mode_config_funcs;
1784
Christopher Harveyf1998fe2013-02-20 09:34:22 -05001785 mga_crtc_init(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +01001786
Thomas Zimmermanned5877b2020-05-07 11:03:11 +02001787 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_DAC);
Thomas Zimmermann03e44ad2020-02-28 09:18:27 +01001788 if (ret) {
Thomas Zimmermanned5877b2020-05-07 11:03:11 +02001789 drm_err(dev,
Thomas Zimmermann03e44ad2020-02-28 09:18:27 +01001790 "drm_simple_encoder_init() failed, error %d\n",
1791 ret);
1792 return ret;
Dave Airlie414c4532012-04-17 15:01:25 +01001793 }
Thomas Zimmermann03e44ad2020-02-28 09:18:27 +01001794 encoder->possible_crtcs = 0x1;
Dave Airlie414c4532012-04-17 15:01:25 +01001795
Thomas Zimmermann81a15b92020-05-07 11:03:15 +02001796 ret = mgag200_vga_connector_init(mdev);
1797 if (ret) {
1798 drm_err(dev,
1799 "mgag200_vga_connector_init() failed, error %d\n",
1800 ret);
1801 return ret;
Dave Airlie414c4532012-04-17 15:01:25 +01001802 }
1803
Daniel Vettercde4c442018-07-09 10:40:07 +02001804 drm_connector_attach_encoder(connector, encoder);
Dave Airlie414c4532012-04-17 15:01:25 +01001805
Dave Airlie414c4532012-04-17 15:01:25 +01001806 return 0;
1807}