2 * Display driver for Allwinner SoCs.
4 * (C) Copyright 2013-2014 Luc Verhaegen <libv@skynet.be>
5 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/arch/clock.h>
13 #include <asm/arch/display.h>
14 #include <asm/arch/gpio.h>
15 #include <asm/global_data.h>
20 #include <fdt_support.h>
22 #include "videomodes.h"
24 DECLARE_GLOBAL_DATA_PTR;
33 #define SUNXI_MONITOR_LAST sunxi_monitor_vga
35 struct sunxi_display {
36 GraphicDevice graphic_device;
38 enum sunxi_monitor monitor;
43 * Wait up to 200ms for value to be set in given part of reg.
45 static int await_completion(u32 *reg, u32 mask, u32 val)
47 unsigned long tmo = timer_get_us() + 200000;
49 while ((readl(reg) & mask) != val) {
50 if (timer_get_us() > tmo) {
51 printf("DDC: timeout reading EDID\n");
58 static int sunxi_hdmi_hpd_detect(void)
60 struct sunxi_ccm_reg * const ccm =
61 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
62 struct sunxi_hdmi_reg * const hdmi =
63 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
64 unsigned long tmo = timer_get_us() + 300000;
66 /* Set pll3 to 300MHz */
67 clock_set_pll3(300000000);
69 /* Set hdmi parent to pll3 */
70 clrsetbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_PLL_MASK,
73 /* Set ahb gating to pass */
74 #ifdef CONFIG_MACH_SUN6I
75 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
77 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
80 setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
82 writel(SUNXI_HDMI_CTRL_ENABLE, &hdmi->ctrl);
83 writel(SUNXI_HDMI_PAD_CTRL0_HDP, &hdmi->pad_ctrl0);
85 while (timer_get_us() < tmo) {
86 if (readl(&hdmi->hpd) & SUNXI_HDMI_HPD_DETECT)
93 static void sunxi_hdmi_shutdown(void)
95 struct sunxi_ccm_reg * const ccm =
96 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
97 struct sunxi_hdmi_reg * const hdmi =
98 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
100 clrbits_le32(&hdmi->ctrl, SUNXI_HDMI_CTRL_ENABLE);
101 clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
102 clrbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
103 #ifdef CONFIG_MACH_SUN6I
104 clrbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
109 static int sunxi_hdmi_ddc_do_command(u32 cmnd, int offset, int n)
111 struct sunxi_hdmi_reg * const hdmi =
112 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
114 setbits_le32(&hdmi->ddc_fifo_ctrl, SUNXI_HDMI_DDC_FIFO_CTRL_CLEAR);
115 writel(SUNXI_HMDI_DDC_ADDR_EDDC_SEGMENT(offset >> 8) |
116 SUNXI_HMDI_DDC_ADDR_EDDC_ADDR |
117 SUNXI_HMDI_DDC_ADDR_OFFSET(offset) |
118 SUNXI_HMDI_DDC_ADDR_SLAVE_ADDR, &hdmi->ddc_addr);
119 #ifndef CONFIG_MACH_SUN6I
120 writel(n, &hdmi->ddc_byte_count);
121 writel(cmnd, &hdmi->ddc_cmnd);
123 writel(n << 16 | cmnd, &hdmi->ddc_cmnd);
125 setbits_le32(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_START);
127 return await_completion(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_START, 0);
130 static int sunxi_hdmi_ddc_read(int offset, u8 *buf, int count)
132 struct sunxi_hdmi_reg * const hdmi =
133 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
142 if (sunxi_hdmi_ddc_do_command(
143 SUNXI_HDMI_DDC_CMND_EXPLICIT_EDDC_READ,
147 for (i = 0; i < n; i++)
148 *buf++ = readb(&hdmi->ddc_fifo_data);
157 static int sunxi_hdmi_edid_get_block(int block, u8 *buf)
162 r = sunxi_hdmi_ddc_read(block * 128, buf, 128);
165 r = edid_check_checksum(buf);
167 printf("EDID block %d: checksum error%s\n",
168 block, retries ? ", retrying" : "");
170 } while (r && retries--);
175 static int sunxi_hdmi_edid_get_mode(struct ctfb_res_modes *mode)
177 struct edid1_info edid1;
178 struct edid_cea861_info cea681[4];
179 struct edid_detailed_timing *t =
180 (struct edid_detailed_timing *)edid1.monitor_details.timing;
181 struct sunxi_hdmi_reg * const hdmi =
182 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
183 struct sunxi_ccm_reg * const ccm =
184 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
185 int i, r, ext_blocks = 0;
187 /* SUNXI_HDMI_CTRL_ENABLE & PAD_CTRL0 are already set by hpd_detect */
188 writel(SUNXI_HDMI_PAD_CTRL1 | SUNXI_HDMI_PAD_CTRL1_HALVE,
190 writel(SUNXI_HDMI_PLL_CTRL | SUNXI_HDMI_PLL_CTRL_DIV(15),
192 writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
194 /* Reset i2c controller */
195 setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_DDC_GATE);
196 writel(SUNXI_HMDI_DDC_CTRL_ENABLE |
197 SUNXI_HMDI_DDC_CTRL_SDA_ENABLE |
198 SUNXI_HMDI_DDC_CTRL_SCL_ENABLE |
199 SUNXI_HMDI_DDC_CTRL_RESET, &hdmi->ddc_ctrl);
200 if (await_completion(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_RESET, 0))
203 writel(SUNXI_HDMI_DDC_CLOCK, &hdmi->ddc_clock);
204 #ifndef CONFIG_MACH_SUN6I
205 writel(SUNXI_HMDI_DDC_LINE_CTRL_SDA_ENABLE |
206 SUNXI_HMDI_DDC_LINE_CTRL_SCL_ENABLE, &hdmi->ddc_line_ctrl);
209 r = sunxi_hdmi_edid_get_block(0, (u8 *)&edid1);
211 r = edid_check_info(&edid1);
213 printf("EDID: invalid EDID data\n");
218 ext_blocks = edid1.extension_flag;
221 for (i = 0; i < ext_blocks; i++) {
222 if (sunxi_hdmi_edid_get_block(1 + i,
223 (u8 *)&cea681[i]) != 0) {
230 /* Disable DDC engine, no longer needed */
231 clrbits_le32(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_ENABLE);
232 clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_DDC_GATE);
237 /* We want version 1.3 or 1.2 with detailed timing info */
238 if (edid1.version != 1 || (edid1.revision < 3 &&
239 !EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(edid1))) {
240 printf("EDID: unsupported version %d.%d\n",
241 edid1.version, edid1.revision);
245 /* Take the first usable detailed timing */
246 for (i = 0; i < 4; i++, t++) {
247 r = video_edid_dtd_to_ctfb_res_modes(t, mode);
252 printf("EDID: no usable detailed timing found\n");
256 /* Check for basic audio support, if found enable hdmi output */
257 sunxi_display.monitor = sunxi_monitor_dvi;
258 for (i = 0; i < ext_blocks; i++) {
259 if (cea681[i].extension_tag != EDID_CEA861_EXTENSION_TAG ||
260 cea681[i].revision < 2)
263 if (EDID_CEA861_SUPPORTS_BASIC_AUDIO(cea681[i]))
264 sunxi_display.monitor = sunxi_monitor_hdmi;
271 * This is the entity that mixes and matches the different layers and inputs.
272 * Allwinner calls it the back-end, but i like composer better.
274 static void sunxi_composer_init(void)
276 struct sunxi_ccm_reg * const ccm =
277 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
278 struct sunxi_de_be_reg * const de_be =
279 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
282 #ifdef CONFIG_MACH_SUN6I
284 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DE_BE0);
288 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE_BE0);
289 setbits_le32(&ccm->dram_clk_gate, 1 << CCM_DRAM_GATE_OFFSET_DE_BE0);
290 clock_set_de_mod_clock(&ccm->be0_clk_cfg, 300000000);
292 /* Engine bug, clear registers after reset */
293 for (i = 0x0800; i < 0x1000; i += 4)
294 writel(0, SUNXI_DE_BE0_BASE + i);
296 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_ENABLE);
299 static void sunxi_composer_mode_set(const struct ctfb_res_modes *mode,
300 unsigned int address)
302 struct sunxi_de_be_reg * const de_be =
303 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
305 writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
307 writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
308 &de_be->layer0_size);
309 writel(SUNXI_DE_BE_LAYER_STRIDE(mode->xres), &de_be->layer0_stride);
310 writel(address << 3, &de_be->layer0_addr_low32b);
311 writel(address >> 29, &de_be->layer0_addr_high4b);
312 writel(SUNXI_DE_BE_LAYER_ATTR1_FMT_XRGB8888, &de_be->layer0_attr1_ctrl);
314 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_LAYER0_ENABLE);
317 static void sunxi_composer_enable(void)
319 struct sunxi_de_be_reg * const de_be =
320 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
322 setbits_le32(&de_be->reg_ctrl, SUNXI_DE_BE_REG_CTRL_LOAD_REGS);
323 setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
327 * LCDC, what allwinner calls a CRTC, so timing controller and serializer.
329 static void sunxi_lcdc_pll_set(int tcon, int dotclock,
330 int *clk_div, int *clk_double)
332 struct sunxi_ccm_reg * const ccm =
333 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
334 int value, n, m, min_m, max_m, diff;
335 int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
347 * Find the lowest divider resulting in a matching clock, if there
348 * is no match, pick the closest lower clock, as monitors tend to
349 * not sync to higher frequencies.
351 for (m = min_m; m <= max_m; m++) {
352 n = (m * dotclock) / 3000;
354 if ((n >= 9) && (n <= 127)) {
355 value = (3000 * n) / m;
356 diff = dotclock - value;
357 if (diff < best_diff) {
365 /* These are just duplicates */
369 n = (m * dotclock) / 6000;
370 if ((n >= 9) && (n <= 127)) {
371 value = (6000 * n) / m;
372 diff = dotclock - value;
373 if (diff < best_diff) {
382 debug("dotclock: %dkHz = %dkHz: (%d * 3MHz * %d) / %d\n",
383 dotclock, (best_double + 1) * 3000 * best_n / best_m,
384 best_double + 1, best_n, best_m);
386 clock_set_pll3(best_n * 3000000);
389 writel(CCM_LCD_CH0_CTRL_GATE | CCM_LCD_CH0_CTRL_RST |
390 (best_double ? CCM_LCD_CH0_CTRL_PLL3_2X :
391 CCM_LCD_CH0_CTRL_PLL3),
392 &ccm->lcd0_ch0_clk_cfg);
394 writel(CCM_LCD_CH1_CTRL_GATE |
395 (best_double ? CCM_LCD_CH1_CTRL_PLL3_2X :
396 CCM_LCD_CH1_CTRL_PLL3) |
397 CCM_LCD_CH1_CTRL_M(best_m), &ccm->lcd0_ch1_clk_cfg);
401 *clk_double = best_double;
404 static void sunxi_lcdc_init(void)
406 struct sunxi_ccm_reg * const ccm =
407 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
408 struct sunxi_lcdc_reg * const lcdc =
409 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
412 #ifdef CONFIG_MACH_SUN6I
413 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
415 setbits_le32(&ccm->lcd0_ch0_clk_cfg, CCM_LCD_CH0_CTRL_RST);
419 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
422 writel(0, &lcdc->ctrl); /* Disable tcon */
423 writel(0, &lcdc->int0); /* Disable all interrupts */
425 /* Disable tcon0 dot clock */
426 clrbits_le32(&lcdc->tcon0_dclk, SUNXI_LCDC_TCON0_DCLK_ENABLE);
428 /* Set all io lines to tristate */
429 writel(0xffffffff, &lcdc->tcon0_io_tristate);
430 writel(0xffffffff, &lcdc->tcon1_io_tristate);
433 static void sunxi_lcdc_enable(void)
435 struct sunxi_lcdc_reg * const lcdc =
436 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
438 setbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
441 static void sunxi_lcdc_panel_enable(void)
446 * Start with backlight disabled to avoid the screen flashing to
447 * white while the lcd inits.
449 pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_EN);
451 gpio_request(pin, "lcd_backlight_enable");
452 gpio_direction_output(pin, 0);
455 pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_PWM);
457 gpio_request(pin, "lcd_backlight_pwm");
458 /* backlight pwm is inverted, set to 1 to disable backlight */
459 gpio_direction_output(pin, 1);
462 /* Give the backlight some time to turn off and power up the panel. */
464 pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_POWER);
466 gpio_request(pin, "lcd_power");
467 gpio_direction_output(pin, 1);
471 static void sunxi_lcdc_backlight_enable(void)
476 * We want to have scanned out at least one frame before enabling the
477 * backlight to avoid the screen flashing to white when we enable it.
481 pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_EN);
483 gpio_direction_output(pin, 1);
485 pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_PWM);
487 /* backlight pwm is inverted, set to 0 to enable backlight */
488 gpio_direction_output(pin, 0);
492 static int sunxi_lcdc_get_clk_delay(const struct ctfb_res_modes *mode)
496 delay = mode->lower_margin + mode->vsync_len + mode->upper_margin - 2;
497 return (delay > 30) ? 30 : delay;
500 static void sunxi_lcdc_tcon0_mode_set(const struct ctfb_res_modes *mode)
502 struct sunxi_lcdc_reg * const lcdc =
503 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
504 int bp, clk_delay, clk_div, clk_double, pin, total, val;
506 for (pin = SUNXI_GPD(0); pin <= SUNXI_GPD(27); pin++)
507 sunxi_gpio_set_cfgpin(pin, SUNXI_GPD0_LCD0);
509 sunxi_lcdc_pll_set(0, mode->pixclock_khz, &clk_div, &clk_double);
512 clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
513 SUNXI_LCDC_CTRL_IO_MAP_TCON0);
515 clk_delay = sunxi_lcdc_get_clk_delay(mode);
516 writel(SUNXI_LCDC_TCON0_CTRL_ENABLE |
517 SUNXI_LCDC_TCON0_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon0_ctrl);
519 writel(SUNXI_LCDC_TCON0_DCLK_ENABLE |
520 SUNXI_LCDC_TCON0_DCLK_DIV(clk_div), &lcdc->tcon0_dclk);
522 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
523 &lcdc->tcon0_timing_active);
525 bp = mode->hsync_len + mode->left_margin;
526 total = mode->xres + mode->right_margin + bp;
527 writel(SUNXI_LCDC_TCON0_TIMING_H_TOTAL(total) |
528 SUNXI_LCDC_TCON0_TIMING_H_BP(bp), &lcdc->tcon0_timing_h);
530 bp = mode->vsync_len + mode->upper_margin;
531 total = mode->yres + mode->lower_margin + bp;
532 writel(SUNXI_LCDC_TCON0_TIMING_V_TOTAL(total) |
533 SUNXI_LCDC_TCON0_TIMING_V_BP(bp), &lcdc->tcon0_timing_v);
535 writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
536 &lcdc->tcon0_timing_sync);
538 /* We only support hv-sync parallel lcd-s for now */
539 writel(0, &lcdc->tcon0_hv_intf);
540 writel(0, &lcdc->tcon0_cpu_intf);
542 if (sunxi_display.depth == 18 || sunxi_display.depth == 16) {
543 writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[0]);
544 writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[1]);
545 writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[2]);
546 writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[3]);
547 writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[4]);
548 writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[5]);
549 writel(SUNXI_LCDC_TCON0_FRM_TAB0, &lcdc->tcon0_frm_table[0]);
550 writel(SUNXI_LCDC_TCON0_FRM_TAB1, &lcdc->tcon0_frm_table[1]);
551 writel(SUNXI_LCDC_TCON0_FRM_TAB2, &lcdc->tcon0_frm_table[2]);
552 writel(SUNXI_LCDC_TCON0_FRM_TAB3, &lcdc->tcon0_frm_table[3]);
553 writel(((sunxi_display.depth == 18) ?
554 SUNXI_LCDC_TCON0_FRM_CTRL_RGB666 :
555 SUNXI_LCDC_TCON0_FRM_CTRL_RGB565),
556 &lcdc->tcon0_frm_ctrl);
560 if (!(mode->sync & FB_SYNC_HOR_HIGH_ACT))
561 val |= SUNXI_LCDC_TCON_HSYNC_MASK;
562 if (!(mode->sync & FB_SYNC_VERT_HIGH_ACT))
563 val |= SUNXI_LCDC_TCON_VSYNC_MASK;
564 writel(val, &lcdc->tcon0_io_polarity);
566 writel(0, &lcdc->tcon0_io_tristate);
569 static void sunxi_lcdc_tcon1_mode_set(const struct ctfb_res_modes *mode,
570 int *clk_div, int *clk_double)
572 struct sunxi_lcdc_reg * const lcdc =
573 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
577 clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
578 SUNXI_LCDC_CTRL_IO_MAP_TCON1);
580 /* Enabled, 0x1e start delay */
581 writel(SUNXI_LCDC_TCON1_CTRL_ENABLE |
582 SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(0x1e), &lcdc->tcon1_ctrl);
584 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
585 &lcdc->tcon1_timing_source);
586 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
587 &lcdc->tcon1_timing_scale);
588 writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
589 &lcdc->tcon1_timing_out);
591 bp = mode->hsync_len + mode->left_margin;
592 total = mode->xres + mode->right_margin + bp;
593 writel(SUNXI_LCDC_TCON1_TIMING_H_TOTAL(total) |
594 SUNXI_LCDC_TCON1_TIMING_H_BP(bp), &lcdc->tcon1_timing_h);
596 bp = mode->vsync_len + mode->upper_margin;
597 total = mode->yres + mode->lower_margin + bp;
598 writel(SUNXI_LCDC_TCON1_TIMING_V_TOTAL(total) |
599 SUNXI_LCDC_TCON1_TIMING_V_BP(bp), &lcdc->tcon1_timing_v);
601 writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
602 &lcdc->tcon1_timing_sync);
604 sunxi_lcdc_pll_set(1, mode->pixclock_khz, clk_div, clk_double);
607 static void sunxi_hdmi_setup_info_frames(const struct ctfb_res_modes *mode)
609 struct sunxi_hdmi_reg * const hdmi =
610 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
612 u8 avi_info_frame[17] = {
613 0x82, 0x02, 0x0d, 0x00, 0x12, 0x00, 0x88, 0x00,
614 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
617 u8 vendor_info_frame[19] = {
618 0x81, 0x01, 0x06, 0x29, 0x03, 0x0c, 0x00, 0x40,
619 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
624 if (mode->pixclock_khz <= 27000)
625 avi_info_frame[5] = 0x40; /* SD-modes, ITU601 colorspace */
627 avi_info_frame[5] = 0x80; /* HD-modes, ITU709 colorspace */
629 if (mode->xres * 100 / mode->yres < 156)
630 avi_info_frame[5] |= 0x18; /* 4 : 3 */
632 avi_info_frame[5] |= 0x28; /* 16 : 9 */
634 for (i = 0; i < ARRAY_SIZE(avi_info_frame); i++)
635 checksum += avi_info_frame[i];
637 avi_info_frame[3] = 0x100 - checksum;
639 for (i = 0; i < ARRAY_SIZE(avi_info_frame); i++)
640 writeb(avi_info_frame[i], &hdmi->avi_info_frame[i]);
642 writel(SUNXI_HDMI_QCP_PACKET0, &hdmi->qcp_packet0);
643 writel(SUNXI_HDMI_QCP_PACKET1, &hdmi->qcp_packet1);
645 for (i = 0; i < ARRAY_SIZE(vendor_info_frame); i++)
646 writeb(vendor_info_frame[i], &hdmi->vendor_info_frame[i]);
648 writel(SUNXI_HDMI_PKT_CTRL0, &hdmi->pkt_ctrl0);
649 writel(SUNXI_HDMI_PKT_CTRL1, &hdmi->pkt_ctrl1);
651 setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_HDMI);
654 static void sunxi_hdmi_mode_set(const struct ctfb_res_modes *mode,
655 int clk_div, int clk_double)
657 struct sunxi_hdmi_reg * const hdmi =
658 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
661 /* Write clear interrupt status bits */
662 writel(SUNXI_HDMI_IRQ_STATUS_BITS, &hdmi->irq);
664 if (sunxi_display.monitor == sunxi_monitor_hdmi)
665 sunxi_hdmi_setup_info_frames(mode);
667 /* Set input sync enable */
668 writel(SUNXI_HDMI_UNKNOWN_INPUT_SYNC, &hdmi->unknown);
670 /* Init various registers, select pll3 as clock source */
671 writel(SUNXI_HDMI_VIDEO_POL_TX_CLK, &hdmi->video_polarity);
672 writel(SUNXI_HDMI_PAD_CTRL0_RUN, &hdmi->pad_ctrl0);
673 writel(SUNXI_HDMI_PAD_CTRL1, &hdmi->pad_ctrl1);
674 writel(SUNXI_HDMI_PLL_CTRL, &hdmi->pll_ctrl);
675 writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
677 /* Setup clk div and doubler */
678 clrsetbits_le32(&hdmi->pll_ctrl, SUNXI_HDMI_PLL_CTRL_DIV_MASK,
679 SUNXI_HDMI_PLL_CTRL_DIV(clk_div));
681 setbits_le32(&hdmi->pad_ctrl1, SUNXI_HDMI_PAD_CTRL1_HALVE);
683 /* Setup timing registers */
684 writel(SUNXI_HDMI_Y(mode->yres) | SUNXI_HDMI_X(mode->xres),
687 x = mode->hsync_len + mode->left_margin;
688 y = mode->vsync_len + mode->upper_margin;
689 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_bp);
691 x = mode->right_margin;
692 y = mode->lower_margin;
693 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_fp);
697 writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_spw);
699 if (mode->sync & FB_SYNC_HOR_HIGH_ACT)
700 setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_HOR);
702 if (mode->sync & FB_SYNC_VERT_HIGH_ACT)
703 setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_VER);
706 static void sunxi_hdmi_enable(void)
708 struct sunxi_hdmi_reg * const hdmi =
709 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
712 setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
715 static void sunxi_drc_init(void)
717 #ifdef CONFIG_MACH_SUN6I
718 struct sunxi_ccm_reg * const ccm =
719 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
721 /* On sun6i the drc must be clocked even when in pass-through mode */
722 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DRC0);
723 clock_set_de_mod_clock(&ccm->iep_drc0_clk_cfg, 300000000);
727 static void sunxi_engines_init(void)
729 sunxi_composer_init();
734 static void sunxi_mode_set(const struct ctfb_res_modes *mode,
735 unsigned int address)
737 switch (sunxi_display.monitor) {
738 case sunxi_monitor_none:
740 case sunxi_monitor_dvi:
741 case sunxi_monitor_hdmi: {
742 int clk_div, clk_double;
743 sunxi_composer_mode_set(mode, address);
744 sunxi_lcdc_tcon1_mode_set(mode, &clk_div, &clk_double);
745 sunxi_hdmi_mode_set(mode, clk_div, clk_double);
746 sunxi_composer_enable();
751 case sunxi_monitor_lcd:
752 sunxi_lcdc_panel_enable();
753 sunxi_composer_mode_set(mode, address);
754 sunxi_lcdc_tcon0_mode_set(mode);
755 sunxi_composer_enable();
757 sunxi_lcdc_backlight_enable();
759 case sunxi_monitor_vga:
764 static const char *sunxi_get_mon_desc(enum sunxi_monitor monitor)
767 case sunxi_monitor_none: return "none";
768 case sunxi_monitor_dvi: return "dvi";
769 case sunxi_monitor_hdmi: return "hdmi";
770 case sunxi_monitor_lcd: return "lcd";
771 case sunxi_monitor_vga: return "vga";
773 return NULL; /* never reached */
776 void *video_hw_init(void)
778 static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
779 const struct ctfb_res_modes *mode;
780 struct ctfb_res_modes custom;
782 int i, ret, hpd, edid;
784 char *lcd_mode = CONFIG_VIDEO_LCD_MODE;
786 memset(&sunxi_display, 0, sizeof(struct sunxi_display));
788 printf("Reserved %dkB of RAM for Framebuffer.\n",
789 CONFIG_SUNXI_FB_SIZE >> 10);
790 gd->fb_base = gd->ram_top;
792 video_get_ctfb_res_modes(RES_MODE_1024x768, 24, &mode,
793 &sunxi_display.depth, &options);
794 hpd = video_get_option_int(options, "hpd", 1);
795 edid = video_get_option_int(options, "edid", 1);
796 sunxi_display.monitor = sunxi_monitor_dvi;
797 video_get_option_string(options, "monitor", mon, sizeof(mon),
798 sunxi_get_mon_desc(sunxi_display.monitor));
799 for (i = 0; i <= SUNXI_MONITOR_LAST; i++) {
800 if (strcmp(mon, sunxi_get_mon_desc(i)) == 0) {
801 sunxi_display.monitor = i;
805 if (i > SUNXI_MONITOR_LAST)
806 printf("Unknown monitor: '%s', falling back to '%s'\n",
807 mon, sunxi_get_mon_desc(sunxi_display.monitor));
809 switch (sunxi_display.monitor) {
810 case sunxi_monitor_none:
812 case sunxi_monitor_dvi:
813 case sunxi_monitor_hdmi:
814 /* Always call hdp_detect, as it also enables clocks, etc. */
815 ret = sunxi_hdmi_hpd_detect();
817 printf("HDMI connected: ");
818 if (edid && sunxi_hdmi_edid_get_mode(&custom) == 0)
823 break; /* User has requested to ignore hpd */
825 sunxi_hdmi_shutdown();
827 if (lcd_mode[0] == 0)
828 return NULL; /* No LCD, bail */
830 /* Fall back / through to LCD */
831 sunxi_display.monitor = sunxi_monitor_lcd;
832 case sunxi_monitor_lcd:
834 sunxi_display.depth = video_get_params(&custom, lcd_mode);
838 printf("LCD not supported on this board\n");
840 case sunxi_monitor_vga:
841 printf("VGA not supported on this board\n");
845 if (mode->vmode != FB_VMODE_NONINTERLACED) {
846 printf("Only non-interlaced modes supported, falling back to 1024x768\n");
847 mode = &res_mode_init[RES_MODE_1024x768];
849 printf("Setting up a %dx%d %s console\n", mode->xres,
850 mode->yres, sunxi_get_mon_desc(sunxi_display.monitor));
853 sunxi_display.enabled = true;
854 sunxi_engines_init();
855 sunxi_mode_set(mode, gd->fb_base - CONFIG_SYS_SDRAM_BASE);
858 * These are the only members of this structure that are used. All the
859 * others are driver specific. There is nothing to decribe pitch or
860 * stride, but we are lucky with our hw.
862 graphic_device->frameAdrs = gd->fb_base;
863 graphic_device->gdfIndex = GDF_32BIT_X888RGB;
864 graphic_device->gdfBytesPP = 4;
865 graphic_device->winSizeX = mode->xres;
866 graphic_device->winSizeY = mode->yres;
868 return graphic_device;
874 #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_VIDEO_DT_SIMPLEFB)
875 int sunxi_simplefb_setup(void *blob)
877 static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
879 const char *pipeline = NULL;
881 if (!sunxi_display.enabled)
884 switch (sunxi_display.monitor) {
885 case sunxi_monitor_none:
887 case sunxi_monitor_dvi:
888 case sunxi_monitor_hdmi:
889 pipeline = "de_be0-lcd0-hdmi";
891 case sunxi_monitor_lcd:
892 pipeline = "de_be0-lcd0";
894 case sunxi_monitor_vga:
898 /* Find a prefilled simpefb node, matching out pipeline config */
899 offset = fdt_node_offset_by_compatible(blob, -1,
900 "allwinner,simple-framebuffer");
901 while (offset >= 0) {
902 ret = fdt_find_string(blob, offset, "allwinner,pipeline",
906 offset = fdt_node_offset_by_compatible(blob, offset,
907 "allwinner,simple-framebuffer");
910 eprintf("Cannot setup simplefb: node not found\n");
911 return 0; /* Keep older kernels working */
914 ret = fdt_setup_simplefb_node(blob, offset, gd->fb_base,
915 graphic_device->winSizeX, graphic_device->winSizeY,
916 graphic_device->winSizeX * graphic_device->gdfBytesPP,
919 eprintf("Cannot setup simplefb: Error setting properties\n");
923 #endif /* CONFIG_OF_BOARD_SETUP && CONFIG_VIDEO_DT_SIMPLEFB */