blob: bd2f020d5dac90d4a45c9a1c1c12d47ae0302ec0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk8655b6f2004-10-09 23:25:58 +00002/*
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +02003 * Common LCD routines
wdenk8655b6f2004-10-09 23:25:58 +00004 *
5 * (C) Copyright 2001-2002
6 * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
wdenk8655b6f2004-10-09 23:25:58 +00007 */
8
wdenk8655b6f2004-10-09 23:25:58 +00009/* #define DEBUG */
wdenk8655b6f2004-10-09 23:25:58 +000010#include <config.h>
11#include <common.h>
12#include <command.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070013#include <cpu_func.h>
Nikita Kiryanovc0880482013-02-24 21:28:43 +000014#include <env_callback.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass691d7192020-05-10 11:40:02 -060017#include <init.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
wdenk8655b6f2004-10-09 23:25:58 +000019#include <linux/types.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020020#include <stdio_dev.h>
wdenk8655b6f2004-10-09 23:25:58 +000021#include <lcd.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050022#include <mapmem.h>
wdenk8b0bfc62005-04-03 23:11:38 +000023#include <watchdog.h>
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +010024#include <asm/unaligned.h>
Robert Winklerdd4425e2013-06-17 11:31:29 -070025#include <splash.h>
Simon Glass7d95f2a2014-02-27 13:26:19 -070026#include <asm/io.h>
27#include <asm/unaligned.h>
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020028#include <video_font.h>
Robert Winklerdd4425e2013-06-17 11:31:29 -070029
wdenk88804d12005-07-04 00:03:16 +000030#ifdef CONFIG_LCD_LOGO
Nikita Kiryanovc8d2feb2015-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
wdenk88804d12005-07-04 00:03:16 +000036#endif
wdenk8655b6f2004-10-09 23:25:58 +000037
Simon Glass676d3192012-10-17 13:24:54 +000038#ifndef CONFIG_LCD_ALIGNMENT
39#define CONFIG_LCD_ALIGNMENT PAGE_SIZE
40#endif
41
Nikita Kiryanova7de2952014-12-08 17:14:42 +020042#if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
43 (LCD_BPP != LCD_COLOR32)
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020044#error Unsupported LCD BPP.
Jeroen Hofsteea5796c52013-01-12 12:07:59 +000045#endif
46
Wolfgang Denkd87080b2006-03-31 18:32:53 +020047DECLARE_GLOBAL_DATA_PTR;
wdenk8655b6f2004-10-09 23:25:58 +000048
Nikita Kiryanov8f47d912012-05-24 01:42:38 +000049static int lcd_init(void *lcdbase);
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +020050static void lcd_logo(void);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +000051static void lcd_setfgcolor(int color);
52static void lcd_setbgcolor(int color);
wdenk8655b6f2004-10-09 23:25:58 +000053
Wolfgang Denk46d1d5d2013-01-05 09:45:48 +000054static int lcd_color_fg;
55static int lcd_color_bg;
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +000056int lcd_line_length;
wdenk8655b6f2004-10-09 23:25:58 +000057char lcd_is_enabled = 0;
Jeroen Hofstee00a0ca52013-01-22 10:44:12 +000058static void *lcd_base; /* Start of framebuffer memory */
Simon Glass9a8efc42012-10-30 13:40:18 +000059static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
60
Simon Glass9a8efc42012-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 Woerner10015022019-05-03 09:41:00 -040069#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass9a8efc42012-10-30 13:40:18 +000070 int line_length;
71
72 if (lcd_flush_dcache)
Alexander Graff8f58fb2016-03-16 15:41:22 +010073 flush_dcache_range((ulong)lcd_base,
74 (ulong)(lcd_base + lcd_get_size(&line_length)));
Simon Glass9a8efc42012-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 Glass709ea542014-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 Glass709ea542014-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 Gustschincefa4712013-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 Glass676d3192012-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 Hofstee6b035142013-01-12 12:07:56 +0000105int drv_lcd_init(void)
wdenk8655b6f2004-10-09 23:25:58 +0000106{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200107 struct stdio_dev lcddev;
wdenk8655b6f2004-10-09 23:25:58 +0000108 int rc;
109
Simon Glass7d95f2a2014-02-27 13:26:19 -0700110 lcd_base = map_sysmem(gd->fb_base, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000111
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200112 lcd_init(lcd_base);
wdenk8655b6f2004-10-09 23:25:58 +0000113
114 /* Device initialization */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000115 memset(&lcddev, 0, sizeof(lcddev));
wdenk8655b6f2004-10-09 23:25:58 +0000116
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000117 strcpy(lcddev.name, "lcd");
wdenk8655b6f2004-10-09 23:25:58 +0000118 lcddev.ext = 0; /* No extensions */
119 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
Simon Glass709ea542014-07-23 06:54:59 -0600120 lcddev.putc = lcd_stub_putc; /* 'putc' function */
121 lcddev.puts = lcd_stub_puts; /* 'puts' function */
wdenk8655b6f2004-10-09 23:25:58 +0000122
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000123 rc = stdio_register(&lcddev);
wdenk8655b6f2004-10-09 23:25:58 +0000124
125 return (rc == 0) ? 1 : rc;
126}
127
Che-Liang Chiou02110902011-10-20 23:07:03 +0000128void lcd_clear(void)
wdenk8655b6f2004-10-09 23:25:58 +0000129{
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200130 int bg_color;
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000131 __maybe_unused ulong addr;
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200132 static int do_splash = 1;
Nikita Kiryanovf4469f52014-12-08 17:14:38 +0200133#if LCD_BPP == LCD_COLOR8
wdenk8655b6f2004-10-09 23:25:58 +0000134 /* Setting the palette */
Nikita Kiryanov8f47d912012-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);
wdenk8655b6f2004-10-09 23:25:58 +0000144#endif
145
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200146#ifndef CONFIG_SYS_WHITE_ON_BLACK
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000147 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
148 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200149 bg_color = CONSOLE_COLOR_WHITE;
wdenk8655b6f2004-10-09 23:25:58 +0000150#else
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000151 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
152 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200153 bg_color = CONSOLE_COLOR_BLACK;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154#endif /* CONFIG_SYS_WHITE_ON_BLACK */
wdenk8655b6f2004-10-09 23:25:58 +0000155
wdenk8655b6f2004-10-09 23:25:58 +0000156 /* set framebuffer to background color */
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100157#if (LCD_BPP != LCD_COLOR32)
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200158 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
Hannes Petermaier57d76a82014-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 Kiryanov4d036342014-12-08 17:14:43 +0200165 *ppix++ = bg_color;
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100166 }
167#endif
Hannes Petermaier604c7d42015-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);
wdenk8655b6f2004-10-09 23:25:58 +0000174 /* Paint the logo and retrieve LCD base address */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000175 debug("[LCD] Drawing the logo...\n");
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200176 if (do_splash) {
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000177 if (splash_display() == 0) {
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200178 do_splash = 0;
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000179 lcd_sync();
180 return;
Nikita Kiryanov7bf71d12015-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 Ziswiler3b96b902015-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 Kiryanov7bf71d12015-02-03 13:32:32 +0200189#endif
Simon Glass9a8efc42012-10-30 13:40:18 +0000190 lcd_sync();
191}
192
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000193static int lcd_init(void *lcdbase)
wdenk8655b6f2004-10-09 23:25:58 +0000194{
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000195 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000196 lcd_ctrl_init(lcdbase);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100197
198 /*
Stephen Warren9316e142014-11-19 20:41:03 -0700199 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
Anatolij Gustschin1d3dea12013-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 Glass7d95f2a2014-02-27 13:26:19 -0700204 if (map_to_sysmem(lcdbase) != gd->fb_base)
205 lcd_base = map_sysmem(gd->fb_base, 0);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100206
207 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
208
Stephen Warren6d330712013-01-29 16:37:38 +0000209 lcd_get_size(&lcd_line_length);
Haavard Skinnemoen6f93d2b2008-09-01 16:21:21 +0200210 lcd_is_enabled = 1;
Che-Liang Chiou02110902011-10-20 23:07:03 +0000211 lcd_clear();
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000212 lcd_enable();
wdenk8655b6f2004-10-09 23:25:58 +0000213
214 /* Initialize the console */
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200215 lcd_set_col(0);
wdenk88804d12005-07-04 00:03:16 +0000216#ifdef CONFIG_LCD_INFO_BELOW_LOGO
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200217 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
wdenk8655b6f2004-10-09 23:25:58 +0000218#else
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200219 lcd_set_row(1); /* leave 1 blank line below logo */
wdenk8655b6f2004-10-09 23:25:58 +0000220#endif
wdenk8655b6f2004-10-09 23:25:58 +0000221
222 return 0;
223}
224
wdenk8655b6f2004-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 Kiryanov8f47d912012-05-24 01:42:38 +0000232ulong lcd_setmem(ulong addr)
wdenk8655b6f2004-10-09 23:25:58 +0000233{
234 ulong size;
Simon Glass676d3192012-10-17 13:24:54 +0000235 int line_length;
wdenk8655b6f2004-10-09 23:25:58 +0000236
Nikita Kiryanov8f47d912012-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));
wdenk8655b6f2004-10-09 23:25:58 +0000239
Simon Glass676d3192012-10-17 13:24:54 +0000240 size = lcd_get_size(&line_length);
wdenk8655b6f2004-10-09 23:25:58 +0000241
Simon Glass676d3192012-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);
wdenk8655b6f2004-10-09 23:25:58 +0000245
246 /* Allocate pages for the frame buffer. */
247 addr -= size;
248
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000249 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
250 size >> 10, addr);
wdenk8655b6f2004-10-09 23:25:58 +0000251
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000252 return addr;
wdenk8655b6f2004-10-09 23:25:58 +0000253}
254
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000255static void lcd_setfgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000256{
Stelian Pop39cf4802008-05-09 21:57:18 +0200257 lcd_color_fg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000258}
259
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200260int lcd_getfgcolor(void)
261{
262 return lcd_color_fg;
263}
264
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000265static void lcd_setbgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000266{
Stelian Pop39cf4802008-05-09 21:57:18 +0200267 lcd_color_bg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000268}
269
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200270int lcd_getbgcolor(void)
271{
272 return lcd_color_bg;
273}
274
wdenk8655b6f2004-10-09 23:25:58 +0000275#ifdef CONFIG_LCD_LOGO
Nikita Kiryanova02e9482015-02-03 13:32:24 +0200276__weak void lcd_logo_set_cmap(void)
277{
Nikita Kiryanov23064572015-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 Kiryanova02e9482015-02-03 13:32:24 +0200283}
284
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200285void lcd_logo_plot(int x, int y)
wdenk8655b6f2004-10-09 23:25:58 +0000286{
wdenk8655b6f2004-10-09 23:25:58 +0000287 ushort i, j;
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200288 uchar *bmap = &bmp_logo_bitmap[0];
Andre Renaud317461c2013-02-13 17:48:00 +0000289 unsigned bpix = NBITS(panel_info.vl_bpix);
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200290 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
291 ushort *fb16;
wdenk8655b6f2004-10-09 23:25:58 +0000292
Nikita Kiryanov23064572015-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);
wdenk8655b6f2004-10-09 23:25:58 +0000295
Andre Renaud317461c2013-02-13 17:48:00 +0000296 if (bpix < 12) {
wdenk8655b6f2004-10-09 23:25:58 +0000297 WATCHDOG_RESET();
Nikita Kiryanova02e9482015-02-03 13:32:24 +0200298 lcd_logo_set_cmap();
wdenk8655b6f2004-10-09 23:25:58 +0000299 WATCHDOG_RESET();
300
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000301 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
302 memcpy(fb, bmap, BMP_LOGO_WIDTH);
wdenk8655b6f2004-10-09 23:25:58 +0000303 bmap += BMP_LOGO_WIDTH;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000304 fb += panel_info.vl_col;
wdenk8655b6f2004-10-09 23:25:58 +0000305 }
306 }
307 else { /* true color mode */
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100308 u16 col16;
Andre Renaud317461c2013-02-13 17:48:00 +0000309 fb16 = (ushort *)fb;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000310 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
311 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
Alessandro Rubiniacb13862010-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);
wdenk8655b6f2004-10-09 23:25:58 +0000317 }
318 bmap += BMP_LOGO_WIDTH;
319 fb16 += panel_info.vl_col;
320 }
321 }
322
323 WATCHDOG_RESET();
Simon Glass9a8efc42012-10-30 13:40:18 +0000324 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000325}
Anatolij Gustschin2b5cb3d2012-04-27 04:41:27 +0000326#else
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200327static inline void lcd_logo_plot(int x, int y) {}
wdenk8655b6f2004-10-09 23:25:58 +0000328#endif /* CONFIG_LCD_LOGO */
329
Jon Loeligerc3517f92007-07-08 18:10:08 -0500330#if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200331#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-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 Yamadab4141192014-11-07 03:03:31 +0900346 *axis = max(0, (int)axis_alignment);
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000347}
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200348#endif
349
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000350#ifdef CONFIG_LCD_BMP_RLE8
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000351#define BMP_RLE8_ESCAPE 0
352#define BMP_RLE8_EOL 0
353#define BMP_RLE8_EOBMP 1
354#define BMP_RLE8_DELTA 2
355
356static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
357 int cnt)
358{
359 while (cnt > 0) {
360 *(*fbp)++ = cmap[*bmap++];
361 cnt--;
362 }
363}
364
365static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
366{
367 ushort *fb = *fbp;
368 int cnt_8copy = cnt >> 3;
369
370 cnt -= cnt_8copy << 3;
371 while (cnt_8copy > 0) {
372 *fb++ = c;
373 *fb++ = c;
374 *fb++ = c;
375 *fb++ = c;
376 *fb++ = c;
377 *fb++ = c;
378 *fb++ = c;
379 *fb++ = c;
380 cnt_8copy--;
381 }
382 while (cnt > 0) {
383 *fb++ = c;
384 cnt--;
385 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000386 *fbp = fb;
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000387}
388
389/*
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000390 * Do not call this function directly, must be called from lcd_display_bitmap.
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000391 */
Simon Glass1c3dbe52015-05-13 07:02:27 -0600392static void lcd_display_rle8_bitmap(struct bmp_image *bmp, ushort *cmap,
393 uchar *fb, int x_off, int y_off)
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000394{
395 uchar *bmap;
396 ulong width, height;
397 ulong cnt, runlen;
398 int x, y;
399 int decode = 1;
400
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100401 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);
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000404
405 x = 0;
406 y = height - 1;
407
408 while (decode) {
409 if (bmap[0] == BMP_RLE8_ESCAPE) {
410 switch (bmap[1]) {
411 case BMP_RLE8_EOL:
412 /* end of line */
413 bmap += 2;
414 x = 0;
415 y--;
416 /* 16bpix, 2-byte per pixel, width should *2 */
417 fb -= (width * 2 + lcd_line_length);
418 break;
419 case BMP_RLE8_EOBMP:
420 /* end of bitmap */
421 decode = 0;
422 break;
423 case BMP_RLE8_DELTA:
424 /* delta run */
425 x += bmap[2];
426 y -= bmap[3];
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);
430 bmap += 4;
431 break;
432 default:
433 /* unencoded run */
434 runlen = bmap[1];
435 bmap += 2;
436 if (y < height) {
437 if (x < width) {
438 if (x + runlen > width)
439 cnt = width - x;
440 else
441 cnt = runlen;
442 draw_unencoded_bitmap(
443 (ushort **)&fb,
444 bmap, cmap, cnt);
445 }
446 x += runlen;
447 }
448 bmap += runlen;
449 if (runlen & 1)
450 bmap++;
451 }
452 } else {
453 /* encoded run */
454 if (y < height) {
455 runlen = bmap[0];
456 if (x < width) {
457 /* aggregate the same code */
458 while (bmap[0] == 0xff &&
459 bmap[2] != BMP_RLE8_ESCAPE &&
460 bmap[1] == bmap[3]) {
461 runlen += bmap[2];
462 bmap += 2;
463 }
464 if (x + runlen > width)
465 cnt = width - x;
466 else
467 cnt = runlen;
468 draw_encoded_bitmap((ushort **)&fb,
469 cmap[bmap[1]], cnt);
470 }
471 x += runlen;
472 }
473 bmap += 2;
474 }
475 }
476}
477#endif
478
Nikita Kiryanov27fad012015-02-03 13:32:23 +0200479__weak void fb_put_byte(uchar **fb, uchar **from)
480{
481 *(*fb)++ = *(*from)++;
482}
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000483
484#if defined(CONFIG_BMP_16BPP)
Nikita Kiryanovb3d12e92015-02-03 13:32:22 +0200485__weak void fb_put_word(uchar **fb, uchar **from)
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000486{
487 *(*fb)++ = *(*from)++;
488 *(*fb)++ = *(*from)++;
489}
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000490#endif /* CONFIG_BMP_16BPP */
491
Simon Glass1c3dbe52015-05-13 07:02:27 -0600492__weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200493{
494 int i;
Simon Glass1c3dbe52015-05-13 07:02:27 -0600495 struct bmp_color_table_entry cte;
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200496 ushort *cmap = configuration_get_cmap();
497
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);
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200503 cmap++;
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200504 }
505}
506
wdenk8655b6f2004-10-09 23:25:58 +0000507int lcd_display_bitmap(ulong bmp_image, int x, int y)
508{
Anatolij Gustschin00cc5592009-02-25 20:28:13 +0100509 ushort *cmap_base = NULL;
wdenk8655b6f2004-10-09 23:25:58 +0000510 ushort i, j;
511 uchar *fb;
Simon Glass1c3dbe52015-05-13 07:02:27 -0600512 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000513 uchar *bmap;
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000514 ushort padded_width;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100515 unsigned long width, height, byte_width;
Wolfgang Denke8143e72006-08-30 23:09:00 +0200516 unsigned long pwidth = panel_info.vl_col;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100517 unsigned colors, bpix, bmp_bpix;
Simon Glass8d379f12015-05-13 07:02:28 -0600518 int hdr_size;
xypron.glpk@gmx.de021414a2017-07-30 21:59:23 +0200519 struct bmp_color_table_entry *palette;
wdenk8655b6f2004-10-09 23:25:58 +0000520
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000521 if (!bmp || !(bmp->header.signature[0] == 'B' &&
522 bmp->header.signature[1] == 'M')) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000523 printf("Error: no valid bmp image at %lx\n", bmp_image);
524
wdenk8655b6f2004-10-09 23:25:58 +0000525 return 1;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100526 }
wdenk8655b6f2004-10-09 23:25:58 +0000527
xypron.glpk@gmx.de021414a2017-07-30 21:59:23 +0200528 palette = bmp->color_table;
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100529 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);
Simon Glass8d379f12015-05-13 07:02:28 -0600532 hdr_size = get_unaligned_le16(&bmp->header.size);
533 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100534
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100535 colors = 1 << bmp_bpix;
wdenk8655b6f2004-10-09 23:25:58 +0000536
537 bpix = NBITS(panel_info.vl_bpix);
538
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000539 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100540 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
541 bpix, bmp_bpix);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000542
wdenk8655b6f2004-10-09 23:25:58 +0000543 return 1;
544 }
545
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200546 /*
547 * We support displaying 8bpp BMPs on 16bpp LCDs
548 * and displaying 24bpp BMPs on 32bpp LCDs
549 * */
550 if (bpix != bmp_bpix &&
551 !(bmp_bpix == 8 && bpix == 16) &&
552 !(bmp_bpix == 24 && bpix == 32)) {
wdenk8655b6f2004-10-09 23:25:58 +0000553 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100554 bpix, get_unaligned_le16(&bmp->header.bit_count));
wdenk8655b6f2004-10-09 23:25:58 +0000555 return 1;
556 }
557
Simon Glass8d379f12015-05-13 07:02:28 -0600558 debug("Display-bmp: %d x %d with %d colors, display %d\n",
559 (int)width, (int)height, (int)colors, 1 << bpix);
wdenk8655b6f2004-10-09 23:25:58 +0000560
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200561 if (bmp_bpix == 8)
562 lcd_set_cmap(bmp, colors);
Wolfgang Denke8143e72006-08-30 23:09:00 +0200563
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000564 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200565
566#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000567 splash_align_axis(&x, pwidth, width);
568 splash_align_axis(&y, panel_info.vl_row, height);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200569#endif /* CONFIG_SPLASH_SCREEN_ALIGN */
570
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000571 if ((x + width) > pwidth)
Wolfgang Denke8143e72006-08-30 23:09:00 +0200572 width = pwidth - x;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000573 if ((y + height) > panel_info.vl_row)
wdenk8655b6f2004-10-09 23:25:58 +0000574 height = panel_info.vl_row - y;
575
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100576 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
577 fb = (uchar *)(lcd_base +
Liu Ying8d46d5b2011-01-11 15:29:58 +0800578 (y + height - 1) * lcd_line_length + x * bpix / 8);
Mark Jacksona303dfb2009-02-06 10:37:49 +0100579
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100580 switch (bmp_bpix) {
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200581 case 1:
Simon Glass01564442014-10-15 04:53:04 -0600582 case 8: {
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200583 cmap_base = configuration_get_cmap();
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000584#ifdef CONFIG_LCD_BMP_RLE8
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100585 u32 compression = get_unaligned_le32(&bmp->header.compression);
Simon Glass8d379f12015-05-13 07:02:28 -0600586 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100587 if (compression == BMP_BI_RLE8) {
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000588 if (bpix != 16) {
589 /* TODO implement render code for bpix != 16 */
590 printf("Error: only support 16 bpix");
591 return 1;
592 }
593 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
594 break;
595 }
596#endif
597
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100598 if (bpix != 16)
599 byte_width = width;
600 else
601 byte_width = width * 2;
602
Mark Jacksona303dfb2009-02-06 10:37:49 +0100603 for (i = 0; i < height; ++i) {
604 WATCHDOG_RESET();
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100605 for (j = 0; j < width; j++) {
606 if (bpix != 16) {
Nikita Kiryanov27fad012015-02-03 13:32:23 +0200607 fb_put_byte(&fb, &bmap);
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100608 } else {
Simon Glass8d379f12015-05-13 07:02:28 -0600609 struct bmp_color_table_entry *entry;
610 uint val;
611
612 if (cmap_base) {
613 val = cmap_base[*bmap];
614 } else {
615 entry = &palette[*bmap];
616 val = entry->blue >> 3 |
617 entry->green >> 2 << 5 |
618 entry->red >> 3 << 11;
619 }
620 *(uint16_t *)fb = val;
621 bmap++;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100622 fb += sizeof(uint16_t) / sizeof(*fb);
623 }
624 }
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000625 bmap += (padded_width - width);
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000626 fb -= byte_width + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +0100627 }
628 break;
Simon Glass01564442014-10-15 04:53:04 -0600629 }
Mark Jacksona303dfb2009-02-06 10:37:49 +0100630#if defined(CONFIG_BMP_16BPP)
631 case 16:
632 for (i = 0; i < height; ++i) {
633 WATCHDOG_RESET();
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000634 for (j = 0; j < width; j++)
635 fb_put_word(&fb, &bmap);
636
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000637 bmap += (padded_width - width) * 2;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000638 fb -= width * 2 + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +0100639 }
640 break;
641#endif /* CONFIG_BMP_16BPP */
Philipp Tomsich10ba6b32017-05-05 21:48:30 +0200642#if defined(CONFIG_BMP_24BPP)
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200643 case 24:
644 for (i = 0; i < height; ++i) {
645 for (j = 0; j < width; j++) {
646 *(fb++) = *(bmap++);
647 *(fb++) = *(bmap++);
648 *(fb++) = *(bmap++);
649 *(fb++) = 0;
650 }
651 fb -= lcd_line_length + width * (bpix / 8);
652 }
653 break;
Philipp Tomsich10ba6b32017-05-05 21:48:30 +0200654#endif /* CONFIG_BMP_24BPP */
Donghwa Leefb6a9aa2012-05-09 19:23:37 +0000655#if defined(CONFIG_BMP_32BPP)
656 case 32:
657 for (i = 0; i < height; ++i) {
658 for (j = 0; j < width; j++) {
659 *(fb++) = *(bmap++);
660 *(fb++) = *(bmap++);
661 *(fb++) = *(bmap++);
662 *(fb++) = *(bmap++);
663 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000664 fb -= lcd_line_length + width * (bpix / 8);
Donghwa Leefb6a9aa2012-05-09 19:23:37 +0000665 }
666 break;
667#endif /* CONFIG_BMP_32BPP */
Mark Jacksona303dfb2009-02-06 10:37:49 +0100668 default:
669 break;
670 };
wdenk8655b6f2004-10-09 23:25:58 +0000671
Simon Glass9a8efc42012-10-30 13:40:18 +0000672 lcd_sync();
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000673 return 0;
wdenk8655b6f2004-10-09 23:25:58 +0000674}
Jon Loeligerc3517f92007-07-08 18:10:08 -0500675#endif
wdenk8655b6f2004-10-09 23:25:58 +0000676
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200677static void lcd_logo(void)
wdenk8655b6f2004-10-09 23:25:58 +0000678{
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200679 lcd_logo_plot(0, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000680
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +0200681#ifdef CONFIG_LCD_INFO
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200682 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
683 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +0200684 lcd_show_board_info();
685#endif /* CONFIG_LCD_INFO */
wdenk8655b6f2004-10-09 23:25:58 +0000686}
687
Nikita Kiryanovc0880482013-02-24 21:28:43 +0000688#ifdef CONFIG_SPLASHIMAGE_GUARD
689static int on_splashimage(const char *name, const char *value, enum env_op op,
690 int flags)
691{
692 ulong addr;
693 int aligned;
694
695 if (op == env_op_delete)
696 return 0;
697
Simon Glass7e5f4602021-07-24 09:03:29 -0600698 addr = hextoul(value, NULL);
Nikita Kiryanovc0880482013-02-24 21:28:43 +0000699 /* See README.displaying-bmps */
700 aligned = (addr % 4 == 2);
701 if (!aligned) {
702 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
703 return -1;
704 }
705
706 return 0;
707}
708
709U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
710#endif
711
Vadim Bendebury395166c2012-09-28 15:11:13 +0000712int lcd_get_pixel_width(void)
713{
714 return panel_info.vl_col;
715}
716
717int lcd_get_pixel_height(void)
718{
719 return panel_info.vl_row;
720}