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 <fdt_support.h>
24 #include <video_font.h>
26 #if defined(CONFIG_LCD_DT_SIMPLEFB)
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
42 #ifndef CONFIG_LCD_ALIGNMENT
43 #define CONFIG_LCD_ALIGNMENT PAGE_SIZE
46 #if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
47 (LCD_BPP != LCD_COLOR32)
48 #error Unsupported LCD BPP.
51 DECLARE_GLOBAL_DATA_PTR;
53 static int lcd_init(void *lcdbase);
54 static void *lcd_logo(void);
55 static void lcd_setfgcolor(int color);
56 static void lcd_setbgcolor(int color);
58 static int lcd_color_fg;
59 static int lcd_color_bg;
61 char lcd_is_enabled = 0;
62 static void *lcd_base; /* Start of framebuffer memory */
63 static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
65 /* Flush LCD activity to the caches */
69 * flush_dcache_range() is declared in common.h but it seems that some
70 * architectures do not actually implement it. Is there a way to find
71 * out whether it exists? For now, ARM is safe.
73 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
77 flush_dcache_range((u32)lcd_base,
78 (u32)(lcd_base + lcd_get_size(&line_length)));
79 #elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
80 static ulong last_sync;
82 if (get_timer(last_sync) > 10) {
83 sandbox_sdl_sync(lcd_base);
84 last_sync = get_timer(0);
89 void lcd_set_flush_dcache(int flush)
91 lcd_flush_dcache = (flush != 0);
94 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
99 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
104 /* Small utility to check that you got the colours right */
105 #ifdef LCD_TEST_PATTERN
110 static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
111 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
112 CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
115 static void test_pattern(void)
117 ushort v_max = panel_info.vl_row;
118 ushort h_max = panel_info.vl_col;
119 ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
120 ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
122 uchar *pix = (uchar *)lcd_base;
124 printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
125 h_max, v_max, h_step, v_step);
127 /* WARNING: Code silently assumes 8bit/pixel */
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;
175 short console_rows, console_cols;
177 #if LCD_BPP == LCD_COLOR8
178 /* Setting the palette */
179 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
180 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
181 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
182 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
183 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
184 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
185 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
186 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
187 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
190 #ifndef CONFIG_SYS_WHITE_ON_BLACK
191 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
192 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
193 bg_color = CONSOLE_COLOR_WHITE;
195 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
196 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
197 bg_color = CONSOLE_COLOR_BLACK;
198 #endif /* CONFIG_SYS_WHITE_ON_BLACK */
200 #ifdef LCD_TEST_PATTERN
203 /* set framebuffer to background color */
204 #if (LCD_BPP != LCD_COLOR32)
205 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
207 u32 *ppix = lcd_base;
210 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
216 /* Paint the logo and retrieve LCD base address */
217 debug("[LCD] Drawing the logo...\n");
218 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
219 console_rows = (panel_info.vl_row - BMP_LOGO_HEIGHT);
220 console_rows /= VIDEO_FONT_HEIGHT;
222 console_rows = panel_info.vl_row / VIDEO_FONT_HEIGHT;
224 console_cols = panel_info.vl_col / VIDEO_FONT_WIDTH;
225 lcd_init_console(lcd_base, console_rows, console_cols);
226 lcd_init_console(lcd_logo(), console_rows, console_cols);
230 static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
236 U_BOOT_CMD(cls, 1, 1, do_lcd_clear, "clear screen", "");
238 static int lcd_init(void *lcdbase)
240 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
241 lcd_ctrl_init(lcdbase);
244 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
245 * the 'lcdbase' argument and uses custom lcd base address
246 * by setting up gd->fb_base. Check for this condition and fixup
247 * 'lcd_base' address.
249 if (map_to_sysmem(lcdbase) != gd->fb_base)
250 lcd_base = map_sysmem(gd->fb_base, 0);
252 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
254 lcd_get_size(&lcd_line_length);
259 /* Initialize the console */
261 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
262 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
264 lcd_set_row(1); /* leave 1 blank line below logo */
271 * This is called early in the system initialization to grab memory
272 * for the LCD controller.
273 * Returns new address for monitor, after reserving LCD buffer memory
275 * Note that this is running from ROM, so no write access to global data.
277 ulong lcd_setmem(ulong addr)
282 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
283 panel_info.vl_row, NBITS(panel_info.vl_bpix));
285 size = lcd_get_size(&line_length);
287 /* Round up to nearest full page, or MMU section if defined */
288 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
289 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
291 /* Allocate pages for the frame buffer. */
294 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
300 static void lcd_setfgcolor(int color)
302 lcd_color_fg = color;
305 int lcd_getfgcolor(void)
310 static void lcd_setbgcolor(int color)
312 lcd_color_bg = color;
315 int lcd_getbgcolor(void)
320 #ifdef CONFIG_LCD_LOGO
321 __weak void lcd_logo_set_cmap(void)
324 ushort *cmap = configuration_get_cmap();
326 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
327 *cmap++ = bmp_logo_palette[i];
330 void lcd_logo_plot(int x, int y)
333 uchar *bmap = &bmp_logo_bitmap[0];
334 unsigned bpix = NBITS(panel_info.vl_bpix);
335 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
338 debug("Logo: width %d height %d colors %d\n",
339 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
346 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
347 memcpy(fb, bmap, BMP_LOGO_WIDTH);
348 bmap += BMP_LOGO_WIDTH;
349 fb += panel_info.vl_col;
352 else { /* true color mode */
355 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
356 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
357 col16 = bmp_logo_palette[(bmap[j]-16)];
359 ((col16 & 0x000F) << 1) |
360 ((col16 & 0x00F0) << 3) |
361 ((col16 & 0x0F00) << 4);
363 bmap += BMP_LOGO_WIDTH;
364 fb16 += panel_info.vl_col;
372 static inline void lcd_logo_plot(int x, int y) {}
373 #endif /* CONFIG_LCD_LOGO */
375 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
376 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
377 #define BMP_ALIGN_CENTER 0x7FFF
379 static void splash_align_axis(int *axis, unsigned long panel_size,
380 unsigned long picture_size)
382 unsigned long panel_picture_delta = panel_size - picture_size;
383 unsigned long axis_alignment;
385 if (*axis == BMP_ALIGN_CENTER)
386 axis_alignment = panel_picture_delta / 2;
388 axis_alignment = panel_picture_delta + *axis + 1;
392 *axis = max(0, (int)axis_alignment);
396 #ifdef CONFIG_LCD_BMP_RLE8
397 #define BMP_RLE8_ESCAPE 0
398 #define BMP_RLE8_EOL 0
399 #define BMP_RLE8_EOBMP 1
400 #define BMP_RLE8_DELTA 2
402 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
406 *(*fbp)++ = cmap[*bmap++];
411 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
414 int cnt_8copy = cnt >> 3;
416 cnt -= cnt_8copy << 3;
417 while (cnt_8copy > 0) {
436 * Do not call this function directly, must be called from lcd_display_bitmap.
438 static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
439 int x_off, int y_off)
447 width = get_unaligned_le32(&bmp->header.width);
448 height = get_unaligned_le32(&bmp->header.height);
449 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
455 if (bmap[0] == BMP_RLE8_ESCAPE) {
462 /* 16bpix, 2-byte per pixel, width should *2 */
463 fb -= (width * 2 + lcd_line_length);
473 /* 16bpix, 2-byte per pixel, x should *2 */
474 fb = (uchar *) (lcd_base + (y + y_off - 1)
475 * lcd_line_length + (x + x_off) * 2);
484 if (x + runlen > width)
488 draw_unencoded_bitmap(
503 /* aggregate the same code */
504 while (bmap[0] == 0xff &&
505 bmap[2] != BMP_RLE8_ESCAPE &&
506 bmap[1] == bmap[3]) {
510 if (x + runlen > width)
514 draw_encoded_bitmap((ushort **)&fb,
525 __weak void fb_put_byte(uchar **fb, uchar **from)
527 *(*fb)++ = *(*from)++;
530 #if defined(CONFIG_BMP_16BPP)
531 __weak void fb_put_word(uchar **fb, uchar **from)
533 *(*fb)++ = *(*from)++;
534 *(*fb)++ = *(*from)++;
536 #endif /* CONFIG_BMP_16BPP */
538 __weak void lcd_set_cmap(bmp_image_t *bmp, unsigned colors)
541 bmp_color_table_entry_t cte;
542 ushort *cmap = configuration_get_cmap();
544 for (i = 0; i < colors; ++i) {
545 cte = bmp->color_table[i];
546 *cmap = (((cte.red) << 8) & 0xf800) |
547 (((cte.green) << 3) & 0x07e0) |
548 (((cte.blue) >> 3) & 0x001f);
549 #if defined(CONFIG_MPC823)
557 int lcd_display_bitmap(ulong bmp_image, int x, int y)
559 ushort *cmap_base = NULL;
562 bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
565 unsigned long width, height, byte_width;
566 unsigned long pwidth = panel_info.vl_col;
567 unsigned colors, bpix, bmp_bpix;
569 if (!bmp || !(bmp->header.signature[0] == 'B' &&
570 bmp->header.signature[1] == 'M')) {
571 printf("Error: no valid bmp image at %lx\n", bmp_image);
576 width = get_unaligned_le32(&bmp->header.width);
577 height = get_unaligned_le32(&bmp->header.height);
578 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
580 colors = 1 << bmp_bpix;
582 bpix = NBITS(panel_info.vl_bpix);
584 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
585 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
592 * We support displaying 8bpp BMPs on 16bpp LCDs
593 * and displaying 24bpp BMPs on 32bpp LCDs
595 if (bpix != bmp_bpix &&
596 !(bmp_bpix == 8 && bpix == 16) &&
597 !(bmp_bpix == 24 && bpix == 32)) {
598 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
599 bpix, get_unaligned_le16(&bmp->header.bit_count));
603 debug("Display-bmp: %d x %d with %d colors\n",
604 (int)width, (int)height, (int)colors);
607 lcd_set_cmap(bmp, colors);
609 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
611 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
612 splash_align_axis(&x, pwidth, width);
613 splash_align_axis(&y, panel_info.vl_row, height);
614 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
616 if ((x + width) > pwidth)
618 if ((y + height) > panel_info.vl_row)
619 height = panel_info.vl_row - y;
621 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
622 fb = (uchar *)(lcd_base +
623 (y + height - 1) * lcd_line_length + x * bpix / 8);
628 cmap_base = configuration_get_cmap();
629 #ifdef CONFIG_LCD_BMP_RLE8
630 u32 compression = get_unaligned_le32(&bmp->header.compression);
631 if (compression == BMP_BI_RLE8) {
633 /* TODO implement render code for bpix != 16 */
634 printf("Error: only support 16 bpix");
637 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
645 byte_width = width * 2;
647 for (i = 0; i < height; ++i) {
649 for (j = 0; j < width; j++) {
651 fb_put_byte(&fb, &bmap);
653 *(uint16_t *)fb = cmap_base[*(bmap++)];
654 fb += sizeof(uint16_t) / sizeof(*fb);
657 bmap += (padded_width - width);
658 fb -= byte_width + lcd_line_length;
662 #if defined(CONFIG_BMP_16BPP)
664 for (i = 0; i < height; ++i) {
666 for (j = 0; j < width; j++)
667 fb_put_word(&fb, &bmap);
669 bmap += (padded_width - width) * 2;
670 fb -= width * 2 + lcd_line_length;
673 #endif /* CONFIG_BMP_16BPP */
674 #if defined(CONFIG_BMP_24BMP)
676 for (i = 0; i < height; ++i) {
677 for (j = 0; j < width; j++) {
683 fb -= lcd_line_length + width * (bpix / 8);
686 #endif /* CONFIG_BMP_24BMP */
687 #if defined(CONFIG_BMP_32BPP)
689 for (i = 0; i < height; ++i) {
690 for (j = 0; j < width; j++) {
696 fb -= lcd_line_length + width * (bpix / 8);
699 #endif /* CONFIG_BMP_32BPP */
709 static void *lcd_logo(void)
711 #ifdef CONFIG_SPLASH_SCREEN
714 static int do_splash = 1;
716 if (do_splash && (s = getenv("splashimage")) != NULL) {
720 if (splash_screen_prepare())
721 return (void *)lcd_base;
723 addr = simple_strtoul (s, NULL, 16);
725 splash_get_pos(&x, &y);
727 if (bmp_display(addr, x, y) == 0)
728 return (void *)lcd_base;
730 #endif /* CONFIG_SPLASH_SCREEN */
734 #ifdef CONFIG_LCD_INFO
735 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
736 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
737 lcd_show_board_info();
738 #endif /* CONFIG_LCD_INFO */
740 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
741 return (void *)((ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length);
743 return (void *)lcd_base;
744 #endif /* CONFIG_LCD_LOGO && !defined(CONFIG_LCD_INFO_BELOW_LOGO) */
747 #ifdef CONFIG_SPLASHIMAGE_GUARD
748 static int on_splashimage(const char *name, const char *value, enum env_op op,
754 if (op == env_op_delete)
757 addr = simple_strtoul(value, NULL, 16);
758 /* See README.displaying-bmps */
759 aligned = (addr % 4 == 2);
761 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
768 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
771 int lcd_get_pixel_width(void)
773 return panel_info.vl_col;
776 int lcd_get_pixel_height(void)
778 return panel_info.vl_row;
781 #if defined(CONFIG_LCD_DT_SIMPLEFB)
782 static int lcd_dt_simplefb_configure_node(void *blob, int off)
784 #if LCD_BPP == LCD_COLOR16
785 return fdt_setup_simplefb_node(blob, off, gd->fb_base,
786 panel_info.vl_col, panel_info.vl_row,
787 panel_info.vl_col * 2, "r5g6b5");
793 int lcd_dt_simplefb_add_node(void *blob)
795 static const char compat[] = "simple-framebuffer";
796 static const char disabled[] = "disabled";
799 off = fdt_add_subnode(blob, 0, "framebuffer");
803 ret = fdt_setprop(blob, off, "status", disabled, sizeof(disabled));
807 ret = fdt_setprop(blob, off, "compatible", compat, sizeof(compat));
811 return lcd_dt_simplefb_configure_node(blob, off);
814 int lcd_dt_simplefb_enable_existing_node(void *blob)
818 off = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
822 return lcd_dt_simplefb_configure_node(blob, off);