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