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