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 for (v = 0; v < v_max; ++v) {
133 uchar iy = v / v_step;
134 for (h = 0; h < h_max; ++h) {
135 uchar ix = N_BLK_HOR * iy + h / h_step;
136 *pix++ = test_colors[ix];
140 #endif /* LCD_TEST_PATTERN */
143 * With most lcd drivers the line length is set up
144 * by calculating it from panel_info parameters. Some
145 * drivers need to calculate the line length differently,
146 * so make the function weak to allow overriding it.
148 __weak int lcd_get_size(int *line_length)
150 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
151 return *line_length * panel_info.vl_row;
154 int drv_lcd_init(void)
156 struct stdio_dev lcddev;
159 lcd_base = map_sysmem(gd->fb_base, 0);
163 /* Device initialization */
164 memset(&lcddev, 0, sizeof(lcddev));
166 strcpy(lcddev.name, "lcd");
167 lcddev.ext = 0; /* No extensions */
168 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
169 lcddev.putc = lcd_stub_putc; /* 'putc' function */
170 lcddev.puts = lcd_stub_puts; /* 'puts' function */
172 rc = stdio_register(&lcddev);
174 return (rc == 0) ? 1 : rc;
182 static int do_splash = 1;
183 #if LCD_BPP == LCD_COLOR8
184 /* Setting the palette */
185 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
186 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
187 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
188 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
189 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
190 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
191 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
192 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
193 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
196 #ifndef CONFIG_SYS_WHITE_ON_BLACK
197 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
198 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
199 bg_color = CONSOLE_COLOR_WHITE;
201 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
202 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
203 bg_color = CONSOLE_COLOR_BLACK;
204 #endif /* CONFIG_SYS_WHITE_ON_BLACK */
206 #ifdef LCD_TEST_PATTERN
209 /* set framebuffer to background color */
210 #if (LCD_BPP != LCD_COLOR32)
211 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
213 u32 *ppix = lcd_base;
216 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
222 /* setup text-console */
223 debug("[LCD] setting up console...\n");
224 lcd_init_console(lcd_base,
228 /* Paint the logo and retrieve LCD base address */
229 debug("[LCD] Drawing the logo @ %p...\n", lcd_base);
231 s = getenv("splashimage");
234 addr = simple_strtoul(s, NULL, 16);
235 if (lcd_splash(addr) == 0) {
243 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
244 addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
245 lcd_init_console((void *)addr, panel_info.vl_row,
246 panel_info.vl_col, panel_info.vl_rot);
251 static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
257 U_BOOT_CMD(cls, 1, 1, do_lcd_clear, "clear screen", "");
259 static int lcd_init(void *lcdbase)
261 debug("[LCD] Initializing %ux%ux%u LCD framebuffer at %p\n",
262 panel_info.vl_col, panel_info.vl_row, NBITS(panel_info.vl_bpix),
265 lcd_ctrl_init(lcdbase);
268 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
269 * the 'lcdbase' argument and uses custom lcd base address
270 * by setting up gd->fb_base. Check for this condition and fixup
271 * 'lcd_base' address.
273 if (map_to_sysmem(lcdbase) != gd->fb_base)
274 lcd_base = map_sysmem(gd->fb_base, 0);
276 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
278 lcd_get_size(&lcd_line_length);
283 /* Initialize the console */
285 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
286 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
288 lcd_set_row(1); /* leave 1 blank line below logo */
295 * This is called early in the system initialization to grab memory
296 * for the LCD controller.
297 * Returns new address for monitor, after reserving LCD buffer memory
299 * Note that this is running from ROM, so no write access to global data.
301 ulong lcd_setmem(ulong addr)
306 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
307 panel_info.vl_row, NBITS(panel_info.vl_bpix));
309 size = lcd_get_size(&line_length);
311 /* Round up to nearest full page, or MMU section if defined */
312 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
313 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
315 /* Allocate pages for the frame buffer. */
318 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
324 static void lcd_setfgcolor(int color)
326 lcd_color_fg = color;
329 int lcd_getfgcolor(void)
334 static void lcd_setbgcolor(int color)
336 lcd_color_bg = color;
339 int lcd_getbgcolor(void)
344 #ifdef CONFIG_LCD_LOGO
345 __weak void lcd_logo_set_cmap(void)
348 ushort *cmap = configuration_get_cmap();
350 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
351 *cmap++ = bmp_logo_palette[i];
354 void lcd_logo_plot(int x, int y)
357 uchar *bmap = &bmp_logo_bitmap[0];
358 unsigned bpix = NBITS(panel_info.vl_bpix);
359 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
362 debug("Logo: width %d height %d colors %d\n",
363 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
370 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
371 memcpy(fb, bmap, BMP_LOGO_WIDTH);
372 bmap += BMP_LOGO_WIDTH;
373 fb += panel_info.vl_col;
375 } else if (NBITS(panel_info.vl_bpix) == 16) {
378 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
379 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
380 col16 = bmp_logo_palette[(bmap[j]-16)];
382 ((col16 & 0x000F) << 1) |
383 ((col16 & 0x00F0) << 3) |
384 ((col16 & 0x0F00) << 4);
386 bmap += BMP_LOGO_WIDTH;
387 fb16 += panel_info.vl_col;
389 } else { /* true color mode */
391 u32 *fb32 = lcd_base + y * lcd_line_length + x;
393 for (i = 0; i < BMP_LOGO_HEIGHT; i++) {
394 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
395 col16 = bmp_logo_palette[bmap[j] - 16];
397 ((col16 & 0x000F) << 4) |
398 ((col16 & 0x00F0) << 8) |
399 ((col16 & 0x0F00) << 12);
401 bmap += BMP_LOGO_WIDTH;
402 fb32 += panel_info.vl_col;
410 static inline void lcd_logo_plot(int x, int y) {}
411 #endif /* CONFIG_LCD_LOGO */
413 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
414 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
415 #define BMP_ALIGN_CENTER 0x7FFF
417 static void splash_align_axis(int *axis, unsigned long panel_size,
418 unsigned long picture_size)
420 unsigned long panel_picture_delta = panel_size - picture_size;
421 unsigned long axis_alignment;
423 if (*axis == BMP_ALIGN_CENTER)
424 axis_alignment = panel_picture_delta / 2;
426 axis_alignment = panel_picture_delta + *axis + 1;
430 *axis = max(0, (int)axis_alignment);
434 #ifdef CONFIG_LCD_BMP_RLE8
435 #define BMP_RLE8_ESCAPE 0
436 #define BMP_RLE8_EOL 0
437 #define BMP_RLE8_EOBMP 1
438 #define BMP_RLE8_DELTA 2
440 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
444 *(*fbp)++ = cmap[*bmap++];
449 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
452 int cnt_8copy = cnt >> 3;
454 cnt -= cnt_8copy << 3;
455 while (cnt_8copy > 0) {
474 * Do not call this function directly, must be called from lcd_display_bitmap.
476 static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
477 int x_off, int y_off)
485 width = get_unaligned_le32(&bmp->header.width);
486 height = get_unaligned_le32(&bmp->header.height);
487 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
493 if (bmap[0] == BMP_RLE8_ESCAPE) {
500 /* 16bpix, 2-byte per pixel, width should *2 */
501 fb -= (width * 2 + lcd_line_length);
511 /* 16bpix, 2-byte per pixel, x should *2 */
512 fb = (uchar *) (lcd_base + (y + y_off - 1)
513 * lcd_line_length + (x + x_off) * 2);
522 if (x + runlen > width)
526 draw_unencoded_bitmap(
541 /* aggregate the same code */
542 while (bmap[0] == 0xff &&
543 bmap[2] != BMP_RLE8_ESCAPE &&
544 bmap[1] == bmap[3]) {
548 if (x + runlen > width)
552 draw_encoded_bitmap((ushort **)&fb,
563 __weak void fb_put_byte(uchar **fb, uchar **from)
565 *(*fb)++ = *(*from)++;
568 #if defined(CONFIG_BMP_16BPP)
569 __weak void fb_put_word(uchar **fb, uchar **from)
571 *(*fb)++ = *(*from)++;
572 *(*fb)++ = *(*from)++;
574 #endif /* CONFIG_BMP_16BPP */
576 __weak void lcd_set_cmap(bmp_image_t *bmp, unsigned colors)
579 bmp_color_table_entry_t cte;
580 ushort *cmap = configuration_get_cmap();
582 for (i = 0; i < colors; ++i) {
583 cte = bmp->color_table[i];
584 *cmap = (((cte.red) << 8) & 0xf800) |
585 (((cte.green) << 3) & 0x07e0) |
586 (((cte.blue) >> 3) & 0x001f);
587 #if defined(CONFIG_MPC823)
595 int lcd_display_bitmap(ulong bmp_image, int x, int y)
597 ushort *cmap_base = NULL;
600 bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
603 unsigned long width, height;
604 unsigned long pwidth = panel_info.vl_col;
605 unsigned long long colors;
606 unsigned bpix, bmp_bpix;
607 bmp_color_table_entry_t *cte;
609 if (!bmp || !(bmp->header.signature[0] == 'B' &&
610 bmp->header.signature[1] == 'M')) {
611 printf("Error: no valid bmp image at %lx\n", bmp_image);
616 width = get_unaligned_le32(&bmp->header.width);
617 height = get_unaligned_le32(&bmp->header.height);
618 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
620 colors = 1 << bmp_bpix;
622 bpix = NBITS(panel_info.vl_bpix);
624 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
625 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
632 * We support displaying 8bpp BMPs on 16bpp LCDs
633 * and displaying 24bpp BMPs on 32bpp LCDs
635 if (bpix != bmp_bpix &&
636 !(bmp_bpix == 8 && bpix == 16) &&
637 !(bmp_bpix == 24 && bpix == 32)) {
638 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
639 bpix, get_unaligned_le16(&bmp->header.bit_count));
643 debug("Display-bmp: %lu x %lu with %llu colors\n",
644 width, height, colors);
647 lcd_set_cmap(bmp, colors);
649 padded_width = ALIGN(width, 4);
651 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
652 splash_align_axis(&x, pwidth, width);
653 splash_align_axis(&y, panel_info.vl_row, height);
654 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
655 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
657 if ((x + width) > pwidth)
659 if ((y + height) > panel_info.vl_row) {
660 height = panel_info.vl_row - y;
661 bmap += (panel_info.vl_row - y) * padded_width;
664 fb = (uchar *)(lcd_base +
665 (y + height - 1) * lcd_line_length + x * bpix / 8);
670 cmap_base = configuration_get_cmap();
671 #ifdef CONFIG_LCD_BMP_RLE8
672 u32 compression = get_unaligned_le32(&bmp->header.compression);
673 if (compression == BMP_BI_RLE8) {
675 /* TODO implement render code for bpix != 16 */
676 printf("Error: only support 16 bpix");
679 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
684 for (i = 0; i < height; ++i) {
686 for (j = 0; j < width; j++) {
692 fb[1] = cte[i].green;
694 fb += sizeof(uint32_t) / sizeof(*fb);
695 } else if (bpix == 16) {
696 *(uint16_t *)fb = cmap_base[*(bmap++)];
697 fb += sizeof(uint16_t) / sizeof(*fb);
699 FB_PUT_BYTE(fb, bmap);
703 bmap += padded_width - width;
704 fb -= width * bpix / 8 + lcd_line_length;
706 bmap += padded_width;
707 fb -= lcd_line_length;
712 #if defined(CONFIG_BMP_16BPP)
714 for (i = 0; i < height; ++i) {
716 for (j = 0; j < width; j++)
717 fb_put_word(&fb, &bmap);
719 bmap += (padded_width - width) * 2;
720 fb -= width * 2 + lcd_line_length;
723 #endif /* CONFIG_BMP_16BPP */
724 #if defined(CONFIG_BMP_24BMP)
726 for (i = 0; i < height; ++i) {
727 for (j = 0; j < width; j++) {
733 fb -= lcd_line_length + width * (bpix / 8);
736 #endif /* CONFIG_BMP_24BPP */
737 #if defined(CONFIG_BMP_32BPP)
739 for (i = 0; i < height; ++i) {
741 for (j = 0; j < width; j++) {
742 fb[3] = *bmap++; /* T */
743 fb[0] = *bmap++; /* B */
744 fb[1] = *bmap++; /* G */
745 fb[2] = *bmap++; /* R */
748 bmap += (padded_width - width) * 4;
749 fb -= lcd_line_length + width * (bpix / 8);
752 #endif /* CONFIG_BMP_32BPP */
760 static void lcd_logo(void)
764 #ifdef CONFIG_LCD_INFO
765 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
766 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
767 lcd_show_board_info();
768 #endif /* CONFIG_LCD_INFO */
771 #ifdef CONFIG_SPLASHIMAGE_GUARD
772 static int on_splashimage(const char *name, const char *value, enum env_op op,
778 if (op == env_op_delete)
781 addr = simple_strtoul(value, NULL, 16);
782 /* See README.displaying-bmps */
783 aligned = (addr % 4 == 2);
785 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
792 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
795 int lcd_get_pixel_width(void)
797 return panel_info.vl_col;
800 int lcd_get_pixel_height(void)
802 return panel_info.vl_row;