blob: 2ab9b360d3c9b28b0a844001c129b5a94fced051 [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 Deuchereb1300b2009-07-13 11:09:56 -0400504
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200505 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucherfc103322010-01-19 17:16:10 -0500506 pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200507 else
Alex Deucherfc103322010-01-19 17:16:10 -0500508 pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200509 } else {
Alex Deucherfc103322010-01-19 17:16:10 -0500510 pll->flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200511
512 if (mode->clock > 200000) /* range limits??? */
Alex Deucherfc103322010-01-19 17:16:10 -0500513 pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200514 else
Alex Deucherfc103322010-01-19 17:16:10 -0500515 pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200516
517 }
518
519 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
520 if (encoder->crtc == crtc) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500521 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500522 encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400523 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) {
524 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
525 if (connector) {
526 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
527 struct radeon_connector_atom_dig *dig_connector =
528 radeon_connector->con_priv;
529
530 dp_clock = dig_connector->dp_clock;
531 }
532 }
533
Alex Deucher4eaeca32010-01-19 17:32:27 -0500534 if (ASIC_IS_AVIVO(rdev)) {
535 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
536 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
537 adjusted_clock = mode->clock * 2;
Alex Deuchera1a4b232010-04-09 15:31:56 -0400538 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT)) {
539 pll->algo = PLL_ALGO_LEGACY;
540 pll->flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
541 }
Alex Deucherf90087e2010-09-07 11:42:45 -0400542 /* There is some evidence (often anecdotal) that RV515/RV620 LVDS
Alex Deucher0d9958b2010-09-01 12:03:37 -0400543 * (on some boards at least) prefers the legacy algo. I'm not
544 * sure whether this should handled generically or on a
545 * case-by-case quirk basis. Both algos should work fine in the
546 * majority of cases.
547 */
548 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT)) &&
Alex Deucherf90087e2010-09-07 11:42:45 -0400549 ((rdev->family == CHIP_RV515) ||
550 (rdev->family == CHIP_RV620))) {
Alex Deucher0d9958b2010-09-01 12:03:37 -0400551 /* allow the user to overrride just in case */
552 if (radeon_new_pll == 1)
553 pll->algo = PLL_ALGO_NEW;
554 else
555 pll->algo = PLL_ALGO_LEGACY;
556 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500557 } else {
558 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
Alex Deucherfc103322010-01-19 17:16:10 -0500559 pll->flags |= RADEON_PLL_NO_ODD_POST_DIV;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500560 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
Alex Deucherfc103322010-01-19 17:16:10 -0500561 pll->flags |= RADEON_PLL_USE_REF_DIV;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200562 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000563 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200564 }
565 }
566
Alex Deucher2606c882009-10-08 13:36:21 -0400567 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
568 * accordingly based on the encoder/transmitter to work around
569 * special hw requirements.
570 */
571 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500572 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500573 u8 frev, crev;
574 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400575
Alex Deucher2606c882009-10-08 13:36:21 -0400576 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400577 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
578 &crev))
579 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500580
581 memset(&args, 0, sizeof(args));
582
583 switch (frev) {
584 case 1:
585 switch (crev) {
586 case 1:
587 case 2:
588 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
589 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500590 args.v1.ucEncodeMode = encoder_mode;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400591 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
592 /* may want to enable SS on DP eventually */
593 /* args.v1.ucConfig |=
594 ADJUST_DISPLAY_CONFIG_SS_ENABLE;*/
595 } else if (encoder_mode == ATOM_ENCODER_MODE_LVDS) {
596 args.v1.ucConfig |=
597 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
598 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500599
600 atom_execute_table(rdev->mode_info.atom_context,
601 index, (uint32_t *)&args);
602 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
603 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500604 case 3:
605 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
606 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
607 args.v3.sInput.ucEncodeMode = encoder_mode;
608 args.v3.sInput.ucDispPllConfig = 0;
609 if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
610 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
611
Alex Deucherfbee67a2010-08-16 12:44:47 -0400612 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
613 /* may want to enable SS on DP/eDP eventually */
614 /*args.v3.sInput.ucDispPllConfig |=
615 DISPPLL_CONFIG_SS_ENABLE;*/
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500616 args.v3.sInput.ucDispPllConfig |=
617 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400618 /* 16200 or 27000 */
619 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
620 } else {
621 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
622 /* deep color support */
623 args.v3.sInput.usPixelClock =
624 cpu_to_le16((mode->clock * bpc / 8) / 10);
625 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500626 if (dig->coherent_mode)
627 args.v3.sInput.ucDispPllConfig |=
628 DISPPLL_CONFIG_COHERENT_MODE;
629 if (mode->clock > 165000)
630 args.v3.sInput.ucDispPllConfig |=
631 DISPPLL_CONFIG_DUAL_LINK;
632 }
633 } else if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
Alex Deucherfbee67a2010-08-16 12:44:47 -0400634 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
635 /* may want to enable SS on DP/eDP eventually */
636 /*args.v3.sInput.ucDispPllConfig |=
637 DISPPLL_CONFIG_SS_ENABLE;*/
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500638 args.v3.sInput.ucDispPllConfig |=
Alex Deucher9f998ad2010-03-29 21:37:08 -0400639 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400640 /* 16200 or 27000 */
641 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
642 } else if (encoder_mode == ATOM_ENCODER_MODE_LVDS) {
643 /* want to enable SS on LVDS eventually */
644 /*args.v3.sInput.ucDispPllConfig |=
645 DISPPLL_CONFIG_SS_ENABLE;*/
646 } else {
Alex Deucher9f998ad2010-03-29 21:37:08 -0400647 if (mode->clock > 165000)
648 args.v3.sInput.ucDispPllConfig |=
649 DISPPLL_CONFIG_DUAL_LINK;
650 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500651 }
652 atom_execute_table(rdev->mode_info.atom_context,
653 index, (uint32_t *)&args);
654 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
655 if (args.v3.sOutput.ucRefDiv) {
656 pll->flags |= RADEON_PLL_USE_REF_DIV;
657 pll->reference_div = args.v3.sOutput.ucRefDiv;
658 }
659 if (args.v3.sOutput.ucPostDiv) {
660 pll->flags |= RADEON_PLL_USE_POST_DIV;
661 pll->post_div = args.v3.sOutput.ucPostDiv;
662 }
663 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500664 default:
665 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
666 return adjusted_clock;
667 }
668 break;
669 default:
670 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
671 return adjusted_clock;
672 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400673 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500674 return adjusted_clock;
675}
676
677union set_pixel_clock {
678 SET_PIXEL_CLOCK_PS_ALLOCATION base;
679 PIXEL_CLOCK_PARAMETERS v1;
680 PIXEL_CLOCK_PARAMETERS_V2 v2;
681 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500682 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500683};
684
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500685static void atombios_crtc_set_dcpll(struct drm_crtc *crtc)
686{
687 struct drm_device *dev = crtc->dev;
688 struct radeon_device *rdev = dev->dev_private;
689 u8 frev, crev;
690 int index;
691 union set_pixel_clock args;
692
693 memset(&args, 0, sizeof(args));
694
695 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400696 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
697 &crev))
698 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500699
700 switch (frev) {
701 case 1:
702 switch (crev) {
703 case 5:
704 /* if the default dcpll clock is specified,
705 * SetPixelClock provides the dividers
706 */
707 args.v5.ucCRTC = ATOM_CRTC_INVALID;
708 args.v5.usPixelClock = rdev->clock.default_dispclk;
709 args.v5.ucPpll = ATOM_DCPLL;
710 break;
711 default:
712 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
713 return;
714 }
715 break;
716 default:
717 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
718 return;
719 }
720 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
721}
722
Alex Deucher37f90032010-06-11 17:58:38 -0400723static void atombios_crtc_program_pll(struct drm_crtc *crtc,
724 int crtc_id,
725 int pll_id,
726 u32 encoder_mode,
727 u32 encoder_id,
728 u32 clock,
729 u32 ref_div,
730 u32 fb_div,
731 u32 frac_fb_div,
732 u32 post_div)
733{
734 struct drm_device *dev = crtc->dev;
735 struct radeon_device *rdev = dev->dev_private;
736 u8 frev, crev;
737 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
738 union set_pixel_clock args;
739
740 memset(&args, 0, sizeof(args));
741
742 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
743 &crev))
744 return;
745
746 switch (frev) {
747 case 1:
748 switch (crev) {
749 case 1:
750 if (clock == ATOM_DISABLE)
751 return;
752 args.v1.usPixelClock = cpu_to_le16(clock / 10);
753 args.v1.usRefDiv = cpu_to_le16(ref_div);
754 args.v1.usFbDiv = cpu_to_le16(fb_div);
755 args.v1.ucFracFbDiv = frac_fb_div;
756 args.v1.ucPostDiv = post_div;
757 args.v1.ucPpll = pll_id;
758 args.v1.ucCRTC = crtc_id;
759 args.v1.ucRefDivSrc = 1;
760 break;
761 case 2:
762 args.v2.usPixelClock = cpu_to_le16(clock / 10);
763 args.v2.usRefDiv = cpu_to_le16(ref_div);
764 args.v2.usFbDiv = cpu_to_le16(fb_div);
765 args.v2.ucFracFbDiv = frac_fb_div;
766 args.v2.ucPostDiv = post_div;
767 args.v2.ucPpll = pll_id;
768 args.v2.ucCRTC = crtc_id;
769 args.v2.ucRefDivSrc = 1;
770 break;
771 case 3:
772 args.v3.usPixelClock = cpu_to_le16(clock / 10);
773 args.v3.usRefDiv = cpu_to_le16(ref_div);
774 args.v3.usFbDiv = cpu_to_le16(fb_div);
775 args.v3.ucFracFbDiv = frac_fb_div;
776 args.v3.ucPostDiv = post_div;
777 args.v3.ucPpll = pll_id;
778 args.v3.ucMiscInfo = (pll_id << 2);
779 args.v3.ucTransmitterId = encoder_id;
780 args.v3.ucEncoderMode = encoder_mode;
781 break;
782 case 5:
783 args.v5.ucCRTC = crtc_id;
784 args.v5.usPixelClock = cpu_to_le16(clock / 10);
785 args.v5.ucRefDiv = ref_div;
786 args.v5.usFbDiv = cpu_to_le16(fb_div);
787 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
788 args.v5.ucPostDiv = post_div;
789 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
790 args.v5.ucTransmitterID = encoder_id;
791 args.v5.ucEncoderMode = encoder_mode;
792 args.v5.ucPpll = pll_id;
793 break;
794 default:
795 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
796 return;
797 }
798 break;
799 default:
800 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
801 return;
802 }
803
804 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
805}
806
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500807static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -0500808{
809 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
810 struct drm_device *dev = crtc->dev;
811 struct radeon_device *rdev = dev->dev_private;
812 struct drm_encoder *encoder = NULL;
813 struct radeon_encoder *radeon_encoder = NULL;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500814 u32 pll_clock = mode->clock;
815 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
816 struct radeon_pll *pll;
817 u32 adjusted_clock;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500818 int encoder_mode = 0;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500819
Alex Deucher4eaeca32010-01-19 17:32:27 -0500820 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
821 if (encoder->crtc == crtc) {
822 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500823 encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500824 break;
825 }
826 }
827
828 if (!radeon_encoder)
829 return;
830
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500831 switch (radeon_crtc->pll_id) {
832 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -0500833 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500834 break;
835 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -0500836 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500837 break;
838 case ATOM_DCPLL:
839 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +1000840 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500841 pll = &rdev->clock.dcpll;
842 break;
843 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500844
845 /* adjust pixel clock as needed */
846 adjusted_clock = atombios_adjust_pll(crtc, mode, pll);
Alex Deucher2606c882009-10-08 13:36:21 -0400847
Alex Deucher7c27f872010-02-02 12:05:01 -0500848 radeon_compute_pll(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
849 &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200850
Alex Deucher37f90032010-06-11 17:58:38 -0400851 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
852 encoder_mode, radeon_encoder->encoder_id, mode->clock,
853 ref_div, fb_div, frac_fb_div, post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200854
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200855}
856
Chris Ball4dd19b02010-09-26 06:47:23 -0500857static int evergreen_crtc_do_set_base(struct drm_crtc *crtc,
858 struct drm_framebuffer *fb,
859 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500860{
861 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
862 struct drm_device *dev = crtc->dev;
863 struct radeon_device *rdev = dev->dev_private;
864 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -0500865 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500866 struct drm_gem_object *obj;
867 struct radeon_bo *rbo;
868 uint64_t fb_location;
869 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
870 int r;
871
872 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -0500873 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000874 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500875 return 0;
876 }
877
Chris Ball4dd19b02010-09-26 06:47:23 -0500878 if (atomic) {
879 radeon_fb = to_radeon_framebuffer(fb);
880 target_fb = fb;
881 }
882 else {
883 radeon_fb = to_radeon_framebuffer(crtc->fb);
884 target_fb = crtc->fb;
885 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500886
Chris Ball4dd19b02010-09-26 06:47:23 -0500887 /* If atomic, assume fb object is pinned & idle & fenced and
888 * just update base pointers
889 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500890 obj = radeon_fb->obj;
891 rbo = obj->driver_private;
892 r = radeon_bo_reserve(rbo, false);
893 if (unlikely(r != 0))
894 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -0500895
896 if (atomic)
897 fb_location = radeon_bo_gpu_offset(rbo);
898 else {
899 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
900 if (unlikely(r != 0)) {
901 radeon_bo_unreserve(rbo);
902 return -EINVAL;
903 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500904 }
Chris Ball4dd19b02010-09-26 06:47:23 -0500905
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500906 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
907 radeon_bo_unreserve(rbo);
908
Chris Ball4dd19b02010-09-26 06:47:23 -0500909 switch (target_fb->bits_per_pixel) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500910 case 8:
911 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
912 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
913 break;
914 case 15:
915 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
916 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
917 break;
918 case 16:
919 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
920 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
921 break;
922 case 24:
923 case 32:
924 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
925 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
926 break;
927 default:
928 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -0500929 target_fb->bits_per_pixel);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500930 return -EINVAL;
931 }
932
Alex Deucher97d66322010-05-20 12:12:48 -0400933 if (tiling_flags & RADEON_TILING_MACRO)
934 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
935 else if (tiling_flags & RADEON_TILING_MICRO)
936 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
937
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500938 switch (radeon_crtc->crtc_id) {
939 case 0:
940 WREG32(AVIVO_D1VGA_CONTROL, 0);
941 break;
942 case 1:
943 WREG32(AVIVO_D2VGA_CONTROL, 0);
944 break;
945 case 2:
946 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
947 break;
948 case 3:
949 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
950 break;
951 case 4:
952 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
953 break;
954 case 5:
955 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
956 break;
957 default:
958 break;
959 }
960
961 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
962 upper_32_bits(fb_location));
963 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
964 upper_32_bits(fb_location));
965 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
966 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
967 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
968 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
969 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
970
971 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
972 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
973 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
974 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -0500975 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
976 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500977
Chris Ball4dd19b02010-09-26 06:47:23 -0500978 fb_pitch_pixels = target_fb->pitch / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500979 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
980 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
981
982 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
983 crtc->mode.vdisplay);
984 x &= ~3;
985 y &= ~1;
986 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
987 (x << 16) | y);
988 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
989 (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
990
991 if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE)
992 WREG32(EVERGREEN_DATA_FORMAT + radeon_crtc->crtc_offset,
993 EVERGREEN_INTERLEAVE_EN);
994 else
995 WREG32(EVERGREEN_DATA_FORMAT + radeon_crtc->crtc_offset, 0);
996
Chris Ball4dd19b02010-09-26 06:47:23 -0500997 if (!atomic && fb && fb != crtc->fb) {
998 radeon_fb = to_radeon_framebuffer(fb);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500999 rbo = radeon_fb->obj->driver_private;
1000 r = radeon_bo_reserve(rbo, false);
1001 if (unlikely(r != 0))
1002 return r;
1003 radeon_bo_unpin(rbo);
1004 radeon_bo_unreserve(rbo);
1005 }
1006
1007 /* Bytes per pixel may have changed */
1008 radeon_bandwidth_update(rdev);
1009
1010 return 0;
1011}
1012
Chris Ball4dd19b02010-09-26 06:47:23 -05001013static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1014 struct drm_framebuffer *fb,
1015 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001016{
1017 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1018 struct drm_device *dev = crtc->dev;
1019 struct radeon_device *rdev = dev->dev_private;
1020 struct radeon_framebuffer *radeon_fb;
1021 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001022 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001023 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001024 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001025 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse4c788672009-11-20 14:29:23 +01001026 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001027
Jerome Glisse2de3b482009-11-17 14:08:55 -08001028 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001029 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001030 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001031 return 0;
1032 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001033
Chris Ball4dd19b02010-09-26 06:47:23 -05001034 if (atomic) {
1035 radeon_fb = to_radeon_framebuffer(fb);
1036 target_fb = fb;
1037 }
1038 else {
1039 radeon_fb = to_radeon_framebuffer(crtc->fb);
1040 target_fb = crtc->fb;
1041 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001042
1043 obj = radeon_fb->obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001044 rbo = obj->driver_private;
1045 r = radeon_bo_reserve(rbo, false);
1046 if (unlikely(r != 0))
1047 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001048
1049 /* If atomic, assume fb object is pinned & idle & fenced and
1050 * just update base pointers
1051 */
1052 if (atomic)
1053 fb_location = radeon_bo_gpu_offset(rbo);
1054 else {
1055 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1056 if (unlikely(r != 0)) {
1057 radeon_bo_unreserve(rbo);
1058 return -EINVAL;
1059 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001060 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001061 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1062 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001063
Chris Ball4dd19b02010-09-26 06:47:23 -05001064 switch (target_fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001065 case 8:
1066 fb_format =
1067 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1068 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1069 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001070 case 15:
1071 fb_format =
1072 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1073 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1074 break;
1075 case 16:
1076 fb_format =
1077 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1078 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
1079 break;
1080 case 24:
1081 case 32:
1082 fb_format =
1083 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1084 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
1085 break;
1086 default:
1087 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001088 target_fb->bits_per_pixel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001089 return -EINVAL;
1090 }
1091
Alex Deucher40c4ac12010-05-20 12:04:59 -04001092 if (rdev->family >= CHIP_R600) {
1093 if (tiling_flags & RADEON_TILING_MACRO)
1094 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1095 else if (tiling_flags & RADEON_TILING_MICRO)
1096 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1097 } else {
1098 if (tiling_flags & RADEON_TILING_MACRO)
1099 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001100
Alex Deucher40c4ac12010-05-20 12:04:59 -04001101 if (tiling_flags & RADEON_TILING_MICRO)
1102 fb_format |= AVIVO_D1GRPH_TILED;
1103 }
Dave Airliee024e112009-06-24 09:48:08 +10001104
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001105 if (radeon_crtc->crtc_id == 0)
1106 WREG32(AVIVO_D1VGA_CONTROL, 0);
1107 else
1108 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001109
1110 if (rdev->family >= CHIP_RV770) {
1111 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001112 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1113 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001114 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001115 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1116 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001117 }
1118 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001119 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1120 (u32) fb_location);
1121 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1122 radeon_crtc->crtc_offset, (u32) fb_location);
1123 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
1124
1125 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1126 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1127 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1128 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001129 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1130 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001131
Chris Ball4dd19b02010-09-26 06:47:23 -05001132 fb_pitch_pixels = target_fb->pitch / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001133 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1134 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1135
1136 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1137 crtc->mode.vdisplay);
1138 x &= ~3;
1139 y &= ~1;
1140 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1141 (x << 16) | y);
1142 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
1143 (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
1144
1145 if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE)
1146 WREG32(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset,
1147 AVIVO_D1MODE_INTERLEAVE_EN);
1148 else
1149 WREG32(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset, 0);
1150
Chris Ball4dd19b02010-09-26 06:47:23 -05001151 if (!atomic && fb && fb != crtc->fb) {
1152 radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse4c788672009-11-20 14:29:23 +01001153 rbo = radeon_fb->obj->driver_private;
1154 r = radeon_bo_reserve(rbo, false);
1155 if (unlikely(r != 0))
1156 return r;
1157 radeon_bo_unpin(rbo);
1158 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001159 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001160
1161 /* Bytes per pixel may have changed */
1162 radeon_bandwidth_update(rdev);
1163
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001164 return 0;
1165}
1166
Alex Deucher54f088a2010-01-19 16:34:01 -05001167int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1168 struct drm_framebuffer *old_fb)
1169{
1170 struct drm_device *dev = crtc->dev;
1171 struct radeon_device *rdev = dev->dev_private;
1172
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001173 if (ASIC_IS_DCE4(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001174 return evergreen_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001175 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001176 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001177 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001178 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1179}
1180
1181int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1182 struct drm_framebuffer *fb,
1183 int x, int y)
1184{
1185 struct drm_device *dev = crtc->dev;
1186 struct radeon_device *rdev = dev->dev_private;
1187
1188 if (ASIC_IS_DCE4(rdev))
1189 return evergreen_crtc_do_set_base(crtc, fb, x, y, 1);
1190 else if (ASIC_IS_AVIVO(rdev))
1191 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1192 else
1193 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001194}
1195
Alex Deucher615e0cb2010-01-20 16:22:53 -05001196/* properly set additional regs when using atombios */
1197static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1198{
1199 struct drm_device *dev = crtc->dev;
1200 struct radeon_device *rdev = dev->dev_private;
1201 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1202 u32 disp_merge_cntl;
1203
1204 switch (radeon_crtc->crtc_id) {
1205 case 0:
1206 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1207 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1208 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1209 break;
1210 case 1:
1211 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1212 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1213 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1214 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1215 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1216 break;
1217 }
1218}
1219
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001220static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1221{
1222 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1223 struct drm_device *dev = crtc->dev;
1224 struct radeon_device *rdev = dev->dev_private;
1225 struct drm_encoder *test_encoder;
1226 struct drm_crtc *test_crtc;
1227 uint32_t pll_in_use = 0;
1228
1229 if (ASIC_IS_DCE4(rdev)) {
1230 /* if crtc is driving DP and we have an ext clock, use that */
1231 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1232 if (test_encoder->crtc && (test_encoder->crtc == crtc)) {
1233 if (atombios_get_encoder_mode(test_encoder) == ATOM_ENCODER_MODE_DP) {
1234 if (rdev->clock.dp_extclk)
1235 return ATOM_PPLL_INVALID;
1236 }
1237 }
1238 }
1239
1240 /* otherwise, pick one of the plls */
1241 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1242 struct radeon_crtc *radeon_test_crtc;
1243
1244 if (crtc == test_crtc)
1245 continue;
1246
1247 radeon_test_crtc = to_radeon_crtc(test_crtc);
1248 if ((radeon_test_crtc->pll_id >= ATOM_PPLL1) &&
1249 (radeon_test_crtc->pll_id <= ATOM_PPLL2))
1250 pll_in_use |= (1 << radeon_test_crtc->pll_id);
1251 }
1252 if (!(pll_in_use & 1))
1253 return ATOM_PPLL1;
1254 return ATOM_PPLL2;
1255 } else
1256 return radeon_crtc->crtc_id;
1257
1258}
1259
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001260int atombios_crtc_mode_set(struct drm_crtc *crtc,
1261 struct drm_display_mode *mode,
1262 struct drm_display_mode *adjusted_mode,
1263 int x, int y, struct drm_framebuffer *old_fb)
1264{
1265 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1266 struct drm_device *dev = crtc->dev;
1267 struct radeon_device *rdev = dev->dev_private;
Alex Deucher54bfe492010-09-03 15:52:53 -04001268 struct drm_encoder *encoder;
1269 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001270
Alex Deucher54bfe492010-09-03 15:52:53 -04001271 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1272 /* find tv std */
1273 if (encoder->crtc == crtc) {
1274 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1275 if (radeon_encoder->active_device &
1276 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1277 is_tvcv = true;
1278 }
1279 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001280
Alex Deucherb7922102010-03-06 10:57:30 -05001281 atombios_disable_ss(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001282 /* always set DCPLL */
1283 if (ASIC_IS_DCE4(rdev))
1284 atombios_crtc_set_dcpll(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001285 atombios_crtc_set_pll(crtc, adjusted_mode);
Alex Deucherb7922102010-03-06 10:57:30 -05001286 atombios_enable_ss(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001287
Alex Deucher54bfe492010-09-03 15:52:53 -04001288 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001289 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001290 else if (ASIC_IS_AVIVO(rdev)) {
1291 if (is_tvcv)
1292 atombios_crtc_set_timing(crtc, adjusted_mode);
1293 else
1294 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1295 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001296 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001297 if (radeon_crtc->crtc_id == 0)
1298 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001299 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001300 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001301 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001302 atombios_overscan_setup(crtc, mode, adjusted_mode);
1303 atombios_scaler_setup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001304 return 0;
1305}
1306
1307static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
1308 struct drm_display_mode *mode,
1309 struct drm_display_mode *adjusted_mode)
1310{
Alex Deucher03214bd52010-03-16 17:42:46 -04001311 struct drm_device *dev = crtc->dev;
1312 struct radeon_device *rdev = dev->dev_private;
1313
1314 /* adjust pm to upcoming mode change */
1315 radeon_pm_compute_clocks(rdev);
1316
Jerome Glissec93bb852009-07-13 21:04:08 +02001317 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1318 return false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001319 return true;
1320}
1321
1322static void atombios_crtc_prepare(struct drm_crtc *crtc)
1323{
Alex Deucher267364a2010-03-08 17:10:41 -05001324 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1325
1326 /* pick pll */
1327 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1328
Alex Deucher37b43902010-02-09 12:04:43 -05001329 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001330 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001331}
1332
1333static void atombios_crtc_commit(struct drm_crtc *crtc)
1334{
1335 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001336 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001337}
1338
Alex Deucher37f90032010-06-11 17:58:38 -04001339static void atombios_crtc_disable(struct drm_crtc *crtc)
1340{
1341 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1342 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1343
1344 switch (radeon_crtc->pll_id) {
1345 case ATOM_PPLL1:
1346 case ATOM_PPLL2:
1347 /* disable the ppll */
1348 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1349 0, 0, ATOM_DISABLE, 0, 0, 0, 0);
1350 break;
1351 default:
1352 break;
1353 }
1354 radeon_crtc->pll_id = -1;
1355}
1356
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001357static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
1358 .dpms = atombios_crtc_dpms,
1359 .mode_fixup = atombios_crtc_mode_fixup,
1360 .mode_set = atombios_crtc_mode_set,
1361 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05001362 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001363 .prepare = atombios_crtc_prepare,
1364 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10001365 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04001366 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001367};
1368
1369void radeon_atombios_init_crtc(struct drm_device *dev,
1370 struct radeon_crtc *radeon_crtc)
1371{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001372 struct radeon_device *rdev = dev->dev_private;
1373
1374 if (ASIC_IS_DCE4(rdev)) {
1375 switch (radeon_crtc->crtc_id) {
1376 case 0:
1377 default:
Alex Deucher12d77982010-02-09 17:18:48 -05001378 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001379 break;
1380 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05001381 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001382 break;
1383 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05001384 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001385 break;
1386 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05001387 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001388 break;
1389 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05001390 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001391 break;
1392 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05001393 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001394 break;
1395 }
1396 } else {
1397 if (radeon_crtc->crtc_id == 1)
1398 radeon_crtc->crtc_offset =
1399 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
1400 else
1401 radeon_crtc->crtc_offset = 0;
1402 }
1403 radeon_crtc->pll_id = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001404 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
1405}