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