5 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
7 * MX51 Linux framebuffer:
9 * (C) Copyright 2004-2010 Freescale Semiconductor, Inc.
11 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/errno.h>
16 #include <asm/global_data.h>
17 #include <linux/string.h>
18 #include <linux/list.h>
23 #include "videomodes.h"
28 DECLARE_GLOBAL_DATA_PTR;
30 static int mxcfb_map_video_memory(struct fb_info *fbi);
31 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
34 static GraphicDevice panel;
35 static struct fb_videomode const *gmode;
37 static uint32_t gpixfmt;
39 static void fb_videomode_to_var(struct fb_var_screeninfo *var,
40 const struct fb_videomode *mode)
42 var->xres = mode->xres;
43 var->yres = mode->yres;
44 var->xres_virtual = mode->xres;
45 var->yres_virtual = mode->yres;
48 var->pixclock = mode->pixclock;
49 var->left_margin = mode->left_margin;
50 var->right_margin = mode->right_margin;
51 var->upper_margin = mode->upper_margin;
52 var->lower_margin = mode->lower_margin;
53 var->hsync_len = mode->hsync_len;
54 var->vsync_len = mode->vsync_len;
55 var->sync = mode->sync;
56 var->vmode = mode->vmode & FB_VMODE_MASK;
60 * Structure containing the MXC specific framebuffer information.
67 unsigned char overlay;
68 unsigned char alpha_chan_en;
69 dma_addr_t alpha_phy_addr0;
70 dma_addr_t alpha_phy_addr1;
71 void *alpha_virt_addr0;
72 void *alpha_virt_addr1;
73 uint32_t alpha_mem_len;
75 uint32_t cur_ipu_alpha_buf;
77 u32 pseudo_palette[16];
87 static unsigned long default_bpp = 16;
88 static unsigned char g_dp_in_use;
89 static struct fb_info *mxcfb_info[3];
90 static int ext_clk_used;
92 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
96 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
99 return fbi->var.nonstd;
101 switch (fbi->var.bits_per_pixel) {
103 pixfmt = IPU_PIX_FMT_BGR24;
106 pixfmt = IPU_PIX_FMT_BGR32;
109 pixfmt = IPU_PIX_FMT_RGB565;
116 * Set fixed framebuffer parameters based on variable settings.
118 * @param info framebuffer information pointer
120 static int mxcfb_set_fix(struct fb_info *info)
122 struct fb_fix_screeninfo *fix = &info->fix;
123 struct fb_var_screeninfo *var = &info->var;
125 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
127 fix->type = FB_TYPE_PACKED_PIXELS;
128 fix->accel = FB_ACCEL_NONE;
129 fix->visual = FB_VISUAL_TRUECOLOR;
136 static int setup_disp_channel1(struct fb_info *fbi)
138 ipu_channel_params_t params;
139 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
141 memset(¶ms, 0, sizeof(params));
142 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
144 debug("%s called\n", __func__);
146 * Assuming interlaced means yuv output, below setting also
147 * valid for mem_dc_sync. FG should have the same vmode as BG.
149 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
150 params.mem_dp_bg_sync.interlaced = 1;
151 params.mem_dp_bg_sync.out_pixel_fmt =
154 if (mxc_fbi->ipu_di_pix_fmt) {
155 params.mem_dp_bg_sync.out_pixel_fmt =
156 mxc_fbi->ipu_di_pix_fmt;
158 params.mem_dp_bg_sync.out_pixel_fmt =
162 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
163 if (mxc_fbi->alpha_chan_en)
164 params.mem_dp_bg_sync.alpha_chan_en = 1;
166 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms);
171 static int setup_disp_channel2(struct fb_info *fbi)
174 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
176 mxc_fbi->cur_ipu_buf = 1;
177 if (mxc_fbi->alpha_chan_en)
178 mxc_fbi->cur_ipu_alpha_buf = 1;
180 fbi->var.xoffset = fbi->var.yoffset = 0;
182 debug("%s: %x %d %d %d %lx %lx\n",
187 fbi->fix.line_length,
189 fbi->fix.smem_start +
190 (fbi->fix.line_length * fbi->var.yres));
192 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
194 fbi->var.xres, fbi->var.yres,
195 fbi->fix.line_length,
196 fbi->fix.smem_start +
197 (fbi->fix.line_length * fbi->var.yres),
201 printf("ipu_init_channel_buffer error %d\n", retval);
207 * Set framebuffer parameters and change the operating mode.
209 * @param info framebuffer information pointer
211 static int mxcfb_set_par(struct fb_info *fbi)
215 ipu_di_signal_cfg_t sig_cfg;
216 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
217 uint32_t out_pixel_fmt;
219 ipu_disable_channel(mxc_fbi->ipu_ch);
220 ipu_uninit_channel(mxc_fbi->ipu_ch);
223 mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
224 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
225 if (fbi->fix.smem_start)
226 mxcfb_unmap_video_memory(fbi);
228 if (mxcfb_map_video_memory(fbi) < 0)
232 setup_disp_channel1(fbi);
234 memset(&sig_cfg, 0, sizeof(sig_cfg));
235 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
236 sig_cfg.interlaced = 1;
237 out_pixel_fmt = IPU_PIX_FMT_YUV444;
239 if (mxc_fbi->ipu_di_pix_fmt)
240 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
242 out_pixel_fmt = IPU_PIX_FMT_RGB666;
244 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
245 sig_cfg.odd_field_first = 1;
246 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
248 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
249 sig_cfg.Hsync_pol = 1;
250 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
251 sig_cfg.Vsync_pol = 1;
252 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
254 if (fbi->var.sync & FB_SYNC_DATA_INVERT)
255 sig_cfg.data_pol = 1;
256 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
257 sig_cfg.enable_pol = 1;
258 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
259 sig_cfg.clkidle_en = 1;
261 debug("pixclock = %lu Hz\n", PICOS2KHZ(fbi->var.pixclock) * 1000UL);
263 if (ipu_init_sync_panel(mxc_fbi->ipu_di,
264 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
265 fbi->var.xres, fbi->var.yres,
267 fbi->var.left_margin,
269 fbi->var.right_margin,
270 fbi->var.upper_margin,
272 fbi->var.lower_margin,
274 puts("mxcfb: Error initializing panel.\n");
278 retval = setup_disp_channel2(fbi);
282 if (mxc_fbi->blank == FB_BLANK_UNBLANK)
283 ipu_enable_channel(mxc_fbi->ipu_ch);
289 * Check framebuffer variable parameters and adjust to valid values.
291 * @param var framebuffer variable parameters
293 * @param info framebuffer information pointer
295 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
300 if (var->xres_virtual < var->xres)
301 var->xres_virtual = var->xres;
302 if (var->yres_virtual < var->yres)
303 var->yres_virtual = var->yres;
305 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
306 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
307 var->bits_per_pixel = default_bpp;
309 switch (var->bits_per_pixel) {
313 var->red.msb_right = 0;
315 var->green.length = 3;
316 var->green.offset = 2;
317 var->green.msb_right = 0;
319 var->blue.length = 2;
320 var->blue.offset = 0;
321 var->blue.msb_right = 0;
323 var->transp.length = 0;
324 var->transp.offset = 0;
325 var->transp.msb_right = 0;
329 var->red.offset = 11;
330 var->red.msb_right = 0;
332 var->green.length = 6;
333 var->green.offset = 5;
334 var->green.msb_right = 0;
336 var->blue.length = 5;
337 var->blue.offset = 0;
338 var->blue.msb_right = 0;
340 var->transp.length = 0;
341 var->transp.offset = 0;
342 var->transp.msb_right = 0;
346 var->red.offset = 16;
347 var->red.msb_right = 0;
349 var->green.length = 8;
350 var->green.offset = 8;
351 var->green.msb_right = 0;
353 var->blue.length = 8;
354 var->blue.offset = 0;
355 var->blue.msb_right = 0;
357 var->transp.length = 0;
358 var->transp.offset = 0;
359 var->transp.msb_right = 0;
363 var->red.offset = 16;
364 var->red.msb_right = 0;
366 var->green.length = 8;
367 var->green.offset = 8;
368 var->green.msb_right = 0;
370 var->blue.length = 8;
371 var->blue.offset = 0;
372 var->blue.msb_right = 0;
374 var->transp.length = 8;
375 var->transp.offset = 24;
376 var->transp.msb_right = 0;
380 if (var->pixclock < 1000) {
381 htotal = var->xres + var->right_margin + var->hsync_len +
383 vtotal = var->yres + var->lower_margin + var->vsync_len +
385 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
386 var->pixclock = KHZ2PICOS(var->pixclock);
387 printf("pixclock set for 60Hz refresh = %u ps\n",
398 static int mxcfb_map_video_memory(struct fb_info *fbi)
400 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
401 fbi->fix.smem_len = fbi->var.yres_virtual *
402 fbi->fix.line_length;
404 fbi->fix.smem_len = roundup(fbi->fix.smem_len, ARCH_DMA_MINALIGN);
405 fbi->screen_base = (char *)memalign(ARCH_DMA_MINALIGN,
407 fbi->fix.smem_start = (unsigned long)fbi->screen_base;
408 if (fbi->screen_base == 0) {
409 puts("Unable to allocate framebuffer memory\n");
410 fbi->fix.smem_len = 0;
411 fbi->fix.smem_start = 0;
415 debug("allocated fb @ paddr=0x%08X, size=%d.\n",
416 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
418 fbi->screen_size = fbi->fix.smem_len;
420 gd->fb_base = fbi->fix.smem_start;
422 /* Clear the screen */
423 memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
428 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
430 fbi->screen_base = 0;
431 fbi->fix.smem_start = 0;
432 fbi->fix.smem_len = 0;
437 * Initializes the framebuffer information pointer. After allocating
438 * sufficient memory for the framebuffer structure, the fields are
439 * filled with custom information passed in from the configurable
440 * structures. This includes information such as bits per pixel,
441 * color maps, screen width/height and RGBA offsets.
443 * @return Framebuffer structure initialized with our information
445 static struct fb_info *mxcfb_init_fbinfo(void)
447 #define BYTES_PER_LONG 4
448 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
450 struct mxcfb_info *mxcfbi;
452 int size = sizeof(struct mxcfb_info) + PADDING +
453 sizeof(struct fb_info);
455 debug("%s: %d %d %d %d\n",
459 sizeof(struct mxcfb_info),
460 sizeof(struct fb_info));
462 * Allocate sufficient memory for the fb structure
471 fbi = (struct fb_info *)p;
472 fbi->par = p + sizeof(struct fb_info) + PADDING;
474 mxcfbi = (struct mxcfb_info *)fbi->par;
475 debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
476 (unsigned int)fbi, (unsigned int)mxcfbi);
478 fbi->var.activate = FB_ACTIVATE_NOW;
480 fbi->flags = FBINFO_FLAG_DEFAULT;
481 fbi->pseudo_palette = mxcfbi->pseudo_palette;
487 * Probe routine for the framebuffer driver. It is called during the
488 * driver binding process. The following functions are performed in
489 * this routine: Framebuffer initialization, Memory allocation and
490 * mapping, Framebuffer registration, IPU initialization.
492 * @return Appropriate error code to the kernel common code
494 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
495 struct fb_videomode const *mode)
498 struct mxcfb_info *mxcfbi;
502 * Initialize FB structures
504 fbi = mxcfb_init_fbinfo();
509 mxcfbi = (struct mxcfb_info *)fbi->par;
512 mxcfbi->ipu_ch = MEM_BG_SYNC;
513 mxcfbi->blank = FB_BLANK_UNBLANK;
515 mxcfbi->ipu_ch = MEM_DC_SYNC;
516 mxcfbi->blank = FB_BLANK_POWERDOWN;
519 mxcfbi->ipu_di = disp;
521 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
522 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
523 strcpy(fbi->fix.id, "DISP3 BG");
527 mxcfb_info[mxcfbi->ipu_di] = fbi;
529 /* Need dummy values until real panel is configured */
531 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
532 fb_videomode_to_var(&fbi->var, mode);
533 fbi->var.bits_per_pixel = 16;
534 fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
535 fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
537 mxcfb_check_var(&fbi->var, fbi);
539 /* Default Y virtual size is 2x panel size */
540 fbi->var.yres_virtual = fbi->var.yres * 2;
544 /* allocate fb first */
545 if (mxcfb_map_video_memory(fbi) < 0)
550 panel.winSizeX = mode->xres;
551 panel.winSizeY = mode->yres;
552 panel.plnSizeX = mode->xres;
553 panel.plnSizeY = mode->yres;
555 panel.frameAdrs = (u32)fbi->screen_base;
556 panel.memSize = fbi->screen_size;
558 panel.gdfBytesPP = 2;
559 panel.gdfIndex = GDF_16BIT_565RGB;
561 ipu_dump_registers();
569 void ipuv3_fb_shutdown(void)
572 struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT;
574 if (!ipu_clk_enabled())
577 for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) {
578 struct fb_info *fbi = mxcfb_info[i];
580 struct mxcfb_info *mxc_fbi = fbi->par;
581 ipu_disable_channel(mxc_fbi->ipu_ch);
582 ipu_uninit_channel(mxc_fbi->ipu_ch);
585 for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) {
586 __raw_writel(__raw_readl(&stat->int_stat[i]),
591 void *video_hw_init(void)
597 puts("Error initializing IPU\n");
599 ret = mxcfb_probe(gpixfmt, gdisp, gmode);
600 debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
602 return (void *)&panel;
605 int ipuv3_fb_init(struct fb_videomode const *mode,