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