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