]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/lcd.c
Merge branch 'tx6-bugfix'
[karo-tx-uboot.git] / common / lcd.c
1 /*
2  * Common LCD routines
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 /* #define DEBUG */
11 #include <config.h>
12 #include <common.h>
13 #include <command.h>
14 #include <env_callback.h>
15 #include <linux/types.h>
16 #include <stdio_dev.h>
17 #include <lcd.h>
18 #include <mapmem.h>
19 #include <watchdog.h>
20 #include <asm/unaligned.h>
21 #include <splash.h>
22 #include <asm/io.h>
23 #include <asm/unaligned.h>
24 #include <video_font.h>
25
26 #ifdef CONFIG_LCD_LOGO
27 #include <bmp_logo.h>
28 #include <bmp_logo_data.h>
29 #if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP < LCD_COLOR16)
30 #error Default Color Map overlaps with Logo Color Map
31 #endif
32 #endif
33
34 #ifdef CONFIG_SANDBOX
35 #include <asm/sdl.h>
36 #endif
37
38 #ifndef CONFIG_LCD_ALIGNMENT
39 #define CONFIG_LCD_ALIGNMENT PAGE_SIZE
40 #endif
41
42 #if (LCD_BPP != LCD_COLOR8) && (LCD_BPP != LCD_COLOR16) && \
43         (LCD_BPP != LCD_COLOR32)
44 #error Unsupported LCD BPP.
45 #endif
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 static int lcd_init(void *lcdbase);
50 static void lcd_logo(void);
51 static void lcd_setfgcolor(int color);
52 static void lcd_setbgcolor(int color);
53
54 static int lcd_color_fg;
55 static int lcd_color_bg;
56 int lcd_line_length;
57 char lcd_is_enabled = 0;
58 static void *lcd_base;                  /* Start of framebuffer memory  */
59 static char lcd_flush_dcache;   /* 1 to flush dcache after each lcd update */
60
61 /* Flush LCD activity to the caches */
62 void lcd_sync(void)
63 {
64         /*
65          * flush_dcache_range() is declared in common.h but it seems that some
66          * architectures do not actually implement it. Is there a way to find
67          * out whether it exists? For now, ARM is safe.
68          */
69 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
70         int line_length;
71
72         if (lcd_flush_dcache)
73                 flush_dcache_range((u32)lcd_base,
74                         (u32)(lcd_base + lcd_get_size(&line_length)));
75 #elif defined(CONFIG_SANDBOX) && defined(CONFIG_VIDEO_SANDBOX_SDL)
76         static ulong last_sync;
77
78         if (get_timer(last_sync) > 10) {
79                 sandbox_sdl_sync(lcd_base);
80                 last_sync = get_timer(0);
81         }
82 #endif
83 }
84
85 void lcd_set_flush_dcache(int flush)
86 {
87         lcd_flush_dcache = (flush != 0);
88 }
89
90 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
91 {
92         lcd_putc(c);
93 }
94
95 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
96 {
97         lcd_puts(s);
98 }
99
100 /* Small utility to check that you got the colours right */
101 #ifdef LCD_TEST_PATTERN
102
103 #define N_BLK_VERT      2
104 #define N_BLK_HOR       3
105
106 static int test_colors[N_BLK_HOR * N_BLK_VERT] = {
107         CONSOLE_COLOR_RED,      CONSOLE_COLOR_GREEN,    CONSOLE_COLOR_YELLOW,
108         CONSOLE_COLOR_BLUE,     CONSOLE_COLOR_MAGENTA,  CONSOLE_COLOR_CYAN,
109 };
110
111 #if LCD_BPP == LCD_COLOR8
112 typedef uchar pix_t;
113 #elif LCD_BPP == LCD_COLOR16
114 typedef ushort pix_t;
115 #elif LCD_BPP == LCD_COLOR32
116 typedef ulong pix_t;
117 #else
118 #error Unsupported pixelformat
119 #endif
120
121 static void test_pattern(void)
122 {
123         ushort v_max  = panel_info.vl_row;
124         ushort h_max  = panel_info.vl_col;
125         ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
126         ushort h_step = (h_max + N_BLK_HOR  - 1) / N_BLK_HOR;
127         ushort v, h;
128         pix_t *pix = lcd_base;
129
130         printf("[LCD] Test Pattern: %d x %d [%d x %d]\n",
131                 h_max, v_max, h_step, v_step);
132
133         for (v = 0; v < v_max; ++v) {
134                 uchar iy = v / v_step;
135                 for (h = 0; h < h_max; ++h) {
136                         uchar ix = N_BLK_HOR * iy + h / h_step;
137                         *pix++ = test_colors[ix];
138                 }
139         }
140 }
141 #endif /* LCD_TEST_PATTERN */
142
143 /*
144  * With most lcd drivers the line length is set up
145  * by calculating it from panel_info parameters. Some
146  * drivers need to calculate the line length differently,
147  * so make the function weak to allow overriding it.
148  */
149 __weak int lcd_get_size(int *line_length)
150 {
151         *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
152         return *line_length * panel_info.vl_row;
153 }
154
155 int drv_lcd_init(void)
156 {
157         struct stdio_dev lcddev;
158         int rc;
159
160         lcd_base = map_sysmem(gd->fb_base, 0);
161
162         lcd_init(lcd_base);
163
164         /* Device initialization */
165         memset(&lcddev, 0, sizeof(lcddev));
166
167         strcpy(lcddev.name, "lcd");
168         lcddev.ext   = 0;                       /* No extensions */
169         lcddev.flags = DEV_FLAGS_OUTPUT;        /* Output only */
170         lcddev.putc  = lcd_stub_putc;           /* 'putc' function */
171         lcddev.puts  = lcd_stub_puts;           /* 'puts' function */
172
173         rc = stdio_register(&lcddev);
174
175         return (rc == 0) ? 1 : rc;
176 }
177
178 void lcd_clear(void)
179 {
180         int bg_color;
181         char *s;
182         ulong addr;
183         static int do_splash = 1;
184 #if LCD_BPP == LCD_COLOR8
185         /* Setting the palette */
186         lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
187         lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
188         lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
189         lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
190         lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
191         lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
192         lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
193         lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
194         lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
195 #endif
196
197 #ifndef CONFIG_SYS_WHITE_ON_BLACK
198         lcd_setfgcolor(CONSOLE_COLOR_BLACK);
199         lcd_setbgcolor(CONSOLE_COLOR_WHITE);
200         bg_color = CONSOLE_COLOR_WHITE;
201 #else
202         lcd_setfgcolor(CONSOLE_COLOR_WHITE);
203         lcd_setbgcolor(CONSOLE_COLOR_BLACK);
204         bg_color = CONSOLE_COLOR_BLACK;
205 #endif  /* CONFIG_SYS_WHITE_ON_BLACK */
206
207 #ifdef  LCD_TEST_PATTERN
208         test_pattern();
209 #else
210         /* set framebuffer to background color */
211 #if (LCD_BPP != LCD_COLOR32)
212         memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
213 #else
214         u32 *ppix = lcd_base;
215         u32 i;
216         for (i = 0;
217            i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
218            i++) {
219                 *ppix++ = bg_color;
220         }
221 #endif
222 #endif
223         /* setup text-console */
224         debug("[LCD] setting up console...\n");
225         lcd_init_console(lcd_base,
226                          panel_info.vl_col,
227                          panel_info.vl_row,
228                          panel_info.vl_rot);
229         /* Paint the logo and retrieve LCD base address */
230         debug("[LCD] Drawing the logo @ %p...\n", lcd_base);
231         if (do_splash) {
232                 s = getenv("splashimage");
233                 if (s) {
234                         do_splash = 0;
235                         addr = simple_strtoul(s, NULL, 16);
236                         if (lcd_splash(addr) == 0) {
237                                 lcd_sync();
238                                 return;
239                         }
240                 }
241         }
242
243         lcd_logo();
244 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
245         addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
246         lcd_init_console((void *)addr, panel_info.vl_col,
247                          panel_info.vl_row, panel_info.vl_rot);
248 #endif
249         lcd_sync();
250 }
251
252 static int do_lcd_clear(cmd_tbl_t *cmdtp, int flag, int argc,
253                         char *const argv[])
254 {
255         lcd_clear();
256         return 0;
257 }
258 U_BOOT_CMD(cls, 1, 1, do_lcd_clear, "clear screen", "");
259
260 static int lcd_init(void *lcdbase)
261 {
262         debug("[LCD] Initializing %ux%ux%u LCD framebuffer at %p\n",
263                 panel_info.vl_col, panel_info.vl_row, NBITS(panel_info.vl_bpix),
264                 lcdbase);
265
266         lcd_ctrl_init(lcdbase);
267
268         /*
269          * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
270          * the 'lcdbase' argument and uses custom lcd base address
271          * by setting up gd->fb_base. Check for this condition and fixup
272          * 'lcd_base' address.
273          */
274         if (map_to_sysmem(lcdbase) != gd->fb_base)
275                 lcd_base = map_sysmem(gd->fb_base, 0);
276
277         debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
278
279         lcd_get_size(&lcd_line_length);
280         lcd_is_enabled = 1;
281         lcd_clear();
282         lcd_enable();
283
284         /* Initialize the console */
285         lcd_set_col(0);
286 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
287         lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
288 #else
289         lcd_set_row(1); /* leave 1 blank line below logo */
290 #endif
291
292         return 0;
293 }
294
295 /*
296  * This is called early in the system initialization to grab memory
297  * for the LCD controller.
298  * Returns new address for monitor, after reserving LCD buffer memory
299  *
300  * Note that this is running from ROM, so no write access to global data.
301  */
302 ulong lcd_setmem(ulong addr)
303 {
304         ulong size;
305         int line_length;
306
307         debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
308                 panel_info.vl_row, NBITS(panel_info.vl_bpix));
309
310         size = lcd_get_size(&line_length);
311
312         /* Round up to nearest full page, or MMU section if defined */
313         size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
314         addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
315
316         /* Allocate pages for the frame buffer. */
317         addr -= size;
318
319         debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
320               size >> 10, addr);
321
322         return addr;
323 }
324
325 static void lcd_setfgcolor(int color)
326 {
327         lcd_color_fg = color;
328 }
329
330 int lcd_getfgcolor(void)
331 {
332         return lcd_color_fg;
333 }
334
335 static void lcd_setbgcolor(int color)
336 {
337         lcd_color_bg = color;
338 }
339
340 int lcd_getbgcolor(void)
341 {
342         return lcd_color_bg;
343 }
344
345 #ifdef CONFIG_LCD_LOGO
346 __weak void lcd_logo_set_cmap(void)
347 {
348         int i;
349         ushort *cmap = configuration_get_cmap();
350
351         for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
352                 *cmap++ = bmp_logo_palette[i];
353 }
354
355 void lcd_logo_plot(int x, int y)
356 {
357         ushort i, j;
358         uchar *bmap = &bmp_logo_bitmap[0];
359         unsigned bpix = NBITS(panel_info.vl_bpix);
360         uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
361         ushort *fb16;
362
363         debug("Logo: width %d  height %d  colors %d\n",
364               BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
365
366         if (bpix < 12) {
367                 WATCHDOG_RESET();
368                 lcd_logo_set_cmap();
369                 WATCHDOG_RESET();
370
371                 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
372                         memcpy(fb, bmap, BMP_LOGO_WIDTH);
373                         bmap += BMP_LOGO_WIDTH;
374                         fb += panel_info.vl_col;
375                 }
376         } else if (NBITS(panel_info.vl_bpix) == 16) {
377                 u16 col16;
378                 fb16 = (ushort *)fb;
379                 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
380                         for (j = 0; j < BMP_LOGO_WIDTH; j++) {
381                                 col16 = bmp_logo_palette[(bmap[j]-16)];
382                                 fb16[j] =
383                                         ((col16 & 0x000F) << 1) |
384                                         ((col16 & 0x00F0) << 3) |
385                                         ((col16 & 0x0F00) << 4);
386                                 }
387                         bmap += BMP_LOGO_WIDTH;
388                         fb16 += panel_info.vl_col;
389                 }
390         } else { /* true color mode */
391                 u16 col16;
392                 u32 *fb32 = lcd_base + y * lcd_line_length + x;
393
394                 for (i = 0; i < BMP_LOGO_HEIGHT; i++) {
395                         for (j = 0; j < BMP_LOGO_WIDTH; j++) {
396                                 col16 = bmp_logo_palette[bmap[j] - 16];
397                                 fb32[j] =
398                                         ((col16 & 0x000F) << 4) |
399                                         ((col16 & 0x00F0) << 8) |
400                                         ((col16 & 0x0F00) << 12);
401                                 }
402                         bmap += BMP_LOGO_WIDTH;
403                         fb32 += panel_info.vl_col;
404                 }
405         }
406
407         WATCHDOG_RESET();
408         lcd_sync();
409 }
410 #else
411 static inline void lcd_logo_plot(int x, int y) {}
412 #endif /* CONFIG_LCD_LOGO */
413
414 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
415 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
416 #define BMP_ALIGN_CENTER        0x7FFF
417
418 static void splash_align_axis(int *axis, unsigned long panel_size,
419                                         unsigned long picture_size)
420 {
421         int panel_picture_delta = panel_size - picture_size;
422         int axis_alignment;
423
424         if (*axis == BMP_ALIGN_CENTER)
425                 axis_alignment = panel_picture_delta / 2;
426         else if (abs(*axis) > (int)panel_size)
427                 axis_alignment = panel_size;
428         else if (*axis < 0)
429                 axis_alignment = panel_picture_delta + *axis + 1;
430         else
431                 return;
432         *axis = axis_alignment;
433 }
434 #endif
435
436 #ifdef CONFIG_LCD_BMP_RLE8
437 #define BMP_RLE8_ESCAPE         0
438 #define BMP_RLE8_EOL            0
439 #define BMP_RLE8_EOBMP          1
440 #define BMP_RLE8_DELTA          2
441
442 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
443                                   int cnt)
444 {
445         while (cnt > 0) {
446                 *(*fbp)++ = cmap[*bmap++];
447                 cnt--;
448         }
449 }
450
451 static void draw_encoded_bitmap(ushort **fbp, ushort c, int cnt)
452 {
453         ushort *fb = *fbp;
454         int cnt_8copy = cnt >> 3;
455
456         cnt -= cnt_8copy << 3;
457         while (cnt_8copy > 0) {
458                 *fb++ = c;
459                 *fb++ = c;
460                 *fb++ = c;
461                 *fb++ = c;
462                 *fb++ = c;
463                 *fb++ = c;
464                 *fb++ = c;
465                 *fb++ = c;
466                 cnt_8copy--;
467         }
468         while (cnt > 0) {
469                 *fb++ = c;
470                 cnt--;
471         }
472         *fbp = fb;
473 }
474
475 /*
476  * Do not call this function directly, must be called from lcd_display_bitmap.
477  */
478 static void lcd_display_rle8_bitmap(struct bmp_image *bmp, ushort *cmap,
479                                     uchar *fb, int x_off, int y_off)
480 {
481         uchar *bmap;
482         ulong width, height;
483         ulong cnt, runlen;
484         int x, y;
485         int decode = 1;
486
487         width = get_unaligned_le32(&bmp->header.width);
488         height = get_unaligned_le32(&bmp->header.height);
489         bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
490
491         x = 0;
492         y = height - 1;
493
494         while (decode) {
495                 if (bmap[0] == BMP_RLE8_ESCAPE) {
496                         switch (bmap[1]) {
497                         case BMP_RLE8_EOL:
498                                 /* end of line */
499                                 bmap += 2;
500                                 x = 0;
501                                 y--;
502                                 /* 16bpix, 2-byte per pixel, width should *2 */
503                                 fb -= (width * 2 + lcd_line_length);
504                                 break;
505                         case BMP_RLE8_EOBMP:
506                                 /* end of bitmap */
507                                 decode = 0;
508                                 break;
509                         case BMP_RLE8_DELTA:
510                                 /* delta run */
511                                 x += bmap[2];
512                                 y -= bmap[3];
513                                 /* 16bpix, 2-byte per pixel, x should *2 */
514                                 fb = (uchar *) (lcd_base + (y + y_off - 1)
515                                         * lcd_line_length + (x + x_off) * 2);
516                                 bmap += 4;
517                                 break;
518                         default:
519                                 /* unencoded run */
520                                 runlen = bmap[1];
521                                 bmap += 2;
522                                 if (y < height) {
523                                         if (x < width) {
524                                                 if (x + runlen > width)
525                                                         cnt = width - x;
526                                                 else
527                                                         cnt = runlen;
528                                                 draw_unencoded_bitmap(
529                                                         (ushort **)&fb,
530                                                         bmap, cmap, cnt);
531                                         }
532                                         x += runlen;
533                                 }
534                                 bmap += runlen;
535                                 if (runlen & 1)
536                                         bmap++;
537                         }
538                 } else {
539                         /* encoded run */
540                         if (y < height) {
541                                 runlen = bmap[0];
542                                 if (x < width) {
543                                         /* aggregate the same code */
544                                         while (bmap[0] == 0xff &&
545                                                bmap[2] != BMP_RLE8_ESCAPE &&
546                                                bmap[1] == bmap[3]) {
547                                                 runlen += bmap[2];
548                                                 bmap += 2;
549                                         }
550                                         if (x + runlen > width)
551                                                 cnt = width - x;
552                                         else
553                                                 cnt = runlen;
554                                         draw_encoded_bitmap((ushort **)&fb,
555                                                 cmap[bmap[1]], cnt);
556                                 }
557                                 x += runlen;
558                         }
559                         bmap += 2;
560                 }
561         }
562 }
563 #endif
564
565 __weak void fb_put_byte(uchar **fb, uchar **from)
566 {
567         *(*fb)++ = *(*from)++;
568 }
569
570 #if defined(CONFIG_BMP_16BPP)
571 __weak void fb_put_word(uchar **fb, uchar **from)
572 {
573         *(*fb)++ = *(*from)++;
574         *(*fb)++ = *(*from)++;
575 }
576 #endif /* CONFIG_BMP_16BPP */
577
578 __weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
579 {
580         int i;
581         struct bmp_color_table_entry cte;
582         ushort *cmap = configuration_get_cmap();
583
584         for (i = 0; i < colors; ++i) {
585                 cte = bmp->color_table[i];
586                 *cmap = (((cte.red)   << 8) & 0xf800) |
587                         (((cte.green) << 3) & 0x07e0) |
588                         (((cte.blue)  >> 3) & 0x001f);
589 #if defined(CONFIG_MPC823)
590                 cmap--;
591 #else
592                 cmap++;
593 #endif
594         }
595 }
596
597 int lcd_display_bitmap(ulong bmp_image, int x, int y)
598 {
599         ushort *cmap_base = NULL;
600         ushort i, j;
601         uchar *fb;
602         struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
603         uchar *bmap;
604         ushort padded_width;
605         unsigned long width, height;
606         unsigned long pwidth = panel_info.vl_col;
607         unsigned long long colors;
608         unsigned bpix, bmp_bpix;
609         int hdr_size;
610         struct bmp_color_table_entry *palette = bmp->color_table;
611
612         if (!bmp || !(bmp->header.signature[0] == 'B' &&
613                 bmp->header.signature[1] == 'M')) {
614                 printf("Error: no valid bmp image at %lx\n", bmp_image);
615
616                 return 1;
617         }
618
619         width = get_unaligned_le32(&bmp->header.width);
620         height = get_unaligned_le32(&bmp->header.height);
621         bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
622         hdr_size = get_unaligned_le16(&bmp->header.size);
623         debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
624
625         colors = 1ULL << bmp_bpix;
626
627         bpix = NBITS(panel_info.vl_bpix);
628
629         if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
630                 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
631                         bpix, bmp_bpix);
632
633                 return 1;
634         }
635
636         /*
637          * We support displaying 8bpp BMPs on 16bpp LCDs
638          * and displaying 24bpp BMPs on 32bpp LCDs
639          * */
640         if (bpix < bmp_bpix) {
641                 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
642                         bpix, get_unaligned_le16(&bmp->header.bit_count));
643                 return 1;
644         }
645
646         debug("Display-bmp: %lu x %lu  with %llu colors, display %llu\n",
647                 width, height, colors, 1ULL << bpix);
648
649         if (bmp_bpix == 8)
650                 lcd_set_cmap(bmp, colors);
651
652         padded_width = ALIGN(width, 4);
653
654 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
655         splash_align_axis(&x, pwidth, width);
656         splash_align_axis(&y, panel_info.vl_row, height);
657 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
658         bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
659
660         if (x < 0) {
661                 width += x;
662                 bmap += -x * bmp_bpix / 8;
663                 x = 0;
664         }
665         if ((x + width) > pwidth) {
666                 width = pwidth - x;
667         }
668         if (y < 0) {
669                 height += y;
670                 y = 0;
671         }
672         if ((y + height) > panel_info.vl_row) {
673                 bmap += (y + height - panel_info.vl_row) * bmp_bpix / 8 * padded_width;
674                 height = panel_info.vl_row - y;
675         }
676
677         fb   = (uchar *)(lcd_base +
678                 (y + height - 1) * lcd_line_length + x * bpix / 8);
679
680         switch (bmp_bpix) {
681         case 1:
682         case 8: {
683                 cmap_base = configuration_get_cmap();
684 #ifdef CONFIG_LCD_BMP_RLE8
685                 u32 compression = get_unaligned_le32(&bmp->header.compression);
686                 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
687                 if (compression == BMP_BI_RLE8) {
688                         if (bpix != 16) {
689                                 /* TODO implement render code for bpix != 16 */
690                                 printf("Error: only support 16 bpix");
691                                 return 1;
692                         }
693                         lcd_display_rle8_bitmap(bmp, cmap_base, fb, x, y);
694                         break;
695                 }
696 #endif
697
698                 for (i = 0; i < height; ++i) {
699                         WATCHDOG_RESET();
700                         for (j = 0; j < width; j++) {
701                                 if (bpix == 8) {
702                                         fb_put_byte(&fb, &bmap);
703                                 } else if (bpix == 16) {
704                                         struct bmp_color_table_entry *entry;
705                                         uint val;
706
707                                         if (cmap_base) {
708                                                 val = cmap_base[*bmap];
709                                         } else {
710                                                 entry = &palette[*bmap];
711                                                 val = entry->blue >> 3 |
712                                                         entry->green >> 2 << 5 |
713                                                         entry->red >> 3 << 11;
714                                         }
715                                         *(uint16_t *)fb = val;
716                                         fb += sizeof(uint16_t) / sizeof(*fb);
717                                 } else if (bpix == 32) {
718                                         uint32_t *fb32 = (uint32_t *)fb;
719                                         struct bmp_color_table_entry *entry;
720                                         uint val;
721
722                                         if (cmap_base) {
723                                                 val = cmap_base[*bmap];
724                                         } else {
725                                                 entry = &palette[*bmap];
726                                                 val = entry->blue |
727                                                         entry->green << 8 |
728                                                         entry->red << 16;
729                                         }
730                                         *fb32 = val;
731                                         fb += sizeof(uint32_t) / sizeof(*fb);
732                                 }
733                                 bmap++;
734                         }
735                         bmap += padded_width - width;
736                         fb   -= width * bpix / 8 + lcd_line_length;
737                 }
738                 break;
739         }
740 #if defined(CONFIG_BMP_16BPP)
741         case 16:
742                 for (i = 0; i < height; ++i) {
743                         WATCHDOG_RESET();
744                         for (j = 0; j < width; j++)
745                                 fb_put_word(&fb, &bmap);
746
747                         bmap += (padded_width - width) * 2;
748                         fb -= width * 2 + lcd_line_length;
749                 }
750                 break;
751 #endif /* CONFIG_BMP_16BPP */
752 #if defined(CONFIG_BMP_24BPP)
753         case 24:
754                 for (i = 0; i < height; ++i) {
755                         for (j = 0; j < width; j++) {
756                                 *(fb++) = *(bmap++);
757                                 *(fb++) = *(bmap++);
758                                 *(fb++) = *(bmap++);
759                                 *(fb++) = 0;
760                         }
761                         bmap += (padded_width - width) * 3;
762                         fb -= lcd_line_length + width * (bpix / 8);
763                 }
764                 break;
765 #endif /* CONFIG_BMP_24BPP */
766 #if defined(CONFIG_BMP_32BPP)
767         case 32:
768                 for (i = 0; i < height; ++i) {
769                         WATCHDOG_RESET();
770                         for (j = 0; j < width; j++) {
771                                 fb[3] = *bmap++; /* T */
772                                 fb[0] = *bmap++; /* B */
773                                 fb[1] = *bmap++; /* G */
774                                 fb[2] = *bmap++; /* R */
775                                 fb += 4;
776                         }
777                         bmap += (padded_width - width) * 4;
778                         fb -= lcd_line_length + width * (bpix / 8);
779                 }
780                 break;
781 #endif /* CONFIG_BMP_32BPP */
782         default:
783                 printf("Logo with %ubpp on %ubpp display not supported\n",
784                         bmp_bpix, bpix);
785         };
786
787         lcd_sync();
788         return 0;
789 }
790 #endif
791
792 static void lcd_logo(void)
793 {
794         lcd_logo_plot(0, 0);
795
796 #ifdef CONFIG_LCD_INFO
797         lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
798         lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
799         lcd_show_board_info();
800 #endif /* CONFIG_LCD_INFO */
801 }
802
803 #ifdef CONFIG_SPLASHIMAGE_GUARD
804 static int on_splashimage(const char *name, const char *value, enum env_op op,
805         int flags)
806 {
807         ulong addr;
808         int aligned;
809
810         if (op == env_op_delete)
811                 return 0;
812
813         addr = simple_strtoul(value, NULL, 16);
814         /* See README.displaying-bmps */
815         aligned = (addr % 4 == 2);
816         if (!aligned) {
817                 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
818                 return -1;
819         }
820
821         return 0;
822 }
823
824 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
825 #endif
826
827 int lcd_get_pixel_width(void)
828 {
829         return panel_info.vl_col;
830 }
831
832 int lcd_get_pixel_height(void)
833 {
834         return panel_info.vl_row;
835 }