]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/video/sunxi_display.c
sunxi: video: Add hdmi support
[karo-tx-uboot.git] / drivers / video / sunxi_display.c
1 /*
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>
16 #include <errno.h>
17 #include <fdtdec.h>
18 #include <fdt_support.h>
19 #include <video_fb.h>
20 #include "videomodes.h"
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 struct sunxi_display {
25         GraphicDevice graphic_device;
26         bool enabled;
27 } sunxi_display;
28
29 /*
30  * Wait up to 200ms for value to be set in given part of reg.
31  */
32 static int await_completion(u32 *reg, u32 mask, u32 val)
33 {
34         unsigned long tmo = timer_get_us() + 200000;
35
36         while ((readl(reg) & mask) != val) {
37                 if (timer_get_us() > tmo) {
38                         printf("DDC: timeout reading EDID\n");
39                         return -ETIME;
40                 }
41         }
42         return 0;
43 }
44
45 static int sunxi_hdmi_hpd_detect(void)
46 {
47         struct sunxi_ccm_reg * const ccm =
48                 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
49         struct sunxi_hdmi_reg * const hdmi =
50                 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
51
52         /* Set pll3 to 300MHz */
53         clock_set_pll3(300000000);
54
55         /* Set hdmi parent to pll3 */
56         clrsetbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_PLL_MASK,
57                         CCM_HDMI_CTRL_PLL3);
58
59         /* Set ahb gating to pass */
60 #ifdef CONFIG_MACH_SUN6I
61         setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
62 #endif
63         setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
64
65         /* Clock on */
66         setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
67
68         writel(SUNXI_HDMI_CTRL_ENABLE, &hdmi->ctrl);
69         writel(SUNXI_HDMI_PAD_CTRL0_HDP, &hdmi->pad_ctrl0);
70
71         udelay(1000);
72
73         return (readl(&hdmi->hpd) & SUNXI_HDMI_HPD_DETECT) ? 1 : 0;
74 }
75
76 static void sunxi_hdmi_shutdown(void)
77 {
78         struct sunxi_ccm_reg * const ccm =
79                 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
80         struct sunxi_hdmi_reg * const hdmi =
81                 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
82
83         clrbits_le32(&hdmi->ctrl, SUNXI_HDMI_CTRL_ENABLE);
84         clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
85         clrbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
86 #ifdef CONFIG_MACH_SUN6I
87         clrbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
88 #endif
89         clock_set_pll3(0);
90 }
91
92 static int sunxi_hdmi_ddc_do_command(u32 cmnd, int offset, int n)
93 {
94         struct sunxi_hdmi_reg * const hdmi =
95                 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
96
97         setbits_le32(&hdmi->ddc_fifo_ctrl, SUNXI_HDMI_DDC_FIFO_CTRL_CLEAR);
98         writel(SUNXI_HMDI_DDC_ADDR_EDDC_SEGMENT(offset >> 8) |
99                SUNXI_HMDI_DDC_ADDR_EDDC_ADDR |
100                SUNXI_HMDI_DDC_ADDR_OFFSET(offset) |
101                SUNXI_HMDI_DDC_ADDR_SLAVE_ADDR, &hdmi->ddc_addr);
102 #ifndef CONFIG_MACH_SUN6I
103         writel(n, &hdmi->ddc_byte_count);
104         writel(cmnd, &hdmi->ddc_cmnd);
105 #else
106         writel(n << 16 | cmnd, &hdmi->ddc_cmnd);
107 #endif
108         setbits_le32(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_START);
109
110         return await_completion(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_START, 0);
111 }
112
113 static int sunxi_hdmi_ddc_read(int offset, u8 *buf, int count)
114 {
115         struct sunxi_hdmi_reg * const hdmi =
116                 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
117         int i, n;
118
119         while (count > 0) {
120                 if (count > 16)
121                         n = 16;
122                 else
123                         n = count;
124
125                 if (sunxi_hdmi_ddc_do_command(
126                                 SUNXI_HDMI_DDC_CMND_EXPLICIT_EDDC_READ,
127                                 offset, n))
128                         return -ETIME;
129
130                 for (i = 0; i < n; i++)
131                         *buf++ = readb(&hdmi->ddc_fifo_data);
132
133                 offset += n;
134                 count -= n;
135         }
136
137         return 0;
138 }
139
140 static int sunxi_hdmi_edid_get_mode(struct ctfb_res_modes *mode)
141 {
142         struct edid1_info edid1;
143         struct edid_detailed_timing *t =
144                 (struct edid_detailed_timing *)edid1.monitor_details.timing;
145         struct sunxi_hdmi_reg * const hdmi =
146                 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
147         struct sunxi_ccm_reg * const ccm =
148                 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
149         int i, r, retries = 2;
150
151         /* SUNXI_HDMI_CTRL_ENABLE & PAD_CTRL0 are already set by hpd_detect */
152         writel(SUNXI_HDMI_PAD_CTRL1 | SUNXI_HDMI_PAD_CTRL1_HALVE,
153                &hdmi->pad_ctrl1);
154         writel(SUNXI_HDMI_PLL_CTRL | SUNXI_HDMI_PLL_CTRL_DIV(15),
155                &hdmi->pll_ctrl);
156         writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
157
158         /* Reset i2c controller */
159         setbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_DDC_GATE);
160         writel(SUNXI_HMDI_DDC_CTRL_ENABLE |
161                SUNXI_HMDI_DDC_CTRL_SDA_ENABLE |
162                SUNXI_HMDI_DDC_CTRL_SCL_ENABLE |
163                SUNXI_HMDI_DDC_CTRL_RESET, &hdmi->ddc_ctrl);
164         if (await_completion(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_RESET, 0))
165                 return -EIO;
166
167         writel(SUNXI_HDMI_DDC_CLOCK, &hdmi->ddc_clock);
168 #ifndef CONFIG_MACH_SUN6I
169         writel(SUNXI_HMDI_DDC_LINE_CTRL_SDA_ENABLE |
170                SUNXI_HMDI_DDC_LINE_CTRL_SCL_ENABLE, &hdmi->ddc_line_ctrl);
171 #endif
172
173         do {
174                 r = sunxi_hdmi_ddc_read(0, (u8 *)&edid1, 128);
175                 if (r)
176                         continue;
177                 r = edid_check_checksum((u8 *)&edid1);
178                 if (r) {
179                         printf("EDID: checksum error%s\n",
180                                retries ? ", retrying" : "");
181                 }
182         } while (r && retries--);
183
184         /* Disable DDC engine, no longer needed */
185         clrbits_le32(&hdmi->ddc_ctrl, SUNXI_HMDI_DDC_CTRL_ENABLE);
186         clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_DDC_GATE);
187
188         if (r)
189                 return r;
190
191         r = edid_check_info(&edid1);
192         if (r) {
193                 printf("EDID: invalid EDID data\n");
194                 return -EINVAL;
195         }
196
197         /* We want version 1.3 or 1.2 with detailed timing info */
198         if (edid1.version != 1 || (edid1.revision < 3 &&
199                         !EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(edid1))) {
200                 printf("EDID: unsupported version %d.%d\n",
201                        edid1.version, edid1.revision);
202                 return -EINVAL;
203         }
204
205         /* Take the first usable detailed timing */
206         for (i = 0; i < 4; i++, t++) {
207                 r = video_edid_dtd_to_ctfb_res_modes(t, mode);
208                 if (r == 0)
209                         break;
210         }
211         if (i == 4) {
212                 printf("EDID: no usable detailed timing found\n");
213                 return -ENOENT;
214         }
215
216         return 0;
217 }
218
219 /*
220  * This is the entity that mixes and matches the different layers and inputs.
221  * Allwinner calls it the back-end, but i like composer better.
222  */
223 static void sunxi_composer_init(void)
224 {
225         struct sunxi_ccm_reg * const ccm =
226                 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
227         struct sunxi_de_be_reg * const de_be =
228                 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
229         int i;
230
231 #ifdef CONFIG_MACH_SUN6I
232         /* Reset off */
233         setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DE_BE0);
234 #endif
235
236         /* Clocks on */
237         setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE_BE0);
238         setbits_le32(&ccm->dram_clk_gate, 1 << CCM_DRAM_GATE_OFFSET_DE_BE0);
239         clock_set_de_mod_clock(&ccm->be0_clk_cfg, 300000000);
240
241         /* Engine bug, clear registers after reset */
242         for (i = 0x0800; i < 0x1000; i += 4)
243                 writel(0, SUNXI_DE_BE0_BASE + i);
244
245         setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_ENABLE);
246 }
247
248 static void sunxi_composer_mode_set(const struct ctfb_res_modes *mode,
249                                     unsigned int address)
250 {
251         struct sunxi_de_be_reg * const de_be =
252                 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
253
254         writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
255                &de_be->disp_size);
256         writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
257                &de_be->layer0_size);
258         writel(SUNXI_DE_BE_LAYER_STRIDE(mode->xres), &de_be->layer0_stride);
259         writel(address << 3, &de_be->layer0_addr_low32b);
260         writel(address >> 29, &de_be->layer0_addr_high4b);
261         writel(SUNXI_DE_BE_LAYER_ATTR1_FMT_XRGB8888, &de_be->layer0_attr1_ctrl);
262
263         setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_LAYER0_ENABLE);
264 }
265
266 /*
267  * LCDC, what allwinner calls a CRTC, so timing controller and serializer.
268  */
269 static void sunxi_lcdc_pll_set(int dotclock, int *clk_div, int *clk_double)
270 {
271         struct sunxi_ccm_reg * const ccm =
272                 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
273         int value, n, m, diff;
274         int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
275         int best_double = 0;
276
277         /*
278          * Find the lowest divider resulting in a matching clock, if there
279          * is no match, pick the closest lower clock, as monitors tend to
280          * not sync to higher frequencies.
281          */
282         for (m = 15; m > 0; m--) {
283                 n = (m * dotclock) / 3000;
284
285                 if ((n >= 9) && (n <= 127)) {
286                         value = (3000 * n) / m;
287                         diff = dotclock - value;
288                         if (diff < best_diff) {
289                                 best_diff = diff;
290                                 best_m = m;
291                                 best_n = n;
292                                 best_double = 0;
293                         }
294                 }
295
296                 /* These are just duplicates */
297                 if (!(m & 1))
298                         continue;
299
300                 n = (m * dotclock) / 6000;
301                 if ((n >= 9) && (n <= 127)) {
302                         value = (6000 * n) / m;
303                         diff = dotclock - value;
304                         if (diff < best_diff) {
305                                 best_diff = diff;
306                                 best_m = m;
307                                 best_n = n;
308                                 best_double = 1;
309                         }
310                 }
311         }
312
313         debug("dotclock: %dkHz = %dkHz: (%d * 3MHz * %d) / %d\n",
314               dotclock, (best_double + 1) * 3000 * best_n / best_m,
315               best_double + 1, best_n, best_m);
316
317         clock_set_pll3(best_n * 3000000);
318
319         writel(CCM_LCD_CH1_CTRL_GATE |
320             (best_double ? CCM_LCD_CH1_CTRL_PLL3_2X : CCM_LCD_CH1_CTRL_PLL3) |
321             CCM_LCD_CH1_CTRL_M(best_m), &ccm->lcd0_ch1_clk_cfg);
322
323         *clk_div = best_m;
324         *clk_double = best_double;
325 }
326
327 static void sunxi_lcdc_init(void)
328 {
329         struct sunxi_ccm_reg * const ccm =
330                 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
331         struct sunxi_lcdc_reg * const lcdc =
332                 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
333
334         /* Reset off */
335 #ifdef CONFIG_MACH_SUN6I
336         setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
337 #else
338         setbits_le32(&ccm->lcd0_ch0_clk_cfg, CCM_LCD_CH0_CTRL_RST);
339 #endif
340
341         /* Clock on */
342         setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
343
344         /* Init lcdc */
345         writel(0, &lcdc->ctrl); /* Disable tcon */
346         writel(0, &lcdc->int0); /* Disable all interrupts */
347
348         /* Disable tcon0 dot clock */
349         clrbits_le32(&lcdc->tcon0_dclk, SUNXI_LCDC_TCON0_DCLK_ENABLE);
350
351         /* Set all io lines to tristate */
352         writel(0xffffffff, &lcdc->tcon0_io_tristate);
353         writel(0xffffffff, &lcdc->tcon1_io_tristate);
354 }
355
356 static void sunxi_lcdc_mode_set(const struct ctfb_res_modes *mode,
357                                 int *clk_div, int *clk_double)
358 {
359         struct sunxi_lcdc_reg * const lcdc =
360                 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
361         int bp, total;
362
363         /* Use tcon1 */
364         clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
365                         SUNXI_LCDC_CTRL_IO_MAP_TCON1);
366
367         /* Enabled, 0x1e start delay */
368         writel(SUNXI_LCDC_TCON1_CTRL_ENABLE |
369                SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(0x1e), &lcdc->tcon1_ctrl);
370
371         writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
372                &lcdc->tcon1_timing_source);
373         writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
374                &lcdc->tcon1_timing_scale);
375         writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
376                &lcdc->tcon1_timing_out);
377
378         bp = mode->hsync_len + mode->left_margin;
379         total = mode->xres + mode->right_margin + bp;
380         writel(SUNXI_LCDC_TCON1_TIMING_H_TOTAL(total) |
381                SUNXI_LCDC_TCON1_TIMING_H_BP(bp), &lcdc->tcon1_timing_h);
382
383         bp = mode->vsync_len + mode->upper_margin;
384         total = mode->yres + mode->lower_margin + bp;
385         writel(SUNXI_LCDC_TCON1_TIMING_V_TOTAL(total) |
386                SUNXI_LCDC_TCON1_TIMING_V_BP(bp), &lcdc->tcon1_timing_v);
387
388         writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
389                &lcdc->tcon1_timing_sync);
390
391         sunxi_lcdc_pll_set(mode->pixclock_khz, clk_div, clk_double);
392 }
393
394 #ifdef CONFIG_MACH_SUN6I
395 static void sunxi_drc_init(void)
396 {
397         struct sunxi_ccm_reg * const ccm =
398                 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
399
400         /* On sun6i the drc must be clocked even when in pass-through mode */
401         setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DRC0);
402         clock_set_de_mod_clock(&ccm->iep_drc0_clk_cfg, 300000000);
403 }
404 #endif
405
406 static void sunxi_hdmi_setup_info_frames(const struct ctfb_res_modes *mode)
407 {
408         struct sunxi_hdmi_reg * const hdmi =
409                 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
410         u8 checksum = 0;
411         u8 avi_info_frame[17] = {
412                 0x82, 0x02, 0x0d, 0x00, 0x12, 0x00, 0x88, 0x00,
413                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
414                 0x00
415         };
416         u8 vendor_info_frame[19] = {
417                 0x81, 0x01, 0x06, 0x29, 0x03, 0x0c, 0x00, 0x40,
418                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
419                 0x00, 0x00, 0x00
420         };
421         int i;
422
423         if (mode->pixclock_khz <= 27000)
424                 avi_info_frame[5] = 0x40; /* SD-modes, ITU601 colorspace */
425         else
426                 avi_info_frame[5] = 0x80; /* HD-modes, ITU709 colorspace */
427
428         if (mode->xres * 100 / mode->yres < 156)
429                 avi_info_frame[5] |= 0x18; /* 4 : 3 */
430         else
431                 avi_info_frame[5] |= 0x28; /* 16 : 9 */
432
433         for (i = 0; i < ARRAY_SIZE(avi_info_frame); i++)
434                 checksum += avi_info_frame[i];
435
436         avi_info_frame[3] = 0x100 - checksum;
437
438         for (i = 0; i < ARRAY_SIZE(avi_info_frame); i++)
439                 writeb(avi_info_frame[i], &hdmi->avi_info_frame[i]);
440
441         writel(SUNXI_HDMI_QCP_PACKET0, &hdmi->qcp_packet0);
442         writel(SUNXI_HDMI_QCP_PACKET1, &hdmi->qcp_packet1);
443
444         for (i = 0; i < ARRAY_SIZE(vendor_info_frame); i++)
445                 writeb(vendor_info_frame[i], &hdmi->vendor_info_frame[i]);
446
447         writel(SUNXI_HDMI_PKT_CTRL0, &hdmi->pkt_ctrl0);
448         writel(SUNXI_HDMI_PKT_CTRL1, &hdmi->pkt_ctrl1);
449
450         setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_HDMI);
451 }
452
453 static void sunxi_hdmi_mode_set(const struct ctfb_res_modes *mode,
454                                 bool hdmi_mode, int clk_div, int clk_double)
455 {
456         struct sunxi_hdmi_reg * const hdmi =
457                 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
458         int x, y;
459
460         /* Write clear interrupt status bits */
461         writel(SUNXI_HDMI_IRQ_STATUS_BITS, &hdmi->irq);
462
463         if (hdmi_mode)
464                 sunxi_hdmi_setup_info_frames(mode);
465
466         /* Init various registers, select pll3 as clock source */
467         writel(SUNXI_HDMI_VIDEO_POL_TX_CLK, &hdmi->video_polarity);
468         writel(SUNXI_HDMI_PAD_CTRL0_RUN, &hdmi->pad_ctrl0);
469         writel(SUNXI_HDMI_PAD_CTRL1, &hdmi->pad_ctrl1);
470         writel(SUNXI_HDMI_PLL_CTRL, &hdmi->pll_ctrl);
471         writel(SUNXI_HDMI_PLL_DBG0_PLL3, &hdmi->pll_dbg0);
472
473         /* Setup clk div and doubler */
474         clrsetbits_le32(&hdmi->pll_ctrl, SUNXI_HDMI_PLL_CTRL_DIV_MASK,
475                         SUNXI_HDMI_PLL_CTRL_DIV(clk_div));
476         if (!clk_double)
477                 setbits_le32(&hdmi->pad_ctrl1, SUNXI_HDMI_PAD_CTRL1_HALVE);
478
479         /* Setup timing registers */
480         writel(SUNXI_HDMI_Y(mode->yres) | SUNXI_HDMI_X(mode->xres),
481                &hdmi->video_size);
482
483         x = mode->hsync_len + mode->left_margin;
484         y = mode->vsync_len + mode->upper_margin;
485         writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_bp);
486
487         x = mode->right_margin;
488         y = mode->lower_margin;
489         writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_fp);
490
491         x = mode->hsync_len;
492         y = mode->vsync_len;
493         writel(SUNXI_HDMI_Y(y) | SUNXI_HDMI_X(x), &hdmi->video_spw);
494
495         if (mode->sync & FB_SYNC_HOR_HIGH_ACT)
496                 setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_HOR);
497
498         if (mode->sync & FB_SYNC_VERT_HIGH_ACT)
499                 setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_VER);
500 }
501
502 static void sunxi_engines_init(void)
503 {
504         sunxi_composer_init();
505         sunxi_lcdc_init();
506 #ifdef CONFIG_MACH_SUN6I
507         sunxi_drc_init();
508 #endif
509 }
510
511 static void sunxi_mode_set(const struct ctfb_res_modes *mode, char *monitor,
512                            unsigned int address)
513 {
514         struct sunxi_de_be_reg * const de_be =
515                 (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
516         struct sunxi_lcdc_reg * const lcdc =
517                 (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
518         struct sunxi_hdmi_reg * const hdmi =
519                 (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
520         int clk_div, clk_double;
521         int retries = 3;
522         bool hdmi_mode = strcmp(monitor, "hdmi") == 0;
523
524 retry:
525         clrbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
526         clrbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
527         clrbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
528
529         sunxi_composer_mode_set(mode, address);
530         sunxi_lcdc_mode_set(mode, &clk_div, &clk_double);
531         sunxi_hdmi_mode_set(mode, hdmi_mode, clk_div, clk_double);
532
533         setbits_le32(&de_be->reg_ctrl, SUNXI_DE_BE_REG_CTRL_LOAD_REGS);
534         setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
535
536         udelay(1000000 / mode->refresh + 500);
537
538         setbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
539
540         udelay(1000000 / mode->refresh + 500);
541
542         setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
543
544         udelay(1000000 / mode->refresh + 500);
545
546         /*
547          * Sometimes the display pipeline does not sync up properly, if
548          * this happens the hdmi fifo underrun or overrun bits are set.
549          */
550         if (readl(&hdmi->irq) &
551             (SUNXI_HDMI_IRQ_STATUS_FIFO_UF | SUNXI_HDMI_IRQ_STATUS_FIFO_OF)) {
552                 if (retries--)
553                         goto retry;
554                 printf("HDMI fifo under or overrun\n");
555         }
556 }
557
558 void *video_hw_init(void)
559 {
560         static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
561         const struct ctfb_res_modes *mode;
562         struct ctfb_res_modes edid_mode;
563         const char *options;
564         unsigned int depth;
565         int ret, hpd, edid;
566         char monitor[16];
567
568         memset(&sunxi_display, 0, sizeof(struct sunxi_display));
569
570         printf("Reserved %dkB of RAM for Framebuffer.\n",
571                CONFIG_SUNXI_FB_SIZE >> 10);
572         gd->fb_base = gd->ram_top;
573
574         video_get_ctfb_res_modes(RES_MODE_1024x768, 24, &mode, &depth, &options);
575         hpd = video_get_option_int(options, "hpd", 1);
576         edid = video_get_option_int(options, "edid", 1);
577         video_get_option_string(options, "monitor", monitor, sizeof(monitor),
578                                 "dvi");
579
580         /* Always call hdp_detect, as it also enables various clocks, etc. */
581         ret = sunxi_hdmi_hpd_detect();
582         if (hpd && !ret) {
583                 sunxi_hdmi_shutdown();
584                 return NULL;
585         }
586         if (ret)
587                 printf("HDMI connected: ");
588
589         /* Check edid if requested and we've a cable plugged in */
590         if (edid && ret) {
591                 if (sunxi_hdmi_edid_get_mode(&edid_mode) == 0)
592                         mode = &edid_mode;
593         }
594
595         if (mode->vmode != FB_VMODE_NONINTERLACED) {
596                 printf("Only non-interlaced modes supported, falling back to 1024x768\n");
597                 mode = &res_mode_init[RES_MODE_1024x768];
598         } else {
599                 printf("Setting up a %dx%d %s console\n",
600                        mode->xres, mode->yres, monitor);
601         }
602
603         sunxi_display.enabled = true;
604         sunxi_engines_init();
605         sunxi_mode_set(mode, monitor, gd->fb_base - CONFIG_SYS_SDRAM_BASE);
606
607         /*
608          * These are the only members of this structure that are used. All the
609          * others are driver specific. There is nothing to decribe pitch or
610          * stride, but we are lucky with our hw.
611          */
612         graphic_device->frameAdrs = gd->fb_base;
613         graphic_device->gdfIndex = GDF_32BIT_X888RGB;
614         graphic_device->gdfBytesPP = 4;
615         graphic_device->winSizeX = mode->xres;
616         graphic_device->winSizeY = mode->yres;
617
618         return graphic_device;
619 }
620
621 /*
622  * Simplefb support.
623  */
624 #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_VIDEO_DT_SIMPLEFB)
625 int sunxi_simplefb_setup(void *blob)
626 {
627         static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
628         int offset, ret;
629
630         if (!sunxi_display.enabled)
631                 return 0;
632
633         /* Find a framebuffer node, with pipeline == "de_be0-lcd0-hdmi" */
634         offset = fdt_node_offset_by_compatible(blob, -1,
635                                                "allwinner,simple-framebuffer");
636         while (offset >= 0) {
637                 ret = fdt_find_string(blob, offset, "allwinner,pipeline",
638                                       "de_be0-lcd0-hdmi");
639                 if (ret == 0)
640                         break;
641                 offset = fdt_node_offset_by_compatible(blob, offset,
642                                                "allwinner,simple-framebuffer");
643         }
644         if (offset < 0) {
645                 eprintf("Cannot setup simplefb: node not found\n");
646                 return 0; /* Keep older kernels working */
647         }
648
649         ret = fdt_setup_simplefb_node(blob, offset, gd->fb_base,
650                         graphic_device->winSizeX, graphic_device->winSizeY,
651                         graphic_device->winSizeX * graphic_device->gdfBytesPP,
652                         "x8r8g8b8");
653         if (ret)
654                 eprintf("Cannot setup simplefb: Error setting properties\n");
655
656         return ret;
657 }
658 #endif /* CONFIG_OF_BOARD_SETUP && CONFIG_VIDEO_DT_SIMPLEFB */