SPL: Add support for loading image from ram in SPL.
[oweals/u-boot.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 #include <common.h>
31 #include <asm/errno.h>
32 #include <linux/string.h>
33 #include <linux/list.h>
34 #include <linux/fb.h>
35 #include <asm/io.h>
36 #include <malloc.h>
37 #include <video_fb.h>
38 #include "videomodes.h"
39 #include "ipu.h"
40 #include "mxcfb.h"
41
42 static int mxcfb_map_video_memory(struct fb_info *fbi);
43 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
44
45 /* graphics setup */
46 static GraphicDevice panel;
47 static struct fb_videomode *gmode;
48 static uint8_t gdisp;
49 static uint32_t gpixfmt;
50
51 void fb_videomode_to_var(struct fb_var_screeninfo *var,
52                          const struct fb_videomode *mode)
53 {
54         var->xres = mode->xres;
55         var->yres = mode->yres;
56         var->xres_virtual = mode->xres;
57         var->yres_virtual = mode->yres;
58         var->xoffset = 0;
59         var->yoffset = 0;
60         var->pixclock = mode->pixclock;
61         var->left_margin = mode->left_margin;
62         var->right_margin = mode->right_margin;
63         var->upper_margin = mode->upper_margin;
64         var->lower_margin = mode->lower_margin;
65         var->hsync_len = mode->hsync_len;
66         var->vsync_len = mode->vsync_len;
67         var->sync = mode->sync;
68         var->vmode = mode->vmode & FB_VMODE_MASK;
69 }
70
71 /*
72  * Structure containing the MXC specific framebuffer information.
73  */
74 struct mxcfb_info {
75         int blank;
76         ipu_channel_t ipu_ch;
77         int ipu_di;
78         u32 ipu_di_pix_fmt;
79         unsigned char overlay;
80         unsigned char alpha_chan_en;
81         dma_addr_t alpha_phy_addr0;
82         dma_addr_t alpha_phy_addr1;
83         void *alpha_virt_addr0;
84         void *alpha_virt_addr1;
85         uint32_t alpha_mem_len;
86         uint32_t cur_ipu_buf;
87         uint32_t cur_ipu_alpha_buf;
88
89         u32 pseudo_palette[16];
90 };
91
92 enum {
93         BOTH_ON,
94         SRC_ON,
95         TGT_ON,
96         BOTH_OFF
97 };
98
99 static unsigned long default_bpp = 16;
100 static unsigned char g_dp_in_use;
101 static struct fb_info *mxcfb_info[3];
102 static int ext_clk_used;
103
104 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
105 {
106         uint32_t pixfmt = 0;
107
108         debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
109
110         if (fbi->var.nonstd)
111                 return fbi->var.nonstd;
112
113         switch (fbi->var.bits_per_pixel) {
114         case 24:
115                 pixfmt = IPU_PIX_FMT_BGR24;
116                 break;
117         case 32:
118                 pixfmt = IPU_PIX_FMT_BGR32;
119                 break;
120         case 16:
121                 pixfmt = IPU_PIX_FMT_RGB565;
122                 break;
123         }
124         return pixfmt;
125 }
126
127 /*
128  * Set fixed framebuffer parameters based on variable settings.
129  *
130  * @param       info     framebuffer information pointer
131  */
132 static int mxcfb_set_fix(struct fb_info *info)
133 {
134         struct fb_fix_screeninfo *fix = &info->fix;
135         struct fb_var_screeninfo *var = &info->var;
136
137         fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
138
139         fix->type = FB_TYPE_PACKED_PIXELS;
140         fix->accel = FB_ACCEL_NONE;
141         fix->visual = FB_VISUAL_TRUECOLOR;
142         fix->xpanstep = 1;
143         fix->ypanstep = 1;
144
145         return 0;
146 }
147
148 static int setup_disp_channel1(struct fb_info *fbi)
149 {
150         ipu_channel_params_t params;
151         struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
152
153         memset(&params, 0, sizeof(params));
154         params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
155
156         debug("%s called\n", __func__);
157         /*
158          * Assuming interlaced means yuv output, below setting also
159          * valid for mem_dc_sync. FG should have the same vmode as BG.
160          */
161         if (fbi->var.vmode & FB_VMODE_INTERLACED) {
162                 params.mem_dp_bg_sync.interlaced = 1;
163                 params.mem_dp_bg_sync.out_pixel_fmt =
164                         IPU_PIX_FMT_YUV444;
165         } else {
166                 if (mxc_fbi->ipu_di_pix_fmt) {
167                         params.mem_dp_bg_sync.out_pixel_fmt =
168                                 mxc_fbi->ipu_di_pix_fmt;
169                 } else {
170                         params.mem_dp_bg_sync.out_pixel_fmt =
171                                 IPU_PIX_FMT_RGB666;
172                 }
173         }
174         params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
175         if (mxc_fbi->alpha_chan_en)
176                 params.mem_dp_bg_sync.alpha_chan_en = 1;
177
178         ipu_init_channel(mxc_fbi->ipu_ch, &params);
179
180         return 0;
181 }
182
183 static int setup_disp_channel2(struct fb_info *fbi)
184 {
185         int retval = 0;
186         struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
187
188         mxc_fbi->cur_ipu_buf = 1;
189         if (mxc_fbi->alpha_chan_en)
190                 mxc_fbi->cur_ipu_alpha_buf = 1;
191
192         fbi->var.xoffset = fbi->var.yoffset = 0;
193
194         debug("%s: %x %d %d %d %lx %lx\n",
195                 __func__,
196                 mxc_fbi->ipu_ch,
197                 fbi->var.xres,
198                 fbi->var.yres,
199                 fbi->fix.line_length,
200                 fbi->fix.smem_start,
201                 fbi->fix.smem_start +
202                 (fbi->fix.line_length * fbi->var.yres));
203
204         retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
205                                          bpp_to_pixfmt(fbi),
206                                          fbi->var.xres, fbi->var.yres,
207                                          fbi->fix.line_length,
208                                          fbi->fix.smem_start +
209                                          (fbi->fix.line_length * fbi->var.yres),
210                                          fbi->fix.smem_start,
211                                          0, 0);
212         if (retval)
213                 printf("ipu_init_channel_buffer error %d\n", retval);
214
215         return retval;
216 }
217
218 /*
219  * Set framebuffer parameters and change the operating mode.
220  *
221  * @param       info     framebuffer information pointer
222  */
223 static int mxcfb_set_par(struct fb_info *fbi)
224 {
225         int retval = 0;
226         u32 mem_len;
227         ipu_di_signal_cfg_t sig_cfg;
228         struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
229         uint32_t out_pixel_fmt;
230
231         ipu_disable_channel(mxc_fbi->ipu_ch);
232         ipu_uninit_channel(mxc_fbi->ipu_ch);
233         mxcfb_set_fix(fbi);
234
235         mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
236         if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
237                 if (fbi->fix.smem_start)
238                         mxcfb_unmap_video_memory(fbi);
239
240                 if (mxcfb_map_video_memory(fbi) < 0)
241                         return -ENOMEM;
242         }
243
244         setup_disp_channel1(fbi);
245
246         memset(&sig_cfg, 0, sizeof(sig_cfg));
247         if (fbi->var.vmode & FB_VMODE_INTERLACED) {
248                 sig_cfg.interlaced = 1;
249                 out_pixel_fmt = IPU_PIX_FMT_YUV444;
250         } else {
251                 if (mxc_fbi->ipu_di_pix_fmt)
252                         out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
253                 else
254                         out_pixel_fmt = IPU_PIX_FMT_RGB666;
255         }
256         if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
257                 sig_cfg.odd_field_first = 1;
258         if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
259                 sig_cfg.ext_clk = 1;
260         if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
261                 sig_cfg.Hsync_pol = 1;
262         if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
263                 sig_cfg.Vsync_pol = 1;
264         if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
265                 sig_cfg.clk_pol = 1;
266         if (fbi->var.sync & FB_SYNC_DATA_INVERT)
267                 sig_cfg.data_pol = 1;
268         if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
269                 sig_cfg.enable_pol = 1;
270         if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
271                 sig_cfg.clkidle_en = 1;
272
273         debug("pixclock = %ul Hz\n",
274                 (u32) (PICOS2KHZ(fbi->var.pixclock) * 1000UL));
275
276         if (ipu_init_sync_panel(mxc_fbi->ipu_di,
277                                 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
278                                 fbi->var.xres, fbi->var.yres,
279                                 out_pixel_fmt,
280                                 fbi->var.left_margin,
281                                 fbi->var.hsync_len,
282                                 fbi->var.right_margin,
283                                 fbi->var.upper_margin,
284                                 fbi->var.vsync_len,
285                                 fbi->var.lower_margin,
286                                 0, sig_cfg) != 0) {
287                 puts("mxcfb: Error initializing panel.\n");
288                 return -EINVAL;
289         }
290
291         retval = setup_disp_channel2(fbi);
292         if (retval)
293                 return retval;
294
295         if (mxc_fbi->blank == FB_BLANK_UNBLANK)
296                 ipu_enable_channel(mxc_fbi->ipu_ch);
297
298         return retval;
299 }
300
301 /*
302  * Check framebuffer variable parameters and adjust to valid values.
303  *
304  * @param       var      framebuffer variable parameters
305  *
306  * @param       info     framebuffer information pointer
307  */
308 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
309 {
310         u32 vtotal;
311         u32 htotal;
312
313         if (var->xres_virtual < var->xres)
314                 var->xres_virtual = var->xres;
315         if (var->yres_virtual < var->yres)
316                 var->yres_virtual = var->yres;
317
318         if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
319             (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
320                 var->bits_per_pixel = default_bpp;
321
322         switch (var->bits_per_pixel) {
323         case 8:
324                 var->red.length = 3;
325                 var->red.offset = 5;
326                 var->red.msb_right = 0;
327
328                 var->green.length = 3;
329                 var->green.offset = 2;
330                 var->green.msb_right = 0;
331
332                 var->blue.length = 2;
333                 var->blue.offset = 0;
334                 var->blue.msb_right = 0;
335
336                 var->transp.length = 0;
337                 var->transp.offset = 0;
338                 var->transp.msb_right = 0;
339                 break;
340         case 16:
341                 var->red.length = 5;
342                 var->red.offset = 11;
343                 var->red.msb_right = 0;
344
345                 var->green.length = 6;
346                 var->green.offset = 5;
347                 var->green.msb_right = 0;
348
349                 var->blue.length = 5;
350                 var->blue.offset = 0;
351                 var->blue.msb_right = 0;
352
353                 var->transp.length = 0;
354                 var->transp.offset = 0;
355                 var->transp.msb_right = 0;
356                 break;
357         case 24:
358                 var->red.length = 8;
359                 var->red.offset = 16;
360                 var->red.msb_right = 0;
361
362                 var->green.length = 8;
363                 var->green.offset = 8;
364                 var->green.msb_right = 0;
365
366                 var->blue.length = 8;
367                 var->blue.offset = 0;
368                 var->blue.msb_right = 0;
369
370                 var->transp.length = 0;
371                 var->transp.offset = 0;
372                 var->transp.msb_right = 0;
373                 break;
374         case 32:
375                 var->red.length = 8;
376                 var->red.offset = 16;
377                 var->red.msb_right = 0;
378
379                 var->green.length = 8;
380                 var->green.offset = 8;
381                 var->green.msb_right = 0;
382
383                 var->blue.length = 8;
384                 var->blue.offset = 0;
385                 var->blue.msb_right = 0;
386
387                 var->transp.length = 8;
388                 var->transp.offset = 24;
389                 var->transp.msb_right = 0;
390                 break;
391         }
392
393         if (var->pixclock < 1000) {
394                 htotal = var->xres + var->right_margin + var->hsync_len +
395                     var->left_margin;
396                 vtotal = var->yres + var->lower_margin + var->vsync_len +
397                     var->upper_margin;
398                 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
399                 var->pixclock = KHZ2PICOS(var->pixclock);
400                 printf("pixclock set for 60Hz refresh = %u ps\n",
401                         var->pixclock);
402         }
403
404         var->height = -1;
405         var->width = -1;
406         var->grayscale = 0;
407
408         return 0;
409 }
410
411 static int mxcfb_map_video_memory(struct fb_info *fbi)
412 {
413         if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
414                 fbi->fix.smem_len = fbi->var.yres_virtual *
415                                     fbi->fix.line_length;
416         }
417
418         fbi->screen_base = (char *)malloc(fbi->fix.smem_len);
419         fbi->fix.smem_start = (unsigned long)fbi->screen_base;
420         if (fbi->screen_base == 0) {
421                 puts("Unable to allocate framebuffer memory\n");
422                 fbi->fix.smem_len = 0;
423                 fbi->fix.smem_start = 0;
424                 return -EBUSY;
425         }
426
427         debug("allocated fb @ paddr=0x%08X, size=%d.\n",
428                 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
429
430         fbi->screen_size = fbi->fix.smem_len;
431
432         /* Clear the screen */
433         memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
434
435         return 0;
436 }
437
438 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
439 {
440         fbi->screen_base = 0;
441         fbi->fix.smem_start = 0;
442         fbi->fix.smem_len = 0;
443         return 0;
444 }
445
446 /*
447  * Initializes the framebuffer information pointer. After allocating
448  * sufficient memory for the framebuffer structure, the fields are
449  * filled with custom information passed in from the configurable
450  * structures.  This includes information such as bits per pixel,
451  * color maps, screen width/height and RGBA offsets.
452  *
453  * @return      Framebuffer structure initialized with our information
454  */
455 static struct fb_info *mxcfb_init_fbinfo(void)
456 {
457 #define BYTES_PER_LONG 4
458 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
459         struct fb_info *fbi;
460         struct mxcfb_info *mxcfbi;
461         char *p;
462         int size = sizeof(struct mxcfb_info) + PADDING +
463                 sizeof(struct fb_info);
464
465         debug("%s: %d %d %d %d\n",
466                 __func__,
467                 PADDING,
468                 size,
469                 sizeof(struct mxcfb_info),
470                 sizeof(struct fb_info));
471         /*
472          * Allocate sufficient memory for the fb structure
473          */
474
475         p = malloc(size);
476         if (!p)
477                 return NULL;
478
479         memset(p, 0, size);
480
481         fbi = (struct fb_info *)p;
482         fbi->par = p + sizeof(struct fb_info) + PADDING;
483
484         mxcfbi = (struct mxcfb_info *)fbi->par;
485         debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
486                 (unsigned int)fbi, (unsigned int)mxcfbi);
487
488         fbi->var.activate = FB_ACTIVATE_NOW;
489
490         fbi->flags = FBINFO_FLAG_DEFAULT;
491         fbi->pseudo_palette = mxcfbi->pseudo_palette;
492
493         return fbi;
494 }
495
496 /*
497  * Probe routine for the framebuffer driver. It is called during the
498  * driver binding process.      The following functions are performed in
499  * this routine: Framebuffer initialization, Memory allocation and
500  * mapping, Framebuffer registration, IPU initialization.
501  *
502  * @return      Appropriate error code to the kernel common code
503  */
504 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
505                         struct fb_videomode *mode)
506 {
507         struct fb_info *fbi;
508         struct mxcfb_info *mxcfbi;
509         int ret = 0;
510
511         /*
512          * Initialize FB structures
513          */
514         fbi = mxcfb_init_fbinfo();
515         if (!fbi) {
516                 ret = -ENOMEM;
517                 goto err0;
518         }
519         mxcfbi = (struct mxcfb_info *)fbi->par;
520
521         if (!g_dp_in_use) {
522                 mxcfbi->ipu_ch = MEM_BG_SYNC;
523                 mxcfbi->blank = FB_BLANK_UNBLANK;
524         } else {
525                 mxcfbi->ipu_ch = MEM_DC_SYNC;
526                 mxcfbi->blank = FB_BLANK_POWERDOWN;
527         }
528
529         mxcfbi->ipu_di = disp;
530
531         ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
532         ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
533         strcpy(fbi->fix.id, "DISP3 BG");
534
535         g_dp_in_use = 1;
536
537         mxcfb_info[mxcfbi->ipu_di] = fbi;
538
539         /* Need dummy values until real panel is configured */
540
541         mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
542         fb_videomode_to_var(&fbi->var, mode);
543         fbi->var.bits_per_pixel = 16;
544         fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
545         fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
546
547         mxcfb_check_var(&fbi->var, fbi);
548
549         /* Default Y virtual size is 2x panel size */
550         fbi->var.yres_virtual = fbi->var.yres * 2;
551
552         mxcfb_set_fix(fbi);
553
554         /* alocate fb first */
555         if (mxcfb_map_video_memory(fbi) < 0)
556                 return -ENOMEM;
557
558         mxcfb_set_par(fbi);
559
560         panel.winSizeX = mode->xres;
561         panel.winSizeY = mode->yres;
562         panel.plnSizeX = mode->xres;
563         panel.plnSizeY = mode->yres;
564
565         panel.frameAdrs = (u32)fbi->screen_base;
566         panel.memSize = fbi->screen_size;
567
568         panel.gdfBytesPP = 2;
569         panel.gdfIndex = GDF_16BIT_565RGB;
570
571         ipu_dump_registers();
572
573         return 0;
574
575 err0:
576         return ret;
577 }
578
579 void *video_hw_init(void)
580 {
581         int ret;
582
583         ret = ipu_probe();
584         if (ret)
585                 puts("Error initializing IPU\n");
586
587         ret = mxcfb_probe(gpixfmt, gdisp, gmode);
588         debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
589
590         return (void *)&panel;
591 }
592
593 void video_set_lut(unsigned int index, /* color number */
594                         unsigned char r,    /* red */
595                         unsigned char g,    /* green */
596                         unsigned char b     /* blue */
597                         )
598 {
599         return;
600 }
601
602 int ipuv3_fb_init(struct fb_videomode *mode, uint8_t disp, uint32_t pixfmt)
603 {
604         gmode = mode;
605         gdisp = disp;
606         gpixfmt = pixfmt;
607
608         return 0;
609 }