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>
20 #include <asm/unaligned.h>
23 #include <asm/unaligned.h>
24 #include <video_font.h>
26 #ifdef CONFIG_LCD_LOGO
28 #include <bmp_logo_data.h>
29 #if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
30 #error Default Color Map overlaps with Logo Color Map
38 #ifndef CONFIG_LCD_ALIGNMENT
39 #define CONFIG_LCD_ALIGNMENT PAGE_SIZE
42 #if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
43 (LCD_BPP != LCD_COLOR32)
44 #error Unsupported LCD BPP.
47 DECLARE_GLOBAL_DATA_PTR;
49 static int lcd_init(void *lcdbase);
50 static void lcd_logo(void);
51 static void lcd_setfgcolor(int color);
52 static void lcd_setbgcolor(int color);
54 static int lcd_color_fg;
55 static int lcd_color_bg;
57 char lcd_is_enabled = 0;
58 static void *lcd_base; /* Start of framebuffer memory */
59 static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
61 /* Flush LCD activity to the caches */
65 * flush_dcache_range() is declared in common.h but it seems that some
66 * architectures do not actually implement it. Is there a way to find
67 * out whether it exists? For now, ARM is safe.
69 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
73 flush_dcache_range((u32)lcd_base,
74 (u32)(lcd_base + lcd_get_size(&line_length)));
75 #elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
76 static ulong last_sync;
78 if (get_timer(last_sync) > 10) {
79 sandbox_sdl_sync(lcd_base);
80 last_sync = get_timer(0);
85 void lcd_set_flush_dcache(int flush)
87 lcd_flush_dcache = (flush != 0);
90 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
95 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
100 /* Small utility to check that you got the colours right */
101 #ifdef LCD_TEST_PATTERN
106 static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
107 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
108 CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
111 #if LCD_BPP == LCD_COLOR8
113 #elif LCD_BPP == LCD_COLOR16
114 typedef ushort pix_t;
115 #elif LCD_BPP == LCD_COLOR32
118 #error Unsupported pixelformat
121 static void test_pattern(void)
123 ushort v_max = panel_info.vl_row;
124 ushort h_max = panel_info.vl_col;
125 ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
126 ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
128 pix_t *pix = lcd_base;
130 printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
131 h_max, v_max, h_step, v_step);
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;
183 static int do_splash = 1;
184 #if LCD_BPP == LCD_COLOR8
185 /* Setting the palette */
186 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
187 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
188 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
189 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
190 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
191 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
192 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
193 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
194 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
197 #ifndef CONFIG_SYS_WHITE_ON_BLACK
198 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
199 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
200 bg_color = CONSOLE_COLOR_WHITE;
202 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
203 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
204 bg_color = CONSOLE_COLOR_BLACK;
205 #endif /* CONFIG_SYS_WHITE_ON_BLACK */
207 #ifdef LCD_TEST_PATTERN
210 /* set framebuffer to background color */
211 #if (LCD_BPP != LCD_COLOR32)
212 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
214 u32 *ppix = lcd_base;
217 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
223 /* setup text-console */
224 debug("[LCD] setting up console...\n");
225 lcd_init_console(lcd_base,
229 /* Paint the logo and retrieve LCD base address */
230 debug("[LCD] Drawing the logo @ %p...\n", lcd_base);
232 s = getenv("splashimage");
235 addr = simple_strtoul(s, NULL, 16);
236 if (lcd_splash(addr) == 0) {
244 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
245 addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
246 lcd_init_console((void *)addr, panel_info.vl_row,
247 panel_info.vl_col, panel_info.vl_rot);
252 static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
258 U_BOOT_CMD(cls, 1, 1, do_lcd_clear, "clear screen", "");
260 static int lcd_init(void *lcdbase)
262 debug("[LCD] Initializing %ux%ux%u LCD framebuffer at %p\n",
263 panel_info.vl_col, panel_info.vl_row, NBITS(panel_info.vl_bpix),
266 lcd_ctrl_init(lcdbase);
269 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
270 * the 'lcdbase' argument and uses custom lcd base address
271 * by setting up gd->fb_base. Check for this condition and fixup
272 * 'lcd_base' address.
274 if (map_to_sysmem(lcdbase) != gd->fb_base)
275 lcd_base = map_sysmem(gd->fb_base, 0);
277 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
279 lcd_get_size(&lcd_line_length);
284 /* Initialize the console */
286 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
287 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
289 lcd_set_row(1); /* leave 1 blank line below logo */
296 * This is called early in the system initialization to grab memory
297 * for the LCD controller.
298 * Returns new address for monitor, after reserving LCD buffer memory
300 * Note that this is running from ROM, so no write access to global data.
302 ulong lcd_setmem(ulong addr)
307 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
308 panel_info.vl_row, NBITS(panel_info.vl_bpix));
310 size = lcd_get_size(&line_length);
312 /* Round up to nearest full page, or MMU section if defined */
313 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
314 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
316 /* Allocate pages for the frame buffer. */
319 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
325 static void lcd_setfgcolor(int color)
327 lcd_color_fg = color;
330 int lcd_getfgcolor(void)
335 static void lcd_setbgcolor(int color)
337 lcd_color_bg = color;
340 int lcd_getbgcolor(void)
345 #ifdef CONFIG_LCD_LOGO
346 __weak void lcd_logo_set_cmap(void)
349 ushort *cmap = configuration_get_cmap();
351 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
352 *cmap++ = bmp_logo_palette[i];
355 void lcd_logo_plot(int x, int y)
358 uchar *bmap = &bmp_logo_bitmap[0];
359 unsigned bpix = NBITS(panel_info.vl_bpix);
360 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
363 debug("Logo: width %d height %d colors %d\n",
364 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
371 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
372 memcpy(fb, bmap, BMP_LOGO_WIDTH);
373 bmap += BMP_LOGO_WIDTH;
374 fb += panel_info.vl_col;
376 } else if (NBITS(panel_info.vl_bpix) == 16) {
379 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
380 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
381 col16 = bmp_logo_palette[(bmap[j]-16)];
383 ((col16 & 0x000F) << 1) |
384 ((col16 & 0x00F0) << 3) |
385 ((col16 & 0x0F00) << 4);
387 bmap += BMP_LOGO_WIDTH;
388 fb16 += panel_info.vl_col;
390 } else { /* true color mode */
392 u32 *fb32 = lcd_base + y * lcd_line_length + x;
394 for (i = 0; i < BMP_LOGO_HEIGHT; i++) {
395 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
396 col16 = bmp_logo_palette[bmap[j] - 16];
398 ((col16 & 0x000F) << 4) |
399 ((col16 & 0x00F0) << 8) |
400 ((col16 & 0x0F00) << 12);
402 bmap += BMP_LOGO_WIDTH;
403 fb32 += panel_info.vl_col;
411 static inline void lcd_logo_plot(int x, int y) {}
412 #endif /* CONFIG_LCD_LOGO */
414 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
415 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
416 #define BMP_ALIGN_CENTER 0x7FFF
418 static void splash_align_axis(int *axis, unsigned long panel_size,
419 unsigned long picture_size)
421 unsigned long panel_picture_delta = panel_size - picture_size;
422 unsigned long axis_alignment;
424 if (*axis == BMP_ALIGN_CENTER)
425 axis_alignment = panel_picture_delta / 2;
427 axis_alignment = panel_picture_delta + *axis + 1;
431 *axis = max(0, (int)axis_alignment);
435 #ifdef CONFIG_LCD_BMP_RLE8
436 #define BMP_RLE8_ESCAPE 0
437 #define BMP_RLE8_EOL 0
438 #define BMP_RLE8_EOBMP 1
439 #define BMP_RLE8_DELTA 2
441 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
445 *(*fbp)++ = cmap[*bmap++];
450 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
453 int cnt_8copy = cnt >> 3;
455 cnt -= cnt_8copy << 3;
456 while (cnt_8copy > 0) {
475 * Do not call this function directly, must be called from lcd_display_bitmap.
477 static void lcd_display_rle8_bitmap(struct bmp_image *bmp, ushort *cmap,
478 uchar *fb, int x_off, int y_off)
486 width = get_unaligned_le32(&bmp->header.width);
487 height = get_unaligned_le32(&bmp->header.height);
488 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
494 if (bmap[0] == BMP_RLE8_ESCAPE) {
501 /* 16bpix, 2-byte per pixel, width should *2 */
502 fb -= (width * 2 + lcd_line_length);
512 /* 16bpix, 2-byte per pixel, x should *2 */
513 fb = (uchar *) (lcd_base + (y + y_off - 1)
514 * lcd_line_length + (x + x_off) * 2);
523 if (x + runlen > width)
527 draw_unencoded_bitmap(
542 /* aggregate the same code */
543 while (bmap[0] == 0xff &&
544 bmap[2] != BMP_RLE8_ESCAPE &&
545 bmap[1] == bmap[3]) {
549 if (x + runlen > width)
553 draw_encoded_bitmap((ushort **)&fb,
564 __weak void fb_put_byte(uchar **fb, uchar **from)
566 *(*fb)++ = *(*from)++;
569 #if defined(CONFIG_BMP_16BPP)
570 __weak void fb_put_word(uchar **fb, uchar **from)
572 *(*fb)++ = *(*from)++;
573 *(*fb)++ = *(*from)++;
575 #endif /* CONFIG_BMP_16BPP */
577 __weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
580 struct bmp_color_table_entry cte;
581 ushort *cmap = configuration_get_cmap();
583 for (i = 0; i < colors; ++i) {
584 cte = bmp->color_table[i];
585 *cmap = (((cte.red) << 8) & 0xf800) |
586 (((cte.green) << 3) & 0x07e0) |
587 (((cte.blue) >> 3) & 0x001f);
588 #if defined(CONFIG_MPC823)
596 int lcd_display_bitmap(ulong bmp_image, int x, int y)
598 ushort *cmap_base = NULL;
601 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
604 unsigned long width, height;
605 unsigned long pwidth = panel_info.vl_col;
606 unsigned long long colors;
607 unsigned bpix, bmp_bpix;
609 struct bmp_color_table_entry *palette = bmp->color_table;
611 if (!bmp || !(bmp->header.signature[0] == 'B' &&
612 bmp->header.signature[1] == 'M')) {
613 printf("Error: no valid bmp image at %lx\n", bmp_image);
618 width = get_unaligned_le32(&bmp->header.width);
619 height = get_unaligned_le32(&bmp->header.height);
620 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
621 hdr_size = get_unaligned_le16(&bmp->header.size);
622 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
624 colors = 1 << bmp_bpix;
626 bpix = NBITS(panel_info.vl_bpix);
628 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
629 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
636 * We support displaying 8bpp BMPs on 16bpp LCDs
637 * and displaying 24bpp BMPs on 32bpp LCDs
639 if (bpix != bmp_bpix &&
640 !(bmp_bpix == 8 && bpix == 16) &&
641 !(bmp_bpix == 24 && bpix == 32)) {
642 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
643 bpix, get_unaligned_le16(&bmp->header.bit_count));
647 debug("Display-bmp: %lu x %lu with %llu colors, display %d\n",
648 width, height, colors, NBITS(bmp_bpix));
651 lcd_set_cmap(bmp, colors);
653 padded_width = ALIGN(width, 4);
655 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
656 splash_align_axis(&x, pwidth, width);
657 splash_align_axis(&y, panel_info.vl_row, height);
658 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
659 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
661 if ((x + width) > pwidth)
663 if ((y + height) > panel_info.vl_row) {
664 height = panel_info.vl_row - y;
665 bmap += (panel_info.vl_row - y) * padded_width;
668 fb = (uchar *)(lcd_base +
669 (y + height - 1) * lcd_line_length + x * bpix / 8);
674 cmap_base = configuration_get_cmap();
675 #ifdef CONFIG_LCD_BMP_RLE8
676 u32 compression = get_unaligned_le32(&bmp->header.compression);
677 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
678 if (compression == BMP_BI_RLE8) {
680 /* TODO implement render code for bpix != 16 */
681 printf("Error: only support 16 bpix");
684 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
689 for (i = 0; i < height; ++i) {
691 for (j = 0; j < width; j++) {
693 fb_put_byte(&fb, &bmap);
695 struct bmp_color_table_entry *entry;
699 val = cmap_base[*bmap];
701 entry = &palette[*bmap];
702 val = entry->blue >> 3 |
703 entry->green >> 2 << 5 |
704 entry->red >> 3 << 11;
706 *(uint16_t *)fb = val;
708 fb += sizeof(uint16_t) / sizeof(*fb);
710 FB_PUT_BYTE(fb, bmap);
714 bmap += padded_width - width;
715 fb -= width * bpix / 8 + lcd_line_length;
717 bmap += padded_width;
718 fb -= lcd_line_length;
723 #if defined(CONFIG_BMP_16BPP)
725 for (i = 0; i < height; ++i) {
727 for (j = 0; j < width; j++)
728 fb_put_word(&fb, &bmap);
730 bmap += (padded_width - width) * 2;
731 fb -= width * 2 + lcd_line_length;
734 #endif /* CONFIG_BMP_16BPP */
735 #if defined(CONFIG_BMP_24BMP)
737 for (i = 0; i < height; ++i) {
738 for (j = 0; j < width; j++) {
744 fb -= lcd_line_length + width * (bpix / 8);
747 #endif /* CONFIG_BMP_24BPP */
748 #if defined(CONFIG_BMP_32BPP)
750 for (i = 0; i < height; ++i) {
752 for (j = 0; j < width; j++) {
753 fb[3] = *bmap++; /* T */
754 fb[0] = *bmap++; /* B */
755 fb[1] = *bmap++; /* G */
756 fb[2] = *bmap++; /* R */
759 bmap += (padded_width - width) * 4;
760 fb -= lcd_line_length + width * (bpix / 8);
763 #endif /* CONFIG_BMP_32BPP */
771 static void lcd_logo(void)
775 #ifdef CONFIG_LCD_INFO
776 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
777 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
778 lcd_show_board_info();
779 #endif /* CONFIG_LCD_INFO */
782 #ifdef CONFIG_SPLASHIMAGE_GUARD
783 static int on_splashimage(const char *name, const char *value, enum env_op op,
789 if (op == env_op_delete)
792 addr = simple_strtoul(value, NULL, 16);
793 /* See README.displaying-bmps */
794 aligned = (addr % 4 == 2);
796 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
803 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
806 int lcd_get_pixel_width(void)
808 return panel_info.vl_col;
811 int lcd_get_pixel_height(void)
813 return panel_info.vl_row;