1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
4 * Copyright 2019 Toradex AG
6 * FSL DCU Framebuffer driver
10 #include <asm/cache.h>
14 #include <fdt_support.h>
15 #include <fsl_dcu_fb.h>
20 #include "videomodes.h"
22 /* Convert the X,Y resolution pair into a single number */
23 #define RESOLUTION(x, y) (((u32)(x) << 16) | (y))
25 #ifdef CONFIG_SYS_FSL_DCU_LE
26 #define dcu_read32 in_le32
27 #define dcu_write32 out_le32
28 #elif defined(CONFIG_SYS_FSL_DCU_BE)
29 #define dcu_read32 in_be32
30 #define dcu_write32 out_be32
33 #define DCU_MODE_BLEND_ITER(x) ((x) << 20)
34 #define DCU_MODE_RASTER_EN (1 << 14)
35 #define DCU_MODE_NORMAL 1
36 #define DCU_MODE_COLORBAR 3
37 #define DCU_BGND_R(x) ((x) << 16)
38 #define DCU_BGND_G(x) ((x) << 8)
39 #define DCU_BGND_B(x) (x)
40 #define DCU_DISP_SIZE_DELTA_Y(x) ((x) << 16)
41 #define DCU_DISP_SIZE_DELTA_X(x) (x)
42 #define DCU_HSYN_PARA_BP(x) ((x) << 22)
43 #define DCU_HSYN_PARA_PW(x) ((x) << 11)
44 #define DCU_HSYN_PARA_FP(x) (x)
45 #define DCU_VSYN_PARA_BP(x) ((x) << 22)
46 #define DCU_VSYN_PARA_PW(x) ((x) << 11)
47 #define DCU_VSYN_PARA_FP(x) (x)
48 #define DCU_SYN_POL_INV_PXCK_FALL (1 << 6)
49 #define DCU_SYN_POL_NEG_REMAIN (0 << 5)
50 #define DCU_SYN_POL_INV_VS_LOW (1 << 1)
51 #define DCU_SYN_POL_INV_HS_LOW (1)
52 #define DCU_THRESHOLD_LS_BF_VS(x) ((x) << 16)
53 #define DCU_THRESHOLD_OUT_BUF_HIGH(x) ((x) << 8)
54 #define DCU_THRESHOLD_OUT_BUF_LOW(x) (x)
55 #define DCU_UPDATE_MODE_MODE (1 << 31)
56 #define DCU_UPDATE_MODE_READREG (1 << 30)
58 #define DCU_CTRLDESCLN_1_HEIGHT(x) ((x) << 16)
59 #define DCU_CTRLDESCLN_1_WIDTH(x) (x)
60 #define DCU_CTRLDESCLN_2_POSY(x) ((x) << 16)
61 #define DCU_CTRLDESCLN_2_POSX(x) (x)
62 #define DCU_CTRLDESCLN_4_EN (1 << 31)
63 #define DCU_CTRLDESCLN_4_TILE_EN (1 << 30)
64 #define DCU_CTRLDESCLN_4_DATA_SEL_CLUT (1 << 29)
65 #define DCU_CTRLDESCLN_4_SAFETY_EN (1 << 28)
66 #define DCU_CTRLDESCLN_4_TRANS(x) ((x) << 20)
67 #define DCU_CTRLDESCLN_4_BPP(x) ((x) << 16)
68 #define DCU_CTRLDESCLN_4_RLE_EN (1 << 15)
69 #define DCU_CTRLDESCLN_4_LUOFFS(x) ((x) << 4)
70 #define DCU_CTRLDESCLN_4_BB_ON (1 << 2)
71 #define DCU_CTRLDESCLN_4_AB(x) (x)
72 #define DCU_CTRLDESCLN_5_CKMAX_R(x) ((x) << 16)
73 #define DCU_CTRLDESCLN_5_CKMAX_G(x) ((x) << 8)
74 #define DCU_CTRLDESCLN_5_CKMAX_B(x) (x)
75 #define DCU_CTRLDESCLN_6_CKMIN_R(x) ((x) << 16)
76 #define DCU_CTRLDESCLN_6_CKMIN_G(x) ((x) << 8)
77 #define DCU_CTRLDESCLN_6_CKMIN_B(x) (x)
78 #define DCU_CTRLDESCLN_7_TILE_VER(x) ((x) << 16)
79 #define DCU_CTRLDESCLN_7_TILE_HOR(x) (x)
80 #define DCU_CTRLDESCLN_8_FG_FCOLOR(x) (x)
81 #define DCU_CTRLDESCLN_9_BG_BCOLOR(x) (x)
83 #define BPP_16_RGB565 4
84 #define BPP_24_RGB888 5
85 #define BPP_32_ARGB8888 6
87 DECLARE_GLOBAL_DATA_PTR;
90 * This setting is used for the TWR_LCD_RGB card
92 static struct fb_videomode fsl_dcu_mode_480_272 = {
104 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
105 .vmode = FB_VMODE_NONINTERLACED
109 * This setting is used for Siliconimage SiI9022A HDMI
111 static struct fb_videomode fsl_dcu_cea_mode_640_480 = {
112 .name = "640x480-60",
124 .vmode = FB_VMODE_NONINTERLACED,
127 static struct fb_videomode fsl_dcu_mode_640_480 = {
128 .name = "640x480-60",
140 .vmode = FB_VMODE_NONINTERLACED,
143 static struct fb_videomode fsl_dcu_mode_800_480 = {
144 .name = "800x480-60",
156 .vmode = FB_VMODE_NONINTERLACED,
159 static struct fb_videomode fsl_dcu_mode_1024_600 = {
160 .name = "1024x600-60",
172 .vmode = FB_VMODE_NONINTERLACED,
193 u8 res_064[0x6c-0x64];
194 u32 parr_err_status1;
195 u8 res_070[0x7c-0x70];
196 u32 parr_err_status3;
197 u32 mparr_err_status1;
198 u8 res_084[0x90-0x84];
199 u32 mparr_err_status3;
200 u32 threshold_inp_buf[2];
201 u8 res_09c[0xa0-0x9c];
212 u8 res_0c4[0xcc-0xc8];
215 u8 res_0d4[0x100-0xd4];
222 u8 res_120[0x200-0x120];
223 u32 ctrldescl[DCU_LAYER_MAX_NUM][16];
226 static void reset_total_layers(void)
228 struct dcu_reg *regs = (struct dcu_reg *)CONFIG_SYS_DCU_ADDR;
231 for (i = 0; i < DCU_LAYER_MAX_NUM; i++) {
232 dcu_write32(®s->ctrldescl[i][0], 0);
233 dcu_write32(®s->ctrldescl[i][1], 0);
234 dcu_write32(®s->ctrldescl[i][2], 0);
235 dcu_write32(®s->ctrldescl[i][3], 0);
236 dcu_write32(®s->ctrldescl[i][4], 0);
237 dcu_write32(®s->ctrldescl[i][5], 0);
238 dcu_write32(®s->ctrldescl[i][6], 0);
239 dcu_write32(®s->ctrldescl[i][7], 0);
240 dcu_write32(®s->ctrldescl[i][8], 0);
241 dcu_write32(®s->ctrldescl[i][9], 0);
242 dcu_write32(®s->ctrldescl[i][10], 0);
246 static int layer_ctrldesc_init(struct fb_info fbinfo,
247 int index, u32 pixel_format)
249 struct dcu_reg *regs = (struct dcu_reg *)CONFIG_SYS_DCU_ADDR;
250 unsigned int bpp = BPP_24_RGB888;
252 dcu_write32(®s->ctrldescl[index][0],
253 DCU_CTRLDESCLN_1_HEIGHT(fbinfo.var.yres) |
254 DCU_CTRLDESCLN_1_WIDTH(fbinfo.var.xres));
256 dcu_write32(®s->ctrldescl[index][1],
257 DCU_CTRLDESCLN_2_POSY(0) |
258 DCU_CTRLDESCLN_2_POSX(0));
260 dcu_write32(®s->ctrldescl[index][2],
261 (unsigned int)fbinfo.screen_base);
263 switch (pixel_format) {
271 bpp = BPP_32_ARGB8888;
274 printf("unsupported color depth: %u\n", pixel_format);
277 dcu_write32(®s->ctrldescl[index][3],
278 DCU_CTRLDESCLN_4_EN |
279 DCU_CTRLDESCLN_4_TRANS(0xff) |
280 DCU_CTRLDESCLN_4_BPP(bpp) |
281 DCU_CTRLDESCLN_4_AB(0));
283 dcu_write32(®s->ctrldescl[index][4],
284 DCU_CTRLDESCLN_5_CKMAX_R(0xff) |
285 DCU_CTRLDESCLN_5_CKMAX_G(0xff) |
286 DCU_CTRLDESCLN_5_CKMAX_B(0xff));
287 dcu_write32(®s->ctrldescl[index][5],
288 DCU_CTRLDESCLN_6_CKMIN_R(0) |
289 DCU_CTRLDESCLN_6_CKMIN_G(0) |
290 DCU_CTRLDESCLN_6_CKMIN_B(0));
292 dcu_write32(®s->ctrldescl[index][6],
293 DCU_CTRLDESCLN_7_TILE_VER(0) |
294 DCU_CTRLDESCLN_7_TILE_HOR(0));
296 dcu_write32(®s->ctrldescl[index][7], DCU_CTRLDESCLN_8_FG_FCOLOR(0));
297 dcu_write32(®s->ctrldescl[index][8], DCU_CTRLDESCLN_9_BG_BCOLOR(0));
302 int fsl_dcu_init(struct fb_info *fbinfo, unsigned int xres,
303 unsigned int yres, unsigned int pixel_format)
305 struct dcu_reg *regs = (struct dcu_reg *)CONFIG_SYS_DCU_ADDR;
306 unsigned int div, mode;
308 * When DM_VIDEO is enabled reservation of framebuffer is done
309 * in advance during bind() call.
311 #if !CONFIG_IS_ENABLED(DM_VIDEO)
312 fbinfo->screen_size = fbinfo->var.xres * fbinfo->var.yres *
313 (fbinfo->var.bits_per_pixel / 8);
315 if (fbinfo->screen_size > CONFIG_VIDEO_FSL_DCU_MAX_FB_SIZE_MB) {
316 fbinfo->screen_size = 0;
319 /* Reserve framebuffer at the end of memory */
320 gd->fb_base = gd->bd->bi_dram[0].start +
321 gd->bd->bi_dram[0].size - fbinfo->screen_size;
322 fbinfo->screen_base = (char *)gd->fb_base;
324 memset(fbinfo->screen_base, 0, fbinfo->screen_size);
327 reset_total_layers();
329 dcu_write32(®s->disp_size,
330 DCU_DISP_SIZE_DELTA_Y(fbinfo->var.yres) |
331 DCU_DISP_SIZE_DELTA_X(fbinfo->var.xres / 16));
333 dcu_write32(®s->hsyn_para,
334 DCU_HSYN_PARA_BP(fbinfo->var.left_margin) |
335 DCU_HSYN_PARA_PW(fbinfo->var.hsync_len) |
336 DCU_HSYN_PARA_FP(fbinfo->var.right_margin));
338 dcu_write32(®s->vsyn_para,
339 DCU_VSYN_PARA_BP(fbinfo->var.upper_margin) |
340 DCU_VSYN_PARA_PW(fbinfo->var.vsync_len) |
341 DCU_VSYN_PARA_FP(fbinfo->var.lower_margin));
343 dcu_write32(®s->synpol,
344 DCU_SYN_POL_INV_PXCK_FALL |
345 DCU_SYN_POL_NEG_REMAIN |
346 DCU_SYN_POL_INV_VS_LOW |
347 DCU_SYN_POL_INV_HS_LOW);
349 dcu_write32(®s->bgnd,
350 DCU_BGND_R(0) | DCU_BGND_G(0) | DCU_BGND_B(0));
352 dcu_write32(®s->mode,
353 DCU_MODE_BLEND_ITER(2) |
356 dcu_write32(®s->threshold,
357 DCU_THRESHOLD_LS_BF_VS(0x3) |
358 DCU_THRESHOLD_OUT_BUF_HIGH(0x78) |
359 DCU_THRESHOLD_OUT_BUF_LOW(0));
361 mode = dcu_read32(®s->mode);
362 dcu_write32(®s->mode, mode | DCU_MODE_NORMAL);
364 layer_ctrldesc_init(*fbinfo, 0, pixel_format);
366 div = dcu_set_pixel_clock(fbinfo->var.pixclock);
367 dcu_write32(®s->div_ratio, (div - 1));
369 dcu_write32(®s->update_mode, DCU_UPDATE_MODE_READREG);
374 ulong board_get_usable_ram_top(ulong total_size)
376 return gd->ram_top - CONFIG_VIDEO_FSL_DCU_MAX_FB_SIZE_MB;
379 int fsl_probe_common(struct fb_info *fbinfo, unsigned int *win_x,
383 unsigned int depth = 0, freq = 0;
385 struct fb_videomode *fsl_dcu_mode_db = &fsl_dcu_mode_480_272;
387 if (!video_get_video_mode(win_x, win_y, &depth, &freq,
391 /* Find the monitor port, which is a required option */
395 if (strncmp(options, "monitor=", 8) != 0)
398 switch (RESOLUTION(*win_x, *win_y)) {
399 case RESOLUTION(480, 272):
400 fsl_dcu_mode_db = &fsl_dcu_mode_480_272;
402 case RESOLUTION(640, 480):
403 if (!strncmp(options, "monitor=hdmi", 12))
404 fsl_dcu_mode_db = &fsl_dcu_cea_mode_640_480;
406 fsl_dcu_mode_db = &fsl_dcu_mode_640_480;
408 case RESOLUTION(800, 480):
409 fsl_dcu_mode_db = &fsl_dcu_mode_800_480;
411 case RESOLUTION(1024, 600):
412 fsl_dcu_mode_db = &fsl_dcu_mode_1024_600;
415 printf("unsupported resolution %ux%u\n",
419 fbinfo->var.xres = fsl_dcu_mode_db->xres;
420 fbinfo->var.yres = fsl_dcu_mode_db->yres;
421 fbinfo->var.bits_per_pixel = 32;
422 fbinfo->var.pixclock = fsl_dcu_mode_db->pixclock;
423 fbinfo->var.left_margin = fsl_dcu_mode_db->left_margin;
424 fbinfo->var.right_margin = fsl_dcu_mode_db->right_margin;
425 fbinfo->var.upper_margin = fsl_dcu_mode_db->upper_margin;
426 fbinfo->var.lower_margin = fsl_dcu_mode_db->lower_margin;
427 fbinfo->var.hsync_len = fsl_dcu_mode_db->hsync_len;
428 fbinfo->var.vsync_len = fsl_dcu_mode_db->vsync_len;
429 fbinfo->var.sync = fsl_dcu_mode_db->sync;
430 fbinfo->var.vmode = fsl_dcu_mode_db->vmode;
431 fbinfo->fix.line_length = fbinfo->var.xres *
432 fbinfo->var.bits_per_pixel / 8;
434 return platform_dcu_init(fbinfo, *win_x, *win_y,
435 options + 8, fsl_dcu_mode_db);
438 #ifndef CONFIG_DM_VIDEO
439 static struct fb_info info;
441 #if defined(CONFIG_OF_BOARD_SETUP)
442 int fsl_dcu_fixedfb_setup(void *blob)
447 start = gd->bd->bi_dram[0].start;
448 size = gd->bd->bi_dram[0].size - info.screen_size;
451 * Align size on section size (1 MiB).
454 ret = fdt_fixup_memory_banks(blob, &start, &size, 1);
456 eprintf("Cannot setup fb: Error reserving memory\n");
464 void *video_hw_init(void)
466 static GraphicDevice ctfb;
468 if (fsl_probe_common(&info, &ctfb.winSizeX, &ctfb.winSizeY) < 0)
471 ctfb.frameAdrs = (unsigned int)info.screen_base;
472 ctfb.plnSizeX = ctfb.winSizeX;
473 ctfb.plnSizeY = ctfb.winSizeY;
476 ctfb.gdfIndex = GDF_32BIT_X888RGB;
478 ctfb.memSize = info.screen_size;
483 #else /* ifndef CONFIG_DM_VIDEO */
485 static int fsl_dcu_video_probe(struct udevice *dev)
487 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
488 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
489 struct fb_info fbinfo = { 0 };
492 u32 fb_start, fb_end;
495 fb_start = plat->base & ~(MMU_SECTION_SIZE - 1);
496 fb_end = plat->base + plat->size;
497 fb_end = ALIGN(fb_end, 1 << MMU_SECTION_SHIFT);
499 fbinfo.screen_base = (char *)fb_start;
500 fbinfo.screen_size = plat->size;
502 ret = fsl_probe_common(&fbinfo, &win_x, &win_y);
506 uc_priv->bpix = VIDEO_BPP32;
507 uc_priv->xsize = win_x;
508 uc_priv->ysize = win_y;
510 /* Enable dcache for the frame buffer */
511 mmu_set_region_dcache_behaviour(fb_start, fb_end - fb_start,
513 video_set_flush_dcache(dev, true);
517 static int fsl_dcu_video_bind(struct udevice *dev)
519 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
522 unsigned int depth = 0, freq = 0;
526 ret = video_get_video_mode(&win_x, &win_y, &depth, &freq, &options);
530 plat->size = win_x * win_y * 32;
535 static const struct udevice_id fsl_dcu_video_ids[] = {
536 { .compatible = "fsl,vf610-dcu" },
540 U_BOOT_DRIVER(fsl_dcu_video) = {
541 .name = "fsl_dcu_video",
543 .of_match = fsl_dcu_video_ids,
544 .bind = fsl_dcu_video_bind,
545 .probe = fsl_dcu_video_probe,
546 .flags = DM_FLAG_PRE_RELOC,
548 #endif /* ifndef CONFIG_DM_VIDEO */