blob: b657e163f06abbeee5b5585c02b63a2876129865 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jernej Skrabec8d91b462017-03-27 19:22:32 +02002/*
3 * Allwinner DE2 display driver
4 *
5 * (C) Copyright 2017 Jernej Skrabec <jernej.skrabec@siol.net>
Jernej Skrabec8d91b462017-03-27 19:22:32 +02006 */
7
8#include <common.h>
9#include <display.h>
10#include <dm.h>
11#include <edid.h>
Emmanuel Vadot0219a0c2018-05-04 10:26:55 +020012#include <efi_loader.h>
Icenowy Zheng82576de2017-10-26 11:14:47 +080013#include <fdtdec.h>
14#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060016#include <part.h>
Jernej Skrabec8d91b462017-03-27 19:22:32 +020017#include <video.h>
18#include <asm/global_data.h>
19#include <asm/io.h>
20#include <asm/arch/clock.h>
21#include <asm/arch/display2.h>
22#include <dm/device-internal.h>
23#include <dm/uclass-internal.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Icenowy Zheng82576de2017-10-26 11:14:47 +080025#include "simplefb_common.h"
Jernej Skrabec8d91b462017-03-27 19:22:32 +020026
27DECLARE_GLOBAL_DATA_PTR;
28
29enum {
30 /* Maximum LCD size we support */
31 LCD_MAX_WIDTH = 3840,
32 LCD_MAX_HEIGHT = 2160,
33 LCD_MAX_LOG2_BPP = VIDEO_BPP32,
34};
35
36static void sunxi_de2_composer_init(void)
37{
38 struct sunxi_ccm_reg * const ccm =
39 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
40
41#ifdef CONFIG_MACH_SUN50I
42 u32 reg_value;
43
44 /* set SRAM for video use (A64 only) */
45 reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
46 reg_value &= ~(0x01 << 24);
47 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
48#endif
49
50 clock_set_pll10(432000000);
51
52 /* Set DE parent to pll10 */
53 clrsetbits_le32(&ccm->de_clk_cfg, CCM_DE2_CTRL_PLL_MASK,
54 CCM_DE2_CTRL_PLL10);
55
56 /* Set ahb gating to pass */
57 setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DE);
58 setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE);
59
60 /* Clock on */
61 setbits_le32(&ccm->de_clk_cfg, CCM_DE2_CTRL_GATE);
62}
63
64static void sunxi_de2_mode_set(int mux, const struct display_timing *mode,
Jernej Skrabecb8f7cab2017-05-19 17:41:16 +020065 int bpp, ulong address, bool is_composite)
Jernej Skrabec8d91b462017-03-27 19:22:32 +020066{
67 ulong de_mux_base = (mux == 0) ?
68 SUNXI_DE2_MUX0_BASE : SUNXI_DE2_MUX1_BASE;
69 struct de_clk * const de_clk_regs =
70 (struct de_clk *)(SUNXI_DE2_BASE);
71 struct de_glb * const de_glb_regs =
72 (struct de_glb *)(de_mux_base +
73 SUNXI_DE2_MUX_GLB_REGS);
74 struct de_bld * const de_bld_regs =
75 (struct de_bld *)(de_mux_base +
76 SUNXI_DE2_MUX_BLD_REGS);
77 struct de_ui * const de_ui_regs =
78 (struct de_ui *)(de_mux_base +
79 SUNXI_DE2_MUX_CHAN_REGS +
80 SUNXI_DE2_MUX_CHAN_SZ * 1);
Jernej Skrabecb8f7cab2017-05-19 17:41:16 +020081 struct de_csc * const de_csc_regs =
82 (struct de_csc *)(de_mux_base +
83 SUNXI_DE2_MUX_DCSC_REGS);
Jernej Skrabec8d91b462017-03-27 19:22:32 +020084 u32 size = SUNXI_DE2_WH(mode->hactive.typ, mode->vactive.typ);
85 int channel;
86 u32 format;
87
88 /* enable clock */
89#ifdef CONFIG_MACH_SUN8I_H3
90 setbits_le32(&de_clk_regs->rst_cfg, (mux == 0) ? 1 : 4);
91#else
92 setbits_le32(&de_clk_regs->rst_cfg, BIT(mux));
93#endif
94 setbits_le32(&de_clk_regs->gate_cfg, BIT(mux));
95 setbits_le32(&de_clk_regs->bus_cfg, BIT(mux));
96
97 clrbits_le32(&de_clk_regs->sel_cfg, 1);
98
99 writel(SUNXI_DE2_MUX_GLB_CTL_EN, &de_glb_regs->ctl);
100 writel(0, &de_glb_regs->status);
101 writel(1, &de_glb_regs->dbuff);
102 writel(size, &de_glb_regs->size);
103
104 for (channel = 0; channel < 4; channel++) {
105 void *ch = (void *)(de_mux_base + SUNXI_DE2_MUX_CHAN_REGS +
106 SUNXI_DE2_MUX_CHAN_SZ * channel);
107 memset(ch, 0, (channel == 0) ?
108 sizeof(struct de_vi) : sizeof(struct de_ui));
109 }
110 memset(de_bld_regs, 0, sizeof(struct de_bld));
111
112 writel(0x00000101, &de_bld_regs->fcolor_ctl);
113
114 writel(1, &de_bld_regs->route);
115
116 writel(0, &de_bld_regs->premultiply);
117 writel(0xff000000, &de_bld_regs->bkcolor);
118
119 writel(0x03010301, &de_bld_regs->bld_mode[0]);
120
121 writel(size, &de_bld_regs->output_size);
122 writel(mode->flags & DISPLAY_FLAGS_INTERLACED ? 2 : 0,
123 &de_bld_regs->out_ctl);
124 writel(0, &de_bld_regs->ck_ctl);
125
126 writel(0xff000000, &de_bld_regs->attr[0].fcolor);
127 writel(size, &de_bld_regs->attr[0].insize);
128
129 /* Disable all other units */
130 writel(0, de_mux_base + SUNXI_DE2_MUX_VSU_REGS);
131 writel(0, de_mux_base + SUNXI_DE2_MUX_GSU1_REGS);
132 writel(0, de_mux_base + SUNXI_DE2_MUX_GSU2_REGS);
133 writel(0, de_mux_base + SUNXI_DE2_MUX_GSU3_REGS);
134 writel(0, de_mux_base + SUNXI_DE2_MUX_FCE_REGS);
135 writel(0, de_mux_base + SUNXI_DE2_MUX_BWS_REGS);
136 writel(0, de_mux_base + SUNXI_DE2_MUX_LTI_REGS);
137 writel(0, de_mux_base + SUNXI_DE2_MUX_PEAK_REGS);
138 writel(0, de_mux_base + SUNXI_DE2_MUX_ASE_REGS);
139 writel(0, de_mux_base + SUNXI_DE2_MUX_FCC_REGS);
Jernej Skrabecb8f7cab2017-05-19 17:41:16 +0200140
141 if (is_composite) {
142 /* set CSC coefficients */
143 writel(0x107, &de_csc_regs->coef11);
144 writel(0x204, &de_csc_regs->coef12);
145 writel(0x64, &de_csc_regs->coef13);
146 writel(0x4200, &de_csc_regs->coef14);
147 writel(0x1f68, &de_csc_regs->coef21);
148 writel(0x1ed6, &de_csc_regs->coef22);
149 writel(0x1c2, &de_csc_regs->coef23);
150 writel(0x20200, &de_csc_regs->coef24);
151 writel(0x1c2, &de_csc_regs->coef31);
152 writel(0x1e87, &de_csc_regs->coef32);
153 writel(0x1fb7, &de_csc_regs->coef33);
154 writel(0x20200, &de_csc_regs->coef34);
155
156 /* enable CSC unit */
157 writel(1, &de_csc_regs->csc_ctl);
158 } else {
159 writel(0, &de_csc_regs->csc_ctl);
160 }
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200161
162 switch (bpp) {
163 case 16:
164 format = SUNXI_DE2_UI_CFG_ATTR_FMT(SUNXI_DE2_FORMAT_RGB_565);
165 break;
166 case 32:
167 default:
168 format = SUNXI_DE2_UI_CFG_ATTR_FMT(SUNXI_DE2_FORMAT_XRGB_8888);
169 break;
170 }
171
172 writel(SUNXI_DE2_UI_CFG_ATTR_EN | format, &de_ui_regs->cfg[0].attr);
173 writel(size, &de_ui_regs->cfg[0].size);
174 writel(0, &de_ui_regs->cfg[0].coord);
175 writel((bpp / 8) * mode->hactive.typ, &de_ui_regs->cfg[0].pitch);
176 writel(address, &de_ui_regs->cfg[0].top_laddr);
177 writel(size, &de_ui_regs->ovl_size);
178
179 /* apply settings */
180 writel(1, &de_glb_regs->dbuff);
181}
182
183static int sunxi_de2_init(struct udevice *dev, ulong fbbase,
184 enum video_log2_bpp l2bpp,
Jernej Skrabecb8f7cab2017-05-19 17:41:16 +0200185 struct udevice *disp, int mux, bool is_composite)
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200186{
187 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
188 struct display_timing timing;
189 struct display_plat *disp_uc_plat;
190 int ret;
191
192 disp_uc_plat = dev_get_uclass_platdata(disp);
193 debug("Using device '%s', disp_uc_priv=%p\n", disp->name, disp_uc_plat);
194 if (display_in_use(disp)) {
195 debug(" - device in use\n");
196 return -EBUSY;
197 }
198
199 disp_uc_plat->source_id = mux;
200
201 ret = device_probe(disp);
202 if (ret) {
203 debug("%s: device '%s' display won't probe (ret=%d)\n",
204 __func__, dev->name, ret);
205 return ret;
206 }
207
208 ret = display_read_timing(disp, &timing);
209 if (ret) {
210 debug("%s: Failed to read timings\n", __func__);
211 return ret;
212 }
213
214 sunxi_de2_composer_init();
Jernej Skrabecb8f7cab2017-05-19 17:41:16 +0200215 sunxi_de2_mode_set(mux, &timing, 1 << l2bpp, fbbase, is_composite);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200216
217 ret = display_enable(disp, 1 << l2bpp, &timing);
218 if (ret) {
219 debug("%s: Failed to enable display\n", __func__);
220 return ret;
221 }
222
223 uc_priv->xsize = timing.hactive.typ;
224 uc_priv->ysize = timing.vactive.typ;
225 uc_priv->bpix = l2bpp;
226 debug("fb=%lx, size=%d %d\n", fbbase, uc_priv->xsize, uc_priv->ysize);
227
Emmanuel Vadot0219a0c2018-05-04 10:26:55 +0200228#ifdef CONFIG_EFI_LOADER
229 efi_add_memory_map(fbbase,
Michael Walle282d3862020-05-17 12:29:19 +0200230 timing.hactive.typ * timing.vactive.typ *
231 (1 << l2bpp) / 8,
232 EFI_RESERVED_MEMORY_TYPE);
Emmanuel Vadot0219a0c2018-05-04 10:26:55 +0200233#endif
234
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200235 return 0;
236}
237
238static int sunxi_de2_probe(struct udevice *dev)
239{
240 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
241 struct udevice *disp;
242 int ret;
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200243
244 /* Before relocation we don't need to do anything */
245 if (!(gd->flags & GD_FLG_RELOC))
246 return 0;
247
248 ret = uclass_find_device_by_name(UCLASS_DISPLAY,
Vasily Khoruzhick2f0b6e52017-10-26 21:51:52 -0700249 "sunxi_lcd", &disp);
250 if (!ret) {
251 int mux;
252
253 mux = 0;
254
255 ret = sunxi_de2_init(dev, plat->base, VIDEO_BPP32, disp, mux,
256 false);
257 if (!ret) {
258 video_set_flush_dcache(dev, 1);
259 return 0;
260 }
261 }
262
263 debug("%s: lcd display not found (ret=%d)\n", __func__, ret);
264
265 ret = uclass_find_device_by_name(UCLASS_DISPLAY,
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200266 "sunxi_dw_hdmi", &disp);
Jernej Skrabecb8f7cab2017-05-19 17:41:16 +0200267 if (!ret) {
268 int mux;
269 if (IS_ENABLED(CONFIG_MACH_SUNXI_H3_H5))
270 mux = 0;
271 else
272 mux = 1;
273
274 ret = sunxi_de2_init(dev, plat->base, VIDEO_BPP32, disp, mux,
275 false);
276 if (!ret) {
277 video_set_flush_dcache(dev, 1);
278 return 0;
279 }
280 }
281
282 debug("%s: hdmi display not found (ret=%d)\n", __func__, ret);
283
284 ret = uclass_find_device_by_name(UCLASS_DISPLAY,
285 "sunxi_tve", &disp);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200286 if (ret) {
Jernej Skrabecb8f7cab2017-05-19 17:41:16 +0200287 debug("%s: tv not found (ret=%d)\n", __func__, ret);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200288 return ret;
289 }
290
Jernej Skrabecb8f7cab2017-05-19 17:41:16 +0200291 ret = sunxi_de2_init(dev, plat->base, VIDEO_BPP32, disp, 1, true);
Jernej Skrabec8d91b462017-03-27 19:22:32 +0200292 if (ret)
293 return ret;
294
295 video_set_flush_dcache(dev, 1);
296
297 return 0;
298}
299
300static int sunxi_de2_bind(struct udevice *dev)
301{
302 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
303
304 plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
305 (1 << LCD_MAX_LOG2_BPP) / 8;
306
307 return 0;
308}
309
310static const struct video_ops sunxi_de2_ops = {
311};
312
313U_BOOT_DRIVER(sunxi_de2) = {
314 .name = "sunxi_de2",
315 .id = UCLASS_VIDEO,
316 .ops = &sunxi_de2_ops,
317 .bind = sunxi_de2_bind,
318 .probe = sunxi_de2_probe,
319 .flags = DM_FLAG_PRE_RELOC,
320};
321
322U_BOOT_DEVICE(sunxi_de2) = {
323 .name = "sunxi_de2"
324};
Icenowy Zheng82576de2017-10-26 11:14:47 +0800325
326/*
327 * Simplefb support.
328 */
329#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_VIDEO_DT_SIMPLEFB)
330int sunxi_simplefb_setup(void *blob)
331{
Icenowy Zhengf6dc7712017-11-01 22:18:07 +0800332 struct udevice *de2, *hdmi, *lcd;
Icenowy Zheng82576de2017-10-26 11:14:47 +0800333 struct video_priv *de2_priv;
334 struct video_uc_platdata *de2_plat;
335 int mux;
336 int offset, ret;
337 u64 start, size;
338 const char *pipeline = NULL;
339
340 debug("Setting up simplefb\n");
341
342 if (IS_ENABLED(CONFIG_MACH_SUNXI_H3_H5))
343 mux = 0;
344 else
345 mux = 1;
346
347 /* Skip simplefb setting if DE2 / HDMI is not present */
348 ret = uclass_find_device_by_name(UCLASS_VIDEO,
349 "sunxi_de2", &de2);
350 if (ret) {
351 debug("DE2 not present\n");
352 return 0;
Icenowy Zheng43768b22018-07-27 23:50:53 +0800353 } else if (!device_active(de2)) {
354 debug("DE2 present but not probed\n");
355 return 0;
Icenowy Zheng82576de2017-10-26 11:14:47 +0800356 }
357
358 ret = uclass_find_device_by_name(UCLASS_DISPLAY,
359 "sunxi_dw_hdmi", &hdmi);
360 if (ret) {
361 debug("HDMI not present\n");
Icenowy Zhengde8e06f2017-11-01 22:18:06 +0800362 } else if (device_active(hdmi)) {
363 if (mux == 0)
364 pipeline = "mixer0-lcd0-hdmi";
365 else
366 pipeline = "mixer1-lcd1-hdmi";
367 } else {
368 debug("HDMI present but not probed\n");
Icenowy Zheng82576de2017-10-26 11:14:47 +0800369 }
370
Icenowy Zhengf6dc7712017-11-01 22:18:07 +0800371 ret = uclass_find_device_by_name(UCLASS_DISPLAY,
372 "sunxi_lcd", &lcd);
373 if (ret)
374 debug("LCD not present\n");
375 else if (device_active(lcd))
376 pipeline = "mixer0-lcd0";
377 else
378 debug("LCD present but not probed\n");
379
Icenowy Zhengde8e06f2017-11-01 22:18:06 +0800380 if (!pipeline) {
381 debug("No active display present\n");
382 return 0;
383 }
Icenowy Zheng82576de2017-10-26 11:14:47 +0800384
385 de2_priv = dev_get_uclass_priv(de2);
386 de2_plat = dev_get_uclass_platdata(de2);
387
388 offset = sunxi_simplefb_fdt_match(blob, pipeline);
389 if (offset < 0) {
390 eprintf("Cannot setup simplefb: node not found\n");
391 return 0; /* Keep older kernels working */
392 }
393
394 start = gd->bd->bi_dram[0].start;
395 size = de2_plat->base - start;
396 ret = fdt_fixup_memory_banks(blob, &start, &size, 1);
397 if (ret) {
398 eprintf("Cannot setup simplefb: Error reserving memory\n");
399 return ret;
400 }
401
402 ret = fdt_setup_simplefb_node(blob, offset, de2_plat->base,
403 de2_priv->xsize, de2_priv->ysize,
404 VNBYTES(de2_priv->bpix) * de2_priv->xsize,
405 "x8r8g8b8");
406 if (ret)
407 eprintf("Cannot setup simplefb: Error setting properties\n");
408
409 return ret;
410}
411#endif /* CONFIG_OF_BOARD_SETUP && CONFIG_VIDEO_DT_SIMPLEFB */