]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/video/mxc_ipuv3_fb.c
Merge branch 'master' of git://git.denx.de/u-boot-video
[karo-tx-uboot.git] / drivers / video / mxc_ipuv3_fb.c
1 /*
2  * Porting to u-boot:
3  *
4  * (C) Copyright 2010
5  * Stefano Babic, DENX Software Engineering, sbabic@denx.de
6  *
7  * MX51 Linux framebuffer:
8  *
9  * (C) Copyright 2004-2010 Freescale Semiconductor, Inc.
10  *
11  * See file CREDITS for list of people who contributed to this
12  * project.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 2 of
17  * the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27  * MA 02111-1307 USA
28  */
29
30 /* #define DEBUG */
31 #include <common.h>
32 #include <asm/errno.h>
33 #include <linux/string.h>
34 #include <linux/list.h>
35 #include <linux/fb.h>
36 #include <asm/io.h>
37 #include <malloc.h>
38 #include <lcd.h>
39 #include "videomodes.h"
40 #include "ipu.h"
41 #include "mxcfb.h"
42
43 DECLARE_GLOBAL_DATA_PTR;
44
45 void *lcd_base;                 /* Start of framebuffer memory  */
46 void *lcd_console_address;      /* Start of console buffer      */
47
48 int lcd_line_length;
49 int lcd_color_fg;
50 int lcd_color_bg;
51
52 short console_col;
53 short console_row;
54
55 vidinfo_t panel_info;
56
57 static int mxcfb_map_video_memory(struct fb_info *fbi);
58 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
59
60 void lcd_initcolregs(void)
61 {
62 }
63
64 void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
65 {
66 }
67
68 void lcd_disable(void)
69 {
70 }
71
72 void lcd_panel_disable(void)
73 {
74 }
75
76 void fb_videomode_to_var(struct fb_var_screeninfo *var,
77                          const struct fb_videomode *mode)
78 {
79         var->xres = mode->xres;
80         var->yres = mode->yres;
81         var->xres_virtual = mode->xres;
82         var->yres_virtual = mode->yres;
83         var->xoffset = 0;
84         var->yoffset = 0;
85         var->pixclock = mode->pixclock;
86         var->left_margin = mode->left_margin;
87         var->right_margin = mode->right_margin;
88         var->upper_margin = mode->upper_margin;
89         var->lower_margin = mode->lower_margin;
90         var->hsync_len = mode->hsync_len;
91         var->vsync_len = mode->vsync_len;
92         var->sync = mode->sync;
93         var->vmode = mode->vmode & FB_VMODE_MASK;
94 }
95
96 /*
97  * Structure containing the MXC specific framebuffer information.
98  */
99 struct mxcfb_info {
100         int blank;
101         ipu_channel_t ipu_ch;
102         int ipu_di;
103         u32 ipu_di_pix_fmt;
104         unsigned char overlay;
105         unsigned char alpha_chan_en;
106         dma_addr_t alpha_phy_addr0;
107         dma_addr_t alpha_phy_addr1;
108         void *alpha_virt_addr0;
109         void *alpha_virt_addr1;
110         uint32_t alpha_mem_len;
111         uint32_t cur_ipu_buf;
112         uint32_t cur_ipu_alpha_buf;
113
114         u32 pseudo_palette[16];
115 };
116
117 enum {
118         BOTH_ON,
119         SRC_ON,
120         TGT_ON,
121         BOTH_OFF
122 };
123
124 static unsigned long default_bpp = 16;
125 static unsigned char g_dp_in_use;
126 static struct fb_info *mxcfb_info[3];
127 static int ext_clk_used;
128
129 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
130 {
131         uint32_t pixfmt = 0;
132
133         debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
134
135         if (fbi->var.nonstd)
136                 return fbi->var.nonstd;
137
138         switch (fbi->var.bits_per_pixel) {
139         case 24:
140                 pixfmt = IPU_PIX_FMT_BGR24;
141                 break;
142         case 32:
143                 pixfmt = IPU_PIX_FMT_BGR32;
144                 break;
145         case 16:
146                 pixfmt = IPU_PIX_FMT_RGB565;
147                 break;
148         }
149         return pixfmt;
150 }
151
152 /*
153  * Set fixed framebuffer parameters based on variable settings.
154  *
155  * @param       info     framebuffer information pointer
156  */
157 static int mxcfb_set_fix(struct fb_info *info)
158 {
159         struct fb_fix_screeninfo *fix = &info->fix;
160         struct fb_var_screeninfo *var = &info->var;
161
162         fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
163
164         fix->type = FB_TYPE_PACKED_PIXELS;
165         fix->accel = FB_ACCEL_NONE;
166         fix->visual = FB_VISUAL_TRUECOLOR;
167         fix->xpanstep = 1;
168         fix->ypanstep = 1;
169
170         return 0;
171 }
172
173 static int setup_disp_channel1(struct fb_info *fbi)
174 {
175         ipu_channel_params_t params;
176         struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
177
178         memset(&params, 0, sizeof(params));
179         params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
180
181         debug("%s called\n", __func__);
182         /*
183          * Assuming interlaced means yuv output, below setting also
184          * valid for mem_dc_sync. FG should have the same vmode as BG.
185          */
186         if (fbi->var.vmode & FB_VMODE_INTERLACED) {
187                 params.mem_dp_bg_sync.interlaced = 1;
188                 params.mem_dp_bg_sync.out_pixel_fmt =
189                         IPU_PIX_FMT_YUV444;
190         } else {
191                 if (mxc_fbi->ipu_di_pix_fmt) {
192                         params.mem_dp_bg_sync.out_pixel_fmt =
193                                 mxc_fbi->ipu_di_pix_fmt;
194                 } else {
195                         params.mem_dp_bg_sync.out_pixel_fmt =
196                                 IPU_PIX_FMT_RGB666;
197                 }
198         }
199         params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
200         if (mxc_fbi->alpha_chan_en)
201                 params.mem_dp_bg_sync.alpha_chan_en = 1;
202
203         ipu_init_channel(mxc_fbi->ipu_ch, &params);
204
205         return 0;
206 }
207
208 static int setup_disp_channel2(struct fb_info *fbi)
209 {
210         int retval = 0;
211         struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
212
213         mxc_fbi->cur_ipu_buf = 1;
214         if (mxc_fbi->alpha_chan_en)
215                 mxc_fbi->cur_ipu_alpha_buf = 1;
216
217         fbi->var.xoffset = fbi->var.yoffset = 0;
218
219         debug("%s: %x %d %d %d %lx %lx\n",
220                 __func__,
221                 mxc_fbi->ipu_ch,
222                 fbi->var.xres,
223                 fbi->var.yres,
224                 fbi->fix.line_length,
225                 fbi->fix.smem_start,
226                 fbi->fix.smem_start +
227                 (fbi->fix.line_length * fbi->var.yres));
228
229         retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
230                                          bpp_to_pixfmt(fbi),
231                                          fbi->var.xres, fbi->var.yres,
232                                          fbi->fix.line_length,
233                                          fbi->fix.smem_start +
234                                          (fbi->fix.line_length * fbi->var.yres),
235                                          fbi->fix.smem_start,
236                                          0, 0);
237         if (retval)
238                 printf("ipu_init_channel_buffer error %d\n", retval);
239
240         return retval;
241 }
242
243 /*
244  * Set framebuffer parameters and change the operating mode.
245  *
246  * @param       info     framebuffer information pointer
247  */
248 static int mxcfb_set_par(struct fb_info *fbi)
249 {
250         int retval = 0;
251         u32 mem_len;
252         ipu_di_signal_cfg_t sig_cfg;
253         struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
254         uint32_t out_pixel_fmt;
255
256         ipu_disable_channel(mxc_fbi->ipu_ch);
257         ipu_uninit_channel(mxc_fbi->ipu_ch);
258         mxcfb_set_fix(fbi);
259
260         mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
261         if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
262                 if (fbi->fix.smem_start)
263                         mxcfb_unmap_video_memory(fbi);
264
265                 if (mxcfb_map_video_memory(fbi) < 0)
266                         return -ENOMEM;
267         }
268
269         setup_disp_channel1(fbi);
270
271         memset(&sig_cfg, 0, sizeof(sig_cfg));
272         if (fbi->var.vmode & FB_VMODE_INTERLACED) {
273                 sig_cfg.interlaced = 1;
274                 out_pixel_fmt = IPU_PIX_FMT_YUV444;
275         } else {
276                 if (mxc_fbi->ipu_di_pix_fmt)
277                         out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
278                 else
279                         out_pixel_fmt = IPU_PIX_FMT_RGB666;
280         }
281         if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
282                 sig_cfg.odd_field_first = 1;
283         if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
284                 sig_cfg.ext_clk = 1;
285         if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
286                 sig_cfg.Hsync_pol = 1;
287         if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
288                 sig_cfg.Vsync_pol = 1;
289         if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
290                 sig_cfg.clk_pol = 1;
291         if (fbi->var.sync & FB_SYNC_DATA_INVERT)
292                 sig_cfg.data_pol = 1;
293         if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
294                 sig_cfg.enable_pol = 1;
295         if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
296                 sig_cfg.clkidle_en = 1;
297
298         debug("pixclock = %ul Hz\n",
299                 (u32) (PICOS2KHZ(fbi->var.pixclock) * 1000UL));
300
301         if (ipu_init_sync_panel(mxc_fbi->ipu_di,
302                                 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
303                                 fbi->var.xres, fbi->var.yres,
304                                 out_pixel_fmt,
305                                 fbi->var.left_margin,
306                                 fbi->var.hsync_len,
307                                 fbi->var.right_margin,
308                                 fbi->var.upper_margin,
309                                 fbi->var.vsync_len,
310                                 fbi->var.lower_margin,
311                                 0, sig_cfg) != 0) {
312                 puts("mxcfb: Error initializing panel.\n");
313                 return -EINVAL;
314         }
315
316         retval = setup_disp_channel2(fbi);
317         if (retval)
318                 return retval;
319
320         if (mxc_fbi->blank == FB_BLANK_UNBLANK)
321                 ipu_enable_channel(mxc_fbi->ipu_ch);
322
323         return retval;
324 }
325
326 /*
327  * Check framebuffer variable parameters and adjust to valid values.
328  *
329  * @param       var      framebuffer variable parameters
330  *
331  * @param       info     framebuffer information pointer
332  */
333 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
334 {
335         u32 vtotal;
336         u32 htotal;
337
338         if (var->xres_virtual < var->xres)
339                 var->xres_virtual = var->xres;
340         if (var->yres_virtual < var->yres)
341                 var->yres_virtual = var->yres;
342
343         if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
344             (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
345                 var->bits_per_pixel = default_bpp;
346
347         switch (var->bits_per_pixel) {
348         case 8:
349                 var->red.length = 3;
350                 var->red.offset = 5;
351                 var->red.msb_right = 0;
352
353                 var->green.length = 3;
354                 var->green.offset = 2;
355                 var->green.msb_right = 0;
356
357                 var->blue.length = 2;
358                 var->blue.offset = 0;
359                 var->blue.msb_right = 0;
360
361                 var->transp.length = 0;
362                 var->transp.offset = 0;
363                 var->transp.msb_right = 0;
364                 break;
365         case 16:
366                 var->red.length = 5;
367                 var->red.offset = 11;
368                 var->red.msb_right = 0;
369
370                 var->green.length = 6;
371                 var->green.offset = 5;
372                 var->green.msb_right = 0;
373
374                 var->blue.length = 5;
375                 var->blue.offset = 0;
376                 var->blue.msb_right = 0;
377
378                 var->transp.length = 0;
379                 var->transp.offset = 0;
380                 var->transp.msb_right = 0;
381                 break;
382         case 24:
383                 var->red.length = 8;
384                 var->red.offset = 16;
385                 var->red.msb_right = 0;
386
387                 var->green.length = 8;
388                 var->green.offset = 8;
389                 var->green.msb_right = 0;
390
391                 var->blue.length = 8;
392                 var->blue.offset = 0;
393                 var->blue.msb_right = 0;
394
395                 var->transp.length = 0;
396                 var->transp.offset = 0;
397                 var->transp.msb_right = 0;
398                 break;
399         case 32:
400                 var->red.length = 8;
401                 var->red.offset = 16;
402                 var->red.msb_right = 0;
403
404                 var->green.length = 8;
405                 var->green.offset = 8;
406                 var->green.msb_right = 0;
407
408                 var->blue.length = 8;
409                 var->blue.offset = 0;
410                 var->blue.msb_right = 0;
411
412                 var->transp.length = 8;
413                 var->transp.offset = 24;
414                 var->transp.msb_right = 0;
415                 break;
416         }
417
418         if (var->pixclock < 1000) {
419                 htotal = var->xres + var->right_margin + var->hsync_len +
420                     var->left_margin;
421                 vtotal = var->yres + var->lower_margin + var->vsync_len +
422                     var->upper_margin;
423                 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
424                 var->pixclock = KHZ2PICOS(var->pixclock);
425                 printf("pixclock set for 60Hz refresh = %u ps\n",
426                         var->pixclock);
427         }
428
429         var->height = -1;
430         var->width = -1;
431         var->grayscale = 0;
432
433         return 0;
434 }
435
436 static int mxcfb_map_video_memory(struct fb_info *fbi)
437 {
438         if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
439                 fbi->fix.smem_len = fbi->var.yres_virtual *
440                                     fbi->fix.line_length;
441         }
442
443         fbi->screen_base = (char *)lcd_base;
444         fbi->fix.smem_start = (unsigned long)lcd_base;
445         if (fbi->screen_base == 0) {
446                 puts("Unable to allocate framebuffer memory\n");
447                 fbi->fix.smem_len = 0;
448                 fbi->fix.smem_start = 0;
449                 return -EBUSY;
450         }
451
452         debug("allocated fb @ paddr=0x%08X, size=%d.\n",
453                 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
454
455         fbi->screen_size = fbi->fix.smem_len;
456
457         /* Clear the screen */
458         memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
459
460         return 0;
461 }
462
463 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
464 {
465         fbi->screen_base = 0;
466         fbi->fix.smem_start = 0;
467         fbi->fix.smem_len = 0;
468         return 0;
469 }
470
471 /*
472  * Initializes the framebuffer information pointer. After allocating
473  * sufficient memory for the framebuffer structure, the fields are
474  * filled with custom information passed in from the configurable
475  * structures.  This includes information such as bits per pixel,
476  * color maps, screen width/height and RGBA offsets.
477  *
478  * @return      Framebuffer structure initialized with our information
479  */
480 static struct fb_info *mxcfb_init_fbinfo(void)
481 {
482 #define BYTES_PER_LONG 4
483 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
484         struct fb_info *fbi;
485         struct mxcfb_info *mxcfbi;
486         char *p;
487         int size = sizeof(struct mxcfb_info) + PADDING +
488                 sizeof(struct fb_info);
489
490         debug("%s: %d %d %d %d\n",
491                 __func__,
492                 PADDING,
493                 size,
494                 sizeof(struct mxcfb_info),
495                 sizeof(struct fb_info));
496         /*
497          * Allocate sufficient memory for the fb structure
498          */
499
500         p = malloc(size);
501         if (!p)
502                 return NULL;
503
504         memset(p, 0, size);
505
506         fbi = (struct fb_info *)p;
507         fbi->par = p + sizeof(struct fb_info) + PADDING;
508
509         mxcfbi = (struct mxcfb_info *)fbi->par;
510         debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
511                 (unsigned int)fbi, (unsigned int)mxcfbi);
512
513         fbi->var.activate = FB_ACTIVATE_NOW;
514
515         fbi->flags = FBINFO_FLAG_DEFAULT;
516         fbi->pseudo_palette = mxcfbi->pseudo_palette;
517
518         return fbi;
519 }
520
521 /*
522  * Probe routine for the framebuffer driver. It is called during the
523  * driver binding process.      The following functions are performed in
524  * this routine: Framebuffer initialization, Memory allocation and
525  * mapping, Framebuffer registration, IPU initialization.
526  *
527  * @return      Appropriate error code to the kernel common code
528  */
529 static int mxcfb_probe(u32 interface_pix_fmt, struct fb_videomode *mode)
530 {
531         struct fb_info *fbi;
532         struct mxcfb_info *mxcfbi;
533         int ret = 0;
534
535         /*
536          * Initialize FB structures
537          */
538         fbi = mxcfb_init_fbinfo();
539         if (!fbi) {
540                 ret = -ENOMEM;
541                 goto err0;
542         }
543         mxcfbi = (struct mxcfb_info *)fbi->par;
544
545         if (!g_dp_in_use) {
546                 mxcfbi->ipu_ch = MEM_BG_SYNC;
547                 mxcfbi->blank = FB_BLANK_UNBLANK;
548         } else {
549                 mxcfbi->ipu_ch = MEM_DC_SYNC;
550                 mxcfbi->blank = FB_BLANK_POWERDOWN;
551         }
552
553         mxcfbi->ipu_di = 0;
554
555         ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
556         ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
557         strcpy(fbi->fix.id, "DISP3 BG");
558
559         g_dp_in_use = 1;
560
561         mxcfb_info[mxcfbi->ipu_di] = fbi;
562
563         /* Need dummy values until real panel is configured */
564         fbi->var.xres = 640;
565         fbi->var.yres = 480;
566         fbi->var.bits_per_pixel = 16;
567
568         mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
569         fb_videomode_to_var(&fbi->var, mode);
570
571         mxcfb_check_var(&fbi->var, fbi);
572
573         /* Default Y virtual size is 2x panel size */
574         fbi->var.yres_virtual = fbi->var.yres * 2;
575
576         mxcfb_set_fix(fbi);
577
578         /* alocate fb first */
579         if (mxcfb_map_video_memory(fbi) < 0)
580                 return -ENOMEM;
581
582         mxcfb_set_par(fbi);
583
584         /* Setting panel_info for lcd */
585         panel_info.cmap = NULL;
586         panel_info.vl_col = fbi->var.xres;
587         panel_info.vl_row = fbi->var.yres;
588         panel_info.vl_bpix = LCD_BPP;
589
590         lcd_line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
591
592         debug("MXC IPUV3 configured\n"
593                 "XRES = %d YRES = %d BitsXpixel = %d\n",
594                 panel_info.vl_col,
595                 panel_info.vl_row,
596                 panel_info.vl_bpix);
597
598         ipu_dump_registers();
599
600         return 0;
601
602 err0:
603         return ret;
604 }
605
606 int overwrite_console(void)
607 {
608         /* Keep stdout / stderr on serial, our LCD is for splashscreen only */
609         return 1;
610 }
611
612 void lcd_ctrl_init(void *lcdbase)
613 {
614         u32 mem_len = panel_info.vl_col *
615                 panel_info.vl_row *
616                 NBITS(panel_info.vl_bpix) / 8;
617
618         /*
619          * We rely on lcdbase being a physical address, i.e., either MMU off,
620          * or 1-to-1 mapping. Might want to add some virt2phys here.
621          */
622         if (!lcdbase)
623                 return;
624
625         memset(lcdbase, 0, mem_len);
626 }
627
628 int mx51_fb_init(struct fb_videomode *mode)
629 {
630         int ret;
631
632         ret = ipu_probe();
633         if (ret)
634                 puts("Error initializing IPU\n");
635
636         lcd_base += 56;
637
638         debug("Framebuffer at 0x%x\n", (unsigned int)lcd_base);
639         ret = mxcfb_probe(IPU_PIX_FMT_RGB666, mode);
640
641         return ret;
642 }