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