blob: c421090c87ad84c9d7393a64f31d3c2ff860e43e [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 Glass90526e92020-05-10 11:39:56 -060015#include <asm/cache.h>
wdenk8655b6f2004-10-09 23:25:58 +000016#include <linux/types.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020017#include <stdio_dev.h>
wdenk8655b6f2004-10-09 23:25:58 +000018#include <lcd.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050019#include <mapmem.h>
wdenk8b0bfc62005-04-03 23:11:38 +000020#include <watchdog.h>
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +010021#include <asm/unaligned.h>
Robert Winklerdd4425e2013-06-17 11:31:29 -070022#include <splash.h>
Simon Glass7d95f2a2014-02-27 13:26:19 -070023#include <asm/io.h>
24#include <asm/unaligned.h>
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020025#include <video_font.h>
Robert Winklerdd4425e2013-06-17 11:31:29 -070026
wdenk88804d12005-07-04 00:03:16 +000027#ifdef CONFIG_LCD_LOGO
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020028#include <bmp_logo.h>
29#include <bmp_logo_data.h>
30#if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
31#error Default Color Map overlaps with Logo Color Map
32#endif
wdenk88804d12005-07-04 00:03:16 +000033#endif
wdenk8655b6f2004-10-09 23:25:58 +000034
Simon Glass676d3192012-10-17 13:24:54 +000035#ifndef CONFIG_LCD_ALIGNMENT
36#define CONFIG_LCD_ALIGNMENT PAGE_SIZE
37#endif
38
Nikita Kiryanova7de2952014-12-08 17:14:42 +020039#if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
40 (LCD_BPP != LCD_COLOR32)
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020041#error Unsupported LCD BPP.
Jeroen Hofsteea5796c52013-01-12 12:07:59 +000042#endif
43
Wolfgang Denkd87080b2006-03-31 18:32:53 +020044DECLARE_GLOBAL_DATA_PTR;
wdenk8655b6f2004-10-09 23:25:58 +000045
Nikita Kiryanov8f47d912012-05-24 01:42:38 +000046static int lcd_init(void *lcdbase);
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +020047static void lcd_logo(void);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +000048static void lcd_setfgcolor(int color);
49static void lcd_setbgcolor(int color);
wdenk8655b6f2004-10-09 23:25:58 +000050
Wolfgang Denk46d1d5d2013-01-05 09:45:48 +000051static int lcd_color_fg;
52static int lcd_color_bg;
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +000053int lcd_line_length;
wdenk8655b6f2004-10-09 23:25:58 +000054char lcd_is_enabled = 0;
Jeroen Hofstee00a0ca52013-01-22 10:44:12 +000055static void *lcd_base; /* Start of framebuffer memory */
Simon Glass9a8efc42012-10-30 13:40:18 +000056static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
57
Simon Glass9a8efc42012-10-30 13:40:18 +000058/* Flush LCD activity to the caches */
59void lcd_sync(void)
60{
61 /*
62 * flush_dcache_range() is declared in common.h but it seems that some
63 * architectures do not actually implement it. Is there a way to find
64 * out whether it exists? For now, ARM is safe.
65 */
Trevor Woerner10015022019-05-03 09:41:00 -040066#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass9a8efc42012-10-30 13:40:18 +000067 int line_length;
68
69 if (lcd_flush_dcache)
Alexander Graff8f58fb2016-03-16 15:41:22 +010070 flush_dcache_range((ulong)lcd_base,
71 (ulong)(lcd_base + lcd_get_size(&line_length)));
Simon Glass9a8efc42012-10-30 13:40:18 +000072#endif
73}
74
75void lcd_set_flush_dcache(int flush)
76{
77 lcd_flush_dcache = (flush != 0);
78}
79
Simon Glass709ea542014-07-23 06:54:59 -060080static void lcd_stub_putc(struct stdio_dev *dev, const char c)
81{
82 lcd_putc(c);
83}
84
Simon Glass709ea542014-07-23 06:54:59 -060085static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
86{
87 lcd_puts(s);
88}
89
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020090/* Small utility to check that you got the colours right */
wdenk8655b6f2004-10-09 23:25:58 +000091#ifdef LCD_TEST_PATTERN
92
Andreas Neubachere32951b2016-01-21 13:06:32 +010093#if LCD_BPP == LCD_COLOR8
wdenk8655b6f2004-10-09 23:25:58 +000094#define N_BLK_VERT 2
95#define N_BLK_HOR 3
96
Jeroen Hofstee6b035142013-01-12 12:07:56 +000097static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
wdenk8655b6f2004-10-09 23:25:58 +000098 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
99 CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
Andreas Neubachere32951b2016-01-21 13:06:32 +0100100}; /*LCD_BPP == LCD_COLOR8 */
101
102#elif LCD_BPP == LCD_COLOR16
103#define N_BLK_VERT 2
104#define N_BLK_HOR 4
105
106static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
107 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW, CONSOLE_COLOR_BLUE,
108 CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN, CONSOLE_COLOR_GREY, CONSOLE_COLOR_WHITE,
wdenk8655b6f2004-10-09 23:25:58 +0000109};
Andreas Neubachere32951b2016-01-21 13:06:32 +0100110#endif /*LCD_BPP == LCD_COLOR16 */
wdenk8655b6f2004-10-09 23:25:58 +0000111
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000112static void test_pattern(void)
wdenk8655b6f2004-10-09 23:25:58 +0000113{
114 ushort v_max = panel_info.vl_row;
115 ushort h_max = panel_info.vl_col;
116 ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
117 ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
118 ushort v, h;
Andreas Neubachere32951b2016-01-21 13:06:32 +0100119#if LCD_BPP == LCD_COLOR8
wdenk8655b6f2004-10-09 23:25:58 +0000120 uchar *pix = (uchar *)lcd_base;
Andreas Neubachere32951b2016-01-21 13:06:32 +0100121#elif LCD_BPP == LCD_COLOR16
122 ushort *pix = (ushort *)lcd_base;
123#endif
wdenk8655b6f2004-10-09 23:25:58 +0000124
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000125 printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
wdenk8655b6f2004-10-09 23:25:58 +0000126 h_max, v_max, h_step, v_step);
127
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000128 for (v = 0; v < v_max; ++v) {
wdenk8655b6f2004-10-09 23:25:58 +0000129 uchar iy = v / v_step;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000130 for (h = 0; h < h_max; ++h) {
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000131 uchar ix = N_BLK_HOR * iy + h / h_step;
wdenk8655b6f2004-10-09 23:25:58 +0000132 *pix++ = test_colors[ix];
133 }
134 }
135}
136#endif /* LCD_TEST_PATTERN */
137
Anatolij Gustschincefa4712013-11-09 11:00:09 +0100138/*
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.
143 */
144__weak int lcd_get_size(int *line_length)
Simon Glass676d3192012-10-17 13:24:54 +0000145{
146 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
147 return *line_length * panel_info.vl_row;
148}
149
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000150int drv_lcd_init(void)
wdenk8655b6f2004-10-09 23:25:58 +0000151{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200152 struct stdio_dev lcddev;
wdenk8655b6f2004-10-09 23:25:58 +0000153 int rc;
154
Simon Glass7d95f2a2014-02-27 13:26:19 -0700155 lcd_base = map_sysmem(gd->fb_base, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000156
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200157 lcd_init(lcd_base);
wdenk8655b6f2004-10-09 23:25:58 +0000158
159 /* Device initialization */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000160 memset(&lcddev, 0, sizeof(lcddev));
wdenk8655b6f2004-10-09 23:25:58 +0000161
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000162 strcpy(lcddev.name, "lcd");
wdenk8655b6f2004-10-09 23:25:58 +0000163 lcddev.ext = 0; /* No extensions */
164 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
Simon Glass709ea542014-07-23 06:54:59 -0600165 lcddev.putc = lcd_stub_putc; /* 'putc' function */
166 lcddev.puts = lcd_stub_puts; /* 'puts' function */
wdenk8655b6f2004-10-09 23:25:58 +0000167
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000168 rc = stdio_register(&lcddev);
wdenk8655b6f2004-10-09 23:25:58 +0000169
170 return (rc == 0) ? 1 : rc;
171}
172
Che-Liang Chiou02110902011-10-20 23:07:03 +0000173void lcd_clear(void)
wdenk8655b6f2004-10-09 23:25:58 +0000174{
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200175 int bg_color;
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000176 __maybe_unused ulong addr;
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200177 static int do_splash = 1;
Nikita Kiryanovf4469f52014-12-08 17:14:38 +0200178#if LCD_BPP == LCD_COLOR8
wdenk8655b6f2004-10-09 23:25:58 +0000179 /* Setting the palette */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000180 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
181 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
182 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
183 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
184 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
185 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
186 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
187 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
188 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
wdenk8655b6f2004-10-09 23:25:58 +0000189#endif
190
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200191#ifndef CONFIG_SYS_WHITE_ON_BLACK
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000192 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
193 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200194 bg_color = CONSOLE_COLOR_WHITE;
wdenk8655b6f2004-10-09 23:25:58 +0000195#else
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000196 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
197 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200198 bg_color = CONSOLE_COLOR_BLACK;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200199#endif /* CONFIG_SYS_WHITE_ON_BLACK */
wdenk8655b6f2004-10-09 23:25:58 +0000200
201#ifdef LCD_TEST_PATTERN
202 test_pattern();
203#else
204 /* set framebuffer to background color */
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100205#if (LCD_BPP != LCD_COLOR32)
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200206 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100207#else
208 u32 *ppix = lcd_base;
209 u32 i;
210 for (i = 0;
211 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
212 i++) {
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200213 *ppix++ = bg_color;
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100214 }
215#endif
wdenk8655b6f2004-10-09 23:25:58 +0000216#endif
Hannes Petermaier604c7d42015-03-27 08:01:38 +0100217 /* setup text-console */
218 debug("[LCD] setting up console...\n");
219 lcd_init_console(lcd_base,
220 panel_info.vl_col,
221 panel_info.vl_row,
222 panel_info.vl_rot);
wdenk8655b6f2004-10-09 23:25:58 +0000223 /* Paint the logo and retrieve LCD base address */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000224 debug("[LCD] Drawing the logo...\n");
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200225 if (do_splash) {
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000226 if (splash_display() == 0) {
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200227 do_splash = 0;
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000228 lcd_sync();
229 return;
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200230 }
231 }
232
233 lcd_logo();
234#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
235 addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
Marcel Ziswiler3b96b902015-08-04 15:49:50 +0200236 lcd_init_console((void *)addr, panel_info.vl_col,
237 panel_info.vl_row, panel_info.vl_rot);
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200238#endif
Simon Glass9a8efc42012-10-30 13:40:18 +0000239 lcd_sync();
240}
241
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000242static int lcd_init(void *lcdbase)
wdenk8655b6f2004-10-09 23:25:58 +0000243{
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000244 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000245 lcd_ctrl_init(lcdbase);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100246
247 /*
Stephen Warren9316e142014-11-19 20:41:03 -0700248 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100249 * the 'lcdbase' argument and uses custom lcd base address
250 * by setting up gd->fb_base. Check for this condition and fixup
251 * 'lcd_base' address.
252 */
Simon Glass7d95f2a2014-02-27 13:26:19 -0700253 if (map_to_sysmem(lcdbase) != gd->fb_base)
254 lcd_base = map_sysmem(gd->fb_base, 0);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100255
256 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
257
Stephen Warren6d330712013-01-29 16:37:38 +0000258 lcd_get_size(&lcd_line_length);
Haavard Skinnemoen6f93d2b2008-09-01 16:21:21 +0200259 lcd_is_enabled = 1;
Che-Liang Chiou02110902011-10-20 23:07:03 +0000260 lcd_clear();
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000261 lcd_enable();
wdenk8655b6f2004-10-09 23:25:58 +0000262
263 /* Initialize the console */
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200264 lcd_set_col(0);
wdenk88804d12005-07-04 00:03:16 +0000265#ifdef CONFIG_LCD_INFO_BELOW_LOGO
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200266 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
wdenk8655b6f2004-10-09 23:25:58 +0000267#else
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200268 lcd_set_row(1); /* leave 1 blank line below logo */
wdenk8655b6f2004-10-09 23:25:58 +0000269#endif
wdenk8655b6f2004-10-09 23:25:58 +0000270
271 return 0;
272}
273
wdenk8655b6f2004-10-09 23:25:58 +0000274/*
275 * This is called early in the system initialization to grab memory
276 * for the LCD controller.
277 * Returns new address for monitor, after reserving LCD buffer memory
278 *
279 * Note that this is running from ROM, so no write access to global data.
280 */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000281ulong lcd_setmem(ulong addr)
wdenk8655b6f2004-10-09 23:25:58 +0000282{
283 ulong size;
Simon Glass676d3192012-10-17 13:24:54 +0000284 int line_length;
wdenk8655b6f2004-10-09 23:25:58 +0000285
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000286 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
287 panel_info.vl_row, NBITS(panel_info.vl_bpix));
wdenk8655b6f2004-10-09 23:25:58 +0000288
Simon Glass676d3192012-10-17 13:24:54 +0000289 size = lcd_get_size(&line_length);
wdenk8655b6f2004-10-09 23:25:58 +0000290
Simon Glass676d3192012-10-17 13:24:54 +0000291 /* Round up to nearest full page, or MMU section if defined */
292 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
293 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
wdenk8655b6f2004-10-09 23:25:58 +0000294
295 /* Allocate pages for the frame buffer. */
296 addr -= size;
297
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000298 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
299 size >> 10, addr);
wdenk8655b6f2004-10-09 23:25:58 +0000300
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000301 return addr;
wdenk8655b6f2004-10-09 23:25:58 +0000302}
303
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000304static void lcd_setfgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000305{
Stelian Pop39cf4802008-05-09 21:57:18 +0200306 lcd_color_fg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000307}
308
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200309int lcd_getfgcolor(void)
310{
311 return lcd_color_fg;
312}
313
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000314static void lcd_setbgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000315{
Stelian Pop39cf4802008-05-09 21:57:18 +0200316 lcd_color_bg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000317}
318
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200319int lcd_getbgcolor(void)
320{
321 return lcd_color_bg;
322}
323
wdenk8655b6f2004-10-09 23:25:58 +0000324#ifdef CONFIG_LCD_LOGO
Nikita Kiryanova02e9482015-02-03 13:32:24 +0200325__weak void lcd_logo_set_cmap(void)
326{
Nikita Kiryanov23064572015-02-03 13:32:26 +0200327 int i;
328 ushort *cmap = configuration_get_cmap();
329
330 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
331 *cmap++ = bmp_logo_palette[i];
Nikita Kiryanova02e9482015-02-03 13:32:24 +0200332}
333
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200334void lcd_logo_plot(int x, int y)
wdenk8655b6f2004-10-09 23:25:58 +0000335{
wdenk8655b6f2004-10-09 23:25:58 +0000336 ushort i, j;
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200337 uchar *bmap = &bmp_logo_bitmap[0];
Andre Renaud317461c2013-02-13 17:48:00 +0000338 unsigned bpix = NBITS(panel_info.vl_bpix);
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200339 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
340 ushort *fb16;
wdenk8655b6f2004-10-09 23:25:58 +0000341
Nikita Kiryanov23064572015-02-03 13:32:26 +0200342 debug("Logo: width %d height %d colors %d\n",
343 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
wdenk8655b6f2004-10-09 23:25:58 +0000344
Andre Renaud317461c2013-02-13 17:48:00 +0000345 if (bpix < 12) {
wdenk8655b6f2004-10-09 23:25:58 +0000346 WATCHDOG_RESET();
Nikita Kiryanova02e9482015-02-03 13:32:24 +0200347 lcd_logo_set_cmap();
wdenk8655b6f2004-10-09 23:25:58 +0000348 WATCHDOG_RESET();
349
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000350 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
351 memcpy(fb, bmap, BMP_LOGO_WIDTH);
wdenk8655b6f2004-10-09 23:25:58 +0000352 bmap += BMP_LOGO_WIDTH;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000353 fb += panel_info.vl_col;
wdenk8655b6f2004-10-09 23:25:58 +0000354 }
355 }
356 else { /* true color mode */
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100357 u16 col16;
Andre Renaud317461c2013-02-13 17:48:00 +0000358 fb16 = (ushort *)fb;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000359 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
360 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100361 col16 = bmp_logo_palette[(bmap[j]-16)];
362 fb16[j] =
363 ((col16 & 0x000F) << 1) |
364 ((col16 & 0x00F0) << 3) |
365 ((col16 & 0x0F00) << 4);
wdenk8655b6f2004-10-09 23:25:58 +0000366 }
367 bmap += BMP_LOGO_WIDTH;
368 fb16 += panel_info.vl_col;
369 }
370 }
371
372 WATCHDOG_RESET();
Simon Glass9a8efc42012-10-30 13:40:18 +0000373 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000374}
Anatolij Gustschin2b5cb3d2012-04-27 04:41:27 +0000375#else
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200376static inline void lcd_logo_plot(int x, int y) {}
wdenk8655b6f2004-10-09 23:25:58 +0000377#endif /* CONFIG_LCD_LOGO */
378
Jon Loeligerc3517f92007-07-08 18:10:08 -0500379#if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200380#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000381
382static void splash_align_axis(int *axis, unsigned long panel_size,
383 unsigned long picture_size)
384{
385 unsigned long panel_picture_delta = panel_size - picture_size;
386 unsigned long axis_alignment;
387
388 if (*axis == BMP_ALIGN_CENTER)
389 axis_alignment = panel_picture_delta / 2;
390 else if (*axis < 0)
391 axis_alignment = panel_picture_delta + *axis + 1;
392 else
393 return;
394
Masahiro Yamadab4141192014-11-07 03:03:31 +0900395 *axis = max(0, (int)axis_alignment);
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000396}
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200397#endif
398
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000399#ifdef CONFIG_LCD_BMP_RLE8
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000400#define BMP_RLE8_ESCAPE 0
401#define BMP_RLE8_EOL 0
402#define BMP_RLE8_EOBMP 1
403#define BMP_RLE8_DELTA 2
404
405static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
406 int cnt)
407{
408 while (cnt > 0) {
409 *(*fbp)++ = cmap[*bmap++];
410 cnt--;
411 }
412}
413
414static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
415{
416 ushort *fb = *fbp;
417 int cnt_8copy = cnt >> 3;
418
419 cnt -= cnt_8copy << 3;
420 while (cnt_8copy > 0) {
421 *fb++ = c;
422 *fb++ = c;
423 *fb++ = c;
424 *fb++ = c;
425 *fb++ = c;
426 *fb++ = c;
427 *fb++ = c;
428 *fb++ = c;
429 cnt_8copy--;
430 }
431 while (cnt > 0) {
432 *fb++ = c;
433 cnt--;
434 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000435 *fbp = fb;
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000436}
437
438/*
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000439 * Do not call this function directly, must be called from lcd_display_bitmap.
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000440 */
Simon Glass1c3dbe52015-05-13 07:02:27 -0600441static void lcd_display_rle8_bitmap(struct bmp_image *bmp, ushort *cmap,
442 uchar *fb, int x_off, int y_off)
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000443{
444 uchar *bmap;
445 ulong width, height;
446 ulong cnt, runlen;
447 int x, y;
448 int decode = 1;
449
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100450 width = get_unaligned_le32(&bmp->header.width);
451 height = get_unaligned_le32(&bmp->header.height);
452 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000453
454 x = 0;
455 y = height - 1;
456
457 while (decode) {
458 if (bmap[0] == BMP_RLE8_ESCAPE) {
459 switch (bmap[1]) {
460 case BMP_RLE8_EOL:
461 /* end of line */
462 bmap += 2;
463 x = 0;
464 y--;
465 /* 16bpix, 2-byte per pixel, width should *2 */
466 fb -= (width * 2 + lcd_line_length);
467 break;
468 case BMP_RLE8_EOBMP:
469 /* end of bitmap */
470 decode = 0;
471 break;
472 case BMP_RLE8_DELTA:
473 /* delta run */
474 x += bmap[2];
475 y -= bmap[3];
476 /* 16bpix, 2-byte per pixel, x should *2 */
477 fb = (uchar *) (lcd_base + (y + y_off - 1)
478 * lcd_line_length + (x + x_off) * 2);
479 bmap += 4;
480 break;
481 default:
482 /* unencoded run */
483 runlen = bmap[1];
484 bmap += 2;
485 if (y < height) {
486 if (x < width) {
487 if (x + runlen > width)
488 cnt = width - x;
489 else
490 cnt = runlen;
491 draw_unencoded_bitmap(
492 (ushort **)&fb,
493 bmap, cmap, cnt);
494 }
495 x += runlen;
496 }
497 bmap += runlen;
498 if (runlen & 1)
499 bmap++;
500 }
501 } else {
502 /* encoded run */
503 if (y < height) {
504 runlen = bmap[0];
505 if (x < width) {
506 /* aggregate the same code */
507 while (bmap[0] == 0xff &&
508 bmap[2] != BMP_RLE8_ESCAPE &&
509 bmap[1] == bmap[3]) {
510 runlen += bmap[2];
511 bmap += 2;
512 }
513 if (x + runlen > width)
514 cnt = width - x;
515 else
516 cnt = runlen;
517 draw_encoded_bitmap((ushort **)&fb,
518 cmap[bmap[1]], cnt);
519 }
520 x += runlen;
521 }
522 bmap += 2;
523 }
524 }
525}
526#endif
527
Nikita Kiryanov27fad012015-02-03 13:32:23 +0200528__weak void fb_put_byte(uchar **fb, uchar **from)
529{
530 *(*fb)++ = *(*from)++;
531}
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000532
533#if defined(CONFIG_BMP_16BPP)
Nikita Kiryanovb3d12e92015-02-03 13:32:22 +0200534__weak void fb_put_word(uchar **fb, uchar **from)
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000535{
536 *(*fb)++ = *(*from)++;
537 *(*fb)++ = *(*from)++;
538}
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000539#endif /* CONFIG_BMP_16BPP */
540
Simon Glass1c3dbe52015-05-13 07:02:27 -0600541__weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200542{
543 int i;
Simon Glass1c3dbe52015-05-13 07:02:27 -0600544 struct bmp_color_table_entry cte;
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200545 ushort *cmap = configuration_get_cmap();
546
547 for (i = 0; i < colors; ++i) {
548 cte = bmp->color_table[i];
549 *cmap = (((cte.red) << 8) & 0xf800) |
550 (((cte.green) << 3) & 0x07e0) |
551 (((cte.blue) >> 3) & 0x001f);
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200552 cmap++;
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200553 }
554}
555
wdenk8655b6f2004-10-09 23:25:58 +0000556int lcd_display_bitmap(ulong bmp_image, int x, int y)
557{
Anatolij Gustschin00cc5592009-02-25 20:28:13 +0100558 ushort *cmap_base = NULL;
wdenk8655b6f2004-10-09 23:25:58 +0000559 ushort i, j;
560 uchar *fb;
Simon Glass1c3dbe52015-05-13 07:02:27 -0600561 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000562 uchar *bmap;
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000563 ushort padded_width;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100564 unsigned long width, height, byte_width;
Wolfgang Denke8143e72006-08-30 23:09:00 +0200565 unsigned long pwidth = panel_info.vl_col;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100566 unsigned colors, bpix, bmp_bpix;
Simon Glass8d379f12015-05-13 07:02:28 -0600567 int hdr_size;
xypron.glpk@gmx.de021414a2017-07-30 21:59:23 +0200568 struct bmp_color_table_entry *palette;
wdenk8655b6f2004-10-09 23:25:58 +0000569
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000570 if (!bmp || !(bmp->header.signature[0] == 'B' &&
571 bmp->header.signature[1] == 'M')) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000572 printf("Error: no valid bmp image at %lx\n", bmp_image);
573
wdenk8655b6f2004-10-09 23:25:58 +0000574 return 1;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100575 }
wdenk8655b6f2004-10-09 23:25:58 +0000576
xypron.glpk@gmx.de021414a2017-07-30 21:59:23 +0200577 palette = bmp->color_table;
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100578 width = get_unaligned_le32(&bmp->header.width);
579 height = get_unaligned_le32(&bmp->header.height);
580 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
Simon Glass8d379f12015-05-13 07:02:28 -0600581 hdr_size = get_unaligned_le16(&bmp->header.size);
582 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100583
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100584 colors = 1 << bmp_bpix;
wdenk8655b6f2004-10-09 23:25:58 +0000585
586 bpix = NBITS(panel_info.vl_bpix);
587
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000588 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100589 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
590 bpix, bmp_bpix);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000591
wdenk8655b6f2004-10-09 23:25:58 +0000592 return 1;
593 }
594
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200595 /*
596 * We support displaying 8bpp BMPs on 16bpp LCDs
597 * and displaying 24bpp BMPs on 32bpp LCDs
598 * */
599 if (bpix != bmp_bpix &&
600 !(bmp_bpix == 8 && bpix == 16) &&
601 !(bmp_bpix == 24 && bpix == 32)) {
wdenk8655b6f2004-10-09 23:25:58 +0000602 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100603 bpix, get_unaligned_le16(&bmp->header.bit_count));
wdenk8655b6f2004-10-09 23:25:58 +0000604 return 1;
605 }
606
Simon Glass8d379f12015-05-13 07:02:28 -0600607 debug("Display-bmp: %d x %d with %d colors, display %d\n",
608 (int)width, (int)height, (int)colors, 1 << bpix);
wdenk8655b6f2004-10-09 23:25:58 +0000609
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200610 if (bmp_bpix == 8)
611 lcd_set_cmap(bmp, colors);
Wolfgang Denke8143e72006-08-30 23:09:00 +0200612
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000613 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200614
615#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000616 splash_align_axis(&x, pwidth, width);
617 splash_align_axis(&y, panel_info.vl_row, height);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200618#endif /* CONFIG_SPLASH_SCREEN_ALIGN */
619
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000620 if ((x + width) > pwidth)
Wolfgang Denke8143e72006-08-30 23:09:00 +0200621 width = pwidth - x;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000622 if ((y + height) > panel_info.vl_row)
wdenk8655b6f2004-10-09 23:25:58 +0000623 height = panel_info.vl_row - y;
624
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100625 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
626 fb = (uchar *)(lcd_base +
Liu Ying8d46d5b2011-01-11 15:29:58 +0800627 (y + height - 1) * lcd_line_length + x * bpix / 8);
Mark Jacksona303dfb2009-02-06 10:37:49 +0100628
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100629 switch (bmp_bpix) {
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200630 case 1:
Simon Glass01564442014-10-15 04:53:04 -0600631 case 8: {
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200632 cmap_base = configuration_get_cmap();
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000633#ifdef CONFIG_LCD_BMP_RLE8
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100634 u32 compression = get_unaligned_le32(&bmp->header.compression);
Simon Glass8d379f12015-05-13 07:02:28 -0600635 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100636 if (compression == BMP_BI_RLE8) {
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000637 if (bpix != 16) {
638 /* TODO implement render code for bpix != 16 */
639 printf("Error: only support 16 bpix");
640 return 1;
641 }
642 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
643 break;
644 }
645#endif
646
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100647 if (bpix != 16)
648 byte_width = width;
649 else
650 byte_width = width * 2;
651
Mark Jacksona303dfb2009-02-06 10:37:49 +0100652 for (i = 0; i < height; ++i) {
653 WATCHDOG_RESET();
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100654 for (j = 0; j < width; j++) {
655 if (bpix != 16) {
Nikita Kiryanov27fad012015-02-03 13:32:23 +0200656 fb_put_byte(&fb, &bmap);
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100657 } else {
Simon Glass8d379f12015-05-13 07:02:28 -0600658 struct bmp_color_table_entry *entry;
659 uint val;
660
661 if (cmap_base) {
662 val = cmap_base[*bmap];
663 } else {
664 entry = &palette[*bmap];
665 val = entry->blue >> 3 |
666 entry->green >> 2 << 5 |
667 entry->red >> 3 << 11;
668 }
669 *(uint16_t *)fb = val;
670 bmap++;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100671 fb += sizeof(uint16_t) / sizeof(*fb);
672 }
673 }
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000674 bmap += (padded_width - width);
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000675 fb -= byte_width + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +0100676 }
677 break;
Simon Glass01564442014-10-15 04:53:04 -0600678 }
Mark Jacksona303dfb2009-02-06 10:37:49 +0100679#if defined(CONFIG_BMP_16BPP)
680 case 16:
681 for (i = 0; i < height; ++i) {
682 WATCHDOG_RESET();
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000683 for (j = 0; j < width; j++)
684 fb_put_word(&fb, &bmap);
685
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000686 bmap += (padded_width - width) * 2;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000687 fb -= width * 2 + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +0100688 }
689 break;
690#endif /* CONFIG_BMP_16BPP */
Philipp Tomsich10ba6b32017-05-05 21:48:30 +0200691#if defined(CONFIG_BMP_24BPP)
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200692 case 24:
693 for (i = 0; i < height; ++i) {
694 for (j = 0; j < width; j++) {
695 *(fb++) = *(bmap++);
696 *(fb++) = *(bmap++);
697 *(fb++) = *(bmap++);
698 *(fb++) = 0;
699 }
700 fb -= lcd_line_length + width * (bpix / 8);
701 }
702 break;
Philipp Tomsich10ba6b32017-05-05 21:48:30 +0200703#endif /* CONFIG_BMP_24BPP */
Donghwa Leefb6a9aa2012-05-09 19:23:37 +0000704#if defined(CONFIG_BMP_32BPP)
705 case 32:
706 for (i = 0; i < height; ++i) {
707 for (j = 0; j < width; j++) {
708 *(fb++) = *(bmap++);
709 *(fb++) = *(bmap++);
710 *(fb++) = *(bmap++);
711 *(fb++) = *(bmap++);
712 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000713 fb -= lcd_line_length + width * (bpix / 8);
Donghwa Leefb6a9aa2012-05-09 19:23:37 +0000714 }
715 break;
716#endif /* CONFIG_BMP_32BPP */
Mark Jacksona303dfb2009-02-06 10:37:49 +0100717 default:
718 break;
719 };
wdenk8655b6f2004-10-09 23:25:58 +0000720
Simon Glass9a8efc42012-10-30 13:40:18 +0000721 lcd_sync();
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000722 return 0;
wdenk8655b6f2004-10-09 23:25:58 +0000723}
Jon Loeligerc3517f92007-07-08 18:10:08 -0500724#endif
wdenk8655b6f2004-10-09 23:25:58 +0000725
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200726static void lcd_logo(void)
wdenk8655b6f2004-10-09 23:25:58 +0000727{
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200728 lcd_logo_plot(0, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000729
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +0200730#ifdef CONFIG_LCD_INFO
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200731 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
732 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +0200733 lcd_show_board_info();
734#endif /* CONFIG_LCD_INFO */
wdenk8655b6f2004-10-09 23:25:58 +0000735}
736
Nikita Kiryanovc0880482013-02-24 21:28:43 +0000737#ifdef CONFIG_SPLASHIMAGE_GUARD
738static int on_splashimage(const char *name, const char *value, enum env_op op,
739 int flags)
740{
741 ulong addr;
742 int aligned;
743
744 if (op == env_op_delete)
745 return 0;
746
747 addr = simple_strtoul(value, NULL, 16);
748 /* See README.displaying-bmps */
749 aligned = (addr % 4 == 2);
750 if (!aligned) {
751 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
752 return -1;
753 }
754
755 return 0;
756}
757
758U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
759#endif
760
Vadim Bendebury395166c2012-09-28 15:11:13 +0000761int lcd_get_pixel_width(void)
762{
763 return panel_info.vl_col;
764}
765
766int lcd_get_pixel_height(void)
767{
768 return panel_info.vl_row;
769}