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