1 // SPDX-License-Identifier: GPL-2.0+
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"
33 DECLARE_GLOBAL_DATA_PTR;
35 static int mxcfb_map_video_memory(struct fb_info *fbi);
36 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
39 static GraphicDevice panel;
40 static struct fb_videomode const *gmode;
42 static uint32_t gpixfmt;
44 static void fb_videomode_to_var(struct fb_var_screeninfo *var,
45 const struct fb_videomode *mode)
47 var->xres = mode->xres;
48 var->yres = mode->yres;
49 var->xres_virtual = mode->xres;
50 var->yres_virtual = mode->yres;
53 var->pixclock = mode->pixclock;
54 var->left_margin = mode->left_margin;
55 var->right_margin = mode->right_margin;
56 var->upper_margin = mode->upper_margin;
57 var->lower_margin = mode->lower_margin;
58 var->hsync_len = mode->hsync_len;
59 var->vsync_len = mode->vsync_len;
60 var->sync = mode->sync;
61 var->vmode = mode->vmode & FB_VMODE_MASK;
65 * Structure containing the MXC specific framebuffer information.
72 unsigned char overlay;
73 unsigned char alpha_chan_en;
74 dma_addr_t alpha_phy_addr0;
75 dma_addr_t alpha_phy_addr1;
76 void *alpha_virt_addr0;
77 void *alpha_virt_addr1;
78 uint32_t alpha_mem_len;
80 uint32_t cur_ipu_alpha_buf;
82 u32 pseudo_palette[16];
92 static unsigned long default_bpp = 16;
93 static unsigned char g_dp_in_use;
94 static struct fb_info *mxcfb_info[3];
95 static int ext_clk_used;
97 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
101 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
104 return fbi->var.nonstd;
106 switch (fbi->var.bits_per_pixel) {
108 pixfmt = IPU_PIX_FMT_BGR24;
111 pixfmt = IPU_PIX_FMT_BGR32;
114 pixfmt = IPU_PIX_FMT_RGB565;
121 * Set fixed framebuffer parameters based on variable settings.
123 * @param info framebuffer information pointer
125 static int mxcfb_set_fix(struct fb_info *info)
127 struct fb_fix_screeninfo *fix = &info->fix;
128 struct fb_var_screeninfo *var = &info->var;
130 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
132 fix->type = FB_TYPE_PACKED_PIXELS;
133 fix->accel = FB_ACCEL_NONE;
134 fix->visual = FB_VISUAL_TRUECOLOR;
141 static int setup_disp_channel1(struct fb_info *fbi)
143 ipu_channel_params_t params;
144 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
146 memset(¶ms, 0, sizeof(params));
147 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
149 debug("%s called\n", __func__);
151 * Assuming interlaced means yuv output, below setting also
152 * valid for mem_dc_sync. FG should have the same vmode as BG.
154 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
155 params.mem_dp_bg_sync.interlaced = 1;
156 params.mem_dp_bg_sync.out_pixel_fmt =
159 if (mxc_fbi->ipu_di_pix_fmt) {
160 params.mem_dp_bg_sync.out_pixel_fmt =
161 mxc_fbi->ipu_di_pix_fmt;
163 params.mem_dp_bg_sync.out_pixel_fmt =
167 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
168 if (mxc_fbi->alpha_chan_en)
169 params.mem_dp_bg_sync.alpha_chan_en = 1;
171 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms);
176 static int setup_disp_channel2(struct fb_info *fbi)
179 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
181 mxc_fbi->cur_ipu_buf = 1;
182 if (mxc_fbi->alpha_chan_en)
183 mxc_fbi->cur_ipu_alpha_buf = 1;
185 fbi->var.xoffset = fbi->var.yoffset = 0;
187 debug("%s: %x %d %d %d %lx %lx\n",
192 fbi->fix.line_length,
194 fbi->fix.smem_start +
195 (fbi->fix.line_length * fbi->var.yres));
197 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
199 fbi->var.xres, fbi->var.yres,
200 fbi->fix.line_length,
201 fbi->fix.smem_start +
202 (fbi->fix.line_length * fbi->var.yres),
206 printf("ipu_init_channel_buffer error %d\n", retval);
212 * Set framebuffer parameters and change the operating mode.
214 * @param info framebuffer information pointer
216 static int mxcfb_set_par(struct fb_info *fbi)
220 ipu_di_signal_cfg_t sig_cfg;
221 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
222 uint32_t out_pixel_fmt;
224 ipu_disable_channel(mxc_fbi->ipu_ch);
225 ipu_uninit_channel(mxc_fbi->ipu_ch);
228 mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
229 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
230 if (fbi->fix.smem_start)
231 mxcfb_unmap_video_memory(fbi);
233 if (mxcfb_map_video_memory(fbi) < 0)
237 setup_disp_channel1(fbi);
239 memset(&sig_cfg, 0, sizeof(sig_cfg));
240 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
241 sig_cfg.interlaced = 1;
242 out_pixel_fmt = IPU_PIX_FMT_YUV444;
244 if (mxc_fbi->ipu_di_pix_fmt)
245 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
247 out_pixel_fmt = IPU_PIX_FMT_RGB666;
249 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
250 sig_cfg.odd_field_first = 1;
251 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
253 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
254 sig_cfg.Hsync_pol = 1;
255 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
256 sig_cfg.Vsync_pol = 1;
257 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
259 if (fbi->var.sync & FB_SYNC_DATA_INVERT)
260 sig_cfg.data_pol = 1;
261 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
262 sig_cfg.enable_pol = 1;
263 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
264 sig_cfg.clkidle_en = 1;
266 debug("pixclock = %lu Hz\n", PICOS2KHZ(fbi->var.pixclock) * 1000UL);
268 if (ipu_init_sync_panel(mxc_fbi->ipu_di,
269 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
270 fbi->var.xres, fbi->var.yres,
272 fbi->var.left_margin,
274 fbi->var.right_margin,
275 fbi->var.upper_margin,
277 fbi->var.lower_margin,
279 puts("mxcfb: Error initializing panel.\n");
283 retval = setup_disp_channel2(fbi);
287 if (mxc_fbi->blank == FB_BLANK_UNBLANK)
288 ipu_enable_channel(mxc_fbi->ipu_ch);
294 * Check framebuffer variable parameters and adjust to valid values.
296 * @param var framebuffer variable parameters
298 * @param info framebuffer information pointer
300 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
305 if (var->xres_virtual < var->xres)
306 var->xres_virtual = var->xres;
307 if (var->yres_virtual < var->yres)
308 var->yres_virtual = var->yres;
310 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
311 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
312 var->bits_per_pixel = default_bpp;
314 switch (var->bits_per_pixel) {
318 var->red.msb_right = 0;
320 var->green.length = 3;
321 var->green.offset = 2;
322 var->green.msb_right = 0;
324 var->blue.length = 2;
325 var->blue.offset = 0;
326 var->blue.msb_right = 0;
328 var->transp.length = 0;
329 var->transp.offset = 0;
330 var->transp.msb_right = 0;
334 var->red.offset = 11;
335 var->red.msb_right = 0;
337 var->green.length = 6;
338 var->green.offset = 5;
339 var->green.msb_right = 0;
341 var->blue.length = 5;
342 var->blue.offset = 0;
343 var->blue.msb_right = 0;
345 var->transp.length = 0;
346 var->transp.offset = 0;
347 var->transp.msb_right = 0;
351 var->red.offset = 16;
352 var->red.msb_right = 0;
354 var->green.length = 8;
355 var->green.offset = 8;
356 var->green.msb_right = 0;
358 var->blue.length = 8;
359 var->blue.offset = 0;
360 var->blue.msb_right = 0;
362 var->transp.length = 0;
363 var->transp.offset = 0;
364 var->transp.msb_right = 0;
368 var->red.offset = 16;
369 var->red.msb_right = 0;
371 var->green.length = 8;
372 var->green.offset = 8;
373 var->green.msb_right = 0;
375 var->blue.length = 8;
376 var->blue.offset = 0;
377 var->blue.msb_right = 0;
379 var->transp.length = 8;
380 var->transp.offset = 24;
381 var->transp.msb_right = 0;
385 if (var->pixclock < 1000) {
386 htotal = var->xres + var->right_margin + var->hsync_len +
388 vtotal = var->yres + var->lower_margin + var->vsync_len +
390 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
391 var->pixclock = KHZ2PICOS(var->pixclock);
392 printf("pixclock set for 60Hz refresh = %u ps\n",
403 static int mxcfb_map_video_memory(struct fb_info *fbi)
405 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
406 fbi->fix.smem_len = fbi->var.yres_virtual *
407 fbi->fix.line_length;
409 fbi->fix.smem_len = roundup(fbi->fix.smem_len, ARCH_DMA_MINALIGN);
411 #if CONFIG_IS_ENABLED(DM_VIDEO)
412 fbi->screen_base = (char *)gd->video_bottom;
414 fbi->screen_base = (char *)memalign(ARCH_DMA_MINALIGN,
418 fbi->fix.smem_start = (unsigned long)fbi->screen_base;
419 if (fbi->screen_base == 0) {
420 puts("Unable to allocate framebuffer memory\n");
421 fbi->fix.smem_len = 0;
422 fbi->fix.smem_start = 0;
426 debug("allocated fb @ paddr=0x%08X, size=%d.\n",
427 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
429 fbi->screen_size = fbi->fix.smem_len;
431 #if CONFIG_IS_ENABLED(VIDEO)
432 gd->fb_base = fbi->fix.smem_start;
435 /* Clear the screen */
436 memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
441 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
443 fbi->screen_base = 0;
444 fbi->fix.smem_start = 0;
445 fbi->fix.smem_len = 0;
450 * Initializes the framebuffer information pointer. After allocating
451 * sufficient memory for the framebuffer structure, the fields are
452 * filled with custom information passed in from the configurable
453 * structures. This includes information such as bits per pixel,
454 * color maps, screen width/height and RGBA offsets.
456 * @return Framebuffer structure initialized with our information
458 static struct fb_info *mxcfb_init_fbinfo(void)
460 #define BYTES_PER_LONG 4
461 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
463 struct mxcfb_info *mxcfbi;
465 int size = sizeof(struct mxcfb_info) + PADDING +
466 sizeof(struct fb_info);
468 debug("%s: %d %d %d %d\n",
472 sizeof(struct mxcfb_info),
473 sizeof(struct fb_info));
475 * Allocate sufficient memory for the fb structure
484 fbi = (struct fb_info *)p;
485 fbi->par = p + sizeof(struct fb_info) + PADDING;
487 mxcfbi = (struct mxcfb_info *)fbi->par;
488 debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
489 (unsigned int)fbi, (unsigned int)mxcfbi);
491 fbi->var.activate = FB_ACTIVATE_NOW;
493 fbi->flags = FBINFO_FLAG_DEFAULT;
494 fbi->pseudo_palette = mxcfbi->pseudo_palette;
500 * Probe routine for the framebuffer driver. It is called during the
501 * driver binding process. The following functions are performed in
502 * this routine: Framebuffer initialization, Memory allocation and
503 * mapping, Framebuffer registration, IPU initialization.
505 * @return Appropriate error code to the kernel common code
507 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
508 struct fb_videomode const *mode)
511 struct mxcfb_info *mxcfbi;
515 * Initialize FB structures
517 fbi = mxcfb_init_fbinfo();
522 mxcfbi = (struct mxcfb_info *)fbi->par;
525 mxcfbi->ipu_ch = MEM_BG_SYNC;
526 mxcfbi->blank = FB_BLANK_UNBLANK;
528 mxcfbi->ipu_ch = MEM_DC_SYNC;
529 mxcfbi->blank = FB_BLANK_POWERDOWN;
532 mxcfbi->ipu_di = disp;
534 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
535 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
536 strcpy(fbi->fix.id, "DISP3 BG");
540 mxcfb_info[mxcfbi->ipu_di] = fbi;
542 /* Need dummy values until real panel is configured */
544 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
545 fb_videomode_to_var(&fbi->var, mode);
546 fbi->var.bits_per_pixel = 16;
547 fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
548 fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
550 mxcfb_check_var(&fbi->var, fbi);
552 /* Default Y virtual size is 2x panel size */
553 fbi->var.yres_virtual = fbi->var.yres * 2;
557 /* allocate fb first */
558 if (mxcfb_map_video_memory(fbi) < 0)
563 panel.winSizeX = mode->xres;
564 panel.winSizeY = mode->yres;
565 panel.plnSizeX = mode->xres;
566 panel.plnSizeY = mode->yres;
568 panel.frameAdrs = (u32)fbi->screen_base;
569 panel.memSize = fbi->screen_size;
571 panel.gdfBytesPP = 2;
572 panel.gdfIndex = GDF_16BIT_565RGB;
574 ipu_dump_registers();
582 void ipuv3_fb_shutdown(void)
585 struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT;
587 if (!ipu_clk_enabled())
590 for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) {
591 struct fb_info *fbi = mxcfb_info[i];
593 struct mxcfb_info *mxc_fbi = fbi->par;
594 ipu_disable_channel(mxc_fbi->ipu_ch);
595 ipu_uninit_channel(mxc_fbi->ipu_ch);
598 for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) {
599 __raw_writel(__raw_readl(&stat->int_stat[i]),
604 void *video_hw_init(void)
610 puts("Error initializing IPU\n");
612 ret = mxcfb_probe(gpixfmt, gdisp, gmode);
613 debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
614 gd->fb_base = panel.frameAdrs;
616 return (void *)&panel;
619 int ipuv3_fb_init(struct fb_videomode const *mode,
630 #if CONFIG_IS_ENABLED(DM_VIDEO)
632 /* Maximum display size we support */
633 LCD_MAX_WIDTH = 1920,
634 LCD_MAX_HEIGHT = 1080,
635 LCD_MAX_LOG2_BPP = VIDEO_BPP16,
638 static int ipuv3_video_probe(struct udevice *dev)
640 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
641 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
642 #if defined(CONFIG_DISPLAY)
643 struct udevice *disp_dev;
645 struct udevice *panel_dev;
646 u32 fb_start, fb_end;
649 debug("%s() plat: base 0x%lx, size 0x%x\n",
650 __func__, plat->base, plat->size);
656 ret = ipu_displays_init();
660 ret = mxcfb_probe(gpixfmt, gdisp, gmode);
664 #if defined(CONFIG_DISPLAY)
665 ret = uclass_first_device(UCLASS_DISPLAY, &disp_dev);
667 ret = display_enable(disp_dev, 16, NULL);
672 ret = uclass_get_device(UCLASS_PANEL, 0, &panel_dev);
674 panel_enable_backlight(panel_dev);
676 uc_priv->xsize = gmode->xres;
677 uc_priv->ysize = gmode->yres;
678 uc_priv->bpix = LCD_MAX_LOG2_BPP;
680 /* Enable dcache for the frame buffer */
681 fb_start = plat->base & ~(MMU_SECTION_SIZE - 1);
682 fb_end = plat->base + plat->size;
683 fb_end = ALIGN(fb_end, 1 << MMU_SECTION_SHIFT);
684 mmu_set_region_dcache_behaviour(fb_start, fb_end - fb_start,
686 video_set_flush_dcache(dev, true);
687 gd->fb_base = fb_start;
692 struct ipuv3_video_priv {
696 static int ipuv3_video_bind(struct udevice *dev)
698 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
700 plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
701 (1 << VIDEO_BPP32) / 8;
706 static const struct udevice_id ipuv3_video_ids[] = {
707 { .compatible = "fsl,imx6q-ipu" },
708 { .compatible = "fsl,imx53-ipu" },
712 U_BOOT_DRIVER(ipuv3_video) = {
713 .name = "ipuv3_video",
715 .of_match = ipuv3_video_ids,
716 .bind = ipuv3_video_bind,
717 .probe = ipuv3_video_probe,
718 .priv_auto_alloc_size = sizeof(struct ipuv3_video_priv),
719 .flags = DM_FLAG_PRE_RELOC,
721 #endif /* CONFIG_DM_VIDEO */