xref: /rk3399_rockchip-uboot/common/lcd.c (revision 0ee261f6d3f387a33687bff29342e9321e2f8194)
1 /*
2  * Common LCD routines for supported CPUs
3  *
4  * (C) Copyright 2001-2002
5  * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 /************************************************************************/
11 /* ** HEADER FILES							*/
12 /************************************************************************/
13 
14 /* #define DEBUG */
15 
16 #include <config.h>
17 #include <common.h>
18 #include <command.h>
19 #include <stdarg.h>
20 #include <search.h>
21 #include <env_callback.h>
22 #include <linux/types.h>
23 #include <stdio_dev.h>
24 #if defined(CONFIG_POST)
25 #include <post.h>
26 #endif
27 #include <lcd.h>
28 #include <watchdog.h>
29 #include <asm/unaligned.h>
30 #include <splash.h>
31 #include <asm/io.h>
32 #include <asm/unaligned.h>
33 #include <fdt_support.h>
34 
35 #if defined(CONFIG_CPU_PXA25X) || defined(CONFIG_CPU_PXA27X) || \
36 	defined(CONFIG_CPU_MONAHANS)
37 #include <asm/byteorder.h>
38 #endif
39 
40 #if defined(CONFIG_MPC823)
41 #include <lcdvideo.h>
42 #endif
43 
44 #if defined(CONFIG_ATMEL_LCD)
45 #include <atmel_lcdc.h>
46 #endif
47 
48 #if defined(CONFIG_LCD_DT_SIMPLEFB)
49 #include <libfdt.h>
50 #endif
51 
52 /************************************************************************/
53 /* ** FONT DATA								*/
54 /************************************************************************/
55 #include <video_font.h>		/* Get font data, width and height	*/
56 
57 /************************************************************************/
58 /* ** LOGO DATA								*/
59 /************************************************************************/
60 #ifdef CONFIG_LCD_LOGO
61 # include <bmp_logo.h>		/* Get logo data, width and height	*/
62 # include <bmp_logo_data.h>
63 # if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
64 #  error Default Color Map overlaps with Logo Color Map
65 # endif
66 #endif
67 
68 #ifdef CONFIG_SANDBOX
69 #include <asm/sdl.h>
70 #endif
71 
72 #ifndef CONFIG_LCD_ALIGNMENT
73 #define CONFIG_LCD_ALIGNMENT PAGE_SIZE
74 #endif
75 
76 #if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
77 	(LCD_BPP != LCD_COLOR32)
78 # error Unsupported LCD BPP.
79 #endif
80 
81 DECLARE_GLOBAL_DATA_PTR;
82 
83 static int lcd_init(void *lcdbase);
84 
85 static void *lcd_logo(void);
86 
87 static void lcd_setfgcolor(int color);
88 static void lcd_setbgcolor(int color);
89 
90 static int lcd_color_fg;
91 static int lcd_color_bg;
92 int lcd_line_length;
93 
94 char lcd_is_enabled = 0;
95 
96 static void *lcd_base;			/* Start of framebuffer memory	*/
97 
98 static char lcd_flush_dcache;	/* 1 to flush dcache after each lcd update */
99 
100 /************************************************************************/
101 
102 /* Flush LCD activity to the caches */
103 void lcd_sync(void)
104 {
105 	/*
106 	 * flush_dcache_range() is declared in common.h but it seems that some
107 	 * architectures do not actually implement it. Is there a way to find
108 	 * out whether it exists? For now, ARM is safe.
109 	 */
110 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
111 	int line_length;
112 
113 	if (lcd_flush_dcache)
114 		flush_dcache_range((u32)lcd_base,
115 			(u32)(lcd_base + lcd_get_size(&line_length)));
116 #elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
117 	static ulong last_sync;
118 
119 	if (get_timer(last_sync) > 10) {
120 		sandbox_sdl_sync(lcd_base);
121 		last_sync = get_timer(0);
122 	}
123 #endif
124 }
125 
126 void lcd_set_flush_dcache(int flush)
127 {
128 	lcd_flush_dcache = (flush != 0);
129 }
130 
131 /*----------------------------------------------------------------------*/
132 
133 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
134 {
135 	lcd_putc(c);
136 }
137 
138 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
139 {
140 	lcd_puts(s);
141 }
142 
143 /************************************************************************/
144 /**  Small utility to check that you got the colours right		*/
145 /************************************************************************/
146 #ifdef LCD_TEST_PATTERN
147 
148 #define	N_BLK_VERT	2
149 #define	N_BLK_HOR	3
150 
151 static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
152 	CONSOLE_COLOR_RED,	CONSOLE_COLOR_GREEN,	CONSOLE_COLOR_YELLOW,
153 	CONSOLE_COLOR_BLUE,	CONSOLE_COLOR_MAGENTA,	CONSOLE_COLOR_CYAN,
154 };
155 
156 static void test_pattern(void)
157 {
158 	ushort v_max  = panel_info.vl_row;
159 	ushort h_max  = panel_info.vl_col;
160 	ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
161 	ushort h_step = (h_max + N_BLK_HOR  - 1) / N_BLK_HOR;
162 	ushort v, h;
163 	uchar *pix = (uchar *)lcd_base;
164 
165 	printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
166 		h_max, v_max, h_step, v_step);
167 
168 	/* WARNING: Code silently assumes 8bit/pixel */
169 	for (v = 0; v < v_max; ++v) {
170 		uchar iy = v / v_step;
171 		for (h = 0; h < h_max; ++h) {
172 			uchar ix = N_BLK_HOR * iy + h / h_step;
173 			*pix++ = test_colors[ix];
174 		}
175 	}
176 }
177 #endif /* LCD_TEST_PATTERN */
178 
179 
180 /************************************************************************/
181 /* ** GENERIC Initialization Routines					*/
182 /************************************************************************/
183 /*
184  * With most lcd drivers the line length is set up
185  * by calculating it from panel_info parameters. Some
186  * drivers need to calculate the line length differently,
187  * so make the function weak to allow overriding it.
188  */
189 __weak int lcd_get_size(int *line_length)
190 {
191 	*line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
192 	return *line_length * panel_info.vl_row;
193 }
194 
195 int drv_lcd_init(void)
196 {
197 	struct stdio_dev lcddev;
198 	int rc;
199 
200 	lcd_base = map_sysmem(gd->fb_base, 0);
201 
202 	lcd_init(lcd_base);		/* LCD initialization */
203 
204 	/* Device initialization */
205 	memset(&lcddev, 0, sizeof(lcddev));
206 
207 	strcpy(lcddev.name, "lcd");
208 	lcddev.ext   = 0;			/* No extensions */
209 	lcddev.flags = DEV_FLAGS_OUTPUT;	/* Output only */
210 	lcddev.putc  = lcd_stub_putc;		/* 'putc' function */
211 	lcddev.puts  = lcd_stub_puts;		/* 'puts' function */
212 
213 	rc = stdio_register(&lcddev);
214 
215 	return (rc == 0) ? 1 : rc;
216 }
217 
218 /*----------------------------------------------------------------------*/
219 void lcd_clear(void)
220 {
221 	short console_rows, console_cols;
222 	int bg_color;
223 #if LCD_BPP == LCD_COLOR8
224 	/* Setting the palette */
225 	lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
226 	lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
227 	lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
228 	lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
229 	lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
230 	lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
231 	lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
232 	lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
233 	lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
234 #endif
235 
236 #ifndef CONFIG_SYS_WHITE_ON_BLACK
237 	lcd_setfgcolor(CONSOLE_COLOR_BLACK);
238 	lcd_setbgcolor(CONSOLE_COLOR_WHITE);
239 	bg_color = CONSOLE_COLOR_WHITE;
240 #else
241 	lcd_setfgcolor(CONSOLE_COLOR_WHITE);
242 	lcd_setbgcolor(CONSOLE_COLOR_BLACK);
243 	bg_color = CONSOLE_COLOR_BLACK;
244 #endif	/* CONFIG_SYS_WHITE_ON_BLACK */
245 
246 #ifdef	LCD_TEST_PATTERN
247 	test_pattern();
248 #else
249 	/* set framebuffer to background color */
250 #if (LCD_BPP != LCD_COLOR32)
251 	memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
252 #else
253 	u32 *ppix = lcd_base;
254 	u32 i;
255 	for (i = 0;
256 	   i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
257 	   i++) {
258 		*ppix++ = bg_color;
259 	}
260 #endif
261 #endif
262 	/* Paint the logo and retrieve LCD base address */
263 	debug("[LCD] Drawing the logo...\n");
264 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
265 	console_rows = (panel_info.vl_row - BMP_LOGO_HEIGHT);
266 	console_rows /= VIDEO_FONT_HEIGHT;
267 #else
268 	console_rows = panel_info.vl_row / VIDEO_FONT_HEIGHT;
269 #endif
270 	console_cols = panel_info.vl_col / VIDEO_FONT_WIDTH;
271 	lcd_init_console(lcd_base, console_rows, console_cols);
272 	lcd_init_console(lcd_logo(), console_rows, console_cols);
273 	lcd_sync();
274 }
275 
276 static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
277 			char *const argv[])
278 {
279 	lcd_clear();
280 	return 0;
281 }
282 
283 U_BOOT_CMD(
284 	cls,	1,	1,	do_lcd_clear,
285 	"clear screen",
286 	""
287 );
288 
289 /*----------------------------------------------------------------------*/
290 
291 static int lcd_init(void *lcdbase)
292 {
293 	/* Initialize the lcd controller */
294 	debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
295 
296 	lcd_ctrl_init(lcdbase);
297 
298 	/*
299 	 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
300 	 * the 'lcdbase' argument and uses custom lcd base address
301 	 * by setting up gd->fb_base. Check for this condition and fixup
302 	 * 'lcd_base' address.
303 	 */
304 	if (map_to_sysmem(lcdbase) != gd->fb_base)
305 		lcd_base = map_sysmem(gd->fb_base, 0);
306 
307 	debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
308 
309 	lcd_get_size(&lcd_line_length);
310 	lcd_is_enabled = 1;
311 	lcd_clear();
312 	lcd_enable();
313 
314 	/* Initialize the console */
315 	lcd_set_col(0);
316 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
317 	lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
318 #else
319 	lcd_set_row(1);	/* leave 1 blank line below logo */
320 #endif
321 
322 	return 0;
323 }
324 
325 
326 /************************************************************************/
327 /* ** ROM capable initialization part - needed to reserve FB memory	*/
328 /************************************************************************/
329 /*
330  * This is called early in the system initialization to grab memory
331  * for the LCD controller.
332  * Returns new address for monitor, after reserving LCD buffer memory
333  *
334  * Note that this is running from ROM, so no write access to global data.
335  */
336 ulong lcd_setmem(ulong addr)
337 {
338 	ulong size;
339 	int line_length;
340 
341 	debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
342 		panel_info.vl_row, NBITS(panel_info.vl_bpix));
343 
344 	size = lcd_get_size(&line_length);
345 
346 	/* Round up to nearest full page, or MMU section if defined */
347 	size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
348 	addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
349 
350 	/* Allocate pages for the frame buffer. */
351 	addr -= size;
352 
353 	debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
354 	      size >> 10, addr);
355 
356 	return addr;
357 }
358 
359 /*----------------------------------------------------------------------*/
360 
361 static void lcd_setfgcolor(int color)
362 {
363 	lcd_color_fg = color;
364 }
365 
366 int lcd_getfgcolor(void)
367 {
368 	return lcd_color_fg;
369 }
370 
371 /*----------------------------------------------------------------------*/
372 
373 static void lcd_setbgcolor(int color)
374 {
375 	lcd_color_bg = color;
376 }
377 
378 int lcd_getbgcolor(void)
379 {
380 	return lcd_color_bg;
381 }
382 
383 /************************************************************************/
384 /* ** Chipset depending Bitmap / Logo stuff...                          */
385 /************************************************************************/
386 
387 #ifdef CONFIG_LCD_LOGO
388 __weak void lcd_logo_set_cmap(void)
389 {
390 }
391 
392 void bitmap_plot(int x, int y)
393 {
394 	ushort *cmap = (ushort *)bmp_logo_palette;
395 	ushort i, j;
396 	uchar *bmap;
397 	uchar *fb;
398 	ushort *fb16;
399 	unsigned bpix = NBITS(panel_info.vl_bpix);
400 
401 	debug("Logo: width %d  height %d  colors %d  cmap %d\n",
402 		BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS,
403 		ARRAY_SIZE(bmp_logo_palette));
404 
405 	bmap = &bmp_logo_bitmap[0];
406 	fb   = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
407 
408 	if (bpix < 12) {
409 		/* Leave room for default color map
410 		 * default case: generic system with no cmap (most likely 16bpp)
411 		 * cmap was set to the source palette, so no change is done.
412 		 * This avoids even more ifdefs in the next stanza
413 		 */
414 		cmap = configuration_get_cmap();
415 
416 		WATCHDOG_RESET();
417 
418 		/* Set color map */
419 #if defined(CONFIG_ATMEL_LCD) || defined(CONFIG_MPC823)
420 		lcd_logo_set_cmap();
421 #else
422 		for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i) {
423 			ushort colreg = bmp_logo_palette[i];
424 			*cmap++ = colreg;
425 		}
426 #endif
427 
428 		WATCHDOG_RESET();
429 
430 		for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
431 			memcpy(fb, bmap, BMP_LOGO_WIDTH);
432 			bmap += BMP_LOGO_WIDTH;
433 			fb += panel_info.vl_col;
434 		}
435 	}
436 	else { /* true color mode */
437 		u16 col16;
438 		fb16 = (ushort *)fb;
439 		for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
440 			for (j = 0; j < BMP_LOGO_WIDTH; j++) {
441 				col16 = bmp_logo_palette[(bmap[j]-16)];
442 				fb16[j] =
443 					((col16 & 0x000F) << 1) |
444 					((col16 & 0x00F0) << 3) |
445 					((col16 & 0x0F00) << 4);
446 				}
447 			bmap += BMP_LOGO_WIDTH;
448 			fb16 += panel_info.vl_col;
449 		}
450 	}
451 
452 	WATCHDOG_RESET();
453 	lcd_sync();
454 }
455 #else
456 static inline void bitmap_plot(int x, int y) {}
457 #endif /* CONFIG_LCD_LOGO */
458 
459 /*----------------------------------------------------------------------*/
460 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
461 /*
462  * Display the BMP file located at address bmp_image.
463  * Only uncompressed.
464  */
465 
466 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
467 #define BMP_ALIGN_CENTER	0x7FFF
468 
469 static void splash_align_axis(int *axis, unsigned long panel_size,
470 					unsigned long picture_size)
471 {
472 	unsigned long panel_picture_delta = panel_size - picture_size;
473 	unsigned long axis_alignment;
474 
475 	if (*axis == BMP_ALIGN_CENTER)
476 		axis_alignment = panel_picture_delta / 2;
477 	else if (*axis < 0)
478 		axis_alignment = panel_picture_delta + *axis + 1;
479 	else
480 		return;
481 
482 	*axis = max(0, (int)axis_alignment);
483 }
484 #endif
485 
486 
487 #ifdef CONFIG_LCD_BMP_RLE8
488 
489 #define BMP_RLE8_ESCAPE		0
490 #define BMP_RLE8_EOL		0
491 #define BMP_RLE8_EOBMP		1
492 #define BMP_RLE8_DELTA		2
493 
494 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
495 				  int cnt)
496 {
497 	while (cnt > 0) {
498 		*(*fbp)++ = cmap[*bmap++];
499 		cnt--;
500 	}
501 }
502 
503 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
504 {
505 	ushort *fb = *fbp;
506 	int cnt_8copy = cnt >> 3;
507 
508 	cnt -= cnt_8copy << 3;
509 	while (cnt_8copy > 0) {
510 		*fb++ = c;
511 		*fb++ = c;
512 		*fb++ = c;
513 		*fb++ = c;
514 		*fb++ = c;
515 		*fb++ = c;
516 		*fb++ = c;
517 		*fb++ = c;
518 		cnt_8copy--;
519 	}
520 	while (cnt > 0) {
521 		*fb++ = c;
522 		cnt--;
523 	}
524 	*fbp = fb;
525 }
526 
527 /*
528  * Do not call this function directly, must be called from lcd_display_bitmap.
529  */
530 static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
531 				    int x_off, int y_off)
532 {
533 	uchar *bmap;
534 	ulong width, height;
535 	ulong cnt, runlen;
536 	int x, y;
537 	int decode = 1;
538 
539 	width = get_unaligned_le32(&bmp->header.width);
540 	height = get_unaligned_le32(&bmp->header.height);
541 	bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
542 
543 	x = 0;
544 	y = height - 1;
545 
546 	while (decode) {
547 		if (bmap[0] == BMP_RLE8_ESCAPE) {
548 			switch (bmap[1]) {
549 			case BMP_RLE8_EOL:
550 				/* end of line */
551 				bmap += 2;
552 				x = 0;
553 				y--;
554 				/* 16bpix, 2-byte per pixel, width should *2 */
555 				fb -= (width * 2 + lcd_line_length);
556 				break;
557 			case BMP_RLE8_EOBMP:
558 				/* end of bitmap */
559 				decode = 0;
560 				break;
561 			case BMP_RLE8_DELTA:
562 				/* delta run */
563 				x += bmap[2];
564 				y -= bmap[3];
565 				/* 16bpix, 2-byte per pixel, x should *2 */
566 				fb = (uchar *) (lcd_base + (y + y_off - 1)
567 					* lcd_line_length + (x + x_off) * 2);
568 				bmap += 4;
569 				break;
570 			default:
571 				/* unencoded run */
572 				runlen = bmap[1];
573 				bmap += 2;
574 				if (y < height) {
575 					if (x < width) {
576 						if (x + runlen > width)
577 							cnt = width - x;
578 						else
579 							cnt = runlen;
580 						draw_unencoded_bitmap(
581 							(ushort **)&fb,
582 							bmap, cmap, cnt);
583 					}
584 					x += runlen;
585 				}
586 				bmap += runlen;
587 				if (runlen & 1)
588 					bmap++;
589 			}
590 		} else {
591 			/* encoded run */
592 			if (y < height) {
593 				runlen = bmap[0];
594 				if (x < width) {
595 					/* aggregate the same code */
596 					while (bmap[0] == 0xff &&
597 					       bmap[2] != BMP_RLE8_ESCAPE &&
598 					       bmap[1] == bmap[3]) {
599 						runlen += bmap[2];
600 						bmap += 2;
601 					}
602 					if (x + runlen > width)
603 						cnt = width - x;
604 					else
605 						cnt = runlen;
606 					draw_encoded_bitmap((ushort **)&fb,
607 						cmap[bmap[1]], cnt);
608 				}
609 				x += runlen;
610 			}
611 			bmap += 2;
612 		}
613 	}
614 }
615 #endif
616 
617 __weak void fb_put_byte(uchar **fb, uchar **from)
618 {
619 	*(*fb)++ = *(*from)++;
620 }
621 
622 #if defined(CONFIG_BMP_16BPP)
623 __weak void fb_put_word(uchar **fb, uchar **from)
624 {
625 	*(*fb)++ = *(*from)++;
626 	*(*fb)++ = *(*from)++;
627 }
628 #endif /* CONFIG_BMP_16BPP */
629 
630 int lcd_display_bitmap(ulong bmp_image, int x, int y)
631 {
632 	ushort *cmap = NULL;
633 	ushort *cmap_base = NULL;
634 	ushort i, j;
635 	uchar *fb;
636 	bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
637 	uchar *bmap;
638 	ushort padded_width;
639 	unsigned long width, height, byte_width;
640 	unsigned long pwidth = panel_info.vl_col;
641 	unsigned colors, bpix, bmp_bpix;
642 
643 	if (!bmp || !(bmp->header.signature[0] == 'B' &&
644 		bmp->header.signature[1] == 'M')) {
645 		printf("Error: no valid bmp image at %lx\n", bmp_image);
646 
647 		return 1;
648 	}
649 
650 	width = get_unaligned_le32(&bmp->header.width);
651 	height = get_unaligned_le32(&bmp->header.height);
652 	bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
653 
654 	colors = 1 << bmp_bpix;
655 
656 	bpix = NBITS(panel_info.vl_bpix);
657 
658 	if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
659 		printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
660 			bpix, bmp_bpix);
661 
662 		return 1;
663 	}
664 
665 	/*
666 	 * We support displaying 8bpp BMPs on 16bpp LCDs
667 	 * and displaying 24bpp BMPs on 32bpp LCDs
668 	 * */
669 	if (bpix != bmp_bpix &&
670 	    !(bmp_bpix == 8 && bpix == 16) &&
671 	    !(bmp_bpix == 24 && bpix == 32)) {
672 		printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
673 			bpix, get_unaligned_le16(&bmp->header.bit_count));
674 		return 1;
675 	}
676 
677 	debug("Display-bmp: %d x %d  with %d colors\n",
678 		(int)width, (int)height, (int)colors);
679 
680 	if (bmp_bpix == 8) {
681 		cmap = configuration_get_cmap();
682 		cmap_base = cmap;
683 
684 		/* Set color map */
685 		for (i = 0; i < colors; ++i) {
686 			bmp_color_table_entry_t cte = bmp->color_table[i];
687 #if !defined(CONFIG_ATMEL_LCD)
688 			ushort colreg =
689 				( ((cte.red)   << 8) & 0xf800) |
690 				( ((cte.green) << 3) & 0x07e0) |
691 				( ((cte.blue)  >> 3) & 0x001f) ;
692 			*cmap = colreg;
693 #if defined(CONFIG_MPC823)
694 			cmap--;
695 #else
696 			cmap++;
697 #endif
698 #else /* CONFIG_ATMEL_LCD */
699 			lcd_setcolreg(i, cte.red, cte.green, cte.blue);
700 #endif
701 		}
702 	}
703 
704 	padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
705 
706 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
707 	splash_align_axis(&x, pwidth, width);
708 	splash_align_axis(&y, panel_info.vl_row, height);
709 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
710 
711 	if ((x + width) > pwidth)
712 		width = pwidth - x;
713 	if ((y + height) > panel_info.vl_row)
714 		height = panel_info.vl_row - y;
715 
716 	bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
717 	fb   = (uchar *)(lcd_base +
718 		(y + height - 1) * lcd_line_length + x * bpix / 8);
719 
720 	switch (bmp_bpix) {
721 	case 1: /* pass through */
722 	case 8: {
723 #ifdef CONFIG_LCD_BMP_RLE8
724 		u32 compression = get_unaligned_le32(&bmp->header.compression);
725 		if (compression == BMP_BI_RLE8) {
726 			if (bpix != 16) {
727 				/* TODO implement render code for bpix != 16 */
728 				printf("Error: only support 16 bpix");
729 				return 1;
730 			}
731 			lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
732 			break;
733 		}
734 #endif
735 
736 		if (bpix != 16)
737 			byte_width = width;
738 		else
739 			byte_width = width * 2;
740 
741 		for (i = 0; i < height; ++i) {
742 			WATCHDOG_RESET();
743 			for (j = 0; j < width; j++) {
744 				if (bpix != 16) {
745 					fb_put_byte(&fb, &bmap);
746 				} else {
747 					*(uint16_t *)fb = cmap_base[*(bmap++)];
748 					fb += sizeof(uint16_t) / sizeof(*fb);
749 				}
750 			}
751 			bmap += (padded_width - width);
752 			fb -= byte_width + lcd_line_length;
753 		}
754 		break;
755 	}
756 #if defined(CONFIG_BMP_16BPP)
757 	case 16:
758 		for (i = 0; i < height; ++i) {
759 			WATCHDOG_RESET();
760 			for (j = 0; j < width; j++)
761 				fb_put_word(&fb, &bmap);
762 
763 			bmap += (padded_width - width) * 2;
764 			fb -= width * 2 + lcd_line_length;
765 		}
766 		break;
767 #endif /* CONFIG_BMP_16BPP */
768 #if defined(CONFIG_BMP_24BMP)
769 	case 24:
770 		for (i = 0; i < height; ++i) {
771 			for (j = 0; j < width; j++) {
772 				*(fb++) = *(bmap++);
773 				*(fb++) = *(bmap++);
774 				*(fb++) = *(bmap++);
775 				*(fb++) = 0;
776 			}
777 			fb -= lcd_line_length + width * (bpix / 8);
778 		}
779 		break;
780 #endif /* CONFIG_BMP_24BMP */
781 #if defined(CONFIG_BMP_32BPP)
782 	case 32:
783 		for (i = 0; i < height; ++i) {
784 			for (j = 0; j < width; j++) {
785 				*(fb++) = *(bmap++);
786 				*(fb++) = *(bmap++);
787 				*(fb++) = *(bmap++);
788 				*(fb++) = *(bmap++);
789 			}
790 			fb -= lcd_line_length + width * (bpix / 8);
791 		}
792 		break;
793 #endif /* CONFIG_BMP_32BPP */
794 	default:
795 		break;
796 	};
797 
798 	lcd_sync();
799 	return 0;
800 }
801 #endif
802 
803 static void *lcd_logo(void)
804 {
805 #ifdef CONFIG_SPLASH_SCREEN
806 	char *s;
807 	ulong addr;
808 	static int do_splash = 1;
809 
810 	if (do_splash && (s = getenv("splashimage")) != NULL) {
811 		int x = 0, y = 0;
812 		do_splash = 0;
813 
814 		if (splash_screen_prepare())
815 			return (void *)lcd_base;
816 
817 		addr = simple_strtoul (s, NULL, 16);
818 
819 		splash_get_pos(&x, &y);
820 
821 		if (bmp_display(addr, x, y) == 0)
822 			return (void *)lcd_base;
823 	}
824 #endif /* CONFIG_SPLASH_SCREEN */
825 
826 	bitmap_plot(0, 0);
827 
828 #ifdef CONFIG_LCD_INFO
829 	lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
830 	lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
831 	lcd_show_board_info();
832 #endif /* CONFIG_LCD_INFO */
833 
834 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
835 	return (void *)((ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length);
836 #else
837 	return (void *)lcd_base;
838 #endif /* CONFIG_LCD_LOGO && !defined(CONFIG_LCD_INFO_BELOW_LOGO) */
839 }
840 
841 #ifdef CONFIG_SPLASHIMAGE_GUARD
842 static int on_splashimage(const char *name, const char *value, enum env_op op,
843 	int flags)
844 {
845 	ulong addr;
846 	int aligned;
847 
848 	if (op == env_op_delete)
849 		return 0;
850 
851 	addr = simple_strtoul(value, NULL, 16);
852 	/* See README.displaying-bmps */
853 	aligned = (addr % 4 == 2);
854 	if (!aligned) {
855 		printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
856 		return -1;
857 	}
858 
859 	return 0;
860 }
861 
862 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
863 #endif
864 
865 int lcd_get_pixel_width(void)
866 {
867 	return panel_info.vl_col;
868 }
869 
870 int lcd_get_pixel_height(void)
871 {
872 	return panel_info.vl_row;
873 }
874 
875 #if defined(CONFIG_LCD_DT_SIMPLEFB)
876 static int lcd_dt_simplefb_configure_node(void *blob, int off)
877 {
878 #if LCD_BPP == LCD_COLOR16
879 	return fdt_setup_simplefb_node(blob, off, gd->fb_base,
880 				       panel_info.vl_col, panel_info.vl_row,
881 				       panel_info.vl_col * 2, "r5g6b5");
882 #else
883 	return -1;
884 #endif
885 }
886 
887 int lcd_dt_simplefb_add_node(void *blob)
888 {
889 	static const char compat[] = "simple-framebuffer";
890 	static const char disabled[] = "disabled";
891 	int off, ret;
892 
893 	off = fdt_add_subnode(blob, 0, "framebuffer");
894 	if (off < 0)
895 		return -1;
896 
897 	ret = fdt_setprop(blob, off, "status", disabled, sizeof(disabled));
898 	if (ret < 0)
899 		return -1;
900 
901 	ret = fdt_setprop(blob, off, "compatible", compat, sizeof(compat));
902 	if (ret < 0)
903 		return -1;
904 
905 	return lcd_dt_simplefb_configure_node(blob, off);
906 }
907 
908 int lcd_dt_simplefb_enable_existing_node(void *blob)
909 {
910 	int off;
911 
912 	off = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
913 	if (off < 0)
914 		return -1;
915 
916 	return lcd_dt_simplefb_configure_node(blob, off);
917 }
918 #endif
919