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