Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_VIDEO |
| 7 | |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 8 | #include <common.h> |
Nikhil M Jain | 5bc610a | 2023-07-18 14:27:31 +0530 | [diff] [blame] | 9 | #include <bloblist.h> |
Simon Glass | 9beac5d | 2020-07-02 21:12:20 -0600 | [diff] [blame] | 10 | #include <console.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 15 | #include <mapmem.h> |
Nikhil M Jain | 5bc610a | 2023-07-18 14:27:31 +0530 | [diff] [blame] | 16 | #include <spl.h> |
Simon Glass | 1acafc7 | 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 | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 21 | #include <asm/global_data.h> |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 22 | #include <dm/lists.h> |
Michal Simek | 9de731f | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 23 | #include <dm/device_compat.h> |
Simon Glass | 1acafc7 | 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 | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 34 | * set the @align and @size values in struct video_uc_plat. This |
Simon Glass | 1acafc7 | 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 | bd0df82 | 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 | 1acafc7 | 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 | 7812bbd | 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 | a032e4b | 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 | 68dcdc9 | 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 | 315e367 | 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 | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 96 | static ulong alloc_fb(struct udevice *dev, ulong *addrp) |
| 97 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 98 | struct video_uc_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | 315e367 | 2023-03-10 12:47:17 -0800 | [diff] [blame] | 99 | ulong size; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 100 | |
Simon Glass | 315e367 | 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 | 3968398 | 2016-10-09 04:14:17 -0700 | [diff] [blame] | 108 | return 0; |
Simon Glass | 315e367 | 2023-03-10 12:47:17 -0800 | [diff] [blame] | 109 | } |
Bin Meng | 3968398 | 2016-10-09 04:14:17 -0700 | [diff] [blame] | 110 | |
Pali Rohár | bd0df82 | 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 | 315e367 | 2023-03-10 12:47:17 -0800 | [diff] [blame] | 115 | size = alloc_fb_(plat->align, plat->size, addrp); |
| 116 | plat->base = *addrp; |
Simon Glass | 1acafc7 | 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 | |
| 126 | gd->video_top = *addrp; |
| 127 | for (uclass_find_first_device(UCLASS_VIDEO, &dev); |
| 128 | dev; |
| 129 | uclass_find_next_device(&dev)) { |
| 130 | size = alloc_fb(dev, addrp); |
| 131 | debug("%s: Reserving %lx bytes at %lx for video device '%s'\n", |
| 132 | __func__, size, *addrp, dev->name); |
| 133 | } |
Simon Glass | 551ca0e | 2020-07-02 21:12:33 -0600 | [diff] [blame] | 134 | |
| 135 | /* Allocate space for PCI video devices in case there were not bound */ |
| 136 | if (*addrp == gd->video_top) |
Nikhil M Jain | 86fbee6 | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 137 | *addrp -= CONFIG_VAL(VIDEO_PCI_DEFAULT_FB_SIZE); |
Simon Glass | 551ca0e | 2020-07-02 21:12:33 -0600 | [diff] [blame] | 138 | |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 139 | gd->video_bottom = *addrp; |
Simon Glass | aef43ea | 2020-05-10 14:17:01 -0600 | [diff] [blame] | 140 | gd->fb_base = *addrp; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 141 | debug("Video frame buffers from %lx to %lx\n", gd->video_bottom, |
| 142 | gd->video_top); |
| 143 | |
Simon Glass | b7080bf | 2023-07-30 11:16:05 -0600 | [diff] [blame] | 144 | if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(VIDEO_HANDOFF)) { |
Nikhil M Jain | 5bc610a | 2023-07-18 14:27:31 +0530 | [diff] [blame] | 145 | struct video_handoff *ho; |
| 146 | |
| 147 | ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0); |
| 148 | if (!ho) |
| 149 | return log_msg_ret("blf", -ENOENT); |
| 150 | ho->fb = *addrp; |
| 151 | ho->size = size; |
| 152 | } |
| 153 | |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Simon Glass | 0ab4f91 | 2023-06-01 10:22:33 -0600 | [diff] [blame] | 157 | int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend, |
| 158 | int yend, u32 colour) |
| 159 | { |
| 160 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 161 | void *start, *line; |
| 162 | int pixels = xend - xstart; |
| 163 | int row, i, ret; |
| 164 | |
| 165 | start = priv->fb + ystart * priv->line_length; |
| 166 | start += xstart * VNBYTES(priv->bpix); |
| 167 | line = start; |
| 168 | for (row = ystart; row < yend; row++) { |
| 169 | switch (priv->bpix) { |
| 170 | case VIDEO_BPP8: { |
| 171 | u8 *dst = line; |
| 172 | |
| 173 | if (IS_ENABLED(CONFIG_VIDEO_BPP8)) { |
| 174 | for (i = 0; i < pixels; i++) |
| 175 | *dst++ = colour; |
| 176 | } |
| 177 | break; |
| 178 | } |
| 179 | case VIDEO_BPP16: { |
| 180 | u16 *dst = line; |
| 181 | |
| 182 | if (IS_ENABLED(CONFIG_VIDEO_BPP16)) { |
| 183 | for (i = 0; i < pixels; i++) |
| 184 | *dst++ = colour; |
| 185 | } |
| 186 | break; |
| 187 | } |
| 188 | case VIDEO_BPP32: { |
| 189 | u32 *dst = line; |
| 190 | |
| 191 | if (IS_ENABLED(CONFIG_VIDEO_BPP32)) { |
| 192 | for (i = 0; i < pixels; i++) |
| 193 | *dst++ = colour; |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | default: |
| 198 | return -ENOSYS; |
| 199 | } |
| 200 | line += priv->line_length; |
| 201 | } |
| 202 | ret = video_sync_copy(dev, start, line); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Nikhil M Jain | ccd21ee | 2023-07-18 14:27:30 +0530 | [diff] [blame] | 209 | int video_reserve_from_bloblist(struct video_handoff *ho) |
| 210 | { |
| 211 | gd->video_bottom = ho->fb; |
| 212 | gd->fb_base = ho->fb; |
| 213 | gd->video_top = ho->fb + ho->size; |
| 214 | debug("Reserving %luk for video using blob at: %08x\n", |
| 215 | ((unsigned long)ho->size) >> 10, (u32)ho->fb); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Simon Glass | 50d562c | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 220 | int video_fill(struct udevice *dev, u32 colour) |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 221 | { |
| 222 | struct video_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 138dfea | 2020-07-02 21:12:22 -0600 | [diff] [blame] | 223 | int ret; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 224 | |
Heinrich Schuchardt | d7a75d3 | 2018-02-08 21:47:10 +0100 | [diff] [blame] | 225 | switch (priv->bpix) { |
Simon Glass | 0c20aaf | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 226 | case VIDEO_BPP16: |
Nikhil M Jain | 86fbee6 | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 227 | if (CONFIG_IS_ENABLED(VIDEO_BPP16)) { |
Simon Glass | 0c20aaf | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 228 | u16 *ppix = priv->fb; |
| 229 | u16 *end = priv->fb + priv->fb_size; |
Heinrich Schuchardt | d7a75d3 | 2018-02-08 21:47:10 +0100 | [diff] [blame] | 230 | |
Simon Glass | 0c20aaf | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 231 | while (ppix < end) |
Simon Glass | 50d562c | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 232 | *ppix++ = colour; |
Simon Glass | 0c20aaf | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | case VIDEO_BPP32: |
Nikhil M Jain | 86fbee6 | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 236 | if (CONFIG_IS_ENABLED(VIDEO_BPP32)) { |
Simon Glass | 0c20aaf | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 237 | u32 *ppix = priv->fb; |
| 238 | u32 *end = priv->fb + priv->fb_size; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 239 | |
Simon Glass | 0c20aaf | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 240 | while (ppix < end) |
Simon Glass | 50d562c | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 241 | *ppix++ = colour; |
Simon Glass | 0c20aaf | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 242 | break; |
| 243 | } |
Heinrich Schuchardt | d7a75d3 | 2018-02-08 21:47:10 +0100 | [diff] [blame] | 244 | default: |
Simon Glass | 50d562c | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 245 | memset(priv->fb, colour, priv->fb_size); |
Heinrich Schuchardt | d7a75d3 | 2018-02-08 21:47:10 +0100 | [diff] [blame] | 246 | break; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 247 | } |
Simon Glass | 138dfea | 2020-07-02 21:12:22 -0600 | [diff] [blame] | 248 | ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size); |
| 249 | if (ret) |
| 250 | return ret; |
Simon Glass | c6ebd01 | 2018-10-01 12:22:26 -0600 | [diff] [blame] | 251 | |
Michal Simek | 5337663 | 2020-12-15 15:12:09 +0100 | [diff] [blame] | 252 | return video_sync(dev, false); |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Simon Glass | 50d562c | 2022-10-06 08:36:08 -0600 | [diff] [blame] | 255 | int video_clear(struct udevice *dev) |
| 256 | { |
| 257 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 258 | int ret; |
| 259 | |
| 260 | ret = video_fill(dev, priv->colour_bg); |
| 261 | if (ret) |
| 262 | return ret; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Simon Glass | a032e4b | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 267 | static const struct vid_rgb colours[VID_COLOUR_COUNT] = { |
| 268 | { 0x00, 0x00, 0x00 }, /* black */ |
| 269 | { 0xc0, 0x00, 0x00 }, /* red */ |
| 270 | { 0x00, 0xc0, 0x00 }, /* green */ |
| 271 | { 0xc0, 0x60, 0x00 }, /* brown */ |
| 272 | { 0x00, 0x00, 0xc0 }, /* blue */ |
| 273 | { 0xc0, 0x00, 0xc0 }, /* magenta */ |
| 274 | { 0x00, 0xc0, 0xc0 }, /* cyan */ |
| 275 | { 0xc0, 0xc0, 0xc0 }, /* light gray */ |
| 276 | { 0x80, 0x80, 0x80 }, /* gray */ |
| 277 | { 0xff, 0x00, 0x00 }, /* bright red */ |
| 278 | { 0x00, 0xff, 0x00 }, /* bright green */ |
| 279 | { 0xff, 0xff, 0x00 }, /* yellow */ |
| 280 | { 0x00, 0x00, 0xff }, /* bright blue */ |
| 281 | { 0xff, 0x00, 0xff }, /* bright magenta */ |
| 282 | { 0x00, 0xff, 0xff }, /* bright cyan */ |
| 283 | { 0xff, 0xff, 0xff }, /* white */ |
| 284 | }; |
| 285 | |
Simon Glass | 2d6ee92 | 2023-06-01 10:22:48 -0600 | [diff] [blame] | 286 | u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx) |
Simon Glass | a032e4b | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 287 | { |
| 288 | switch (priv->bpix) { |
| 289 | case VIDEO_BPP16: |
Nikhil M Jain | 86fbee6 | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 290 | if (CONFIG_IS_ENABLED(VIDEO_BPP16)) { |
Simon Glass | a032e4b | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 291 | return ((colours[idx].r >> 3) << 11) | |
| 292 | ((colours[idx].g >> 2) << 5) | |
| 293 | ((colours[idx].b >> 3) << 0); |
| 294 | } |
| 295 | break; |
| 296 | case VIDEO_BPP32: |
Nikhil M Jain | 86fbee6 | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 297 | if (CONFIG_IS_ENABLED(VIDEO_BPP32)) { |
Michal Simek | e9500ba | 2023-05-17 10:42:07 +0200 | [diff] [blame] | 298 | switch (priv->format) { |
| 299 | case VIDEO_X2R10G10B10: |
Simon Glass | a032e4b | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 300 | return (colours[idx].r << 22) | |
| 301 | (colours[idx].g << 12) | |
| 302 | (colours[idx].b << 2); |
Michal Simek | e9500ba | 2023-05-17 10:42:07 +0200 | [diff] [blame] | 303 | case VIDEO_RGBA8888: |
| 304 | return (colours[idx].r << 24) | |
| 305 | (colours[idx].g << 16) | |
| 306 | (colours[idx].b << 8) | 0xff; |
| 307 | default: |
Simon Glass | a032e4b | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 308 | return (colours[idx].r << 16) | |
| 309 | (colours[idx].g << 8) | |
| 310 | (colours[idx].b << 0); |
Michal Simek | e9500ba | 2023-05-17 10:42:07 +0200 | [diff] [blame] | 311 | } |
Simon Glass | a032e4b | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 312 | } |
| 313 | break; |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * For unknown bit arrangements just support |
| 320 | * black and white. |
| 321 | */ |
| 322 | if (idx) |
| 323 | return 0xffffff; /* white */ |
| 324 | |
| 325 | return 0x000000; /* black */ |
| 326 | } |
| 327 | |
Simon Glass | b9f210a | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 328 | void video_set_default_colors(struct udevice *dev, bool invert) |
Heinrich Schuchardt | 5c30fbb | 2018-02-08 21:47:11 +0100 | [diff] [blame] | 329 | { |
Simon Glass | b9f210a | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 330 | struct video_priv *priv = dev_get_uclass_priv(dev); |
| 331 | int fore, back; |
| 332 | |
Simon Glass | 0c20aaf | 2019-12-20 18:10:37 -0700 | [diff] [blame] | 333 | if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) { |
| 334 | /* White is used when switching to bold, use light gray here */ |
| 335 | fore = VID_LIGHT_GRAY; |
| 336 | back = VID_BLACK; |
| 337 | } else { |
| 338 | fore = VID_BLACK; |
| 339 | back = VID_WHITE; |
| 340 | } |
Simon Glass | b9f210a | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 341 | if (invert) { |
| 342 | int temp; |
| 343 | |
| 344 | temp = fore; |
| 345 | fore = back; |
| 346 | back = temp; |
| 347 | } |
| 348 | priv->fg_col_idx = fore; |
Andre Przywara | eabb072 | 2019-03-23 01:29:56 +0000 | [diff] [blame] | 349 | priv->bg_col_idx = back; |
Simon Glass | a032e4b | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 350 | priv->colour_fg = video_index_to_colour(priv, fore); |
| 351 | priv->colour_bg = video_index_to_colour(priv, back); |
Heinrich Schuchardt | 5c30fbb | 2018-02-08 21:47:11 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 354 | /* Flush video activity to the caches */ |
Michal Simek | 9de731f | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 355 | int video_sync(struct udevice *vid, bool force) |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 356 | { |
Michal Simek | 9d69c2d | 2020-12-03 09:30:00 +0100 | [diff] [blame] | 357 | struct video_ops *ops = video_get_ops(vid); |
| 358 | int ret; |
| 359 | |
| 360 | if (ops && ops->video_sync) { |
| 361 | ret = ops->video_sync(vid); |
| 362 | if (ret) |
| 363 | return ret; |
| 364 | } |
| 365 | |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 366 | /* |
| 367 | * flush_dcache_range() is declared in common.h but it seems that some |
| 368 | * architectures do not actually implement it. Is there a way to find |
| 369 | * out whether it exists? For now, ARM is safe. |
| 370 | */ |
Trevor Woerner | 1001502 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 371 | #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 372 | struct video_priv *priv = dev_get_uclass_priv(vid); |
| 373 | |
| 374 | if (priv->flush_dcache) { |
| 375 | flush_dcache_range((ulong)priv->fb, |
Simon Glass | 7981394 | 2016-11-13 14:22:06 -0700 | [diff] [blame] | 376 | ALIGN((ulong)priv->fb + priv->fb_size, |
| 377 | CONFIG_SYS_CACHELINE_SIZE)); |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 378 | } |
| 379 | #elif defined(CONFIG_VIDEO_SANDBOX_SDL) |
| 380 | struct video_priv *priv = dev_get_uclass_priv(vid); |
| 381 | static ulong last_sync; |
| 382 | |
Simon Glass | 7c19e4c | 2022-02-28 15:13:48 -0700 | [diff] [blame] | 383 | if (force || get_timer(last_sync) > 100) { |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 384 | sandbox_sdl_sync(priv->fb); |
| 385 | last_sync = get_timer(0); |
| 386 | } |
| 387 | #endif |
Michal Simek | 9de731f | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 388 | return 0; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void video_sync_all(void) |
| 392 | { |
| 393 | struct udevice *dev; |
Michal Simek | 9de731f | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 394 | int ret; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 395 | |
| 396 | for (uclass_find_first_device(UCLASS_VIDEO, &dev); |
| 397 | dev; |
| 398 | uclass_find_next_device(&dev)) { |
Michal Simek | 9de731f | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 399 | if (device_active(dev)) { |
| 400 | ret = video_sync(dev, true); |
| 401 | if (ret) |
| 402 | dev_dbg(dev, "Video sync failed\n"); |
| 403 | } |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Patrick Delaunay | 2e2e6d8 | 2021-11-15 16:32:20 +0100 | [diff] [blame] | 407 | bool video_is_active(void) |
| 408 | { |
| 409 | struct udevice *dev; |
| 410 | |
| 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 | 1acafc7 | 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 | 9beac5d | 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 | 6a19e93 | 2021-11-19 13:23:51 -0700 | [diff] [blame] | 458 | char str[120]; |
Simon Glass | 9beac5d | 2020-07-02 21:12:20 -0600 | [diff] [blame] | 459 | |
| 460 | snprintf(str, sizeof(str), |
Simon Glass | 6a19e93 | 2021-11-19 13:23:51 -0700 | [diff] [blame] | 461 | "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]", |
Simon Glass | 9beac5d | 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 | 7d70116 | 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 | 9beac5d | 2020-07-02 21:12:20 -0600 | [diff] [blame] | 496 | #endif |
| 497 | |
Simon Glass | 84e63ab | 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 | 0d38901 | 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 | 84e63ab | 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 | c830e28 | 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 | 1acafc7 | 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 | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 535 | struct video_uc_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | 1acafc7 | 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 | 826f35f | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 538 | const char *drv_name = drv; |
Simon Glass | 1acafc7 | 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 | 06696eb | 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 | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 547 | priv->fb_size = priv->line_length * priv->ysize; |
| 548 | |
Simon Glass | 6efa809 | 2020-07-02 21:12:21 -0600 | [diff] [blame] | 549 | if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base) |
| 550 | priv->copy_fb = map_sysmem(plat->copy_base, plat->size); |
| 551 | |
Heinrich Schuchardt | 5c30fbb | 2018-02-08 21:47:11 +0100 | [diff] [blame] | 552 | /* Set up colors */ |
Simon Glass | b9f210a | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 553 | video_set_default_colors(dev, false); |
Rob Clark | 8ef0535 | 2017-08-03 12:47:01 -0400 | [diff] [blame] | 554 | |
| 555 | if (!CONFIG_IS_ENABLED(NO_FB_CLEAR)) |
| 556 | video_clear(dev); |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 557 | |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 558 | /* |
Simon Glass | 826f35f | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 559 | * Create a text console device. For now we always do this, although |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 560 | * it might be useful to support only bitmap drawing on the device |
Simon Glass | 826f35f | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 561 | * for boards that don't need to display text. We create a TrueType |
| 562 | * console if enabled, a rotated console if the video driver requests |
| 563 | * it, otherwise a normal console. |
| 564 | * |
| 565 | * The console can be override by setting vidconsole_drv_name before |
| 566 | * probing this video driver, or in the probe() method. |
| 567 | * |
| 568 | * TrueType does not support rotation at present so fall back to the |
| 569 | * rotated console in that case. |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 570 | */ |
Simon Glass | 826f35f | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 571 | if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) { |
Simon Glass | a29b012 | 2016-01-14 18:10:42 -0700 | [diff] [blame] | 572 | snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name); |
| 573 | strcpy(drv, "vidconsole_tt"); |
| 574 | } else { |
| 575 | snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name, |
| 576 | priv->rot); |
| 577 | snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot); |
| 578 | } |
| 579 | |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 580 | str = strdup(name); |
| 581 | if (!str) |
| 582 | return -ENOMEM; |
Simon Glass | 826f35f | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 583 | if (priv->vidconsole_drv_name) |
| 584 | drv_name = priv->vidconsole_drv_name; |
| 585 | ret = device_bind_driver(dev, drv_name, str, &cons); |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 586 | if (ret) { |
| 587 | debug("%s: Cannot bind console driver\n", __func__); |
| 588 | return ret; |
| 589 | } |
Simon Glass | 826f35f | 2016-01-14 18:10:48 -0700 | [diff] [blame] | 590 | |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 591 | ret = device_probe(cons); |
| 592 | if (ret) { |
| 593 | debug("%s: Cannot probe console driver\n", __func__); |
| 594 | return ret; |
| 595 | } |
| 596 | |
Nikhil M Jain | 86fbee6 | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 597 | if (CONFIG_IS_ENABLED(VIDEO_LOGO) && |
| 598 | !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) { |
Simon Glass | 84e63ab | 2021-11-19 13:24:03 -0700 | [diff] [blame] | 599 | ret = show_splash(dev); |
| 600 | if (ret) { |
| 601 | log_debug("Cannot show splash screen\n"); |
| 602 | return ret; |
| 603 | } |
| 604 | } |
| 605 | |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 606 | return 0; |
| 607 | }; |
| 608 | |
| 609 | /* Post-relocation, allocate memory for the frame buffer */ |
| 610 | static int video_post_bind(struct udevice *dev) |
| 611 | { |
Simon Glass | 7812bbd | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 612 | struct video_uc_priv *uc_priv; |
| 613 | ulong addr; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 614 | ulong size; |
| 615 | |
| 616 | /* Before relocation there is nothing to do here */ |
Tom Rini | 7516418 | 2018-04-22 09:47:48 -0400 | [diff] [blame] | 617 | if (!(gd->flags & GD_FLG_RELOC)) |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 618 | return 0; |
Simon Glass | 7812bbd | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 619 | |
| 620 | /* Set up the video pointer, if this is the first device */ |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 621 | uc_priv = uclass_get_priv(dev->uclass); |
Simon Glass | 7812bbd | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 622 | if (!uc_priv->video_ptr) |
| 623 | uc_priv->video_ptr = gd->video_top; |
| 624 | |
| 625 | /* Allocate framebuffer space for this device */ |
| 626 | addr = uc_priv->video_ptr; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 627 | size = alloc_fb(dev, &addr); |
| 628 | if (addr < gd->video_bottom) { |
Bin Meng | 08ece5b | 2023-07-23 12:40:24 +0800 | [diff] [blame] | 629 | /* |
| 630 | * Device tree node may need the 'bootph-all' or |
Simon Glass | e316fba | 2023-02-13 08:56:34 -0700 | [diff] [blame] | 631 | * 'bootph-some-ram' tag |
Patrick Delaunay | 6998974 | 2019-05-21 19:19:12 +0200 | [diff] [blame] | 632 | */ |
Bin Meng | 08ece5b | 2023-07-23 12:40:24 +0800 | [diff] [blame] | 633 | printf("Video device '%s' cannot allocate frame buffer memory " |
| 634 | "- ensure the device is set up before relocation\n", |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 635 | dev->name); |
| 636 | return -ENOSPC; |
| 637 | } |
| 638 | debug("%s: Claiming %lx bytes at %lx for video device '%s'\n", |
| 639 | __func__, size, addr, dev->name); |
Simon Glass | 7812bbd | 2020-07-02 21:12:32 -0600 | [diff] [blame] | 640 | uc_priv->video_ptr = addr; |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | UCLASS_DRIVER(video) = { |
| 646 | .id = UCLASS_VIDEO, |
| 647 | .name = "video", |
| 648 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 649 | .post_bind = video_post_bind, |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 650 | .post_probe = video_post_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 651 | .priv_auto = sizeof(struct video_uc_priv), |
| 652 | .per_device_auto = sizeof(struct video_priv), |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 653 | .per_device_plat_auto = sizeof(struct video_uc_plat), |
Simon Glass | 1acafc7 | 2016-01-18 19:52:15 -0700 | [diff] [blame] | 654 | }; |