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 "radeon_drm.h"
28 #include "radeon_drv.h"
30 #include "drm_crtc_helper.h"
32 void radeon_restore_common_regs(struct drm_device *dev, struct radeon_legacy_state *state)
34 /* don't need this yet */
37 static void radeon_pll_wait_for_read_update_complete(struct drm_device *dev)
39 struct drm_radeon_private *dev_priv = dev->dev_private;
42 /* FIXME: Certain revisions of R300 can't recover here. Not sure of
43 the cause yet, but this workaround will mask the problem for now.
44 Other chips usually will pass at the very first test, so the
45 workaround shouldn't have any effect on them. */
48 RADEON_READ_PLL(dev_priv, RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
52 static void radeon_pll_write_update(struct drm_device *dev)
54 struct drm_radeon_private *dev_priv = dev->dev_private;
56 while (RADEON_READ_PLL(dev_priv, RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
58 RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_REF_DIV,
59 RADEON_PPLL_ATOMIC_UPDATE_W,
60 ~(RADEON_PPLL_ATOMIC_UPDATE_W));
63 static void radeon_pll2_wait_for_read_update_complete(struct drm_device *dev)
65 struct drm_radeon_private *dev_priv = dev->dev_private;
69 /* FIXME: Certain revisions of R300 can't recover here. Not sure of
70 the cause yet, but this workaround will mask the problem for now.
71 Other chips usually will pass at the very first test, so the
72 workaround shouldn't have any effect on them. */
75 RADEON_READ_PLL(dev_priv, RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
79 static void radeon_pll2_write_update(struct drm_device *dev)
81 struct drm_radeon_private *dev_priv = dev->dev_private;
83 while (RADEON_READ_PLL(dev_priv, RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
85 RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_REF_DIV,
86 RADEON_P2PLL_ATOMIC_UPDATE_W,
87 ~(RADEON_P2PLL_ATOMIC_UPDATE_W));
90 static uint8_t radeon_compute_pll_gain(uint16_t ref_freq, uint16_t ref_div,
98 vcoFreq = ((unsigned)ref_freq & fb_div) / ref_div;
101 * This is horribly crude: the VCO frequency range is divided into
102 * 3 parts, each part having a fixed PLL gain value.
104 if (vcoFreq >= 30000)
109 else if (vcoFreq >= 18000)
121 void radeon_crtc_dpms(struct drm_crtc *crtc, int mode)
123 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
124 struct drm_device *dev = crtc->dev;
125 struct drm_radeon_private *dev_priv = dev->dev_private;
129 mask = radeon_crtc->crtc_id ?
130 (RADEON_CRTC2_DISP_DIS | RADEON_CRTC2_VSYNC_DIS | RADEON_CRTC2_HSYNC_DIS | RADEON_CRTC2_DISP_REQ_EN_B) :
131 (RADEON_CRTC_DISPLAY_DIS | RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS);
134 case DRM_MODE_DPMS_ON:
135 if (radeon_crtc->crtc_id) {
136 RADEON_WRITE_P(RADEON_CRTC2_GEN_CNTL, 0, ~mask);
138 RADEON_WRITE_P(RADEON_CRTC_GEN_CNTL, 0, ~RADEON_CRTC_DISP_REQ_EN_B);
139 RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, 0, ~mask);
142 case DRM_MODE_DPMS_STANDBY:
143 if (radeon_crtc->crtc_id) {
144 RADEON_WRITE_P(RADEON_CRTC2_GEN_CNTL, (RADEON_CRTC2_DISP_DIS | RADEON_CRTC2_HSYNC_DIS), ~mask);
146 RADEON_WRITE_P(RADEON_CRTC_GEN_CNTL, 0, ~RADEON_CRTC_DISP_REQ_EN_B);
147 RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, (RADEON_CRTC_DISPLAY_DIS | RADEON_CRTC_HSYNC_DIS), ~mask);
150 case DRM_MODE_DPMS_SUSPEND:
151 if (radeon_crtc->crtc_id) {
152 RADEON_WRITE_P(RADEON_CRTC2_GEN_CNTL, (RADEON_CRTC2_DISP_DIS | RADEON_CRTC2_VSYNC_DIS), ~mask);
154 RADEON_WRITE_P(RADEON_CRTC_GEN_CNTL, 0, ~RADEON_CRTC_DISP_REQ_EN_B);
155 RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, (RADEON_CRTC_DISPLAY_DIS | RADEON_CRTC_VSYNC_DIS), ~mask);
158 case DRM_MODE_DPMS_OFF:
159 if (radeon_crtc->crtc_id) {
160 RADEON_WRITE_P(RADEON_CRTC2_GEN_CNTL, mask, ~mask);
162 RADEON_WRITE_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_DISP_REQ_EN_B, ~RADEON_CRTC_DISP_REQ_EN_B);
163 RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, mask, ~mask);
168 if (mode != DRM_MODE_DPMS_OFF) {
169 radeon_crtc_load_lut(crtc);
173 static bool radeon_set_crtc1_base(struct drm_crtc *crtc, int x, int y)
175 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
176 struct drm_device *dev = crtc->dev;
177 struct drm_radeon_private *dev_priv = dev->dev_private;
178 struct radeon_framebuffer *radeon_fb;
179 struct drm_radeon_gem_object *obj_priv;
181 uint32_t crtc_offset, crtc_offset_cntl, crtc_tile_x0_y0 = 0;
183 radeon_fb = to_radeon_framebuffer(crtc->fb);
185 obj_priv = radeon_fb->obj->driver_private;
187 crtc_offset = obj_priv->bo->offset;
189 crtc_offset_cntl = 0;
193 if (radeon_is_r300(dev_priv))
194 crtc_offset_cntl |= (R300_CRTC_X_Y_MODE_EN |
195 R300_CRTC_MICRO_TILE_BUFFER_DIS |
196 R300_CRTC_MACRO_TILE_EN);
198 crtc_offset_cntl |= RADEON_CRTC_TILE_EN;
200 if (radeon_is_r300(dev_priv))
201 crtc_offset_cntl &= ~(R300_CRTC_X_Y_MODE_EN |
202 R300_CRTC_MICRO_TILE_BUFFER_DIS |
203 R300_CRTC_MACRO_TILE_EN);
205 crtc_offset_cntl &= ~RADEON_CRTC_TILE_EN;
208 base = obj_priv->bo->offset;
210 /* TODO more tiling */
212 if (radeon_is_r300(dev_priv)) {
213 crtc_tile_x0_y0 = x | (y << 16);
216 int byteshift = crtc->fb->bits_per_pixel >> 4;
217 int tile_addr = (((y >> 3) * crtc->fb->width + x) >> (8 - byteshift)) << 11;
218 base += tile_addr + ((x << byteshift) % 256) + ((y % 8) << 8);
219 crtc_offset_cntl |= (y % 16);
222 int offset = y * crtc->fb->pitch + x;
223 switch (crtc->fb->bits_per_pixel) {
242 /* update sarea TODO */
246 if (radeon_is_r300(dev_priv))
247 RADEON_WRITE(R300_CRTC_TILE_X0_Y0, crtc_tile_x0_y0);
249 RADEON_WRITE(RADEON_CRTC_OFFSET_CNTL, crtc_offset_cntl);
250 RADEON_WRITE(RADEON_CRTC_OFFSET, crtc_offset);
255 static bool radeon_set_crtc1_timing(struct drm_crtc *crtc, struct drm_display_mode *mode)
257 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
258 struct drm_device *dev = crtc->dev;
259 struct drm_radeon_private *dev_priv = dev->dev_private;
264 uint32_t crtc_gen_cntl;
265 uint32_t crtc_ext_cntl;
266 uint32_t crtc_h_total_disp;
267 uint32_t crtc_h_sync_strt_wid;
268 uint32_t crtc_v_total_disp;
269 uint32_t crtc_v_sync_strt_wid;
271 uint32_t disp_merge_cntl;
273 switch (crtc->fb->depth) {
291 crtc_gen_cntl = (RADEON_CRTC_EXT_DISP_EN
294 | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
295 ? RADEON_CRTC_DBL_SCAN_EN
297 | ((mode->flags & DRM_MODE_FLAG_CSYNC)
298 ? RADEON_CRTC_CSYNC_EN
300 | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
301 ? RADEON_CRTC_INTERLACE_EN
304 crtc_ext_cntl = RADEON_READ(RADEON_CRTC_EXT_CNTL);
305 crtc_ext_cntl |= (RADEON_XCRT_CNT_EN|
306 RADEON_CRTC_VSYNC_DIS |
307 RADEON_CRTC_HSYNC_DIS |
308 RADEON_CRTC_DISPLAY_DIS);
310 disp_merge_cntl = RADEON_READ(RADEON_DISP_MERGE_CNTL);
311 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
313 crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
314 | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
316 hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
319 hsync_start = mode->crtc_hsync_start - 8;
321 crtc_h_sync_strt_wid = ((hsync_start & 0x1fff)
322 | ((hsync_wid & 0x3f) << 16)
323 | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
324 ? RADEON_CRTC_H_SYNC_POL
327 /* This works for double scan mode. */
328 crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
329 | ((mode->crtc_vdisplay - 1) << 16));
331 vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
335 crtc_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
336 | ((vsync_wid & 0x1f) << 16)
337 | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
338 ? RADEON_CRTC_V_SYNC_POL
341 crtc_pitch = (((crtc->fb->pitch * crtc->fb->bits_per_pixel) +
342 ((crtc->fb->bits_per_pixel * 8) -1)) /
343 (crtc->fb->bits_per_pixel * 8));
344 crtc_pitch |= crtc_pitch << 16;
346 /* TODO -> Dell Server */
348 uint32_t disp_hw_debug = RADEON_READ(RADEON_DISP_HW_DEBUG);
349 uint32_t tv_dac_cntl = RADEON_READ(RADEON_TV_DAC_CNTL);
350 uint32_t dac2_cntl = RADEON_READ(RADEON_DAC_CNTL2);
351 uint32_t crtc2_gen_cntl = RADEON_READ(RADEON_CRTC2_GEN_CNTL);
352 // state->dac2_cntl = info->StatedReg->dac2_cntl;
353 // state->tv_dac_cntl = info->StatedReg->tv_dac_cntl;
354 // state->crtc2_gen_cntl = info->StatedReg->crtc2_gen_cntl;
355 // state->disp_hw_debug = info->StatedReg->disp_hw_debug;
357 // state->dac2_cntl &= ~RADEON_DAC2_DAC_CLK_SEL;
358 // state->dac2_cntl |= RADEON_DAC2_DAC2_CLK_SEL;
360 /* For CRT on DAC2, don't turn it on if BIOS didn't
361 enable it, even it's detected.
363 disp_hw_debug |= RADEON_CRT2_DISP1_SEL;
364 tv_dac_cntl &= ~((1<<2) | (3<<8) | (7<<24) | (0xff<<16));
365 tv_dac_cntl |= (0x03 | (2<<8) | (0x58<<16));
367 RADEON_WRITE(RADEON_TV_DAC_CNTL, tv_dac_cntl);
368 RADEON_WRITE(RADEON_DISP_HW_DEBUG, disp_hw_debug);
369 RADEON_WRITE(RADEON_DAC_CNTL2, dac2_cntl);
370 RADEON_WRITE(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
373 RADEON_WRITE(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl |
374 RADEON_CRTC_DISP_REQ_EN_B);
376 RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, crtc_ext_cntl,
377 RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS | RADEON_CRTC_DISPLAY_DIS);
379 RADEON_WRITE(RADEON_CRTC_H_TOTAL_DISP, crtc_h_total_disp);
380 RADEON_WRITE(RADEON_CRTC_H_SYNC_STRT_WID, crtc_h_sync_strt_wid);
381 RADEON_WRITE(RADEON_CRTC_V_TOTAL_DISP, crtc_v_total_disp);
382 RADEON_WRITE(RADEON_CRTC_V_SYNC_STRT_WID, crtc_v_sync_strt_wid);
384 RADEON_WRITE(RADEON_CRTC_PITCH, crtc_pitch);
385 RADEON_WRITE(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
387 RADEON_WRITE(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);
392 static void radeon_set_pll1(struct drm_crtc *crtc, struct drm_display_mode *mode, int flags)
394 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
395 struct drm_device *dev = crtc->dev;
396 struct drm_radeon_private *dev_priv = dev->dev_private;
397 uint32_t feedback_div = 0;
398 uint32_t reference_div = 0;
399 uint32_t post_divider = 0;
403 uint32_t ppll_ref_div;
405 uint32_t htotal_cntl;
406 uint32_t vclk_ecp_cntl;
408 struct radeon_pll *pll = &dev_priv->mode_info.pll;
413 } *post_div, post_divs[] = {
414 /* From RAGE 128 VR/RAGE 128 GL Register
415 * Reference Manual (Technical Reference
416 * Manual P/N RRG-G04100-C Rev. 0.04), page
417 * 3-17 (PLL_DIV_[3:0]).
419 { 1, 0 }, /* VCLK_SRC */
420 { 2, 1 }, /* VCLK_SRC/2 */
421 { 4, 2 }, /* VCLK_SRC/4 */
422 { 8, 3 }, /* VCLK_SRC/8 */
423 { 3, 4 }, /* VCLK_SRC/3 */
424 { 16, 5 }, /* VCLK_SRC/16 */
425 { 6, 6 }, /* VCLK_SRC/6 */
426 { 12, 7 }, /* VCLK_SRC/12 */
431 if ((flags & RADEON_PLL_USE_BIOS_DIVS) && info->UseBiosDividers) {
432 ppll_ref_div = info->RefDivider;
433 ppll_div_3 = info->FeedbackDivider | (info->PostDivider << 16);
440 radeon_compute_pll(pll, mode->clock, &freq, &feedback_div, &reference_div, &post_divider, flags);
442 for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
443 if (post_div->divider == post_divider)
447 if (!post_div->divider) {
448 post_div = &post_divs[0];
451 DRM_DEBUG("dc=%u, fd=%d, rd=%d, pd=%d\n",
457 ppll_ref_div = reference_div;
459 #if defined(__powerpc__) && (0) /* TODO */
460 /* apparently programming this otherwise causes a hang??? */
461 if (info->MacModel == RADEON_MAC_IBOOK)
462 state->ppll_div_3 = 0x000600ad;
465 ppll_div_3 = (feedback_div | (post_div->bitvalue << 16));
467 htotal_cntl = mode->htotal & 0x7;
469 vclk_ecp_cntl = (RADEON_READ_PLL(dev_priv, RADEON_VCLK_ECP_CNTL) &
470 ~RADEON_VCLK_SRC_SEL_MASK) | RADEON_VCLK_SRC_SEL_PPLLCLK;
472 pll_gain = radeon_compute_pll_gain(dev_priv->mode_info.pll.reference_freq,
473 ppll_ref_div & RADEON_PPLL_REF_DIV_MASK,
474 ppll_div_3 & RADEON_PPLL_FB3_DIV_MASK);
476 if (dev_priv->flags & RADEON_IS_MOBILITY) {
477 /* A temporal workaround for the occational blanking on certain laptop panels.
478 This appears to related to the PLL divider registers (fail to lock?).
479 It occurs even when all dividers are the same with their old settings.
480 In this case we really don't need to fiddle with PLL registers.
481 By doing this we can avoid the blanking problem with some panels.
483 if ((ppll_ref_div == (RADEON_READ_PLL(dev_priv, RADEON_PPLL_REF_DIV) & RADEON_PPLL_REF_DIV_MASK)) &&
484 (ppll_div_3 == (RADEON_READ_PLL(dev_priv, RADEON_PPLL_DIV_3) &
485 (RADEON_PPLL_POST3_DIV_MASK | RADEON_PPLL_FB3_DIV_MASK)))) {
486 RADEON_WRITE_P(RADEON_CLOCK_CNTL_INDEX,
488 ~(RADEON_PLL_DIV_SEL));
489 radeon_pll_errata_after_index(dev_priv);
494 RADEON_WRITE_PLL_P(dev_priv, RADEON_VCLK_ECP_CNTL,
495 RADEON_VCLK_SRC_SEL_CPUCLK,
496 ~(RADEON_VCLK_SRC_SEL_MASK));
497 RADEON_WRITE_PLL_P(dev_priv,
500 | RADEON_PPLL_ATOMIC_UPDATE_EN
501 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
502 | ((uint32_t)pll_gain << RADEON_PPLL_PVG_SHIFT),
504 | RADEON_PPLL_ATOMIC_UPDATE_EN
505 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
506 | RADEON_PPLL_PVG_MASK));
508 RADEON_WRITE_P(RADEON_CLOCK_CNTL_INDEX,
510 ~(RADEON_PLL_DIV_SEL));
511 radeon_pll_errata_after_index(dev_priv);
513 if (radeon_is_r300(dev_priv) ||
514 (dev_priv->chip_family == CHIP_RS300) ||
515 (dev_priv->chip_family == CHIP_RS400) ||
516 (dev_priv->chip_family == CHIP_RS480)) {
517 if (ppll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
518 /* When restoring console mode, use saved PPLL_REF_DIV
521 RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_REF_DIV,
525 /* R300 uses ref_div_acc field as real ref divider */
526 RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_REF_DIV,
527 (ppll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
528 ~R300_PPLL_REF_DIV_ACC_MASK);
531 RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_REF_DIV,
533 ~RADEON_PPLL_REF_DIV_MASK);
536 RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_DIV_3,
538 ~RADEON_PPLL_FB3_DIV_MASK);
540 RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_DIV_3,
542 ~RADEON_PPLL_POST3_DIV_MASK);
544 radeon_pll_write_update(dev);
545 radeon_pll_wait_for_read_update_complete(dev);
547 RADEON_WRITE_PLL(dev_priv, RADEON_HTOTAL_CNTL, htotal_cntl);
549 RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_CNTL,
553 | RADEON_PPLL_ATOMIC_UPDATE_EN
554 | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN));
556 DRM_DEBUG("Wrote: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
559 (unsigned)htotal_cntl,
560 RADEON_READ_PLL(dev_priv, RADEON_PPLL_CNTL));
561 DRM_DEBUG("Wrote: rd=%d, fd=%d, pd=%d\n",
562 ppll_ref_div & RADEON_PPLL_REF_DIV_MASK,
563 ppll_div_3 & RADEON_PPLL_FB3_DIV_MASK,
564 (ppll_div_3 & RADEON_PPLL_POST3_DIV_MASK) >> 16);
566 mdelay(50); /* Let the clock to lock */
568 RADEON_WRITE_PLL_P(dev_priv, RADEON_VCLK_ECP_CNTL,
569 RADEON_VCLK_SRC_SEL_PPLLCLK,
570 ~(RADEON_VCLK_SRC_SEL_MASK));
572 /*RADEON_WRITE_PLL(dev_priv, RADEON_VCLK_ECP_CNTL, state->vclk_ecp_cntl);*/
576 static bool radeon_set_crtc2_base(struct drm_crtc *crtc, int x, int y)
578 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
579 struct drm_device *dev = crtc->dev;
580 struct drm_radeon_private *dev_priv = dev->dev_private;
581 struct radeon_framebuffer *radeon_fb;
582 struct drm_radeon_gem_object *obj_priv;
583 uint32_t crtc2_offset, crtc2_offset_cntl, crtc2_tile_x0_y0 = 0;
586 radeon_fb = to_radeon_framebuffer(crtc->fb);
588 obj_priv = radeon_fb->obj->driver_private;
590 crtc2_offset = obj_priv->bo->offset;
592 crtc2_offset_cntl = 0;
596 if (radeon_is_r300(dev_priv))
597 crtc2_offset_cntl |= (R300_CRTC_X_Y_MODE_EN |
598 R300_CRTC_MICRO_TILE_BUFFER_DIS |
599 R300_CRTC_MACRO_TILE_EN);
601 crtc2_offset_cntl |= RADEON_CRTC_TILE_EN;
603 if (radeon_is_r300(dev_priv))
604 crtc2_offset_cntl &= ~(R300_CRTC_X_Y_MODE_EN |
605 R300_CRTC_MICRO_TILE_BUFFER_DIS |
606 R300_CRTC_MACRO_TILE_EN);
608 crtc2_offset_cntl &= ~RADEON_CRTC_TILE_EN;
611 base = obj_priv->bo->offset;
613 /* TODO more tiling */
615 if (radeon_is_r300(dev_priv)) {
616 crtc2_tile_x0_y0 = x | (y << 16);
619 int byteshift = crtc->fb->bits_per_pixel >> 4;
620 int tile_addr = (((y >> 3) * crtc->fb->width + x) >> (8 - byteshift)) << 11;
621 base += tile_addr + ((x << byteshift) % 256) + ((y % 8) << 8);
622 crtc2_offset_cntl |= (y % 16);
625 int offset = y * crtc->fb->pitch + x;
626 switch (crtc->fb->bits_per_pixel) {
645 /* update sarea TODO */
649 if (radeon_is_r300(dev_priv))
650 RADEON_WRITE(R300_CRTC2_TILE_X0_Y0, crtc2_tile_x0_y0);
651 RADEON_WRITE(RADEON_CRTC2_OFFSET_CNTL, crtc2_offset_cntl);
652 RADEON_WRITE(RADEON_CRTC2_OFFSET, crtc2_offset);
657 static bool radeon_set_crtc2_timing(struct drm_crtc *crtc, struct drm_display_mode *mode)
659 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
660 struct drm_device *dev = crtc->dev;
661 struct drm_radeon_private *dev_priv = dev->dev_private;
666 uint32_t crtc2_gen_cntl;
667 uint32_t crtc2_h_total_disp;
668 uint32_t crtc2_h_sync_strt_wid;
669 uint32_t crtc2_v_total_disp;
670 uint32_t crtc2_v_sync_strt_wid;
671 uint32_t crtc2_pitch;
672 uint32_t disp2_merge_cntl;
673 uint32_t fp_h2_sync_strt_wid;
674 uint32_t fp_v2_sync_strt_wid;
676 switch (crtc->fb->depth) {
695 ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
696 | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
698 hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
701 hsync_start = mode->crtc_hsync_start - 8;
703 crtc2_h_sync_strt_wid = ((hsync_start & 0x1fff)
704 | ((hsync_wid & 0x3f) << 16)
705 | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
706 ? RADEON_CRTC_H_SYNC_POL
709 /* This works for double scan mode. */
710 crtc2_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
711 | ((mode->crtc_vdisplay - 1) << 16));
713 vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
717 crtc2_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
718 | ((vsync_wid & 0x1f) << 16)
719 | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
720 ? RADEON_CRTC2_V_SYNC_POL
723 crtc2_pitch = (((crtc->fb->pitch * crtc->fb->bits_per_pixel) +
724 ((crtc->fb->bits_per_pixel * 8) -1)) /
725 (crtc->fb->bits_per_pixel * 8));
726 crtc2_pitch |= crtc2_pitch << 16;
728 /* check to see if TV DAC is enabled for another crtc and keep it enabled */
729 if (RADEON_READ(RADEON_CRTC2_GEN_CNTL) & RADEON_CRTC2_CRT2_ON)
730 crtc2_gen_cntl = RADEON_CRTC2_CRT2_ON;
734 crtc2_gen_cntl |= (RADEON_CRTC2_EN
736 | RADEON_CRTC2_VSYNC_DIS
737 | RADEON_CRTC2_HSYNC_DIS
738 | RADEON_CRTC2_DISP_DIS
739 | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
740 ? RADEON_CRTC2_DBL_SCAN_EN
742 | ((mode->flags & DRM_MODE_FLAG_CSYNC)
743 ? RADEON_CRTC2_CSYNC_EN
745 | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
746 ? RADEON_CRTC2_INTERLACE_EN
749 disp2_merge_cntl = RADEON_READ(RADEON_DISP2_MERGE_CNTL);
750 disp2_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
752 fp_h2_sync_strt_wid = crtc2_h_sync_strt_wid;
753 fp_v2_sync_strt_wid = crtc2_v_sync_strt_wid;
755 RADEON_WRITE(RADEON_CRTC2_GEN_CNTL,
756 crtc2_gen_cntl | RADEON_CRTC2_VSYNC_DIS |
757 RADEON_CRTC2_HSYNC_DIS | RADEON_CRTC2_DISP_DIS |
758 RADEON_CRTC2_DISP_REQ_EN_B);
760 RADEON_WRITE(RADEON_CRTC2_H_TOTAL_DISP, crtc2_h_total_disp);
761 RADEON_WRITE(RADEON_CRTC2_H_SYNC_STRT_WID, crtc2_h_sync_strt_wid);
762 RADEON_WRITE(RADEON_CRTC2_V_TOTAL_DISP, crtc2_v_total_disp);
763 RADEON_WRITE(RADEON_CRTC2_V_SYNC_STRT_WID, crtc2_v_sync_strt_wid);
765 RADEON_WRITE(RADEON_FP_H2_SYNC_STRT_WID, fp_h2_sync_strt_wid);
766 RADEON_WRITE(RADEON_FP_V2_SYNC_STRT_WID, fp_v2_sync_strt_wid);
768 RADEON_WRITE(RADEON_CRTC2_PITCH, crtc2_pitch);
769 RADEON_WRITE(RADEON_DISP2_MERGE_CNTL, disp2_merge_cntl);
771 RADEON_WRITE(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
777 static void radeon_set_pll2(struct drm_crtc *crtc, struct drm_display_mode *mode, int flags)
779 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
780 struct drm_device *dev = crtc->dev;
781 struct drm_radeon_private *dev_priv = dev->dev_private;
782 uint32_t feedback_div = 0;
783 uint32_t reference_div = 0;
784 uint32_t post_divider = 0;
788 uint32_t p2pll_ref_div;
789 uint32_t p2pll_div_0;
790 uint32_t htotal_cntl2;
791 uint32_t pixclks_cntl;
793 struct radeon_pll *pll = &dev_priv->mode_info.pll;
798 } *post_div, post_divs[] = {
799 /* From RAGE 128 VR/RAGE 128 GL Register
800 * Reference Manual (Technical Reference
801 * Manual P/N RRG-G04100-C Rev. 0.04), page
802 * 3-17 (PLL_DIV_[3:0]).
804 { 1, 0 }, /* VCLK_SRC */
805 { 2, 1 }, /* VCLK_SRC/2 */
806 { 4, 2 }, /* VCLK_SRC/4 */
807 { 8, 3 }, /* VCLK_SRC/8 */
808 { 3, 4 }, /* VCLK_SRC/3 */
809 { 6, 6 }, /* VCLK_SRC/6 */
810 { 12, 7 }, /* VCLK_SRC/12 */
815 if ((flags & RADEON_PLL_USE_BIOS_DIVS) && info->UseBiosDividers) {
816 p2pll_ref_div = info->RefDivider;
817 p2pll_div_0 = info->FeedbackDivider | (info->PostDivider << 16);
823 radeon_compute_pll(pll, mode->clock, &freq, &feedback_div, &reference_div, &post_divider, flags);
825 for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
826 if (post_div->divider == post_divider)
830 if (!post_div->divider) {
831 post_div = &post_divs[0];
834 DRM_DEBUG("dc=%u, fd=%d, rd=%d, pd=%d\n",
840 p2pll_ref_div = reference_div;
842 p2pll_div_0 = (feedback_div | (post_div->bitvalue << 16));
844 htotal_cntl2 = mode->htotal & 0x7;
846 pixclks_cntl = ((RADEON_READ_PLL(dev_priv, RADEON_PIXCLKS_CNTL) &
847 ~(RADEON_PIX2CLK_SRC_SEL_MASK)) |
848 RADEON_PIX2CLK_SRC_SEL_P2PLLCLK);
850 pll_gain = radeon_compute_pll_gain(dev_priv->mode_info.pll.reference_freq,
851 p2pll_ref_div & RADEON_P2PLL_REF_DIV_MASK,
852 p2pll_div_0 & RADEON_P2PLL_FB0_DIV_MASK);
855 RADEON_WRITE_PLL_P(dev_priv, RADEON_PIXCLKS_CNTL,
856 RADEON_PIX2CLK_SRC_SEL_CPUCLK,
857 ~(RADEON_PIX2CLK_SRC_SEL_MASK));
859 RADEON_WRITE_PLL_P(dev_priv,
862 | RADEON_P2PLL_ATOMIC_UPDATE_EN
863 | ((uint32_t)pll_gain << RADEON_P2PLL_PVG_SHIFT),
865 | RADEON_P2PLL_ATOMIC_UPDATE_EN
866 | RADEON_P2PLL_PVG_MASK));
869 RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_REF_DIV,
871 ~RADEON_P2PLL_REF_DIV_MASK);
873 RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_DIV_0,
875 ~RADEON_P2PLL_FB0_DIV_MASK);
877 RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_DIV_0,
879 ~RADEON_P2PLL_POST0_DIV_MASK);
881 radeon_pll2_write_update(dev);
882 radeon_pll2_wait_for_read_update_complete(dev);
884 RADEON_WRITE_PLL(dev_priv, RADEON_HTOTAL2_CNTL, htotal_cntl2);
886 RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_CNTL,
890 | RADEON_P2PLL_ATOMIC_UPDATE_EN));
892 DRM_DEBUG("Wrote2: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
893 (unsigned)p2pll_ref_div,
894 (unsigned)p2pll_div_0,
895 (unsigned)htotal_cntl2,
896 RADEON_READ_PLL(dev_priv, RADEON_P2PLL_CNTL));
897 DRM_DEBUG("Wrote2: rd=%u, fd=%u, pd=%u\n",
898 (unsigned)p2pll_ref_div & RADEON_P2PLL_REF_DIV_MASK,
899 (unsigned)p2pll_div_0 & RADEON_P2PLL_FB0_DIV_MASK,
900 (unsigned)((p2pll_div_0 &
901 RADEON_P2PLL_POST0_DIV_MASK) >>16));
903 mdelay(50); /* Let the clock to lock */
905 RADEON_WRITE_PLL_P(dev_priv, RADEON_PIXCLKS_CNTL,
906 RADEON_PIX2CLK_SRC_SEL_P2PLLCLK,
907 ~(RADEON_PIX2CLK_SRC_SEL_MASK));
909 RADEON_WRITE_PLL(dev_priv, RADEON_PIXCLKS_CNTL, pixclks_cntl);
913 static bool radeon_crtc_mode_fixup(struct drm_crtc *crtc,
914 struct drm_display_mode *mode,
915 struct drm_display_mode *adjusted_mode)
920 static void radeon_crtc_mode_set(struct drm_crtc *crtc,
921 struct drm_display_mode *mode,
922 struct drm_display_mode *adjusted_mode,
925 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
926 struct drm_device *dev = crtc->dev;
927 struct drm_radeon_private *dev_priv = dev->dev_private;
928 struct drm_encoder *encoder;
929 int pll_flags = RADEON_PLL_LEGACY | RADEON_PLL_PREFER_LOW_REF_DIV;
932 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
933 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
935 if (encoder->crtc == crtc) {
936 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
937 pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
938 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
939 pll_flags |= RADEON_PLL_USE_BIOS_DIVS | RADEON_PLL_USE_REF_DIV;
945 switch(radeon_crtc->crtc_id) {
947 radeon_set_crtc1_timing(crtc, adjusted_mode);
948 radeon_set_pll1(crtc, adjusted_mode, pll_flags);
951 radeon_set_crtc2_timing(crtc, adjusted_mode);
952 radeon_set_pll2(crtc, adjusted_mode, pll_flags);
958 void radeon_crtc_set_base(struct drm_crtc *crtc, int x, int y)
960 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
962 switch(radeon_crtc->crtc_id) {
964 radeon_set_crtc1_base(crtc, x, y);
967 radeon_set_crtc2_base(crtc, x, y);
973 static void radeon_crtc_prepare(struct drm_crtc *crtc)
975 radeon_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
978 static void radeon_crtc_commit(struct drm_crtc *crtc)
980 radeon_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
983 static const struct drm_crtc_helper_funcs legacy_helper_funcs = {
984 .dpms = radeon_crtc_dpms,
985 .mode_fixup = radeon_crtc_mode_fixup,
986 .mode_set = radeon_crtc_mode_set,
987 .mode_set_base = radeon_crtc_set_base,
988 .prepare = radeon_crtc_prepare,
989 .commit = radeon_crtc_commit,
993 void radeon_legacy_init_crtc(struct drm_device *dev,
994 struct radeon_crtc *radeon_crtc)
996 drm_crtc_helper_add(&radeon_crtc->base, &legacy_helper_funcs);