radeon_legacy_crtc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /*
  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 <drm/drm_fixed.h>
  30. #include "radeon.h"
  31. #include "atom.h"
  32. static void radeon_overscan_setup(struct drm_crtc *crtc,
  33. struct drm_display_mode *mode)
  34. {
  35. struct drm_device *dev = crtc->dev;
  36. struct radeon_device *rdev = dev->dev_private;
  37. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  38. WREG32(RADEON_OVR_CLR + radeon_crtc->crtc_offset, 0);
  39. WREG32(RADEON_OVR_WID_LEFT_RIGHT + radeon_crtc->crtc_offset, 0);
  40. WREG32(RADEON_OVR_WID_TOP_BOTTOM + radeon_crtc->crtc_offset, 0);
  41. }
  42. static void radeon_legacy_rmx_mode_set(struct drm_crtc *crtc,
  43. struct drm_display_mode *mode)
  44. {
  45. struct drm_device *dev = crtc->dev;
  46. struct radeon_device *rdev = dev->dev_private;
  47. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  48. int xres = mode->hdisplay;
  49. int yres = mode->vdisplay;
  50. bool hscale = true, vscale = true;
  51. int hsync_wid;
  52. int vsync_wid;
  53. int hsync_start;
  54. int blank_width;
  55. u32 scale, inc, crtc_more_cntl;
  56. u32 fp_horz_stretch, fp_vert_stretch, fp_horz_vert_active;
  57. u32 fp_h_sync_strt_wid, fp_crtc_h_total_disp;
  58. u32 fp_v_sync_strt_wid, fp_crtc_v_total_disp;
  59. struct drm_display_mode *native_mode = &radeon_crtc->native_mode;
  60. fp_vert_stretch = RREG32(RADEON_FP_VERT_STRETCH) &
  61. (RADEON_VERT_STRETCH_RESERVED |
  62. RADEON_VERT_AUTO_RATIO_INC);
  63. fp_horz_stretch = RREG32(RADEON_FP_HORZ_STRETCH) &
  64. (RADEON_HORZ_FP_LOOP_STRETCH |
  65. RADEON_HORZ_AUTO_RATIO_INC);
  66. crtc_more_cntl = 0;
  67. if ((rdev->family == CHIP_RS100) ||
  68. (rdev->family == CHIP_RS200)) {
  69. /* This is to workaround the asic bug for RMX, some versions
  70. of BIOS dosen't have this register initialized correctly. */
  71. crtc_more_cntl |= RADEON_CRTC_H_CUTOFF_ACTIVE_EN;
  72. }
  73. fp_crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
  74. | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
  75. hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
  76. if (!hsync_wid)
  77. hsync_wid = 1;
  78. hsync_start = mode->crtc_hsync_start - 8;
  79. fp_h_sync_strt_wid = ((hsync_start & 0x1fff)
  80. | ((hsync_wid & 0x3f) << 16)
  81. | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
  82. ? RADEON_CRTC_H_SYNC_POL
  83. : 0));
  84. fp_crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
  85. | ((mode->crtc_vdisplay - 1) << 16));
  86. vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
  87. if (!vsync_wid)
  88. vsync_wid = 1;
  89. fp_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
  90. | ((vsync_wid & 0x1f) << 16)
  91. | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
  92. ? RADEON_CRTC_V_SYNC_POL
  93. : 0));
  94. fp_horz_vert_active = 0;
  95. if (native_mode->hdisplay == 0 ||
  96. native_mode->vdisplay == 0) {
  97. hscale = false;
  98. vscale = false;
  99. } else {
  100. if (xres > native_mode->hdisplay)
  101. xres = native_mode->hdisplay;
  102. if (yres > native_mode->vdisplay)
  103. yres = native_mode->vdisplay;
  104. if (xres == native_mode->hdisplay)
  105. hscale = false;
  106. if (yres == native_mode->vdisplay)
  107. vscale = false;
  108. }
  109. switch (radeon_crtc->rmx_type) {
  110. case RMX_FULL:
  111. case RMX_ASPECT:
  112. if (!hscale)
  113. fp_horz_stretch |= ((xres/8-1) << 16);
  114. else {
  115. inc = (fp_horz_stretch & RADEON_HORZ_AUTO_RATIO_INC) ? 1 : 0;
  116. scale = ((xres + inc) * RADEON_HORZ_STRETCH_RATIO_MAX)
  117. / native_mode->hdisplay + 1;
  118. fp_horz_stretch |= (((scale) & RADEON_HORZ_STRETCH_RATIO_MASK) |
  119. RADEON_HORZ_STRETCH_BLEND |
  120. RADEON_HORZ_STRETCH_ENABLE |
  121. ((native_mode->hdisplay/8-1) << 16));
  122. }
  123. if (!vscale)
  124. fp_vert_stretch |= ((yres-1) << 12);
  125. else {
  126. inc = (fp_vert_stretch & RADEON_VERT_AUTO_RATIO_INC) ? 1 : 0;
  127. scale = ((yres + inc) * RADEON_VERT_STRETCH_RATIO_MAX)
  128. / native_mode->vdisplay + 1;
  129. fp_vert_stretch |= (((scale) & RADEON_VERT_STRETCH_RATIO_MASK) |
  130. RADEON_VERT_STRETCH_ENABLE |
  131. RADEON_VERT_STRETCH_BLEND |
  132. ((native_mode->vdisplay-1) << 12));
  133. }
  134. break;
  135. case RMX_CENTER:
  136. fp_horz_stretch |= ((xres/8-1) << 16);
  137. fp_vert_stretch |= ((yres-1) << 12);
  138. crtc_more_cntl |= (RADEON_CRTC_AUTO_HORZ_CENTER_EN |
  139. RADEON_CRTC_AUTO_VERT_CENTER_EN);
  140. blank_width = (mode->crtc_hblank_end - mode->crtc_hblank_start) / 8;
  141. if (blank_width > 110)
  142. blank_width = 110;
  143. fp_crtc_h_total_disp = (((blank_width) & 0x3ff)
  144. | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
  145. hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
  146. if (!hsync_wid)
  147. hsync_wid = 1;
  148. fp_h_sync_strt_wid = ((((mode->crtc_hsync_start - mode->crtc_hblank_start) / 8) & 0x1fff)
  149. | ((hsync_wid & 0x3f) << 16)
  150. | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
  151. ? RADEON_CRTC_H_SYNC_POL
  152. : 0));
  153. fp_crtc_v_total_disp = (((mode->crtc_vblank_end - mode->crtc_vblank_start) & 0xffff)
  154. | ((mode->crtc_vdisplay - 1) << 16));
  155. vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
  156. if (!vsync_wid)
  157. vsync_wid = 1;
  158. fp_v_sync_strt_wid = ((((mode->crtc_vsync_start - mode->crtc_vblank_start) & 0xfff)
  159. | ((vsync_wid & 0x1f) << 16)
  160. | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
  161. ? RADEON_CRTC_V_SYNC_POL
  162. : 0)));
  163. fp_horz_vert_active = (((native_mode->vdisplay) & 0xfff) |
  164. (((native_mode->hdisplay / 8) & 0x1ff) << 16));
  165. break;
  166. case RMX_OFF:
  167. default:
  168. fp_horz_stretch |= ((xres/8-1) << 16);
  169. fp_vert_stretch |= ((yres-1) << 12);
  170. break;
  171. }
  172. WREG32(RADEON_FP_HORZ_STRETCH, fp_horz_stretch);
  173. WREG32(RADEON_FP_VERT_STRETCH, fp_vert_stretch);
  174. WREG32(RADEON_CRTC_MORE_CNTL, crtc_more_cntl);
  175. WREG32(RADEON_FP_HORZ_VERT_ACTIVE, fp_horz_vert_active);
  176. WREG32(RADEON_FP_H_SYNC_STRT_WID, fp_h_sync_strt_wid);
  177. WREG32(RADEON_FP_V_SYNC_STRT_WID, fp_v_sync_strt_wid);
  178. WREG32(RADEON_FP_CRTC_H_TOTAL_DISP, fp_crtc_h_total_disp);
  179. WREG32(RADEON_FP_CRTC_V_TOTAL_DISP, fp_crtc_v_total_disp);
  180. }
  181. void radeon_restore_common_regs(struct drm_device *dev)
  182. {
  183. /* don't need this yet */
  184. }
  185. static void radeon_pll_wait_for_read_update_complete(struct drm_device *dev)
  186. {
  187. struct radeon_device *rdev = dev->dev_private;
  188. int i = 0;
  189. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  190. the cause yet, but this workaround will mask the problem for now.
  191. Other chips usually will pass at the very first test, so the
  192. workaround shouldn't have any effect on them. */
  193. for (i = 0;
  194. (i < 10000 &&
  195. RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
  196. i++);
  197. }
  198. static void radeon_pll_write_update(struct drm_device *dev)
  199. {
  200. struct radeon_device *rdev = dev->dev_private;
  201. while (RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
  202. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  203. RADEON_PPLL_ATOMIC_UPDATE_W,
  204. ~(RADEON_PPLL_ATOMIC_UPDATE_W));
  205. }
  206. static void radeon_pll2_wait_for_read_update_complete(struct drm_device *dev)
  207. {
  208. struct radeon_device *rdev = dev->dev_private;
  209. int i = 0;
  210. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  211. the cause yet, but this workaround will mask the problem for now.
  212. Other chips usually will pass at the very first test, so the
  213. workaround shouldn't have any effect on them. */
  214. for (i = 0;
  215. (i < 10000 &&
  216. RREG32_PLL(RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
  217. i++);
  218. }
  219. static void radeon_pll2_write_update(struct drm_device *dev)
  220. {
  221. struct radeon_device *rdev = dev->dev_private;
  222. while (RREG32_PLL(RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
  223. WREG32_PLL_P(RADEON_P2PLL_REF_DIV,
  224. RADEON_P2PLL_ATOMIC_UPDATE_W,
  225. ~(RADEON_P2PLL_ATOMIC_UPDATE_W));
  226. }
  227. static uint8_t radeon_compute_pll_gain(uint16_t ref_freq, uint16_t ref_div,
  228. uint16_t fb_div)
  229. {
  230. unsigned int vcoFreq;
  231. if (!ref_div)
  232. return 1;
  233. vcoFreq = ((unsigned)ref_freq * fb_div) / ref_div;
  234. /*
  235. * This is horribly crude: the VCO frequency range is divided into
  236. * 3 parts, each part having a fixed PLL gain value.
  237. */
  238. if (vcoFreq >= 30000)
  239. /*
  240. * [300..max] MHz : 7
  241. */
  242. return 7;
  243. else if (vcoFreq >= 18000)
  244. /*
  245. * [180..300) MHz : 4
  246. */
  247. return 4;
  248. else
  249. /*
  250. * [0..180) MHz : 1
  251. */
  252. return 1;
  253. }
  254. void radeon_crtc_dpms(struct drm_crtc *crtc, int mode)
  255. {
  256. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  257. struct drm_device *dev = crtc->dev;
  258. struct radeon_device *rdev = dev->dev_private;
  259. uint32_t mask;
  260. if (radeon_crtc->crtc_id)
  261. mask = (RADEON_CRTC2_DISP_DIS |
  262. RADEON_CRTC2_VSYNC_DIS |
  263. RADEON_CRTC2_HSYNC_DIS |
  264. RADEON_CRTC2_DISP_REQ_EN_B);
  265. else
  266. mask = (RADEON_CRTC_DISPLAY_DIS |
  267. RADEON_CRTC_VSYNC_DIS |
  268. RADEON_CRTC_HSYNC_DIS);
  269. switch (mode) {
  270. case DRM_MODE_DPMS_ON:
  271. radeon_crtc->enabled = true;
  272. /* adjust pm to dpms changes BEFORE enabling crtcs */
  273. radeon_pm_compute_clocks(rdev);
  274. if (radeon_crtc->crtc_id)
  275. WREG32_P(RADEON_CRTC2_GEN_CNTL, RADEON_CRTC2_EN, ~(RADEON_CRTC2_EN | mask));
  276. else {
  277. WREG32_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_EN, ~(RADEON_CRTC_EN |
  278. RADEON_CRTC_DISP_REQ_EN_B));
  279. WREG32_P(RADEON_CRTC_EXT_CNTL, 0, ~mask);
  280. }
  281. drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
  282. radeon_crtc_load_lut(crtc);
  283. break;
  284. case DRM_MODE_DPMS_STANDBY:
  285. case DRM_MODE_DPMS_SUSPEND:
  286. case DRM_MODE_DPMS_OFF:
  287. drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
  288. if (radeon_crtc->crtc_id)
  289. WREG32_P(RADEON_CRTC2_GEN_CNTL, mask, ~(RADEON_CRTC2_EN | mask));
  290. else {
  291. WREG32_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_DISP_REQ_EN_B, ~(RADEON_CRTC_EN |
  292. RADEON_CRTC_DISP_REQ_EN_B));
  293. WREG32_P(RADEON_CRTC_EXT_CNTL, mask, ~mask);
  294. }
  295. radeon_crtc->enabled = false;
  296. /* adjust pm to dpms changes AFTER disabling crtcs */
  297. radeon_pm_compute_clocks(rdev);
  298. break;
  299. }
  300. }
  301. int radeon_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  302. struct drm_framebuffer *old_fb)
  303. {
  304. return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
  305. }
  306. int radeon_crtc_set_base_atomic(struct drm_crtc *crtc,
  307. struct drm_framebuffer *fb,
  308. int x, int y, enum mode_set_atomic state)
  309. {
  310. return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
  311. }
  312. int radeon_crtc_do_set_base(struct drm_crtc *crtc,
  313. struct drm_framebuffer *fb,
  314. int x, int y, int atomic)
  315. {
  316. struct drm_device *dev = crtc->dev;
  317. struct radeon_device *rdev = dev->dev_private;
  318. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  319. struct radeon_framebuffer *radeon_fb;
  320. struct drm_framebuffer *target_fb;
  321. struct drm_gem_object *obj;
  322. struct radeon_bo *rbo;
  323. uint64_t base;
  324. uint32_t crtc_offset, crtc_offset_cntl, crtc_tile_x0_y0 = 0;
  325. uint32_t crtc_pitch, pitch_pixels;
  326. uint32_t tiling_flags;
  327. int format;
  328. uint32_t gen_cntl_reg, gen_cntl_val;
  329. int r;
  330. DRM_DEBUG_KMS("\n");
  331. /* no fb bound */
  332. if (!atomic && !crtc->fb) {
  333. DRM_DEBUG_KMS("No FB bound\n");
  334. return 0;
  335. }
  336. if (atomic) {
  337. radeon_fb = to_radeon_framebuffer(fb);
  338. target_fb = fb;
  339. }
  340. else {
  341. radeon_fb = to_radeon_framebuffer(crtc->fb);
  342. target_fb = crtc->fb;
  343. }
  344. switch (target_fb->bits_per_pixel) {
  345. case 8:
  346. format = 2;
  347. break;
  348. case 15: /* 555 */
  349. format = 3;
  350. break;
  351. case 16: /* 565 */
  352. format = 4;
  353. break;
  354. case 24: /* RGB */
  355. format = 5;
  356. break;
  357. case 32: /* xRGB */
  358. format = 6;
  359. break;
  360. default:
  361. return false;
  362. }
  363. /* Pin framebuffer & get tilling informations */
  364. obj = radeon_fb->obj;
  365. rbo = gem_to_radeon_bo(obj);
  366. retry:
  367. r = radeon_bo_reserve(rbo, false);
  368. if (unlikely(r != 0))
  369. return r;
  370. /* Only 27 bit offset for legacy CRTC */
  371. r = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM, 1 << 27,
  372. &base);
  373. if (unlikely(r != 0)) {
  374. radeon_bo_unreserve(rbo);
  375. /* On old GPU like RN50 with little vram pining can fails because
  376. * current fb is taking all space needed. So instead of unpining
  377. * the old buffer after pining the new one, first unpin old one
  378. * and then retry pining new one.
  379. *
  380. * As only master can set mode only master can pin and it is
  381. * unlikely the master client will race with itself especialy
  382. * on those old gpu with single crtc.
  383. *
  384. * We don't shutdown the display controller because new buffer
  385. * will end up in same spot.
  386. */
  387. if (!atomic && fb && fb != crtc->fb) {
  388. struct radeon_bo *old_rbo;
  389. unsigned long nsize, osize;
  390. old_rbo = gem_to_radeon_bo(to_radeon_framebuffer(fb)->obj);
  391. osize = radeon_bo_size(old_rbo);
  392. nsize = radeon_bo_size(rbo);
  393. if (nsize <= osize && !radeon_bo_reserve(old_rbo, false)) {
  394. radeon_bo_unpin(old_rbo);
  395. radeon_bo_unreserve(old_rbo);
  396. fb = NULL;
  397. goto retry;
  398. }
  399. }
  400. return -EINVAL;
  401. }
  402. radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
  403. radeon_bo_unreserve(rbo);
  404. if (tiling_flags & RADEON_TILING_MICRO)
  405. DRM_ERROR("trying to scanout microtiled buffer\n");
  406. /* if scanout was in GTT this really wouldn't work */
  407. /* crtc offset is from display base addr not FB location */
  408. radeon_crtc->legacy_display_base_addr = rdev->mc.vram_start;
  409. base -= radeon_crtc->legacy_display_base_addr;
  410. crtc_offset_cntl = 0;
  411. pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
  412. crtc_pitch = (((pitch_pixels * target_fb->bits_per_pixel) +
  413. ((target_fb->bits_per_pixel * 8) - 1)) /
  414. (target_fb->bits_per_pixel * 8));
  415. crtc_pitch |= crtc_pitch << 16;
  416. crtc_offset_cntl |= RADEON_CRTC_GUI_TRIG_OFFSET_LEFT_EN;
  417. if (tiling_flags & RADEON_TILING_MACRO) {
  418. if (ASIC_IS_R300(rdev))
  419. crtc_offset_cntl |= (R300_CRTC_X_Y_MODE_EN |
  420. R300_CRTC_MICRO_TILE_BUFFER_DIS |
  421. R300_CRTC_MACRO_TILE_EN);
  422. else
  423. crtc_offset_cntl |= RADEON_CRTC_TILE_EN;
  424. } else {
  425. if (ASIC_IS_R300(rdev))
  426. crtc_offset_cntl &= ~(R300_CRTC_X_Y_MODE_EN |
  427. R300_CRTC_MICRO_TILE_BUFFER_DIS |
  428. R300_CRTC_MACRO_TILE_EN);
  429. else
  430. crtc_offset_cntl &= ~RADEON_CRTC_TILE_EN;
  431. }
  432. if (tiling_flags & RADEON_TILING_MACRO) {
  433. if (ASIC_IS_R300(rdev)) {
  434. crtc_tile_x0_y0 = x | (y << 16);
  435. base &= ~0x7ff;
  436. } else {
  437. int byteshift = target_fb->bits_per_pixel >> 4;
  438. int tile_addr = (((y >> 3) * pitch_pixels + x) >> (8 - byteshift)) << 11;
  439. base += tile_addr + ((x << byteshift) % 256) + ((y % 8) << 8);
  440. crtc_offset_cntl |= (y % 16);
  441. }
  442. } else {
  443. int offset = y * pitch_pixels + x;
  444. switch (target_fb->bits_per_pixel) {
  445. case 8:
  446. offset *= 1;
  447. break;
  448. case 15:
  449. case 16:
  450. offset *= 2;
  451. break;
  452. case 24:
  453. offset *= 3;
  454. break;
  455. case 32:
  456. offset *= 4;
  457. break;
  458. default:
  459. return false;
  460. }
  461. base += offset;
  462. }
  463. base &= ~7;
  464. if (radeon_crtc->crtc_id == 1)
  465. gen_cntl_reg = RADEON_CRTC2_GEN_CNTL;
  466. else
  467. gen_cntl_reg = RADEON_CRTC_GEN_CNTL;
  468. gen_cntl_val = RREG32(gen_cntl_reg);
  469. gen_cntl_val &= ~(0xf << 8);
  470. gen_cntl_val |= (format << 8);
  471. gen_cntl_val &= ~RADEON_CRTC_VSTAT_MODE_MASK;
  472. WREG32(gen_cntl_reg, gen_cntl_val);
  473. crtc_offset = (u32)base;
  474. WREG32(RADEON_DISPLAY_BASE_ADDR + radeon_crtc->crtc_offset, radeon_crtc->legacy_display_base_addr);
  475. if (ASIC_IS_R300(rdev)) {
  476. if (radeon_crtc->crtc_id)
  477. WREG32(R300_CRTC2_TILE_X0_Y0, crtc_tile_x0_y0);
  478. else
  479. WREG32(R300_CRTC_TILE_X0_Y0, crtc_tile_x0_y0);
  480. }
  481. WREG32(RADEON_CRTC_OFFSET_CNTL + radeon_crtc->crtc_offset, crtc_offset_cntl);
  482. WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, crtc_offset);
  483. WREG32(RADEON_CRTC_PITCH + radeon_crtc->crtc_offset, crtc_pitch);
  484. if (!atomic && fb && fb != crtc->fb) {
  485. radeon_fb = to_radeon_framebuffer(fb);
  486. rbo = gem_to_radeon_bo(radeon_fb->obj);
  487. r = radeon_bo_reserve(rbo, false);
  488. if (unlikely(r != 0))
  489. return r;
  490. radeon_bo_unpin(rbo);
  491. radeon_bo_unreserve(rbo);
  492. }
  493. /* Bytes per pixel may have changed */
  494. radeon_bandwidth_update(rdev);
  495. return 0;
  496. }
  497. static bool radeon_set_crtc_timing(struct drm_crtc *crtc, struct drm_display_mode *mode)
  498. {
  499. struct drm_device *dev = crtc->dev;
  500. struct radeon_device *rdev = dev->dev_private;
  501. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  502. struct drm_encoder *encoder;
  503. int format;
  504. int hsync_start;
  505. int hsync_wid;
  506. int vsync_wid;
  507. uint32_t crtc_h_total_disp;
  508. uint32_t crtc_h_sync_strt_wid;
  509. uint32_t crtc_v_total_disp;
  510. uint32_t crtc_v_sync_strt_wid;
  511. bool is_tv = false;
  512. DRM_DEBUG_KMS("\n");
  513. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  514. if (encoder->crtc == crtc) {
  515. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  516. if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
  517. is_tv = true;
  518. DRM_INFO("crtc %d is connected to a TV\n", radeon_crtc->crtc_id);
  519. break;
  520. }
  521. }
  522. }
  523. switch (crtc->fb->bits_per_pixel) {
  524. case 8:
  525. format = 2;
  526. break;
  527. case 15: /* 555 */
  528. format = 3;
  529. break;
  530. case 16: /* 565 */
  531. format = 4;
  532. break;
  533. case 24: /* RGB */
  534. format = 5;
  535. break;
  536. case 32: /* xRGB */
  537. format = 6;
  538. break;
  539. default:
  540. return false;
  541. }
  542. crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
  543. | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
  544. hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
  545. if (!hsync_wid)
  546. hsync_wid = 1;
  547. hsync_start = mode->crtc_hsync_start - 8;
  548. crtc_h_sync_strt_wid = ((hsync_start & 0x1fff)
  549. | ((hsync_wid & 0x3f) << 16)
  550. | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
  551. ? RADEON_CRTC_H_SYNC_POL
  552. : 0));
  553. /* This works for double scan mode. */
  554. crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
  555. | ((mode->crtc_vdisplay - 1) << 16));
  556. vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
  557. if (!vsync_wid)
  558. vsync_wid = 1;
  559. crtc_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
  560. | ((vsync_wid & 0x1f) << 16)
  561. | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
  562. ? RADEON_CRTC_V_SYNC_POL
  563. : 0));
  564. if (radeon_crtc->crtc_id) {
  565. uint32_t crtc2_gen_cntl;
  566. uint32_t disp2_merge_cntl;
  567. /* if TV DAC is enabled for another crtc and keep it enabled */
  568. crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL) & 0x00718080;
  569. crtc2_gen_cntl |= ((format << 8)
  570. | RADEON_CRTC2_VSYNC_DIS
  571. | RADEON_CRTC2_HSYNC_DIS
  572. | RADEON_CRTC2_DISP_DIS
  573. | RADEON_CRTC2_DISP_REQ_EN_B
  574. | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
  575. ? RADEON_CRTC2_DBL_SCAN_EN
  576. : 0)
  577. | ((mode->flags & DRM_MODE_FLAG_CSYNC)
  578. ? RADEON_CRTC2_CSYNC_EN
  579. : 0)
  580. | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
  581. ? RADEON_CRTC2_INTERLACE_EN
  582. : 0));
  583. /* rs4xx chips seem to like to have the crtc enabled when the timing is set */
  584. if ((rdev->family == CHIP_RS400) || (rdev->family == CHIP_RS480))
  585. crtc2_gen_cntl |= RADEON_CRTC2_EN;
  586. disp2_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
  587. disp2_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
  588. WREG32(RADEON_DISP2_MERGE_CNTL, disp2_merge_cntl);
  589. WREG32(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
  590. WREG32(RADEON_FP_H2_SYNC_STRT_WID, crtc_h_sync_strt_wid);
  591. WREG32(RADEON_FP_V2_SYNC_STRT_WID, crtc_v_sync_strt_wid);
  592. } else {
  593. uint32_t crtc_gen_cntl;
  594. uint32_t crtc_ext_cntl;
  595. uint32_t disp_merge_cntl;
  596. crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL) & 0x00718000;
  597. crtc_gen_cntl |= (RADEON_CRTC_EXT_DISP_EN
  598. | (format << 8)
  599. | RADEON_CRTC_DISP_REQ_EN_B
  600. | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
  601. ? RADEON_CRTC_DBL_SCAN_EN
  602. : 0)
  603. | ((mode->flags & DRM_MODE_FLAG_CSYNC)
  604. ? RADEON_CRTC_CSYNC_EN
  605. : 0)
  606. | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
  607. ? RADEON_CRTC_INTERLACE_EN
  608. : 0));
  609. /* rs4xx chips seem to like to have the crtc enabled when the timing is set */
  610. if ((rdev->family == CHIP_RS400) || (rdev->family == CHIP_RS480))
  611. crtc_gen_cntl |= RADEON_CRTC_EN;
  612. crtc_ext_cntl = RREG32(RADEON_CRTC_EXT_CNTL);
  613. crtc_ext_cntl |= (RADEON_XCRT_CNT_EN |
  614. RADEON_CRTC_VSYNC_DIS |
  615. RADEON_CRTC_HSYNC_DIS |
  616. RADEON_CRTC_DISPLAY_DIS);
  617. disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
  618. disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
  619. WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
  620. WREG32(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);
  621. WREG32(RADEON_CRTC_EXT_CNTL, crtc_ext_cntl);
  622. }
  623. if (is_tv)
  624. radeon_legacy_tv_adjust_crtc_reg(encoder, &crtc_h_total_disp,
  625. &crtc_h_sync_strt_wid, &crtc_v_total_disp,
  626. &crtc_v_sync_strt_wid);
  627. WREG32(RADEON_CRTC_H_TOTAL_DISP + radeon_crtc->crtc_offset, crtc_h_total_disp);
  628. WREG32(RADEON_CRTC_H_SYNC_STRT_WID + radeon_crtc->crtc_offset, crtc_h_sync_strt_wid);
  629. WREG32(RADEON_CRTC_V_TOTAL_DISP + radeon_crtc->crtc_offset, crtc_v_total_disp);
  630. WREG32(RADEON_CRTC_V_SYNC_STRT_WID + radeon_crtc->crtc_offset, crtc_v_sync_strt_wid);
  631. return true;
  632. }
  633. static void radeon_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
  634. {
  635. struct drm_device *dev = crtc->dev;
  636. struct radeon_device *rdev = dev->dev_private;
  637. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  638. struct drm_encoder *encoder;
  639. uint32_t feedback_div = 0;
  640. uint32_t frac_fb_div = 0;
  641. uint32_t reference_div = 0;
  642. uint32_t post_divider = 0;
  643. uint32_t freq = 0;
  644. uint8_t pll_gain;
  645. bool use_bios_divs = false;
  646. /* PLL registers */
  647. uint32_t pll_ref_div = 0;
  648. uint32_t pll_fb_post_div = 0;
  649. uint32_t htotal_cntl = 0;
  650. bool is_tv = false;
  651. struct radeon_pll *pll;
  652. struct {
  653. int divider;
  654. int bitvalue;
  655. } *post_div, post_divs[] = {
  656. /* From RAGE 128 VR/RAGE 128 GL Register
  657. * Reference Manual (Technical Reference
  658. * Manual P/N RRG-G04100-C Rev. 0.04), page
  659. * 3-17 (PLL_DIV_[3:0]).
  660. */
  661. { 1, 0 }, /* VCLK_SRC */
  662. { 2, 1 }, /* VCLK_SRC/2 */
  663. { 4, 2 }, /* VCLK_SRC/4 */
  664. { 8, 3 }, /* VCLK_SRC/8 */
  665. { 3, 4 }, /* VCLK_SRC/3 */
  666. { 16, 5 }, /* VCLK_SRC/16 */
  667. { 6, 6 }, /* VCLK_SRC/6 */
  668. { 12, 7 }, /* VCLK_SRC/12 */
  669. { 0, 0 }
  670. };
  671. if (radeon_crtc->crtc_id)
  672. pll = &rdev->clock.p2pll;
  673. else
  674. pll = &rdev->clock.p1pll;
  675. pll->flags = RADEON_PLL_LEGACY;
  676. if (mode->clock > 200000) /* range limits??? */
  677. pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
  678. else
  679. pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
  680. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  681. if (encoder->crtc == crtc) {
  682. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  683. if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
  684. is_tv = true;
  685. break;
  686. }
  687. if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
  688. pll->flags |= RADEON_PLL_NO_ODD_POST_DIV;
  689. if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS) {
  690. if (!rdev->is_atom_bios) {
  691. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  692. struct radeon_encoder_lvds *lvds = (struct radeon_encoder_lvds *)radeon_encoder->enc_priv;
  693. if (lvds) {
  694. if (lvds->use_bios_dividers) {
  695. pll_ref_div = lvds->panel_ref_divider;
  696. pll_fb_post_div = (lvds->panel_fb_divider |
  697. (lvds->panel_post_divider << 16));
  698. htotal_cntl = 0;
  699. use_bios_divs = true;
  700. }
  701. }
  702. }
  703. pll->flags |= RADEON_PLL_USE_REF_DIV;
  704. }
  705. }
  706. }
  707. DRM_DEBUG_KMS("\n");
  708. if (!use_bios_divs) {
  709. radeon_compute_pll_legacy(pll, mode->clock,
  710. &freq, &feedback_div, &frac_fb_div,
  711. &reference_div, &post_divider);
  712. for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
  713. if (post_div->divider == post_divider)
  714. break;
  715. }
  716. if (!post_div->divider)
  717. post_div = &post_divs[0];
  718. DRM_DEBUG_KMS("dc=%u, fd=%d, rd=%d, pd=%d\n",
  719. (unsigned)freq,
  720. feedback_div,
  721. reference_div,
  722. post_divider);
  723. pll_ref_div = reference_div;
  724. #if defined(__powerpc__) && (0) /* TODO */
  725. /* apparently programming this otherwise causes a hang??? */
  726. if (info->MacModel == RADEON_MAC_IBOOK)
  727. pll_fb_post_div = 0x000600ad;
  728. else
  729. #endif
  730. pll_fb_post_div = (feedback_div | (post_div->bitvalue << 16));
  731. htotal_cntl = mode->htotal & 0x7;
  732. }
  733. pll_gain = radeon_compute_pll_gain(pll->reference_freq,
  734. pll_ref_div & 0x3ff,
  735. pll_fb_post_div & 0x7ff);
  736. if (radeon_crtc->crtc_id) {
  737. uint32_t pixclks_cntl = ((RREG32_PLL(RADEON_PIXCLKS_CNTL) &
  738. ~(RADEON_PIX2CLK_SRC_SEL_MASK)) |
  739. RADEON_PIX2CLK_SRC_SEL_P2PLLCLK);
  740. if (is_tv) {
  741. radeon_legacy_tv_adjust_pll2(encoder, &htotal_cntl,
  742. &pll_ref_div, &pll_fb_post_div,
  743. &pixclks_cntl);
  744. }
  745. WREG32_PLL_P(RADEON_PIXCLKS_CNTL,
  746. RADEON_PIX2CLK_SRC_SEL_CPUCLK,
  747. ~(RADEON_PIX2CLK_SRC_SEL_MASK));
  748. WREG32_PLL_P(RADEON_P2PLL_CNTL,
  749. RADEON_P2PLL_RESET
  750. | RADEON_P2PLL_ATOMIC_UPDATE_EN
  751. | ((uint32_t)pll_gain << RADEON_P2PLL_PVG_SHIFT),
  752. ~(RADEON_P2PLL_RESET
  753. | RADEON_P2PLL_ATOMIC_UPDATE_EN
  754. | RADEON_P2PLL_PVG_MASK));
  755. WREG32_PLL_P(RADEON_P2PLL_REF_DIV,
  756. pll_ref_div,
  757. ~RADEON_P2PLL_REF_DIV_MASK);
  758. WREG32_PLL_P(RADEON_P2PLL_DIV_0,
  759. pll_fb_post_div,
  760. ~RADEON_P2PLL_FB0_DIV_MASK);
  761. WREG32_PLL_P(RADEON_P2PLL_DIV_0,
  762. pll_fb_post_div,
  763. ~RADEON_P2PLL_POST0_DIV_MASK);
  764. radeon_pll2_write_update(dev);
  765. radeon_pll2_wait_for_read_update_complete(dev);
  766. WREG32_PLL(RADEON_HTOTAL2_CNTL, htotal_cntl);
  767. WREG32_PLL_P(RADEON_P2PLL_CNTL,
  768. 0,
  769. ~(RADEON_P2PLL_RESET
  770. | RADEON_P2PLL_SLEEP
  771. | RADEON_P2PLL_ATOMIC_UPDATE_EN));
  772. DRM_DEBUG_KMS("Wrote2: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
  773. (unsigned)pll_ref_div,
  774. (unsigned)pll_fb_post_div,
  775. (unsigned)htotal_cntl,
  776. RREG32_PLL(RADEON_P2PLL_CNTL));
  777. DRM_DEBUG_KMS("Wrote2: rd=%u, fd=%u, pd=%u\n",
  778. (unsigned)pll_ref_div & RADEON_P2PLL_REF_DIV_MASK,
  779. (unsigned)pll_fb_post_div & RADEON_P2PLL_FB0_DIV_MASK,
  780. (unsigned)((pll_fb_post_div &
  781. RADEON_P2PLL_POST0_DIV_MASK) >> 16));
  782. mdelay(50); /* Let the clock to lock */
  783. WREG32_PLL_P(RADEON_PIXCLKS_CNTL,
  784. RADEON_PIX2CLK_SRC_SEL_P2PLLCLK,
  785. ~(RADEON_PIX2CLK_SRC_SEL_MASK));
  786. WREG32_PLL(RADEON_PIXCLKS_CNTL, pixclks_cntl);
  787. } else {
  788. uint32_t pixclks_cntl;
  789. if (is_tv) {
  790. pixclks_cntl = RREG32_PLL(RADEON_PIXCLKS_CNTL);
  791. radeon_legacy_tv_adjust_pll1(encoder, &htotal_cntl, &pll_ref_div,
  792. &pll_fb_post_div, &pixclks_cntl);
  793. }
  794. if (rdev->flags & RADEON_IS_MOBILITY) {
  795. /* A temporal workaround for the occasional blanking on certain laptop panels.
  796. This appears to related to the PLL divider registers (fail to lock?).
  797. It occurs even when all dividers are the same with their old settings.
  798. In this case we really don't need to fiddle with PLL registers.
  799. By doing this we can avoid the blanking problem with some panels.
  800. */
  801. if ((pll_ref_div == (RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_REF_DIV_MASK)) &&
  802. (pll_fb_post_div == (RREG32_PLL(RADEON_PPLL_DIV_3) &
  803. (RADEON_PPLL_POST3_DIV_MASK | RADEON_PPLL_FB3_DIV_MASK)))) {
  804. WREG32_P(RADEON_CLOCK_CNTL_INDEX,
  805. RADEON_PLL_DIV_SEL,
  806. ~(RADEON_PLL_DIV_SEL));
  807. r100_pll_errata_after_index(rdev);
  808. return;
  809. }
  810. }
  811. WREG32_PLL_P(RADEON_VCLK_ECP_CNTL,
  812. RADEON_VCLK_SRC_SEL_CPUCLK,
  813. ~(RADEON_VCLK_SRC_SEL_MASK));
  814. WREG32_PLL_P(RADEON_PPLL_CNTL,
  815. RADEON_PPLL_RESET
  816. | RADEON_PPLL_ATOMIC_UPDATE_EN
  817. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
  818. | ((uint32_t)pll_gain << RADEON_PPLL_PVG_SHIFT),
  819. ~(RADEON_PPLL_RESET
  820. | RADEON_PPLL_ATOMIC_UPDATE_EN
  821. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
  822. | RADEON_PPLL_PVG_MASK));
  823. WREG32_P(RADEON_CLOCK_CNTL_INDEX,
  824. RADEON_PLL_DIV_SEL,
  825. ~(RADEON_PLL_DIV_SEL));
  826. r100_pll_errata_after_index(rdev);
  827. if (ASIC_IS_R300(rdev) ||
  828. (rdev->family == CHIP_RS300) ||
  829. (rdev->family == CHIP_RS400) ||
  830. (rdev->family == CHIP_RS480)) {
  831. if (pll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
  832. /* When restoring console mode, use saved PPLL_REF_DIV
  833. * setting.
  834. */
  835. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  836. pll_ref_div,
  837. 0);
  838. } else {
  839. /* R300 uses ref_div_acc field as real ref divider */
  840. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  841. (pll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
  842. ~R300_PPLL_REF_DIV_ACC_MASK);
  843. }
  844. } else
  845. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  846. pll_ref_div,
  847. ~RADEON_PPLL_REF_DIV_MASK);
  848. WREG32_PLL_P(RADEON_PPLL_DIV_3,
  849. pll_fb_post_div,
  850. ~RADEON_PPLL_FB3_DIV_MASK);
  851. WREG32_PLL_P(RADEON_PPLL_DIV_3,
  852. pll_fb_post_div,
  853. ~RADEON_PPLL_POST3_DIV_MASK);
  854. radeon_pll_write_update(dev);
  855. radeon_pll_wait_for_read_update_complete(dev);
  856. WREG32_PLL(RADEON_HTOTAL_CNTL, htotal_cntl);
  857. WREG32_PLL_P(RADEON_PPLL_CNTL,
  858. 0,
  859. ~(RADEON_PPLL_RESET
  860. | RADEON_PPLL_SLEEP
  861. | RADEON_PPLL_ATOMIC_UPDATE_EN
  862. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN));
  863. DRM_DEBUG_KMS("Wrote: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
  864. pll_ref_div,
  865. pll_fb_post_div,
  866. (unsigned)htotal_cntl,
  867. RREG32_PLL(RADEON_PPLL_CNTL));
  868. DRM_DEBUG_KMS("Wrote: rd=%d, fd=%d, pd=%d\n",
  869. pll_ref_div & RADEON_PPLL_REF_DIV_MASK,
  870. pll_fb_post_div & RADEON_PPLL_FB3_DIV_MASK,
  871. (pll_fb_post_div & RADEON_PPLL_POST3_DIV_MASK) >> 16);
  872. mdelay(50); /* Let the clock to lock */
  873. WREG32_PLL_P(RADEON_VCLK_ECP_CNTL,
  874. RADEON_VCLK_SRC_SEL_PPLLCLK,
  875. ~(RADEON_VCLK_SRC_SEL_MASK));
  876. if (is_tv)
  877. WREG32_PLL(RADEON_PIXCLKS_CNTL, pixclks_cntl);
  878. }
  879. }
  880. static bool radeon_crtc_mode_fixup(struct drm_crtc *crtc,
  881. struct drm_display_mode *mode,
  882. struct drm_display_mode *adjusted_mode)
  883. {
  884. if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  885. return false;
  886. return true;
  887. }
  888. static int radeon_crtc_mode_set(struct drm_crtc *crtc,
  889. struct drm_display_mode *mode,
  890. struct drm_display_mode *adjusted_mode,
  891. int x, int y, struct drm_framebuffer *old_fb)
  892. {
  893. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  894. /* TODO TV */
  895. radeon_crtc_set_base(crtc, x, y, old_fb);
  896. radeon_set_crtc_timing(crtc, adjusted_mode);
  897. radeon_set_pll(crtc, adjusted_mode);
  898. radeon_overscan_setup(crtc, adjusted_mode);
  899. if (radeon_crtc->crtc_id == 0) {
  900. radeon_legacy_rmx_mode_set(crtc, adjusted_mode);
  901. } else {
  902. if (radeon_crtc->rmx_type != RMX_OFF) {
  903. /* FIXME: only first crtc has rmx what should we
  904. * do ?
  905. */
  906. DRM_ERROR("Mode need scaling but only first crtc can do that.\n");
  907. }
  908. }
  909. return 0;
  910. }
  911. static void radeon_crtc_prepare(struct drm_crtc *crtc)
  912. {
  913. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  914. struct drm_device *dev = crtc->dev;
  915. struct drm_crtc *crtci;
  916. radeon_crtc->in_mode_set = true;
  917. /*
  918. * The hardware wedges sometimes if you reconfigure one CRTC
  919. * whilst another is running (see fdo bug #24611).
  920. */
  921. list_for_each_entry(crtci, &dev->mode_config.crtc_list, head)
  922. radeon_crtc_dpms(crtci, DRM_MODE_DPMS_OFF);
  923. }
  924. static void radeon_crtc_commit(struct drm_crtc *crtc)
  925. {
  926. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  927. struct drm_device *dev = crtc->dev;
  928. struct drm_crtc *crtci;
  929. /*
  930. * Reenable the CRTCs that should be running.
  931. */
  932. list_for_each_entry(crtci, &dev->mode_config.crtc_list, head) {
  933. if (crtci->enabled)
  934. radeon_crtc_dpms(crtci, DRM_MODE_DPMS_ON);
  935. }
  936. radeon_crtc->in_mode_set = false;
  937. }
  938. static const struct drm_crtc_helper_funcs legacy_helper_funcs = {
  939. .dpms = radeon_crtc_dpms,
  940. .mode_fixup = radeon_crtc_mode_fixup,
  941. .mode_set = radeon_crtc_mode_set,
  942. .mode_set_base = radeon_crtc_set_base,
  943. .mode_set_base_atomic = radeon_crtc_set_base_atomic,
  944. .prepare = radeon_crtc_prepare,
  945. .commit = radeon_crtc_commit,
  946. .load_lut = radeon_crtc_load_lut,
  947. };
  948. void radeon_legacy_init_crtc(struct drm_device *dev,
  949. struct radeon_crtc *radeon_crtc)
  950. {
  951. if (radeon_crtc->crtc_id == 1)
  952. radeon_crtc->crtc_offset = RADEON_CRTC2_H_TOTAL_DISP - RADEON_CRTC_H_TOTAL_DISP;
  953. drm_crtc_helper_add(&radeon_crtc->base, &legacy_helper_funcs);
  954. }