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>
22 #include "videomodes.h"
27 DECLARE_GLOBAL_DATA_PTR;
29 static int mxcfb_map_video_memory(struct fb_info *fbi);
30 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
33 static GraphicDevice panel;
34 static struct fb_videomode const *gmode;
36 static uint32_t gpixfmt;
38 static void fb_videomode_to_var(struct fb_var_screeninfo *var,
39 const struct fb_videomode *mode)
41 var->xres = mode->xres;
42 var->yres = mode->yres;
43 var->xres_virtual = mode->xres;
44 var->yres_virtual = mode->yres;
47 var->pixclock = mode->pixclock;
48 var->left_margin = mode->left_margin;
49 var->right_margin = mode->right_margin;
50 var->upper_margin = mode->upper_margin;
51 var->lower_margin = mode->lower_margin;
52 var->hsync_len = mode->hsync_len;
53 var->vsync_len = mode->vsync_len;
54 var->sync = mode->sync;
55 var->vmode = mode->vmode & FB_VMODE_MASK;
59 * Structure containing the MXC specific framebuffer information.
66 unsigned char overlay;
67 unsigned char alpha_chan_en;
68 dma_addr_t alpha_phy_addr0;
69 dma_addr_t alpha_phy_addr1;
70 void *alpha_virt_addr0;
71 void *alpha_virt_addr1;
72 uint32_t alpha_mem_len;
74 uint32_t cur_ipu_alpha_buf;
76 u32 pseudo_palette[16];
86 static unsigned long default_bpp = 16;
87 static unsigned char g_dp_in_use;
88 static struct fb_info *mxcfb_info[3];
89 static int ext_clk_used;
91 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
95 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
98 return fbi->var.nonstd;
100 switch (fbi->var.bits_per_pixel) {
102 pixfmt = IPU_PIX_FMT_BGR24;
105 pixfmt = IPU_PIX_FMT_BGR32;
108 pixfmt = IPU_PIX_FMT_RGB565;
115 * Set fixed framebuffer parameters based on variable settings.
117 * @param info framebuffer information pointer
119 static int mxcfb_set_fix(struct fb_info *info)
121 struct fb_fix_screeninfo *fix = &info->fix;
122 struct fb_var_screeninfo *var = &info->var;
124 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
126 fix->type = FB_TYPE_PACKED_PIXELS;
127 fix->accel = FB_ACCEL_NONE;
128 fix->visual = FB_VISUAL_TRUECOLOR;
135 static int setup_disp_channel1(struct fb_info *fbi)
137 ipu_channel_params_t params;
138 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
140 memset(¶ms, 0, sizeof(params));
141 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
143 debug("%s called\n", __func__);
145 * Assuming interlaced means yuv output, below setting also
146 * valid for mem_dc_sync. FG should have the same vmode as BG.
148 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
149 params.mem_dp_bg_sync.interlaced = 1;
150 params.mem_dp_bg_sync.out_pixel_fmt =
153 if (mxc_fbi->ipu_di_pix_fmt) {
154 params.mem_dp_bg_sync.out_pixel_fmt =
155 mxc_fbi->ipu_di_pix_fmt;
157 params.mem_dp_bg_sync.out_pixel_fmt =
161 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
162 if (mxc_fbi->alpha_chan_en)
163 params.mem_dp_bg_sync.alpha_chan_en = 1;
165 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms);
170 static int setup_disp_channel2(struct fb_info *fbi)
173 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
175 mxc_fbi->cur_ipu_buf = 1;
176 if (mxc_fbi->alpha_chan_en)
177 mxc_fbi->cur_ipu_alpha_buf = 1;
179 fbi->var.xoffset = fbi->var.yoffset = 0;
181 debug("%s: %x %d %d %d %lx %lx\n",
186 fbi->fix.line_length,
188 fbi->fix.smem_start +
189 (fbi->fix.line_length * fbi->var.yres));
191 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
193 fbi->var.xres, fbi->var.yres,
194 fbi->fix.line_length,
195 fbi->fix.smem_start +
196 (fbi->fix.line_length * fbi->var.yres),
200 printf("ipu_init_channel_buffer error %d\n", retval);
206 * Set framebuffer parameters and change the operating mode.
208 * @param info framebuffer information pointer
210 static int mxcfb_set_par(struct fb_info *fbi)
214 ipu_di_signal_cfg_t sig_cfg;
215 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
216 uint32_t out_pixel_fmt;
218 ipu_disable_channel(mxc_fbi->ipu_ch);
219 ipu_uninit_channel(mxc_fbi->ipu_ch);
222 mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
223 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
224 if (fbi->fix.smem_start)
225 mxcfb_unmap_video_memory(fbi);
227 if (mxcfb_map_video_memory(fbi) < 0)
231 setup_disp_channel1(fbi);
233 memset(&sig_cfg, 0, sizeof(sig_cfg));
234 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
235 sig_cfg.interlaced = 1;
236 out_pixel_fmt = IPU_PIX_FMT_YUV444;
238 if (mxc_fbi->ipu_di_pix_fmt)
239 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
241 out_pixel_fmt = IPU_PIX_FMT_RGB666;
243 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
244 sig_cfg.odd_field_first = 1;
245 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
247 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
248 sig_cfg.Hsync_pol = 1;
249 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
250 sig_cfg.Vsync_pol = 1;
251 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
253 if (fbi->var.sync & FB_SYNC_DATA_INVERT)
254 sig_cfg.data_pol = 1;
255 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
256 sig_cfg.enable_pol = 1;
257 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
258 sig_cfg.clkidle_en = 1;
260 debug("pixclock = %lu Hz\n", PICOS2KHZ(fbi->var.pixclock) * 1000UL);
262 if (ipu_init_sync_panel(mxc_fbi->ipu_di,
263 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
264 fbi->var.xres, fbi->var.yres,
266 fbi->var.left_margin,
268 fbi->var.right_margin,
269 fbi->var.upper_margin,
271 fbi->var.lower_margin,
273 puts("mxcfb: Error initializing panel.\n");
277 retval = setup_disp_channel2(fbi);
281 if (mxc_fbi->blank == FB_BLANK_UNBLANK)
282 ipu_enable_channel(mxc_fbi->ipu_ch);
288 * Check framebuffer variable parameters and adjust to valid values.
290 * @param var framebuffer variable parameters
292 * @param info framebuffer information pointer
294 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
299 if (var->xres_virtual < var->xres)
300 var->xres_virtual = var->xres;
301 if (var->yres_virtual < var->yres)
302 var->yres_virtual = var->yres;
304 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
305 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
306 var->bits_per_pixel = default_bpp;
308 switch (var->bits_per_pixel) {
312 var->red.msb_right = 0;
314 var->green.length = 3;
315 var->green.offset = 2;
316 var->green.msb_right = 0;
318 var->blue.length = 2;
319 var->blue.offset = 0;
320 var->blue.msb_right = 0;
322 var->transp.length = 0;
323 var->transp.offset = 0;
324 var->transp.msb_right = 0;
328 var->red.offset = 11;
329 var->red.msb_right = 0;
331 var->green.length = 6;
332 var->green.offset = 5;
333 var->green.msb_right = 0;
335 var->blue.length = 5;
336 var->blue.offset = 0;
337 var->blue.msb_right = 0;
339 var->transp.length = 0;
340 var->transp.offset = 0;
341 var->transp.msb_right = 0;
345 var->red.offset = 16;
346 var->red.msb_right = 0;
348 var->green.length = 8;
349 var->green.offset = 8;
350 var->green.msb_right = 0;
352 var->blue.length = 8;
353 var->blue.offset = 0;
354 var->blue.msb_right = 0;
356 var->transp.length = 0;
357 var->transp.offset = 0;
358 var->transp.msb_right = 0;
362 var->red.offset = 16;
363 var->red.msb_right = 0;
365 var->green.length = 8;
366 var->green.offset = 8;
367 var->green.msb_right = 0;
369 var->blue.length = 8;
370 var->blue.offset = 0;
371 var->blue.msb_right = 0;
373 var->transp.length = 8;
374 var->transp.offset = 24;
375 var->transp.msb_right = 0;
379 if (var->pixclock < 1000) {
380 htotal = var->xres + var->right_margin + var->hsync_len +
382 vtotal = var->yres + var->lower_margin + var->vsync_len +
384 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
385 var->pixclock = KHZ2PICOS(var->pixclock);
386 printf("pixclock set for 60Hz refresh = %u ps\n",
397 static int mxcfb_map_video_memory(struct fb_info *fbi)
399 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
400 fbi->fix.smem_len = fbi->var.yres_virtual *
401 fbi->fix.line_length;
403 fbi->fix.smem_len = roundup(fbi->fix.smem_len, ARCH_DMA_MINALIGN);
404 fbi->screen_base = (char *)memalign(ARCH_DMA_MINALIGN,
406 fbi->fix.smem_start = (unsigned long)fbi->screen_base;
407 if (fbi->screen_base == 0) {
408 puts("Unable to allocate framebuffer memory\n");
409 fbi->fix.smem_len = 0;
410 fbi->fix.smem_start = 0;
414 debug("allocated fb @ paddr=0x%08X, size=%d.\n",
415 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
417 fbi->screen_size = fbi->fix.smem_len;
419 gd->fb_base = fbi->fix.smem_start;
421 /* Clear the screen */
422 memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
427 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
429 fbi->screen_base = 0;
430 fbi->fix.smem_start = 0;
431 fbi->fix.smem_len = 0;
436 * Initializes the framebuffer information pointer. After allocating
437 * sufficient memory for the framebuffer structure, the fields are
438 * filled with custom information passed in from the configurable
439 * structures. This includes information such as bits per pixel,
440 * color maps, screen width/height and RGBA offsets.
442 * @return Framebuffer structure initialized with our information
444 static struct fb_info *mxcfb_init_fbinfo(void)
446 #define BYTES_PER_LONG 4
447 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
449 struct mxcfb_info *mxcfbi;
451 int size = sizeof(struct mxcfb_info) + PADDING +
452 sizeof(struct fb_info);
454 debug("%s: %d %d %d %d\n",
458 sizeof(struct mxcfb_info),
459 sizeof(struct fb_info));
461 * Allocate sufficient memory for the fb structure
470 fbi = (struct fb_info *)p;
471 fbi->par = p + sizeof(struct fb_info) + PADDING;
473 mxcfbi = (struct mxcfb_info *)fbi->par;
474 debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
475 (unsigned int)fbi, (unsigned int)mxcfbi);
477 fbi->var.activate = FB_ACTIVATE_NOW;
479 fbi->flags = FBINFO_FLAG_DEFAULT;
480 fbi->pseudo_palette = mxcfbi->pseudo_palette;
486 * Probe routine for the framebuffer driver. It is called during the
487 * driver binding process. The following functions are performed in
488 * this routine: Framebuffer initialization, Memory allocation and
489 * mapping, Framebuffer registration, IPU initialization.
491 * @return Appropriate error code to the kernel common code
493 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
494 struct fb_videomode const *mode)
497 struct mxcfb_info *mxcfbi;
501 * Initialize FB structures
503 fbi = mxcfb_init_fbinfo();
508 mxcfbi = (struct mxcfb_info *)fbi->par;
511 mxcfbi->ipu_ch = MEM_BG_SYNC;
512 mxcfbi->blank = FB_BLANK_UNBLANK;
514 mxcfbi->ipu_ch = MEM_DC_SYNC;
515 mxcfbi->blank = FB_BLANK_POWERDOWN;
518 mxcfbi->ipu_di = disp;
520 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
521 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
522 strcpy(fbi->fix.id, "DISP3 BG");
526 mxcfb_info[mxcfbi->ipu_di] = fbi;
528 /* Need dummy values until real panel is configured */
530 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
531 fb_videomode_to_var(&fbi->var, mode);
532 fbi->var.bits_per_pixel = 16;
533 fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
534 fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
536 mxcfb_check_var(&fbi->var, fbi);
538 /* Default Y virtual size is 2x panel size */
539 fbi->var.yres_virtual = fbi->var.yres * 2;
543 /* allocate fb first */
544 if (mxcfb_map_video_memory(fbi) < 0)
549 panel.winSizeX = mode->xres;
550 panel.winSizeY = mode->yres;
551 panel.plnSizeX = mode->xres;
552 panel.plnSizeY = mode->yres;
554 panel.frameAdrs = (u32)fbi->screen_base;
555 panel.memSize = fbi->screen_size;
557 panel.gdfBytesPP = 2;
558 panel.gdfIndex = GDF_16BIT_565RGB;
560 ipu_dump_registers();
568 void ipuv3_fb_shutdown(void)
571 struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT;
573 if (!ipu_clk_enabled())
576 for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) {
577 struct fb_info *fbi = mxcfb_info[i];
579 struct mxcfb_info *mxc_fbi = fbi->par;
580 ipu_disable_channel(mxc_fbi->ipu_ch);
581 ipu_uninit_channel(mxc_fbi->ipu_ch);
584 for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) {
585 __raw_writel(__raw_readl(&stat->int_stat[i]),
590 void *video_hw_init(void)
596 puts("Error initializing IPU\n");
598 ret = mxcfb_probe(gpixfmt, gdisp, gmode);
599 debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
601 return (void *)&panel;
604 int ipuv3_fb_init(struct fb_videomode const *mode,