Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_VIDEO |
| 7 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 8 | #include <common.h> |
Nikhil M Jain | f7ec531 | 2023-07-18 14:27:31 +0530 | [diff] [blame] | 9 | #include <bloblist.h> |
Simon Glass | 73c9c37 | 2020-07-02 21:12:20 -0600 | [diff] [blame] | 10 | #include <console.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 15 | #include <mapmem.h> |
Nikhil M Jain | f7ec531 | 2023-07-18 14:27:31 +0530 | [diff] [blame] | 16 | #include <spl.h> |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 17 | #include <stdio_dev.h> |
| 18 | #include <video.h> |
| 19 | #include <video_console.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 21 | #include <asm/global_data.h> |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 22 | #include <dm/lists.h> |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 23 | #include <dm/device_compat.h> |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 24 | #include <dm/device-internal.h> |
| 25 | #include <dm/uclass-internal.h> |
| 26 | #ifdef CONFIG_SANDBOX |
| 27 | #include <asm/sdl.h> |
| 28 | #endif |
| 29 | |
| 30 | /* |
| 31 | * Theory of operation: |
| 32 | * |
| 33 | * Before relocation each device is bound. The driver for each device must |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 34 | * set the @align and @size values in struct video_uc_plat. This |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 35 | * information represents the requires size and alignment of the frame buffer |
| 36 | * for the device. The values can be an over-estimate but cannot be too |
| 37 | * small. The actual values will be suppled (in the same manner) by the bind() |
Pali Rohár | f204fce | 2022-03-09 20:46:00 +0100 | [diff] [blame] | 38 | * method after relocation. Additionally driver can allocate frame buffer |
| 39 | * itself by setting plat->base. |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 40 | * |
| 41 | * This information is then picked up by video_reserve() which works out how |
| 42 | * much memory is needed for all devices. This is allocated between |
| 43 | * gd->video_bottom and gd->video_top. |
| 44 | * |
| 45 | * After relocation the same process occurs. The driver supplies the same |
| 46 | * @size and @align information and this time video_post_bind() checks that |
| 47 | * the drivers does not overflow the allocated memory. |
| 48 | * |
| 49 | * The frame buffer address is actually set (to plat->base) in |
| 50 | * video_post_probe(). This function also clears the frame buffer and |
| 51 | * allocates a suitable text console device. This can then be used to write |
| 52 | * text to the video device. |
| 53 | */ |
| 54 | DECLARE_GLOBAL_DATA_PTR; |
| 55 | |
Simon Glass | 951255d | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 56 | /** |
| 57 | * struct video_uc_priv - Information for the video uclass |
| 58 | * |
| 59 | * @video_ptr: Current allocation position of the video framebuffer pointer. |
| 60 | * While binding devices after relocation, this points to the next |
| 61 | * available address to use for a device's framebuffer. It starts at |
| 62 | * gd->video_top and works downwards, running out of space when it hits |
| 63 | * gd->video_bottom. |
| 64 | */ |
| 65 | struct video_uc_priv { |
| 66 | ulong video_ptr; |
| 67 | }; |
| 68 | |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 69 | /** struct vid_rgb - Describes a video colour */ |
| 70 | struct vid_rgb { |
| 71 | u32 r; |
| 72 | u32 g; |
| 73 | u32 b; |
| 74 | }; |
| 75 | |
Simon Glass | 6463538 | 2016-01-21 19:44:52 -0700 | [diff] [blame] | 76 | void video_set_flush_dcache(struct udevice *dev, bool flush) |
| 77 | { |
| 78 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 79 | |
| 80 | priv->flush_dcache = flush; |
| 81 | } |
| 82 | |
Simon Glass | df865d3 | 2023-03-10 12:47:17 -0800 | [diff] [blame] | 83 | static ulong alloc_fb_(ulong align, ulong size, ulong *addrp) |
| 84 | { |
| 85 | ulong base; |
| 86 | |
| 87 | align = align ? align : 1 << 20; |
| 88 | base = *addrp - size; |
| 89 | base &= ~(align - 1); |
| 90 | size = *addrp - base; |
| 91 | *addrp = base; |
| 92 | |
| 93 | return size; |
| 94 | } |
| 95 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 96 | static ulong alloc_fb(struct udevice *dev, ulong *addrp) |
| 97 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 98 | struct video_uc_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | df865d3 | 2023-03-10 12:47:17 -0800 | [diff] [blame] | 99 | ulong size; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 100 | |
Simon Glass | df865d3 | 2023-03-10 12:47:17 -0800 | [diff] [blame] | 101 | if (!plat->size) { |
| 102 | if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_size) { |
| 103 | size = alloc_fb_(plat->align, plat->copy_size, addrp); |
| 104 | plat->copy_base = *addrp; |
| 105 | return size; |
| 106 | } |
| 107 | |
Bin Meng | 755623d | 2016-10-09 04:14:17 -0700 | [diff] [blame] | 108 | return 0; |
Simon Glass | df865d3 | 2023-03-10 12:47:17 -0800 | [diff] [blame] | 109 | } |
Bin Meng | 755623d | 2016-10-09 04:14:17 -0700 | [diff] [blame] | 110 | |
Pali Rohár | f204fce | 2022-03-09 20:46:00 +0100 | [diff] [blame] | 111 | /* Allow drivers to allocate the frame buffer themselves */ |
| 112 | if (plat->base) |
| 113 | return 0; |
| 114 | |
Simon Glass | df865d3 | 2023-03-10 12:47:17 -0800 | [diff] [blame] | 115 | size = alloc_fb_(plat->align, plat->size, addrp); |
| 116 | plat->base = *addrp; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 117 | |
| 118 | return size; |
| 119 | } |
| 120 | |
| 121 | int video_reserve(ulong *addrp) |
| 122 | { |
| 123 | struct udevice *dev; |
| 124 | ulong size; |
| 125 | |
Devarsh Thakkar | 2febd46 | 2023-12-05 21:25:20 +0530 | [diff] [blame] | 126 | if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() == PHASE_BOARD_F) |
| 127 | return 0; |
| 128 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 129 | gd->video_top = *addrp; |
| 130 | for (uclass_find_first_device(UCLASS_VIDEO, &dev); |
| 131 | dev; |
| 132 | uclass_find_next_device(&dev)) { |
| 133 | size = alloc_fb(dev, addrp); |
| 134 | debug("%s: Reserving %lx bytes at %lx for video device '%s'\n", |
| 135 | __func__, size, *addrp, dev->name); |
| 136 | } |
Simon Glass | c3d2f35 | 2020-07-02 21:12:33 -0600 | [diff] [blame] | 137 | |
| 138 | /* Allocate space for PCI video devices in case there were not bound */ |
| 139 | if (*addrp == gd->video_top) |
Nikhil M Jain | 9e3301d | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 140 | *addrp -= CONFIG_VAL(VIDEO_PCI_DEFAULT_FB_SIZE); |
Simon Glass | c3d2f35 | 2020-07-02 21:12:33 -0600 | [diff] [blame] | 141 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 142 | gd->video_bottom = *addrp; |
Simon Glass | b2b2961 | 2020-05-10 14:17:01 -0600 | [diff] [blame] | 143 | gd->fb_base = *addrp; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 144 | debug("Video frame buffers from %lx to %lx\n", gd->video_bottom, |
| 145 | gd->video_top); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Simon Glass | 062673b | 2023-06-01 10:22:33 -0600 | [diff] [blame] | 150 | int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend, |
| 151 | int yend, u32 colour) |
| 152 | { |
| 153 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 154 | void *start, *line; |
| 155 | int pixels = xend - xstart; |
| 156 | int row, i, ret; |
| 157 | |
| 158 | start = priv->fb + ystart * priv->line_length; |
| 159 | start += xstart * VNBYTES(priv->bpix); |
| 160 | line = start; |
| 161 | for (row = ystart; row < yend; row++) { |
| 162 | switch (priv->bpix) { |
| 163 | case VIDEO_BPP8: { |
| 164 | u8 *dst = line; |
| 165 | |
| 166 | if (IS_ENABLED(CONFIG_VIDEO_BPP8)) { |
| 167 | for (i = 0; i < pixels; i++) |
| 168 | *dst++ = colour; |
| 169 | } |
| 170 | break; |
| 171 | } |
| 172 | case VIDEO_BPP16: { |
| 173 | u16 *dst = line; |
| 174 | |
| 175 | if (IS_ENABLED(CONFIG_VIDEO_BPP16)) { |
| 176 | for (i = 0; i < pixels; i++) |
| 177 | *dst++ = colour; |
| 178 | } |
| 179 | break; |
| 180 | } |
| 181 | case VIDEO_BPP32: { |
| 182 | u32 *dst = line; |
| 183 | |
| 184 | if (IS_ENABLED(CONFIG_VIDEO_BPP32)) { |
| 185 | for (i = 0; i < pixels; i++) |
| 186 | *dst++ = colour; |
| 187 | } |
| 188 | break; |
| 189 | } |
| 190 | default: |
| 191 | return -ENOSYS; |
| 192 | } |
| 193 | line += priv->line_length; |
| 194 | } |
| 195 | ret = video_sync_copy(dev, start, line); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Nikhil M Jain | 7683353 | 2023-07-18 14:27:30 +0530 | [diff] [blame] | 202 | int video_reserve_from_bloblist(struct video_handoff *ho) |
| 203 | { |
Devarsh Thakkar | 2febd46 | 2023-12-05 21:25:20 +0530 | [diff] [blame] | 204 | if (!ho->fb || ho->size == 0) |
| 205 | return -ENOENT; |
| 206 | |
Nikhil M Jain | 7683353 | 2023-07-18 14:27:30 +0530 | [diff] [blame] | 207 | gd->video_bottom = ho->fb; |
| 208 | gd->fb_base = ho->fb; |
| 209 | gd->video_top = ho->fb + ho->size; |
Devarsh Thakkar | 2febd46 | 2023-12-05 21:25:20 +0530 | [diff] [blame] | 210 | debug("%s: Reserving %lx bytes at %08x as per bloblist received\n", |
| 211 | __func__, (unsigned long)ho->size, (u32)ho->fb); |
Nikhil M Jain | 7683353 | 2023-07-18 14:27:30 +0530 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Simon Glass | 2baa6f8 | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 216 | int video_fill(struct udevice *dev, u32 colour) |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 217 | { |
| 218 | struct video_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | f8ec621 | 2020-07-02 21:12:22 -0600 | [diff] [blame] | 219 | int ret; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 220 | |
Heinrich Schuchardt | 5e4947e | 2018-02-08 21:47:10 +0100 | [diff] [blame] | 221 | switch (priv->bpix) { |
Simon Glass | 05c17d6 | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 222 | case VIDEO_BPP16: |
Nikhil M Jain | 9e3301d | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 223 | if (CONFIG_IS_ENABLED(VIDEO_BPP16)) { |
Simon Glass | 05c17d6 | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 224 | u16 *ppix = priv->fb; |
| 225 | u16 *end = priv->fb + priv->fb_size; |
Heinrich Schuchardt | 5e4947e | 2018-02-08 21:47:10 +0100 | [diff] [blame] | 226 | |
Simon Glass | 05c17d6 | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 227 | while (ppix < end) |
Simon Glass | 2baa6f8 | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 228 | *ppix++ = colour; |
Simon Glass | 05c17d6 | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | case VIDEO_BPP32: |
Nikhil M Jain | 9e3301d | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 232 | if (CONFIG_IS_ENABLED(VIDEO_BPP32)) { |
Simon Glass | 05c17d6 | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 233 | u32 *ppix = priv->fb; |
| 234 | u32 *end = priv->fb + priv->fb_size; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 235 | |
Simon Glass | 05c17d6 | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 236 | while (ppix < end) |
Simon Glass | 2baa6f8 | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 237 | *ppix++ = colour; |
Simon Glass | 05c17d6 | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 238 | break; |
| 239 | } |
Heinrich Schuchardt | 5e4947e | 2018-02-08 21:47:10 +0100 | [diff] [blame] | 240 | default: |
Simon Glass | 2baa6f8 | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 241 | memset(priv->fb, colour, priv->fb_size); |
Heinrich Schuchardt | 5e4947e | 2018-02-08 21:47:10 +0100 | [diff] [blame] | 242 | break; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 243 | } |
Simon Glass | f8ec621 | 2020-07-02 21:12:22 -0600 | [diff] [blame] | 244 | ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size); |
| 245 | if (ret) |
| 246 | return ret; |
Simon Glass | 5534312 | 2018-10-01 12:22:26 -0600 | [diff] [blame] | 247 | |
Michal Simek | a1e136d | 2020-12-15 15:12:09 +0100 | [diff] [blame] | 248 | return video_sync(dev, false); |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Simon Glass | 2baa6f8 | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 251 | int video_clear(struct udevice *dev) |
| 252 | { |
| 253 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 254 | int ret; |
| 255 | |
| 256 | ret = video_fill(dev, priv->colour_bg); |
| 257 | if (ret) |
| 258 | return ret; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 263 | static const struct vid_rgb colours[VID_COLOUR_COUNT] = { |
| 264 | { 0x00, 0x00, 0x00 }, /* black */ |
| 265 | { 0xc0, 0x00, 0x00 }, /* red */ |
| 266 | { 0x00, 0xc0, 0x00 }, /* green */ |
| 267 | { 0xc0, 0x60, 0x00 }, /* brown */ |
| 268 | { 0x00, 0x00, 0xc0 }, /* blue */ |
| 269 | { 0xc0, 0x00, 0xc0 }, /* magenta */ |
| 270 | { 0x00, 0xc0, 0xc0 }, /* cyan */ |
| 271 | { 0xc0, 0xc0, 0xc0 }, /* light gray */ |
| 272 | { 0x80, 0x80, 0x80 }, /* gray */ |
| 273 | { 0xff, 0x00, 0x00 }, /* bright red */ |
| 274 | { 0x00, 0xff, 0x00 }, /* bright green */ |
| 275 | { 0xff, 0xff, 0x00 }, /* yellow */ |
| 276 | { 0x00, 0x00, 0xff }, /* bright blue */ |
| 277 | { 0xff, 0x00, 0xff }, /* bright magenta */ |
| 278 | { 0x00, 0xff, 0xff }, /* bright cyan */ |
| 279 | { 0xff, 0xff, 0xff }, /* white */ |
| 280 | }; |
| 281 | |
Simon Glass | d17a624 | 2023-06-01 10:22:48 -0600 | [diff] [blame] | 282 | u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx) |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 283 | { |
| 284 | switch (priv->bpix) { |
| 285 | case VIDEO_BPP16: |
Nikhil M Jain | 9e3301d | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 286 | if (CONFIG_IS_ENABLED(VIDEO_BPP16)) { |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 287 | return ((colours[idx].r >> 3) << 11) | |
| 288 | ((colours[idx].g >> 2) << 5) | |
| 289 | ((colours[idx].b >> 3) << 0); |
| 290 | } |
| 291 | break; |
| 292 | case VIDEO_BPP32: |
Nikhil M Jain | 9e3301d | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 293 | if (CONFIG_IS_ENABLED(VIDEO_BPP32)) { |
Michal Simek | 985eac8 | 2023-05-17 10:42:07 +0200 | [diff] [blame] | 294 | switch (priv->format) { |
| 295 | case VIDEO_X2R10G10B10: |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 296 | return (colours[idx].r << 22) | |
| 297 | (colours[idx].g << 12) | |
| 298 | (colours[idx].b << 2); |
Michal Simek | 985eac8 | 2023-05-17 10:42:07 +0200 | [diff] [blame] | 299 | case VIDEO_RGBA8888: |
| 300 | return (colours[idx].r << 24) | |
| 301 | (colours[idx].g << 16) | |
| 302 | (colours[idx].b << 8) | 0xff; |
| 303 | default: |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 304 | return (colours[idx].r << 16) | |
| 305 | (colours[idx].g << 8) | |
| 306 | (colours[idx].b << 0); |
Michal Simek | 985eac8 | 2023-05-17 10:42:07 +0200 | [diff] [blame] | 307 | } |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 308 | } |
| 309 | break; |
| 310 | default: |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * For unknown bit arrangements just support |
| 316 | * black and white. |
| 317 | */ |
| 318 | if (idx) |
| 319 | return 0xffffff; /* white */ |
| 320 | |
| 321 | return 0x000000; /* black */ |
| 322 | } |
| 323 | |
Simon Glass | 2b063b8 | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 324 | void video_set_default_colors(struct udevice *dev, bool invert) |
Heinrich Schuchardt | 290e1d8 | 2018-02-08 21:47:11 +0100 | [diff] [blame] | 325 | { |
Simon Glass | 2b063b8 | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 326 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 327 | int fore, back; |
| 328 | |
Simon Glass | 05c17d6 | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 329 | if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) { |
| 330 | /* White is used when switching to bold, use light gray here */ |
| 331 | fore = VID_LIGHT_GRAY; |
| 332 | back = VID_BLACK; |
| 333 | } else { |
| 334 | fore = VID_BLACK; |
| 335 | back = VID_WHITE; |
| 336 | } |
Simon Glass | 2b063b8 | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 337 | if (invert) { |
| 338 | int temp; |
| 339 | |
| 340 | temp = fore; |
| 341 | fore = back; |
| 342 | back = temp; |
| 343 | } |
| 344 | priv->fg_col_idx = fore; |
Andre Przywara | 4ed5bc8 | 2019-03-23 01:29:56 +0000 | [diff] [blame] | 345 | priv->bg_col_idx = back; |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 346 | priv->colour_fg = video_index_to_colour(priv, fore); |
| 347 | priv->colour_bg = video_index_to_colour(priv, back); |
Heinrich Schuchardt | 290e1d8 | 2018-02-08 21:47:11 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 350 | /* Flush video activity to the caches */ |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 351 | int video_sync(struct udevice *vid, bool force) |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 352 | { |
Michal Simek | 8ae95df | 2020-12-03 09:30:00 +0100 | [diff] [blame] | 353 | struct video_ops *ops = video_get_ops(vid); |
| 354 | int ret; |
| 355 | |
| 356 | if (ops && ops->video_sync) { |
| 357 | ret = ops->video_sync(vid); |
| 358 | if (ret) |
| 359 | return ret; |
| 360 | } |
| 361 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 362 | /* |
| 363 | * flush_dcache_range() is declared in common.h but it seems that some |
| 364 | * architectures do not actually implement it. Is there a way to find |
| 365 | * out whether it exists? For now, ARM is safe. |
| 366 | */ |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 367 | #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 368 | struct video_priv *priv = dev_get_uclass_priv(vid); |
| 369 | |
| 370 | if (priv->flush_dcache) { |
| 371 | flush_dcache_range((ulong)priv->fb, |
Simon Glass | eebc3fd | 2016-11-13 14:22:06 -0700 | [diff] [blame] | 372 | ALIGN((ulong)priv->fb + priv->fb_size, |
| 373 | CONFIG_SYS_CACHELINE_SIZE)); |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 374 | } |
| 375 | #elif defined(CONFIG_VIDEO_SANDBOX_SDL) |
| 376 | struct video_priv *priv = dev_get_uclass_priv(vid); |
| 377 | static ulong last_sync; |
| 378 | |
Simon Glass | ea11373 | 2022-02-28 15:13:48 -0700 | [diff] [blame] | 379 | if (force || get_timer(last_sync) > 100) { |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 380 | sandbox_sdl_sync(priv->fb); |
| 381 | last_sync = get_timer(0); |
| 382 | } |
| 383 | #endif |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 384 | return 0; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void video_sync_all(void) |
| 388 | { |
| 389 | struct udevice *dev; |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 390 | int ret; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 391 | |
| 392 | for (uclass_find_first_device(UCLASS_VIDEO, &dev); |
| 393 | dev; |
| 394 | uclass_find_next_device(&dev)) { |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 395 | if (device_active(dev)) { |
| 396 | ret = video_sync(dev, true); |
| 397 | if (ret) |
| 398 | dev_dbg(dev, "Video sync failed\n"); |
| 399 | } |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Patrick Delaunay | efcc84b | 2021-11-15 16:32:20 +0100 | [diff] [blame] | 403 | bool video_is_active(void) |
| 404 | { |
| 405 | struct udevice *dev; |
| 406 | |
Devarsh Thakkar | 42e4106 | 2024-02-22 18:38:09 +0530 | [diff] [blame] | 407 | /* Assume video to be active if SPL passed video hand-off to U-boot */ |
| 408 | if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() > PHASE_SPL) |
| 409 | return true; |
| 410 | |
Patrick Delaunay | efcc84b | 2021-11-15 16:32:20 +0100 | [diff] [blame] | 411 | for (uclass_find_first_device(UCLASS_VIDEO, &dev); |
| 412 | dev; |
| 413 | uclass_find_next_device(&dev)) { |
| 414 | if (device_active(dev)) |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | return false; |
| 419 | } |
| 420 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 421 | int video_get_xsize(struct udevice *dev) |
| 422 | { |
| 423 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 424 | |
| 425 | return priv->xsize; |
| 426 | } |
| 427 | |
| 428 | int video_get_ysize(struct udevice *dev) |
| 429 | { |
| 430 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 431 | |
| 432 | return priv->ysize; |
| 433 | } |
| 434 | |
Simon Glass | 73c9c37 | 2020-07-02 21:12:20 -0600 | [diff] [blame] | 435 | #ifdef CONFIG_VIDEO_COPY |
| 436 | int video_sync_copy(struct udevice *dev, void *from, void *to) |
| 437 | { |
| 438 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 439 | |
| 440 | if (priv->copy_fb) { |
| 441 | long offset, size; |
| 442 | |
| 443 | /* Find the offset of the first byte to copy */ |
| 444 | if ((ulong)to > (ulong)from) { |
| 445 | size = to - from; |
| 446 | offset = from - priv->fb; |
| 447 | } else { |
| 448 | size = from - to; |
| 449 | offset = to - priv->fb; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Allow a bit of leeway for valid requests somewhere near the |
| 454 | * frame buffer |
| 455 | */ |
| 456 | if (offset < -priv->fb_size || offset > 2 * priv->fb_size) { |
| 457 | #ifdef DEBUG |
Simon Glass | 78cdc5d | 2021-11-19 13:23:51 -0700 | [diff] [blame] | 458 | char str[120]; |
Simon Glass | 73c9c37 | 2020-07-02 21:12:20 -0600 | [diff] [blame] | 459 | |
| 460 | snprintf(str, sizeof(str), |
Simon Glass | 78cdc5d | 2021-11-19 13:23:51 -0700 | [diff] [blame] | 461 | "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]", |
Simon Glass | 73c9c37 | 2020-07-02 21:12:20 -0600 | [diff] [blame] | 462 | priv->fb, from, to, offset); |
| 463 | console_puts_select_stderr(true, str); |
| 464 | #endif |
| 465 | return -EFAULT; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Silently crop the memcpy. This allows callers to avoid doing |
| 470 | * this themselves. It is common for the end pointer to go a |
| 471 | * few lines after the end of the frame buffer, since most of |
| 472 | * the update algorithms terminate a line after their last write |
| 473 | */ |
| 474 | if (offset + size > priv->fb_size) { |
| 475 | size = priv->fb_size - offset; |
| 476 | } else if (offset < 0) { |
| 477 | size += offset; |
| 478 | offset = 0; |
| 479 | } |
| 480 | |
| 481 | memcpy(priv->copy_fb + offset, priv->fb + offset, size); |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
Simon Glass | 62b535e | 2021-01-13 20:29:46 -0700 | [diff] [blame] | 486 | |
| 487 | int video_sync_copy_all(struct udevice *dev) |
| 488 | { |
| 489 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 490 | |
| 491 | video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Simon Glass | 73c9c37 | 2020-07-02 21:12:20 -0600 | [diff] [blame] | 496 | #endif |
| 497 | |
Simon Glass | 87a3cd7 | 2021-11-19 13:24:03 -0700 | [diff] [blame] | 498 | #define SPLASH_DECL(_name) \ |
| 499 | extern u8 __splash_ ## _name ## _begin[]; \ |
| 500 | extern u8 __splash_ ## _name ## _end[] |
| 501 | |
| 502 | #define SPLASH_START(_name) __splash_ ## _name ## _begin |
| 503 | |
| 504 | SPLASH_DECL(u_boot_logo); |
| 505 | |
Simon Glass | 2247742 | 2022-10-06 08:36:09 -0600 | [diff] [blame] | 506 | void *video_get_u_boot_logo(void) |
| 507 | { |
| 508 | return SPLASH_START(u_boot_logo); |
| 509 | } |
| 510 | |
Simon Glass | 87a3cd7 | 2021-11-19 13:24:03 -0700 | [diff] [blame] | 511 | static int show_splash(struct udevice *dev) |
| 512 | { |
| 513 | u8 *data = SPLASH_START(u_boot_logo); |
| 514 | int ret; |
| 515 | |
| 516 | ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true); |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
Simon Glass | 7045990 | 2022-10-06 08:36:18 -0600 | [diff] [blame] | 521 | int video_default_font_height(struct udevice *dev) |
| 522 | { |
| 523 | struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev); |
| 524 | |
| 525 | if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) |
| 526 | return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE, |
| 527 | CONFIG_CONSOLE_TRUETYPE_SIZE); |
| 528 | |
| 529 | return vc_priv->y_charsize; |
| 530 | } |
| 531 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 532 | /* Set up the display ready for use */ |
| 533 | static int video_post_probe(struct udevice *dev) |
| 534 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 535 | struct video_uc_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 536 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 537 | char name[30], drv[15], *str; |
Simon Glass | b3a72b3 | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 538 | const char *drv_name = drv; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 539 | struct udevice *cons; |
| 540 | int ret; |
| 541 | |
| 542 | /* Set up the line and display size */ |
| 543 | priv->fb = map_sysmem(plat->base, plat->size); |
Simon Glass | 7d18673 | 2018-11-29 15:08:52 -0700 | [diff] [blame] | 544 | if (!priv->line_length) |
| 545 | priv->line_length = priv->xsize * VNBYTES(priv->bpix); |
| 546 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 547 | priv->fb_size = priv->line_length * priv->ysize; |
| 548 | |
Devarsh Thakkar | f5254cc | 2023-12-05 21:25:21 +0530 | [diff] [blame] | 549 | /* |
| 550 | * Set up video handoff fields for passing video blob to next stage |
| 551 | * NOTE: |
| 552 | * This assumes that reserved video memory only uses a single framebuffer |
| 553 | */ |
| 554 | if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(BLOBLIST)) { |
| 555 | struct video_handoff *ho; |
| 556 | |
| 557 | ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0); |
| 558 | if (!ho) |
| 559 | return log_msg_ret("blf", -ENOENT); |
| 560 | ho->fb = gd->video_bottom; |
| 561 | /* Fill aligned size here as calculated in video_reserve() */ |
| 562 | ho->size = gd->video_top - gd->video_bottom; |
| 563 | ho->xsize = priv->xsize; |
| 564 | ho->ysize = priv->ysize; |
| 565 | ho->line_length = priv->line_length; |
| 566 | ho->bpix = priv->bpix; |
| 567 | } |
| 568 | |
Simon Glass | b4928e5 | 2020-07-02 21:12:21 -0600 | [diff] [blame] | 569 | if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base) |
| 570 | priv->copy_fb = map_sysmem(plat->copy_base, plat->size); |
| 571 | |
Heinrich Schuchardt | 290e1d8 | 2018-02-08 21:47:11 +0100 | [diff] [blame] | 572 | /* Set up colors */ |
Simon Glass | 2b063b8 | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 573 | video_set_default_colors(dev, false); |
Rob Clark | f141188 | 2017-08-03 12:47:01 -0400 | [diff] [blame] | 574 | |
| 575 | if (!CONFIG_IS_ENABLED(NO_FB_CLEAR)) |
| 576 | video_clear(dev); |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 577 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 578 | /* |
Simon Glass | b3a72b3 | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 579 | * Create a text console device. For now we always do this, although |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 580 | * it might be useful to support only bitmap drawing on the device |
Simon Glass | b3a72b3 | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 581 | * for boards that don't need to display text. We create a TrueType |
| 582 | * console if enabled, a rotated console if the video driver requests |
| 583 | * it, otherwise a normal console. |
| 584 | * |
| 585 | * The console can be override by setting vidconsole_drv_name before |
| 586 | * probing this video driver, or in the probe() method. |
| 587 | * |
| 588 | * TrueType does not support rotation at present so fall back to the |
| 589 | * rotated console in that case. |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 590 | */ |
Simon Glass | b3a72b3 | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 591 | if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) { |
Simon Glass | 2ef353e | 2016-01-14 18:10:42 -0700 | [diff] [blame] | 592 | snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name); |
| 593 | strcpy(drv, "vidconsole_tt"); |
| 594 | } else { |
| 595 | snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name, |
| 596 | priv->rot); |
| 597 | snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot); |
| 598 | } |
| 599 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 600 | str = strdup(name); |
| 601 | if (!str) |
| 602 | return -ENOMEM; |
Simon Glass | b3a72b3 | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 603 | if (priv->vidconsole_drv_name) |
| 604 | drv_name = priv->vidconsole_drv_name; |
| 605 | ret = device_bind_driver(dev, drv_name, str, &cons); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 606 | if (ret) { |
| 607 | debug("%s: Cannot bind console driver\n", __func__); |
| 608 | return ret; |
| 609 | } |
Simon Glass | b3a72b3 | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 610 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 611 | ret = device_probe(cons); |
| 612 | if (ret) { |
| 613 | debug("%s: Cannot probe console driver\n", __func__); |
| 614 | return ret; |
| 615 | } |
| 616 | |
Nikhil M Jain | 9e3301d | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 617 | if (CONFIG_IS_ENABLED(VIDEO_LOGO) && |
| 618 | !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) { |
Simon Glass | 87a3cd7 | 2021-11-19 13:24:03 -0700 | [diff] [blame] | 619 | ret = show_splash(dev); |
| 620 | if (ret) { |
| 621 | log_debug("Cannot show splash screen\n"); |
| 622 | return ret; |
| 623 | } |
| 624 | } |
| 625 | |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 626 | return 0; |
| 627 | }; |
| 628 | |
| 629 | /* Post-relocation, allocate memory for the frame buffer */ |
| 630 | static int video_post_bind(struct udevice *dev) |
| 631 | { |
Simon Glass | 951255d | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 632 | struct video_uc_priv *uc_priv; |
| 633 | ulong addr; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 634 | ulong size; |
| 635 | |
| 636 | /* Before relocation there is nothing to do here */ |
Tom Rini | 6985a72 | 2018-04-22 09:47:48 -0400 | [diff] [blame] | 637 | if (!(gd->flags & GD_FLG_RELOC)) |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 638 | return 0; |
Simon Glass | 951255d | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 639 | |
| 640 | /* Set up the video pointer, if this is the first device */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 641 | uc_priv = uclass_get_priv(dev->uclass); |
Simon Glass | 951255d | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 642 | if (!uc_priv->video_ptr) |
| 643 | uc_priv->video_ptr = gd->video_top; |
| 644 | |
| 645 | /* Allocate framebuffer space for this device */ |
| 646 | addr = uc_priv->video_ptr; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 647 | size = alloc_fb(dev, &addr); |
| 648 | if (addr < gd->video_bottom) { |
Bin Meng | acbafdd | 2023-07-23 12:40:24 +0800 | [diff] [blame] | 649 | /* |
| 650 | * Device tree node may need the 'bootph-all' or |
Simon Glass | fc1aa35 | 2023-02-13 08:56:34 -0700 | [diff] [blame] | 651 | * 'bootph-some-ram' tag |
Patrick Delaunay | d193718 | 2019-05-21 19:19:12 +0200 | [diff] [blame] | 652 | */ |
Bin Meng | acbafdd | 2023-07-23 12:40:24 +0800 | [diff] [blame] | 653 | printf("Video device '%s' cannot allocate frame buffer memory " |
| 654 | "- ensure the device is set up before relocation\n", |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 655 | dev->name); |
| 656 | return -ENOSPC; |
| 657 | } |
| 658 | debug("%s: Claiming %lx bytes at %lx for video device '%s'\n", |
| 659 | __func__, size, addr, dev->name); |
Simon Glass | 951255d | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 660 | uc_priv->video_ptr = addr; |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | UCLASS_DRIVER(video) = { |
| 666 | .id = UCLASS_VIDEO, |
| 667 | .name = "video", |
| 668 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 669 | .post_bind = video_post_bind, |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 670 | .post_probe = video_post_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 671 | .priv_auto = sizeof(struct video_uc_priv), |
| 672 | .per_device_auto = sizeof(struct video_priv), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 673 | .per_device_plat_auto = sizeof(struct video_uc_plat), |
Simon Glass | 623d28f | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 674 | }; |