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