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