blob: a7edd0f2ac37d0ac8a9cd508f63d8320df354d27 [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>
29#include "radeon_fixed.h"
30#include "radeon.h"
31#include "atom.h"
32#include "atom-bits.h"
33
Dave Airlie4ce001a2009-08-13 16:32:14 +100034/* evil but including atombios.h is much worse */
35bool radeon_atom_get_tv_timings(struct radeon_device *rdev, int index,
36 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION *crtc_timing,
37 int32_t *pixel_clock);
Jerome Glissec93bb852009-07-13 21:04:08 +020038static void atombios_overscan_setup(struct drm_crtc *crtc,
39 struct drm_display_mode *mode,
40 struct drm_display_mode *adjusted_mode)
41{
42 struct drm_device *dev = crtc->dev;
43 struct radeon_device *rdev = dev->dev_private;
44 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
45 SET_CRTC_OVERSCAN_PS_ALLOCATION args;
46 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
47 int a1, a2;
48
49 memset(&args, 0, sizeof(args));
50
51 args.usOverscanRight = 0;
52 args.usOverscanLeft = 0;
53 args.usOverscanBottom = 0;
54 args.usOverscanTop = 0;
55 args.ucCRTC = radeon_crtc->crtc_id;
56
57 switch (radeon_crtc->rmx_type) {
58 case RMX_CENTER:
59 args.usOverscanTop = (adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2;
60 args.usOverscanBottom = (adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2;
61 args.usOverscanLeft = (adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2;
62 args.usOverscanRight = (adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2;
63 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
64 break;
65 case RMX_ASPECT:
66 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
67 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
68
69 if (a1 > a2) {
70 args.usOverscanLeft = (adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2;
71 args.usOverscanRight = (adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2;
72 } else if (a2 > a1) {
73 args.usOverscanLeft = (adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2;
74 args.usOverscanRight = (adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2;
75 }
76 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
77 break;
78 case RMX_FULL:
79 default:
80 args.usOverscanRight = 0;
81 args.usOverscanLeft = 0;
82 args.usOverscanBottom = 0;
83 args.usOverscanTop = 0;
84 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
85 break;
86 }
87}
88
89static void atombios_scaler_setup(struct drm_crtc *crtc)
90{
91 struct drm_device *dev = crtc->dev;
92 struct radeon_device *rdev = dev->dev_private;
93 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
94 ENABLE_SCALER_PS_ALLOCATION args;
95 int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
Dave Airlie4ce001a2009-08-13 16:32:14 +100096
Jerome Glissec93bb852009-07-13 21:04:08 +020097 /* fixme - fill in enc_priv for atom dac */
98 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100099 bool is_tv = false, is_cv = false;
100 struct drm_encoder *encoder;
Jerome Glissec93bb852009-07-13 21:04:08 +0200101
102 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
103 return;
104
Dave Airlie4ce001a2009-08-13 16:32:14 +1000105 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
106 /* find tv std */
107 if (encoder->crtc == crtc) {
108 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
109 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
110 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
111 tv_std = tv_dac->tv_std;
112 is_tv = true;
113 }
114 }
115 }
116
Jerome Glissec93bb852009-07-13 21:04:08 +0200117 memset(&args, 0, sizeof(args));
118
119 args.ucScaler = radeon_crtc->crtc_id;
120
Dave Airlie4ce001a2009-08-13 16:32:14 +1000121 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200122 switch (tv_std) {
123 case TV_STD_NTSC:
124 default:
125 args.ucTVStandard = ATOM_TV_NTSC;
126 break;
127 case TV_STD_PAL:
128 args.ucTVStandard = ATOM_TV_PAL;
129 break;
130 case TV_STD_PAL_M:
131 args.ucTVStandard = ATOM_TV_PALM;
132 break;
133 case TV_STD_PAL_60:
134 args.ucTVStandard = ATOM_TV_PAL60;
135 break;
136 case TV_STD_NTSC_J:
137 args.ucTVStandard = ATOM_TV_NTSCJ;
138 break;
139 case TV_STD_SCART_PAL:
140 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
141 break;
142 case TV_STD_SECAM:
143 args.ucTVStandard = ATOM_TV_SECAM;
144 break;
145 case TV_STD_PAL_CN:
146 args.ucTVStandard = ATOM_TV_PALCN;
147 break;
148 }
149 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000150 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200151 args.ucTVStandard = ATOM_TV_CV;
152 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
153 } else {
154 switch (radeon_crtc->rmx_type) {
155 case RMX_FULL:
156 args.ucEnable = ATOM_SCALER_EXPANSION;
157 break;
158 case RMX_CENTER:
159 args.ucEnable = ATOM_SCALER_CENTER;
160 break;
161 case RMX_ASPECT:
162 args.ucEnable = ATOM_SCALER_EXPANSION;
163 break;
164 default:
165 if (ASIC_IS_AVIVO(rdev))
166 args.ucEnable = ATOM_SCALER_DISABLE;
167 else
168 args.ucEnable = ATOM_SCALER_CENTER;
169 break;
170 }
171 }
172 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000173 if ((is_tv || is_cv)
174 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
175 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200176 }
177}
178
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200179static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
180{
181 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
182 struct drm_device *dev = crtc->dev;
183 struct radeon_device *rdev = dev->dev_private;
184 int index =
185 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
186 ENABLE_CRTC_PS_ALLOCATION args;
187
188 memset(&args, 0, sizeof(args));
189
190 args.ucCRTC = radeon_crtc->crtc_id;
191 args.ucEnable = lock;
192
193 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
194}
195
196static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
197{
198 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
199 struct drm_device *dev = crtc->dev;
200 struct radeon_device *rdev = dev->dev_private;
201 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
202 ENABLE_CRTC_PS_ALLOCATION args;
203
204 memset(&args, 0, sizeof(args));
205
206 args.ucCRTC = radeon_crtc->crtc_id;
207 args.ucEnable = state;
208
209 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
210}
211
212static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
213{
214 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
215 struct drm_device *dev = crtc->dev;
216 struct radeon_device *rdev = dev->dev_private;
217 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
218 ENABLE_CRTC_PS_ALLOCATION args;
219
220 memset(&args, 0, sizeof(args));
221
222 args.ucCRTC = radeon_crtc->crtc_id;
223 args.ucEnable = state;
224
225 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
226}
227
228static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
229{
230 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
231 struct drm_device *dev = crtc->dev;
232 struct radeon_device *rdev = dev->dev_private;
233 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
234 BLANK_CRTC_PS_ALLOCATION args;
235
236 memset(&args, 0, sizeof(args));
237
238 args.ucCRTC = radeon_crtc->crtc_id;
239 args.ucBlanking = state;
240
241 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
242}
243
244void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
245{
246 struct drm_device *dev = crtc->dev;
247 struct radeon_device *rdev = dev->dev_private;
248
249 switch (mode) {
250 case DRM_MODE_DPMS_ON:
251 if (ASIC_IS_DCE3(rdev))
252 atombios_enable_crtc_memreq(crtc, 1);
253 atombios_enable_crtc(crtc, 1);
254 atombios_blank_crtc(crtc, 0);
255 break;
256 case DRM_MODE_DPMS_STANDBY:
257 case DRM_MODE_DPMS_SUSPEND:
258 case DRM_MODE_DPMS_OFF:
259 atombios_blank_crtc(crtc, 1);
260 atombios_enable_crtc(crtc, 0);
261 if (ASIC_IS_DCE3(rdev))
262 atombios_enable_crtc_memreq(crtc, 0);
263 break;
264 }
265
266 if (mode != DRM_MODE_DPMS_OFF) {
267 radeon_crtc_load_lut(crtc);
268 }
269}
270
271static void
272atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
273 SET_CRTC_USING_DTD_TIMING_PARAMETERS * crtc_param)
274{
275 struct drm_device *dev = crtc->dev;
276 struct radeon_device *rdev = dev->dev_private;
277 SET_CRTC_USING_DTD_TIMING_PARAMETERS conv_param;
278 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
279
280 conv_param.usH_Size = cpu_to_le16(crtc_param->usH_Size);
281 conv_param.usH_Blanking_Time =
282 cpu_to_le16(crtc_param->usH_Blanking_Time);
283 conv_param.usV_Size = cpu_to_le16(crtc_param->usV_Size);
284 conv_param.usV_Blanking_Time =
285 cpu_to_le16(crtc_param->usV_Blanking_Time);
286 conv_param.usH_SyncOffset = cpu_to_le16(crtc_param->usH_SyncOffset);
287 conv_param.usH_SyncWidth = cpu_to_le16(crtc_param->usH_SyncWidth);
288 conv_param.usV_SyncOffset = cpu_to_le16(crtc_param->usV_SyncOffset);
289 conv_param.usV_SyncWidth = cpu_to_le16(crtc_param->usV_SyncWidth);
290 conv_param.susModeMiscInfo.usAccess =
291 cpu_to_le16(crtc_param->susModeMiscInfo.usAccess);
292 conv_param.ucCRTC = crtc_param->ucCRTC;
293
294 printk("executing set crtc dtd timing\n");
295 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&conv_param);
296}
297
298void atombios_crtc_set_timing(struct drm_crtc *crtc,
299 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION *
300 crtc_param)
301{
302 struct drm_device *dev = crtc->dev;
303 struct radeon_device *rdev = dev->dev_private;
304 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION conv_param;
305 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
306
307 conv_param.usH_Total = cpu_to_le16(crtc_param->usH_Total);
308 conv_param.usH_Disp = cpu_to_le16(crtc_param->usH_Disp);
309 conv_param.usH_SyncStart = cpu_to_le16(crtc_param->usH_SyncStart);
310 conv_param.usH_SyncWidth = cpu_to_le16(crtc_param->usH_SyncWidth);
311 conv_param.usV_Total = cpu_to_le16(crtc_param->usV_Total);
312 conv_param.usV_Disp = cpu_to_le16(crtc_param->usV_Disp);
313 conv_param.usV_SyncStart = cpu_to_le16(crtc_param->usV_SyncStart);
314 conv_param.usV_SyncWidth = cpu_to_le16(crtc_param->usV_SyncWidth);
315 conv_param.susModeMiscInfo.usAccess =
316 cpu_to_le16(crtc_param->susModeMiscInfo.usAccess);
317 conv_param.ucCRTC = crtc_param->ucCRTC;
318 conv_param.ucOverscanRight = crtc_param->ucOverscanRight;
319 conv_param.ucOverscanLeft = crtc_param->ucOverscanLeft;
320 conv_param.ucOverscanBottom = crtc_param->ucOverscanBottom;
321 conv_param.ucOverscanTop = crtc_param->ucOverscanTop;
322 conv_param.ucReserved = crtc_param->ucReserved;
323
324 printk("executing set crtc timing\n");
325 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&conv_param);
326}
327
328void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
329{
330 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
331 struct drm_device *dev = crtc->dev;
332 struct radeon_device *rdev = dev->dev_private;
333 struct drm_encoder *encoder = NULL;
334 struct radeon_encoder *radeon_encoder = NULL;
335 uint8_t frev, crev;
336 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
337 SET_PIXEL_CLOCK_PS_ALLOCATION args;
338 PIXEL_CLOCK_PARAMETERS *spc1_ptr;
339 PIXEL_CLOCK_PARAMETERS_V2 *spc2_ptr;
340 PIXEL_CLOCK_PARAMETERS_V3 *spc3_ptr;
341 uint32_t sclock = mode->clock;
342 uint32_t ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
343 struct radeon_pll *pll;
344 int pll_flags = 0;
345
346 memset(&args, 0, sizeof(args));
347
348 if (ASIC_IS_AVIVO(rdev)) {
349 uint32_t ss_cntl;
350
Alex Deuchereb1300b2009-07-13 11:09:56 -0400351 if ((rdev->family == CHIP_RS600) ||
352 (rdev->family == CHIP_RS690) ||
353 (rdev->family == CHIP_RS740))
354 pll_flags |= (RADEON_PLL_USE_FRAC_FB_DIV |
355 RADEON_PLL_PREFER_CLOSEST_LOWER);
356
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200357 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
358 pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
359 else
360 pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
361
362 /* disable spread spectrum clocking for now -- thanks Hedy Lamarr */
363 if (radeon_crtc->crtc_id == 0) {
364 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
365 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl & ~1);
366 } else {
367 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
368 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl & ~1);
369 }
370 } else {
371 pll_flags |= RADEON_PLL_LEGACY;
372
373 if (mode->clock > 200000) /* range limits??? */
374 pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
375 else
376 pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
377
378 }
379
380 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
381 if (encoder->crtc == crtc) {
382 if (!ASIC_IS_AVIVO(rdev)) {
383 if (encoder->encoder_type !=
384 DRM_MODE_ENCODER_DAC)
385 pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
386 if (!ASIC_IS_AVIVO(rdev)
387 && (encoder->encoder_type ==
388 DRM_MODE_ENCODER_LVDS))
389 pll_flags |= RADEON_PLL_USE_REF_DIV;
390 }
391 radeon_encoder = to_radeon_encoder(encoder);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000392 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200393 }
394 }
395
396 if (radeon_crtc->crtc_id == 0)
397 pll = &rdev->clock.p1pll;
398 else
399 pll = &rdev->clock.p2pll;
400
401 radeon_compute_pll(pll, mode->clock, &sclock, &fb_div, &frac_fb_div,
402 &ref_div, &post_div, pll_flags);
403
404 atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
405 &crev);
406
407 switch (frev) {
408 case 1:
409 switch (crev) {
410 case 1:
411 spc1_ptr = (PIXEL_CLOCK_PARAMETERS *) & args.sPCLKInput;
412 spc1_ptr->usPixelClock = cpu_to_le16(sclock);
413 spc1_ptr->usRefDiv = cpu_to_le16(ref_div);
414 spc1_ptr->usFbDiv = cpu_to_le16(fb_div);
415 spc1_ptr->ucFracFbDiv = frac_fb_div;
416 spc1_ptr->ucPostDiv = post_div;
417 spc1_ptr->ucPpll =
418 radeon_crtc->crtc_id ? ATOM_PPLL2 : ATOM_PPLL1;
419 spc1_ptr->ucCRTC = radeon_crtc->crtc_id;
420 spc1_ptr->ucRefDivSrc = 1;
421 break;
422 case 2:
423 spc2_ptr =
424 (PIXEL_CLOCK_PARAMETERS_V2 *) & args.sPCLKInput;
425 spc2_ptr->usPixelClock = cpu_to_le16(sclock);
426 spc2_ptr->usRefDiv = cpu_to_le16(ref_div);
427 spc2_ptr->usFbDiv = cpu_to_le16(fb_div);
428 spc2_ptr->ucFracFbDiv = frac_fb_div;
429 spc2_ptr->ucPostDiv = post_div;
430 spc2_ptr->ucPpll =
431 radeon_crtc->crtc_id ? ATOM_PPLL2 : ATOM_PPLL1;
432 spc2_ptr->ucCRTC = radeon_crtc->crtc_id;
433 spc2_ptr->ucRefDivSrc = 1;
434 break;
435 case 3:
436 if (!encoder)
437 return;
438 spc3_ptr =
439 (PIXEL_CLOCK_PARAMETERS_V3 *) & args.sPCLKInput;
440 spc3_ptr->usPixelClock = cpu_to_le16(sclock);
441 spc3_ptr->usRefDiv = cpu_to_le16(ref_div);
442 spc3_ptr->usFbDiv = cpu_to_le16(fb_div);
443 spc3_ptr->ucFracFbDiv = frac_fb_div;
444 spc3_ptr->ucPostDiv = post_div;
445 spc3_ptr->ucPpll =
446 radeon_crtc->crtc_id ? ATOM_PPLL2 : ATOM_PPLL1;
447 spc3_ptr->ucMiscInfo = (radeon_crtc->crtc_id << 2);
448 spc3_ptr->ucTransmitterId = radeon_encoder->encoder_id;
449 spc3_ptr->ucEncoderMode =
450 atombios_get_encoder_mode(encoder);
451 break;
452 default:
453 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
454 return;
455 }
456 break;
457 default:
458 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
459 return;
460 }
461
462 printk("executing set pll\n");
463 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
464}
465
466int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
467 struct drm_framebuffer *old_fb)
468{
469 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
470 struct drm_device *dev = crtc->dev;
471 struct radeon_device *rdev = dev->dev_private;
472 struct radeon_framebuffer *radeon_fb;
473 struct drm_gem_object *obj;
474 struct drm_radeon_gem_object *obj_priv;
475 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +1000476 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200477
478 if (!crtc->fb)
479 return -EINVAL;
480
481 radeon_fb = to_radeon_framebuffer(crtc->fb);
482
483 obj = radeon_fb->obj;
484 obj_priv = obj->driver_private;
485
486 if (radeon_gem_object_pin(obj, RADEON_GEM_DOMAIN_VRAM, &fb_location)) {
487 return -EINVAL;
488 }
489
490 switch (crtc->fb->bits_per_pixel) {
491 case 15:
492 fb_format =
493 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
494 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
495 break;
496 case 16:
497 fb_format =
498 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
499 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
500 break;
501 case 24:
502 case 32:
503 fb_format =
504 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
505 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
506 break;
507 default:
508 DRM_ERROR("Unsupported screen depth %d\n",
509 crtc->fb->bits_per_pixel);
510 return -EINVAL;
511 }
512
Dave Airliee024e112009-06-24 09:48:08 +1000513 radeon_object_get_tiling_flags(obj->driver_private,
514 &tiling_flags, NULL);
515 if (tiling_flags & RADEON_TILING_MACRO)
516 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
517
518 if (tiling_flags & RADEON_TILING_MICRO)
519 fb_format |= AVIVO_D1GRPH_TILED;
520
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200521 if (radeon_crtc->crtc_id == 0)
522 WREG32(AVIVO_D1VGA_CONTROL, 0);
523 else
524 WREG32(AVIVO_D2VGA_CONTROL, 0);
525 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
526 (u32) fb_location);
527 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
528 radeon_crtc->crtc_offset, (u32) fb_location);
529 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
530
531 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
532 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
533 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
534 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
535 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, crtc->fb->width);
536 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, crtc->fb->height);
537
538 fb_pitch_pixels = crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8);
539 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
540 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
541
542 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
543 crtc->mode.vdisplay);
544 x &= ~3;
545 y &= ~1;
546 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
547 (x << 16) | y);
548 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
549 (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
550
551 if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE)
552 WREG32(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset,
553 AVIVO_D1MODE_INTERLEAVE_EN);
554 else
555 WREG32(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset, 0);
556
557 if (old_fb && old_fb != crtc->fb) {
558 radeon_fb = to_radeon_framebuffer(old_fb);
559 radeon_gem_object_unpin(radeon_fb->obj);
560 }
561 return 0;
562}
563
564int atombios_crtc_mode_set(struct drm_crtc *crtc,
565 struct drm_display_mode *mode,
566 struct drm_display_mode *adjusted_mode,
567 int x, int y, struct drm_framebuffer *old_fb)
568{
569 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
570 struct drm_device *dev = crtc->dev;
571 struct radeon_device *rdev = dev->dev_private;
572 struct drm_encoder *encoder;
573 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION crtc_timing;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000574 int need_tv_timings = 0;
575 bool ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200576
577 /* TODO color tiling */
578 memset(&crtc_timing, 0, sizeof(crtc_timing));
579
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200580 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Dave Airlie4ce001a2009-08-13 16:32:14 +1000581 /* find tv std */
582 if (encoder->crtc == crtc) {
583 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200584
Dave Airlie4ce001a2009-08-13 16:32:14 +1000585 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
586 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
587 if (tv_dac) {
588 if (tv_dac->tv_std == TV_STD_NTSC ||
589 tv_dac->tv_std == TV_STD_NTSC_J ||
590 tv_dac->tv_std == TV_STD_PAL_M)
591 need_tv_timings = 1;
592 else
593 need_tv_timings = 2;
594 break;
595 }
596 }
597 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200598 }
599
600 crtc_timing.ucCRTC = radeon_crtc->crtc_id;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000601 if (need_tv_timings) {
602 ret = radeon_atom_get_tv_timings(rdev, need_tv_timings - 1,
603 &crtc_timing, &adjusted_mode->clock);
604 if (ret == false)
605 need_tv_timings = 0;
606 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200607
Dave Airlie4ce001a2009-08-13 16:32:14 +1000608 if (!need_tv_timings) {
609 crtc_timing.usH_Total = adjusted_mode->crtc_htotal;
610 crtc_timing.usH_Disp = adjusted_mode->crtc_hdisplay;
611 crtc_timing.usH_SyncStart = adjusted_mode->crtc_hsync_start;
612 crtc_timing.usH_SyncWidth =
613 adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200614
Dave Airlie4ce001a2009-08-13 16:32:14 +1000615 crtc_timing.usV_Total = adjusted_mode->crtc_vtotal;
616 crtc_timing.usV_Disp = adjusted_mode->crtc_vdisplay;
617 crtc_timing.usV_SyncStart = adjusted_mode->crtc_vsync_start;
618 crtc_timing.usV_SyncWidth =
619 adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200620
Dave Airlie4ce001a2009-08-13 16:32:14 +1000621 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
622 crtc_timing.susModeMiscInfo.usAccess |= ATOM_VSYNC_POLARITY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200623
Dave Airlie4ce001a2009-08-13 16:32:14 +1000624 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
625 crtc_timing.susModeMiscInfo.usAccess |= ATOM_HSYNC_POLARITY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200626
Dave Airlie4ce001a2009-08-13 16:32:14 +1000627 if (adjusted_mode->flags & DRM_MODE_FLAG_CSYNC)
628 crtc_timing.susModeMiscInfo.usAccess |= ATOM_COMPOSITESYNC;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200629
Dave Airlie4ce001a2009-08-13 16:32:14 +1000630 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
631 crtc_timing.susModeMiscInfo.usAccess |= ATOM_INTERLACE;
632
633 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
634 crtc_timing.susModeMiscInfo.usAccess |= ATOM_DOUBLE_CLOCK_MODE;
635 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200636
637 atombios_crtc_set_pll(crtc, adjusted_mode);
638 atombios_crtc_set_timing(crtc, &crtc_timing);
639
640 if (ASIC_IS_AVIVO(rdev))
641 atombios_crtc_set_base(crtc, x, y, old_fb);
642 else {
643 if (radeon_crtc->crtc_id == 0) {
644 SET_CRTC_USING_DTD_TIMING_PARAMETERS crtc_dtd_timing;
645 memset(&crtc_dtd_timing, 0, sizeof(crtc_dtd_timing));
646
647 /* setup FP shadow regs on R4xx */
648 crtc_dtd_timing.ucCRTC = radeon_crtc->crtc_id;
649 crtc_dtd_timing.usH_Size = adjusted_mode->crtc_hdisplay;
650 crtc_dtd_timing.usV_Size = adjusted_mode->crtc_vdisplay;
651 crtc_dtd_timing.usH_Blanking_Time =
652 adjusted_mode->crtc_hblank_end -
653 adjusted_mode->crtc_hdisplay;
654 crtc_dtd_timing.usV_Blanking_Time =
655 adjusted_mode->crtc_vblank_end -
656 adjusted_mode->crtc_vdisplay;
657 crtc_dtd_timing.usH_SyncOffset =
658 adjusted_mode->crtc_hsync_start -
659 adjusted_mode->crtc_hdisplay;
660 crtc_dtd_timing.usV_SyncOffset =
661 adjusted_mode->crtc_vsync_start -
662 adjusted_mode->crtc_vdisplay;
663 crtc_dtd_timing.usH_SyncWidth =
664 adjusted_mode->crtc_hsync_end -
665 adjusted_mode->crtc_hsync_start;
666 crtc_dtd_timing.usV_SyncWidth =
667 adjusted_mode->crtc_vsync_end -
668 adjusted_mode->crtc_vsync_start;
669 /* crtc_dtd_timing.ucH_Border = adjusted_mode->crtc_hborder; */
670 /* crtc_dtd_timing.ucV_Border = adjusted_mode->crtc_vborder; */
671
672 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
673 crtc_dtd_timing.susModeMiscInfo.usAccess |=
674 ATOM_VSYNC_POLARITY;
675
676 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
677 crtc_dtd_timing.susModeMiscInfo.usAccess |=
678 ATOM_HSYNC_POLARITY;
679
680 if (adjusted_mode->flags & DRM_MODE_FLAG_CSYNC)
681 crtc_dtd_timing.susModeMiscInfo.usAccess |=
682 ATOM_COMPOSITESYNC;
683
684 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
685 crtc_dtd_timing.susModeMiscInfo.usAccess |=
686 ATOM_INTERLACE;
687
688 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
689 crtc_dtd_timing.susModeMiscInfo.usAccess |=
690 ATOM_DOUBLE_CLOCK_MODE;
691
692 atombios_set_crtc_dtd_timing(crtc, &crtc_dtd_timing);
693 }
694 radeon_crtc_set_base(crtc, x, y, old_fb);
695 radeon_legacy_atom_set_surface(crtc);
696 }
Jerome Glissec93bb852009-07-13 21:04:08 +0200697 atombios_overscan_setup(crtc, mode, adjusted_mode);
698 atombios_scaler_setup(crtc);
699 radeon_bandwidth_update(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200700 return 0;
701}
702
703static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
704 struct drm_display_mode *mode,
705 struct drm_display_mode *adjusted_mode)
706{
Jerome Glissec93bb852009-07-13 21:04:08 +0200707 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
708 return false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200709 return true;
710}
711
712static void atombios_crtc_prepare(struct drm_crtc *crtc)
713{
714 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
715 atombios_lock_crtc(crtc, 1);
716}
717
718static void atombios_crtc_commit(struct drm_crtc *crtc)
719{
720 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
721 atombios_lock_crtc(crtc, 0);
722}
723
724static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
725 .dpms = atombios_crtc_dpms,
726 .mode_fixup = atombios_crtc_mode_fixup,
727 .mode_set = atombios_crtc_mode_set,
728 .mode_set_base = atombios_crtc_set_base,
729 .prepare = atombios_crtc_prepare,
730 .commit = atombios_crtc_commit,
731};
732
733void radeon_atombios_init_crtc(struct drm_device *dev,
734 struct radeon_crtc *radeon_crtc)
735{
736 if (radeon_crtc->crtc_id == 1)
737 radeon_crtc->crtc_offset =
738 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
739 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
740}