xref: /rk3399_rockchip-uboot/drivers/video/cfb_console.c (revision 8a805df13615667ebdcc9f3a3a6fbf6c7778a992)
1 /*
2  * (C) Copyright 2002 ELTEC Elektronik AG
3  * Frank Gottschling <fgottschling@eltec.de>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 /*
25  * cfb_console.c
26  *
27  * Color Framebuffer Console driver for 8/15/16/24/32 bits per pixel.
28  *
29  * At the moment only the 8x16 font is tested and the font fore- and
30  * background color is limited to black/white/gray colors. The Linux
31  * logo can be placed in the upper left corner and additional board
32  * information strings (that normaly goes to serial port) can be drawed.
33  *
34  * The console driver can use the standard PC keyboard interface (i8042)
35  * for character input. Character output goes to a memory mapped video
36  * framebuffer with little or big-endian organisation.
37  * With environment setting 'console=serial' the console i/o can be
38  * forced to serial port.
39 
40  The driver uses graphic specific defines/parameters/functions:
41 
42  (for SMI LynxE graphic chip)
43 
44  CONFIG_VIDEO_SMI_LYNXEM - use graphic driver for SMI 710,712,810
45  VIDEO_FB_LITTLE_ENDIAN	 - framebuffer organisation default: big endian
46  VIDEO_HW_RECTFILL	 - graphic driver supports hardware rectangle fill
47  VIDEO_HW_BITBLT	 - graphic driver supports hardware bit blt
48 
49  Console Parameters are set by graphic drivers global struct:
50 
51  VIDEO_VISIBLE_COLS	     - x resolution
52  VIDEO_VISIBLE_ROWS	     - y resolution
53  VIDEO_PIXEL_SIZE	     - storage size in byte per pixel
54  VIDEO_DATA_FORMAT	     - graphical data format GDF
55  VIDEO_FB_ADRS		     - start of video memory
56 
57  CONFIG_I8042_KBD	     - AT Keyboard driver for i8042
58  VIDEO_KBD_INIT_FCT	     - init function for keyboard
59  VIDEO_TSTC_FCT		     - keyboard_tstc function
60  VIDEO_GETC_FCT		     - keyboard_getc function
61 
62  CONFIG_CONSOLE_CURSOR	     - on/off drawing cursor is done with delay
63 			       loop in VIDEO_TSTC_FCT (i8042)
64  CONFIG_SYS_CONSOLE_BLINK_COUNT     - value for delay loop - blink rate
65  CONFIG_CONSOLE_TIME	     - display time/date in upper right corner,
66 			       needs CONFIG_CMD_DATE and CONFIG_CONSOLE_CURSOR
67  CONFIG_VIDEO_LOGO	     - display Linux Logo in upper left corner
68  CONFIG_VIDEO_BMP_LOGO	     - use bmp_logo instead of linux_logo
69  CONFIG_CONSOLE_EXTRA_INFO   - display additional board information strings
70 			       that normaly goes to serial port. This define
71 			       requires a board specific function:
72 			       video_drawstring (VIDEO_INFO_X,
73 						 VIDEO_INFO_Y + i*VIDEO_FONT_HEIGHT,
74 						 info);
75 			       that fills a info buffer at i=row.
76 			       s.a: board/eltec/bab7xx.
77 CONFIG_VGA_AS_SINGLE_DEVICE  - If set the framebuffer device will be initialised
78 			       as an output only device. The Keyboard driver
79 			       will not be set-up. This may be used, if you
80 			       have none or more than one Keyboard devices
81 			       (USB Keyboard, AT Keyboard).
82 
83 CONFIG_VIDEO_SW_CURSOR:	     - Draws a cursor after the last character. No
84 			       blinking is provided. Uses the macros CURSOR_SET
85 			       and CURSOR_OFF.
86 CONFIG_VIDEO_HW_CURSOR:	     - Uses the hardware cursor capability of the
87 			       graphic chip. Uses the macro CURSOR_SET.
88 			       ATTENTION: If booting an OS, the display driver
89 			       must disable the hardware register of the graphic
90 			       chip. Otherwise a blinking field is displayed
91 */
92 
93 #include <common.h>
94 
95 #include <malloc.h>
96 
97 /*****************************************************************************/
98 /* Console device defines with SMI graphic				     */
99 /* Any other graphic must change this section				     */
100 /*****************************************************************************/
101 
102 #ifdef	CONFIG_VIDEO_SMI_LYNXEM
103 
104 #define VIDEO_FB_LITTLE_ENDIAN
105 #define VIDEO_HW_RECTFILL
106 #define VIDEO_HW_BITBLT
107 #endif
108 
109 /*****************************************************************************/
110 /* Defines for the CT69000 driver					     */
111 /*****************************************************************************/
112 #ifdef	CONFIG_VIDEO_CT69000
113 
114 #define VIDEO_FB_LITTLE_ENDIAN
115 #define VIDEO_HW_RECTFILL
116 #define VIDEO_HW_BITBLT
117 #endif
118 
119 /*****************************************************************************/
120 /* Defines for the SED13806 driver					     */
121 /*****************************************************************************/
122 #ifdef CONFIG_VIDEO_SED13806
123 
124 #ifndef CONFIG_TOTAL5200
125 #define VIDEO_FB_LITTLE_ENDIAN
126 #endif
127 #define VIDEO_HW_RECTFILL
128 #define VIDEO_HW_BITBLT
129 #endif
130 
131 /*****************************************************************************/
132 /* Defines for the SED13806 driver					     */
133 /*****************************************************************************/
134 #ifdef CONFIG_VIDEO_SM501
135 
136 #ifdef CONFIG_HH405
137 #define VIDEO_FB_LITTLE_ENDIAN
138 #endif
139 #endif
140 
141 /*****************************************************************************/
142 /* Defines for the MB862xx driver					     */
143 /*****************************************************************************/
144 #ifdef CONFIG_VIDEO_MB862xx
145 
146 #ifdef CONFIG_VIDEO_CORALP
147 #define VIDEO_FB_LITTLE_ENDIAN
148 #endif
149 #ifdef CONFIG_VIDEO_MB862xx_ACCEL
150 #define VIDEO_HW_RECTFILL
151 #define VIDEO_HW_BITBLT
152 #endif
153 #endif
154 
155 /*****************************************************************************/
156 /* Include video_fb.h after definitions of VIDEO_HW_RECTFILL etc	     */
157 /*****************************************************************************/
158 #include <video_fb.h>
159 
160 /*****************************************************************************/
161 /* some Macros								     */
162 /*****************************************************************************/
163 #define VIDEO_VISIBLE_COLS	(pGD->winSizeX)
164 #define VIDEO_VISIBLE_ROWS	(pGD->winSizeY)
165 #define VIDEO_PIXEL_SIZE	(pGD->gdfBytesPP)
166 #define VIDEO_DATA_FORMAT	(pGD->gdfIndex)
167 #define VIDEO_FB_ADRS		(pGD->frameAdrs)
168 
169 /*****************************************************************************/
170 /* Console device defines with i8042 keyboard controller		     */
171 /* Any other keyboard controller must change this section		     */
172 /*****************************************************************************/
173 
174 #ifdef	CONFIG_I8042_KBD
175 #include <i8042.h>
176 
177 #define VIDEO_KBD_INIT_FCT	i8042_kbd_init()
178 #define VIDEO_TSTC_FCT		i8042_tstc
179 #define VIDEO_GETC_FCT		i8042_getc
180 #endif
181 
182 /*****************************************************************************/
183 /* Console device							     */
184 /*****************************************************************************/
185 
186 #include <version.h>
187 #include <linux/types.h>
188 #include <stdio_dev.h>
189 #include <video_font.h>
190 
191 #if defined(CONFIG_CMD_DATE)
192 #include <rtc.h>
193 #endif
194 
195 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
196 #include <watchdog.h>
197 #include <bmp_layout.h>
198 
199 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
200 #define BMP_ALIGN_CENTER	0x7FFF
201 #endif
202 
203 #endif
204 
205 /*****************************************************************************/
206 /* Cursor definition:							     */
207 /* CONFIG_CONSOLE_CURSOR:  Uses a timer function (see drivers/input/i8042.c) */
208 /*                         to let the cursor blink. Uses the macros	     */
209 /*                         CURSOR_OFF and CURSOR_ON.			     */
210 /* CONFIG_VIDEO_SW_CURSOR: Draws a cursor after the last character. No	     */
211 /*			   blinking is provided. Uses the macros CURSOR_SET  */
212 /*			   and CURSOR_OFF.				     */
213 /* CONFIG_VIDEO_HW_CURSOR: Uses the hardware cursor capability of the	     */
214 /*			   graphic chip. Uses the macro CURSOR_SET.	     */
215 /*			   ATTENTION: If booting an OS, the display driver   */
216 /*			   must disable the hardware register of the graphic */
217 /*			   chip. Otherwise a blinking field is displayed     */
218 /*****************************************************************************/
219 #if !defined(CONFIG_CONSOLE_CURSOR) && \
220     !defined(CONFIG_VIDEO_SW_CURSOR) && \
221     !defined(CONFIG_VIDEO_HW_CURSOR)
222 /* no Cursor defined */
223 #define CURSOR_ON
224 #define CURSOR_OFF
225 #define CURSOR_SET
226 #endif
227 
228 #ifdef	CONFIG_CONSOLE_CURSOR
229 #ifdef	CURSOR_ON
230 #error	only one of CONFIG_CONSOLE_CURSOR,CONFIG_VIDEO_SW_CURSOR,CONFIG_VIDEO_HW_CURSOR can be defined
231 #endif
232 void	console_cursor (int state);
233 #define CURSOR_ON  console_cursor(1)
234 #define CURSOR_OFF console_cursor(0)
235 #define CURSOR_SET
236 #ifndef CONFIG_I8042_KBD
237 #warning Cursor drawing on/off needs timer function s.a. drivers/input/i8042.c
238 #endif
239 #else
240 #ifdef	CONFIG_CONSOLE_TIME
241 #error	CONFIG_CONSOLE_CURSOR must be defined for CONFIG_CONSOLE_TIME
242 #endif
243 #endif /* CONFIG_CONSOLE_CURSOR */
244 
245 #ifdef	CONFIG_VIDEO_SW_CURSOR
246 #ifdef	CURSOR_ON
247 #error	only one of CONFIG_CONSOLE_CURSOR,CONFIG_VIDEO_SW_CURSOR,CONFIG_VIDEO_HW_CURSOR can be defined
248 #endif
249 #define CURSOR_ON
250 #define CURSOR_OFF video_putchar(console_col * VIDEO_FONT_WIDTH,\
251 				 console_row * VIDEO_FONT_HEIGHT, ' ')
252 #define CURSOR_SET video_set_cursor()
253 #endif /* CONFIG_VIDEO_SW_CURSOR */
254 
255 
256 #ifdef CONFIG_VIDEO_HW_CURSOR
257 #ifdef	CURSOR_ON
258 #error	only one of CONFIG_CONSOLE_CURSOR,CONFIG_VIDEO_SW_CURSOR,CONFIG_VIDEO_HW_CURSOR can be defined
259 #endif
260 #define CURSOR_ON
261 #define CURSOR_OFF
262 #define CURSOR_SET video_set_hw_cursor(console_col * VIDEO_FONT_WIDTH, \
263 		  (console_row * VIDEO_FONT_HEIGHT) + video_logo_height)
264 #endif	/* CONFIG_VIDEO_HW_CURSOR */
265 
266 #ifdef	CONFIG_VIDEO_LOGO
267 #ifdef	CONFIG_VIDEO_BMP_LOGO
268 #include <bmp_logo.h>
269 #define VIDEO_LOGO_WIDTH	BMP_LOGO_WIDTH
270 #define VIDEO_LOGO_HEIGHT	BMP_LOGO_HEIGHT
271 #define VIDEO_LOGO_LUT_OFFSET	BMP_LOGO_OFFSET
272 #define VIDEO_LOGO_COLORS	BMP_LOGO_COLORS
273 
274 #else	/* CONFIG_VIDEO_BMP_LOGO */
275 #define LINUX_LOGO_WIDTH	80
276 #define LINUX_LOGO_HEIGHT	80
277 #define LINUX_LOGO_COLORS	214
278 #define LINUX_LOGO_LUT_OFFSET	0x20
279 #define __initdata
280 #include <linux_logo.h>
281 #define VIDEO_LOGO_WIDTH	LINUX_LOGO_WIDTH
282 #define VIDEO_LOGO_HEIGHT	LINUX_LOGO_HEIGHT
283 #define VIDEO_LOGO_LUT_OFFSET	LINUX_LOGO_LUT_OFFSET
284 #define VIDEO_LOGO_COLORS	LINUX_LOGO_COLORS
285 #endif	/* CONFIG_VIDEO_BMP_LOGO */
286 #define VIDEO_INFO_X		(VIDEO_LOGO_WIDTH)
287 #define VIDEO_INFO_Y		(VIDEO_FONT_HEIGHT/2)
288 #else	/* CONFIG_VIDEO_LOGO */
289 #define VIDEO_LOGO_WIDTH	0
290 #define VIDEO_LOGO_HEIGHT	0
291 #endif	/* CONFIG_VIDEO_LOGO */
292 
293 #define VIDEO_COLS		VIDEO_VISIBLE_COLS
294 #define VIDEO_ROWS		VIDEO_VISIBLE_ROWS
295 #define VIDEO_SIZE		(VIDEO_ROWS*VIDEO_COLS*VIDEO_PIXEL_SIZE)
296 #define VIDEO_PIX_BLOCKS	(VIDEO_SIZE >> 2)
297 #define VIDEO_LINE_LEN		(VIDEO_COLS*VIDEO_PIXEL_SIZE)
298 #define VIDEO_BURST_LEN		(VIDEO_COLS/8)
299 
300 #ifdef	CONFIG_VIDEO_LOGO
301 #define CONSOLE_ROWS		((VIDEO_ROWS - video_logo_height) / VIDEO_FONT_HEIGHT)
302 #else
303 #define CONSOLE_ROWS		(VIDEO_ROWS / VIDEO_FONT_HEIGHT)
304 #endif
305 
306 #define CONSOLE_COLS		(VIDEO_COLS / VIDEO_FONT_WIDTH)
307 #define CONSOLE_ROW_SIZE	(VIDEO_FONT_HEIGHT * VIDEO_LINE_LEN)
308 #define CONSOLE_ROW_FIRST	(video_console_address)
309 #define CONSOLE_ROW_SECOND	(video_console_address + CONSOLE_ROW_SIZE)
310 #define CONSOLE_ROW_LAST	(video_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE)
311 #define CONSOLE_SIZE		(CONSOLE_ROW_SIZE * CONSOLE_ROWS)
312 #define CONSOLE_SCROLL_SIZE	(CONSOLE_SIZE - CONSOLE_ROW_SIZE)
313 
314 /* Macros */
315 #ifdef	VIDEO_FB_LITTLE_ENDIAN
316 #define SWAP16(x)	 ((((x) & 0x00ff) << 8) | ( (x) >> 8))
317 #define SWAP32(x)	 ((((x) & 0x000000ff) << 24) | (((x) & 0x0000ff00) << 8)|\
318 			  (((x) & 0x00ff0000) >>  8) | (((x) & 0xff000000) >> 24) )
319 #define SHORTSWAP32(x)	 ((((x) & 0x000000ff) <<  8) | (((x) & 0x0000ff00) >> 8)|\
320 			  (((x) & 0x00ff0000) <<  8) | (((x) & 0xff000000) >> 8) )
321 #else
322 #define SWAP16(x)	 (x)
323 #define SWAP32(x)	 (x)
324 #if defined(VIDEO_FB_16BPP_WORD_SWAP)
325 #define SHORTSWAP32(x)	 ( ((x) >> 16) | ((x) << 16) )
326 #else
327 #define SHORTSWAP32(x)	 (x)
328 #endif
329 #endif
330 
331 #if defined(DEBUG) || defined(DEBUG_CFB_CONSOLE)
332 #define PRINTD(x)	  printf(x)
333 #else
334 #define PRINTD(x)
335 #endif
336 
337 
338 #ifdef CONFIG_CONSOLE_EXTRA_INFO
339 extern void video_get_info_str (    /* setup a board string: type, speed, etc. */
340     int line_number,	    /* location to place info string beside logo */
341     char *info		    /* buffer for info string */
342     );
343 
344 #endif
345 
346 /* Locals */
347 static GraphicDevice *pGD;	/* Pointer to Graphic array */
348 
349 static void *video_fb_address;		/* frame buffer address */
350 static void *video_console_address;	/* console buffer start address */
351 
352 static int video_logo_height = VIDEO_LOGO_HEIGHT;
353 
354 static int console_col = 0; /* cursor col */
355 static int console_row = 0; /* cursor row */
356 
357 static u32 eorx, fgx, bgx;  /* color pats */
358 
359 static const int video_font_draw_table8[] = {
360 	    0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff,
361 	    0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
362 	    0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff,
363 	    0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff };
364 
365 static const int video_font_draw_table15[] = {
366 	    0x00000000, 0x00007fff, 0x7fff0000, 0x7fff7fff };
367 
368 static const int video_font_draw_table16[] = {
369 	    0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff };
370 
371 static const int video_font_draw_table24[16][3] = {
372 	    { 0x00000000, 0x00000000, 0x00000000 },
373 	    { 0x00000000, 0x00000000, 0x00ffffff },
374 	    { 0x00000000, 0x0000ffff, 0xff000000 },
375 	    { 0x00000000, 0x0000ffff, 0xffffffff },
376 	    { 0x000000ff, 0xffff0000, 0x00000000 },
377 	    { 0x000000ff, 0xffff0000, 0x00ffffff },
378 	    { 0x000000ff, 0xffffffff, 0xff000000 },
379 	    { 0x000000ff, 0xffffffff, 0xffffffff },
380 	    { 0xffffff00, 0x00000000, 0x00000000 },
381 	    { 0xffffff00, 0x00000000, 0x00ffffff },
382 	    { 0xffffff00, 0x0000ffff, 0xff000000 },
383 	    { 0xffffff00, 0x0000ffff, 0xffffffff },
384 	    { 0xffffffff, 0xffff0000, 0x00000000 },
385 	    { 0xffffffff, 0xffff0000, 0x00ffffff },
386 	    { 0xffffffff, 0xffffffff, 0xff000000 },
387 	    { 0xffffffff, 0xffffffff, 0xffffffff } };
388 
389 static const int video_font_draw_table32[16][4] = {
390 	    { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
391 	    { 0x00000000, 0x00000000, 0x00000000, 0x00ffffff },
392 	    { 0x00000000, 0x00000000, 0x00ffffff, 0x00000000 },
393 	    { 0x00000000, 0x00000000, 0x00ffffff, 0x00ffffff },
394 	    { 0x00000000, 0x00ffffff, 0x00000000, 0x00000000 },
395 	    { 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff },
396 	    { 0x00000000, 0x00ffffff, 0x00ffffff, 0x00000000 },
397 	    { 0x00000000, 0x00ffffff, 0x00ffffff, 0x00ffffff },
398 	    { 0x00ffffff, 0x00000000, 0x00000000, 0x00000000 },
399 	    { 0x00ffffff, 0x00000000, 0x00000000, 0x00ffffff },
400 	    { 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000 },
401 	    { 0x00ffffff, 0x00000000, 0x00ffffff, 0x00ffffff },
402 	    { 0x00ffffff, 0x00ffffff, 0x00000000, 0x00000000 },
403 	    { 0x00ffffff, 0x00ffffff, 0x00000000, 0x00ffffff },
404 	    { 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00000000 },
405 	    { 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff } };
406 
407 
408 /******************************************************************************/
409 
410 static void video_drawchars (int xx, int yy, unsigned char *s, int count)
411 {
412 	u8 *cdat, *dest, *dest0;
413 	int rows, offset, c;
414 
415 	offset = yy * VIDEO_LINE_LEN + xx * VIDEO_PIXEL_SIZE;
416 	dest0 = video_fb_address + offset;
417 
418 	switch (VIDEO_DATA_FORMAT) {
419 	case GDF__8BIT_INDEX:
420 	case GDF__8BIT_332RGB:
421 		while (count--) {
422 			c = *s;
423 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
424 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
425 			     rows--;
426 			     dest += VIDEO_LINE_LEN) {
427 				u8 bits = *cdat++;
428 
429 				((u32 *) dest)[0] = (video_font_draw_table8[bits >> 4] & eorx) ^ bgx;
430 				((u32 *) dest)[1] = (video_font_draw_table8[bits & 15] & eorx) ^ bgx;
431 			}
432 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
433 			s++;
434 		}
435 		break;
436 
437 	case GDF_15BIT_555RGB:
438 		while (count--) {
439 			c = *s;
440 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
441 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
442 			     rows--;
443 			     dest += VIDEO_LINE_LEN) {
444 				u8 bits = *cdat++;
445 
446 				((u32 *) dest)[0] = SHORTSWAP32 ((video_font_draw_table15 [bits >> 6] & eorx) ^ bgx);
447 				((u32 *) dest)[1] = SHORTSWAP32 ((video_font_draw_table15 [bits >> 4 & 3] & eorx) ^ bgx);
448 				((u32 *) dest)[2] = SHORTSWAP32 ((video_font_draw_table15 [bits >> 2 & 3] & eorx) ^ bgx);
449 				((u32 *) dest)[3] = SHORTSWAP32 ((video_font_draw_table15 [bits & 3] & eorx) ^ 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--;
462 			     dest += VIDEO_LINE_LEN) {
463 				u8 bits = *cdat++;
464 
465 				((u32 *) dest)[0] = SHORTSWAP32 ((video_font_draw_table16 [bits >> 6] & eorx) ^ bgx);
466 				((u32 *) dest)[1] = SHORTSWAP32 ((video_font_draw_table16 [bits >> 4 & 3] & eorx) ^ bgx);
467 				((u32 *) dest)[2] = SHORTSWAP32 ((video_font_draw_table16 [bits >> 2 & 3] & eorx) ^ bgx);
468 				((u32 *) dest)[3] = SHORTSWAP32 ((video_font_draw_table16 [bits & 3] & eorx) ^ bgx);
469 			}
470 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
471 			s++;
472 		}
473 		break;
474 
475 	case GDF_32BIT_X888RGB:
476 		while (count--) {
477 			c = *s;
478 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
479 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
480 			     rows--;
481 			     dest += VIDEO_LINE_LEN) {
482 				u8 bits = *cdat++;
483 
484 				((u32 *) dest)[0] = SWAP32 ((video_font_draw_table32 [bits >> 4][0] & eorx) ^ bgx);
485 				((u32 *) dest)[1] = SWAP32 ((video_font_draw_table32 [bits >> 4][1] & eorx) ^ bgx);
486 				((u32 *) dest)[2] = SWAP32 ((video_font_draw_table32 [bits >> 4][2] & eorx) ^ bgx);
487 				((u32 *) dest)[3] = SWAP32 ((video_font_draw_table32 [bits >> 4][3] & eorx) ^ bgx);
488 				((u32 *) dest)[4] = SWAP32 ((video_font_draw_table32 [bits & 15][0] & eorx) ^ bgx);
489 				((u32 *) dest)[5] = SWAP32 ((video_font_draw_table32 [bits & 15][1] & eorx) ^ bgx);
490 				((u32 *) dest)[6] = SWAP32 ((video_font_draw_table32 [bits & 15][2] & eorx) ^ bgx);
491 				((u32 *) dest)[7] = SWAP32 ((video_font_draw_table32 [bits & 15][3] & eorx) ^ bgx);
492 			}
493 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
494 			s++;
495 		}
496 		break;
497 
498 	case GDF_24BIT_888RGB:
499 		while (count--) {
500 			c = *s;
501 			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
502 			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
503 			     rows--;
504 			     dest += VIDEO_LINE_LEN) {
505 				u8 bits = *cdat++;
506 
507 				((u32 *) dest)[0] = (video_font_draw_table24[bits >> 4][0] & eorx) ^ bgx;
508 				((u32 *) dest)[1] = (video_font_draw_table24[bits >> 4][1] & eorx) ^ bgx;
509 				((u32 *) dest)[2] = (video_font_draw_table24[bits >> 4][2] & eorx) ^ bgx;
510 				((u32 *) dest)[3] = (video_font_draw_table24[bits & 15][0] & eorx) ^ bgx;
511 				((u32 *) dest)[4] = (video_font_draw_table24[bits & 15][1] & eorx) ^ bgx;
512 				((u32 *) dest)[5] = (video_font_draw_table24[bits & 15][2] & eorx) ^ bgx;
513 			}
514 			dest0 += VIDEO_FONT_WIDTH * VIDEO_PIXEL_SIZE;
515 			s++;
516 		}
517 		break;
518 	}
519 }
520 
521 /*****************************************************************************/
522 
523 static inline void video_drawstring (int xx, int yy, unsigned char *s)
524 {
525 	video_drawchars (xx, yy, s, strlen ((char *)s));
526 }
527 
528 /*****************************************************************************/
529 
530 static void video_putchar (int xx, int yy, unsigned char c)
531 {
532 	video_drawchars (xx, yy + video_logo_height, &c, 1);
533 }
534 
535 /*****************************************************************************/
536 #if defined(CONFIG_CONSOLE_CURSOR) || defined(CONFIG_VIDEO_SW_CURSOR)
537 static void video_set_cursor (void)
538 {
539 	/* swap drawing colors */
540 	eorx = fgx;
541 	fgx = bgx;
542 	bgx = eorx;
543 	eorx = fgx ^ bgx;
544 	/* draw cursor */
545 	video_putchar (console_col * VIDEO_FONT_WIDTH,
546 		       console_row * VIDEO_FONT_HEIGHT,
547 		       ' ');
548 	/* restore drawing colors */
549 	eorx = fgx;
550 	fgx = bgx;
551 	bgx = eorx;
552 	eorx = fgx ^ bgx;
553 }
554 #endif
555 /*****************************************************************************/
556 #ifdef CONFIG_CONSOLE_CURSOR
557 void console_cursor (int state)
558 {
559 	static int last_state = 0;
560 
561 #ifdef CONFIG_CONSOLE_TIME
562 	struct rtc_time tm;
563 	char info[16];
564 
565 	/* time update only if cursor is on (faster scroll) */
566 	if (state) {
567 		rtc_get (&tm);
568 
569 		sprintf (info, " %02d:%02d:%02d ", tm.tm_hour, tm.tm_min,
570 			 tm.tm_sec);
571 		video_drawstring (VIDEO_VISIBLE_COLS - 10 * VIDEO_FONT_WIDTH,
572 				  VIDEO_INFO_Y, (uchar *)info);
573 
574 		sprintf (info, "%02d.%02d.%04d", tm.tm_mday, tm.tm_mon,
575 			 tm.tm_year);
576 		video_drawstring (VIDEO_VISIBLE_COLS - 10 * VIDEO_FONT_WIDTH,
577 				  VIDEO_INFO_Y + 1 * VIDEO_FONT_HEIGHT, (uchar *)info);
578 	}
579 #endif
580 
581 	if (state && (last_state != state)) {
582 		video_set_cursor ();
583 	}
584 
585 	if (!state && (last_state != state)) {
586 		/* clear cursor */
587 		video_putchar (console_col * VIDEO_FONT_WIDTH,
588 			       console_row * VIDEO_FONT_HEIGHT,
589 			       ' ');
590 	}
591 
592 	last_state = state;
593 }
594 #endif
595 
596 /*****************************************************************************/
597 
598 #ifndef VIDEO_HW_RECTFILL
599 static void memsetl (int *p, int c, int v)
600 {
601 	while (c--)
602 		*(p++) = v;
603 }
604 #endif
605 
606 /*****************************************************************************/
607 
608 #ifndef VIDEO_HW_BITBLT
609 static void memcpyl (int *d, int *s, int c)
610 {
611 	while (c--)
612 		*(d++) = *(s++);
613 }
614 #endif
615 
616 /*****************************************************************************/
617 
618 static void console_scrollup (void)
619 {
620 	/* copy up rows ignoring the first one */
621 
622 #ifdef VIDEO_HW_BITBLT
623 	video_hw_bitblt (VIDEO_PIXEL_SIZE,	/* bytes per pixel */
624 			 0,	/* source pos x */
625 			 video_logo_height + VIDEO_FONT_HEIGHT, /* source pos y */
626 			 0,	/* dest pos x */
627 			 video_logo_height,	/* dest pos y */
628 			 VIDEO_VISIBLE_COLS,	/* frame width */
629 			 VIDEO_VISIBLE_ROWS - video_logo_height - VIDEO_FONT_HEIGHT	/* frame height */
630 		);
631 #else
632 	memcpyl (CONSOLE_ROW_FIRST, CONSOLE_ROW_SECOND,
633 		 CONSOLE_SCROLL_SIZE >> 2);
634 #endif
635 
636 	/* clear the last one */
637 #ifdef VIDEO_HW_RECTFILL
638 	video_hw_rectfill (VIDEO_PIXEL_SIZE,	/* bytes per pixel */
639 			   0,	/* dest pos x */
640 			   VIDEO_VISIBLE_ROWS - VIDEO_FONT_HEIGHT,	/* dest pos y */
641 			   VIDEO_VISIBLE_COLS,	/* frame width */
642 			   VIDEO_FONT_HEIGHT,	/* frame height */
643 			   CONSOLE_BG_COL	/* fill color */
644 		);
645 #else
646 	memsetl (CONSOLE_ROW_LAST, CONSOLE_ROW_SIZE >> 2, CONSOLE_BG_COL);
647 #endif
648 }
649 
650 /*****************************************************************************/
651 
652 static void console_back (void)
653 {
654 	CURSOR_OFF;
655 	console_col--;
656 
657 	if (console_col < 0) {
658 		console_col = CONSOLE_COLS - 1;
659 		console_row--;
660 		if (console_row < 0)
661 			console_row = 0;
662 	}
663 	video_putchar (console_col * VIDEO_FONT_WIDTH,
664 		       console_row * VIDEO_FONT_HEIGHT,
665 		       ' ');
666 }
667 
668 /*****************************************************************************/
669 
670 static void console_newline (void)
671 {
672 	/* Check if last character in the line was just drawn. If so, cursor was
673 	   overwriten and need not to be cleared. Cursor clearing without this
674 	   check causes overwriting the 1st character of the line if line lenght
675 	   is >= CONSOLE_COLS
676 	 */
677 	if (console_col < CONSOLE_COLS)
678 		CURSOR_OFF;
679 	console_row++;
680 	console_col = 0;
681 
682 	/* Check if we need to scroll the terminal */
683 	if (console_row >= CONSOLE_ROWS) {
684 		/* Scroll everything up */
685 		console_scrollup ();
686 
687 		/* Decrement row number */
688 		console_row--;
689 	}
690 }
691 
692 static void console_cr (void)
693 {
694 	CURSOR_OFF;
695 	console_col = 0;
696 }
697 
698 /*****************************************************************************/
699 
700 void video_putc (const char c)
701 {
702 	static int nl = 1;
703 
704 	switch (c) {
705 	case 13:		/* back to first column */
706 		console_cr ();
707 		break;
708 
709 	case '\n':		/* next line */
710 		if (console_col || (!console_col && nl))
711 			console_newline ();
712 		nl = 1;
713 		break;
714 
715 	case 9:		/* tab 8 */
716 		CURSOR_OFF;
717 		console_col |= 0x0008;
718 		console_col &= ~0x0007;
719 
720 		if (console_col >= CONSOLE_COLS)
721 			console_newline ();
722 		break;
723 
724 	case 8:		/* backspace */
725 		console_back ();
726 		break;
727 
728 	default:		/* draw the char */
729 		video_putchar (console_col * VIDEO_FONT_WIDTH,
730 			       console_row * VIDEO_FONT_HEIGHT,
731 			       c);
732 		console_col++;
733 
734 		/* check for newline */
735 		if (console_col >= CONSOLE_COLS) {
736 			console_newline ();
737 			nl = 0;
738 		}
739 	}
740 	CURSOR_SET;
741 }
742 
743 
744 /*****************************************************************************/
745 
746 void video_puts (const char *s)
747 {
748 	int count = strlen (s);
749 
750 	while (count--)
751 		video_putc (*s++);
752 }
753 
754 /*****************************************************************************/
755 
756 /*
757  * Do not enforce drivers (or board code) to provide empty
758  * video_set_lut() if they do not support 8 bpp format.
759  * Implement weak default function instead.
760  */
761 void __video_set_lut (unsigned int index, unsigned char r,
762 		      unsigned char g, unsigned char b)
763 {
764 }
765 void video_set_lut (unsigned int, unsigned char, unsigned char, unsigned char)
766 			__attribute__((weak, alias("__video_set_lut")));
767 
768 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
769 
770 #define FILL_8BIT_332RGB(r,g,b)	{			\
771 	*fb = ((r>>5)<<5) | ((g>>5)<<2) | (b>>6);	\
772 	fb ++;						\
773 }
774 
775 #define FILL_15BIT_555RGB(r,g,b) {			\
776 	*(unsigned short *)fb = SWAP16((unsigned short)(((r>>3)<<10) | ((g>>3)<<5) | (b>>3))); \
777 	fb += 2;					\
778 }
779 
780 #define FILL_16BIT_565RGB(r,g,b) {			\
781 	*(unsigned short *)fb = SWAP16((unsigned short)((((r)>>3)<<11) | (((g)>>2)<<5) | ((b)>>3))); \
782 	fb += 2;					\
783 }
784 
785 #define FILL_32BIT_X888RGB(r,g,b) {			\
786 	*(unsigned long *)fb = SWAP32((unsigned long)(((r<<16) | (g<<8) | b))); \
787 	fb += 4;					\
788 }
789 
790 #ifdef VIDEO_FB_LITTLE_ENDIAN
791 #define FILL_24BIT_888RGB(r,g,b) {			\
792 	fb[0] = b;					\
793 	fb[1] = g;					\
794 	fb[2] = r;					\
795 	fb += 3;					\
796 }
797 #else
798 #define FILL_24BIT_888RGB(r,g,b) {			\
799 	fb[0] = r;					\
800 	fb[1] = g;					\
801 	fb[2] = b;					\
802 	fb += 3;					\
803 }
804 #endif
805 
806 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
807 static void inline fill_555rgb_pswap(uchar *fb, int x,
808 				     u8 r, u8 g, u8 b)
809 {
810 	ushort *dst = (ushort *)fb;
811 	ushort color = (ushort)(((r >> 3) << 10) |
812 				((g >> 3) << 5) |
813 				(b >> 3));
814 	if (x & 1)
815 		*(--dst) = color;
816 	else
817 		*(++dst) = color;
818 }
819 #endif
820 
821 /*
822  * RLE8 bitmap support
823  */
824 
825 #ifdef CONFIG_VIDEO_BMP_RLE8
826 /* Pre-calculated color table entry */
827 struct palette {
828 	union {
829 		unsigned short	w;	/* word */
830 		unsigned int	dw;	/* double word */
831 	} ce; /* color entry */
832 };
833 
834 /*
835  * Helper to draw encoded/unencoded run.
836  */
837 static void draw_bitmap (uchar **fb, uchar *bm, struct palette *p,
838 			 int cnt, int enc)
839 {
840 	ulong addr = (ulong)*fb;
841 	int *off;
842 	int enc_off = 1;
843 	int i;
844 
845 	/*
846 	 * Setup offset of the color index in the bitmap.
847 	 * Color index of encoded run is at offset 1.
848 	 */
849 	off = enc ? &enc_off : &i;
850 
851 	switch (VIDEO_DATA_FORMAT) {
852 	case GDF__8BIT_INDEX:
853 		for (i = 0; i < cnt; i++)
854 			*(unsigned char *)addr++ = bm[*off];
855 		break;
856 	case GDF_15BIT_555RGB:
857 	case GDF_16BIT_565RGB:
858 		/* differences handled while pre-calculating palette */
859 		for (i = 0; i < cnt; i++) {
860 			*(unsigned short *)addr = p[bm[*off]].ce.w;
861 			addr += 2;
862 		}
863 		break;
864 	case GDF_32BIT_X888RGB:
865 		for (i = 0; i < cnt; i++) {
866 			*(unsigned long *)addr = p[bm[*off]].ce.dw;
867 			addr += 4;
868 		}
869 		break;
870 	}
871 	*fb = (uchar *)addr; /* return modified address */
872 }
873 
874 static int display_rle8_bitmap (bmp_image_t *img, int xoff, int yoff,
875 				int width, int height)
876 {
877 	unsigned char *bm;
878 	unsigned char *fbp;
879 	unsigned int cnt, runlen;
880 	int decode = 1;
881 	int x, y, bpp, i, ncolors;
882 	struct palette p[256];
883 	bmp_color_table_entry_t cte;
884 	int green_shift, red_off;
885 
886 	x = 0;
887 	y = __le32_to_cpu(img->header.height) - 1;
888 	ncolors = __le32_to_cpu(img->header.colors_used);
889 	bpp = VIDEO_PIXEL_SIZE;
890 	fbp = (unsigned char *)((unsigned int)video_fb_address +
891 				(((y + yoff) * VIDEO_COLS) + xoff) * bpp);
892 
893 	bm = (uchar *)img + __le32_to_cpu(img->header.data_offset);
894 
895 	/* pre-calculate and setup palette */
896 	switch (VIDEO_DATA_FORMAT) {
897 	case GDF__8BIT_INDEX:
898 		for (i = 0; i < ncolors; i++) {
899 			cte = img->color_table[i];
900 			video_set_lut (i, cte.red, cte.green, cte.blue);
901 		}
902 		break;
903 	case GDF_15BIT_555RGB:
904 	case GDF_16BIT_565RGB:
905 		if (VIDEO_DATA_FORMAT == GDF_15BIT_555RGB) {
906 			green_shift = 3;
907 			red_off = 10;
908 		} else {
909 			green_shift = 2;
910 			red_off = 11;
911 		}
912 		for (i = 0; i < ncolors; i++) {
913 			cte = img->color_table[i];
914 			p[i].ce.w = SWAP16((unsigned short)
915 					   (((cte.red >> 3) << red_off) |
916 					    ((cte.green >> green_shift) << 5) |
917 					    cte.blue >> 3));
918 		}
919 		break;
920 	case GDF_32BIT_X888RGB:
921 		for (i = 0; i < ncolors; i++) {
922 			cte = img->color_table[i];
923 			p[i].ce.dw = SWAP32((cte.red << 16) | (cte.green << 8) |
924 					     cte.blue);
925 		}
926 		break;
927 	default:
928 		printf("RLE Bitmap unsupported in video mode 0x%x\n",
929 			VIDEO_DATA_FORMAT);
930 		return -1;
931 	}
932 
933 	while (decode) {
934 		switch (bm[0]) {
935 		case 0:
936 			switch (bm[1]) {
937 			case 0:
938 				/* scan line end marker */
939 				bm += 2;
940 				x = 0;
941 				y--;
942 				fbp = (unsigned char *)
943 					((unsigned int)video_fb_address +
944 					 (((y + yoff) * VIDEO_COLS) +
945 					  xoff) * bpp);
946 				continue;
947 			case 1:
948 				/* end of bitmap data marker */
949 				decode = 0;
950 				break;
951 			case 2:
952 				/* run offset marker */
953 				x += bm[2];
954 				y -= bm[3];
955 				fbp = (unsigned char *)
956 					((unsigned int)video_fb_address +
957 					 (((y + yoff) * VIDEO_COLS) +
958 					  x + xoff) * bpp);
959 				bm += 4;
960 				break;
961 			default:
962 				/* unencoded run */
963 				cnt = bm[1];
964 				runlen = cnt;
965 				bm += 2;
966 				if (y < height) {
967 					if (x >= width) {
968 						x += runlen;
969 						goto next_run;
970 					}
971 					if (x + runlen > width)
972 						cnt = width - x;
973 
974 					draw_bitmap (&fbp, bm, p, cnt, 0);
975 					x += runlen;
976 				}
977 next_run:
978 				bm += runlen;
979 				if (runlen & 1)
980 					bm++; /* 0 padding if length is odd */
981 			}
982 			break;
983 		default:
984 			/* encoded run */
985 			if (y < height) { /* only draw into visible area */
986 				cnt = bm[0];
987 				runlen = cnt;
988 				if (x >= width) {
989 					x += runlen;
990 					bm += 2;
991 					continue;
992 				}
993 				if (x + runlen > width)
994 					cnt = width - x;
995 
996 				draw_bitmap (&fbp, bm, p, cnt, 1);
997 				x += runlen;
998 			}
999 			bm += 2;
1000 			break;
1001 		}
1002 	}
1003 	return 0;
1004 }
1005 #endif
1006 
1007 /*
1008  * Display the BMP file located at address bmp_image.
1009  */
1010 int video_display_bitmap (ulong bmp_image, int x, int y)
1011 {
1012 	ushort xcount, ycount;
1013 	uchar *fb;
1014 	bmp_image_t *bmp = (bmp_image_t *) bmp_image;
1015 	uchar *bmap;
1016 	ushort padded_line;
1017 	unsigned long width, height, bpp;
1018 	unsigned colors;
1019 	unsigned long compression;
1020 	bmp_color_table_entry_t cte;
1021 #ifdef CONFIG_VIDEO_BMP_GZIP
1022 	unsigned char *dst = NULL;
1023 	ulong len;
1024 #endif
1025 
1026 	WATCHDOG_RESET ();
1027 
1028 	if (!((bmp->header.signature[0] == 'B') &&
1029 	      (bmp->header.signature[1] == 'M'))) {
1030 
1031 #ifdef CONFIG_VIDEO_BMP_GZIP
1032 		/*
1033 		 * Could be a gzipped bmp image, try to decrompress...
1034 		 */
1035 		len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
1036 		dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE);
1037 		if (dst == NULL) {
1038 			printf("Error: malloc in gunzip failed!\n");
1039 			return(1);
1040 		}
1041 		if (gunzip(dst, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, (uchar *)bmp_image, &len) != 0) {
1042 			printf ("Error: no valid bmp or bmp.gz image at %lx\n", bmp_image);
1043 			free(dst);
1044 			return 1;
1045 		}
1046 		if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE) {
1047 			printf("Image could be truncated (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
1048 		}
1049 
1050 		/*
1051 		 * Set addr to decompressed image
1052 		 */
1053 		bmp = (bmp_image_t *)dst;
1054 
1055 		if (!((bmp->header.signature[0] == 'B') &&
1056 		      (bmp->header.signature[1] == 'M'))) {
1057 			printf ("Error: no valid bmp.gz image at %lx\n", bmp_image);
1058 			free(dst);
1059 			return 1;
1060 		}
1061 #else
1062 		printf ("Error: no valid bmp image at %lx\n", bmp_image);
1063 		return 1;
1064 #endif /* CONFIG_VIDEO_BMP_GZIP */
1065 	}
1066 
1067 	width = le32_to_cpu (bmp->header.width);
1068 	height = le32_to_cpu (bmp->header.height);
1069 	bpp = le16_to_cpu (bmp->header.bit_count);
1070 	colors = le32_to_cpu (bmp->header.colors_used);
1071 	compression = le32_to_cpu (bmp->header.compression);
1072 
1073 	debug ("Display-bmp: %d x %d  with %d colors\n",
1074 	       width, height, colors);
1075 
1076 	if (compression != BMP_BI_RGB
1077 #ifdef CONFIG_VIDEO_BMP_RLE8
1078 	    && compression != BMP_BI_RLE8
1079 #endif
1080 	   ) {
1081 		printf ("Error: compression type %ld not supported\n",
1082 			compression);
1083 #ifdef CONFIG_VIDEO_BMP_GZIP
1084 		if (dst)
1085 			free(dst);
1086 #endif
1087 		return 1;
1088 	}
1089 
1090 	padded_line = (((width * bpp + 7) / 8) + 3) & ~0x3;
1091 
1092 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
1093 	if (x == BMP_ALIGN_CENTER)
1094 		x = max(0, (VIDEO_VISIBLE_COLS - width) / 2);
1095 	else if (x < 0)
1096 		x = max(0, VIDEO_VISIBLE_COLS - width + x + 1);
1097 
1098 	if (y == BMP_ALIGN_CENTER)
1099 		y = max(0, (VIDEO_VISIBLE_ROWS - height) / 2);
1100 	else if (y < 0)
1101 		y = max(0, VIDEO_VISIBLE_ROWS - height + y + 1);
1102 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
1103 
1104 	if ((x + width) > VIDEO_VISIBLE_COLS)
1105 		width = VIDEO_VISIBLE_COLS - x;
1106 	if ((y + height) > VIDEO_VISIBLE_ROWS)
1107 		height = VIDEO_VISIBLE_ROWS - y;
1108 
1109 	bmap = (uchar *) bmp + le32_to_cpu (bmp->header.data_offset);
1110 	fb = (uchar *) (video_fb_address +
1111 			((y + height - 1) * VIDEO_COLS * VIDEO_PIXEL_SIZE) +
1112 			x * VIDEO_PIXEL_SIZE);
1113 
1114 #ifdef CONFIG_VIDEO_BMP_RLE8
1115 	if (compression == BMP_BI_RLE8) {
1116 		return display_rle8_bitmap(bmp,
1117 					   x, y, width, height);
1118 	}
1119 #endif
1120 
1121 	/* We handle only 8bpp or 24 bpp bitmap */
1122 	switch (le16_to_cpu (bmp->header.bit_count)) {
1123 	case 8:
1124 		padded_line -= width;
1125 		if (VIDEO_DATA_FORMAT == GDF__8BIT_INDEX) {
1126 			/* Copy colormap */
1127 			for (xcount = 0; xcount < colors; ++xcount) {
1128 				cte = bmp->color_table[xcount];
1129 				video_set_lut (xcount, cte.red, cte.green, cte.blue);
1130 			}
1131 		}
1132 		ycount = height;
1133 		switch (VIDEO_DATA_FORMAT) {
1134 		case GDF__8BIT_INDEX:
1135 			while (ycount--) {
1136 				WATCHDOG_RESET ();
1137 				xcount = width;
1138 				while (xcount--) {
1139 					*fb++ = *bmap++;
1140 				}
1141 				bmap += padded_line;
1142 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1143 			}
1144 			break;
1145 		case GDF__8BIT_332RGB:
1146 			while (ycount--) {
1147 				WATCHDOG_RESET ();
1148 				xcount = width;
1149 				while (xcount--) {
1150 					cte = bmp->color_table[*bmap++];
1151 					FILL_8BIT_332RGB (cte.red, cte.green, cte.blue);
1152 				}
1153 				bmap += padded_line;
1154 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1155 			}
1156 			break;
1157 		case GDF_15BIT_555RGB:
1158 			while (ycount--) {
1159 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1160 				int xpos = x;
1161 #endif
1162 				WATCHDOG_RESET ();
1163 				xcount = width;
1164 				while (xcount--) {
1165 					cte = bmp->color_table[*bmap++];
1166 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1167 					fill_555rgb_pswap (fb, xpos++, cte.red,
1168 							   cte.green, cte.blue);
1169 					fb += 2;
1170 #else
1171 					FILL_15BIT_555RGB (cte.red, cte.green, cte.blue);
1172 #endif
1173 				}
1174 				bmap += padded_line;
1175 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1176 			}
1177 			break;
1178 		case GDF_16BIT_565RGB:
1179 			while (ycount--) {
1180 				WATCHDOG_RESET ();
1181 				xcount = width;
1182 				while (xcount--) {
1183 					cte = bmp->color_table[*bmap++];
1184 					FILL_16BIT_565RGB (cte.red, cte.green, cte.blue);
1185 				}
1186 				bmap += padded_line;
1187 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1188 			}
1189 			break;
1190 		case GDF_32BIT_X888RGB:
1191 			while (ycount--) {
1192 				WATCHDOG_RESET ();
1193 				xcount = width;
1194 				while (xcount--) {
1195 					cte = bmp->color_table[*bmap++];
1196 					FILL_32BIT_X888RGB (cte.red, cte.green, cte.blue);
1197 				}
1198 				bmap += padded_line;
1199 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1200 			}
1201 			break;
1202 		case GDF_24BIT_888RGB:
1203 			while (ycount--) {
1204 				WATCHDOG_RESET ();
1205 				xcount = width;
1206 				while (xcount--) {
1207 					cte = bmp->color_table[*bmap++];
1208 					FILL_24BIT_888RGB (cte.red, cte.green, cte.blue);
1209 				}
1210 				bmap += padded_line;
1211 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1212 			}
1213 			break;
1214 		}
1215 		break;
1216 	case 24:
1217 		padded_line -= 3 * width;
1218 		ycount = height;
1219 		switch (VIDEO_DATA_FORMAT) {
1220 		case GDF__8BIT_332RGB:
1221 			while (ycount--) {
1222 				WATCHDOG_RESET ();
1223 				xcount = width;
1224 				while (xcount--) {
1225 					FILL_8BIT_332RGB (bmap[2], bmap[1], bmap[0]);
1226 					bmap += 3;
1227 				}
1228 				bmap += padded_line;
1229 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1230 			}
1231 			break;
1232 		case GDF_15BIT_555RGB:
1233 			while (ycount--) {
1234 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1235 				int xpos = x;
1236 #endif
1237 				WATCHDOG_RESET ();
1238 				xcount = width;
1239 				while (xcount--) {
1240 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1241 					fill_555rgb_pswap (fb, xpos++, bmap[2],
1242 							   bmap[1], bmap[0]);
1243 					fb += 2;
1244 #else
1245 					FILL_15BIT_555RGB (bmap[2], bmap[1], bmap[0]);
1246 #endif
1247 					bmap += 3;
1248 				}
1249 				bmap += padded_line;
1250 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1251 			}
1252 			break;
1253 		case GDF_16BIT_565RGB:
1254 			while (ycount--) {
1255 				WATCHDOG_RESET ();
1256 				xcount = width;
1257 				while (xcount--) {
1258 					FILL_16BIT_565RGB (bmap[2], bmap[1], bmap[0]);
1259 					bmap += 3;
1260 				}
1261 				bmap += padded_line;
1262 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1263 			}
1264 			break;
1265 		case GDF_32BIT_X888RGB:
1266 			while (ycount--) {
1267 				WATCHDOG_RESET ();
1268 				xcount = width;
1269 				while (xcount--) {
1270 					FILL_32BIT_X888RGB (bmap[2], bmap[1], bmap[0]);
1271 					bmap += 3;
1272 				}
1273 				bmap += padded_line;
1274 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1275 			}
1276 			break;
1277 		case GDF_24BIT_888RGB:
1278 			while (ycount--) {
1279 				WATCHDOG_RESET ();
1280 				xcount = width;
1281 				while (xcount--) {
1282 					FILL_24BIT_888RGB (bmap[2], bmap[1], bmap[0]);
1283 					bmap += 3;
1284 				}
1285 				bmap += padded_line;
1286 				fb -= (VIDEO_VISIBLE_COLS + width) * VIDEO_PIXEL_SIZE;
1287 			}
1288 			break;
1289 		default:
1290 			printf ("Error: 24 bits/pixel bitmap incompatible with current video mode\n");
1291 			break;
1292 		}
1293 		break;
1294 	default:
1295 		printf ("Error: %d bit/pixel bitmaps not supported by U-Boot\n",
1296 			le16_to_cpu (bmp->header.bit_count));
1297 		break;
1298 	}
1299 
1300 #ifdef CONFIG_VIDEO_BMP_GZIP
1301 	if (dst) {
1302 		free(dst);
1303 	}
1304 #endif
1305 
1306 	return (0);
1307 }
1308 #endif
1309 
1310 /*****************************************************************************/
1311 
1312 #ifdef CONFIG_VIDEO_LOGO
1313 void logo_plot (void *screen, int width, int x, int y)
1314 {
1315 
1316 	int xcount, i;
1317 	int skip   = (width - VIDEO_LOGO_WIDTH) * VIDEO_PIXEL_SIZE;
1318 	int ycount = video_logo_height;
1319 	unsigned char r, g, b, *logo_red, *logo_blue, *logo_green;
1320 	unsigned char *source;
1321 	unsigned char *dest = (unsigned char *)screen +
1322 			      ((y * width * VIDEO_PIXEL_SIZE) +
1323 			       x * VIDEO_PIXEL_SIZE);
1324 
1325 #ifdef CONFIG_VIDEO_BMP_LOGO
1326 	source = bmp_logo_bitmap;
1327 
1328 	/* Allocate temporary space for computing colormap */
1329 	logo_red = malloc (BMP_LOGO_COLORS);
1330 	logo_green = malloc (BMP_LOGO_COLORS);
1331 	logo_blue = malloc (BMP_LOGO_COLORS);
1332 	/* Compute color map */
1333 	for (i = 0; i < VIDEO_LOGO_COLORS; i++) {
1334 		logo_red[i] = (bmp_logo_palette[i] & 0x0f00) >> 4;
1335 		logo_green[i] = (bmp_logo_palette[i] & 0x00f0);
1336 		logo_blue[i] = (bmp_logo_palette[i] & 0x000f) << 4;
1337 	}
1338 #else
1339 	source = linux_logo;
1340 	logo_red = linux_logo_red;
1341 	logo_green = linux_logo_green;
1342 	logo_blue = linux_logo_blue;
1343 #endif
1344 
1345 	if (VIDEO_DATA_FORMAT == GDF__8BIT_INDEX) {
1346 		for (i = 0; i < VIDEO_LOGO_COLORS; i++) {
1347 			video_set_lut (i + VIDEO_LOGO_LUT_OFFSET,
1348 				       logo_red[i], logo_green[i], logo_blue[i]);
1349 		}
1350 	}
1351 
1352 	while (ycount--) {
1353 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1354 		int xpos = x;
1355 #endif
1356 		xcount = VIDEO_LOGO_WIDTH;
1357 		while (xcount--) {
1358 			r = logo_red[*source - VIDEO_LOGO_LUT_OFFSET];
1359 			g = logo_green[*source - VIDEO_LOGO_LUT_OFFSET];
1360 			b = logo_blue[*source - VIDEO_LOGO_LUT_OFFSET];
1361 
1362 			switch (VIDEO_DATA_FORMAT) {
1363 			case GDF__8BIT_INDEX:
1364 				*dest = *source;
1365 				break;
1366 			case GDF__8BIT_332RGB:
1367 				*dest = ((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6);
1368 				break;
1369 			case GDF_15BIT_555RGB:
1370 #if defined(VIDEO_FB_16BPP_PIXEL_SWAP)
1371 				fill_555rgb_pswap (dest, xpos++, r, g, b);
1372 #else
1373 				*(unsigned short *) dest =
1374 					SWAP16 ((unsigned short) (((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3)));
1375 #endif
1376 				break;
1377 			case GDF_16BIT_565RGB:
1378 				*(unsigned short *) dest =
1379 					SWAP16 ((unsigned short) (((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)));
1380 				break;
1381 			case GDF_32BIT_X888RGB:
1382 				*(unsigned long *) dest =
1383 					SWAP32 ((unsigned long) ((r << 16) | (g << 8) | b));
1384 				break;
1385 			case GDF_24BIT_888RGB:
1386 #ifdef VIDEO_FB_LITTLE_ENDIAN
1387 				dest[0] = b;
1388 				dest[1] = g;
1389 				dest[2] = r;
1390 #else
1391 				dest[0] = r;
1392 				dest[1] = g;
1393 				dest[2] = b;
1394 #endif
1395 				break;
1396 			}
1397 			source++;
1398 			dest += VIDEO_PIXEL_SIZE;
1399 		}
1400 		dest += skip;
1401 	}
1402 #ifdef CONFIG_VIDEO_BMP_LOGO
1403 	free (logo_red);
1404 	free (logo_green);
1405 	free (logo_blue);
1406 #endif
1407 }
1408 
1409 /*****************************************************************************/
1410 
1411 static void *video_logo (void)
1412 {
1413 	char info[128];
1414 	extern char version_string;
1415 	int space, len, y_off = 0;
1416 
1417 #ifdef CONFIG_SPLASH_SCREEN
1418 	char *s;
1419 	ulong addr;
1420 
1421 	if ((s = getenv ("splashimage")) != NULL) {
1422 		int x = 0, y = 0;
1423 
1424 		addr = simple_strtoul (s, NULL, 16);
1425 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
1426 		if ((s = getenv ("splashpos")) != NULL) {
1427 			if (s[0] == 'm')
1428 				x = BMP_ALIGN_CENTER;
1429 			else
1430 				x = simple_strtol (s, NULL, 0);
1431 
1432 			if ((s = strchr (s + 1, ',')) != NULL) {
1433 				if (s[1] == 'm')
1434 					y = BMP_ALIGN_CENTER;
1435 				else
1436 					y = simple_strtol (s + 1, NULL, 0);
1437 			}
1438 		}
1439 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
1440 
1441 		if (video_display_bitmap (addr, x, y) == 0) {
1442 			video_logo_height = 0;
1443 			return ((void *) (video_fb_address));
1444 		}
1445 	}
1446 #endif /* CONFIG_SPLASH_SCREEN */
1447 
1448 	logo_plot (video_fb_address, VIDEO_COLS, 0, 0);
1449 
1450 	sprintf (info, " %s", &version_string);
1451 
1452 	space = (VIDEO_LINE_LEN / 2 - VIDEO_INFO_X) / VIDEO_FONT_WIDTH;
1453 	len = strlen(info);
1454 
1455 	if (len > space) {
1456 		video_drawchars (VIDEO_INFO_X, VIDEO_INFO_Y,
1457 				 (uchar *)info, space);
1458 		video_drawchars (VIDEO_INFO_X + VIDEO_FONT_WIDTH,
1459 				 VIDEO_INFO_Y + VIDEO_FONT_HEIGHT,
1460 				 (uchar *)info + space, len - space);
1461 		y_off = 1;
1462 	} else
1463 		video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y, (uchar *)info);
1464 
1465 #ifdef CONFIG_CONSOLE_EXTRA_INFO
1466 	{
1467 		int i, n = ((video_logo_height - VIDEO_FONT_HEIGHT) / VIDEO_FONT_HEIGHT);
1468 
1469 		for (i = 1; i < n; i++) {
1470 			video_get_info_str (i, info);
1471 			if (!*info)
1472 				continue;
1473 
1474 			len = strlen(info);
1475 			if (len > space) {
1476 				video_drawchars (VIDEO_INFO_X,
1477 						 VIDEO_INFO_Y +
1478 						 (i + y_off) * VIDEO_FONT_HEIGHT,
1479 						 (uchar *)info, space);
1480 				y_off++;
1481 				video_drawchars (VIDEO_INFO_X + VIDEO_FONT_WIDTH,
1482 						 VIDEO_INFO_Y +
1483 						 (i + y_off) * VIDEO_FONT_HEIGHT,
1484 						 (uchar *)info + space,
1485 						 len - space);
1486 			} else {
1487 				video_drawstring (VIDEO_INFO_X,
1488 						  VIDEO_INFO_Y +
1489 						  (i + y_off) * VIDEO_FONT_HEIGHT,
1490 						  (uchar *)info);
1491 			}
1492 		}
1493 	}
1494 #endif
1495 
1496 	return (video_fb_address + video_logo_height * VIDEO_LINE_LEN);
1497 }
1498 #endif
1499 
1500 
1501 /*****************************************************************************/
1502 
1503 static int video_init (void)
1504 {
1505 	unsigned char color8;
1506 
1507 	if ((pGD = video_hw_init ()) == NULL)
1508 		return -1;
1509 
1510 	video_fb_address = (void *) VIDEO_FB_ADRS;
1511 #ifdef CONFIG_VIDEO_HW_CURSOR
1512 	video_init_hw_cursor (VIDEO_FONT_WIDTH, VIDEO_FONT_HEIGHT);
1513 #endif
1514 
1515 	/* Init drawing pats */
1516 	switch (VIDEO_DATA_FORMAT) {
1517 	case GDF__8BIT_INDEX:
1518 		video_set_lut (0x01, CONSOLE_FG_COL, CONSOLE_FG_COL, CONSOLE_FG_COL);
1519 		video_set_lut (0x00, CONSOLE_BG_COL, CONSOLE_BG_COL, CONSOLE_BG_COL);
1520 		fgx = 0x01010101;
1521 		bgx = 0x00000000;
1522 		break;
1523 	case GDF__8BIT_332RGB:
1524 		color8 = ((CONSOLE_FG_COL & 0xe0) |
1525 			  ((CONSOLE_FG_COL >> 3) & 0x1c) | CONSOLE_FG_COL >> 6);
1526 		fgx = (color8 << 24) | (color8 << 16) | (color8 << 8) | color8;
1527 		color8 = ((CONSOLE_BG_COL & 0xe0) |
1528 			  ((CONSOLE_BG_COL >> 3) & 0x1c) | CONSOLE_BG_COL >> 6);
1529 		bgx = (color8 << 24) | (color8 << 16) | (color8 << 8) | color8;
1530 		break;
1531 	case GDF_15BIT_555RGB:
1532 		fgx = (((CONSOLE_FG_COL >> 3) << 26) |
1533 		       ((CONSOLE_FG_COL >> 3) << 21) | ((CONSOLE_FG_COL >> 3) << 16) |
1534 		       ((CONSOLE_FG_COL >> 3) << 10) | ((CONSOLE_FG_COL >> 3) << 5) |
1535 		       (CONSOLE_FG_COL >> 3));
1536 		bgx = (((CONSOLE_BG_COL >> 3) << 26) |
1537 		       ((CONSOLE_BG_COL >> 3) << 21) | ((CONSOLE_BG_COL >> 3) << 16) |
1538 		       ((CONSOLE_BG_COL >> 3) << 10) | ((CONSOLE_BG_COL >> 3) << 5) |
1539 		       (CONSOLE_BG_COL >> 3));
1540 		break;
1541 	case GDF_16BIT_565RGB:
1542 		fgx = (((CONSOLE_FG_COL >> 3) << 27) |
1543 		       ((CONSOLE_FG_COL >> 2) << 21) | ((CONSOLE_FG_COL >> 3) << 16) |
1544 		       ((CONSOLE_FG_COL >> 3) << 11) | ((CONSOLE_FG_COL >> 2) << 5) |
1545 		       (CONSOLE_FG_COL >> 3));
1546 		bgx = (((CONSOLE_BG_COL >> 3) << 27) |
1547 		       ((CONSOLE_BG_COL >> 2) << 21) | ((CONSOLE_BG_COL >> 3) << 16) |
1548 		       ((CONSOLE_BG_COL >> 3) << 11) | ((CONSOLE_BG_COL >> 2) << 5) |
1549 		       (CONSOLE_BG_COL >> 3));
1550 		break;
1551 	case GDF_32BIT_X888RGB:
1552 		fgx = (CONSOLE_FG_COL << 16) | (CONSOLE_FG_COL << 8) | CONSOLE_FG_COL;
1553 		bgx = (CONSOLE_BG_COL << 16) | (CONSOLE_BG_COL << 8) | CONSOLE_BG_COL;
1554 		break;
1555 	case GDF_24BIT_888RGB:
1556 		fgx = (CONSOLE_FG_COL << 24) | (CONSOLE_FG_COL << 16) |
1557 			(CONSOLE_FG_COL << 8) | CONSOLE_FG_COL;
1558 		bgx = (CONSOLE_BG_COL << 24) | (CONSOLE_BG_COL << 16) |
1559 			(CONSOLE_BG_COL << 8) | CONSOLE_BG_COL;
1560 		break;
1561 	}
1562 	eorx = fgx ^ bgx;
1563 
1564 #ifdef CONFIG_VIDEO_LOGO
1565 	/* Plot the logo and get start point of console */
1566 	PRINTD ("Video: Drawing the logo ...\n");
1567 	video_console_address = video_logo ();
1568 #else
1569 	video_console_address = video_fb_address;
1570 #endif
1571 
1572 	/* Initialize the console */
1573 	console_col = 0;
1574 	console_row = 0;
1575 
1576 	return 0;
1577 }
1578 
1579 
1580 /*****************************************************************************/
1581 
1582 /*
1583  * Implement a weak default function for boards that optionally
1584  * need to skip the video initialization.
1585  */
1586 int __board_video_skip(void)
1587 {
1588 	/* As default, don't skip test */
1589 	return 0;
1590 }
1591 int board_video_skip(void) __attribute__((weak, alias("__board_video_skip")));
1592 
1593 int drv_video_init (void)
1594 {
1595 	int skip_dev_init;
1596 	struct stdio_dev console_dev;
1597 
1598 	/* Check if video initialization should be skipped */
1599 	if (board_video_skip())
1600 		return 0;
1601 
1602 	/* Init video chip - returns with framebuffer cleared */
1603 	skip_dev_init = (video_init () == -1);
1604 
1605 #if !defined(CONFIG_VGA_AS_SINGLE_DEVICE)
1606 	PRINTD ("KBD: Keyboard init ...\n");
1607 	skip_dev_init |= (VIDEO_KBD_INIT_FCT == -1);
1608 #endif
1609 
1610 	if (skip_dev_init)
1611 		return 0;
1612 
1613 	/* Init vga device */
1614 	memset (&console_dev, 0, sizeof (console_dev));
1615 	strcpy (console_dev.name, "vga");
1616 	console_dev.ext = DEV_EXT_VIDEO;	/* Video extensions */
1617 	console_dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_SYSTEM;
1618 	console_dev.putc = video_putc;	/* 'putc' function */
1619 	console_dev.puts = video_puts;	/* 'puts' function */
1620 	console_dev.tstc = NULL;	/* 'tstc' function */
1621 	console_dev.getc = NULL;	/* 'getc' function */
1622 
1623 #if !defined(CONFIG_VGA_AS_SINGLE_DEVICE)
1624 	/* Also init console device */
1625 	console_dev.flags |= DEV_FLAGS_INPUT;
1626 	console_dev.tstc = VIDEO_TSTC_FCT;	/* 'tstc' function */
1627 	console_dev.getc = VIDEO_GETC_FCT;	/* 'getc' function */
1628 #endif /* CONFIG_VGA_AS_SINGLE_DEVICE */
1629 
1630 	if (stdio_register (&console_dev) != 0)
1631 		return 0;
1632 
1633 	/* Return success */
1634 	return 1;
1635 }
1636