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