1 // SPDX-License-Identifier: GPL-2.0+
6 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
8 * MX51 Linux framebuffer:
10 * (C) Copyright 2004-2010 Freescale Semiconductor, Inc.
14 #include <linux/errno.h>
15 #include <asm/global_data.h>
16 #include <linux/string.h>
17 #include <linux/list.h>
20 #include <asm/mach-imx/video.h>
23 #include "videomodes.h"
31 DECLARE_GLOBAL_DATA_PTR;
33 static int mxcfb_map_video_memory(struct fb_info *fbi);
34 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
37 static GraphicDevice panel;
38 static struct fb_videomode const *gmode;
40 static uint32_t gpixfmt;
42 static void fb_videomode_to_var(struct fb_var_screeninfo *var,
43 const struct fb_videomode *mode)
45 var->xres = mode->xres;
46 var->yres = mode->yres;
47 var->xres_virtual = mode->xres;
48 var->yres_virtual = mode->yres;
51 var->pixclock = mode->pixclock;
52 var->left_margin = mode->left_margin;
53 var->right_margin = mode->right_margin;
54 var->upper_margin = mode->upper_margin;
55 var->lower_margin = mode->lower_margin;
56 var->hsync_len = mode->hsync_len;
57 var->vsync_len = mode->vsync_len;
58 var->sync = mode->sync;
59 var->vmode = mode->vmode & FB_VMODE_MASK;
63 * Structure containing the MXC specific framebuffer information.
70 unsigned char overlay;
71 unsigned char alpha_chan_en;
72 dma_addr_t alpha_phy_addr0;
73 dma_addr_t alpha_phy_addr1;
74 void *alpha_virt_addr0;
75 void *alpha_virt_addr1;
76 uint32_t alpha_mem_len;
78 uint32_t cur_ipu_alpha_buf;
80 u32 pseudo_palette[16];
90 static unsigned long default_bpp = 16;
91 static unsigned char g_dp_in_use;
92 static struct fb_info *mxcfb_info[3];
93 static int ext_clk_used;
95 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
99 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
102 return fbi->var.nonstd;
104 switch (fbi->var.bits_per_pixel) {
106 pixfmt = IPU_PIX_FMT_BGR24;
109 pixfmt = IPU_PIX_FMT_BGR32;
112 pixfmt = IPU_PIX_FMT_RGB565;
119 * Set fixed framebuffer parameters based on variable settings.
121 * @param info framebuffer information pointer
123 static int mxcfb_set_fix(struct fb_info *info)
125 struct fb_fix_screeninfo *fix = &info->fix;
126 struct fb_var_screeninfo *var = &info->var;
128 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
130 fix->type = FB_TYPE_PACKED_PIXELS;
131 fix->accel = FB_ACCEL_NONE;
132 fix->visual = FB_VISUAL_TRUECOLOR;
139 static int setup_disp_channel1(struct fb_info *fbi)
141 ipu_channel_params_t params;
142 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
144 memset(¶ms, 0, sizeof(params));
145 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
147 debug("%s called\n", __func__);
149 * Assuming interlaced means yuv output, below setting also
150 * valid for mem_dc_sync. FG should have the same vmode as BG.
152 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
153 params.mem_dp_bg_sync.interlaced = 1;
154 params.mem_dp_bg_sync.out_pixel_fmt =
157 if (mxc_fbi->ipu_di_pix_fmt) {
158 params.mem_dp_bg_sync.out_pixel_fmt =
159 mxc_fbi->ipu_di_pix_fmt;
161 params.mem_dp_bg_sync.out_pixel_fmt =
165 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
166 if (mxc_fbi->alpha_chan_en)
167 params.mem_dp_bg_sync.alpha_chan_en = 1;
169 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms);
174 static int setup_disp_channel2(struct fb_info *fbi)
177 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
179 mxc_fbi->cur_ipu_buf = 1;
180 if (mxc_fbi->alpha_chan_en)
181 mxc_fbi->cur_ipu_alpha_buf = 1;
183 fbi->var.xoffset = fbi->var.yoffset = 0;
185 debug("%s: %x %d %d %d %lx %lx\n",
190 fbi->fix.line_length,
192 fbi->fix.smem_start +
193 (fbi->fix.line_length * fbi->var.yres));
195 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
197 fbi->var.xres, fbi->var.yres,
198 fbi->fix.line_length,
199 fbi->fix.smem_start +
200 (fbi->fix.line_length * fbi->var.yres),
204 printf("ipu_init_channel_buffer error %d\n", retval);
210 * Set framebuffer parameters and change the operating mode.
212 * @param info framebuffer information pointer
214 static int mxcfb_set_par(struct fb_info *fbi)
218 ipu_di_signal_cfg_t sig_cfg;
219 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
220 uint32_t out_pixel_fmt;
222 ipu_disable_channel(mxc_fbi->ipu_ch);
223 ipu_uninit_channel(mxc_fbi->ipu_ch);
226 mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
227 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
228 if (fbi->fix.smem_start)
229 mxcfb_unmap_video_memory(fbi);
231 if (mxcfb_map_video_memory(fbi) < 0)
235 setup_disp_channel1(fbi);
237 memset(&sig_cfg, 0, sizeof(sig_cfg));
238 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
239 sig_cfg.interlaced = 1;
240 out_pixel_fmt = IPU_PIX_FMT_YUV444;
242 if (mxc_fbi->ipu_di_pix_fmt)
243 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
245 out_pixel_fmt = IPU_PIX_FMT_RGB666;
247 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
248 sig_cfg.odd_field_first = 1;
249 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
251 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
252 sig_cfg.Hsync_pol = 1;
253 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
254 sig_cfg.Vsync_pol = 1;
255 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
257 if (fbi->var.sync & FB_SYNC_DATA_INVERT)
258 sig_cfg.data_pol = 1;
259 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
260 sig_cfg.enable_pol = 1;
261 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
262 sig_cfg.clkidle_en = 1;
264 debug("pixclock = %lu Hz\n", PICOS2KHZ(fbi->var.pixclock) * 1000UL);
266 if (ipu_init_sync_panel(mxc_fbi->ipu_di,
267 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
268 fbi->var.xres, fbi->var.yres,
270 fbi->var.left_margin,
272 fbi->var.right_margin,
273 fbi->var.upper_margin,
275 fbi->var.lower_margin,
277 puts("mxcfb: Error initializing panel.\n");
281 retval = setup_disp_channel2(fbi);
285 if (mxc_fbi->blank == FB_BLANK_UNBLANK)
286 ipu_enable_channel(mxc_fbi->ipu_ch);
292 * Check framebuffer variable parameters and adjust to valid values.
294 * @param var framebuffer variable parameters
296 * @param info framebuffer information pointer
298 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
303 if (var->xres_virtual < var->xres)
304 var->xres_virtual = var->xres;
305 if (var->yres_virtual < var->yres)
306 var->yres_virtual = var->yres;
308 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
309 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
310 var->bits_per_pixel = default_bpp;
312 switch (var->bits_per_pixel) {
316 var->red.msb_right = 0;
318 var->green.length = 3;
319 var->green.offset = 2;
320 var->green.msb_right = 0;
322 var->blue.length = 2;
323 var->blue.offset = 0;
324 var->blue.msb_right = 0;
326 var->transp.length = 0;
327 var->transp.offset = 0;
328 var->transp.msb_right = 0;
332 var->red.offset = 11;
333 var->red.msb_right = 0;
335 var->green.length = 6;
336 var->green.offset = 5;
337 var->green.msb_right = 0;
339 var->blue.length = 5;
340 var->blue.offset = 0;
341 var->blue.msb_right = 0;
343 var->transp.length = 0;
344 var->transp.offset = 0;
345 var->transp.msb_right = 0;
349 var->red.offset = 16;
350 var->red.msb_right = 0;
352 var->green.length = 8;
353 var->green.offset = 8;
354 var->green.msb_right = 0;
356 var->blue.length = 8;
357 var->blue.offset = 0;
358 var->blue.msb_right = 0;
360 var->transp.length = 0;
361 var->transp.offset = 0;
362 var->transp.msb_right = 0;
366 var->red.offset = 16;
367 var->red.msb_right = 0;
369 var->green.length = 8;
370 var->green.offset = 8;
371 var->green.msb_right = 0;
373 var->blue.length = 8;
374 var->blue.offset = 0;
375 var->blue.msb_right = 0;
377 var->transp.length = 8;
378 var->transp.offset = 24;
379 var->transp.msb_right = 0;
383 if (var->pixclock < 1000) {
384 htotal = var->xres + var->right_margin + var->hsync_len +
386 vtotal = var->yres + var->lower_margin + var->vsync_len +
388 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
389 var->pixclock = KHZ2PICOS(var->pixclock);
390 printf("pixclock set for 60Hz refresh = %u ps\n",
401 static int mxcfb_map_video_memory(struct fb_info *fbi)
403 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
404 fbi->fix.smem_len = fbi->var.yres_virtual *
405 fbi->fix.line_length;
407 fbi->fix.smem_len = roundup(fbi->fix.smem_len, ARCH_DMA_MINALIGN);
409 #if CONFIG_IS_ENABLED(DM_VIDEO)
410 fbi->screen_base = (char *)gd->video_bottom;
412 fbi->screen_base = (char *)memalign(ARCH_DMA_MINALIGN,
416 fbi->fix.smem_start = (unsigned long)fbi->screen_base;
417 if (fbi->screen_base == 0) {
418 puts("Unable to allocate framebuffer memory\n");
419 fbi->fix.smem_len = 0;
420 fbi->fix.smem_start = 0;
424 debug("allocated fb @ paddr=0x%08X, size=%d.\n",
425 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
427 fbi->screen_size = fbi->fix.smem_len;
429 #if CONFIG_IS_ENABLED(VIDEO)
430 gd->fb_base = fbi->fix.smem_start;
433 /* Clear the screen */
434 memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
439 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
441 fbi->screen_base = 0;
442 fbi->fix.smem_start = 0;
443 fbi->fix.smem_len = 0;
448 * Initializes the framebuffer information pointer. After allocating
449 * sufficient memory for the framebuffer structure, the fields are
450 * filled with custom information passed in from the configurable
451 * structures. This includes information such as bits per pixel,
452 * color maps, screen width/height and RGBA offsets.
454 * @return Framebuffer structure initialized with our information
456 static struct fb_info *mxcfb_init_fbinfo(void)
458 #define BYTES_PER_LONG 4
459 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
461 struct mxcfb_info *mxcfbi;
463 int size = sizeof(struct mxcfb_info) + PADDING +
464 sizeof(struct fb_info);
466 debug("%s: %d %d %d %d\n",
470 sizeof(struct mxcfb_info),
471 sizeof(struct fb_info));
473 * Allocate sufficient memory for the fb structure
482 fbi = (struct fb_info *)p;
483 fbi->par = p + sizeof(struct fb_info) + PADDING;
485 mxcfbi = (struct mxcfb_info *)fbi->par;
486 debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
487 (unsigned int)fbi, (unsigned int)mxcfbi);
489 fbi->var.activate = FB_ACTIVATE_NOW;
491 fbi->flags = FBINFO_FLAG_DEFAULT;
492 fbi->pseudo_palette = mxcfbi->pseudo_palette;
498 * Probe routine for the framebuffer driver. It is called during the
499 * driver binding process. The following functions are performed in
500 * this routine: Framebuffer initialization, Memory allocation and
501 * mapping, Framebuffer registration, IPU initialization.
503 * @return Appropriate error code to the kernel common code
505 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
506 struct fb_videomode const *mode)
509 struct mxcfb_info *mxcfbi;
513 * Initialize FB structures
515 fbi = mxcfb_init_fbinfo();
520 mxcfbi = (struct mxcfb_info *)fbi->par;
523 mxcfbi->ipu_ch = MEM_BG_SYNC;
524 mxcfbi->blank = FB_BLANK_UNBLANK;
526 mxcfbi->ipu_ch = MEM_DC_SYNC;
527 mxcfbi->blank = FB_BLANK_POWERDOWN;
530 mxcfbi->ipu_di = disp;
532 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
533 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
534 strcpy(fbi->fix.id, "DISP3 BG");
538 mxcfb_info[mxcfbi->ipu_di] = fbi;
540 /* Need dummy values until real panel is configured */
542 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
543 fb_videomode_to_var(&fbi->var, mode);
544 fbi->var.bits_per_pixel = 16;
545 fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
546 fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
548 mxcfb_check_var(&fbi->var, fbi);
550 /* Default Y virtual size is 2x panel size */
551 fbi->var.yres_virtual = fbi->var.yres * 2;
555 /* allocate fb first */
556 if (mxcfb_map_video_memory(fbi) < 0)
561 panel.winSizeX = mode->xres;
562 panel.winSizeY = mode->yres;
563 panel.plnSizeX = mode->xres;
564 panel.plnSizeY = mode->yres;
566 panel.frameAdrs = (u32)fbi->screen_base;
567 panel.memSize = fbi->screen_size;
569 panel.gdfBytesPP = 2;
570 panel.gdfIndex = GDF_16BIT_565RGB;
572 ipu_dump_registers();
580 void ipuv3_fb_shutdown(void)
583 struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT;
585 if (!ipu_clk_enabled())
588 for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) {
589 struct fb_info *fbi = mxcfb_info[i];
591 struct mxcfb_info *mxc_fbi = fbi->par;
592 ipu_disable_channel(mxc_fbi->ipu_ch);
593 ipu_uninit_channel(mxc_fbi->ipu_ch);
596 for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) {
597 __raw_writel(__raw_readl(&stat->int_stat[i]),
602 void *video_hw_init(void)
608 puts("Error initializing IPU\n");
610 ret = mxcfb_probe(gpixfmt, gdisp, gmode);
611 debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
613 return (void *)&panel;
616 int ipuv3_fb_init(struct fb_videomode const *mode,
627 #if CONFIG_IS_ENABLED(DM_VIDEO)
629 /* Maximum display size we support */
630 LCD_MAX_WIDTH = 1920,
631 LCD_MAX_HEIGHT = 1080,
632 LCD_MAX_LOG2_BPP = VIDEO_BPP16,
635 static int ipuv3_video_probe(struct udevice *dev)
637 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
638 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
639 u32 fb_start, fb_end;
642 debug("%s() plat: base 0x%lx, size 0x%x\n",
643 __func__, plat->base, plat->size);
649 ret = ipu_displays_init();
653 ret = mxcfb_probe(gpixfmt, gdisp, gmode);
657 uc_priv->xsize = gmode->xres;
658 uc_priv->ysize = gmode->yres;
659 uc_priv->bpix = LCD_MAX_LOG2_BPP;
661 /* Enable dcache for the frame buffer */
662 fb_start = plat->base & ~(MMU_SECTION_SIZE - 1);
663 fb_end = plat->base + plat->size;
664 fb_end = ALIGN(fb_end, 1 << MMU_SECTION_SHIFT);
665 mmu_set_region_dcache_behaviour(fb_start, fb_end - fb_start,
667 video_set_flush_dcache(dev, true);
672 struct ipuv3_video_priv {
676 static int ipuv3_video_bind(struct udevice *dev)
678 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
680 plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
681 (1 << LCD_MAX_LOG2_BPP) / 8;
686 static const struct udevice_id ipuv3_video_ids[] = {
687 { .compatible = "fsl,imx6q-ipu" },
691 U_BOOT_DRIVER(ipuv3_video) = {
692 .name = "ipuv3_video",
694 .of_match = ipuv3_video_ids,
695 .bind = ipuv3_video_bind,
696 .probe = ipuv3_video_probe,
697 .priv_auto_alloc_size = sizeof(struct ipuv3_video_priv),
698 .flags = DM_FLAG_PRE_RELOC,
700 #endif /* CONFIG_DM_VIDEO */