2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: Dave Airlie
27 #include <drm/drm_crtc_helper.h>
28 #include <drm/radeon_drm.h>
29 #include "radeon_fixed.h"
33 static void radeon_legacy_rmx_mode_set(struct drm_crtc *crtc,
34 struct drm_display_mode *mode,
35 struct drm_display_mode *adjusted_mode)
37 struct drm_device *dev = crtc->dev;
38 struct radeon_device *rdev = dev->dev_private;
39 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
40 int xres = mode->hdisplay;
41 int yres = mode->vdisplay;
42 bool hscale = true, vscale = true;
47 u32 scale, inc, crtc_more_cntl;
48 u32 fp_horz_stretch, fp_vert_stretch, fp_horz_vert_active;
49 u32 fp_h_sync_strt_wid, fp_crtc_h_total_disp;
50 u32 fp_v_sync_strt_wid, fp_crtc_v_total_disp;
51 struct drm_display_mode *native_mode = &radeon_crtc->native_mode;
53 fp_vert_stretch = RREG32(RADEON_FP_VERT_STRETCH) &
54 (RADEON_VERT_STRETCH_RESERVED |
55 RADEON_VERT_AUTO_RATIO_INC);
56 fp_horz_stretch = RREG32(RADEON_FP_HORZ_STRETCH) &
57 (RADEON_HORZ_FP_LOOP_STRETCH |
58 RADEON_HORZ_AUTO_RATIO_INC);
61 if ((rdev->family == CHIP_RS100) ||
62 (rdev->family == CHIP_RS200)) {
63 /* This is to workaround the asic bug for RMX, some versions
64 of BIOS dosen't have this register initialized correctly. */
65 crtc_more_cntl |= RADEON_CRTC_H_CUTOFF_ACTIVE_EN;
69 fp_crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
70 | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
72 hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
75 hsync_start = mode->crtc_hsync_start - 8;
77 fp_h_sync_strt_wid = ((hsync_start & 0x1fff)
78 | ((hsync_wid & 0x3f) << 16)
79 | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
80 ? RADEON_CRTC_H_SYNC_POL
83 fp_crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
84 | ((mode->crtc_vdisplay - 1) << 16));
86 vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
90 fp_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
91 | ((vsync_wid & 0x1f) << 16)
92 | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
93 ? RADEON_CRTC_V_SYNC_POL
96 fp_horz_vert_active = 0;
98 if (native_mode->hdisplay == 0 ||
99 native_mode->vdisplay == 0) {
103 if (xres > native_mode->hdisplay)
104 xres = native_mode->hdisplay;
105 if (yres > native_mode->vdisplay)
106 yres = native_mode->vdisplay;
108 if (xres == native_mode->hdisplay)
110 if (yres == native_mode->vdisplay)
114 switch (radeon_crtc->rmx_type) {
118 fp_horz_stretch |= ((xres/8-1) << 16);
120 inc = (fp_horz_stretch & RADEON_HORZ_AUTO_RATIO_INC) ? 1 : 0;
121 scale = ((xres + inc) * RADEON_HORZ_STRETCH_RATIO_MAX)
122 / native_mode->hdisplay + 1;
123 fp_horz_stretch |= (((scale) & RADEON_HORZ_STRETCH_RATIO_MASK) |
124 RADEON_HORZ_STRETCH_BLEND |
125 RADEON_HORZ_STRETCH_ENABLE |
126 ((native_mode->hdisplay/8-1) << 16));
130 fp_vert_stretch |= ((yres-1) << 12);
132 inc = (fp_vert_stretch & RADEON_VERT_AUTO_RATIO_INC) ? 1 : 0;
133 scale = ((yres + inc) * RADEON_VERT_STRETCH_RATIO_MAX)
134 / native_mode->vdisplay + 1;
135 fp_vert_stretch |= (((scale) & RADEON_VERT_STRETCH_RATIO_MASK) |
136 RADEON_VERT_STRETCH_ENABLE |
137 RADEON_VERT_STRETCH_BLEND |
138 ((native_mode->vdisplay-1) << 12));
142 fp_horz_stretch |= ((xres/8-1) << 16);
143 fp_vert_stretch |= ((yres-1) << 12);
145 crtc_more_cntl |= (RADEON_CRTC_AUTO_HORZ_CENTER_EN |
146 RADEON_CRTC_AUTO_VERT_CENTER_EN);
148 blank_width = (mode->crtc_hblank_end - mode->crtc_hblank_start) / 8;
149 if (blank_width > 110)
152 fp_crtc_h_total_disp = (((blank_width) & 0x3ff)
153 | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
155 hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
159 fp_h_sync_strt_wid = ((((mode->crtc_hsync_start - mode->crtc_hblank_start) / 8) & 0x1fff)
160 | ((hsync_wid & 0x3f) << 16)
161 | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
162 ? RADEON_CRTC_H_SYNC_POL
165 fp_crtc_v_total_disp = (((mode->crtc_vblank_end - mode->crtc_vblank_start) & 0xffff)
166 | ((mode->crtc_vdisplay - 1) << 16));
168 vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
172 fp_v_sync_strt_wid = ((((mode->crtc_vsync_start - mode->crtc_vblank_start) & 0xfff)
173 | ((vsync_wid & 0x1f) << 16)
174 | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
175 ? RADEON_CRTC_V_SYNC_POL
178 fp_horz_vert_active = (((native_mode->vdisplay) & 0xfff) |
179 (((native_mode->hdisplay / 8) & 0x1ff) << 16));
183 fp_horz_stretch |= ((xres/8-1) << 16);
184 fp_vert_stretch |= ((yres-1) << 12);
188 WREG32(RADEON_FP_HORZ_STRETCH, fp_horz_stretch);
189 WREG32(RADEON_FP_VERT_STRETCH, fp_vert_stretch);
190 WREG32(RADEON_CRTC_MORE_CNTL, crtc_more_cntl);
191 WREG32(RADEON_FP_HORZ_VERT_ACTIVE, fp_horz_vert_active);
192 WREG32(RADEON_FP_H_SYNC_STRT_WID, fp_h_sync_strt_wid);
193 WREG32(RADEON_FP_V_SYNC_STRT_WID, fp_v_sync_strt_wid);
194 WREG32(RADEON_FP_CRTC_H_TOTAL_DISP, fp_crtc_h_total_disp);
195 WREG32(RADEON_FP_CRTC_V_TOTAL_DISP, fp_crtc_v_total_disp);
198 void radeon_restore_common_regs(struct drm_device *dev)
200 /* don't need this yet */
203 static void radeon_pll_wait_for_read_update_complete(struct drm_device *dev)
205 struct radeon_device *rdev = dev->dev_private;
208 /* FIXME: Certain revisions of R300 can't recover here. Not sure of
209 the cause yet, but this workaround will mask the problem for now.
210 Other chips usually will pass at the very first test, so the
211 workaround shouldn't have any effect on them. */
214 RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
218 static void radeon_pll_write_update(struct drm_device *dev)
220 struct radeon_device *rdev = dev->dev_private;
222 while (RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
224 WREG32_PLL_P(RADEON_PPLL_REF_DIV,
225 RADEON_PPLL_ATOMIC_UPDATE_W,
226 ~(RADEON_PPLL_ATOMIC_UPDATE_W));
229 static void radeon_pll2_wait_for_read_update_complete(struct drm_device *dev)
231 struct radeon_device *rdev = dev->dev_private;
235 /* FIXME: Certain revisions of R300 can't recover here. Not sure of
236 the cause yet, but this workaround will mask the problem for now.
237 Other chips usually will pass at the very first test, so the
238 workaround shouldn't have any effect on them. */
241 RREG32_PLL(RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
245 static void radeon_pll2_write_update(struct drm_device *dev)
247 struct radeon_device *rdev = dev->dev_private;
249 while (RREG32_PLL(RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
251 WREG32_PLL_P(RADEON_P2PLL_REF_DIV,
252 RADEON_P2PLL_ATOMIC_UPDATE_W,
253 ~(RADEON_P2PLL_ATOMIC_UPDATE_W));
256 static uint8_t radeon_compute_pll_gain(uint16_t ref_freq, uint16_t ref_div,
259 unsigned int vcoFreq;
264 vcoFreq = ((unsigned)ref_freq & fb_div) / ref_div;
267 * This is horribly crude: the VCO frequency range is divided into
268 * 3 parts, each part having a fixed PLL gain value.
270 if (vcoFreq >= 30000)
275 else if (vcoFreq >= 18000)
287 void radeon_crtc_dpms(struct drm_crtc *crtc, int mode)
289 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
290 struct drm_device *dev = crtc->dev;
291 struct radeon_device *rdev = dev->dev_private;
294 if (radeon_crtc->crtc_id)
295 mask = (RADEON_CRTC2_EN |
296 RADEON_CRTC2_DISP_DIS |
297 RADEON_CRTC2_VSYNC_DIS |
298 RADEON_CRTC2_HSYNC_DIS |
299 RADEON_CRTC2_DISP_REQ_EN_B);
301 mask = (RADEON_CRTC_DISPLAY_DIS |
302 RADEON_CRTC_VSYNC_DIS |
303 RADEON_CRTC_HSYNC_DIS);
306 case DRM_MODE_DPMS_ON:
307 if (radeon_crtc->crtc_id)
308 WREG32_P(RADEON_CRTC2_GEN_CNTL, RADEON_CRTC2_EN, ~mask);
310 WREG32_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_EN, ~(RADEON_CRTC_EN |
311 RADEON_CRTC_DISP_REQ_EN_B));
312 WREG32_P(RADEON_CRTC_EXT_CNTL, 0, ~mask);
314 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
315 radeon_crtc_load_lut(crtc);
317 case DRM_MODE_DPMS_STANDBY:
318 case DRM_MODE_DPMS_SUSPEND:
319 case DRM_MODE_DPMS_OFF:
320 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
321 if (radeon_crtc->crtc_id)
322 WREG32_P(RADEON_CRTC2_GEN_CNTL, mask, ~mask);
324 WREG32_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_DISP_REQ_EN_B, ~(RADEON_CRTC_EN |
325 RADEON_CRTC_DISP_REQ_EN_B));
326 WREG32_P(RADEON_CRTC_EXT_CNTL, mask, ~mask);
332 /* properly set crtc bpp when using atombios */
333 void radeon_legacy_atom_set_surface(struct drm_crtc *crtc)
335 struct drm_device *dev = crtc->dev;
336 struct radeon_device *rdev = dev->dev_private;
337 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
339 uint32_t crtc_gen_cntl;
340 uint32_t disp_merge_cntl;
343 switch (crtc->fb->bits_per_pixel) {
363 crtc_pitch = ((((crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8)) * crtc->fb->bits_per_pixel) +
364 ((crtc->fb->bits_per_pixel * 8) - 1)) /
365 (crtc->fb->bits_per_pixel * 8));
366 crtc_pitch |= crtc_pitch << 16;
368 WREG32(RADEON_CRTC_PITCH + radeon_crtc->crtc_offset, crtc_pitch);
370 switch (radeon_crtc->crtc_id) {
372 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
373 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
374 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
376 crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL) & 0xfffff0ff;
377 crtc_gen_cntl |= (format << 8);
378 crtc_gen_cntl |= RADEON_CRTC_EXT_DISP_EN;
379 WREG32(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);
382 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
383 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
384 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
386 crtc_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL) & 0xfffff0ff;
387 crtc_gen_cntl |= (format << 8);
388 WREG32(RADEON_CRTC2_GEN_CNTL, crtc_gen_cntl);
389 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
390 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
395 int radeon_crtc_set_base(struct drm_crtc *crtc, int x, int y,
396 struct drm_framebuffer *old_fb)
398 struct drm_device *dev = crtc->dev;
399 struct radeon_device *rdev = dev->dev_private;
400 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
401 struct radeon_framebuffer *radeon_fb;
402 struct drm_gem_object *obj;
404 uint32_t crtc_offset, crtc_offset_cntl, crtc_tile_x0_y0 = 0;
405 uint32_t crtc_pitch, pitch_pixels;
406 uint32_t tiling_flags;
408 uint32_t gen_cntl_reg, gen_cntl_val;
413 DRM_DEBUG("No FB bound\n");
417 radeon_fb = to_radeon_framebuffer(crtc->fb);
419 switch (crtc->fb->bits_per_pixel) {
439 obj = radeon_fb->obj;
440 if (radeon_gem_object_pin(obj, RADEON_GEM_DOMAIN_VRAM, &base)) {
443 /* if scanout was in GTT this really wouldn't work */
444 /* crtc offset is from display base addr not FB location */
445 radeon_crtc->legacy_display_base_addr = rdev->mc.vram_location;
447 base -= radeon_crtc->legacy_display_base_addr;
449 crtc_offset_cntl = 0;
451 pitch_pixels = crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8);
452 crtc_pitch = (((pitch_pixels * crtc->fb->bits_per_pixel) +
453 ((crtc->fb->bits_per_pixel * 8) - 1)) /
454 (crtc->fb->bits_per_pixel * 8));
455 crtc_pitch |= crtc_pitch << 16;
457 radeon_object_get_tiling_flags(obj->driver_private,
458 &tiling_flags, NULL);
459 if (tiling_flags & RADEON_TILING_MICRO)
460 DRM_ERROR("trying to scanout microtiled buffer\n");
462 if (tiling_flags & RADEON_TILING_MACRO) {
463 if (ASIC_IS_R300(rdev))
464 crtc_offset_cntl |= (R300_CRTC_X_Y_MODE_EN |
465 R300_CRTC_MICRO_TILE_BUFFER_DIS |
466 R300_CRTC_MACRO_TILE_EN);
468 crtc_offset_cntl |= RADEON_CRTC_TILE_EN;
470 if (ASIC_IS_R300(rdev))
471 crtc_offset_cntl &= ~(R300_CRTC_X_Y_MODE_EN |
472 R300_CRTC_MICRO_TILE_BUFFER_DIS |
473 R300_CRTC_MACRO_TILE_EN);
475 crtc_offset_cntl &= ~RADEON_CRTC_TILE_EN;
478 if (tiling_flags & RADEON_TILING_MACRO) {
479 if (ASIC_IS_R300(rdev)) {
480 crtc_tile_x0_y0 = x | (y << 16);
483 int byteshift = crtc->fb->bits_per_pixel >> 4;
484 int tile_addr = (((y >> 3) * pitch_pixels + x) >> (8 - byteshift)) << 11;
485 base += tile_addr + ((x << byteshift) % 256) + ((y % 8) << 8);
486 crtc_offset_cntl |= (y % 16);
489 int offset = y * pitch_pixels + x;
490 switch (crtc->fb->bits_per_pixel) {
512 if (radeon_crtc->crtc_id == 1)
513 gen_cntl_reg = RADEON_CRTC2_GEN_CNTL;
515 gen_cntl_reg = RADEON_CRTC_GEN_CNTL;
517 gen_cntl_val = RREG32(gen_cntl_reg);
518 gen_cntl_val &= ~(0xf << 8);
519 gen_cntl_val |= (format << 8);
520 WREG32(gen_cntl_reg, gen_cntl_val);
522 crtc_offset = (u32)base;
524 WREG32(RADEON_DISPLAY_BASE_ADDR + radeon_crtc->crtc_offset, radeon_crtc->legacy_display_base_addr);
526 if (ASIC_IS_R300(rdev)) {
527 if (radeon_crtc->crtc_id)
528 WREG32(R300_CRTC2_TILE_X0_Y0, crtc_tile_x0_y0);
530 WREG32(R300_CRTC_TILE_X0_Y0, crtc_tile_x0_y0);
532 WREG32(RADEON_CRTC_OFFSET_CNTL + radeon_crtc->crtc_offset, crtc_offset_cntl);
533 WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, crtc_offset);
534 WREG32(RADEON_CRTC_PITCH + radeon_crtc->crtc_offset, crtc_pitch);
536 if (old_fb && old_fb != crtc->fb) {
537 radeon_fb = to_radeon_framebuffer(old_fb);
538 radeon_gem_object_unpin(radeon_fb->obj);
541 /* Bytes per pixel may have changed */
542 radeon_bandwidth_update(rdev);
547 static bool radeon_set_crtc_timing(struct drm_crtc *crtc, struct drm_display_mode *mode)
549 struct drm_device *dev = crtc->dev;
550 struct radeon_device *rdev = dev->dev_private;
551 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
552 struct drm_encoder *encoder;
557 uint32_t crtc_h_total_disp;
558 uint32_t crtc_h_sync_strt_wid;
559 uint32_t crtc_v_total_disp;
560 uint32_t crtc_v_sync_strt_wid;
564 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
565 if (encoder->crtc == crtc) {
566 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
567 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
569 DRM_INFO("crtc %d is connected to a TV\n", radeon_crtc->crtc_id);
575 switch (crtc->fb->bits_per_pixel) {
595 crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
596 | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
598 hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
601 hsync_start = mode->crtc_hsync_start - 8;
603 crtc_h_sync_strt_wid = ((hsync_start & 0x1fff)
604 | ((hsync_wid & 0x3f) << 16)
605 | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
606 ? RADEON_CRTC_H_SYNC_POL
609 /* This works for double scan mode. */
610 crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
611 | ((mode->crtc_vdisplay - 1) << 16));
613 vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
617 crtc_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
618 | ((vsync_wid & 0x1f) << 16)
619 | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
620 ? RADEON_CRTC_V_SYNC_POL
623 /* TODO -> Dell Server */
625 uint32_t disp_hw_debug = RREG32(RADEON_DISP_HW_DEBUG);
626 uint32_t tv_dac_cntl = RREG32(RADEON_TV_DAC_CNTL);
627 uint32_t dac2_cntl = RREG32(RADEON_DAC_CNTL2);
628 uint32_t crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL);
630 dac2_cntl &= ~RADEON_DAC2_DAC_CLK_SEL;
631 dac2_cntl |= RADEON_DAC2_DAC2_CLK_SEL;
633 /* For CRT on DAC2, don't turn it on if BIOS didn't
634 enable it, even it's detected.
636 disp_hw_debug |= RADEON_CRT2_DISP1_SEL;
637 tv_dac_cntl &= ~((1<<2) | (3<<8) | (7<<24) | (0xff<<16));
638 tv_dac_cntl |= (0x03 | (2<<8) | (0x58<<16));
640 WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
641 WREG32(RADEON_DISP_HW_DEBUG, disp_hw_debug);
642 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
643 WREG32(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
646 if (radeon_crtc->crtc_id) {
647 uint32_t crtc2_gen_cntl;
648 uint32_t disp2_merge_cntl;
650 /* check to see if TV DAC is enabled for another crtc and keep it enabled */
651 if (RREG32(RADEON_CRTC2_GEN_CNTL) & RADEON_CRTC2_CRT2_ON)
652 crtc2_gen_cntl = RADEON_CRTC2_CRT2_ON;
656 crtc2_gen_cntl |= ((format << 8)
657 | RADEON_CRTC2_VSYNC_DIS
658 | RADEON_CRTC2_HSYNC_DIS
659 | RADEON_CRTC2_DISP_DIS
660 | RADEON_CRTC2_DISP_REQ_EN_B
661 | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
662 ? RADEON_CRTC2_DBL_SCAN_EN
664 | ((mode->flags & DRM_MODE_FLAG_CSYNC)
665 ? RADEON_CRTC2_CSYNC_EN
667 | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
668 ? RADEON_CRTC2_INTERLACE_EN
671 disp2_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
672 disp2_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
674 WREG32(RADEON_DISP2_MERGE_CNTL, disp2_merge_cntl);
675 WREG32(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
677 WREG32(RADEON_FP_H2_SYNC_STRT_WID, crtc_h_sync_strt_wid);
678 WREG32(RADEON_FP_V2_SYNC_STRT_WID, crtc_v_sync_strt_wid);
680 uint32_t crtc_gen_cntl;
681 uint32_t crtc_ext_cntl;
682 uint32_t disp_merge_cntl;
684 crtc_gen_cntl = (RADEON_CRTC_EXT_DISP_EN
686 | RADEON_CRTC_DISP_REQ_EN_B
687 | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
688 ? RADEON_CRTC_DBL_SCAN_EN
690 | ((mode->flags & DRM_MODE_FLAG_CSYNC)
691 ? RADEON_CRTC_CSYNC_EN
693 | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
694 ? RADEON_CRTC_INTERLACE_EN
697 crtc_ext_cntl = RREG32(RADEON_CRTC_EXT_CNTL);
698 crtc_ext_cntl |= (RADEON_XCRT_CNT_EN |
699 RADEON_CRTC_VSYNC_DIS |
700 RADEON_CRTC_HSYNC_DIS |
701 RADEON_CRTC_DISPLAY_DIS);
703 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
704 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
706 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
707 WREG32(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);
708 WREG32(RADEON_CRTC_EXT_CNTL, crtc_ext_cntl);
712 radeon_legacy_tv_adjust_crtc_reg(encoder, &crtc_h_total_disp,
713 &crtc_h_sync_strt_wid, &crtc_v_total_disp,
714 &crtc_v_sync_strt_wid);
716 WREG32(RADEON_CRTC_H_TOTAL_DISP + radeon_crtc->crtc_offset, crtc_h_total_disp);
717 WREG32(RADEON_CRTC_H_SYNC_STRT_WID + radeon_crtc->crtc_offset, crtc_h_sync_strt_wid);
718 WREG32(RADEON_CRTC_V_TOTAL_DISP + radeon_crtc->crtc_offset, crtc_v_total_disp);
719 WREG32(RADEON_CRTC_V_SYNC_STRT_WID + radeon_crtc->crtc_offset, crtc_v_sync_strt_wid);
724 static void radeon_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
726 struct drm_device *dev = crtc->dev;
727 struct radeon_device *rdev = dev->dev_private;
728 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
729 struct drm_encoder *encoder;
730 uint32_t feedback_div = 0;
731 uint32_t frac_fb_div = 0;
732 uint32_t reference_div = 0;
733 uint32_t post_divider = 0;
736 int pll_flags = RADEON_PLL_LEGACY;
737 bool use_bios_divs = false;
739 uint32_t pll_ref_div = 0;
740 uint32_t pll_fb_post_div = 0;
741 uint32_t htotal_cntl = 0;
743 struct radeon_pll *pll;
748 } *post_div, post_divs[] = {
749 /* From RAGE 128 VR/RAGE 128 GL Register
750 * Reference Manual (Technical Reference
751 * Manual P/N RRG-G04100-C Rev. 0.04), page
752 * 3-17 (PLL_DIV_[3:0]).
754 { 1, 0 }, /* VCLK_SRC */
755 { 2, 1 }, /* VCLK_SRC/2 */
756 { 4, 2 }, /* VCLK_SRC/4 */
757 { 8, 3 }, /* VCLK_SRC/8 */
758 { 3, 4 }, /* VCLK_SRC/3 */
759 { 16, 5 }, /* VCLK_SRC/16 */
760 { 6, 6 }, /* VCLK_SRC/6 */
761 { 12, 7 }, /* VCLK_SRC/12 */
765 if (radeon_crtc->crtc_id)
766 pll = &rdev->clock.p2pll;
768 pll = &rdev->clock.p1pll;
770 if (mode->clock > 200000) /* range limits??? */
771 pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
773 pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
775 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
776 if (encoder->crtc == crtc) {
777 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
779 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
784 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
785 pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
786 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS) {
787 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
788 struct radeon_encoder_lvds *lvds = (struct radeon_encoder_lvds *)radeon_encoder->enc_priv;
790 if (lvds->use_bios_dividers) {
791 pll_ref_div = lvds->panel_ref_divider;
792 pll_fb_post_div = (lvds->panel_fb_divider |
793 (lvds->panel_post_divider << 16));
795 use_bios_divs = true;
798 pll_flags |= RADEON_PLL_USE_REF_DIV;
805 if (!use_bios_divs) {
806 radeon_compute_pll(pll, mode->clock,
807 &freq, &feedback_div, &frac_fb_div,
808 &reference_div, &post_divider,
811 for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
812 if (post_div->divider == post_divider)
816 if (!post_div->divider)
817 post_div = &post_divs[0];
819 DRM_DEBUG("dc=%u, fd=%d, rd=%d, pd=%d\n",
825 pll_ref_div = reference_div;
826 #if defined(__powerpc__) && (0) /* TODO */
827 /* apparently programming this otherwise causes a hang??? */
828 if (info->MacModel == RADEON_MAC_IBOOK)
829 pll_fb_post_div = 0x000600ad;
832 pll_fb_post_div = (feedback_div | (post_div->bitvalue << 16));
834 htotal_cntl = mode->htotal & 0x7;
838 pll_gain = radeon_compute_pll_gain(pll->reference_freq,
840 pll_fb_post_div & 0x7ff);
842 if (radeon_crtc->crtc_id) {
843 uint32_t pixclks_cntl = ((RREG32_PLL(RADEON_PIXCLKS_CNTL) &
844 ~(RADEON_PIX2CLK_SRC_SEL_MASK)) |
845 RADEON_PIX2CLK_SRC_SEL_P2PLLCLK);
848 radeon_legacy_tv_adjust_pll2(encoder, &htotal_cntl,
849 &pll_ref_div, &pll_fb_post_div,
853 WREG32_PLL_P(RADEON_PIXCLKS_CNTL,
854 RADEON_PIX2CLK_SRC_SEL_CPUCLK,
855 ~(RADEON_PIX2CLK_SRC_SEL_MASK));
857 WREG32_PLL_P(RADEON_P2PLL_CNTL,
859 | RADEON_P2PLL_ATOMIC_UPDATE_EN
860 | ((uint32_t)pll_gain << RADEON_P2PLL_PVG_SHIFT),
862 | RADEON_P2PLL_ATOMIC_UPDATE_EN
863 | RADEON_P2PLL_PVG_MASK));
865 WREG32_PLL_P(RADEON_P2PLL_REF_DIV,
867 ~RADEON_P2PLL_REF_DIV_MASK);
869 WREG32_PLL_P(RADEON_P2PLL_DIV_0,
871 ~RADEON_P2PLL_FB0_DIV_MASK);
873 WREG32_PLL_P(RADEON_P2PLL_DIV_0,
875 ~RADEON_P2PLL_POST0_DIV_MASK);
877 radeon_pll2_write_update(dev);
878 radeon_pll2_wait_for_read_update_complete(dev);
880 WREG32_PLL(RADEON_HTOTAL2_CNTL, htotal_cntl);
882 WREG32_PLL_P(RADEON_P2PLL_CNTL,
886 | RADEON_P2PLL_ATOMIC_UPDATE_EN));
888 DRM_DEBUG("Wrote2: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
889 (unsigned)pll_ref_div,
890 (unsigned)pll_fb_post_div,
891 (unsigned)htotal_cntl,
892 RREG32_PLL(RADEON_P2PLL_CNTL));
893 DRM_DEBUG("Wrote2: rd=%u, fd=%u, pd=%u\n",
894 (unsigned)pll_ref_div & RADEON_P2PLL_REF_DIV_MASK,
895 (unsigned)pll_fb_post_div & RADEON_P2PLL_FB0_DIV_MASK,
896 (unsigned)((pll_fb_post_div &
897 RADEON_P2PLL_POST0_DIV_MASK) >> 16));
899 mdelay(50); /* Let the clock to lock */
901 WREG32_PLL_P(RADEON_PIXCLKS_CNTL,
902 RADEON_PIX2CLK_SRC_SEL_P2PLLCLK,
903 ~(RADEON_PIX2CLK_SRC_SEL_MASK));
905 WREG32_PLL(RADEON_PIXCLKS_CNTL, pixclks_cntl);
907 uint32_t pixclks_cntl;
911 pixclks_cntl = RREG32_PLL(RADEON_PIXCLKS_CNTL);
912 radeon_legacy_tv_adjust_pll1(encoder, &htotal_cntl, &pll_ref_div,
913 &pll_fb_post_div, &pixclks_cntl);
916 if (rdev->flags & RADEON_IS_MOBILITY) {
917 /* A temporal workaround for the occational blanking on certain laptop panels.
918 This appears to related to the PLL divider registers (fail to lock?).
919 It occurs even when all dividers are the same with their old settings.
920 In this case we really don't need to fiddle with PLL registers.
921 By doing this we can avoid the blanking problem with some panels.
923 if ((pll_ref_div == (RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_REF_DIV_MASK)) &&
924 (pll_fb_post_div == (RREG32_PLL(RADEON_PPLL_DIV_3) &
925 (RADEON_PPLL_POST3_DIV_MASK | RADEON_PPLL_FB3_DIV_MASK)))) {
926 WREG32_P(RADEON_CLOCK_CNTL_INDEX,
928 ~(RADEON_PLL_DIV_SEL));
929 r100_pll_errata_after_index(rdev);
934 WREG32_PLL_P(RADEON_VCLK_ECP_CNTL,
935 RADEON_VCLK_SRC_SEL_CPUCLK,
936 ~(RADEON_VCLK_SRC_SEL_MASK));
937 WREG32_PLL_P(RADEON_PPLL_CNTL,
939 | RADEON_PPLL_ATOMIC_UPDATE_EN
940 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
941 | ((uint32_t)pll_gain << RADEON_PPLL_PVG_SHIFT),
943 | RADEON_PPLL_ATOMIC_UPDATE_EN
944 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
945 | RADEON_PPLL_PVG_MASK));
947 WREG32_P(RADEON_CLOCK_CNTL_INDEX,
949 ~(RADEON_PLL_DIV_SEL));
950 r100_pll_errata_after_index(rdev);
952 if (ASIC_IS_R300(rdev) ||
953 (rdev->family == CHIP_RS300) ||
954 (rdev->family == CHIP_RS400) ||
955 (rdev->family == CHIP_RS480)) {
956 if (pll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
957 /* When restoring console mode, use saved PPLL_REF_DIV
960 WREG32_PLL_P(RADEON_PPLL_REF_DIV,
964 /* R300 uses ref_div_acc field as real ref divider */
965 WREG32_PLL_P(RADEON_PPLL_REF_DIV,
966 (pll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
967 ~R300_PPLL_REF_DIV_ACC_MASK);
970 WREG32_PLL_P(RADEON_PPLL_REF_DIV,
972 ~RADEON_PPLL_REF_DIV_MASK);
974 WREG32_PLL_P(RADEON_PPLL_DIV_3,
976 ~RADEON_PPLL_FB3_DIV_MASK);
978 WREG32_PLL_P(RADEON_PPLL_DIV_3,
980 ~RADEON_PPLL_POST3_DIV_MASK);
982 radeon_pll_write_update(dev);
983 radeon_pll_wait_for_read_update_complete(dev);
985 WREG32_PLL(RADEON_HTOTAL_CNTL, htotal_cntl);
987 WREG32_PLL_P(RADEON_PPLL_CNTL,
991 | RADEON_PPLL_ATOMIC_UPDATE_EN
992 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN));
994 DRM_DEBUG("Wrote: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
997 (unsigned)htotal_cntl,
998 RREG32_PLL(RADEON_PPLL_CNTL));
999 DRM_DEBUG("Wrote: rd=%d, fd=%d, pd=%d\n",
1000 pll_ref_div & RADEON_PPLL_REF_DIV_MASK,
1001 pll_fb_post_div & RADEON_PPLL_FB3_DIV_MASK,
1002 (pll_fb_post_div & RADEON_PPLL_POST3_DIV_MASK) >> 16);
1004 mdelay(50); /* Let the clock to lock */
1006 WREG32_PLL_P(RADEON_VCLK_ECP_CNTL,
1007 RADEON_VCLK_SRC_SEL_PPLLCLK,
1008 ~(RADEON_VCLK_SRC_SEL_MASK));
1011 WREG32_PLL(RADEON_PIXCLKS_CNTL, pixclks_cntl);
1015 static bool radeon_crtc_mode_fixup(struct drm_crtc *crtc,
1016 struct drm_display_mode *mode,
1017 struct drm_display_mode *adjusted_mode)
1019 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1024 static int radeon_crtc_mode_set(struct drm_crtc *crtc,
1025 struct drm_display_mode *mode,
1026 struct drm_display_mode *adjusted_mode,
1027 int x, int y, struct drm_framebuffer *old_fb)
1029 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1032 radeon_crtc_set_base(crtc, x, y, old_fb);
1033 radeon_set_crtc_timing(crtc, adjusted_mode);
1034 radeon_set_pll(crtc, adjusted_mode);
1035 if (radeon_crtc->crtc_id == 0) {
1036 radeon_legacy_rmx_mode_set(crtc, mode, adjusted_mode);
1038 if (radeon_crtc->rmx_type != RMX_OFF) {
1039 /* FIXME: only first crtc has rmx what should we
1042 DRM_ERROR("Mode need scaling but only first crtc can do that.\n");
1048 static void radeon_crtc_prepare(struct drm_crtc *crtc)
1050 radeon_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1053 static void radeon_crtc_commit(struct drm_crtc *crtc)
1055 radeon_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
1058 static const struct drm_crtc_helper_funcs legacy_helper_funcs = {
1059 .dpms = radeon_crtc_dpms,
1060 .mode_fixup = radeon_crtc_mode_fixup,
1061 .mode_set = radeon_crtc_mode_set,
1062 .mode_set_base = radeon_crtc_set_base,
1063 .prepare = radeon_crtc_prepare,
1064 .commit = radeon_crtc_commit,
1065 .load_lut = radeon_crtc_load_lut,
1069 void radeon_legacy_init_crtc(struct drm_device *dev,
1070 struct radeon_crtc *radeon_crtc)
1072 if (radeon_crtc->crtc_id == 1)
1073 radeon_crtc->crtc_offset = RADEON_CRTC2_H_TOTAL_DISP - RADEON_CRTC_H_TOTAL_DISP;
1074 drm_crtc_helper_add(&radeon_crtc->base, &legacy_helper_funcs);