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>
16 #include <asm/cache.h>
18 #include <asm/global_data.h>
19 #include <linux/types.h>
20 #include <stdio_dev.h>
24 #include <asm/unaligned.h>
27 #include <asm/unaligned.h>
28 #include <video_font.h>
30 #ifdef CONFIG_LCD_LOGO
32 #include <bmp_logo_data.h>
33 #if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
34 #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) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
73 flush_dcache_range((ulong)lcd_base,
74 (ulong)(lcd_base + lcd_get_size(&line_length)));
78 void lcd_set_flush_dcache(int flush)
80 lcd_flush_dcache = (flush != 0);
83 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
88 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
94 * With most lcd drivers the line length is set up
95 * by calculating it from panel_info parameters. Some
96 * drivers need to calculate the line length differently,
97 * so make the function weak to allow overriding it.
99 __weak int lcd_get_size(int *line_length)
101 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
102 return *line_length * panel_info.vl_row;
105 int drv_lcd_init(void)
107 struct stdio_dev lcddev;
110 lcd_base = map_sysmem(gd->fb_base, 0);
114 /* Device initialization */
115 memset(&lcddev, 0, sizeof(lcddev));
117 strcpy(lcddev.name, "lcd");
118 lcddev.ext = 0; /* No extensions */
119 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
120 lcddev.putc = lcd_stub_putc; /* 'putc' function */
121 lcddev.puts = lcd_stub_puts; /* 'puts' function */
123 rc = stdio_register(&lcddev);
125 return (rc == 0) ? 1 : rc;
131 __maybe_unused ulong addr;
132 static int do_splash = 1;
133 #if LCD_BPP == LCD_COLOR8
134 /* Setting the palette */
135 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
136 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
137 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
138 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
139 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
140 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
141 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
142 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
143 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
146 #ifndef CONFIG_SYS_WHITE_ON_BLACK
147 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
148 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
149 bg_color = CONSOLE_COLOR_WHITE;
151 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
152 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
153 bg_color = CONSOLE_COLOR_BLACK;
154 #endif /* CONFIG_SYS_WHITE_ON_BLACK */
156 /* set framebuffer to background color */
157 #if (LCD_BPP != LCD_COLOR32)
158 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
160 u32 *ppix = lcd_base;
163 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
168 /* setup text-console */
169 debug("[LCD] setting up console...\n");
170 lcd_init_console(lcd_base,
174 /* Paint the logo and retrieve LCD base address */
175 debug("[LCD] Drawing the logo...\n");
177 if (splash_display() == 0) {
185 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
186 addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
187 lcd_init_console((void *)addr, panel_info.vl_col,
188 panel_info.vl_row, panel_info.vl_rot);
193 static int lcd_init(void *lcdbase)
195 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
196 lcd_ctrl_init(lcdbase);
199 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
200 * the 'lcdbase' argument and uses custom lcd base address
201 * by setting up gd->fb_base. Check for this condition and fixup
202 * 'lcd_base' address.
204 if (map_to_sysmem(lcdbase) != gd->fb_base)
205 lcd_base = map_sysmem(gd->fb_base, 0);
207 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
209 lcd_get_size(&lcd_line_length);
214 /* Initialize the console */
216 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
217 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
219 lcd_set_row(1); /* leave 1 blank line below logo */
226 * This is called early in the system initialization to grab memory
227 * for the LCD controller.
228 * Returns new address for monitor, after reserving LCD buffer memory
230 * Note that this is running from ROM, so no write access to global data.
232 ulong lcd_setmem(ulong addr)
237 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
238 panel_info.vl_row, NBITS(panel_info.vl_bpix));
240 size = lcd_get_size(&line_length);
242 /* Round up to nearest full page, or MMU section if defined */
243 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
244 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
246 /* Allocate pages for the frame buffer. */
249 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
255 static void lcd_setfgcolor(int color)
257 lcd_color_fg = color;
260 int lcd_getfgcolor(void)
265 static void lcd_setbgcolor(int color)
267 lcd_color_bg = color;
270 int lcd_getbgcolor(void)
275 #ifdef CONFIG_LCD_LOGO
276 __weak void lcd_logo_set_cmap(void)
279 ushort *cmap = configuration_get_cmap();
281 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
282 *cmap++ = bmp_logo_palette[i];
285 void lcd_logo_plot(int x, int y)
288 uchar *bmap = &bmp_logo_bitmap[0];
289 unsigned bpix = NBITS(panel_info.vl_bpix);
290 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
293 debug("Logo: width %d height %d colors %d\n",
294 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
301 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
302 memcpy(fb, bmap, BMP_LOGO_WIDTH);
303 bmap += BMP_LOGO_WIDTH;
304 fb += panel_info.vl_col;
307 else { /* true color mode */
310 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
311 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
312 col16 = bmp_logo_palette[(bmap[j]-16)];
314 ((col16 & 0x000F) << 1) |
315 ((col16 & 0x00F0) << 3) |
316 ((col16 & 0x0F00) << 4);
318 bmap += BMP_LOGO_WIDTH;
319 fb16 += panel_info.vl_col;
327 static inline void lcd_logo_plot(int x, int y) {}
328 #endif /* CONFIG_LCD_LOGO */
330 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
331 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
333 static void splash_align_axis(int *axis, unsigned long panel_size,
334 unsigned long picture_size)
336 unsigned long panel_picture_delta = panel_size - picture_size;
337 unsigned long axis_alignment;
339 if (*axis == BMP_ALIGN_CENTER)
340 axis_alignment = panel_picture_delta / 2;
342 axis_alignment = panel_picture_delta + *axis + 1;
346 *axis = max(0, (int)axis_alignment);
350 #ifdef CONFIG_LCD_BMP_RLE8
351 #define BMP_RLE8_ESCAPE 0
352 #define BMP_RLE8_EOL 0
353 #define BMP_RLE8_EOBMP 1
354 #define BMP_RLE8_DELTA 2
356 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
360 *(*fbp)++ = cmap[*bmap++];
365 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
368 int cnt_8copy = cnt >> 3;
370 cnt -= cnt_8copy << 3;
371 while (cnt_8copy > 0) {
390 * Do not call this function directly, must be called from lcd_display_bitmap.
392 static void lcd_display_rle8_bitmap(struct bmp_image *bmp, ushort *cmap,
393 uchar *fb, int x_off, int y_off)
401 width = get_unaligned_le32(&bmp->header.width);
402 height = get_unaligned_le32(&bmp->header.height);
403 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
409 if (bmap[0] == BMP_RLE8_ESCAPE) {
416 /* 16bpix, 2-byte per pixel, width should *2 */
417 fb -= (width * 2 + lcd_line_length);
427 /* 16bpix, 2-byte per pixel, x should *2 */
428 fb = (uchar *) (lcd_base + (y + y_off - 1)
429 * lcd_line_length + (x + x_off) * 2);
438 if (x + runlen > width)
442 draw_unencoded_bitmap(
457 /* aggregate the same code */
458 while (bmap[0] == 0xff &&
459 bmap[2] != BMP_RLE8_ESCAPE &&
460 bmap[1] == bmap[3]) {
464 if (x + runlen > width)
468 draw_encoded_bitmap((ushort **)&fb,
479 __weak void fb_put_byte(uchar **fb, uchar **from)
481 *(*fb)++ = *(*from)++;
484 #if defined(CONFIG_BMP_16BPP)
485 __weak void fb_put_word(uchar **fb, uchar **from)
487 *(*fb)++ = *(*from)++;
488 *(*fb)++ = *(*from)++;
490 #endif /* CONFIG_BMP_16BPP */
492 __weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
495 struct bmp_color_table_entry cte;
496 ushort *cmap = configuration_get_cmap();
498 for (i = 0; i < colors; ++i) {
499 cte = bmp->color_table[i];
500 *cmap = (((cte.red) << 8) & 0xf800) |
501 (((cte.green) << 3) & 0x07e0) |
502 (((cte.blue) >> 3) & 0x001f);
507 int lcd_display_bitmap(ulong bmp_image, int x, int y)
509 ushort *cmap_base = NULL;
512 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
515 unsigned long width, height, byte_width;
516 unsigned long pwidth = panel_info.vl_col;
517 unsigned colors, bpix, bmp_bpix;
519 struct bmp_color_table_entry *palette;
521 if (!bmp || !(bmp->header.signature[0] == 'B' &&
522 bmp->header.signature[1] == 'M')) {
523 printf("Error: no valid bmp image at %lx\n", bmp_image);
528 palette = bmp->color_table;
529 width = get_unaligned_le32(&bmp->header.width);
530 height = get_unaligned_le32(&bmp->header.height);
531 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
532 hdr_size = get_unaligned_le16(&bmp->header.size);
533 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
535 colors = 1 << bmp_bpix;
537 bpix = NBITS(panel_info.vl_bpix);
539 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
540 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
547 * We support displaying 8bpp BMPs on 16bpp LCDs
548 * and displaying 24bpp BMPs on 32bpp LCDs
550 if (bpix != bmp_bpix &&
551 !(bmp_bpix == 8 && bpix == 16) &&
552 !(bmp_bpix == 24 && bpix == 32)) {
553 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
554 bpix, get_unaligned_le16(&bmp->header.bit_count));
558 debug("Display-bmp: %d x %d with %d colors, display %d\n",
559 (int)width, (int)height, (int)colors, 1 << bpix);
562 lcd_set_cmap(bmp, colors);
564 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
566 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
567 splash_align_axis(&x, pwidth, width);
568 splash_align_axis(&y, panel_info.vl_row, height);
569 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
571 if ((x + width) > pwidth)
573 if ((y + height) > panel_info.vl_row)
574 height = panel_info.vl_row - y;
576 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
577 fb = (uchar *)(lcd_base +
578 (y + height - 1) * lcd_line_length + x * bpix / 8);
583 cmap_base = configuration_get_cmap();
584 #ifdef CONFIG_LCD_BMP_RLE8
585 u32 compression = get_unaligned_le32(&bmp->header.compression);
586 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
587 if (compression == BMP_BI_RLE8) {
589 /* TODO implement render code for bpix != 16 */
590 printf("Error: only support 16 bpix");
593 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
601 byte_width = width * 2;
603 for (i = 0; i < height; ++i) {
605 for (j = 0; j < width; j++) {
607 fb_put_byte(&fb, &bmap);
609 struct bmp_color_table_entry *entry;
613 val = cmap_base[*bmap];
615 entry = &palette[*bmap];
616 val = entry->blue >> 3 |
617 entry->green >> 2 << 5 |
618 entry->red >> 3 << 11;
620 *(uint16_t *)fb = val;
622 fb += sizeof(uint16_t) / sizeof(*fb);
625 bmap += (padded_width - width);
626 fb -= byte_width + lcd_line_length;
630 #if defined(CONFIG_BMP_16BPP)
632 for (i = 0; i < height; ++i) {
634 for (j = 0; j < width; j++)
635 fb_put_word(&fb, &bmap);
637 bmap += (padded_width - width) * 2;
638 fb -= width * 2 + lcd_line_length;
641 #endif /* CONFIG_BMP_16BPP */
642 #if defined(CONFIG_BMP_24BPP)
644 for (i = 0; i < height; ++i) {
645 for (j = 0; j < width; j++) {
651 fb -= lcd_line_length + width * (bpix / 8);
654 #endif /* CONFIG_BMP_24BPP */
655 #if defined(CONFIG_BMP_32BPP)
657 for (i = 0; i < height; ++i) {
658 for (j = 0; j < width; j++) {
664 fb -= lcd_line_length + width * (bpix / 8);
667 #endif /* CONFIG_BMP_32BPP */
677 static void lcd_logo(void)
681 #ifdef CONFIG_LCD_INFO
682 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
683 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
684 lcd_show_board_info();
685 #endif /* CONFIG_LCD_INFO */
688 #ifdef CONFIG_SPLASHIMAGE_GUARD
689 static int on_splashimage(const char *name, const char *value, enum env_op op,
695 if (op == env_op_delete)
698 addr = hextoul(value, NULL);
699 /* See README.displaying-bmps */
700 aligned = (addr % 4 == 2);
702 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
709 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
712 int lcd_get_pixel_width(void)
714 return panel_info.vl_col;
717 int lcd_get_pixel_height(void)
719 return panel_info.vl_row;