dw_hdmi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. * Copyright 2014 Rockchip Inc.
  5. * Copyright 2017 Jernej Skrabec <jernej.skrabec@siol.net>
  6. */
  7. #include <common.h>
  8. #include <fdtdec.h>
  9. #include <asm/io.h>
  10. #include "dw_hdmi.h"
  11. struct tmds_n_cts {
  12. u32 tmds;
  13. u32 cts;
  14. u32 n;
  15. };
  16. static const struct tmds_n_cts n_cts_table[] = {
  17. {
  18. .tmds = 25175000, .n = 6144, .cts = 25175,
  19. }, {
  20. .tmds = 25200000, .n = 6144, .cts = 25200,
  21. }, {
  22. .tmds = 27000000, .n = 6144, .cts = 27000,
  23. }, {
  24. .tmds = 27027000, .n = 6144, .cts = 27027,
  25. }, {
  26. .tmds = 40000000, .n = 6144, .cts = 40000,
  27. }, {
  28. .tmds = 54000000, .n = 6144, .cts = 54000,
  29. }, {
  30. .tmds = 54054000, .n = 6144, .cts = 54054,
  31. }, {
  32. .tmds = 65000000, .n = 6144, .cts = 65000,
  33. }, {
  34. .tmds = 74176000, .n = 11648, .cts = 140625,
  35. }, {
  36. .tmds = 74250000, .n = 6144, .cts = 74250,
  37. }, {
  38. .tmds = 83500000, .n = 6144, .cts = 83500,
  39. }, {
  40. .tmds = 106500000, .n = 6144, .cts = 106500,
  41. }, {
  42. .tmds = 108000000, .n = 6144, .cts = 108000,
  43. }, {
  44. .tmds = 148352000, .n = 5824, .cts = 140625,
  45. }, {
  46. .tmds = 148500000, .n = 6144, .cts = 148500,
  47. }, {
  48. .tmds = 297000000, .n = 5120, .cts = 247500,
  49. }
  50. };
  51. static void hdmi_write(struct dw_hdmi *hdmi, u8 val, int offset)
  52. {
  53. switch (hdmi->reg_io_width) {
  54. case 1:
  55. writeb(val, hdmi->ioaddr + offset);
  56. break;
  57. case 4:
  58. writel(val, hdmi->ioaddr + (offset << 2));
  59. break;
  60. default:
  61. debug("reg_io_width has unsupported width!\n");
  62. break;
  63. }
  64. }
  65. static u8 hdmi_read(struct dw_hdmi *hdmi, int offset)
  66. {
  67. switch (hdmi->reg_io_width) {
  68. case 1:
  69. return readb(hdmi->ioaddr + offset);
  70. case 4:
  71. return readl(hdmi->ioaddr + (offset << 2));
  72. default:
  73. debug("reg_io_width has unsupported width!\n");
  74. break;
  75. }
  76. return 0;
  77. }
  78. static void hdmi_mod(struct dw_hdmi *hdmi, unsigned reg, u8 mask, u8 data)
  79. {
  80. u8 val = hdmi_read(hdmi, reg) & ~mask;
  81. val |= data & mask;
  82. hdmi_write(hdmi, val, reg);
  83. }
  84. static void hdmi_set_clock_regenerator(struct dw_hdmi *hdmi, u32 n, u32 cts)
  85. {
  86. uint cts3;
  87. uint n3;
  88. /* first set ncts_atomic_write (if present) */
  89. n3 = HDMI_AUD_N3_NCTS_ATOMIC_WRITE;
  90. hdmi_write(hdmi, n3, HDMI_AUD_N3);
  91. /* set cts_manual (if present) */
  92. cts3 = HDMI_AUD_CTS3_CTS_MANUAL;
  93. cts3 |= HDMI_AUD_CTS3_N_SHIFT_1 << HDMI_AUD_CTS3_N_SHIFT_OFFSET;
  94. cts3 |= (cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK;
  95. /* write cts values; cts3 must be written first */
  96. hdmi_write(hdmi, cts3, HDMI_AUD_CTS3);
  97. hdmi_write(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
  98. hdmi_write(hdmi, cts & 0xff, HDMI_AUD_CTS1);
  99. /* write n values; n1 must be written last */
  100. n3 |= (n >> 16) & HDMI_AUD_N3_AUDN19_16_MASK;
  101. hdmi_write(hdmi, n3, HDMI_AUD_N3);
  102. hdmi_write(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
  103. hdmi_write(hdmi, n & 0xff, HDMI_AUD_N3);
  104. hdmi_write(hdmi, HDMI_AUD_INPUTCLKFS_128, HDMI_AUD_INPUTCLKFS);
  105. }
  106. static int hdmi_lookup_n_cts(u32 pixel_clk)
  107. {
  108. int i;
  109. for (i = 0; i < ARRAY_SIZE(n_cts_table); i++)
  110. if (pixel_clk <= n_cts_table[i].tmds)
  111. break;
  112. if (i >= ARRAY_SIZE(n_cts_table))
  113. return -1;
  114. return i;
  115. }
  116. static void hdmi_audio_set_samplerate(struct dw_hdmi *hdmi, u32 pixel_clk)
  117. {
  118. u32 clk_n, clk_cts;
  119. int index;
  120. index = hdmi_lookup_n_cts(pixel_clk);
  121. if (index == -1) {
  122. debug("audio not supported for pixel clk %d\n", pixel_clk);
  123. return;
  124. }
  125. clk_n = n_cts_table[index].n;
  126. clk_cts = n_cts_table[index].cts;
  127. hdmi_set_clock_regenerator(hdmi, clk_n, clk_cts);
  128. }
  129. /*
  130. * this submodule is responsible for the video data synchronization.
  131. * for example, for rgb 4:4:4 input, the data map is defined as
  132. * pin{47~40} <==> r[7:0]
  133. * pin{31~24} <==> g[7:0]
  134. * pin{15~8} <==> b[7:0]
  135. */
  136. static void hdmi_video_sample(struct dw_hdmi *hdmi)
  137. {
  138. u32 color_format = 0x01;
  139. uint val;
  140. val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
  141. ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
  142. HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
  143. hdmi_write(hdmi, val, HDMI_TX_INVID0);
  144. /* enable tx stuffing: when de is inactive, fix the output data to 0 */
  145. val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
  146. HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
  147. HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
  148. hdmi_write(hdmi, val, HDMI_TX_INSTUFFING);
  149. hdmi_write(hdmi, 0x0, HDMI_TX_GYDATA0);
  150. hdmi_write(hdmi, 0x0, HDMI_TX_GYDATA1);
  151. hdmi_write(hdmi, 0x0, HDMI_TX_RCRDATA0);
  152. hdmi_write(hdmi, 0x0, HDMI_TX_RCRDATA1);
  153. hdmi_write(hdmi, 0x0, HDMI_TX_BCBDATA0);
  154. hdmi_write(hdmi, 0x0, HDMI_TX_BCBDATA1);
  155. }
  156. static void hdmi_video_packetize(struct dw_hdmi *hdmi)
  157. {
  158. u32 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
  159. u32 remap_size = HDMI_VP_REMAP_YCC422_16BIT;
  160. u32 color_depth = 0;
  161. uint val, vp_conf;
  162. /* set the packetizer registers */
  163. val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
  164. HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
  165. ((0 << HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
  166. HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
  167. hdmi_write(hdmi, val, HDMI_VP_PR_CD);
  168. hdmi_mod(hdmi, HDMI_VP_STUFF, HDMI_VP_STUFF_PR_STUFFING_MASK,
  169. HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE);
  170. /* data from pixel repeater block */
  171. vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
  172. HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
  173. hdmi_mod(hdmi, HDMI_VP_CONF, HDMI_VP_CONF_PR_EN_MASK |
  174. HDMI_VP_CONF_BYPASS_SELECT_MASK, vp_conf);
  175. hdmi_mod(hdmi, HDMI_VP_STUFF, HDMI_VP_STUFF_IDEFAULT_PHASE_MASK,
  176. 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET);
  177. hdmi_write(hdmi, remap_size, HDMI_VP_REMAP);
  178. vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
  179. HDMI_VP_CONF_PP_EN_DISABLE |
  180. HDMI_VP_CONF_YCC422_EN_DISABLE;
  181. hdmi_mod(hdmi, HDMI_VP_CONF, HDMI_VP_CONF_BYPASS_EN_MASK |
  182. HDMI_VP_CONF_PP_EN_ENMASK | HDMI_VP_CONF_YCC422_EN_MASK,
  183. vp_conf);
  184. hdmi_mod(hdmi, HDMI_VP_STUFF, HDMI_VP_STUFF_PP_STUFFING_MASK |
  185. HDMI_VP_STUFF_YCC422_STUFFING_MASK,
  186. HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
  187. HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE);
  188. hdmi_mod(hdmi, HDMI_VP_CONF, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
  189. output_select);
  190. }
  191. static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi, uint bit)
  192. {
  193. hdmi_mod(hdmi, HDMI_PHY_TST0, HDMI_PHY_TST0_TSTCLR_MASK,
  194. bit << HDMI_PHY_TST0_TSTCLR_OFFSET);
  195. }
  196. static int hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, u32 msec)
  197. {
  198. ulong start;
  199. u32 val;
  200. start = get_timer(0);
  201. do {
  202. val = hdmi_read(hdmi, HDMI_IH_I2CMPHY_STAT0);
  203. if (val & 0x3) {
  204. hdmi_write(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
  205. return 0;
  206. }
  207. udelay(100);
  208. } while (get_timer(start) < msec);
  209. return 1;
  210. }
  211. static void hdmi_phy_i2c_write(struct dw_hdmi *hdmi, uint data, uint addr)
  212. {
  213. hdmi_write(hdmi, 0xff, HDMI_IH_I2CMPHY_STAT0);
  214. hdmi_write(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
  215. hdmi_write(hdmi, (u8)(data >> 8), HDMI_PHY_I2CM_DATAO_1_ADDR);
  216. hdmi_write(hdmi, (u8)(data >> 0), HDMI_PHY_I2CM_DATAO_0_ADDR);
  217. hdmi_write(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
  218. HDMI_PHY_I2CM_OPERATION_ADDR);
  219. hdmi_phy_wait_i2c_done(hdmi, 1000);
  220. }
  221. static void hdmi_phy_enable_power(struct dw_hdmi *hdmi, uint enable)
  222. {
  223. hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_PDZ_MASK,
  224. enable << HDMI_PHY_CONF0_PDZ_OFFSET);
  225. }
  226. static void hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, uint enable)
  227. {
  228. hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_ENTMDS_MASK,
  229. enable << HDMI_PHY_CONF0_ENTMDS_OFFSET);
  230. }
  231. static void hdmi_phy_enable_spare(struct dw_hdmi *hdmi, uint enable)
  232. {
  233. hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_SPARECTRL_MASK,
  234. enable << HDMI_PHY_CONF0_SPARECTRL_OFFSET);
  235. }
  236. static void hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, uint enable)
  237. {
  238. hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_GEN2_PDDQ_MASK,
  239. enable << HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET);
  240. }
  241. static void hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, uint enable)
  242. {
  243. hdmi_mod(hdmi, HDMI_PHY_CONF0,
  244. HDMI_PHY_CONF0_GEN2_TXPWRON_MASK,
  245. enable << HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET);
  246. }
  247. static void hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, uint enable)
  248. {
  249. hdmi_mod(hdmi, HDMI_PHY_CONF0,
  250. HDMI_PHY_CONF0_SELDATAENPOL_MASK,
  251. enable << HDMI_PHY_CONF0_SELDATAENPOL_OFFSET);
  252. }
  253. static void hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi,
  254. uint enable)
  255. {
  256. hdmi_mod(hdmi, HDMI_PHY_CONF0, HDMI_PHY_CONF0_SELDIPIF_MASK,
  257. enable << HDMI_PHY_CONF0_SELDIPIF_OFFSET);
  258. }
  259. static int hdmi_phy_configure(struct dw_hdmi *hdmi, u32 mpixelclock)
  260. {
  261. ulong start;
  262. uint i, val;
  263. if (!hdmi->mpll_cfg || !hdmi->phy_cfg)
  264. return -1;
  265. /* gen2 tx power off */
  266. hdmi_phy_gen2_txpwron(hdmi, 0);
  267. /* gen2 pddq */
  268. hdmi_phy_gen2_pddq(hdmi, 1);
  269. /* phy reset */
  270. hdmi_write(hdmi, HDMI_MC_PHYRSTZ_DEASSERT, HDMI_MC_PHYRSTZ);
  271. hdmi_write(hdmi, HDMI_MC_PHYRSTZ_ASSERT, HDMI_MC_PHYRSTZ);
  272. hdmi_write(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
  273. hdmi_phy_test_clear(hdmi, 1);
  274. hdmi_write(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
  275. HDMI_PHY_I2CM_SLAVE_ADDR);
  276. hdmi_phy_test_clear(hdmi, 0);
  277. /* pll/mpll cfg - always match on final entry */
  278. for (i = 0; hdmi->mpll_cfg[i].mpixelclock != (~0ul); i++)
  279. if (mpixelclock <= hdmi->mpll_cfg[i].mpixelclock)
  280. break;
  281. hdmi_phy_i2c_write(hdmi, hdmi->mpll_cfg[i].cpce, PHY_OPMODE_PLLCFG);
  282. hdmi_phy_i2c_write(hdmi, hdmi->mpll_cfg[i].gmp, PHY_PLLGMPCTRL);
  283. hdmi_phy_i2c_write(hdmi, hdmi->mpll_cfg[i].curr, PHY_PLLCURRCTRL);
  284. hdmi_phy_i2c_write(hdmi, 0x0000, PHY_PLLPHBYCTRL);
  285. hdmi_phy_i2c_write(hdmi, 0x0006, PHY_PLLCLKBISTPHASE);
  286. for (i = 0; hdmi->phy_cfg[i].mpixelclock != (~0ul); i++)
  287. if (mpixelclock <= hdmi->phy_cfg[i].mpixelclock)
  288. break;
  289. /*
  290. * resistance term 133ohm cfg
  291. * preemp cgf 0.00
  292. * tx/ck lvl 10
  293. */
  294. hdmi_phy_i2c_write(hdmi, hdmi->phy_cfg[i].term, PHY_TXTERM);
  295. hdmi_phy_i2c_write(hdmi, hdmi->phy_cfg[i].sym_ctr, PHY_CKSYMTXCTRL);
  296. hdmi_phy_i2c_write(hdmi, hdmi->phy_cfg[i].vlev_ctr, PHY_VLEVCTRL);
  297. /* remove clk term */
  298. hdmi_phy_i2c_write(hdmi, 0x8000, PHY_CKCALCTRL);
  299. hdmi_phy_enable_power(hdmi, 1);
  300. /* toggle tmds enable */
  301. hdmi_phy_enable_tmds(hdmi, 0);
  302. hdmi_phy_enable_tmds(hdmi, 1);
  303. /* gen2 tx power on */
  304. hdmi_phy_gen2_txpwron(hdmi, 1);
  305. hdmi_phy_gen2_pddq(hdmi, 0);
  306. hdmi_phy_enable_spare(hdmi, 1);
  307. /* wait for phy pll lock */
  308. start = get_timer(0);
  309. do {
  310. val = hdmi_read(hdmi, HDMI_PHY_STAT0);
  311. if (!(val & HDMI_PHY_TX_PHY_LOCK))
  312. return 0;
  313. udelay(100);
  314. } while (get_timer(start) < 5);
  315. return -1;
  316. }
  317. static void hdmi_av_composer(struct dw_hdmi *hdmi,
  318. const struct display_timing *edid)
  319. {
  320. bool mdataenablepolarity = true;
  321. uint inv_val;
  322. uint hbl;
  323. uint vbl;
  324. hbl = edid->hback_porch.typ + edid->hfront_porch.typ +
  325. edid->hsync_len.typ;
  326. vbl = edid->vback_porch.typ + edid->vfront_porch.typ +
  327. edid->vsync_len.typ;
  328. /* set up hdmi_fc_invidconf */
  329. inv_val = HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE;
  330. inv_val |= (edid->flags & DISPLAY_FLAGS_VSYNC_HIGH ?
  331. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
  332. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW);
  333. inv_val |= (edid->flags & DISPLAY_FLAGS_HSYNC_HIGH ?
  334. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
  335. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW);
  336. inv_val |= (mdataenablepolarity ?
  337. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
  338. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
  339. inv_val |= (edid->hdmi_monitor ?
  340. HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
  341. HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE);
  342. inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
  343. inv_val |= HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
  344. hdmi_write(hdmi, inv_val, HDMI_FC_INVIDCONF);
  345. /* set up horizontal active pixel width */
  346. hdmi_write(hdmi, edid->hactive.typ >> 8, HDMI_FC_INHACTV1);
  347. hdmi_write(hdmi, edid->hactive.typ, HDMI_FC_INHACTV0);
  348. /* set up vertical active lines */
  349. hdmi_write(hdmi, edid->vactive.typ >> 8, HDMI_FC_INVACTV1);
  350. hdmi_write(hdmi, edid->vactive.typ, HDMI_FC_INVACTV0);
  351. /* set up horizontal blanking pixel region width */
  352. hdmi_write(hdmi, hbl >> 8, HDMI_FC_INHBLANK1);
  353. hdmi_write(hdmi, hbl, HDMI_FC_INHBLANK0);
  354. /* set up vertical blanking pixel region width */
  355. hdmi_write(hdmi, vbl, HDMI_FC_INVBLANK);
  356. /* set up hsync active edge delay width (in pixel clks) */
  357. hdmi_write(hdmi, edid->hfront_porch.typ >> 8, HDMI_FC_HSYNCINDELAY1);
  358. hdmi_write(hdmi, edid->hfront_porch.typ, HDMI_FC_HSYNCINDELAY0);
  359. /* set up vsync active edge delay (in lines) */
  360. hdmi_write(hdmi, edid->vfront_porch.typ, HDMI_FC_VSYNCINDELAY);
  361. /* set up hsync active pulse width (in pixel clks) */
  362. hdmi_write(hdmi, edid->hsync_len.typ >> 8, HDMI_FC_HSYNCINWIDTH1);
  363. hdmi_write(hdmi, edid->hsync_len.typ, HDMI_FC_HSYNCINWIDTH0);
  364. /* set up vsync active edge delay (in lines) */
  365. hdmi_write(hdmi, edid->vsync_len.typ, HDMI_FC_VSYNCINWIDTH);
  366. }
  367. /* hdmi initialization step b.4 */
  368. static void hdmi_enable_video_path(struct dw_hdmi *hdmi, bool audio)
  369. {
  370. uint clkdis;
  371. /* control period minimum duration */
  372. hdmi_write(hdmi, 12, HDMI_FC_CTRLDUR);
  373. hdmi_write(hdmi, 32, HDMI_FC_EXCTRLDUR);
  374. hdmi_write(hdmi, 1, HDMI_FC_EXCTRLSPAC);
  375. /* set to fill tmds data channels */
  376. hdmi_write(hdmi, 0x0b, HDMI_FC_CH0PREAM);
  377. hdmi_write(hdmi, 0x16, HDMI_FC_CH1PREAM);
  378. hdmi_write(hdmi, 0x21, HDMI_FC_CH2PREAM);
  379. hdmi_write(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
  380. HDMI_MC_FLOWCTRL);
  381. /* enable pixel clock and tmds data path */
  382. clkdis = 0x7f;
  383. clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
  384. hdmi_write(hdmi, clkdis, HDMI_MC_CLKDIS);
  385. clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
  386. hdmi_write(hdmi, clkdis, HDMI_MC_CLKDIS);
  387. if (audio) {
  388. clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;
  389. hdmi_write(hdmi, clkdis, HDMI_MC_CLKDIS);
  390. }
  391. }
  392. /* workaround to clear the overflow condition */
  393. static void hdmi_clear_overflow(struct dw_hdmi *hdmi)
  394. {
  395. uint val, count;
  396. /* tmds software reset */
  397. hdmi_write(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
  398. val = hdmi_read(hdmi, HDMI_FC_INVIDCONF);
  399. for (count = 0; count < 4; count++)
  400. hdmi_write(hdmi, val, HDMI_FC_INVIDCONF);
  401. }
  402. static void hdmi_audio_set_format(struct dw_hdmi *hdmi)
  403. {
  404. hdmi_write(hdmi, HDMI_AUD_CONF0_I2S_SELECT | HDMI_AUD_CONF0_I2S_IN_EN_0,
  405. HDMI_AUD_CONF0);
  406. hdmi_write(hdmi, HDMI_AUD_CONF1_I2S_MODE_STANDARD_MODE |
  407. HDMI_AUD_CONF1_I2S_WIDTH_16BIT, HDMI_AUD_CONF1);
  408. hdmi_write(hdmi, 0x00, HDMI_AUD_CONF2);
  409. }
  410. static void hdmi_audio_fifo_reset(struct dw_hdmi *hdmi)
  411. {
  412. hdmi_write(hdmi, (u8)~HDMI_MC_SWRSTZ_II2SSWRST_REQ, HDMI_MC_SWRSTZ);
  413. hdmi_write(hdmi, HDMI_AUD_CONF0_SW_AUDIO_FIFO_RST, HDMI_AUD_CONF0);
  414. hdmi_write(hdmi, 0x00, HDMI_AUD_INT);
  415. hdmi_write(hdmi, 0x00, HDMI_AUD_INT1);
  416. }
  417. static int hdmi_get_plug_in_status(struct dw_hdmi *hdmi)
  418. {
  419. uint val = hdmi_read(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD;
  420. return !!val;
  421. }
  422. static int hdmi_ddc_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
  423. {
  424. u32 val;
  425. ulong start;
  426. start = get_timer(0);
  427. do {
  428. val = hdmi_read(hdmi, HDMI_IH_I2CM_STAT0);
  429. if (val & 0x2) {
  430. hdmi_write(hdmi, val, HDMI_IH_I2CM_STAT0);
  431. return 0;
  432. }
  433. udelay(100);
  434. } while (get_timer(start) < msec);
  435. return 1;
  436. }
  437. static void hdmi_ddc_reset(struct dw_hdmi *hdmi)
  438. {
  439. hdmi_mod(hdmi, HDMI_I2CM_SOFTRSTZ, HDMI_I2CM_SOFTRSTZ_MASK, 0);
  440. }
  441. static int hdmi_read_edid(struct dw_hdmi *hdmi, int block, u8 *buff)
  442. {
  443. int shift = (block % 2) * 0x80;
  444. int edid_read_err = 0;
  445. u32 trytime = 5;
  446. u32 n;
  447. /* set ddc i2c clk which devided from ddc_clk to 100khz */
  448. hdmi_write(hdmi, hdmi->i2c_clk_high, HDMI_I2CM_SS_SCL_HCNT_0_ADDR);
  449. hdmi_write(hdmi, hdmi->i2c_clk_low, HDMI_I2CM_SS_SCL_LCNT_0_ADDR);
  450. hdmi_mod(hdmi, HDMI_I2CM_DIV, HDMI_I2CM_DIV_FAST_STD_MODE,
  451. HDMI_I2CM_DIV_STD_MODE);
  452. hdmi_write(hdmi, HDMI_I2CM_SLAVE_DDC_ADDR, HDMI_I2CM_SLAVE);
  453. hdmi_write(hdmi, HDMI_I2CM_SEGADDR_DDC, HDMI_I2CM_SEGADDR);
  454. hdmi_write(hdmi, block >> 1, HDMI_I2CM_SEGPTR);
  455. while (trytime--) {
  456. edid_read_err = 0;
  457. for (n = 0; n < HDMI_EDID_BLOCK_SIZE; n++) {
  458. hdmi_write(hdmi, shift + n, HDMI_I2CM_ADDRESS);
  459. if (block == 0)
  460. hdmi_write(hdmi, HDMI_I2CM_OP_RD8,
  461. HDMI_I2CM_OPERATION);
  462. else
  463. hdmi_write(hdmi, HDMI_I2CM_OP_RD8_EXT,
  464. HDMI_I2CM_OPERATION);
  465. if (hdmi_ddc_wait_i2c_done(hdmi, 10)) {
  466. hdmi_ddc_reset(hdmi);
  467. edid_read_err = 1;
  468. break;
  469. }
  470. buff[n] = hdmi_read(hdmi, HDMI_I2CM_DATAI);
  471. }
  472. if (!edid_read_err)
  473. break;
  474. }
  475. return edid_read_err;
  476. }
  477. static const u8 pre_buf[] = {
  478. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  479. 0x04, 0x69, 0xfa, 0x23, 0xc8, 0x28, 0x01, 0x00,
  480. 0x10, 0x17, 0x01, 0x03, 0x80, 0x33, 0x1d, 0x78,
  481. 0x2a, 0xd9, 0x45, 0xa2, 0x55, 0x4d, 0xa0, 0x27,
  482. 0x12, 0x50, 0x54, 0xb7, 0xef, 0x00, 0x71, 0x4f,
  483. 0x81, 0x40, 0x81, 0x80, 0x95, 0x00, 0xb3, 0x00,
  484. 0xd1, 0xc0, 0x81, 0xc0, 0x81, 0x00, 0x02, 0x3a,
  485. 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  486. 0x45, 0x00, 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e,
  487. 0x00, 0x00, 0x00, 0xff, 0x00, 0x44, 0x34, 0x4c,
  488. 0x4d, 0x54, 0x46, 0x30, 0x37, 0x35, 0x39, 0x37,
  489. 0x36, 0x0a, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x32,
  490. 0x4b, 0x18, 0x53, 0x11, 0x00, 0x0a, 0x20, 0x20,
  491. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  492. 0x00, 0x41, 0x53, 0x55, 0x53, 0x20, 0x56, 0x53,
  493. 0x32, 0x33, 0x38, 0x0a, 0x20, 0x20, 0x01, 0xb0,
  494. 0x02, 0x03, 0x22, 0x71, 0x4f, 0x01, 0x02, 0x03,
  495. 0x11, 0x12, 0x13, 0x04, 0x14, 0x05, 0x0e, 0x0f,
  496. 0x1d, 0x1e, 0x1f, 0x10, 0x23, 0x09, 0x17, 0x07,
  497. 0x83, 0x01, 0x00, 0x00, 0x65, 0x03, 0x0c, 0x00,
  498. 0x10, 0x00, 0x8c, 0x0a, 0xd0, 0x8a, 0x20, 0xe0,
  499. 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xfd, 0x1e,
  500. 0x11, 0x00, 0x00, 0x18, 0x01, 0x1d, 0x00, 0x72,
  501. 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
  502. 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e, 0x01, 0x1d,
  503. 0x00, 0xbc, 0x52, 0xd0, 0x1e, 0x20, 0xb8, 0x28,
  504. 0x55, 0x40, 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e,
  505. 0x8c, 0x0a, 0xd0, 0x90, 0x20, 0x40, 0x31, 0x20,
  506. 0x0c, 0x40, 0x55, 0x00, 0xfd, 0x1e, 0x11, 0x00,
  507. 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  508. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  509. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe9,
  510. };
  511. int dw_hdmi_phy_cfg(struct dw_hdmi *hdmi, uint mpixelclock)
  512. {
  513. int i, ret;
  514. /* hdmi phy spec says to do the phy initialization sequence twice */
  515. for (i = 0; i < 2; i++) {
  516. hdmi_phy_sel_data_en_pol(hdmi, 1);
  517. hdmi_phy_sel_interface_control(hdmi, 0);
  518. hdmi_phy_enable_tmds(hdmi, 0);
  519. hdmi_phy_enable_power(hdmi, 0);
  520. ret = hdmi_phy_configure(hdmi, mpixelclock);
  521. if (ret) {
  522. debug("hdmi phy config failure %d\n", ret);
  523. return ret;
  524. }
  525. }
  526. return 0;
  527. }
  528. int dw_hdmi_phy_wait_for_hpd(struct dw_hdmi *hdmi)
  529. {
  530. ulong start;
  531. start = get_timer(0);
  532. do {
  533. if (hdmi_get_plug_in_status(hdmi))
  534. return 0;
  535. udelay(100);
  536. } while (get_timer(start) < 300);
  537. return -1;
  538. }
  539. void dw_hdmi_phy_init(struct dw_hdmi *hdmi)
  540. {
  541. /* enable phy i2cm done irq */
  542. hdmi_write(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
  543. HDMI_PHY_I2CM_INT_ADDR);
  544. /* enable phy i2cm nack & arbitration error irq */
  545. hdmi_write(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
  546. HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
  547. HDMI_PHY_I2CM_CTLINT_ADDR);
  548. /* enable cable hot plug irq */
  549. hdmi_write(hdmi, (u8)~HDMI_PHY_HPD, HDMI_PHY_MASK0);
  550. /* clear hotplug interrupts */
  551. hdmi_write(hdmi, HDMI_IH_PHY_STAT0_HPD, HDMI_IH_PHY_STAT0);
  552. }
  553. int dw_hdmi_read_edid(struct dw_hdmi *hdmi, u8 *buf, int buf_size)
  554. {
  555. u32 edid_size = HDMI_EDID_BLOCK_SIZE;
  556. int ret;
  557. if (0) {
  558. edid_size = sizeof(pre_buf);
  559. memcpy(buf, pre_buf, edid_size);
  560. } else {
  561. ret = hdmi_read_edid(hdmi, 0, buf);
  562. if (ret) {
  563. debug("failed to read edid.\n");
  564. return -1;
  565. }
  566. if (buf[0x7e] != 0) {
  567. hdmi_read_edid(hdmi, 1, buf + HDMI_EDID_BLOCK_SIZE);
  568. edid_size += HDMI_EDID_BLOCK_SIZE;
  569. }
  570. }
  571. return edid_size;
  572. }
  573. int dw_hdmi_enable(struct dw_hdmi *hdmi, const struct display_timing *edid)
  574. {
  575. int ret;
  576. debug("%s, mode info : clock %d hdis %d vdis %d\n",
  577. edid->hdmi_monitor ? "hdmi" : "dvi",
  578. edid->pixelclock.typ, edid->hactive.typ, edid->vactive.typ);
  579. hdmi_av_composer(hdmi, edid);
  580. ret = hdmi->phy_set(hdmi, edid->pixelclock.typ);
  581. if (ret)
  582. return ret;
  583. hdmi_enable_video_path(hdmi, edid->hdmi_monitor);
  584. if (edid->hdmi_monitor) {
  585. hdmi_audio_fifo_reset(hdmi);
  586. hdmi_audio_set_format(hdmi);
  587. hdmi_audio_set_samplerate(hdmi, edid->pixelclock.typ);
  588. }
  589. hdmi_video_packetize(hdmi);
  590. hdmi_video_sample(hdmi);
  591. hdmi_clear_overflow(hdmi);
  592. return 0;
  593. }
  594. void dw_hdmi_init(struct dw_hdmi *hdmi)
  595. {
  596. uint ih_mute;
  597. /*
  598. * boot up defaults are:
  599. * hdmi_ih_mute = 0x03 (disabled)
  600. * hdmi_ih_mute_* = 0x00 (enabled)
  601. *
  602. * disable top level interrupt bits in hdmi block
  603. */
  604. ih_mute = /*hdmi_read(hdmi, HDMI_IH_MUTE) |*/
  605. HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
  606. HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
  607. hdmi_write(hdmi, ih_mute, HDMI_IH_MUTE);
  608. /* enable i2c master done irq */
  609. hdmi_write(hdmi, ~0x04, HDMI_I2CM_INT);
  610. /* enable i2c client nack % arbitration error irq */
  611. hdmi_write(hdmi, ~0x44, HDMI_I2CM_CTLINT);
  612. }