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