sunxi_dw_hdmi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Allwinner DW HDMI bridge
  4. *
  5. * (C) Copyright 2017 Jernej Skrabec <jernej.skrabec@siol.net>
  6. */
  7. #include <common.h>
  8. #include <display.h>
  9. #include <dm.h>
  10. #include <dw_hdmi.h>
  11. #include <edid.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/clock.h>
  14. #include <asm/arch/lcdc.h>
  15. struct sunxi_dw_hdmi_priv {
  16. struct dw_hdmi hdmi;
  17. int mux;
  18. };
  19. struct sunxi_hdmi_phy {
  20. u32 pol;
  21. u32 res1[3];
  22. u32 read_en;
  23. u32 unscramble;
  24. u32 res2[2];
  25. u32 ctrl;
  26. u32 unk1;
  27. u32 unk2;
  28. u32 pll;
  29. u32 clk;
  30. u32 unk3;
  31. u32 status;
  32. };
  33. #define HDMI_PHY_OFFS 0x10000
  34. static int sunxi_dw_hdmi_get_divider(uint clock)
  35. {
  36. /*
  37. * Due to missing documentaion of HDMI PHY, we know correct
  38. * settings only for following four PHY dividers. Select one
  39. * based on clock speed.
  40. */
  41. if (clock <= 27000000)
  42. return 11;
  43. else if (clock <= 74250000)
  44. return 4;
  45. else if (clock <= 148500000)
  46. return 2;
  47. else
  48. return 1;
  49. }
  50. static void sunxi_dw_hdmi_phy_init(void)
  51. {
  52. struct sunxi_hdmi_phy * const phy =
  53. (struct sunxi_hdmi_phy *)(SUNXI_HDMI_BASE + HDMI_PHY_OFFS);
  54. unsigned long tmo;
  55. u32 tmp;
  56. /*
  57. * HDMI PHY settings are taken as-is from Allwinner BSP code.
  58. * There is no documentation.
  59. */
  60. writel(0, &phy->ctrl);
  61. setbits_le32(&phy->ctrl, BIT(0));
  62. udelay(5);
  63. setbits_le32(&phy->ctrl, BIT(16));
  64. setbits_le32(&phy->ctrl, BIT(1));
  65. udelay(10);
  66. setbits_le32(&phy->ctrl, BIT(2));
  67. udelay(5);
  68. setbits_le32(&phy->ctrl, BIT(3));
  69. udelay(40);
  70. setbits_le32(&phy->ctrl, BIT(19));
  71. udelay(100);
  72. setbits_le32(&phy->ctrl, BIT(18));
  73. setbits_le32(&phy->ctrl, 7 << 4);
  74. /* Note that Allwinner code doesn't fail in case of timeout */
  75. tmo = timer_get_us() + 2000;
  76. while ((readl(&phy->status) & 0x80) == 0) {
  77. if (timer_get_us() > tmo) {
  78. printf("Warning: HDMI PHY init timeout!\n");
  79. break;
  80. }
  81. }
  82. setbits_le32(&phy->ctrl, 0xf << 8);
  83. setbits_le32(&phy->ctrl, BIT(7));
  84. writel(0x39dc5040, &phy->pll);
  85. writel(0x80084343, &phy->clk);
  86. udelay(10000);
  87. writel(1, &phy->unk3);
  88. setbits_le32(&phy->pll, BIT(25));
  89. udelay(100000);
  90. tmp = (readl(&phy->status) & 0x1f800) >> 11;
  91. setbits_le32(&phy->pll, BIT(31) | BIT(30));
  92. setbits_le32(&phy->pll, tmp);
  93. writel(0x01FF0F7F, &phy->ctrl);
  94. writel(0x80639000, &phy->unk1);
  95. writel(0x0F81C405, &phy->unk2);
  96. /* enable read access to HDMI controller */
  97. writel(0x54524545, &phy->read_en);
  98. /* descramble register offsets */
  99. writel(0x42494E47, &phy->unscramble);
  100. }
  101. static int sunxi_dw_hdmi_get_plug_in_status(void)
  102. {
  103. struct sunxi_hdmi_phy * const phy =
  104. (struct sunxi_hdmi_phy *)(SUNXI_HDMI_BASE + HDMI_PHY_OFFS);
  105. return !!(readl(&phy->status) & (1 << 19));
  106. }
  107. static int sunxi_dw_hdmi_wait_for_hpd(void)
  108. {
  109. ulong start;
  110. start = get_timer(0);
  111. do {
  112. if (sunxi_dw_hdmi_get_plug_in_status())
  113. return 0;
  114. udelay(100);
  115. } while (get_timer(start) < 300);
  116. return -1;
  117. }
  118. static void sunxi_dw_hdmi_phy_set(uint clock)
  119. {
  120. struct sunxi_hdmi_phy * const phy =
  121. (struct sunxi_hdmi_phy *)(SUNXI_HDMI_BASE + HDMI_PHY_OFFS);
  122. int div = sunxi_dw_hdmi_get_divider(clock);
  123. u32 tmp;
  124. /*
  125. * Unfortunately, we don't know much about those magic
  126. * numbers. They are taken from Allwinner BSP driver.
  127. */
  128. switch (div) {
  129. case 1:
  130. writel(0x30dc5fc0, &phy->pll);
  131. writel(0x800863C0, &phy->clk);
  132. mdelay(10);
  133. writel(0x00000001, &phy->unk3);
  134. setbits_le32(&phy->pll, BIT(25));
  135. mdelay(200);
  136. tmp = (readl(&phy->status) & 0x1f800) >> 11;
  137. setbits_le32(&phy->pll, BIT(31) | BIT(30));
  138. if (tmp < 0x3d)
  139. setbits_le32(&phy->pll, tmp + 2);
  140. else
  141. setbits_le32(&phy->pll, 0x3f);
  142. mdelay(100);
  143. writel(0x01FFFF7F, &phy->ctrl);
  144. writel(0x8063b000, &phy->unk1);
  145. writel(0x0F8246B5, &phy->unk2);
  146. break;
  147. case 2:
  148. writel(0x39dc5040, &phy->pll);
  149. writel(0x80084381, &phy->clk);
  150. mdelay(10);
  151. writel(0x00000001, &phy->unk3);
  152. setbits_le32(&phy->pll, BIT(25));
  153. mdelay(100);
  154. tmp = (readl(&phy->status) & 0x1f800) >> 11;
  155. setbits_le32(&phy->pll, BIT(31) | BIT(30));
  156. setbits_le32(&phy->pll, tmp);
  157. writel(0x01FFFF7F, &phy->ctrl);
  158. writel(0x8063a800, &phy->unk1);
  159. writel(0x0F81C485, &phy->unk2);
  160. break;
  161. case 4:
  162. writel(0x39dc5040, &phy->pll);
  163. writel(0x80084343, &phy->clk);
  164. mdelay(10);
  165. writel(0x00000001, &phy->unk3);
  166. setbits_le32(&phy->pll, BIT(25));
  167. mdelay(100);
  168. tmp = (readl(&phy->status) & 0x1f800) >> 11;
  169. setbits_le32(&phy->pll, BIT(31) | BIT(30));
  170. setbits_le32(&phy->pll, tmp);
  171. writel(0x01FFFF7F, &phy->ctrl);
  172. writel(0x8063b000, &phy->unk1);
  173. writel(0x0F81C405, &phy->unk2);
  174. break;
  175. case 11:
  176. writel(0x39dc5040, &phy->pll);
  177. writel(0x8008430a, &phy->clk);
  178. mdelay(10);
  179. writel(0x00000001, &phy->unk3);
  180. setbits_le32(&phy->pll, BIT(25));
  181. mdelay(100);
  182. tmp = (readl(&phy->status) & 0x1f800) >> 11;
  183. setbits_le32(&phy->pll, BIT(31) | BIT(30));
  184. setbits_le32(&phy->pll, tmp);
  185. writel(0x01FFFF7F, &phy->ctrl);
  186. writel(0x8063b000, &phy->unk1);
  187. writel(0x0F81C405, &phy->unk2);
  188. break;
  189. }
  190. }
  191. static void sunxi_dw_hdmi_pll_set(uint clk_khz)
  192. {
  193. int value, n, m, div = 0, diff;
  194. int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
  195. div = sunxi_dw_hdmi_get_divider(clk_khz * 1000);
  196. /*
  197. * Find the lowest divider resulting in a matching clock. If there
  198. * is no match, pick the closest lower clock, as monitors tend to
  199. * not sync to higher frequencies.
  200. */
  201. for (m = 1; m <= 16; m++) {
  202. n = (m * div * clk_khz) / 24000;
  203. if ((n >= 1) && (n <= 128)) {
  204. value = (24000 * n) / m / div;
  205. diff = clk_khz - value;
  206. if (diff < best_diff) {
  207. best_diff = diff;
  208. best_m = m;
  209. best_n = n;
  210. }
  211. }
  212. }
  213. clock_set_pll3_factors(best_m, best_n);
  214. debug("dotclock: %dkHz = %dkHz: (24MHz * %d) / %d / %d\n",
  215. clk_khz, (clock_get_pll3() / 1000) / div,
  216. best_n, best_m, div);
  217. }
  218. static void sunxi_dw_hdmi_lcdc_init(int mux, const struct display_timing *edid,
  219. int bpp)
  220. {
  221. struct sunxi_ccm_reg * const ccm =
  222. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  223. int div = sunxi_dw_hdmi_get_divider(edid->pixelclock.typ);
  224. struct sunxi_lcdc_reg *lcdc;
  225. if (mux == 0) {
  226. lcdc = (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
  227. /* Reset off */
  228. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
  229. /* Clock on */
  230. setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
  231. writel(CCM_LCD0_CTRL_GATE | CCM_LCD0_CTRL_M(div),
  232. &ccm->lcd0_clk_cfg);
  233. } else {
  234. lcdc = (struct sunxi_lcdc_reg *)SUNXI_LCD1_BASE;
  235. /* Reset off */
  236. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD1);
  237. /* Clock on */
  238. setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD1);
  239. writel(CCM_LCD1_CTRL_GATE | CCM_LCD1_CTRL_M(div),
  240. &ccm->lcd1_clk_cfg);
  241. }
  242. lcdc_init(lcdc);
  243. lcdc_tcon1_mode_set(lcdc, edid, false, false);
  244. lcdc_enable(lcdc, bpp);
  245. }
  246. static int sunxi_dw_hdmi_phy_cfg(struct dw_hdmi *hdmi, uint mpixelclock)
  247. {
  248. sunxi_dw_hdmi_pll_set(mpixelclock/1000);
  249. sunxi_dw_hdmi_phy_set(mpixelclock);
  250. return 0;
  251. }
  252. static int sunxi_dw_hdmi_read_edid(struct udevice *dev, u8 *buf, int buf_size)
  253. {
  254. struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
  255. return dw_hdmi_read_edid(&priv->hdmi, buf, buf_size);
  256. }
  257. static int sunxi_dw_hdmi_enable(struct udevice *dev, int panel_bpp,
  258. const struct display_timing *edid)
  259. {
  260. struct sunxi_hdmi_phy * const phy =
  261. (struct sunxi_hdmi_phy *)(SUNXI_HDMI_BASE + HDMI_PHY_OFFS);
  262. struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
  263. int ret;
  264. ret = dw_hdmi_enable(&priv->hdmi, edid);
  265. if (ret)
  266. return ret;
  267. sunxi_dw_hdmi_lcdc_init(priv->mux, edid, panel_bpp);
  268. if (edid->flags & DISPLAY_FLAGS_VSYNC_LOW)
  269. setbits_le32(&phy->pol, 0x200);
  270. if (edid->flags & DISPLAY_FLAGS_HSYNC_LOW)
  271. setbits_le32(&phy->pol, 0x100);
  272. setbits_le32(&phy->ctrl, 0xf << 12);
  273. /*
  274. * This is last hdmi access before boot, so scramble addresses
  275. * again or othwerwise BSP driver won't work. Dummy read is
  276. * needed or otherwise last write doesn't get written correctly.
  277. */
  278. (void)readb(SUNXI_HDMI_BASE);
  279. writel(0, &phy->unscramble);
  280. return 0;
  281. }
  282. static int sunxi_dw_hdmi_probe(struct udevice *dev)
  283. {
  284. struct display_plat *uc_plat = dev_get_uclass_platdata(dev);
  285. struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
  286. struct sunxi_ccm_reg * const ccm =
  287. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  288. int ret;
  289. /* Set pll3 to 297 MHz */
  290. clock_set_pll3(297000000);
  291. /* Set hdmi parent to pll3 */
  292. clrsetbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_PLL_MASK,
  293. CCM_HDMI_CTRL_PLL3);
  294. /* Set ahb gating to pass */
  295. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
  296. setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI2);
  297. setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
  298. setbits_le32(&ccm->hdmi_slow_clk_cfg, CCM_HDMI_SLOW_CTRL_DDC_GATE);
  299. /* Clock on */
  300. setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
  301. sunxi_dw_hdmi_phy_init();
  302. ret = sunxi_dw_hdmi_wait_for_hpd();
  303. if (ret < 0) {
  304. debug("hdmi can not get hpd signal\n");
  305. return -1;
  306. }
  307. priv->hdmi.ioaddr = SUNXI_HDMI_BASE;
  308. priv->hdmi.i2c_clk_high = 0xd8;
  309. priv->hdmi.i2c_clk_low = 0xfe;
  310. priv->hdmi.reg_io_width = 1;
  311. priv->hdmi.phy_set = sunxi_dw_hdmi_phy_cfg;
  312. priv->mux = uc_plat->source_id;
  313. dw_hdmi_init(&priv->hdmi);
  314. return 0;
  315. }
  316. static const struct dm_display_ops sunxi_dw_hdmi_ops = {
  317. .read_edid = sunxi_dw_hdmi_read_edid,
  318. .enable = sunxi_dw_hdmi_enable,
  319. };
  320. U_BOOT_DRIVER(sunxi_dw_hdmi) = {
  321. .name = "sunxi_dw_hdmi",
  322. .id = UCLASS_DISPLAY,
  323. .ops = &sunxi_dw_hdmi_ops,
  324. .probe = sunxi_dw_hdmi_probe,
  325. .priv_auto_alloc_size = sizeof(struct sunxi_dw_hdmi_priv),
  326. };
  327. U_BOOT_DEVICE(sunxi_dw_hdmi) = {
  328. .name = "sunxi_dw_hdmi"
  329. };