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