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