video: fsl: colibri_vf: Drop FSL DCU driver
[platform/kernel/u-boot.git] / drivers / video / efi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
4  *
5  * EFI framebuffer driver based on GOP
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <efi_api.h>
11 #include <log.h>
12 #include <vbe.h>
13 #include <video.h>
14
15 struct pixel {
16         u8 pos;
17         u8 size;
18 };
19
20 static const struct efi_framebuffer {
21         struct pixel red;
22         struct pixel green;
23         struct pixel blue;
24         struct pixel rsvd;
25 } efi_framebuffer_format_map[] = {
26         [EFI_GOT_RGBA8] = { {0, 8}, {8, 8}, {16, 8}, {24, 8} },
27         [EFI_GOT_BGRA8] = { {16, 8}, {8, 8}, {0, 8}, {24, 8} },
28 };
29
30 static void efi_find_pixel_bits(u32 mask, u8 *pos, u8 *size)
31 {
32         u8 first, len;
33
34         first = 0;
35         len = 0;
36
37         if (mask) {
38                 while (!(mask & 0x1)) {
39                         mask = mask >> 1;
40                         first++;
41                 }
42
43                 while (mask & 0x1) {
44                         mask = mask >> 1;
45                         len++;
46                 }
47         }
48
49         *pos = first;
50         *size = len;
51 }
52
53 static int get_mode_info(struct vesa_mode_info *vesa)
54 {
55         efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
56         struct efi_boot_services *boot = efi_get_boot();
57         struct efi_gop_mode *mode;
58         struct efi_gop *gop;
59         int ret;
60
61         if (!boot)
62                 return log_msg_ret("sys", -ENOSYS);
63         ret = boot->locate_protocol(&efi_gop_guid, NULL, (void **)&gop);
64         if (ret)
65                 return log_msg_ret("prot", -ENOTSUPP);
66
67         mode = gop->mode;
68         vesa->phys_base_ptr = mode->fb_base;
69         vesa->x_resolution = mode->info->width;
70         vesa->y_resolution = mode->info->height;
71
72         return 0;
73 }
74
75 static int save_vesa_mode(struct vesa_mode_info *vesa)
76 {
77         struct efi_entry_gopmode *mode;
78         const struct efi_framebuffer *fbinfo;
79         int size;
80         int ret;
81
82         if (IS_ENABLED(CONFIG_EFI_APP)) {
83                 ret = get_mode_info(vesa);
84                 if (ret) {
85                         printf("EFI graphics output protocol not found\n");
86                         return -ENXIO;
87                 }
88         } else {
89                 ret = efi_info_get(EFIET_GOP_MODE, (void **)&mode, &size);
90                 if (ret == -ENOENT) {
91                         printf("EFI graphics output protocol mode not found\n");
92                         return -ENXIO;
93                 }
94                 vesa->phys_base_ptr = mode->fb_base;
95                 vesa->x_resolution = mode->info->width;
96                 vesa->y_resolution = mode->info->height;
97         }
98
99         if (mode->info->pixel_format < EFI_GOT_BITMASK) {
100                 fbinfo = &efi_framebuffer_format_map[mode->info->pixel_format];
101                 vesa->red_mask_size = fbinfo->red.size;
102                 vesa->red_mask_pos = fbinfo->red.pos;
103                 vesa->green_mask_size = fbinfo->green.size;
104                 vesa->green_mask_pos = fbinfo->green.pos;
105                 vesa->blue_mask_size = fbinfo->blue.size;
106                 vesa->blue_mask_pos = fbinfo->blue.pos;
107                 vesa->reserved_mask_size = fbinfo->rsvd.size;
108                 vesa->reserved_mask_pos = fbinfo->rsvd.pos;
109
110                 vesa->bits_per_pixel = 32;
111                 vesa->bytes_per_scanline = mode->info->pixels_per_scanline * 4;
112         } else if (mode->info->pixel_format == EFI_GOT_BITMASK) {
113                 efi_find_pixel_bits(mode->info->pixel_bitmask[0],
114                                     &vesa->red_mask_pos,
115                                     &vesa->red_mask_size);
116                 efi_find_pixel_bits(mode->info->pixel_bitmask[1],
117                                     &vesa->green_mask_pos,
118                                     &vesa->green_mask_size);
119                 efi_find_pixel_bits(mode->info->pixel_bitmask[2],
120                                     &vesa->blue_mask_pos,
121                                     &vesa->blue_mask_size);
122                 efi_find_pixel_bits(mode->info->pixel_bitmask[3],
123                                     &vesa->reserved_mask_pos,
124                                     &vesa->reserved_mask_size);
125                 vesa->bits_per_pixel = vesa->red_mask_size +
126                                        vesa->green_mask_size +
127                                        vesa->blue_mask_size +
128                                        vesa->reserved_mask_size;
129                 vesa->bytes_per_scanline = (mode->info->pixels_per_scanline *
130                                             vesa->bits_per_pixel) / 8;
131         } else {
132                 debug("efi set unknown framebuffer format: %d\n",
133                       mode->info->pixel_format);
134                 return -EINVAL;
135         }
136
137         return 0;
138 }
139
140 static int efi_video_probe(struct udevice *dev)
141 {
142         struct video_uc_plat *plat = dev_get_uclass_plat(dev);
143         struct video_priv *uc_priv = dev_get_uclass_priv(dev);
144         struct vesa_mode_info *vesa = &mode_info.vesa;
145         int ret;
146
147         /* Initialize vesa_mode_info structure */
148         ret = save_vesa_mode(vesa);
149         if (ret)
150                 goto err;
151
152         ret = vbe_setup_video_priv(vesa, uc_priv, plat);
153         if (ret)
154                 goto err;
155
156         printf("Video: %dx%dx%d\n", uc_priv->xsize, uc_priv->ysize,
157                vesa->bits_per_pixel);
158
159         return 0;
160
161 err:
162         printf("No video mode configured in EFI!\n");
163         return ret;
164 }
165
166 static const struct udevice_id efi_video_ids[] = {
167         { .compatible = "efi-fb" },
168         { }
169 };
170
171 U_BOOT_DRIVER(efi_video) = {
172         .name   = "efi_video",
173         .id     = UCLASS_VIDEO,
174         .of_match = efi_video_ids,
175         .probe  = efi_video_probe,
176 };