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