blob: 0898bc025d679758eb851372e35edca58f3582f4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk9ca7bbc2004-10-09 23:25:58 +00002/*
Nikita Kiryanovf54c0262015-02-03 13:32:29 +02003 * Common LCD routines
wdenk9ca7bbc2004-10-09 23:25:58 +00004 *
5 * (C) Copyright 2001-2002
6 * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
wdenk9ca7bbc2004-10-09 23:25:58 +00007 */
8
wdenk9ca7bbc2004-10-09 23:25:58 +00009/* #define DEBUG */
wdenk9ca7bbc2004-10-09 23:25:58 +000010#include <config.h>
11#include <common.h>
12#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Nikita Kiryanov2f3e2ca2013-02-24 21:28:43 +000014#include <env_callback.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass97589732020-05-10 11:40:02 -060017#include <init.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
wdenk9ca7bbc2004-10-09 23:25:58 +000019#include <linux/types.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020020#include <stdio_dev.h>
wdenk9ca7bbc2004-10-09 23:25:58 +000021#include <lcd.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050022#include <mapmem.h>
wdenkbc3202a2005-04-03 23:11:38 +000023#include <watchdog.h>
Przemyslaw Marczakc3bf15e2014-01-22 11:24:13 +010024#include <asm/unaligned.h>
Robert Winkler2a90ed02013-06-17 11:31:29 -070025#include <splash.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -070026#include <asm/io.h>
27#include <asm/unaligned.h>
Nikita Kiryanovf54c0262015-02-03 13:32:29 +020028#include <video_font.h>
Robert Winkler2a90ed02013-06-17 11:31:29 -070029
wdenk2b9d1862005-07-04 00:03:16 +000030#ifdef CONFIG_LCD_LOGO
Nikita Kiryanovf54c0262015-02-03 13:32:29 +020031#include <bmp_logo.h>
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
35#endif
wdenk2b9d1862005-07-04 00:03:16 +000036#endif
wdenk9ca7bbc2004-10-09 23:25:58 +000037
Simon Glass599a4df2012-10-17 13:24:54 +000038#ifndef CONFIG_LCD_ALIGNMENT
39#define CONFIG_LCD_ALIGNMENT PAGE_SIZE
40#endif
41
Nikita Kiryanov6c8cf8d2014-12-08 17:14:42 +020042#if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
43 (LCD_BPP != LCD_COLOR32)
Nikita Kiryanovf54c0262015-02-03 13:32:29 +020044#error Unsupported LCD BPP.
Jeroen Hofsteecdc348a2013-01-12 12:07:59 +000045#endif
46
Wolfgang Denk6405a152006-03-31 18:32:53 +020047DECLARE_GLOBAL_DATA_PTR;
wdenk9ca7bbc2004-10-09 23:25:58 +000048
Nikita Kiryanov1f828412012-05-24 01:42:38 +000049static int lcd_init(void *lcdbase);
Nikita Kiryanov1ae13d32015-02-03 13:32:32 +020050static void lcd_logo(void);
Nikita Kiryanov1f828412012-05-24 01:42:38 +000051static void lcd_setfgcolor(int color);
52static void lcd_setbgcolor(int color);
wdenk9ca7bbc2004-10-09 23:25:58 +000053
Wolfgang Denk67f53912013-01-05 09:45:48 +000054static int lcd_color_fg;
55static int lcd_color_bg;
Jeroen Hofsteeb9092e62013-01-22 10:44:11 +000056int lcd_line_length;
wdenk9ca7bbc2004-10-09 23:25:58 +000057char lcd_is_enabled = 0;
Jeroen Hofstee881c4ec2013-01-22 10:44:12 +000058static void *lcd_base; /* Start of framebuffer memory */
Simon Glassef45ffd2012-10-30 13:40:18 +000059static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
wdenk9ca7bbc2004-10-09 23:25:58 +000060
Simon Glassef45ffd2012-10-30 13:40:18 +000061/* Flush LCD activity to the caches */
62void lcd_sync(void)
63{
64 /*
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.
68 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -040069#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glassef45ffd2012-10-30 13:40:18 +000070 int line_length;
71
72 if (lcd_flush_dcache)
Alexander Grafd2dc92e2016-03-16 15:41:22 +010073 flush_dcache_range((ulong)lcd_base,
74 (ulong)(lcd_base + lcd_get_size(&line_length)));
Simon Glassef45ffd2012-10-30 13:40:18 +000075#endif
76}
77
78void lcd_set_flush_dcache(int flush)
79{
80 lcd_flush_dcache = (flush != 0);
81}
82
Simon Glass0d1e1f72014-07-23 06:54:59 -060083static void lcd_stub_putc(struct stdio_dev *dev, const char c)
84{
85 lcd_putc(c);
86}
87
Simon Glass0d1e1f72014-07-23 06:54:59 -060088static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
89{
90 lcd_puts(s);
91}
92
Anatolij Gustschinfa50d902013-11-09 11:00:09 +010093/*
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.
98 */
99__weak int lcd_get_size(int *line_length)
Simon Glass599a4df2012-10-17 13:24:54 +0000100{
101 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
102 return *line_length * panel_info.vl_row;
103}
104
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000105int drv_lcd_init(void)
wdenk9ca7bbc2004-10-09 23:25:58 +0000106{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200107 struct stdio_dev lcddev;
wdenk9ca7bbc2004-10-09 23:25:58 +0000108 int rc;
109
Simon Glassb9ddbf42014-02-27 13:26:19 -0700110 lcd_base = map_sysmem(gd->fb_base, 0);
wdenk9ca7bbc2004-10-09 23:25:58 +0000111
Nikita Kiryanovf54c0262015-02-03 13:32:29 +0200112 lcd_init(lcd_base);
wdenk9ca7bbc2004-10-09 23:25:58 +0000113
114 /* Device initialization */
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000115 memset(&lcddev, 0, sizeof(lcddev));
wdenk9ca7bbc2004-10-09 23:25:58 +0000116
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000117 strcpy(lcddev.name, "lcd");
wdenk9ca7bbc2004-10-09 23:25:58 +0000118 lcddev.ext = 0; /* No extensions */
119 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600120 lcddev.putc = lcd_stub_putc; /* 'putc' function */
121 lcddev.puts = lcd_stub_puts; /* 'puts' function */
wdenk9ca7bbc2004-10-09 23:25:58 +0000122
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000123 rc = stdio_register(&lcddev);
wdenk9ca7bbc2004-10-09 23:25:58 +0000124
125 return (rc == 0) ? 1 : rc;
126}
127
Che-Liang Chiou2444b172011-10-20 23:07:03 +0000128void lcd_clear(void)
wdenk9ca7bbc2004-10-09 23:25:58 +0000129{
Nikita Kiryanov631d2e12014-12-08 17:14:43 +0200130 int bg_color;
Igor Opaniuk1f6bfad2019-05-29 09:01:43 +0000131 __maybe_unused ulong addr;
Nikita Kiryanov1ae13d32015-02-03 13:32:32 +0200132 static int do_splash = 1;
Nikita Kiryanov5aec5812014-12-08 17:14:38 +0200133#if LCD_BPP == LCD_COLOR8
wdenk9ca7bbc2004-10-09 23:25:58 +0000134 /* Setting the palette */
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000135 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);
wdenk9ca7bbc2004-10-09 23:25:58 +0000144#endif
145
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200146#ifndef CONFIG_SYS_WHITE_ON_BLACK
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000147 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
148 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
Nikita Kiryanov631d2e12014-12-08 17:14:43 +0200149 bg_color = CONSOLE_COLOR_WHITE;
wdenk9ca7bbc2004-10-09 23:25:58 +0000150#else
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000151 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
152 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
Nikita Kiryanov631d2e12014-12-08 17:14:43 +0200153 bg_color = CONSOLE_COLOR_BLACK;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200154#endif /* CONFIG_SYS_WHITE_ON_BLACK */
wdenk9ca7bbc2004-10-09 23:25:58 +0000155
wdenk9ca7bbc2004-10-09 23:25:58 +0000156 /* set framebuffer to background color */
Hannes Petermaier95f87df2014-03-07 18:55:40 +0100157#if (LCD_BPP != LCD_COLOR32)
Nikita Kiryanov631d2e12014-12-08 17:14:43 +0200158 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
Hannes Petermaier95f87df2014-03-07 18:55:40 +0100159#else
160 u32 *ppix = lcd_base;
161 u32 i;
162 for (i = 0;
163 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
164 i++) {
Nikita Kiryanov631d2e12014-12-08 17:14:43 +0200165 *ppix++ = bg_color;
Hannes Petermaier95f87df2014-03-07 18:55:40 +0100166 }
167#endif
Hannes Petermaiera3c8e862015-03-27 08:01:38 +0100168 /* setup text-console */
169 debug("[LCD] setting up console...\n");
170 lcd_init_console(lcd_base,
171 panel_info.vl_col,
172 panel_info.vl_row,
173 panel_info.vl_rot);
wdenk9ca7bbc2004-10-09 23:25:58 +0000174 /* Paint the logo and retrieve LCD base address */
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000175 debug("[LCD] Drawing the logo...\n");
Nikita Kiryanov1ae13d32015-02-03 13:32:32 +0200176 if (do_splash) {
Igor Opaniuk1f6bfad2019-05-29 09:01:43 +0000177 if (splash_display() == 0) {
Nikita Kiryanov1ae13d32015-02-03 13:32:32 +0200178 do_splash = 0;
Igor Opaniuk1f6bfad2019-05-29 09:01:43 +0000179 lcd_sync();
180 return;
Nikita Kiryanov1ae13d32015-02-03 13:32:32 +0200181 }
182 }
183
184 lcd_logo();
185#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
186 addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
Marcel Ziswiler39a62072015-08-04 15:49:50 +0200187 lcd_init_console((void *)addr, panel_info.vl_col,
188 panel_info.vl_row, panel_info.vl_rot);
Nikita Kiryanov1ae13d32015-02-03 13:32:32 +0200189#endif
Simon Glassef45ffd2012-10-30 13:40:18 +0000190 lcd_sync();
wdenk9ca7bbc2004-10-09 23:25:58 +0000191}
192
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000193static int lcd_init(void *lcdbase)
wdenk9ca7bbc2004-10-09 23:25:58 +0000194{
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000195 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000196 lcd_ctrl_init(lcdbase);
Anatolij Gustschin02591592013-03-29 14:00:13 +0100197
198 /*
Stephen Warren682b6b52014-11-19 20:41:03 -0700199 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
Anatolij Gustschin02591592013-03-29 14:00:13 +0100200 * 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.
203 */
Simon Glassb9ddbf42014-02-27 13:26:19 -0700204 if (map_to_sysmem(lcdbase) != gd->fb_base)
205 lcd_base = map_sysmem(gd->fb_base, 0);
Anatolij Gustschin02591592013-03-29 14:00:13 +0100206
207 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
208
Stephen Warrenefc8f792013-01-29 16:37:38 +0000209 lcd_get_size(&lcd_line_length);
Haavard Skinnemoen7eb8b232008-09-01 16:21:21 +0200210 lcd_is_enabled = 1;
Che-Liang Chiou2444b172011-10-20 23:07:03 +0000211 lcd_clear();
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000212 lcd_enable();
wdenk9ca7bbc2004-10-09 23:25:58 +0000213
214 /* Initialize the console */
Nikita Kiryanov699c05e2014-12-08 17:14:41 +0200215 lcd_set_col(0);
wdenk2b9d1862005-07-04 00:03:16 +0000216#ifdef CONFIG_LCD_INFO_BELOW_LOGO
Nikita Kiryanov699c05e2014-12-08 17:14:41 +0200217 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
wdenk9ca7bbc2004-10-09 23:25:58 +0000218#else
Nikita Kiryanov699c05e2014-12-08 17:14:41 +0200219 lcd_set_row(1); /* leave 1 blank line below logo */
wdenk9ca7bbc2004-10-09 23:25:58 +0000220#endif
wdenk9ca7bbc2004-10-09 23:25:58 +0000221
222 return 0;
223}
224
wdenk9ca7bbc2004-10-09 23:25:58 +0000225/*
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
229 *
230 * Note that this is running from ROM, so no write access to global data.
231 */
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000232ulong lcd_setmem(ulong addr)
wdenk9ca7bbc2004-10-09 23:25:58 +0000233{
234 ulong size;
Simon Glass599a4df2012-10-17 13:24:54 +0000235 int line_length;
wdenk9ca7bbc2004-10-09 23:25:58 +0000236
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000237 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));
wdenk9ca7bbc2004-10-09 23:25:58 +0000239
Simon Glass599a4df2012-10-17 13:24:54 +0000240 size = lcd_get_size(&line_length);
wdenk9ca7bbc2004-10-09 23:25:58 +0000241
Simon Glass599a4df2012-10-17 13:24:54 +0000242 /* 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);
wdenk9ca7bbc2004-10-09 23:25:58 +0000245
246 /* Allocate pages for the frame buffer. */
247 addr -= size;
248
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000249 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
250 size >> 10, addr);
wdenk9ca7bbc2004-10-09 23:25:58 +0000251
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000252 return addr;
wdenk9ca7bbc2004-10-09 23:25:58 +0000253}
254
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000255static void lcd_setfgcolor(int color)
wdenk9ca7bbc2004-10-09 23:25:58 +0000256{
Stelian Popf6f86652008-05-09 21:57:18 +0200257 lcd_color_fg = color;
wdenk9ca7bbc2004-10-09 23:25:58 +0000258}
259
Nikita Kiryanov631d2e12014-12-08 17:14:43 +0200260int lcd_getfgcolor(void)
261{
262 return lcd_color_fg;
263}
264
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000265static void lcd_setbgcolor(int color)
wdenk9ca7bbc2004-10-09 23:25:58 +0000266{
Stelian Popf6f86652008-05-09 21:57:18 +0200267 lcd_color_bg = color;
wdenk9ca7bbc2004-10-09 23:25:58 +0000268}
269
Nikita Kiryanov631d2e12014-12-08 17:14:43 +0200270int lcd_getbgcolor(void)
271{
272 return lcd_color_bg;
273}
274
wdenk9ca7bbc2004-10-09 23:25:58 +0000275#ifdef CONFIG_LCD_LOGO
Nikita Kiryanov7f45a7e2015-02-03 13:32:24 +0200276__weak void lcd_logo_set_cmap(void)
277{
Nikita Kiryanov70080dc2015-02-03 13:32:26 +0200278 int i;
279 ushort *cmap = configuration_get_cmap();
280
281 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
282 *cmap++ = bmp_logo_palette[i];
Nikita Kiryanov7f45a7e2015-02-03 13:32:24 +0200283}
284
Nikita Kiryanov618212c2015-02-03 13:32:30 +0200285void lcd_logo_plot(int x, int y)
wdenk9ca7bbc2004-10-09 23:25:58 +0000286{
wdenk9ca7bbc2004-10-09 23:25:58 +0000287 ushort i, j;
Nikita Kiryanovf54c0262015-02-03 13:32:29 +0200288 uchar *bmap = &bmp_logo_bitmap[0];
Andre Renaudada32ec2013-02-13 17:48:00 +0000289 unsigned bpix = NBITS(panel_info.vl_bpix);
Nikita Kiryanovf54c0262015-02-03 13:32:29 +0200290 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
291 ushort *fb16;
wdenk9ca7bbc2004-10-09 23:25:58 +0000292
Nikita Kiryanov70080dc2015-02-03 13:32:26 +0200293 debug("Logo: width %d height %d colors %d\n",
294 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
wdenk9ca7bbc2004-10-09 23:25:58 +0000295
Andre Renaudada32ec2013-02-13 17:48:00 +0000296 if (bpix < 12) {
wdenk9ca7bbc2004-10-09 23:25:58 +0000297 WATCHDOG_RESET();
Nikita Kiryanov7f45a7e2015-02-03 13:32:24 +0200298 lcd_logo_set_cmap();
wdenk9ca7bbc2004-10-09 23:25:58 +0000299 WATCHDOG_RESET();
300
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000301 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
302 memcpy(fb, bmap, BMP_LOGO_WIDTH);
wdenk9ca7bbc2004-10-09 23:25:58 +0000303 bmap += BMP_LOGO_WIDTH;
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000304 fb += panel_info.vl_col;
wdenk9ca7bbc2004-10-09 23:25:58 +0000305 }
306 }
307 else { /* true color mode */
Alessandro Rubini2d68c262010-03-13 17:44:08 +0100308 u16 col16;
Andre Renaudada32ec2013-02-13 17:48:00 +0000309 fb16 = (ushort *)fb;
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000310 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
311 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
Alessandro Rubini2d68c262010-03-13 17:44:08 +0100312 col16 = bmp_logo_palette[(bmap[j]-16)];
313 fb16[j] =
314 ((col16 & 0x000F) << 1) |
315 ((col16 & 0x00F0) << 3) |
316 ((col16 & 0x0F00) << 4);
wdenk9ca7bbc2004-10-09 23:25:58 +0000317 }
318 bmap += BMP_LOGO_WIDTH;
319 fb16 += panel_info.vl_col;
320 }
321 }
322
323 WATCHDOG_RESET();
Simon Glassef45ffd2012-10-30 13:40:18 +0000324 lcd_sync();
wdenk9ca7bbc2004-10-09 23:25:58 +0000325}
Anatolij Gustschin6f1378f2012-04-27 04:41:27 +0000326#else
Nikita Kiryanov618212c2015-02-03 13:32:30 +0200327static inline void lcd_logo_plot(int x, int y) {}
wdenk9ca7bbc2004-10-09 23:25:58 +0000328#endif /* CONFIG_LCD_LOGO */
329
Jon Loeliger052fc842007-07-08 18:10:08 -0500330#if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
Matthias Weisser53884182009-07-09 16:07:30 +0200331#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanovb4eb54d2012-08-09 00:14:51 +0000332
333static void splash_align_axis(int *axis, unsigned long panel_size,
334 unsigned long picture_size)
335{
336 unsigned long panel_picture_delta = panel_size - picture_size;
337 unsigned long axis_alignment;
338
339 if (*axis == BMP_ALIGN_CENTER)
340 axis_alignment = panel_picture_delta / 2;
341 else if (*axis < 0)
342 axis_alignment = panel_picture_delta + *axis + 1;
343 else
344 return;
345
Masahiro Yamadadb204642014-11-07 03:03:31 +0900346 *axis = max(0, (int)axis_alignment);
Nikita Kiryanovb4eb54d2012-08-09 00:14:51 +0000347}
Matthias Weisser53884182009-07-09 16:07:30 +0200348#endif
349
Nikita Kiryanovc999bcc2015-02-03 13:32:23 +0200350__weak void fb_put_byte(uchar **fb, uchar **from)
351{
352 *(*fb)++ = *(*from)++;
353}
Nikita Kiryanovb4571182012-08-09 00:14:53 +0000354
355#if defined(CONFIG_BMP_16BPP)
Nikita Kiryanovc6cc0652015-02-03 13:32:22 +0200356__weak void fb_put_word(uchar **fb, uchar **from)
Nikita Kiryanovb4571182012-08-09 00:14:53 +0000357{
358 *(*fb)++ = *(*from)++;
359 *(*fb)++ = *(*from)++;
360}
Nikita Kiryanovb4571182012-08-09 00:14:53 +0000361#endif /* CONFIG_BMP_16BPP */
362
Simon Glass92e1f852015-05-13 07:02:27 -0600363__weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
Nikita Kiryanov1dce1e72015-02-03 13:32:27 +0200364{
365 int i;
Simon Glass92e1f852015-05-13 07:02:27 -0600366 struct bmp_color_table_entry cte;
Nikita Kiryanov1dce1e72015-02-03 13:32:27 +0200367 ushort *cmap = configuration_get_cmap();
368
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);
Nikita Kiryanov1dce1e72015-02-03 13:32:27 +0200374 cmap++;
Nikita Kiryanov1dce1e72015-02-03 13:32:27 +0200375 }
376}
377
wdenk9ca7bbc2004-10-09 23:25:58 +0000378int lcd_display_bitmap(ulong bmp_image, int x, int y)
379{
Anatolij Gustschina11ca282009-02-25 20:28:13 +0100380 ushort *cmap_base = NULL;
wdenk9ca7bbc2004-10-09 23:25:58 +0000381 ushort i, j;
382 uchar *fb;
Simon Glass92e1f852015-05-13 07:02:27 -0600383 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
wdenk9ca7bbc2004-10-09 23:25:58 +0000384 uchar *bmap;
Tom Wai-Hong Tam93f04d62012-09-28 15:11:14 +0000385 ushort padded_width;
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100386 unsigned long width, height, byte_width;
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200387 unsigned long pwidth = panel_info.vl_col;
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100388 unsigned colors, bpix, bmp_bpix;
Simon Glass8980cc02015-05-13 07:02:28 -0600389 int hdr_size;
xypron.glpk@gmx.defb9fc962017-07-30 21:59:23 +0200390 struct bmp_color_table_entry *palette;
wdenk9ca7bbc2004-10-09 23:25:58 +0000391
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000392 if (!bmp || !(bmp->header.signature[0] == 'B' &&
393 bmp->header.signature[1] == 'M')) {
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000394 printf("Error: no valid bmp image at %lx\n", bmp_image);
395
wdenk9ca7bbc2004-10-09 23:25:58 +0000396 return 1;
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100397 }
wdenk9ca7bbc2004-10-09 23:25:58 +0000398
xypron.glpk@gmx.defb9fc962017-07-30 21:59:23 +0200399 palette = bmp->color_table;
Przemyslaw Marczakc3bf15e2014-01-22 11:24:13 +0100400 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);
Simon Glass8980cc02015-05-13 07:02:28 -0600403 hdr_size = get_unaligned_le16(&bmp->header.size);
404 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
Przemyslaw Marczakc3bf15e2014-01-22 11:24:13 +0100405
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100406 colors = 1 << bmp_bpix;
wdenk9ca7bbc2004-10-09 23:25:58 +0000407
408 bpix = NBITS(panel_info.vl_bpix);
409
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000410 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100411 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
412 bpix, bmp_bpix);
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000413
wdenk9ca7bbc2004-10-09 23:25:58 +0000414 return 1;
415 }
416
Hannes Petermaier14b7eed2014-07-15 16:28:46 +0200417 /*
418 * We support displaying 8bpp BMPs on 16bpp LCDs
419 * and displaying 24bpp BMPs on 32bpp LCDs
420 * */
421 if (bpix != bmp_bpix &&
422 !(bmp_bpix == 8 && bpix == 16) &&
423 !(bmp_bpix == 24 && bpix == 32)) {
wdenk9ca7bbc2004-10-09 23:25:58 +0000424 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Przemyslaw Marczakc3bf15e2014-01-22 11:24:13 +0100425 bpix, get_unaligned_le16(&bmp->header.bit_count));
wdenk9ca7bbc2004-10-09 23:25:58 +0000426 return 1;
427 }
428
Simon Glass8980cc02015-05-13 07:02:28 -0600429 debug("Display-bmp: %d x %d with %d colors, display %d\n",
430 (int)width, (int)height, (int)colors, 1 << bpix);
wdenk9ca7bbc2004-10-09 23:25:58 +0000431
Nikita Kiryanov1dce1e72015-02-03 13:32:27 +0200432 if (bmp_bpix == 8)
433 lcd_set_cmap(bmp, colors);
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200434
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000435 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
Matthias Weisser53884182009-07-09 16:07:30 +0200436
437#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanovb4eb54d2012-08-09 00:14:51 +0000438 splash_align_axis(&x, pwidth, width);
439 splash_align_axis(&y, panel_info.vl_row, height);
Matthias Weisser53884182009-07-09 16:07:30 +0200440#endif /* CONFIG_SPLASH_SCREEN_ALIGN */
441
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000442 if ((x + width) > pwidth)
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200443 width = pwidth - x;
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000444 if ((y + height) > panel_info.vl_row)
wdenk9ca7bbc2004-10-09 23:25:58 +0000445 height = panel_info.vl_row - y;
446
Przemyslaw Marczakc3bf15e2014-01-22 11:24:13 +0100447 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
448 fb = (uchar *)(lcd_base +
Liu Ying1bb98712011-01-11 15:29:58 +0800449 (y + height - 1) * lcd_line_length + x * bpix / 8);
Mark Jackson7b298f32009-02-06 10:37:49 +0100450
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100451 switch (bmp_bpix) {
Nikita Kiryanovf54c0262015-02-03 13:32:29 +0200452 case 1:
Simon Glassf461f122014-10-15 04:53:04 -0600453 case 8: {
Nikita Kiryanov1dce1e72015-02-03 13:32:27 +0200454 cmap_base = configuration_get_cmap();
Tom Wai-Hong Tam79926a42012-09-28 15:11:16 +0000455
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100456 if (bpix != 16)
457 byte_width = width;
458 else
459 byte_width = width * 2;
460
Mark Jackson7b298f32009-02-06 10:37:49 +0100461 for (i = 0; i < height; ++i) {
462 WATCHDOG_RESET();
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100463 for (j = 0; j < width; j++) {
464 if (bpix != 16) {
Nikita Kiryanovc999bcc2015-02-03 13:32:23 +0200465 fb_put_byte(&fb, &bmap);
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100466 } else {
Simon Glass8980cc02015-05-13 07:02:28 -0600467 struct bmp_color_table_entry *entry;
468 uint val;
469
470 if (cmap_base) {
471 val = cmap_base[*bmap];
472 } else {
473 entry = &palette[*bmap];
474 val = entry->blue >> 3 |
475 entry->green >> 2 << 5 |
476 entry->red >> 3 << 11;
477 }
478 *(uint16_t *)fb = val;
479 bmap++;
Guennadi Liakhovetski332d0b52009-02-06 10:37:53 +0100480 fb += sizeof(uint16_t) / sizeof(*fb);
481 }
482 }
Tom Wai-Hong Tam93f04d62012-09-28 15:11:14 +0000483 bmap += (padded_width - width);
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000484 fb -= byte_width + lcd_line_length;
Mark Jackson7b298f32009-02-06 10:37:49 +0100485 }
486 break;
Simon Glassf461f122014-10-15 04:53:04 -0600487 }
Mark Jackson7b298f32009-02-06 10:37:49 +0100488#if defined(CONFIG_BMP_16BPP)
489 case 16:
490 for (i = 0; i < height; ++i) {
491 WATCHDOG_RESET();
Nikita Kiryanovb4571182012-08-09 00:14:53 +0000492 for (j = 0; j < width; j++)
493 fb_put_word(&fb, &bmap);
494
Tom Wai-Hong Tam93f04d62012-09-28 15:11:14 +0000495 bmap += (padded_width - width) * 2;
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000496 fb -= width * 2 + lcd_line_length;
Mark Jackson7b298f32009-02-06 10:37:49 +0100497 }
498 break;
499#endif /* CONFIG_BMP_16BPP */
Philipp Tomsich5cfa3f42017-05-05 21:48:30 +0200500#if defined(CONFIG_BMP_24BPP)
Hannes Petermaier14b7eed2014-07-15 16:28:46 +0200501 case 24:
502 for (i = 0; i < height; ++i) {
503 for (j = 0; j < width; j++) {
504 *(fb++) = *(bmap++);
505 *(fb++) = *(bmap++);
506 *(fb++) = *(bmap++);
507 *(fb++) = 0;
508 }
509 fb -= lcd_line_length + width * (bpix / 8);
510 }
511 break;
Philipp Tomsich5cfa3f42017-05-05 21:48:30 +0200512#endif /* CONFIG_BMP_24BPP */
Donghwa Leebf345a12012-05-09 19:23:37 +0000513#if defined(CONFIG_BMP_32BPP)
514 case 32:
515 for (i = 0; i < height; ++i) {
516 for (j = 0; j < width; j++) {
517 *(fb++) = *(bmap++);
518 *(fb++) = *(bmap++);
519 *(fb++) = *(bmap++);
520 *(fb++) = *(bmap++);
521 }
Jeroen Hofstee729b09e2013-01-12 12:07:56 +0000522 fb -= lcd_line_length + width * (bpix / 8);
Donghwa Leebf345a12012-05-09 19:23:37 +0000523 }
524 break;
525#endif /* CONFIG_BMP_32BPP */
Mark Jackson7b298f32009-02-06 10:37:49 +0100526 default:
527 break;
528 };
wdenk9ca7bbc2004-10-09 23:25:58 +0000529
Simon Glassef45ffd2012-10-30 13:40:18 +0000530 lcd_sync();
Nikita Kiryanov1f828412012-05-24 01:42:38 +0000531 return 0;
wdenk9ca7bbc2004-10-09 23:25:58 +0000532}
Jon Loeliger052fc842007-07-08 18:10:08 -0500533#endif
wdenk9ca7bbc2004-10-09 23:25:58 +0000534
Nikita Kiryanov1ae13d32015-02-03 13:32:32 +0200535static void lcd_logo(void)
wdenk9ca7bbc2004-10-09 23:25:58 +0000536{
Nikita Kiryanov618212c2015-02-03 13:32:30 +0200537 lcd_logo_plot(0, 0);
wdenk9ca7bbc2004-10-09 23:25:58 +0000538
Haavard Skinnemoenddbcf952008-09-01 16:21:22 +0200539#ifdef CONFIG_LCD_INFO
Nikita Kiryanov699c05e2014-12-08 17:14:41 +0200540 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
541 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
Haavard Skinnemoenddbcf952008-09-01 16:21:22 +0200542 lcd_show_board_info();
543#endif /* CONFIG_LCD_INFO */
wdenk9ca7bbc2004-10-09 23:25:58 +0000544}
545
Nikita Kiryanov2f3e2ca2013-02-24 21:28:43 +0000546#ifdef CONFIG_SPLASHIMAGE_GUARD
547static int on_splashimage(const char *name, const char *value, enum env_op op,
548 int flags)
549{
550 ulong addr;
551 int aligned;
552
553 if (op == env_op_delete)
554 return 0;
555
Simon Glass3ff49ec2021-07-24 09:03:29 -0600556 addr = hextoul(value, NULL);
Nikita Kiryanov2f3e2ca2013-02-24 21:28:43 +0000557 /* See README.displaying-bmps */
558 aligned = (addr % 4 == 2);
559 if (!aligned) {
560 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
561 return -1;
562 }
563
564 return 0;
565}
566
567U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
568#endif
569
Vadim Bendeburya7eaa2f2012-09-28 15:11:13 +0000570int lcd_get_pixel_width(void)
571{
572 return panel_info.vl_col;
573}
574
575int lcd_get_pixel_height(void)
576{
577 return panel_info.vl_row;
578}