blob: ad38f162fde4853915dc25e335bba8aea4f33ed7 [file] [log] [blame]
Luc Verhaegen7f2c5212014-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>
Hans de Goede75481602014-12-19 16:05:12 +010016#include <errno.h>
Luc Verhaegen2d7a0842014-08-13 07:55:07 +020017#include <fdtdec.h>
18#include <fdt_support.h>
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020019#include <video_fb.h>
Hans de Goedebe8ec632014-12-19 13:46:33 +010020#include "videomodes.h"
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020021
22DECLARE_GLOBAL_DATA_PTR;
23
Hans de Goede1c092202014-12-21 14:37:45 +010024enum sunxi_monitor {
25 sunxi_monitor_none,
26 sunxi_monitor_dvi,
27 sunxi_monitor_hdmi,
28 sunxi_monitor_lcd,
29 sunxi_monitor_vga,
30};
31#define SUNXI_MONITOR_LAST sunxi_monitor_vga
32
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020033struct sunxi_display {
34 GraphicDevice graphic_device;
35 bool enabled;
Hans de Goede1c092202014-12-21 14:37:45 +010036 enum sunxi_monitor monitor;
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020037} sunxi_display;
38
Hans de Goede75481602014-12-19 16:05:12 +010039/*
40 * Wait up to 200ms for value to be set in given part of reg.
41 */
42static int await_completion(u32 *reg, u32 mask, u32 val)
43{
44 unsigned long tmo = timer_get_us() + 200000;
45
46 while ((readl(reg) & mask) != val) {
47 if (timer_get_us() > tmo) {
48 printf("DDC: timeout reading EDID\n");
49 return -ETIME;
50 }
51 }
52 return 0;
53}
54
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020055static int sunxi_hdmi_hpd_detect(void)
56{
57 struct sunxi_ccm_reg * const ccm =
58 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
59 struct sunxi_hdmi_reg * const hdmi =
60 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
Hans de Goede40f1b872014-12-20 15:15:23 +010061 unsigned long tmo = timer_get_us() + 300000;
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020062
63 /* Set pll3 to 300MHz */
64 clock_set_pll3(300000000);
65
66 /* Set hdmi parent to pll3 */
67 clrsetbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_PLL_MASK,
68 CCM_HDMI_CTRL_PLL3);
69
70 /* Set ahb gating to pass */
Hans de Goede211717a2014-11-14 17:42:14 +010071#ifdef CONFIG_MACH_SUN6I
72 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
73#endif
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020074 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
75
76 /* Clock on */
77 setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
78
79 writel(SUNXI_HDMI_CTRL_ENABLE, &hdmi->ctrl);
80 writel(SUNXI_HDMI_PAD_CTRL0_HDP, &hdmi->pad_ctrl0);
81
Hans de Goede40f1b872014-12-20 15:15:23 +010082 while (timer_get_us() < tmo) {
83 if (readl(&hdmi->hpd) & SUNXI_HDMI_HPD_DETECT)
84 return 1;
85 }
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020086
Hans de Goede40f1b872014-12-20 15:15:23 +010087 return 0;
Hans de Goede518cef22014-12-19 15:13:57 +010088}
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020089
Hans de Goede518cef22014-12-19 15:13:57 +010090static void sunxi_hdmi_shutdown(void)
91{
92 struct sunxi_ccm_reg * const ccm =
93 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
94 struct sunxi_hdmi_reg * const hdmi =
95 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
96
Luc Verhaegen7f2c5212014-08-13 07:55:06 +020097 clrbits_le32(&hdmi->ctrl, SUNXI_HDMI_CTRL_ENABLE);
98 clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
99 clrbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
Hans de Goede211717a2014-11-14 17:42:14 +0100100#ifdef CONFIG_MACH_SUN6I
101 clrbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
102#endif
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200103 clock_set_pll3(0);
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200104}
105
Hans de Goede75481602014-12-19 16:05:12 +0100106static int sunxi_hdmi_ddc_do_command(u32 cmnd, int offset, int n)
107{
108 struct sunxi_hdmi_reg * const hdmi =
109 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
110
111 setbits_le32(&hdmi->ddc_fifo_ctrl, SUNXI_HDMI_DDC_FIFO_CTRL_CLEAR);
112 writel(SUNXI_HMDI_DDC_ADDR_EDDC_SEGMENT(offset >> 8) |
113 SUNXI_HMDI_DDC_ADDR_EDDC_ADDR |
114 SUNXI_HMDI_DDC_ADDR_OFFSET(offset) |
115 SUNXI_HMDI_DDC_ADDR_SLAVE_ADDR, &hdmi->ddc_addr);
116#ifndef CONFIG_MACH_SUN6I
117 writel(n, &hdmi->ddc_byte_count);
118 writel(cmnd, &hdmi->ddc_cmnd);
119#else
120 writel(n << 16 | cmnd, &hdmi->ddc_cmnd);
121#endif
122 setbits_le32(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_START);
123
124 return await_completion(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_START, 0);
125}
126
127static int sunxi_hdmi_ddc_read(int offset, u8 *buf, int count)
128{
129 struct sunxi_hdmi_reg * const hdmi =
130 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
131 int i, n;
132
133 while (count > 0) {
134 if (count > 16)
135 n = 16;
136 else
137 n = count;
138
139 if (sunxi_hdmi_ddc_do_command(
140 SUNXI_HDMI_DDC_CMND_EXPLICIT_EDDC_READ,
141 offset, n))
142 return -ETIME;
143
144 for (i = 0; i < n; i++)
145 *buf++ = readb(&hdmi->ddc_fifo_data);
146
147 offset += n;
148 count -= n;
149 }
150
151 return 0;
152}
153
Hans de Goede63c5fbd2014-12-20 14:01:48 +0100154static int sunxi_hdmi_edid_get_block(int block, u8 *buf)
155{
156 int r, retries = 2;
157
158 do {
159 r = sunxi_hdmi_ddc_read(block * 128, buf, 128);
160 if (r)
161 continue;
162 r = edid_check_checksum(buf);
163 if (r) {
164 printf("EDID block %d: checksum error%s\n",
165 block, retries ? ", retrying" : "");
166 }
167 } while (r && retries--);
168
169 return r;
170}
171
Hans de Goede1c092202014-12-21 14:37:45 +0100172static int sunxi_hdmi_edid_get_mode(struct ctfb_res_modes *mode)
Hans de Goede75481602014-12-19 16:05:12 +0100173{
174 struct edid1_info edid1;
Hans de Goedef3000682014-12-20 14:31:45 +0100175 struct edid_cea861_info cea681[4];
Hans de Goede75481602014-12-19 16:05:12 +0100176 struct edid_detailed_timing *t =
177 (struct edid_detailed_timing *)edid1.monitor_details.timing;
178 struct sunxi_hdmi_reg * const hdmi =
179 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
180 struct sunxi_ccm_reg * const ccm =
181 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedef3000682014-12-20 14:31:45 +0100182 int i, r, ext_blocks = 0;
Hans de Goede75481602014-12-19 16:05:12 +0100183
184 /* SUNXI_HDMI_CTRL_ENABLE & PAD_CTRL0 are already set by hpd_detect */
185 writel(SUNXI_HDMI_PAD_CTRL1 | SUNXI_HDMI_PAD_CTRL1_HALVE,
186 &hdmi->pad_ctrl1);
187 writel(SUNXI_HDMI_PLL_CTRL | SUNXI_HDMI_PLL_CTRL_DIV(15),
188 &hdmi->pll_ctrl);
189 writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
190
191 /* Reset i2c controller */
192 setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_DDC_GATE);
193 writel(SUNXI_HMDI_DDC_CTRL_ENABLE |
194 SUNXI_HMDI_DDC_CTRL_SDA_ENABLE |
195 SUNXI_HMDI_DDC_CTRL_SCL_ENABLE |
196 SUNXI_HMDI_DDC_CTRL_RESET, &hdmi->ddc_ctrl);
197 if (await_completion(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_RESET, 0))
198 return -EIO;
199
200 writel(SUNXI_HDMI_DDC_CLOCK, &hdmi->ddc_clock);
201#ifndef CONFIG_MACH_SUN6I
202 writel(SUNXI_HMDI_DDC_LINE_CTRL_SDA_ENABLE |
203 SUNXI_HMDI_DDC_LINE_CTRL_SCL_ENABLE, &hdmi->ddc_line_ctrl);
204#endif
205
Hans de Goede63c5fbd2014-12-20 14:01:48 +0100206 r = sunxi_hdmi_edid_get_block(0, (u8 *)&edid1);
Hans de Goedef3000682014-12-20 14:31:45 +0100207 if (r == 0) {
208 r = edid_check_info(&edid1);
209 if (r) {
210 printf("EDID: invalid EDID data\n");
211 r = -EINVAL;
212 }
213 }
214 if (r == 0) {
215 ext_blocks = edid1.extension_flag;
216 if (ext_blocks > 4)
217 ext_blocks = 4;
218 for (i = 0; i < ext_blocks; i++) {
219 if (sunxi_hdmi_edid_get_block(1 + i,
220 (u8 *)&cea681[i]) != 0) {
221 ext_blocks = i;
222 break;
223 }
224 }
225 }
Hans de Goede75481602014-12-19 16:05:12 +0100226
227 /* Disable DDC engine, no longer needed */
228 clrbits_le32(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_ENABLE);
229 clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_DDC_GATE);
230
231 if (r)
232 return r;
233
Hans de Goede75481602014-12-19 16:05:12 +0100234 /* We want version 1.3 or 1.2 with detailed timing info */
235 if (edid1.version != 1 || (edid1.revision < 3 &&
236 !EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(edid1))) {
237 printf("EDID: unsupported version %d.%d\n",
238 edid1.version, edid1.revision);
239 return -EINVAL;
240 }
241
242 /* Take the first usable detailed timing */
243 for (i = 0; i < 4; i++, t++) {
244 r = video_edid_dtd_to_ctfb_res_modes(t, mode);
245 if (r == 0)
246 break;
247 }
248 if (i == 4) {
249 printf("EDID: no usable detailed timing found\n");
250 return -ENOENT;
251 }
252
Hans de Goedef3000682014-12-20 14:31:45 +0100253 /* Check for basic audio support, if found enable hdmi output */
Hans de Goede1c092202014-12-21 14:37:45 +0100254 sunxi_display.monitor = sunxi_monitor_dvi;
Hans de Goedef3000682014-12-20 14:31:45 +0100255 for (i = 0; i < ext_blocks; i++) {
256 if (cea681[i].extension_tag != EDID_CEA861_EXTENSION_TAG ||
257 cea681[i].revision < 2)
258 continue;
259
260 if (EDID_CEA861_SUPPORTS_BASIC_AUDIO(cea681[i]))
Hans de Goede1c092202014-12-21 14:37:45 +0100261 sunxi_display.monitor = sunxi_monitor_hdmi;
Hans de Goedef3000682014-12-20 14:31:45 +0100262 }
263
Hans de Goede75481602014-12-19 16:05:12 +0100264 return 0;
265}
266
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200267/*
268 * This is the entity that mixes and matches the different layers and inputs.
269 * Allwinner calls it the back-end, but i like composer better.
270 */
271static void sunxi_composer_init(void)
272{
273 struct sunxi_ccm_reg * const ccm =
274 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
275 struct sunxi_de_be_reg * const de_be =
276 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
277 int i;
278
Hans de Goede211717a2014-11-14 17:42:14 +0100279#ifdef CONFIG_MACH_SUN6I
280 /* Reset off */
281 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DE_BE0);
282#endif
283
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200284 /* Clocks on */
285 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE_BE0);
286 setbits_le32(&ccm->dram_clk_gate, 1 << CCM_DRAM_GATE_OFFSET_DE_BE0);
287 clock_set_de_mod_clock(&ccm->be0_clk_cfg, 300000000);
288
289 /* Engine bug, clear registers after reset */
290 for (i = 0x0800; i < 0x1000; i += 4)
291 writel(0, SUNXI_DE_BE0_BASE + i);
292
293 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_ENABLE);
294}
295
Hans de Goedebe8ec632014-12-19 13:46:33 +0100296static void sunxi_composer_mode_set(const struct ctfb_res_modes *mode,
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200297 unsigned int address)
298{
299 struct sunxi_de_be_reg * const de_be =
300 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
301
302 writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
303 &de_be->disp_size);
304 writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
305 &de_be->layer0_size);
306 writel(SUNXI_DE_BE_LAYER_STRIDE(mode->xres), &de_be->layer0_stride);
307 writel(address << 3, &de_be->layer0_addr_low32b);
308 writel(address >> 29, &de_be->layer0_addr_high4b);
309 writel(SUNXI_DE_BE_LAYER_ATTR1_FMT_XRGB8888, &de_be->layer0_attr1_ctrl);
310
311 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_LAYER0_ENABLE);
312}
313
Hans de Goede0e045212014-12-21 14:49:34 +0100314static void sunxi_composer_enable(void)
315{
316 struct sunxi_de_be_reg * const de_be =
317 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
318
319 setbits_le32(&de_be->reg_ctrl, SUNXI_DE_BE_REG_CTRL_LOAD_REGS);
320 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
321}
322
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200323/*
324 * LCDC, what allwinner calls a CRTC, so timing controller and serializer.
325 */
Hans de Goede5489ebc2014-12-21 16:27:45 +0100326static void sunxi_lcdc_pll_set(int tcon, int dotclock,
327 int *clk_div, int *clk_double)
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200328{
329 struct sunxi_ccm_reg * const ccm =
330 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede5489ebc2014-12-21 16:27:45 +0100331 int value, n, m, min_m, max_m, diff;
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200332 int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
333 int best_double = 0;
334
Hans de Goede5489ebc2014-12-21 16:27:45 +0100335 if (tcon == 0) {
336 min_m = 6;
337 max_m = 127;
338 } else {
339 min_m = 1;
340 max_m = 15;
341 }
342
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200343 /*
344 * Find the lowest divider resulting in a matching clock, if there
345 * is no match, pick the closest lower clock, as monitors tend to
346 * not sync to higher frequencies.
347 */
Hans de Goede5489ebc2014-12-21 16:27:45 +0100348 for (m = min_m; m <= max_m; m++) {
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200349 n = (m * dotclock) / 3000;
350
351 if ((n >= 9) && (n <= 127)) {
352 value = (3000 * n) / m;
353 diff = dotclock - value;
354 if (diff < best_diff) {
355 best_diff = diff;
356 best_m = m;
357 best_n = n;
358 best_double = 0;
359 }
360 }
361
362 /* These are just duplicates */
363 if (!(m & 1))
364 continue;
365
366 n = (m * dotclock) / 6000;
367 if ((n >= 9) && (n <= 127)) {
368 value = (6000 * n) / m;
369 diff = dotclock - value;
370 if (diff < best_diff) {
371 best_diff = diff;
372 best_m = m;
373 best_n = n;
374 best_double = 1;
375 }
376 }
377 }
378
379 debug("dotclock: %dkHz = %dkHz: (%d * 3MHz * %d) / %d\n",
380 dotclock, (best_double + 1) * 3000 * best_n / best_m,
381 best_double + 1, best_n, best_m);
382
383 clock_set_pll3(best_n * 3000000);
384
Hans de Goede5489ebc2014-12-21 16:27:45 +0100385 if (tcon == 0) {
386 writel(CCM_LCD_CH0_CTRL_GATE | CCM_LCD_CH0_CTRL_RST |
387 (best_double ? CCM_LCD_CH0_CTRL_PLL3_2X :
388 CCM_LCD_CH0_CTRL_PLL3),
389 &ccm->lcd0_ch0_clk_cfg);
390 } else {
391 writel(CCM_LCD_CH1_CTRL_GATE |
392 (best_double ? CCM_LCD_CH1_CTRL_PLL3_2X :
393 CCM_LCD_CH1_CTRL_PLL3) |
394 CCM_LCD_CH1_CTRL_M(best_m), &ccm->lcd0_ch1_clk_cfg);
395 }
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200396
397 *clk_div = best_m;
398 *clk_double = best_double;
399}
400
401static void sunxi_lcdc_init(void)
402{
403 struct sunxi_ccm_reg * const ccm =
404 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
405 struct sunxi_lcdc_reg * const lcdc =
406 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
407
408 /* Reset off */
Hans de Goede211717a2014-11-14 17:42:14 +0100409#ifdef CONFIG_MACH_SUN6I
410 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
411#else
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200412 setbits_le32(&ccm->lcd0_ch0_clk_cfg, CCM_LCD_CH0_CTRL_RST);
Hans de Goede211717a2014-11-14 17:42:14 +0100413#endif
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200414
415 /* Clock on */
416 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
417
418 /* Init lcdc */
419 writel(0, &lcdc->ctrl); /* Disable tcon */
420 writel(0, &lcdc->int0); /* Disable all interrupts */
421
422 /* Disable tcon0 dot clock */
423 clrbits_le32(&lcdc->tcon0_dclk, SUNXI_LCDC_TCON0_DCLK_ENABLE);
424
425 /* Set all io lines to tristate */
426 writel(0xffffffff, &lcdc->tcon0_io_tristate);
427 writel(0xffffffff, &lcdc->tcon1_io_tristate);
428}
429
Hans de Goede0e045212014-12-21 14:49:34 +0100430static void sunxi_lcdc_enable(void)
431{
432 struct sunxi_lcdc_reg * const lcdc =
433 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
434
435 setbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
436}
437
438static void sunxi_lcdc_tcon1_mode_set(const struct ctfb_res_modes *mode,
439 int *clk_div, int *clk_double)
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200440{
441 struct sunxi_lcdc_reg * const lcdc =
442 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
443 int bp, total;
444
445 /* Use tcon1 */
446 clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
447 SUNXI_LCDC_CTRL_IO_MAP_TCON1);
448
449 /* Enabled, 0x1e start delay */
450 writel(SUNXI_LCDC_TCON1_CTRL_ENABLE |
451 SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(0x1e), &lcdc->tcon1_ctrl);
452
453 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
454 &lcdc->tcon1_timing_source);
455 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
456 &lcdc->tcon1_timing_scale);
457 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
458 &lcdc->tcon1_timing_out);
459
460 bp = mode->hsync_len + mode->left_margin;
461 total = mode->xres + mode->right_margin + bp;
462 writel(SUNXI_LCDC_TCON1_TIMING_H_TOTAL(total) |
463 SUNXI_LCDC_TCON1_TIMING_H_BP(bp), &lcdc->tcon1_timing_h);
464
465 bp = mode->vsync_len + mode->upper_margin;
466 total = mode->yres + mode->lower_margin + bp;
467 writel(SUNXI_LCDC_TCON1_TIMING_V_TOTAL(total) |
468 SUNXI_LCDC_TCON1_TIMING_V_BP(bp), &lcdc->tcon1_timing_v);
469
470 writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
471 &lcdc->tcon1_timing_sync);
472
Hans de Goede5489ebc2014-12-21 16:27:45 +0100473 sunxi_lcdc_pll_set(1, mode->pixclock_khz, clk_div, clk_double);
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200474}
475
Hans de Goede5ee0bea2014-12-20 13:38:06 +0100476static void sunxi_hdmi_setup_info_frames(const struct ctfb_res_modes *mode)
477{
478 struct sunxi_hdmi_reg * const hdmi =
479 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
480 u8 checksum = 0;
481 u8 avi_info_frame[17] = {
482 0x82, 0x02, 0x0d, 0x00, 0x12, 0x00, 0x88, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
484 0x00
485 };
486 u8 vendor_info_frame[19] = {
487 0x81, 0x01, 0x06, 0x29, 0x03, 0x0c, 0x00, 0x40,
488 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
489 0x00, 0x00, 0x00
490 };
491 int i;
492
493 if (mode->pixclock_khz <= 27000)
494 avi_info_frame[5] = 0x40; /* SD-modes, ITU601 colorspace */
495 else
496 avi_info_frame[5] = 0x80; /* HD-modes, ITU709 colorspace */
497
498 if (mode->xres * 100 / mode->yres < 156)
499 avi_info_frame[5] |= 0x18; /* 4 : 3 */
500 else
501 avi_info_frame[5] |= 0x28; /* 16 : 9 */
502
503 for (i = 0; i < ARRAY_SIZE(avi_info_frame); i++)
504 checksum += avi_info_frame[i];
505
506 avi_info_frame[3] = 0x100 - checksum;
507
508 for (i = 0; i < ARRAY_SIZE(avi_info_frame); i++)
509 writeb(avi_info_frame[i], &hdmi->avi_info_frame[i]);
510
511 writel(SUNXI_HDMI_QCP_PACKET0, &hdmi->qcp_packet0);
512 writel(SUNXI_HDMI_QCP_PACKET1, &hdmi->qcp_packet1);
513
514 for (i = 0; i < ARRAY_SIZE(vendor_info_frame); i++)
515 writeb(vendor_info_frame[i], &hdmi->vendor_info_frame[i]);
516
517 writel(SUNXI_HDMI_PKT_CTRL0, &hdmi->pkt_ctrl0);
518 writel(SUNXI_HDMI_PKT_CTRL1, &hdmi->pkt_ctrl1);
519
520 setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_HDMI);
521}
522
Hans de Goedebe8ec632014-12-19 13:46:33 +0100523static void sunxi_hdmi_mode_set(const struct ctfb_res_modes *mode,
Hans de Goede1c092202014-12-21 14:37:45 +0100524 int clk_div, int clk_double)
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200525{
526 struct sunxi_hdmi_reg * const hdmi =
527 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
528 int x, y;
529
530 /* Write clear interrupt status bits */
531 writel(SUNXI_HDMI_IRQ_STATUS_BITS, &hdmi->irq);
532
Hans de Goede1c092202014-12-21 14:37:45 +0100533 if (sunxi_display.monitor == sunxi_monitor_hdmi)
Hans de Goede5ee0bea2014-12-20 13:38:06 +0100534 sunxi_hdmi_setup_info_frames(mode);
535
Hans de Goede876aaaf2014-12-20 13:51:16 +0100536 /* Set input sync enable */
537 writel(SUNXI_HDMI_UNKNOWN_INPUT_SYNC, &hdmi->unknown);
538
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200539 /* Init various registers, select pll3 as clock source */
540 writel(SUNXI_HDMI_VIDEO_POL_TX_CLK, &hdmi->video_polarity);
541 writel(SUNXI_HDMI_PAD_CTRL0_RUN, &hdmi->pad_ctrl0);
542 writel(SUNXI_HDMI_PAD_CTRL1, &hdmi->pad_ctrl1);
543 writel(SUNXI_HDMI_PLL_CTRL, &hdmi->pll_ctrl);
544 writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
545
546 /* Setup clk div and doubler */
547 clrsetbits_le32(&hdmi->pll_ctrl, SUNXI_HDMI_PLL_CTRL_DIV_MASK,
548 SUNXI_HDMI_PLL_CTRL_DIV(clk_div));
549 if (!clk_double)
550 setbits_le32(&hdmi->pad_ctrl1, SUNXI_HDMI_PAD_CTRL1_HALVE);
551
552 /* Setup timing registers */
553 writel(SUNXI_HDMI_Y(mode->yres) | SUNXI_HDMI_X(mode->xres),
554 &hdmi->video_size);
555
556 x = mode->hsync_len + mode->left_margin;
557 y = mode->vsync_len + mode->upper_margin;
558 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_bp);
559
560 x = mode->right_margin;
561 y = mode->lower_margin;
562 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_fp);
563
564 x = mode->hsync_len;
565 y = mode->vsync_len;
566 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_spw);
567
568 if (mode->sync & FB_SYNC_HOR_HIGH_ACT)
569 setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_HOR);
570
571 if (mode->sync & FB_SYNC_VERT_HIGH_ACT)
572 setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_VER);
573}
574
Hans de Goede0e045212014-12-21 14:49:34 +0100575static void sunxi_hdmi_enable(void)
576{
577 struct sunxi_hdmi_reg * const hdmi =
578 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
579
580 udelay(100);
581 setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
582}
583
Hans de Goedee8400792014-12-23 18:39:52 +0100584static void sunxi_drc_init(void)
585{
586#ifdef CONFIG_MACH_SUN6I
587 struct sunxi_ccm_reg * const ccm =
588 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
589
590 /* On sun6i the drc must be clocked even when in pass-through mode */
591 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DRC0);
592 clock_set_de_mod_clock(&ccm->iep_drc0_clk_cfg, 300000000);
593#endif
594}
595
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200596static void sunxi_engines_init(void)
597{
598 sunxi_composer_init();
599 sunxi_lcdc_init();
Hans de Goede211717a2014-11-14 17:42:14 +0100600 sunxi_drc_init();
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200601}
602
Hans de Goede1c092202014-12-21 14:37:45 +0100603static void sunxi_mode_set(const struct ctfb_res_modes *mode,
Hans de Goede5ee0bea2014-12-20 13:38:06 +0100604 unsigned int address)
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200605{
Hans de Goede0e045212014-12-21 14:49:34 +0100606 switch (sunxi_display.monitor) {
607 case sunxi_monitor_none:
608 break;
609 case sunxi_monitor_dvi:
610 case sunxi_monitor_hdmi: {
611 int clk_div, clk_double;
612 sunxi_composer_mode_set(mode, address);
613 sunxi_lcdc_tcon1_mode_set(mode, &clk_div, &clk_double);
614 sunxi_hdmi_mode_set(mode, clk_div, clk_double);
615 sunxi_composer_enable();
616 sunxi_lcdc_enable();
617 sunxi_hdmi_enable();
618 }
619 break;
620 case sunxi_monitor_lcd:
621 /* TODO */
622 break;
623 case sunxi_monitor_vga:
624 break;
625 }
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200626}
627
Hans de Goede1c092202014-12-21 14:37:45 +0100628static const char *sunxi_get_mon_desc(enum sunxi_monitor monitor)
629{
630 switch (monitor) {
631 case sunxi_monitor_none: return "none";
632 case sunxi_monitor_dvi: return "dvi";
633 case sunxi_monitor_hdmi: return "hdmi";
634 case sunxi_monitor_lcd: return "lcd";
635 case sunxi_monitor_vga: return "vga";
636 }
637 return NULL; /* never reached */
638}
639
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200640void *video_hw_init(void)
641{
642 static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
Hans de Goede5f339932014-12-19 14:03:40 +0100643 const struct ctfb_res_modes *mode;
Hans de Goede75481602014-12-19 16:05:12 +0100644 struct ctfb_res_modes edid_mode;
Hans de Goede5f339932014-12-19 14:03:40 +0100645 const char *options;
646 unsigned int depth;
Hans de Goede1c092202014-12-21 14:37:45 +0100647 int i, ret, hpd, edid;
648 char mon[16];
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200649
650 memset(&sunxi_display, 0, sizeof(struct sunxi_display));
651
652 printf("Reserved %dkB of RAM for Framebuffer.\n",
653 CONFIG_SUNXI_FB_SIZE >> 10);
654 gd->fb_base = gd->ram_top;
655
Hans de Goede5f339932014-12-19 14:03:40 +0100656 video_get_ctfb_res_modes(RES_MODE_1024x768, 24, &mode, &depth, &options);
Hans de Goede518cef22014-12-19 15:13:57 +0100657 hpd = video_get_option_int(options, "hpd", 1);
Hans de Goede75481602014-12-19 16:05:12 +0100658 edid = video_get_option_int(options, "edid", 1);
Hans de Goede1c092202014-12-21 14:37:45 +0100659 sunxi_display.monitor = sunxi_monitor_dvi;
660 video_get_option_string(options, "monitor", mon, sizeof(mon),
661 sunxi_get_mon_desc(sunxi_display.monitor));
662 for (i = 0; i <= SUNXI_MONITOR_LAST; i++) {
663 if (strcmp(mon, sunxi_get_mon_desc(i)) == 0) {
664 sunxi_display.monitor = i;
665 break;
666 }
667 }
668 if (i > SUNXI_MONITOR_LAST)
669 printf("Unknown monitor: '%s', falling back to '%s'\n",
670 mon, sunxi_get_mon_desc(sunxi_display.monitor));
Hans de Goede5f339932014-12-19 14:03:40 +0100671
Hans de Goede0e045212014-12-21 14:49:34 +0100672 switch (sunxi_display.monitor) {
673 case sunxi_monitor_none:
674 return NULL;
675 case sunxi_monitor_dvi:
676 case sunxi_monitor_hdmi:
677 /* Always call hdp_detect, as it also enables clocks, etc. */
678 ret = sunxi_hdmi_hpd_detect();
679 if (ret) {
680 printf("HDMI connected: ");
681 if (edid && sunxi_hdmi_edid_get_mode(&edid_mode) == 0)
682 mode = &edid_mode;
683 break;
684 }
685 if (!hpd)
686 break; /* User has requested to ignore hpd */
687
Hans de Goede518cef22014-12-19 15:13:57 +0100688 sunxi_hdmi_shutdown();
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200689 return NULL;
Hans de Goede0e045212014-12-21 14:49:34 +0100690 case sunxi_monitor_lcd:
691 printf("LCD not supported on this board\n");
692 return NULL;
693 case sunxi_monitor_vga:
694 printf("VGA not supported on this board\n");
695 return NULL;
Hans de Goede75481602014-12-19 16:05:12 +0100696 }
697
Hans de Goede5f339932014-12-19 14:03:40 +0100698 if (mode->vmode != FB_VMODE_NONINTERLACED) {
699 printf("Only non-interlaced modes supported, falling back to 1024x768\n");
700 mode = &res_mode_init[RES_MODE_1024x768];
701 } else {
Hans de Goede1c092202014-12-21 14:37:45 +0100702 printf("Setting up a %dx%d %s console\n", mode->xres,
703 mode->yres, sunxi_get_mon_desc(sunxi_display.monitor));
Hans de Goede5f339932014-12-19 14:03:40 +0100704 }
705
706 sunxi_display.enabled = true;
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200707 sunxi_engines_init();
Hans de Goede1c092202014-12-21 14:37:45 +0100708 sunxi_mode_set(mode, gd->fb_base - CONFIG_SYS_SDRAM_BASE);
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200709
710 /*
711 * These are the only members of this structure that are used. All the
712 * others are driver specific. There is nothing to decribe pitch or
713 * stride, but we are lucky with our hw.
714 */
715 graphic_device->frameAdrs = gd->fb_base;
716 graphic_device->gdfIndex = GDF_32BIT_X888RGB;
717 graphic_device->gdfBytesPP = 4;
Hans de Goedebe8ec632014-12-19 13:46:33 +0100718 graphic_device->winSizeX = mode->xres;
719 graphic_device->winSizeY = mode->yres;
Luc Verhaegen7f2c5212014-08-13 07:55:06 +0200720
721 return graphic_device;
722}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200723
724/*
725 * Simplefb support.
726 */
727#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_VIDEO_DT_SIMPLEFB)
728int sunxi_simplefb_setup(void *blob)
729{
730 static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
731 int offset, ret;
732
733 if (!sunxi_display.enabled)
734 return 0;
735
736 /* Find a framebuffer node, with pipeline == "de_be0-lcd0-hdmi" */
737 offset = fdt_node_offset_by_compatible(blob, -1,
738 "allwinner,simple-framebuffer");
739 while (offset >= 0) {
740 ret = fdt_find_string(blob, offset, "allwinner,pipeline",
741 "de_be0-lcd0-hdmi");
742 if (ret == 0)
743 break;
744 offset = fdt_node_offset_by_compatible(blob, offset,
745 "allwinner,simple-framebuffer");
746 }
747 if (offset < 0) {
748 eprintf("Cannot setup simplefb: node not found\n");
749 return 0; /* Keep older kernels working */
750 }
751
752 ret = fdt_setup_simplefb_node(blob, offset, gd->fb_base,
753 graphic_device->winSizeX, graphic_device->winSizeY,
754 graphic_device->winSizeX * graphic_device->gdfBytesPP,
755 "x8r8g8b8");
756 if (ret)
757 eprintf("Cannot setup simplefb: Error setting properties\n");
758
759 return ret;
760}
761#endif /* CONFIG_OF_BOARD_SETUP && CONFIG_VIDEO_DT_SIMPLEFB */