blob: f5819ba481d961c15452c4c351b947c0c2d0b098 [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:
Cédric Cano45894332011-02-11 19:45:37 -050051 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
52 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
53 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
54 args.usOverscanRight = cpu_to_le16((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) {
Cédric Cano45894332011-02-11 19:45:37 -050061 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
62 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020063 } else if (a2 > a1) {
Alex Deucher942b0e92011-03-14 23:18:00 -040064 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
65 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020066 }
Jerome Glissec93bb852009-07-13 21:04:08 +020067 break;
68 case RMX_FULL:
69 default:
Cédric Cano45894332011-02-11 19:45:37 -050070 args.usOverscanRight = cpu_to_le16(radeon_crtc->h_border);
71 args.usOverscanLeft = cpu_to_le16(radeon_crtc->h_border);
72 args.usOverscanBottom = cpu_to_le16(radeon_crtc->v_border);
73 args.usOverscanTop = cpu_to_le16(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 Deuchera93f3442010-12-20 11:22:29 -0500256 if (radeon_crtc->enabled)
257 atombios_blank_crtc(crtc, ATOM_ENABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200258 if (ASIC_IS_DCE3(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500259 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
260 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400261 radeon_crtc->enabled = false;
Alex Deucherd7311172010-05-03 01:13:14 -0400262 /* adjust pm to dpms changes AFTER disabling crtcs */
263 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200264 break;
265 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200266}
267
268static void
269atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400270 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200271{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400272 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200273 struct drm_device *dev = crtc->dev;
274 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400275 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200276 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400277 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200278
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400279 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400280 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400281 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400282 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
283 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400284 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400285 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400286 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400287 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400288 args.usH_SyncWidth =
289 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
290 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400291 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400292 args.usV_SyncWidth =
293 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400294 args.ucH_Border = radeon_crtc->h_border;
295 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400296
297 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
298 misc |= ATOM_VSYNC_POLARITY;
299 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
300 misc |= ATOM_HSYNC_POLARITY;
301 if (mode->flags & DRM_MODE_FLAG_CSYNC)
302 misc |= ATOM_COMPOSITESYNC;
303 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
304 misc |= ATOM_INTERLACE;
305 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
306 misc |= ATOM_DOUBLE_CLOCK_MODE;
307
308 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
309 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200310
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400311 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200312}
313
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400314static void atombios_crtc_set_timing(struct drm_crtc *crtc,
315 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200316{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400317 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200318 struct drm_device *dev = crtc->dev;
319 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400320 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200321 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400322 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200323
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400324 memset(&args, 0, sizeof(args));
325 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
326 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
327 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
328 args.usH_SyncWidth =
329 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
330 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
331 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
332 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
333 args.usV_SyncWidth =
334 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
335
Alex Deucher54bfe492010-09-03 15:52:53 -0400336 args.ucOverscanRight = radeon_crtc->h_border;
337 args.ucOverscanLeft = radeon_crtc->h_border;
338 args.ucOverscanBottom = radeon_crtc->v_border;
339 args.ucOverscanTop = radeon_crtc->v_border;
340
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400341 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
342 misc |= ATOM_VSYNC_POLARITY;
343 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
344 misc |= ATOM_HSYNC_POLARITY;
345 if (mode->flags & DRM_MODE_FLAG_CSYNC)
346 misc |= ATOM_COMPOSITESYNC;
347 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
348 misc |= ATOM_INTERLACE;
349 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
350 misc |= ATOM_DOUBLE_CLOCK_MODE;
351
352 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
353 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200354
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400355 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200356}
357
Alex Deucherb7922102010-03-06 10:57:30 -0500358static void atombios_disable_ss(struct drm_crtc *crtc)
359{
360 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
361 struct drm_device *dev = crtc->dev;
362 struct radeon_device *rdev = dev->dev_private;
363 u32 ss_cntl;
364
365 if (ASIC_IS_DCE4(rdev)) {
366 switch (radeon_crtc->pll_id) {
367 case ATOM_PPLL1:
368 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
369 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
370 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
371 break;
372 case ATOM_PPLL2:
373 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
374 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
375 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
376 break;
377 case ATOM_DCPLL:
378 case ATOM_PPLL_INVALID:
379 return;
380 }
381 } else if (ASIC_IS_AVIVO(rdev)) {
382 switch (radeon_crtc->pll_id) {
383 case ATOM_PPLL1:
384 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
385 ss_cntl &= ~1;
386 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
387 break;
388 case ATOM_PPLL2:
389 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
390 ss_cntl &= ~1;
391 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
392 break;
393 case ATOM_DCPLL:
394 case ATOM_PPLL_INVALID:
395 return;
396 }
397 }
398}
399
400
Alex Deucher26b9fc32010-02-01 16:39:11 -0500401union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400402 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
403 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500404 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400405 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500406 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500407};
408
Alex Deucherba032a52010-10-04 17:13:01 -0400409static void atombios_crtc_program_ss(struct drm_crtc *crtc,
410 int enable,
411 int pll_id,
412 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400413{
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400414 struct drm_device *dev = crtc->dev;
415 struct radeon_device *rdev = dev->dev_private;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400416 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500417 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400418
Alex Deucher26b9fc32010-02-01 16:39:11 -0500419 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400420
Alex Deuchera572eaa2011-01-06 21:19:16 -0500421 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500422 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400423 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500424 switch (pll_id) {
425 case ATOM_PPLL1:
426 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Cédric Cano45894332011-02-11 19:45:37 -0500427 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
428 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deuchera572eaa2011-01-06 21:19:16 -0500429 break;
430 case ATOM_PPLL2:
431 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Cédric Cano45894332011-02-11 19:45:37 -0500432 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
433 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deuchera572eaa2011-01-06 21:19:16 -0500434 break;
435 case ATOM_DCPLL:
436 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Cédric Cano45894332011-02-11 19:45:37 -0500437 args.v3.usSpreadSpectrumAmount = cpu_to_le16(0);
438 args.v3.usSpreadSpectrumStep = cpu_to_le16(0);
Alex Deuchera572eaa2011-01-06 21:19:16 -0500439 break;
440 case ATOM_PPLL_INVALID:
441 return;
442 }
443 args.v2.ucEnable = enable;
Alex Deucher8e8e5232011-05-20 04:34:16 -0400444 if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK))
445 args.v3.ucEnable = ATOM_DISABLE;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500446 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400447 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400448 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400449 switch (pll_id) {
450 case ATOM_PPLL1:
451 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Cédric Cano45894332011-02-11 19:45:37 -0500452 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
453 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400454 break;
455 case ATOM_PPLL2:
456 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Cédric Cano45894332011-02-11 19:45:37 -0500457 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
458 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400459 break;
460 case ATOM_DCPLL:
461 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Cédric Cano45894332011-02-11 19:45:37 -0500462 args.v2.usSpreadSpectrumAmount = cpu_to_le16(0);
463 args.v2.usSpreadSpectrumStep = cpu_to_le16(0);
Alex Deucherba032a52010-10-04 17:13:01 -0400464 break;
465 case ATOM_PPLL_INVALID:
466 return;
467 }
468 args.v2.ucEnable = enable;
Alex Deucher8e8e5232011-05-20 04:34:16 -0400469 if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK))
470 args.v2.ucEnable = ATOM_DISABLE;
Alex Deucherba032a52010-10-04 17:13:01 -0400471 } else if (ASIC_IS_DCE3(rdev)) {
472 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400473 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400474 args.v1.ucSpreadSpectrumStep = ss->step;
475 args.v1.ucSpreadSpectrumDelay = ss->delay;
476 args.v1.ucSpreadSpectrumRange = ss->range;
477 args.v1.ucPpll = pll_id;
478 args.v1.ucEnable = enable;
479 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400480 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
481 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400482 atombios_disable_ss(crtc);
483 return;
484 }
485 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400486 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400487 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
488 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
489 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
490 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400491 } else {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400492 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
493 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400494 atombios_disable_ss(crtc);
495 return;
496 }
497 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400498 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400499 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
500 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
501 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400502 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500503 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400504}
505
Alex Deucher4eaeca32010-01-19 17:32:27 -0500506union adjust_pixel_clock {
507 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500508 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500509};
510
511static u32 atombios_adjust_pll(struct drm_crtc *crtc,
512 struct drm_display_mode *mode,
Alex Deucherba032a52010-10-04 17:13:01 -0400513 struct radeon_pll *pll,
514 bool ss_enabled,
515 struct radeon_atom_ss *ss)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200516{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200517 struct drm_device *dev = crtc->dev;
518 struct radeon_device *rdev = dev->dev_private;
519 struct drm_encoder *encoder = NULL;
520 struct radeon_encoder *radeon_encoder = NULL;
Alex Deucherdf271be2011-05-20 04:34:15 -0400521 struct drm_connector *connector = NULL;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500522 u32 adjusted_clock = mode->clock;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500523 int encoder_mode = 0;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400524 u32 dp_clock = mode->clock;
525 int bpc = 8;
Alex Deucherfc103322010-01-19 17:16:10 -0500526
Alex Deucher4eaeca32010-01-19 17:32:27 -0500527 /* reset the pll flags */
528 pll->flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200529
530 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400531 if ((rdev->family == CHIP_RS600) ||
532 (rdev->family == CHIP_RS690) ||
533 (rdev->family == CHIP_RS740))
Alex Deucher2ff776c2010-06-08 19:44:36 -0400534 pll->flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
Alex Deucherfc103322010-01-19 17:16:10 -0500535 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000536
537 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
538 pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
539 else
540 pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400541
Alex Deucher5785e532011-04-19 15:24:59 -0400542 if (rdev->family < CHIP_RV770)
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400543 pll->flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Dave Airlie5480f722010-10-19 10:36:47 +1000544 } else {
Alex Deucherfc103322010-01-19 17:16:10 -0500545 pll->flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200546
Dave Airlie5480f722010-10-19 10:36:47 +1000547 if (mode->clock > 200000) /* range limits??? */
548 pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
549 else
550 pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000551 }
552
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200553 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
554 if (encoder->crtc == crtc) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500555 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherdf271be2011-05-20 04:34:15 -0400556 connector = radeon_get_connector_for_encoder(encoder);
557 if (connector)
558 bpc = connector->display_info.bpc;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500559 encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deuchereac4dff2011-05-20 04:34:22 -0400560 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
561 radeon_encoder_is_dp_bridge(encoder)) {
Alex Deucherfbee67a2010-08-16 12:44:47 -0400562 if (connector) {
563 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
564 struct radeon_connector_atom_dig *dig_connector =
565 radeon_connector->con_priv;
566
567 dp_clock = dig_connector->dp_clock;
568 }
569 }
Alex Deucher5b40ddf2011-02-14 11:43:11 -0500570
Alex Deucherba032a52010-10-04 17:13:01 -0400571 /* use recommended ref_div for ss */
572 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
573 if (ss_enabled) {
574 if (ss->refdiv) {
575 pll->flags |= RADEON_PLL_USE_REF_DIV;
576 pll->reference_div = ss->refdiv;
Alex Deucher5b40ddf2011-02-14 11:43:11 -0500577 if (ASIC_IS_AVIVO(rdev))
578 pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucherba032a52010-10-04 17:13:01 -0400579 }
580 }
581 }
Alex Deucher5b40ddf2011-02-14 11:43:11 -0500582
Alex Deucher4eaeca32010-01-19 17:32:27 -0500583 if (ASIC_IS_AVIVO(rdev)) {
584 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
585 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
586 adjusted_clock = mode->clock * 2;
Alex Deucher48dfaae2010-09-29 11:37:41 -0400587 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
Alex Deuchera1a4b232010-04-09 15:31:56 -0400588 pll->flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
Alex Deucher5b40ddf2011-02-14 11:43:11 -0500589 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
590 pll->flags |= RADEON_PLL_IS_LCD;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500591 } else {
592 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
Alex Deucherfc103322010-01-19 17:16:10 -0500593 pll->flags |= RADEON_PLL_NO_ODD_POST_DIV;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500594 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
Alex Deucherfc103322010-01-19 17:16:10 -0500595 pll->flags |= RADEON_PLL_USE_REF_DIV;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200596 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000597 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200598 }
599 }
600
Alex Deucher2606c882009-10-08 13:36:21 -0400601 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
602 * accordingly based on the encoder/transmitter to work around
603 * special hw requirements.
604 */
605 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500606 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500607 u8 frev, crev;
608 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400609
Alex Deucher2606c882009-10-08 13:36:21 -0400610 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400611 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
612 &crev))
613 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500614
615 memset(&args, 0, sizeof(args));
616
617 switch (frev) {
618 case 1:
619 switch (crev) {
620 case 1:
621 case 2:
622 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
623 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500624 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher8e8e5232011-05-20 04:34:16 -0400625 if (ss_enabled && ss->percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400626 args.v1.ucConfig |=
627 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500628
629 atom_execute_table(rdev->mode_info.atom_context,
630 index, (uint32_t *)&args);
631 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
632 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500633 case 3:
634 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
635 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
636 args.v3.sInput.ucEncodeMode = encoder_mode;
637 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher8e8e5232011-05-20 04:34:16 -0400638 if (ss_enabled && ss->percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000639 args.v3.sInput.ucDispPllConfig |=
640 DISPPLL_CONFIG_SS_ENABLE;
Alex Deuchereac4dff2011-05-20 04:34:22 -0400641 if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT) ||
642 radeon_encoder_is_dp_bridge(encoder)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500643 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400644 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500645 args.v3.sInput.ucDispPllConfig |=
646 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400647 /* 16200 or 27000 */
648 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
649 } else {
650 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
651 /* deep color support */
652 args.v3.sInput.usPixelClock =
653 cpu_to_le16((mode->clock * bpc / 8) / 10);
654 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500655 if (dig->coherent_mode)
656 args.v3.sInput.ucDispPllConfig |=
657 DISPPLL_CONFIG_COHERENT_MODE;
658 if (mode->clock > 165000)
659 args.v3.sInput.ucDispPllConfig |=
660 DISPPLL_CONFIG_DUAL_LINK;
661 }
662 } else if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
Alex Deucherfbee67a2010-08-16 12:44:47 -0400663 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500664 args.v3.sInput.ucDispPllConfig |=
Alex Deucher9f998ad2010-03-29 21:37:08 -0400665 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400666 /* 16200 or 27000 */
667 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
Alex Deucherb526ce22011-01-20 23:35:58 +0000668 } else if (encoder_mode != ATOM_ENCODER_MODE_LVDS) {
Alex Deucher9f998ad2010-03-29 21:37:08 -0400669 if (mode->clock > 165000)
670 args.v3.sInput.ucDispPllConfig |=
671 DISPPLL_CONFIG_DUAL_LINK;
672 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500673 }
674 atom_execute_table(rdev->mode_info.atom_context,
675 index, (uint32_t *)&args);
676 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
677 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher9f4283f2011-02-16 21:17:04 -0500678 pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500679 pll->flags |= RADEON_PLL_USE_REF_DIV;
680 pll->reference_div = args.v3.sOutput.ucRefDiv;
681 }
682 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher9f4283f2011-02-16 21:17:04 -0500683 pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500684 pll->flags |= RADEON_PLL_USE_POST_DIV;
685 pll->post_div = args.v3.sOutput.ucPostDiv;
686 }
687 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500688 default:
689 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
690 return adjusted_clock;
691 }
692 break;
693 default:
694 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
695 return adjusted_clock;
696 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400697 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500698 return adjusted_clock;
699}
700
701union set_pixel_clock {
702 SET_PIXEL_CLOCK_PS_ALLOCATION base;
703 PIXEL_CLOCK_PARAMETERS v1;
704 PIXEL_CLOCK_PARAMETERS_V2 v2;
705 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500706 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500707 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500708};
709
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500710/* on DCE5, make sure the voltage is high enough to support the
711 * required disp clk.
712 */
713static void atombios_crtc_set_dcpll(struct drm_crtc *crtc,
714 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500715{
716 struct drm_device *dev = crtc->dev;
717 struct radeon_device *rdev = dev->dev_private;
718 u8 frev, crev;
719 int index;
720 union set_pixel_clock args;
721
722 memset(&args, 0, sizeof(args));
723
724 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400725 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
726 &crev))
727 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500728
729 switch (frev) {
730 case 1:
731 switch (crev) {
732 case 5:
733 /* if the default dcpll clock is specified,
734 * SetPixelClock provides the dividers
735 */
736 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500737 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500738 args.v5.ucPpll = ATOM_DCPLL;
739 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500740 case 6:
741 /* if the default dcpll clock is specified,
742 * SetPixelClock provides the dividers
743 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500744 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500745 args.v6.ucPpll = ATOM_DCPLL;
746 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500747 default:
748 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
749 return;
750 }
751 break;
752 default:
753 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
754 return;
755 }
756 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
757}
758
Alex Deucher37f90032010-06-11 17:58:38 -0400759static void atombios_crtc_program_pll(struct drm_crtc *crtc,
760 int crtc_id,
761 int pll_id,
762 u32 encoder_mode,
763 u32 encoder_id,
764 u32 clock,
765 u32 ref_div,
766 u32 fb_div,
767 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400768 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400769 int bpc,
770 bool ss_enabled,
771 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400772{
773 struct drm_device *dev = crtc->dev;
774 struct radeon_device *rdev = dev->dev_private;
775 u8 frev, crev;
776 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
777 union set_pixel_clock args;
778
779 memset(&args, 0, sizeof(args));
780
781 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
782 &crev))
783 return;
784
785 switch (frev) {
786 case 1:
787 switch (crev) {
788 case 1:
789 if (clock == ATOM_DISABLE)
790 return;
791 args.v1.usPixelClock = cpu_to_le16(clock / 10);
792 args.v1.usRefDiv = cpu_to_le16(ref_div);
793 args.v1.usFbDiv = cpu_to_le16(fb_div);
794 args.v1.ucFracFbDiv = frac_fb_div;
795 args.v1.ucPostDiv = post_div;
796 args.v1.ucPpll = pll_id;
797 args.v1.ucCRTC = crtc_id;
798 args.v1.ucRefDivSrc = 1;
799 break;
800 case 2:
801 args.v2.usPixelClock = cpu_to_le16(clock / 10);
802 args.v2.usRefDiv = cpu_to_le16(ref_div);
803 args.v2.usFbDiv = cpu_to_le16(fb_div);
804 args.v2.ucFracFbDiv = frac_fb_div;
805 args.v2.ucPostDiv = post_div;
806 args.v2.ucPpll = pll_id;
807 args.v2.ucCRTC = crtc_id;
808 args.v2.ucRefDivSrc = 1;
809 break;
810 case 3:
811 args.v3.usPixelClock = cpu_to_le16(clock / 10);
812 args.v3.usRefDiv = cpu_to_le16(ref_div);
813 args.v3.usFbDiv = cpu_to_le16(fb_div);
814 args.v3.ucFracFbDiv = frac_fb_div;
815 args.v3.ucPostDiv = post_div;
816 args.v3.ucPpll = pll_id;
817 args.v3.ucMiscInfo = (pll_id << 2);
Alex Deucher6f15c502011-05-20 12:36:12 -0400818 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
819 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400820 args.v3.ucTransmitterId = encoder_id;
821 args.v3.ucEncoderMode = encoder_mode;
822 break;
823 case 5:
824 args.v5.ucCRTC = crtc_id;
825 args.v5.usPixelClock = cpu_to_le16(clock / 10);
826 args.v5.ucRefDiv = ref_div;
827 args.v5.usFbDiv = cpu_to_le16(fb_div);
828 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
829 args.v5.ucPostDiv = post_div;
830 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400831 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
832 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400833 switch (bpc) {
834 case 8:
835 default:
836 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
837 break;
838 case 10:
839 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
840 break;
841 }
Alex Deucher37f90032010-06-11 17:58:38 -0400842 args.v5.ucTransmitterID = encoder_id;
843 args.v5.ucEncoderMode = encoder_mode;
844 args.v5.ucPpll = pll_id;
845 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500846 case 6:
847 args.v6.ulCrtcPclkFreq.ucCRTC = crtc_id;
848 args.v6.ulCrtcPclkFreq.ulPixelClock = cpu_to_le32(clock / 10);
849 args.v6.ucRefDiv = ref_div;
850 args.v6.usFbDiv = cpu_to_le16(fb_div);
851 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
852 args.v6.ucPostDiv = post_div;
853 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400854 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
855 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400856 switch (bpc) {
857 case 8:
858 default:
859 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
860 break;
861 case 10:
862 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP;
863 break;
864 case 12:
865 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP;
866 break;
867 case 16:
868 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
869 break;
870 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500871 args.v6.ucTransmitterID = encoder_id;
872 args.v6.ucEncoderMode = encoder_mode;
873 args.v6.ucPpll = pll_id;
874 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400875 default:
876 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
877 return;
878 }
879 break;
880 default:
881 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
882 return;
883 }
884
885 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
886}
887
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500888static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -0500889{
890 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
891 struct drm_device *dev = crtc->dev;
892 struct radeon_device *rdev = dev->dev_private;
893 struct drm_encoder *encoder = NULL;
894 struct radeon_encoder *radeon_encoder = NULL;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500895 u32 pll_clock = mode->clock;
896 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
897 struct radeon_pll *pll;
898 u32 adjusted_clock;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500899 int encoder_mode = 0;
Alex Deucherba032a52010-10-04 17:13:01 -0400900 struct radeon_atom_ss ss;
901 bool ss_enabled = false;
Alex Deucherdf271be2011-05-20 04:34:15 -0400902 int bpc = 8;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500903
Alex Deucher4eaeca32010-01-19 17:32:27 -0500904 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
905 if (encoder->crtc == crtc) {
906 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500907 encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500908 break;
909 }
910 }
911
912 if (!radeon_encoder)
913 return;
914
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500915 switch (radeon_crtc->pll_id) {
916 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -0500917 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500918 break;
919 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -0500920 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500921 break;
922 case ATOM_DCPLL:
923 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +1000924 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500925 pll = &rdev->clock.dcpll;
926 break;
927 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500928
Alex Deucherba032a52010-10-04 17:13:01 -0400929 if (radeon_encoder->active_device &
930 (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) {
931 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
932 struct drm_connector *connector =
933 radeon_get_connector_for_encoder(encoder);
934 struct radeon_connector *radeon_connector =
935 to_radeon_connector(connector);
936 struct radeon_connector_atom_dig *dig_connector =
937 radeon_connector->con_priv;
938 int dp_clock;
Alex Deucherdf271be2011-05-20 04:34:15 -0400939 bpc = connector->display_info.bpc;
Alex Deucherba032a52010-10-04 17:13:01 -0400940
941 switch (encoder_mode) {
942 case ATOM_ENCODER_MODE_DP:
943 /* DP/eDP */
944 dp_clock = dig_connector->dp_clock / 10;
945 if (radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400946 if (ASIC_IS_DCE4(rdev)) {
947 /* first try ASIC_INTERNAL_SS_ON_DP */
Alex Deucherba032a52010-10-04 17:13:01 -0400948 ss_enabled =
949 radeon_atombios_get_asic_ss_info(rdev, &ss,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400950 ASIC_INTERNAL_SS_ON_DP,
Alex Deucherba032a52010-10-04 17:13:01 -0400951 dp_clock);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400952 if (!ss_enabled)
953 ss_enabled =
954 radeon_atombios_get_asic_ss_info(rdev, &ss,
955 dig->lcd_ss_id,
956 dp_clock);
957 } else
Alex Deucherba032a52010-10-04 17:13:01 -0400958 ss_enabled =
959 radeon_atombios_get_ppll_ss_info(rdev, &ss,
960 dig->lcd_ss_id);
961 } else {
962 if (ASIC_IS_DCE4(rdev))
963 ss_enabled =
964 radeon_atombios_get_asic_ss_info(rdev, &ss,
965 ASIC_INTERNAL_SS_ON_DP,
966 dp_clock);
967 else {
968 if (dp_clock == 16200) {
969 ss_enabled =
970 radeon_atombios_get_ppll_ss_info(rdev, &ss,
971 ATOM_DP_SS_ID2);
972 if (!ss_enabled)
973 ss_enabled =
974 radeon_atombios_get_ppll_ss_info(rdev, &ss,
975 ATOM_DP_SS_ID1);
976 } else
977 ss_enabled =
978 radeon_atombios_get_ppll_ss_info(rdev, &ss,
979 ATOM_DP_SS_ID1);
980 }
981 }
982 break;
983 case ATOM_ENCODER_MODE_LVDS:
984 if (ASIC_IS_DCE4(rdev))
985 ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
986 dig->lcd_ss_id,
987 mode->clock / 10);
988 else
989 ss_enabled = radeon_atombios_get_ppll_ss_info(rdev, &ss,
990 dig->lcd_ss_id);
991 break;
992 case ATOM_ENCODER_MODE_DVI:
993 if (ASIC_IS_DCE4(rdev))
994 ss_enabled =
995 radeon_atombios_get_asic_ss_info(rdev, &ss,
996 ASIC_INTERNAL_SS_ON_TMDS,
997 mode->clock / 10);
998 break;
999 case ATOM_ENCODER_MODE_HDMI:
1000 if (ASIC_IS_DCE4(rdev))
1001 ss_enabled =
1002 radeon_atombios_get_asic_ss_info(rdev, &ss,
1003 ASIC_INTERNAL_SS_ON_HDMI,
1004 mode->clock / 10);
1005 break;
1006 default:
1007 break;
1008 }
1009 }
1010
Alex Deucher4eaeca32010-01-19 17:32:27 -05001011 /* adjust pixel clock as needed */
Alex Deucherba032a52010-10-04 17:13:01 -04001012 adjusted_clock = atombios_adjust_pll(crtc, mode, pll, ss_enabled, &ss);
Alex Deucher2606c882009-10-08 13:36:21 -04001013
Alex Deucher64146f82011-03-22 01:46:12 -04001014 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1015 /* TV seems to prefer the legacy algo on some boards */
1016 radeon_compute_pll_legacy(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
1017 &ref_div, &post_div);
1018 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher619efb12011-01-31 16:48:53 -05001019 radeon_compute_pll_avivo(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
1020 &ref_div, &post_div);
1021 else
1022 radeon_compute_pll_legacy(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
1023 &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001024
Alex Deucherba032a52010-10-04 17:13:01 -04001025 atombios_crtc_program_ss(crtc, ATOM_DISABLE, radeon_crtc->pll_id, &ss);
1026
Alex Deucher37f90032010-06-11 17:58:38 -04001027 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1028 encoder_mode, radeon_encoder->encoder_id, mode->clock,
Alex Deucher8e8e5232011-05-20 04:34:16 -04001029 ref_div, fb_div, frac_fb_div, post_div, bpc, ss_enabled, &ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001030
Alex Deucherba032a52010-10-04 17:13:01 -04001031 if (ss_enabled) {
1032 /* calculate ss amount and step size */
1033 if (ASIC_IS_DCE4(rdev)) {
1034 u32 step_size;
1035 u32 amount = (((fb_div * 10) + frac_fb_div) * ss.percentage) / 10000;
1036 ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001037 ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001038 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
1039 if (ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
1040 step_size = (4 * amount * ref_div * (ss.rate * 2048)) /
1041 (125 * 25 * pll->reference_freq / 100);
1042 else
1043 step_size = (2 * amount * ref_div * (ss.rate * 2048)) /
1044 (125 * 25 * pll->reference_freq / 100);
1045 ss.step = step_size;
1046 }
1047
1048 atombios_crtc_program_ss(crtc, ATOM_ENABLE, radeon_crtc->pll_id, &ss);
1049 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001050}
1051
Alex Deucherc9417bd2011-02-06 14:23:26 -05001052static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1053 struct drm_framebuffer *fb,
1054 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001055{
1056 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1057 struct drm_device *dev = crtc->dev;
1058 struct radeon_device *rdev = dev->dev_private;
1059 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001060 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001061 struct drm_gem_object *obj;
1062 struct radeon_bo *rbo;
1063 uint64_t fb_location;
1064 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001065 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucherfb9674b2011-04-02 09:15:50 -04001066 u32 tmp;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001067 int r;
1068
1069 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001070 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001071 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001072 return 0;
1073 }
1074
Chris Ball4dd19b02010-09-26 06:47:23 -05001075 if (atomic) {
1076 radeon_fb = to_radeon_framebuffer(fb);
1077 target_fb = fb;
1078 }
1079 else {
1080 radeon_fb = to_radeon_framebuffer(crtc->fb);
1081 target_fb = crtc->fb;
1082 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001083
Chris Ball4dd19b02010-09-26 06:47:23 -05001084 /* If atomic, assume fb object is pinned & idle & fenced and
1085 * just update base pointers
1086 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001087 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001088 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001089 r = radeon_bo_reserve(rbo, false);
1090 if (unlikely(r != 0))
1091 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001092
1093 if (atomic)
1094 fb_location = radeon_bo_gpu_offset(rbo);
1095 else {
1096 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1097 if (unlikely(r != 0)) {
1098 radeon_bo_unreserve(rbo);
1099 return -EINVAL;
1100 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001101 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001102
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001103 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1104 radeon_bo_unreserve(rbo);
1105
Chris Ball4dd19b02010-09-26 06:47:23 -05001106 switch (target_fb->bits_per_pixel) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001107 case 8:
1108 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1109 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1110 break;
1111 case 15:
1112 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1113 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
1114 break;
1115 case 16:
1116 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1117 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001118#ifdef __BIG_ENDIAN
1119 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1120#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001121 break;
1122 case 24:
1123 case 32:
1124 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1125 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001126#ifdef __BIG_ENDIAN
1127 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1128#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001129 break;
1130 default:
1131 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001132 target_fb->bits_per_pixel);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001133 return -EINVAL;
1134 }
1135
Alex Deucher97d66322010-05-20 12:12:48 -04001136 if (tiling_flags & RADEON_TILING_MACRO)
1137 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
1138 else if (tiling_flags & RADEON_TILING_MICRO)
1139 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1140
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001141 switch (radeon_crtc->crtc_id) {
1142 case 0:
1143 WREG32(AVIVO_D1VGA_CONTROL, 0);
1144 break;
1145 case 1:
1146 WREG32(AVIVO_D2VGA_CONTROL, 0);
1147 break;
1148 case 2:
1149 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1150 break;
1151 case 3:
1152 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1153 break;
1154 case 4:
1155 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1156 break;
1157 case 5:
1158 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1159 break;
1160 default:
1161 break;
1162 }
1163
1164 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1165 upper_32_bits(fb_location));
1166 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1167 upper_32_bits(fb_location));
1168 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1169 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1170 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1171 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1172 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001173 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001174
1175 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1176 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1177 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1178 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001179 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1180 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001181
Chris Ball4dd19b02010-09-26 06:47:23 -05001182 fb_pitch_pixels = target_fb->pitch / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001183 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1184 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1185
1186 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1187 crtc->mode.vdisplay);
1188 x &= ~3;
1189 y &= ~1;
1190 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1191 (x << 16) | y);
1192 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
1193 (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
1194
Alex Deucherfb9674b2011-04-02 09:15:50 -04001195 /* pageflip setup */
1196 /* make sure flip is at vb rather than hb */
1197 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1198 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1199 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1200
1201 /* set pageflip to happen anywhere in vblank interval */
1202 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1203
Chris Ball4dd19b02010-09-26 06:47:23 -05001204 if (!atomic && fb && fb != crtc->fb) {
1205 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001206 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001207 r = radeon_bo_reserve(rbo, false);
1208 if (unlikely(r != 0))
1209 return r;
1210 radeon_bo_unpin(rbo);
1211 radeon_bo_unreserve(rbo);
1212 }
1213
1214 /* Bytes per pixel may have changed */
1215 radeon_bandwidth_update(rdev);
1216
1217 return 0;
1218}
1219
Chris Ball4dd19b02010-09-26 06:47:23 -05001220static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1221 struct drm_framebuffer *fb,
1222 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001223{
1224 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1225 struct drm_device *dev = crtc->dev;
1226 struct radeon_device *rdev = dev->dev_private;
1227 struct radeon_framebuffer *radeon_fb;
1228 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001229 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001230 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001231 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001232 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001233 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucherfb9674b2011-04-02 09:15:50 -04001234 u32 tmp;
Jerome Glisse4c788672009-11-20 14:29:23 +01001235 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001236
Jerome Glisse2de3b482009-11-17 14:08:55 -08001237 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001238 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001239 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001240 return 0;
1241 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001242
Chris Ball4dd19b02010-09-26 06:47:23 -05001243 if (atomic) {
1244 radeon_fb = to_radeon_framebuffer(fb);
1245 target_fb = fb;
1246 }
1247 else {
1248 radeon_fb = to_radeon_framebuffer(crtc->fb);
1249 target_fb = crtc->fb;
1250 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001251
1252 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001253 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001254 r = radeon_bo_reserve(rbo, false);
1255 if (unlikely(r != 0))
1256 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001257
1258 /* If atomic, assume fb object is pinned & idle & fenced and
1259 * just update base pointers
1260 */
1261 if (atomic)
1262 fb_location = radeon_bo_gpu_offset(rbo);
1263 else {
1264 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1265 if (unlikely(r != 0)) {
1266 radeon_bo_unreserve(rbo);
1267 return -EINVAL;
1268 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001269 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001270 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1271 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001272
Chris Ball4dd19b02010-09-26 06:47:23 -05001273 switch (target_fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001274 case 8:
1275 fb_format =
1276 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1277 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1278 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001279 case 15:
1280 fb_format =
1281 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1282 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1283 break;
1284 case 16:
1285 fb_format =
1286 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1287 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001288#ifdef __BIG_ENDIAN
1289 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1290#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001291 break;
1292 case 24:
1293 case 32:
1294 fb_format =
1295 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1296 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001297#ifdef __BIG_ENDIAN
1298 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1299#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001300 break;
1301 default:
1302 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001303 target_fb->bits_per_pixel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001304 return -EINVAL;
1305 }
1306
Alex Deucher40c4ac12010-05-20 12:04:59 -04001307 if (rdev->family >= CHIP_R600) {
1308 if (tiling_flags & RADEON_TILING_MACRO)
1309 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1310 else if (tiling_flags & RADEON_TILING_MICRO)
1311 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1312 } else {
1313 if (tiling_flags & RADEON_TILING_MACRO)
1314 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001315
Alex Deucher40c4ac12010-05-20 12:04:59 -04001316 if (tiling_flags & RADEON_TILING_MICRO)
1317 fb_format |= AVIVO_D1GRPH_TILED;
1318 }
Dave Airliee024e112009-06-24 09:48:08 +10001319
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001320 if (radeon_crtc->crtc_id == 0)
1321 WREG32(AVIVO_D1VGA_CONTROL, 0);
1322 else
1323 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001324
1325 if (rdev->family >= CHIP_RV770) {
1326 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001327 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1328 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001329 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001330 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1331 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001332 }
1333 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001334 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1335 (u32) fb_location);
1336 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1337 radeon_crtc->crtc_offset, (u32) fb_location);
1338 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001339 if (rdev->family >= CHIP_R600)
1340 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001341
1342 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1343 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1344 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1345 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001346 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1347 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001348
Chris Ball4dd19b02010-09-26 06:47:23 -05001349 fb_pitch_pixels = target_fb->pitch / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001350 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1351 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1352
1353 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1354 crtc->mode.vdisplay);
1355 x &= ~3;
1356 y &= ~1;
1357 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1358 (x << 16) | y);
1359 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
1360 (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
1361
Alex Deucherfb9674b2011-04-02 09:15:50 -04001362 /* pageflip setup */
1363 /* make sure flip is at vb rather than hb */
1364 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1365 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1366 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1367
1368 /* set pageflip to happen anywhere in vblank interval */
1369 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1370
Chris Ball4dd19b02010-09-26 06:47:23 -05001371 if (!atomic && fb && fb != crtc->fb) {
1372 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001373 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001374 r = radeon_bo_reserve(rbo, false);
1375 if (unlikely(r != 0))
1376 return r;
1377 radeon_bo_unpin(rbo);
1378 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001379 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001380
1381 /* Bytes per pixel may have changed */
1382 radeon_bandwidth_update(rdev);
1383
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001384 return 0;
1385}
1386
Alex Deucher54f088a2010-01-19 16:34:01 -05001387int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1388 struct drm_framebuffer *old_fb)
1389{
1390 struct drm_device *dev = crtc->dev;
1391 struct radeon_device *rdev = dev->dev_private;
1392
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001393 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001394 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001395 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001396 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001397 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001398 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1399}
1400
1401int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1402 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001403 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001404{
1405 struct drm_device *dev = crtc->dev;
1406 struct radeon_device *rdev = dev->dev_private;
1407
1408 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001409 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001410 else if (ASIC_IS_AVIVO(rdev))
1411 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1412 else
1413 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001414}
1415
Alex Deucher615e0cb2010-01-20 16:22:53 -05001416/* properly set additional regs when using atombios */
1417static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1418{
1419 struct drm_device *dev = crtc->dev;
1420 struct radeon_device *rdev = dev->dev_private;
1421 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1422 u32 disp_merge_cntl;
1423
1424 switch (radeon_crtc->crtc_id) {
1425 case 0:
1426 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1427 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1428 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1429 break;
1430 case 1:
1431 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1432 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1433 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1434 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1435 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1436 break;
1437 }
1438}
1439
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001440static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1441{
1442 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1443 struct drm_device *dev = crtc->dev;
1444 struct radeon_device *rdev = dev->dev_private;
1445 struct drm_encoder *test_encoder;
1446 struct drm_crtc *test_crtc;
1447 uint32_t pll_in_use = 0;
1448
1449 if (ASIC_IS_DCE4(rdev)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001450 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1451 if (test_encoder->crtc && (test_encoder->crtc == crtc)) {
Alex Deucher86a94de2011-05-20 04:34:17 -04001452 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1453 * depending on the asic:
1454 * DCE4: PPLL or ext clock
1455 * DCE5: DCPLL or ext clock
1456 *
1457 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1458 * PPLL/DCPLL programming and only program the DP DTO for the
1459 * crtc virtual pixel clock.
1460 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001461 if (atombios_get_encoder_mode(test_encoder) == ATOM_ENCODER_MODE_DP) {
Alex Deucher86a94de2011-05-20 04:34:17 -04001462 if (ASIC_IS_DCE5(rdev) || rdev->clock.dp_extclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001463 return ATOM_PPLL_INVALID;
1464 }
1465 }
1466 }
1467
1468 /* otherwise, pick one of the plls */
1469 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1470 struct radeon_crtc *radeon_test_crtc;
1471
1472 if (crtc == test_crtc)
1473 continue;
1474
1475 radeon_test_crtc = to_radeon_crtc(test_crtc);
1476 if ((radeon_test_crtc->pll_id >= ATOM_PPLL1) &&
1477 (radeon_test_crtc->pll_id <= ATOM_PPLL2))
1478 pll_in_use |= (1 << radeon_test_crtc->pll_id);
1479 }
1480 if (!(pll_in_use & 1))
1481 return ATOM_PPLL1;
1482 return ATOM_PPLL2;
1483 } else
1484 return radeon_crtc->crtc_id;
1485
1486}
1487
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001488int atombios_crtc_mode_set(struct drm_crtc *crtc,
1489 struct drm_display_mode *mode,
1490 struct drm_display_mode *adjusted_mode,
1491 int x, int y, struct drm_framebuffer *old_fb)
1492{
1493 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1494 struct drm_device *dev = crtc->dev;
1495 struct radeon_device *rdev = dev->dev_private;
Alex Deucher54bfe492010-09-03 15:52:53 -04001496 struct drm_encoder *encoder;
1497 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001498
Alex Deucher54bfe492010-09-03 15:52:53 -04001499 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1500 /* find tv std */
1501 if (encoder->crtc == crtc) {
1502 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1503 if (radeon_encoder->active_device &
1504 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1505 is_tvcv = true;
1506 }
1507 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001508
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001509 /* always set DCPLL */
Alex Deucherba032a52010-10-04 17:13:01 -04001510 if (ASIC_IS_DCE4(rdev)) {
1511 struct radeon_atom_ss ss;
1512 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1513 ASIC_INTERNAL_SS_ON_DCPLL,
1514 rdev->clock.default_dispclk);
1515 if (ss_enabled)
1516 atombios_crtc_program_ss(crtc, ATOM_DISABLE, ATOM_DCPLL, &ss);
Alex Deucherf82b3dd2011-01-06 21:19:15 -05001517 /* XXX: DCE5, make sure voltage, dispclk is high enough */
1518 atombios_crtc_set_dcpll(crtc, rdev->clock.default_dispclk);
Alex Deucherba032a52010-10-04 17:13:01 -04001519 if (ss_enabled)
1520 atombios_crtc_program_ss(crtc, ATOM_ENABLE, ATOM_DCPLL, &ss);
1521 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001522 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001523
Alex Deucher54bfe492010-09-03 15:52:53 -04001524 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001525 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001526 else if (ASIC_IS_AVIVO(rdev)) {
1527 if (is_tvcv)
1528 atombios_crtc_set_timing(crtc, adjusted_mode);
1529 else
1530 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1531 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001532 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001533 if (radeon_crtc->crtc_id == 0)
1534 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001535 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001536 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001537 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001538 atombios_overscan_setup(crtc, mode, adjusted_mode);
1539 atombios_scaler_setup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001540 return 0;
1541}
1542
1543static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
1544 struct drm_display_mode *mode,
1545 struct drm_display_mode *adjusted_mode)
1546{
Alex Deucher03214bd52010-03-16 17:42:46 -04001547 struct drm_device *dev = crtc->dev;
1548 struct radeon_device *rdev = dev->dev_private;
1549
1550 /* adjust pm to upcoming mode change */
1551 radeon_pm_compute_clocks(rdev);
1552
Jerome Glissec93bb852009-07-13 21:04:08 +02001553 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1554 return false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001555 return true;
1556}
1557
1558static void atombios_crtc_prepare(struct drm_crtc *crtc)
1559{
Alex Deucher267364a2010-03-08 17:10:41 -05001560 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1561
1562 /* pick pll */
1563 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1564
Alex Deucher37b43902010-02-09 12:04:43 -05001565 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001566 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001567}
1568
1569static void atombios_crtc_commit(struct drm_crtc *crtc)
1570{
1571 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001572 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001573}
1574
Alex Deucher37f90032010-06-11 17:58:38 -04001575static void atombios_crtc_disable(struct drm_crtc *crtc)
1576{
1577 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher8e8e5232011-05-20 04:34:16 -04001578 struct radeon_atom_ss ss;
1579
Alex Deucher37f90032010-06-11 17:58:38 -04001580 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1581
1582 switch (radeon_crtc->pll_id) {
1583 case ATOM_PPLL1:
1584 case ATOM_PPLL2:
1585 /* disable the ppll */
1586 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04001587 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04001588 break;
1589 default:
1590 break;
1591 }
1592 radeon_crtc->pll_id = -1;
1593}
1594
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001595static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
1596 .dpms = atombios_crtc_dpms,
1597 .mode_fixup = atombios_crtc_mode_fixup,
1598 .mode_set = atombios_crtc_mode_set,
1599 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05001600 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001601 .prepare = atombios_crtc_prepare,
1602 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10001603 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04001604 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001605};
1606
1607void radeon_atombios_init_crtc(struct drm_device *dev,
1608 struct radeon_crtc *radeon_crtc)
1609{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001610 struct radeon_device *rdev = dev->dev_private;
1611
1612 if (ASIC_IS_DCE4(rdev)) {
1613 switch (radeon_crtc->crtc_id) {
1614 case 0:
1615 default:
Alex Deucher12d77982010-02-09 17:18:48 -05001616 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001617 break;
1618 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05001619 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001620 break;
1621 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05001622 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001623 break;
1624 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05001625 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001626 break;
1627 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05001628 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001629 break;
1630 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05001631 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001632 break;
1633 }
1634 } else {
1635 if (radeon_crtc->crtc_id == 1)
1636 radeon_crtc->crtc_offset =
1637 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
1638 else
1639 radeon_crtc->crtc_offset = 0;
1640 }
1641 radeon_crtc->pll_id = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001642 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
1643}