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 __weak void fb_put_byte(uchar **fb, uchar **from)
352 *(*fb)++ = *(*from)++;
355 #if defined(CONFIG_BMP_16BPP)
356 __weak void fb_put_word(uchar **fb, uchar **from)
358 *(*fb)++ = *(*from)++;
359 *(*fb)++ = *(*from)++;
361 #endif /* CONFIG_BMP_16BPP */
363 __weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
366 struct bmp_color_table_entry cte;
367 ushort *cmap = configuration_get_cmap();
369 for (i = 0; i < colors; ++i) {
370 cte = bmp->color_table[i];
371 *cmap = (((cte.red) << 8) & 0xf800) |
372 (((cte.green) << 3) & 0x07e0) |
373 (((cte.blue) >> 3) & 0x001f);
378 int lcd_display_bitmap(ulong bmp_image, int x, int y)
380 ushort *cmap_base = NULL;
383 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
386 unsigned long width, height, byte_width;
387 unsigned long pwidth = panel_info.vl_col;
388 unsigned colors, bpix, bmp_bpix;
390 struct bmp_color_table_entry *palette;
392 if (!bmp || !(bmp->header.signature[0] == 'B' &&
393 bmp->header.signature[1] == 'M')) {
394 printf("Error: no valid bmp image at %lx\n", bmp_image);
399 palette = bmp->color_table;
400 width = get_unaligned_le32(&bmp->header.width);
401 height = get_unaligned_le32(&bmp->header.height);
402 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
403 hdr_size = get_unaligned_le16(&bmp->header.size);
404 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
406 colors = 1 << bmp_bpix;
408 bpix = NBITS(panel_info.vl_bpix);
410 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
411 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
418 * We support displaying 8bpp BMPs on 16bpp LCDs
419 * and displaying 24bpp BMPs on 32bpp LCDs
421 if (bpix != bmp_bpix &&
422 !(bmp_bpix == 8 && bpix == 16) &&
423 !(bmp_bpix == 24 && bpix == 32)) {
424 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
425 bpix, get_unaligned_le16(&bmp->header.bit_count));
429 debug("Display-bmp: %d x %d with %d colors, display %d\n",
430 (int)width, (int)height, (int)colors, 1 << bpix);
433 lcd_set_cmap(bmp, colors);
435 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
437 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
438 splash_align_axis(&x, pwidth, width);
439 splash_align_axis(&y, panel_info.vl_row, height);
440 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
442 if ((x + width) > pwidth)
444 if ((y + height) > panel_info.vl_row)
445 height = panel_info.vl_row - y;
447 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
448 fb = (uchar *)(lcd_base +
449 (y + height - 1) * lcd_line_length + x * bpix / 8);
454 cmap_base = configuration_get_cmap();
459 byte_width = width * 2;
461 for (i = 0; i < height; ++i) {
463 for (j = 0; j < width; j++) {
465 fb_put_byte(&fb, &bmap);
467 struct bmp_color_table_entry *entry;
471 val = cmap_base[*bmap];
473 entry = &palette[*bmap];
474 val = entry->blue >> 3 |
475 entry->green >> 2 << 5 |
476 entry->red >> 3 << 11;
478 *(uint16_t *)fb = val;
480 fb += sizeof(uint16_t) / sizeof(*fb);
483 bmap += (padded_width - width);
484 fb -= byte_width + lcd_line_length;
488 #if defined(CONFIG_BMP_16BPP)
490 for (i = 0; i < height; ++i) {
492 for (j = 0; j < width; j++)
493 fb_put_word(&fb, &bmap);
495 bmap += (padded_width - width) * 2;
496 fb -= width * 2 + lcd_line_length;
499 #endif /* CONFIG_BMP_16BPP */
500 #if defined(CONFIG_BMP_24BPP)
502 for (i = 0; i < height; ++i) {
503 for (j = 0; j < width; j++) {
509 fb -= lcd_line_length + width * (bpix / 8);
512 #endif /* CONFIG_BMP_24BPP */
513 #if defined(CONFIG_BMP_32BPP)
515 for (i = 0; i < height; ++i) {
516 for (j = 0; j < width; j++) {
522 fb -= lcd_line_length + width * (bpix / 8);
525 #endif /* CONFIG_BMP_32BPP */
535 static void lcd_logo(void)
539 #ifdef CONFIG_LCD_INFO
540 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
541 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
542 lcd_show_board_info();
543 #endif /* CONFIG_LCD_INFO */
546 #ifdef CONFIG_SPLASHIMAGE_GUARD
547 static int on_splashimage(const char *name, const char *value, enum env_op op,
553 if (op == env_op_delete)
556 addr = hextoul(value, NULL);
557 /* See README.displaying-bmps */
558 aligned = (addr % 4 == 2);
560 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
567 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
570 int lcd_get_pixel_width(void)
572 return panel_info.vl_col;
575 int lcd_get_pixel_height(void)
577 return panel_info.vl_row;