blob: 4acf91e623c89f5db74b6d95ea9bf145a66e459c [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>
Simon Glass691d7192020-05-10 11:40:02 -060016#include <init.h>
wdenk8655b6f2004-10-09 23:25:58 +000017#include <linux/types.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020018#include <stdio_dev.h>
wdenk8655b6f2004-10-09 23:25:58 +000019#include <lcd.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050020#include <mapmem.h>
wdenk8b0bfc62005-04-03 23:11:38 +000021#include <watchdog.h>
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +010022#include <asm/unaligned.h>
Robert Winklerdd4425e2013-06-17 11:31:29 -070023#include <splash.h>
Simon Glass7d95f2a2014-02-27 13:26:19 -070024#include <asm/io.h>
25#include <asm/unaligned.h>
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020026#include <video_font.h>
Robert Winklerdd4425e2013-06-17 11:31:29 -070027
wdenk88804d12005-07-04 00:03:16 +000028#ifdef CONFIG_LCD_LOGO
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020029#include <bmp_logo.h>
30#include <bmp_logo_data.h>
31#if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
32#error Default Color Map overlaps with Logo Color Map
33#endif
wdenk88804d12005-07-04 00:03:16 +000034#endif
wdenk8655b6f2004-10-09 23:25:58 +000035
Simon Glass676d3192012-10-17 13:24:54 +000036#ifndef CONFIG_LCD_ALIGNMENT
37#define CONFIG_LCD_ALIGNMENT PAGE_SIZE
38#endif
39
Nikita Kiryanova7de2952014-12-08 17:14:42 +020040#if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
41 (LCD_BPP != LCD_COLOR32)
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020042#error Unsupported LCD BPP.
Jeroen Hofsteea5796c52013-01-12 12:07:59 +000043#endif
44
Wolfgang Denkd87080b2006-03-31 18:32:53 +020045DECLARE_GLOBAL_DATA_PTR;
wdenk8655b6f2004-10-09 23:25:58 +000046
Nikita Kiryanov8f47d912012-05-24 01:42:38 +000047static int lcd_init(void *lcdbase);
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +020048static void lcd_logo(void);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +000049static void lcd_setfgcolor(int color);
50static void lcd_setbgcolor(int color);
wdenk8655b6f2004-10-09 23:25:58 +000051
Wolfgang Denk46d1d5d2013-01-05 09:45:48 +000052static int lcd_color_fg;
53static int lcd_color_bg;
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +000054int lcd_line_length;
wdenk8655b6f2004-10-09 23:25:58 +000055char lcd_is_enabled = 0;
Jeroen Hofstee00a0ca52013-01-22 10:44:12 +000056static void *lcd_base; /* Start of framebuffer memory */
Simon Glass9a8efc42012-10-30 13:40:18 +000057static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
58
Simon Glass9a8efc42012-10-30 13:40:18 +000059/* Flush LCD activity to the caches */
60void lcd_sync(void)
61{
62 /*
63 * flush_dcache_range() is declared in common.h but it seems that some
64 * architectures do not actually implement it. Is there a way to find
65 * out whether it exists? For now, ARM is safe.
66 */
Trevor Woerner10015022019-05-03 09:41:00 -040067#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass9a8efc42012-10-30 13:40:18 +000068 int line_length;
69
70 if (lcd_flush_dcache)
Alexander Graff8f58fb2016-03-16 15:41:22 +010071 flush_dcache_range((ulong)lcd_base,
72 (ulong)(lcd_base + lcd_get_size(&line_length)));
Simon Glass9a8efc42012-10-30 13:40:18 +000073#endif
74}
75
76void lcd_set_flush_dcache(int flush)
77{
78 lcd_flush_dcache = (flush != 0);
79}
80
Simon Glass709ea542014-07-23 06:54:59 -060081static void lcd_stub_putc(struct stdio_dev *dev, const char c)
82{
83 lcd_putc(c);
84}
85
Simon Glass709ea542014-07-23 06:54:59 -060086static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
87{
88 lcd_puts(s);
89}
90
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +020091/* Small utility to check that you got the colours right */
wdenk8655b6f2004-10-09 23:25:58 +000092#ifdef LCD_TEST_PATTERN
93
Andreas Neubachere32951b2016-01-21 13:06:32 +010094#if LCD_BPP == LCD_COLOR8
wdenk8655b6f2004-10-09 23:25:58 +000095#define N_BLK_VERT 2
96#define N_BLK_HOR 3
97
Jeroen Hofstee6b035142013-01-12 12:07:56 +000098static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
wdenk8655b6f2004-10-09 23:25:58 +000099 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
100 CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
Andreas Neubachere32951b2016-01-21 13:06:32 +0100101}; /*LCD_BPP == LCD_COLOR8 */
102
103#elif LCD_BPP == LCD_COLOR16
104#define N_BLK_VERT 2
105#define N_BLK_HOR 4
106
107static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
108 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW, CONSOLE_COLOR_BLUE,
109 CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN, CONSOLE_COLOR_GREY, CONSOLE_COLOR_WHITE,
wdenk8655b6f2004-10-09 23:25:58 +0000110};
Andreas Neubachere32951b2016-01-21 13:06:32 +0100111#endif /*LCD_BPP == LCD_COLOR16 */
wdenk8655b6f2004-10-09 23:25:58 +0000112
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000113static void test_pattern(void)
wdenk8655b6f2004-10-09 23:25:58 +0000114{
115 ushort v_max = panel_info.vl_row;
116 ushort h_max = panel_info.vl_col;
117 ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
118 ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
119 ushort v, h;
Andreas Neubachere32951b2016-01-21 13:06:32 +0100120#if LCD_BPP == LCD_COLOR8
wdenk8655b6f2004-10-09 23:25:58 +0000121 uchar *pix = (uchar *)lcd_base;
Andreas Neubachere32951b2016-01-21 13:06:32 +0100122#elif LCD_BPP == LCD_COLOR16
123 ushort *pix = (ushort *)lcd_base;
124#endif
wdenk8655b6f2004-10-09 23:25:58 +0000125
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000126 printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
wdenk8655b6f2004-10-09 23:25:58 +0000127 h_max, v_max, h_step, v_step);
128
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000129 for (v = 0; v < v_max; ++v) {
wdenk8655b6f2004-10-09 23:25:58 +0000130 uchar iy = v / v_step;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000131 for (h = 0; h < h_max; ++h) {
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000132 uchar ix = N_BLK_HOR * iy + h / h_step;
wdenk8655b6f2004-10-09 23:25:58 +0000133 *pix++ = test_colors[ix];
134 }
135 }
136}
137#endif /* LCD_TEST_PATTERN */
138
Anatolij Gustschincefa4712013-11-09 11:00:09 +0100139/*
140 * With most lcd drivers the line length is set up
141 * by calculating it from panel_info parameters. Some
142 * drivers need to calculate the line length differently,
143 * so make the function weak to allow overriding it.
144 */
145__weak int lcd_get_size(int *line_length)
Simon Glass676d3192012-10-17 13:24:54 +0000146{
147 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
148 return *line_length * panel_info.vl_row;
149}
150
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000151int drv_lcd_init(void)
wdenk8655b6f2004-10-09 23:25:58 +0000152{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200153 struct stdio_dev lcddev;
wdenk8655b6f2004-10-09 23:25:58 +0000154 int rc;
155
Simon Glass7d95f2a2014-02-27 13:26:19 -0700156 lcd_base = map_sysmem(gd->fb_base, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000157
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200158 lcd_init(lcd_base);
wdenk8655b6f2004-10-09 23:25:58 +0000159
160 /* Device initialization */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000161 memset(&lcddev, 0, sizeof(lcddev));
wdenk8655b6f2004-10-09 23:25:58 +0000162
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000163 strcpy(lcddev.name, "lcd");
wdenk8655b6f2004-10-09 23:25:58 +0000164 lcddev.ext = 0; /* No extensions */
165 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
Simon Glass709ea542014-07-23 06:54:59 -0600166 lcddev.putc = lcd_stub_putc; /* 'putc' function */
167 lcddev.puts = lcd_stub_puts; /* 'puts' function */
wdenk8655b6f2004-10-09 23:25:58 +0000168
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000169 rc = stdio_register(&lcddev);
wdenk8655b6f2004-10-09 23:25:58 +0000170
171 return (rc == 0) ? 1 : rc;
172}
173
Che-Liang Chiou02110902011-10-20 23:07:03 +0000174void lcd_clear(void)
wdenk8655b6f2004-10-09 23:25:58 +0000175{
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200176 int bg_color;
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000177 __maybe_unused ulong addr;
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200178 static int do_splash = 1;
Nikita Kiryanovf4469f52014-12-08 17:14:38 +0200179#if LCD_BPP == LCD_COLOR8
wdenk8655b6f2004-10-09 23:25:58 +0000180 /* Setting the palette */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000181 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
182 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
183 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
184 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
185 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
186 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
187 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
188 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
189 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
wdenk8655b6f2004-10-09 23:25:58 +0000190#endif
191
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200192#ifndef CONFIG_SYS_WHITE_ON_BLACK
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000193 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
194 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200195 bg_color = CONSOLE_COLOR_WHITE;
wdenk8655b6f2004-10-09 23:25:58 +0000196#else
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000197 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
198 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200199 bg_color = CONSOLE_COLOR_BLACK;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200200#endif /* CONFIG_SYS_WHITE_ON_BLACK */
wdenk8655b6f2004-10-09 23:25:58 +0000201
202#ifdef LCD_TEST_PATTERN
203 test_pattern();
204#else
205 /* set framebuffer to background color */
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100206#if (LCD_BPP != LCD_COLOR32)
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200207 memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100208#else
209 u32 *ppix = lcd_base;
210 u32 i;
211 for (i = 0;
212 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
213 i++) {
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200214 *ppix++ = bg_color;
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100215 }
216#endif
wdenk8655b6f2004-10-09 23:25:58 +0000217#endif
Hannes Petermaier604c7d42015-03-27 08:01:38 +0100218 /* setup text-console */
219 debug("[LCD] setting up console...\n");
220 lcd_init_console(lcd_base,
221 panel_info.vl_col,
222 panel_info.vl_row,
223 panel_info.vl_rot);
wdenk8655b6f2004-10-09 23:25:58 +0000224 /* Paint the logo and retrieve LCD base address */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000225 debug("[LCD] Drawing the logo...\n");
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200226 if (do_splash) {
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000227 if (splash_display() == 0) {
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200228 do_splash = 0;
Igor Opaniuk5eb83c02019-05-29 09:01:43 +0000229 lcd_sync();
230 return;
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200231 }
232 }
233
234 lcd_logo();
235#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
236 addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
Marcel Ziswiler3b96b902015-08-04 15:49:50 +0200237 lcd_init_console((void *)addr, panel_info.vl_col,
238 panel_info.vl_row, panel_info.vl_rot);
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200239#endif
Simon Glass9a8efc42012-10-30 13:40:18 +0000240 lcd_sync();
241}
242
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000243static int lcd_init(void *lcdbase)
wdenk8655b6f2004-10-09 23:25:58 +0000244{
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000245 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000246 lcd_ctrl_init(lcdbase);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100247
248 /*
Stephen Warren9316e142014-11-19 20:41:03 -0700249 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100250 * the 'lcdbase' argument and uses custom lcd base address
251 * by setting up gd->fb_base. Check for this condition and fixup
252 * 'lcd_base' address.
253 */
Simon Glass7d95f2a2014-02-27 13:26:19 -0700254 if (map_to_sysmem(lcdbase) != gd->fb_base)
255 lcd_base = map_sysmem(gd->fb_base, 0);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100256
257 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
258
Stephen Warren6d330712013-01-29 16:37:38 +0000259 lcd_get_size(&lcd_line_length);
Haavard Skinnemoen6f93d2b2008-09-01 16:21:21 +0200260 lcd_is_enabled = 1;
Che-Liang Chiou02110902011-10-20 23:07:03 +0000261 lcd_clear();
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000262 lcd_enable();
wdenk8655b6f2004-10-09 23:25:58 +0000263
264 /* Initialize the console */
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200265 lcd_set_col(0);
wdenk88804d12005-07-04 00:03:16 +0000266#ifdef CONFIG_LCD_INFO_BELOW_LOGO
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200267 lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
wdenk8655b6f2004-10-09 23:25:58 +0000268#else
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200269 lcd_set_row(1); /* leave 1 blank line below logo */
wdenk8655b6f2004-10-09 23:25:58 +0000270#endif
wdenk8655b6f2004-10-09 23:25:58 +0000271
272 return 0;
273}
274
wdenk8655b6f2004-10-09 23:25:58 +0000275/*
276 * This is called early in the system initialization to grab memory
277 * for the LCD controller.
278 * Returns new address for monitor, after reserving LCD buffer memory
279 *
280 * Note that this is running from ROM, so no write access to global data.
281 */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000282ulong lcd_setmem(ulong addr)
wdenk8655b6f2004-10-09 23:25:58 +0000283{
284 ulong size;
Simon Glass676d3192012-10-17 13:24:54 +0000285 int line_length;
wdenk8655b6f2004-10-09 23:25:58 +0000286
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000287 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
288 panel_info.vl_row, NBITS(panel_info.vl_bpix));
wdenk8655b6f2004-10-09 23:25:58 +0000289
Simon Glass676d3192012-10-17 13:24:54 +0000290 size = lcd_get_size(&line_length);
wdenk8655b6f2004-10-09 23:25:58 +0000291
Simon Glass676d3192012-10-17 13:24:54 +0000292 /* Round up to nearest full page, or MMU section if defined */
293 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
294 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
wdenk8655b6f2004-10-09 23:25:58 +0000295
296 /* Allocate pages for the frame buffer. */
297 addr -= size;
298
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000299 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
300 size >> 10, addr);
wdenk8655b6f2004-10-09 23:25:58 +0000301
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000302 return addr;
wdenk8655b6f2004-10-09 23:25:58 +0000303}
304
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000305static void lcd_setfgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000306{
Stelian Pop39cf4802008-05-09 21:57:18 +0200307 lcd_color_fg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000308}
309
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200310int lcd_getfgcolor(void)
311{
312 return lcd_color_fg;
313}
314
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000315static void lcd_setbgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000316{
Stelian Pop39cf4802008-05-09 21:57:18 +0200317 lcd_color_bg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000318}
319
Nikita Kiryanov4d036342014-12-08 17:14:43 +0200320int lcd_getbgcolor(void)
321{
322 return lcd_color_bg;
323}
324
wdenk8655b6f2004-10-09 23:25:58 +0000325#ifdef CONFIG_LCD_LOGO
Nikita Kiryanova02e9482015-02-03 13:32:24 +0200326__weak void lcd_logo_set_cmap(void)
327{
Nikita Kiryanov23064572015-02-03 13:32:26 +0200328 int i;
329 ushort *cmap = configuration_get_cmap();
330
331 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
332 *cmap++ = bmp_logo_palette[i];
Nikita Kiryanova02e9482015-02-03 13:32:24 +0200333}
334
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200335void lcd_logo_plot(int x, int y)
wdenk8655b6f2004-10-09 23:25:58 +0000336{
wdenk8655b6f2004-10-09 23:25:58 +0000337 ushort i, j;
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200338 uchar *bmap = &bmp_logo_bitmap[0];
Andre Renaud317461c2013-02-13 17:48:00 +0000339 unsigned bpix = NBITS(panel_info.vl_bpix);
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200340 uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
341 ushort *fb16;
wdenk8655b6f2004-10-09 23:25:58 +0000342
Nikita Kiryanov23064572015-02-03 13:32:26 +0200343 debug("Logo: width %d height %d colors %d\n",
344 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
wdenk8655b6f2004-10-09 23:25:58 +0000345
Andre Renaud317461c2013-02-13 17:48:00 +0000346 if (bpix < 12) {
wdenk8655b6f2004-10-09 23:25:58 +0000347 WATCHDOG_RESET();
Nikita Kiryanova02e9482015-02-03 13:32:24 +0200348 lcd_logo_set_cmap();
wdenk8655b6f2004-10-09 23:25:58 +0000349 WATCHDOG_RESET();
350
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000351 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
352 memcpy(fb, bmap, BMP_LOGO_WIDTH);
wdenk8655b6f2004-10-09 23:25:58 +0000353 bmap += BMP_LOGO_WIDTH;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000354 fb += panel_info.vl_col;
wdenk8655b6f2004-10-09 23:25:58 +0000355 }
356 }
357 else { /* true color mode */
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100358 u16 col16;
Andre Renaud317461c2013-02-13 17:48:00 +0000359 fb16 = (ushort *)fb;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000360 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
361 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100362 col16 = bmp_logo_palette[(bmap[j]-16)];
363 fb16[j] =
364 ((col16 & 0x000F) << 1) |
365 ((col16 & 0x00F0) << 3) |
366 ((col16 & 0x0F00) << 4);
wdenk8655b6f2004-10-09 23:25:58 +0000367 }
368 bmap += BMP_LOGO_WIDTH;
369 fb16 += panel_info.vl_col;
370 }
371 }
372
373 WATCHDOG_RESET();
Simon Glass9a8efc42012-10-30 13:40:18 +0000374 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000375}
Anatolij Gustschin2b5cb3d2012-04-27 04:41:27 +0000376#else
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200377static inline void lcd_logo_plot(int x, int y) {}
wdenk8655b6f2004-10-09 23:25:58 +0000378#endif /* CONFIG_LCD_LOGO */
379
Jon Loeligerc3517f92007-07-08 18:10:08 -0500380#if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200381#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000382
383static void splash_align_axis(int *axis, unsigned long panel_size,
384 unsigned long picture_size)
385{
386 unsigned long panel_picture_delta = panel_size - picture_size;
387 unsigned long axis_alignment;
388
389 if (*axis == BMP_ALIGN_CENTER)
390 axis_alignment = panel_picture_delta / 2;
391 else if (*axis < 0)
392 axis_alignment = panel_picture_delta + *axis + 1;
393 else
394 return;
395
Masahiro Yamadab4141192014-11-07 03:03:31 +0900396 *axis = max(0, (int)axis_alignment);
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000397}
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200398#endif
399
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000400#ifdef CONFIG_LCD_BMP_RLE8
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000401#define BMP_RLE8_ESCAPE 0
402#define BMP_RLE8_EOL 0
403#define BMP_RLE8_EOBMP 1
404#define BMP_RLE8_DELTA 2
405
406static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
407 int cnt)
408{
409 while (cnt > 0) {
410 *(*fbp)++ = cmap[*bmap++];
411 cnt--;
412 }
413}
414
415static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
416{
417 ushort *fb = *fbp;
418 int cnt_8copy = cnt >> 3;
419
420 cnt -= cnt_8copy << 3;
421 while (cnt_8copy > 0) {
422 *fb++ = c;
423 *fb++ = c;
424 *fb++ = c;
425 *fb++ = c;
426 *fb++ = c;
427 *fb++ = c;
428 *fb++ = c;
429 *fb++ = c;
430 cnt_8copy--;
431 }
432 while (cnt > 0) {
433 *fb++ = c;
434 cnt--;
435 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000436 *fbp = fb;
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000437}
438
439/*
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000440 * Do not call this function directly, must be called from lcd_display_bitmap.
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000441 */
Simon Glass1c3dbe52015-05-13 07:02:27 -0600442static void lcd_display_rle8_bitmap(struct bmp_image *bmp, ushort *cmap,
443 uchar *fb, int x_off, int y_off)
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000444{
445 uchar *bmap;
446 ulong width, height;
447 ulong cnt, runlen;
448 int x, y;
449 int decode = 1;
450
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100451 width = get_unaligned_le32(&bmp->header.width);
452 height = get_unaligned_le32(&bmp->header.height);
453 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000454
455 x = 0;
456 y = height - 1;
457
458 while (decode) {
459 if (bmap[0] == BMP_RLE8_ESCAPE) {
460 switch (bmap[1]) {
461 case BMP_RLE8_EOL:
462 /* end of line */
463 bmap += 2;
464 x = 0;
465 y--;
466 /* 16bpix, 2-byte per pixel, width should *2 */
467 fb -= (width * 2 + lcd_line_length);
468 break;
469 case BMP_RLE8_EOBMP:
470 /* end of bitmap */
471 decode = 0;
472 break;
473 case BMP_RLE8_DELTA:
474 /* delta run */
475 x += bmap[2];
476 y -= bmap[3];
477 /* 16bpix, 2-byte per pixel, x should *2 */
478 fb = (uchar *) (lcd_base + (y + y_off - 1)
479 * lcd_line_length + (x + x_off) * 2);
480 bmap += 4;
481 break;
482 default:
483 /* unencoded run */
484 runlen = bmap[1];
485 bmap += 2;
486 if (y < height) {
487 if (x < width) {
488 if (x + runlen > width)
489 cnt = width - x;
490 else
491 cnt = runlen;
492 draw_unencoded_bitmap(
493 (ushort **)&fb,
494 bmap, cmap, cnt);
495 }
496 x += runlen;
497 }
498 bmap += runlen;
499 if (runlen & 1)
500 bmap++;
501 }
502 } else {
503 /* encoded run */
504 if (y < height) {
505 runlen = bmap[0];
506 if (x < width) {
507 /* aggregate the same code */
508 while (bmap[0] == 0xff &&
509 bmap[2] != BMP_RLE8_ESCAPE &&
510 bmap[1] == bmap[3]) {
511 runlen += bmap[2];
512 bmap += 2;
513 }
514 if (x + runlen > width)
515 cnt = width - x;
516 else
517 cnt = runlen;
518 draw_encoded_bitmap((ushort **)&fb,
519 cmap[bmap[1]], cnt);
520 }
521 x += runlen;
522 }
523 bmap += 2;
524 }
525 }
526}
527#endif
528
Nikita Kiryanov27fad012015-02-03 13:32:23 +0200529__weak void fb_put_byte(uchar **fb, uchar **from)
530{
531 *(*fb)++ = *(*from)++;
532}
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000533
534#if defined(CONFIG_BMP_16BPP)
Nikita Kiryanovb3d12e92015-02-03 13:32:22 +0200535__weak void fb_put_word(uchar **fb, uchar **from)
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000536{
537 *(*fb)++ = *(*from)++;
538 *(*fb)++ = *(*from)++;
539}
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000540#endif /* CONFIG_BMP_16BPP */
541
Simon Glass1c3dbe52015-05-13 07:02:27 -0600542__weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200543{
544 int i;
Simon Glass1c3dbe52015-05-13 07:02:27 -0600545 struct bmp_color_table_entry cte;
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200546 ushort *cmap = configuration_get_cmap();
547
548 for (i = 0; i < colors; ++i) {
549 cte = bmp->color_table[i];
550 *cmap = (((cte.red) << 8) & 0xf800) |
551 (((cte.green) << 3) & 0x07e0) |
552 (((cte.blue) >> 3) & 0x001f);
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200553 cmap++;
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200554 }
555}
556
wdenk8655b6f2004-10-09 23:25:58 +0000557int lcd_display_bitmap(ulong bmp_image, int x, int y)
558{
Anatolij Gustschin00cc5592009-02-25 20:28:13 +0100559 ushort *cmap_base = NULL;
wdenk8655b6f2004-10-09 23:25:58 +0000560 ushort i, j;
561 uchar *fb;
Simon Glass1c3dbe52015-05-13 07:02:27 -0600562 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000563 uchar *bmap;
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000564 ushort padded_width;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100565 unsigned long width, height, byte_width;
Wolfgang Denke8143e72006-08-30 23:09:00 +0200566 unsigned long pwidth = panel_info.vl_col;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100567 unsigned colors, bpix, bmp_bpix;
Simon Glass8d379f12015-05-13 07:02:28 -0600568 int hdr_size;
xypron.glpk@gmx.de021414a2017-07-30 21:59:23 +0200569 struct bmp_color_table_entry *palette;
wdenk8655b6f2004-10-09 23:25:58 +0000570
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000571 if (!bmp || !(bmp->header.signature[0] == 'B' &&
572 bmp->header.signature[1] == 'M')) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000573 printf("Error: no valid bmp image at %lx\n", bmp_image);
574
wdenk8655b6f2004-10-09 23:25:58 +0000575 return 1;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100576 }
wdenk8655b6f2004-10-09 23:25:58 +0000577
xypron.glpk@gmx.de021414a2017-07-30 21:59:23 +0200578 palette = bmp->color_table;
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100579 width = get_unaligned_le32(&bmp->header.width);
580 height = get_unaligned_le32(&bmp->header.height);
581 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
Simon Glass8d379f12015-05-13 07:02:28 -0600582 hdr_size = get_unaligned_le16(&bmp->header.size);
583 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100584
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100585 colors = 1 << bmp_bpix;
wdenk8655b6f2004-10-09 23:25:58 +0000586
587 bpix = NBITS(panel_info.vl_bpix);
588
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000589 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100590 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
591 bpix, bmp_bpix);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000592
wdenk8655b6f2004-10-09 23:25:58 +0000593 return 1;
594 }
595
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200596 /*
597 * We support displaying 8bpp BMPs on 16bpp LCDs
598 * and displaying 24bpp BMPs on 32bpp LCDs
599 * */
600 if (bpix != bmp_bpix &&
601 !(bmp_bpix == 8 && bpix == 16) &&
602 !(bmp_bpix == 24 && bpix == 32)) {
wdenk8655b6f2004-10-09 23:25:58 +0000603 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100604 bpix, get_unaligned_le16(&bmp->header.bit_count));
wdenk8655b6f2004-10-09 23:25:58 +0000605 return 1;
606 }
607
Simon Glass8d379f12015-05-13 07:02:28 -0600608 debug("Display-bmp: %d x %d with %d colors, display %d\n",
609 (int)width, (int)height, (int)colors, 1 << bpix);
wdenk8655b6f2004-10-09 23:25:58 +0000610
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200611 if (bmp_bpix == 8)
612 lcd_set_cmap(bmp, colors);
Wolfgang Denke8143e72006-08-30 23:09:00 +0200613
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000614 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200615
616#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000617 splash_align_axis(&x, pwidth, width);
618 splash_align_axis(&y, panel_info.vl_row, height);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200619#endif /* CONFIG_SPLASH_SCREEN_ALIGN */
620
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000621 if ((x + width) > pwidth)
Wolfgang Denke8143e72006-08-30 23:09:00 +0200622 width = pwidth - x;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000623 if ((y + height) > panel_info.vl_row)
wdenk8655b6f2004-10-09 23:25:58 +0000624 height = panel_info.vl_row - y;
625
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100626 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
627 fb = (uchar *)(lcd_base +
Liu Ying8d46d5b2011-01-11 15:29:58 +0800628 (y + height - 1) * lcd_line_length + x * bpix / 8);
Mark Jacksona303dfb2009-02-06 10:37:49 +0100629
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100630 switch (bmp_bpix) {
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200631 case 1:
Simon Glass01564442014-10-15 04:53:04 -0600632 case 8: {
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200633 cmap_base = configuration_get_cmap();
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000634#ifdef CONFIG_LCD_BMP_RLE8
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100635 u32 compression = get_unaligned_le32(&bmp->header.compression);
Simon Glass8d379f12015-05-13 07:02:28 -0600636 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100637 if (compression == BMP_BI_RLE8) {
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000638 if (bpix != 16) {
639 /* TODO implement render code for bpix != 16 */
640 printf("Error: only support 16 bpix");
641 return 1;
642 }
643 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
644 break;
645 }
646#endif
647
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100648 if (bpix != 16)
649 byte_width = width;
650 else
651 byte_width = width * 2;
652
Mark Jacksona303dfb2009-02-06 10:37:49 +0100653 for (i = 0; i < height; ++i) {
654 WATCHDOG_RESET();
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100655 for (j = 0; j < width; j++) {
656 if (bpix != 16) {
Nikita Kiryanov27fad012015-02-03 13:32:23 +0200657 fb_put_byte(&fb, &bmap);
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100658 } else {
Simon Glass8d379f12015-05-13 07:02:28 -0600659 struct bmp_color_table_entry *entry;
660 uint val;
661
662 if (cmap_base) {
663 val = cmap_base[*bmap];
664 } else {
665 entry = &palette[*bmap];
666 val = entry->blue >> 3 |
667 entry->green >> 2 << 5 |
668 entry->red >> 3 << 11;
669 }
670 *(uint16_t *)fb = val;
671 bmap++;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100672 fb += sizeof(uint16_t) / sizeof(*fb);
673 }
674 }
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000675 bmap += (padded_width - width);
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000676 fb -= byte_width + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +0100677 }
678 break;
Simon Glass01564442014-10-15 04:53:04 -0600679 }
Mark Jacksona303dfb2009-02-06 10:37:49 +0100680#if defined(CONFIG_BMP_16BPP)
681 case 16:
682 for (i = 0; i < height; ++i) {
683 WATCHDOG_RESET();
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000684 for (j = 0; j < width; j++)
685 fb_put_word(&fb, &bmap);
686
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000687 bmap += (padded_width - width) * 2;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000688 fb -= width * 2 + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +0100689 }
690 break;
691#endif /* CONFIG_BMP_16BPP */
Philipp Tomsich10ba6b32017-05-05 21:48:30 +0200692#if defined(CONFIG_BMP_24BPP)
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200693 case 24:
694 for (i = 0; i < height; ++i) {
695 for (j = 0; j < width; j++) {
696 *(fb++) = *(bmap++);
697 *(fb++) = *(bmap++);
698 *(fb++) = *(bmap++);
699 *(fb++) = 0;
700 }
701 fb -= lcd_line_length + width * (bpix / 8);
702 }
703 break;
Philipp Tomsich10ba6b32017-05-05 21:48:30 +0200704#endif /* CONFIG_BMP_24BPP */
Donghwa Leefb6a9aa2012-05-09 19:23:37 +0000705#if defined(CONFIG_BMP_32BPP)
706 case 32:
707 for (i = 0; i < height; ++i) {
708 for (j = 0; j < width; j++) {
709 *(fb++) = *(bmap++);
710 *(fb++) = *(bmap++);
711 *(fb++) = *(bmap++);
712 *(fb++) = *(bmap++);
713 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000714 fb -= lcd_line_length + width * (bpix / 8);
Donghwa Leefb6a9aa2012-05-09 19:23:37 +0000715 }
716 break;
717#endif /* CONFIG_BMP_32BPP */
Mark Jacksona303dfb2009-02-06 10:37:49 +0100718 default:
719 break;
720 };
wdenk8655b6f2004-10-09 23:25:58 +0000721
Simon Glass9a8efc42012-10-30 13:40:18 +0000722 lcd_sync();
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000723 return 0;
wdenk8655b6f2004-10-09 23:25:58 +0000724}
Jon Loeligerc3517f92007-07-08 18:10:08 -0500725#endif
wdenk8655b6f2004-10-09 23:25:58 +0000726
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200727static void lcd_logo(void)
wdenk8655b6f2004-10-09 23:25:58 +0000728{
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200729 lcd_logo_plot(0, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000730
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +0200731#ifdef CONFIG_LCD_INFO
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200732 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
733 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +0200734 lcd_show_board_info();
735#endif /* CONFIG_LCD_INFO */
wdenk8655b6f2004-10-09 23:25:58 +0000736}
737
Nikita Kiryanovc0880482013-02-24 21:28:43 +0000738#ifdef CONFIG_SPLASHIMAGE_GUARD
739static int on_splashimage(const char *name, const char *value, enum env_op op,
740 int flags)
741{
742 ulong addr;
743 int aligned;
744
745 if (op == env_op_delete)
746 return 0;
747
748 addr = simple_strtoul(value, NULL, 16);
749 /* See README.displaying-bmps */
750 aligned = (addr % 4 == 2);
751 if (!aligned) {
752 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
753 return -1;
754 }
755
756 return 0;
757}
758
759U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
760#endif
761
Vadim Bendebury395166c2012-09-28 15:11:13 +0000762int lcd_get_pixel_width(void)
763{
764 return panel_info.vl_col;
765}
766
767int lcd_get_pixel_height(void)
768{
769 return panel_info.vl_row;
770}