xref: /rk3399_rockchip-uboot/common/lcd.c (revision a7de2953f51e70754190d3516167d58d27d17219)
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 /* By default we scroll by a single line */
77 #ifndef CONFIG_CONSOLE_SCROLL_LINES
78 #define CONFIG_CONSOLE_SCROLL_LINES 1
79 #endif
80 
81 /************************************************************************/
82 /* ** CONSOLE DEFINITIONS & FUNCTIONS					*/
83 /************************************************************************/
84 #define CONSOLE_ROW_SIZE	(VIDEO_FONT_HEIGHT * lcd_line_length)
85 #define CONSOLE_ROW_FIRST	lcd_console_address
86 #define CONSOLE_ROW_SECOND	(lcd_console_address + CONSOLE_ROW_SIZE)
87 #define CONSOLE_ROW_LAST	(lcd_console_address + CONSOLE_SIZE \
88 					- CONSOLE_ROW_SIZE)
89 #define CONSOLE_SIZE		(CONSOLE_ROW_SIZE * console_rows)
90 #define CONSOLE_SCROLL_SIZE	(CONSOLE_SIZE - CONSOLE_ROW_SIZE)
91 
92 #if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
93 	(LCD_BPP != LCD_COLOR32)
94 # error Unsupported LCD BPP.
95 #endif
96 
97 DECLARE_GLOBAL_DATA_PTR;
98 
99 static void lcd_drawchars(ushort x, ushort y, uchar *str, int count);
100 static inline void lcd_putc_xy(ushort x, ushort y, uchar  c);
101 
102 static int lcd_init(void *lcdbase);
103 
104 static void *lcd_logo(void);
105 
106 static void lcd_setfgcolor(int color);
107 static void lcd_setbgcolor(int color);
108 
109 static int lcd_color_fg;
110 static int lcd_color_bg;
111 int lcd_line_length;
112 
113 char lcd_is_enabled = 0;
114 
115 static short console_curr_col;
116 static short console_curr_row;
117 static short console_cols;
118 static short console_rows;
119 
120 static void *lcd_console_address;
121 static void *lcd_base;			/* Start of framebuffer memory	*/
122 
123 static char lcd_flush_dcache;	/* 1 to flush dcache after each lcd update */
124 
125 /************************************************************************/
126 
127 /* Flush LCD activity to the caches */
128 void lcd_sync(void)
129 {
130 	/*
131 	 * flush_dcache_range() is declared in common.h but it seems that some
132 	 * architectures do not actually implement it. Is there a way to find
133 	 * out whether it exists? For now, ARM is safe.
134 	 */
135 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
136 	int line_length;
137 
138 	if (lcd_flush_dcache)
139 		flush_dcache_range((u32)lcd_base,
140 			(u32)(lcd_base + lcd_get_size(&line_length)));
141 #elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
142 	static ulong last_sync;
143 
144 	if (get_timer(last_sync) > 10) {
145 		sandbox_sdl_sync(lcd_base);
146 		last_sync = get_timer(0);
147 	}
148 #endif
149 }
150 
151 void lcd_set_flush_dcache(int flush)
152 {
153 	lcd_flush_dcache = (flush != 0);
154 }
155 
156 void lcd_init_console(void *address, int rows, int cols)
157 {
158 	console_curr_col = 0;
159 	console_curr_row = 0;
160 	console_cols = cols;
161 	console_rows = rows;
162 	lcd_console_address = address;
163 }
164 
165 void lcd_set_col(short col)
166 {
167 	console_curr_col = col;
168 }
169 
170 void lcd_set_row(short row)
171 {
172 	console_curr_row = row;
173 }
174 
175 /*----------------------------------------------------------------------*/
176 
177 static void console_scrollup(void)
178 {
179 	const int rows = CONFIG_CONSOLE_SCROLL_LINES;
180 
181 	/* Copy up rows ignoring those that will be overwritten */
182 	memcpy(CONSOLE_ROW_FIRST,
183 	       lcd_console_address + CONSOLE_ROW_SIZE * rows,
184 	       CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows);
185 
186 	/* Clear the last rows */
187 #if (LCD_BPP != LCD_COLOR32)
188 	memset(lcd_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows,
189 		lcd_color_bg,
190 		CONSOLE_ROW_SIZE * rows);
191 #else
192 	u32 *ppix = lcd_console_address +
193 		    CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows;
194 	u32 i;
195 	for (i = 0;
196 	    i < (CONSOLE_ROW_SIZE * rows) / NBYTES(panel_info.vl_bpix);
197 	    i++) {
198 		*ppix++ = lcd_color_bg;
199 	}
200 #endif
201 	lcd_sync();
202 	console_curr_row -= rows;
203 }
204 
205 /*----------------------------------------------------------------------*/
206 
207 static inline void console_back(void)
208 {
209 	if (--console_curr_col < 0) {
210 		console_curr_col = console_cols - 1;
211 		if (--console_curr_row < 0)
212 			console_curr_row = 0;
213 	}
214 
215 	lcd_putc_xy(console_curr_col * VIDEO_FONT_WIDTH,
216 		    console_curr_row * VIDEO_FONT_HEIGHT, ' ');
217 }
218 
219 /*----------------------------------------------------------------------*/
220 
221 static inline void console_newline(void)
222 {
223 	console_curr_col = 0;
224 
225 	/* Check if we need to scroll the terminal */
226 	if (++console_curr_row >= console_rows)
227 		console_scrollup();
228 	else
229 		lcd_sync();
230 }
231 
232 /*----------------------------------------------------------------------*/
233 
234 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
235 {
236 	lcd_putc(c);
237 }
238 
239 void lcd_putc(const char c)
240 {
241 	if (!lcd_is_enabled) {
242 		serial_putc(c);
243 
244 		return;
245 	}
246 
247 	switch (c) {
248 	case '\r':
249 		console_curr_col = 0;
250 
251 		return;
252 	case '\n':
253 		console_newline();
254 
255 		return;
256 	case '\t':	/* Tab (8 chars alignment) */
257 		console_curr_col +=  8;
258 		console_curr_col &= ~7;
259 
260 		if (console_curr_col >= console_cols)
261 			console_newline();
262 
263 		return;
264 	case '\b':
265 		console_back();
266 
267 		return;
268 	default:
269 		lcd_putc_xy(console_curr_col * VIDEO_FONT_WIDTH,
270 			    console_curr_row * VIDEO_FONT_HEIGHT, c);
271 		if (++console_curr_col >= console_cols)
272 			console_newline();
273 	}
274 }
275 
276 /*----------------------------------------------------------------------*/
277 
278 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
279 {
280 	lcd_puts(s);
281 }
282 
283 void lcd_puts(const char *s)
284 {
285 	if (!lcd_is_enabled) {
286 		serial_puts(s);
287 
288 		return;
289 	}
290 
291 	while (*s)
292 		lcd_putc(*s++);
293 
294 	lcd_sync();
295 }
296 
297 /*----------------------------------------------------------------------*/
298 
299 void lcd_printf(const char *fmt, ...)
300 {
301 	va_list args;
302 	char buf[CONFIG_SYS_PBSIZE];
303 
304 	va_start(args, fmt);
305 	vsprintf(buf, fmt, args);
306 	va_end(args);
307 
308 	lcd_puts(buf);
309 }
310 
311 /************************************************************************/
312 /* ** Low-Level Graphics Routines					*/
313 /************************************************************************/
314 
315 static void lcd_drawchars(ushort x, ushort y, uchar *str, int count)
316 {
317 	uchar *dest;
318 	ushort row;
319 
320 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
321 	y += BMP_LOGO_HEIGHT;
322 #endif
323 
324 	dest = (uchar *)(lcd_base + y * lcd_line_length + x * NBITS(LCD_BPP)/8);
325 
326 	for (row = 0; row < VIDEO_FONT_HEIGHT; ++row, dest += lcd_line_length) {
327 		uchar *s = str;
328 		int i;
329 #if LCD_BPP == LCD_COLOR16
330 		ushort *d = (ushort *)dest;
331 #elif LCD_BPP == LCD_COLOR32
332 		u32 *d = (u32 *)dest;
333 #else
334 		uchar *d = dest;
335 #endif
336 
337 		for (i = 0; i < count; ++i) {
338 			uchar c, bits;
339 
340 			c = *s++;
341 			bits = video_fontdata[c * VIDEO_FONT_HEIGHT + row];
342 
343 			for (c = 0; c < 8; ++c) {
344 				*d++ = (bits & 0x80) ?
345 						lcd_color_fg : lcd_color_bg;
346 				bits <<= 1;
347 			}
348 		}
349 	}
350 }
351 
352 static inline void lcd_putc_xy(ushort x, ushort y, uchar c)
353 {
354 	lcd_drawchars(x, y, &c, 1);
355 }
356 
357 /************************************************************************/
358 /**  Small utility to check that you got the colours right		*/
359 /************************************************************************/
360 #ifdef LCD_TEST_PATTERN
361 
362 #define	N_BLK_VERT	2
363 #define	N_BLK_HOR	3
364 
365 static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
366 	CONSOLE_COLOR_RED,	CONSOLE_COLOR_GREEN,	CONSOLE_COLOR_YELLOW,
367 	CONSOLE_COLOR_BLUE,	CONSOLE_COLOR_MAGENTA,	CONSOLE_COLOR_CYAN,
368 };
369 
370 static void test_pattern(void)
371 {
372 	ushort v_max  = panel_info.vl_row;
373 	ushort h_max  = panel_info.vl_col;
374 	ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
375 	ushort h_step = (h_max + N_BLK_HOR  - 1) / N_BLK_HOR;
376 	ushort v, h;
377 	uchar *pix = (uchar *)lcd_base;
378 
379 	printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
380 		h_max, v_max, h_step, v_step);
381 
382 	/* WARNING: Code silently assumes 8bit/pixel */
383 	for (v = 0; v < v_max; ++v) {
384 		uchar iy = v / v_step;
385 		for (h = 0; h < h_max; ++h) {
386 			uchar ix = N_BLK_HOR * iy + h / h_step;
387 			*pix++ = test_colors[ix];
388 		}
389 	}
390 }
391 #endif /* LCD_TEST_PATTERN */
392 
393 
394 /************************************************************************/
395 /* ** GENERIC Initialization Routines					*/
396 /************************************************************************/
397 /*
398  * With most lcd drivers the line length is set up
399  * by calculating it from panel_info parameters. Some
400  * drivers need to calculate the line length differently,
401  * so make the function weak to allow overriding it.
402  */
403 __weak int lcd_get_size(int *line_length)
404 {
405 	*line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
406 	return *line_length * panel_info.vl_row;
407 }
408 
409 int drv_lcd_init(void)
410 {
411 	struct stdio_dev lcddev;
412 	int rc;
413 
414 	lcd_base = map_sysmem(gd->fb_base, 0);
415 
416 	lcd_init(lcd_base);		/* LCD initialization */
417 
418 	/* Device initialization */
419 	memset(&lcddev, 0, sizeof(lcddev));
420 
421 	strcpy(lcddev.name, "lcd");
422 	lcddev.ext   = 0;			/* No extensions */
423 	lcddev.flags = DEV_FLAGS_OUTPUT;	/* Output only */
424 	lcddev.putc  = lcd_stub_putc;		/* 'putc' function */
425 	lcddev.puts  = lcd_stub_puts;		/* 'puts' function */
426 
427 	rc = stdio_register(&lcddev);
428 
429 	return (rc == 0) ? 1 : rc;
430 }
431 
432 /*----------------------------------------------------------------------*/
433 void lcd_clear(void)
434 {
435 	short console_rows, console_cols;
436 #if LCD_BPP == LCD_COLOR8
437 	/* Setting the palette */
438 	lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
439 	lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
440 	lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
441 	lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
442 	lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
443 	lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
444 	lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
445 	lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
446 	lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
447 #endif
448 
449 #ifndef CONFIG_SYS_WHITE_ON_BLACK
450 	lcd_setfgcolor(CONSOLE_COLOR_BLACK);
451 	lcd_setbgcolor(CONSOLE_COLOR_WHITE);
452 #else
453 	lcd_setfgcolor(CONSOLE_COLOR_WHITE);
454 	lcd_setbgcolor(CONSOLE_COLOR_BLACK);
455 #endif	/* CONFIG_SYS_WHITE_ON_BLACK */
456 
457 #ifdef	LCD_TEST_PATTERN
458 	test_pattern();
459 #else
460 	/* set framebuffer to background color */
461 #if (LCD_BPP != LCD_COLOR32)
462 	memset((char *)lcd_base,
463 		lcd_color_bg,
464 		lcd_line_length * panel_info.vl_row);
465 #else
466 	u32 *ppix = lcd_base;
467 	u32 i;
468 	for (i = 0;
469 	   i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
470 	   i++) {
471 		*ppix++ = lcd_color_bg;
472 	}
473 #endif
474 #endif
475 	/* Paint the logo and retrieve LCD base address */
476 	debug("[LCD] Drawing the logo...\n");
477 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
478 	console_rows = (panel_info.vl_row - BMP_LOGO_HEIGHT);
479 	console_rows /= VIDEO_FONT_HEIGHT;
480 #else
481 	console_rows = panel_info.vl_row / VIDEO_FONT_HEIGHT;
482 #endif
483 	console_cols = panel_info.vl_col / VIDEO_FONT_WIDTH;
484 	lcd_init_console(lcd_logo(), console_rows, console_cols);
485 	lcd_sync();
486 }
487 
488 static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
489 			char *const argv[])
490 {
491 	lcd_clear();
492 	return 0;
493 }
494 
495 U_BOOT_CMD(
496 	cls,	1,	1,	do_lcd_clear,
497 	"clear screen",
498 	""
499 );
500 
501 /*----------------------------------------------------------------------*/
502 
503 static int lcd_init(void *lcdbase)
504 {
505 	/* Initialize the lcd controller */
506 	debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
507 
508 	lcd_ctrl_init(lcdbase);
509 
510 	/*
511 	 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
512 	 * the 'lcdbase' argument and uses custom lcd base address
513 	 * by setting up gd->fb_base. Check for this condition and fixup
514 	 * 'lcd_base' address.
515 	 */
516 	if (map_to_sysmem(lcdbase) != gd->fb_base)
517 		lcd_base = map_sysmem(gd->fb_base, 0);
518 
519 	debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
520 
521 	lcd_get_size(&lcd_line_length);
522 	lcd_is_enabled = 1;
523 	lcd_clear();
524 	lcd_enable();
525 
526 	/* Initialize the console */
527 	lcd_set_col(0);
528 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
529 	lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
530 #else
531 	lcd_set_row(1);	/* leave 1 blank line below logo */
532 #endif
533 
534 	return 0;
535 }
536 
537 
538 /************************************************************************/
539 /* ** ROM capable initialization part - needed to reserve FB memory	*/
540 /************************************************************************/
541 /*
542  * This is called early in the system initialization to grab memory
543  * for the LCD controller.
544  * Returns new address for monitor, after reserving LCD buffer memory
545  *
546  * Note that this is running from ROM, so no write access to global data.
547  */
548 ulong lcd_setmem(ulong addr)
549 {
550 	ulong size;
551 	int line_length;
552 
553 	debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
554 		panel_info.vl_row, NBITS(panel_info.vl_bpix));
555 
556 	size = lcd_get_size(&line_length);
557 
558 	/* Round up to nearest full page, or MMU section if defined */
559 	size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
560 	addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
561 
562 	/* Allocate pages for the frame buffer. */
563 	addr -= size;
564 
565 	debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
566 	      size >> 10, addr);
567 
568 	return addr;
569 }
570 
571 /*----------------------------------------------------------------------*/
572 
573 static void lcd_setfgcolor(int color)
574 {
575 	lcd_color_fg = color;
576 }
577 
578 /*----------------------------------------------------------------------*/
579 
580 static void lcd_setbgcolor(int color)
581 {
582 	lcd_color_bg = color;
583 }
584 
585 /************************************************************************/
586 /* ** Chipset depending Bitmap / Logo stuff...                          */
587 /************************************************************************/
588 static inline ushort *configuration_get_cmap(void)
589 {
590 #if defined CONFIG_CPU_PXA
591 	struct pxafb_info *fbi = &panel_info.pxa;
592 	return (ushort *)fbi->palette;
593 #elif defined(CONFIG_MPC823)
594 	immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
595 	cpm8xx_t *cp = &(immr->im_cpm);
596 	return (ushort *)&(cp->lcd_cmap[255 * sizeof(ushort)]);
597 #elif defined(CONFIG_ATMEL_LCD)
598 	return (ushort *)(panel_info.mmio + ATMEL_LCDC_LUT(0));
599 #elif !defined(CONFIG_ATMEL_HLCD) && !defined(CONFIG_EXYNOS_FB)
600 	return panel_info.cmap;
601 #elif defined(CONFIG_LCD_LOGO)
602 	return bmp_logo_palette;
603 #else
604 	return NULL;
605 #endif
606 }
607 
608 #ifdef CONFIG_LCD_LOGO
609 void bitmap_plot(int x, int y)
610 {
611 #ifdef CONFIG_ATMEL_LCD
612 	uint *cmap = (uint *)bmp_logo_palette;
613 #else
614 	ushort *cmap = (ushort *)bmp_logo_palette;
615 #endif
616 	ushort i, j;
617 	uchar *bmap;
618 	uchar *fb;
619 	ushort *fb16;
620 #if defined(CONFIG_MPC823)
621 	immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
622 	cpm8xx_t *cp = &(immr->im_cpm);
623 #endif
624 	unsigned bpix = NBITS(panel_info.vl_bpix);
625 
626 	debug("Logo: width %d  height %d  colors %d  cmap %d\n",
627 		BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS,
628 		ARRAY_SIZE(bmp_logo_palette));
629 
630 	bmap = &bmp_logo_bitmap[0];
631 	fb   = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
632 
633 	if (bpix < 12) {
634 		/* Leave room for default color map
635 		 * default case: generic system with no cmap (most likely 16bpp)
636 		 * cmap was set to the source palette, so no change is done.
637 		 * This avoids even more ifdefs in the next stanza
638 		 */
639 #if defined(CONFIG_MPC823)
640 		cmap = (ushort *) &(cp->lcd_cmap[BMP_LOGO_OFFSET * sizeof(ushort)]);
641 #elif defined(CONFIG_ATMEL_LCD)
642 		cmap = (uint *)configuration_get_cmap();
643 #else
644 		cmap = configuration_get_cmap();
645 #endif
646 
647 		WATCHDOG_RESET();
648 
649 		/* Set color map */
650 		for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i) {
651 			ushort colreg = bmp_logo_palette[i];
652 #ifdef CONFIG_ATMEL_LCD
653 			uint lut_entry;
654 #ifdef CONFIG_ATMEL_LCD_BGR555
655 			lut_entry = ((colreg & 0x000F) << 11) |
656 					((colreg & 0x00F0) <<  2) |
657 					((colreg & 0x0F00) >>  7);
658 #else /* CONFIG_ATMEL_LCD_RGB565 */
659 			lut_entry = ((colreg & 0x000F) << 1) |
660 					((colreg & 0x00F0) << 3) |
661 					((colreg & 0x0F00) << 4);
662 #endif
663 			*(cmap + BMP_LOGO_OFFSET) = lut_entry;
664 			cmap++;
665 #else /* !CONFIG_ATMEL_LCD */
666 			*cmap++ = colreg;
667 #endif /* CONFIG_ATMEL_LCD */
668 		}
669 
670 		WATCHDOG_RESET();
671 
672 		for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
673 			memcpy(fb, bmap, BMP_LOGO_WIDTH);
674 			bmap += BMP_LOGO_WIDTH;
675 			fb += panel_info.vl_col;
676 		}
677 	}
678 	else { /* true color mode */
679 		u16 col16;
680 		fb16 = (ushort *)fb;
681 		for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
682 			for (j = 0; j < BMP_LOGO_WIDTH; j++) {
683 				col16 = bmp_logo_palette[(bmap[j]-16)];
684 				fb16[j] =
685 					((col16 & 0x000F) << 1) |
686 					((col16 & 0x00F0) << 3) |
687 					((col16 & 0x0F00) << 4);
688 				}
689 			bmap += BMP_LOGO_WIDTH;
690 			fb16 += panel_info.vl_col;
691 		}
692 	}
693 
694 	WATCHDOG_RESET();
695 	lcd_sync();
696 }
697 #else
698 static inline void bitmap_plot(int x, int y) {}
699 #endif /* CONFIG_LCD_LOGO */
700 
701 /*----------------------------------------------------------------------*/
702 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
703 /*
704  * Display the BMP file located at address bmp_image.
705  * Only uncompressed.
706  */
707 
708 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
709 #define BMP_ALIGN_CENTER	0x7FFF
710 
711 static void splash_align_axis(int *axis, unsigned long panel_size,
712 					unsigned long picture_size)
713 {
714 	unsigned long panel_picture_delta = panel_size - picture_size;
715 	unsigned long axis_alignment;
716 
717 	if (*axis == BMP_ALIGN_CENTER)
718 		axis_alignment = panel_picture_delta / 2;
719 	else if (*axis < 0)
720 		axis_alignment = panel_picture_delta + *axis + 1;
721 	else
722 		return;
723 
724 	*axis = max(0, (int)axis_alignment);
725 }
726 #endif
727 
728 
729 #ifdef CONFIG_LCD_BMP_RLE8
730 
731 #define BMP_RLE8_ESCAPE		0
732 #define BMP_RLE8_EOL		0
733 #define BMP_RLE8_EOBMP		1
734 #define BMP_RLE8_DELTA		2
735 
736 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
737 				  int cnt)
738 {
739 	while (cnt > 0) {
740 		*(*fbp)++ = cmap[*bmap++];
741 		cnt--;
742 	}
743 }
744 
745 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
746 {
747 	ushort *fb = *fbp;
748 	int cnt_8copy = cnt >> 3;
749 
750 	cnt -= cnt_8copy << 3;
751 	while (cnt_8copy > 0) {
752 		*fb++ = c;
753 		*fb++ = c;
754 		*fb++ = c;
755 		*fb++ = c;
756 		*fb++ = c;
757 		*fb++ = c;
758 		*fb++ = c;
759 		*fb++ = c;
760 		cnt_8copy--;
761 	}
762 	while (cnt > 0) {
763 		*fb++ = c;
764 		cnt--;
765 	}
766 	*fbp = fb;
767 }
768 
769 /*
770  * Do not call this function directly, must be called from lcd_display_bitmap.
771  */
772 static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
773 				    int x_off, int y_off)
774 {
775 	uchar *bmap;
776 	ulong width, height;
777 	ulong cnt, runlen;
778 	int x, y;
779 	int decode = 1;
780 
781 	width = get_unaligned_le32(&bmp->header.width);
782 	height = get_unaligned_le32(&bmp->header.height);
783 	bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
784 
785 	x = 0;
786 	y = height - 1;
787 
788 	while (decode) {
789 		if (bmap[0] == BMP_RLE8_ESCAPE) {
790 			switch (bmap[1]) {
791 			case BMP_RLE8_EOL:
792 				/* end of line */
793 				bmap += 2;
794 				x = 0;
795 				y--;
796 				/* 16bpix, 2-byte per pixel, width should *2 */
797 				fb -= (width * 2 + lcd_line_length);
798 				break;
799 			case BMP_RLE8_EOBMP:
800 				/* end of bitmap */
801 				decode = 0;
802 				break;
803 			case BMP_RLE8_DELTA:
804 				/* delta run */
805 				x += bmap[2];
806 				y -= bmap[3];
807 				/* 16bpix, 2-byte per pixel, x should *2 */
808 				fb = (uchar *) (lcd_base + (y + y_off - 1)
809 					* lcd_line_length + (x + x_off) * 2);
810 				bmap += 4;
811 				break;
812 			default:
813 				/* unencoded run */
814 				runlen = bmap[1];
815 				bmap += 2;
816 				if (y < height) {
817 					if (x < width) {
818 						if (x + runlen > width)
819 							cnt = width - x;
820 						else
821 							cnt = runlen;
822 						draw_unencoded_bitmap(
823 							(ushort **)&fb,
824 							bmap, cmap, cnt);
825 					}
826 					x += runlen;
827 				}
828 				bmap += runlen;
829 				if (runlen & 1)
830 					bmap++;
831 			}
832 		} else {
833 			/* encoded run */
834 			if (y < height) {
835 				runlen = bmap[0];
836 				if (x < width) {
837 					/* aggregate the same code */
838 					while (bmap[0] == 0xff &&
839 					       bmap[2] != BMP_RLE8_ESCAPE &&
840 					       bmap[1] == bmap[3]) {
841 						runlen += bmap[2];
842 						bmap += 2;
843 					}
844 					if (x + runlen > width)
845 						cnt = width - x;
846 					else
847 						cnt = runlen;
848 					draw_encoded_bitmap((ushort **)&fb,
849 						cmap[bmap[1]], cnt);
850 				}
851 				x += runlen;
852 			}
853 			bmap += 2;
854 		}
855 	}
856 }
857 #endif
858 
859 #if defined(CONFIG_MPC823)
860 #define FB_PUT_BYTE(fb, from) *(fb)++ = (255 - *(from)++)
861 #else
862 #define FB_PUT_BYTE(fb, from) *(fb)++ = *(from)++
863 #endif
864 
865 #if defined(CONFIG_BMP_16BPP)
866 #if defined(CONFIG_ATMEL_LCD_BGR555)
867 static inline void fb_put_word(uchar **fb, uchar **from)
868 {
869 	*(*fb)++ = (((*from)[0] & 0x1f) << 2) | ((*from)[1] & 0x03);
870 	*(*fb)++ = ((*from)[0] & 0xe0) | (((*from)[1] & 0x7c) >> 2);
871 	*from += 2;
872 }
873 #else
874 static inline void fb_put_word(uchar **fb, uchar **from)
875 {
876 	*(*fb)++ = *(*from)++;
877 	*(*fb)++ = *(*from)++;
878 }
879 #endif
880 #endif /* CONFIG_BMP_16BPP */
881 
882 int lcd_display_bitmap(ulong bmp_image, int x, int y)
883 {
884 	ushort *cmap = NULL;
885 	ushort *cmap_base = NULL;
886 	ushort i, j;
887 	uchar *fb;
888 	bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
889 	uchar *bmap;
890 	ushort padded_width;
891 	unsigned long width, height, byte_width;
892 	unsigned long pwidth = panel_info.vl_col;
893 	unsigned colors, bpix, bmp_bpix;
894 
895 	if (!bmp || !(bmp->header.signature[0] == 'B' &&
896 		bmp->header.signature[1] == 'M')) {
897 		printf("Error: no valid bmp image at %lx\n", bmp_image);
898 
899 		return 1;
900 	}
901 
902 	width = get_unaligned_le32(&bmp->header.width);
903 	height = get_unaligned_le32(&bmp->header.height);
904 	bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
905 
906 	colors = 1 << bmp_bpix;
907 
908 	bpix = NBITS(panel_info.vl_bpix);
909 
910 	if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
911 		printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
912 			bpix, bmp_bpix);
913 
914 		return 1;
915 	}
916 
917 	/*
918 	 * We support displaying 8bpp BMPs on 16bpp LCDs
919 	 * and displaying 24bpp BMPs on 32bpp LCDs
920 	 * */
921 	if (bpix != bmp_bpix &&
922 	    !(bmp_bpix == 8 && bpix == 16) &&
923 	    !(bmp_bpix == 24 && bpix == 32)) {
924 		printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
925 			bpix, get_unaligned_le16(&bmp->header.bit_count));
926 		return 1;
927 	}
928 
929 	debug("Display-bmp: %d x %d  with %d colors\n",
930 		(int)width, (int)height, (int)colors);
931 
932 	if (bmp_bpix == 8) {
933 		cmap = configuration_get_cmap();
934 		cmap_base = cmap;
935 
936 		/* Set color map */
937 		for (i = 0; i < colors; ++i) {
938 			bmp_color_table_entry_t cte = bmp->color_table[i];
939 #if !defined(CONFIG_ATMEL_LCD)
940 			ushort colreg =
941 				( ((cte.red)   << 8) & 0xf800) |
942 				( ((cte.green) << 3) & 0x07e0) |
943 				( ((cte.blue)  >> 3) & 0x001f) ;
944 			*cmap = colreg;
945 #if defined(CONFIG_MPC823)
946 			cmap--;
947 #else
948 			cmap++;
949 #endif
950 #else /* CONFIG_ATMEL_LCD */
951 			lcd_setcolreg(i, cte.red, cte.green, cte.blue);
952 #endif
953 		}
954 	}
955 
956 	padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
957 
958 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
959 	splash_align_axis(&x, pwidth, width);
960 	splash_align_axis(&y, panel_info.vl_row, height);
961 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
962 
963 	if ((x + width) > pwidth)
964 		width = pwidth - x;
965 	if ((y + height) > panel_info.vl_row)
966 		height = panel_info.vl_row - y;
967 
968 	bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
969 	fb   = (uchar *)(lcd_base +
970 		(y + height - 1) * lcd_line_length + x * bpix / 8);
971 
972 	switch (bmp_bpix) {
973 	case 1: /* pass through */
974 	case 8: {
975 #ifdef CONFIG_LCD_BMP_RLE8
976 		u32 compression = get_unaligned_le32(&bmp->header.compression);
977 		if (compression == BMP_BI_RLE8) {
978 			if (bpix != 16) {
979 				/* TODO implement render code for bpix != 16 */
980 				printf("Error: only support 16 bpix");
981 				return 1;
982 			}
983 			lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
984 			break;
985 		}
986 #endif
987 
988 		if (bpix != 16)
989 			byte_width = width;
990 		else
991 			byte_width = width * 2;
992 
993 		for (i = 0; i < height; ++i) {
994 			WATCHDOG_RESET();
995 			for (j = 0; j < width; j++) {
996 				if (bpix != 16) {
997 					FB_PUT_BYTE(fb, bmap);
998 				} else {
999 					*(uint16_t *)fb = cmap_base[*(bmap++)];
1000 					fb += sizeof(uint16_t) / sizeof(*fb);
1001 				}
1002 			}
1003 			bmap += (padded_width - width);
1004 			fb -= byte_width + lcd_line_length;
1005 		}
1006 		break;
1007 	}
1008 #if defined(CONFIG_BMP_16BPP)
1009 	case 16:
1010 		for (i = 0; i < height; ++i) {
1011 			WATCHDOG_RESET();
1012 			for (j = 0; j < width; j++)
1013 				fb_put_word(&fb, &bmap);
1014 
1015 			bmap += (padded_width - width) * 2;
1016 			fb -= width * 2 + lcd_line_length;
1017 		}
1018 		break;
1019 #endif /* CONFIG_BMP_16BPP */
1020 #if defined(CONFIG_BMP_24BMP)
1021 	case 24:
1022 		for (i = 0; i < height; ++i) {
1023 			for (j = 0; j < width; j++) {
1024 				*(fb++) = *(bmap++);
1025 				*(fb++) = *(bmap++);
1026 				*(fb++) = *(bmap++);
1027 				*(fb++) = 0;
1028 			}
1029 			fb -= lcd_line_length + width * (bpix / 8);
1030 		}
1031 		break;
1032 #endif /* CONFIG_BMP_24BMP */
1033 #if defined(CONFIG_BMP_32BPP)
1034 	case 32:
1035 		for (i = 0; i < height; ++i) {
1036 			for (j = 0; j < width; j++) {
1037 				*(fb++) = *(bmap++);
1038 				*(fb++) = *(bmap++);
1039 				*(fb++) = *(bmap++);
1040 				*(fb++) = *(bmap++);
1041 			}
1042 			fb -= lcd_line_length + width * (bpix / 8);
1043 		}
1044 		break;
1045 #endif /* CONFIG_BMP_32BPP */
1046 	default:
1047 		break;
1048 	};
1049 
1050 	lcd_sync();
1051 	return 0;
1052 }
1053 #endif
1054 
1055 static void *lcd_logo(void)
1056 {
1057 #ifdef CONFIG_SPLASH_SCREEN
1058 	char *s;
1059 	ulong addr;
1060 	static int do_splash = 1;
1061 
1062 	if (do_splash && (s = getenv("splashimage")) != NULL) {
1063 		int x = 0, y = 0;
1064 		do_splash = 0;
1065 
1066 		if (splash_screen_prepare())
1067 			return (void *)lcd_base;
1068 
1069 		addr = simple_strtoul (s, NULL, 16);
1070 
1071 		splash_get_pos(&x, &y);
1072 
1073 		if (bmp_display(addr, x, y) == 0)
1074 			return (void *)lcd_base;
1075 	}
1076 #endif /* CONFIG_SPLASH_SCREEN */
1077 
1078 	bitmap_plot(0, 0);
1079 
1080 #ifdef CONFIG_LCD_INFO
1081 	lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
1082 	lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
1083 	lcd_show_board_info();
1084 #endif /* CONFIG_LCD_INFO */
1085 
1086 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
1087 	return (void *)((ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length);
1088 #else
1089 	return (void *)lcd_base;
1090 #endif /* CONFIG_LCD_LOGO && !defined(CONFIG_LCD_INFO_BELOW_LOGO) */
1091 }
1092 
1093 #ifdef CONFIG_SPLASHIMAGE_GUARD
1094 static int on_splashimage(const char *name, const char *value, enum env_op op,
1095 	int flags)
1096 {
1097 	ulong addr;
1098 	int aligned;
1099 
1100 	if (op == env_op_delete)
1101 		return 0;
1102 
1103 	addr = simple_strtoul(value, NULL, 16);
1104 	/* See README.displaying-bmps */
1105 	aligned = (addr % 4 == 2);
1106 	if (!aligned) {
1107 		printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
1108 		return -1;
1109 	}
1110 
1111 	return 0;
1112 }
1113 
1114 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
1115 #endif
1116 
1117 void lcd_position_cursor(unsigned col, unsigned row)
1118 {
1119 	console_curr_col = min_t(short, col, console_cols - 1);
1120 	console_curr_row = min_t(short, row, console_rows - 1);
1121 }
1122 
1123 int lcd_get_pixel_width(void)
1124 {
1125 	return panel_info.vl_col;
1126 }
1127 
1128 int lcd_get_pixel_height(void)
1129 {
1130 	return panel_info.vl_row;
1131 }
1132 
1133 int lcd_get_screen_rows(void)
1134 {
1135 	return console_rows;
1136 }
1137 
1138 int lcd_get_screen_columns(void)
1139 {
1140 	return console_cols;
1141 }
1142 
1143 #if defined(CONFIG_LCD_DT_SIMPLEFB)
1144 static int lcd_dt_simplefb_configure_node(void *blob, int off)
1145 {
1146 #if LCD_BPP == LCD_COLOR16
1147 	return fdt_setup_simplefb_node(blob, off, gd->fb_base,
1148 				       panel_info.vl_col, panel_info.vl_row,
1149 				       panel_info.vl_col * 2, "r5g6b5");
1150 #else
1151 	return -1;
1152 #endif
1153 }
1154 
1155 int lcd_dt_simplefb_add_node(void *blob)
1156 {
1157 	static const char compat[] = "simple-framebuffer";
1158 	static const char disabled[] = "disabled";
1159 	int off, ret;
1160 
1161 	off = fdt_add_subnode(blob, 0, "framebuffer");
1162 	if (off < 0)
1163 		return -1;
1164 
1165 	ret = fdt_setprop(blob, off, "status", disabled, sizeof(disabled));
1166 	if (ret < 0)
1167 		return -1;
1168 
1169 	ret = fdt_setprop(blob, off, "compatible", compat, sizeof(compat));
1170 	if (ret < 0)
1171 		return -1;
1172 
1173 	return lcd_dt_simplefb_configure_node(blob, off);
1174 }
1175 
1176 int lcd_dt_simplefb_enable_existing_node(void *blob)
1177 {
1178 	int off;
1179 
1180 	off = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
1181 	if (off < 0)
1182 		return -1;
1183 
1184 	return lcd_dt_simplefb_configure_node(blob, off);
1185 }
1186 #endif
1187