blob: 80923e91185f2056f9a0a45f42363fc17e31666b [file] [log] [blame]
wdenk8655b6f2004-10-09 23:25:58 +00001/*
2 * Common LCD routines for supported CPUs
3 *
4 * (C) Copyright 2001-2002
5 * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
6 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02007 * SPDX-License-Identifier: GPL-2.0+
wdenk8655b6f2004-10-09 23:25:58 +00008 */
9
10/************************************************************************/
11/* ** HEADER FILES */
12/************************************************************************/
13
14/* #define DEBUG */
15
16#include <config.h>
17#include <common.h>
18#include <command.h>
wdenk8655b6f2004-10-09 23:25:58 +000019#include <stdarg.h>
Nikita Kiryanovc0880482013-02-24 21:28:43 +000020#include <search.h>
21#include <env_callback.h>
wdenk8655b6f2004-10-09 23:25:58 +000022#include <linux/types.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020023#include <stdio_dev.h>
wdenk8655b6f2004-10-09 23:25:58 +000024#if defined(CONFIG_POST)
25#include <post.h>
26#endif
27#include <lcd.h>
wdenk8b0bfc62005-04-03 23:11:38 +000028#include <watchdog.h>
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +010029#include <asm/unaligned.h>
Robert Winklerdd4425e2013-06-17 11:31:29 -070030#include <splash.h>
Simon Glass7d95f2a2014-02-27 13:26:19 -070031#include <asm/io.h>
32#include <asm/unaligned.h>
Hans de Goede11b8dfa2014-11-19 13:53:27 +010033#include <fdt_support.h>
Robert Winklerdd4425e2013-06-17 11:31:29 -070034
Marek Vasutabc20ab2011-11-26 07:20:07 +010035#if defined(CONFIG_CPU_PXA25X) || defined(CONFIG_CPU_PXA27X) || \
36 defined(CONFIG_CPU_MONAHANS)
wdenk8655b6f2004-10-09 23:25:58 +000037#include <asm/byteorder.h>
38#endif
39
40#if defined(CONFIG_MPC823)
wdenk8655b6f2004-10-09 23:25:58 +000041#include <lcdvideo.h>
42#endif
43
Stelian Pop39cf4802008-05-09 21:57:18 +020044#if defined(CONFIG_ATMEL_LCD)
45#include <atmel_lcdc.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020046#endif
47
Stephen Warren6a195d22013-05-27 18:31:17 +000048#if defined(CONFIG_LCD_DT_SIMPLEFB)
49#include <libfdt.h>
50#endif
51
wdenk8655b6f2004-10-09 23:25:58 +000052/************************************************************************/
53/* ** FONT DATA */
54/************************************************************************/
55#include <video_font.h> /* Get font data, width and height */
56
wdenk88804d12005-07-04 00:03:16 +000057/************************************************************************/
58/* ** LOGO DATA */
59/************************************************************************/
60#ifdef CONFIG_LCD_LOGO
61# include <bmp_logo.h> /* Get logo data, width and height */
Che-Liang Chiouc2707302011-10-20 23:04:20 +000062# include <bmp_logo_data.h>
Alessandro Rubiniacb13862010-03-13 17:44:08 +010063# if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
wdenk88804d12005-07-04 00:03:16 +000064# error Default Color Map overlaps with Logo Color Map
65# endif
66#endif
wdenk8655b6f2004-10-09 23:25:58 +000067
Simon Glass7d95f2a2014-02-27 13:26:19 -070068#ifdef CONFIG_SANDBOX
69#include <asm/sdl.h>
70#endif
71
Simon Glass676d3192012-10-17 13:24:54 +000072#ifndef CONFIG_LCD_ALIGNMENT
73#define CONFIG_LCD_ALIGNMENT PAGE_SIZE
74#endif
75
Simon Glass0d89efe2012-10-17 13:24:59 +000076/* By default we scroll by a single line */
77#ifndef CONFIG_CONSOLE_SCROLL_LINES
78#define CONFIG_CONSOLE_SCROLL_LINES 1
79#endif
80
Jeroen Hofsteea5796c52013-01-12 12:07:59 +000081/************************************************************************/
82/* ** CONSOLE DEFINITIONS & FUNCTIONS */
83/************************************************************************/
84#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
85# define CONSOLE_ROWS ((panel_info.vl_row-BMP_LOGO_HEIGHT) \
86 / VIDEO_FONT_HEIGHT)
87#else
88# define CONSOLE_ROWS (panel_info.vl_row / VIDEO_FONT_HEIGHT)
89#endif
Wolfgang Denkd87080b2006-03-31 18:32:53 +020090
Jeroen Hofsteea5796c52013-01-12 12:07:59 +000091#define CONSOLE_COLS (panel_info.vl_col / VIDEO_FONT_WIDTH)
92#define CONSOLE_ROW_SIZE (VIDEO_FONT_HEIGHT * lcd_line_length)
93#define CONSOLE_ROW_FIRST lcd_console_address
94#define CONSOLE_ROW_SECOND (lcd_console_address + CONSOLE_ROW_SIZE)
95#define CONSOLE_ROW_LAST (lcd_console_address + CONSOLE_SIZE \
96 - CONSOLE_ROW_SIZE)
97#define CONSOLE_SIZE (CONSOLE_ROW_SIZE * CONSOLE_ROWS)
98#define CONSOLE_SCROLL_SIZE (CONSOLE_SIZE - CONSOLE_ROW_SIZE)
99
100#if LCD_BPP == LCD_MONOCHROME
101# define COLOR_MASK(c) ((c) | (c) << 1 | (c) << 2 | (c) << 3 | \
102 (c) << 4 | (c) << 5 | (c) << 6 | (c) << 7)
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100103#elif (LCD_BPP == LCD_COLOR8) || (LCD_BPP == LCD_COLOR16) || \
104 (LCD_BPP == LCD_COLOR32)
Jeroen Hofsteea5796c52013-01-12 12:07:59 +0000105# define COLOR_MASK(c) (c)
106#else
107# error Unsupported LCD BPP.
108#endif
109
wdenk8655b6f2004-10-09 23:25:58 +0000110DECLARE_GLOBAL_DATA_PTR;
wdenk8655b6f2004-10-09 23:25:58 +0000111
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000112static void lcd_drawchars(ushort x, ushort y, uchar *str, int count);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000113static inline void lcd_putc_xy(ushort x, ushort y, uchar c);
wdenk8655b6f2004-10-09 23:25:58 +0000114
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000115static int lcd_init(void *lcdbase);
wdenk8655b6f2004-10-09 23:25:58 +0000116
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000117static void *lcd_logo(void);
wdenk8655b6f2004-10-09 23:25:58 +0000118
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000119static void lcd_setfgcolor(int color);
120static void lcd_setbgcolor(int color);
wdenk8655b6f2004-10-09 23:25:58 +0000121
Wolfgang Denk46d1d5d2013-01-05 09:45:48 +0000122static int lcd_color_fg;
123static int lcd_color_bg;
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +0000124int lcd_line_length;
Wolfgang Denk46d1d5d2013-01-05 09:45:48 +0000125
wdenk8655b6f2004-10-09 23:25:58 +0000126char lcd_is_enabled = 0;
wdenk8655b6f2004-10-09 23:25:58 +0000127
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +0000128static short console_col;
129static short console_row;
130
131static void *lcd_console_address;
Jeroen Hofstee00a0ca52013-01-22 10:44:12 +0000132static void *lcd_base; /* Start of framebuffer memory */
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +0000133
Simon Glass9a8efc42012-10-30 13:40:18 +0000134static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
135
wdenk8655b6f2004-10-09 23:25:58 +0000136/************************************************************************/
137
Simon Glass9a8efc42012-10-30 13:40:18 +0000138/* Flush LCD activity to the caches */
139void lcd_sync(void)
140{
141 /*
142 * flush_dcache_range() is declared in common.h but it seems that some
143 * architectures do not actually implement it. Is there a way to find
144 * out whether it exists? For now, ARM is safe.
145 */
146#if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
147 int line_length;
148
149 if (lcd_flush_dcache)
150 flush_dcache_range((u32)lcd_base,
151 (u32)(lcd_base + lcd_get_size(&line_length)));
Simon Glass7d95f2a2014-02-27 13:26:19 -0700152#elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
153 static ulong last_sync;
154
155 if (get_timer(last_sync) > 10) {
156 sandbox_sdl_sync(lcd_base);
157 last_sync = get_timer(0);
158 }
Simon Glass9a8efc42012-10-30 13:40:18 +0000159#endif
160}
161
162void lcd_set_flush_dcache(int flush)
163{
164 lcd_flush_dcache = (flush != 0);
165}
166
wdenk8655b6f2004-10-09 23:25:58 +0000167/*----------------------------------------------------------------------*/
168
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000169static void console_scrollup(void)
wdenk8655b6f2004-10-09 23:25:58 +0000170{
Simon Glass0d89efe2012-10-17 13:24:59 +0000171 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
wdenk8655b6f2004-10-09 23:25:58 +0000172
Simon Glass0d89efe2012-10-17 13:24:59 +0000173 /* Copy up rows ignoring those that will be overwritten */
174 memcpy(CONSOLE_ROW_FIRST,
175 lcd_console_address + CONSOLE_ROW_SIZE * rows,
176 CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows);
177
178 /* Clear the last rows */
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100179#if (LCD_BPP != LCD_COLOR32)
Simon Glass0d89efe2012-10-17 13:24:59 +0000180 memset(lcd_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows,
181 COLOR_MASK(lcd_color_bg),
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000182 CONSOLE_ROW_SIZE * rows);
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100183#else
184 u32 *ppix = lcd_console_address +
185 CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows;
186 u32 i;
187 for (i = 0;
188 i < (CONSOLE_ROW_SIZE * rows) / NBYTES(panel_info.vl_bpix);
189 i++) {
190 *ppix++ = COLOR_MASK(lcd_color_bg);
191 }
192#endif
Simon Glass9a8efc42012-10-30 13:40:18 +0000193 lcd_sync();
Simon Glass0d89efe2012-10-17 13:24:59 +0000194 console_row -= rows;
wdenk8655b6f2004-10-09 23:25:58 +0000195}
196
197/*----------------------------------------------------------------------*/
198
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000199static inline void console_back(void)
wdenk8655b6f2004-10-09 23:25:58 +0000200{
201 if (--console_col < 0) {
202 console_col = CONSOLE_COLS-1 ;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000203 if (--console_row < 0)
wdenk8655b6f2004-10-09 23:25:58 +0000204 console_row = 0;
wdenk8655b6f2004-10-09 23:25:58 +0000205 }
206
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000207 lcd_putc_xy(console_col * VIDEO_FONT_WIDTH,
208 console_row * VIDEO_FONT_HEIGHT, ' ');
wdenk8655b6f2004-10-09 23:25:58 +0000209}
210
211/*----------------------------------------------------------------------*/
212
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000213static inline void console_newline(void)
wdenk8655b6f2004-10-09 23:25:58 +0000214{
wdenk8655b6f2004-10-09 23:25:58 +0000215 console_col = 0;
216
217 /* Check if we need to scroll the terminal */
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000218 if (++console_row >= CONSOLE_ROWS)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000219 console_scrollup();
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000220 else
Simon Glass9a8efc42012-10-30 13:40:18 +0000221 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000222}
223
224/*----------------------------------------------------------------------*/
225
Simon Glass709ea542014-07-23 06:54:59 -0600226static void lcd_stub_putc(struct stdio_dev *dev, const char c)
227{
228 lcd_putc(c);
229}
230
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000231void lcd_putc(const char c)
wdenk8655b6f2004-10-09 23:25:58 +0000232{
233 if (!lcd_is_enabled) {
234 serial_putc(c);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000235
wdenk8655b6f2004-10-09 23:25:58 +0000236 return;
237 }
238
239 switch (c) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000240 case '\r':
241 console_col = 0;
wdenk8655b6f2004-10-09 23:25:58 +0000242
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000243 return;
244 case '\n':
245 console_newline();
wdenk8655b6f2004-10-09 23:25:58 +0000246
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000247 return;
wdenk8655b6f2004-10-09 23:25:58 +0000248 case '\t': /* Tab (8 chars alignment) */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000249 console_col += 8;
250 console_col &= ~7;
wdenk8655b6f2004-10-09 23:25:58 +0000251
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000252 if (console_col >= CONSOLE_COLS)
253 console_newline();
wdenk8655b6f2004-10-09 23:25:58 +0000254
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000255 return;
256 case '\b':
257 console_back();
wdenk8655b6f2004-10-09 23:25:58 +0000258
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000259 return;
260 default:
261 lcd_putc_xy(console_col * VIDEO_FONT_WIDTH,
262 console_row * VIDEO_FONT_HEIGHT, c);
263 if (++console_col >= CONSOLE_COLS)
264 console_newline();
wdenk8655b6f2004-10-09 23:25:58 +0000265 }
wdenk8655b6f2004-10-09 23:25:58 +0000266}
267
268/*----------------------------------------------------------------------*/
269
Simon Glass709ea542014-07-23 06:54:59 -0600270static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
271{
272 lcd_puts(s);
273}
274
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000275void lcd_puts(const char *s)
wdenk8655b6f2004-10-09 23:25:58 +0000276{
277 if (!lcd_is_enabled) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000278 serial_puts(s);
279
wdenk8655b6f2004-10-09 23:25:58 +0000280 return;
281 }
282
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000283 while (*s)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000284 lcd_putc(*s++);
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000285
Simon Glass9a8efc42012-10-30 13:40:18 +0000286 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000287}
288
Haavard Skinnemoen15b17ab2008-09-01 16:21:20 +0200289/*----------------------------------------------------------------------*/
290
291void lcd_printf(const char *fmt, ...)
292{
293 va_list args;
294 char buf[CONFIG_SYS_PBSIZE];
295
296 va_start(args, fmt);
297 vsprintf(buf, fmt, args);
298 va_end(args);
299
300 lcd_puts(buf);
301}
302
wdenk8655b6f2004-10-09 23:25:58 +0000303/************************************************************************/
304/* ** Low-Level Graphics Routines */
305/************************************************************************/
306
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000307static void lcd_drawchars(ushort x, ushort y, uchar *str, int count)
wdenk8655b6f2004-10-09 23:25:58 +0000308{
309 uchar *dest;
Marek Vasutbe547c62011-09-26 02:26:04 +0200310 ushort row;
311
Anatolij Gustschin0f999c42012-04-27 04:41:16 +0000312#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
313 y += BMP_LOGO_HEIGHT;
314#endif
315
Marek Vasutbe547c62011-09-26 02:26:04 +0200316#if LCD_BPP == LCD_MONOCHROME
317 ushort off = x * (1 << LCD_BPP) % 8;
318#endif
wdenk8655b6f2004-10-09 23:25:58 +0000319
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100320 dest = (uchar *)(lcd_base + y * lcd_line_length + x * NBITS(LCD_BPP)/8);
wdenk8655b6f2004-10-09 23:25:58 +0000321
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000322 for (row = 0; row < VIDEO_FONT_HEIGHT; ++row, dest += lcd_line_length) {
wdenk8655b6f2004-10-09 23:25:58 +0000323 uchar *s = str;
wdenk8655b6f2004-10-09 23:25:58 +0000324 int i;
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100325#if LCD_BPP == LCD_COLOR16
326 ushort *d = (ushort *)dest;
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100327#elif LCD_BPP == LCD_COLOR32
328 u32 *d = (u32 *)dest;
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100329#else
330 uchar *d = dest;
331#endif
wdenk8655b6f2004-10-09 23:25:58 +0000332
333#if LCD_BPP == LCD_MONOCHROME
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000334 uchar rest = *d & -(1 << (8 - off));
wdenk8655b6f2004-10-09 23:25:58 +0000335 uchar sym;
336#endif
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000337 for (i = 0; i < count; ++i) {
wdenk8655b6f2004-10-09 23:25:58 +0000338 uchar c, bits;
339
340 c = *s++;
341 bits = video_fontdata[c * VIDEO_FONT_HEIGHT + row];
342
343#if LCD_BPP == LCD_MONOCHROME
344 sym = (COLOR_MASK(lcd_color_fg) & bits) |
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000345 (COLOR_MASK(lcd_color_bg) & ~bits);
wdenk8655b6f2004-10-09 23:25:58 +0000346
347 *d++ = rest | (sym >> off);
348 rest = sym << (8-off);
Nikita Kiryanov3707ad42014-12-08 17:14:36 +0200349#else /* LCD_BPP == LCD_COLOR8 or LCD_COLOR16 or LCD_COLOR32 */
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100350 for (c = 0; c < 8; ++c) {
351 *d++ = (bits & 0x80) ?
352 lcd_color_fg : lcd_color_bg;
353 bits <<= 1;
354 }
wdenk8655b6f2004-10-09 23:25:58 +0000355#endif
356 }
357#if LCD_BPP == LCD_MONOCHROME
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000358 *d = rest | (*d & ((1 << (8 - off)) - 1));
wdenk8655b6f2004-10-09 23:25:58 +0000359#endif
360 }
361}
362
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000363static inline void lcd_putc_xy(ushort x, ushort y, uchar c)
wdenk8655b6f2004-10-09 23:25:58 +0000364{
Anatolij Gustschin0f999c42012-04-27 04:41:16 +0000365 lcd_drawchars(x, y, &c, 1);
wdenk8655b6f2004-10-09 23:25:58 +0000366}
367
368/************************************************************************/
369/** Small utility to check that you got the colours right */
370/************************************************************************/
371#ifdef LCD_TEST_PATTERN
372
373#define N_BLK_VERT 2
374#define N_BLK_HOR 3
375
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000376static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
wdenk8655b6f2004-10-09 23:25:58 +0000377 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
378 CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
379};
380
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000381static void test_pattern(void)
wdenk8655b6f2004-10-09 23:25:58 +0000382{
383 ushort v_max = panel_info.vl_row;
384 ushort h_max = panel_info.vl_col;
385 ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
386 ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
387 ushort v, h;
388 uchar *pix = (uchar *)lcd_base;
389
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000390 printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
wdenk8655b6f2004-10-09 23:25:58 +0000391 h_max, v_max, h_step, v_step);
392
393 /* WARNING: Code silently assumes 8bit/pixel */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000394 for (v = 0; v < v_max; ++v) {
wdenk8655b6f2004-10-09 23:25:58 +0000395 uchar iy = v / v_step;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000396 for (h = 0; h < h_max; ++h) {
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000397 uchar ix = N_BLK_HOR * iy + h / h_step;
wdenk8655b6f2004-10-09 23:25:58 +0000398 *pix++ = test_colors[ix];
399 }
400 }
401}
402#endif /* LCD_TEST_PATTERN */
403
404
405/************************************************************************/
406/* ** GENERIC Initialization Routines */
407/************************************************************************/
Anatolij Gustschincefa4712013-11-09 11:00:09 +0100408/*
409 * With most lcd drivers the line length is set up
410 * by calculating it from panel_info parameters. Some
411 * drivers need to calculate the line length differently,
412 * so make the function weak to allow overriding it.
413 */
414__weak int lcd_get_size(int *line_length)
Simon Glass676d3192012-10-17 13:24:54 +0000415{
416 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
417 return *line_length * panel_info.vl_row;
418}
419
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000420int drv_lcd_init(void)
wdenk8655b6f2004-10-09 23:25:58 +0000421{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200422 struct stdio_dev lcddev;
wdenk8655b6f2004-10-09 23:25:58 +0000423 int rc;
424
Simon Glass7d95f2a2014-02-27 13:26:19 -0700425 lcd_base = map_sysmem(gd->fb_base, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000426
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000427 lcd_init(lcd_base); /* LCD initialization */
wdenk8655b6f2004-10-09 23:25:58 +0000428
429 /* Device initialization */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000430 memset(&lcddev, 0, sizeof(lcddev));
wdenk8655b6f2004-10-09 23:25:58 +0000431
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000432 strcpy(lcddev.name, "lcd");
wdenk8655b6f2004-10-09 23:25:58 +0000433 lcddev.ext = 0; /* No extensions */
434 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
Simon Glass709ea542014-07-23 06:54:59 -0600435 lcddev.putc = lcd_stub_putc; /* 'putc' function */
436 lcddev.puts = lcd_stub_puts; /* 'puts' function */
wdenk8655b6f2004-10-09 23:25:58 +0000437
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000438 rc = stdio_register(&lcddev);
wdenk8655b6f2004-10-09 23:25:58 +0000439
440 return (rc == 0) ? 1 : rc;
441}
442
443/*----------------------------------------------------------------------*/
Che-Liang Chiou02110902011-10-20 23:07:03 +0000444void lcd_clear(void)
wdenk8655b6f2004-10-09 23:25:58 +0000445{
446#if LCD_BPP == LCD_MONOCHROME
447 /* Setting the palette */
448 lcd_initcolregs();
449
450#elif LCD_BPP == LCD_COLOR8
451 /* Setting the palette */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000452 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
453 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
454 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
455 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
456 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
457 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
458 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
459 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
460 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
wdenk8655b6f2004-10-09 23:25:58 +0000461#endif
462
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200463#ifndef CONFIG_SYS_WHITE_ON_BLACK
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000464 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
465 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
wdenk8655b6f2004-10-09 23:25:58 +0000466#else
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000467 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
468 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200469#endif /* CONFIG_SYS_WHITE_ON_BLACK */
wdenk8655b6f2004-10-09 23:25:58 +0000470
471#ifdef LCD_TEST_PATTERN
472 test_pattern();
473#else
474 /* set framebuffer to background color */
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100475#if (LCD_BPP != LCD_COLOR32)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000476 memset((char *)lcd_base,
Hannes Petermaier8d9eaaf2014-03-07 18:45:20 +0100477 COLOR_MASK(lcd_color_bg),
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000478 lcd_line_length * panel_info.vl_row);
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100479#else
480 u32 *ppix = lcd_base;
481 u32 i;
482 for (i = 0;
483 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
484 i++) {
485 *ppix++ = COLOR_MASK(lcd_color_bg);
486 }
487#endif
wdenk8655b6f2004-10-09 23:25:58 +0000488#endif
489 /* Paint the logo and retrieve LCD base address */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000490 debug("[LCD] Drawing the logo...\n");
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000491 lcd_console_address = lcd_logo();
wdenk8655b6f2004-10-09 23:25:58 +0000492
493 console_col = 0;
494 console_row = 0;
Simon Glass9a8efc42012-10-30 13:40:18 +0000495 lcd_sync();
496}
497
498static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
499 char *const argv[])
500{
501 lcd_clear();
502 return 0;
wdenk8655b6f2004-10-09 23:25:58 +0000503}
504
505U_BOOT_CMD(
Che-Liang Chiou02110902011-10-20 23:07:03 +0000506 cls, 1, 1, do_lcd_clear,
Peter Tyser2fb26042009-01-27 18:03:12 -0600507 "clear screen",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200508 ""
wdenk8655b6f2004-10-09 23:25:58 +0000509);
510
511/*----------------------------------------------------------------------*/
512
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000513static int lcd_init(void *lcdbase)
wdenk8655b6f2004-10-09 23:25:58 +0000514{
515 /* Initialize the lcd controller */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000516 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
wdenk8655b6f2004-10-09 23:25:58 +0000517
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000518 lcd_ctrl_init(lcdbase);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100519
520 /*
Stephen Warren9316e142014-11-19 20:41:03 -0700521 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100522 * the 'lcdbase' argument and uses custom lcd base address
523 * by setting up gd->fb_base. Check for this condition and fixup
524 * 'lcd_base' address.
525 */
Simon Glass7d95f2a2014-02-27 13:26:19 -0700526 if (map_to_sysmem(lcdbase) != gd->fb_base)
527 lcd_base = map_sysmem(gd->fb_base, 0);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100528
529 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
530
Stephen Warren6d330712013-01-29 16:37:38 +0000531 lcd_get_size(&lcd_line_length);
Haavard Skinnemoen6f93d2b2008-09-01 16:21:21 +0200532 lcd_is_enabled = 1;
Che-Liang Chiou02110902011-10-20 23:07:03 +0000533 lcd_clear();
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000534 lcd_enable();
wdenk8655b6f2004-10-09 23:25:58 +0000535
536 /* Initialize the console */
537 console_col = 0;
wdenk88804d12005-07-04 00:03:16 +0000538#ifdef CONFIG_LCD_INFO_BELOW_LOGO
wdenk8655b6f2004-10-09 23:25:58 +0000539 console_row = 7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT;
540#else
541 console_row = 1; /* leave 1 blank line below logo */
542#endif
wdenk8655b6f2004-10-09 23:25:58 +0000543
544 return 0;
545}
546
547
548/************************************************************************/
549/* ** ROM capable initialization part - needed to reserve FB memory */
550/************************************************************************/
551/*
552 * This is called early in the system initialization to grab memory
553 * for the LCD controller.
554 * Returns new address for monitor, after reserving LCD buffer memory
555 *
556 * Note that this is running from ROM, so no write access to global data.
557 */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000558ulong lcd_setmem(ulong addr)
wdenk8655b6f2004-10-09 23:25:58 +0000559{
560 ulong size;
Simon Glass676d3192012-10-17 13:24:54 +0000561 int line_length;
wdenk8655b6f2004-10-09 23:25:58 +0000562
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000563 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
564 panel_info.vl_row, NBITS(panel_info.vl_bpix));
wdenk8655b6f2004-10-09 23:25:58 +0000565
Simon Glass676d3192012-10-17 13:24:54 +0000566 size = lcd_get_size(&line_length);
wdenk8655b6f2004-10-09 23:25:58 +0000567
Simon Glass676d3192012-10-17 13:24:54 +0000568 /* Round up to nearest full page, or MMU section if defined */
569 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
570 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
wdenk8655b6f2004-10-09 23:25:58 +0000571
572 /* Allocate pages for the frame buffer. */
573 addr -= size;
574
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000575 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
576 size >> 10, addr);
wdenk8655b6f2004-10-09 23:25:58 +0000577
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000578 return addr;
wdenk8655b6f2004-10-09 23:25:58 +0000579}
580
581/*----------------------------------------------------------------------*/
582
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000583static void lcd_setfgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000584{
Stelian Pop39cf4802008-05-09 21:57:18 +0200585 lcd_color_fg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000586}
587
588/*----------------------------------------------------------------------*/
589
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000590static void lcd_setbgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000591{
Stelian Pop39cf4802008-05-09 21:57:18 +0200592 lcd_color_bg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000593}
594
wdenk8655b6f2004-10-09 23:25:58 +0000595/************************************************************************/
596/* ** Chipset depending Bitmap / Logo stuff... */
597/************************************************************************/
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000598static inline ushort *configuration_get_cmap(void)
599{
600#if defined CONFIG_CPU_PXA
601 struct pxafb_info *fbi = &panel_info.pxa;
602 return (ushort *)fbi->palette;
603#elif defined(CONFIG_MPC823)
604 immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
605 cpm8xx_t *cp = &(immr->im_cpm);
606 return (ushort *)&(cp->lcd_cmap[255 * sizeof(ushort)]);
607#elif defined(CONFIG_ATMEL_LCD)
608 return (ushort *)(panel_info.mmio + ATMEL_LCDC_LUT(0));
Anatolij Gustschind23019f2012-09-22 06:55:53 +0000609#elif !defined(CONFIG_ATMEL_HLCD) && !defined(CONFIG_EXYNOS_FB)
610 return panel_info.cmap;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000611#elif defined(CONFIG_LCD_LOGO)
Anatolij Gustschind23019f2012-09-22 06:55:53 +0000612 return bmp_logo_palette;
613#else
614 return NULL;
615#endif
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000616}
617
wdenk8655b6f2004-10-09 23:25:58 +0000618#ifdef CONFIG_LCD_LOGO
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000619void bitmap_plot(int x, int y)
wdenk8655b6f2004-10-09 23:25:58 +0000620{
Stelian Pop39cf4802008-05-09 21:57:18 +0200621#ifdef CONFIG_ATMEL_LCD
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000622 uint *cmap = (uint *)bmp_logo_palette;
Stelian Pop39cf4802008-05-09 21:57:18 +0200623#else
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000624 ushort *cmap = (ushort *)bmp_logo_palette;
Stelian Pop39cf4802008-05-09 21:57:18 +0200625#endif
wdenk8655b6f2004-10-09 23:25:58 +0000626 ushort i, j;
627 uchar *bmap;
628 uchar *fb;
629 ushort *fb16;
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000630#if defined(CONFIG_MPC823)
631 immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
632 cpm8xx_t *cp = &(immr->im_cpm);
wdenk8655b6f2004-10-09 23:25:58 +0000633#endif
Andre Renaud317461c2013-02-13 17:48:00 +0000634 unsigned bpix = NBITS(panel_info.vl_bpix);
wdenk8655b6f2004-10-09 23:25:58 +0000635
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000636 debug("Logo: width %d height %d colors %d cmap %d\n",
wdenk8655b6f2004-10-09 23:25:58 +0000637 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS,
Anatolij Gustschin095407d2012-04-27 04:41:06 +0000638 ARRAY_SIZE(bmp_logo_palette));
wdenk8655b6f2004-10-09 23:25:58 +0000639
640 bmap = &bmp_logo_bitmap[0];
Andre Renaud317461c2013-02-13 17:48:00 +0000641 fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
wdenk8655b6f2004-10-09 23:25:58 +0000642
Andre Renaud317461c2013-02-13 17:48:00 +0000643 if (bpix < 12) {
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000644 /* Leave room for default color map
645 * default case: generic system with no cmap (most likely 16bpp)
646 * cmap was set to the source palette, so no change is done.
647 * This avoids even more ifdefs in the next stanza
648 */
649#if defined(CONFIG_MPC823)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000650 cmap = (ushort *) &(cp->lcd_cmap[BMP_LOGO_OFFSET * sizeof(ushort)]);
Stelian Pop39cf4802008-05-09 21:57:18 +0200651#elif defined(CONFIG_ATMEL_LCD)
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000652 cmap = (uint *)configuration_get_cmap();
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100653#else
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000654 cmap = configuration_get_cmap();
wdenk8655b6f2004-10-09 23:25:58 +0000655#endif
656
657 WATCHDOG_RESET();
658
659 /* Set color map */
Anatolij Gustschin095407d2012-04-27 04:41:06 +0000660 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i) {
wdenk8655b6f2004-10-09 23:25:58 +0000661 ushort colreg = bmp_logo_palette[i];
Stelian Pop39cf4802008-05-09 21:57:18 +0200662#ifdef CONFIG_ATMEL_LCD
663 uint lut_entry;
664#ifdef CONFIG_ATMEL_LCD_BGR555
665 lut_entry = ((colreg & 0x000F) << 11) |
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000666 ((colreg & 0x00F0) << 2) |
667 ((colreg & 0x0F00) >> 7);
Stelian Pop39cf4802008-05-09 21:57:18 +0200668#else /* CONFIG_ATMEL_LCD_RGB565 */
669 lut_entry = ((colreg & 0x000F) << 1) |
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000670 ((colreg & 0x00F0) << 3) |
671 ((colreg & 0x0F00) << 4);
Stelian Pop39cf4802008-05-09 21:57:18 +0200672#endif
673 *(cmap + BMP_LOGO_OFFSET) = lut_entry;
674 cmap++;
675#else /* !CONFIG_ATMEL_LCD */
wdenk8655b6f2004-10-09 23:25:58 +0000676 *cmap++ = colreg;
Stelian Pop39cf4802008-05-09 21:57:18 +0200677#endif /* CONFIG_ATMEL_LCD */
wdenk8655b6f2004-10-09 23:25:58 +0000678 }
679
680 WATCHDOG_RESET();
681
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000682 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
683 memcpy(fb, bmap, BMP_LOGO_WIDTH);
wdenk8655b6f2004-10-09 23:25:58 +0000684 bmap += BMP_LOGO_WIDTH;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000685 fb += panel_info.vl_col;
wdenk8655b6f2004-10-09 23:25:58 +0000686 }
687 }
688 else { /* true color mode */
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100689 u16 col16;
Andre Renaud317461c2013-02-13 17:48:00 +0000690 fb16 = (ushort *)fb;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000691 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
692 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100693 col16 = bmp_logo_palette[(bmap[j]-16)];
694 fb16[j] =
695 ((col16 & 0x000F) << 1) |
696 ((col16 & 0x00F0) << 3) |
697 ((col16 & 0x0F00) << 4);
wdenk8655b6f2004-10-09 23:25:58 +0000698 }
699 bmap += BMP_LOGO_WIDTH;
700 fb16 += panel_info.vl_col;
701 }
702 }
703
704 WATCHDOG_RESET();
Simon Glass9a8efc42012-10-30 13:40:18 +0000705 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000706}
Anatolij Gustschin2b5cb3d2012-04-27 04:41:27 +0000707#else
708static inline void bitmap_plot(int x, int y) {}
wdenk8655b6f2004-10-09 23:25:58 +0000709#endif /* CONFIG_LCD_LOGO */
710
711/*----------------------------------------------------------------------*/
Jon Loeligerc3517f92007-07-08 18:10:08 -0500712#if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
wdenk8655b6f2004-10-09 23:25:58 +0000713/*
714 * Display the BMP file located at address bmp_image.
715 * Only uncompressed.
716 */
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200717
718#ifdef CONFIG_SPLASH_SCREEN_ALIGN
719#define BMP_ALIGN_CENTER 0x7FFF
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000720
721static void splash_align_axis(int *axis, unsigned long panel_size,
722 unsigned long picture_size)
723{
724 unsigned long panel_picture_delta = panel_size - picture_size;
725 unsigned long axis_alignment;
726
727 if (*axis == BMP_ALIGN_CENTER)
728 axis_alignment = panel_picture_delta / 2;
729 else if (*axis < 0)
730 axis_alignment = panel_picture_delta + *axis + 1;
731 else
732 return;
733
Masahiro Yamadab4141192014-11-07 03:03:31 +0900734 *axis = max(0, (int)axis_alignment);
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000735}
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200736#endif
737
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000738
739#ifdef CONFIG_LCD_BMP_RLE8
740
741#define BMP_RLE8_ESCAPE 0
742#define BMP_RLE8_EOL 0
743#define BMP_RLE8_EOBMP 1
744#define BMP_RLE8_DELTA 2
745
746static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
747 int cnt)
748{
749 while (cnt > 0) {
750 *(*fbp)++ = cmap[*bmap++];
751 cnt--;
752 }
753}
754
755static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
756{
757 ushort *fb = *fbp;
758 int cnt_8copy = cnt >> 3;
759
760 cnt -= cnt_8copy << 3;
761 while (cnt_8copy > 0) {
762 *fb++ = c;
763 *fb++ = c;
764 *fb++ = c;
765 *fb++ = c;
766 *fb++ = c;
767 *fb++ = c;
768 *fb++ = c;
769 *fb++ = c;
770 cnt_8copy--;
771 }
772 while (cnt > 0) {
773 *fb++ = c;
774 cnt--;
775 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000776 *fbp = fb;
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000777}
778
779/*
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000780 * Do not call this function directly, must be called from lcd_display_bitmap.
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000781 */
782static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
783 int x_off, int y_off)
784{
785 uchar *bmap;
786 ulong width, height;
787 ulong cnt, runlen;
788 int x, y;
789 int decode = 1;
790
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100791 width = get_unaligned_le32(&bmp->header.width);
792 height = get_unaligned_le32(&bmp->header.height);
793 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000794
795 x = 0;
796 y = height - 1;
797
798 while (decode) {
799 if (bmap[0] == BMP_RLE8_ESCAPE) {
800 switch (bmap[1]) {
801 case BMP_RLE8_EOL:
802 /* end of line */
803 bmap += 2;
804 x = 0;
805 y--;
806 /* 16bpix, 2-byte per pixel, width should *2 */
807 fb -= (width * 2 + lcd_line_length);
808 break;
809 case BMP_RLE8_EOBMP:
810 /* end of bitmap */
811 decode = 0;
812 break;
813 case BMP_RLE8_DELTA:
814 /* delta run */
815 x += bmap[2];
816 y -= bmap[3];
817 /* 16bpix, 2-byte per pixel, x should *2 */
818 fb = (uchar *) (lcd_base + (y + y_off - 1)
819 * lcd_line_length + (x + x_off) * 2);
820 bmap += 4;
821 break;
822 default:
823 /* unencoded run */
824 runlen = bmap[1];
825 bmap += 2;
826 if (y < height) {
827 if (x < width) {
828 if (x + runlen > width)
829 cnt = width - x;
830 else
831 cnt = runlen;
832 draw_unencoded_bitmap(
833 (ushort **)&fb,
834 bmap, cmap, cnt);
835 }
836 x += runlen;
837 }
838 bmap += runlen;
839 if (runlen & 1)
840 bmap++;
841 }
842 } else {
843 /* encoded run */
844 if (y < height) {
845 runlen = bmap[0];
846 if (x < width) {
847 /* aggregate the same code */
848 while (bmap[0] == 0xff &&
849 bmap[2] != BMP_RLE8_ESCAPE &&
850 bmap[1] == bmap[3]) {
851 runlen += bmap[2];
852 bmap += 2;
853 }
854 if (x + runlen > width)
855 cnt = width - x;
856 else
857 cnt = runlen;
858 draw_encoded_bitmap((ushort **)&fb,
859 cmap[bmap[1]], cnt);
860 }
861 x += runlen;
862 }
863 bmap += 2;
864 }
865 }
866}
867#endif
868
Nikita Kiryanovecfdcee2014-11-11 15:46:05 +0200869#if defined(CONFIG_MPC823)
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000870#define FB_PUT_BYTE(fb, from) *(fb)++ = (255 - *(from)++)
Anatolij Gustschind23019f2012-09-22 06:55:53 +0000871#else
872#define FB_PUT_BYTE(fb, from) *(fb)++ = *(from)++
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000873#endif
874
875#if defined(CONFIG_BMP_16BPP)
876#if defined(CONFIG_ATMEL_LCD_BGR555)
877static inline void fb_put_word(uchar **fb, uchar **from)
878{
879 *(*fb)++ = (((*from)[0] & 0x1f) << 2) | ((*from)[1] & 0x03);
880 *(*fb)++ = ((*from)[0] & 0xe0) | (((*from)[1] & 0x7c) >> 2);
881 *from += 2;
882}
883#else
884static inline void fb_put_word(uchar **fb, uchar **from)
885{
886 *(*fb)++ = *(*from)++;
887 *(*fb)++ = *(*from)++;
888}
889#endif
890#endif /* CONFIG_BMP_16BPP */
891
wdenk8655b6f2004-10-09 23:25:58 +0000892int lcd_display_bitmap(ulong bmp_image, int x, int y)
893{
Anatolij Gustschin00cc5592009-02-25 20:28:13 +0100894 ushort *cmap = NULL;
Anatolij Gustschin00cc5592009-02-25 20:28:13 +0100895 ushort *cmap_base = NULL;
wdenk8655b6f2004-10-09 23:25:58 +0000896 ushort i, j;
897 uchar *fb;
Simon Glass7d95f2a2014-02-27 13:26:19 -0700898 bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000899 uchar *bmap;
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000900 ushort padded_width;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100901 unsigned long width, height, byte_width;
Wolfgang Denke8143e72006-08-30 23:09:00 +0200902 unsigned long pwidth = panel_info.vl_col;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100903 unsigned colors, bpix, bmp_bpix;
wdenk8655b6f2004-10-09 23:25:58 +0000904
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000905 if (!bmp || !(bmp->header.signature[0] == 'B' &&
906 bmp->header.signature[1] == 'M')) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000907 printf("Error: no valid bmp image at %lx\n", bmp_image);
908
wdenk8655b6f2004-10-09 23:25:58 +0000909 return 1;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100910 }
wdenk8655b6f2004-10-09 23:25:58 +0000911
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100912 width = get_unaligned_le32(&bmp->header.width);
913 height = get_unaligned_le32(&bmp->header.height);
914 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
915
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100916 colors = 1 << bmp_bpix;
wdenk8655b6f2004-10-09 23:25:58 +0000917
918 bpix = NBITS(panel_info.vl_bpix);
919
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000920 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100921 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
922 bpix, bmp_bpix);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000923
wdenk8655b6f2004-10-09 23:25:58 +0000924 return 1;
925 }
926
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200927 /*
928 * We support displaying 8bpp BMPs on 16bpp LCDs
929 * and displaying 24bpp BMPs on 32bpp LCDs
930 * */
931 if (bpix != bmp_bpix &&
932 !(bmp_bpix == 8 && bpix == 16) &&
933 !(bmp_bpix == 24 && bpix == 32)) {
wdenk8655b6f2004-10-09 23:25:58 +0000934 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100935 bpix, get_unaligned_le16(&bmp->header.bit_count));
wdenk8655b6f2004-10-09 23:25:58 +0000936 return 1;
937 }
938
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000939 debug("Display-bmp: %d x %d with %d colors\n",
wdenk8655b6f2004-10-09 23:25:58 +0000940 (int)width, (int)height, (int)colors);
941
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100942 if (bmp_bpix == 8) {
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000943 cmap = configuration_get_cmap();
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100944 cmap_base = cmap;
945
wdenk8655b6f2004-10-09 23:25:58 +0000946 /* Set color map */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000947 for (i = 0; i < colors; ++i) {
wdenk8655b6f2004-10-09 23:25:58 +0000948 bmp_color_table_entry_t cte = bmp->color_table[i];
Mark Jackson1464eff2008-08-01 09:48:29 +0100949#if !defined(CONFIG_ATMEL_LCD)
wdenk8655b6f2004-10-09 23:25:58 +0000950 ushort colreg =
951 ( ((cte.red) << 8) & 0xf800) |
Wolfgang Denk59d80bf2005-09-21 15:24:52 +0200952 ( ((cte.green) << 3) & 0x07e0) |
953 ( ((cte.blue) >> 3) & 0x001f) ;
wdenk25d67122004-12-10 11:40:40 +0000954 *cmap = colreg;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100955#if defined(CONFIG_MPC823)
wdenk25d67122004-12-10 11:40:40 +0000956 cmap--;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100957#else
958 cmap++;
wdenk8655b6f2004-10-09 23:25:58 +0000959#endif
Mark Jackson1464eff2008-08-01 09:48:29 +0100960#else /* CONFIG_ATMEL_LCD */
961 lcd_setcolreg(i, cte.red, cte.green, cte.blue);
962#endif
wdenk8655b6f2004-10-09 23:25:58 +0000963 }
964 }
Wolfgang Denke8143e72006-08-30 23:09:00 +0200965
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000966 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200967
968#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000969 splash_align_axis(&x, pwidth, width);
970 splash_align_axis(&y, panel_info.vl_row, height);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200971#endif /* CONFIG_SPLASH_SCREEN_ALIGN */
972
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000973 if ((x + width) > pwidth)
Wolfgang Denke8143e72006-08-30 23:09:00 +0200974 width = pwidth - x;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000975 if ((y + height) > panel_info.vl_row)
wdenk8655b6f2004-10-09 23:25:58 +0000976 height = panel_info.vl_row - y;
977
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100978 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
979 fb = (uchar *)(lcd_base +
Liu Ying8d46d5b2011-01-11 15:29:58 +0800980 (y + height - 1) * lcd_line_length + x * bpix / 8);
Mark Jacksona303dfb2009-02-06 10:37:49 +0100981
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100982 switch (bmp_bpix) {
Mark Jacksona303dfb2009-02-06 10:37:49 +0100983 case 1: /* pass through */
Simon Glass01564442014-10-15 04:53:04 -0600984 case 8: {
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000985#ifdef CONFIG_LCD_BMP_RLE8
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100986 u32 compression = get_unaligned_le32(&bmp->header.compression);
987 if (compression == BMP_BI_RLE8) {
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000988 if (bpix != 16) {
989 /* TODO implement render code for bpix != 16 */
990 printf("Error: only support 16 bpix");
991 return 1;
992 }
993 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
994 break;
995 }
996#endif
997
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100998 if (bpix != 16)
999 byte_width = width;
1000 else
1001 byte_width = width * 2;
1002
Mark Jacksona303dfb2009-02-06 10:37:49 +01001003 for (i = 0; i < height; ++i) {
1004 WATCHDOG_RESET();
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +01001005 for (j = 0; j < width; j++) {
1006 if (bpix != 16) {
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +00001007 FB_PUT_BYTE(fb, bmap);
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +01001008 } else {
1009 *(uint16_t *)fb = cmap_base[*(bmap++)];
1010 fb += sizeof(uint16_t) / sizeof(*fb);
1011 }
1012 }
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +00001013 bmap += (padded_width - width);
Jeroen Hofstee6b035142013-01-12 12:07:56 +00001014 fb -= byte_width + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +01001015 }
1016 break;
Simon Glass01564442014-10-15 04:53:04 -06001017 }
Mark Jacksona303dfb2009-02-06 10:37:49 +01001018#if defined(CONFIG_BMP_16BPP)
1019 case 16:
1020 for (i = 0; i < height; ++i) {
1021 WATCHDOG_RESET();
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +00001022 for (j = 0; j < width; j++)
1023 fb_put_word(&fb, &bmap);
1024
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +00001025 bmap += (padded_width - width) * 2;
Jeroen Hofstee6b035142013-01-12 12:07:56 +00001026 fb -= width * 2 + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +01001027 }
1028 break;
1029#endif /* CONFIG_BMP_16BPP */
Hannes Petermaiera305fb12014-07-15 16:28:46 +02001030#if defined(CONFIG_BMP_24BMP)
1031 case 24:
1032 for (i = 0; i < height; ++i) {
1033 for (j = 0; j < width; j++) {
1034 *(fb++) = *(bmap++);
1035 *(fb++) = *(bmap++);
1036 *(fb++) = *(bmap++);
1037 *(fb++) = 0;
1038 }
1039 fb -= lcd_line_length + width * (bpix / 8);
1040 }
1041 break;
1042#endif /* CONFIG_BMP_24BMP */
Donghwa Leefb6a9aa2012-05-09 19:23:37 +00001043#if defined(CONFIG_BMP_32BPP)
1044 case 32:
1045 for (i = 0; i < height; ++i) {
1046 for (j = 0; j < width; j++) {
1047 *(fb++) = *(bmap++);
1048 *(fb++) = *(bmap++);
1049 *(fb++) = *(bmap++);
1050 *(fb++) = *(bmap++);
1051 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +00001052 fb -= lcd_line_length + width * (bpix / 8);
Donghwa Leefb6a9aa2012-05-09 19:23:37 +00001053 }
1054 break;
1055#endif /* CONFIG_BMP_32BPP */
Mark Jacksona303dfb2009-02-06 10:37:49 +01001056 default:
1057 break;
1058 };
wdenk8655b6f2004-10-09 23:25:58 +00001059
Simon Glass9a8efc42012-10-30 13:40:18 +00001060 lcd_sync();
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001061 return 0;
wdenk8655b6f2004-10-09 23:25:58 +00001062}
Jon Loeligerc3517f92007-07-08 18:10:08 -05001063#endif
wdenk8655b6f2004-10-09 23:25:58 +00001064
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001065static void *lcd_logo(void)
wdenk8655b6f2004-10-09 23:25:58 +00001066{
wdenk8655b6f2004-10-09 23:25:58 +00001067#ifdef CONFIG_SPLASH_SCREEN
1068 char *s;
1069 ulong addr;
1070 static int do_splash = 1;
1071
1072 if (do_splash && (s = getenv("splashimage")) != NULL) {
Matthias Weisser1ca298c2009-07-09 16:07:30 +02001073 int x = 0, y = 0;
wdenk8655b6f2004-10-09 23:25:58 +00001074 do_splash = 0;
1075
Nikita Kiryanov581bb412013-01-30 21:39:57 +00001076 if (splash_screen_prepare())
Robert Winklerdd4425e2013-06-17 11:31:29 -07001077 return (void *)lcd_base;
Nikita Kiryanov581bb412013-01-30 21:39:57 +00001078
Matthias Weisser1ca298c2009-07-09 16:07:30 +02001079 addr = simple_strtoul (s, NULL, 16);
Matthias Weisser1ca298c2009-07-09 16:07:30 +02001080
Anatolij Gustschinff8fb562013-07-02 00:04:05 +02001081 splash_get_pos(&x, &y);
Matthias Weisser1ca298c2009-07-09 16:07:30 +02001082
Nikita Kiryanovd3a555e2012-08-09 00:14:50 +00001083 if (bmp_display(addr, x, y) == 0)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001084 return (void *)lcd_base;
wdenk8655b6f2004-10-09 23:25:58 +00001085 }
1086#endif /* CONFIG_SPLASH_SCREEN */
1087
Anatolij Gustschin2b5cb3d2012-04-27 04:41:27 +00001088 bitmap_plot(0, 0);
wdenk8655b6f2004-10-09 23:25:58 +00001089
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +02001090#ifdef CONFIG_LCD_INFO
1091 console_col = LCD_INFO_X / VIDEO_FONT_WIDTH;
1092 console_row = LCD_INFO_Y / VIDEO_FONT_HEIGHT;
1093 lcd_show_board_info();
1094#endif /* CONFIG_LCD_INFO */
Stelian Pop39cf4802008-05-09 21:57:18 +02001095
wdenk88804d12005-07-04 00:03:16 +00001096#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001097 return (void *)((ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length);
wdenk8655b6f2004-10-09 23:25:58 +00001098#else
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001099 return (void *)lcd_base;
Jeroen Hofstee6b035142013-01-12 12:07:56 +00001100#endif /* CONFIG_LCD_LOGO && !defined(CONFIG_LCD_INFO_BELOW_LOGO) */
wdenk8655b6f2004-10-09 23:25:58 +00001101}
1102
Nikita Kiryanovc0880482013-02-24 21:28:43 +00001103#ifdef CONFIG_SPLASHIMAGE_GUARD
1104static int on_splashimage(const char *name, const char *value, enum env_op op,
1105 int flags)
1106{
1107 ulong addr;
1108 int aligned;
1109
1110 if (op == env_op_delete)
1111 return 0;
1112
1113 addr = simple_strtoul(value, NULL, 16);
1114 /* See README.displaying-bmps */
1115 aligned = (addr % 4 == 2);
1116 if (!aligned) {
1117 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
1118 return -1;
1119 }
1120
1121 return 0;
1122}
1123
1124U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
1125#endif
1126
Vadim Bendebury395166c2012-09-28 15:11:13 +00001127void lcd_position_cursor(unsigned col, unsigned row)
1128{
Masahiro Yamadab4141192014-11-07 03:03:31 +09001129 console_col = min_t(short, col, CONSOLE_COLS - 1);
1130 console_row = min_t(short, row, CONSOLE_ROWS - 1);
Vadim Bendebury395166c2012-09-28 15:11:13 +00001131}
1132
1133int lcd_get_pixel_width(void)
1134{
1135 return panel_info.vl_col;
1136}
1137
1138int lcd_get_pixel_height(void)
1139{
1140 return panel_info.vl_row;
1141}
1142
1143int lcd_get_screen_rows(void)
1144{
1145 return CONSOLE_ROWS;
1146}
1147
1148int lcd_get_screen_columns(void)
1149{
1150 return CONSOLE_COLS;
1151}
Stephen Warren6a195d22013-05-27 18:31:17 +00001152
1153#if defined(CONFIG_LCD_DT_SIMPLEFB)
1154static int lcd_dt_simplefb_configure_node(void *blob, int off)
1155{
Stephen Warren6a195d22013-05-27 18:31:17 +00001156#if LCD_BPP == LCD_COLOR16
Hans de Goede11b8dfa2014-11-19 13:53:27 +01001157 return fdt_setup_simplefb_node(blob, off, gd->fb_base,
1158 panel_info.vl_col, panel_info.vl_row,
1159 panel_info.vl_col * 2, "r5g6b5");
Stephen Warren6a195d22013-05-27 18:31:17 +00001160#else
Hans de Goede11b8dfa2014-11-19 13:53:27 +01001161 return -1;
Stephen Warren6a195d22013-05-27 18:31:17 +00001162#endif
Stephen Warren6a195d22013-05-27 18:31:17 +00001163}
1164
1165int lcd_dt_simplefb_add_node(void *blob)
1166{
Stephen Warren5af7d0f2013-06-13 17:13:11 -06001167 static const char compat[] = "simple-framebuffer";
1168 static const char disabled[] = "disabled";
Stephen Warren6a195d22013-05-27 18:31:17 +00001169 int off, ret;
1170
1171 off = fdt_add_subnode(blob, 0, "framebuffer");
1172 if (off < 0)
1173 return -1;
1174
1175 ret = fdt_setprop(blob, off, "status", disabled, sizeof(disabled));
1176 if (ret < 0)
1177 return -1;
1178
1179 ret = fdt_setprop(blob, off, "compatible", compat, sizeof(compat));
1180 if (ret < 0)
1181 return -1;
1182
1183 return lcd_dt_simplefb_configure_node(blob, off);
1184}
1185
1186int lcd_dt_simplefb_enable_existing_node(void *blob)
1187{
1188 int off;
1189
1190 off = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
1191 if (off < 0)
1192 return -1;
1193
1194 return lcd_dt_simplefb_configure_node(blob, off);
1195}
1196#endif