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 <drm/drm_fixed.h>
33 static void radeon_overscan_setup(struct drm_crtc
*crtc
,
34 struct drm_display_mode
*mode
)
36 struct drm_device
*dev
= crtc
->dev
;
37 struct radeon_device
*rdev
= dev
->dev_private
;
38 struct radeon_crtc
*radeon_crtc
= to_radeon_crtc(crtc
);
40 WREG32(RADEON_OVR_CLR
+ radeon_crtc
->crtc_offset
, 0);
41 WREG32(RADEON_OVR_WID_LEFT_RIGHT
+ radeon_crtc
->crtc_offset
, 0);
42 WREG32(RADEON_OVR_WID_TOP_BOTTOM
+ radeon_crtc
->crtc_offset
, 0);
45 static void radeon_legacy_rmx_mode_set(struct drm_crtc
*crtc
,
46 struct drm_display_mode
*mode
)
48 struct drm_device
*dev
= crtc
->dev
;
49 struct radeon_device
*rdev
= dev
->dev_private
;
50 struct radeon_crtc
*radeon_crtc
= to_radeon_crtc(crtc
);
51 int xres
= mode
->hdisplay
;
52 int yres
= mode
->vdisplay
;
53 bool hscale
= true, vscale
= true;
58 u32 scale
, inc
, crtc_more_cntl
;
59 u32 fp_horz_stretch
, fp_vert_stretch
, fp_horz_vert_active
;
60 u32 fp_h_sync_strt_wid
, fp_crtc_h_total_disp
;
61 u32 fp_v_sync_strt_wid
, fp_crtc_v_total_disp
;
62 struct drm_display_mode
*native_mode
= &radeon_crtc
->native_mode
;
64 fp_vert_stretch
= RREG32(RADEON_FP_VERT_STRETCH
) &
65 (RADEON_VERT_STRETCH_RESERVED
|
66 RADEON_VERT_AUTO_RATIO_INC
);
67 fp_horz_stretch
= RREG32(RADEON_FP_HORZ_STRETCH
) &
68 (RADEON_HORZ_FP_LOOP_STRETCH
|
69 RADEON_HORZ_AUTO_RATIO_INC
);
72 if ((rdev
->family
== CHIP_RS100
) ||
73 (rdev
->family
== CHIP_RS200
)) {
74 crtc_more_cntl
|= RADEON_CRTC_H_CUTOFF_ACTIVE_EN
;
78 fp_crtc_h_total_disp
= ((((mode
->crtc_htotal
/ 8) - 1) & 0x3ff)
79 | ((((mode
->crtc_hdisplay
/ 8) - 1) & 0x1ff) << 16));
81 hsync_wid
= (mode
->crtc_hsync_end
- mode
->crtc_hsync_start
) / 8;
84 hsync_start
= mode
->crtc_hsync_start
- 8;
86 fp_h_sync_strt_wid
= ((hsync_start
& 0x1fff)
87 | ((hsync_wid
& 0x3f) << 16)
88 | ((mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
89 ? RADEON_CRTC_H_SYNC_POL
92 fp_crtc_v_total_disp
= (((mode
->crtc_vtotal
- 1) & 0xffff)
93 | ((mode
->crtc_vdisplay
- 1) << 16));
95 vsync_wid
= mode
->crtc_vsync_end
- mode
->crtc_vsync_start
;
99 fp_v_sync_strt_wid
= (((mode
->crtc_vsync_start
- 1) & 0xfff)
100 | ((vsync_wid
& 0x1f) << 16)
101 | ((mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
102 ? RADEON_CRTC_V_SYNC_POL
105 fp_horz_vert_active
= 0;
107 if (native_mode
->hdisplay
== 0 ||
108 native_mode
->vdisplay
== 0) {
112 if (xres
> native_mode
->hdisplay
)
113 xres
= native_mode
->hdisplay
;
114 if (yres
> native_mode
->vdisplay
)
115 yres
= native_mode
->vdisplay
;
117 if (xres
== native_mode
->hdisplay
)
119 if (yres
== native_mode
->vdisplay
)
123 switch (radeon_crtc
->rmx_type
) {
127 fp_horz_stretch
|= ((xres
/8-1) << 16);
129 inc
= (fp_horz_stretch
& RADEON_HORZ_AUTO_RATIO_INC
) ? 1 : 0;
130 scale
= ((xres
+ inc
) * RADEON_HORZ_STRETCH_RATIO_MAX
)
131 / native_mode
->hdisplay
+ 1;
132 fp_horz_stretch
|= (((scale
) & RADEON_HORZ_STRETCH_RATIO_MASK
) |
133 RADEON_HORZ_STRETCH_BLEND
|
134 RADEON_HORZ_STRETCH_ENABLE
|
135 ((native_mode
->hdisplay
/8-1) << 16));
139 fp_vert_stretch
|= ((yres
-1) << 12);
141 inc
= (fp_vert_stretch
& RADEON_VERT_AUTO_RATIO_INC
) ? 1 : 0;
142 scale
= ((yres
+ inc
) * RADEON_VERT_STRETCH_RATIO_MAX
)
143 / native_mode
->vdisplay
+ 1;
144 fp_vert_stretch
|= (((scale
) & RADEON_VERT_STRETCH_RATIO_MASK
) |
145 RADEON_VERT_STRETCH_ENABLE
|
146 RADEON_VERT_STRETCH_BLEND
|
147 ((native_mode
->vdisplay
-1) << 12));
151 fp_horz_stretch
|= ((xres
/8-1) << 16);
152 fp_vert_stretch
|= ((yres
-1) << 12);
154 crtc_more_cntl
|= (RADEON_CRTC_AUTO_HORZ_CENTER_EN
|
155 RADEON_CRTC_AUTO_VERT_CENTER_EN
);
157 blank_width
= (mode
->crtc_hblank_end
- mode
->crtc_hblank_start
) / 8;
158 if (blank_width
> 110)
161 fp_crtc_h_total_disp
= (((blank_width
) & 0x3ff)
162 | ((((mode
->crtc_hdisplay
/ 8) - 1) & 0x1ff) << 16));
164 hsync_wid
= (mode
->crtc_hsync_end
- mode
->crtc_hsync_start
) / 8;
168 fp_h_sync_strt_wid
= ((((mode
->crtc_hsync_start
- mode
->crtc_hblank_start
) / 8) & 0x1fff)
169 | ((hsync_wid
& 0x3f) << 16)
170 | ((mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
171 ? RADEON_CRTC_H_SYNC_POL
174 fp_crtc_v_total_disp
= (((mode
->crtc_vblank_end
- mode
->crtc_vblank_start
) & 0xffff)
175 | ((mode
->crtc_vdisplay
- 1) << 16));
177 vsync_wid
= mode
->crtc_vsync_end
- mode
->crtc_vsync_start
;
181 fp_v_sync_strt_wid
= ((((mode
->crtc_vsync_start
- mode
->crtc_vblank_start
) & 0xfff)
182 | ((vsync_wid
& 0x1f) << 16)
183 | ((mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
184 ? RADEON_CRTC_V_SYNC_POL
187 fp_horz_vert_active
= (((native_mode
->vdisplay
) & 0xfff) |
188 (((native_mode
->hdisplay
/ 8) & 0x1ff) << 16));
192 fp_horz_stretch
|= ((xres
/8-1) << 16);
193 fp_vert_stretch
|= ((yres
-1) << 12);
197 WREG32(RADEON_FP_HORZ_STRETCH
, fp_horz_stretch
);
198 WREG32(RADEON_FP_VERT_STRETCH
, fp_vert_stretch
);
199 WREG32(RADEON_CRTC_MORE_CNTL
, crtc_more_cntl
);
200 WREG32(RADEON_FP_HORZ_VERT_ACTIVE
, fp_horz_vert_active
);
201 WREG32(RADEON_FP_H_SYNC_STRT_WID
, fp_h_sync_strt_wid
);
202 WREG32(RADEON_FP_V_SYNC_STRT_WID
, fp_v_sync_strt_wid
);
203 WREG32(RADEON_FP_CRTC_H_TOTAL_DISP
, fp_crtc_h_total_disp
);
204 WREG32(RADEON_FP_CRTC_V_TOTAL_DISP
, fp_crtc_v_total_disp
);
207 void radeon_restore_common_regs(struct drm_device
*dev
)
209 /* don't need this yet */
212 static void radeon_pll_wait_for_read_update_complete(struct drm_device
*dev
)
214 struct radeon_device
*rdev
= dev
->dev_private
;
219 RREG32_PLL(RADEON_PPLL_REF_DIV
) & RADEON_PPLL_ATOMIC_UPDATE_R
);
223 static void radeon_pll_write_update(struct drm_device
*dev
)
225 struct radeon_device
*rdev
= dev
->dev_private
;
227 while (RREG32_PLL(RADEON_PPLL_REF_DIV
) & RADEON_PPLL_ATOMIC_UPDATE_R
);
229 WREG32_PLL_P(RADEON_PPLL_REF_DIV
,
230 RADEON_PPLL_ATOMIC_UPDATE_W
,
231 ~(RADEON_PPLL_ATOMIC_UPDATE_W
));
234 static void radeon_pll2_wait_for_read_update_complete(struct drm_device
*dev
)
236 struct radeon_device
*rdev
= dev
->dev_private
;
242 RREG32_PLL(RADEON_P2PLL_REF_DIV
) & RADEON_P2PLL_ATOMIC_UPDATE_R
);
246 static void radeon_pll2_write_update(struct drm_device
*dev
)
248 struct radeon_device
*rdev
= dev
->dev_private
;
250 while (RREG32_PLL(RADEON_P2PLL_REF_DIV
) & RADEON_P2PLL_ATOMIC_UPDATE_R
);
252 WREG32_PLL_P(RADEON_P2PLL_REF_DIV
,
253 RADEON_P2PLL_ATOMIC_UPDATE_W
,
254 ~(RADEON_P2PLL_ATOMIC_UPDATE_W
));
257 static uint8_t radeon_compute_pll_gain(uint16_t ref_freq
, uint16_t ref_div
,
260 unsigned int vcoFreq
;
265 vcoFreq
= ((unsigned)ref_freq
* fb_div
) / ref_div
;
268 * This is horribly crude: the VCO frequency range is divided into
269 * 3 parts, each part having a fixed PLL gain value.
271 if (vcoFreq
>= 30000)
276 else if (vcoFreq
>= 18000)
288 void radeon_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
290 struct radeon_crtc
*radeon_crtc
= to_radeon_crtc(crtc
);
291 struct drm_device
*dev
= crtc
->dev
;
292 struct radeon_device
*rdev
= dev
->dev_private
;
295 if (radeon_crtc
->crtc_id
)
296 mask
= (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 radeon_crtc
->enabled
= true;
308 /* adjust pm to dpms changes BEFORE enabling crtcs */
309 radeon_pm_compute_clocks(rdev
);
310 if (radeon_crtc
->crtc_id
)
311 WREG32_P(RADEON_CRTC2_GEN_CNTL
, RADEON_CRTC2_EN
, ~(RADEON_CRTC2_EN
| mask
));
313 WREG32_P(RADEON_CRTC_GEN_CNTL
, RADEON_CRTC_EN
, ~(RADEON_CRTC_EN
|
314 RADEON_CRTC_DISP_REQ_EN_B
));
315 WREG32_P(RADEON_CRTC_EXT_CNTL
, 0, ~mask
);
317 drm_vblank_post_modeset(dev
, radeon_crtc
->crtc_id
);
318 radeon_crtc_load_lut(crtc
);
320 case DRM_MODE_DPMS_STANDBY
:
321 case DRM_MODE_DPMS_SUSPEND
:
322 case DRM_MODE_DPMS_OFF
:
323 drm_vblank_pre_modeset(dev
, radeon_crtc
->crtc_id
);
324 if (radeon_crtc
->crtc_id
)
325 WREG32_P(RADEON_CRTC2_GEN_CNTL
, mask
, ~(RADEON_CRTC2_EN
| mask
));
327 WREG32_P(RADEON_CRTC_GEN_CNTL
, RADEON_CRTC_DISP_REQ_EN_B
, ~(RADEON_CRTC_EN
|
328 RADEON_CRTC_DISP_REQ_EN_B
));
329 WREG32_P(RADEON_CRTC_EXT_CNTL
, mask
, ~mask
);
331 radeon_crtc
->enabled
= false;
332 /* adjust pm to dpms changes AFTER disabling crtcs */
333 radeon_pm_compute_clocks(rdev
);
338 int radeon_crtc_set_base(struct drm_crtc
*crtc
, int x
, int y
,
339 struct drm_framebuffer
*old_fb
)
341 struct drm_device
*dev
= crtc
->dev
;
342 struct radeon_device
*rdev
= dev
->dev_private
;
343 struct radeon_crtc
*radeon_crtc
= to_radeon_crtc(crtc
);
344 struct radeon_framebuffer
*radeon_fb
;
345 struct drm_gem_object
*obj
;
346 struct radeon_bo
*rbo
;
348 uint32_t crtc_offset
, crtc_offset_cntl
, crtc_tile_x0_y0
= 0;
349 uint32_t crtc_pitch
, pitch_pixels
;
350 uint32_t tiling_flags
;
352 uint32_t gen_cntl_reg
, gen_cntl_val
;
358 DRM_DEBUG_KMS("No FB bound\n");
362 radeon_fb
= to_radeon_framebuffer(crtc
->fb
);
364 switch (crtc
->fb
->bits_per_pixel
) {
384 /* Pin framebuffer & get tilling informations */
385 obj
= radeon_fb
->obj
;
386 rbo
= obj
->driver_private
;
387 r
= radeon_bo_reserve(rbo
, false);
388 if (unlikely(r
!= 0))
390 r
= radeon_bo_pin(rbo
, RADEON_GEM_DOMAIN_VRAM
, &base
);
391 if (unlikely(r
!= 0)) {
392 radeon_bo_unreserve(rbo
);
395 radeon_bo_get_tiling_flags(rbo
, &tiling_flags
, NULL
);
396 radeon_bo_unreserve(rbo
);
397 if (tiling_flags
& RADEON_TILING_MICRO
)
398 DRM_ERROR("trying to scanout microtiled buffer\n");
400 /* if scanout was in GTT this really wouldn't work */
401 /* crtc offset is from display base addr not FB location */
402 radeon_crtc
->legacy_display_base_addr
= rdev
->mc
.vram_start
;
404 base
-= radeon_crtc
->legacy_display_base_addr
;
406 crtc_offset_cntl
= 0;
408 pitch_pixels
= crtc
->fb
->pitch
/ (crtc
->fb
->bits_per_pixel
/ 8);
409 crtc_pitch
= (((pitch_pixels
* crtc
->fb
->bits_per_pixel
) +
410 ((crtc
->fb
->bits_per_pixel
* 8) - 1)) /
411 (crtc
->fb
->bits_per_pixel
* 8));
412 crtc_pitch
|= crtc_pitch
<< 16;
415 if (tiling_flags
& RADEON_TILING_MACRO
) {
416 if (ASIC_IS_R300(rdev
))
417 crtc_offset_cntl
|= (R300_CRTC_X_Y_MODE_EN
|
418 R300_CRTC_MICRO_TILE_BUFFER_DIS
|
419 R300_CRTC_MACRO_TILE_EN
);
421 crtc_offset_cntl
|= RADEON_CRTC_TILE_EN
;
423 if (ASIC_IS_R300(rdev
))
424 crtc_offset_cntl
&= ~(R300_CRTC_X_Y_MODE_EN
|
425 R300_CRTC_MICRO_TILE_BUFFER_DIS
|
426 R300_CRTC_MACRO_TILE_EN
);
428 crtc_offset_cntl
&= ~RADEON_CRTC_TILE_EN
;
431 if (tiling_flags
& RADEON_TILING_MACRO
) {
432 if (ASIC_IS_R300(rdev
)) {
433 crtc_tile_x0_y0
= x
| (y
<< 16);
436 int byteshift
= crtc
->fb
->bits_per_pixel
>> 4;
437 int tile_addr
= (((y
>> 3) * pitch_pixels
+ x
) >> (8 - byteshift
)) << 11;
438 base
+= tile_addr
+ ((x
<< byteshift
) % 256) + ((y
% 8) << 8);
439 crtc_offset_cntl
|= (y
% 16);
442 int offset
= y
* pitch_pixels
+ x
;
443 switch (crtc
->fb
->bits_per_pixel
) {
465 if (radeon_crtc
->crtc_id
== 1)
466 gen_cntl_reg
= RADEON_CRTC2_GEN_CNTL
;
468 gen_cntl_reg
= RADEON_CRTC_GEN_CNTL
;
470 gen_cntl_val
= RREG32(gen_cntl_reg
);
471 gen_cntl_val
&= ~(0xf << 8);
472 gen_cntl_val
|= (format
<< 8);
473 WREG32(gen_cntl_reg
, gen_cntl_val
);
475 crtc_offset
= (u32
)base
;
477 WREG32(RADEON_DISPLAY_BASE_ADDR
+ radeon_crtc
->crtc_offset
, radeon_crtc
->legacy_display_base_addr
);
479 if (ASIC_IS_R300(rdev
)) {
480 if (radeon_crtc
->crtc_id
)
481 WREG32(R300_CRTC2_TILE_X0_Y0
, crtc_tile_x0_y0
);
483 WREG32(R300_CRTC_TILE_X0_Y0
, crtc_tile_x0_y0
);
485 WREG32(RADEON_CRTC_OFFSET_CNTL
+ radeon_crtc
->crtc_offset
, crtc_offset_cntl
);
486 WREG32(RADEON_CRTC_OFFSET
+ radeon_crtc
->crtc_offset
, crtc_offset
);
487 WREG32(RADEON_CRTC_PITCH
+ radeon_crtc
->crtc_offset
, crtc_pitch
);
489 if (old_fb
&& old_fb
!= crtc
->fb
) {
490 radeon_fb
= to_radeon_framebuffer(old_fb
);
491 rbo
= radeon_fb
->obj
->driver_private
;
492 r
= radeon_bo_reserve(rbo
, false);
493 if (unlikely(r
!= 0))
495 radeon_bo_unpin(rbo
);
496 radeon_bo_unreserve(rbo
);
499 /* Bytes per pixel may have changed */
500 radeon_bandwidth_update(rdev
);
505 static bool radeon_set_crtc_timing(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
)
507 struct drm_device
*dev
= crtc
->dev
;
508 struct radeon_device
*rdev
= dev
->dev_private
;
509 struct radeon_crtc
*radeon_crtc
= to_radeon_crtc(crtc
);
510 struct drm_encoder
*encoder
;
515 uint32_t crtc_h_total_disp
;
516 uint32_t crtc_h_sync_strt_wid
;
517 uint32_t crtc_v_total_disp
;
518 uint32_t crtc_v_sync_strt_wid
;
522 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
523 if (encoder
->crtc
== crtc
) {
524 struct radeon_encoder
*radeon_encoder
= to_radeon_encoder(encoder
);
525 if (radeon_encoder
->active_device
& ATOM_DEVICE_TV_SUPPORT
) {
527 DRM_INFO("crtc %d is connected to a TV\n", radeon_crtc
->crtc_id
);
533 switch (crtc
->fb
->bits_per_pixel
) {
553 crtc_h_total_disp
= ((((mode
->crtc_htotal
/ 8) - 1) & 0x3ff)
554 | ((((mode
->crtc_hdisplay
/ 8) - 1) & 0x1ff) << 16));
556 hsync_wid
= (mode
->crtc_hsync_end
- mode
->crtc_hsync_start
) / 8;
559 hsync_start
= mode
->crtc_hsync_start
- 8;
561 crtc_h_sync_strt_wid
= ((hsync_start
& 0x1fff)
562 | ((hsync_wid
& 0x3f) << 16)
563 | ((mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
564 ? RADEON_CRTC_H_SYNC_POL
567 /* This works for double scan mode. */
568 crtc_v_total_disp
= (((mode
->crtc_vtotal
- 1) & 0xffff)
569 | ((mode
->crtc_vdisplay
- 1) << 16));
571 vsync_wid
= mode
->crtc_vsync_end
- mode
->crtc_vsync_start
;
575 crtc_v_sync_strt_wid
= (((mode
->crtc_vsync_start
- 1) & 0xfff)
576 | ((vsync_wid
& 0x1f) << 16)
577 | ((mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
578 ? RADEON_CRTC_V_SYNC_POL
581 if (radeon_crtc
->crtc_id
) {
582 uint32_t crtc2_gen_cntl
;
583 uint32_t disp2_merge_cntl
;
585 /* if TV DAC is enabled for another crtc and keep it enabled */
586 crtc2_gen_cntl
= RREG32(RADEON_CRTC2_GEN_CNTL
) & 0x00718080;
587 crtc2_gen_cntl
|= ((format
<< 8)
588 | RADEON_CRTC2_VSYNC_DIS
589 | RADEON_CRTC2_HSYNC_DIS
590 | RADEON_CRTC2_DISP_DIS
591 | RADEON_CRTC2_DISP_REQ_EN_B
592 | ((mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
593 ? RADEON_CRTC2_DBL_SCAN_EN
595 | ((mode
->flags
& DRM_MODE_FLAG_CSYNC
)
596 ? RADEON_CRTC2_CSYNC_EN
598 | ((mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
599 ? RADEON_CRTC2_INTERLACE_EN
602 /* rs4xx chips seem to like to have the crtc enabled when the timing is set */
603 if ((rdev
->family
== CHIP_RS400
) || (rdev
->family
== CHIP_RS480
))
604 crtc2_gen_cntl
|= RADEON_CRTC2_EN
;
606 disp2_merge_cntl
= RREG32(RADEON_DISP2_MERGE_CNTL
);
607 disp2_merge_cntl
&= ~RADEON_DISP2_RGB_OFFSET_EN
;
609 WREG32(RADEON_DISP2_MERGE_CNTL
, disp2_merge_cntl
);
610 WREG32(RADEON_CRTC2_GEN_CNTL
, crtc2_gen_cntl
);
612 WREG32(RADEON_FP_H2_SYNC_STRT_WID
, crtc_h_sync_strt_wid
);
613 WREG32(RADEON_FP_V2_SYNC_STRT_WID
, crtc_v_sync_strt_wid
);
615 uint32_t crtc_gen_cntl
;
616 uint32_t crtc_ext_cntl
;
617 uint32_t disp_merge_cntl
;
619 crtc_gen_cntl
= RREG32(RADEON_CRTC_GEN_CNTL
) & 0x00718000;
620 crtc_gen_cntl
|= (RADEON_CRTC_EXT_DISP_EN
622 | RADEON_CRTC_DISP_REQ_EN_B
623 | ((mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
624 ? RADEON_CRTC_DBL_SCAN_EN
626 | ((mode
->flags
& DRM_MODE_FLAG_CSYNC
)
627 ? RADEON_CRTC_CSYNC_EN
629 | ((mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
630 ? RADEON_CRTC_INTERLACE_EN
633 /* rs4xx chips seem to like to have the crtc enabled when the timing is set */
634 if ((rdev
->family
== CHIP_RS400
) || (rdev
->family
== CHIP_RS480
))
635 crtc_gen_cntl
|= RADEON_CRTC_EN
;
637 crtc_ext_cntl
= RREG32(RADEON_CRTC_EXT_CNTL
);
638 crtc_ext_cntl
|= (RADEON_XCRT_CNT_EN
|
639 RADEON_CRTC_VSYNC_DIS
|
640 RADEON_CRTC_HSYNC_DIS
|
641 RADEON_CRTC_DISPLAY_DIS
);
643 disp_merge_cntl
= RREG32(RADEON_DISP_MERGE_CNTL
);
644 disp_merge_cntl
&= ~RADEON_DISP_RGB_OFFSET_EN
;
646 WREG32(RADEON_DISP_MERGE_CNTL
, disp_merge_cntl
);
647 WREG32(RADEON_CRTC_GEN_CNTL
, crtc_gen_cntl
);
648 WREG32(RADEON_CRTC_EXT_CNTL
, crtc_ext_cntl
);
652 radeon_legacy_tv_adjust_crtc_reg(encoder
, &crtc_h_total_disp
,
653 &crtc_h_sync_strt_wid
, &crtc_v_total_disp
,
654 &crtc_v_sync_strt_wid
);
656 WREG32(RADEON_CRTC_H_TOTAL_DISP
+ radeon_crtc
->crtc_offset
, crtc_h_total_disp
);
657 WREG32(RADEON_CRTC_H_SYNC_STRT_WID
+ radeon_crtc
->crtc_offset
, crtc_h_sync_strt_wid
);
658 WREG32(RADEON_CRTC_V_TOTAL_DISP
+ radeon_crtc
->crtc_offset
, crtc_v_total_disp
);
659 WREG32(RADEON_CRTC_V_SYNC_STRT_WID
+ radeon_crtc
->crtc_offset
, crtc_v_sync_strt_wid
);
664 static void radeon_set_pll(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
)
666 struct drm_device
*dev
= crtc
->dev
;
667 struct radeon_device
*rdev
= dev
->dev_private
;
668 struct radeon_crtc
*radeon_crtc
= to_radeon_crtc(crtc
);
669 struct drm_encoder
*encoder
;
670 uint32_t feedback_div
= 0;
671 uint32_t frac_fb_div
= 0;
672 uint32_t reference_div
= 0;
673 uint32_t post_divider
= 0;
676 bool use_bios_divs
= false;
678 uint32_t pll_ref_div
= 0;
679 uint32_t pll_fb_post_div
= 0;
680 uint32_t htotal_cntl
= 0;
682 struct radeon_pll
*pll
;
687 } *post_div
, post_divs
[] = {
688 /* From RAGE 128 VR/RAGE 128 GL Register
689 * Reference Manual (Technical Reference
690 * Manual P/N RRG-G04100-C Rev. 0.04), page
691 * 3-17 (PLL_DIV_[3:0]).
693 { 1, 0 }, /* VCLK_SRC */
694 { 2, 1 }, /* VCLK_SRC/2 */
695 { 4, 2 }, /* VCLK_SRC/4 */
696 { 8, 3 }, /* VCLK_SRC/8 */
697 { 3, 4 }, /* VCLK_SRC/3 */
698 { 16, 5 }, /* VCLK_SRC/16 */
699 { 6, 6 }, /* VCLK_SRC/6 */
700 { 12, 7 }, /* VCLK_SRC/12 */
704 if (radeon_crtc
->crtc_id
)
705 pll
= &rdev
->clock
.p2pll
;
707 pll
= &rdev
->clock
.p1pll
;
709 pll
->flags
= RADEON_PLL_LEGACY
;
710 if (radeon_new_pll
== 1)
711 pll
->algo
= PLL_ALGO_NEW
;
713 pll
->algo
= PLL_ALGO_LEGACY
;
715 if (mode
->clock
> 200000) /* range limits??? */
716 pll
->flags
|= RADEON_PLL_PREFER_HIGH_FB_DIV
;
718 pll
->flags
|= RADEON_PLL_PREFER_LOW_REF_DIV
;
720 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
721 if (encoder
->crtc
== crtc
) {
722 struct radeon_encoder
*radeon_encoder
= to_radeon_encoder(encoder
);
724 if (radeon_encoder
->active_device
& ATOM_DEVICE_TV_SUPPORT
) {
729 if (encoder
->encoder_type
!= DRM_MODE_ENCODER_DAC
)
730 pll
->flags
|= RADEON_PLL_NO_ODD_POST_DIV
;
731 if (encoder
->encoder_type
== DRM_MODE_ENCODER_LVDS
) {
732 if (!rdev
->is_atom_bios
) {
733 struct radeon_encoder
*radeon_encoder
= to_radeon_encoder(encoder
);
734 struct radeon_encoder_lvds
*lvds
= (struct radeon_encoder_lvds
*)radeon_encoder
->enc_priv
;
736 if (lvds
->use_bios_dividers
) {
737 pll_ref_div
= lvds
->panel_ref_divider
;
738 pll_fb_post_div
= (lvds
->panel_fb_divider
|
739 (lvds
->panel_post_divider
<< 16));
741 use_bios_divs
= true;
745 pll
->flags
|= RADEON_PLL_USE_REF_DIV
;
752 if (!use_bios_divs
) {
753 radeon_compute_pll(pll
, mode
->clock
,
754 &freq
, &feedback_div
, &frac_fb_div
,
755 &reference_div
, &post_divider
);
757 for (post_div
= &post_divs
[0]; post_div
->divider
; ++post_div
) {
758 if (post_div
->divider
== post_divider
)
762 if (!post_div
->divider
)
763 post_div
= &post_divs
[0];
765 DRM_DEBUG_KMS("dc=%u, fd=%d, rd=%d, pd=%d\n",
771 pll_ref_div
= reference_div
;
772 pll_fb_post_div
= (feedback_div
| (post_div
->bitvalue
<< 16));
774 htotal_cntl
= mode
->htotal
& 0x7;
778 pll_gain
= radeon_compute_pll_gain(pll
->reference_freq
,
780 pll_fb_post_div
& 0x7ff);
782 if (radeon_crtc
->crtc_id
) {
783 uint32_t pixclks_cntl
= ((RREG32_PLL(RADEON_PIXCLKS_CNTL
) &
784 ~(RADEON_PIX2CLK_SRC_SEL_MASK
)) |
785 RADEON_PIX2CLK_SRC_SEL_P2PLLCLK
);
788 radeon_legacy_tv_adjust_pll2(encoder
, &htotal_cntl
,
789 &pll_ref_div
, &pll_fb_post_div
,
793 WREG32_PLL_P(RADEON_PIXCLKS_CNTL
,
794 RADEON_PIX2CLK_SRC_SEL_CPUCLK
,
795 ~(RADEON_PIX2CLK_SRC_SEL_MASK
));
797 WREG32_PLL_P(RADEON_P2PLL_CNTL
,
799 | RADEON_P2PLL_ATOMIC_UPDATE_EN
800 | ((uint32_t)pll_gain
<< RADEON_P2PLL_PVG_SHIFT
),
802 | RADEON_P2PLL_ATOMIC_UPDATE_EN
803 | RADEON_P2PLL_PVG_MASK
));
805 WREG32_PLL_P(RADEON_P2PLL_REF_DIV
,
807 ~RADEON_P2PLL_REF_DIV_MASK
);
809 WREG32_PLL_P(RADEON_P2PLL_DIV_0
,
811 ~RADEON_P2PLL_FB0_DIV_MASK
);
813 WREG32_PLL_P(RADEON_P2PLL_DIV_0
,
815 ~RADEON_P2PLL_POST0_DIV_MASK
);
817 radeon_pll2_write_update(dev
);
818 radeon_pll2_wait_for_read_update_complete(dev
);
820 WREG32_PLL(RADEON_HTOTAL2_CNTL
, htotal_cntl
);
822 WREG32_PLL_P(RADEON_P2PLL_CNTL
,
826 | RADEON_P2PLL_ATOMIC_UPDATE_EN
));
828 DRM_DEBUG_KMS("Wrote2: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
829 (unsigned)pll_ref_div
,
830 (unsigned)pll_fb_post_div
,
831 (unsigned)htotal_cntl
,
832 RREG32_PLL(RADEON_P2PLL_CNTL
));
833 DRM_DEBUG_KMS("Wrote2: rd=%u, fd=%u, pd=%u\n",
834 (unsigned)pll_ref_div
& RADEON_P2PLL_REF_DIV_MASK
,
835 (unsigned)pll_fb_post_div
& RADEON_P2PLL_FB0_DIV_MASK
,
836 (unsigned)((pll_fb_post_div
&
837 RADEON_P2PLL_POST0_DIV_MASK
) >> 16));
839 mdelay(50); /* Let the clock to lock */
841 WREG32_PLL_P(RADEON_PIXCLKS_CNTL
,
842 RADEON_PIX2CLK_SRC_SEL_P2PLLCLK
,
843 ~(RADEON_PIX2CLK_SRC_SEL_MASK
));
845 WREG32_PLL(RADEON_PIXCLKS_CNTL
, pixclks_cntl
);
847 uint32_t pixclks_cntl
;
851 pixclks_cntl
= RREG32_PLL(RADEON_PIXCLKS_CNTL
);
852 radeon_legacy_tv_adjust_pll1(encoder
, &htotal_cntl
, &pll_ref_div
,
853 &pll_fb_post_div
, &pixclks_cntl
);
856 if (rdev
->flags
& RADEON_IS_MOBILITY
) {
857 if ((pll_ref_div
== (RREG32_PLL(RADEON_PPLL_REF_DIV
) & RADEON_PPLL_REF_DIV_MASK
)) &&
858 (pll_fb_post_div
== (RREG32_PLL(RADEON_PPLL_DIV_3
) &
859 (RADEON_PPLL_POST3_DIV_MASK
| RADEON_PPLL_FB3_DIV_MASK
)))) {
860 WREG32_P(RADEON_CLOCK_CNTL_INDEX
,
862 ~(RADEON_PLL_DIV_SEL
));
863 r100_pll_errata_after_index(rdev
);
868 WREG32_PLL_P(RADEON_VCLK_ECP_CNTL
,
869 RADEON_VCLK_SRC_SEL_CPUCLK
,
870 ~(RADEON_VCLK_SRC_SEL_MASK
));
871 WREG32_PLL_P(RADEON_PPLL_CNTL
,
873 | RADEON_PPLL_ATOMIC_UPDATE_EN
874 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
875 | ((uint32_t)pll_gain
<< RADEON_PPLL_PVG_SHIFT
),
877 | RADEON_PPLL_ATOMIC_UPDATE_EN
878 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
879 | RADEON_PPLL_PVG_MASK
));
881 WREG32_P(RADEON_CLOCK_CNTL_INDEX
,
883 ~(RADEON_PLL_DIV_SEL
));
884 r100_pll_errata_after_index(rdev
);
886 if (ASIC_IS_R300(rdev
) ||
887 (rdev
->family
== CHIP_RS300
) ||
888 (rdev
->family
== CHIP_RS400
) ||
889 (rdev
->family
== CHIP_RS480
)) {
890 if (pll_ref_div
& R300_PPLL_REF_DIV_ACC_MASK
) {
891 /* When restoring console mode, use saved PPLL_REF_DIV
894 WREG32_PLL_P(RADEON_PPLL_REF_DIV
,
898 /* R300 uses ref_div_acc field as real ref divider */
899 WREG32_PLL_P(RADEON_PPLL_REF_DIV
,
900 (pll_ref_div
<< R300_PPLL_REF_DIV_ACC_SHIFT
),
901 ~R300_PPLL_REF_DIV_ACC_MASK
);
904 WREG32_PLL_P(RADEON_PPLL_REF_DIV
,
906 ~RADEON_PPLL_REF_DIV_MASK
);
908 WREG32_PLL_P(RADEON_PPLL_DIV_3
,
910 ~RADEON_PPLL_FB3_DIV_MASK
);
912 WREG32_PLL_P(RADEON_PPLL_DIV_3
,
914 ~RADEON_PPLL_POST3_DIV_MASK
);
916 radeon_pll_write_update(dev
);
917 radeon_pll_wait_for_read_update_complete(dev
);
919 WREG32_PLL(RADEON_HTOTAL_CNTL
, htotal_cntl
);
921 WREG32_PLL_P(RADEON_PPLL_CNTL
,
925 | RADEON_PPLL_ATOMIC_UPDATE_EN
926 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
));
928 DRM_DEBUG_KMS("Wrote: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
931 (unsigned)htotal_cntl
,
932 RREG32_PLL(RADEON_PPLL_CNTL
));
933 DRM_DEBUG_KMS("Wrote: rd=%d, fd=%d, pd=%d\n",
934 pll_ref_div
& RADEON_PPLL_REF_DIV_MASK
,
935 pll_fb_post_div
& RADEON_PPLL_FB3_DIV_MASK
,
936 (pll_fb_post_div
& RADEON_PPLL_POST3_DIV_MASK
) >> 16);
938 mdelay(50); /* Let the clock to lock */
940 WREG32_PLL_P(RADEON_VCLK_ECP_CNTL
,
941 RADEON_VCLK_SRC_SEL_PPLLCLK
,
942 ~(RADEON_VCLK_SRC_SEL_MASK
));
945 WREG32_PLL(RADEON_PIXCLKS_CNTL
, pixclks_cntl
);
949 static bool radeon_crtc_mode_fixup(struct drm_crtc
*crtc
,
950 struct drm_display_mode
*mode
,
951 struct drm_display_mode
*adjusted_mode
)
953 struct drm_device
*dev
= crtc
->dev
;
954 struct radeon_device
*rdev
= dev
->dev_private
;
956 /* adjust pm to upcoming mode change */
957 radeon_pm_compute_clocks(rdev
);
959 if (!radeon_crtc_scaling_mode_fixup(crtc
, mode
, adjusted_mode
))
964 static int radeon_crtc_mode_set(struct drm_crtc
*crtc
,
965 struct drm_display_mode
*mode
,
966 struct drm_display_mode
*adjusted_mode
,
967 int x
, int y
, struct drm_framebuffer
*old_fb
)
969 struct radeon_crtc
*radeon_crtc
= to_radeon_crtc(crtc
);
972 radeon_crtc_set_base(crtc
, x
, y
, old_fb
);
973 radeon_set_crtc_timing(crtc
, adjusted_mode
);
974 radeon_set_pll(crtc
, adjusted_mode
);
975 radeon_overscan_setup(crtc
, adjusted_mode
);
976 if (radeon_crtc
->crtc_id
== 0) {
977 radeon_legacy_rmx_mode_set(crtc
, adjusted_mode
);
979 if (radeon_crtc
->rmx_type
!= RMX_OFF
) {
980 DRM_ERROR("Mode need scaling but only first crtc can do that.\n");
986 static void radeon_crtc_prepare(struct drm_crtc
*crtc
)
988 struct drm_device
*dev
= crtc
->dev
;
989 struct drm_crtc
*crtci
;
992 * The hardware wedges sometimes if you reconfigure one CRTC
993 * whilst another is running (see fdo bug #24611).
995 list_for_each_entry(crtci
, &dev
->mode_config
.crtc_list
, head
)
996 radeon_crtc_dpms(crtci
, DRM_MODE_DPMS_OFF
);
999 static void radeon_crtc_commit(struct drm_crtc
*crtc
)
1001 struct drm_device
*dev
= crtc
->dev
;
1002 struct drm_crtc
*crtci
;
1005 * Reenable the CRTCs that should be running.
1007 list_for_each_entry(crtci
, &dev
->mode_config
.crtc_list
, head
) {
1009 radeon_crtc_dpms(crtci
, DRM_MODE_DPMS_ON
);
1013 static const struct drm_crtc_helper_funcs legacy_helper_funcs
= {
1014 .dpms
= radeon_crtc_dpms
,
1015 .mode_fixup
= radeon_crtc_mode_fixup
,
1016 .mode_set
= radeon_crtc_mode_set
,
1017 .mode_set_base
= radeon_crtc_set_base
,
1018 .prepare
= radeon_crtc_prepare
,
1019 .commit
= radeon_crtc_commit
,
1020 .load_lut
= radeon_crtc_load_lut
,
1024 void radeon_legacy_init_crtc(struct drm_device
*dev
,
1025 struct radeon_crtc
*radeon_crtc
)
1027 if (radeon_crtc
->crtc_id
== 1)
1028 radeon_crtc
->crtc_offset
= RADEON_CRTC2_H_TOTAL_DISP
- RADEON_CRTC_H_TOTAL_DISP
;
1029 drm_crtc_helper_add(&radeon_crtc
->base
, &legacy_helper_funcs
);