blob: 0898bc025d679758eb851372e35edca58f3582f4 [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
Nikita Kiryanov27fad012015-02-03 13:32:23 +0200350__weak void fb_put_byte(uchar **fb, uchar **from)
351{
352 *(*fb)++ = *(*from)++;
353}
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000354
355#if defined(CONFIG_BMP_16BPP)
Nikita Kiryanovb3d12e92015-02-03 13:32:22 +0200356__weak void fb_put_word(uchar **fb, uchar **from)
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000357{
358 *(*fb)++ = *(*from)++;
359 *(*fb)++ = *(*from)++;
360}
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000361#endif /* CONFIG_BMP_16BPP */
362
Simon Glass1c3dbe52015-05-13 07:02:27 -0600363__weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200364{
365 int i;
Simon Glass1c3dbe52015-05-13 07:02:27 -0600366 struct bmp_color_table_entry cte;
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200367 ushort *cmap = configuration_get_cmap();
368
369 for (i = 0; i < colors; ++i) {
370 cte = bmp->color_table[i];
371 *cmap = (((cte.red) << 8) & 0xf800) |
372 (((cte.green) << 3) & 0x07e0) |
373 (((cte.blue) >> 3) & 0x001f);
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200374 cmap++;
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200375 }
376}
377
wdenk8655b6f2004-10-09 23:25:58 +0000378int lcd_display_bitmap(ulong bmp_image, int x, int y)
379{
Anatolij Gustschin00cc5592009-02-25 20:28:13 +0100380 ushort *cmap_base = NULL;
wdenk8655b6f2004-10-09 23:25:58 +0000381 ushort i, j;
382 uchar *fb;
Simon Glass1c3dbe52015-05-13 07:02:27 -0600383 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000384 uchar *bmap;
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000385 ushort padded_width;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100386 unsigned long width, height, byte_width;
Wolfgang Denke8143e72006-08-30 23:09:00 +0200387 unsigned long pwidth = panel_info.vl_col;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100388 unsigned colors, bpix, bmp_bpix;
Simon Glass8d379f12015-05-13 07:02:28 -0600389 int hdr_size;
xypron.glpk@gmx.de021414a2017-07-30 21:59:23 +0200390 struct bmp_color_table_entry *palette;
wdenk8655b6f2004-10-09 23:25:58 +0000391
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000392 if (!bmp || !(bmp->header.signature[0] == 'B' &&
393 bmp->header.signature[1] == 'M')) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000394 printf("Error: no valid bmp image at %lx\n", bmp_image);
395
wdenk8655b6f2004-10-09 23:25:58 +0000396 return 1;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100397 }
wdenk8655b6f2004-10-09 23:25:58 +0000398
xypron.glpk@gmx.de021414a2017-07-30 21:59:23 +0200399 palette = bmp->color_table;
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100400 width = get_unaligned_le32(&bmp->header.width);
401 height = get_unaligned_le32(&bmp->header.height);
402 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
Simon Glass8d379f12015-05-13 07:02:28 -0600403 hdr_size = get_unaligned_le16(&bmp->header.size);
404 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100405
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100406 colors = 1 << bmp_bpix;
wdenk8655b6f2004-10-09 23:25:58 +0000407
408 bpix = NBITS(panel_info.vl_bpix);
409
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000410 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100411 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
412 bpix, bmp_bpix);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000413
wdenk8655b6f2004-10-09 23:25:58 +0000414 return 1;
415 }
416
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200417 /*
418 * We support displaying 8bpp BMPs on 16bpp LCDs
419 * and displaying 24bpp BMPs on 32bpp LCDs
420 * */
421 if (bpix != bmp_bpix &&
422 !(bmp_bpix == 8 && bpix == 16) &&
423 !(bmp_bpix == 24 && bpix == 32)) {
wdenk8655b6f2004-10-09 23:25:58 +0000424 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100425 bpix, get_unaligned_le16(&bmp->header.bit_count));
wdenk8655b6f2004-10-09 23:25:58 +0000426 return 1;
427 }
428
Simon Glass8d379f12015-05-13 07:02:28 -0600429 debug("Display-bmp: %d x %d with %d colors, display %d\n",
430 (int)width, (int)height, (int)colors, 1 << bpix);
wdenk8655b6f2004-10-09 23:25:58 +0000431
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200432 if (bmp_bpix == 8)
433 lcd_set_cmap(bmp, colors);
Wolfgang Denke8143e72006-08-30 23:09:00 +0200434
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000435 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200436
437#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000438 splash_align_axis(&x, pwidth, width);
439 splash_align_axis(&y, panel_info.vl_row, height);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200440#endif /* CONFIG_SPLASH_SCREEN_ALIGN */
441
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000442 if ((x + width) > pwidth)
Wolfgang Denke8143e72006-08-30 23:09:00 +0200443 width = pwidth - x;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000444 if ((y + height) > panel_info.vl_row)
wdenk8655b6f2004-10-09 23:25:58 +0000445 height = panel_info.vl_row - y;
446
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100447 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
448 fb = (uchar *)(lcd_base +
Liu Ying8d46d5b2011-01-11 15:29:58 +0800449 (y + height - 1) * lcd_line_length + x * bpix / 8);
Mark Jacksona303dfb2009-02-06 10:37:49 +0100450
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100451 switch (bmp_bpix) {
Nikita Kiryanovc8d2feb2015-02-03 13:32:29 +0200452 case 1:
Simon Glass01564442014-10-15 04:53:04 -0600453 case 8: {
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200454 cmap_base = configuration_get_cmap();
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000455
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100456 if (bpix != 16)
457 byte_width = width;
458 else
459 byte_width = width * 2;
460
Mark Jacksona303dfb2009-02-06 10:37:49 +0100461 for (i = 0; i < height; ++i) {
462 WATCHDOG_RESET();
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100463 for (j = 0; j < width; j++) {
464 if (bpix != 16) {
Nikita Kiryanov27fad012015-02-03 13:32:23 +0200465 fb_put_byte(&fb, &bmap);
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100466 } else {
Simon Glass8d379f12015-05-13 07:02:28 -0600467 struct bmp_color_table_entry *entry;
468 uint val;
469
470 if (cmap_base) {
471 val = cmap_base[*bmap];
472 } else {
473 entry = &palette[*bmap];
474 val = entry->blue >> 3 |
475 entry->green >> 2 << 5 |
476 entry->red >> 3 << 11;
477 }
478 *(uint16_t *)fb = val;
479 bmap++;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100480 fb += sizeof(uint16_t) / sizeof(*fb);
481 }
482 }
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000483 bmap += (padded_width - width);
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000484 fb -= byte_width + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +0100485 }
486 break;
Simon Glass01564442014-10-15 04:53:04 -0600487 }
Mark Jacksona303dfb2009-02-06 10:37:49 +0100488#if defined(CONFIG_BMP_16BPP)
489 case 16:
490 for (i = 0; i < height; ++i) {
491 WATCHDOG_RESET();
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000492 for (j = 0; j < width; j++)
493 fb_put_word(&fb, &bmap);
494
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000495 bmap += (padded_width - width) * 2;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000496 fb -= width * 2 + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +0100497 }
498 break;
499#endif /* CONFIG_BMP_16BPP */
Philipp Tomsich10ba6b32017-05-05 21:48:30 +0200500#if defined(CONFIG_BMP_24BPP)
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200501 case 24:
502 for (i = 0; i < height; ++i) {
503 for (j = 0; j < width; j++) {
504 *(fb++) = *(bmap++);
505 *(fb++) = *(bmap++);
506 *(fb++) = *(bmap++);
507 *(fb++) = 0;
508 }
509 fb -= lcd_line_length + width * (bpix / 8);
510 }
511 break;
Philipp Tomsich10ba6b32017-05-05 21:48:30 +0200512#endif /* CONFIG_BMP_24BPP */
Donghwa Leefb6a9aa2012-05-09 19:23:37 +0000513#if defined(CONFIG_BMP_32BPP)
514 case 32:
515 for (i = 0; i < height; ++i) {
516 for (j = 0; j < width; j++) {
517 *(fb++) = *(bmap++);
518 *(fb++) = *(bmap++);
519 *(fb++) = *(bmap++);
520 *(fb++) = *(bmap++);
521 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000522 fb -= lcd_line_length + width * (bpix / 8);
Donghwa Leefb6a9aa2012-05-09 19:23:37 +0000523 }
524 break;
525#endif /* CONFIG_BMP_32BPP */
Mark Jacksona303dfb2009-02-06 10:37:49 +0100526 default:
527 break;
528 };
wdenk8655b6f2004-10-09 23:25:58 +0000529
Simon Glass9a8efc42012-10-30 13:40:18 +0000530 lcd_sync();
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000531 return 0;
wdenk8655b6f2004-10-09 23:25:58 +0000532}
Jon Loeligerc3517f92007-07-08 18:10:08 -0500533#endif
wdenk8655b6f2004-10-09 23:25:58 +0000534
Nikita Kiryanov7bf71d12015-02-03 13:32:32 +0200535static void lcd_logo(void)
wdenk8655b6f2004-10-09 23:25:58 +0000536{
Nikita Kiryanovbf21a5d2015-02-03 13:32:30 +0200537 lcd_logo_plot(0, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000538
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +0200539#ifdef CONFIG_LCD_INFO
Nikita Kiryanov140beb92014-12-08 17:14:41 +0200540 lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
541 lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +0200542 lcd_show_board_info();
543#endif /* CONFIG_LCD_INFO */
wdenk8655b6f2004-10-09 23:25:58 +0000544}
545
Nikita Kiryanovc0880482013-02-24 21:28:43 +0000546#ifdef CONFIG_SPLASHIMAGE_GUARD
547static int on_splashimage(const char *name, const char *value, enum env_op op,
548 int flags)
549{
550 ulong addr;
551 int aligned;
552
553 if (op == env_op_delete)
554 return 0;
555
Simon Glass7e5f4602021-07-24 09:03:29 -0600556 addr = hextoul(value, NULL);
Nikita Kiryanovc0880482013-02-24 21:28:43 +0000557 /* See README.displaying-bmps */
558 aligned = (addr % 4 == 2);
559 if (!aligned) {
560 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
561 return -1;
562 }
563
564 return 0;
565}
566
567U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
568#endif
569
Vadim Bendebury395166c2012-09-28 15:11:13 +0000570int lcd_get_pixel_width(void)
571{
572 return panel_info.vl_col;
573}
574
575int lcd_get_pixel_height(void)
576{
577 return panel_info.vl_row;
578}