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;
182 #if LCD_BPP == LCD_COLOR8
183 /* Setting the palette */
184 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
185 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
186 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
187 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
188 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
189 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
190 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
191 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
192 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
195 #ifndef CONFIG_SYS_WHITE_ON_BLACK
196 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
197 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
198 bg_color = CONSOLE_COLOR_WHITE;
200 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
201 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
202 bg_color = CONSOLE_COLOR_BLACK;
203 #endif /* CONFIG_SYS_WHITE_ON_BLACK */
205 #ifdef LCD_TEST_PATTERN
208 /* set framebuffer to background color */
209 #if (LCD_BPP != LCD_COLOR32)
210 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
212 u32 *ppix = lcd_base;
215 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
221 /* Paint the logo and retrieve LCD base address */
222 debug("[LCD] Drawing the logo @ %p...\n", lcd_base);
223 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
224 console_rows = (panel_info.vl_row - BMP_LOGO_HEIGHT);
225 console_rows /= VIDEO_FONT_HEIGHT;
227 console_rows = panel_info.vl_row / VIDEO_FONT_HEIGHT;
229 console_cols = panel_info.vl_col / VIDEO_FONT_WIDTH;
230 lcd_init_console(lcd_base, console_rows, console_cols);
231 lcd_init_console(lcd_logo(), console_rows, console_cols);
235 static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
241 U_BOOT_CMD(cls, 1, 1, do_lcd_clear, "clear screen", "");
243 static int lcd_init(void *lcdbase)
245 debug("[LCD] Initializing %ux%ux%u LCD framebuffer at %p\n",
246 panel_info.vl_col, panel_info.vl_row, NBITS(panel_info.vl_bpix),
249 lcd_ctrl_init(lcdbase);
252 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
253 * the 'lcdbase' argument and uses custom lcd base address
254 * by setting up gd->fb_base. Check for this condition and fixup
255 * 'lcd_base' address.
257 if (map_to_sysmem(lcdbase) != gd->fb_base)
258 lcd_base = map_sysmem(gd->fb_base, 0);
260 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
262 lcd_get_size(&lcd_line_length);
267 /* Initialize the console */
269 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
270 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
272 lcd_set_row(1); /* leave 1 blank line below logo */
279 * This is called early in the system initialization to grab memory
280 * for the LCD controller.
281 * Returns new address for monitor, after reserving LCD buffer memory
283 * Note that this is running from ROM, so no write access to global data.
285 ulong lcd_setmem(ulong addr)
290 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
291 panel_info.vl_row, NBITS(panel_info.vl_bpix));
293 size = lcd_get_size(&line_length);
295 /* Round up to nearest full page, or MMU section if defined */
296 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
297 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
299 /* Allocate pages for the frame buffer. */
302 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
308 static void lcd_setfgcolor(int color)
310 lcd_color_fg = color;
313 int lcd_getfgcolor(void)
318 static void lcd_setbgcolor(int color)
320 lcd_color_bg = color;
323 int lcd_getbgcolor(void)
328 #ifdef CONFIG_LCD_LOGO
329 __weak void lcd_logo_set_cmap(void)
332 ushort *cmap = configuration_get_cmap();
334 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
335 *cmap++ = bmp_logo_palette[i];
338 void lcd_logo_plot(int x, int y)
341 uchar *bmap = &bmp_logo_bitmap[0];
342 unsigned bpix = NBITS(panel_info.vl_bpix);
343 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
346 debug("Logo: width %d height %d colors %d\n",
347 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
354 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
355 memcpy(fb, bmap, BMP_LOGO_WIDTH);
356 bmap += BMP_LOGO_WIDTH;
357 fb += panel_info.vl_col;
359 } else if (NBITS(panel_info.vl_bpix) == 16) {
362 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
363 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
364 col16 = bmp_logo_palette[(bmap[j]-16)];
366 ((col16 & 0x000F) << 1) |
367 ((col16 & 0x00F0) << 3) |
368 ((col16 & 0x0F00) << 4);
370 bmap += BMP_LOGO_WIDTH;
371 fb16 += panel_info.vl_col;
373 } else { /* true color mode */
375 u32 *fb32 = lcd_base + y * lcd_line_length + x;
377 for (i = 0; i < BMP_LOGO_HEIGHT; i++) {
378 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
379 col16 = bmp_logo_palette[bmap[j] - 16];
381 ((col16 & 0x000F) << 4) |
382 ((col16 & 0x00F0) << 8) |
383 ((col16 & 0x0F00) << 12);
385 bmap += BMP_LOGO_WIDTH;
386 fb32 += panel_info.vl_col;
394 static inline void lcd_logo_plot(int x, int y) {}
395 #endif /* CONFIG_LCD_LOGO */
397 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
398 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
399 #define BMP_ALIGN_CENTER 0x7FFF
401 static void splash_align_axis(int *axis, unsigned long panel_size,
402 unsigned long picture_size)
404 unsigned long panel_picture_delta = panel_size - picture_size;
405 unsigned long axis_alignment;
407 if (*axis == BMP_ALIGN_CENTER)
408 axis_alignment = panel_picture_delta / 2;
410 axis_alignment = panel_picture_delta + *axis + 1;
414 *axis = max(0, (int)axis_alignment);
418 #ifdef CONFIG_LCD_BMP_RLE8
419 #define BMP_RLE8_ESCAPE 0
420 #define BMP_RLE8_EOL 0
421 #define BMP_RLE8_EOBMP 1
422 #define BMP_RLE8_DELTA 2
424 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
428 *(*fbp)++ = cmap[*bmap++];
433 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
436 int cnt_8copy = cnt >> 3;
438 cnt -= cnt_8copy << 3;
439 while (cnt_8copy > 0) {
458 * Do not call this function directly, must be called from lcd_display_bitmap.
460 static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
461 int x_off, int y_off)
469 width = get_unaligned_le32(&bmp->header.width);
470 height = get_unaligned_le32(&bmp->header.height);
471 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
477 if (bmap[0] == BMP_RLE8_ESCAPE) {
484 /* 16bpix, 2-byte per pixel, width should *2 */
485 fb -= (width * 2 + lcd_line_length);
495 /* 16bpix, 2-byte per pixel, x should *2 */
496 fb = (uchar *) (lcd_base + (y + y_off - 1)
497 * lcd_line_length + (x + x_off) * 2);
506 if (x + runlen > width)
510 draw_unencoded_bitmap(
525 /* aggregate the same code */
526 while (bmap[0] == 0xff &&
527 bmap[2] != BMP_RLE8_ESCAPE &&
528 bmap[1] == bmap[3]) {
532 if (x + runlen > width)
536 draw_encoded_bitmap((ushort **)&fb,
547 __weak void fb_put_byte(uchar **fb, uchar **from)
549 *(*fb)++ = *(*from)++;
552 #if defined(CONFIG_BMP_16BPP)
553 __weak void fb_put_word(uchar **fb, uchar **from)
555 *(*fb)++ = *(*from)++;
556 *(*fb)++ = *(*from)++;
558 #endif /* CONFIG_BMP_16BPP */
560 __weak void lcd_set_cmap(bmp_image_t *bmp, unsigned colors)
563 bmp_color_table_entry_t cte;
564 ushort *cmap = configuration_get_cmap();
566 for (i = 0; i < colors; ++i) {
567 cte = bmp->color_table[i];
568 *cmap = (((cte.red) << 8) & 0xf800) |
569 (((cte.green) << 3) & 0x07e0) |
570 (((cte.blue) >> 3) & 0x001f);
571 #if defined(CONFIG_MPC823)
579 int lcd_display_bitmap(ulong bmp_image, int x, int y)
581 ushort *cmap_base = NULL;
584 bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
587 unsigned long width, height;
588 unsigned long pwidth = panel_info.vl_col;
589 unsigned long long colors;
590 unsigned bpix, bmp_bpix;
591 bmp_color_table_entry_t *cte;
593 if (!bmp || !(bmp->header.signature[0] == 'B' &&
594 bmp->header.signature[1] == 'M')) {
595 printf("Error: no valid bmp image at %lx\n", bmp_image);
600 width = get_unaligned_le32(&bmp->header.width);
601 height = get_unaligned_le32(&bmp->header.height);
602 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
604 colors = 1 << bmp_bpix;
606 bpix = NBITS(panel_info.vl_bpix);
608 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
609 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
616 * We support displaying 8bpp BMPs on 16bpp LCDs
617 * and displaying 24bpp BMPs on 32bpp LCDs
619 if (bpix != bmp_bpix &&
620 !(bmp_bpix == 8 && bpix == 16) &&
621 !(bmp_bpix == 24 && bpix == 32)) {
622 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
623 bpix, get_unaligned_le16(&bmp->header.bit_count));
627 debug("Display-bmp: %lu x %lu with %llu colors\n",
628 width, height, colors);
631 lcd_set_cmap(bmp, colors);
633 padded_width = ALIGN(width, 4);
635 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
636 splash_align_axis(&x, pwidth, width);
637 splash_align_axis(&y, panel_info.vl_row, height);
638 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
639 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
641 if ((x + width) > pwidth)
643 if ((y + height) > panel_info.vl_row) {
644 height = panel_info.vl_row - y;
645 bmap += (panel_info.vl_row - y) * padded_width;
648 fb = (uchar *)(lcd_base +
649 (y + height - 1) * lcd_line_length + x * bpix / 8);
654 cmap_base = configuration_get_cmap();
655 #ifdef CONFIG_LCD_BMP_RLE8
656 u32 compression = get_unaligned_le32(&bmp->header.compression);
657 if (compression == BMP_BI_RLE8) {
659 /* TODO implement render code for bpix != 16 */
660 printf("Error: only support 16 bpix");
663 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
668 for (i = 0; i < height; ++i) {
670 for (j = 0; j < width; j++) {
676 fb[1] = cte[i].green;
678 fb += sizeof(uint32_t) / sizeof(*fb);
679 } else if (bpix == 16) {
680 *(uint16_t *)fb = cmap_base[*(bmap++)];
681 fb += sizeof(uint16_t) / sizeof(*fb);
683 FB_PUT_BYTE(fb, bmap);
687 bmap += padded_width - width;
688 fb -= width * bpix / 8 + lcd_line_length;
690 bmap += padded_width;
691 fb -= lcd_line_length;
696 #if defined(CONFIG_BMP_16BPP)
698 for (i = 0; i < height; ++i) {
700 for (j = 0; j < width; j++)
701 fb_put_word(&fb, &bmap);
703 bmap += (padded_width - width) * 2;
704 fb -= width * 2 + lcd_line_length;
707 #endif /* CONFIG_BMP_16BPP */
708 #if defined(CONFIG_BMP_24BMP)
710 for (i = 0; i < height; ++i) {
711 for (j = 0; j < width; j++) {
717 fb -= lcd_line_length + width * (bpix / 8);
720 #endif /* CONFIG_BMP_24BPP */
721 #if defined(CONFIG_BMP_32BPP)
723 for (i = 0; i < height; ++i) {
725 for (j = 0; j < width; j++) {
726 fb[3] = *bmap++; /* T */
727 fb[0] = *bmap++; /* B */
728 fb[1] = *bmap++; /* G */
729 fb[2] = *bmap++; /* R */
732 bmap += (padded_width - width) * 4;
733 fb -= lcd_line_length + width * (bpix / 8);
736 #endif /* CONFIG_BMP_32BPP */
743 static void *lcd_logo(void)
745 #ifdef CONFIG_SPLASH_SCREEN
748 static int do_splash = 1;
750 if (do_splash && (s = getenv("splashimage")) != NULL) {
756 if (splash_screen_prepare())
757 return (void *)lcd_base;
759 addr = simple_strtoul (s, &end, 16);
760 if (addr == 0 || *end != '\0')
763 splash_get_pos(&x, &y);
765 if (bmp_display(addr, x, y) == 0)
766 return (void *)lcd_base;
768 #endif /* CONFIG_SPLASH_SCREEN */
772 #ifdef CONFIG_LCD_INFO
773 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
774 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
775 lcd_show_board_info();
776 #endif /* CONFIG_LCD_INFO */
778 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
779 return lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
782 #endif /* CONFIG_LCD_LOGO && !CONFIG_LCD_INFO_BELOW_LOGO */
785 #ifdef CONFIG_SPLASHIMAGE_GUARD
786 static int on_splashimage(const char *name, const char *value, enum env_op op,
792 if (op == env_op_delete)
795 addr = simple_strtoul(value, NULL, 16);
796 /* See README.displaying-bmps */
797 aligned = (addr % 4 == 2);
799 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
806 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
809 int lcd_get_pixel_width(void)
811 return panel_info.vl_col;
814 int lcd_get_pixel_height(void)
816 return panel_info.vl_row;