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