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