1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright 2010 Matt Turner. 4 * Copyright 2012 Red Hat 5 * 6 * Authors: Matthew Garrett 7 * Matt Turner 8 * Dave Airlie 9 */ 10 #ifndef __MGAG200_DRV_H__ 11 #define __MGAG200_DRV_H__ 12 13 #include <video/vga.h> 14 15 #include <drm/drm_connector.h> 16 #include <drm/drm_crtc.h> 17 #include <drm/drm_encoder.h> 18 #include <drm/drm_gem.h> 19 #include <drm/drm_gem_shmem_helper.h> 20 #include <drm/drm_panic_helper.h> 21 #include <drm/drm_plane.h> 22 23 #include "mgag200_reg.h" 24 25 #define DRIVER_AUTHOR "Matthew Garrett" 26 27 #define DRIVER_NAME "mgag200" 28 #define DRIVER_DESC "MGA G200 SE" 29 30 #define DRIVER_MAJOR 1 31 #define DRIVER_MINOR 0 32 #define DRIVER_PATCHLEVEL 0 33 34 #define RREG8(reg) ioread8(((void __iomem *)mdev->rmmio) + (reg)) 35 #define WREG8(reg, v) iowrite8(v, ((void __iomem *)mdev->rmmio) + (reg)) 36 #define RREG32(reg) ioread32(((void __iomem *)mdev->rmmio) + (reg)) 37 #define WREG32(reg, v) iowrite32(v, ((void __iomem *)mdev->rmmio) + (reg)) 38 39 #define MGA_BIOS_OFFSET 0x7ffc 40 41 #define ATTR_INDEX 0x1fc0 42 #define ATTR_DATA 0x1fc1 43 44 #define WREG_MISC(v) \ 45 WREG8(MGA_MISC_OUT, v) 46 47 #define RREG_MISC(v) \ 48 ((v) = RREG8(MGA_MISC_IN)) 49 50 #define WREG_MISC_MASKED(v, mask) \ 51 do { \ 52 u8 misc_; \ 53 u8 mask_ = (mask); \ 54 RREG_MISC(misc_); \ 55 misc_ &= ~mask_; \ 56 misc_ |= ((v) & mask_); \ 57 WREG_MISC(misc_); \ 58 } while (0) 59 60 #define WREG_ATTR(reg, v) \ 61 do { \ 62 RREG8(0x1fda); \ 63 WREG8(ATTR_INDEX, reg); \ 64 WREG8(ATTR_DATA, v); \ 65 } while (0) \ 66 67 #define RREG_SEQ(reg, v) \ 68 do { \ 69 WREG8(MGAREG_SEQ_INDEX, reg); \ 70 v = RREG8(MGAREG_SEQ_DATA); \ 71 } while (0) \ 72 73 #define WREG_SEQ(reg, v) \ 74 do { \ 75 WREG8(MGAREG_SEQ_INDEX, reg); \ 76 WREG8(MGAREG_SEQ_DATA, v); \ 77 } while (0) \ 78 79 #define RREG_CRT(reg, v) \ 80 do { \ 81 WREG8(MGAREG_CRTC_INDEX, reg); \ 82 v = RREG8(MGAREG_CRTC_DATA); \ 83 } while (0) \ 84 85 #define WREG_CRT(reg, v) \ 86 do { \ 87 WREG8(MGAREG_CRTC_INDEX, reg); \ 88 WREG8(MGAREG_CRTC_DATA, v); \ 89 } while (0) \ 90 91 #define RREG_ECRT(reg, v) \ 92 do { \ 93 WREG8(MGAREG_CRTCEXT_INDEX, reg); \ 94 v = RREG8(MGAREG_CRTCEXT_DATA); \ 95 } while (0) \ 96 97 #define WREG_ECRT(reg, v) \ 98 do { \ 99 WREG8(MGAREG_CRTCEXT_INDEX, reg); \ 100 WREG8(MGAREG_CRTCEXT_DATA, v); \ 101 } while (0) \ 102 103 #define GFX_INDEX 0x1fce 104 #define GFX_DATA 0x1fcf 105 106 #define WREG_GFX(reg, v) \ 107 do { \ 108 WREG8(GFX_INDEX, reg); \ 109 WREG8(GFX_DATA, v); \ 110 } while (0) \ 111 112 #define DAC_INDEX 0x3c00 113 #define DAC_DATA 0x3c0a 114 115 #define RREG_DAC(reg) \ 116 ({ \ 117 WREG8(DAC_INDEX, reg); \ 118 RREG8(DAC_DATA); \ 119 }) \ 120 121 #define WREG_DAC(reg, v) \ 122 do { \ 123 WREG8(DAC_INDEX, reg); \ 124 WREG8(DAC_DATA, v); \ 125 } while (0) \ 126 127 #define MGA_MISC_OUT 0x1fc2 128 #define MGA_MISC_IN 0x1fcc 129 130 /* 131 * TODO: This is a pretty large set of default values for all kinds of 132 * settings. It should be split and set in the various DRM helpers, 133 * such as the CRTC reset or atomic_enable helpers. The PLL values 134 * probably belong to each model's PLL code. 135 */ 136 #define MGAG200_DAC_DEFAULT(xvrefctrl, xpixclkctrl, xmiscctrl, xsyspllm, xsysplln, xsyspllp) \ 137 /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0, \ 138 /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0, \ 139 /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0, \ 140 /* 0x18: */ (xvrefctrl), \ 141 /* 0x19: */ 0, \ 142 /* 0x1a: */ (xpixclkctrl), \ 143 /* 0x1b: */ 0xff, 0xbf, 0x20, \ 144 /* 0x1e: */ (xmiscctrl), \ 145 /* 0x1f: */ 0x20, \ 146 /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 147 /* 0x28: */ 0x00, 0x00, 0x00, 0x00, \ 148 /* 0x2c: */ (xsyspllm), \ 149 /* 0x2d: */ (xsysplln), \ 150 /* 0x2e: */ (xsyspllp), \ 151 /* 0x2f: */ 0x40, \ 152 /* 0x30: */ 0x00, 0xb0, 0x00, 0xc2, 0x34, 0x14, 0x02, 0x83, \ 153 /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3a, \ 154 /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0, \ 155 /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0 \ 156 157 #define MGAG200_LUT_SIZE 256 158 159 #define MGAG200_MAX_FB_HEIGHT 4096 160 #define MGAG200_MAX_FB_WIDTH 4096 161 162 struct mga_device; 163 164 /* 165 * Stores parameters for programming the PLLs 166 * 167 * Fref: reference frequency (A: 25.175 Mhz, B: 28.361, C: XX Mhz) 168 * Fo: output frequency 169 * Fvco = Fref * (N / M) 170 * Fo = Fvco / P 171 * 172 * S = [0..3] 173 */ 174 struct mgag200_pll_values { 175 unsigned int m; 176 unsigned int n; 177 unsigned int p; 178 unsigned int s; 179 }; 180 181 struct mgag200_crtc_state { 182 struct drm_crtc_state base; 183 184 /* Primary-plane format; required for modesetting and color mgmt. */ 185 const struct drm_format_info *format; 186 187 struct mgag200_pll_values pixpllc; 188 189 bool set_vidrst; 190 }; 191 192 static inline struct mgag200_crtc_state *to_mgag200_crtc_state(struct drm_crtc_state *base) 193 { 194 return container_of(base, struct mgag200_crtc_state, base); 195 } 196 197 enum mga_type { 198 G200_PCI, 199 G200_AGP, 200 G200_SE_A, 201 G200_SE_B, 202 G200_WB, 203 G200_EV, 204 G200_EH, 205 G200_EH3, 206 G200_EH5, 207 G200_ER, 208 G200_EW3, 209 }; 210 211 struct mgag200_device_info { 212 u16 max_hdisplay; 213 u16 max_vdisplay; 214 215 /* 216 * Maximum memory bandwidth (MiB/sec). Setting this to zero disables 217 * the rsp test during mode validation. 218 */ 219 unsigned long max_mem_bandwidth; 220 221 /* Synchronize scanout with BMC */ 222 bool sync_bmc:1; 223 224 struct { 225 unsigned data_bit:3; 226 unsigned clock_bit:3; 227 } i2c; 228 229 /* 230 * HW does not handle 'startadd' register correctly. Always set 231 * it's value to 0. 232 */ 233 bool bug_no_startadd:1; 234 }; 235 236 #define MGAG200_DEVICE_INFO_INIT(_max_hdisplay, _max_vdisplay, _max_mem_bandwidth, \ 237 _sync_bmc, _i2c_data_bit, _i2c_clock_bit, \ 238 _bug_no_startadd) \ 239 { \ 240 .max_hdisplay = (_max_hdisplay), \ 241 .max_vdisplay = (_max_vdisplay), \ 242 .max_mem_bandwidth = (_max_mem_bandwidth), \ 243 .sync_bmc = (_sync_bmc), \ 244 .i2c = { \ 245 .data_bit = (_i2c_data_bit), \ 246 .clock_bit = (_i2c_clock_bit), \ 247 }, \ 248 .bug_no_startadd = (_bug_no_startadd), \ 249 } 250 251 struct mgag200_device_funcs { 252 /* 253 * Validate that the given state can be programmed into PIXPLLC. On 254 * success, the calculated parameters should be stored in the CRTC's 255 * state in struct @mgag200_crtc_state.pixpllc. 256 */ 257 int (*pixpllc_atomic_check)(struct drm_crtc *crtc, struct drm_atomic_commit *new_state); 258 259 /* 260 * Program PIXPLLC from the CRTC state. The parameters should have been 261 * stored in struct @mgag200_crtc_state.pixpllc by the corresponding 262 * implementation of @pixpllc_atomic_check. 263 */ 264 void (*pixpllc_atomic_update)(struct drm_crtc *crtc, struct drm_atomic_commit *old_state); 265 }; 266 267 struct mga_device { 268 struct drm_device base; 269 270 const struct mgag200_device_info *info; 271 const struct mgag200_device_funcs *funcs; 272 273 struct resource *rmmio_res; 274 void __iomem *rmmio; 275 struct mutex rmmio_lock; /* Protects access to rmmio */ 276 277 struct resource *vram_res; 278 void __iomem *vram; 279 resource_size_t vram_available; 280 281 struct drm_plane primary_plane; 282 struct drm_crtc crtc; 283 struct { 284 struct { 285 struct drm_encoder encoder; 286 struct drm_connector connector; 287 } vga; 288 } output; 289 }; 290 291 static inline struct mga_device *to_mga_device(struct drm_device *dev) 292 { 293 return container_of(dev, struct mga_device, base); 294 } 295 296 struct mgag200_g200_device { 297 struct mga_device base; 298 299 /* PLL constants */ 300 long ref_clk; 301 long pclk_min; 302 long pclk_max; 303 }; 304 305 static inline struct mgag200_g200_device *to_mgag200_g200_device(struct drm_device *dev) 306 { 307 return container_of(to_mga_device(dev), struct mgag200_g200_device, base); 308 } 309 310 struct mgag200_g200se_device { 311 struct mga_device base; 312 313 /* SE model number stored in reg 0x1e24 */ 314 u32 unique_rev_id; 315 }; 316 317 static inline struct mgag200_g200se_device *to_mgag200_g200se_device(struct drm_device *dev) 318 { 319 return container_of(to_mga_device(dev), struct mgag200_g200se_device, base); 320 } 321 322 /* mgag200_drv.c */ 323 int mgag200_init_pci_options(struct pci_dev *pdev, u32 option, u32 option2); 324 resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size); 325 resource_size_t mgag200_device_probe_vram(struct mga_device *mdev); 326 int mgag200_device_preinit(struct mga_device *mdev); 327 int mgag200_device_init(struct mga_device *mdev, 328 const struct mgag200_device_info *info, 329 const struct mgag200_device_funcs *funcs); 330 331 /* mgag200_<device type>.c */ 332 struct mga_device *mgag200_g200_device_create(struct pci_dev *pdev, const struct drm_driver *drv); 333 struct mga_device *mgag200_g200se_device_create(struct pci_dev *pdev, const struct drm_driver *drv, 334 enum mga_type type); 335 void mgag200_g200wb_init_registers(struct mga_device *mdev); 336 void mgag200_g200wb_pixpllc_atomic_update(struct drm_crtc *crtc, struct drm_atomic_commit *old_state); 337 struct mga_device *mgag200_g200wb_device_create(struct pci_dev *pdev, const struct drm_driver *drv); 338 struct mga_device *mgag200_g200ev_device_create(struct pci_dev *pdev, const struct drm_driver *drv); 339 void mgag200_g200eh_init_registers(struct mga_device *mdev); 340 void mgag200_g200eh_pixpllc_atomic_update(struct drm_crtc *crtc, struct drm_atomic_commit *old_state); 341 struct mga_device *mgag200_g200eh_device_create(struct pci_dev *pdev, 342 const struct drm_driver *drv); 343 struct mga_device *mgag200_g200eh3_device_create(struct pci_dev *pdev, 344 const struct drm_driver *drv); 345 struct mga_device *mgag200_g200eh5_device_create(struct pci_dev *pdev, 346 const struct drm_driver *drv); 347 struct mga_device *mgag200_g200er_device_create(struct pci_dev *pdev, 348 const struct drm_driver *drv); 349 struct mga_device *mgag200_g200ew3_device_create(struct pci_dev *pdev, 350 const struct drm_driver *drv); 351 352 /* 353 * mgag200_mode.c 354 */ 355 356 struct drm_crtc; 357 struct drm_crtc_state; 358 struct drm_display_mode; 359 struct drm_plane; 360 struct drm_atomic_commit; 361 struct drm_scanout_buffer; 362 363 extern const uint32_t mgag200_primary_plane_formats[]; 364 extern const size_t mgag200_primary_plane_formats_size; 365 extern const uint64_t mgag200_primary_plane_fmtmods[]; 366 367 int mgag200_primary_plane_helper_atomic_check(struct drm_plane *plane, 368 struct drm_atomic_commit *new_state); 369 void mgag200_primary_plane_helper_atomic_update(struct drm_plane *plane, 370 struct drm_atomic_commit *old_state); 371 void mgag200_primary_plane_helper_atomic_enable(struct drm_plane *plane, 372 struct drm_atomic_commit *state); 373 void mgag200_primary_plane_helper_atomic_disable(struct drm_plane *plane, 374 struct drm_atomic_commit *old_state); 375 int mgag200_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane, 376 struct drm_scanout_buffer *sb); 377 378 #define MGAG200_PRIMARY_PLANE_HELPER_FUNCS \ 379 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, \ 380 .atomic_check = mgag200_primary_plane_helper_atomic_check, \ 381 .atomic_update = mgag200_primary_plane_helper_atomic_update, \ 382 .atomic_enable = mgag200_primary_plane_helper_atomic_enable, \ 383 .atomic_disable = mgag200_primary_plane_helper_atomic_disable, \ 384 .get_scanout_buffer = mgag200_primary_plane_helper_get_scanout_buffer 385 386 #define MGAG200_PRIMARY_PLANE_FUNCS \ 387 .update_plane = drm_atomic_helper_update_plane, \ 388 .disable_plane = drm_atomic_helper_disable_plane, \ 389 .destroy = drm_plane_cleanup, \ 390 DRM_GEM_SHADOW_PLANE_FUNCS, \ 391 DRM_PANIC_PLANE_FUNCS 392 393 void mgag200_crtc_fill_gamma(struct mga_device *mdev, const struct drm_format_info *format); 394 void mgag200_crtc_load_gamma(struct mga_device *mdev, 395 const struct drm_format_info *format, 396 struct drm_color_lut *lut); 397 398 enum drm_mode_status mgag200_crtc_helper_mode_valid(struct drm_crtc *crtc, 399 const struct drm_display_mode *mode); 400 int mgag200_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_commit *new_state); 401 void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_commit *old_state); 402 void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_commit *old_state); 403 void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_commit *old_state); 404 405 #define MGAG200_CRTC_HELPER_FUNCS \ 406 .mode_valid = mgag200_crtc_helper_mode_valid, \ 407 .atomic_check = mgag200_crtc_helper_atomic_check, \ 408 .atomic_flush = mgag200_crtc_helper_atomic_flush, \ 409 .atomic_enable = mgag200_crtc_helper_atomic_enable, \ 410 .atomic_disable = mgag200_crtc_helper_atomic_disable 411 412 struct drm_crtc_state *mgag200_crtc_atomic_create_state(struct drm_crtc *crtc); 413 struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc); 414 void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *crtc_state); 415 416 #define MGAG200_CRTC_FUNCS \ 417 .destroy = drm_crtc_cleanup, \ 418 .set_config = drm_atomic_helper_set_config, \ 419 .page_flip = drm_atomic_helper_page_flip, \ 420 .atomic_create_state = mgag200_crtc_atomic_create_state, \ 421 .atomic_duplicate_state = mgag200_crtc_atomic_duplicate_state, \ 422 .atomic_destroy_state = mgag200_crtc_atomic_destroy_state 423 424 void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode, 425 bool set_vidrst); 426 void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format); 427 void mgag200_enable_display(struct mga_device *mdev); 428 void mgag200_init_registers(struct mga_device *mdev); 429 int mgag200_mode_config_init(struct mga_device *mdev, resource_size_t vram_available); 430 431 /* mgag200_vga_bmc.c */ 432 int mgag200_vga_bmc_output_init(struct mga_device *mdev); 433 434 /* mgag200_vga.c */ 435 int mgag200_vga_output_init(struct mga_device *mdev); 436 437 /* mgag200_bmc.c */ 438 void mgag200_bmc_stop_scanout(struct mga_device *mdev); 439 void mgag200_bmc_start_scanout(struct mga_device *mdev); 440 441 #endif /* __MGAG200_DRV_H__ */ 442