blob: ef18d1f281fa82062075288dc01d7f683e899af4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jernej Skrabec8d91b462017-03-27 19:22:32 +02002/*
3 * Allwinner DW HDMI bridge
4 *
5 * (C) Copyright 2017 Jernej Skrabec <jernej.skrabec@siol.net>
Jernej Skrabec8d91b462017-03-27 19:22:32 +02006 */
7
Samuel Holland65bd46b2022-11-28 01:02:27 -06008#include <clk.h>
Jernej Skrabec8d91b462017-03-27 19:22:32 +02009#include <common.h>
10#include <display.h>
11#include <dm.h>
12#include <dw_hdmi.h>
13#include <edid.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Samuel Holland65bd46b2022-11-28 01:02:27 -060015#include <reset.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070016#include <time.h>
Jernej Skrabec8d91b462017-03-27 19:22:32 +020017#include <asm/io.h>
18#include <asm/arch/clock.h>
19#include <asm/arch/lcdc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Jernej Skrabec8d91b462017-03-27 19:22:32 +020022
23struct sunxi_dw_hdmi_priv {
24 struct dw_hdmi hdmi;
Samuel Holland65bd46b2022-11-28 01:02:27 -060025 struct reset_ctl_bulk resets;
26 struct clk_bulk clocks;
Jernej Skrabec8d91b462017-03-27 19:22:32 +020027};
28
29struct sunxi_hdmi_phy {
30 u32 pol;
31 u32 res1[3];
32 u32 read_en;
33 u32 unscramble;
34 u32 res2[2];
35 u32 ctrl;
36 u32 unk1;
37 u32 unk2;
38 u32 pll;
39 u32 clk;
40 u32 unk3;
41 u32 status;
42};
43
44#define HDMI_PHY_OFFS 0x10000
45
46static int sunxi_dw_hdmi_get_divider(uint clock)
47{
48 /*
49 * Due to missing documentaion of HDMI PHY, we know correct
50 * settings only for following four PHY dividers. Select one
51 * based on clock speed.
52 */
53 if (clock <= 27000000)
54 return 11;
55 else if (clock <= 74250000)
56 return 4;
57 else if (clock <= 148500000)
58 return 2;
59 else
60 return 1;
61}
62
Jernej Skrabecd04dbf72022-11-28 01:02:26 -060063static void sunxi_dw_hdmi_phy_init(struct dw_hdmi *hdmi)
Jernej Skrabec8d91b462017-03-27 19:22:32 +020064{
65 struct sunxi_hdmi_phy * const phy =
Jernej Skrabecd04dbf72022-11-28 01:02:26 -060066 (struct sunxi_hdmi_phy *)(hdmi->ioaddr + HDMI_PHY_OFFS);
Jernej Skrabec8d91b462017-03-27 19:22:32 +020067 unsigned long tmo;
68 u32 tmp;
69
70 /*
71 * HDMI PHY settings are taken as-is from Allwinner BSP code.
72 * There is no documentation.
73 */
74 writel(0, &phy->ctrl);
75 setbits_le32(&phy->ctrl, BIT(0));
76 udelay(5);
77 setbits_le32(&phy->ctrl, BIT(16));
78 setbits_le32(&phy->ctrl, BIT(1));
79 udelay(10);
80 setbits_le32(&phy->ctrl, BIT(2));
81 udelay(5);
82 setbits_le32(&phy->ctrl, BIT(3));
83 udelay(40);
84 setbits_le32(&phy->ctrl, BIT(19));
85 udelay(100);
86 setbits_le32(&phy->ctrl, BIT(18));
87 setbits_le32(&phy->ctrl, 7 << 4);
88
89 /* Note that Allwinner code doesn't fail in case of timeout */
90 tmo = timer_get_us() + 2000;
91 while ((readl(&phy->status) & 0x80) == 0) {
92 if (timer_get_us() > tmo) {
93 printf("Warning: HDMI PHY init timeout!\n");
94 break;
95 }
96 }
97
98 setbits_le32(&phy->ctrl, 0xf << 8);
99 setbits_le32(&phy->ctrl, BIT(7));
100
101 writel(0x39dc5040, &phy->pll);
102 writel(0x80084343, &phy->clk);
103 udelay(10000);
104 writel(1, &phy->unk3);
105 setbits_le32(&phy->pll, BIT(25));
106 udelay(100000);
107 tmp = (readl(&phy->status) & 0x1f800) >> 11;
108 setbits_le32(&phy->pll, BIT(31) | BIT(30));
109 setbits_le32(&phy->pll, tmp);
110 writel(0x01FF0F7F, &phy->ctrl);
111 writel(0x80639000, &phy->unk1);
112 writel(0x0F81C405, &phy->unk2);
113
114 /* enable read access to HDMI controller */
115 writel(0x54524545, &phy->read_en);
116 /* descramble register offsets */
117 writel(0x42494E47, &phy->unscramble);
118}
119
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600120static void sunxi_dw_hdmi_phy_set(struct dw_hdmi *hdmi, uint clock, int phy_div)
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200121{
122 struct sunxi_hdmi_phy * const phy =
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600123 (struct sunxi_hdmi_phy *)(hdmi->ioaddr + HDMI_PHY_OFFS);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200124 int div = sunxi_dw_hdmi_get_divider(clock);
125 u32 tmp;
126
127 /*
128 * Unfortunately, we don't know much about those magic
129 * numbers. They are taken from Allwinner BSP driver.
130 */
131 switch (div) {
132 case 1:
133 writel(0x30dc5fc0, &phy->pll);
Jernej Skrabec89d18022019-03-24 19:26:40 +0100134 writel(0x800863C0 | (phy_div - 1), &phy->clk);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200135 mdelay(10);
136 writel(0x00000001, &phy->unk3);
137 setbits_le32(&phy->pll, BIT(25));
138 mdelay(200);
139 tmp = (readl(&phy->status) & 0x1f800) >> 11;
140 setbits_le32(&phy->pll, BIT(31) | BIT(30));
141 if (tmp < 0x3d)
142 setbits_le32(&phy->pll, tmp + 2);
143 else
144 setbits_le32(&phy->pll, 0x3f);
145 mdelay(100);
146 writel(0x01FFFF7F, &phy->ctrl);
147 writel(0x8063b000, &phy->unk1);
148 writel(0x0F8246B5, &phy->unk2);
149 break;
150 case 2:
151 writel(0x39dc5040, &phy->pll);
Jernej Skrabec89d18022019-03-24 19:26:40 +0100152 writel(0x80084380 | (phy_div - 1), &phy->clk);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200153 mdelay(10);
154 writel(0x00000001, &phy->unk3);
155 setbits_le32(&phy->pll, BIT(25));
156 mdelay(100);
157 tmp = (readl(&phy->status) & 0x1f800) >> 11;
158 setbits_le32(&phy->pll, BIT(31) | BIT(30));
159 setbits_le32(&phy->pll, tmp);
160 writel(0x01FFFF7F, &phy->ctrl);
161 writel(0x8063a800, &phy->unk1);
162 writel(0x0F81C485, &phy->unk2);
163 break;
164 case 4:
165 writel(0x39dc5040, &phy->pll);
Jernej Skrabec89d18022019-03-24 19:26:40 +0100166 writel(0x80084340 | (phy_div - 1), &phy->clk);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200167 mdelay(10);
168 writel(0x00000001, &phy->unk3);
169 setbits_le32(&phy->pll, BIT(25));
170 mdelay(100);
171 tmp = (readl(&phy->status) & 0x1f800) >> 11;
172 setbits_le32(&phy->pll, BIT(31) | BIT(30));
173 setbits_le32(&phy->pll, tmp);
174 writel(0x01FFFF7F, &phy->ctrl);
175 writel(0x8063b000, &phy->unk1);
176 writel(0x0F81C405, &phy->unk2);
177 break;
178 case 11:
179 writel(0x39dc5040, &phy->pll);
Jernej Skrabec89d18022019-03-24 19:26:40 +0100180 writel(0x80084300 | (phy_div - 1), &phy->clk);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200181 mdelay(10);
182 writel(0x00000001, &phy->unk3);
183 setbits_le32(&phy->pll, BIT(25));
184 mdelay(100);
185 tmp = (readl(&phy->status) & 0x1f800) >> 11;
186 setbits_le32(&phy->pll, BIT(31) | BIT(30));
187 setbits_le32(&phy->pll, tmp);
188 writel(0x01FFFF7F, &phy->ctrl);
189 writel(0x8063b000, &phy->unk1);
190 writel(0x0F81C405, &phy->unk2);
191 break;
192 }
193}
194
Jernej Skrabec89d18022019-03-24 19:26:40 +0100195static void sunxi_dw_hdmi_pll_set(uint clk_khz, int *phy_div)
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200196{
Jernej Skrabec89d18022019-03-24 19:26:40 +0100197 int value, n, m, div, diff;
198 int best_n = 0, best_m = 0, best_div = 0, best_diff = 0x0FFFFFFF;
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200199
200 /*
201 * Find the lowest divider resulting in a matching clock. If there
202 * is no match, pick the closest lower clock, as monitors tend to
203 * not sync to higher frequencies.
204 */
Jernej Skrabec89d18022019-03-24 19:26:40 +0100205 for (div = 1; div <= 16; div++) {
206 int target = clk_khz * div;
207
208 if (target < 192000)
209 continue;
210 if (target > 912000)
211 continue;
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200212
Jernej Skrabec89d18022019-03-24 19:26:40 +0100213 for (m = 1; m <= 16; m++) {
214 n = (m * target) / 24000;
215
216 if (n >= 1 && n <= 128) {
217 value = (24000 * n) / m / div;
218 diff = clk_khz - value;
219 if (diff < best_diff) {
220 best_diff = diff;
221 best_m = m;
222 best_n = n;
223 best_div = div;
224 }
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200225 }
226 }
227 }
228
Jernej Skrabec89d18022019-03-24 19:26:40 +0100229 *phy_div = best_div;
230
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200231 clock_set_pll3_factors(best_m, best_n);
232 debug("dotclock: %dkHz = %dkHz: (24MHz * %d) / %d / %d\n",
Jernej Skrabec89d18022019-03-24 19:26:40 +0100233 clk_khz, (clock_get_pll3() / 1000) / best_div,
234 best_n, best_m, best_div);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200235}
236
237static void sunxi_dw_hdmi_lcdc_init(int mux, const struct display_timing *edid,
238 int bpp)
239{
240 struct sunxi_ccm_reg * const ccm =
241 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Mark Kettenis898c6092019-08-09 22:30:26 +0200242 int div = DIV_ROUND_UP(clock_get_pll3(), edid->pixelclock.typ);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200243 struct sunxi_lcdc_reg *lcdc;
244
245 if (mux == 0) {
246 lcdc = (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
247
248 /* Reset off */
249 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
250
251 /* Clock on */
252 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
253 writel(CCM_LCD0_CTRL_GATE | CCM_LCD0_CTRL_M(div),
254 &ccm->lcd0_clk_cfg);
255 } else {
256 lcdc = (struct sunxi_lcdc_reg *)SUNXI_LCD1_BASE;
257
258 /* Reset off */
259 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD1);
260
261 /* Clock on */
262 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD1);
263 writel(CCM_LCD1_CTRL_GATE | CCM_LCD1_CTRL_M(div),
264 &ccm->lcd1_clk_cfg);
265 }
266
267 lcdc_init(lcdc);
268 lcdc_tcon1_mode_set(lcdc, edid, false, false);
269 lcdc_enable(lcdc, bpp);
270}
271
272static int sunxi_dw_hdmi_phy_cfg(struct dw_hdmi *hdmi, uint mpixelclock)
273{
Jernej Skrabec89d18022019-03-24 19:26:40 +0100274 int phy_div;
275
276 sunxi_dw_hdmi_pll_set(mpixelclock / 1000, &phy_div);
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600277 sunxi_dw_hdmi_phy_set(hdmi, mpixelclock, phy_div);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200278
279 return 0;
280}
281
282static int sunxi_dw_hdmi_read_edid(struct udevice *dev, u8 *buf, int buf_size)
283{
284 struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
285
286 return dw_hdmi_read_edid(&priv->hdmi, buf, buf_size);
287}
288
Jernej Skrabec5b2b0a72021-04-22 01:14:26 +0100289static bool sunxi_dw_hdmi_mode_valid(struct udevice *dev,
290 const struct display_timing *timing)
291{
292 return timing->pixelclock.typ <= 297000000;
293}
294
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200295static int sunxi_dw_hdmi_enable(struct udevice *dev, int panel_bpp,
296 const struct display_timing *edid)
297{
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600298 struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200299 struct sunxi_hdmi_phy * const phy =
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600300 (struct sunxi_hdmi_phy *)(priv->hdmi.ioaddr + HDMI_PHY_OFFS);
Jernej Skrabec5dd59e52021-04-22 01:14:33 +0100301 struct display_plat *uc_plat = dev_get_uclass_plat(dev);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200302 int ret;
303
304 ret = dw_hdmi_enable(&priv->hdmi, edid);
305 if (ret)
306 return ret;
307
Jernej Skrabec5dd59e52021-04-22 01:14:33 +0100308 sunxi_dw_hdmi_lcdc_init(uc_plat->source_id, edid, panel_bpp);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200309
Vasily Khoruzhickefce4122018-05-14 13:49:52 -0700310 if (edid->flags & DISPLAY_FLAGS_VSYNC_LOW)
Vasily Khoruzhick97d19672017-11-28 22:33:27 -0800311 setbits_le32(&phy->pol, 0x200);
312
Vasily Khoruzhickefce4122018-05-14 13:49:52 -0700313 if (edid->flags & DISPLAY_FLAGS_HSYNC_LOW)
Vasily Khoruzhick97d19672017-11-28 22:33:27 -0800314 setbits_le32(&phy->pol, 0x100);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200315
316 setbits_le32(&phy->ctrl, 0xf << 12);
317
318 /*
319 * This is last hdmi access before boot, so scramble addresses
320 * again or othwerwise BSP driver won't work. Dummy read is
321 * needed or otherwise last write doesn't get written correctly.
322 */
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600323 (void)readb(priv->hdmi.ioaddr);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200324 writel(0, &phy->unscramble);
325
326 return 0;
327}
328
329static int sunxi_dw_hdmi_probe(struct udevice *dev)
330{
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200331 struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
332 struct sunxi_ccm_reg * const ccm =
333 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
334 int ret;
335
336 /* Set pll3 to 297 MHz */
337 clock_set_pll3(297000000);
338
339 /* Set hdmi parent to pll3 */
340 clrsetbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_PLL_MASK,
341 CCM_HDMI_CTRL_PLL3);
342
Samuel Holland65bd46b2022-11-28 01:02:27 -0600343 /* This reset is referenced from the PHY devicetree node. */
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200344 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI2);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200345
Samuel Holland65bd46b2022-11-28 01:02:27 -0600346 ret = reset_deassert_bulk(&priv->resets);
347 if (ret)
348 return ret;
349
350 ret = clk_enable_bulk(&priv->clocks);
351 if (ret)
352 return ret;
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200353
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600354 sunxi_dw_hdmi_phy_init(&priv->hdmi);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200355
Jernej Skrabec1cf66192021-04-22 01:14:30 +0100356 ret = dw_hdmi_phy_wait_for_hpd(&priv->hdmi);
357 if (ret < 0) {
358 debug("hdmi can not get hpd signal\n");
359 return -1;
360 }
361
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200362 dw_hdmi_init(&priv->hdmi);
363
364 return 0;
365}
366
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600367static int sunxi_dw_hdmi_of_to_plat(struct udevice *dev)
368{
369 struct sunxi_dw_hdmi_priv *priv = dev_get_priv(dev);
370 struct dw_hdmi *hdmi = &priv->hdmi;
Samuel Holland65bd46b2022-11-28 01:02:27 -0600371 int ret;
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600372
373 hdmi->ioaddr = (ulong)dev_read_addr(dev);
374 hdmi->i2c_clk_high = 0xd8;
375 hdmi->i2c_clk_low = 0xfe;
376 hdmi->reg_io_width = 1;
377 hdmi->phy_set = sunxi_dw_hdmi_phy_cfg;
378
Samuel Holland65bd46b2022-11-28 01:02:27 -0600379 ret = reset_get_bulk(dev, &priv->resets);
380 if (ret)
381 return ret;
382
383 ret = clk_get_bulk(dev, &priv->clocks);
384 if (ret)
385 return ret;
386
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600387 return 0;
388}
389
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200390static const struct dm_display_ops sunxi_dw_hdmi_ops = {
391 .read_edid = sunxi_dw_hdmi_read_edid,
392 .enable = sunxi_dw_hdmi_enable,
Jernej Skrabec5b2b0a72021-04-22 01:14:26 +0100393 .mode_valid = sunxi_dw_hdmi_mode_valid,
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200394};
395
Jernej Skrabec97d10d42022-11-28 01:02:25 -0600396static const struct udevice_id sunxi_dw_hdmi_ids[] = {
397 { .compatible = "allwinner,sun8i-a83t-dw-hdmi" },
398 { }
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200399};
400
Jernej Skrabec97d10d42022-11-28 01:02:25 -0600401U_BOOT_DRIVER(sunxi_dw_hdmi) = {
402 .name = "sunxi_dw_hdmi",
403 .id = UCLASS_DISPLAY,
404 .of_match = sunxi_dw_hdmi_ids,
405 .probe = sunxi_dw_hdmi_probe,
Jernej Skrabecd04dbf72022-11-28 01:02:26 -0600406 .of_to_plat = sunxi_dw_hdmi_of_to_plat,
Jernej Skrabec97d10d42022-11-28 01:02:25 -0600407 .priv_auto = sizeof(struct sunxi_dw_hdmi_priv),
408 .ops = &sunxi_dw_hdmi_ops,
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200409};