Merge tag 'next-20220328' of https://source.denx.de/u-boot/custodians/u-boot-video...
[platform/kernel/u-boot.git] / common / lcd.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Common LCD routines
4  *
5  * (C) Copyright 2001-2002
6  * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
7  */
8
9 /* #define DEBUG */
10 #include <config.h>
11 #include <common.h>
12 #include <command.h>
13 #include <cpu_func.h>
14 #include <env_callback.h>
15 #include <log.h>
16 #include <asm/cache.h>
17 #include <init.h>
18 #include <asm/global_data.h>
19 #include <linux/types.h>
20 #include <stdio_dev.h>
21 #include <lcd.h>
22 #include <mapmem.h>
23 #include <watchdog.h>
24 #include <asm/unaligned.h>
25 #include <splash.h>
26 #include <asm/io.h>
27 #include <asm/unaligned.h>
28 #include <video_font.h>
29
30 #ifdef CONFIG_LCD_LOGO
31 #include <bmp_logo.h>
32 #include <bmp_logo_data.h>
33 #if (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET) && (LCD_BPP != LCD_COLOR16)
34 #error Default Color Map overlaps with Logo Color Map
35 #endif
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) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
70         int line_length;
71
72         if (lcd_flush_dcache)
73                 flush_dcache_range((ulong)lcd_base,
74                         (ulong)(lcd_base + lcd_get_size(&line_length)));
75 #endif
76 }
77
78 void lcd_set_flush_dcache(int flush)
79 {
80         lcd_flush_dcache = (flush != 0);
81 }
82
83 static void lcd_stub_putc(struct stdio_dev *dev, const char c)
84 {
85         lcd_putc(c);
86 }
87
88 static void lcd_stub_puts(struct stdio_dev *dev, const char *s)
89 {
90         lcd_puts(s);
91 }
92
93 /*
94  * With most lcd drivers the line length is set up
95  * by calculating it from panel_info parameters. Some
96  * drivers need to calculate the line length differently,
97  * so make the function weak to allow overriding it.
98  */
99 __weak int lcd_get_size(int *line_length)
100 {
101         *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
102         return *line_length * panel_info.vl_row;
103 }
104
105 int drv_lcd_init(void)
106 {
107         struct stdio_dev lcddev;
108         int rc;
109
110         lcd_base = map_sysmem(gd->fb_base, 0);
111
112         lcd_init(lcd_base);
113
114         /* Device initialization */
115         memset(&lcddev, 0, sizeof(lcddev));
116
117         strcpy(lcddev.name, "lcd");
118         lcddev.ext   = 0;                       /* No extensions */
119         lcddev.flags = DEV_FLAGS_OUTPUT;        /* Output only */
120         lcddev.putc  = lcd_stub_putc;           /* 'putc' function */
121         lcddev.puts  = lcd_stub_puts;           /* 'puts' function */
122
123         rc = stdio_register(&lcddev);
124
125         return (rc == 0) ? 1 : rc;
126 }
127
128 void lcd_clear(void)
129 {
130         int bg_color;
131         __maybe_unused ulong addr;
132         static int do_splash = 1;
133 #if LCD_BPP == LCD_COLOR8
134         /* Setting the palette */
135         lcd_setcolreg(CONSOLE_COLOR_BLACK, 0, 0, 0);
136         lcd_setcolreg(CONSOLE_COLOR_RED, 0xFF, 0, 0);
137         lcd_setcolreg(CONSOLE_COLOR_GREEN, 0, 0xFF, 0);
138         lcd_setcolreg(CONSOLE_COLOR_YELLOW, 0xFF, 0xFF, 0);
139         lcd_setcolreg(CONSOLE_COLOR_BLUE, 0, 0, 0xFF);
140         lcd_setcolreg(CONSOLE_COLOR_MAGENTA, 0xFF, 0, 0xFF);
141         lcd_setcolreg(CONSOLE_COLOR_CYAN, 0, 0xFF, 0xFF);
142         lcd_setcolreg(CONSOLE_COLOR_GREY, 0xAA, 0xAA, 0xAA);
143         lcd_setcolreg(CONSOLE_COLOR_WHITE, 0xFF, 0xFF, 0xFF);
144 #endif
145
146 #ifndef CONFIG_SYS_WHITE_ON_BLACK
147         lcd_setfgcolor(CONSOLE_COLOR_BLACK);
148         lcd_setbgcolor(CONSOLE_COLOR_WHITE);
149         bg_color = CONSOLE_COLOR_WHITE;
150 #else
151         lcd_setfgcolor(CONSOLE_COLOR_WHITE);
152         lcd_setbgcolor(CONSOLE_COLOR_BLACK);
153         bg_color = CONSOLE_COLOR_BLACK;
154 #endif  /* CONFIG_SYS_WHITE_ON_BLACK */
155
156         /* set framebuffer to background color */
157 #if (LCD_BPP != LCD_COLOR32)
158         memset((char *)lcd_base, bg_color, lcd_line_length * panel_info.vl_row);
159 #else
160         u32 *ppix = lcd_base;
161         u32 i;
162         for (i = 0;
163            i < (lcd_line_length * panel_info.vl_row)/NBYTES(panel_info.vl_bpix);
164            i++) {
165                 *ppix++ = bg_color;
166         }
167 #endif
168         /* setup text-console */
169         debug("[LCD] setting up console...\n");
170         lcd_init_console(lcd_base,
171                          panel_info.vl_col,
172                          panel_info.vl_row,
173                          panel_info.vl_rot);
174         /* Paint the logo and retrieve LCD base address */
175         debug("[LCD] Drawing the logo...\n");
176         if (do_splash) {
177                 if (splash_display() == 0) {
178                         do_splash = 0;
179                         lcd_sync();
180                         return;
181                 }
182         }
183
184         lcd_logo();
185 #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
186         addr = (ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length;
187         lcd_init_console((void *)addr, panel_info.vl_col,
188                          panel_info.vl_row, panel_info.vl_rot);
189 #endif
190         lcd_sync();
191 }
192
193 static int lcd_init(void *lcdbase)
194 {
195         debug("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
196         lcd_ctrl_init(lcdbase);
197
198         /*
199          * lcd_ctrl_init() of some drivers (i.e. bcm2835 on rpi) ignores
200          * the 'lcdbase' argument and uses custom lcd base address
201          * by setting up gd->fb_base. Check for this condition and fixup
202          * 'lcd_base' address.
203          */
204         if (map_to_sysmem(lcdbase) != gd->fb_base)
205                 lcd_base = map_sysmem(gd->fb_base, 0);
206
207         debug("[LCD] Using LCD frambuffer at %p\n", lcd_base);
208
209         lcd_get_size(&lcd_line_length);
210         lcd_is_enabled = 1;
211         lcd_clear();
212         lcd_enable();
213
214         /* Initialize the console */
215         lcd_set_col(0);
216 #ifdef CONFIG_LCD_INFO_BELOW_LOGO
217         lcd_set_row(7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT);
218 #else
219         lcd_set_row(1); /* leave 1 blank line below logo */
220 #endif
221
222         return 0;
223 }
224
225 /*
226  * This is called early in the system initialization to grab memory
227  * for the LCD controller.
228  * Returns new address for monitor, after reserving LCD buffer memory
229  *
230  * Note that this is running from ROM, so no write access to global data.
231  */
232 ulong lcd_setmem(ulong addr)
233 {
234         ulong size;
235         int line_length;
236
237         debug("LCD panel info: %d x %d, %d bit/pix\n", panel_info.vl_col,
238                 panel_info.vl_row, NBITS(panel_info.vl_bpix));
239
240         size = lcd_get_size(&line_length);
241
242         /* Round up to nearest full page, or MMU section if defined */
243         size = ALIGN(size, CONFIG_LCD_ALIGNMENT);
244         addr = ALIGN(addr - CONFIG_LCD_ALIGNMENT + 1, CONFIG_LCD_ALIGNMENT);
245
246         /* Allocate pages for the frame buffer. */
247         addr -= size;
248
249         debug("Reserving %ldk for LCD Framebuffer at: %08lx\n",
250               size >> 10, addr);
251
252         return addr;
253 }
254
255 static void lcd_setfgcolor(int color)
256 {
257         lcd_color_fg = color;
258 }
259
260 int lcd_getfgcolor(void)
261 {
262         return lcd_color_fg;
263 }
264
265 static void lcd_setbgcolor(int color)
266 {
267         lcd_color_bg = color;
268 }
269
270 int lcd_getbgcolor(void)
271 {
272         return lcd_color_bg;
273 }
274
275 #ifdef CONFIG_LCD_LOGO
276 __weak void lcd_logo_set_cmap(void)
277 {
278         int i;
279         ushort *cmap = configuration_get_cmap();
280
281         for (i = 0; i < ARRAY_SIZE(bmp_logo_palette); ++i)
282                 *cmap++ = bmp_logo_palette[i];
283 }
284
285 void lcd_logo_plot(int x, int y)
286 {
287         ushort i, j;
288         uchar *bmap = &bmp_logo_bitmap[0];
289         unsigned bpix = NBITS(panel_info.vl_bpix);
290         uchar *fb = (uchar *)(lcd_base + y * lcd_line_length + x * bpix / 8);
291         ushort *fb16;
292
293         debug("Logo: width %d  height %d  colors %d\n",
294               BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS);
295
296         if (bpix < 12) {
297                 WATCHDOG_RESET();
298                 lcd_logo_set_cmap();
299                 WATCHDOG_RESET();
300
301                 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
302                         memcpy(fb, bmap, BMP_LOGO_WIDTH);
303                         bmap += BMP_LOGO_WIDTH;
304                         fb += panel_info.vl_col;
305                 }
306         }
307         else { /* true color mode */
308                 u16 col16;
309                 fb16 = (ushort *)fb;
310                 for (i = 0; i < BMP_LOGO_HEIGHT; ++i) {
311                         for (j = 0; j < BMP_LOGO_WIDTH; j++) {
312                                 col16 = bmp_logo_palette[(bmap[j]-16)];
313                                 fb16[j] =
314                                         ((col16 & 0x000F) << 1) |
315                                         ((col16 & 0x00F0) << 3) |
316                                         ((col16 & 0x0F00) << 4);
317                                 }
318                         bmap += BMP_LOGO_WIDTH;
319                         fb16 += panel_info.vl_col;
320                 }
321         }
322
323         WATCHDOG_RESET();
324         lcd_sync();
325 }
326 #else
327 static inline void lcd_logo_plot(int x, int y) {}
328 #endif /* CONFIG_LCD_LOGO */
329
330 #if defined(CONFIG_CMD_BMP) || defined(CONFIG_SPLASH_SCREEN)
331 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
332
333 static void splash_align_axis(int *axis, unsigned long panel_size,
334                                         unsigned long picture_size)
335 {
336         unsigned long panel_picture_delta = panel_size - picture_size;
337         unsigned long axis_alignment;
338
339         if (*axis == BMP_ALIGN_CENTER)
340                 axis_alignment = panel_picture_delta / 2;
341         else if (*axis < 0)
342                 axis_alignment = panel_picture_delta + *axis + 1;
343         else
344                 return;
345
346         *axis = max(0, (int)axis_alignment);
347 }
348 #endif
349
350 __weak void fb_put_byte(uchar **fb, uchar **from)
351 {
352         *(*fb)++ = *(*from)++;
353 }
354
355 #if defined(CONFIG_BMP_16BPP)
356 __weak void fb_put_word(uchar **fb, uchar **from)
357 {
358         *(*fb)++ = *(*from)++;
359         *(*fb)++ = *(*from)++;
360 }
361 #endif /* CONFIG_BMP_16BPP */
362
363 __weak void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
364 {
365         int i;
366         struct bmp_color_table_entry cte;
367         ushort *cmap = configuration_get_cmap();
368
369         for (i = 0; i < colors; ++i) {
370                 cte = bmp->color_table[i];
371                 *cmap = (((cte.red)   << 8) & 0xf800) |
372                         (((cte.green) << 3) & 0x07e0) |
373                         (((cte.blue)  >> 3) & 0x001f);
374                 cmap++;
375         }
376 }
377
378 int lcd_display_bitmap(ulong bmp_image, int x, int y)
379 {
380         ushort *cmap_base = NULL;
381         ushort i, j;
382         uchar *fb;
383         struct bmp_image *bmp = (struct bmp_image *)map_sysmem(bmp_image, 0);
384         uchar *bmap;
385         ushort padded_width;
386         unsigned long width, height, byte_width;
387         unsigned long pwidth = panel_info.vl_col;
388         unsigned colors, bpix, bmp_bpix;
389         int hdr_size;
390         struct bmp_color_table_entry *palette;
391
392         if (!bmp || !(bmp->header.signature[0] == 'B' &&
393                 bmp->header.signature[1] == 'M')) {
394                 printf("Error: no valid bmp image at %lx\n", bmp_image);
395
396                 return 1;
397         }
398
399         palette = bmp->color_table;
400         width = get_unaligned_le32(&bmp->header.width);
401         height = get_unaligned_le32(&bmp->header.height);
402         bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
403         hdr_size = get_unaligned_le16(&bmp->header.size);
404         debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
405
406         colors = 1 << bmp_bpix;
407
408         bpix = NBITS(panel_info.vl_bpix);
409
410         if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
411                 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
412                         bpix, bmp_bpix);
413
414                 return 1;
415         }
416
417         /*
418          * We support displaying 8bpp BMPs on 16bpp LCDs
419          * and displaying 24bpp BMPs on 32bpp LCDs
420          * */
421         if (bpix != bmp_bpix &&
422             !(bmp_bpix == 8 && bpix == 16) &&
423             !(bmp_bpix == 24 && bpix == 32)) {
424                 printf ("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
425                         bpix, get_unaligned_le16(&bmp->header.bit_count));
426                 return 1;
427         }
428
429         debug("Display-bmp: %d x %d  with %d colors, display %d\n",
430               (int)width, (int)height, (int)colors, 1 << bpix);
431
432         if (bmp_bpix == 8)
433                 lcd_set_cmap(bmp, colors);
434
435         padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
436
437 #ifdef CONFIG_SPLASH_SCREEN_ALIGN
438         splash_align_axis(&x, pwidth, width);
439         splash_align_axis(&y, panel_info.vl_row, height);
440 #endif /* CONFIG_SPLASH_SCREEN_ALIGN */
441
442         if ((x + width) > pwidth)
443                 width = pwidth - x;
444         if ((y + height) > panel_info.vl_row)
445                 height = panel_info.vl_row - y;
446
447         bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
448         fb   = (uchar *)(lcd_base +
449                 (y + height - 1) * lcd_line_length + x * bpix / 8);
450
451         switch (bmp_bpix) {
452         case 1:
453         case 8: {
454                 cmap_base = configuration_get_cmap();
455
456                 if (bpix != 16)
457                         byte_width = width;
458                 else
459                         byte_width = width * 2;
460
461                 for (i = 0; i < height; ++i) {
462                         WATCHDOG_RESET();
463                         for (j = 0; j < width; j++) {
464                                 if (bpix != 16) {
465                                         fb_put_byte(&fb, &bmap);
466                                 } else {
467                                         struct bmp_color_table_entry *entry;
468                                         uint val;
469
470                                         if (cmap_base) {
471                                                 val = cmap_base[*bmap];
472                                         } else {
473                                                 entry = &palette[*bmap];
474                                                 val = entry->blue >> 3 |
475                                                         entry->green >> 2 << 5 |
476                                                         entry->red >> 3 << 11;
477                                         }
478                                         *(uint16_t *)fb = val;
479                                         bmap++;
480                                         fb += sizeof(uint16_t) / sizeof(*fb);
481                                 }
482                         }
483                         bmap += (padded_width - width);
484                         fb -= byte_width + lcd_line_length;
485                 }
486                 break;
487         }
488 #if defined(CONFIG_BMP_16BPP)
489         case 16:
490                 for (i = 0; i < height; ++i) {
491                         WATCHDOG_RESET();
492                         for (j = 0; j < width; j++)
493                                 fb_put_word(&fb, &bmap);
494
495                         bmap += (padded_width - width) * 2;
496                         fb -= width * 2 + lcd_line_length;
497                 }
498                 break;
499 #endif /* CONFIG_BMP_16BPP */
500 #if defined(CONFIG_BMP_24BPP)
501         case 24:
502                 for (i = 0; i < height; ++i) {
503                         for (j = 0; j < width; j++) {
504                                 *(fb++) = *(bmap++);
505                                 *(fb++) = *(bmap++);
506                                 *(fb++) = *(bmap++);
507                                 *(fb++) = 0;
508                         }
509                         fb -= lcd_line_length + width * (bpix / 8);
510                 }
511                 break;
512 #endif /* CONFIG_BMP_24BPP */
513 #if defined(CONFIG_BMP_32BPP)
514         case 32:
515                 for (i = 0; i < height; ++i) {
516                         for (j = 0; j < width; j++) {
517                                 *(fb++) = *(bmap++);
518                                 *(fb++) = *(bmap++);
519                                 *(fb++) = *(bmap++);
520                                 *(fb++) = *(bmap++);
521                         }
522                         fb -= lcd_line_length + width * (bpix / 8);
523                 }
524                 break;
525 #endif /* CONFIG_BMP_32BPP */
526         default:
527                 break;
528         };
529
530         lcd_sync();
531         return 0;
532 }
533 #endif
534
535 static void lcd_logo(void)
536 {
537         lcd_logo_plot(0, 0);
538
539 #ifdef CONFIG_LCD_INFO
540         lcd_set_col(LCD_INFO_X / VIDEO_FONT_WIDTH);
541         lcd_set_row(LCD_INFO_Y / VIDEO_FONT_HEIGHT);
542         lcd_show_board_info();
543 #endif /* CONFIG_LCD_INFO */
544 }
545
546 #ifdef CONFIG_SPLASHIMAGE_GUARD
547 static int on_splashimage(const char *name, const char *value, enum env_op op,
548         int flags)
549 {
550         ulong addr;
551         int aligned;
552
553         if (op == env_op_delete)
554                 return 0;
555
556         addr = hextoul(value, NULL);
557         /* See README.displaying-bmps */
558         aligned = (addr % 4 == 2);
559         if (!aligned) {
560                 printf("Invalid splashimage value. Value must be 16 bit aligned, but not 32 bit aligned\n");
561                 return -1;
562         }
563
564         return 0;
565 }
566
567 U_BOOT_ENV_CALLBACK(splashimage, on_splashimage);
568 #endif
569
570 int lcd_get_pixel_width(void)
571 {
572         return panel_info.vl_col;
573 }
574
575 int lcd_get_pixel_height(void)
576 {
577         return panel_info.vl_row;
578 }