lcd.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. * Common LCD routines for supported CPUs
  3. *
  4. * (C) Copyright 2001-2002
  5. * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. /************************************************************************/
  10. /* ** HEADER FILES */
  11. /************************************************************************/
  12. /* #define DEBUG */
  13. #include <config.h>
  14. #include <common.h>
  15. #include <command.h>
  16. #include <stdarg.h>
  17. #include <search.h>
  18. #include <env_callback.h>
  19. #include <linux/types.h>
  20. #include <stdio_dev.h>
  21. #if defined(CONFIG_POST)
  22. #include <post.h>
  23. #endif
  24. #include <lcd.h>
  25. #include <watchdog.h>
  26. #include <asm/unaligned.h>
  27. #include <splash.h>
  28. #include <asm/io.h>
  29. #include <asm/unaligned.h>
  30. #if defined(CONFIG_CPU_PXA25X) || defined(CONFIG_CPU_PXA27X) || \
  31. defined(CONFIG_CPU_MONAHANS)
  32. #include <asm/byteorder.h>
  33. #endif
  34. #if defined(CONFIG_MPC823)
  35. #include <lcdvideo.h>
  36. #endif
  37. #if defined(CONFIG_ATMEL_LCD)
  38. #include <atmel_lcdc.h>
  39. #endif
  40. #if defined(CONFIG_LCD_DT_SIMPLEFB)
  41. #include <libfdt.h>
  42. #endif
  43. /************************************************************************/
  44. /* ** FONT DATA */
  45. /************************************************************************/
  46. #include <video_font.h> /* Get font data, width and height */
  47. /************************************************************************/
  48. /* ** LOGO DATA */
  49. /************************************************************************/
  50. #ifdef CONFIG_LCD_LOGO
  51. # include <bmp_logo.h> /* Get logo data, width and height */
  52. # include <bmp_logo_data.h>
  53. # if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
  54. # error Default Color Map overlaps with Logo Color Map
  55. # endif
  56. #endif
  57. #ifdef CONFIG_SANDBOX
  58. #include <asm/sdl.h>
  59. #endif
  60. #ifndef CONFIG_LCD_ALIGNMENT
  61. #define CONFIG_LCD_ALIGNMENT PAGE_SIZE
  62. #endif
  63. /* By default we scroll by a single line */
  64. #ifndef CONFIG_CONSOLE_SCROLL_LINES
  65. #define CONFIG_CONSOLE_SCROLL_LINES 1
  66. #endif
  67. /************************************************************************/
  68. /* ** CONSOLE DEFINITIONS & FUNCTIONS */
  69. /************************************************************************/
  70. #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
  71. # define CONSOLE_ROWS ((panel_info.vl_row-BMP_LOGO_HEIGHT) \
  72. / VIDEO_FONT_HEIGHT)
  73. #else
  74. # define CONSOLE_ROWS (panel_info.vl_row / VIDEO_FONT_HEIGHT)
  75. #endif
  76. #define CONSOLE_COLS (panel_info.vl_col / VIDEO_FONT_WIDTH)
  77. #define CONSOLE_ROW_SIZE (VIDEO_FONT_HEIGHT * lcd_line_length)
  78. #define CONSOLE_ROW_FIRST lcd_console_address
  79. #define CONSOLE_ROW_SECOND (lcd_console_address + CONSOLE_ROW_SIZE)
  80. #define CONSOLE_ROW_LAST (lcd_console_address + CONSOLE_SIZE \
  81. - CONSOLE_ROW_SIZE)
  82. #define CONSOLE_SIZE (CONSOLE_ROW_SIZE * CONSOLE_ROWS)
  83. #define CONSOLE_SCROLL_SIZE (CONSOLE_SIZE - CONSOLE_ROW_SIZE)
  84. #if LCD_BPP == LCD_MONOCHROME
  85. # define COLOR_MASK(c) ((c) | (c) << 1 | (c) << 2 | (c) << 3 | \
  86. (c) << 4 | (c) << 5 | (c) << 6 | (c) << 7)
  87. #elif (LCD_BPP == LCD_COLOR8) || (LCD_BPP == LCD_COLOR16) || \
  88. (LCD_BPP == LCD_COLOR32)
  89. # define COLOR_MASK(c) (c)
  90. #else
  91. # error Unsupported LCD BPP.
  92. #endif
  93. DECLARE_GLOBAL_DATA_PTR;
  94. static void lcd_drawchars(ushort x, ushort y, uchar *str, int count);
  95. static inline void lcd_putc_xy(ushort x, ushort y, uchar c);
  96. static int lcd_init(void *lcdbase);
  97. static void *lcd_logo(void);
  98. static void lcd_setfgcolor(int color);
  99. static void lcd_setbgcolor(int color);
  100. static int lcd_color_fg;
  101. static int lcd_color_bg;
  102. int lcd_line_length;
  103. char lcd_is_enabled = 0;
  104. static short console_col;
  105. static short console_row;
  106. static void *lcd_console_address;
  107. static void *lcd_base; /* Start of framebuffer memory */
  108. static char lcd_flush_dcache; /* 1 to flush dcache after each lcd update */
  109. /************************************************************************/
  110. /* Flush LCD activity to the caches */
  111. void lcd_sync(void)
  112. {
  113. /*
  114. * flush_dcache_range() is declared in common.h but it seems that some
  115. * architectures do not actually implement it. Is there a way to find
  116. * out whether it exists? For now, ARM is safe.
  117. */
  118. #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
  119. int line_length;
  120. if (lcd_flush_dcache)
  121. flush_dcache_range((u32)lcd_base,
  122. (u32)(lcd_base + lcd_get_size(&line_length)));
  123. #elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
  124. static ulong last_sync;
  125. if (get_timer(last_sync) > 10) {
  126. sandbox_sdl_sync(lcd_base);
  127. last_sync = get_timer(0);
  128. }
  129. #endif
  130. }
  131. void lcd_set_flush_dcache(int flush)
  132. {
  133. lcd_flush_dcache = (flush != 0);
  134. }
  135. /*----------------------------------------------------------------------*/
  136. static void console_scrollup(void)
  137. {
  138. const int rows = CONFIG_CONSOLE_SCROLL_LINES;
  139. /* Copy up rows ignoring those that will be overwritten */
  140. memcpy(CONSOLE_ROW_FIRST,
  141. lcd_console_address + CONSOLE_ROW_SIZE * rows,
  142. CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows);
  143. /* Clear the last rows */
  144. #if (LCD_BPP != LCD_COLOR32)
  145. memset(lcd_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows,
  146. COLOR_MASK(lcd_color_bg),
  147. CONSOLE_ROW_SIZE * rows);
  148. #else
  149. u32 *ppix = lcd_console_address +
  150. CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows;
  151. u32 i;
  152. for (i = 0;
  153. i < (CONSOLE_ROW_SIZE * rows) / NBYTES(panel_info.vl_bpix);
  154. i++) {
  155. *ppix++ = COLOR_MASK(lcd_color_bg);
  156. }
  157. #endif
  158. lcd_sync();
  159. console_row -= rows;
  160. }
  161. /*----------------------------------------------------------------------*/
  162. static inline void console_back(void)
  163. {
  164. if (--console_col < 0) {
  165. console_col = CONSOLE_COLS-1 ;
  166. if (--console_row < 0)
  167. console_row = 0;
  168. }
  169. lcd_putc_xy(console_col * VIDEO_FONT_WIDTH,
  170. console_row * VIDEO_FONT_HEIGHT, ' ');
  171. }
  172. /*----------------------------------------------------------------------*/
  173. static inline void console_newline(void)
  174. {
  175. console_col = 0;
  176. /* Check if we need to scroll the terminal */
  177. if (++console_row >= CONSOLE_ROWS)
  178. console_scrollup();
  179. else
  180. lcd_sync();
  181. }
  182. /*----------------------------------------------------------------------*/
  183. static void lcd_stub_putc(struct stdio_dev *dev, const char c)
  184. {
  185. lcd_putc(c);
  186. }
  187. void lcd_putc(const char c)
  188. {
  189. if (!lcd_is_enabled) {
  190. serial_putc(c);
  191. return;
  192. }
  193. switch (c) {
  194. case '\r':
  195. console_col = 0;
  196. return;
  197. case '\n':
  198. console_newline();
  199. return;
  200. case '\t': /* Tab (8 chars alignment) */
  201. console_col += 8;
  202. console_col &= ~7;
  203. if (console_col >= CONSOLE_COLS)
  204. console_newline();
  205. return;
  206. case '\b':
  207. console_back();
  208. return;
  209. default:
  210. lcd_putc_xy(console_col * VIDEO_FONT_WIDTH,
  211. console_row * VIDEO_FONT_HEIGHT, c);
  212. if (++console_col >= CONSOLE_COLS)
  213. console_newline();
  214. }
  215. }
  216. /*----------------------------------------------------------------------*/
  217. static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
  218. {
  219. lcd_puts(s);
  220. }
  221. void lcd_puts(const char *s)
  222. {
  223. if (!lcd_is_enabled) {
  224. serial_puts(s);
  225. return;
  226. }
  227. while (*s)
  228. lcd_putc(*s++);
  229. lcd_sync();
  230. }
  231. /*----------------------------------------------------------------------*/
  232. void lcd_printf(const char *fmt, ...)
  233. {
  234. va_list args;
  235. char buf[CONFIG_SYS_PBSIZE];
  236. va_start(args, fmt);
  237. vsprintf(buf, fmt, args);
  238. va_end(args);
  239. lcd_puts(buf);
  240. }
  241. /************************************************************************/
  242. /* ** Low-Level Graphics Routines */
  243. /************************************************************************/
  244. static void lcd_drawchars(ushort x, ushort y, uchar *str, int count)
  245. {
  246. uchar *dest;
  247. ushort row;
  248. #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
  249. y += BMP_LOGO_HEIGHT;
  250. #endif
  251. #if LCD_BPP == LCD_MONOCHROME
  252. ushort off = x * (1 << LCD_BPP) % 8;
  253. #endif
  254. dest = (uchar *)(lcd_base + y * lcd_line_length + x * NBITS(LCD_BPP)/8);
  255. for (row = 0; row < VIDEO_FONT_HEIGHT; ++row, dest += lcd_line_length) {
  256. uchar *s = str;
  257. int i;
  258. #if LCD_BPP == LCD_COLOR16
  259. ushort *d = (ushort *)dest;
  260. #elif LCD_BPP == LCD_COLOR32
  261. u32 *d = (u32 *)dest;
  262. #else
  263. uchar *d = dest;
  264. #endif
  265. #if LCD_BPP == LCD_MONOCHROME
  266. uchar rest = *d & -(1 << (8 - off));
  267. uchar sym;
  268. #endif
  269. for (i = 0; i < count; ++i) {
  270. uchar c, bits;
  271. c = *s++;
  272. bits = video_fontdata[c * VIDEO_FONT_HEIGHT + row];
  273. #if LCD_BPP == LCD_MONOCHROME
  274. sym = (COLOR_MASK(lcd_color_fg) & bits) |
  275. (COLOR_MASK(lcd_color_bg) & ~bits);
  276. *d++ = rest | (sym >> off);
  277. rest = sym << (8-off);
  278. #elif LCD_BPP == LCD_COLOR8
  279. for (c = 0; c < 8; ++c) {
  280. *d++ = (bits & 0x80) ?
  281. lcd_color_fg : lcd_color_bg;
  282. bits <<= 1;
  283. }
  284. #elif LCD_BPP == LCD_COLOR16
  285. for (c = 0; c < 8; ++c) {
  286. *d++ = (bits & 0x80) ?
  287. lcd_color_fg : lcd_color_bg;
  288. bits <<= 1;
  289. }
  290. #elif LCD_BPP == LCD_COLOR32
  291. for (c = 0; c < 8; ++c) {
  292. *d++ = (bits & 0x80) ?
  293. lcd_color_fg : lcd_color_bg;
  294. bits <<= 1;
  295. }
  296. #endif
  297. }
  298. #if LCD_BPP == LCD_MONOCHROME
  299. *d = rest | (*d & ((1 << (8 - off)) - 1));
  300. #endif
  301. }
  302. }
  303. static inline void lcd_putc_xy(ushort x, ushort y, uchar c)
  304. {
  305. lcd_drawchars(x, y, &c, 1);
  306. }
  307. /************************************************************************/
  308. /** Small utility to check that you got the colours right */
  309. /************************************************************************/
  310. #ifdef LCD_TEST_PATTERN
  311. #define N_BLK_VERT 2
  312. #define N_BLK_HOR 3
  313. static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
  314. CONSOLE_COLOR_RED, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_YELLOW,
  315. CONSOLE_COLOR_BLUE, CONSOLE_COLOR_MAGENTA, CONSOLE_COLOR_CYAN,
  316. };
  317. static void test_pattern(void)
  318. {
  319. ushort v_max = panel_info.vl_row;
  320. ushort h_max = panel_info.vl_col;
  321. ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
  322. ushort h_step = (h_max + N_BLK_HOR - 1) / N_BLK_HOR;
  323. ushort v, h;
  324. uchar *pix = (uchar *)lcd_base;
  325. printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
  326. h_max, v_max, h_step, v_step);
  327. /* WARNING: Code silently assumes 8bit/pixel */
  328. for (v = 0; v < v_max; ++v) {
  329. uchar iy = v / v_step;
  330. for (h = 0; h < h_max; ++h) {
  331. uchar ix = N_BLK_HOR * iy + h / h_step;
  332. *pix++ = test_colors[ix];
  333. }
  334. }
  335. }
  336. #endif /* LCD_TEST_PATTERN */
  337. /************************************************************************/
  338. /* ** GENERIC Initialization Routines */
  339. /************************************************************************/
  340. /*
  341. * With most lcd drivers the line length is set up
  342. * by calculating it from panel_info parameters. Some
  343. * drivers need to calculate the line length differently,
  344. * so make the function weak to allow overriding it.
  345. */
  346. __weak int lcd_get_size(int *line_length)
  347. {
  348. *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
  349. return *line_length * panel_info.vl_row;
  350. }
  351. int drv_lcd_init(void)
  352. {
  353. struct stdio_dev lcddev;
  354. int rc;
  355. lcd_base = map_sysmem(gd->fb_base, 0);
  356. lcd_init(lcd_base); /* LCD initialization */
  357. /* Device initialization */
  358. memset(&lcddev, 0, sizeof(lcddev));
  359. strcpy(lcddev.name, "lcd");
  360. lcddev.ext = 0; /* No extensions */
  361. lcddev.flags = DEV_FLAGS_OUTPUT; /* Output only */
  362. lcddev.putc = lcd_stub_putc; /* 'putc' function */
  363. lcddev.puts = lcd_stub_puts; /* 'puts' function */
  364. rc = stdio_register(&lcddev);
  365. return (rc == 0) ? 1 : rc;
  366. }
  367. /*----------------------------------------------------------------------*/
  368. void lcd_clear(void)
  369. {
  370. #if LCD_BPP == LCD_MONOCHROME
  371. /* Setting the palette */
  372. lcd_initcolregs();
  373. #elif LCD_BPP == LCD_COLOR8
  374. /* Setting the palette */
  375. lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
  376. lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
  377. lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
  378. lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
  379. lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
  380. lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
  381. lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
  382. lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
  383. lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
  384. #endif
  385. #ifndef CONFIG_SYS_WHITE_ON_BLACK
  386. lcd_setfgcolor(CONSOLE_COLOR_BLACK);
  387. lcd_setbgcolor(CONSOLE_COLOR_WHITE);
  388. #else
  389. lcd_setfgcolor(CONSOLE_COLOR_WHITE);
  390. lcd_setbgcolor(CONSOLE_COLOR_BLACK);
  391. #endif /* CONFIG_SYS_WHITE_ON_BLACK */
  392. #ifdef LCD_TEST_PATTERN
  393. test_pattern();
  394. #else
  395. /* set framebuffer to background color */
  396. #if (LCD_BPP != LCD_COLOR32)
  397. memset((char *)lcd_base,
  398. COLOR_MASK(lcd_color_bg),
  399. lcd_line_length * panel_info.vl_row);
  400. #else
  401. u32 *ppix = lcd_base;
  402. u32 i;
  403. for (i = 0;
  404. i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
  405. i++) {
  406. *ppix++ = COLOR_MASK(lcd_color_bg);
  407. }
  408. #endif
  409. #endif
  410. /* Paint the logo and retrieve LCD base address */
  411. debug("[LCD] Drawing the logo...\n");
  412. lcd_console_address = lcd_logo();
  413. console_col = 0;
  414. console_row = 0;
  415. lcd_sync();
  416. }
  417. static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
  418. char *const argv[])
  419. {
  420. lcd_clear();
  421. return 0;
  422. }
  423. U_BOOT_CMD(
  424. cls, 1, 1, do_lcd_clear,
  425. "clear screen",
  426. ""
  427. );
  428. /*----------------------------------------------------------------------*/
  429. static int lcd_init(void *lcdbase)
  430. {
  431. /* Initialize the lcd controller */
  432. debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
  433. lcd_ctrl_init(lcdbase);
  434. /*
  435. * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi_b) ignores
  436. * the 'lcdbase' argument and uses custom lcd base address
  437. * by setting up gd->fb_base. Check for this condition and fixup
  438. * 'lcd_base' address.
  439. */
  440. if (map_to_sysmem(lcdbase) != gd->fb_base)
  441. lcd_base = map_sysmem(gd->fb_base, 0);
  442. debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
  443. lcd_get_size(&lcd_line_length);
  444. lcd_is_enabled = 1;
  445. lcd_clear();
  446. lcd_enable();
  447. /* Initialize the console */
  448. console_col = 0;
  449. #ifdef CONFIG_LCD_INFO_BELOW_LOGO
  450. console_row = 7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT;
  451. #else
  452. console_row = 1; /* leave 1 blank line below logo */
  453. #endif
  454. return 0;
  455. }
  456. /************************************************************************/
  457. /* ** ROM capable initialization part - needed to reserve FB memory */
  458. /************************************************************************/
  459. /*
  460. * This is called early in the system initialization to grab memory
  461. * for the LCD controller.
  462. * Returns new address for monitor, after reserving LCD buffer memory
  463. *
  464. * Note that this is running from ROM, so no write access to global data.
  465. */
  466. ulong lcd_setmem(ulong addr)
  467. {
  468. ulong size;
  469. int line_length;
  470. debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
  471. panel_info.vl_row, NBITS(panel_info.vl_bpix));
  472. size = lcd_get_size(&line_length);
  473. /* Round up to nearest full page, or MMU section if defined */
  474. size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
  475. addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
  476. /* Allocate pages for the frame buffer. */
  477. addr -= size;
  478. debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
  479. size >> 10, addr);
  480. return addr;
  481. }
  482. /*----------------------------------------------------------------------*/
  483. static void lcd_setfgcolor(int color)
  484. {
  485. lcd_color_fg = color;
  486. }
  487. /*----------------------------------------------------------------------*/
  488. static void lcd_setbgcolor(int color)
  489. {
  490. lcd_color_bg = color;
  491. }
  492. /************************************************************************/
  493. /* ** Chipset depending Bitmap / Logo stuff... */
  494. /************************************************************************/
  495. static inline ushort *configuration_get_cmap(void)
  496. {
  497. #if defined CONFIG_CPU_PXA
  498. struct pxafb_info *fbi = &panel_info.pxa;
  499. return (ushort *)fbi->palette;
  500. #elif defined(CONFIG_MPC823)
  501. immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  502. cpm8xx_t *cp = &(immr->im_cpm);
  503. return (ushort *)&(cp->lcd_cmap[255 * sizeof(ushort)]);
  504. #elif defined(CONFIG_ATMEL_LCD)
  505. return (ushort *)(panel_info.mmio + ATMEL_LCDC_LUT(0));
  506. #elif !defined(CONFIG_ATMEL_HLCD) && !defined(CONFIG_EXYNOS_FB)
  507. return panel_info.cmap;
  508. #elif defined(CONFIG_LCD_LOGO)
  509. return bmp_logo_palette;
  510. #else
  511. return NULL;
  512. #endif
  513. }
  514. #ifdef CONFIG_LCD_LOGO
  515. void bitmap_plot(int x, int y)
  516. {
  517. #ifdef CONFIG_ATMEL_LCD
  518. uint *cmap = (uint *)bmp_logo_palette;
  519. #else
  520. ushort *cmap = (ushort *)bmp_logo_palette;
  521. #endif
  522. ushort i, j;
  523. uchar *bmap;
  524. uchar *fb;
  525. ushort *fb16;
  526. #if defined(CONFIG_MPC823)
  527. immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  528. cpm8xx_t *cp = &(immr->im_cpm);
  529. #endif
  530. unsigned bpix = NBITS(panel_info.vl_bpix);
  531. debug("Logo: width %d height %d colors %d cmap %d\n",
  532. BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS,
  533. ARRAY_SIZE(bmp_logo_palette));
  534. bmap = &bmp_logo_bitmap[0];
  535. fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
  536. if (bpix < 12) {
  537. /* Leave room for default color map
  538. * default case: generic system with no cmap (most likely 16bpp)
  539. * cmap was set to the source palette, so no change is done.
  540. * This avoids even more ifdefs in the next stanza
  541. */
  542. #if defined(CONFIG_MPC823)
  543. cmap = (ushort *) &(cp->lcd_cmap[BMP_LOGO_OFFSET * sizeof(ushort)]);
  544. #elif defined(CONFIG_ATMEL_LCD)
  545. cmap = (uint *)configuration_get_cmap();
  546. #else
  547. cmap = configuration_get_cmap();
  548. #endif
  549. WATCHDOG_RESET();
  550. /* Set color map */
  551. for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i) {
  552. ushort colreg = bmp_logo_palette[i];
  553. #ifdef CONFIG_ATMEL_LCD
  554. uint lut_entry;
  555. #ifdef CONFIG_ATMEL_LCD_BGR555
  556. lut_entry = ((colreg & 0x000F) << 11) |
  557. ((colreg & 0x00F0) << 2) |
  558. ((colreg & 0x0F00) >> 7);
  559. #else /* CONFIG_ATMEL_LCD_RGB565 */
  560. lut_entry = ((colreg & 0x000F) << 1) |
  561. ((colreg & 0x00F0) << 3) |
  562. ((colreg & 0x0F00) << 4);
  563. #endif
  564. *(cmap + BMP_LOGO_OFFSET) = lut_entry;
  565. cmap++;
  566. #else /* !CONFIG_ATMEL_LCD */
  567. #ifdef CONFIG_SYS_INVERT_COLORS
  568. *cmap++ = 0xffff - colreg;
  569. #else
  570. *cmap++ = colreg;
  571. #endif
  572. #endif /* CONFIG_ATMEL_LCD */
  573. }
  574. WATCHDOG_RESET();
  575. for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
  576. memcpy(fb, bmap, BMP_LOGO_WIDTH);
  577. bmap += BMP_LOGO_WIDTH;
  578. fb += panel_info.vl_col;
  579. }
  580. }
  581. else { /* true color mode */
  582. u16 col16;
  583. fb16 = (ushort *)fb;
  584. for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
  585. for (j = 0; j < BMP_LOGO_WIDTH; j++) {
  586. col16 = bmp_logo_palette[(bmap[j]-16)];
  587. fb16[j] =
  588. ((col16 & 0x000F) << 1) |
  589. ((col16 & 0x00F0) << 3) |
  590. ((col16 & 0x0F00) << 4);
  591. }
  592. bmap += BMP_LOGO_WIDTH;
  593. fb16 += panel_info.vl_col;
  594. }
  595. }
  596. WATCHDOG_RESET();
  597. lcd_sync();
  598. }
  599. #else
  600. static inline void bitmap_plot(int x, int y) {}
  601. #endif /* CONFIG_LCD_LOGO */
  602. /*----------------------------------------------------------------------*/
  603. #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
  604. /*
  605. * Display the BMP file located at address bmp_image.
  606. * Only uncompressed.
  607. */
  608. #ifdef CONFIG_SPLASH_SCREEN_ALIGN
  609. #define BMP_ALIGN_CENTER 0x7FFF
  610. static void splash_align_axis(int *axis, unsigned long panel_size,
  611. unsigned long picture_size)
  612. {
  613. unsigned long panel_picture_delta = panel_size - picture_size;
  614. unsigned long axis_alignment;
  615. if (*axis == BMP_ALIGN_CENTER)
  616. axis_alignment = panel_picture_delta / 2;
  617. else if (*axis < 0)
  618. axis_alignment = panel_picture_delta + *axis + 1;
  619. else
  620. return;
  621. *axis = max(0, axis_alignment);
  622. }
  623. #endif
  624. #ifdef CONFIG_LCD_BMP_RLE8
  625. #define BMP_RLE8_ESCAPE 0
  626. #define BMP_RLE8_EOL 0
  627. #define BMP_RLE8_EOBMP 1
  628. #define BMP_RLE8_DELTA 2
  629. static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
  630. int cnt)
  631. {
  632. while (cnt > 0) {
  633. *(*fbp)++ = cmap[*bmap++];
  634. cnt--;
  635. }
  636. }
  637. static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
  638. {
  639. ushort *fb = *fbp;
  640. int cnt_8copy = cnt >> 3;
  641. cnt -= cnt_8copy << 3;
  642. while (cnt_8copy > 0) {
  643. *fb++ = c;
  644. *fb++ = c;
  645. *fb++ = c;
  646. *fb++ = c;
  647. *fb++ = c;
  648. *fb++ = c;
  649. *fb++ = c;
  650. *fb++ = c;
  651. cnt_8copy--;
  652. }
  653. while (cnt > 0) {
  654. *fb++ = c;
  655. cnt--;
  656. }
  657. *fbp = fb;
  658. }
  659. /*
  660. * Do not call this function directly, must be called from lcd_display_bitmap.
  661. */
  662. static void lcd_display_rle8_bitmap(bmp_image_t *bmp, ushort *cmap, uchar *fb,
  663. int x_off, int y_off)
  664. {
  665. uchar *bmap;
  666. ulong width, height;
  667. ulong cnt, runlen;
  668. int x, y;
  669. int decode = 1;
  670. width = get_unaligned_le32(&bmp->header.width);
  671. height = get_unaligned_le32(&bmp->header.height);
  672. bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
  673. x = 0;
  674. y = height - 1;
  675. while (decode) {
  676. if (bmap[0] == BMP_RLE8_ESCAPE) {
  677. switch (bmap[1]) {
  678. case BMP_RLE8_EOL:
  679. /* end of line */
  680. bmap += 2;
  681. x = 0;
  682. y--;
  683. /* 16bpix, 2-byte per pixel, width should *2 */
  684. fb -= (width * 2 + lcd_line_length);
  685. break;
  686. case BMP_RLE8_EOBMP:
  687. /* end of bitmap */
  688. decode = 0;
  689. break;
  690. case BMP_RLE8_DELTA:
  691. /* delta run */
  692. x += bmap[2];
  693. y -= bmap[3];
  694. /* 16bpix, 2-byte per pixel, x should *2 */
  695. fb = (uchar *) (lcd_base + (y + y_off - 1)
  696. * lcd_line_length + (x + x_off) * 2);
  697. bmap += 4;
  698. break;
  699. default:
  700. /* unencoded run */
  701. runlen = bmap[1];
  702. bmap += 2;
  703. if (y < height) {
  704. if (x < width) {
  705. if (x + runlen > width)
  706. cnt = width - x;
  707. else
  708. cnt = runlen;
  709. draw_unencoded_bitmap(
  710. (ushort **)&fb,
  711. bmap, cmap, cnt);
  712. }
  713. x += runlen;
  714. }
  715. bmap += runlen;
  716. if (runlen & 1)
  717. bmap++;
  718. }
  719. } else {
  720. /* encoded run */
  721. if (y < height) {
  722. runlen = bmap[0];
  723. if (x < width) {
  724. /* aggregate the same code */
  725. while (bmap[0] == 0xff &&
  726. bmap[2] != BMP_RLE8_ESCAPE &&
  727. bmap[1] == bmap[3]) {
  728. runlen += bmap[2];
  729. bmap += 2;
  730. }
  731. if (x + runlen > width)
  732. cnt = width - x;
  733. else
  734. cnt = runlen;
  735. draw_encoded_bitmap((ushort **)&fb,
  736. cmap[bmap[1]], cnt);
  737. }
  738. x += runlen;
  739. }
  740. bmap += 2;
  741. }
  742. }
  743. }
  744. #endif
  745. #if defined(CONFIG_MPC823)
  746. #define FB_PUT_BYTE(fb, from) *(fb)++ = (255 - *(from)++)
  747. #else
  748. #define FB_PUT_BYTE(fb, from) *(fb)++ = *(from)++
  749. #endif
  750. #if defined(CONFIG_BMP_16BPP)
  751. #if defined(CONFIG_ATMEL_LCD_BGR555)
  752. static inline void fb_put_word(uchar **fb, uchar **from)
  753. {
  754. *(*fb)++ = (((*from)[0] & 0x1f) << 2) | ((*from)[1] & 0x03);
  755. *(*fb)++ = ((*from)[0] & 0xe0) | (((*from)[1] & 0x7c) >> 2);
  756. *from += 2;
  757. }
  758. #else
  759. static inline void fb_put_word(uchar **fb, uchar **from)
  760. {
  761. *(*fb)++ = *(*from)++;
  762. *(*fb)++ = *(*from)++;
  763. }
  764. #endif
  765. #endif /* CONFIG_BMP_16BPP */
  766. int lcd_display_bitmap(ulong bmp_image, int x, int y)
  767. {
  768. ushort *cmap = NULL;
  769. ushort *cmap_base = NULL;
  770. ushort i, j;
  771. uchar *fb;
  772. bmp_image_t *bmp = (bmp_image_t *)map_sysmem(bmp_image, 0);
  773. uchar *bmap;
  774. ushort padded_width;
  775. unsigned long width, height, byte_width;
  776. unsigned long pwidth = panel_info.vl_col;
  777. unsigned colors, bpix, bmp_bpix;
  778. if (!bmp || !(bmp->header.signature[0] == 'B' &&
  779. bmp->header.signature[1] == 'M')) {
  780. printf("Error: no valid bmp image at %lx\n", bmp_image);
  781. return 1;
  782. }
  783. width = get_unaligned_le32(&bmp->header.width);
  784. height = get_unaligned_le32(&bmp->header.height);
  785. bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
  786. colors = 1 << bmp_bpix;
  787. bpix = NBITS(panel_info.vl_bpix);
  788. if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
  789. printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
  790. bpix, bmp_bpix);
  791. return 1;
  792. }
  793. /*
  794. * We support displaying 8bpp BMPs on 16bpp LCDs
  795. * and displaying 24bpp BMPs on 32bpp LCDs
  796. * */
  797. if (bpix != bmp_bpix &&
  798. !(bmp_bpix == 8 && bpix == 16) &&
  799. !(bmp_bpix == 24 && bpix == 32)) {
  800. printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
  801. bpix, get_unaligned_le16(&bmp->header.bit_count));
  802. return 1;
  803. }
  804. debug("Display-bmp: %d x %d with %d colors\n",
  805. (int)width, (int)height, (int)colors);
  806. if (bmp_bpix == 8) {
  807. cmap = configuration_get_cmap();
  808. cmap_base = cmap;
  809. /* Set color map */
  810. for (i = 0; i < colors; ++i) {
  811. bmp_color_table_entry_t cte = bmp->color_table[i];
  812. #if !defined(CONFIG_ATMEL_LCD)
  813. ushort colreg =
  814. ( ((cte.red) << 8) & 0xf800) |
  815. ( ((cte.green) << 3) & 0x07e0) |
  816. ( ((cte.blue) >> 3) & 0x001f) ;
  817. #ifdef CONFIG_SYS_INVERT_COLORS
  818. *cmap = 0xffff - colreg;
  819. #else
  820. *cmap = colreg;
  821. #endif
  822. #if defined(CONFIG_MPC823)
  823. cmap--;
  824. #else
  825. cmap++;
  826. #endif
  827. #else /* CONFIG_ATMEL_LCD */
  828. lcd_setcolreg(i, cte.red, cte.green, cte.blue);
  829. #endif
  830. }
  831. }
  832. padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
  833. #ifdef CONFIG_SPLASH_SCREEN_ALIGN
  834. splash_align_axis(&x, pwidth, width);
  835. splash_align_axis(&y, panel_info.vl_row, height);
  836. #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
  837. if ((x + width) > pwidth)
  838. width = pwidth - x;
  839. if ((y + height) > panel_info.vl_row)
  840. height = panel_info.vl_row - y;
  841. bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
  842. fb = (uchar *)(lcd_base +
  843. (y + height - 1) * lcd_line_length + x * bpix / 8);
  844. switch (bmp_bpix) {
  845. case 1: /* pass through */
  846. case 8: {
  847. #ifdef CONFIG_LCD_BMP_RLE8
  848. u32 compression = get_unaligned_le32(&bmp->header.compression);
  849. if (compression == BMP_BI_RLE8) {
  850. if (bpix != 16) {
  851. /* TODO implement render code for bpix != 16 */
  852. printf("Error: only support 16 bpix");
  853. return 1;
  854. }
  855. lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
  856. break;
  857. }
  858. #endif
  859. if (bpix != 16)
  860. byte_width = width;
  861. else
  862. byte_width = width * 2;
  863. for (i = 0; i < height; ++i) {
  864. WATCHDOG_RESET();
  865. for (j = 0; j < width; j++) {
  866. if (bpix != 16) {
  867. FB_PUT_BYTE(fb, bmap);
  868. } else {
  869. *(uint16_t *)fb = cmap_base[*(bmap++)];
  870. fb += sizeof(uint16_t) / sizeof(*fb);
  871. }
  872. }
  873. bmap += (padded_width - width);
  874. fb -= byte_width + lcd_line_length;
  875. }
  876. break;
  877. }
  878. #if defined(CONFIG_BMP_16BPP)
  879. case 16:
  880. for (i = 0; i < height; ++i) {
  881. WATCHDOG_RESET();
  882. for (j = 0; j < width; j++)
  883. fb_put_word(&fb, &bmap);
  884. bmap += (padded_width - width) * 2;
  885. fb -= width * 2 + lcd_line_length;
  886. }
  887. break;
  888. #endif /* CONFIG_BMP_16BPP */
  889. #if defined(CONFIG_BMP_24BMP)
  890. case 24:
  891. for (i = 0; i < height; ++i) {
  892. for (j = 0; j < width; j++) {
  893. *(fb++) = *(bmap++);
  894. *(fb++) = *(bmap++);
  895. *(fb++) = *(bmap++);
  896. *(fb++) = 0;
  897. }
  898. fb -= lcd_line_length + width * (bpix / 8);
  899. }
  900. break;
  901. #endif /* CONFIG_BMP_24BMP */
  902. #if defined(CONFIG_BMP_32BPP)
  903. case 32:
  904. for (i = 0; i < height; ++i) {
  905. for (j = 0; j < width; j++) {
  906. *(fb++) = *(bmap++);
  907. *(fb++) = *(bmap++);
  908. *(fb++) = *(bmap++);
  909. *(fb++) = *(bmap++);
  910. }
  911. fb -= lcd_line_length + width * (bpix / 8);
  912. }
  913. break;
  914. #endif /* CONFIG_BMP_32BPP */
  915. default:
  916. break;
  917. };
  918. lcd_sync();
  919. return 0;
  920. }
  921. #endif
  922. static void *lcd_logo(void)
  923. {
  924. #ifdef CONFIG_SPLASH_SCREEN
  925. char *s;
  926. ulong addr;
  927. static int do_splash = 1;
  928. if (do_splash && (s = getenv("splashimage")) != NULL) {
  929. int x = 0, y = 0;
  930. do_splash = 0;
  931. if (splash_screen_prepare())
  932. return (void *)lcd_base;
  933. addr = simple_strtoul (s, NULL, 16);
  934. splash_get_pos(&x, &y);
  935. if (bmp_display(addr, x, y) == 0)
  936. return (void *)lcd_base;
  937. }
  938. #endif /* CONFIG_SPLASH_SCREEN */
  939. bitmap_plot(0, 0);
  940. #ifdef CONFIG_LCD_INFO
  941. console_col = LCD_INFO_X / VIDEO_FONT_WIDTH;
  942. console_row = LCD_INFO_Y / VIDEO_FONT_HEIGHT;
  943. lcd_show_board_info();
  944. #endif /* CONFIG_LCD_INFO */
  945. #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
  946. return (void *)((ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length);
  947. #else
  948. return (void *)lcd_base;
  949. #endif /* CONFIG_LCD_LOGO && !defined(CONFIG_LCD_INFO_BELOW_LOGO) */
  950. }
  951. #ifdef CONFIG_SPLASHIMAGE_GUARD
  952. static int on_splashimage(const char *name, const char *value, enum env_op op,
  953. int flags)
  954. {
  955. ulong addr;
  956. int aligned;
  957. if (op == env_op_delete)
  958. return 0;
  959. addr = simple_strtoul(value, NULL, 16);
  960. /* See README.displaying-bmps */
  961. aligned = (addr % 4 == 2);
  962. if (!aligned) {
  963. printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
  964. return -1;
  965. }
  966. return 0;
  967. }
  968. U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
  969. #endif
  970. void lcd_position_cursor(unsigned col, unsigned row)
  971. {
  972. console_col = min(col, CONSOLE_COLS - 1);
  973. console_row = min(row, CONSOLE_ROWS - 1);
  974. }
  975. int lcd_get_pixel_width(void)
  976. {
  977. return panel_info.vl_col;
  978. }
  979. int lcd_get_pixel_height(void)
  980. {
  981. return panel_info.vl_row;
  982. }
  983. int lcd_get_screen_rows(void)
  984. {
  985. return CONSOLE_ROWS;
  986. }
  987. int lcd_get_screen_columns(void)
  988. {
  989. return CONSOLE_COLS;
  990. }
  991. #if defined(CONFIG_LCD_DT_SIMPLEFB)
  992. static int lcd_dt_simplefb_configure_node(void *blob, int off)
  993. {
  994. u32 stride;
  995. fdt32_t cells[2];
  996. int ret;
  997. static const char format[] =
  998. #if LCD_BPP == LCD_COLOR16
  999. "r5g6b5";
  1000. #else
  1001. "";
  1002. #endif
  1003. if (!format[0])
  1004. return -1;
  1005. stride = panel_info.vl_col * 2;
  1006. cells[0] = cpu_to_fdt32(gd->fb_base);
  1007. cells[1] = cpu_to_fdt32(stride * panel_info.vl_row);
  1008. ret = fdt_setprop(blob, off, "reg", cells, sizeof(cells[0]) * 2);
  1009. if (ret < 0)
  1010. return -1;
  1011. cells[0] = cpu_to_fdt32(panel_info.vl_col);
  1012. ret = fdt_setprop(blob, off, "width", cells, sizeof(cells[0]));
  1013. if (ret < 0)
  1014. return -1;
  1015. cells[0] = cpu_to_fdt32(panel_info.vl_row);
  1016. ret = fdt_setprop(blob, off, "height", cells, sizeof(cells[0]));
  1017. if (ret < 0)
  1018. return -1;
  1019. cells[0] = cpu_to_fdt32(stride);
  1020. ret = fdt_setprop(blob, off, "stride", cells, sizeof(cells[0]));
  1021. if (ret < 0)
  1022. return -1;
  1023. ret = fdt_setprop(blob, off, "format", format, strlen(format) + 1);
  1024. if (ret < 0)
  1025. return -1;
  1026. ret = fdt_delprop(blob, off, "status");
  1027. if (ret < 0)
  1028. return -1;
  1029. return 0;
  1030. }
  1031. int lcd_dt_simplefb_add_node(void *blob)
  1032. {
  1033. static const char compat[] = "simple-framebuffer";
  1034. static const char disabled[] = "disabled";
  1035. int off, ret;
  1036. off = fdt_add_subnode(blob, 0, "framebuffer");
  1037. if (off < 0)
  1038. return -1;
  1039. ret = fdt_setprop(blob, off, "status", disabled, sizeof(disabled));
  1040. if (ret < 0)
  1041. return -1;
  1042. ret = fdt_setprop(blob, off, "compatible", compat, sizeof(compat));
  1043. if (ret < 0)
  1044. return -1;
  1045. return lcd_dt_simplefb_configure_node(blob, off);
  1046. }
  1047. int lcd_dt_simplefb_enable_existing_node(void *blob)
  1048. {
  1049. int off;
  1050. off = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
  1051. if (off < 0)
  1052. return -1;
  1053. return lcd_dt_simplefb_configure_node(blob, off);
  1054. }
  1055. #endif