blob: 1c372bd58b7b74fa0a882ada1b520ba645460d9c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass623d28f2016-01-18 19:52:15 -07002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glass623d28f2016-01-18 19:52:15 -07004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_VIDEO
7
Nikhil M Jainf7ec5312023-07-18 14:27:31 +05308#include <bloblist.h>
Simon Glass73c9c372020-07-02 21:12:20 -06009#include <console.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glasse0337a52024-07-31 08:44:10 -060011#include <cyclic.h>
Simon Glass623d28f2016-01-18 19:52:15 -070012#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glass623d28f2016-01-18 19:52:15 -070015#include <mapmem.h>
Nikhil M Jainf7ec5312023-07-18 14:27:31 +053016#include <spl.h>
Simon Glass623d28f2016-01-18 19:52:15 -070017#include <stdio_dev.h>
18#include <video.h>
19#include <video_console.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glass623d28f2016-01-18 19:52:15 -070022#include <dm/lists.h>
Michal Simek632e3d42020-12-14 08:47:52 +010023#include <dm/device_compat.h>
Simon Glass623d28f2016-01-18 19:52:15 -070024#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 Glassb75b15b2020-12-03 16:55:23 -070034 * set the @align and @size values in struct video_uc_plat. This
Simon Glass623d28f2016-01-18 19:52:15 -070035 * 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árf204fce2022-03-09 20:46:00 +010038 * method after relocation. Additionally driver can allocate frame buffer
39 * itself by setting plat->base.
Simon Glass623d28f2016-01-18 19:52:15 -070040 *
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 */
54DECLARE_GLOBAL_DATA_PTR;
55
Simon Glasse0337a52024-07-31 08:44:10 -060056struct cyclic_info;
57
Simon Glass951255d2020-07-02 21:12:32 -060058/**
59 * struct video_uc_priv - Information for the video uclass
60 *
61 * @video_ptr: Current allocation position of the video framebuffer pointer.
62 * While binding devices after relocation, this points to the next
63 * available address to use for a device's framebuffer. It starts at
64 * gd->video_top and works downwards, running out of space when it hits
65 * gd->video_bottom.
Simon Glasse0337a52024-07-31 08:44:10 -060066 * @cyc: handle for cyclic-execution function, or NULL if none
Simon Glass951255d2020-07-02 21:12:32 -060067 */
68struct video_uc_priv {
69 ulong video_ptr;
Simon Glasse0337a52024-07-31 08:44:10 -060070 bool cyc_active;
71 struct cyclic_info cyc;
Simon Glass951255d2020-07-02 21:12:32 -060072};
73
Simon Glass2a006332022-10-06 08:36:03 -060074/** struct vid_rgb - Describes a video colour */
75struct vid_rgb {
76 u32 r;
77 u32 g;
78 u32 b;
79};
80
Simon Glass64635382016-01-21 19:44:52 -070081void video_set_flush_dcache(struct udevice *dev, bool flush)
82{
83 struct video_priv *priv = dev_get_uclass_priv(dev);
84
85 priv->flush_dcache = flush;
86}
87
Simon Glassdf865d32023-03-10 12:47:17 -080088static ulong alloc_fb_(ulong align, ulong size, ulong *addrp)
89{
90 ulong base;
91
92 align = align ? align : 1 << 20;
93 base = *addrp - size;
94 base &= ~(align - 1);
95 size = *addrp - base;
96 *addrp = base;
97
98 return size;
99}
100
Simon Glass623d28f2016-01-18 19:52:15 -0700101static ulong alloc_fb(struct udevice *dev, ulong *addrp)
102{
Simon Glassb75b15b2020-12-03 16:55:23 -0700103 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glassdf865d32023-03-10 12:47:17 -0800104 ulong size;
Simon Glass623d28f2016-01-18 19:52:15 -0700105
Simon Glassdf865d32023-03-10 12:47:17 -0800106 if (!plat->size) {
107 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_size) {
108 size = alloc_fb_(plat->align, plat->copy_size, addrp);
109 plat->copy_base = *addrp;
110 return size;
111 }
112
Bin Meng755623d2016-10-09 04:14:17 -0700113 return 0;
Simon Glassdf865d32023-03-10 12:47:17 -0800114 }
Bin Meng755623d2016-10-09 04:14:17 -0700115
Pali Rohárf204fce2022-03-09 20:46:00 +0100116 /* Allow drivers to allocate the frame buffer themselves */
117 if (plat->base)
118 return 0;
119
Simon Glassdf865d32023-03-10 12:47:17 -0800120 size = alloc_fb_(plat->align, plat->size, addrp);
121 plat->base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700122
123 return size;
124}
125
126int video_reserve(ulong *addrp)
127{
128 struct udevice *dev;
129 ulong size;
130
Simon Glassd4dce4a2024-09-29 19:49:36 -0600131 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && xpl_phase() == PHASE_BOARD_F)
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530132 return 0;
133
Simon Glass623d28f2016-01-18 19:52:15 -0700134 gd->video_top = *addrp;
135 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
136 dev;
137 uclass_find_next_device(&dev)) {
138 size = alloc_fb(dev, addrp);
139 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
140 __func__, size, *addrp, dev->name);
141 }
Simon Glassc3d2f352020-07-02 21:12:33 -0600142
143 /* Allocate space for PCI video devices in case there were not bound */
144 if (*addrp == gd->video_top)
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530145 *addrp -= CONFIG_VAL(VIDEO_PCI_DEFAULT_FB_SIZE);
Simon Glassc3d2f352020-07-02 21:12:33 -0600146
Simon Glass623d28f2016-01-18 19:52:15 -0700147 gd->video_bottom = *addrp;
148 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
149 gd->video_top);
150
151 return 0;
152}
153
Simon Glass853e8d22024-08-21 10:18:55 -0600154ulong video_get_fb(void)
155{
156 struct udevice *dev;
157
158 uclass_find_first_device(UCLASS_VIDEO, &dev);
159 if (dev) {
160 const struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
161
162 return uc_plat->base;
163 }
164
165 return 0;
166}
167
Simon Glass062673b2023-06-01 10:22:33 -0600168int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
169 int yend, u32 colour)
170{
171 struct video_priv *priv = dev_get_uclass_priv(dev);
172 void *start, *line;
173 int pixels = xend - xstart;
Alexander Graff88b6a22022-06-10 00:59:21 +0200174 int row, i;
Simon Glass062673b2023-06-01 10:22:33 -0600175
176 start = priv->fb + ystart * priv->line_length;
177 start += xstart * VNBYTES(priv->bpix);
178 line = start;
179 for (row = ystart; row < yend; row++) {
180 switch (priv->bpix) {
181 case VIDEO_BPP8: {
182 u8 *dst = line;
183
184 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
185 for (i = 0; i < pixels; i++)
186 *dst++ = colour;
187 }
188 break;
189 }
190 case VIDEO_BPP16: {
191 u16 *dst = line;
192
193 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
194 for (i = 0; i < pixels; i++)
195 *dst++ = colour;
196 }
197 break;
198 }
199 case VIDEO_BPP32: {
200 u32 *dst = line;
201
202 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
203 for (i = 0; i < pixels; i++)
204 *dst++ = colour;
205 }
206 break;
207 }
208 default:
209 return -ENOSYS;
210 }
211 line += priv->line_length;
212 }
Simon Glass062673b2023-06-01 10:22:33 -0600213
Alexander Graf3b2cc822022-06-10 00:59:16 +0200214 video_damage(dev, xstart, ystart, xend - xstart, yend - ystart);
215
Simon Glass062673b2023-06-01 10:22:33 -0600216 return 0;
217}
218
Nikhil M Jain76833532023-07-18 14:27:30 +0530219int video_reserve_from_bloblist(struct video_handoff *ho)
220{
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530221 if (!ho->fb || ho->size == 0)
222 return -ENOENT;
223
Nikhil M Jain76833532023-07-18 14:27:30 +0530224 gd->video_bottom = ho->fb;
Nikhil M Jain76833532023-07-18 14:27:30 +0530225 gd->video_top = ho->fb + ho->size;
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530226 debug("%s: Reserving %lx bytes at %08x as per bloblist received\n",
227 __func__, (unsigned long)ho->size, (u32)ho->fb);
Nikhil M Jain76833532023-07-18 14:27:30 +0530228
229 return 0;
230}
231
Simon Glass2baa6f82022-10-06 08:36:08 -0600232int video_fill(struct udevice *dev, u32 colour)
Simon Glass623d28f2016-01-18 19:52:15 -0700233{
234 struct video_priv *priv = dev_get_uclass_priv(dev);
235
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100236 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700237 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530238 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700239 u16 *ppix = priv->fb;
240 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100241
Simon Glass05c17d62019-12-20 18:10:37 -0700242 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600243 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700244 break;
245 }
Andre Przywara0cff09f2025-03-27 15:33:05 +0000246 fallthrough;
Simon Glass05c17d62019-12-20 18:10:37 -0700247 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530248 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700249 u32 *ppix = priv->fb;
250 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700251
Simon Glass05c17d62019-12-20 18:10:37 -0700252 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600253 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700254 break;
255 }
Andre Przywara0cff09f2025-03-27 15:33:05 +0000256 fallthrough;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100257 default:
Simon Glass2baa6f82022-10-06 08:36:08 -0600258 memset(priv->fb, colour, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100259 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700260 }
Simon Glass55343122018-10-01 12:22:26 -0600261
Alexander Graf3b2cc822022-06-10 00:59:16 +0200262 video_damage(dev, 0, 0, priv->xsize, priv->ysize);
263
Michal Simeka1e136d2020-12-15 15:12:09 +0100264 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700265}
266
Simon Glass2baa6f82022-10-06 08:36:08 -0600267int video_clear(struct udevice *dev)
268{
269 struct video_priv *priv = dev_get_uclass_priv(dev);
270 int ret;
271
272 ret = video_fill(dev, priv->colour_bg);
273 if (ret)
274 return ret;
275
276 return 0;
277}
278
Simon Glass2a006332022-10-06 08:36:03 -0600279static const struct vid_rgb colours[VID_COLOUR_COUNT] = {
280 { 0x00, 0x00, 0x00 }, /* black */
281 { 0xc0, 0x00, 0x00 }, /* red */
282 { 0x00, 0xc0, 0x00 }, /* green */
283 { 0xc0, 0x60, 0x00 }, /* brown */
284 { 0x00, 0x00, 0xc0 }, /* blue */
285 { 0xc0, 0x00, 0xc0 }, /* magenta */
286 { 0x00, 0xc0, 0xc0 }, /* cyan */
287 { 0xc0, 0xc0, 0xc0 }, /* light gray */
288 { 0x80, 0x80, 0x80 }, /* gray */
289 { 0xff, 0x00, 0x00 }, /* bright red */
290 { 0x00, 0xff, 0x00 }, /* bright green */
291 { 0xff, 0xff, 0x00 }, /* yellow */
292 { 0x00, 0x00, 0xff }, /* bright blue */
293 { 0xff, 0x00, 0xff }, /* bright magenta */
294 { 0x00, 0xff, 0xff }, /* bright cyan */
295 { 0xff, 0xff, 0xff }, /* white */
Simon Glassbda3adc2024-10-14 16:31:53 -0600296
297 /* an extra one for menus */
298 { 0x40, 0x40, 0x40 }, /* dark gray */
Simon Glass2a006332022-10-06 08:36:03 -0600299};
300
Simon Glassd17a6242023-06-01 10:22:48 -0600301u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx)
Simon Glass2a006332022-10-06 08:36:03 -0600302{
303 switch (priv->bpix) {
304 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530305 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass2a006332022-10-06 08:36:03 -0600306 return ((colours[idx].r >> 3) << 11) |
307 ((colours[idx].g >> 2) << 5) |
308 ((colours[idx].b >> 3) << 0);
309 }
310 break;
311 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530312 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Michal Simek985eac82023-05-17 10:42:07 +0200313 switch (priv->format) {
314 case VIDEO_X2R10G10B10:
Simon Glass2a006332022-10-06 08:36:03 -0600315 return (colours[idx].r << 22) |
316 (colours[idx].g << 12) |
317 (colours[idx].b << 2);
Michal Simek985eac82023-05-17 10:42:07 +0200318 case VIDEO_RGBA8888:
319 return (colours[idx].r << 24) |
320 (colours[idx].g << 16) |
321 (colours[idx].b << 8) | 0xff;
322 default:
Simon Glass2a006332022-10-06 08:36:03 -0600323 return (colours[idx].r << 16) |
324 (colours[idx].g << 8) |
325 (colours[idx].b << 0);
Michal Simek985eac82023-05-17 10:42:07 +0200326 }
Simon Glass2a006332022-10-06 08:36:03 -0600327 }
328 break;
329 default:
330 break;
331 }
332
333 /*
334 * For unknown bit arrangements just support
335 * black and white.
336 */
337 if (idx)
338 return 0xffffff; /* white */
339
340 return 0x000000; /* black */
341}
342
Simon Glass2b063b82018-11-06 15:21:36 -0700343void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100344{
Simon Glass2b063b82018-11-06 15:21:36 -0700345 struct video_priv *priv = dev_get_uclass_priv(dev);
346 int fore, back;
347
Simon Glass21320da2025-04-02 06:29:33 +1300348 if (priv->white_on_black) {
Simon Glass05c17d62019-12-20 18:10:37 -0700349 /* White is used when switching to bold, use light gray here */
350 fore = VID_LIGHT_GRAY;
351 back = VID_BLACK;
352 } else {
353 fore = VID_BLACK;
354 back = VID_WHITE;
355 }
Simon Glass2b063b82018-11-06 15:21:36 -0700356 if (invert) {
357 int temp;
358
359 temp = fore;
360 fore = back;
361 back = temp;
362 }
363 priv->fg_col_idx = fore;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000364 priv->bg_col_idx = back;
Simon Glass2a006332022-10-06 08:36:03 -0600365 priv->colour_fg = video_index_to_colour(priv, fore);
366 priv->colour_bg = video_index_to_colour(priv, back);
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100367}
368
Alexander Grafdb757752022-06-10 00:59:15 +0200369/* Notify about changes in the frame buffer */
370#ifdef CONFIG_VIDEO_DAMAGE
371void video_damage(struct udevice *vid, int x, int y, int width, int height)
372{
373 struct video_priv *priv = dev_get_uclass_priv(vid);
374 int xend = x + width;
375 int yend = y + height;
376
377 if (x > priv->xsize)
378 return;
379
380 if (y > priv->ysize)
381 return;
382
383 if (xend > priv->xsize)
384 xend = priv->xsize;
385
386 if (yend > priv->ysize)
387 yend = priv->ysize;
388
389 /* Span a rectangle across all old and new damage */
390 priv->damage.xstart = min(x, priv->damage.xstart);
391 priv->damage.ystart = min(y, priv->damage.ystart);
392 priv->damage.xend = max(xend, priv->damage.xend);
393 priv->damage.yend = max(yend, priv->damage.yend);
394}
395#endif
396
Alexander Graf2b978f22022-06-10 00:59:20 +0200397static void video_flush_dcache(struct udevice *vid, bool use_copy)
398{
399 struct video_priv *priv = dev_get_uclass_priv(vid);
400 ulong fb = use_copy ? (ulong)priv->copy_fb : (ulong)priv->fb;
Alexander Grafa4b815e2023-01-03 22:50:03 +0100401 uint cacheline_size = 32;
402
403#ifdef CONFIG_SYS_CACHELINE_SIZE
404 cacheline_size = CONFIG_SYS_CACHELINE_SIZE;
405#endif
406
407 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
408 return;
Alexander Graf2b978f22022-06-10 00:59:20 +0200409
410 if (!priv->flush_dcache)
411 return;
412
413 if (!IS_ENABLED(CONFIG_VIDEO_DAMAGE)) {
414 flush_dcache_range(fb, ALIGN(fb + priv->fb_size,
Alexander Grafa4b815e2023-01-03 22:50:03 +0100415 cacheline_size));
Alexander Graf2b978f22022-06-10 00:59:20 +0200416
417 return;
418 }
419
420 if (priv->damage.xend && priv->damage.yend) {
421 int lstart = priv->damage.xstart * VNBYTES(priv->bpix);
422 int lend = priv->damage.xend * VNBYTES(priv->bpix);
423 int y;
424
425 for (y = priv->damage.ystart; y < priv->damage.yend; y++) {
426 ulong start = fb + (y * priv->line_length) + lstart;
427 ulong end = start + lend - lstart;
428
Alexander Grafa4b815e2023-01-03 22:50:03 +0100429 start = ALIGN_DOWN(start, cacheline_size);
430 end = ALIGN(end, cacheline_size);
Alexander Graf2b978f22022-06-10 00:59:20 +0200431
432 flush_dcache_range(start, end);
433 }
434 }
435}
Alexander Graf2b978f22022-06-10 00:59:20 +0200436
Alexander Graff88b6a22022-06-10 00:59:21 +0200437static void video_flush_copy(struct udevice *vid)
438{
439 struct video_priv *priv = dev_get_uclass_priv(vid);
440
441 if (!priv->copy_fb)
442 return;
443
444 if (priv->damage.xend && priv->damage.yend) {
445 int lstart = priv->damage.xstart * VNBYTES(priv->bpix);
446 int lend = priv->damage.xend * VNBYTES(priv->bpix);
447 int y;
448
449 for (y = priv->damage.ystart; y < priv->damage.yend; y++) {
450 ulong offset = (y * priv->line_length) + lstart;
451 ulong len = lend - lstart;
452
453 memcpy(priv->copy_fb + offset, priv->fb + offset, len);
454 }
455 }
456}
457
Simon Glass623d28f2016-01-18 19:52:15 -0700458/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100459int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700460{
Simon Glass7f6280f2024-07-31 08:44:09 -0600461 struct video_priv *priv = dev_get_uclass_priv(vid);
Michal Simek8ae95df2020-12-03 09:30:00 +0100462 struct video_ops *ops = video_get_ops(vid);
463 int ret;
464
Alexander Graff88b6a22022-06-10 00:59:21 +0200465 if (IS_ENABLED(CONFIG_VIDEO_COPY))
466 video_flush_copy(vid);
467
Michal Simek8ae95df2020-12-03 09:30:00 +0100468 if (ops && ops->video_sync) {
469 ret = ops->video_sync(vid);
470 if (ret)
471 return ret;
472 }
473
Simon Glasse0337a52024-07-31 08:44:10 -0600474 if (CONFIG_IS_ENABLED(CYCLIC) && !force &&
475 get_timer(priv->last_sync) < CONFIG_VIDEO_SYNC_MS)
476 return 0;
477
Alexander Graf2b978f22022-06-10 00:59:20 +0200478 video_flush_dcache(vid, false);
479
480 if (IS_ENABLED(CONFIG_VIDEO_COPY))
481 video_flush_dcache(vid, true);
Alexander Grafa4b815e2023-01-03 22:50:03 +0100482
483#if defined(CONFIG_VIDEO_SANDBOX_SDL)
Simon Glasse0337a52024-07-31 08:44:10 -0600484 sandbox_sdl_sync(priv->fb);
Simon Glass623d28f2016-01-18 19:52:15 -0700485#endif
Simon Glasse0337a52024-07-31 08:44:10 -0600486 priv->last_sync = get_timer(0);
487
Alexander Grafdb757752022-06-10 00:59:15 +0200488 if (IS_ENABLED(CONFIG_VIDEO_DAMAGE)) {
489 priv->damage.xstart = priv->xsize;
490 priv->damage.ystart = priv->ysize;
491 priv->damage.xend = 0;
492 priv->damage.yend = 0;
493 }
494
Michal Simek632e3d42020-12-14 08:47:52 +0100495 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700496}
497
498void video_sync_all(void)
499{
500 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100501 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700502
503 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
504 dev;
505 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100506 if (device_active(dev)) {
507 ret = video_sync(dev, true);
508 if (ret)
509 dev_dbg(dev, "Video sync failed\n");
510 }
Simon Glass623d28f2016-01-18 19:52:15 -0700511 }
512}
513
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100514bool video_is_active(void)
515{
516 struct udevice *dev;
517
Devarsh Thakkar42e41062024-02-22 18:38:09 +0530518 /* Assume video to be active if SPL passed video hand-off to U-boot */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600519 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && xpl_phase() > PHASE_SPL)
Devarsh Thakkar42e41062024-02-22 18:38:09 +0530520 return true;
521
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100522 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
523 dev;
524 uclass_find_next_device(&dev)) {
525 if (device_active(dev))
526 return true;
527 }
528
529 return false;
530}
531
Simon Glass623d28f2016-01-18 19:52:15 -0700532int video_get_xsize(struct udevice *dev)
533{
534 struct video_priv *priv = dev_get_uclass_priv(dev);
535
536 return priv->xsize;
537}
538
539int video_get_ysize(struct udevice *dev)
540{
541 struct video_priv *priv = dev_get_uclass_priv(dev);
542
543 return priv->ysize;
544}
545
Simon Glass87a3cd72021-11-19 13:24:03 -0700546#define SPLASH_DECL(_name) \
547 extern u8 __splash_ ## _name ## _begin[]; \
548 extern u8 __splash_ ## _name ## _end[]
549
550#define SPLASH_START(_name) __splash_ ## _name ## _begin
551
552SPLASH_DECL(u_boot_logo);
553
Simon Glass22477422022-10-06 08:36:09 -0600554void *video_get_u_boot_logo(void)
555{
556 return SPLASH_START(u_boot_logo);
557}
558
Simon Glass87a3cd72021-11-19 13:24:03 -0700559static int show_splash(struct udevice *dev)
560{
561 u8 *data = SPLASH_START(u_boot_logo);
562 int ret;
563
564 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
565
566 return 0;
567}
568
Simon Glass70459902022-10-06 08:36:18 -0600569int video_default_font_height(struct udevice *dev)
570{
571 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
572
573 if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
574 return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
575 CONFIG_CONSOLE_TRUETYPE_SIZE);
576
577 return vc_priv->y_charsize;
578}
579
Simon Glasse0337a52024-07-31 08:44:10 -0600580static void video_idle(struct cyclic_info *cyc)
581{
582 video_sync_all();
583}
584
Simon Glass21320da2025-04-02 06:29:33 +1300585void video_set_white_on_black(struct udevice *dev, bool white_on_black)
586{
587 struct video_priv *priv = dev_get_uclass_priv(dev);
588
589 if (priv->white_on_black != white_on_black) {
590 priv->white_on_black = white_on_black;
591 video_set_default_colors(dev, false);
592
593 video_clear(dev);
594 }
595}
596
Simon Glass623d28f2016-01-18 19:52:15 -0700597/* Set up the display ready for use */
598static int video_post_probe(struct udevice *dev)
599{
Simon Glassb75b15b2020-12-03 16:55:23 -0700600 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glasse0337a52024-07-31 08:44:10 -0600601 struct video_uc_priv *uc_priv = uclass_get_priv(dev->uclass);
Simon Glass623d28f2016-01-18 19:52:15 -0700602 struct video_priv *priv = dev_get_uclass_priv(dev);
603 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700604 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700605 struct udevice *cons;
606 int ret;
607
608 /* Set up the line and display size */
609 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700610 if (!priv->line_length)
611 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
612
Simon Glass623d28f2016-01-18 19:52:15 -0700613 priv->fb_size = priv->line_length * priv->ysize;
614
Devarsh Thakkarf5254cc2023-12-05 21:25:21 +0530615 /*
616 * Set up video handoff fields for passing video blob to next stage
617 * NOTE:
618 * This assumes that reserved video memory only uses a single framebuffer
619 */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600620 if (xpl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(BLOBLIST)) {
Devarsh Thakkarf5254cc2023-12-05 21:25:21 +0530621 struct video_handoff *ho;
622
623 ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0);
624 if (!ho)
625 return log_msg_ret("blf", -ENOENT);
626 ho->fb = gd->video_bottom;
627 /* Fill aligned size here as calculated in video_reserve() */
628 ho->size = gd->video_top - gd->video_bottom;
629 ho->xsize = priv->xsize;
630 ho->ysize = priv->ysize;
631 ho->line_length = priv->line_length;
632 ho->bpix = priv->bpix;
Simon Glass30aaa9b2025-01-10 17:00:19 -0700633 ho->format = priv->format;
Devarsh Thakkarf5254cc2023-12-05 21:25:21 +0530634 }
635
Simon Glassb4928e52020-07-02 21:12:21 -0600636 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
637 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
638
Simon Glass21320da2025-04-02 06:29:33 +1300639 priv->white_on_black = CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK);
640
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100641 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700642 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400643
644 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
645 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700646
Simon Glass84c7fb32016-01-18 19:52:17 -0700647 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700648 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700649 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700650 * for boards that don't need to display text. We create a TrueType
651 * console if enabled, a rotated console if the video driver requests
652 * it, otherwise a normal console.
653 *
654 * The console can be override by setting vidconsole_drv_name before
655 * probing this video driver, or in the probe() method.
656 *
657 * TrueType does not support rotation at present so fall back to the
658 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700659 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700660 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700661 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
662 strcpy(drv, "vidconsole_tt");
663 } else {
664 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
665 priv->rot);
666 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
667 }
668
Simon Glass84c7fb32016-01-18 19:52:17 -0700669 str = strdup(name);
670 if (!str)
671 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700672 if (priv->vidconsole_drv_name)
673 drv_name = priv->vidconsole_drv_name;
674 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700675 if (ret) {
676 debug("%s: Cannot bind console driver\n", __func__);
677 return ret;
678 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700679
Simon Glass84c7fb32016-01-18 19:52:17 -0700680 ret = device_probe(cons);
681 if (ret) {
682 debug("%s: Cannot probe console driver\n", __func__);
683 return ret;
684 }
685
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530686 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
687 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass87a3cd72021-11-19 13:24:03 -0700688 ret = show_splash(dev);
689 if (ret) {
690 log_debug("Cannot show splash screen\n");
691 return ret;
692 }
693 }
694
Simon Glasse0337a52024-07-31 08:44:10 -0600695 /* register cyclic as soon as the first video device is probed */
696 if (CONFIG_IS_ENABLED(CYCLIC) && (gd->flags && GD_FLG_RELOC) &&
697 !uc_priv->cyc_active) {
698 uint ms = CONFIG_IF_ENABLED_INT(CYCLIC, VIDEO_SYNC_CYCLIC_MS);
699
700 cyclic_register(&uc_priv->cyc, video_idle, ms * 1000,
701 "video_init");
702 uc_priv->cyc_active = true;
703 }
704
Simon Glass623d28f2016-01-18 19:52:15 -0700705 return 0;
706};
707
708/* Post-relocation, allocate memory for the frame buffer */
709static int video_post_bind(struct udevice *dev)
710{
Simon Glass951255d2020-07-02 21:12:32 -0600711 struct video_uc_priv *uc_priv;
712 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700713 ulong size;
714
715 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400716 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700717 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600718
719 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700720 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600721 if (!uc_priv->video_ptr)
722 uc_priv->video_ptr = gd->video_top;
723
724 /* Allocate framebuffer space for this device */
725 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700726 size = alloc_fb(dev, &addr);
727 if (addr < gd->video_bottom) {
Bin Mengacbafdd2023-07-23 12:40:24 +0800728 /*
729 * Device tree node may need the 'bootph-all' or
Simon Glassfc1aa352023-02-13 08:56:34 -0700730 * 'bootph-some-ram' tag
Patrick Delaunayd1937182019-05-21 19:19:12 +0200731 */
Bin Mengacbafdd2023-07-23 12:40:24 +0800732 printf("Video device '%s' cannot allocate frame buffer memory "
733 "- ensure the device is set up before relocation\n",
Simon Glass623d28f2016-01-18 19:52:15 -0700734 dev->name);
735 return -ENOSPC;
736 }
737 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
738 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600739 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700740
741 return 0;
742}
743
Simon Glasse0337a52024-07-31 08:44:10 -0600744__maybe_unused static int video_destroy(struct uclass *uc)
745{
746 struct video_uc_priv *uc_priv = uclass_get_priv(uc);
747
748 if (uc_priv->cyc_active) {
749 cyclic_unregister(&uc_priv->cyc);
750 uc_priv->cyc_active = false;
751 }
752
753 return 0;
754}
755
Simon Glass623d28f2016-01-18 19:52:15 -0700756UCLASS_DRIVER(video) = {
757 .id = UCLASS_VIDEO,
758 .name = "video",
759 .flags = DM_UC_FLAG_SEQ_ALIAS,
760 .post_bind = video_post_bind,
Simon Glass623d28f2016-01-18 19:52:15 -0700761 .post_probe = video_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700762 .priv_auto = sizeof(struct video_uc_priv),
763 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700764 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glasse0337a52024-07-31 08:44:10 -0600765 CONFIG_IS_ENABLED(CYCLIC, (.destroy = video_destroy, ))
Simon Glass623d28f2016-01-18 19:52:15 -0700766};