blob: 37147af805ce8972081dd7a59420ecb14a2b0453 [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>
Robert Winklerdd4425e2013-06-17 11:31:29 -070033
Marek Vasutabc20ab2011-11-26 07:20:07 +010034#if defined(CONFIG_CPU_PXA25X) || defined(CONFIG_CPU_PXA27X) || \
35 defined(CONFIG_CPU_MONAHANS)
wdenk8655b6f2004-10-09 23:25:58 +000036#include <asm/byteorder.h>
37#endif
38
39#if defined(CONFIG_MPC823)
wdenk8655b6f2004-10-09 23:25:58 +000040#include <lcdvideo.h>
41#endif
42
Stelian Pop39cf4802008-05-09 21:57:18 +020043#if defined(CONFIG_ATMEL_LCD)
44#include <atmel_lcdc.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020045#endif
46
Stephen Warren6a195d22013-05-27 18:31:17 +000047#if defined(CONFIG_LCD_DT_SIMPLEFB)
48#include <libfdt.h>
49#endif
50
wdenk8655b6f2004-10-09 23:25:58 +000051/************************************************************************/
52/* ** FONT DATA */
53/************************************************************************/
54#include <video_font.h> /* Get font data, width and height */
55
wdenk88804d12005-07-04 00:03:16 +000056/************************************************************************/
57/* ** LOGO DATA */
58/************************************************************************/
59#ifdef CONFIG_LCD_LOGO
60# include <bmp_logo.h> /* Get logo data, width and height */
Che-Liang Chiouc2707302011-10-20 23:04:20 +000061# include <bmp_logo_data.h>
Alessandro Rubiniacb13862010-03-13 17:44:08 +010062# if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
wdenk88804d12005-07-04 00:03:16 +000063# error Default Color Map overlaps with Logo Color Map
64# endif
65#endif
wdenk8655b6f2004-10-09 23:25:58 +000066
Simon Glass7d95f2a2014-02-27 13:26:19 -070067#ifdef CONFIG_SANDBOX
68#include <asm/sdl.h>
69#endif
70
Simon Glass676d3192012-10-17 13:24:54 +000071#ifndef CONFIG_LCD_ALIGNMENT
72#define CONFIG_LCD_ALIGNMENT PAGE_SIZE
73#endif
74
Simon Glass0d89efe2012-10-17 13:24:59 +000075/* By default we scroll by a single line */
76#ifndef CONFIG_CONSOLE_SCROLL_LINES
77#define CONFIG_CONSOLE_SCROLL_LINES 1
78#endif
79
Jeroen Hofsteea5796c52013-01-12 12:07:59 +000080/************************************************************************/
81/* ** CONSOLE DEFINITIONS & FUNCTIONS */
82/************************************************************************/
83#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
84# define CONSOLE_ROWS ((panel_info.vl_row-BMP_LOGO_HEIGHT) \
85 / VIDEO_FONT_HEIGHT)
86#else
87# define CONSOLE_ROWS (panel_info.vl_row / VIDEO_FONT_HEIGHT)
88#endif
Wolfgang Denkd87080b2006-03-31 18:32:53 +020089
Jeroen Hofsteea5796c52013-01-12 12:07:59 +000090#define CONSOLE_COLS (panel_info.vl_col / VIDEO_FONT_WIDTH)
91#define CONSOLE_ROW_SIZE (VIDEO_FONT_HEIGHT * lcd_line_length)
92#define CONSOLE_ROW_FIRST lcd_console_address
93#define CONSOLE_ROW_SECOND (lcd_console_address + CONSOLE_ROW_SIZE)
94#define CONSOLE_ROW_LAST (lcd_console_address + CONSOLE_SIZE \
95 - CONSOLE_ROW_SIZE)
96#define CONSOLE_SIZE (CONSOLE_ROW_SIZE * CONSOLE_ROWS)
97#define CONSOLE_SCROLL_SIZE (CONSOLE_SIZE - CONSOLE_ROW_SIZE)
98
99#if LCD_BPP == LCD_MONOCHROME
100# define COLOR_MASK(c) ((c) | (c) << 1 | (c) << 2 | (c) << 3 | \
101 (c) << 4 | (c) << 5 | (c) << 6 | (c) << 7)
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100102#elif (LCD_BPP == LCD_COLOR8) || (LCD_BPP == LCD_COLOR16) || \
103 (LCD_BPP == LCD_COLOR32)
Jeroen Hofsteea5796c52013-01-12 12:07:59 +0000104# define COLOR_MASK(c) (c)
105#else
106# error Unsupported LCD BPP.
107#endif
108
wdenk8655b6f2004-10-09 23:25:58 +0000109DECLARE_GLOBAL_DATA_PTR;
wdenk8655b6f2004-10-09 23:25:58 +0000110
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000111static void lcd_drawchars(ushort x, ushort y, uchar *str, int count);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000112static inline void lcd_putc_xy(ushort x, ushort y, uchar c);
wdenk8655b6f2004-10-09 23:25:58 +0000113
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000114static int lcd_init(void *lcdbase);
wdenk8655b6f2004-10-09 23:25:58 +0000115
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000116static void *lcd_logo(void);
wdenk8655b6f2004-10-09 23:25:58 +0000117
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000118static void lcd_setfgcolor(int color);
119static void lcd_setbgcolor(int color);
wdenk8655b6f2004-10-09 23:25:58 +0000120
Wolfgang Denk46d1d5d2013-01-05 09:45:48 +0000121static int lcd_color_fg;
122static int lcd_color_bg;
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +0000123int lcd_line_length;
Wolfgang Denk46d1d5d2013-01-05 09:45:48 +0000124
wdenk8655b6f2004-10-09 23:25:58 +0000125char lcd_is_enabled = 0;
wdenk8655b6f2004-10-09 23:25:58 +0000126
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +0000127static short console_col;
128static short console_row;
129
130static void *lcd_console_address;
Jeroen Hofstee00a0ca52013-01-22 10:44:12 +0000131static void *lcd_base; /* Start of framebuffer memory */
Jeroen Hofsteef1d205a2013-01-22 10:44:11 +0000132
Simon Glass9a8efc42012-10-30 13:40:18 +0000133static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
134
wdenk8655b6f2004-10-09 23:25:58 +0000135/************************************************************************/
136
Simon Glass9a8efc42012-10-30 13:40:18 +0000137/* Flush LCD activity to the caches */
138void lcd_sync(void)
139{
140 /*
141 * flush_dcache_range() is declared in common.h but it seems that some
142 * architectures do not actually implement it. Is there a way to find
143 * out whether it exists? For now, ARM is safe.
144 */
145#if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
146 int line_length;
147
148 if (lcd_flush_dcache)
149 flush_dcache_range((u32)lcd_base,
150 (u32)(lcd_base + lcd_get_size(&line_length)));
Simon Glass7d95f2a2014-02-27 13:26:19 -0700151#elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
152 static ulong last_sync;
153
154 if (get_timer(last_sync) > 10) {
155 sandbox_sdl_sync(lcd_base);
156 last_sync = get_timer(0);
157 }
Simon Glass9a8efc42012-10-30 13:40:18 +0000158#endif
159}
160
161void lcd_set_flush_dcache(int flush)
162{
163 lcd_flush_dcache = (flush != 0);
164}
165
wdenk8655b6f2004-10-09 23:25:58 +0000166/*----------------------------------------------------------------------*/
167
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000168static void console_scrollup(void)
wdenk8655b6f2004-10-09 23:25:58 +0000169{
Simon Glass0d89efe2012-10-17 13:24:59 +0000170 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
wdenk8655b6f2004-10-09 23:25:58 +0000171
Simon Glass0d89efe2012-10-17 13:24:59 +0000172 /* Copy up rows ignoring those that will be overwritten */
173 memcpy(CONSOLE_ROW_FIRST,
174 lcd_console_address + CONSOLE_ROW_SIZE * rows,
175 CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows);
176
177 /* Clear the last rows */
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100178#if (LCD_BPP != LCD_COLOR32)
Simon Glass0d89efe2012-10-17 13:24:59 +0000179 memset(lcd_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows,
180 COLOR_MASK(lcd_color_bg),
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000181 CONSOLE_ROW_SIZE * rows);
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100182#else
183 u32 *ppix = lcd_console_address +
184 CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows;
185 u32 i;
186 for (i = 0;
187 i < (CONSOLE_ROW_SIZE * rows) / NBYTES(panel_info.vl_bpix);
188 i++) {
189 *ppix++ = COLOR_MASK(lcd_color_bg);
190 }
191#endif
Simon Glass9a8efc42012-10-30 13:40:18 +0000192 lcd_sync();
Simon Glass0d89efe2012-10-17 13:24:59 +0000193 console_row -= rows;
wdenk8655b6f2004-10-09 23:25:58 +0000194}
195
196/*----------------------------------------------------------------------*/
197
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000198static inline void console_back(void)
wdenk8655b6f2004-10-09 23:25:58 +0000199{
200 if (--console_col < 0) {
201 console_col = CONSOLE_COLS-1 ;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000202 if (--console_row < 0)
wdenk8655b6f2004-10-09 23:25:58 +0000203 console_row = 0;
wdenk8655b6f2004-10-09 23:25:58 +0000204 }
205
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000206 lcd_putc_xy(console_col * VIDEO_FONT_WIDTH,
207 console_row * VIDEO_FONT_HEIGHT, ' ');
wdenk8655b6f2004-10-09 23:25:58 +0000208}
209
210/*----------------------------------------------------------------------*/
211
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000212static inline void console_newline(void)
wdenk8655b6f2004-10-09 23:25:58 +0000213{
wdenk8655b6f2004-10-09 23:25:58 +0000214 console_col = 0;
215
216 /* Check if we need to scroll the terminal */
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000217 if (++console_row >= CONSOLE_ROWS)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000218 console_scrollup();
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000219 else
Simon Glass9a8efc42012-10-30 13:40:18 +0000220 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000221}
222
223/*----------------------------------------------------------------------*/
224
Simon Glass709ea542014-07-23 06:54:59 -0600225static void lcd_stub_putc(struct stdio_dev *dev, const char c)
226{
227 lcd_putc(c);
228}
229
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000230void lcd_putc(const char c)
wdenk8655b6f2004-10-09 23:25:58 +0000231{
232 if (!lcd_is_enabled) {
233 serial_putc(c);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000234
wdenk8655b6f2004-10-09 23:25:58 +0000235 return;
236 }
237
238 switch (c) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000239 case '\r':
240 console_col = 0;
wdenk8655b6f2004-10-09 23:25:58 +0000241
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000242 return;
243 case '\n':
244 console_newline();
wdenk8655b6f2004-10-09 23:25:58 +0000245
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000246 return;
wdenk8655b6f2004-10-09 23:25:58 +0000247 case '\t': /* Tab (8 chars alignment) */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000248 console_col += 8;
249 console_col &= ~7;
wdenk8655b6f2004-10-09 23:25:58 +0000250
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000251 if (console_col >= CONSOLE_COLS)
252 console_newline();
wdenk8655b6f2004-10-09 23:25:58 +0000253
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000254 return;
255 case '\b':
256 console_back();
wdenk8655b6f2004-10-09 23:25:58 +0000257
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000258 return;
259 default:
260 lcd_putc_xy(console_col * VIDEO_FONT_WIDTH,
261 console_row * VIDEO_FONT_HEIGHT, c);
262 if (++console_col >= CONSOLE_COLS)
263 console_newline();
wdenk8655b6f2004-10-09 23:25:58 +0000264 }
wdenk8655b6f2004-10-09 23:25:58 +0000265}
266
267/*----------------------------------------------------------------------*/
268
Simon Glass709ea542014-07-23 06:54:59 -0600269static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
270{
271 lcd_puts(s);
272}
273
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000274void lcd_puts(const char *s)
wdenk8655b6f2004-10-09 23:25:58 +0000275{
276 if (!lcd_is_enabled) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000277 serial_puts(s);
278
wdenk8655b6f2004-10-09 23:25:58 +0000279 return;
280 }
281
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000282 while (*s)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000283 lcd_putc(*s++);
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000284
Simon Glass9a8efc42012-10-30 13:40:18 +0000285 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000286}
287
Haavard Skinnemoen15b17ab2008-09-01 16:21:20 +0200288/*----------------------------------------------------------------------*/
289
290void lcd_printf(const char *fmt, ...)
291{
292 va_list args;
293 char buf[CONFIG_SYS_PBSIZE];
294
295 va_start(args, fmt);
296 vsprintf(buf, fmt, args);
297 va_end(args);
298
299 lcd_puts(buf);
300}
301
wdenk8655b6f2004-10-09 23:25:58 +0000302/************************************************************************/
303/* ** Low-Level Graphics Routines */
304/************************************************************************/
305
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000306static void lcd_drawchars(ushort x, ushort y, uchar *str, int count)
wdenk8655b6f2004-10-09 23:25:58 +0000307{
308 uchar *dest;
Marek Vasutbe547c62011-09-26 02:26:04 +0200309 ushort row;
310
Anatolij Gustschin0f999c42012-04-27 04:41:16 +0000311#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
312 y += BMP_LOGO_HEIGHT;
313#endif
314
Marek Vasutbe547c62011-09-26 02:26:04 +0200315#if LCD_BPP == LCD_MONOCHROME
316 ushort off = x * (1 << LCD_BPP) % 8;
317#endif
wdenk8655b6f2004-10-09 23:25:58 +0000318
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100319 dest = (uchar *)(lcd_base + y * lcd_line_length + x * NBITS(LCD_BPP)/8);
wdenk8655b6f2004-10-09 23:25:58 +0000320
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000321 for (row = 0; row < VIDEO_FONT_HEIGHT; ++row, dest += lcd_line_length) {
wdenk8655b6f2004-10-09 23:25:58 +0000322 uchar *s = str;
wdenk8655b6f2004-10-09 23:25:58 +0000323 int i;
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100324#if LCD_BPP == LCD_COLOR16
325 ushort *d = (ushort *)dest;
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100326#elif LCD_BPP == LCD_COLOR32
327 u32 *d = (u32 *)dest;
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100328#else
329 uchar *d = dest;
330#endif
wdenk8655b6f2004-10-09 23:25:58 +0000331
332#if LCD_BPP == LCD_MONOCHROME
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000333 uchar rest = *d & -(1 << (8 - off));
wdenk8655b6f2004-10-09 23:25:58 +0000334 uchar sym;
335#endif
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000336 for (i = 0; i < count; ++i) {
wdenk8655b6f2004-10-09 23:25:58 +0000337 uchar c, bits;
338
339 c = *s++;
340 bits = video_fontdata[c * VIDEO_FONT_HEIGHT + row];
341
342#if LCD_BPP == LCD_MONOCHROME
343 sym = (COLOR_MASK(lcd_color_fg) & bits) |
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000344 (COLOR_MASK(lcd_color_bg) & ~bits);
wdenk8655b6f2004-10-09 23:25:58 +0000345
346 *d++ = rest | (sym >> off);
347 rest = sym << (8-off);
348#elif LCD_BPP == LCD_COLOR8
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000349 for (c = 0; c < 8; ++c) {
wdenk8655b6f2004-10-09 23:25:58 +0000350 *d++ = (bits & 0x80) ?
351 lcd_color_fg : lcd_color_bg;
352 bits <<= 1;
353 }
354#elif LCD_BPP == LCD_COLOR16
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000355 for (c = 0; c < 8; ++c) {
wdenk8655b6f2004-10-09 23:25:58 +0000356 *d++ = (bits & 0x80) ?
357 lcd_color_fg : lcd_color_bg;
358 bits <<= 1;
359 }
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100360#elif LCD_BPP == LCD_COLOR32
361 for (c = 0; c < 8; ++c) {
362 *d++ = (bits & 0x80) ?
363 lcd_color_fg : lcd_color_bg;
364 bits <<= 1;
365 }
wdenk8655b6f2004-10-09 23:25:58 +0000366#endif
367 }
368#if LCD_BPP == LCD_MONOCHROME
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000369 *d = rest | (*d & ((1 << (8 - off)) - 1));
wdenk8655b6f2004-10-09 23:25:58 +0000370#endif
371 }
372}
373
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000374static inline void lcd_putc_xy(ushort x, ushort y, uchar c)
wdenk8655b6f2004-10-09 23:25:58 +0000375{
Anatolij Gustschin0f999c42012-04-27 04:41:16 +0000376 lcd_drawchars(x, y, &c, 1);
wdenk8655b6f2004-10-09 23:25:58 +0000377}
378
379/************************************************************************/
380/** Small utility to check that you got the colours right */
381/************************************************************************/
382#ifdef LCD_TEST_PATTERN
383
384#define N_BLK_VERT 2
385#define N_BLK_HOR 3
386
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000387static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
wdenk8655b6f2004-10-09 23:25:58 +0000388 CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
389 CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
390};
391
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000392static void test_pattern(void)
wdenk8655b6f2004-10-09 23:25:58 +0000393{
394 ushort v_max = panel_info.vl_row;
395 ushort h_max = panel_info.vl_col;
396 ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
397 ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
398 ushort v, h;
399 uchar *pix = (uchar *)lcd_base;
400
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000401 printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
wdenk8655b6f2004-10-09 23:25:58 +0000402 h_max, v_max, h_step, v_step);
403
404 /* WARNING: Code silently assumes 8bit/pixel */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000405 for (v = 0; v < v_max; ++v) {
wdenk8655b6f2004-10-09 23:25:58 +0000406 uchar iy = v / v_step;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000407 for (h = 0; h < h_max; ++h) {
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000408 uchar ix = N_BLK_HOR * iy + h / h_step;
wdenk8655b6f2004-10-09 23:25:58 +0000409 *pix++ = test_colors[ix];
410 }
411 }
412}
413#endif /* LCD_TEST_PATTERN */
414
415
416/************************************************************************/
417/* ** GENERIC Initialization Routines */
418/************************************************************************/
Anatolij Gustschincefa4712013-11-09 11:00:09 +0100419/*
420 * With most lcd drivers the line length is set up
421 * by calculating it from panel_info parameters. Some
422 * drivers need to calculate the line length differently,
423 * so make the function weak to allow overriding it.
424 */
425__weak int lcd_get_size(int *line_length)
Simon Glass676d3192012-10-17 13:24:54 +0000426{
427 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
428 return *line_length * panel_info.vl_row;
429}
430
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000431int drv_lcd_init(void)
wdenk8655b6f2004-10-09 23:25:58 +0000432{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200433 struct stdio_dev lcddev;
wdenk8655b6f2004-10-09 23:25:58 +0000434 int rc;
435
Simon Glass7d95f2a2014-02-27 13:26:19 -0700436 lcd_base = map_sysmem(gd->fb_base, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000437
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000438 lcd_init(lcd_base); /* LCD initialization */
wdenk8655b6f2004-10-09 23:25:58 +0000439
440 /* Device initialization */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000441 memset(&lcddev, 0, sizeof(lcddev));
wdenk8655b6f2004-10-09 23:25:58 +0000442
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000443 strcpy(lcddev.name, "lcd");
wdenk8655b6f2004-10-09 23:25:58 +0000444 lcddev.ext = 0; /* No extensions */
445 lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
Simon Glass709ea542014-07-23 06:54:59 -0600446 lcddev.putc = lcd_stub_putc; /* 'putc' function */
447 lcddev.puts = lcd_stub_puts; /* 'puts' function */
wdenk8655b6f2004-10-09 23:25:58 +0000448
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000449 rc = stdio_register(&lcddev);
wdenk8655b6f2004-10-09 23:25:58 +0000450
451 return (rc == 0) ? 1 : rc;
452}
453
454/*----------------------------------------------------------------------*/
Che-Liang Chiou02110902011-10-20 23:07:03 +0000455void lcd_clear(void)
wdenk8655b6f2004-10-09 23:25:58 +0000456{
457#if LCD_BPP == LCD_MONOCHROME
458 /* Setting the palette */
459 lcd_initcolregs();
460
461#elif LCD_BPP == LCD_COLOR8
462 /* Setting the palette */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000463 lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
464 lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
465 lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
466 lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
467 lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
468 lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
469 lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
470 lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
471 lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
wdenk8655b6f2004-10-09 23:25:58 +0000472#endif
473
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200474#ifndef CONFIG_SYS_WHITE_ON_BLACK
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000475 lcd_setfgcolor(CONSOLE_COLOR_BLACK);
476 lcd_setbgcolor(CONSOLE_COLOR_WHITE);
wdenk8655b6f2004-10-09 23:25:58 +0000477#else
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000478 lcd_setfgcolor(CONSOLE_COLOR_WHITE);
479 lcd_setbgcolor(CONSOLE_COLOR_BLACK);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200480#endif /* CONFIG_SYS_WHITE_ON_BLACK */
wdenk8655b6f2004-10-09 23:25:58 +0000481
482#ifdef LCD_TEST_PATTERN
483 test_pattern();
484#else
485 /* set framebuffer to background color */
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100486#if (LCD_BPP != LCD_COLOR32)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000487 memset((char *)lcd_base,
Hannes Petermaier8d9eaaf2014-03-07 18:45:20 +0100488 COLOR_MASK(lcd_color_bg),
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000489 lcd_line_length * panel_info.vl_row);
Hannes Petermaier57d76a82014-03-07 18:55:40 +0100490#else
491 u32 *ppix = lcd_base;
492 u32 i;
493 for (i = 0;
494 i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
495 i++) {
496 *ppix++ = COLOR_MASK(lcd_color_bg);
497 }
498#endif
wdenk8655b6f2004-10-09 23:25:58 +0000499#endif
500 /* Paint the logo and retrieve LCD base address */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000501 debug("[LCD] Drawing the logo...\n");
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000502 lcd_console_address = lcd_logo();
wdenk8655b6f2004-10-09 23:25:58 +0000503
504 console_col = 0;
505 console_row = 0;
Simon Glass9a8efc42012-10-30 13:40:18 +0000506 lcd_sync();
507}
508
509static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
510 char *const argv[])
511{
512 lcd_clear();
513 return 0;
wdenk8655b6f2004-10-09 23:25:58 +0000514}
515
516U_BOOT_CMD(
Che-Liang Chiou02110902011-10-20 23:07:03 +0000517 cls, 1, 1, do_lcd_clear,
Peter Tyser2fb26042009-01-27 18:03:12 -0600518 "clear screen",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200519 ""
wdenk8655b6f2004-10-09 23:25:58 +0000520);
521
522/*----------------------------------------------------------------------*/
523
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000524static int lcd_init(void *lcdbase)
wdenk8655b6f2004-10-09 23:25:58 +0000525{
526 /* Initialize the lcd controller */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000527 debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
wdenk8655b6f2004-10-09 23:25:58 +0000528
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000529 lcd_ctrl_init(lcdbase);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100530
531 /*
532 * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi_b) ignores
533 * the 'lcdbase' argument and uses custom lcd base address
534 * by setting up gd->fb_base. Check for this condition and fixup
535 * 'lcd_base' address.
536 */
Simon Glass7d95f2a2014-02-27 13:26:19 -0700537 if (map_to_sysmem(lcdbase) != gd->fb_base)
538 lcd_base = map_sysmem(gd->fb_base, 0);
Anatolij Gustschin1d3dea12013-03-29 14:00:13 +0100539
540 debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
541
Stephen Warren6d330712013-01-29 16:37:38 +0000542 lcd_get_size(&lcd_line_length);
Haavard Skinnemoen6f93d2b2008-09-01 16:21:21 +0200543 lcd_is_enabled = 1;
Che-Liang Chiou02110902011-10-20 23:07:03 +0000544 lcd_clear();
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000545 lcd_enable();
wdenk8655b6f2004-10-09 23:25:58 +0000546
547 /* Initialize the console */
548 console_col = 0;
wdenk88804d12005-07-04 00:03:16 +0000549#ifdef CONFIG_LCD_INFO_BELOW_LOGO
wdenk8655b6f2004-10-09 23:25:58 +0000550 console_row = 7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT;
551#else
552 console_row = 1; /* leave 1 blank line below logo */
553#endif
wdenk8655b6f2004-10-09 23:25:58 +0000554
555 return 0;
556}
557
558
559/************************************************************************/
560/* ** ROM capable initialization part - needed to reserve FB memory */
561/************************************************************************/
562/*
563 * This is called early in the system initialization to grab memory
564 * for the LCD controller.
565 * Returns new address for monitor, after reserving LCD buffer memory
566 *
567 * Note that this is running from ROM, so no write access to global data.
568 */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000569ulong lcd_setmem(ulong addr)
wdenk8655b6f2004-10-09 23:25:58 +0000570{
571 ulong size;
Simon Glass676d3192012-10-17 13:24:54 +0000572 int line_length;
wdenk8655b6f2004-10-09 23:25:58 +0000573
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000574 debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
575 panel_info.vl_row, NBITS(panel_info.vl_bpix));
wdenk8655b6f2004-10-09 23:25:58 +0000576
Simon Glass676d3192012-10-17 13:24:54 +0000577 size = lcd_get_size(&line_length);
wdenk8655b6f2004-10-09 23:25:58 +0000578
Simon Glass676d3192012-10-17 13:24:54 +0000579 /* Round up to nearest full page, or MMU section if defined */
580 size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
581 addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
wdenk8655b6f2004-10-09 23:25:58 +0000582
583 /* Allocate pages for the frame buffer. */
584 addr -= size;
585
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000586 debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
587 size >> 10, addr);
wdenk8655b6f2004-10-09 23:25:58 +0000588
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000589 return addr;
wdenk8655b6f2004-10-09 23:25:58 +0000590}
591
592/*----------------------------------------------------------------------*/
593
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000594static void lcd_setfgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000595{
Stelian Pop39cf4802008-05-09 21:57:18 +0200596 lcd_color_fg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000597}
598
599/*----------------------------------------------------------------------*/
600
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000601static void lcd_setbgcolor(int color)
wdenk8655b6f2004-10-09 23:25:58 +0000602{
Stelian Pop39cf4802008-05-09 21:57:18 +0200603 lcd_color_bg = color;
wdenk8655b6f2004-10-09 23:25:58 +0000604}
605
wdenk8655b6f2004-10-09 23:25:58 +0000606/************************************************************************/
607/* ** Chipset depending Bitmap / Logo stuff... */
608/************************************************************************/
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000609static inline ushort *configuration_get_cmap(void)
610{
611#if defined CONFIG_CPU_PXA
612 struct pxafb_info *fbi = &panel_info.pxa;
613 return (ushort *)fbi->palette;
614#elif defined(CONFIG_MPC823)
615 immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
616 cpm8xx_t *cp = &(immr->im_cpm);
617 return (ushort *)&(cp->lcd_cmap[255 * sizeof(ushort)]);
618#elif defined(CONFIG_ATMEL_LCD)
619 return (ushort *)(panel_info.mmio + ATMEL_LCDC_LUT(0));
Anatolij Gustschind23019f2012-09-22 06:55:53 +0000620#elif !defined(CONFIG_ATMEL_HLCD) && !defined(CONFIG_EXYNOS_FB)
621 return panel_info.cmap;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000622#elif defined(CONFIG_LCD_LOGO)
Anatolij Gustschind23019f2012-09-22 06:55:53 +0000623 return bmp_logo_palette;
624#else
625 return NULL;
626#endif
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000627}
628
wdenk8655b6f2004-10-09 23:25:58 +0000629#ifdef CONFIG_LCD_LOGO
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000630void bitmap_plot(int x, int y)
wdenk8655b6f2004-10-09 23:25:58 +0000631{
Stelian Pop39cf4802008-05-09 21:57:18 +0200632#ifdef CONFIG_ATMEL_LCD
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000633 uint *cmap = (uint *)bmp_logo_palette;
Stelian Pop39cf4802008-05-09 21:57:18 +0200634#else
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000635 ushort *cmap = (ushort *)bmp_logo_palette;
Stelian Pop39cf4802008-05-09 21:57:18 +0200636#endif
wdenk8655b6f2004-10-09 23:25:58 +0000637 ushort i, j;
638 uchar *bmap;
639 uchar *fb;
640 ushort *fb16;
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000641#if defined(CONFIG_MPC823)
642 immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
643 cpm8xx_t *cp = &(immr->im_cpm);
wdenk8655b6f2004-10-09 23:25:58 +0000644#endif
Andre Renaud317461c2013-02-13 17:48:00 +0000645 unsigned bpix = NBITS(panel_info.vl_bpix);
wdenk8655b6f2004-10-09 23:25:58 +0000646
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000647 debug("Logo: width %d height %d colors %d cmap %d\n",
wdenk8655b6f2004-10-09 23:25:58 +0000648 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS,
Anatolij Gustschin095407d2012-04-27 04:41:06 +0000649 ARRAY_SIZE(bmp_logo_palette));
wdenk8655b6f2004-10-09 23:25:58 +0000650
651 bmap = &bmp_logo_bitmap[0];
Andre Renaud317461c2013-02-13 17:48:00 +0000652 fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
wdenk8655b6f2004-10-09 23:25:58 +0000653
Andre Renaud317461c2013-02-13 17:48:00 +0000654 if (bpix < 12) {
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000655 /* Leave room for default color map
656 * default case: generic system with no cmap (most likely 16bpp)
657 * cmap was set to the source palette, so no change is done.
658 * This avoids even more ifdefs in the next stanza
659 */
660#if defined(CONFIG_MPC823)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000661 cmap = (ushort *) &(cp->lcd_cmap[BMP_LOGO_OFFSET * sizeof(ushort)]);
Stelian Pop39cf4802008-05-09 21:57:18 +0200662#elif defined(CONFIG_ATMEL_LCD)
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000663 cmap = (uint *)configuration_get_cmap();
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100664#else
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000665 cmap = configuration_get_cmap();
wdenk8655b6f2004-10-09 23:25:58 +0000666#endif
667
668 WATCHDOG_RESET();
669
670 /* Set color map */
Anatolij Gustschin095407d2012-04-27 04:41:06 +0000671 for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i) {
wdenk8655b6f2004-10-09 23:25:58 +0000672 ushort colreg = bmp_logo_palette[i];
Stelian Pop39cf4802008-05-09 21:57:18 +0200673#ifdef CONFIG_ATMEL_LCD
674 uint lut_entry;
675#ifdef CONFIG_ATMEL_LCD_BGR555
676 lut_entry = ((colreg & 0x000F) << 11) |
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000677 ((colreg & 0x00F0) << 2) |
678 ((colreg & 0x0F00) >> 7);
Stelian Pop39cf4802008-05-09 21:57:18 +0200679#else /* CONFIG_ATMEL_LCD_RGB565 */
680 lut_entry = ((colreg & 0x000F) << 1) |
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000681 ((colreg & 0x00F0) << 3) |
682 ((colreg & 0x0F00) << 4);
Stelian Pop39cf4802008-05-09 21:57:18 +0200683#endif
684 *(cmap + BMP_LOGO_OFFSET) = lut_entry;
685 cmap++;
686#else /* !CONFIG_ATMEL_LCD */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200687#ifdef CONFIG_SYS_INVERT_COLORS
wdenk8655b6f2004-10-09 23:25:58 +0000688 *cmap++ = 0xffff - colreg;
689#else
690 *cmap++ = colreg;
691#endif
Stelian Pop39cf4802008-05-09 21:57:18 +0200692#endif /* CONFIG_ATMEL_LCD */
wdenk8655b6f2004-10-09 23:25:58 +0000693 }
694
695 WATCHDOG_RESET();
696
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000697 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
698 memcpy(fb, bmap, BMP_LOGO_WIDTH);
wdenk8655b6f2004-10-09 23:25:58 +0000699 bmap += BMP_LOGO_WIDTH;
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000700 fb += panel_info.vl_col;
wdenk8655b6f2004-10-09 23:25:58 +0000701 }
702 }
703 else { /* true color mode */
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100704 u16 col16;
Andre Renaud317461c2013-02-13 17:48:00 +0000705 fb16 = (ushort *)fb;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000706 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
707 for (j = 0; j < BMP_LOGO_WIDTH; j++) {
Alessandro Rubiniacb13862010-03-13 17:44:08 +0100708 col16 = bmp_logo_palette[(bmap[j]-16)];
709 fb16[j] =
710 ((col16 & 0x000F) << 1) |
711 ((col16 & 0x00F0) << 3) |
712 ((col16 & 0x0F00) << 4);
wdenk8655b6f2004-10-09 23:25:58 +0000713 }
714 bmap += BMP_LOGO_WIDTH;
715 fb16 += panel_info.vl_col;
716 }
717 }
718
719 WATCHDOG_RESET();
Simon Glass9a8efc42012-10-30 13:40:18 +0000720 lcd_sync();
wdenk8655b6f2004-10-09 23:25:58 +0000721}
Anatolij Gustschin2b5cb3d2012-04-27 04:41:27 +0000722#else
723static inline void bitmap_plot(int x, int y) {}
wdenk8655b6f2004-10-09 23:25:58 +0000724#endif /* CONFIG_LCD_LOGO */
725
726/*----------------------------------------------------------------------*/
Jon Loeligerc3517f92007-07-08 18:10:08 -0500727#if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
wdenk8655b6f2004-10-09 23:25:58 +0000728/*
729 * Display the BMP file located at address bmp_image.
730 * Only uncompressed.
731 */
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200732
733#ifdef CONFIG_SPLASH_SCREEN_ALIGN
734#define BMP_ALIGN_CENTER 0x7FFF
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000735
736static void splash_align_axis(int *axis, unsigned long panel_size,
737 unsigned long picture_size)
738{
739 unsigned long panel_picture_delta = panel_size - picture_size;
740 unsigned long axis_alignment;
741
742 if (*axis == BMP_ALIGN_CENTER)
743 axis_alignment = panel_picture_delta / 2;
744 else if (*axis < 0)
745 axis_alignment = panel_picture_delta + *axis + 1;
746 else
747 return;
748
749 *axis = max(0, axis_alignment);
750}
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200751#endif
752
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000753
754#ifdef CONFIG_LCD_BMP_RLE8
755
756#define BMP_RLE8_ESCAPE 0
757#define BMP_RLE8_EOL 0
758#define BMP_RLE8_EOBMP 1
759#define BMP_RLE8_DELTA 2
760
761static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
762 int cnt)
763{
764 while (cnt > 0) {
765 *(*fbp)++ = cmap[*bmap++];
766 cnt--;
767 }
768}
769
770static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
771{
772 ushort *fb = *fbp;
773 int cnt_8copy = cnt >> 3;
774
775 cnt -= cnt_8copy << 3;
776 while (cnt_8copy > 0) {
777 *fb++ = c;
778 *fb++ = c;
779 *fb++ = c;
780 *fb++ = c;
781 *fb++ = c;
782 *fb++ = c;
783 *fb++ = c;
784 *fb++ = c;
785 cnt_8copy--;
786 }
787 while (cnt > 0) {
788 *fb++ = c;
789 cnt--;
790 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000791 *fbp = fb;
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000792}
793
794/*
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000795 * Do not call this function directly, must be called from lcd_display_bitmap.
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000796 */
797static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
798 int x_off, int y_off)
799{
800 uchar *bmap;
801 ulong width, height;
802 ulong cnt, runlen;
803 int x, y;
804 int decode = 1;
805
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100806 width = get_unaligned_le32(&bmp->header.width);
807 height = get_unaligned_le32(&bmp->header.height);
808 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +0000809
810 x = 0;
811 y = height - 1;
812
813 while (decode) {
814 if (bmap[0] == BMP_RLE8_ESCAPE) {
815 switch (bmap[1]) {
816 case BMP_RLE8_EOL:
817 /* end of line */
818 bmap += 2;
819 x = 0;
820 y--;
821 /* 16bpix, 2-byte per pixel, width should *2 */
822 fb -= (width * 2 + lcd_line_length);
823 break;
824 case BMP_RLE8_EOBMP:
825 /* end of bitmap */
826 decode = 0;
827 break;
828 case BMP_RLE8_DELTA:
829 /* delta run */
830 x += bmap[2];
831 y -= bmap[3];
832 /* 16bpix, 2-byte per pixel, x should *2 */
833 fb = (uchar *) (lcd_base + (y + y_off - 1)
834 * lcd_line_length + (x + x_off) * 2);
835 bmap += 4;
836 break;
837 default:
838 /* unencoded run */
839 runlen = bmap[1];
840 bmap += 2;
841 if (y < height) {
842 if (x < width) {
843 if (x + runlen > width)
844 cnt = width - x;
845 else
846 cnt = runlen;
847 draw_unencoded_bitmap(
848 (ushort **)&fb,
849 bmap, cmap, cnt);
850 }
851 x += runlen;
852 }
853 bmap += runlen;
854 if (runlen & 1)
855 bmap++;
856 }
857 } else {
858 /* encoded run */
859 if (y < height) {
860 runlen = bmap[0];
861 if (x < width) {
862 /* aggregate the same code */
863 while (bmap[0] == 0xff &&
864 bmap[2] != BMP_RLE8_ESCAPE &&
865 bmap[1] == bmap[3]) {
866 runlen += bmap[2];
867 bmap += 2;
868 }
869 if (x + runlen > width)
870 cnt = width - x;
871 else
872 cnt = runlen;
873 draw_encoded_bitmap((ushort **)&fb,
874 cmap[bmap[1]], cnt);
875 }
876 x += runlen;
877 }
878 bmap += 2;
879 }
880 }
881}
882#endif
883
Nikita Kiryanovecfdcee2014-11-11 15:46:05 +0200884#if defined(CONFIG_MPC823)
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000885#define FB_PUT_BYTE(fb, from) *(fb)++ = (255 - *(from)++)
Anatolij Gustschind23019f2012-09-22 06:55:53 +0000886#else
887#define FB_PUT_BYTE(fb, from) *(fb)++ = *(from)++
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +0000888#endif
889
890#if defined(CONFIG_BMP_16BPP)
891#if defined(CONFIG_ATMEL_LCD_BGR555)
892static inline void fb_put_word(uchar **fb, uchar **from)
893{
894 *(*fb)++ = (((*from)[0] & 0x1f) << 2) | ((*from)[1] & 0x03);
895 *(*fb)++ = ((*from)[0] & 0xe0) | (((*from)[1] & 0x7c) >> 2);
896 *from += 2;
897}
898#else
899static inline void fb_put_word(uchar **fb, uchar **from)
900{
901 *(*fb)++ = *(*from)++;
902 *(*fb)++ = *(*from)++;
903}
904#endif
905#endif /* CONFIG_BMP_16BPP */
906
wdenk8655b6f2004-10-09 23:25:58 +0000907int lcd_display_bitmap(ulong bmp_image, int x, int y)
908{
Anatolij Gustschin00cc5592009-02-25 20:28:13 +0100909 ushort *cmap = NULL;
Anatolij Gustschin00cc5592009-02-25 20:28:13 +0100910 ushort *cmap_base = NULL;
wdenk8655b6f2004-10-09 23:25:58 +0000911 ushort i, j;
912 uchar *fb;
Simon Glass7d95f2a2014-02-27 13:26:19 -0700913 bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
wdenk8655b6f2004-10-09 23:25:58 +0000914 uchar *bmap;
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +0000915 ushort padded_width;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100916 unsigned long width, height, byte_width;
Wolfgang Denke8143e72006-08-30 23:09:00 +0200917 unsigned long pwidth = panel_info.vl_col;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100918 unsigned colors, bpix, bmp_bpix;
wdenk8655b6f2004-10-09 23:25:58 +0000919
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000920 if (!bmp || !(bmp->header.signature[0] == 'B' &&
921 bmp->header.signature[1] == 'M')) {
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000922 printf("Error: no valid bmp image at %lx\n", bmp_image);
923
wdenk8655b6f2004-10-09 23:25:58 +0000924 return 1;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100925 }
wdenk8655b6f2004-10-09 23:25:58 +0000926
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100927 width = get_unaligned_le32(&bmp->header.width);
928 height = get_unaligned_le32(&bmp->header.height);
929 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
930
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100931 colors = 1 << bmp_bpix;
wdenk8655b6f2004-10-09 23:25:58 +0000932
933 bpix = NBITS(panel_info.vl_bpix);
934
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000935 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100936 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
937 bpix, bmp_bpix);
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000938
wdenk8655b6f2004-10-09 23:25:58 +0000939 return 1;
940 }
941
Hannes Petermaiera305fb12014-07-15 16:28:46 +0200942 /*
943 * We support displaying 8bpp BMPs on 16bpp LCDs
944 * and displaying 24bpp BMPs on 32bpp LCDs
945 * */
946 if (bpix != bmp_bpix &&
947 !(bmp_bpix == 8 && bpix == 16) &&
948 !(bmp_bpix == 24 && bpix == 32)) {
wdenk8655b6f2004-10-09 23:25:58 +0000949 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100950 bpix, get_unaligned_le16(&bmp->header.bit_count));
wdenk8655b6f2004-10-09 23:25:58 +0000951 return 1;
952 }
953
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000954 debug("Display-bmp: %d x %d with %d colors\n",
wdenk8655b6f2004-10-09 23:25:58 +0000955 (int)width, (int)height, (int)colors);
956
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100957 if (bmp_bpix == 8) {
Nikita Kiryanov203c37b2012-08-09 00:14:52 +0000958 cmap = configuration_get_cmap();
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100959 cmap_base = cmap;
960
wdenk8655b6f2004-10-09 23:25:58 +0000961 /* Set color map */
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000962 for (i = 0; i < colors; ++i) {
wdenk8655b6f2004-10-09 23:25:58 +0000963 bmp_color_table_entry_t cte = bmp->color_table[i];
Mark Jackson1464eff2008-08-01 09:48:29 +0100964#if !defined(CONFIG_ATMEL_LCD)
wdenk8655b6f2004-10-09 23:25:58 +0000965 ushort colreg =
966 ( ((cte.red) << 8) & 0xf800) |
Wolfgang Denk59d80bf2005-09-21 15:24:52 +0200967 ( ((cte.green) << 3) & 0x07e0) |
968 ( ((cte.blue) >> 3) & 0x001f) ;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200969#ifdef CONFIG_SYS_INVERT_COLORS
wdenk25d67122004-12-10 11:40:40 +0000970 *cmap = 0xffff - colreg;
wdenk8655b6f2004-10-09 23:25:58 +0000971#else
wdenk25d67122004-12-10 11:40:40 +0000972 *cmap = colreg;
973#endif
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100974#if defined(CONFIG_MPC823)
wdenk25d67122004-12-10 11:40:40 +0000975 cmap--;
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +0100976#else
977 cmap++;
wdenk8655b6f2004-10-09 23:25:58 +0000978#endif
Mark Jackson1464eff2008-08-01 09:48:29 +0100979#else /* CONFIG_ATMEL_LCD */
980 lcd_setcolreg(i, cte.red, cte.green, cte.blue);
981#endif
wdenk8655b6f2004-10-09 23:25:58 +0000982 }
983 }
Wolfgang Denke8143e72006-08-30 23:09:00 +0200984
Jeroen Hofstee6b035142013-01-12 12:07:56 +0000985 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200986
987#ifdef CONFIG_SPLASH_SCREEN_ALIGN
Nikita Kiryanov7c7e2802012-08-09 00:14:51 +0000988 splash_align_axis(&x, pwidth, width);
989 splash_align_axis(&y, panel_info.vl_row, height);
Matthias Weisser1ca298c2009-07-09 16:07:30 +0200990#endif /* CONFIG_SPLASH_SCREEN_ALIGN */
991
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000992 if ((x + width) > pwidth)
Wolfgang Denke8143e72006-08-30 23:09:00 +0200993 width = pwidth - x;
Nikita Kiryanov8f47d912012-05-24 01:42:38 +0000994 if ((y + height) > panel_info.vl_row)
wdenk8655b6f2004-10-09 23:25:58 +0000995 height = panel_info.vl_row - y;
996
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +0100997 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
998 fb = (uchar *)(lcd_base +
Liu Ying8d46d5b2011-01-11 15:29:58 +0800999 (y + height - 1) * lcd_line_length + x * bpix / 8);
Mark Jacksona303dfb2009-02-06 10:37:49 +01001000
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +01001001 switch (bmp_bpix) {
Mark Jacksona303dfb2009-02-06 10:37:49 +01001002 case 1: /* pass through */
Simon Glass01564442014-10-15 04:53:04 -06001003 case 8: {
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +00001004#ifdef CONFIG_LCD_BMP_RLE8
Przemyslaw Marczakdca2a1c2014-01-22 11:24:13 +01001005 u32 compression = get_unaligned_le32(&bmp->header.compression);
1006 if (compression == BMP_BI_RLE8) {
Tom Wai-Hong Tam45d7f522012-09-28 15:11:16 +00001007 if (bpix != 16) {
1008 /* TODO implement render code for bpix != 16 */
1009 printf("Error: only support 16 bpix");
1010 return 1;
1011 }
1012 lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
1013 break;
1014 }
1015#endif
1016
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +01001017 if (bpix != 16)
1018 byte_width = width;
1019 else
1020 byte_width = width * 2;
1021
Mark Jacksona303dfb2009-02-06 10:37:49 +01001022 for (i = 0; i < height; ++i) {
1023 WATCHDOG_RESET();
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +01001024 for (j = 0; j < width; j++) {
1025 if (bpix != 16) {
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +00001026 FB_PUT_BYTE(fb, bmap);
Guennadi Liakhovetskib245e652009-02-06 10:37:53 +01001027 } else {
1028 *(uint16_t *)fb = cmap_base[*(bmap++)];
1029 fb += sizeof(uint16_t) / sizeof(*fb);
1030 }
1031 }
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +00001032 bmap += (padded_width - width);
Jeroen Hofstee6b035142013-01-12 12:07:56 +00001033 fb -= byte_width + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +01001034 }
1035 break;
Simon Glass01564442014-10-15 04:53:04 -06001036 }
Mark Jacksona303dfb2009-02-06 10:37:49 +01001037#if defined(CONFIG_BMP_16BPP)
1038 case 16:
1039 for (i = 0; i < height; ++i) {
1040 WATCHDOG_RESET();
Nikita Kiryanovbfdcc652012-08-09 00:14:53 +00001041 for (j = 0; j < width; j++)
1042 fb_put_word(&fb, &bmap);
1043
Tom Wai-Hong Tamfecac462012-09-28 15:11:14 +00001044 bmap += (padded_width - width) * 2;
Jeroen Hofstee6b035142013-01-12 12:07:56 +00001045 fb -= width * 2 + lcd_line_length;
Mark Jacksona303dfb2009-02-06 10:37:49 +01001046 }
1047 break;
1048#endif /* CONFIG_BMP_16BPP */
Hannes Petermaiera305fb12014-07-15 16:28:46 +02001049#if defined(CONFIG_BMP_24BMP)
1050 case 24:
1051 for (i = 0; i < height; ++i) {
1052 for (j = 0; j < width; j++) {
1053 *(fb++) = *(bmap++);
1054 *(fb++) = *(bmap++);
1055 *(fb++) = *(bmap++);
1056 *(fb++) = 0;
1057 }
1058 fb -= lcd_line_length + width * (bpix / 8);
1059 }
1060 break;
1061#endif /* CONFIG_BMP_24BMP */
Donghwa Leefb6a9aa2012-05-09 19:23:37 +00001062#if defined(CONFIG_BMP_32BPP)
1063 case 32:
1064 for (i = 0; i < height; ++i) {
1065 for (j = 0; j < width; j++) {
1066 *(fb++) = *(bmap++);
1067 *(fb++) = *(bmap++);
1068 *(fb++) = *(bmap++);
1069 *(fb++) = *(bmap++);
1070 }
Jeroen Hofstee6b035142013-01-12 12:07:56 +00001071 fb -= lcd_line_length + width * (bpix / 8);
Donghwa Leefb6a9aa2012-05-09 19:23:37 +00001072 }
1073 break;
1074#endif /* CONFIG_BMP_32BPP */
Mark Jacksona303dfb2009-02-06 10:37:49 +01001075 default:
1076 break;
1077 };
wdenk8655b6f2004-10-09 23:25:58 +00001078
Simon Glass9a8efc42012-10-30 13:40:18 +00001079 lcd_sync();
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001080 return 0;
wdenk8655b6f2004-10-09 23:25:58 +00001081}
Jon Loeligerc3517f92007-07-08 18:10:08 -05001082#endif
wdenk8655b6f2004-10-09 23:25:58 +00001083
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001084static void *lcd_logo(void)
wdenk8655b6f2004-10-09 23:25:58 +00001085{
wdenk8655b6f2004-10-09 23:25:58 +00001086#ifdef CONFIG_SPLASH_SCREEN
1087 char *s;
1088 ulong addr;
1089 static int do_splash = 1;
1090
1091 if (do_splash && (s = getenv("splashimage")) != NULL) {
Matthias Weisser1ca298c2009-07-09 16:07:30 +02001092 int x = 0, y = 0;
wdenk8655b6f2004-10-09 23:25:58 +00001093 do_splash = 0;
1094
Nikita Kiryanov581bb412013-01-30 21:39:57 +00001095 if (splash_screen_prepare())
Robert Winklerdd4425e2013-06-17 11:31:29 -07001096 return (void *)lcd_base;
Nikita Kiryanov581bb412013-01-30 21:39:57 +00001097
Matthias Weisser1ca298c2009-07-09 16:07:30 +02001098 addr = simple_strtoul (s, NULL, 16);
Matthias Weisser1ca298c2009-07-09 16:07:30 +02001099
Anatolij Gustschinff8fb562013-07-02 00:04:05 +02001100 splash_get_pos(&x, &y);
Matthias Weisser1ca298c2009-07-09 16:07:30 +02001101
Nikita Kiryanovd3a555e2012-08-09 00:14:50 +00001102 if (bmp_display(addr, x, y) == 0)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001103 return (void *)lcd_base;
wdenk8655b6f2004-10-09 23:25:58 +00001104 }
1105#endif /* CONFIG_SPLASH_SCREEN */
1106
Anatolij Gustschin2b5cb3d2012-04-27 04:41:27 +00001107 bitmap_plot(0, 0);
wdenk8655b6f2004-10-09 23:25:58 +00001108
Haavard Skinnemoen6b59e032008-09-01 16:21:22 +02001109#ifdef CONFIG_LCD_INFO
1110 console_col = LCD_INFO_X / VIDEO_FONT_WIDTH;
1111 console_row = LCD_INFO_Y / VIDEO_FONT_HEIGHT;
1112 lcd_show_board_info();
1113#endif /* CONFIG_LCD_INFO */
Stelian Pop39cf4802008-05-09 21:57:18 +02001114
wdenk88804d12005-07-04 00:03:16 +00001115#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001116 return (void *)((ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length);
wdenk8655b6f2004-10-09 23:25:58 +00001117#else
Nikita Kiryanov8f47d912012-05-24 01:42:38 +00001118 return (void *)lcd_base;
Jeroen Hofstee6b035142013-01-12 12:07:56 +00001119#endif /* CONFIG_LCD_LOGO && !defined(CONFIG_LCD_INFO_BELOW_LOGO) */
wdenk8655b6f2004-10-09 23:25:58 +00001120}
1121
Nikita Kiryanovc0880482013-02-24 21:28:43 +00001122#ifdef CONFIG_SPLASHIMAGE_GUARD
1123static int on_splashimage(const char *name, const char *value, enum env_op op,
1124 int flags)
1125{
1126 ulong addr;
1127 int aligned;
1128
1129 if (op == env_op_delete)
1130 return 0;
1131
1132 addr = simple_strtoul(value, NULL, 16);
1133 /* See README.displaying-bmps */
1134 aligned = (addr % 4 == 2);
1135 if (!aligned) {
1136 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
1137 return -1;
1138 }
1139
1140 return 0;
1141}
1142
1143U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
1144#endif
1145
Vadim Bendebury395166c2012-09-28 15:11:13 +00001146void lcd_position_cursor(unsigned col, unsigned row)
1147{
1148 console_col = min(col, CONSOLE_COLS - 1);
1149 console_row = min(row, CONSOLE_ROWS - 1);
1150}
1151
1152int lcd_get_pixel_width(void)
1153{
1154 return panel_info.vl_col;
1155}
1156
1157int lcd_get_pixel_height(void)
1158{
1159 return panel_info.vl_row;
1160}
1161
1162int lcd_get_screen_rows(void)
1163{
1164 return CONSOLE_ROWS;
1165}
1166
1167int lcd_get_screen_columns(void)
1168{
1169 return CONSOLE_COLS;
1170}
Stephen Warren6a195d22013-05-27 18:31:17 +00001171
1172#if defined(CONFIG_LCD_DT_SIMPLEFB)
1173static int lcd_dt_simplefb_configure_node(void *blob, int off)
1174{
1175 u32 stride;
1176 fdt32_t cells[2];
1177 int ret;
Stephen Warren5af7d0f2013-06-13 17:13:11 -06001178 static const char format[] =
Stephen Warren6a195d22013-05-27 18:31:17 +00001179#if LCD_BPP == LCD_COLOR16
1180 "r5g6b5";
1181#else
1182 "";
1183#endif
1184
1185 if (!format[0])
1186 return -1;
1187
1188 stride = panel_info.vl_col * 2;
1189
1190 cells[0] = cpu_to_fdt32(gd->fb_base);
1191 cells[1] = cpu_to_fdt32(stride * panel_info.vl_row);
1192 ret = fdt_setprop(blob, off, "reg", cells, sizeof(cells[0]) * 2);
1193 if (ret < 0)
1194 return -1;
1195
1196 cells[0] = cpu_to_fdt32(panel_info.vl_col);
1197 ret = fdt_setprop(blob, off, "width", cells, sizeof(cells[0]));
1198 if (ret < 0)
1199 return -1;
1200
1201 cells[0] = cpu_to_fdt32(panel_info.vl_row);
1202 ret = fdt_setprop(blob, off, "height", cells, sizeof(cells[0]));
1203 if (ret < 0)
1204 return -1;
1205
1206 cells[0] = cpu_to_fdt32(stride);
1207 ret = fdt_setprop(blob, off, "stride", cells, sizeof(cells[0]));
1208 if (ret < 0)
1209 return -1;
1210
1211 ret = fdt_setprop(blob, off, "format", format, strlen(format) + 1);
1212 if (ret < 0)
1213 return -1;
1214
1215 ret = fdt_delprop(blob, off, "status");
1216 if (ret < 0)
1217 return -1;
1218
1219 return 0;
1220}
1221
1222int lcd_dt_simplefb_add_node(void *blob)
1223{
Stephen Warren5af7d0f2013-06-13 17:13:11 -06001224 static const char compat[] = "simple-framebuffer";
1225 static const char disabled[] = "disabled";
Stephen Warren6a195d22013-05-27 18:31:17 +00001226 int off, ret;
1227
1228 off = fdt_add_subnode(blob, 0, "framebuffer");
1229 if (off < 0)
1230 return -1;
1231
1232 ret = fdt_setprop(blob, off, "status", disabled, sizeof(disabled));
1233 if (ret < 0)
1234 return -1;
1235
1236 ret = fdt_setprop(blob, off, "compatible", compat, sizeof(compat));
1237 if (ret < 0)
1238 return -1;
1239
1240 return lcd_dt_simplefb_configure_node(blob, off);
1241}
1242
1243int lcd_dt_simplefb_enable_existing_node(void *blob)
1244{
1245 int off;
1246
1247 off = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
1248 if (off < 0)
1249 return -1;
1250
1251 return lcd_dt_simplefb_configure_node(blob, off);
1252}
1253#endif