radeon_legacy_crtc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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 "radeon_fixed.h"
  30. #include "radeon.h"
  31. void radeon_restore_common_regs(struct drm_device *dev)
  32. {
  33. /* don't need this yet */
  34. }
  35. static void radeon_pll_wait_for_read_update_complete(struct drm_device *dev)
  36. {
  37. struct radeon_device *rdev = dev->dev_private;
  38. int i = 0;
  39. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  40. the cause yet, but this workaround will mask the problem for now.
  41. Other chips usually will pass at the very first test, so the
  42. workaround shouldn't have any effect on them. */
  43. for (i = 0;
  44. (i < 10000 &&
  45. RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
  46. i++);
  47. }
  48. static void radeon_pll_write_update(struct drm_device *dev)
  49. {
  50. struct radeon_device *rdev = dev->dev_private;
  51. while (RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
  52. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  53. RADEON_PPLL_ATOMIC_UPDATE_W,
  54. ~(RADEON_PPLL_ATOMIC_UPDATE_W));
  55. }
  56. static void radeon_pll2_wait_for_read_update_complete(struct drm_device *dev)
  57. {
  58. struct radeon_device *rdev = dev->dev_private;
  59. int i = 0;
  60. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  61. the cause yet, but this workaround will mask the problem for now.
  62. Other chips usually will pass at the very first test, so the
  63. workaround shouldn't have any effect on them. */
  64. for (i = 0;
  65. (i < 10000 &&
  66. RREG32_PLL(RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
  67. i++);
  68. }
  69. static void radeon_pll2_write_update(struct drm_device *dev)
  70. {
  71. struct radeon_device *rdev = dev->dev_private;
  72. while (RREG32_PLL(RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
  73. WREG32_PLL_P(RADEON_P2PLL_REF_DIV,
  74. RADEON_P2PLL_ATOMIC_UPDATE_W,
  75. ~(RADEON_P2PLL_ATOMIC_UPDATE_W));
  76. }
  77. static uint8_t radeon_compute_pll_gain(uint16_t ref_freq, uint16_t ref_div,
  78. uint16_t fb_div)
  79. {
  80. unsigned int vcoFreq;
  81. if (!ref_div)
  82. return 1;
  83. vcoFreq = ((unsigned)ref_freq & fb_div) / ref_div;
  84. /*
  85. * This is horribly crude: the VCO frequency range is divided into
  86. * 3 parts, each part having a fixed PLL gain value.
  87. */
  88. if (vcoFreq >= 30000)
  89. /*
  90. * [300..max] MHz : 7
  91. */
  92. return 7;
  93. else if (vcoFreq >= 18000)
  94. /*
  95. * [180..300) MHz : 4
  96. */
  97. return 4;
  98. else
  99. /*
  100. * [0..180) MHz : 1
  101. */
  102. return 1;
  103. }
  104. void radeon_crtc_dpms(struct drm_crtc *crtc, int mode)
  105. {
  106. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  107. struct drm_device *dev = crtc->dev;
  108. struct radeon_device *rdev = dev->dev_private;
  109. uint32_t mask;
  110. if (radeon_crtc->crtc_id)
  111. mask = (RADEON_CRTC2_EN |
  112. RADEON_CRTC2_DISP_DIS |
  113. RADEON_CRTC2_VSYNC_DIS |
  114. RADEON_CRTC2_HSYNC_DIS |
  115. RADEON_CRTC2_DISP_REQ_EN_B);
  116. else
  117. mask = (RADEON_CRTC_DISPLAY_DIS |
  118. RADEON_CRTC_VSYNC_DIS |
  119. RADEON_CRTC_HSYNC_DIS);
  120. switch (mode) {
  121. case DRM_MODE_DPMS_ON:
  122. if (radeon_crtc->crtc_id)
  123. WREG32_P(RADEON_CRTC2_GEN_CNTL, RADEON_CRTC2_EN, ~mask);
  124. else {
  125. WREG32_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_EN, ~(RADEON_CRTC_EN |
  126. RADEON_CRTC_DISP_REQ_EN_B));
  127. WREG32_P(RADEON_CRTC_EXT_CNTL, 0, ~mask);
  128. }
  129. break;
  130. case DRM_MODE_DPMS_STANDBY:
  131. case DRM_MODE_DPMS_SUSPEND:
  132. case DRM_MODE_DPMS_OFF:
  133. if (radeon_crtc->crtc_id)
  134. WREG32_P(RADEON_CRTC2_GEN_CNTL, mask, ~mask);
  135. else {
  136. WREG32_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_DISP_REQ_EN_B, ~(RADEON_CRTC_EN |
  137. RADEON_CRTC_DISP_REQ_EN_B));
  138. WREG32_P(RADEON_CRTC_EXT_CNTL, mask, ~mask);
  139. }
  140. break;
  141. }
  142. if (mode != DRM_MODE_DPMS_OFF) {
  143. radeon_crtc_load_lut(crtc);
  144. }
  145. }
  146. /* properly set crtc bpp when using atombios */
  147. void radeon_legacy_atom_set_surface(struct drm_crtc *crtc)
  148. {
  149. struct drm_device *dev = crtc->dev;
  150. struct radeon_device *rdev = dev->dev_private;
  151. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  152. int format;
  153. uint32_t crtc_gen_cntl;
  154. uint32_t disp_merge_cntl;
  155. uint32_t crtc_pitch;
  156. switch (crtc->fb->bits_per_pixel) {
  157. case 15: /* 555 */
  158. format = 3;
  159. break;
  160. case 16: /* 565 */
  161. format = 4;
  162. break;
  163. case 24: /* RGB */
  164. format = 5;
  165. break;
  166. case 32: /* xRGB */
  167. format = 6;
  168. break;
  169. default:
  170. return;
  171. }
  172. crtc_pitch = ((((crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8)) * crtc->fb->bits_per_pixel) +
  173. ((crtc->fb->bits_per_pixel * 8) - 1)) /
  174. (crtc->fb->bits_per_pixel * 8));
  175. crtc_pitch |= crtc_pitch << 16;
  176. WREG32(RADEON_CRTC_PITCH + radeon_crtc->crtc_offset, crtc_pitch);
  177. switch (radeon_crtc->crtc_id) {
  178. case 0:
  179. disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
  180. disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
  181. WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
  182. crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL) & 0xfffff0ff;
  183. crtc_gen_cntl |= (format << 8);
  184. crtc_gen_cntl |= RADEON_CRTC_EXT_DISP_EN;
  185. WREG32(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);
  186. break;
  187. case 1:
  188. disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
  189. disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
  190. WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
  191. crtc_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL) & 0xfffff0ff;
  192. crtc_gen_cntl |= (format << 8);
  193. WREG32(RADEON_CRTC2_GEN_CNTL, crtc_gen_cntl);
  194. WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
  195. WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
  196. break;
  197. }
  198. }
  199. int radeon_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  200. struct drm_framebuffer *old_fb)
  201. {
  202. struct drm_device *dev = crtc->dev;
  203. struct radeon_device *rdev = dev->dev_private;
  204. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  205. struct radeon_framebuffer *radeon_fb;
  206. struct drm_gem_object *obj;
  207. uint64_t base;
  208. uint32_t crtc_offset, crtc_offset_cntl, crtc_tile_x0_y0 = 0;
  209. uint32_t crtc_pitch, pitch_pixels;
  210. DRM_DEBUG("\n");
  211. radeon_fb = to_radeon_framebuffer(crtc->fb);
  212. obj = radeon_fb->obj;
  213. if (radeon_gem_object_pin(obj, RADEON_GEM_DOMAIN_VRAM, &base)) {
  214. return -EINVAL;
  215. }
  216. crtc_offset = (u32)base;
  217. crtc_offset_cntl = 0;
  218. pitch_pixels = crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8);
  219. crtc_pitch = (((pitch_pixels * crtc->fb->bits_per_pixel) +
  220. ((crtc->fb->bits_per_pixel * 8) - 1)) /
  221. (crtc->fb->bits_per_pixel * 8));
  222. crtc_pitch |= crtc_pitch << 16;
  223. /* TODO tiling */
  224. if (0) {
  225. if (ASIC_IS_R300(rdev))
  226. crtc_offset_cntl |= (R300_CRTC_X_Y_MODE_EN |
  227. R300_CRTC_MICRO_TILE_BUFFER_DIS |
  228. R300_CRTC_MACRO_TILE_EN);
  229. else
  230. crtc_offset_cntl |= RADEON_CRTC_TILE_EN;
  231. } else {
  232. if (ASIC_IS_R300(rdev))
  233. crtc_offset_cntl &= ~(R300_CRTC_X_Y_MODE_EN |
  234. R300_CRTC_MICRO_TILE_BUFFER_DIS |
  235. R300_CRTC_MACRO_TILE_EN);
  236. else
  237. crtc_offset_cntl &= ~RADEON_CRTC_TILE_EN;
  238. }
  239. /* TODO more tiling */
  240. if (0) {
  241. if (ASIC_IS_R300(rdev)) {
  242. crtc_tile_x0_y0 = x | (y << 16);
  243. base &= ~0x7ff;
  244. } else {
  245. int byteshift = crtc->fb->bits_per_pixel >> 4;
  246. int tile_addr = (((y >> 3) * crtc->fb->width + x) >> (8 - byteshift)) << 11;
  247. base += tile_addr + ((x << byteshift) % 256) + ((y % 8) << 8);
  248. crtc_offset_cntl |= (y % 16);
  249. }
  250. } else {
  251. int offset = y * pitch_pixels + x;
  252. switch (crtc->fb->bits_per_pixel) {
  253. case 15:
  254. case 16:
  255. offset *= 2;
  256. break;
  257. case 24:
  258. offset *= 3;
  259. break;
  260. case 32:
  261. offset *= 4;
  262. break;
  263. default:
  264. return false;
  265. }
  266. base += offset;
  267. }
  268. base &= ~7;
  269. /* update sarea TODO */
  270. crtc_offset = (u32)base;
  271. WREG32(RADEON_DISPLAY_BASE_ADDR + radeon_crtc->crtc_offset, rdev->mc.vram_location);
  272. if (ASIC_IS_R300(rdev)) {
  273. if (radeon_crtc->crtc_id)
  274. WREG32(R300_CRTC2_TILE_X0_Y0, crtc_tile_x0_y0);
  275. else
  276. WREG32(R300_CRTC_TILE_X0_Y0, crtc_tile_x0_y0);
  277. }
  278. WREG32(RADEON_CRTC_OFFSET_CNTL + radeon_crtc->crtc_offset, crtc_offset_cntl);
  279. WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, crtc_offset);
  280. WREG32(RADEON_CRTC_PITCH + radeon_crtc->crtc_offset, crtc_pitch);
  281. if (old_fb && old_fb != crtc->fb) {
  282. radeon_fb = to_radeon_framebuffer(old_fb);
  283. radeon_gem_object_unpin(radeon_fb->obj);
  284. }
  285. return 0;
  286. }
  287. static bool radeon_set_crtc_timing(struct drm_crtc *crtc, struct drm_display_mode *mode)
  288. {
  289. struct drm_device *dev = crtc->dev;
  290. struct radeon_device *rdev = dev->dev_private;
  291. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  292. int format;
  293. int hsync_start;
  294. int hsync_wid;
  295. int vsync_wid;
  296. uint32_t crtc_h_total_disp;
  297. uint32_t crtc_h_sync_strt_wid;
  298. uint32_t crtc_v_total_disp;
  299. uint32_t crtc_v_sync_strt_wid;
  300. DRM_DEBUG("\n");
  301. switch (crtc->fb->bits_per_pixel) {
  302. case 15: /* 555 */
  303. format = 3;
  304. break;
  305. case 16: /* 565 */
  306. format = 4;
  307. break;
  308. case 24: /* RGB */
  309. format = 5;
  310. break;
  311. case 32: /* xRGB */
  312. format = 6;
  313. break;
  314. default:
  315. return false;
  316. }
  317. crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
  318. | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));
  319. hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
  320. if (!hsync_wid)
  321. hsync_wid = 1;
  322. hsync_start = mode->crtc_hsync_start - 8;
  323. crtc_h_sync_strt_wid = ((hsync_start & 0x1fff)
  324. | ((hsync_wid & 0x3f) << 16)
  325. | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
  326. ? RADEON_CRTC_H_SYNC_POL
  327. : 0));
  328. /* This works for double scan mode. */
  329. crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
  330. | ((mode->crtc_vdisplay - 1) << 16));
  331. vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
  332. if (!vsync_wid)
  333. vsync_wid = 1;
  334. crtc_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
  335. | ((vsync_wid & 0x1f) << 16)
  336. | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
  337. ? RADEON_CRTC_V_SYNC_POL
  338. : 0));
  339. /* TODO -> Dell Server */
  340. if (0) {
  341. uint32_t disp_hw_debug = RREG32(RADEON_DISP_HW_DEBUG);
  342. uint32_t tv_dac_cntl = RREG32(RADEON_TV_DAC_CNTL);
  343. uint32_t dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  344. uint32_t crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL);
  345. dac2_cntl &= ~RADEON_DAC2_DAC_CLK_SEL;
  346. dac2_cntl |= RADEON_DAC2_DAC2_CLK_SEL;
  347. /* For CRT on DAC2, don't turn it on if BIOS didn't
  348. enable it, even it's detected.
  349. */
  350. disp_hw_debug |= RADEON_CRT2_DISP1_SEL;
  351. tv_dac_cntl &= ~((1<<2) | (3<<8) | (7<<24) | (0xff<<16));
  352. tv_dac_cntl |= (0x03 | (2<<8) | (0x58<<16));
  353. WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
  354. WREG32(RADEON_DISP_HW_DEBUG, disp_hw_debug);
  355. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  356. WREG32(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
  357. }
  358. if (radeon_crtc->crtc_id) {
  359. uint32_t crtc2_gen_cntl;
  360. uint32_t disp2_merge_cntl;
  361. /* check to see if TV DAC is enabled for another crtc and keep it enabled */
  362. if (RREG32(RADEON_CRTC2_GEN_CNTL) & RADEON_CRTC2_CRT2_ON)
  363. crtc2_gen_cntl = RADEON_CRTC2_CRT2_ON;
  364. else
  365. crtc2_gen_cntl = 0;
  366. crtc2_gen_cntl |= ((format << 8)
  367. | RADEON_CRTC2_VSYNC_DIS
  368. | RADEON_CRTC2_HSYNC_DIS
  369. | RADEON_CRTC2_DISP_DIS
  370. | RADEON_CRTC2_DISP_REQ_EN_B
  371. | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
  372. ? RADEON_CRTC2_DBL_SCAN_EN
  373. : 0)
  374. | ((mode->flags & DRM_MODE_FLAG_CSYNC)
  375. ? RADEON_CRTC2_CSYNC_EN
  376. : 0)
  377. | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
  378. ? RADEON_CRTC2_INTERLACE_EN
  379. : 0));
  380. disp2_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
  381. disp2_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
  382. WREG32(RADEON_DISP2_MERGE_CNTL, disp2_merge_cntl);
  383. WREG32(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
  384. } else {
  385. uint32_t crtc_gen_cntl;
  386. uint32_t crtc_ext_cntl;
  387. uint32_t disp_merge_cntl;
  388. crtc_gen_cntl = (RADEON_CRTC_EXT_DISP_EN
  389. | (format << 8)
  390. | RADEON_CRTC_DISP_REQ_EN_B
  391. | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
  392. ? RADEON_CRTC_DBL_SCAN_EN
  393. : 0)
  394. | ((mode->flags & DRM_MODE_FLAG_CSYNC)
  395. ? RADEON_CRTC_CSYNC_EN
  396. : 0)
  397. | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
  398. ? RADEON_CRTC_INTERLACE_EN
  399. : 0));
  400. crtc_ext_cntl = RREG32(RADEON_CRTC_EXT_CNTL);
  401. crtc_ext_cntl |= (RADEON_XCRT_CNT_EN |
  402. RADEON_CRTC_VSYNC_DIS |
  403. RADEON_CRTC_HSYNC_DIS |
  404. RADEON_CRTC_DISPLAY_DIS);
  405. disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
  406. disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
  407. WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
  408. WREG32(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);
  409. WREG32(RADEON_CRTC_EXT_CNTL, crtc_ext_cntl);
  410. }
  411. WREG32(RADEON_CRTC_H_TOTAL_DISP + radeon_crtc->crtc_offset, crtc_h_total_disp);
  412. WREG32(RADEON_CRTC_H_SYNC_STRT_WID + radeon_crtc->crtc_offset, crtc_h_sync_strt_wid);
  413. WREG32(RADEON_CRTC_V_TOTAL_DISP + radeon_crtc->crtc_offset, crtc_v_total_disp);
  414. WREG32(RADEON_CRTC_V_SYNC_STRT_WID + radeon_crtc->crtc_offset, crtc_v_sync_strt_wid);
  415. return true;
  416. }
  417. static void radeon_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
  418. {
  419. struct drm_device *dev = crtc->dev;
  420. struct radeon_device *rdev = dev->dev_private;
  421. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  422. struct drm_encoder *encoder;
  423. uint32_t feedback_div = 0;
  424. uint32_t frac_fb_div = 0;
  425. uint32_t reference_div = 0;
  426. uint32_t post_divider = 0;
  427. uint32_t freq = 0;
  428. uint8_t pll_gain;
  429. int pll_flags = RADEON_PLL_LEGACY;
  430. bool use_bios_divs = false;
  431. /* PLL registers */
  432. uint32_t pll_ref_div = 0;
  433. uint32_t pll_fb_post_div = 0;
  434. uint32_t htotal_cntl = 0;
  435. struct radeon_pll *pll;
  436. struct {
  437. int divider;
  438. int bitvalue;
  439. } *post_div, post_divs[] = {
  440. /* From RAGE 128 VR/RAGE 128 GL Register
  441. * Reference Manual (Technical Reference
  442. * Manual P/N RRG-G04100-C Rev. 0.04), page
  443. * 3-17 (PLL_DIV_[3:0]).
  444. */
  445. { 1, 0 }, /* VCLK_SRC */
  446. { 2, 1 }, /* VCLK_SRC/2 */
  447. { 4, 2 }, /* VCLK_SRC/4 */
  448. { 8, 3 }, /* VCLK_SRC/8 */
  449. { 3, 4 }, /* VCLK_SRC/3 */
  450. { 16, 5 }, /* VCLK_SRC/16 */
  451. { 6, 6 }, /* VCLK_SRC/6 */
  452. { 12, 7 }, /* VCLK_SRC/12 */
  453. { 0, 0 }
  454. };
  455. if (radeon_crtc->crtc_id)
  456. pll = &rdev->clock.p2pll;
  457. else
  458. pll = &rdev->clock.p1pll;
  459. if (mode->clock > 200000) /* range limits??? */
  460. pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
  461. else
  462. pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
  463. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  464. if (encoder->crtc == crtc) {
  465. if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
  466. pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
  467. if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS) {
  468. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  469. struct radeon_encoder_lvds *lvds = (struct radeon_encoder_lvds *)radeon_encoder->enc_priv;
  470. if (lvds) {
  471. if (lvds->use_bios_dividers) {
  472. pll_ref_div = lvds->panel_ref_divider;
  473. pll_fb_post_div = (lvds->panel_fb_divider |
  474. (lvds->panel_post_divider << 16));
  475. htotal_cntl = 0;
  476. use_bios_divs = true;
  477. }
  478. }
  479. pll_flags |= RADEON_PLL_USE_REF_DIV;
  480. }
  481. }
  482. }
  483. DRM_DEBUG("\n");
  484. if (!use_bios_divs) {
  485. radeon_compute_pll(pll, mode->clock,
  486. &freq, &feedback_div, &frac_fb_div,
  487. &reference_div, &post_divider,
  488. pll_flags);
  489. for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
  490. if (post_div->divider == post_divider)
  491. break;
  492. }
  493. if (!post_div->divider)
  494. post_div = &post_divs[0];
  495. DRM_DEBUG("dc=%u, fd=%d, rd=%d, pd=%d\n",
  496. (unsigned)freq,
  497. feedback_div,
  498. reference_div,
  499. post_divider);
  500. pll_ref_div = reference_div;
  501. #if defined(__powerpc__) && (0) /* TODO */
  502. /* apparently programming this otherwise causes a hang??? */
  503. if (info->MacModel == RADEON_MAC_IBOOK)
  504. pll_fb_post_div = 0x000600ad;
  505. else
  506. #endif
  507. pll_fb_post_div = (feedback_div | (post_div->bitvalue << 16));
  508. htotal_cntl = mode->htotal & 0x7;
  509. }
  510. pll_gain = radeon_compute_pll_gain(pll->reference_freq,
  511. pll_ref_div & 0x3ff,
  512. pll_fb_post_div & 0x7ff);
  513. if (radeon_crtc->crtc_id) {
  514. uint32_t pixclks_cntl = ((RREG32_PLL(RADEON_PIXCLKS_CNTL) &
  515. ~(RADEON_PIX2CLK_SRC_SEL_MASK)) |
  516. RADEON_PIX2CLK_SRC_SEL_P2PLLCLK);
  517. WREG32_PLL_P(RADEON_PIXCLKS_CNTL,
  518. RADEON_PIX2CLK_SRC_SEL_CPUCLK,
  519. ~(RADEON_PIX2CLK_SRC_SEL_MASK));
  520. WREG32_PLL_P(RADEON_P2PLL_CNTL,
  521. RADEON_P2PLL_RESET
  522. | RADEON_P2PLL_ATOMIC_UPDATE_EN
  523. | ((uint32_t)pll_gain << RADEON_P2PLL_PVG_SHIFT),
  524. ~(RADEON_P2PLL_RESET
  525. | RADEON_P2PLL_ATOMIC_UPDATE_EN
  526. | RADEON_P2PLL_PVG_MASK));
  527. WREG32_PLL_P(RADEON_P2PLL_REF_DIV,
  528. pll_ref_div,
  529. ~RADEON_P2PLL_REF_DIV_MASK);
  530. WREG32_PLL_P(RADEON_P2PLL_DIV_0,
  531. pll_fb_post_div,
  532. ~RADEON_P2PLL_FB0_DIV_MASK);
  533. WREG32_PLL_P(RADEON_P2PLL_DIV_0,
  534. pll_fb_post_div,
  535. ~RADEON_P2PLL_POST0_DIV_MASK);
  536. radeon_pll2_write_update(dev);
  537. radeon_pll2_wait_for_read_update_complete(dev);
  538. WREG32_PLL(RADEON_HTOTAL2_CNTL, htotal_cntl);
  539. WREG32_PLL_P(RADEON_P2PLL_CNTL,
  540. 0,
  541. ~(RADEON_P2PLL_RESET
  542. | RADEON_P2PLL_SLEEP
  543. | RADEON_P2PLL_ATOMIC_UPDATE_EN));
  544. DRM_DEBUG("Wrote2: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
  545. (unsigned)pll_ref_div,
  546. (unsigned)pll_fb_post_div,
  547. (unsigned)htotal_cntl,
  548. RREG32_PLL(RADEON_P2PLL_CNTL));
  549. DRM_DEBUG("Wrote2: rd=%u, fd=%u, pd=%u\n",
  550. (unsigned)pll_ref_div & RADEON_P2PLL_REF_DIV_MASK,
  551. (unsigned)pll_fb_post_div & RADEON_P2PLL_FB0_DIV_MASK,
  552. (unsigned)((pll_fb_post_div &
  553. RADEON_P2PLL_POST0_DIV_MASK) >> 16));
  554. mdelay(50); /* Let the clock to lock */
  555. WREG32_PLL_P(RADEON_PIXCLKS_CNTL,
  556. RADEON_PIX2CLK_SRC_SEL_P2PLLCLK,
  557. ~(RADEON_PIX2CLK_SRC_SEL_MASK));
  558. WREG32_PLL(RADEON_PIXCLKS_CNTL, pixclks_cntl);
  559. } else {
  560. if (rdev->flags & RADEON_IS_MOBILITY) {
  561. /* A temporal workaround for the occational blanking on certain laptop panels.
  562. This appears to related to the PLL divider registers (fail to lock?).
  563. It occurs even when all dividers are the same with their old settings.
  564. In this case we really don't need to fiddle with PLL registers.
  565. By doing this we can avoid the blanking problem with some panels.
  566. */
  567. if ((pll_ref_div == (RREG32_PLL(RADEON_PPLL_REF_DIV) & RADEON_PPLL_REF_DIV_MASK)) &&
  568. (pll_fb_post_div == (RREG32_PLL(RADEON_PPLL_DIV_3) &
  569. (RADEON_PPLL_POST3_DIV_MASK | RADEON_PPLL_FB3_DIV_MASK)))) {
  570. WREG32_P(RADEON_CLOCK_CNTL_INDEX,
  571. RADEON_PLL_DIV_SEL,
  572. ~(RADEON_PLL_DIV_SEL));
  573. r100_pll_errata_after_index(rdev);
  574. return;
  575. }
  576. }
  577. WREG32_PLL_P(RADEON_VCLK_ECP_CNTL,
  578. RADEON_VCLK_SRC_SEL_CPUCLK,
  579. ~(RADEON_VCLK_SRC_SEL_MASK));
  580. WREG32_PLL_P(RADEON_PPLL_CNTL,
  581. RADEON_PPLL_RESET
  582. | RADEON_PPLL_ATOMIC_UPDATE_EN
  583. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
  584. | ((uint32_t)pll_gain << RADEON_PPLL_PVG_SHIFT),
  585. ~(RADEON_PPLL_RESET
  586. | RADEON_PPLL_ATOMIC_UPDATE_EN
  587. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
  588. | RADEON_PPLL_PVG_MASK));
  589. WREG32_P(RADEON_CLOCK_CNTL_INDEX,
  590. RADEON_PLL_DIV_SEL,
  591. ~(RADEON_PLL_DIV_SEL));
  592. r100_pll_errata_after_index(rdev);
  593. if (ASIC_IS_R300(rdev) ||
  594. (rdev->family == CHIP_RS300) ||
  595. (rdev->family == CHIP_RS400) ||
  596. (rdev->family == CHIP_RS480)) {
  597. if (pll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
  598. /* When restoring console mode, use saved PPLL_REF_DIV
  599. * setting.
  600. */
  601. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  602. pll_ref_div,
  603. 0);
  604. } else {
  605. /* R300 uses ref_div_acc field as real ref divider */
  606. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  607. (pll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
  608. ~R300_PPLL_REF_DIV_ACC_MASK);
  609. }
  610. } else
  611. WREG32_PLL_P(RADEON_PPLL_REF_DIV,
  612. pll_ref_div,
  613. ~RADEON_PPLL_REF_DIV_MASK);
  614. WREG32_PLL_P(RADEON_PPLL_DIV_3,
  615. pll_fb_post_div,
  616. ~RADEON_PPLL_FB3_DIV_MASK);
  617. WREG32_PLL_P(RADEON_PPLL_DIV_3,
  618. pll_fb_post_div,
  619. ~RADEON_PPLL_POST3_DIV_MASK);
  620. radeon_pll_write_update(dev);
  621. radeon_pll_wait_for_read_update_complete(dev);
  622. WREG32_PLL(RADEON_HTOTAL_CNTL, htotal_cntl);
  623. WREG32_PLL_P(RADEON_PPLL_CNTL,
  624. 0,
  625. ~(RADEON_PPLL_RESET
  626. | RADEON_PPLL_SLEEP
  627. | RADEON_PPLL_ATOMIC_UPDATE_EN
  628. | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN));
  629. DRM_DEBUG("Wrote: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
  630. pll_ref_div,
  631. pll_fb_post_div,
  632. (unsigned)htotal_cntl,
  633. RREG32_PLL(RADEON_PPLL_CNTL));
  634. DRM_DEBUG("Wrote: rd=%d, fd=%d, pd=%d\n",
  635. pll_ref_div & RADEON_PPLL_REF_DIV_MASK,
  636. pll_fb_post_div & RADEON_PPLL_FB3_DIV_MASK,
  637. (pll_fb_post_div & RADEON_PPLL_POST3_DIV_MASK) >> 16);
  638. mdelay(50); /* Let the clock to lock */
  639. WREG32_PLL_P(RADEON_VCLK_ECP_CNTL,
  640. RADEON_VCLK_SRC_SEL_PPLLCLK,
  641. ~(RADEON_VCLK_SRC_SEL_MASK));
  642. }
  643. }
  644. static bool radeon_crtc_mode_fixup(struct drm_crtc *crtc,
  645. struct drm_display_mode *mode,
  646. struct drm_display_mode *adjusted_mode)
  647. {
  648. return true;
  649. }
  650. static int radeon_crtc_mode_set(struct drm_crtc *crtc,
  651. struct drm_display_mode *mode,
  652. struct drm_display_mode *adjusted_mode,
  653. int x, int y, struct drm_framebuffer *old_fb)
  654. {
  655. DRM_DEBUG("\n");
  656. /* TODO TV */
  657. radeon_crtc_set_base(crtc, x, y, old_fb);
  658. radeon_set_crtc_timing(crtc, adjusted_mode);
  659. radeon_set_pll(crtc, adjusted_mode);
  660. radeon_init_disp_bandwidth(crtc->dev);
  661. return 0;
  662. }
  663. static void radeon_crtc_prepare(struct drm_crtc *crtc)
  664. {
  665. radeon_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  666. }
  667. static void radeon_crtc_commit(struct drm_crtc *crtc)
  668. {
  669. radeon_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  670. }
  671. static const struct drm_crtc_helper_funcs legacy_helper_funcs = {
  672. .dpms = radeon_crtc_dpms,
  673. .mode_fixup = radeon_crtc_mode_fixup,
  674. .mode_set = radeon_crtc_mode_set,
  675. .mode_set_base = radeon_crtc_set_base,
  676. .prepare = radeon_crtc_prepare,
  677. .commit = radeon_crtc_commit,
  678. };
  679. void radeon_legacy_init_crtc(struct drm_device *dev,
  680. struct radeon_crtc *radeon_crtc)
  681. {
  682. if (radeon_crtc->crtc_id == 1)
  683. radeon_crtc->crtc_offset = RADEON_CRTC2_H_TOTAL_DISP - RADEON_CRTC_H_TOTAL_DISP;
  684. drm_crtc_helper_add(&radeon_crtc->base, &legacy_helper_funcs);
  685. }
  686. void radeon_init_disp_bw_legacy(struct drm_device *dev,
  687. struct drm_display_mode *mode1,
  688. uint32_t pixel_bytes1,
  689. struct drm_display_mode *mode2,
  690. uint32_t pixel_bytes2)
  691. {
  692. struct radeon_device *rdev = dev->dev_private;
  693. fixed20_12 trcd_ff, trp_ff, tras_ff, trbs_ff, tcas_ff;
  694. fixed20_12 sclk_ff, mclk_ff, sclk_eff_ff, sclk_delay_ff;
  695. fixed20_12 peak_disp_bw, mem_bw, pix_clk, pix_clk2, temp_ff, crit_point_ff;
  696. uint32_t temp, data, mem_trcd, mem_trp, mem_tras;
  697. fixed20_12 memtcas_ff[8] = {
  698. fixed_init(1),
  699. fixed_init(2),
  700. fixed_init(3),
  701. fixed_init(0),
  702. fixed_init_half(1),
  703. fixed_init_half(2),
  704. fixed_init(0),
  705. };
  706. fixed20_12 memtcas_rs480_ff[8] = {
  707. fixed_init(0),
  708. fixed_init(1),
  709. fixed_init(2),
  710. fixed_init(3),
  711. fixed_init(0),
  712. fixed_init_half(1),
  713. fixed_init_half(2),
  714. fixed_init_half(3),
  715. };
  716. fixed20_12 memtcas2_ff[8] = {
  717. fixed_init(0),
  718. fixed_init(1),
  719. fixed_init(2),
  720. fixed_init(3),
  721. fixed_init(4),
  722. fixed_init(5),
  723. fixed_init(6),
  724. fixed_init(7),
  725. };
  726. fixed20_12 memtrbs[8] = {
  727. fixed_init(1),
  728. fixed_init_half(1),
  729. fixed_init(2),
  730. fixed_init_half(2),
  731. fixed_init(3),
  732. fixed_init_half(3),
  733. fixed_init(4),
  734. fixed_init_half(4)
  735. };
  736. fixed20_12 memtrbs_r4xx[8] = {
  737. fixed_init(4),
  738. fixed_init(5),
  739. fixed_init(6),
  740. fixed_init(7),
  741. fixed_init(8),
  742. fixed_init(9),
  743. fixed_init(10),
  744. fixed_init(11)
  745. };
  746. fixed20_12 min_mem_eff;
  747. fixed20_12 mc_latency_sclk, mc_latency_mclk, k1;
  748. fixed20_12 cur_latency_mclk, cur_latency_sclk;
  749. fixed20_12 disp_latency, disp_latency_overhead, disp_drain_rate,
  750. disp_drain_rate2, read_return_rate;
  751. fixed20_12 time_disp1_drop_priority;
  752. int c;
  753. int cur_size = 16; /* in octawords */
  754. int critical_point = 0, critical_point2;
  755. /* uint32_t read_return_rate, time_disp1_drop_priority; */
  756. int stop_req, max_stop_req;
  757. min_mem_eff.full = rfixed_const_8(0);
  758. /* get modes */
  759. if ((rdev->disp_priority == 2) && ASIC_IS_R300(rdev)) {
  760. uint32_t mc_init_misc_lat_timer = RREG32(R300_MC_INIT_MISC_LAT_TIMER);
  761. mc_init_misc_lat_timer &= ~(R300_MC_DISP1R_INIT_LAT_MASK << R300_MC_DISP1R_INIT_LAT_SHIFT);
  762. mc_init_misc_lat_timer &= ~(R300_MC_DISP0R_INIT_LAT_MASK << R300_MC_DISP0R_INIT_LAT_SHIFT);
  763. /* check crtc enables */
  764. if (mode2)
  765. mc_init_misc_lat_timer |= (1 << R300_MC_DISP1R_INIT_LAT_SHIFT);
  766. if (mode1)
  767. mc_init_misc_lat_timer |= (1 << R300_MC_DISP0R_INIT_LAT_SHIFT);
  768. WREG32(R300_MC_INIT_MISC_LAT_TIMER, mc_init_misc_lat_timer);
  769. }
  770. /*
  771. * determine is there is enough bw for current mode
  772. */
  773. mclk_ff.full = rfixed_const(rdev->clock.default_mclk);
  774. temp_ff.full = rfixed_const(100);
  775. mclk_ff.full = rfixed_div(mclk_ff, temp_ff);
  776. sclk_ff.full = rfixed_const(rdev->clock.default_sclk);
  777. sclk_ff.full = rfixed_div(sclk_ff, temp_ff);
  778. temp = (rdev->mc.vram_width / 8) * (rdev->mc.vram_is_ddr ? 2 : 1);
  779. temp_ff.full = rfixed_const(temp);
  780. mem_bw.full = rfixed_mul(mclk_ff, temp_ff);
  781. pix_clk.full = 0;
  782. pix_clk2.full = 0;
  783. peak_disp_bw.full = 0;
  784. if (mode1) {
  785. temp_ff.full = rfixed_const(1000);
  786. pix_clk.full = rfixed_const(mode1->clock); /* convert to fixed point */
  787. pix_clk.full = rfixed_div(pix_clk, temp_ff);
  788. temp_ff.full = rfixed_const(pixel_bytes1);
  789. peak_disp_bw.full += rfixed_mul(pix_clk, temp_ff);
  790. }
  791. if (mode2) {
  792. temp_ff.full = rfixed_const(1000);
  793. pix_clk2.full = rfixed_const(mode2->clock); /* convert to fixed point */
  794. pix_clk2.full = rfixed_div(pix_clk2, temp_ff);
  795. temp_ff.full = rfixed_const(pixel_bytes2);
  796. peak_disp_bw.full += rfixed_mul(pix_clk2, temp_ff);
  797. }
  798. mem_bw.full = rfixed_mul(mem_bw, min_mem_eff);
  799. if (peak_disp_bw.full >= mem_bw.full) {
  800. DRM_ERROR("You may not have enough display bandwidth for current mode\n"
  801. "If you have flickering problem, try to lower resolution, refresh rate, or color depth\n");
  802. }
  803. /* Get values from the EXT_MEM_CNTL register...converting its contents. */
  804. temp = RREG32(RADEON_MEM_TIMING_CNTL);
  805. if ((rdev->family == CHIP_RV100) || (rdev->flags & RADEON_IS_IGP)) { /* RV100, M6, IGPs */
  806. mem_trcd = ((temp >> 2) & 0x3) + 1;
  807. mem_trp = ((temp & 0x3)) + 1;
  808. mem_tras = ((temp & 0x70) >> 4) + 1;
  809. } else if (rdev->family == CHIP_R300 ||
  810. rdev->family == CHIP_R350) { /* r300, r350 */
  811. mem_trcd = (temp & 0x7) + 1;
  812. mem_trp = ((temp >> 8) & 0x7) + 1;
  813. mem_tras = ((temp >> 11) & 0xf) + 4;
  814. } else if (rdev->family == CHIP_RV350 ||
  815. rdev->family <= CHIP_RV380) {
  816. /* rv3x0 */
  817. mem_trcd = (temp & 0x7) + 3;
  818. mem_trp = ((temp >> 8) & 0x7) + 3;
  819. mem_tras = ((temp >> 11) & 0xf) + 6;
  820. } else if (rdev->family == CHIP_R420 ||
  821. rdev->family == CHIP_R423 ||
  822. rdev->family == CHIP_RV410) {
  823. /* r4xx */
  824. mem_trcd = (temp & 0xf) + 3;
  825. if (mem_trcd > 15)
  826. mem_trcd = 15;
  827. mem_trp = ((temp >> 8) & 0xf) + 3;
  828. if (mem_trp > 15)
  829. mem_trp = 15;
  830. mem_tras = ((temp >> 12) & 0x1f) + 6;
  831. if (mem_tras > 31)
  832. mem_tras = 31;
  833. } else { /* RV200, R200 */
  834. mem_trcd = (temp & 0x7) + 1;
  835. mem_trp = ((temp >> 8) & 0x7) + 1;
  836. mem_tras = ((temp >> 12) & 0xf) + 4;
  837. }
  838. /* convert to FF */
  839. trcd_ff.full = rfixed_const(mem_trcd);
  840. trp_ff.full = rfixed_const(mem_trp);
  841. tras_ff.full = rfixed_const(mem_tras);
  842. /* Get values from the MEM_SDRAM_MODE_REG register...converting its */
  843. temp = RREG32(RADEON_MEM_SDRAM_MODE_REG);
  844. data = (temp & (7 << 20)) >> 20;
  845. if ((rdev->family == CHIP_RV100) || rdev->flags & RADEON_IS_IGP) {
  846. if (rdev->family == CHIP_RS480) /* don't think rs400 */
  847. tcas_ff = memtcas_rs480_ff[data];
  848. else
  849. tcas_ff = memtcas_ff[data];
  850. } else
  851. tcas_ff = memtcas2_ff[data];
  852. if (rdev->family == CHIP_RS400 ||
  853. rdev->family == CHIP_RS480) {
  854. /* extra cas latency stored in bits 23-25 0-4 clocks */
  855. data = (temp >> 23) & 0x7;
  856. if (data < 5)
  857. tcas_ff.full += rfixed_const(data);
  858. }
  859. if (ASIC_IS_R300(rdev) && !(rdev->flags & RADEON_IS_IGP)) {
  860. /* on the R300, Tcas is included in Trbs.
  861. */
  862. temp = RREG32(RADEON_MEM_CNTL);
  863. data = (R300_MEM_NUM_CHANNELS_MASK & temp);
  864. if (data == 1) {
  865. if (R300_MEM_USE_CD_CH_ONLY & temp) {
  866. temp = RREG32(R300_MC_IND_INDEX);
  867. temp &= ~R300_MC_IND_ADDR_MASK;
  868. temp |= R300_MC_READ_CNTL_CD_mcind;
  869. WREG32(R300_MC_IND_INDEX, temp);
  870. temp = RREG32(R300_MC_IND_DATA);
  871. data = (R300_MEM_RBS_POSITION_C_MASK & temp);
  872. } else {
  873. temp = RREG32(R300_MC_READ_CNTL_AB);
  874. data = (R300_MEM_RBS_POSITION_A_MASK & temp);
  875. }
  876. } else {
  877. temp = RREG32(R300_MC_READ_CNTL_AB);
  878. data = (R300_MEM_RBS_POSITION_A_MASK & temp);
  879. }
  880. if (rdev->family == CHIP_RV410 ||
  881. rdev->family == CHIP_R420 ||
  882. rdev->family == CHIP_R423)
  883. trbs_ff = memtrbs_r4xx[data];
  884. else
  885. trbs_ff = memtrbs[data];
  886. tcas_ff.full += trbs_ff.full;
  887. }
  888. sclk_eff_ff.full = sclk_ff.full;
  889. if (rdev->flags & RADEON_IS_AGP) {
  890. fixed20_12 agpmode_ff;
  891. agpmode_ff.full = rfixed_const(radeon_agpmode);
  892. temp_ff.full = rfixed_const_666(16);
  893. sclk_eff_ff.full -= rfixed_mul(agpmode_ff, temp_ff);
  894. }
  895. /* TODO PCIE lanes may affect this - agpmode == 16?? */
  896. if (ASIC_IS_R300(rdev)) {
  897. sclk_delay_ff.full = rfixed_const(250);
  898. } else {
  899. if ((rdev->family == CHIP_RV100) ||
  900. rdev->flags & RADEON_IS_IGP) {
  901. if (rdev->mc.vram_is_ddr)
  902. sclk_delay_ff.full = rfixed_const(41);
  903. else
  904. sclk_delay_ff.full = rfixed_const(33);
  905. } else {
  906. if (rdev->mc.vram_width == 128)
  907. sclk_delay_ff.full = rfixed_const(57);
  908. else
  909. sclk_delay_ff.full = rfixed_const(41);
  910. }
  911. }
  912. mc_latency_sclk.full = rfixed_div(sclk_delay_ff, sclk_eff_ff);
  913. if (rdev->mc.vram_is_ddr) {
  914. if (rdev->mc.vram_width == 32) {
  915. k1.full = rfixed_const(40);
  916. c = 3;
  917. } else {
  918. k1.full = rfixed_const(20);
  919. c = 1;
  920. }
  921. } else {
  922. k1.full = rfixed_const(40);
  923. c = 3;
  924. }
  925. temp_ff.full = rfixed_const(2);
  926. mc_latency_mclk.full = rfixed_mul(trcd_ff, temp_ff);
  927. temp_ff.full = rfixed_const(c);
  928. mc_latency_mclk.full += rfixed_mul(tcas_ff, temp_ff);
  929. temp_ff.full = rfixed_const(4);
  930. mc_latency_mclk.full += rfixed_mul(tras_ff, temp_ff);
  931. mc_latency_mclk.full += rfixed_mul(trp_ff, temp_ff);
  932. mc_latency_mclk.full += k1.full;
  933. mc_latency_mclk.full = rfixed_div(mc_latency_mclk, mclk_ff);
  934. mc_latency_mclk.full += rfixed_div(temp_ff, sclk_eff_ff);
  935. /*
  936. HW cursor time assuming worst case of full size colour cursor.
  937. */
  938. temp_ff.full = rfixed_const((2 * (cur_size - (rdev->mc.vram_is_ddr + 1))));
  939. temp_ff.full += trcd_ff.full;
  940. if (temp_ff.full < tras_ff.full)
  941. temp_ff.full = tras_ff.full;
  942. cur_latency_mclk.full = rfixed_div(temp_ff, mclk_ff);
  943. temp_ff.full = rfixed_const(cur_size);
  944. cur_latency_sclk.full = rfixed_div(temp_ff, sclk_eff_ff);
  945. /*
  946. Find the total latency for the display data.
  947. */
  948. disp_latency_overhead.full = rfixed_const(80);
  949. disp_latency_overhead.full = rfixed_div(disp_latency_overhead, sclk_ff);
  950. mc_latency_mclk.full += disp_latency_overhead.full + cur_latency_mclk.full;
  951. mc_latency_sclk.full += disp_latency_overhead.full + cur_latency_sclk.full;
  952. if (mc_latency_mclk.full > mc_latency_sclk.full)
  953. disp_latency.full = mc_latency_mclk.full;
  954. else
  955. disp_latency.full = mc_latency_sclk.full;
  956. /* setup Max GRPH_STOP_REQ default value */
  957. if (ASIC_IS_RV100(rdev))
  958. max_stop_req = 0x5c;
  959. else
  960. max_stop_req = 0x7c;
  961. if (mode1) {
  962. /* CRTC1
  963. Set GRPH_BUFFER_CNTL register using h/w defined optimal values.
  964. GRPH_STOP_REQ <= MIN[ 0x7C, (CRTC_H_DISP + 1) * (bit depth) / 0x10 ]
  965. */
  966. stop_req = mode1->hdisplay * pixel_bytes1 / 16;
  967. if (stop_req > max_stop_req)
  968. stop_req = max_stop_req;
  969. /*
  970. Find the drain rate of the display buffer.
  971. */
  972. temp_ff.full = rfixed_const((16/pixel_bytes1));
  973. disp_drain_rate.full = rfixed_div(pix_clk, temp_ff);
  974. /*
  975. Find the critical point of the display buffer.
  976. */
  977. crit_point_ff.full = rfixed_mul(disp_drain_rate, disp_latency);
  978. crit_point_ff.full += rfixed_const_half(0);
  979. critical_point = rfixed_trunc(crit_point_ff);
  980. if (rdev->disp_priority == 2) {
  981. critical_point = 0;
  982. }
  983. /*
  984. The critical point should never be above max_stop_req-4. Setting
  985. GRPH_CRITICAL_CNTL = 0 will thus force high priority all the time.
  986. */
  987. if (max_stop_req - critical_point < 4)
  988. critical_point = 0;
  989. if (critical_point == 0 && mode2 && rdev->family == CHIP_R300) {
  990. /* some R300 cards have problem with this set to 0, when CRTC2 is enabled.*/
  991. critical_point = 0x10;
  992. }
  993. temp = RREG32(RADEON_GRPH_BUFFER_CNTL);
  994. temp &= ~(RADEON_GRPH_STOP_REQ_MASK);
  995. temp |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
  996. temp &= ~(RADEON_GRPH_START_REQ_MASK);
  997. if ((rdev->family == CHIP_R350) &&
  998. (stop_req > 0x15)) {
  999. stop_req -= 0x10;
  1000. }
  1001. temp |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
  1002. temp |= RADEON_GRPH_BUFFER_SIZE;
  1003. temp &= ~(RADEON_GRPH_CRITICAL_CNTL |
  1004. RADEON_GRPH_CRITICAL_AT_SOF |
  1005. RADEON_GRPH_STOP_CNTL);
  1006. /*
  1007. Write the result into the register.
  1008. */
  1009. WREG32(RADEON_GRPH_BUFFER_CNTL, ((temp & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
  1010. (critical_point << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
  1011. #if 0
  1012. if ((rdev->family == CHIP_RS400) ||
  1013. (rdev->family == CHIP_RS480)) {
  1014. /* attempt to program RS400 disp regs correctly ??? */
  1015. temp = RREG32(RS400_DISP1_REG_CNTL);
  1016. temp &= ~(RS400_DISP1_START_REQ_LEVEL_MASK |
  1017. RS400_DISP1_STOP_REQ_LEVEL_MASK);
  1018. WREG32(RS400_DISP1_REQ_CNTL1, (temp |
  1019. (critical_point << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
  1020. (critical_point << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
  1021. temp = RREG32(RS400_DMIF_MEM_CNTL1);
  1022. temp &= ~(RS400_DISP1_CRITICAL_POINT_START_MASK |
  1023. RS400_DISP1_CRITICAL_POINT_STOP_MASK);
  1024. WREG32(RS400_DMIF_MEM_CNTL1, (temp |
  1025. (critical_point << RS400_DISP1_CRITICAL_POINT_START_SHIFT) |
  1026. (critical_point << RS400_DISP1_CRITICAL_POINT_STOP_SHIFT)));
  1027. }
  1028. #endif
  1029. DRM_DEBUG("GRPH_BUFFER_CNTL from to %x\n",
  1030. /* (unsigned int)info->SavedReg->grph_buffer_cntl, */
  1031. (unsigned int)RREG32(RADEON_GRPH_BUFFER_CNTL));
  1032. }
  1033. if (mode2) {
  1034. u32 grph2_cntl;
  1035. stop_req = mode2->hdisplay * pixel_bytes2 / 16;
  1036. if (stop_req > max_stop_req)
  1037. stop_req = max_stop_req;
  1038. /*
  1039. Find the drain rate of the display buffer.
  1040. */
  1041. temp_ff.full = rfixed_const((16/pixel_bytes2));
  1042. disp_drain_rate2.full = rfixed_div(pix_clk2, temp_ff);
  1043. grph2_cntl = RREG32(RADEON_GRPH2_BUFFER_CNTL);
  1044. grph2_cntl &= ~(RADEON_GRPH_STOP_REQ_MASK);
  1045. grph2_cntl |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
  1046. grph2_cntl &= ~(RADEON_GRPH_START_REQ_MASK);
  1047. if ((rdev->family == CHIP_R350) &&
  1048. (stop_req > 0x15)) {
  1049. stop_req -= 0x10;
  1050. }
  1051. grph2_cntl |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
  1052. grph2_cntl |= RADEON_GRPH_BUFFER_SIZE;
  1053. grph2_cntl &= ~(RADEON_GRPH_CRITICAL_CNTL |
  1054. RADEON_GRPH_CRITICAL_AT_SOF |
  1055. RADEON_GRPH_STOP_CNTL);
  1056. if ((rdev->family == CHIP_RS100) ||
  1057. (rdev->family == CHIP_RS200))
  1058. critical_point2 = 0;
  1059. else {
  1060. temp = (rdev->mc.vram_width * rdev->mc.vram_is_ddr + 1)/128;
  1061. temp_ff.full = rfixed_const(temp);
  1062. temp_ff.full = rfixed_mul(mclk_ff, temp_ff);
  1063. if (sclk_ff.full < temp_ff.full)
  1064. temp_ff.full = sclk_ff.full;
  1065. read_return_rate.full = temp_ff.full;
  1066. if (mode1) {
  1067. temp_ff.full = read_return_rate.full - disp_drain_rate.full;
  1068. time_disp1_drop_priority.full = rfixed_div(crit_point_ff, temp_ff);
  1069. } else {
  1070. time_disp1_drop_priority.full = 0;
  1071. }
  1072. crit_point_ff.full = disp_latency.full + time_disp1_drop_priority.full + disp_latency.full;
  1073. crit_point_ff.full = rfixed_mul(crit_point_ff, disp_drain_rate2);
  1074. crit_point_ff.full += rfixed_const_half(0);
  1075. critical_point2 = rfixed_trunc(crit_point_ff);
  1076. if (rdev->disp_priority == 2) {
  1077. critical_point2 = 0;
  1078. }
  1079. if (max_stop_req - critical_point2 < 4)
  1080. critical_point2 = 0;
  1081. }
  1082. if (critical_point2 == 0 && rdev->family == CHIP_R300) {
  1083. /* some R300 cards have problem with this set to 0 */
  1084. critical_point2 = 0x10;
  1085. }
  1086. WREG32(RADEON_GRPH2_BUFFER_CNTL, ((grph2_cntl & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
  1087. (critical_point2 << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
  1088. if ((rdev->family == CHIP_RS400) ||
  1089. (rdev->family == CHIP_RS480)) {
  1090. #if 0
  1091. /* attempt to program RS400 disp2 regs correctly ??? */
  1092. temp = RREG32(RS400_DISP2_REQ_CNTL1);
  1093. temp &= ~(RS400_DISP2_START_REQ_LEVEL_MASK |
  1094. RS400_DISP2_STOP_REQ_LEVEL_MASK);
  1095. WREG32(RS400_DISP2_REQ_CNTL1, (temp |
  1096. (critical_point2 << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
  1097. (critical_point2 << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
  1098. temp = RREG32(RS400_DISP2_REQ_CNTL2);
  1099. temp &= ~(RS400_DISP2_CRITICAL_POINT_START_MASK |
  1100. RS400_DISP2_CRITICAL_POINT_STOP_MASK);
  1101. WREG32(RS400_DISP2_REQ_CNTL2, (temp |
  1102. (critical_point2 << RS400_DISP2_CRITICAL_POINT_START_SHIFT) |
  1103. (critical_point2 << RS400_DISP2_CRITICAL_POINT_STOP_SHIFT)));
  1104. #endif
  1105. WREG32(RS400_DISP2_REQ_CNTL1, 0x105DC1CC);
  1106. WREG32(RS400_DISP2_REQ_CNTL2, 0x2749D000);
  1107. WREG32(RS400_DMIF_MEM_CNTL1, 0x29CA71DC);
  1108. WREG32(RS400_DISP1_REQ_CNTL1, 0x28FBC3AC);
  1109. }
  1110. DRM_DEBUG("GRPH2_BUFFER_CNTL from to %x\n",
  1111. (unsigned int)RREG32(RADEON_GRPH2_BUFFER_CNTL));
  1112. }
  1113. }