Merge git://git.denx.de/u-boot-rockchip
[oweals/u-boot.git] / lib / efi_loader / efi_gop.c
1 /*
2  *  EFI application disk support
3  *
4  *  Copyright (c) 2016 Alexander Graf
5  *
6  *  SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <efi_loader.h>
11 #include <inttypes.h>
12 #include <lcd.h>
13 #include <malloc.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 static const efi_guid_t efi_gop_guid = EFI_GOP_GUID;
18
19 struct efi_gop_obj {
20         /* Generic EFI object parent class data */
21         struct efi_object parent;
22         /* EFI Interface callback struct for gop */
23         struct efi_gop ops;
24         /* The only mode we support */
25         struct efi_gop_mode_info info;
26         struct efi_gop_mode mode;
27 };
28
29 static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
30                                           unsigned long *size_of_info,
31                                           struct efi_gop_mode_info **info)
32 {
33         struct efi_gop_obj *gopobj;
34
35         EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
36
37         gopobj = container_of(this, struct efi_gop_obj, ops);
38         *size_of_info = sizeof(gopobj->info);
39         *info = &gopobj->info;
40
41         return EFI_EXIT(EFI_SUCCESS);
42 }
43
44 static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
45 {
46         EFI_ENTRY("%p, %x", this, mode_number);
47
48         if (mode_number != 0)
49                 return EFI_EXIT(EFI_INVALID_PARAMETER);
50
51         return EFI_EXIT(EFI_SUCCESS);
52 }
53
54 static efi_status_t EFIAPI gop_blt(struct efi_gop *this, void *buffer,
55                                    unsigned long operation, unsigned long sx,
56                                    unsigned long sy, unsigned long dx,
57                                    unsigned long dy, unsigned long width,
58                                    unsigned long height, unsigned long delta)
59 {
60         int i, j, line_len16, line_len32;
61         void *fb;
62
63         EFI_ENTRY("%p, %p, %lx, %lx, %lx, %lx, %lx, %lx, %lx, %lx", this,
64                   buffer, operation, sx, sy, dx, dy, width, height, delta);
65
66         if (operation != EFI_BLT_BUFFER_TO_VIDEO)
67                 return EFI_EXIT(EFI_INVALID_PARAMETER);
68
69         fb = (void*)gd->fb_base;
70         line_len16 = panel_info.vl_col * sizeof(u16);
71         line_len32 = panel_info.vl_col * sizeof(u32);
72
73         /* Copy the contents line by line */
74
75         switch (panel_info.vl_bpix) {
76         case LCD_COLOR32:
77                 for (i = 0; i < height; i++) {
78                         u32 *dest = fb + ((i + dy)  * line_len32) +
79                                          (dx * sizeof(u32));
80                         u32 *src = buffer + ((i + sy)  * line_len32) +
81                                          (sx * sizeof(u32));
82
83                         /* Same color format, just memcpy */
84                         memcpy(dest, src, width * sizeof(u32));
85                 }
86                 break;
87         case LCD_COLOR16:
88                 for (i = 0; i < height; i++) {
89                         u16 *dest = fb + ((i + dy)  * line_len16) +
90                                          (dx * sizeof(u16));
91                         u32 *src = buffer + ((i + sy)  * line_len32) +
92                                          (sx * sizeof(u32));
93
94                         /* Convert from rgb888 to rgb565 */
95                         for (j = 0; j < width; j++) {
96                                 u32 rgb888 = src[j];
97                                 dest[j] = ((((rgb888 >> (16 + 3)) & 0x1f) << 11) |
98                                            (((rgb888 >> (8 + 2)) & 0x3f) << 5) |
99                                            (((rgb888 >> (0 + 3)) & 0x1f) << 0));
100                         }
101                 }
102                 break;
103         }
104
105         lcd_sync();
106
107         return EFI_EXIT(EFI_SUCCESS);
108 }
109
110 /* This gets called from do_bootefi_exec(). */
111 int efi_gop_register(void)
112 {
113         struct efi_gop_obj *gopobj;
114
115         switch (panel_info.vl_bpix) {
116         case LCD_COLOR32:
117         case LCD_COLOR16:
118                 break;
119         default:
120                 /* So far, we only work in 16 or 32 bit mode */
121                 return -1;
122         }
123
124         gopobj = calloc(1, sizeof(*gopobj));
125
126         /* Fill in object data */
127         gopobj->parent.protocols[0].guid = &efi_gop_guid;
128         gopobj->parent.protocols[0].open = efi_return_handle;
129         gopobj->parent.handle = &gopobj->ops;
130         gopobj->ops.query_mode = gop_query_mode;
131         gopobj->ops.set_mode = gop_set_mode;
132         gopobj->ops.blt = gop_blt;
133         gopobj->ops.mode = &gopobj->mode;
134
135         gopobj->mode.max_mode = 1;
136         gopobj->mode.info = &gopobj->info;
137         gopobj->mode.info_size = sizeof(gopobj->info);
138
139         gopobj->info.version = 0;
140         gopobj->info.width = panel_info.vl_col;
141         gopobj->info.height = panel_info.vl_row;
142         gopobj->info.pixel_format = EFI_GOT_RGBA8;
143         gopobj->info.pixels_per_scanline = panel_info.vl_col;
144
145         /* Hook up to the device list */
146         list_add_tail(&gopobj->parent.link, &efi_obj_list);
147
148         return 0;
149 }