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