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