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