Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 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> |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 10 | #include <dm.h> |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 11 | #include <efi_loader.h> |
| 12 | #include <inttypes.h> |
| 13 | #include <lcd.h> |
| 14 | #include <malloc.h> |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 15 | #include <video.h> |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | static const efi_guid_t efi_gop_guid = EFI_GOP_GUID; |
| 20 | |
| 21 | struct efi_gop_obj { |
| 22 | /* Generic EFI object parent class data */ |
| 23 | struct efi_object parent; |
| 24 | /* EFI Interface callback struct for gop */ |
| 25 | struct efi_gop ops; |
| 26 | /* The only mode we support */ |
| 27 | struct efi_gop_mode_info info; |
| 28 | struct efi_gop_mode mode; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 29 | /* Fields we only have acces to during init */ |
| 30 | u32 bpix; |
Rob Clark | 92cec96 | 2017-07-21 15:00:27 -0400 | [diff] [blame] | 31 | void *fb; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number, |
| 35 | unsigned long *size_of_info, |
| 36 | struct efi_gop_mode_info **info) |
| 37 | { |
| 38 | struct efi_gop_obj *gopobj; |
| 39 | |
| 40 | EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info); |
| 41 | |
| 42 | gopobj = container_of(this, struct efi_gop_obj, ops); |
| 43 | *size_of_info = sizeof(gopobj->info); |
| 44 | *info = &gopobj->info; |
| 45 | |
| 46 | return EFI_EXIT(EFI_SUCCESS); |
| 47 | } |
| 48 | |
| 49 | static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number) |
| 50 | { |
| 51 | EFI_ENTRY("%p, %x", this, mode_number); |
| 52 | |
| 53 | if (mode_number != 0) |
| 54 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 55 | |
| 56 | return EFI_EXIT(EFI_SUCCESS); |
| 57 | } |
| 58 | |
| 59 | static efi_status_t EFIAPI gop_blt(struct efi_gop *this, void *buffer, |
| 60 | unsigned long operation, unsigned long sx, |
| 61 | unsigned long sy, unsigned long dx, |
| 62 | unsigned long dy, unsigned long width, |
| 63 | unsigned long height, unsigned long delta) |
| 64 | { |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 65 | struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops); |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 66 | int i, j, line_len16, line_len32; |
| 67 | void *fb; |
| 68 | |
| 69 | EFI_ENTRY("%p, %p, %lx, %lx, %lx, %lx, %lx, %lx, %lx, %lx", this, |
| 70 | buffer, operation, sx, sy, dx, dy, width, height, delta); |
| 71 | |
| 72 | if (operation != EFI_BLT_BUFFER_TO_VIDEO) |
| 73 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 74 | |
Rob Clark | 92cec96 | 2017-07-21 15:00:27 -0400 | [diff] [blame] | 75 | fb = gopobj->fb; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 76 | line_len16 = gopobj->info.width * sizeof(u16); |
| 77 | line_len32 = gopobj->info.width * sizeof(u32); |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 78 | |
| 79 | /* Copy the contents line by line */ |
| 80 | |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 81 | switch (gopobj->bpix) { |
| 82 | #ifdef CONFIG_DM_VIDEO |
| 83 | case VIDEO_BPP32: |
| 84 | #else |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 85 | case LCD_COLOR32: |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 86 | #endif |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 87 | for (i = 0; i < height; i++) { |
| 88 | u32 *dest = fb + ((i + dy) * line_len32) + |
| 89 | (dx * sizeof(u32)); |
| 90 | u32 *src = buffer + ((i + sy) * line_len32) + |
| 91 | (sx * sizeof(u32)); |
| 92 | |
| 93 | /* Same color format, just memcpy */ |
| 94 | memcpy(dest, src, width * sizeof(u32)); |
| 95 | } |
| 96 | break; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_DM_VIDEO |
| 98 | case VIDEO_BPP16: |
| 99 | #else |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 100 | case LCD_COLOR16: |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 101 | #endif |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 102 | for (i = 0; i < height; i++) { |
| 103 | u16 *dest = fb + ((i + dy) * line_len16) + |
| 104 | (dx * sizeof(u16)); |
| 105 | u32 *src = buffer + ((i + sy) * line_len32) + |
| 106 | (sx * sizeof(u32)); |
| 107 | |
| 108 | /* Convert from rgb888 to rgb565 */ |
| 109 | for (j = 0; j < width; j++) { |
| 110 | u32 rgb888 = src[j]; |
| 111 | dest[j] = ((((rgb888 >> (16 + 3)) & 0x1f) << 11) | |
| 112 | (((rgb888 >> (8 + 2)) & 0x3f) << 5) | |
| 113 | (((rgb888 >> (0 + 3)) & 0x1f) << 0)); |
| 114 | } |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 119 | #ifdef CONFIG_DM_VIDEO |
| 120 | video_sync_all(); |
| 121 | #else |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 122 | lcd_sync(); |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 123 | #endif |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 124 | |
| 125 | return EFI_EXIT(EFI_SUCCESS); |
| 126 | } |
| 127 | |
| 128 | /* This gets called from do_bootefi_exec(). */ |
| 129 | int efi_gop_register(void) |
| 130 | { |
| 131 | struct efi_gop_obj *gopobj; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 132 | u32 bpix, col, row; |
Alexander Graf | bc76639 | 2016-06-07 00:57:05 +0200 | [diff] [blame] | 133 | u64 fb_base, fb_size; |
Rob Clark | 92cec96 | 2017-07-21 15:00:27 -0400 | [diff] [blame] | 134 | void *fb; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 135 | |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 136 | #ifdef CONFIG_DM_VIDEO |
| 137 | struct udevice *vdev; |
| 138 | |
| 139 | /* We only support a single video output device for now */ |
| 140 | if (uclass_first_device(UCLASS_VIDEO, &vdev)) |
| 141 | return -1; |
| 142 | |
| 143 | struct video_priv *priv = dev_get_uclass_priv(vdev); |
| 144 | bpix = priv->bpix; |
| 145 | col = video_get_xsize(vdev); |
| 146 | row = video_get_ysize(vdev); |
Alexander Graf | bc76639 | 2016-06-07 00:57:05 +0200 | [diff] [blame] | 147 | fb_base = (uintptr_t)priv->fb; |
| 148 | fb_size = priv->fb_size; |
Rob Clark | 92cec96 | 2017-07-21 15:00:27 -0400 | [diff] [blame] | 149 | fb = priv->fb; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 150 | #else |
Alexander Graf | bc76639 | 2016-06-07 00:57:05 +0200 | [diff] [blame] | 151 | int line_len; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 152 | |
| 153 | bpix = panel_info.vl_bpix; |
| 154 | col = panel_info.vl_col; |
| 155 | row = panel_info.vl_row; |
Alexander Graf | bc76639 | 2016-06-07 00:57:05 +0200 | [diff] [blame] | 156 | fb_base = gd->fb_base; |
| 157 | fb_size = lcd_get_size(&line_len); |
Alexander Graf | da29c60 | 2017-07-31 09:15:57 +0200 | [diff] [blame^] | 158 | fb = (void*)gd->fb_base; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 159 | #endif |
| 160 | |
| 161 | switch (bpix) { |
| 162 | #ifdef CONFIG_DM_VIDEO |
| 163 | case VIDEO_BPP16: |
| 164 | case VIDEO_BPP32: |
| 165 | #else |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 166 | case LCD_COLOR32: |
| 167 | case LCD_COLOR16: |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 168 | #endif |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 169 | break; |
| 170 | default: |
| 171 | /* So far, we only work in 16 or 32 bit mode */ |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | gopobj = calloc(1, sizeof(*gopobj)); |
| 176 | |
| 177 | /* Fill in object data */ |
| 178 | gopobj->parent.protocols[0].guid = &efi_gop_guid; |
xypron.glpk@gmx.de | c35c924 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 179 | gopobj->parent.protocols[0].protocol_interface = &gopobj->ops; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 180 | gopobj->parent.handle = &gopobj->ops; |
| 181 | gopobj->ops.query_mode = gop_query_mode; |
| 182 | gopobj->ops.set_mode = gop_set_mode; |
| 183 | gopobj->ops.blt = gop_blt; |
| 184 | gopobj->ops.mode = &gopobj->mode; |
| 185 | |
| 186 | gopobj->mode.max_mode = 1; |
| 187 | gopobj->mode.info = &gopobj->info; |
| 188 | gopobj->mode.info_size = sizeof(gopobj->info); |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 189 | |
Alexander Graf | bc76639 | 2016-06-07 00:57:05 +0200 | [diff] [blame] | 190 | #ifdef CONFIG_DM_VIDEO |
| 191 | if (bpix == VIDEO_BPP32) { |
| 192 | #else |
| 193 | if (bpix == LCD_COLOR32) { |
| 194 | #endif |
| 195 | /* With 32bit color space we can directly expose the fb */ |
| 196 | gopobj->mode.fb_base = fb_base; |
| 197 | gopobj->mode.fb_size = fb_size; |
| 198 | } |
| 199 | |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 200 | gopobj->info.version = 0; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 201 | gopobj->info.width = col; |
| 202 | gopobj->info.height = row; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 203 | gopobj->info.pixel_format = EFI_GOT_RGBA8; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 204 | gopobj->info.pixels_per_scanline = col; |
| 205 | |
| 206 | gopobj->bpix = bpix; |
Rob Clark | 92cec96 | 2017-07-21 15:00:27 -0400 | [diff] [blame] | 207 | gopobj->fb = fb; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 208 | |
| 209 | /* Hook up to the device list */ |
| 210 | list_add_tail(&gopobj->parent.link, &efi_obj_list); |
| 211 | |
| 212 | return 0; |
| 213 | } |