blob: ca04a1bdb75b3121a01bcab69c1f4bba94213b82 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/drm_crtc_helper.h>
28#include <drm/radeon_drm.h>
Ben Skeggs68adac52010-04-28 11:46:42 +100029#include <drm/drm_fixed.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "radeon.h"
31#include "atom.h"
32#include "atom-bits.h"
33
Jerome Glissec93bb852009-07-13 21:04:08 +020034static void atombios_overscan_setup(struct drm_crtc *crtc,
35 struct drm_display_mode *mode,
36 struct drm_display_mode *adjusted_mode)
37{
38 struct drm_device *dev = crtc->dev;
39 struct radeon_device *rdev = dev->dev_private;
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 SET_CRTC_OVERSCAN_PS_ALLOCATION args;
42 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
43 int a1, a2;
44
45 memset(&args, 0, sizeof(args));
46
Jerome Glissec93bb852009-07-13 21:04:08 +020047 args.ucCRTC = radeon_crtc->crtc_id;
48
49 switch (radeon_crtc->rmx_type) {
50 case RMX_CENTER:
51 args.usOverscanTop = (adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2;
52 args.usOverscanBottom = (adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2;
53 args.usOverscanLeft = (adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2;
54 args.usOverscanRight = (adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2;
Jerome Glissec93bb852009-07-13 21:04:08 +020055 break;
56 case RMX_ASPECT:
57 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
58 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
59
60 if (a1 > a2) {
61 args.usOverscanLeft = (adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2;
62 args.usOverscanRight = (adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2;
63 } else if (a2 > a1) {
64 args.usOverscanLeft = (adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2;
65 args.usOverscanRight = (adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2;
66 }
Jerome Glissec93bb852009-07-13 21:04:08 +020067 break;
68 case RMX_FULL:
69 default:
Alex Deucher5b1714d2010-08-03 19:59:20 -040070 args.usOverscanRight = radeon_crtc->h_border;
71 args.usOverscanLeft = radeon_crtc->h_border;
72 args.usOverscanBottom = radeon_crtc->v_border;
73 args.usOverscanTop = radeon_crtc->v_border;
Jerome Glissec93bb852009-07-13 21:04:08 +020074 break;
75 }
Alex Deucher5b1714d2010-08-03 19:59:20 -040076 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glissec93bb852009-07-13 21:04:08 +020077}
78
79static void atombios_scaler_setup(struct drm_crtc *crtc)
80{
81 struct drm_device *dev = crtc->dev;
82 struct radeon_device *rdev = dev->dev_private;
83 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
84 ENABLE_SCALER_PS_ALLOCATION args;
85 int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
Dave Airlie4ce001a2009-08-13 16:32:14 +100086
Jerome Glissec93bb852009-07-13 21:04:08 +020087 /* fixme - fill in enc_priv for atom dac */
88 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100089 bool is_tv = false, is_cv = false;
90 struct drm_encoder *encoder;
Jerome Glissec93bb852009-07-13 21:04:08 +020091
92 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
93 return;
94
Dave Airlie4ce001a2009-08-13 16:32:14 +100095 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
96 /* find tv std */
97 if (encoder->crtc == crtc) {
98 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
99 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
100 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
101 tv_std = tv_dac->tv_std;
102 is_tv = true;
103 }
104 }
105 }
106
Jerome Glissec93bb852009-07-13 21:04:08 +0200107 memset(&args, 0, sizeof(args));
108
109 args.ucScaler = radeon_crtc->crtc_id;
110
Dave Airlie4ce001a2009-08-13 16:32:14 +1000111 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200112 switch (tv_std) {
113 case TV_STD_NTSC:
114 default:
115 args.ucTVStandard = ATOM_TV_NTSC;
116 break;
117 case TV_STD_PAL:
118 args.ucTVStandard = ATOM_TV_PAL;
119 break;
120 case TV_STD_PAL_M:
121 args.ucTVStandard = ATOM_TV_PALM;
122 break;
123 case TV_STD_PAL_60:
124 args.ucTVStandard = ATOM_TV_PAL60;
125 break;
126 case TV_STD_NTSC_J:
127 args.ucTVStandard = ATOM_TV_NTSCJ;
128 break;
129 case TV_STD_SCART_PAL:
130 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
131 break;
132 case TV_STD_SECAM:
133 args.ucTVStandard = ATOM_TV_SECAM;
134 break;
135 case TV_STD_PAL_CN:
136 args.ucTVStandard = ATOM_TV_PALCN;
137 break;
138 }
139 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000140 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200141 args.ucTVStandard = ATOM_TV_CV;
142 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
143 } else {
144 switch (radeon_crtc->rmx_type) {
145 case RMX_FULL:
146 args.ucEnable = ATOM_SCALER_EXPANSION;
147 break;
148 case RMX_CENTER:
149 args.ucEnable = ATOM_SCALER_CENTER;
150 break;
151 case RMX_ASPECT:
152 args.ucEnable = ATOM_SCALER_EXPANSION;
153 break;
154 default:
155 if (ASIC_IS_AVIVO(rdev))
156 args.ucEnable = ATOM_SCALER_DISABLE;
157 else
158 args.ucEnable = ATOM_SCALER_CENTER;
159 break;
160 }
161 }
162 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000163 if ((is_tv || is_cv)
164 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
165 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200166 }
167}
168
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
170{
171 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
172 struct drm_device *dev = crtc->dev;
173 struct radeon_device *rdev = dev->dev_private;
174 int index =
175 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
176 ENABLE_CRTC_PS_ALLOCATION args;
177
178 memset(&args, 0, sizeof(args));
179
180 args.ucCRTC = radeon_crtc->crtc_id;
181 args.ucEnable = lock;
182
183 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
184}
185
186static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
187{
188 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
189 struct drm_device *dev = crtc->dev;
190 struct radeon_device *rdev = dev->dev_private;
191 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
192 ENABLE_CRTC_PS_ALLOCATION args;
193
194 memset(&args, 0, sizeof(args));
195
196 args.ucCRTC = radeon_crtc->crtc_id;
197 args.ucEnable = state;
198
199 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
200}
201
202static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
203{
204 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
205 struct drm_device *dev = crtc->dev;
206 struct radeon_device *rdev = dev->dev_private;
207 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
208 ENABLE_CRTC_PS_ALLOCATION args;
209
210 memset(&args, 0, sizeof(args));
211
212 args.ucCRTC = radeon_crtc->crtc_id;
213 args.ucEnable = state;
214
215 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
216}
217
218static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
219{
220 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
221 struct drm_device *dev = crtc->dev;
222 struct radeon_device *rdev = dev->dev_private;
223 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
224 BLANK_CRTC_PS_ALLOCATION args;
225
226 memset(&args, 0, sizeof(args));
227
228 args.ucCRTC = radeon_crtc->crtc_id;
229 args.ucBlanking = state;
230
231 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
232}
233
234void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
235{
236 struct drm_device *dev = crtc->dev;
237 struct radeon_device *rdev = dev->dev_private;
Alex Deucher500b7582009-12-02 11:46:52 -0500238 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200239
240 switch (mode) {
241 case DRM_MODE_DPMS_ON:
Alex Deucherd7311172010-05-03 01:13:14 -0400242 radeon_crtc->enabled = true;
243 /* adjust pm to dpms changes BEFORE enabling crtcs */
244 radeon_pm_compute_clocks(rdev);
Alex Deucher37b43902010-02-09 12:04:43 -0500245 atombios_enable_crtc(crtc, ATOM_ENABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200246 if (ASIC_IS_DCE3(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500247 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
248 atombios_blank_crtc(crtc, ATOM_DISABLE);
Alex Deucher45f9a392010-03-24 13:55:51 -0400249 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
Alex Deucher500b7582009-12-02 11:46:52 -0500250 radeon_crtc_load_lut(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200251 break;
252 case DRM_MODE_DPMS_STANDBY:
253 case DRM_MODE_DPMS_SUSPEND:
254 case DRM_MODE_DPMS_OFF:
Alex Deucher45f9a392010-03-24 13:55:51 -0400255 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
Alex Deucher37b43902010-02-09 12:04:43 -0500256 atombios_blank_crtc(crtc, ATOM_ENABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200257 if (ASIC_IS_DCE3(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500258 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
259 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400260 radeon_crtc->enabled = false;
Alex Deucherd7311172010-05-03 01:13:14 -0400261 /* adjust pm to dpms changes AFTER disabling crtcs */
262 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200263 break;
264 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200265}
266
267static void
268atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400269 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400271 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200272 struct drm_device *dev = crtc->dev;
273 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400274 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200275 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400276 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200277
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400278 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400279 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400280 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400281 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
282 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400283 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400284 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400285 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400286 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400287 args.usH_SyncWidth =
288 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
289 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400290 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400291 args.usV_SyncWidth =
292 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400293 args.ucH_Border = radeon_crtc->h_border;
294 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400295
296 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
297 misc |= ATOM_VSYNC_POLARITY;
298 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
299 misc |= ATOM_HSYNC_POLARITY;
300 if (mode->flags & DRM_MODE_FLAG_CSYNC)
301 misc |= ATOM_COMPOSITESYNC;
302 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
303 misc |= ATOM_INTERLACE;
304 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
305 misc |= ATOM_DOUBLE_CLOCK_MODE;
306
307 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
308 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200309
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400310 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200311}
312
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400313static void atombios_crtc_set_timing(struct drm_crtc *crtc,
314 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200315{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400316 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200317 struct drm_device *dev = crtc->dev;
318 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400319 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200320 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400321 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200322
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400323 memset(&args, 0, sizeof(args));
324 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
325 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
326 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
327 args.usH_SyncWidth =
328 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
329 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
330 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
331 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
332 args.usV_SyncWidth =
333 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
334
Alex Deucher54bfe492010-09-03 15:52:53 -0400335 args.ucOverscanRight = radeon_crtc->h_border;
336 args.ucOverscanLeft = radeon_crtc->h_border;
337 args.ucOverscanBottom = radeon_crtc->v_border;
338 args.ucOverscanTop = radeon_crtc->v_border;
339
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400340 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
341 misc |= ATOM_VSYNC_POLARITY;
342 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
343 misc |= ATOM_HSYNC_POLARITY;
344 if (mode->flags & DRM_MODE_FLAG_CSYNC)
345 misc |= ATOM_COMPOSITESYNC;
346 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
347 misc |= ATOM_INTERLACE;
348 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
349 misc |= ATOM_DOUBLE_CLOCK_MODE;
350
351 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
352 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200353
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400354 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200355}
356
Alex Deucherb7922102010-03-06 10:57:30 -0500357static void atombios_disable_ss(struct drm_crtc *crtc)
358{
359 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
360 struct drm_device *dev = crtc->dev;
361 struct radeon_device *rdev = dev->dev_private;
362 u32 ss_cntl;
363
364 if (ASIC_IS_DCE4(rdev)) {
365 switch (radeon_crtc->pll_id) {
366 case ATOM_PPLL1:
367 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
368 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
369 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
370 break;
371 case ATOM_PPLL2:
372 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
373 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
374 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
375 break;
376 case ATOM_DCPLL:
377 case ATOM_PPLL_INVALID:
378 return;
379 }
380 } else if (ASIC_IS_AVIVO(rdev)) {
381 switch (radeon_crtc->pll_id) {
382 case ATOM_PPLL1:
383 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
384 ss_cntl &= ~1;
385 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
386 break;
387 case ATOM_PPLL2:
388 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
389 ss_cntl &= ~1;
390 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
391 break;
392 case ATOM_DCPLL:
393 case ATOM_PPLL_INVALID:
394 return;
395 }
396 }
397}
398
399
Alex Deucher26b9fc32010-02-01 16:39:11 -0500400union atom_enable_ss {
401 ENABLE_LVDS_SS_PARAMETERS legacy;
402 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
403};
404
Alex Deucherb7922102010-03-06 10:57:30 -0500405static void atombios_enable_ss(struct drm_crtc *crtc)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400406{
407 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
408 struct drm_device *dev = crtc->dev;
409 struct radeon_device *rdev = dev->dev_private;
410 struct drm_encoder *encoder = NULL;
411 struct radeon_encoder *radeon_encoder = NULL;
412 struct radeon_encoder_atom_dig *dig = NULL;
413 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500414 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400415 uint16_t percentage = 0;
416 uint8_t type = 0, step = 0, delay = 0, range = 0;
417
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500418 /* XXX add ss support for DCE4 */
419 if (ASIC_IS_DCE4(rdev))
420 return;
421
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400422 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
423 if (encoder->crtc == crtc) {
424 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400425 /* only enable spread spectrum on LVDS */
Alex Deucherd11aa882009-10-28 00:51:20 -0400426 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
427 dig = radeon_encoder->enc_priv;
428 if (dig && dig->ss) {
429 percentage = dig->ss->percentage;
430 type = dig->ss->type;
431 step = dig->ss->step;
432 delay = dig->ss->delay;
433 range = dig->ss->range;
Alex Deucherb7922102010-03-06 10:57:30 -0500434 } else
Alex Deucherd11aa882009-10-28 00:51:20 -0400435 return;
Alex Deucherb7922102010-03-06 10:57:30 -0500436 } else
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400437 return;
438 break;
439 }
440 }
441
442 if (!radeon_encoder)
443 return;
444
Alex Deucher26b9fc32010-02-01 16:39:11 -0500445 memset(&args, 0, sizeof(args));
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400446 if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher26b9fc32010-02-01 16:39:11 -0500447 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(percentage);
448 args.v1.ucSpreadSpectrumType = type;
449 args.v1.ucSpreadSpectrumStep = step;
450 args.v1.ucSpreadSpectrumDelay = delay;
451 args.v1.ucSpreadSpectrumRange = range;
452 args.v1.ucPpll = radeon_crtc->crtc_id ? ATOM_PPLL2 : ATOM_PPLL1;
Alex Deucherb7922102010-03-06 10:57:30 -0500453 args.v1.ucEnable = ATOM_ENABLE;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400454 } else {
Alex Deucher26b9fc32010-02-01 16:39:11 -0500455 args.legacy.usSpreadSpectrumPercentage = cpu_to_le16(percentage);
456 args.legacy.ucSpreadSpectrumType = type;
457 args.legacy.ucSpreadSpectrumStepSize_Delay = (step & 3) << 2;
458 args.legacy.ucSpreadSpectrumStepSize_Delay |= (delay & 7) << 4;
Alex Deucherb7922102010-03-06 10:57:30 -0500459 args.legacy.ucEnable = ATOM_ENABLE;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400460 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500461 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400462}
463
Alex Deucher4eaeca32010-01-19 17:32:27 -0500464union adjust_pixel_clock {
465 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500466 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500467};
468
469static u32 atombios_adjust_pll(struct drm_crtc *crtc,
470 struct drm_display_mode *mode,
471 struct radeon_pll *pll)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200472{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200473 struct drm_device *dev = crtc->dev;
474 struct radeon_device *rdev = dev->dev_private;
475 struct drm_encoder *encoder = NULL;
476 struct radeon_encoder *radeon_encoder = NULL;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500477 u32 adjusted_clock = mode->clock;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500478 int encoder_mode = 0;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400479 u32 dp_clock = mode->clock;
480 int bpc = 8;
Alex Deucherfc103322010-01-19 17:16:10 -0500481
Alex Deucher4eaeca32010-01-19 17:32:27 -0500482 /* reset the pll flags */
483 pll->flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200484
Alex Deucher7c27f872010-02-02 12:05:01 -0500485 /* select the PLL algo */
486 if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher383be5d2010-02-23 03:24:38 -0500487 if (radeon_new_pll == 0)
488 pll->algo = PLL_ALGO_LEGACY;
489 else
490 pll->algo = PLL_ALGO_NEW;
491 } else {
492 if (radeon_new_pll == 1)
493 pll->algo = PLL_ALGO_NEW;
Alex Deucher7c27f872010-02-02 12:05:01 -0500494 else
495 pll->algo = PLL_ALGO_LEGACY;
Alex Deucher383be5d2010-02-23 03:24:38 -0500496 }
Alex Deucher7c27f872010-02-02 12:05:01 -0500497
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200498 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400499 if ((rdev->family == CHIP_RS600) ||
500 (rdev->family == CHIP_RS690) ||
501 (rdev->family == CHIP_RS740))
Alex Deucher2ff776c2010-06-08 19:44:36 -0400502 pll->flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
Alex Deucherfc103322010-01-19 17:16:10 -0500503 RADEON_PLL_PREFER_CLOSEST_LOWER);
Alex Deucherf28488c2010-09-29 11:37:40 -0400504 } else
Alex Deucherfc103322010-01-19 17:16:10 -0500505 pll->flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200506
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200507 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
508 if (encoder->crtc == crtc) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500509 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500510 encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400511 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) {
512 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
513 if (connector) {
514 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
515 struct radeon_connector_atom_dig *dig_connector =
516 radeon_connector->con_priv;
517
518 dp_clock = dig_connector->dp_clock;
519 }
520 }
521
Alex Deucher4eaeca32010-01-19 17:32:27 -0500522 if (ASIC_IS_AVIVO(rdev)) {
523 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
524 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
525 adjusted_clock = mode->clock * 2;
Alex Deuchera1a4b232010-04-09 15:31:56 -0400526 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT)) {
527 pll->algo = PLL_ALGO_LEGACY;
528 pll->flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
529 }
Alex Deucherf90087e2010-09-07 11:42:45 -0400530 /* There is some evidence (often anecdotal) that RV515/RV620 LVDS
Alex Deucher0d9958b2010-09-01 12:03:37 -0400531 * (on some boards at least) prefers the legacy algo. I'm not
532 * sure whether this should handled generically or on a
533 * case-by-case quirk basis. Both algos should work fine in the
534 * majority of cases.
535 */
536 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT)) &&
Alex Deucherf90087e2010-09-07 11:42:45 -0400537 ((rdev->family == CHIP_RV515) ||
538 (rdev->family == CHIP_RV620))) {
Alex Deucher0d9958b2010-09-01 12:03:37 -0400539 /* allow the user to overrride just in case */
540 if (radeon_new_pll == 1)
541 pll->algo = PLL_ALGO_NEW;
542 else
543 pll->algo = PLL_ALGO_LEGACY;
544 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500545 } else {
546 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
Alex Deucherfc103322010-01-19 17:16:10 -0500547 pll->flags |= RADEON_PLL_NO_ODD_POST_DIV;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500548 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
Alex Deucherfc103322010-01-19 17:16:10 -0500549 pll->flags |= RADEON_PLL_USE_REF_DIV;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200550 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000551 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200552 }
553 }
554
Alex Deucher2606c882009-10-08 13:36:21 -0400555 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
556 * accordingly based on the encoder/transmitter to work around
557 * special hw requirements.
558 */
559 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500560 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500561 u8 frev, crev;
562 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400563
Alex Deucher2606c882009-10-08 13:36:21 -0400564 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400565 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
566 &crev))
567 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500568
569 memset(&args, 0, sizeof(args));
570
571 switch (frev) {
572 case 1:
573 switch (crev) {
574 case 1:
575 case 2:
576 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
577 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500578 args.v1.ucEncodeMode = encoder_mode;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400579 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
580 /* may want to enable SS on DP eventually */
581 /* args.v1.ucConfig |=
582 ADJUST_DISPLAY_CONFIG_SS_ENABLE;*/
583 } else if (encoder_mode == ATOM_ENCODER_MODE_LVDS) {
584 args.v1.ucConfig |=
585 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
586 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500587
588 atom_execute_table(rdev->mode_info.atom_context,
589 index, (uint32_t *)&args);
590 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
591 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500592 case 3:
593 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
594 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
595 args.v3.sInput.ucEncodeMode = encoder_mode;
596 args.v3.sInput.ucDispPllConfig = 0;
597 if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
598 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
599
Alex Deucherfbee67a2010-08-16 12:44:47 -0400600 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
601 /* may want to enable SS on DP/eDP eventually */
602 /*args.v3.sInput.ucDispPllConfig |=
603 DISPPLL_CONFIG_SS_ENABLE;*/
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500604 args.v3.sInput.ucDispPllConfig |=
605 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400606 /* 16200 or 27000 */
607 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
608 } else {
609 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
610 /* deep color support */
611 args.v3.sInput.usPixelClock =
612 cpu_to_le16((mode->clock * bpc / 8) / 10);
613 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500614 if (dig->coherent_mode)
615 args.v3.sInput.ucDispPllConfig |=
616 DISPPLL_CONFIG_COHERENT_MODE;
617 if (mode->clock > 165000)
618 args.v3.sInput.ucDispPllConfig |=
619 DISPPLL_CONFIG_DUAL_LINK;
620 }
621 } else if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
Alex Deucherfbee67a2010-08-16 12:44:47 -0400622 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
623 /* may want to enable SS on DP/eDP eventually */
624 /*args.v3.sInput.ucDispPllConfig |=
625 DISPPLL_CONFIG_SS_ENABLE;*/
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500626 args.v3.sInput.ucDispPllConfig |=
Alex Deucher9f998ad2010-03-29 21:37:08 -0400627 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400628 /* 16200 or 27000 */
629 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
630 } else if (encoder_mode == ATOM_ENCODER_MODE_LVDS) {
631 /* want to enable SS on LVDS eventually */
632 /*args.v3.sInput.ucDispPllConfig |=
633 DISPPLL_CONFIG_SS_ENABLE;*/
634 } else {
Alex Deucher9f998ad2010-03-29 21:37:08 -0400635 if (mode->clock > 165000)
636 args.v3.sInput.ucDispPllConfig |=
637 DISPPLL_CONFIG_DUAL_LINK;
638 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500639 }
640 atom_execute_table(rdev->mode_info.atom_context,
641 index, (uint32_t *)&args);
642 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
643 if (args.v3.sOutput.ucRefDiv) {
644 pll->flags |= RADEON_PLL_USE_REF_DIV;
645 pll->reference_div = args.v3.sOutput.ucRefDiv;
646 }
647 if (args.v3.sOutput.ucPostDiv) {
648 pll->flags |= RADEON_PLL_USE_POST_DIV;
649 pll->post_div = args.v3.sOutput.ucPostDiv;
650 }
651 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500652 default:
653 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
654 return adjusted_clock;
655 }
656 break;
657 default:
658 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
659 return adjusted_clock;
660 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400661 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500662 return adjusted_clock;
663}
664
665union set_pixel_clock {
666 SET_PIXEL_CLOCK_PS_ALLOCATION base;
667 PIXEL_CLOCK_PARAMETERS v1;
668 PIXEL_CLOCK_PARAMETERS_V2 v2;
669 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500670 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500671};
672
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500673static void atombios_crtc_set_dcpll(struct drm_crtc *crtc)
674{
675 struct drm_device *dev = crtc->dev;
676 struct radeon_device *rdev = dev->dev_private;
677 u8 frev, crev;
678 int index;
679 union set_pixel_clock args;
680
681 memset(&args, 0, sizeof(args));
682
683 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400684 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
685 &crev))
686 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500687
688 switch (frev) {
689 case 1:
690 switch (crev) {
691 case 5:
692 /* if the default dcpll clock is specified,
693 * SetPixelClock provides the dividers
694 */
695 args.v5.ucCRTC = ATOM_CRTC_INVALID;
696 args.v5.usPixelClock = rdev->clock.default_dispclk;
697 args.v5.ucPpll = ATOM_DCPLL;
698 break;
699 default:
700 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
701 return;
702 }
703 break;
704 default:
705 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
706 return;
707 }
708 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
709}
710
Alex Deucher37f90032010-06-11 17:58:38 -0400711static void atombios_crtc_program_pll(struct drm_crtc *crtc,
712 int crtc_id,
713 int pll_id,
714 u32 encoder_mode,
715 u32 encoder_id,
716 u32 clock,
717 u32 ref_div,
718 u32 fb_div,
719 u32 frac_fb_div,
720 u32 post_div)
721{
722 struct drm_device *dev = crtc->dev;
723 struct radeon_device *rdev = dev->dev_private;
724 u8 frev, crev;
725 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
726 union set_pixel_clock args;
727
728 memset(&args, 0, sizeof(args));
729
730 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
731 &crev))
732 return;
733
734 switch (frev) {
735 case 1:
736 switch (crev) {
737 case 1:
738 if (clock == ATOM_DISABLE)
739 return;
740 args.v1.usPixelClock = cpu_to_le16(clock / 10);
741 args.v1.usRefDiv = cpu_to_le16(ref_div);
742 args.v1.usFbDiv = cpu_to_le16(fb_div);
743 args.v1.ucFracFbDiv = frac_fb_div;
744 args.v1.ucPostDiv = post_div;
745 args.v1.ucPpll = pll_id;
746 args.v1.ucCRTC = crtc_id;
747 args.v1.ucRefDivSrc = 1;
748 break;
749 case 2:
750 args.v2.usPixelClock = cpu_to_le16(clock / 10);
751 args.v2.usRefDiv = cpu_to_le16(ref_div);
752 args.v2.usFbDiv = cpu_to_le16(fb_div);
753 args.v2.ucFracFbDiv = frac_fb_div;
754 args.v2.ucPostDiv = post_div;
755 args.v2.ucPpll = pll_id;
756 args.v2.ucCRTC = crtc_id;
757 args.v2.ucRefDivSrc = 1;
758 break;
759 case 3:
760 args.v3.usPixelClock = cpu_to_le16(clock / 10);
761 args.v3.usRefDiv = cpu_to_le16(ref_div);
762 args.v3.usFbDiv = cpu_to_le16(fb_div);
763 args.v3.ucFracFbDiv = frac_fb_div;
764 args.v3.ucPostDiv = post_div;
765 args.v3.ucPpll = pll_id;
766 args.v3.ucMiscInfo = (pll_id << 2);
767 args.v3.ucTransmitterId = encoder_id;
768 args.v3.ucEncoderMode = encoder_mode;
769 break;
770 case 5:
771 args.v5.ucCRTC = crtc_id;
772 args.v5.usPixelClock = cpu_to_le16(clock / 10);
773 args.v5.ucRefDiv = ref_div;
774 args.v5.usFbDiv = cpu_to_le16(fb_div);
775 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
776 args.v5.ucPostDiv = post_div;
777 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
778 args.v5.ucTransmitterID = encoder_id;
779 args.v5.ucEncoderMode = encoder_mode;
780 args.v5.ucPpll = pll_id;
781 break;
782 default:
783 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
784 return;
785 }
786 break;
787 default:
788 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
789 return;
790 }
791
792 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
793}
794
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500795static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -0500796{
797 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
798 struct drm_device *dev = crtc->dev;
799 struct radeon_device *rdev = dev->dev_private;
800 struct drm_encoder *encoder = NULL;
801 struct radeon_encoder *radeon_encoder = NULL;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500802 u32 pll_clock = mode->clock;
803 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
804 struct radeon_pll *pll;
805 u32 adjusted_clock;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500806 int encoder_mode = 0;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500807
Alex Deucher4eaeca32010-01-19 17:32:27 -0500808 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
809 if (encoder->crtc == crtc) {
810 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500811 encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500812 break;
813 }
814 }
815
816 if (!radeon_encoder)
817 return;
818
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500819 switch (radeon_crtc->pll_id) {
820 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -0500821 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500822 break;
823 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -0500824 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500825 break;
826 case ATOM_DCPLL:
827 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +1000828 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500829 pll = &rdev->clock.dcpll;
830 break;
831 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500832
833 /* adjust pixel clock as needed */
834 adjusted_clock = atombios_adjust_pll(crtc, mode, pll);
Alex Deucher2606c882009-10-08 13:36:21 -0400835
Alex Deucher7c27f872010-02-02 12:05:01 -0500836 radeon_compute_pll(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
837 &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200838
Alex Deucher37f90032010-06-11 17:58:38 -0400839 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
840 encoder_mode, radeon_encoder->encoder_id, mode->clock,
841 ref_div, fb_div, frac_fb_div, post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200842
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200843}
844
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500845static int evergreen_crtc_set_base(struct drm_crtc *crtc, int x, int y,
846 struct drm_framebuffer *old_fb)
847{
848 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
849 struct drm_device *dev = crtc->dev;
850 struct radeon_device *rdev = dev->dev_private;
851 struct radeon_framebuffer *radeon_fb;
852 struct drm_gem_object *obj;
853 struct radeon_bo *rbo;
854 uint64_t fb_location;
855 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
856 int r;
857
858 /* no fb bound */
859 if (!crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000860 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500861 return 0;
862 }
863
864 radeon_fb = to_radeon_framebuffer(crtc->fb);
865
866 /* Pin framebuffer & get tilling informations */
867 obj = radeon_fb->obj;
868 rbo = obj->driver_private;
869 r = radeon_bo_reserve(rbo, false);
870 if (unlikely(r != 0))
871 return r;
872 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
873 if (unlikely(r != 0)) {
874 radeon_bo_unreserve(rbo);
875 return -EINVAL;
876 }
877 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
878 radeon_bo_unreserve(rbo);
879
880 switch (crtc->fb->bits_per_pixel) {
881 case 8:
882 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
883 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
884 break;
885 case 15:
886 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
887 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
888 break;
889 case 16:
890 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
891 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
892 break;
893 case 24:
894 case 32:
895 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
896 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
897 break;
898 default:
899 DRM_ERROR("Unsupported screen depth %d\n",
900 crtc->fb->bits_per_pixel);
901 return -EINVAL;
902 }
903
Alex Deucher97d66322010-05-20 12:12:48 -0400904 if (tiling_flags & RADEON_TILING_MACRO)
905 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
906 else if (tiling_flags & RADEON_TILING_MICRO)
907 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
908
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500909 switch (radeon_crtc->crtc_id) {
910 case 0:
911 WREG32(AVIVO_D1VGA_CONTROL, 0);
912 break;
913 case 1:
914 WREG32(AVIVO_D2VGA_CONTROL, 0);
915 break;
916 case 2:
917 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
918 break;
919 case 3:
920 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
921 break;
922 case 4:
923 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
924 break;
925 case 5:
926 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
927 break;
928 default:
929 break;
930 }
931
932 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
933 upper_32_bits(fb_location));
934 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
935 upper_32_bits(fb_location));
936 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
937 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
938 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
939 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
940 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
941
942 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
943 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
944 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
945 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
946 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, crtc->fb->width);
947 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, crtc->fb->height);
948
949 fb_pitch_pixels = crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8);
950 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
951 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
952
953 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
954 crtc->mode.vdisplay);
955 x &= ~3;
956 y &= ~1;
957 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
958 (x << 16) | y);
959 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
960 (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
961
962 if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE)
963 WREG32(EVERGREEN_DATA_FORMAT + radeon_crtc->crtc_offset,
964 EVERGREEN_INTERLEAVE_EN);
965 else
966 WREG32(EVERGREEN_DATA_FORMAT + radeon_crtc->crtc_offset, 0);
967
968 if (old_fb && old_fb != crtc->fb) {
969 radeon_fb = to_radeon_framebuffer(old_fb);
970 rbo = radeon_fb->obj->driver_private;
971 r = radeon_bo_reserve(rbo, false);
972 if (unlikely(r != 0))
973 return r;
974 radeon_bo_unpin(rbo);
975 radeon_bo_unreserve(rbo);
976 }
977
978 /* Bytes per pixel may have changed */
979 radeon_bandwidth_update(rdev);
980
981 return 0;
982}
983
Alex Deucher54f088a2010-01-19 16:34:01 -0500984static int avivo_crtc_set_base(struct drm_crtc *crtc, int x, int y,
985 struct drm_framebuffer *old_fb)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200986{
987 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
988 struct drm_device *dev = crtc->dev;
989 struct radeon_device *rdev = dev->dev_private;
990 struct radeon_framebuffer *radeon_fb;
991 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100992 struct radeon_bo *rbo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200993 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +1000994 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse4c788672009-11-20 14:29:23 +0100995 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200996
Jerome Glisse2de3b482009-11-17 14:08:55 -0800997 /* no fb bound */
998 if (!crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000999 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001000 return 0;
1001 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001002
1003 radeon_fb = to_radeon_framebuffer(crtc->fb);
1004
Jerome Glisse4c788672009-11-20 14:29:23 +01001005 /* Pin framebuffer & get tilling informations */
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001006 obj = radeon_fb->obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001007 rbo = obj->driver_private;
1008 r = radeon_bo_reserve(rbo, false);
1009 if (unlikely(r != 0))
1010 return r;
1011 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1012 if (unlikely(r != 0)) {
1013 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001014 return -EINVAL;
1015 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001016 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1017 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001018
1019 switch (crtc->fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001020 case 8:
1021 fb_format =
1022 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1023 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1024 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001025 case 15:
1026 fb_format =
1027 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1028 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1029 break;
1030 case 16:
1031 fb_format =
1032 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1033 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
1034 break;
1035 case 24:
1036 case 32:
1037 fb_format =
1038 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1039 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
1040 break;
1041 default:
1042 DRM_ERROR("Unsupported screen depth %d\n",
1043 crtc->fb->bits_per_pixel);
1044 return -EINVAL;
1045 }
1046
Alex Deucher40c4ac12010-05-20 12:04:59 -04001047 if (rdev->family >= CHIP_R600) {
1048 if (tiling_flags & RADEON_TILING_MACRO)
1049 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1050 else if (tiling_flags & RADEON_TILING_MICRO)
1051 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1052 } else {
1053 if (tiling_flags & RADEON_TILING_MACRO)
1054 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001055
Alex Deucher40c4ac12010-05-20 12:04:59 -04001056 if (tiling_flags & RADEON_TILING_MICRO)
1057 fb_format |= AVIVO_D1GRPH_TILED;
1058 }
Dave Airliee024e112009-06-24 09:48:08 +10001059
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001060 if (radeon_crtc->crtc_id == 0)
1061 WREG32(AVIVO_D1VGA_CONTROL, 0);
1062 else
1063 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001064
1065 if (rdev->family >= CHIP_RV770) {
1066 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001067 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1068 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001069 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001070 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1071 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001072 }
1073 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001074 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1075 (u32) fb_location);
1076 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1077 radeon_crtc->crtc_offset, (u32) fb_location);
1078 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
1079
1080 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1081 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1082 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1083 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
1084 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, crtc->fb->width);
1085 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, crtc->fb->height);
1086
1087 fb_pitch_pixels = crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8);
1088 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1089 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1090
1091 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1092 crtc->mode.vdisplay);
1093 x &= ~3;
1094 y &= ~1;
1095 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1096 (x << 16) | y);
1097 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
1098 (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
1099
1100 if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE)
1101 WREG32(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset,
1102 AVIVO_D1MODE_INTERLEAVE_EN);
1103 else
1104 WREG32(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset, 0);
1105
1106 if (old_fb && old_fb != crtc->fb) {
1107 radeon_fb = to_radeon_framebuffer(old_fb);
Jerome Glisse4c788672009-11-20 14:29:23 +01001108 rbo = radeon_fb->obj->driver_private;
1109 r = radeon_bo_reserve(rbo, false);
1110 if (unlikely(r != 0))
1111 return r;
1112 radeon_bo_unpin(rbo);
1113 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001114 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001115
1116 /* Bytes per pixel may have changed */
1117 radeon_bandwidth_update(rdev);
1118
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001119 return 0;
1120}
1121
Alex Deucher54f088a2010-01-19 16:34:01 -05001122int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1123 struct drm_framebuffer *old_fb)
1124{
1125 struct drm_device *dev = crtc->dev;
1126 struct radeon_device *rdev = dev->dev_private;
1127
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001128 if (ASIC_IS_DCE4(rdev))
1129 return evergreen_crtc_set_base(crtc, x, y, old_fb);
1130 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher54f088a2010-01-19 16:34:01 -05001131 return avivo_crtc_set_base(crtc, x, y, old_fb);
1132 else
1133 return radeon_crtc_set_base(crtc, x, y, old_fb);
1134}
1135
Alex Deucher615e0cb2010-01-20 16:22:53 -05001136/* properly set additional regs when using atombios */
1137static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1138{
1139 struct drm_device *dev = crtc->dev;
1140 struct radeon_device *rdev = dev->dev_private;
1141 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1142 u32 disp_merge_cntl;
1143
1144 switch (radeon_crtc->crtc_id) {
1145 case 0:
1146 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1147 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1148 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1149 break;
1150 case 1:
1151 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1152 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1153 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1154 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1155 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1156 break;
1157 }
1158}
1159
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001160static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1161{
1162 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1163 struct drm_device *dev = crtc->dev;
1164 struct radeon_device *rdev = dev->dev_private;
1165 struct drm_encoder *test_encoder;
1166 struct drm_crtc *test_crtc;
1167 uint32_t pll_in_use = 0;
1168
1169 if (ASIC_IS_DCE4(rdev)) {
1170 /* if crtc is driving DP and we have an ext clock, use that */
1171 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1172 if (test_encoder->crtc && (test_encoder->crtc == crtc)) {
1173 if (atombios_get_encoder_mode(test_encoder) == ATOM_ENCODER_MODE_DP) {
1174 if (rdev->clock.dp_extclk)
1175 return ATOM_PPLL_INVALID;
1176 }
1177 }
1178 }
1179
1180 /* otherwise, pick one of the plls */
1181 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1182 struct radeon_crtc *radeon_test_crtc;
1183
1184 if (crtc == test_crtc)
1185 continue;
1186
1187 radeon_test_crtc = to_radeon_crtc(test_crtc);
1188 if ((radeon_test_crtc->pll_id >= ATOM_PPLL1) &&
1189 (radeon_test_crtc->pll_id <= ATOM_PPLL2))
1190 pll_in_use |= (1 << radeon_test_crtc->pll_id);
1191 }
1192 if (!(pll_in_use & 1))
1193 return ATOM_PPLL1;
1194 return ATOM_PPLL2;
1195 } else
1196 return radeon_crtc->crtc_id;
1197
1198}
1199
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001200int atombios_crtc_mode_set(struct drm_crtc *crtc,
1201 struct drm_display_mode *mode,
1202 struct drm_display_mode *adjusted_mode,
1203 int x, int y, struct drm_framebuffer *old_fb)
1204{
1205 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1206 struct drm_device *dev = crtc->dev;
1207 struct radeon_device *rdev = dev->dev_private;
Alex Deucher54bfe492010-09-03 15:52:53 -04001208 struct drm_encoder *encoder;
1209 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001210
Alex Deucher54bfe492010-09-03 15:52:53 -04001211 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1212 /* find tv std */
1213 if (encoder->crtc == crtc) {
1214 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1215 if (radeon_encoder->active_device &
1216 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1217 is_tvcv = true;
1218 }
1219 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001220
Alex Deucherb7922102010-03-06 10:57:30 -05001221 atombios_disable_ss(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001222 /* always set DCPLL */
1223 if (ASIC_IS_DCE4(rdev))
1224 atombios_crtc_set_dcpll(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001225 atombios_crtc_set_pll(crtc, adjusted_mode);
Alex Deucherb7922102010-03-06 10:57:30 -05001226 atombios_enable_ss(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001227
Alex Deucher54bfe492010-09-03 15:52:53 -04001228 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001229 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001230 else if (ASIC_IS_AVIVO(rdev)) {
1231 if (is_tvcv)
1232 atombios_crtc_set_timing(crtc, adjusted_mode);
1233 else
1234 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1235 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001236 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001237 if (radeon_crtc->crtc_id == 0)
1238 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001239 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001240 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001241 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001242 atombios_overscan_setup(crtc, mode, adjusted_mode);
1243 atombios_scaler_setup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001244 return 0;
1245}
1246
1247static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
1248 struct drm_display_mode *mode,
1249 struct drm_display_mode *adjusted_mode)
1250{
Alex Deucher03214bd52010-03-16 17:42:46 -04001251 struct drm_device *dev = crtc->dev;
1252 struct radeon_device *rdev = dev->dev_private;
1253
1254 /* adjust pm to upcoming mode change */
1255 radeon_pm_compute_clocks(rdev);
1256
Jerome Glissec93bb852009-07-13 21:04:08 +02001257 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1258 return false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001259 return true;
1260}
1261
1262static void atombios_crtc_prepare(struct drm_crtc *crtc)
1263{
Alex Deucher267364a2010-03-08 17:10:41 -05001264 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1265
1266 /* pick pll */
1267 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1268
Alex Deucher37b43902010-02-09 12:04:43 -05001269 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001270 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001271}
1272
1273static void atombios_crtc_commit(struct drm_crtc *crtc)
1274{
1275 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001276 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001277}
1278
Alex Deucher37f90032010-06-11 17:58:38 -04001279static void atombios_crtc_disable(struct drm_crtc *crtc)
1280{
1281 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1282 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1283
1284 switch (radeon_crtc->pll_id) {
1285 case ATOM_PPLL1:
1286 case ATOM_PPLL2:
1287 /* disable the ppll */
1288 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1289 0, 0, ATOM_DISABLE, 0, 0, 0, 0);
1290 break;
1291 default:
1292 break;
1293 }
1294 radeon_crtc->pll_id = -1;
1295}
1296
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001297static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
1298 .dpms = atombios_crtc_dpms,
1299 .mode_fixup = atombios_crtc_mode_fixup,
1300 .mode_set = atombios_crtc_mode_set,
1301 .mode_set_base = atombios_crtc_set_base,
1302 .prepare = atombios_crtc_prepare,
1303 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10001304 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04001305 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001306};
1307
1308void radeon_atombios_init_crtc(struct drm_device *dev,
1309 struct radeon_crtc *radeon_crtc)
1310{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001311 struct radeon_device *rdev = dev->dev_private;
1312
1313 if (ASIC_IS_DCE4(rdev)) {
1314 switch (radeon_crtc->crtc_id) {
1315 case 0:
1316 default:
Alex Deucher12d77982010-02-09 17:18:48 -05001317 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001318 break;
1319 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05001320 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001321 break;
1322 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05001323 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001324 break;
1325 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05001326 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001327 break;
1328 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05001329 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001330 break;
1331 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05001332 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001333 break;
1334 }
1335 } else {
1336 if (radeon_crtc->crtc_id == 1)
1337 radeon_crtc->crtc_offset =
1338 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
1339 else
1340 radeon_crtc->crtc_offset = 0;
1341 }
1342 radeon_crtc->pll_id = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001343 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
1344}