4 * (C) Copyright 2001-2002
5 * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
7 * SPDX-License-Identifier: GPL-2.0+
14 #include <env_callback.h>
15 #include <linux/types.h>
16 #include <stdio_dev.h>
19 #include <asm/unaligned.h>
22 #include <asm/unaligned.h>
23 #include <video_font.h>
25 #ifdef CONFIG_LCD_LOGO
27 #include <bmp_logo_data.h>
28 #if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
29 #error Default Color Map overlaps with Logo Color Map
37 #ifndef CONFIG_LCD_ALIGNMENT
38 #define CONFIG_LCD_ALIGNMENT PAGE_SIZE
41 #if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
42 (LCD_BPP != LCD_COLOR32)
43 #error Unsupported LCD BPP.
46 DECLARE_GLOBAL_DATA_PTR;
48 static int lcd_init(void *lcdbase);
49 static void lcd_logo(void);
50 static void lcd_setfgcolor(int color);
51 static void lcd_setbgcolor(int color);
53 static int lcd_color_fg;
54 static int lcd_color_bg;
56 char lcd_is_enabled = 0;
57 static void *lcd_base; /* Start of framebuffer memory */
58 static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
60 /* Flush LCD activity to the caches */
64 * flush_dcache_range() is declared in common.h but it seems that some
65 * architectures do not actually implement it. Is there a way to find
66 * out whether it exists? For now, ARM is safe.
68 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
72 flush_dcache_range((u32)lcd_base,
73 (u32)(lcd_base + lcd_get_size(&line_length)));
74 #elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
75 static ulong last_sync;
77 if (get_timer(last_sync) > 10) {
78 sandbox_sdl_sync(lcd_base);
79 last_sync = get_timer(0);
84 void lcd_set_flush_dcache(int flush)
86 lcd_flush_dcache = (flush != 0);
89 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
94 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
99 /* Small utility to check that you got the colours right */
100 #ifdef LCD_TEST_PATTERN
105 static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
106 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
107 CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
110 #if LCD_BPP == LCD_COLOR8
112 #elif LCD_BPP == LCD_COLOR16
113 typedef ushort pix_t;
114 #elif LCD_BPP == LCD_COLOR32
117 #error Unsupported pixelformat
120 static void test_pattern(void)
122 ushort v_max = panel_info.vl_row;
123 ushort h_max = panel_info.vl_col;
124 ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
125 ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
127 pix_t *pix = lcd_base;
129 printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
130 h_max, v_max, h_step, v_step);
132 /* WARNING: Code silently assumes 8bit/pixel */
133 for (v = 0; v < v_max; ++v) {
134 uchar iy = v / v_step;
135 for (h = 0; h < h_max; ++h) {
136 uchar ix = N_BLK_HOR * iy + h / h_step;
137 *pix++ = test_colors[ix];
141 #endif /* LCD_TEST_PATTERN */
144 * With most lcd drivers the line length is set up
145 * by calculating it from panel_info parameters. Some
146 * drivers need to calculate the line length differently,
147 * so make the function weak to allow overriding it.
149 __weak int lcd_get_size(int *line_length)
151 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
152 return *line_length * panel_info.vl_row;
155 int drv_lcd_init(void)
157 struct stdio_dev lcddev;
160 lcd_base = map_sysmem(gd->fb_base, 0);
164 /* Device initialization */
165 memset(&lcddev, 0, sizeof(lcddev));
167 strcpy(lcddev.name, "lcd");
168 lcddev.ext = 0; /* No extensions */
169 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
170 lcddev.putc = lcd_stub_putc; /* 'putc' function */
171 lcddev.puts = lcd_stub_puts; /* 'puts' function */
173 rc = stdio_register(&lcddev);
175 return (rc == 0) ? 1 : rc;
180 short console_rows, console_cols;
184 static int do_splash = 1;
185 #if LCD_BPP == LCD_COLOR8
186 /* Setting the palette */
187 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
188 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
189 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
190 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
191 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
192 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
193 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
194 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
195 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
198 #ifndef CONFIG_SYS_WHITE_ON_BLACK
199 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
200 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
201 bg_color = CONSOLE_COLOR_WHITE;
203 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
204 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
205 bg_color = CONSOLE_COLOR_BLACK;
206 #endif /* CONFIG_SYS_WHITE_ON_BLACK */
208 #ifdef LCD_TEST_PATTERN
211 /* set framebuffer to background color */
212 #if (LCD_BPP != LCD_COLOR32)
213 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
215 u32 *ppix = lcd_base;
218 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
224 /* Paint the logo and retrieve LCD base address */
225 debug("[LCD] Drawing the logo @ %p...\n", lcd_base);
226 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
227 console_rows = (panel_info.vl_row - BMP_LOGO_HEIGHT);
228 console_rows /= VIDEO_FONT_HEIGHT;
230 console_rows = panel_info.vl_row / VIDEO_FONT_HEIGHT;
232 console_cols = panel_info.vl_col / VIDEO_FONT_WIDTH;
233 lcd_init_console(lcd_base, console_rows, console_cols);
235 s = getenv("splashimage");
238 addr = simple_strtoul(s, NULL, 16);
239 if (lcd_splash(addr) == 0) {
247 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
248 addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
249 lcd_init_console((void *)addr, console_rows, console_cols);
254 static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
260 U_BOOT_CMD(cls, 1, 1, do_lcd_clear, "clear screen", "");
262 static int lcd_init(void *lcdbase)
264 debug("[LCD] Initializing %ux%ux%u LCD framebuffer at %p\n",
265 panel_info.vl_col, panel_info.vl_row, NBITS(panel_info.vl_bpix),
268 lcd_ctrl_init(lcdbase);
271 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
272 * the 'lcdbase' argument and uses custom lcd base address
273 * by setting up gd->fb_base. Check for this condition and fixup
274 * 'lcd_base' address.
276 if (map_to_sysmem(lcdbase) != gd->fb_base)
277 lcd_base = map_sysmem(gd->fb_base, 0);
279 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
281 lcd_get_size(&lcd_line_length);
286 /* Initialize the console */
288 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
289 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
291 lcd_set_row(1); /* leave 1 blank line below logo */
298 * This is called early in the system initialization to grab memory
299 * for the LCD controller.
300 * Returns new address for monitor, after reserving LCD buffer memory
302 * Note that this is running from ROM, so no write access to global data.
304 ulong lcd_setmem(ulong addr)
309 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
310 panel_info.vl_row, NBITS(panel_info.vl_bpix));
312 size = lcd_get_size(&line_length);
314 /* Round up to nearest full page, or MMU section if defined */
315 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
316 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
318 /* Allocate pages for the frame buffer. */
321 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
327 static void lcd_setfgcolor(int color)
329 lcd_color_fg = color;
332 int lcd_getfgcolor(void)
337 static void lcd_setbgcolor(int color)
339 lcd_color_bg = color;
342 int lcd_getbgcolor(void)
347 #ifdef CONFIG_LCD_LOGO
348 __weak void lcd_logo_set_cmap(void)
351 ushort *cmap = configuration_get_cmap();
353 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
354 *cmap++ = bmp_logo_palette[i];
357 void lcd_logo_plot(int x, int y)
360 uchar *bmap = &bmp_logo_bitmap[0];
361 unsigned bpix = NBITS(panel_info.vl_bpix);
362 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
365 debug("Logo: width %d height %d colors %d\n",
366 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
373 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
374 memcpy(fb, bmap, BMP_LOGO_WIDTH);
375 bmap += BMP_LOGO_WIDTH;
376 fb += panel_info.vl_col;
378 } else if (NBITS(panel_info.vl_bpix) == 16) {
381 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
382 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
383 col16 = bmp_logo_palette[(bmap[j]-16)];
385 ((col16 & 0x000F) << 1) |
386 ((col16 & 0x00F0) << 3) |
387 ((col16 & 0x0F00) << 4);
389 bmap += BMP_LOGO_WIDTH;
390 fb16 += panel_info.vl_col;
392 } else { /* true color mode */
394 u32 *fb32 = lcd_base + y * lcd_line_length + x;
396 for (i = 0; i < BMP_LOGO_HEIGHT; i++) {
397 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
398 col16 = bmp_logo_palette[bmap[j] - 16];
400 ((col16 & 0x000F) << 4) |
401 ((col16 & 0x00F0) << 8) |
402 ((col16 & 0x0F00) << 12);
404 bmap += BMP_LOGO_WIDTH;
405 fb32 += panel_info.vl_col;
413 static inline void lcd_logo_plot(int x, int y) {}
414 #endif /* CONFIG_LCD_LOGO */
416 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
417 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
418 #define BMP_ALIGN_CENTER 0x7FFF
420 static void splash_align_axis(int *axis, unsigned long panel_size,
421 unsigned long picture_size)
423 unsigned long panel_picture_delta = panel_size - picture_size;
424 unsigned long axis_alignment;
426 if (*axis == BMP_ALIGN_CENTER)
427 axis_alignment = panel_picture_delta / 2;
429 axis_alignment = panel_picture_delta + *axis + 1;
433 *axis = max(0, (int)axis_alignment);
437 #ifdef CONFIG_LCD_BMP_RLE8
438 #define BMP_RLE8_ESCAPE 0
439 #define BMP_RLE8_EOL 0
440 #define BMP_RLE8_EOBMP 1
441 #define BMP_RLE8_DELTA 2
443 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
447 *(*fbp)++ = cmap[*bmap++];
452 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
455 int cnt_8copy = cnt >> 3;
457 cnt -= cnt_8copy << 3;
458 while (cnt_8copy > 0) {
477 * Do not call this function directly, must be called from lcd_display_bitmap.
479 static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
480 int x_off, int y_off)
488 width = get_unaligned_le32(&bmp->header.width);
489 height = get_unaligned_le32(&bmp->header.height);
490 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
496 if (bmap[0] == BMP_RLE8_ESCAPE) {
503 /* 16bpix, 2-byte per pixel, width should *2 */
504 fb -= (width * 2 + lcd_line_length);
514 /* 16bpix, 2-byte per pixel, x should *2 */
515 fb = (uchar *) (lcd_base + (y + y_off - 1)
516 * lcd_line_length + (x + x_off) * 2);
525 if (x + runlen > width)
529 draw_unencoded_bitmap(
544 /* aggregate the same code */
545 while (bmap[0] == 0xff &&
546 bmap[2] != BMP_RLE8_ESCAPE &&
547 bmap[1] == bmap[3]) {
551 if (x + runlen > width)
555 draw_encoded_bitmap((ushort **)&fb,
566 __weak void fb_put_byte(uchar **fb, uchar **from)
568 *(*fb)++ = *(*from)++;
571 #if defined(CONFIG_BMP_16BPP)
572 __weak void fb_put_word(uchar **fb, uchar **from)
574 *(*fb)++ = *(*from)++;
575 *(*fb)++ = *(*from)++;
577 #endif /* CONFIG_BMP_16BPP */
579 __weak void lcd_set_cmap(bmp_image_t *bmp, unsigned colors)
582 bmp_color_table_entry_t cte;
583 ushort *cmap = configuration_get_cmap();
585 for (i = 0; i < colors; ++i) {
586 cte = bmp->color_table[i];
587 *cmap = (((cte.red) << 8) & 0xf800) |
588 (((cte.green) << 3) & 0x07e0) |
589 (((cte.blue) >> 3) & 0x001f);
590 #if defined(CONFIG_MPC823)
598 int lcd_display_bitmap(ulong bmp_image, int x, int y)
600 ushort *cmap_base = NULL;
603 bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
606 unsigned long width, height;
607 unsigned long pwidth = panel_info.vl_col;
608 unsigned long long colors;
609 unsigned bpix, bmp_bpix;
610 bmp_color_table_entry_t *cte;
612 if (!bmp || !(bmp->header.signature[0] == 'B' &&
613 bmp->header.signature[1] == 'M')) {
614 printf("Error: no valid bmp image at %lx\n", bmp_image);
619 width = get_unaligned_le32(&bmp->header.width);
620 height = get_unaligned_le32(&bmp->header.height);
621 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
623 colors = 1 << bmp_bpix;
625 bpix = NBITS(panel_info.vl_bpix);
627 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
628 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
635 * We support displaying 8bpp BMPs on 16bpp LCDs
636 * and displaying 24bpp BMPs on 32bpp LCDs
638 if (bpix != bmp_bpix &&
639 !(bmp_bpix == 8 && bpix == 16) &&
640 !(bmp_bpix == 24 && bpix == 32)) {
641 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
642 bpix, get_unaligned_le16(&bmp->header.bit_count));
646 debug("Display-bmp: %lu x %lu with %llu colors\n",
647 width, height, colors);
650 lcd_set_cmap(bmp, colors);
652 padded_width = ALIGN(width, 4);
654 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
655 splash_align_axis(&x, pwidth, width);
656 splash_align_axis(&y, panel_info.vl_row, height);
657 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
658 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
660 if ((x + width) > pwidth)
662 if ((y + height) > panel_info.vl_row) {
663 height = panel_info.vl_row - y;
664 bmap += (panel_info.vl_row - y) * padded_width;
667 fb = (uchar *)(lcd_base +
668 (y + height - 1) * lcd_line_length + x * bpix / 8);
673 cmap_base = configuration_get_cmap();
674 #ifdef CONFIG_LCD_BMP_RLE8
675 u32 compression = get_unaligned_le32(&bmp->header.compression);
676 if (compression == BMP_BI_RLE8) {
678 /* TODO implement render code for bpix != 16 */
679 printf("Error: only support 16 bpix");
682 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
687 for (i = 0; i < height; ++i) {
689 for (j = 0; j < width; j++) {
695 fb[1] = cte[i].green;
697 fb += sizeof(uint32_t) / sizeof(*fb);
698 } else if (bpix == 16) {
699 *(uint16_t *)fb = cmap_base[*(bmap++)];
700 fb += sizeof(uint16_t) / sizeof(*fb);
702 FB_PUT_BYTE(fb, bmap);
706 bmap += padded_width - width;
707 fb -= width * bpix / 8 + lcd_line_length;
709 bmap += padded_width;
710 fb -= lcd_line_length;
715 #if defined(CONFIG_BMP_16BPP)
717 for (i = 0; i < height; ++i) {
719 for (j = 0; j < width; j++)
720 fb_put_word(&fb, &bmap);
722 bmap += (padded_width - width) * 2;
723 fb -= width * 2 + lcd_line_length;
726 #endif /* CONFIG_BMP_16BPP */
727 #if defined(CONFIG_BMP_24BMP)
729 for (i = 0; i < height; ++i) {
730 for (j = 0; j < width; j++) {
736 fb -= lcd_line_length + width * (bpix / 8);
739 #endif /* CONFIG_BMP_24BPP */
740 #if defined(CONFIG_BMP_32BPP)
742 for (i = 0; i < height; ++i) {
744 for (j = 0; j < width; j++) {
745 fb[3] = *bmap++; /* T */
746 fb[0] = *bmap++; /* B */
747 fb[1] = *bmap++; /* G */
748 fb[2] = *bmap++; /* R */
751 bmap += (padded_width - width) * 4;
752 fb -= lcd_line_length + width * (bpix / 8);
755 #endif /* CONFIG_BMP_32BPP */
762 static void lcd_logo(void)
766 #ifdef CONFIG_LCD_INFO
767 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
768 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
769 lcd_show_board_info();
770 #endif /* CONFIG_LCD_INFO */
773 #ifdef CONFIG_SPLASHIMAGE_GUARD
774 static int on_splashimage(const char *name, const char *value, enum env_op op,
780 if (op == env_op_delete)
783 addr = simple_strtoul(value, NULL, 16);
784 /* See README.displaying-bmps */
785 aligned = (addr % 4 == 2);
787 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
794 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
797 int lcd_get_pixel_width(void)
799 return panel_info.vl_col;
802 int lcd_get_pixel_height(void)
804 return panel_info.vl_row;