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