blob: 18fa83dcedf9a576c43d98339def12067fe6b24e [file] [log] [blame]
Luc Verhaegenb01df1e2014-08-13 07:55:06 +02001/*
2 * Display driver for Allwinner SoCs.
3 *
4 * (C) Copyright 2013-2014 Luc Verhaegen <libv@skynet.be>
5 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11
12#include <asm/arch/clock.h>
13#include <asm/arch/display.h>
14#include <asm/global_data.h>
15#include <asm/io.h>
Luc Verhaegen4869a8c2014-08-13 07:55:07 +020016#include <fdtdec.h>
17#include <fdt_support.h>
Luc Verhaegenb01df1e2014-08-13 07:55:06 +020018#include <video_fb.h>
Hans de Goedeccb0ed52014-12-19 13:46:33 +010019#include "videomodes.h"
Luc Verhaegenb01df1e2014-08-13 07:55:06 +020020
21DECLARE_GLOBAL_DATA_PTR;
22
23struct sunxi_display {
24 GraphicDevice graphic_device;
25 bool enabled;
26} sunxi_display;
27
28static int sunxi_hdmi_hpd_detect(void)
29{
30 struct sunxi_ccm_reg * const ccm =
31 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
32 struct sunxi_hdmi_reg * const hdmi =
33 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
34
35 /* Set pll3 to 300MHz */
36 clock_set_pll3(300000000);
37
38 /* Set hdmi parent to pll3 */
39 clrsetbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_PLL_MASK,
40 CCM_HDMI_CTRL_PLL3);
41
42 /* Set ahb gating to pass */
Hans de Goedef651e0a2014-11-14 17:42:14 +010043#ifdef CONFIG_MACH_SUN6I
44 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
45#endif
Luc Verhaegenb01df1e2014-08-13 07:55:06 +020046 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
47
48 /* Clock on */
49 setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
50
51 writel(SUNXI_HDMI_CTRL_ENABLE, &hdmi->ctrl);
52 writel(SUNXI_HDMI_PAD_CTRL0_HDP, &hdmi->pad_ctrl0);
53
54 udelay(1000);
55
Hans de Goede695bda42014-12-19 15:13:57 +010056 return (readl(&hdmi->hpd) & SUNXI_HDMI_HPD_DETECT) ? 1 : 0;
57}
58
59static void sunxi_hdmi_shutdown(void)
60{
61 struct sunxi_ccm_reg * const ccm =
62 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
63 struct sunxi_hdmi_reg * const hdmi =
64 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
Luc Verhaegenb01df1e2014-08-13 07:55:06 +020065
Luc Verhaegenb01df1e2014-08-13 07:55:06 +020066 clrbits_le32(&hdmi->ctrl, SUNXI_HDMI_CTRL_ENABLE);
67 clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
68 clrbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
Hans de Goedef651e0a2014-11-14 17:42:14 +010069#ifdef CONFIG_MACH_SUN6I
70 clrbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
71#endif
Luc Verhaegenb01df1e2014-08-13 07:55:06 +020072 clock_set_pll3(0);
Luc Verhaegenb01df1e2014-08-13 07:55:06 +020073}
74
75/*
76 * This is the entity that mixes and matches the different layers and inputs.
77 * Allwinner calls it the back-end, but i like composer better.
78 */
79static void sunxi_composer_init(void)
80{
81 struct sunxi_ccm_reg * const ccm =
82 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
83 struct sunxi_de_be_reg * const de_be =
84 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
85 int i;
86
Hans de Goedef651e0a2014-11-14 17:42:14 +010087#ifdef CONFIG_MACH_SUN6I
88 /* Reset off */
89 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DE_BE0);
90#endif
91
Luc Verhaegenb01df1e2014-08-13 07:55:06 +020092 /* Clocks on */
93 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE_BE0);
94 setbits_le32(&ccm->dram_clk_gate, 1 << CCM_DRAM_GATE_OFFSET_DE_BE0);
95 clock_set_de_mod_clock(&ccm->be0_clk_cfg, 300000000);
96
97 /* Engine bug, clear registers after reset */
98 for (i = 0x0800; i < 0x1000; i += 4)
99 writel(0, SUNXI_DE_BE0_BASE + i);
100
101 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_ENABLE);
102}
103
Hans de Goedeccb0ed52014-12-19 13:46:33 +0100104static void sunxi_composer_mode_set(const struct ctfb_res_modes *mode,
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200105 unsigned int address)
106{
107 struct sunxi_de_be_reg * const de_be =
108 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
109
110 writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
111 &de_be->disp_size);
112 writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
113 &de_be->layer0_size);
114 writel(SUNXI_DE_BE_LAYER_STRIDE(mode->xres), &de_be->layer0_stride);
115 writel(address << 3, &de_be->layer0_addr_low32b);
116 writel(address >> 29, &de_be->layer0_addr_high4b);
117 writel(SUNXI_DE_BE_LAYER_ATTR1_FMT_XRGB8888, &de_be->layer0_attr1_ctrl);
118
119 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_LAYER0_ENABLE);
120}
121
122/*
123 * LCDC, what allwinner calls a CRTC, so timing controller and serializer.
124 */
125static void sunxi_lcdc_pll_set(int dotclock, int *clk_div, int *clk_double)
126{
127 struct sunxi_ccm_reg * const ccm =
128 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
129 int value, n, m, diff;
130 int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
131 int best_double = 0;
132
133 /*
134 * Find the lowest divider resulting in a matching clock, if there
135 * is no match, pick the closest lower clock, as monitors tend to
136 * not sync to higher frequencies.
137 */
138 for (m = 15; m > 0; m--) {
139 n = (m * dotclock) / 3000;
140
141 if ((n >= 9) && (n <= 127)) {
142 value = (3000 * n) / m;
143 diff = dotclock - value;
144 if (diff < best_diff) {
145 best_diff = diff;
146 best_m = m;
147 best_n = n;
148 best_double = 0;
149 }
150 }
151
152 /* These are just duplicates */
153 if (!(m & 1))
154 continue;
155
156 n = (m * dotclock) / 6000;
157 if ((n >= 9) && (n <= 127)) {
158 value = (6000 * n) / m;
159 diff = dotclock - value;
160 if (diff < best_diff) {
161 best_diff = diff;
162 best_m = m;
163 best_n = n;
164 best_double = 1;
165 }
166 }
167 }
168
169 debug("dotclock: %dkHz = %dkHz: (%d * 3MHz * %d) / %d\n",
170 dotclock, (best_double + 1) * 3000 * best_n / best_m,
171 best_double + 1, best_n, best_m);
172
173 clock_set_pll3(best_n * 3000000);
174
175 writel(CCM_LCD_CH1_CTRL_GATE |
176 (best_double ? CCM_LCD_CH1_CTRL_PLL3_2X : CCM_LCD_CH1_CTRL_PLL3) |
177 CCM_LCD_CH1_CTRL_M(best_m), &ccm->lcd0_ch1_clk_cfg);
178
179 *clk_div = best_m;
180 *clk_double = best_double;
181}
182
183static void sunxi_lcdc_init(void)
184{
185 struct sunxi_ccm_reg * const ccm =
186 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
187 struct sunxi_lcdc_reg * const lcdc =
188 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
189
190 /* Reset off */
Hans de Goedef651e0a2014-11-14 17:42:14 +0100191#ifdef CONFIG_MACH_SUN6I
192 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
193#else
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200194 setbits_le32(&ccm->lcd0_ch0_clk_cfg, CCM_LCD_CH0_CTRL_RST);
Hans de Goedef651e0a2014-11-14 17:42:14 +0100195#endif
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200196
197 /* Clock on */
198 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
199
200 /* Init lcdc */
201 writel(0, &lcdc->ctrl); /* Disable tcon */
202 writel(0, &lcdc->int0); /* Disable all interrupts */
203
204 /* Disable tcon0 dot clock */
205 clrbits_le32(&lcdc->tcon0_dclk, SUNXI_LCDC_TCON0_DCLK_ENABLE);
206
207 /* Set all io lines to tristate */
208 writel(0xffffffff, &lcdc->tcon0_io_tristate);
209 writel(0xffffffff, &lcdc->tcon1_io_tristate);
210}
211
Hans de Goedeccb0ed52014-12-19 13:46:33 +0100212static void sunxi_lcdc_mode_set(const struct ctfb_res_modes *mode,
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200213 int *clk_div, int *clk_double)
214{
215 struct sunxi_lcdc_reg * const lcdc =
216 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
217 int bp, total;
218
219 /* Use tcon1 */
220 clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
221 SUNXI_LCDC_CTRL_IO_MAP_TCON1);
222
223 /* Enabled, 0x1e start delay */
224 writel(SUNXI_LCDC_TCON1_CTRL_ENABLE |
225 SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(0x1e), &lcdc->tcon1_ctrl);
226
227 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
228 &lcdc->tcon1_timing_source);
229 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
230 &lcdc->tcon1_timing_scale);
231 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
232 &lcdc->tcon1_timing_out);
233
234 bp = mode->hsync_len + mode->left_margin;
235 total = mode->xres + mode->right_margin + bp;
236 writel(SUNXI_LCDC_TCON1_TIMING_H_TOTAL(total) |
237 SUNXI_LCDC_TCON1_TIMING_H_BP(bp), &lcdc->tcon1_timing_h);
238
239 bp = mode->vsync_len + mode->upper_margin;
240 total = mode->yres + mode->lower_margin + bp;
241 writel(SUNXI_LCDC_TCON1_TIMING_V_TOTAL(total) |
242 SUNXI_LCDC_TCON1_TIMING_V_BP(bp), &lcdc->tcon1_timing_v);
243
244 writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
245 &lcdc->tcon1_timing_sync);
246
Hans de Goedeccb0ed52014-12-19 13:46:33 +0100247 sunxi_lcdc_pll_set(mode->pixclock_khz, clk_div, clk_double);
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200248}
Hans de Goedef651e0a2014-11-14 17:42:14 +0100249
250#ifdef CONFIG_MACH_SUN6I
251static void sunxi_drc_init(void)
252{
253 struct sunxi_ccm_reg * const ccm =
254 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
255
256 /* On sun6i the drc must be clocked even when in pass-through mode */
257 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DRC0);
258 clock_set_de_mod_clock(&ccm->iep_drc0_clk_cfg, 300000000);
259}
260#endif
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200261
Hans de Goedeccb0ed52014-12-19 13:46:33 +0100262static void sunxi_hdmi_mode_set(const struct ctfb_res_modes *mode,
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200263 int clk_div, int clk_double)
264{
265 struct sunxi_hdmi_reg * const hdmi =
266 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
267 int x, y;
268
269 /* Write clear interrupt status bits */
270 writel(SUNXI_HDMI_IRQ_STATUS_BITS, &hdmi->irq);
271
272 /* Init various registers, select pll3 as clock source */
273 writel(SUNXI_HDMI_VIDEO_POL_TX_CLK, &hdmi->video_polarity);
274 writel(SUNXI_HDMI_PAD_CTRL0_RUN, &hdmi->pad_ctrl0);
275 writel(SUNXI_HDMI_PAD_CTRL1, &hdmi->pad_ctrl1);
276 writel(SUNXI_HDMI_PLL_CTRL, &hdmi->pll_ctrl);
277 writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
278
279 /* Setup clk div and doubler */
280 clrsetbits_le32(&hdmi->pll_ctrl, SUNXI_HDMI_PLL_CTRL_DIV_MASK,
281 SUNXI_HDMI_PLL_CTRL_DIV(clk_div));
282 if (!clk_double)
283 setbits_le32(&hdmi->pad_ctrl1, SUNXI_HDMI_PAD_CTRL1_HALVE);
284
285 /* Setup timing registers */
286 writel(SUNXI_HDMI_Y(mode->yres) | SUNXI_HDMI_X(mode->xres),
287 &hdmi->video_size);
288
289 x = mode->hsync_len + mode->left_margin;
290 y = mode->vsync_len + mode->upper_margin;
291 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_bp);
292
293 x = mode->right_margin;
294 y = mode->lower_margin;
295 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_fp);
296
297 x = mode->hsync_len;
298 y = mode->vsync_len;
299 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_spw);
300
301 if (mode->sync & FB_SYNC_HOR_HIGH_ACT)
302 setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_HOR);
303
304 if (mode->sync & FB_SYNC_VERT_HIGH_ACT)
305 setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_VER);
306}
307
308static void sunxi_engines_init(void)
309{
310 sunxi_composer_init();
311 sunxi_lcdc_init();
Hans de Goedef651e0a2014-11-14 17:42:14 +0100312#ifdef CONFIG_MACH_SUN6I
313 sunxi_drc_init();
314#endif
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200315}
316
Hans de Goedeccb0ed52014-12-19 13:46:33 +0100317static void sunxi_mode_set(const struct ctfb_res_modes *mode, unsigned int address)
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200318{
319 struct sunxi_de_be_reg * const de_be =
320 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
321 struct sunxi_lcdc_reg * const lcdc =
322 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
323 struct sunxi_hdmi_reg * const hdmi =
324 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
325 int clk_div, clk_double;
326 int retries = 3;
327
328retry:
329 clrbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
330 clrbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
331 clrbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
332
333 sunxi_composer_mode_set(mode, address);
334 sunxi_lcdc_mode_set(mode, &clk_div, &clk_double);
335 sunxi_hdmi_mode_set(mode, clk_div, clk_double);
336
337 setbits_le32(&de_be->reg_ctrl, SUNXI_DE_BE_REG_CTRL_LOAD_REGS);
338 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
339
340 udelay(1000000 / mode->refresh + 500);
341
342 setbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
343
344 udelay(1000000 / mode->refresh + 500);
345
346 setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
347
348 udelay(1000000 / mode->refresh + 500);
349
350 /*
351 * Sometimes the display pipeline does not sync up properly, if
352 * this happens the hdmi fifo underrun or overrun bits are set.
353 */
354 if (readl(&hdmi->irq) &
355 (SUNXI_HDMI_IRQ_STATUS_FIFO_UF | SUNXI_HDMI_IRQ_STATUS_FIFO_OF)) {
356 if (retries--)
357 goto retry;
358 printf("HDMI fifo under or overrun\n");
359 }
360}
361
362void *video_hw_init(void)
363{
364 static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
Hans de Goede3f21d2a2014-12-19 14:03:40 +0100365 const struct ctfb_res_modes *mode;
366 const char *options;
367 unsigned int depth;
Hans de Goede695bda42014-12-19 15:13:57 +0100368 int ret, hpd;
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200369
370 memset(&sunxi_display, 0, sizeof(struct sunxi_display));
371
372 printf("Reserved %dkB of RAM for Framebuffer.\n",
373 CONFIG_SUNXI_FB_SIZE >> 10);
374 gd->fb_base = gd->ram_top;
375
Hans de Goede3f21d2a2014-12-19 14:03:40 +0100376 video_get_ctfb_res_modes(RES_MODE_1024x768, 24, &mode, &depth, &options);
Hans de Goede695bda42014-12-19 15:13:57 +0100377 hpd = video_get_option_int(options, "hpd", 1);
Hans de Goede3f21d2a2014-12-19 14:03:40 +0100378
Hans de Goede695bda42014-12-19 15:13:57 +0100379 /* Always call hdp_detect, as it also enables various clocks, etc. */
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200380 ret = sunxi_hdmi_hpd_detect();
Hans de Goede695bda42014-12-19 15:13:57 +0100381 if (hpd && !ret) {
382 sunxi_hdmi_shutdown();
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200383 return NULL;
Hans de Goede695bda42014-12-19 15:13:57 +0100384 }
385 if (ret)
386 printf("HDMI connected: ");
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200387
Hans de Goede3f21d2a2014-12-19 14:03:40 +0100388 if (mode->vmode != FB_VMODE_NONINTERLACED) {
389 printf("Only non-interlaced modes supported, falling back to 1024x768\n");
390 mode = &res_mode_init[RES_MODE_1024x768];
391 } else {
392 printf("Setting up a %dx%d console\n", mode->xres, mode->yres);
393 }
394
395 sunxi_display.enabled = true;
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200396 sunxi_engines_init();
Hans de Goedeccb0ed52014-12-19 13:46:33 +0100397 sunxi_mode_set(mode, gd->fb_base - CONFIG_SYS_SDRAM_BASE);
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200398
399 /*
400 * These are the only members of this structure that are used. All the
401 * others are driver specific. There is nothing to decribe pitch or
402 * stride, but we are lucky with our hw.
403 */
404 graphic_device->frameAdrs = gd->fb_base;
405 graphic_device->gdfIndex = GDF_32BIT_X888RGB;
406 graphic_device->gdfBytesPP = 4;
Hans de Goedeccb0ed52014-12-19 13:46:33 +0100407 graphic_device->winSizeX = mode->xres;
408 graphic_device->winSizeY = mode->yres;
Luc Verhaegenb01df1e2014-08-13 07:55:06 +0200409
410 return graphic_device;
411}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200412
413/*
414 * Simplefb support.
415 */
416#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_VIDEO_DT_SIMPLEFB)
417int sunxi_simplefb_setup(void *blob)
418{
419 static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
420 int offset, ret;
421
422 if (!sunxi_display.enabled)
423 return 0;
424
425 /* Find a framebuffer node, with pipeline == "de_be0-lcd0-hdmi" */
426 offset = fdt_node_offset_by_compatible(blob, -1,
427 "allwinner,simple-framebuffer");
428 while (offset >= 0) {
429 ret = fdt_find_string(blob, offset, "allwinner,pipeline",
430 "de_be0-lcd0-hdmi");
431 if (ret == 0)
432 break;
433 offset = fdt_node_offset_by_compatible(blob, offset,
434 "allwinner,simple-framebuffer");
435 }
436 if (offset < 0) {
437 eprintf("Cannot setup simplefb: node not found\n");
438 return 0; /* Keep older kernels working */
439 }
440
441 ret = fdt_setup_simplefb_node(blob, offset, gd->fb_base,
442 graphic_device->winSizeX, graphic_device->winSizeY,
443 graphic_device->winSizeX * graphic_device->gdfBytesPP,
444 "x8r8g8b8");
445 if (ret)
446 eprintf("Cannot setup simplefb: Error setting properties\n");
447
448 return ret;
449}
450#endif /* CONFIG_OF_BOARD_SETUP && CONFIG_VIDEO_DT_SIMPLEFB */