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