Thomas Gleixner | c51669e | 2019-05-31 01:09:37 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2010 Matt Turner. |
| 4 | * Copyright 2012 Red Hat |
| 5 | * |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 6 | * Authors: Matthew Garrett |
| 7 | * Matt Turner |
| 8 | * Dave Airlie |
| 9 | */ |
| 10 | |
| 11 | #include <linux/delay.h> |
| 12 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 13 | #include <drm/drm_crtc_helper.h> |
Sam Ravnborg | 9f39780 | 2019-06-23 12:35:42 +0200 | [diff] [blame^] | 14 | #include <drm/drm_fourcc.h> |
| 15 | #include <drm/drm_pci.h> |
Daniel Vetter | 3cb9ae4 | 2014-10-29 10:03:57 +0100 | [diff] [blame] | 16 | #include <drm/drm_plane_helper.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 17 | #include <drm/drm_probe_helper.h> |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 18 | |
| 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 | |
| 27 | static void mga_crtc_load_lut(struct drm_crtc *crtc) |
| 28 | { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 29 | struct drm_device *dev = crtc->dev; |
| 30 | struct mga_device *mdev = dev->dev_private; |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 31 | struct drm_framebuffer *fb = crtc->primary->fb; |
Peter Rosin | 9ed85e1 | 2017-07-13 18:25:34 +0200 | [diff] [blame] | 32 | u16 *r_ptr, *g_ptr, *b_ptr; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 33 | int i; |
| 34 | |
| 35 | if (!crtc->enabled) |
| 36 | return; |
| 37 | |
Peter Rosin | 9ed85e1 | 2017-07-13 18:25:34 +0200 | [diff] [blame] | 38 | r_ptr = crtc->gamma_store; |
| 39 | g_ptr = r_ptr + crtc->gamma_size; |
| 40 | b_ptr = g_ptr + crtc->gamma_size; |
| 41 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 42 | WREG8(DAC_INDEX + MGA1064_INDEX, 0); |
| 43 | |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 44 | if (fb && fb->format->cpp[0] * 8 == 16) { |
Ville Syrjälä | b00c600 | 2016-12-14 23:31:35 +0200 | [diff] [blame] | 45 | int inc = (fb->format->depth == 15) ? 8 : 4; |
Egbert Eich | de7500e | 2013-07-17 15:07:27 +0200 | [diff] [blame] | 46 | u8 r, b; |
| 47 | for (i = 0; i < MGAG200_LUT_SIZE; i += inc) { |
Ville Syrjälä | b00c600 | 2016-12-14 23:31:35 +0200 | [diff] [blame] | 48 | if (fb->format->depth == 16) { |
Egbert Eich | de7500e | 2013-07-17 15:07:27 +0200 | [diff] [blame] | 49 | if (i > (MGAG200_LUT_SIZE >> 1)) { |
| 50 | r = b = 0; |
| 51 | } else { |
Peter Rosin | 9ed85e1 | 2017-07-13 18:25:34 +0200 | [diff] [blame] | 52 | r = *r_ptr++ >> 8; |
| 53 | b = *b_ptr++ >> 8; |
| 54 | r_ptr++; |
| 55 | b_ptr++; |
Egbert Eich | de7500e | 2013-07-17 15:07:27 +0200 | [diff] [blame] | 56 | } |
| 57 | } else { |
Peter Rosin | 9ed85e1 | 2017-07-13 18:25:34 +0200 | [diff] [blame] | 58 | r = *r_ptr++ >> 8; |
| 59 | b = *b_ptr++ >> 8; |
Egbert Eich | de7500e | 2013-07-17 15:07:27 +0200 | [diff] [blame] | 60 | } |
| 61 | /* VGA registers */ |
| 62 | WREG8(DAC_INDEX + MGA1064_COL_PAL, r); |
Peter Rosin | 9ed85e1 | 2017-07-13 18:25:34 +0200 | [diff] [blame] | 63 | WREG8(DAC_INDEX + MGA1064_COL_PAL, *g_ptr++ >> 8); |
Egbert Eich | de7500e | 2013-07-17 15:07:27 +0200 | [diff] [blame] | 64 | WREG8(DAC_INDEX + MGA1064_COL_PAL, b); |
| 65 | } |
| 66 | return; |
| 67 | } |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 68 | for (i = 0; i < MGAG200_LUT_SIZE; i++) { |
| 69 | /* VGA registers */ |
Peter Rosin | 9ed85e1 | 2017-07-13 18:25:34 +0200 | [diff] [blame] | 70 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | static inline void mga_wait_vsync(struct mga_device *mdev) |
| 77 | { |
Christopher Harvey | 3cdc0e8 | 2013-05-06 15:56:17 +0000 | [diff] [blame] | 78 | unsigned long timeout = jiffies + HZ/10; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 79 | unsigned int status = 0; |
| 80 | |
| 81 | do { |
| 82 | status = RREG32(MGAREG_Status); |
Christopher Harvey | 3cdc0e8 | 2013-05-06 15:56:17 +0000 | [diff] [blame] | 83 | } while ((status & 0x08) && time_before(jiffies, timeout)); |
| 84 | timeout = jiffies + HZ/10; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 85 | status = 0; |
| 86 | do { |
| 87 | status = RREG32(MGAREG_Status); |
Christopher Harvey | 3cdc0e8 | 2013-05-06 15:56:17 +0000 | [diff] [blame] | 88 | } while (!(status & 0x08) && time_before(jiffies, timeout)); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static inline void mga_wait_busy(struct mga_device *mdev) |
| 92 | { |
Christopher Harvey | 3cdc0e8 | 2013-05-06 15:56:17 +0000 | [diff] [blame] | 93 | unsigned long timeout = jiffies + HZ; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 94 | unsigned int status = 0; |
| 95 | do { |
| 96 | status = RREG8(MGAREG_Status + 2); |
Christopher Harvey | 3cdc0e8 | 2013-05-06 15:56:17 +0000 | [diff] [blame] | 97 | } while ((status & 0x01) && time_before(jiffies, timeout)); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Mathieu Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 100 | #define P_ARRAY_SIZE 9 |
| 101 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 102 | static 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 Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 109 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 112 | |
Mathieu Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 113 | if (mdev->unique_rev_id <= 0x03) { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 114 | |
Mathieu Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 115 | m = n = p = 0; |
| 116 | vcomax = 320000; |
| 117 | vcomin = 160000; |
| 118 | pllreffreq = 25000; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 119 | |
Mathieu Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 120 | delta = 0xffffffff; |
| 121 | permitteddelta = clock * 5 / 1000; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 122 | |
Mathieu Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 123 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
Mathieu Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 146 | } 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 Larouche | d3922b6 | 2016-05-27 15:12:50 -0400 | [diff] [blame] | 190 | fvv = pllreffreq * (n + 1) / (m + 1); |
Mathieu Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 191 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (delta > permitteddelta) { |
Joe Perches | 8dfe162 | 2017-02-28 04:55:54 -0800 | [diff] [blame] | 203 | pr_warn("PLL delta too large\n"); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 204 | 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 Larouche | d3922b6 | 2016-05-27 15:12:50 -0400 | [diff] [blame] | 210 | |
| 211 | if (mdev->unique_rev_id >= 0x04) { |
| 212 | WREG_DAC(0x1a, 0x09); |
| 213 | msleep(20); |
| 214 | WREG_DAC(0x1a, 0x01); |
| 215 | |
| 216 | } |
| 217 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int mga_g200wb_set_plls(struct mga_device *mdev, long clock) |
| 222 | { |
| 223 | unsigned int vcomax, vcomin, pllreffreq; |
Sudip Mukherjee | 546aee5 | 2015-07-01 17:12:45 +0530 | [diff] [blame] | 224 | unsigned int delta, tmpdelta; |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 225 | unsigned int testp, testm, testn, testp2; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 226 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 233 | |
| 234 | delta = 0xffffffff; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 235 | |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 236 | if (mdev->type == G200_EW3) { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 237 | |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 238 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 298 | } |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 315 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 316 | |
| 317 | WREG8(DAC_INDEX, MGA1064_REMHEADCTL); |
| 318 | tmp = RREG8(DAC_DATA); |
| 319 | tmp |= MGA1064_REMHEADCTL_CLKDIS; |
Christopher Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 320 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 321 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 330 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 331 | |
| 332 | udelay(500); |
| 333 | |
| 334 | /* reset the PLL */ |
| 335 | WREG8(DAC_INDEX, MGA1064_VREF_CTL); |
| 336 | tmp = RREG8(DAC_DATA); |
| 337 | tmp &= ~0x04; |
Christopher Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 338 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 339 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 362 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 363 | |
| 364 | WREG8(DAC_INDEX, MGA1064_REMHEADCTL); |
| 365 | tmp = RREG8(DAC_DATA); |
| 366 | tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK; |
| 367 | tmp |= MGA1064_REMHEADCTL_CLKSL_PLL; |
Christopher Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 368 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 369 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 379 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 380 | |
| 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 | |
| 400 | static int mga_g200ev_set_plls(struct mga_device *mdev, long clock) |
| 401 | { |
| 402 | unsigned int vcomax, vcomin, pllreffreq; |
Sudip Mukherjee | 546aee5 | 2015-07-01 17:12:45 +0530 | [diff] [blame] | 403 | unsigned int delta, tmpdelta; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 404 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 415 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 443 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 444 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 451 | WREG8(DAC_DATA, tmp & ~0x40); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 452 | |
| 453 | WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL); |
| 454 | tmp = RREG8(DAC_DATA); |
| 455 | tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN; |
Christopher Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 456 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 457 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 467 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 468 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 475 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 476 | |
| 477 | WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT); |
| 478 | tmp = RREG8(DAC_DATA); |
Christopher Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 479 | WREG8(DAC_DATA, tmp | 0x40); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 480 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 488 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int mga_g200eh_set_plls(struct mga_device *mdev, long clock) |
| 494 | { |
| 495 | unsigned int vcomax, vcomin, pllreffreq; |
Sudip Mukherjee | 546aee5 | 2015-07-01 17:12:45 +0530 | [diff] [blame] | 496 | unsigned int delta, tmpdelta; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 497 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 505 | |
Mathieu Larouche | f0493e6 | 2016-10-21 12:47:07 -0400 | [diff] [blame] | 506 | if (mdev->type == G200_EH3) { |
| 507 | vcomax = 3000000; |
| 508 | vcomin = 1500000; |
| 509 | pllreffreq = 25000; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 510 | |
Mathieu Larouche | f0493e6 | 2016-10-21 12:47:07 -0400 | [diff] [blame] | 511 | delta = 0xffffffff; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 512 | |
Mathieu Larouche | f0493e6 | 2016-10-21 12:47:07 -0400 | [diff] [blame] | 513 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 522 | if (computed > clock) |
| 523 | tmpdelta = computed - clock; |
| 524 | else |
| 525 | tmpdelta = clock - computed; |
| 526 | if (tmpdelta < delta) { |
| 527 | delta = tmpdelta; |
Mathieu Larouche | f0493e6 | 2016-10-21 12:47:07 -0400 | [diff] [blame] | 528 | n = testn; |
| 529 | m = testm; |
| 530 | p = testp; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 531 | } |
Mathieu Larouche | f0493e6 | 2016-10-21 12:47:07 -0400 | [diff] [blame] | 532 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 569 | } |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 576 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 577 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 585 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 586 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 599 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 600 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 605 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 606 | |
| 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 | |
| 623 | static int mga_g200er_set_plls(struct mga_device *mdev, long clock) |
| 624 | { |
| 625 | unsigned int vcomax, vcomin, pllreffreq; |
| 626 | unsigned int delta, tmpdelta; |
Dave Airlie | 9830605 | 2012-08-09 15:00:15 +1000 | [diff] [blame] | 627 | int testr, testn, testm, testo; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 628 | unsigned int p, m, n; |
Dave Airlie | 9830605 | 2012-08-09 15:00:15 +1000 | [diff] [blame] | 629 | unsigned int computed, vco; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 630 | int tmp; |
Dave Airlie | 9830605 | 2012-08-09 15:00:15 +1000 | [diff] [blame] | 631 | const unsigned int m_div_val[] = { 1, 2, 4, 8 }; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 632 | |
| 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 Airlie | 9830605 | 2012-08-09 15:00:15 +1000 | [diff] [blame] | 650 | vco = pllreffreq * (testn + 1) / |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 651 | (testr + 1); |
Dave Airlie | 9830605 | 2012-08-09 15:00:15 +1000 | [diff] [blame] | 652 | if (vco < vcomin) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 653 | continue; |
Dave Airlie | 9830605 | 2012-08-09 15:00:15 +1000 | [diff] [blame] | 654 | if (vco > vcomax) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 655 | continue; |
Dave Airlie | 9830605 | 2012-08-09 15:00:15 +1000 | [diff] [blame] | 656 | computed = vco / (m_div_val[testm] * (testo + 1)); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 657 | 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 675 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 676 | |
| 677 | WREG8(DAC_INDEX, MGA1064_REMHEADCTL); |
| 678 | tmp = RREG8(DAC_DATA); |
| 679 | tmp |= MGA1064_REMHEADCTL_CLKDIS; |
Christopher Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 680 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 681 | |
| 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 Harvey | fb70a66 | 2013-04-12 22:24:05 +0000 | [diff] [blame] | 690 | WREG8(DAC_DATA, tmp); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 691 | |
| 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 | |
| 703 | static 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 Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 711 | case G200_EW3: |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 712 | 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 Larouche | f0493e6 | 2016-10-21 12:47:07 -0400 | [diff] [blame] | 718 | case G200_EH3: |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 719 | 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 | |
| 728 | static 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 | |
| 782 | static 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 Harvey | 9f1d036 | 2013-05-08 19:10:38 +0000 | [diff] [blame] | 819 | /* |
| 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 | */ |
Rashika | 080fd6b | 2014-01-06 20:37:15 +0530 | [diff] [blame] | 833 | static void mga_set_start_address(struct drm_crtc *crtc, unsigned offset) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 834 | { |
| 835 | struct mga_device *mdev = crtc->dev->dev_private; |
| 836 | u32 addr; |
| 837 | int count; |
Christopher Harvey | 9f1d036 | 2013-05-08 19:10:38 +0000 | [diff] [blame] | 838 | u8 crtcext0; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 839 | |
| 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 Harvey | 9f1d036 | 2013-05-08 19:10:38 +0000 | [diff] [blame] | 846 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 854 | WREG_CRT(0x0d, (u8)(addr & 0xff)); |
| 855 | WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff); |
Christopher Harvey | 9f1d036 | 2013-05-08 19:10:38 +0000 | [diff] [blame] | 856 | WREG_ECRT(0x0, ((u8)(addr >> 16) & 0xf) | crtcext0); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 859 | static 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 Zimmermann | ebb04eb | 2019-05-08 10:26:24 +0200 | [diff] [blame] | 864 | struct drm_gem_vram_object *gbo; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 865 | int ret; |
Thomas Zimmermann | ebb04eb | 2019-05-08 10:26:24 +0200 | [diff] [blame] | 866 | s64 gpu_addr; |
Thomas Zimmermann | a758134 | 2019-05-08 10:26:26 +0200 | [diff] [blame] | 867 | void *base; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 868 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 869 | if (!atomic && fb) { |
Thomas Zimmermann | 5d17718 | 2019-06-27 10:09:09 +0200 | [diff] [blame] | 870 | gbo = drm_gem_vram_of_gem(fb->obj[0]); |
Thomas Zimmermann | 81da87f | 2019-05-21 13:08:29 +0200 | [diff] [blame] | 871 | |
| 872 | /* unmap if console */ |
Thomas Zimmermann | 5d17718 | 2019-06-27 10:09:09 +0200 | [diff] [blame] | 873 | if (mdev->mfbdev->helper.fb == fb) |
Thomas Zimmermann | 81da87f | 2019-05-21 13:08:29 +0200 | [diff] [blame] | 874 | drm_gem_vram_kunmap(gbo); |
| 875 | drm_gem_vram_unpin(gbo); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 876 | } |
| 877 | |
Thomas Zimmermann | 5d17718 | 2019-06-27 10:09:09 +0200 | [diff] [blame] | 878 | gbo = drm_gem_vram_of_gem(crtc->primary->fb->obj[0]); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 879 | |
Thomas Zimmermann | ebb04eb | 2019-05-08 10:26:24 +0200 | [diff] [blame] | 880 | ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM); |
| 881 | if (ret) |
Thomas Zimmermann | 5b24f71 | 2019-05-16 18:27:46 +0200 | [diff] [blame] | 882 | return ret; |
Thomas Zimmermann | ebb04eb | 2019-05-08 10:26:24 +0200 | [diff] [blame] | 883 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Thomas Zimmermann | 5d17718 | 2019-06-27 10:09:09 +0200 | [diff] [blame] | 889 | if (mdev->mfbdev->helper.fb == crtc->primary->fb) { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 890 | /* if pushing console in kmap it */ |
Thomas Zimmermann | a758134 | 2019-05-08 10:26:26 +0200 | [diff] [blame] | 891 | base = drm_gem_vram_kmap(gbo, true, NULL); |
| 892 | if (IS_ERR(base)) { |
| 893 | ret = PTR_ERR(base); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 894 | DRM_ERROR("failed to kmap fbcon\n"); |
Thomas Zimmermann | a758134 | 2019-05-08 10:26:26 +0200 | [diff] [blame] | 895 | } |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 896 | } |
Thomas Zimmermann | a758134 | 2019-05-08 10:26:26 +0200 | [diff] [blame] | 897 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 898 | mga_set_start_address(crtc, (u32)gpu_addr); |
| 899 | |
| 900 | return 0; |
Thomas Zimmermann | ebb04eb | 2019-05-08 10:26:24 +0200 | [diff] [blame] | 901 | |
| 902 | err_drm_gem_vram_unpin: |
| 903 | drm_gem_vram_unpin(gbo); |
Thomas Zimmermann | ebb04eb | 2019-05-08 10:26:24 +0200 | [diff] [blame] | 904 | return ret; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | static 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 | |
| 913 | static 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ä | 7295275 | 2016-11-18 21:52:40 +0200 | [diff] [blame] | 920 | const struct drm_framebuffer *fb = crtc->primary->fb; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 921 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 928 | 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ä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 943 | bppshift = mdev->bpp_shifts[fb->format->cpp[0] - 1]; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 944 | |
| 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 Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 960 | case G200_EW3: |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 961 | 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 Larouche | f0493e6 | 2016-10-21 12:47:07 -0400 | [diff] [blame] | 973 | case G200_EH3: |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 974 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 980 | break; |
| 981 | } |
| 982 | |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 983 | switch (fb->format->cpp[0] * 8) { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 984 | case 8: |
| 985 | dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits; |
| 986 | break; |
| 987 | case 16: |
Ville Syrjälä | b00c600 | 2016-12-14 23:31:35 +0200 | [diff] [blame] | 988 | if (fb->format->depth == 15) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 989 | 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 Harvey | 9d8aa55 | 2013-04-12 20:42:19 +0000 | [diff] [blame] | 1008 | if ((i <= 0x17) || |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1009 | (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 Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 1017 | if ((mdev->type == G200_EV || |
| 1018 | mdev->type == G200_WB || |
| 1019 | mdev->type == G200_EH || |
Mathieu Larouche | f0493e6 | 2016-10-21 12:47:07 -0400 | [diff] [blame] | 1020 | mdev->type == G200_EW3 || |
| 1021 | mdev->type == G200_EH3) && |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1022 | (i >= 0x44) && (i <= 0x4e)) |
| 1023 | continue; |
| 1024 | |
| 1025 | WREG_DAC(i, dacvalue[i]); |
| 1026 | } |
| 1027 | |
Christopher Harvey | 1812a3d | 2013-04-05 10:51:15 -0400 | [diff] [blame] | 1028 | if (mdev->type == G200_ER) |
| 1029 | WREG_DAC(0x90, 0); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1030 | |
| 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ä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1040 | pitch = fb->pitches[0] / fb->format->cpp[0]; |
| 1041 | if (fb->format->cpp[0] * 8 == 24) |
Takashi Iwai | da55839 | 2013-07-17 15:07:26 +0200 | [diff] [blame] | 1042 | pitch = (pitch * 3) >> (4 - bppshift); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1043 | 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ä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1118 | if (fb->format->cpp[0] * 8 == 24) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1119 | ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80; |
| 1120 | else |
| 1121 | ext_vga[3] = ((1 << bppshift) - 1) | 0x80; |
| 1122 | ext_vga[4] = 0; |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 1123 | if (mdev->type == G200_WB || mdev->type == G200_EW3) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1124 | ext_vga[1] |= 0x88; |
| 1125 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1126 | /* 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 Harvey | 1812a3d | 2013-04-05 10:51:15 -0400 | [diff] [blame] | 1137 | WREG_ECRT(0x24, 0x5); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1138 | |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 1139 | if (mdev->type == G200_EW3) |
| 1140 | WREG_ECRT(0x34, 0x5); |
| 1141 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1142 | 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 Larouche | 0cbb738 | 2017-06-14 10:39:42 -0400 | [diff] [blame] | 1176 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1180 | u8 hi_pri_lvl; |
| 1181 | u32 bpp; |
| 1182 | u32 mb; |
| 1183 | |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1184 | if (fb->format->cpp[0] * 8 > 16) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1185 | bpp = 32; |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1186 | else if (fb->format->cpp[0] * 8 > 8) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1187 | 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 Harvey | 91f8f10 | 2013-05-31 20:33:07 +0000 | [diff] [blame] | 1205 | WREG8(MGAREG_CRTCEXT_INDEX, 0x06); |
| 1206 | WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1207 | } else { |
Christopher Harvey | 91f8f10 | 2013-05-31 20:33:07 +0000 | [diff] [blame] | 1208 | WREG8(MGAREG_CRTCEXT_INDEX, 0x06); |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1209 | if (mdev->unique_rev_id >= 0x01) |
Christopher Harvey | 91f8f10 | 2013-05-31 20:33:07 +0000 | [diff] [blame] | 1210 | WREG8(MGAREG_CRTCEXT_DATA, 0x03); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1211 | else |
Christopher Harvey | 91f8f10 | 2013-05-31 20:33:07 +0000 | [diff] [blame] | 1212 | WREG8(MGAREG_CRTCEXT_DATA, 0x04); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */ |
| 1219 | static 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 | |
| 1251 | static 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 | |
| 1277 | static 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 | */ |
| 1331 | static 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 Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 1357 | if (mdev->type == G200_WB || mdev->type == G200_EW3) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1358 | 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 | */ |
| 1367 | static 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 Nikula | d584ff8 | 2015-03-11 11:51:00 +0200 | [diff] [blame] | 1371 | const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1372 | u8 tmp; |
| 1373 | |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 1374 | if (mdev->type == G200_WB || mdev->type == G200_EW3) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1375 | 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 Lankhorst | 7ea7728 | 2016-06-07 12:49:30 +0200 | [diff] [blame] | 1398 | static int mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green, |
Daniel Vetter | 6d124ff | 2017-04-03 10:33:01 +0200 | [diff] [blame] | 1399 | u16 *blue, uint32_t size, |
| 1400 | struct drm_modeset_acquire_ctx *ctx) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1401 | { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1402 | mga_crtc_load_lut(crtc); |
Maarten Lankhorst | 7ea7728 | 2016-06-07 12:49:30 +0200 | [diff] [blame] | 1403 | |
| 1404 | return 0; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | /* Simple cleanup function */ |
| 1408 | static 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 Eich | 64c2907 | 2013-07-17 15:07:22 +0200 | [diff] [blame] | 1416 | static void mga_crtc_disable(struct drm_crtc *crtc) |
| 1417 | { |
Egbert Eich | 64c2907 | 2013-07-17 15:07:22 +0200 | [diff] [blame] | 1418 | DRM_DEBUG_KMS("\n"); |
| 1419 | mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 1420 | if (crtc->primary->fb) { |
Thomas Zimmermann | 81da87f | 2019-05-21 13:08:29 +0200 | [diff] [blame] | 1421 | struct mga_device *mdev = crtc->dev->dev_private; |
Thomas Zimmermann | 5d17718 | 2019-06-27 10:09:09 +0200 | [diff] [blame] | 1422 | struct drm_framebuffer *fb = crtc->primary->fb; |
| 1423 | struct drm_gem_vram_object *gbo = |
| 1424 | drm_gem_vram_of_gem(fb->obj[0]); |
Thomas Zimmermann | ebb04eb | 2019-05-08 10:26:24 +0200 | [diff] [blame] | 1425 | |
Thomas Zimmermann | 81da87f | 2019-05-21 13:08:29 +0200 | [diff] [blame] | 1426 | /* unmap if console */ |
Thomas Zimmermann | 5d17718 | 2019-06-27 10:09:09 +0200 | [diff] [blame] | 1427 | if (mdev->mfbdev->helper.fb == fb) |
Thomas Zimmermann | 81da87f | 2019-05-21 13:08:29 +0200 | [diff] [blame] | 1428 | drm_gem_vram_kunmap(gbo); |
| 1429 | drm_gem_vram_unpin(gbo); |
Egbert Eich | 64c2907 | 2013-07-17 15:07:22 +0200 | [diff] [blame] | 1430 | } |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 1431 | crtc->primary->fb = NULL; |
Egbert Eich | 64c2907 | 2013-07-17 15:07:22 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1434 | /* These provide the minimum set of functions required to handle a CRTC */ |
| 1435 | static const struct drm_crtc_funcs mga_crtc_funcs = { |
Christopher Harvey | a080db9 | 2013-06-05 15:24:26 -0400 | [diff] [blame] | 1436 | .cursor_set = mga_crtc_cursor_set, |
| 1437 | .cursor_move = mga_crtc_cursor_move, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1438 | .gamma_set = mga_crtc_gamma_set, |
| 1439 | .set_config = drm_crtc_helper_set_config, |
| 1440 | .destroy = mga_crtc_destroy, |
| 1441 | }; |
| 1442 | |
| 1443 | static const struct drm_crtc_helper_funcs mga_helper_funcs = { |
Egbert Eich | 64c2907 | 2013-07-17 15:07:22 +0200 | [diff] [blame] | 1444 | .disable = mga_crtc_disable, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1445 | .dpms = mga_crtc_dpms, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1446 | .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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1450 | }; |
| 1451 | |
| 1452 | /* CRTC setup */ |
Christopher Harvey | f1998fe | 2013-02-20 09:34:22 -0500 | [diff] [blame] | 1453 | static void mga_crtc_init(struct mga_device *mdev) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1454 | { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1455 | struct mga_crtc *mga_crtc; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1456 | |
| 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 Harvey | f1998fe | 2013-02-20 09:34:22 -0500 | [diff] [blame] | 1464 | drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1465 | |
| 1466 | drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE); |
| 1467 | mdev->mode_info.crtc = mga_crtc; |
| 1468 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1469 | drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs); |
| 1470 | } |
| 1471 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1472 | /* |
| 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1484 | static 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 | |
| 1491 | static void mga_encoder_dpms(struct drm_encoder *encoder, int state) |
| 1492 | { |
| 1493 | return; |
| 1494 | } |
| 1495 | |
| 1496 | static void mga_encoder_prepare(struct drm_encoder *encoder) |
| 1497 | { |
| 1498 | } |
| 1499 | |
| 1500 | static void mga_encoder_commit(struct drm_encoder *encoder) |
| 1501 | { |
| 1502 | } |
| 1503 | |
Rashika | 080fd6b | 2014-01-06 20:37:15 +0530 | [diff] [blame] | 1504 | static void mga_encoder_destroy(struct drm_encoder *encoder) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1505 | { |
| 1506 | struct mga_encoder *mga_encoder = to_mga_encoder(encoder); |
| 1507 | drm_encoder_cleanup(encoder); |
| 1508 | kfree(mga_encoder); |
| 1509 | } |
| 1510 | |
| 1511 | static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = { |
| 1512 | .dpms = mga_encoder_dpms, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1513 | .mode_set = mga_encoder_mode_set, |
| 1514 | .prepare = mga_encoder_prepare, |
| 1515 | .commit = mga_encoder_commit, |
| 1516 | }; |
| 1517 | |
| 1518 | static const struct drm_encoder_funcs mga_encoder_encoder_funcs = { |
| 1519 | .destroy = mga_encoder_destroy, |
| 1520 | }; |
| 1521 | |
| 1522 | static 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ä | 13a3d91 | 2015-12-09 16:20:18 +0200 | [diff] [blame] | 1535 | DRM_MODE_ENCODER_DAC, NULL); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1536 | drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs); |
| 1537 | |
| 1538 | return encoder; |
| 1539 | } |
| 1540 | |
| 1541 | |
| 1542 | static 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 Vetter | c555f02 | 2018-07-09 10:40:06 +0200 | [diff] [blame] | 1550 | drm_connector_update_edid_property(connector, edid); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1551 | ret = drm_add_edid_modes(connector, edid); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1552 | kfree(edid); |
| 1553 | } |
| 1554 | return ret; |
| 1555 | } |
| 1556 | |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1557 | static 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 Pitre | c24ca5b | 2015-11-03 23:09:58 -0500 | [diff] [blame] | 1561 | uint64_t active_area, pixels_per_second, bandwidth; |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1562 | 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 Oostenryck | c69e52d | 2018-04-24 15:15:06 +0200 | [diff] [blame] | 1583 | static enum drm_mode_status mga_vga_mode_valid(struct drm_connector *connector, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1584 | struct drm_display_mode *mode) |
| 1585 | { |
Christopher Harvey | 0ba5317 | 2013-02-26 10:55:44 -0500 | [diff] [blame] | 1586 | struct drm_device *dev = connector->dev; |
| 1587 | struct mga_device *mdev = (struct mga_device*)dev->dev_private; |
Christopher Harvey | 0ba5317 | 2013-02-26 10:55:44 -0500 | [diff] [blame] | 1588 | int bpp = 32; |
Christopher Harvey | 0ba5317 | 2013-02-26 10:55:44 -0500 | [diff] [blame] | 1589 | |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1590 | 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 Larouche | e829d7e | 2015-08-21 09:24:13 -0400 | [diff] [blame] | 1599 | } else if (mdev->unique_rev_id == 0x02) { |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1600 | 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 Larouche | 0cbb738 | 2017-06-14 10:39:42 -0400 | [diff] [blame] | 1607 | } else { |
| 1608 | if (mga_vga_calculate_mode_bandwidth(mode, bpp) |
| 1609 | > (55000 * 1024)) |
| 1610 | return MODE_BANDWIDTH; |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1611 | } |
| 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 Carpenter | 9eb8d7a | 2018-01-25 17:26:55 +0300 | [diff] [blame] | 1617 | if (mga_vga_calculate_mode_bandwidth(mode, bpp) > |
| 1618 | (31877 * 1024)) |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1619 | 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 Airlie | ec22b4a | 2014-02-05 14:13:56 +1000 | [diff] [blame] | 1624 | } else if (mdev->type == G200_EH && |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1625 | (mga_vga_calculate_mode_bandwidth(mode, bpp) |
| 1626 | > (37500 * 1024))) { |
| 1627 | return MODE_BANDWIDTH; |
Dave Airlie | ec22b4a | 2014-02-05 14:13:56 +1000 | [diff] [blame] | 1628 | } else if (mdev->type == G200_ER && |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 1629 | (mga_vga_calculate_mode_bandwidth(mode, |
| 1630 | bpp) > (55000 * 1024))) { |
| 1631 | return MODE_BANDWIDTH; |
| 1632 | } |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1633 | |
Adam Jackson | 2516108 | 2015-06-15 16:16:15 -0400 | [diff] [blame] | 1634 | 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 Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1639 | 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 Harvey | 0ba5317 | 2013-02-26 10:55:44 -0500 | [diff] [blame] | 1646 | /* Validate the mode input by the user */ |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1647 | if (connector->cmdline_mode.specified) { |
| 1648 | if (connector->cmdline_mode.bpp_specified) |
| 1649 | bpp = connector->cmdline_mode.bpp; |
Christopher Harvey | 0ba5317 | 2013-02-26 10:55:44 -0500 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) { |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1653 | if (connector->cmdline_mode.specified) |
| 1654 | connector->cmdline_mode.specified = false; |
Christopher Harvey | 0ba5317 | 2013-02-26 10:55:44 -0500 | [diff] [blame] | 1655 | return MODE_BAD; |
| 1656 | } |
| 1657 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1658 | return MODE_OK; |
| 1659 | } |
| 1660 | |
Rashika | 080fd6b | 2014-01-06 20:37:15 +0530 | [diff] [blame] | 1661 | static struct drm_encoder *mga_connector_best_encoder(struct drm_connector |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1662 | *connector) |
| 1663 | { |
| 1664 | int enc_id = connector->encoder_ids[0]; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1665 | /* pick the encoder ids */ |
Rob Clark | c7e9511 | 2014-07-17 23:30:00 -0400 | [diff] [blame] | 1666 | if (enc_id) |
Keith Packard | 418da17 | 2017-03-14 23:25:07 -0700 | [diff] [blame] | 1667 | return drm_encoder_find(connector->dev, NULL, enc_id); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1668 | return NULL; |
| 1669 | } |
| 1670 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1671 | static 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ä | 71cb749 | 2015-12-15 12:21:10 +0100 | [diff] [blame] | 1679 | static const struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1680 | .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ä | 71cb749 | 2015-12-15 12:21:10 +0100 | [diff] [blame] | 1685 | static const struct drm_connector_funcs mga_vga_connector_funcs = { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1686 | .dpms = drm_helper_connector_dpms, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1687 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 1688 | .destroy = mga_connector_destroy, |
| 1689 | }; |
| 1690 | |
| 1691 | static 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 Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 1707 | drm_connector_register(connector); |
Egbert Eich | 3d5a1c5 | 2013-07-17 15:07:25 +0200 | [diff] [blame] | 1708 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1709 | 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 | |
| 1717 | int 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 Harvey | f1998fe | 2013-02-20 09:34:22 -0500 | [diff] [blame] | 1730 | mga_crtc_init(mdev); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1731 | |
| 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 Vetter | cde4c44 | 2018-07-09 10:40:07 +0200 | [diff] [blame] | 1744 | drm_connector_attach_encoder(connector, encoder); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1745 | |
| 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 | |
| 1755 | void mgag200_modeset_fini(struct mga_device *mdev) |
| 1756 | { |
| 1757 | |
| 1758 | } |