radeon_legacy_tv.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #include <drm/drmP.h>
  2. #include <drm/drm_crtc_helper.h>
  3. #include "radeon.h"
  4. /*
  5. * Integrated TV out support based on the GATOS code by
  6. * Federico Ulivi <fulivi@lycos.com>
  7. */
  8. /*
  9. * Limits of h/v positions (hPos & vPos)
  10. */
  11. #define MAX_H_POSITION 5 /* Range: [-5..5], negative is on the left, 0 is default, positive is on the right */
  12. #define MAX_V_POSITION 5 /* Range: [-5..5], negative is up, 0 is default, positive is down */
  13. /*
  14. * Unit for hPos (in TV clock periods)
  15. */
  16. #define H_POS_UNIT 10
  17. /*
  18. * Indexes in h. code timing table for horizontal line position adjustment
  19. */
  20. #define H_TABLE_POS1 6
  21. #define H_TABLE_POS2 8
  22. /*
  23. * Limits of hor. size (hSize)
  24. */
  25. #define MAX_H_SIZE 5 /* Range: [-5..5], negative is smaller, positive is larger */
  26. /* tv standard constants */
  27. #define NTSC_TV_CLOCK_T 233
  28. #define NTSC_TV_VFTOTAL 1
  29. #define NTSC_TV_LINES_PER_FRAME 525
  30. #define NTSC_TV_ZERO_H_SIZE 479166
  31. #define NTSC_TV_H_SIZE_UNIT 9478
  32. #define PAL_TV_CLOCK_T 188
  33. #define PAL_TV_VFTOTAL 3
  34. #define PAL_TV_LINES_PER_FRAME 625
  35. #define PAL_TV_ZERO_H_SIZE 473200
  36. #define PAL_TV_H_SIZE_UNIT 9360
  37. /* tv pll setting for 27 mhz ref clk */
  38. #define NTSC_TV_PLL_M_27 22
  39. #define NTSC_TV_PLL_N_27 175
  40. #define NTSC_TV_PLL_P_27 5
  41. #define PAL_TV_PLL_M_27 113
  42. #define PAL_TV_PLL_N_27 668
  43. #define PAL_TV_PLL_P_27 3
  44. /* tv pll setting for 14 mhz ref clk */
  45. #define NTSC_TV_PLL_M_14 33
  46. #define NTSC_TV_PLL_N_14 693
  47. #define NTSC_TV_PLL_P_14 7
  48. #define PAL_TV_PLL_M_14 19
  49. #define PAL_TV_PLL_N_14 353
  50. #define PAL_TV_PLL_P_14 5
  51. #define VERT_LEAD_IN_LINES 2
  52. #define FRAC_BITS 0xe
  53. #define FRAC_MASK 0x3fff
  54. struct radeon_tv_mode_constants {
  55. uint16_t hor_resolution;
  56. uint16_t ver_resolution;
  57. enum radeon_tv_std standard;
  58. uint16_t hor_total;
  59. uint16_t ver_total;
  60. uint16_t hor_start;
  61. uint16_t hor_syncstart;
  62. uint16_t ver_syncstart;
  63. unsigned def_restart;
  64. uint16_t crtcPLL_N;
  65. uint8_t crtcPLL_M;
  66. uint8_t crtcPLL_post_div;
  67. unsigned pix_to_tv;
  68. };
  69. static const uint16_t hor_timing_NTSC[MAX_H_CODE_TIMING_LEN] = {
  70. 0x0007,
  71. 0x003f,
  72. 0x0263,
  73. 0x0a24,
  74. 0x2a6b,
  75. 0x0a36,
  76. 0x126d, /* H_TABLE_POS1 */
  77. 0x1bfe,
  78. 0x1a8f, /* H_TABLE_POS2 */
  79. 0x1ec7,
  80. 0x3863,
  81. 0x1bfe,
  82. 0x1bfe,
  83. 0x1a2a,
  84. 0x1e95,
  85. 0x0e31,
  86. 0x201b,
  87. 0
  88. };
  89. static const uint16_t vert_timing_NTSC[MAX_V_CODE_TIMING_LEN] = {
  90. 0x2001,
  91. 0x200d,
  92. 0x1006,
  93. 0x0c06,
  94. 0x1006,
  95. 0x1818,
  96. 0x21e3,
  97. 0x1006,
  98. 0x0c06,
  99. 0x1006,
  100. 0x1817,
  101. 0x21d4,
  102. 0x0002,
  103. 0
  104. };
  105. static const uint16_t hor_timing_PAL[MAX_H_CODE_TIMING_LEN] = {
  106. 0x0007,
  107. 0x0058,
  108. 0x027c,
  109. 0x0a31,
  110. 0x2a77,
  111. 0x0a95,
  112. 0x124f, /* H_TABLE_POS1 */
  113. 0x1bfe,
  114. 0x1b22, /* H_TABLE_POS2 */
  115. 0x1ef9,
  116. 0x387c,
  117. 0x1bfe,
  118. 0x1bfe,
  119. 0x1b31,
  120. 0x1eb5,
  121. 0x0e43,
  122. 0x201b,
  123. 0
  124. };
  125. static const uint16_t vert_timing_PAL[MAX_V_CODE_TIMING_LEN] = {
  126. 0x2001,
  127. 0x200c,
  128. 0x1005,
  129. 0x0c05,
  130. 0x1005,
  131. 0x1401,
  132. 0x1821,
  133. 0x2240,
  134. 0x1005,
  135. 0x0c05,
  136. 0x1005,
  137. 0x1401,
  138. 0x1822,
  139. 0x2230,
  140. 0x0002,
  141. 0
  142. };
  143. /**********************************************************************
  144. *
  145. * availableModes
  146. *
  147. * Table of all allowed modes for tv output
  148. *
  149. **********************************************************************/
  150. static const struct radeon_tv_mode_constants available_tv_modes[] = {
  151. { /* NTSC timing for 27 Mhz ref clk */
  152. 800, /* horResolution */
  153. 600, /* verResolution */
  154. TV_STD_NTSC, /* standard */
  155. 990, /* horTotal */
  156. 740, /* verTotal */
  157. 813, /* horStart */
  158. 824, /* horSyncStart */
  159. 632, /* verSyncStart */
  160. 625592, /* defRestart */
  161. 592, /* crtcPLL_N */
  162. 91, /* crtcPLL_M */
  163. 4, /* crtcPLL_postDiv */
  164. 1022, /* pixToTV */
  165. },
  166. { /* PAL timing for 27 Mhz ref clk */
  167. 800, /* horResolution */
  168. 600, /* verResolution */
  169. TV_STD_PAL, /* standard */
  170. 1144, /* horTotal */
  171. 706, /* verTotal */
  172. 812, /* horStart */
  173. 824, /* horSyncStart */
  174. 669, /* verSyncStart */
  175. 696700, /* defRestart */
  176. 1382, /* crtcPLL_N */
  177. 231, /* crtcPLL_M */
  178. 4, /* crtcPLL_postDiv */
  179. 759, /* pixToTV */
  180. },
  181. { /* NTSC timing for 14 Mhz ref clk */
  182. 800, /* horResolution */
  183. 600, /* verResolution */
  184. TV_STD_NTSC, /* standard */
  185. 1018, /* horTotal */
  186. 727, /* verTotal */
  187. 813, /* horStart */
  188. 840, /* horSyncStart */
  189. 633, /* verSyncStart */
  190. 630627, /* defRestart */
  191. 347, /* crtcPLL_N */
  192. 14, /* crtcPLL_M */
  193. 8, /* crtcPLL_postDiv */
  194. 1022, /* pixToTV */
  195. },
  196. { /* PAL timing for 14 Mhz ref clk */
  197. 800, /* horResolution */
  198. 600, /* verResolution */
  199. TV_STD_PAL, /* standard */
  200. 1131, /* horTotal */
  201. 742, /* verTotal */
  202. 813, /* horStart */
  203. 840, /* horSyncStart */
  204. 633, /* verSyncStart */
  205. 708369, /* defRestart */
  206. 211, /* crtcPLL_N */
  207. 9, /* crtcPLL_M */
  208. 8, /* crtcPLL_postDiv */
  209. 759, /* pixToTV */
  210. },
  211. };
  212. #define N_AVAILABLE_MODES ARRAY_SIZE(available_tv_modes)
  213. static const struct radeon_tv_mode_constants *radeon_legacy_tv_get_std_mode(struct radeon_encoder *radeon_encoder,
  214. uint16_t *pll_ref_freq)
  215. {
  216. struct drm_device *dev = radeon_encoder->base.dev;
  217. struct radeon_device *rdev = dev->dev_private;
  218. struct radeon_crtc *radeon_crtc;
  219. struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
  220. const struct radeon_tv_mode_constants *const_ptr;
  221. struct radeon_pll *pll;
  222. radeon_crtc = to_radeon_crtc(radeon_encoder->base.crtc);
  223. if (radeon_crtc->crtc_id == 1)
  224. pll = &rdev->clock.p2pll;
  225. else
  226. pll = &rdev->clock.p1pll;
  227. if (pll_ref_freq)
  228. *pll_ref_freq = pll->reference_freq;
  229. if (tv_dac->tv_std == TV_STD_NTSC ||
  230. tv_dac->tv_std == TV_STD_NTSC_J ||
  231. tv_dac->tv_std == TV_STD_PAL_M) {
  232. if (pll->reference_freq == 2700)
  233. const_ptr = &available_tv_modes[0];
  234. else
  235. const_ptr = &available_tv_modes[2];
  236. } else {
  237. if (pll->reference_freq == 2700)
  238. const_ptr = &available_tv_modes[1];
  239. else
  240. const_ptr = &available_tv_modes[3];
  241. }
  242. return const_ptr;
  243. }
  244. static long YCOEF_value[5] = { 2, 2, 0, 4, 0 };
  245. static long YCOEF_EN_value[5] = { 1, 1, 0, 1, 0 };
  246. static long SLOPE_value[5] = { 1, 2, 2, 4, 8 };
  247. static long SLOPE_limit[5] = { 6, 5, 4, 3, 2 };
  248. static void radeon_wait_pll_lock(struct drm_encoder *encoder, unsigned n_tests,
  249. unsigned n_wait_loops, unsigned cnt_threshold)
  250. {
  251. struct drm_device *dev = encoder->dev;
  252. struct radeon_device *rdev = dev->dev_private;
  253. uint32_t save_pll_test;
  254. unsigned int i, j;
  255. WREG32(RADEON_TEST_DEBUG_MUX, (RREG32(RADEON_TEST_DEBUG_MUX) & 0xffff60ff) | 0x100);
  256. save_pll_test = RREG32_PLL(RADEON_PLL_TEST_CNTL);
  257. WREG32_PLL(RADEON_PLL_TEST_CNTL, save_pll_test & ~RADEON_PLL_MASK_READ_B);
  258. WREG8(RADEON_CLOCK_CNTL_INDEX, RADEON_PLL_TEST_CNTL);
  259. for (i = 0; i < n_tests; i++) {
  260. WREG8(RADEON_CLOCK_CNTL_DATA + 3, 0);
  261. for (j = 0; j < n_wait_loops; j++)
  262. if (RREG8(RADEON_CLOCK_CNTL_DATA + 3) >= cnt_threshold)
  263. break;
  264. }
  265. WREG32_PLL(RADEON_PLL_TEST_CNTL, save_pll_test);
  266. WREG32(RADEON_TEST_DEBUG_MUX, RREG32(RADEON_TEST_DEBUG_MUX) & 0xffffe0ff);
  267. }
  268. static void radeon_legacy_tv_write_fifo(struct radeon_encoder *radeon_encoder,
  269. uint16_t addr, uint32_t value)
  270. {
  271. struct drm_device *dev = radeon_encoder->base.dev;
  272. struct radeon_device *rdev = dev->dev_private;
  273. uint32_t tmp;
  274. int i = 0;
  275. WREG32(RADEON_TV_HOST_WRITE_DATA, value);
  276. WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr);
  277. WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr | RADEON_HOST_FIFO_WT);
  278. do {
  279. tmp = RREG32(RADEON_TV_HOST_RD_WT_CNTL);
  280. if ((tmp & RADEON_HOST_FIFO_WT_ACK) == 0)
  281. break;
  282. i++;
  283. } while (i < 10000);
  284. WREG32(RADEON_TV_HOST_RD_WT_CNTL, 0);
  285. }
  286. #if 0 /* included for completeness */
  287. static uint32_t radeon_legacy_tv_read_fifo(struct radeon_encoder *radeon_encoder, uint16_t addr)
  288. {
  289. struct drm_device *dev = radeon_encoder->base.dev;
  290. struct radeon_device *rdev = dev->dev_private;
  291. uint32_t tmp;
  292. int i = 0;
  293. WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr);
  294. WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr | RADEON_HOST_FIFO_RD);
  295. do {
  296. tmp = RREG32(RADEON_TV_HOST_RD_WT_CNTL);
  297. if ((tmp & RADEON_HOST_FIFO_RD_ACK) == 0)
  298. break;
  299. i++;
  300. } while (i < 10000);
  301. WREG32(RADEON_TV_HOST_RD_WT_CNTL, 0);
  302. return RREG32(RADEON_TV_HOST_READ_DATA);
  303. }
  304. #endif
  305. static uint16_t radeon_get_htiming_tables_addr(uint32_t tv_uv_adr)
  306. {
  307. uint16_t h_table;
  308. switch ((tv_uv_adr & RADEON_HCODE_TABLE_SEL_MASK) >> RADEON_HCODE_TABLE_SEL_SHIFT) {
  309. case 0:
  310. h_table = RADEON_TV_MAX_FIFO_ADDR_INTERNAL;
  311. break;
  312. case 1:
  313. h_table = ((tv_uv_adr & RADEON_TABLE1_BOT_ADR_MASK) >> RADEON_TABLE1_BOT_ADR_SHIFT) * 2;
  314. break;
  315. case 2:
  316. h_table = ((tv_uv_adr & RADEON_TABLE3_TOP_ADR_MASK) >> RADEON_TABLE3_TOP_ADR_SHIFT) * 2;
  317. break;
  318. default:
  319. h_table = 0;
  320. break;
  321. }
  322. return h_table;
  323. }
  324. static uint16_t radeon_get_vtiming_tables_addr(uint32_t tv_uv_adr)
  325. {
  326. uint16_t v_table;
  327. switch ((tv_uv_adr & RADEON_VCODE_TABLE_SEL_MASK) >> RADEON_VCODE_TABLE_SEL_SHIFT) {
  328. case 0:
  329. v_table = ((tv_uv_adr & RADEON_MAX_UV_ADR_MASK) >> RADEON_MAX_UV_ADR_SHIFT) * 2 + 1;
  330. break;
  331. case 1:
  332. v_table = ((tv_uv_adr & RADEON_TABLE1_BOT_ADR_MASK) >> RADEON_TABLE1_BOT_ADR_SHIFT) * 2 + 1;
  333. break;
  334. case 2:
  335. v_table = ((tv_uv_adr & RADEON_TABLE3_TOP_ADR_MASK) >> RADEON_TABLE3_TOP_ADR_SHIFT) * 2 + 1;
  336. break;
  337. default:
  338. v_table = 0;
  339. break;
  340. }
  341. return v_table;
  342. }
  343. static void radeon_restore_tv_timing_tables(struct radeon_encoder *radeon_encoder)
  344. {
  345. struct drm_device *dev = radeon_encoder->base.dev;
  346. struct radeon_device *rdev = dev->dev_private;
  347. struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
  348. uint16_t h_table, v_table;
  349. uint32_t tmp;
  350. int i;
  351. WREG32(RADEON_TV_UV_ADR, tv_dac->tv.tv_uv_adr);
  352. h_table = radeon_get_htiming_tables_addr(tv_dac->tv.tv_uv_adr);
  353. v_table = radeon_get_vtiming_tables_addr(tv_dac->tv.tv_uv_adr);
  354. for (i = 0; i < MAX_H_CODE_TIMING_LEN; i += 2, h_table--) {
  355. tmp = ((uint32_t)tv_dac->tv.h_code_timing[i] << 14) | ((uint32_t)tv_dac->tv.h_code_timing[i+1]);
  356. radeon_legacy_tv_write_fifo(radeon_encoder, h_table, tmp);
  357. if (tv_dac->tv.h_code_timing[i] == 0 || tv_dac->tv.h_code_timing[i + 1] == 0)
  358. break;
  359. }
  360. for (i = 0; i < MAX_V_CODE_TIMING_LEN; i += 2, v_table++) {
  361. tmp = ((uint32_t)tv_dac->tv.v_code_timing[i+1] << 14) | ((uint32_t)tv_dac->tv.v_code_timing[i]);
  362. radeon_legacy_tv_write_fifo(radeon_encoder, v_table, tmp);
  363. if (tv_dac->tv.v_code_timing[i] == 0 || tv_dac->tv.v_code_timing[i + 1] == 0)
  364. break;
  365. }
  366. }
  367. static void radeon_legacy_write_tv_restarts(struct radeon_encoder *radeon_encoder)
  368. {
  369. struct drm_device *dev = radeon_encoder->base.dev;
  370. struct radeon_device *rdev = dev->dev_private;
  371. struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
  372. WREG32(RADEON_TV_FRESTART, tv_dac->tv.frestart);
  373. WREG32(RADEON_TV_HRESTART, tv_dac->tv.hrestart);
  374. WREG32(RADEON_TV_VRESTART, tv_dac->tv.vrestart);
  375. }
  376. static bool radeon_legacy_tv_init_restarts(struct drm_encoder *encoder)
  377. {
  378. struct drm_device *dev = encoder->dev;
  379. struct radeon_device *rdev = dev->dev_private;
  380. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  381. struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
  382. struct radeon_crtc *radeon_crtc;
  383. int restart;
  384. unsigned int h_total, v_total, f_total;
  385. int v_offset, h_offset;
  386. u16 p1, p2, h_inc;
  387. bool h_changed;
  388. const struct radeon_tv_mode_constants *const_ptr;
  389. struct radeon_pll *pll;
  390. radeon_crtc = to_radeon_crtc(radeon_encoder->base.crtc);
  391. if (radeon_crtc->crtc_id == 1)
  392. pll = &rdev->clock.p2pll;
  393. else
  394. pll = &rdev->clock.p1pll;
  395. const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
  396. if (!const_ptr)
  397. return false;
  398. h_total = const_ptr->hor_total;
  399. v_total = const_ptr->ver_total;
  400. if (tv_dac->tv_std == TV_STD_NTSC ||
  401. tv_dac->tv_std == TV_STD_NTSC_J ||
  402. tv_dac->tv_std == TV_STD_PAL_M ||
  403. tv_dac->tv_std == TV_STD_PAL_60)
  404. f_total = NTSC_TV_VFTOTAL + 1;
  405. else
  406. f_total = PAL_TV_VFTOTAL + 1;
  407. /* adjust positions 1&2 in hor. cod timing table */
  408. h_offset = tv_dac->h_pos * H_POS_UNIT;
  409. if (tv_dac->tv_std == TV_STD_NTSC ||
  410. tv_dac->tv_std == TV_STD_NTSC_J ||
  411. tv_dac->tv_std == TV_STD_PAL_M) {
  412. h_offset -= 50;
  413. p1 = hor_timing_NTSC[H_TABLE_POS1];
  414. p2 = hor_timing_NTSC[H_TABLE_POS2];
  415. } else {
  416. p1 = hor_timing_PAL[H_TABLE_POS1];
  417. p2 = hor_timing_PAL[H_TABLE_POS2];
  418. }
  419. p1 = (u16)((int)p1 + h_offset);
  420. p2 = (u16)((int)p2 - h_offset);
  421. h_changed = (p1 != tv_dac->tv.h_code_timing[H_TABLE_POS1] ||
  422. p2 != tv_dac->tv.h_code_timing[H_TABLE_POS2]);
  423. tv_dac->tv.h_code_timing[H_TABLE_POS1] = p1;
  424. tv_dac->tv.h_code_timing[H_TABLE_POS2] = p2;
  425. /* Convert hOffset from n. of TV clock periods to n. of CRTC clock periods (CRTC pixels) */
  426. h_offset = (h_offset * (int)(const_ptr->pix_to_tv)) / 1000;
  427. /* adjust restart */
  428. restart = const_ptr->def_restart;
  429. /*
  430. * convert v_pos TV lines to n. of CRTC pixels
  431. */
  432. if (tv_dac->tv_std == TV_STD_NTSC ||
  433. tv_dac->tv_std == TV_STD_NTSC_J ||
  434. tv_dac->tv_std == TV_STD_PAL_M ||
  435. tv_dac->tv_std == TV_STD_PAL_60)
  436. v_offset = ((int)(v_total * h_total) * 2 * tv_dac->v_pos) / (int)(NTSC_TV_LINES_PER_FRAME);
  437. else
  438. v_offset = ((int)(v_total * h_total) * 2 * tv_dac->v_pos) / (int)(PAL_TV_LINES_PER_FRAME);
  439. restart -= v_offset + h_offset;
  440. DRM_DEBUG_KMS("compute_restarts: def = %u h = %d v = %d, p1 = %04x, p2 = %04x, restart = %d\n",
  441. const_ptr->def_restart, tv_dac->h_pos, tv_dac->v_pos, p1, p2, restart);
  442. tv_dac->tv.hrestart = restart % h_total;
  443. restart /= h_total;
  444. tv_dac->tv.vrestart = restart % v_total;
  445. restart /= v_total;
  446. tv_dac->tv.frestart = restart % f_total;
  447. DRM_DEBUG_KMS("compute_restart: F/H/V=%u,%u,%u\n",
  448. (unsigned)tv_dac->tv.frestart,
  449. (unsigned)tv_dac->tv.vrestart,
  450. (unsigned)tv_dac->tv.hrestart);
  451. /* compute h_inc from hsize */
  452. if (tv_dac->tv_std == TV_STD_NTSC ||
  453. tv_dac->tv_std == TV_STD_NTSC_J ||
  454. tv_dac->tv_std == TV_STD_PAL_M)
  455. h_inc = (u16)((int)(const_ptr->hor_resolution * 4096 * NTSC_TV_CLOCK_T) /
  456. (tv_dac->h_size * (int)(NTSC_TV_H_SIZE_UNIT) + (int)(NTSC_TV_ZERO_H_SIZE)));
  457. else
  458. h_inc = (u16)((int)(const_ptr->hor_resolution * 4096 * PAL_TV_CLOCK_T) /
  459. (tv_dac->h_size * (int)(PAL_TV_H_SIZE_UNIT) + (int)(PAL_TV_ZERO_H_SIZE)));
  460. tv_dac->tv.timing_cntl = (tv_dac->tv.timing_cntl & ~RADEON_H_INC_MASK) |
  461. ((u32)h_inc << RADEON_H_INC_SHIFT);
  462. DRM_DEBUG_KMS("compute_restart: h_size = %d h_inc = %d\n", tv_dac->h_size, h_inc);
  463. return h_changed;
  464. }
  465. void radeon_legacy_tv_mode_set(struct drm_encoder *encoder,
  466. struct drm_display_mode *mode,
  467. struct drm_display_mode *adjusted_mode)
  468. {
  469. struct drm_device *dev = encoder->dev;
  470. struct radeon_device *rdev = dev->dev_private;
  471. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  472. struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
  473. const struct radeon_tv_mode_constants *const_ptr;
  474. struct radeon_crtc *radeon_crtc;
  475. int i;
  476. uint16_t pll_ref_freq;
  477. uint32_t vert_space, flicker_removal, tmp;
  478. uint32_t tv_master_cntl, tv_rgb_cntl, tv_dac_cntl;
  479. uint32_t tv_modulator_cntl1, tv_modulator_cntl2;
  480. uint32_t tv_vscaler_cntl1, tv_vscaler_cntl2;
  481. uint32_t tv_pll_cntl, tv_pll_cntl1, tv_ftotal;
  482. uint32_t tv_y_fall_cntl, tv_y_rise_cntl, tv_y_saw_tooth_cntl;
  483. uint32_t m, n, p;
  484. const uint16_t *hor_timing;
  485. const uint16_t *vert_timing;
  486. const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, &pll_ref_freq);
  487. if (!const_ptr)
  488. return;
  489. radeon_crtc = to_radeon_crtc(encoder->crtc);
  490. tv_master_cntl = (RADEON_VIN_ASYNC_RST |
  491. RADEON_CRT_FIFO_CE_EN |
  492. RADEON_TV_FIFO_CE_EN |
  493. RADEON_TV_ON);
  494. if (!ASIC_IS_R300(rdev))
  495. tv_master_cntl |= RADEON_TVCLK_ALWAYS_ONb;
  496. if (tv_dac->tv_std == TV_STD_NTSC ||
  497. tv_dac->tv_std == TV_STD_NTSC_J)
  498. tv_master_cntl |= RADEON_RESTART_PHASE_FIX;
  499. tv_modulator_cntl1 = (RADEON_SLEW_RATE_LIMIT |
  500. RADEON_SYNC_TIP_LEVEL |
  501. RADEON_YFLT_EN |
  502. RADEON_UVFLT_EN |
  503. (6 << RADEON_CY_FILT_BLEND_SHIFT));
  504. if (tv_dac->tv_std == TV_STD_NTSC ||
  505. tv_dac->tv_std == TV_STD_NTSC_J) {
  506. tv_modulator_cntl1 |= (0x46 << RADEON_SET_UP_LEVEL_SHIFT) |
  507. (0x3b << RADEON_BLANK_LEVEL_SHIFT);
  508. tv_modulator_cntl2 = (-111 & RADEON_TV_U_BURST_LEVEL_MASK) |
  509. ((0 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
  510. } else if (tv_dac->tv_std == TV_STD_SCART_PAL) {
  511. tv_modulator_cntl1 |= RADEON_ALT_PHASE_EN;
  512. tv_modulator_cntl2 = (0 & RADEON_TV_U_BURST_LEVEL_MASK) |
  513. ((0 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
  514. } else {
  515. tv_modulator_cntl1 |= RADEON_ALT_PHASE_EN |
  516. (0x3b << RADEON_SET_UP_LEVEL_SHIFT) |
  517. (0x3b << RADEON_BLANK_LEVEL_SHIFT);
  518. tv_modulator_cntl2 = (-78 & RADEON_TV_U_BURST_LEVEL_MASK) |
  519. ((62 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
  520. }
  521. tv_rgb_cntl = (RADEON_RGB_DITHER_EN
  522. | RADEON_TVOUT_SCALE_EN
  523. | (0x0b << RADEON_UVRAM_READ_MARGIN_SHIFT)
  524. | (0x07 << RADEON_FIFORAM_FFMACRO_READ_MARGIN_SHIFT)
  525. | RADEON_RGB_ATTEN_SEL(0x3)
  526. | RADEON_RGB_ATTEN_VAL(0xc));
  527. if (radeon_crtc->crtc_id == 1)
  528. tv_rgb_cntl |= RADEON_RGB_SRC_SEL_CRTC2;
  529. else {
  530. if (radeon_crtc->rmx_type != RMX_OFF)
  531. tv_rgb_cntl |= RADEON_RGB_SRC_SEL_RMX;
  532. else
  533. tv_rgb_cntl |= RADEON_RGB_SRC_SEL_CRTC1;
  534. }
  535. if (tv_dac->tv_std == TV_STD_NTSC ||
  536. tv_dac->tv_std == TV_STD_NTSC_J ||
  537. tv_dac->tv_std == TV_STD_PAL_M ||
  538. tv_dac->tv_std == TV_STD_PAL_60)
  539. vert_space = const_ptr->ver_total * 2 * 10000 / NTSC_TV_LINES_PER_FRAME;
  540. else
  541. vert_space = const_ptr->ver_total * 2 * 10000 / PAL_TV_LINES_PER_FRAME;
  542. tmp = RREG32(RADEON_TV_VSCALER_CNTL1);
  543. tmp &= 0xe3ff0000;
  544. tmp |= (vert_space * (1 << FRAC_BITS) / 10000);
  545. tv_vscaler_cntl1 = tmp;
  546. if (pll_ref_freq == 2700)
  547. tv_vscaler_cntl1 |= RADEON_RESTART_FIELD;
  548. if (const_ptr->hor_resolution == 1024)
  549. tv_vscaler_cntl1 |= (4 << RADEON_Y_DEL_W_SIG_SHIFT);
  550. else
  551. tv_vscaler_cntl1 |= (2 << RADEON_Y_DEL_W_SIG_SHIFT);
  552. /* scale up for int divide */
  553. tmp = const_ptr->ver_total * 2 * 1000;
  554. if (tv_dac->tv_std == TV_STD_NTSC ||
  555. tv_dac->tv_std == TV_STD_NTSC_J ||
  556. tv_dac->tv_std == TV_STD_PAL_M ||
  557. tv_dac->tv_std == TV_STD_PAL_60) {
  558. tmp /= NTSC_TV_LINES_PER_FRAME;
  559. } else {
  560. tmp /= PAL_TV_LINES_PER_FRAME;
  561. }
  562. flicker_removal = (tmp + 500) / 1000;
  563. if (flicker_removal < 3)
  564. flicker_removal = 3;
  565. for (i = 0; i < ARRAY_SIZE(SLOPE_limit); ++i) {
  566. if (flicker_removal == SLOPE_limit[i])
  567. break;
  568. }
  569. tv_y_saw_tooth_cntl = (vert_space * SLOPE_value[i] * (1 << (FRAC_BITS - 1)) +
  570. 5001) / 10000 / 8 | ((SLOPE_value[i] *
  571. (1 << (FRAC_BITS - 1)) / 8) << 16);
  572. tv_y_fall_cntl =
  573. (YCOEF_EN_value[i] << 17) | ((YCOEF_value[i] * (1 << 8) / 8) << 24) |
  574. RADEON_Y_FALL_PING_PONG | (272 * SLOPE_value[i] / 8) * (1 << (FRAC_BITS - 1)) /
  575. 1024;
  576. tv_y_rise_cntl = RADEON_Y_RISE_PING_PONG|
  577. (flicker_removal * 1024 - 272) * SLOPE_value[i] / 8 * (1 << (FRAC_BITS - 1)) / 1024;
  578. tv_vscaler_cntl2 = RREG32(RADEON_TV_VSCALER_CNTL2) & 0x00fffff0;
  579. tv_vscaler_cntl2 |= (0x10 << 24) |
  580. RADEON_DITHER_MODE |
  581. RADEON_Y_OUTPUT_DITHER_EN |
  582. RADEON_UV_OUTPUT_DITHER_EN |
  583. RADEON_UV_TO_BUF_DITHER_EN;
  584. tmp = (tv_vscaler_cntl1 >> RADEON_UV_INC_SHIFT) & RADEON_UV_INC_MASK;
  585. tmp = ((16384 * 256 * 10) / tmp + 5) / 10;
  586. tmp = (tmp << RADEON_UV_OUTPUT_POST_SCALE_SHIFT) | 0x000b0000;
  587. tv_dac->tv.timing_cntl = tmp;
  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. tv_dac->tv_std == TV_STD_PAL_60)
  592. tv_dac_cntl = tv_dac->ntsc_tvdac_adj;
  593. else
  594. tv_dac_cntl = tv_dac->pal_tvdac_adj;
  595. tv_dac_cntl |= RADEON_TV_DAC_NBLANK | RADEON_TV_DAC_NHOLD;
  596. if (tv_dac->tv_std == TV_STD_NTSC ||
  597. tv_dac->tv_std == TV_STD_NTSC_J)
  598. tv_dac_cntl |= RADEON_TV_DAC_STD_NTSC;
  599. else
  600. tv_dac_cntl |= RADEON_TV_DAC_STD_PAL;
  601. if (tv_dac->tv_std == TV_STD_NTSC ||
  602. tv_dac->tv_std == TV_STD_NTSC_J) {
  603. if (pll_ref_freq == 2700) {
  604. m = NTSC_TV_PLL_M_27;
  605. n = NTSC_TV_PLL_N_27;
  606. p = NTSC_TV_PLL_P_27;
  607. } else {
  608. m = NTSC_TV_PLL_M_14;
  609. n = NTSC_TV_PLL_N_14;
  610. p = NTSC_TV_PLL_P_14;
  611. }
  612. } else {
  613. if (pll_ref_freq == 2700) {
  614. m = PAL_TV_PLL_M_27;
  615. n = PAL_TV_PLL_N_27;
  616. p = PAL_TV_PLL_P_27;
  617. } else {
  618. m = PAL_TV_PLL_M_14;
  619. n = PAL_TV_PLL_N_14;
  620. p = PAL_TV_PLL_P_14;
  621. }
  622. }
  623. tv_pll_cntl = (m & RADEON_TV_M0LO_MASK) |
  624. (((m >> 8) & RADEON_TV_M0HI_MASK) << RADEON_TV_M0HI_SHIFT) |
  625. ((n & RADEON_TV_N0LO_MASK) << RADEON_TV_N0LO_SHIFT) |
  626. (((n >> 9) & RADEON_TV_N0HI_MASK) << RADEON_TV_N0HI_SHIFT) |
  627. ((p & RADEON_TV_P_MASK) << RADEON_TV_P_SHIFT);
  628. tv_pll_cntl1 = (((4 & RADEON_TVPCP_MASK) << RADEON_TVPCP_SHIFT) |
  629. ((4 & RADEON_TVPVG_MASK) << RADEON_TVPVG_SHIFT) |
  630. ((1 & RADEON_TVPDC_MASK) << RADEON_TVPDC_SHIFT) |
  631. RADEON_TVCLK_SRC_SEL_TVPLL |
  632. RADEON_TVPLL_TEST_DIS);
  633. tv_dac->tv.tv_uv_adr = 0xc8;
  634. if (tv_dac->tv_std == TV_STD_NTSC ||
  635. tv_dac->tv_std == TV_STD_NTSC_J ||
  636. tv_dac->tv_std == TV_STD_PAL_M ||
  637. tv_dac->tv_std == TV_STD_PAL_60) {
  638. tv_ftotal = NTSC_TV_VFTOTAL;
  639. hor_timing = hor_timing_NTSC;
  640. vert_timing = vert_timing_NTSC;
  641. } else {
  642. hor_timing = hor_timing_PAL;
  643. vert_timing = vert_timing_PAL;
  644. tv_ftotal = PAL_TV_VFTOTAL;
  645. }
  646. for (i = 0; i < MAX_H_CODE_TIMING_LEN; i++) {
  647. if ((tv_dac->tv.h_code_timing[i] = hor_timing[i]) == 0)
  648. break;
  649. }
  650. for (i = 0; i < MAX_V_CODE_TIMING_LEN; i++) {
  651. if ((tv_dac->tv.v_code_timing[i] = vert_timing[i]) == 0)
  652. break;
  653. }
  654. radeon_legacy_tv_init_restarts(encoder);
  655. /* play with DAC_CNTL */
  656. /* play with GPIOPAD_A */
  657. /* DISP_OUTPUT_CNTL */
  658. /* use reference freq */
  659. /* program the TV registers */
  660. WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST |
  661. RADEON_CRT_ASYNC_RST | RADEON_TV_FIFO_ASYNC_RST));
  662. tmp = RREG32(RADEON_TV_DAC_CNTL);
  663. tmp &= ~RADEON_TV_DAC_NBLANK;
  664. tmp |= RADEON_TV_DAC_BGSLEEP |
  665. RADEON_TV_DAC_RDACPD |
  666. RADEON_TV_DAC_GDACPD |
  667. RADEON_TV_DAC_BDACPD;
  668. WREG32(RADEON_TV_DAC_CNTL, tmp);
  669. /* TV PLL */
  670. WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVCLK_SRC_SEL_TVPLL);
  671. WREG32_PLL(RADEON_TV_PLL_CNTL, tv_pll_cntl);
  672. WREG32_PLL_P(RADEON_TV_PLL_CNTL1, RADEON_TVPLL_RESET, ~RADEON_TVPLL_RESET);
  673. radeon_wait_pll_lock(encoder, 200, 800, 135);
  674. WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVPLL_RESET);
  675. radeon_wait_pll_lock(encoder, 300, 160, 27);
  676. radeon_wait_pll_lock(encoder, 200, 800, 135);
  677. WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~0xf);
  678. WREG32_PLL_P(RADEON_TV_PLL_CNTL1, RADEON_TVCLK_SRC_SEL_TVPLL, ~RADEON_TVCLK_SRC_SEL_TVPLL);
  679. WREG32_PLL_P(RADEON_TV_PLL_CNTL1, (1 << RADEON_TVPDC_SHIFT), ~RADEON_TVPDC_MASK);
  680. WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVPLL_SLEEP);
  681. /* TV HV */
  682. WREG32(RADEON_TV_RGB_CNTL, tv_rgb_cntl);
  683. WREG32(RADEON_TV_HTOTAL, const_ptr->hor_total - 1);
  684. WREG32(RADEON_TV_HDISP, const_ptr->hor_resolution - 1);
  685. WREG32(RADEON_TV_HSTART, const_ptr->hor_start);
  686. WREG32(RADEON_TV_VTOTAL, const_ptr->ver_total - 1);
  687. WREG32(RADEON_TV_VDISP, const_ptr->ver_resolution - 1);
  688. WREG32(RADEON_TV_FTOTAL, tv_ftotal);
  689. WREG32(RADEON_TV_VSCALER_CNTL1, tv_vscaler_cntl1);
  690. WREG32(RADEON_TV_VSCALER_CNTL2, tv_vscaler_cntl2);
  691. WREG32(RADEON_TV_Y_FALL_CNTL, tv_y_fall_cntl);
  692. WREG32(RADEON_TV_Y_RISE_CNTL, tv_y_rise_cntl);
  693. WREG32(RADEON_TV_Y_SAW_TOOTH_CNTL, tv_y_saw_tooth_cntl);
  694. WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST |
  695. RADEON_CRT_ASYNC_RST));
  696. /* TV restarts */
  697. radeon_legacy_write_tv_restarts(radeon_encoder);
  698. /* tv timings */
  699. radeon_restore_tv_timing_tables(radeon_encoder);
  700. WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST));
  701. /* tv std */
  702. WREG32(RADEON_TV_SYNC_CNTL, (RADEON_SYNC_PUB | RADEON_TV_SYNC_IO_DRIVE));
  703. WREG32(RADEON_TV_TIMING_CNTL, tv_dac->tv.timing_cntl);
  704. WREG32(RADEON_TV_MODULATOR_CNTL1, tv_modulator_cntl1);
  705. WREG32(RADEON_TV_MODULATOR_CNTL2, tv_modulator_cntl2);
  706. WREG32(RADEON_TV_PRE_DAC_MUX_CNTL, (RADEON_Y_RED_EN |
  707. RADEON_C_GRN_EN |
  708. RADEON_CMP_BLU_EN |
  709. RADEON_DAC_DITHER_EN));
  710. WREG32(RADEON_TV_CRC_CNTL, 0);
  711. WREG32(RADEON_TV_MASTER_CNTL, tv_master_cntl);
  712. WREG32(RADEON_TV_GAIN_LIMIT_SETTINGS, ((0x17f << RADEON_UV_GAIN_LIMIT_SHIFT) |
  713. (0x5ff << RADEON_Y_GAIN_LIMIT_SHIFT)));
  714. WREG32(RADEON_TV_LINEAR_GAIN_SETTINGS, ((0x100 << RADEON_UV_GAIN_SHIFT) |
  715. (0x100 << RADEON_Y_GAIN_SHIFT)));
  716. WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
  717. }
  718. void radeon_legacy_tv_adjust_crtc_reg(struct drm_encoder *encoder,
  719. uint32_t *h_total_disp, uint32_t *h_sync_strt_wid,
  720. uint32_t *v_total_disp, uint32_t *v_sync_strt_wid)
  721. {
  722. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  723. const struct radeon_tv_mode_constants *const_ptr;
  724. uint32_t tmp;
  725. const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
  726. if (!const_ptr)
  727. return;
  728. *h_total_disp = (((const_ptr->hor_resolution / 8) - 1) << RADEON_CRTC_H_DISP_SHIFT) |
  729. (((const_ptr->hor_total / 8) - 1) << RADEON_CRTC_H_TOTAL_SHIFT);
  730. tmp = *h_sync_strt_wid;
  731. tmp &= ~(RADEON_CRTC_H_SYNC_STRT_PIX | RADEON_CRTC_H_SYNC_STRT_CHAR);
  732. tmp |= (((const_ptr->hor_syncstart / 8) - 1) << RADEON_CRTC_H_SYNC_STRT_CHAR_SHIFT) |
  733. (const_ptr->hor_syncstart & 7);
  734. *h_sync_strt_wid = tmp;
  735. *v_total_disp = ((const_ptr->ver_resolution - 1) << RADEON_CRTC_V_DISP_SHIFT) |
  736. ((const_ptr->ver_total - 1) << RADEON_CRTC_V_TOTAL_SHIFT);
  737. tmp = *v_sync_strt_wid;
  738. tmp &= ~RADEON_CRTC_V_SYNC_STRT;
  739. tmp |= ((const_ptr->ver_syncstart - 1) << RADEON_CRTC_V_SYNC_STRT_SHIFT);
  740. *v_sync_strt_wid = tmp;
  741. }
  742. static int get_post_div(int value)
  743. {
  744. int post_div;
  745. switch (value) {
  746. case 1: post_div = 0; break;
  747. case 2: post_div = 1; break;
  748. case 3: post_div = 4; break;
  749. case 4: post_div = 2; break;
  750. case 6: post_div = 6; break;
  751. case 8: post_div = 3; break;
  752. case 12: post_div = 7; break;
  753. case 16:
  754. default: post_div = 5; break;
  755. }
  756. return post_div;
  757. }
  758. void radeon_legacy_tv_adjust_pll1(struct drm_encoder *encoder,
  759. uint32_t *htotal_cntl, uint32_t *ppll_ref_div,
  760. uint32_t *ppll_div_3, uint32_t *pixclks_cntl)
  761. {
  762. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  763. const struct radeon_tv_mode_constants *const_ptr;
  764. const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
  765. if (!const_ptr)
  766. return;
  767. *htotal_cntl = (const_ptr->hor_total & 0x7) | RADEON_HTOT_CNTL_VGA_EN;
  768. *ppll_ref_div = const_ptr->crtcPLL_M;
  769. *ppll_div_3 = (const_ptr->crtcPLL_N & 0x7ff) | (get_post_div(const_ptr->crtcPLL_post_div) << 16);
  770. *pixclks_cntl &= ~(RADEON_PIX2CLK_SRC_SEL_MASK | RADEON_PIXCLK_TV_SRC_SEL);
  771. *pixclks_cntl |= RADEON_PIX2CLK_SRC_SEL_P2PLLCLK;
  772. }
  773. void radeon_legacy_tv_adjust_pll2(struct drm_encoder *encoder,
  774. uint32_t *htotal2_cntl, uint32_t *p2pll_ref_div,
  775. uint32_t *p2pll_div_0, uint32_t *pixclks_cntl)
  776. {
  777. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  778. const struct radeon_tv_mode_constants *const_ptr;
  779. const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
  780. if (!const_ptr)
  781. return;
  782. *htotal2_cntl = (const_ptr->hor_total & 0x7);
  783. *p2pll_ref_div = const_ptr->crtcPLL_M;
  784. *p2pll_div_0 = (const_ptr->crtcPLL_N & 0x7ff) | (get_post_div(const_ptr->crtcPLL_post_div) << 16);
  785. *pixclks_cntl &= ~RADEON_PIX2CLK_SRC_SEL_MASK;
  786. *pixclks_cntl |= RADEON_PIX2CLK_SRC_SEL_P2PLLCLK | RADEON_PIXCLK_TV_SRC_SEL;
  787. }