xref: /openbmc/u-boot/drivers/video/cfb_console.c (revision 657fd2d0)
1 /*
2  * (C) Copyright 2002 ELTEC Elektronik AG
3  * Frank Gottschling <fgottschling@eltec.de>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 /*
9  * cfb_console.c
10  *
11  * Color Framebuffer Console driver for 8/15/16/24/32 bits per pixel.
12  *
13  * At the moment only the 8x16 font is tested and the font fore- and
14  * background color is limited to black/white/gray colors. The Linux
15  * logo can be placed in the upper left corner and additional board
16  * information strings (that normally goes to serial port) can be drawn.
17  *
18  * The console driver can use the standard PC keyboard interface (i8042)
19  * for character input. Character output goes to a memory mapped video
20  * framebuffer with little or big-endian organisation.
21  * With environment setting 'console=serial' the console i/o can be
22  * forced to serial port.
23  *
24  * The driver uses graphic specific defines/parameters/functions:
25  *
26  * (for SMI LynxE graphic chip)
27  *
28  * CONFIG_VIDEO_SMI_LYNXEM    - use graphic driver for SMI 710,712,810
29  * VIDEO_FB_LITTLE_ENDIAN     - framebuffer organisation default: big endian
30  * VIDEO_HW_RECTFILL	      - graphic driver supports hardware rectangle fill
31  * VIDEO_HW_BITBLT	      - graphic driver supports hardware bit blt
32  *
33  * Console Parameters are set by graphic drivers global struct:
34  *
35  * VIDEO_VISIBLE_COLS	      - x resolution
36  * VIDEO_VISIBLE_ROWS	      - y resolution
37  * VIDEO_PIXEL_SIZE	      - storage size in byte per pixel
38  * VIDEO_DATA_FORMAT	      - graphical data format GDF
39  * VIDEO_FB_ADRS	      - start of video memory
40  *
41  * CONFIG_I8042_KBD	      - AT Keyboard driver for i8042
42  * VIDEO_KBD_INIT_FCT	      - init function for keyboard
43  * VIDEO_TSTC_FCT	      - keyboard_tstc function
44  * VIDEO_GETC_FCT	      - keyboard_getc function
45  *
46  * CONFIG_CONSOLE_CURSOR      - on/off drawing cursor is done with
47  *				delay loop in VIDEO_TSTC_FCT (i8042)
48  *
49  * CONFIG_SYS_CONSOLE_BLINK_COUNT - value for delay loop - blink rate
50  * CONFIG_CONSOLE_TIME	      - display time/date in upper right
51  *				corner, needs CONFIG_CMD_DATE and
52  *				CONFIG_CONSOLE_CURSOR
53  * CONFIG_VIDEO_LOGO	      - display Linux Logo in upper left corner.
54  *				Use CONFIG_SPLASH_SCREEN_ALIGN with
55  *				environment variable "splashpos" to place
56  *				the logo on other position. In this case
57  *				no CONSOLE_EXTRA_INFO is possible.
58  * CONFIG_VIDEO_BMP_LOGO      - use bmp_logo instead of linux_logo
59  * CONFIG_CONSOLE_EXTRA_INFO  - display additional board information
60  *				strings that normaly goes to serial
61  *				port.  This define requires a board
62  *				specific function:
63  *				video_drawstring (VIDEO_INFO_X,
64  *					VIDEO_INFO_Y + i*VIDEO_FONT_HEIGHT,
65  *					info);
66  *				that fills a info buffer at i=row.
67  *				s.a: board/eltec/bab7xx.
68  * CONFIG_VGA_AS_SINGLE_DEVICE - If set the framebuffer device will be
69  *				initialized as an output only device.
70  *				The Keyboard driver will not be
71  *				set-up.  This may be used, if you have
72  *				no or more than one Keyboard devices
73  *				(USB Keyboard, AT Keyboard).
74  *
75  * CONFIG_VIDEO_SW_CURSOR:    - Draws a cursor after the last
76  *				character. No blinking is provided.
77  *				Uses the macros CURSOR_SET and
78  *				CURSOR_OFF.
79  *
80  * CONFIG_VIDEO_HW_CURSOR:    - Uses the hardware cursor capability
81  *				of the graphic chip. Uses the macro
82  *				CURSOR_SET. ATTENTION: If booting an
83  *				OS, the display driver must disable
84  *				the hardware register of the graphic
85  *				chip. Otherwise a blinking field is
86  *				displayed.
87  */
88 
89 #include <common.h>
90 #include <fdtdec.h>
91 #include <version.h>
92 #include <malloc.h>
93 #include <linux/compiler.h>
94 
95 /*
96  * Console device defines with SMI graphic
97  * Any other graphic must change this section
98  */
99 
100 #ifdef	CONFIG_VIDEO_SMI_LYNXEM
101 
102 #define VIDEO_FB_LITTLE_ENDIAN
103 #define VIDEO_HW_RECTFILL
104 #define VIDEO_HW_BITBLT
105 #endif
106 
107 /*
108  * Defines for the CT69000 driver
109  */
110 #ifdef	CONFIG_VIDEO_CT69000
111 
112 #define VIDEO_FB_LITTLE_ENDIAN
113 #define VIDEO_HW_RECTFILL
114 #define VIDEO_HW_BITBLT
115 #endif
116 
117 /*
118  * Defines for the SED13806 driver
119  */
120 #ifdef CONFIG_VIDEO_SED13806
121 #define VIDEO_FB_LITTLE_ENDIAN
122 #define VIDEO_HW_RECTFILL
123 #define VIDEO_HW_BITBLT
124 #endif
125 
126 #ifdef CONFIG_VIDEO_MXS
127 #define VIDEO_FB_16BPP_WORD_SWAP
128 #endif
129 
130 /*
131  * Defines for the MB862xx driver
132  */
133 #ifdef CONFIG_VIDEO_MB862xx
134 
135 #ifdef CONFIG_VIDEO_CORALP
136 #define VIDEO_FB_LITTLE_ENDIAN
137 #endif
138 #ifdef CONFIG_VIDEO_MB862xx_ACCEL
139 #define VIDEO_HW_RECTFILL
140 #define VIDEO_HW_BITBLT
141 #endif
142 #endif
143 
144 /*
145  * Defines for the i.MX31 driver (mx3fb.c)
146  */
147 #if defined(CONFIG_VIDEO_MX3) || defined(CONFIG_VIDEO_IPUV3)
148 #define VIDEO_FB_16BPP_WORD_SWAP
149 #endif
150 
151 /*
152  * Include video_fb.h after definitions of VIDEO_HW_RECTFILL etc.
153  */
154 #include <video_fb.h>
155 
156 #include <splash.h>
157 
158 /*
159  * some Macros
160  */
161 #define VIDEO_VISIBLE_COLS	(pGD->winSizeX)
162 #define VIDEO_VISIBLE_ROWS	(pGD->winSizeY)
163 #define VIDEO_PIXEL_SIZE	(pGD->gdfBytesPP)
164 #define VIDEO_DATA_FORMAT	(pGD->gdfIndex)
165 #define VIDEO_FB_ADRS		(pGD->frameAdrs)
166 
167 /*
168  * Console device defines with i8042 keyboard controller
169  * Any other keyboard controller must change this section
170  */
171 
172 #ifdef	CONFIG_I8042_KBD
173 #include <i8042.h>
174 
175 #define VIDEO_KBD_INIT_FCT	i8042_kbd_init()
176 #define VIDEO_TSTC_FCT		i8042_tstc
177 #define VIDEO_GETC_FCT		i8042_getc
178 #endif
179 
180 /*
181  * Console device
182  */
183 
184 #include <version.h>
185 #include <linux/types.h>
186 #include <stdio_dev.h>
187 #include <video_font.h>
188 
189 #if defined(CONFIG_CMD_DATE)
190 #include <rtc.h>
191 #endif
192 
193 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
194 #include <watchdog.h>
195 #include <bmp_layout.h>
196 #include <splash.h>
197 #endif
198 
199 /*
200  * Cursor definition:
201  * CONFIG_CONSOLE_CURSOR:  Uses a timer function (see drivers/input/i8042.c)
202  *			   to let the cursor blink. Uses the macros
203  *			   CURSOR_OFF and CURSOR_ON.
204  * CONFIG_VIDEO_SW_CURSOR: Draws a cursor after the last character. No
205  *			   blinking is provided. Uses the macros CURSOR_SET
206  *			   and CURSOR_OFF.
207  * CONFIG_VIDEO_HW_CURSOR: Uses the hardware cursor capability of the
208  *			   graphic chip. Uses the macro CURSOR_SET.
209  *			   ATTENTION: If booting an OS, the display driver
210  *			   must disable the hardware register of the graphic
211  *			   chip. Otherwise a blinking field is displayed
212  */
213 #if !defined(CONFIG_CONSOLE_CURSOR) && \
214     !defined(CONFIG_VIDEO_SW_CURSOR) && \
215     !defined(CONFIG_VIDEO_HW_CURSOR)
216 /* no Cursor defined */
217 #define CURSOR_ON
218 #define CURSOR_OFF
219 #define CURSOR_SET
220 #endif
221 
222 #if defined(CONFIG_CONSOLE_CURSOR) || defined(CONFIG_VIDEO_SW_CURSOR)
223 #if defined(CURSOR_ON) || \
224 	(defined(CONFIG_CONSOLE_CURSOR) && defined(CONFIG_VIDEO_SW_CURSOR))
225 #error	only one of CONFIG_CONSOLE_CURSOR, CONFIG_VIDEO_SW_CURSOR, \
226 	or CONFIG_VIDEO_HW_CURSOR can be defined
227 #endif
228 void console_cursor(int state);
229 
230 #define CURSOR_ON  console_cursor(1)
231 #define CURSOR_OFF console_cursor(0)
232 #define CURSOR_SET video_set_cursor()
233 #endif /* CONFIG_CONSOLE_CURSOR || CONFIG_VIDEO_SW_CURSOR */
234 
235 #ifdef	CONFIG_CONSOLE_CURSOR
236 #ifndef	CONFIG_CONSOLE_TIME
237 #error	CONFIG_CONSOLE_CURSOR must be defined for CONFIG_CONSOLE_TIME
238 #endif
239 #ifndef CONFIG_I8042_KBD
240 #warning Cursor drawing on/off needs timer function s.a. drivers/input/i8042.c
241 #endif
242 #endif /* CONFIG_CONSOLE_CURSOR */
243 
244 
245 #ifdef CONFIG_VIDEO_HW_CURSOR
246 #ifdef	CURSOR_ON
247 #error	only one of CONFIG_CONSOLE_CURSOR, CONFIG_VIDEO_SW_CURSOR, \
248 	or CONFIG_VIDEO_HW_CURSOR can be defined
249 #endif
250 #define CURSOR_ON
251 #define CURSOR_OFF
252 #define CURSOR_SET video_set_hw_cursor(console_col * VIDEO_FONT_WIDTH, \
253 		  (console_row * VIDEO_FONT_HEIGHT) + video_logo_height)
254 #endif /* CONFIG_VIDEO_HW_CURSOR */
255 
256 #ifdef	CONFIG_VIDEO_LOGO
257 #ifdef	CONFIG_VIDEO_BMP_LOGO
258 #include <bmp_logo.h>
259 #include <bmp_logo_data.h>
260 #define VIDEO_LOGO_WIDTH	BMP_LOGO_WIDTH
261 #define VIDEO_LOGO_HEIGHT	BMP_LOGO_HEIGHT
262 #define VIDEO_LOGO_LUT_OFFSET	BMP_LOGO_OFFSET
263 #define VIDEO_LOGO_COLORS	BMP_LOGO_COLORS
264 
265 #else  /* CONFIG_VIDEO_BMP_LOGO */
266 #define LINUX_LOGO_WIDTH	80
267 #define LINUX_LOGO_HEIGHT	80
268 #define LINUX_LOGO_COLORS	214
269 #define LINUX_LOGO_LUT_OFFSET	0x20
270 #define __initdata
271 #include <linux_logo.h>
272 #define VIDEO_LOGO_WIDTH	LINUX_LOGO_WIDTH
273 #define VIDEO_LOGO_HEIGHT	LINUX_LOGO_HEIGHT
274 #define VIDEO_LOGO_LUT_OFFSET	LINUX_LOGO_LUT_OFFSET
275 #define VIDEO_LOGO_COLORS	LINUX_LOGO_COLORS
276 #endif /* CONFIG_VIDEO_BMP_LOGO */
277 #define VIDEO_INFO_X		(VIDEO_LOGO_WIDTH)
278 #define VIDEO_INFO_Y		(VIDEO_FONT_HEIGHT/2)
279 #else  /* CONFIG_VIDEO_LOGO */
280 #define VIDEO_LOGO_WIDTH	0
281 #define VIDEO_LOGO_HEIGHT	0
282 #endif /* CONFIG_VIDEO_LOGO */
283 
284 #define VIDEO_COLS		VIDEO_VISIBLE_COLS
285 #define VIDEO_ROWS		VIDEO_VISIBLE_ROWS
286 #define VIDEO_SIZE		(VIDEO_ROWS*VIDEO_COLS*VIDEO_PIXEL_SIZE)
287 #define VIDEO_PIX_BLOCKS	(VIDEO_SIZE >> 2)
288 #define VIDEO_LINE_LEN		(VIDEO_COLS*VIDEO_PIXEL_SIZE)
289 #define VIDEO_BURST_LEN		(VIDEO_COLS/8)
290 
291 #ifdef	CONFIG_VIDEO_LOGO
292 #define CONSOLE_ROWS		((VIDEO_ROWS - video_logo_height) / VIDEO_FONT_HEIGHT)
293 #else
294 #define CONSOLE_ROWS		(VIDEO_ROWS / VIDEO_FONT_HEIGHT)
295 #endif
296 
297 #define CONSOLE_COLS		(VIDEO_COLS / VIDEO_FONT_WIDTH)
298 #define CONSOLE_ROW_SIZE	(VIDEO_FONT_HEIGHT * VIDEO_LINE_LEN)
299 #define CONSOLE_ROW_FIRST	(video_console_address)
300 #define CONSOLE_ROW_SECOND	(video_console_address + CONSOLE_ROW_SIZE)
301 #define CONSOLE_ROW_LAST	(video_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE)
302 #define CONSOLE_SIZE		(CONSOLE_ROW_SIZE * CONSOLE_ROWS)
303 
304 /* By default we scroll by a single line */
305 #ifndef CONFIG_CONSOLE_SCROLL_LINES
306 #define CONFIG_CONSOLE_SCROLL_LINES 1
307 #endif
308 
309 /* Macros */
310 #ifdef	VIDEO_FB_LITTLE_ENDIAN
311 #define SWAP16(x)		((((x) & 0x00ff) << 8) | \
312 				  ((x) >> 8) \
313 				)
314 #define SWAP32(x)		((((x) & 0x000000ff) << 24) | \
315 				 (((x) & 0x0000ff00) <<  8) | \
316 				 (((x) & 0x00ff0000) >>  8) | \
317 				 (((x) & 0xff000000) >> 24)   \
318 				)
319 #define SHORTSWAP32(x)		((((x) & 0x000000ff) <<  8) | \
320 				 (((x) & 0x0000ff00) >>  8) | \
321 				 (((x) & 0x00ff0000) <<  8) | \
322 				 (((x) & 0xff000000) >>  8)   \
323 				)
324 #else
325 #define SWAP16(x)		(x)
326 #define SWAP32(x)		(x)
327 #if defined(VIDEO_FB_16BPP_WORD_SWAP)
328 #define SHORTSWAP32(x)		(((x) >> 16) | ((x) << 16))
329 #else
330 #define SHORTSWAP32(x)		(x)
331 #endif
332 #endif
333 
334 #ifdef CONFIG_CONSOLE_EXTRA_INFO
335 /*
336  * setup a board string: type, speed, etc.
337  *
338  * line_number:	location to place info string beside logo
339  * info:	buffer for info string
340  */
341 extern void video_get_info_str(int line_number,	char *info);
342 #endif
343 
344 DECLARE_GLOBAL_DATA_PTR;
345 
346 /* Locals */
347 static GraphicDevice *pGD;	/* Pointer to Graphic array */
348 
349 static void *video_fb_address;	/* frame buffer address */
350 static void *video_console_address;	/* console buffer start address */
351 
352 static int video_logo_height = VIDEO_LOGO_HEIGHT;
353 
354 static int __maybe_unused cursor_state;
355 static int __maybe_unused old_col;
356 static int __maybe_unused old_row;
357 
358 static int console_col;		/* cursor col */
359 static int console_row;		/* cursor row */
360 
361 static u32 eorx, fgx, bgx;	/* color pats */
362 
363 static int cfb_do_flush_cache;
364 
365 #ifdef CONFIG_CFB_CONSOLE_ANSI
366 static char ansi_buf[10];
367 static int ansi_buf_size;
368 static int ansi_colors_need_revert;
369 static int ansi_cursor_hidden;
370 #endif
371 
372 static const int video_font_draw_table8[] = {
373 	0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff,
374 	0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
375 	0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff,
376 	0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff
377 };
378 
379 static const int video_font_draw_table15[] = {
380 	0x00000000, 0x00007fff, 0x7fff0000, 0x7fff7fff
381 };
382 
383 static const int video_font_draw_table16[] = {
384 	0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
385 };
386 
387 static const int video_font_draw_table24[16][3] = {
388 	{0x00000000, 0x00000000, 0x00000000},
389 	{0x00000000, 0x00000000, 0x00ffffff},
390 	{0x00000000, 0x0000ffff, 0xff000000},
391 	{0x00000000, 0x0000ffff, 0xffffffff},
392 	{0x000000ff, 0xffff0000, 0x00000000},
393 	{0x000000ff, 0xffff0000, 0x00ffffff},
394 	{0x000000ff, 0xffffffff, 0xff000000},
395 	{0x000000ff, 0xffffffff, 0xffffffff},
396 	{0xffffff00, 0x00000000, 0x00000000},
397 	{0xffffff00, 0x00000000, 0x00ffffff},
398 	{0xffffff00, 0x0000ffff, 0xff000000},
399 	{0xffffff00, 0x0000ffff, 0xffffffff},
400 	{0xffffffff, 0xffff0000, 0x00000000},
401 	{0xffffffff, 0xffff0000, 0x00ffffff},
402 	{0xffffffff, 0xffffffff, 0xff000000},
403 	{0xffffffff, 0xffffffff, 0xffffffff}
404 };
405 
406 static const int video_font_draw_table32[16][4] = {
407 	{0x00000000, 0x00000000, 0x00000000, 0x00000000},
408 	{0x00000000, 0x00000000, 0x00000000, 0x00ffffff},
409 	{0x00000000, 0x00000000, 0x00ffffff, 0x00000000},
410 	{0x00000000, 0x00000000, 0x00ffffff, 0x00ffffff},
411 	{0x00000000, 0x00ffffff, 0x00000000, 0x00000000},
412 	{0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff},
413 	{0x00000000, 0x00ffffff, 0x00ffffff, 0x00000000},
414 	{0x00000000, 0x00ffffff, 0x00ffffff, 0x00ffffff},
415 	{0x00ffffff, 0x00000000, 0x00000000, 0x00000000},
416 	{0x00ffffff, 0x00000000, 0x00000000, 0x00ffffff},
417 	{0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000},
418 	{0x00ffffff, 0x00000000, 0x00ffffff, 0x00ffffff},
419 	{0x00ffffff, 0x00ffffff, 0x00000000, 0x00000000},
420 	{0x00ffffff, 0x00ffffff, 0x00000000, 0x00ffffff},
421 	{0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00000000},
422 	{0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff}
423 };
424 
425 /*
426  * Implement a weak default function for boards that optionally
427  * need to skip the cfb initialization.
428  */
429 __weak int board_cfb_skip(void)
430 {
431 	/* As default, don't skip cfb init */
432 	return 0;
433 }
434 
435 static void video_drawchars(int xx, int yy, unsigned char *s, int count)
436 {
437 	u8 *cdat, *dest, *dest0;
438 	int rows, offset, c;
439 
440 	offset = yy * VIDEO_LINE_LEN + xx * VIDEO_PIXEL_SIZE;
441 	dest0 = video_fb_address + offset;
442 
443 	switch (VIDEO_DATA_FORMAT) {
444 	case GDF__8BIT_INDEX:
445 	case GDF__8BIT_332RGB:
446 		while (count--) {
447 			c = *s;
448 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
449 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
450 			     rows--; dest += VIDEO_LINE_LEN) {
451 				u8 bits = *cdat++;
452 
453 				((u32 *) dest)[0] =
454 					(video_font_draw_table8[bits >> 4] &
455 					 eorx) ^ bgx;
456 
457 				if (VIDEO_FONT_WIDTH == 4)
458 					continue;
459 
460 				((u32 *) dest)[1] =
461 					(video_font_draw_table8[bits & 15] &
462 					 eorx) ^ bgx;
463 			}
464 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
465 			s++;
466 		}
467 		break;
468 
469 	case GDF_15BIT_555RGB:
470 		while (count--) {
471 			c = *s;
472 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
473 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
474 			     rows--; dest += VIDEO_LINE_LEN) {
475 				u8 bits = *cdat++;
476 
477 				((u32 *) dest)[0] =
478 					SHORTSWAP32((video_font_draw_table15
479 						     [bits >> 6] & eorx) ^
480 						    bgx);
481 				((u32 *) dest)[1] =
482 					SHORTSWAP32((video_font_draw_table15
483 						     [bits >> 4 & 3] & eorx) ^
484 						    bgx);
485 
486 				if (VIDEO_FONT_WIDTH == 4)
487 					continue;
488 
489 				((u32 *) dest)[2] =
490 					SHORTSWAP32((video_font_draw_table15
491 						     [bits >> 2 & 3] & eorx) ^
492 						    bgx);
493 				((u32 *) dest)[3] =
494 					SHORTSWAP32((video_font_draw_table15
495 						     [bits & 3] & eorx) ^
496 						    bgx);
497 			}
498 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
499 			s++;
500 		}
501 		break;
502 
503 	case GDF_16BIT_565RGB:
504 		while (count--) {
505 			c = *s;
506 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
507 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
508 			     rows--; dest += VIDEO_LINE_LEN) {
509 				u8 bits = *cdat++;
510 
511 				((u32 *) dest)[0] =
512 					SHORTSWAP32((video_font_draw_table16
513 						     [bits >> 6] & eorx) ^
514 						    bgx);
515 				((u32 *) dest)[1] =
516 					SHORTSWAP32((video_font_draw_table16
517 						     [bits >> 4 & 3] & eorx) ^
518 						    bgx);
519 
520 				if (VIDEO_FONT_WIDTH == 4)
521 					continue;
522 
523 				((u32 *) dest)[2] =
524 					SHORTSWAP32((video_font_draw_table16
525 						     [bits >> 2 & 3] & eorx) ^
526 						    bgx);
527 				((u32 *) dest)[3] =
528 					SHORTSWAP32((video_font_draw_table16
529 						     [bits & 3] & eorx) ^
530 						    bgx);
531 			}
532 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
533 			s++;
534 		}
535 		break;
536 
537 	case GDF_32BIT_X888RGB:
538 		while (count--) {
539 			c = *s;
540 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
541 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
542 			     rows--; dest += VIDEO_LINE_LEN) {
543 				u8 bits = *cdat++;
544 
545 				((u32 *) dest)[0] =
546 					SWAP32((video_font_draw_table32
547 						[bits >> 4][0] & eorx) ^ bgx);
548 				((u32 *) dest)[1] =
549 					SWAP32((video_font_draw_table32
550 						[bits >> 4][1] & eorx) ^ bgx);
551 				((u32 *) dest)[2] =
552 					SWAP32((video_font_draw_table32
553 						[bits >> 4][2] & eorx) ^ bgx);
554 				((u32 *) dest)[3] =
555 					SWAP32((video_font_draw_table32
556 						[bits >> 4][3] & eorx) ^ bgx);
557 
558 
559 				if (VIDEO_FONT_WIDTH == 4)
560 					continue;
561 
562 				((u32 *) dest)[4] =
563 					SWAP32((video_font_draw_table32
564 						[bits & 15][0] & eorx) ^ bgx);
565 				((u32 *) dest)[5] =
566 					SWAP32((video_font_draw_table32
567 						[bits & 15][1] & eorx) ^ bgx);
568 				((u32 *) dest)[6] =
569 					SWAP32((video_font_draw_table32
570 						[bits & 15][2] & eorx) ^ bgx);
571 				((u32 *) dest)[7] =
572 					SWAP32((video_font_draw_table32
573 						[bits & 15][3] & eorx) ^ bgx);
574 			}
575 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
576 			s++;
577 		}
578 		break;
579 
580 	case GDF_24BIT_888RGB:
581 		while (count--) {
582 			c = *s;
583 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
584 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
585 			     rows--; dest += VIDEO_LINE_LEN) {
586 				u8 bits = *cdat++;
587 
588 				((u32 *) dest)[0] =
589 					(video_font_draw_table24[bits >> 4][0]
590 					 & eorx) ^ bgx;
591 				((u32 *) dest)[1] =
592 					(video_font_draw_table24[bits >> 4][1]
593 					 & eorx) ^ bgx;
594 				((u32 *) dest)[2] =
595 					(video_font_draw_table24[bits >> 4][2]
596 					 & eorx) ^ bgx;
597 
598 				if (VIDEO_FONT_WIDTH == 4)
599 					continue;
600 
601 				((u32 *) dest)[3] =
602 					(video_font_draw_table24[bits & 15][0]
603 					 & eorx) ^ bgx;
604 				((u32 *) dest)[4] =
605 					(video_font_draw_table24[bits & 15][1]
606 					 & eorx) ^ bgx;
607 				((u32 *) dest)[5] =
608 					(video_font_draw_table24[bits & 15][2]
609 					 & eorx) ^ bgx;
610 			}
611 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
612 			s++;
613 		}
614 		break;
615 	}
616 }
617 
618 static inline void video_drawstring(int xx, int yy, unsigned char *s)
619 {
620 	video_drawchars(xx, yy, s, strlen((char *) s));
621 }
622 
623 static void video_putchar(int xx, int yy, unsigned char c)
624 {
625 	video_drawchars(xx, yy + video_logo_height, &c, 1);
626 }
627 
628 #if defined(CONFIG_CONSOLE_CURSOR) || defined(CONFIG_VIDEO_SW_CURSOR)
629 static void video_set_cursor(void)
630 {
631 	if (cursor_state)
632 		console_cursor(0);
633 	console_cursor(1);
634 }
635 
636 static void video_invertchar(int xx, int yy)
637 {
638 	int firstx = xx * VIDEO_PIXEL_SIZE;
639 	int lastx = (xx + VIDEO_FONT_WIDTH) * VIDEO_PIXEL_SIZE;
640 	int firsty = yy * VIDEO_LINE_LEN;
641 	int lasty = (yy + VIDEO_FONT_HEIGHT) * VIDEO_LINE_LEN;
642 	int x, y;
643 	for (y = firsty; y < lasty; y += VIDEO_LINE_LEN) {
644 		for (x = firstx; x < lastx; x++) {
645 			u8 *dest = (u8 *)(video_fb_address) + x + y;
646 			*dest = ~*dest;
647 		}
648 	}
649 }
650 
651 void console_cursor(int state)
652 {
653 #ifdef CONFIG_CONSOLE_TIME
654 	struct rtc_time tm;
655 	char info[16];
656 
657 	/* time update only if cursor is on (faster scroll) */
658 	if (state) {
659 		rtc_get(&tm);
660 
661 		sprintf(info, " %02d:%02d:%02d ", tm.tm_hour, tm.tm_min,
662 			tm.tm_sec);
663 		video_drawstring(VIDEO_VISIBLE_COLS - 10 * VIDEO_FONT_WIDTH,
664 				 VIDEO_INFO_Y, (uchar *) info);
665 
666 		sprintf(info, "%02d.%02d.%04d", tm.tm_mday, tm.tm_mon,
667 			tm.tm_year);
668 		video_drawstring(VIDEO_VISIBLE_COLS - 10 * VIDEO_FONT_WIDTH,
669 				 VIDEO_INFO_Y + 1 * VIDEO_FONT_HEIGHT,
670 				 (uchar *) info);
671 	}
672 #endif
673 
674 	if (cursor_state != state) {
675 		if (cursor_state) {
676 			/* turn off the cursor */
677 			video_invertchar(old_col * VIDEO_FONT_WIDTH,
678 					 old_row * VIDEO_FONT_HEIGHT +
679 					 video_logo_height);
680 		} else {
681 			/* turn off the cursor and record where it is */
682 			video_invertchar(console_col * VIDEO_FONT_WIDTH,
683 					 console_row * VIDEO_FONT_HEIGHT +
684 					 video_logo_height);
685 			old_col = console_col;
686 			old_row = console_row;
687 		}
688 		cursor_state = state;
689 	}
690 	if (cfb_do_flush_cache)
691 		flush_cache(VIDEO_FB_ADRS, VIDEO_SIZE);
692 }
693 #endif
694 
695 #ifndef VIDEO_HW_RECTFILL
696 static void memsetl(int *p, int c, int v)
697 {
698 	while (c--)
699 		*(p++) = v;
700 }
701 #endif
702 
703 #ifndef VIDEO_HW_BITBLT
704 static void memcpyl(int *d, int *s, int c)
705 {
706 	while (c--)
707 		*(d++) = *(s++);
708 }
709 #endif
710 
711 static void console_clear_line(int line, int begin, int end)
712 {
713 #ifdef VIDEO_HW_RECTFILL
714 	video_hw_rectfill(VIDEO_PIXEL_SIZE,		/* bytes per pixel */
715 			  VIDEO_FONT_WIDTH * begin,	/* dest pos x */
716 			  video_logo_height +
717 			  VIDEO_FONT_HEIGHT * line,	/* dest pos y */
718 			  VIDEO_FONT_WIDTH * (end - begin + 1), /* fr. width */
719 			  VIDEO_FONT_HEIGHT,		/* frame height */
720 			  bgx				/* fill color */
721 		);
722 #else
723 	if (begin == 0 && (end + 1) == CONSOLE_COLS) {
724 		memsetl(CONSOLE_ROW_FIRST +
725 			CONSOLE_ROW_SIZE * line,	/* offset of row */
726 			CONSOLE_ROW_SIZE >> 2,		/* length of row */
727 			bgx				/* fill color */
728 		);
729 	} else {
730 		void *offset;
731 		int i, size;
732 
733 		offset = CONSOLE_ROW_FIRST +
734 			 CONSOLE_ROW_SIZE * line +	/* offset of row */
735 			 VIDEO_FONT_WIDTH *
736 			 VIDEO_PIXEL_SIZE * begin;	/* offset of col */
737 		size = VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE * (end - begin + 1);
738 		size >>= 2; /* length to end for memsetl() */
739 		/* fill at col offset of i'th line using bgx as fill color */
740 		for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
741 			memsetl(offset + i * VIDEO_LINE_LEN, size, bgx);
742 	}
743 #endif
744 }
745 
746 static void console_scrollup(void)
747 {
748 	const int rows = CONFIG_CONSOLE_SCROLL_LINES;
749 	int i;
750 
751 	/* copy up rows ignoring the first one */
752 
753 #ifdef VIDEO_HW_BITBLT
754 	video_hw_bitblt(VIDEO_PIXEL_SIZE,	/* bytes per pixel */
755 			0,			/* source pos x */
756 			video_logo_height +
757 				VIDEO_FONT_HEIGHT * rows, /* source pos y */
758 			0,			/* dest pos x */
759 			video_logo_height,	/* dest pos y */
760 			VIDEO_VISIBLE_COLS,	/* frame width */
761 			VIDEO_VISIBLE_ROWS
762 			- video_logo_height
763 			- VIDEO_FONT_HEIGHT * rows	/* frame height */
764 		);
765 #else
766 	memcpyl(CONSOLE_ROW_FIRST, CONSOLE_ROW_FIRST + rows * CONSOLE_ROW_SIZE,
767 		(CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows) >> 2);
768 #endif
769 	/* clear the last one */
770 	for (i = 1; i <= rows; i++)
771 		console_clear_line(CONSOLE_ROWS - i, 0, CONSOLE_COLS - 1);
772 
773 	/* Decrement row number */
774 	console_row -= rows;
775 }
776 
777 static void console_back(void)
778 {
779 	console_col--;
780 
781 	if (console_col < 0) {
782 		console_col = CONSOLE_COLS - 1;
783 		console_row--;
784 		if (console_row < 0)
785 			console_row = 0;
786 	}
787 }
788 
789 #ifdef CONFIG_CFB_CONSOLE_ANSI
790 
791 static void console_clear(void)
792 {
793 #ifdef VIDEO_HW_RECTFILL
794 	video_hw_rectfill(VIDEO_PIXEL_SIZE,	/* bytes per pixel */
795 			  0,			/* dest pos x */
796 			  video_logo_height,	/* dest pos y */
797 			  VIDEO_VISIBLE_COLS,	/* frame width */
798 			  VIDEO_VISIBLE_ROWS,	/* frame height */
799 			  bgx			/* fill color */
800 	);
801 #else
802 	memsetl(CONSOLE_ROW_FIRST, CONSOLE_SIZE, bgx);
803 #endif
804 }
805 
806 static void console_cursor_fix(void)
807 {
808 	if (console_row < 0)
809 		console_row = 0;
810 	if (console_row >= CONSOLE_ROWS)
811 		console_row = CONSOLE_ROWS - 1;
812 	if (console_col < 0)
813 		console_col = 0;
814 	if (console_col >= CONSOLE_COLS)
815 		console_col = CONSOLE_COLS - 1;
816 }
817 
818 static void console_cursor_up(int n)
819 {
820 	console_row -= n;
821 	console_cursor_fix();
822 }
823 
824 static void console_cursor_down(int n)
825 {
826 	console_row += n;
827 	console_cursor_fix();
828 }
829 
830 static void console_cursor_left(int n)
831 {
832 	console_col -= n;
833 	console_cursor_fix();
834 }
835 
836 static void console_cursor_right(int n)
837 {
838 	console_col += n;
839 	console_cursor_fix();
840 }
841 
842 static void console_cursor_set_position(int row, int col)
843 {
844 	if (console_row != -1)
845 		console_row = row;
846 	if (console_col != -1)
847 		console_col = col;
848 	console_cursor_fix();
849 }
850 
851 static void console_previousline(int n)
852 {
853 	/* FIXME: also scroll terminal ? */
854 	console_row -= n;
855 	console_cursor_fix();
856 }
857 
858 static void console_swap_colors(void)
859 {
860 	eorx = fgx;
861 	fgx = bgx;
862 	bgx = eorx;
863 	eorx = fgx ^ bgx;
864 }
865 
866 static inline int console_cursor_is_visible(void)
867 {
868 	return !ansi_cursor_hidden;
869 }
870 #else
871 static inline int console_cursor_is_visible(void)
872 {
873 	return 1;
874 }
875 #endif
876 
877 static void console_newline(int n)
878 {
879 	console_row += n;
880 	console_col = 0;
881 
882 	/* Check if we need to scroll the terminal */
883 	if (console_row >= CONSOLE_ROWS) {
884 		/* Scroll everything up */
885 		console_scrollup();
886 	}
887 }
888 
889 static void console_cr(void)
890 {
891 	console_col = 0;
892 }
893 
894 static void parse_putc(const char c)
895 {
896 	static int nl = 1;
897 
898 	if (console_cursor_is_visible())
899 		CURSOR_OFF;
900 
901 	switch (c) {
902 	case 13:		/* back to first column */
903 		console_cr();
904 		break;
905 
906 	case '\n':		/* next line */
907 		if (console_col || (!console_col && nl))
908 			console_newline(1);
909 		nl = 1;
910 		break;
911 
912 	case 9:		/* tab 8 */
913 		console_col |= 0x0008;
914 		console_col &= ~0x0007;
915 
916 		if (console_col >= CONSOLE_COLS)
917 			console_newline(1);
918 		break;
919 
920 	case 8:		/* backspace */
921 		console_back();
922 		break;
923 
924 	case 7:		/* bell */
925 		break;	/* ignored */
926 
927 	default:		/* draw the char */
928 		video_putchar(console_col * VIDEO_FONT_WIDTH,
929 			      console_row * VIDEO_FONT_HEIGHT, c);
930 		console_col++;
931 
932 		/* check for newline */
933 		if (console_col >= CONSOLE_COLS) {
934 			console_newline(1);
935 			nl = 0;
936 		}
937 	}
938 
939 	if (console_cursor_is_visible())
940 		CURSOR_SET;
941 }
942 
943 static void video_putc(struct stdio_dev *dev, const char c)
944 {
945 #ifdef CONFIG_CFB_CONSOLE_ANSI
946 	int i;
947 
948 	if (c == 27) {
949 		for (i = 0; i < ansi_buf_size; ++i)
950 			parse_putc(ansi_buf[i]);
951 		ansi_buf[0] = 27;
952 		ansi_buf_size = 1;
953 		return;
954 	}
955 
956 	if (ansi_buf_size > 0) {
957 		/*
958 		 * 0 - ESC
959 		 * 1 - [
960 		 * 2 - num1
961 		 * 3 - ..
962 		 * 4 - ;
963 		 * 5 - num2
964 		 * 6 - ..
965 		 * - cchar
966 		 */
967 		int next = 0;
968 
969 		int flush = 0;
970 		int fail = 0;
971 
972 		int num1 = 0;
973 		int num2 = 0;
974 		int cchar = 0;
975 
976 		ansi_buf[ansi_buf_size++] = c;
977 
978 		if (ansi_buf_size >= sizeof(ansi_buf))
979 			fail = 1;
980 
981 		for (i = 0; i < ansi_buf_size; ++i) {
982 			if (fail)
983 				break;
984 
985 			switch (next) {
986 			case 0:
987 				if (ansi_buf[i] == 27)
988 					next = 1;
989 				else
990 					fail = 1;
991 				break;
992 
993 			case 1:
994 				if (ansi_buf[i] == '[')
995 					next = 2;
996 				else
997 					fail = 1;
998 				break;
999 
1000 			case 2:
1001 				if (ansi_buf[i] >= '0' && ansi_buf[i] <= '9') {
1002 					num1 = ansi_buf[i]-'0';
1003 					next = 3;
1004 				} else if (ansi_buf[i] != '?') {
1005 					--i;
1006 					num1 = 1;
1007 					next = 4;
1008 				}
1009 				break;
1010 
1011 			case 3:
1012 				if (ansi_buf[i] >= '0' && ansi_buf[i] <= '9') {
1013 					num1 *= 10;
1014 					num1 += ansi_buf[i]-'0';
1015 				} else {
1016 					--i;
1017 					next = 4;
1018 				}
1019 				break;
1020 
1021 			case 4:
1022 				if (ansi_buf[i] != ';') {
1023 					--i;
1024 					next = 7;
1025 				} else
1026 					next = 5;
1027 				break;
1028 
1029 			case 5:
1030 				if (ansi_buf[i] >= '0' && ansi_buf[i] <= '9') {
1031 					num2 = ansi_buf[i]-'0';
1032 					next = 6;
1033 				} else
1034 					fail = 1;
1035 				break;
1036 
1037 			case 6:
1038 				if (ansi_buf[i] >= '0' && ansi_buf[i] <= '9') {
1039 					num2 *= 10;
1040 					num2 += ansi_buf[i]-'0';
1041 				} else {
1042 					--i;
1043 					next = 7;
1044 				}
1045 				break;
1046 
1047 			case 7:
1048 				if ((ansi_buf[i] >= 'A' && ansi_buf[i] <= 'H')
1049 					|| ansi_buf[i] == 'J'
1050 					|| ansi_buf[i] == 'K'
1051 					|| ansi_buf[i] == 'h'
1052 					|| ansi_buf[i] == 'l'
1053 					|| ansi_buf[i] == 'm') {
1054 					cchar = ansi_buf[i];
1055 					flush = 1;
1056 				} else
1057 					fail = 1;
1058 				break;
1059 			}
1060 		}
1061 
1062 		if (fail) {
1063 			for (i = 0; i < ansi_buf_size; ++i)
1064 				parse_putc(ansi_buf[i]);
1065 			ansi_buf_size = 0;
1066 			return;
1067 		}
1068 
1069 		if (flush) {
1070 			if (!ansi_cursor_hidden)
1071 				CURSOR_OFF;
1072 			ansi_buf_size = 0;
1073 			switch (cchar) {
1074 			case 'A':
1075 				/* move cursor num1 rows up */
1076 				console_cursor_up(num1);
1077 				break;
1078 			case 'B':
1079 				/* move cursor num1 rows down */
1080 				console_cursor_down(num1);
1081 				break;
1082 			case 'C':
1083 				/* move cursor num1 columns forward */
1084 				console_cursor_right(num1);
1085 				break;
1086 			case 'D':
1087 				/* move cursor num1 columns back */
1088 				console_cursor_left(num1);
1089 				break;
1090 			case 'E':
1091 				/* move cursor num1 rows up at begin of row */
1092 				console_previousline(num1);
1093 				break;
1094 			case 'F':
1095 				/* move cursor num1 rows down at begin of row */
1096 				console_newline(num1);
1097 				break;
1098 			case 'G':
1099 				/* move cursor to column num1 */
1100 				console_cursor_set_position(-1, num1-1);
1101 				break;
1102 			case 'H':
1103 				/* move cursor to row num1, column num2 */
1104 				console_cursor_set_position(num1-1, num2-1);
1105 				break;
1106 			case 'J':
1107 				/* clear console and move cursor to 0, 0 */
1108 				console_clear();
1109 				console_cursor_set_position(0, 0);
1110 				break;
1111 			case 'K':
1112 				/* clear line */
1113 				if (num1 == 0)
1114 					console_clear_line(console_row,
1115 							console_col,
1116 							CONSOLE_COLS-1);
1117 				else if (num1 == 1)
1118 					console_clear_line(console_row,
1119 							0, console_col);
1120 				else
1121 					console_clear_line(console_row,
1122 							0, CONSOLE_COLS-1);
1123 				break;
1124 			case 'h':
1125 				ansi_cursor_hidden = 0;
1126 				break;
1127 			case 'l':
1128 				ansi_cursor_hidden = 1;
1129 				break;
1130 			case 'm':
1131 				if (num1 == 0) { /* reset swapped colors */
1132 					if (ansi_colors_need_revert) {
1133 						console_swap_colors();
1134 						ansi_colors_need_revert = 0;
1135 					}
1136 				} else if (num1 == 7) { /* once swap colors */
1137 					if (!ansi_colors_need_revert) {
1138 						console_swap_colors();
1139 						ansi_colors_need_revert = 1;
1140 					}
1141 				}
1142 				break;
1143 			}
1144 			if (!ansi_cursor_hidden)
1145 				CURSOR_SET;
1146 		}
1147 	} else {
1148 		parse_putc(c);
1149 	}
1150 #else
1151 	parse_putc(c);
1152 #endif
1153 	if (cfb_do_flush_cache)
1154 		flush_cache(VIDEO_FB_ADRS, VIDEO_SIZE);
1155 }
1156 
1157 static void video_puts(struct stdio_dev *dev, const char *s)
1158 {
1159 	int flush = cfb_do_flush_cache;
1160 	int count = strlen(s);
1161 
1162 	/* temporarily disable cache flush */
1163 	cfb_do_flush_cache = 0;
1164 
1165 	while (count--)
1166 		video_putc(dev, *s++);
1167 
1168 	if (flush) {
1169 		cfb_do_flush_cache = flush;
1170 		flush_cache(VIDEO_FB_ADRS, VIDEO_SIZE);
1171 	}
1172 }
1173 
1174 /*
1175  * Do not enforce drivers (or board code) to provide empty
1176  * video_set_lut() if they do not support 8 bpp format.
1177  * Implement weak default function instead.
1178  */
1179 __weak void video_set_lut(unsigned int index, unsigned char r,
1180 		     unsigned char g, unsigned char b)
1181 {
1182 }
1183 
1184 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
1185 
1186 #define FILL_8BIT_332RGB(r,g,b)	{			\
1187 	*fb = ((r>>5)<<5) | ((g>>5)<<2) | (b>>6);	\
1188 	fb ++;						\
1189 }
1190 
1191 #define FILL_15BIT_555RGB(r,g,b) {			\
1192 	*(unsigned short *)fb =				\
1193 		SWAP16((unsigned short)(((r>>3)<<10) |	\
1194 					((g>>3)<<5)  |	\
1195 					 (b>>3)));	\
1196 	fb += 2;					\
1197 }
1198 
1199 #define FILL_16BIT_565RGB(r,g,b) {			\
1200 	*(unsigned short *)fb =				\
1201 		SWAP16((unsigned short)((((r)>>3)<<11)| \
1202 					(((g)>>2)<<5) | \
1203 					 ((b)>>3)));	\
1204 	fb += 2;					\
1205 }
1206 
1207 #define FILL_32BIT_X888RGB(r,g,b) {			\
1208 	*(unsigned long *)fb =				\
1209 		SWAP32((unsigned long)(((r<<16) |	\
1210 					(g<<8)  |	\
1211 					 b)));		\
1212 	fb += 4;					\
1213 }
1214 
1215 #ifdef VIDEO_FB_LITTLE_ENDIAN
1216 #define FILL_24BIT_888RGB(r,g,b) {			\
1217 	fb[0] = b;					\
1218 	fb[1] = g;					\
1219 	fb[2] = r;					\
1220 	fb += 3;					\
1221 }
1222 #else
1223 #define FILL_24BIT_888RGB(r,g,b) {			\
1224 	fb[0] = r;					\
1225 	fb[1] = g;					\
1226 	fb[2] = b;					\
1227 	fb += 3;					\
1228 }
1229 #endif
1230 
1231 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1232 static inline void fill_555rgb_pswap(uchar *fb, int x, u8 r, u8 g, u8 b)
1233 {
1234 	ushort *dst = (ushort *) fb;
1235 	ushort color = (ushort) (((r >> 3) << 10) |
1236 				 ((g >> 3) <<  5) |
1237 				  (b >> 3));
1238 	if (x & 1)
1239 		*(--dst) = color;
1240 	else
1241 		*(++dst) = color;
1242 }
1243 #endif
1244 
1245 /*
1246  * RLE8 bitmap support
1247  */
1248 
1249 #ifdef CONFIG_VIDEO_BMP_RLE8
1250 /* Pre-calculated color table entry */
1251 struct palette {
1252 	union {
1253 		unsigned short w;	/* word */
1254 		unsigned int dw;	/* double word */
1255 	} ce;				/* color entry */
1256 };
1257 
1258 /*
1259  * Helper to draw encoded/unencoded run.
1260  */
1261 static void draw_bitmap(uchar **fb, uchar *bm, struct palette *p,
1262 			int cnt, int enc)
1263 {
1264 	ulong addr = (ulong) *fb;
1265 	int *off;
1266 	int enc_off = 1;
1267 	int i;
1268 
1269 	/*
1270 	 * Setup offset of the color index in the bitmap.
1271 	 * Color index of encoded run is at offset 1.
1272 	 */
1273 	off = enc ? &enc_off : &i;
1274 
1275 	switch (VIDEO_DATA_FORMAT) {
1276 	case GDF__8BIT_INDEX:
1277 		for (i = 0; i < cnt; i++)
1278 			*(unsigned char *) addr++ = bm[*off];
1279 		break;
1280 	case GDF_15BIT_555RGB:
1281 	case GDF_16BIT_565RGB:
1282 		/* differences handled while pre-calculating palette */
1283 		for (i = 0; i < cnt; i++) {
1284 			*(unsigned short *) addr = p[bm[*off]].ce.w;
1285 			addr += 2;
1286 		}
1287 		break;
1288 	case GDF_32BIT_X888RGB:
1289 		for (i = 0; i < cnt; i++) {
1290 			*(unsigned long *) addr = p[bm[*off]].ce.dw;
1291 			addr += 4;
1292 		}
1293 		break;
1294 	}
1295 	*fb = (uchar *) addr;	/* return modified address */
1296 }
1297 
1298 static int display_rle8_bitmap(bmp_image_t *img, int xoff, int yoff,
1299 			       int width, int height)
1300 {
1301 	unsigned char *bm;
1302 	unsigned char *fbp;
1303 	unsigned int cnt, runlen;
1304 	int decode = 1;
1305 	int x, y, bpp, i, ncolors;
1306 	struct palette p[256];
1307 	bmp_color_table_entry_t cte;
1308 	int green_shift, red_off;
1309 	int limit = VIDEO_COLS * VIDEO_ROWS;
1310 	int pixels = 0;
1311 
1312 	x = 0;
1313 	y = __le32_to_cpu(img->header.height) - 1;
1314 	ncolors = __le32_to_cpu(img->header.colors_used);
1315 	bpp = VIDEO_PIXEL_SIZE;
1316 	fbp = (unsigned char *) ((unsigned int) video_fb_address +
1317 				 (((y + yoff) * VIDEO_COLS) + xoff) * bpp);
1318 
1319 	bm = (uchar *) img + __le32_to_cpu(img->header.data_offset);
1320 
1321 	/* pre-calculate and setup palette */
1322 	switch (VIDEO_DATA_FORMAT) {
1323 	case GDF__8BIT_INDEX:
1324 		for (i = 0; i < ncolors; i++) {
1325 			cte = img->color_table[i];
1326 			video_set_lut(i, cte.red, cte.green, cte.blue);
1327 		}
1328 		break;
1329 	case GDF_15BIT_555RGB:
1330 	case GDF_16BIT_565RGB:
1331 		if (VIDEO_DATA_FORMAT == GDF_15BIT_555RGB) {
1332 			green_shift = 3;
1333 			red_off = 10;
1334 		} else {
1335 			green_shift = 2;
1336 			red_off = 11;
1337 		}
1338 		for (i = 0; i < ncolors; i++) {
1339 			cte = img->color_table[i];
1340 			p[i].ce.w = SWAP16((unsigned short)
1341 					   (((cte.red >> 3) << red_off) |
1342 					    ((cte.green >> green_shift) << 5) |
1343 					    cte.blue >> 3));
1344 		}
1345 		break;
1346 	case GDF_32BIT_X888RGB:
1347 		for (i = 0; i < ncolors; i++) {
1348 			cte = img->color_table[i];
1349 			p[i].ce.dw = SWAP32((cte.red << 16) |
1350 					    (cte.green << 8) |
1351 					     cte.blue);
1352 		}
1353 		break;
1354 	default:
1355 		printf("RLE Bitmap unsupported in video mode 0x%x\n",
1356 		       VIDEO_DATA_FORMAT);
1357 		return -1;
1358 	}
1359 
1360 	while (decode) {
1361 		switch (bm[0]) {
1362 		case 0:
1363 			switch (bm[1]) {
1364 			case 0:
1365 				/* scan line end marker */
1366 				bm += 2;
1367 				x = 0;
1368 				y--;
1369 				fbp = (unsigned char *)
1370 					((unsigned int) video_fb_address +
1371 					 (((y + yoff) * VIDEO_COLS) +
1372 					  xoff) * bpp);
1373 				continue;
1374 			case 1:
1375 				/* end of bitmap data marker */
1376 				decode = 0;
1377 				break;
1378 			case 2:
1379 				/* run offset marker */
1380 				x += bm[2];
1381 				y -= bm[3];
1382 				fbp = (unsigned char *)
1383 					((unsigned int) video_fb_address +
1384 					 (((y + yoff) * VIDEO_COLS) +
1385 					  x + xoff) * bpp);
1386 				bm += 4;
1387 				break;
1388 			default:
1389 				/* unencoded run */
1390 				cnt = bm[1];
1391 				runlen = cnt;
1392 				pixels += cnt;
1393 				if (pixels > limit)
1394 					goto error;
1395 
1396 				bm += 2;
1397 				if (y < height) {
1398 					if (x >= width) {
1399 						x += runlen;
1400 						goto next_run;
1401 					}
1402 					if (x + runlen > width)
1403 						cnt = width - x;
1404 					draw_bitmap(&fbp, bm, p, cnt, 0);
1405 					x += runlen;
1406 				}
1407 next_run:
1408 				bm += runlen;
1409 				if (runlen & 1)
1410 					bm++;	/* 0 padding if length is odd */
1411 			}
1412 			break;
1413 		default:
1414 			/* encoded run */
1415 			cnt = bm[0];
1416 			runlen = cnt;
1417 			pixels += cnt;
1418 			if (pixels > limit)
1419 				goto error;
1420 
1421 			if (y < height) {     /* only draw into visible area */
1422 				if (x >= width) {
1423 					x += runlen;
1424 					bm += 2;
1425 					continue;
1426 				}
1427 				if (x + runlen > width)
1428 					cnt = width - x;
1429 				draw_bitmap(&fbp, bm, p, cnt, 1);
1430 				x += runlen;
1431 			}
1432 			bm += 2;
1433 			break;
1434 		}
1435 	}
1436 	return 0;
1437 error:
1438 	printf("Error: Too much encoded pixel data, validate your bitmap\n");
1439 	return -1;
1440 }
1441 #endif
1442 
1443 /*
1444  * Display the BMP file located at address bmp_image.
1445  */
1446 int video_display_bitmap(ulong bmp_image, int x, int y)
1447 {
1448 	ushort xcount, ycount;
1449 	uchar *fb;
1450 	bmp_image_t *bmp = (bmp_image_t *) bmp_image;
1451 	uchar *bmap;
1452 	ushort padded_line;
1453 	unsigned long width, height, bpp;
1454 	unsigned colors;
1455 	unsigned long compression;
1456 	bmp_color_table_entry_t cte;
1457 
1458 #ifdef CONFIG_VIDEO_BMP_GZIP
1459 	unsigned char *dst = NULL;
1460 	ulong len;
1461 #endif
1462 
1463 	WATCHDOG_RESET();
1464 
1465 	if (!((bmp->header.signature[0] == 'B') &&
1466 	      (bmp->header.signature[1] == 'M'))) {
1467 
1468 #ifdef CONFIG_VIDEO_BMP_GZIP
1469 		/*
1470 		 * Could be a gzipped bmp image, try to decrompress...
1471 		 */
1472 		len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
1473 		dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE);
1474 		if (dst == NULL) {
1475 			printf("Error: malloc in gunzip failed!\n");
1476 			return 1;
1477 		}
1478 		/*
1479 		 * NB: we need to force offset of +2
1480 		 * See doc/README.displaying-bmps
1481 		 */
1482 		if (gunzip(dst+2, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE-2,
1483 			   (uchar *) bmp_image,
1484 			   &len) != 0) {
1485 			printf("Error: no valid bmp or bmp.gz image at %lx\n",
1486 			       bmp_image);
1487 			free(dst);
1488 			return 1;
1489 		}
1490 		if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE) {
1491 			printf("Image could be truncated "
1492 				"(increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
1493 		}
1494 
1495 		/*
1496 		 * Set addr to decompressed image
1497 		 */
1498 		bmp = (bmp_image_t *)(dst+2);
1499 
1500 		if (!((bmp->header.signature[0] == 'B') &&
1501 		      (bmp->header.signature[1] == 'M'))) {
1502 			printf("Error: no valid bmp.gz image at %lx\n",
1503 			       bmp_image);
1504 			free(dst);
1505 			return 1;
1506 		}
1507 #else
1508 		printf("Error: no valid bmp image at %lx\n", bmp_image);
1509 		return 1;
1510 #endif /* CONFIG_VIDEO_BMP_GZIP */
1511 	}
1512 
1513 	width = le32_to_cpu(bmp->header.width);
1514 	height = le32_to_cpu(bmp->header.height);
1515 	bpp = le16_to_cpu(bmp->header.bit_count);
1516 	colors = le32_to_cpu(bmp->header.colors_used);
1517 	compression = le32_to_cpu(bmp->header.compression);
1518 
1519 	debug("Display-bmp: %ld x %ld  with %d colors\n",
1520 	      width, height, colors);
1521 
1522 	if (compression != BMP_BI_RGB
1523 #ifdef CONFIG_VIDEO_BMP_RLE8
1524 	    && compression != BMP_BI_RLE8
1525 #endif
1526 		) {
1527 		printf("Error: compression type %ld not supported\n",
1528 		       compression);
1529 #ifdef CONFIG_VIDEO_BMP_GZIP
1530 		if (dst)
1531 			free(dst);
1532 #endif
1533 		return 1;
1534 	}
1535 
1536 	padded_line = (((width * bpp + 7) / 8) + 3) & ~0x3;
1537 
1538 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
1539 	if (x == BMP_ALIGN_CENTER)
1540 		x = max(0, (int)(VIDEO_VISIBLE_COLS - width) / 2);
1541 	else if (x < 0)
1542 		x = max(0, (int)(VIDEO_VISIBLE_COLS - width + x + 1));
1543 
1544 	if (y == BMP_ALIGN_CENTER)
1545 		y = max(0, (int)(VIDEO_VISIBLE_ROWS - height) / 2);
1546 	else if (y < 0)
1547 		y = max(0, (int)(VIDEO_VISIBLE_ROWS - height + y + 1));
1548 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
1549 
1550 	/*
1551 	 * Just ignore elements which are completely beyond screen
1552 	 * dimensions.
1553 	 */
1554 	if ((x >= VIDEO_VISIBLE_COLS) || (y >= VIDEO_VISIBLE_ROWS))
1555 		return 0;
1556 
1557 	if ((x + width) > VIDEO_VISIBLE_COLS)
1558 		width = VIDEO_VISIBLE_COLS - x;
1559 	if ((y + height) > VIDEO_VISIBLE_ROWS)
1560 		height = VIDEO_VISIBLE_ROWS - y;
1561 
1562 	bmap = (uchar *) bmp + le32_to_cpu(bmp->header.data_offset);
1563 	fb = (uchar *) (video_fb_address +
1564 			((y + height - 1) * VIDEO_COLS * VIDEO_PIXEL_SIZE) +
1565 			x * VIDEO_PIXEL_SIZE);
1566 
1567 #ifdef CONFIG_VIDEO_BMP_RLE8
1568 	if (compression == BMP_BI_RLE8) {
1569 		return display_rle8_bitmap(bmp, x, y, width, height);
1570 	}
1571 #endif
1572 
1573 	/* We handle only 4, 8, or 24 bpp bitmaps */
1574 	switch (le16_to_cpu(bmp->header.bit_count)) {
1575 	case 4:
1576 		padded_line -= width / 2;
1577 		ycount = height;
1578 
1579 		switch (VIDEO_DATA_FORMAT) {
1580 		case GDF_32BIT_X888RGB:
1581 			while (ycount--) {
1582 				WATCHDOG_RESET();
1583 				/*
1584 				 * Don't assume that 'width' is an
1585 				 * even number
1586 				 */
1587 				for (xcount = 0; xcount < width; xcount++) {
1588 					uchar idx;
1589 
1590 					if (xcount & 1) {
1591 						idx = *bmap & 0xF;
1592 						bmap++;
1593 					} else
1594 						idx = *bmap >> 4;
1595 					cte = bmp->color_table[idx];
1596 					FILL_32BIT_X888RGB(cte.red, cte.green,
1597 							   cte.blue);
1598 				}
1599 				bmap += padded_line;
1600 				fb -= (VIDEO_VISIBLE_COLS + width) *
1601 					VIDEO_PIXEL_SIZE;
1602 			}
1603 			break;
1604 		default:
1605 			puts("4bpp bitmap unsupported with current "
1606 			     "video mode\n");
1607 			break;
1608 		}
1609 		break;
1610 
1611 	case 8:
1612 		padded_line -= width;
1613 		if (VIDEO_DATA_FORMAT == GDF__8BIT_INDEX) {
1614 			/* Copy colormap */
1615 			for (xcount = 0; xcount < colors; ++xcount) {
1616 				cte = bmp->color_table[xcount];
1617 				video_set_lut(xcount, cte.red, cte.green,
1618 					      cte.blue);
1619 			}
1620 		}
1621 		ycount = height;
1622 		switch (VIDEO_DATA_FORMAT) {
1623 		case GDF__8BIT_INDEX:
1624 			while (ycount--) {
1625 				WATCHDOG_RESET();
1626 				xcount = width;
1627 				while (xcount--) {
1628 					*fb++ = *bmap++;
1629 				}
1630 				bmap += padded_line;
1631 				fb -= (VIDEO_VISIBLE_COLS + width) *
1632 							VIDEO_PIXEL_SIZE;
1633 			}
1634 			break;
1635 		case GDF__8BIT_332RGB:
1636 			while (ycount--) {
1637 				WATCHDOG_RESET();
1638 				xcount = width;
1639 				while (xcount--) {
1640 					cte = bmp->color_table[*bmap++];
1641 					FILL_8BIT_332RGB(cte.red, cte.green,
1642 							 cte.blue);
1643 				}
1644 				bmap += padded_line;
1645 				fb -= (VIDEO_VISIBLE_COLS + width) *
1646 							VIDEO_PIXEL_SIZE;
1647 			}
1648 			break;
1649 		case GDF_15BIT_555RGB:
1650 			while (ycount--) {
1651 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1652 				int xpos = x;
1653 #endif
1654 				WATCHDOG_RESET();
1655 				xcount = width;
1656 				while (xcount--) {
1657 					cte = bmp->color_table[*bmap++];
1658 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1659 					fill_555rgb_pswap(fb, xpos++, cte.red,
1660 							  cte.green,
1661 							  cte.blue);
1662 					fb += 2;
1663 #else
1664 					FILL_15BIT_555RGB(cte.red, cte.green,
1665 							  cte.blue);
1666 #endif
1667 				}
1668 				bmap += padded_line;
1669 				fb -= (VIDEO_VISIBLE_COLS + width) *
1670 							VIDEO_PIXEL_SIZE;
1671 			}
1672 			break;
1673 		case GDF_16BIT_565RGB:
1674 			while (ycount--) {
1675 				WATCHDOG_RESET();
1676 				xcount = width;
1677 				while (xcount--) {
1678 					cte = bmp->color_table[*bmap++];
1679 					FILL_16BIT_565RGB(cte.red, cte.green,
1680 							  cte.blue);
1681 				}
1682 				bmap += padded_line;
1683 				fb -= (VIDEO_VISIBLE_COLS + width) *
1684 							VIDEO_PIXEL_SIZE;
1685 			}
1686 			break;
1687 		case GDF_32BIT_X888RGB:
1688 			while (ycount--) {
1689 				WATCHDOG_RESET();
1690 				xcount = width;
1691 				while (xcount--) {
1692 					cte = bmp->color_table[*bmap++];
1693 					FILL_32BIT_X888RGB(cte.red, cte.green,
1694 							   cte.blue);
1695 				}
1696 				bmap += padded_line;
1697 				fb -= (VIDEO_VISIBLE_COLS + width) *
1698 							VIDEO_PIXEL_SIZE;
1699 			}
1700 			break;
1701 		case GDF_24BIT_888RGB:
1702 			while (ycount--) {
1703 				WATCHDOG_RESET();
1704 				xcount = width;
1705 				while (xcount--) {
1706 					cte = bmp->color_table[*bmap++];
1707 					FILL_24BIT_888RGB(cte.red, cte.green,
1708 							  cte.blue);
1709 				}
1710 				bmap += padded_line;
1711 				fb -= (VIDEO_VISIBLE_COLS + width) *
1712 							VIDEO_PIXEL_SIZE;
1713 			}
1714 			break;
1715 		}
1716 		break;
1717 	case 24:
1718 		padded_line -= 3 * width;
1719 		ycount = height;
1720 		switch (VIDEO_DATA_FORMAT) {
1721 		case GDF__8BIT_332RGB:
1722 			while (ycount--) {
1723 				WATCHDOG_RESET();
1724 				xcount = width;
1725 				while (xcount--) {
1726 					FILL_8BIT_332RGB(bmap[2], bmap[1],
1727 							 bmap[0]);
1728 					bmap += 3;
1729 				}
1730 				bmap += padded_line;
1731 				fb -= (VIDEO_VISIBLE_COLS + width) *
1732 							VIDEO_PIXEL_SIZE;
1733 			}
1734 			break;
1735 		case GDF_15BIT_555RGB:
1736 			while (ycount--) {
1737 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1738 				int xpos = x;
1739 #endif
1740 				WATCHDOG_RESET();
1741 				xcount = width;
1742 				while (xcount--) {
1743 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1744 					fill_555rgb_pswap(fb, xpos++, bmap[2],
1745 							  bmap[1], bmap[0]);
1746 					fb += 2;
1747 #else
1748 					FILL_15BIT_555RGB(bmap[2], bmap[1],
1749 							  bmap[0]);
1750 #endif
1751 					bmap += 3;
1752 				}
1753 				bmap += padded_line;
1754 				fb -= (VIDEO_VISIBLE_COLS + width) *
1755 							VIDEO_PIXEL_SIZE;
1756 			}
1757 			break;
1758 		case GDF_16BIT_565RGB:
1759 			while (ycount--) {
1760 				WATCHDOG_RESET();
1761 				xcount = width;
1762 				while (xcount--) {
1763 					FILL_16BIT_565RGB(bmap[2], bmap[1],
1764 							  bmap[0]);
1765 					bmap += 3;
1766 				}
1767 				bmap += padded_line;
1768 				fb -= (VIDEO_VISIBLE_COLS + width) *
1769 							VIDEO_PIXEL_SIZE;
1770 			}
1771 			break;
1772 		case GDF_32BIT_X888RGB:
1773 			while (ycount--) {
1774 				WATCHDOG_RESET();
1775 				xcount = width;
1776 				while (xcount--) {
1777 					FILL_32BIT_X888RGB(bmap[2], bmap[1],
1778 							   bmap[0]);
1779 					bmap += 3;
1780 				}
1781 				bmap += padded_line;
1782 				fb -= (VIDEO_VISIBLE_COLS + width) *
1783 							VIDEO_PIXEL_SIZE;
1784 			}
1785 			break;
1786 		case GDF_24BIT_888RGB:
1787 			while (ycount--) {
1788 				WATCHDOG_RESET();
1789 				xcount = width;
1790 				while (xcount--) {
1791 					FILL_24BIT_888RGB(bmap[2], bmap[1],
1792 							  bmap[0]);
1793 					bmap += 3;
1794 				}
1795 				bmap += padded_line;
1796 				fb -= (VIDEO_VISIBLE_COLS + width) *
1797 							VIDEO_PIXEL_SIZE;
1798 			}
1799 			break;
1800 		default:
1801 			printf("Error: 24 bits/pixel bitmap incompatible "
1802 				"with current video mode\n");
1803 			break;
1804 		}
1805 		break;
1806 	default:
1807 		printf("Error: %d bit/pixel bitmaps not supported by U-Boot\n",
1808 			le16_to_cpu(bmp->header.bit_count));
1809 		break;
1810 	}
1811 
1812 #ifdef CONFIG_VIDEO_BMP_GZIP
1813 	if (dst) {
1814 		free(dst);
1815 	}
1816 #endif
1817 
1818 	if (cfb_do_flush_cache)
1819 		flush_cache(VIDEO_FB_ADRS, VIDEO_SIZE);
1820 	return (0);
1821 }
1822 #endif
1823 
1824 
1825 #ifdef CONFIG_VIDEO_LOGO
1826 static int video_logo_xpos;
1827 static int video_logo_ypos;
1828 
1829 static void plot_logo_or_black(void *screen, int width, int x, int y,	\
1830 			int black);
1831 
1832 static void logo_plot(void *screen, int width, int x, int y)
1833 {
1834 	plot_logo_or_black(screen, width, x, y, 0);
1835 }
1836 
1837 static void logo_black(void)
1838 {
1839 	plot_logo_or_black(video_fb_address, \
1840 			VIDEO_COLS, \
1841 			video_logo_xpos, \
1842 			video_logo_ypos, \
1843 			1);
1844 }
1845 
1846 static int do_clrlogo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1847 {
1848 	if (argc != 1)
1849 		return cmd_usage(cmdtp);
1850 
1851 	logo_black();
1852 	return 0;
1853 }
1854 
1855 U_BOOT_CMD(
1856 	   clrlogo, 1, 0, do_clrlogo,
1857 	   "fill the boot logo area with black",
1858 	   " "
1859 	   );
1860 
1861 static void plot_logo_or_black(void *screen, int width, int x, int y, int black)
1862 {
1863 
1864 	int xcount, i;
1865 	int skip = (width - VIDEO_LOGO_WIDTH) * VIDEO_PIXEL_SIZE;
1866 	int ycount = video_logo_height;
1867 	unsigned char r, g, b, *logo_red, *logo_blue, *logo_green;
1868 	unsigned char *source;
1869 	unsigned char *dest;
1870 
1871 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
1872 	if (x == BMP_ALIGN_CENTER)
1873 		x = max(0, (int)(VIDEO_VISIBLE_COLS - VIDEO_LOGO_WIDTH) / 2);
1874 	else if (x < 0)
1875 		x = max(0, (int)(VIDEO_VISIBLE_COLS - VIDEO_LOGO_WIDTH + x + 1));
1876 
1877 	if (y == BMP_ALIGN_CENTER)
1878 		y = max(0, (int)(VIDEO_VISIBLE_ROWS - VIDEO_LOGO_HEIGHT) / 2);
1879 	else if (y < 0)
1880 		y = max(0, (int)(VIDEO_VISIBLE_ROWS - VIDEO_LOGO_HEIGHT + y + 1));
1881 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
1882 
1883 	dest = (unsigned char *)screen + (y * width  + x) * VIDEO_PIXEL_SIZE;
1884 
1885 #ifdef CONFIG_VIDEO_BMP_LOGO
1886 	source = bmp_logo_bitmap;
1887 
1888 	/* Allocate temporary space for computing colormap */
1889 	logo_red = malloc(BMP_LOGO_COLORS);
1890 	logo_green = malloc(BMP_LOGO_COLORS);
1891 	logo_blue = malloc(BMP_LOGO_COLORS);
1892 	/* Compute color map */
1893 	for (i = 0; i < VIDEO_LOGO_COLORS; i++) {
1894 		logo_red[i] = (bmp_logo_palette[i] & 0x0f00) >> 4;
1895 		logo_green[i] = (bmp_logo_palette[i] & 0x00f0);
1896 		logo_blue[i] = (bmp_logo_palette[i] & 0x000f) << 4;
1897 	}
1898 #else
1899 	source = linux_logo;
1900 	logo_red = linux_logo_red;
1901 	logo_green = linux_logo_green;
1902 	logo_blue = linux_logo_blue;
1903 #endif
1904 
1905 	if (VIDEO_DATA_FORMAT == GDF__8BIT_INDEX) {
1906 		for (i = 0; i < VIDEO_LOGO_COLORS; i++) {
1907 			video_set_lut(i + VIDEO_LOGO_LUT_OFFSET,
1908 				      logo_red[i], logo_green[i],
1909 				      logo_blue[i]);
1910 		}
1911 	}
1912 
1913 	while (ycount--) {
1914 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1915 		int xpos = x;
1916 #endif
1917 		xcount = VIDEO_LOGO_WIDTH;
1918 		while (xcount--) {
1919 			if (black) {
1920 				r = 0x00;
1921 				g = 0x00;
1922 				b = 0x00;
1923 			} else {
1924 				r = logo_red[*source - VIDEO_LOGO_LUT_OFFSET];
1925 				g = logo_green[*source - VIDEO_LOGO_LUT_OFFSET];
1926 				b = logo_blue[*source - VIDEO_LOGO_LUT_OFFSET];
1927 			}
1928 
1929 			switch (VIDEO_DATA_FORMAT) {
1930 			case GDF__8BIT_INDEX:
1931 				*dest = *source;
1932 				break;
1933 			case GDF__8BIT_332RGB:
1934 				*dest = ((r >> 5) << 5) |
1935 					((g >> 5) << 2) |
1936 					 (b >> 6);
1937 				break;
1938 			case GDF_15BIT_555RGB:
1939 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1940 				fill_555rgb_pswap(dest, xpos++, r, g, b);
1941 #else
1942 				*(unsigned short *) dest =
1943 					SWAP16((unsigned short) (
1944 							((r >> 3) << 10) |
1945 							((g >> 3) <<  5) |
1946 							 (b >> 3)));
1947 #endif
1948 				break;
1949 			case GDF_16BIT_565RGB:
1950 				*(unsigned short *) dest =
1951 					SWAP16((unsigned short) (
1952 							((r >> 3) << 11) |
1953 							((g >> 2) <<  5) |
1954 							 (b >> 3)));
1955 				break;
1956 			case GDF_32BIT_X888RGB:
1957 				*(unsigned long *) dest =
1958 					SWAP32((unsigned long) (
1959 							(r << 16) |
1960 							(g <<  8) |
1961 							 b));
1962 				break;
1963 			case GDF_24BIT_888RGB:
1964 #ifdef VIDEO_FB_LITTLE_ENDIAN
1965 				dest[0] = b;
1966 				dest[1] = g;
1967 				dest[2] = r;
1968 #else
1969 				dest[0] = r;
1970 				dest[1] = g;
1971 				dest[2] = b;
1972 #endif
1973 				break;
1974 			}
1975 			source++;
1976 			dest += VIDEO_PIXEL_SIZE;
1977 		}
1978 		dest += skip;
1979 	}
1980 #ifdef CONFIG_VIDEO_BMP_LOGO
1981 	free(logo_red);
1982 	free(logo_green);
1983 	free(logo_blue);
1984 #endif
1985 }
1986 
1987 static void *video_logo(void)
1988 {
1989 	char info[128];
1990 	int space, len;
1991 	__maybe_unused int y_off = 0;
1992 	__maybe_unused ulong addr;
1993 	__maybe_unused char *s;
1994 
1995 	splash_get_pos(&video_logo_xpos, &video_logo_ypos);
1996 
1997 #ifdef CONFIG_SPLASH_SCREEN
1998 	s = getenv("splashimage");
1999 	if (s != NULL) {
2000 		splash_screen_prepare();
2001 		addr = simple_strtoul(s, NULL, 16);
2002 
2003 		if (video_display_bitmap(addr,
2004 					video_logo_xpos,
2005 					video_logo_ypos) == 0) {
2006 			video_logo_height = 0;
2007 			return ((void *) (video_fb_address));
2008 		}
2009 	}
2010 #endif /* CONFIG_SPLASH_SCREEN */
2011 
2012 	logo_plot(video_fb_address, VIDEO_COLS,
2013 		  video_logo_xpos, video_logo_ypos);
2014 
2015 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
2016 	/*
2017 	 * when using splashpos for video_logo, skip any info
2018 	 * output on video console if the logo is not at 0,0
2019 	 */
2020 	if (video_logo_xpos || video_logo_ypos) {
2021 		/*
2022 		 * video_logo_height is used in text and cursor offset
2023 		 * calculations. Since the console is below the logo,
2024 		 * we need to adjust the logo height
2025 		 */
2026 		if (video_logo_ypos == BMP_ALIGN_CENTER)
2027 			video_logo_height += max(0, (int)(VIDEO_VISIBLE_ROWS -
2028 						     VIDEO_LOGO_HEIGHT) / 2);
2029 		else if (video_logo_ypos > 0)
2030 			video_logo_height += video_logo_ypos;
2031 
2032 		return video_fb_address + video_logo_height * VIDEO_LINE_LEN;
2033 	}
2034 #endif
2035 	if (board_cfb_skip())
2036 		return 0;
2037 
2038 	sprintf(info, " %s", version_string);
2039 
2040 	space = (VIDEO_LINE_LEN / 2 - VIDEO_INFO_X) / VIDEO_FONT_WIDTH;
2041 	len = strlen(info);
2042 
2043 	if (len > space) {
2044 		video_drawchars(VIDEO_INFO_X, VIDEO_INFO_Y,
2045 				(uchar *) info, space);
2046 		video_drawchars(VIDEO_INFO_X + VIDEO_FONT_WIDTH,
2047 				VIDEO_INFO_Y + VIDEO_FONT_HEIGHT,
2048 				(uchar *) info + space, len - space);
2049 		y_off = 1;
2050 	} else
2051 		video_drawstring(VIDEO_INFO_X, VIDEO_INFO_Y, (uchar *) info);
2052 
2053 #ifdef CONFIG_CONSOLE_EXTRA_INFO
2054 	{
2055 		int i, n =
2056 			((video_logo_height -
2057 			  VIDEO_FONT_HEIGHT) / VIDEO_FONT_HEIGHT);
2058 
2059 		for (i = 1; i < n; i++) {
2060 			video_get_info_str(i, info);
2061 			if (!*info)
2062 				continue;
2063 
2064 			len = strlen(info);
2065 			if (len > space) {
2066 				video_drawchars(VIDEO_INFO_X,
2067 						VIDEO_INFO_Y +
2068 						(i + y_off) *
2069 							VIDEO_FONT_HEIGHT,
2070 						(uchar *) info, space);
2071 				y_off++;
2072 				video_drawchars(VIDEO_INFO_X +
2073 						VIDEO_FONT_WIDTH,
2074 						VIDEO_INFO_Y +
2075 							(i + y_off) *
2076 							VIDEO_FONT_HEIGHT,
2077 						(uchar *) info + space,
2078 						len - space);
2079 			} else {
2080 				video_drawstring(VIDEO_INFO_X,
2081 						 VIDEO_INFO_Y +
2082 						 (i + y_off) *
2083 							VIDEO_FONT_HEIGHT,
2084 						 (uchar *) info);
2085 			}
2086 		}
2087 	}
2088 #endif
2089 
2090 	return (video_fb_address + video_logo_height * VIDEO_LINE_LEN);
2091 }
2092 #endif
2093 
2094 static int cfb_fb_is_in_dram(void)
2095 {
2096 	bd_t *bd = gd->bd;
2097 #if defined(CONFIG_ARM) || defined(CONFIG_AVR32) || defined(COFNIG_NDS32) || \
2098 defined(CONFIG_SANDBOX) || defined(CONFIG_X86)
2099 	ulong start, end;
2100 	int i;
2101 
2102 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
2103 		start = bd->bi_dram[i].start;
2104 		end = bd->bi_dram[i].start + bd->bi_dram[i].size - 1;
2105 		if ((ulong)video_fb_address >= start &&
2106 		    (ulong)video_fb_address < end)
2107 			return 1;
2108 	}
2109 #else
2110 	if ((ulong)video_fb_address >= bd->bi_memstart &&
2111 	    (ulong)video_fb_address < bd->bi_memstart + bd->bi_memsize)
2112 		return 1;
2113 #endif
2114 	return 0;
2115 }
2116 
2117 void video_clear(void)
2118 {
2119 	if (!video_fb_address)
2120 		return;
2121 #ifdef VIDEO_HW_RECTFILL
2122 	video_hw_rectfill(VIDEO_PIXEL_SIZE,	/* bytes per pixel */
2123 			  0,			/* dest pos x */
2124 			  0,			/* dest pos y */
2125 			  VIDEO_VISIBLE_COLS,	/* frame width */
2126 			  VIDEO_VISIBLE_ROWS,	/* frame height */
2127 			  bgx			/* fill color */
2128 	);
2129 #else
2130 	memsetl(video_fb_address,
2131 		(VIDEO_VISIBLE_ROWS * VIDEO_LINE_LEN) / sizeof(int), bgx);
2132 #endif
2133 }
2134 
2135 static int video_init(void)
2136 {
2137 	unsigned char color8;
2138 
2139 	pGD = video_hw_init();
2140 	if (pGD == NULL)
2141 		return -1;
2142 
2143 	video_fb_address = (void *) VIDEO_FB_ADRS;
2144 #ifdef CONFIG_VIDEO_HW_CURSOR
2145 	video_init_hw_cursor(VIDEO_FONT_WIDTH, VIDEO_FONT_HEIGHT);
2146 #endif
2147 
2148 	cfb_do_flush_cache = cfb_fb_is_in_dram() && dcache_status();
2149 
2150 	/* Init drawing pats */
2151 	switch (VIDEO_DATA_FORMAT) {
2152 	case GDF__8BIT_INDEX:
2153 		video_set_lut(0x01, CONSOLE_FG_COL, CONSOLE_FG_COL,
2154 			      CONSOLE_FG_COL);
2155 		video_set_lut(0x00, CONSOLE_BG_COL, CONSOLE_BG_COL,
2156 			      CONSOLE_BG_COL);
2157 		fgx = 0x01010101;
2158 		bgx = 0x00000000;
2159 		break;
2160 	case GDF__8BIT_332RGB:
2161 		color8 = ((CONSOLE_FG_COL & 0xe0) |
2162 			  ((CONSOLE_FG_COL >> 3) & 0x1c) |
2163 			  CONSOLE_FG_COL >> 6);
2164 		fgx = (color8 << 24) | (color8 << 16) | (color8 << 8) |
2165 			color8;
2166 		color8 = ((CONSOLE_BG_COL & 0xe0) |
2167 			  ((CONSOLE_BG_COL >> 3) & 0x1c) |
2168 			  CONSOLE_BG_COL >> 6);
2169 		bgx = (color8 << 24) | (color8 << 16) | (color8 << 8) |
2170 			color8;
2171 		break;
2172 	case GDF_15BIT_555RGB:
2173 		fgx = (((CONSOLE_FG_COL >> 3) << 26) |
2174 		       ((CONSOLE_FG_COL >> 3) << 21) |
2175 		       ((CONSOLE_FG_COL >> 3) << 16) |
2176 		       ((CONSOLE_FG_COL >> 3) << 10) |
2177 		       ((CONSOLE_FG_COL >> 3) <<  5) |
2178 			(CONSOLE_FG_COL >> 3));
2179 		bgx = (((CONSOLE_BG_COL >> 3) << 26) |
2180 		       ((CONSOLE_BG_COL >> 3) << 21) |
2181 		       ((CONSOLE_BG_COL >> 3) << 16) |
2182 		       ((CONSOLE_BG_COL >> 3) << 10) |
2183 		       ((CONSOLE_BG_COL >> 3) <<  5) |
2184 			(CONSOLE_BG_COL >> 3));
2185 		break;
2186 	case GDF_16BIT_565RGB:
2187 		fgx = (((CONSOLE_FG_COL >> 3) << 27) |
2188 		       ((CONSOLE_FG_COL >> 2) << 21) |
2189 		       ((CONSOLE_FG_COL >> 3) << 16) |
2190 		       ((CONSOLE_FG_COL >> 3) << 11) |
2191 		       ((CONSOLE_FG_COL >> 2) <<  5) |
2192 			(CONSOLE_FG_COL >> 3));
2193 		bgx = (((CONSOLE_BG_COL >> 3) << 27) |
2194 		       ((CONSOLE_BG_COL >> 2) << 21) |
2195 		       ((CONSOLE_BG_COL >> 3) << 16) |
2196 		       ((CONSOLE_BG_COL >> 3) << 11) |
2197 		       ((CONSOLE_BG_COL >> 2) <<  5) |
2198 			(CONSOLE_BG_COL >> 3));
2199 		break;
2200 	case GDF_32BIT_X888RGB:
2201 		fgx =	(CONSOLE_FG_COL << 16) |
2202 			(CONSOLE_FG_COL <<  8) |
2203 			 CONSOLE_FG_COL;
2204 		bgx =	(CONSOLE_BG_COL << 16) |
2205 			(CONSOLE_BG_COL <<  8) |
2206 			 CONSOLE_BG_COL;
2207 		break;
2208 	case GDF_24BIT_888RGB:
2209 		fgx =	(CONSOLE_FG_COL << 24) |
2210 			(CONSOLE_FG_COL << 16) |
2211 			(CONSOLE_FG_COL <<  8) |
2212 			 CONSOLE_FG_COL;
2213 		bgx =	(CONSOLE_BG_COL << 24) |
2214 			(CONSOLE_BG_COL << 16) |
2215 			(CONSOLE_BG_COL <<  8) |
2216 			 CONSOLE_BG_COL;
2217 		break;
2218 	}
2219 	eorx = fgx ^ bgx;
2220 
2221 	video_clear();
2222 
2223 #ifdef CONFIG_VIDEO_LOGO
2224 	/* Plot the logo and get start point of console */
2225 	debug("Video: Drawing the logo ...\n");
2226 	video_console_address = video_logo();
2227 #else
2228 	video_console_address = video_fb_address;
2229 #endif
2230 
2231 	/* Initialize the console */
2232 	console_col = 0;
2233 	console_row = 0;
2234 
2235 	if (cfb_do_flush_cache)
2236 		flush_cache(VIDEO_FB_ADRS, VIDEO_SIZE);
2237 
2238 	return 0;
2239 }
2240 
2241 /*
2242  * Implement a weak default function for boards that optionally
2243  * need to skip the video initialization.
2244  */
2245 __weak int board_video_skip(void)
2246 {
2247 	/* As default, don't skip test */
2248 	return 0;
2249 }
2250 
2251 int drv_video_init(void)
2252 {
2253 	int skip_dev_init;
2254 	struct stdio_dev console_dev;
2255 	bool have_keyboard;
2256 
2257 	/* Check if video initialization should be skipped */
2258 	if (board_video_skip())
2259 		return 0;
2260 
2261 	/* Init video chip - returns with framebuffer cleared */
2262 	skip_dev_init = (video_init() == -1);
2263 
2264 	if (board_cfb_skip())
2265 		return 0;
2266 
2267 #if defined(CONFIG_VGA_AS_SINGLE_DEVICE)
2268 	have_keyboard = false;
2269 #elif defined(CONFIG_OF_CONTROL)
2270 	have_keyboard = !fdtdec_get_config_bool(gd->fdt_blob,
2271 						"u-boot,no-keyboard");
2272 #else
2273 	have_keyboard = true;
2274 #endif
2275 	if (have_keyboard) {
2276 		debug("KBD: Keyboard init ...\n");
2277 #if !defined(CONFIG_VGA_AS_SINGLE_DEVICE)
2278 		skip_dev_init |= (VIDEO_KBD_INIT_FCT == -1);
2279 #endif
2280 	}
2281 	if (skip_dev_init)
2282 		return 0;
2283 
2284 	/* Init vga device */
2285 	memset(&console_dev, 0, sizeof(console_dev));
2286 	strcpy(console_dev.name, "vga");
2287 	console_dev.ext = DEV_EXT_VIDEO;	/* Video extensions */
2288 	console_dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_SYSTEM;
2289 	console_dev.putc = video_putc;	/* 'putc' function */
2290 	console_dev.puts = video_puts;	/* 'puts' function */
2291 
2292 #if !defined(CONFIG_VGA_AS_SINGLE_DEVICE)
2293 	if (have_keyboard) {
2294 		/* Also init console device */
2295 		console_dev.flags |= DEV_FLAGS_INPUT;
2296 		console_dev.tstc = VIDEO_TSTC_FCT;	/* 'tstc' function */
2297 		console_dev.getc = VIDEO_GETC_FCT;	/* 'getc' function */
2298 	}
2299 #endif
2300 
2301 	if (stdio_register(&console_dev) != 0)
2302 		return 0;
2303 
2304 	/* Return success */
2305 	return 1;
2306 }
2307 
2308 void video_position_cursor(unsigned col, unsigned row)
2309 {
2310 	console_col = min(col, CONSOLE_COLS - 1);
2311 	console_row = min(row, CONSOLE_ROWS - 1);
2312 }
2313 
2314 int video_get_pixel_width(void)
2315 {
2316 	return VIDEO_VISIBLE_COLS;
2317 }
2318 
2319 int video_get_pixel_height(void)
2320 {
2321 	return VIDEO_VISIBLE_ROWS;
2322 }
2323 
2324 int video_get_screen_rows(void)
2325 {
2326 	return CONSOLE_ROWS;
2327 }
2328 
2329 int video_get_screen_columns(void)
2330 {
2331 	return CONSOLE_COLS;
2332 }
2333