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