1 // SPDX-License-Identifier: GPL-2.0+
5 * (C) Copyright 2001-2002
6 * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
14 #include <env_callback.h>
15 #include <asm/cache.h>
16 #include <linux/types.h>
17 #include <stdio_dev.h>
21 #include <asm/unaligned.h>
24 #include <asm/unaligned.h>
25 #include <video_font.h>
27 #ifdef CONFIG_LCD_LOGO
29 #include <bmp_logo_data.h>
30 #if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
31 #error Default Color Map overlaps with Logo Color Map
35 #ifndef CONFIG_LCD_ALIGNMENT
36 #define CONFIG_LCD_ALIGNMENT PAGE_SIZE
39 #if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
40 (LCD_BPP != LCD_COLOR32)
41 #error Unsupported LCD BPP.
44 DECLARE_GLOBAL_DATA_PTR;
46 static int lcd_init(void *lcdbase);
47 static void lcd_logo(void);
48 static void lcd_setfgcolor(int color);
49 static void lcd_setbgcolor(int color);
51 static int lcd_color_fg;
52 static int lcd_color_bg;
54 char lcd_is_enabled = 0;
55 static void *lcd_base; /* Start of framebuffer memory */
56 static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
58 /* Flush LCD activity to the caches */
62 * flush_dcache_range() is declared in common.h but it seems that some
63 * architectures do not actually implement it. Is there a way to find
64 * out whether it exists? For now, ARM is safe.
66 #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
70 flush_dcache_range((ulong)lcd_base,
71 (ulong)(lcd_base + lcd_get_size(&line_length)));
75 void lcd_set_flush_dcache(int flush)
77 lcd_flush_dcache = (flush != 0);
80 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
85 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
90 /* Small utility to check that you got the colours right */
91 #ifdef LCD_TEST_PATTERN
93 #if LCD_BPP == LCD_COLOR8
97 static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
98 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
99 CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
100 }; /*LCD_BPP == LCD_COLOR8 */
102 #elif LCD_BPP == LCD_COLOR16
106 static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
107 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW, CONSOLE_COLOR_BLUE,
108 CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN, CONSOLE_COLOR_GREY, CONSOLE_COLOR_WHITE,
110 #endif /*LCD_BPP == LCD_COLOR16 */
112 static void test_pattern(void)
114 ushort v_max = panel_info.vl_row;
115 ushort h_max = panel_info.vl_col;
116 ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
117 ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
119 #if LCD_BPP == LCD_COLOR8
120 uchar *pix = (uchar *)lcd_base;
121 #elif LCD_BPP == LCD_COLOR16
122 ushort *pix = (ushort *)lcd_base;
125 printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
126 h_max, v_max, h_step, v_step);
128 for (v = 0; v < v_max; ++v) {
129 uchar iy = v / v_step;
130 for (h = 0; h < h_max; ++h) {
131 uchar ix = N_BLK_HOR * iy + h / h_step;
132 *pix++ = test_colors[ix];
136 #endif /* LCD_TEST_PATTERN */
139 * With most lcd drivers the line length is set up
140 * by calculating it from panel_info parameters. Some
141 * drivers need to calculate the line length differently,
142 * so make the function weak to allow overriding it.
144 __weak int lcd_get_size(int *line_length)
146 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
147 return *line_length * panel_info.vl_row;
150 int drv_lcd_init(void)
152 struct stdio_dev lcddev;
155 lcd_base = map_sysmem(gd->fb_base, 0);
159 /* Device initialization */
160 memset(&lcddev, 0, sizeof(lcddev));
162 strcpy(lcddev.name, "lcd");
163 lcddev.ext = 0; /* No extensions */
164 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
165 lcddev.putc = lcd_stub_putc; /* 'putc' function */
166 lcddev.puts = lcd_stub_puts; /* 'puts' function */
168 rc = stdio_register(&lcddev);
170 return (rc == 0) ? 1 : rc;
176 __maybe_unused ulong addr;
177 static int do_splash = 1;
178 #if LCD_BPP == LCD_COLOR8
179 /* Setting the palette */
180 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
181 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
182 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
183 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
184 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
185 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
186 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
187 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
188 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
191 #ifndef CONFIG_SYS_WHITE_ON_BLACK
192 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
193 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
194 bg_color = CONSOLE_COLOR_WHITE;
196 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
197 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
198 bg_color = CONSOLE_COLOR_BLACK;
199 #endif /* CONFIG_SYS_WHITE_ON_BLACK */
201 #ifdef LCD_TEST_PATTERN
204 /* set framebuffer to background color */
205 #if (LCD_BPP != LCD_COLOR32)
206 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
208 u32 *ppix = lcd_base;
211 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
217 /* setup text-console */
218 debug("[LCD] setting up console...\n");
219 lcd_init_console(lcd_base,
223 /* Paint the logo and retrieve LCD base address */
224 debug("[LCD] Drawing the logo...\n");
226 if (splash_display() == 0) {
234 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
235 addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
236 lcd_init_console((void *)addr, panel_info.vl_col,
237 panel_info.vl_row, panel_info.vl_rot);
242 static int lcd_init(void *lcdbase)
244 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
245 lcd_ctrl_init(lcdbase);
248 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
249 * the 'lcdbase' argument and uses custom lcd base address
250 * by setting up gd->fb_base. Check for this condition and fixup
251 * 'lcd_base' address.
253 if (map_to_sysmem(lcdbase) != gd->fb_base)
254 lcd_base = map_sysmem(gd->fb_base, 0);
256 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
258 lcd_get_size(&lcd_line_length);
263 /* Initialize the console */
265 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
266 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
268 lcd_set_row(1); /* leave 1 blank line below logo */
275 * This is called early in the system initialization to grab memory
276 * for the LCD controller.
277 * Returns new address for monitor, after reserving LCD buffer memory
279 * Note that this is running from ROM, so no write access to global data.
281 ulong lcd_setmem(ulong addr)
286 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
287 panel_info.vl_row, NBITS(panel_info.vl_bpix));
289 size = lcd_get_size(&line_length);
291 /* Round up to nearest full page, or MMU section if defined */
292 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
293 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
295 /* Allocate pages for the frame buffer. */
298 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
304 static void lcd_setfgcolor(int color)
306 lcd_color_fg = color;
309 int lcd_getfgcolor(void)
314 static void lcd_setbgcolor(int color)
316 lcd_color_bg = color;
319 int lcd_getbgcolor(void)
324 #ifdef CONFIG_LCD_LOGO
325 __weak void lcd_logo_set_cmap(void)
328 ushort *cmap = configuration_get_cmap();
330 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
331 *cmap++ = bmp_logo_palette[i];
334 void lcd_logo_plot(int x, int y)
337 uchar *bmap = &bmp_logo_bitmap[0];
338 unsigned bpix = NBITS(panel_info.vl_bpix);
339 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
342 debug("Logo: width %d height %d colors %d\n",
343 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
350 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
351 memcpy(fb, bmap, BMP_LOGO_WIDTH);
352 bmap += BMP_LOGO_WIDTH;
353 fb += panel_info.vl_col;
356 else { /* true color mode */
359 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
360 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
361 col16 = bmp_logo_palette[(bmap[j]-16)];
363 ((col16 & 0x000F) << 1) |
364 ((col16 & 0x00F0) << 3) |
365 ((col16 & 0x0F00) << 4);
367 bmap += BMP_LOGO_WIDTH;
368 fb16 += panel_info.vl_col;
376 static inline void lcd_logo_plot(int x, int y) {}
377 #endif /* CONFIG_LCD_LOGO */
379 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
380 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
382 static void splash_align_axis(int *axis, unsigned long panel_size,
383 unsigned long picture_size)
385 unsigned long panel_picture_delta = panel_size - picture_size;
386 unsigned long axis_alignment;
388 if (*axis == BMP_ALIGN_CENTER)
389 axis_alignment = panel_picture_delta / 2;
391 axis_alignment = panel_picture_delta + *axis + 1;
395 *axis = max(0, (int)axis_alignment);
399 #ifdef CONFIG_LCD_BMP_RLE8
400 #define BMP_RLE8_ESCAPE 0
401 #define BMP_RLE8_EOL 0
402 #define BMP_RLE8_EOBMP 1
403 #define BMP_RLE8_DELTA 2
405 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
409 *(*fbp)++ = cmap[*bmap++];
414 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
417 int cnt_8copy = cnt >> 3;
419 cnt -= cnt_8copy << 3;
420 while (cnt_8copy > 0) {
439 * Do not call this function directly, must be called from lcd_display_bitmap.
441 static void lcd_display_rle8_bitmap(struct bmp_image *bmp, ushort *cmap,
442 uchar *fb, int x_off, int y_off)
450 width = get_unaligned_le32(&bmp->header.width);
451 height = get_unaligned_le32(&bmp->header.height);
452 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
458 if (bmap[0] == BMP_RLE8_ESCAPE) {
465 /* 16bpix, 2-byte per pixel, width should *2 */
466 fb -= (width * 2 + lcd_line_length);
476 /* 16bpix, 2-byte per pixel, x should *2 */
477 fb = (uchar *) (lcd_base + (y + y_off - 1)
478 * lcd_line_length + (x + x_off) * 2);
487 if (x + runlen > width)
491 draw_unencoded_bitmap(
506 /* aggregate the same code */
507 while (bmap[0] == 0xff &&
508 bmap[2] != BMP_RLE8_ESCAPE &&
509 bmap[1] == bmap[3]) {
513 if (x + runlen > width)
517 draw_encoded_bitmap((ushort **)&fb,
528 __weak void fb_put_byte(uchar **fb, uchar **from)
530 *(*fb)++ = *(*from)++;
533 #if defined(CONFIG_BMP_16BPP)
534 __weak void fb_put_word(uchar **fb, uchar **from)
536 *(*fb)++ = *(*from)++;
537 *(*fb)++ = *(*from)++;
539 #endif /* CONFIG_BMP_16BPP */
541 __weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
544 struct bmp_color_table_entry cte;
545 ushort *cmap = configuration_get_cmap();
547 for (i = 0; i < colors; ++i) {
548 cte = bmp->color_table[i];
549 *cmap = (((cte.red) << 8) & 0xf800) |
550 (((cte.green) << 3) & 0x07e0) |
551 (((cte.blue) >> 3) & 0x001f);
556 int lcd_display_bitmap(ulong bmp_image, int x, int y)
558 ushort *cmap_base = NULL;
561 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
564 unsigned long width, height, byte_width;
565 unsigned long pwidth = panel_info.vl_col;
566 unsigned colors, bpix, bmp_bpix;
568 struct bmp_color_table_entry *palette;
570 if (!bmp || !(bmp->header.signature[0] == 'B' &&
571 bmp->header.signature[1] == 'M')) {
572 printf("Error: no valid bmp image at %lx\n", bmp_image);
577 palette = bmp->color_table;
578 width = get_unaligned_le32(&bmp->header.width);
579 height = get_unaligned_le32(&bmp->header.height);
580 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
581 hdr_size = get_unaligned_le16(&bmp->header.size);
582 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
584 colors = 1 << bmp_bpix;
586 bpix = NBITS(panel_info.vl_bpix);
588 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
589 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
596 * We support displaying 8bpp BMPs on 16bpp LCDs
597 * and displaying 24bpp BMPs on 32bpp LCDs
599 if (bpix != bmp_bpix &&
600 !(bmp_bpix == 8 && bpix == 16) &&
601 !(bmp_bpix == 24 && bpix == 32)) {
602 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
603 bpix, get_unaligned_le16(&bmp->header.bit_count));
607 debug("Display-bmp: %d x %d with %d colors, display %d\n",
608 (int)width, (int)height, (int)colors, 1 << bpix);
611 lcd_set_cmap(bmp, colors);
613 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
615 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
616 splash_align_axis(&x, pwidth, width);
617 splash_align_axis(&y, panel_info.vl_row, height);
618 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
620 if ((x + width) > pwidth)
622 if ((y + height) > panel_info.vl_row)
623 height = panel_info.vl_row - y;
625 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
626 fb = (uchar *)(lcd_base +
627 (y + height - 1) * lcd_line_length + x * bpix / 8);
632 cmap_base = configuration_get_cmap();
633 #ifdef CONFIG_LCD_BMP_RLE8
634 u32 compression = get_unaligned_le32(&bmp->header.compression);
635 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
636 if (compression == BMP_BI_RLE8) {
638 /* TODO implement render code for bpix != 16 */
639 printf("Error: only support 16 bpix");
642 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
650 byte_width = width * 2;
652 for (i = 0; i < height; ++i) {
654 for (j = 0; j < width; j++) {
656 fb_put_byte(&fb, &bmap);
658 struct bmp_color_table_entry *entry;
662 val = cmap_base[*bmap];
664 entry = &palette[*bmap];
665 val = entry->blue >> 3 |
666 entry->green >> 2 << 5 |
667 entry->red >> 3 << 11;
669 *(uint16_t *)fb = val;
671 fb += sizeof(uint16_t) / sizeof(*fb);
674 bmap += (padded_width - width);
675 fb -= byte_width + lcd_line_length;
679 #if defined(CONFIG_BMP_16BPP)
681 for (i = 0; i < height; ++i) {
683 for (j = 0; j < width; j++)
684 fb_put_word(&fb, &bmap);
686 bmap += (padded_width - width) * 2;
687 fb -= width * 2 + lcd_line_length;
690 #endif /* CONFIG_BMP_16BPP */
691 #if defined(CONFIG_BMP_24BPP)
693 for (i = 0; i < height; ++i) {
694 for (j = 0; j < width; j++) {
700 fb -= lcd_line_length + width * (bpix / 8);
703 #endif /* CONFIG_BMP_24BPP */
704 #if defined(CONFIG_BMP_32BPP)
706 for (i = 0; i < height; ++i) {
707 for (j = 0; j < width; j++) {
713 fb -= lcd_line_length + width * (bpix / 8);
716 #endif /* CONFIG_BMP_32BPP */
726 static void lcd_logo(void)
730 #ifdef CONFIG_LCD_INFO
731 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
732 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
733 lcd_show_board_info();
734 #endif /* CONFIG_LCD_INFO */
737 #ifdef CONFIG_SPLASHIMAGE_GUARD
738 static int on_splashimage(const char *name, const char *value, enum env_op op,
744 if (op == env_op_delete)
747 addr = simple_strtoul(value, NULL, 16);
748 /* See README.displaying-bmps */
749 aligned = (addr % 4 == 2);
751 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
758 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
761 int lcd_get_pixel_width(void)
763 return panel_info.vl_col;
766 int lcd_get_pixel_height(void)
768 return panel_info.vl_row;