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