blob: a5aa8dd52954af81df9adb44111ccd8b523d0e64 [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
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530131 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() == PHASE_BOARD_F)
132 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;
Simon Glassb2b29612020-05-10 14:17:01 -0600148 gd->fb_base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700149 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
150 gd->video_top);
151
152 return 0;
153}
154
Simon Glass062673b2023-06-01 10:22:33 -0600155int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
156 int yend, u32 colour)
157{
158 struct video_priv *priv = dev_get_uclass_priv(dev);
159 void *start, *line;
160 int pixels = xend - xstart;
161 int row, i, ret;
162
163 start = priv->fb + ystart * priv->line_length;
164 start += xstart * VNBYTES(priv->bpix);
165 line = start;
166 for (row = ystart; row < yend; row++) {
167 switch (priv->bpix) {
168 case VIDEO_BPP8: {
169 u8 *dst = line;
170
171 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
172 for (i = 0; i < pixels; i++)
173 *dst++ = colour;
174 }
175 break;
176 }
177 case VIDEO_BPP16: {
178 u16 *dst = line;
179
180 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
181 for (i = 0; i < pixels; i++)
182 *dst++ = colour;
183 }
184 break;
185 }
186 case VIDEO_BPP32: {
187 u32 *dst = line;
188
189 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
190 for (i = 0; i < pixels; i++)
191 *dst++ = colour;
192 }
193 break;
194 }
195 default:
196 return -ENOSYS;
197 }
198 line += priv->line_length;
199 }
200 ret = video_sync_copy(dev, start, line);
201 if (ret)
202 return ret;
203
204 return 0;
205}
206
Nikhil M Jain76833532023-07-18 14:27:30 +0530207int video_reserve_from_bloblist(struct video_handoff *ho)
208{
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530209 if (!ho->fb || ho->size == 0)
210 return -ENOENT;
211
Nikhil M Jain76833532023-07-18 14:27:30 +0530212 gd->video_bottom = ho->fb;
213 gd->fb_base = ho->fb;
214 gd->video_top = ho->fb + ho->size;
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530215 debug("%s: Reserving %lx bytes at %08x as per bloblist received\n",
216 __func__, (unsigned long)ho->size, (u32)ho->fb);
Nikhil M Jain76833532023-07-18 14:27:30 +0530217
218 return 0;
219}
220
Simon Glass2baa6f82022-10-06 08:36:08 -0600221int video_fill(struct udevice *dev, u32 colour)
Simon Glass623d28f2016-01-18 19:52:15 -0700222{
223 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf8ec6212020-07-02 21:12:22 -0600224 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700225
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100226 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700227 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530228 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700229 u16 *ppix = priv->fb;
230 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100231
Simon Glass05c17d62019-12-20 18:10:37 -0700232 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600233 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700234 break;
235 }
236 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530237 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700238 u32 *ppix = priv->fb;
239 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700240
Simon Glass05c17d62019-12-20 18:10:37 -0700241 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600242 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700243 break;
244 }
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100245 default:
Simon Glass2baa6f82022-10-06 08:36:08 -0600246 memset(priv->fb, colour, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100247 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700248 }
Simon Glassf8ec6212020-07-02 21:12:22 -0600249 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
250 if (ret)
251 return ret;
Simon Glass55343122018-10-01 12:22:26 -0600252
Michal Simeka1e136d2020-12-15 15:12:09 +0100253 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700254}
255
Simon Glass2baa6f82022-10-06 08:36:08 -0600256int video_clear(struct udevice *dev)
257{
258 struct video_priv *priv = dev_get_uclass_priv(dev);
259 int ret;
260
261 ret = video_fill(dev, priv->colour_bg);
262 if (ret)
263 return ret;
264
265 return 0;
266}
267
Simon Glass2a006332022-10-06 08:36:03 -0600268static const struct vid_rgb colours[VID_COLOUR_COUNT] = {
269 { 0x00, 0x00, 0x00 }, /* black */
270 { 0xc0, 0x00, 0x00 }, /* red */
271 { 0x00, 0xc0, 0x00 }, /* green */
272 { 0xc0, 0x60, 0x00 }, /* brown */
273 { 0x00, 0x00, 0xc0 }, /* blue */
274 { 0xc0, 0x00, 0xc0 }, /* magenta */
275 { 0x00, 0xc0, 0xc0 }, /* cyan */
276 { 0xc0, 0xc0, 0xc0 }, /* light gray */
277 { 0x80, 0x80, 0x80 }, /* gray */
278 { 0xff, 0x00, 0x00 }, /* bright red */
279 { 0x00, 0xff, 0x00 }, /* bright green */
280 { 0xff, 0xff, 0x00 }, /* yellow */
281 { 0x00, 0x00, 0xff }, /* bright blue */
282 { 0xff, 0x00, 0xff }, /* bright magenta */
283 { 0x00, 0xff, 0xff }, /* bright cyan */
284 { 0xff, 0xff, 0xff }, /* white */
285};
286
Simon Glassd17a6242023-06-01 10:22:48 -0600287u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx)
Simon Glass2a006332022-10-06 08:36:03 -0600288{
289 switch (priv->bpix) {
290 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530291 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass2a006332022-10-06 08:36:03 -0600292 return ((colours[idx].r >> 3) << 11) |
293 ((colours[idx].g >> 2) << 5) |
294 ((colours[idx].b >> 3) << 0);
295 }
296 break;
297 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530298 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Michal Simek985eac82023-05-17 10:42:07 +0200299 switch (priv->format) {
300 case VIDEO_X2R10G10B10:
Simon Glass2a006332022-10-06 08:36:03 -0600301 return (colours[idx].r << 22) |
302 (colours[idx].g << 12) |
303 (colours[idx].b << 2);
Michal Simek985eac82023-05-17 10:42:07 +0200304 case VIDEO_RGBA8888:
305 return (colours[idx].r << 24) |
306 (colours[idx].g << 16) |
307 (colours[idx].b << 8) | 0xff;
308 default:
Simon Glass2a006332022-10-06 08:36:03 -0600309 return (colours[idx].r << 16) |
310 (colours[idx].g << 8) |
311 (colours[idx].b << 0);
Michal Simek985eac82023-05-17 10:42:07 +0200312 }
Simon Glass2a006332022-10-06 08:36:03 -0600313 }
314 break;
315 default:
316 break;
317 }
318
319 /*
320 * For unknown bit arrangements just support
321 * black and white.
322 */
323 if (idx)
324 return 0xffffff; /* white */
325
326 return 0x000000; /* black */
327}
328
Simon Glass2b063b82018-11-06 15:21:36 -0700329void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100330{
Simon Glass2b063b82018-11-06 15:21:36 -0700331 struct video_priv *priv = dev_get_uclass_priv(dev);
332 int fore, back;
333
Simon Glass05c17d62019-12-20 18:10:37 -0700334 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
335 /* White is used when switching to bold, use light gray here */
336 fore = VID_LIGHT_GRAY;
337 back = VID_BLACK;
338 } else {
339 fore = VID_BLACK;
340 back = VID_WHITE;
341 }
Simon Glass2b063b82018-11-06 15:21:36 -0700342 if (invert) {
343 int temp;
344
345 temp = fore;
346 fore = back;
347 back = temp;
348 }
349 priv->fg_col_idx = fore;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000350 priv->bg_col_idx = back;
Simon Glass2a006332022-10-06 08:36:03 -0600351 priv->colour_fg = video_index_to_colour(priv, fore);
352 priv->colour_bg = video_index_to_colour(priv, back);
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100353}
354
Simon Glass623d28f2016-01-18 19:52:15 -0700355/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100356int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700357{
Simon Glass7f6280f2024-07-31 08:44:09 -0600358 struct video_priv *priv = dev_get_uclass_priv(vid);
Michal Simek8ae95df2020-12-03 09:30:00 +0100359 struct video_ops *ops = video_get_ops(vid);
360 int ret;
361
362 if (ops && ops->video_sync) {
363 ret = ops->video_sync(vid);
364 if (ret)
365 return ret;
366 }
367
Simon Glasse0337a52024-07-31 08:44:10 -0600368 if (CONFIG_IS_ENABLED(CYCLIC) && !force &&
369 get_timer(priv->last_sync) < CONFIG_VIDEO_SYNC_MS)
370 return 0;
371
Simon Glass623d28f2016-01-18 19:52:15 -0700372 /*
373 * flush_dcache_range() is declared in common.h but it seems that some
374 * architectures do not actually implement it. Is there a way to find
375 * out whether it exists? For now, ARM is safe.
376 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400377#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass623d28f2016-01-18 19:52:15 -0700378 if (priv->flush_dcache) {
379 flush_dcache_range((ulong)priv->fb,
Simon Glasseebc3fd2016-11-13 14:22:06 -0700380 ALIGN((ulong)priv->fb + priv->fb_size,
381 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass623d28f2016-01-18 19:52:15 -0700382 }
383#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
Simon Glasse0337a52024-07-31 08:44:10 -0600384 sandbox_sdl_sync(priv->fb);
Simon Glass623d28f2016-01-18 19:52:15 -0700385#endif
Simon Glasse0337a52024-07-31 08:44:10 -0600386 priv->last_sync = get_timer(0);
387
Michal Simek632e3d42020-12-14 08:47:52 +0100388 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700389}
390
391void video_sync_all(void)
392{
393 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100394 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700395
396 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
397 dev;
398 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100399 if (device_active(dev)) {
400 ret = video_sync(dev, true);
401 if (ret)
402 dev_dbg(dev, "Video sync failed\n");
403 }
Simon Glass623d28f2016-01-18 19:52:15 -0700404 }
405}
406
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100407bool video_is_active(void)
408{
409 struct udevice *dev;
410
Devarsh Thakkar42e41062024-02-22 18:38:09 +0530411 /* Assume video to be active if SPL passed video hand-off to U-boot */
412 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() > PHASE_SPL)
413 return true;
414
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100415 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
416 dev;
417 uclass_find_next_device(&dev)) {
418 if (device_active(dev))
419 return true;
420 }
421
422 return false;
423}
424
Simon Glass623d28f2016-01-18 19:52:15 -0700425int video_get_xsize(struct udevice *dev)
426{
427 struct video_priv *priv = dev_get_uclass_priv(dev);
428
429 return priv->xsize;
430}
431
432int video_get_ysize(struct udevice *dev)
433{
434 struct video_priv *priv = dev_get_uclass_priv(dev);
435
436 return priv->ysize;
437}
438
Simon Glass73c9c372020-07-02 21:12:20 -0600439#ifdef CONFIG_VIDEO_COPY
440int video_sync_copy(struct udevice *dev, void *from, void *to)
441{
442 struct video_priv *priv = dev_get_uclass_priv(dev);
443
444 if (priv->copy_fb) {
445 long offset, size;
446
447 /* Find the offset of the first byte to copy */
448 if ((ulong)to > (ulong)from) {
449 size = to - from;
450 offset = from - priv->fb;
451 } else {
452 size = from - to;
453 offset = to - priv->fb;
454 }
455
456 /*
457 * Allow a bit of leeway for valid requests somewhere near the
458 * frame buffer
459 */
460 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
461#ifdef DEBUG
Simon Glass78cdc5d2021-11-19 13:23:51 -0700462 char str[120];
Simon Glass73c9c372020-07-02 21:12:20 -0600463
464 snprintf(str, sizeof(str),
Simon Glass78cdc5d2021-11-19 13:23:51 -0700465 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
Simon Glass73c9c372020-07-02 21:12:20 -0600466 priv->fb, from, to, offset);
467 console_puts_select_stderr(true, str);
468#endif
469 return -EFAULT;
470 }
471
472 /*
473 * Silently crop the memcpy. This allows callers to avoid doing
474 * this themselves. It is common for the end pointer to go a
475 * few lines after the end of the frame buffer, since most of
476 * the update algorithms terminate a line after their last write
477 */
478 if (offset + size > priv->fb_size) {
479 size = priv->fb_size - offset;
480 } else if (offset < 0) {
481 size += offset;
482 offset = 0;
483 }
484
485 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
486 }
487
488 return 0;
489}
Simon Glass62b535e2021-01-13 20:29:46 -0700490
491int video_sync_copy_all(struct udevice *dev)
492{
493 struct video_priv *priv = dev_get_uclass_priv(dev);
494
495 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
496
497 return 0;
498}
499
Simon Glass73c9c372020-07-02 21:12:20 -0600500#endif
501
Simon Glass87a3cd72021-11-19 13:24:03 -0700502#define SPLASH_DECL(_name) \
503 extern u8 __splash_ ## _name ## _begin[]; \
504 extern u8 __splash_ ## _name ## _end[]
505
506#define SPLASH_START(_name) __splash_ ## _name ## _begin
507
508SPLASH_DECL(u_boot_logo);
509
Simon Glass22477422022-10-06 08:36:09 -0600510void *video_get_u_boot_logo(void)
511{
512 return SPLASH_START(u_boot_logo);
513}
514
Simon Glass87a3cd72021-11-19 13:24:03 -0700515static int show_splash(struct udevice *dev)
516{
517 u8 *data = SPLASH_START(u_boot_logo);
518 int ret;
519
520 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
521
522 return 0;
523}
524
Simon Glass70459902022-10-06 08:36:18 -0600525int video_default_font_height(struct udevice *dev)
526{
527 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
528
529 if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
530 return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
531 CONFIG_CONSOLE_TRUETYPE_SIZE);
532
533 return vc_priv->y_charsize;
534}
535
Simon Glasse0337a52024-07-31 08:44:10 -0600536static void video_idle(struct cyclic_info *cyc)
537{
538 video_sync_all();
539}
540
Simon Glass623d28f2016-01-18 19:52:15 -0700541/* Set up the display ready for use */
542static int video_post_probe(struct udevice *dev)
543{
Simon Glassb75b15b2020-12-03 16:55:23 -0700544 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glasse0337a52024-07-31 08:44:10 -0600545 struct video_uc_priv *uc_priv = uclass_get_priv(dev->uclass);
Simon Glass623d28f2016-01-18 19:52:15 -0700546 struct video_priv *priv = dev_get_uclass_priv(dev);
547 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700548 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700549 struct udevice *cons;
550 int ret;
551
552 /* Set up the line and display size */
553 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700554 if (!priv->line_length)
555 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
556
Simon Glass623d28f2016-01-18 19:52:15 -0700557 priv->fb_size = priv->line_length * priv->ysize;
558
Devarsh Thakkarf5254cc2023-12-05 21:25:21 +0530559 /*
560 * Set up video handoff fields for passing video blob to next stage
561 * NOTE:
562 * This assumes that reserved video memory only uses a single framebuffer
563 */
564 if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(BLOBLIST)) {
565 struct video_handoff *ho;
566
567 ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0);
568 if (!ho)
569 return log_msg_ret("blf", -ENOENT);
570 ho->fb = gd->video_bottom;
571 /* Fill aligned size here as calculated in video_reserve() */
572 ho->size = gd->video_top - gd->video_bottom;
573 ho->xsize = priv->xsize;
574 ho->ysize = priv->ysize;
575 ho->line_length = priv->line_length;
576 ho->bpix = priv->bpix;
577 }
578
Simon Glassb4928e52020-07-02 21:12:21 -0600579 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
580 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
581
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100582 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700583 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400584
585 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
586 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700587
Simon Glass84c7fb32016-01-18 19:52:17 -0700588 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700589 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700590 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700591 * for boards that don't need to display text. We create a TrueType
592 * console if enabled, a rotated console if the video driver requests
593 * it, otherwise a normal console.
594 *
595 * The console can be override by setting vidconsole_drv_name before
596 * probing this video driver, or in the probe() method.
597 *
598 * TrueType does not support rotation at present so fall back to the
599 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700600 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700601 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700602 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
603 strcpy(drv, "vidconsole_tt");
604 } else {
605 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
606 priv->rot);
607 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
608 }
609
Simon Glass84c7fb32016-01-18 19:52:17 -0700610 str = strdup(name);
611 if (!str)
612 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700613 if (priv->vidconsole_drv_name)
614 drv_name = priv->vidconsole_drv_name;
615 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700616 if (ret) {
617 debug("%s: Cannot bind console driver\n", __func__);
618 return ret;
619 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700620
Simon Glass84c7fb32016-01-18 19:52:17 -0700621 ret = device_probe(cons);
622 if (ret) {
623 debug("%s: Cannot probe console driver\n", __func__);
624 return ret;
625 }
626
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530627 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
628 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass87a3cd72021-11-19 13:24:03 -0700629 ret = show_splash(dev);
630 if (ret) {
631 log_debug("Cannot show splash screen\n");
632 return ret;
633 }
634 }
635
Simon Glasse0337a52024-07-31 08:44:10 -0600636 /* register cyclic as soon as the first video device is probed */
637 if (CONFIG_IS_ENABLED(CYCLIC) && (gd->flags && GD_FLG_RELOC) &&
638 !uc_priv->cyc_active) {
639 uint ms = CONFIG_IF_ENABLED_INT(CYCLIC, VIDEO_SYNC_CYCLIC_MS);
640
641 cyclic_register(&uc_priv->cyc, video_idle, ms * 1000,
642 "video_init");
643 uc_priv->cyc_active = true;
644 }
645
Simon Glass623d28f2016-01-18 19:52:15 -0700646 return 0;
647};
648
649/* Post-relocation, allocate memory for the frame buffer */
650static int video_post_bind(struct udevice *dev)
651{
Simon Glass951255d2020-07-02 21:12:32 -0600652 struct video_uc_priv *uc_priv;
653 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700654 ulong size;
655
656 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400657 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700658 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600659
660 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700661 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600662 if (!uc_priv->video_ptr)
663 uc_priv->video_ptr = gd->video_top;
664
665 /* Allocate framebuffer space for this device */
666 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700667 size = alloc_fb(dev, &addr);
668 if (addr < gd->video_bottom) {
Bin Mengacbafdd2023-07-23 12:40:24 +0800669 /*
670 * Device tree node may need the 'bootph-all' or
Simon Glassfc1aa352023-02-13 08:56:34 -0700671 * 'bootph-some-ram' tag
Patrick Delaunayd1937182019-05-21 19:19:12 +0200672 */
Bin Mengacbafdd2023-07-23 12:40:24 +0800673 printf("Video device '%s' cannot allocate frame buffer memory "
674 "- ensure the device is set up before relocation\n",
Simon Glass623d28f2016-01-18 19:52:15 -0700675 dev->name);
676 return -ENOSPC;
677 }
678 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
679 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600680 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700681
682 return 0;
683}
684
Simon Glasse0337a52024-07-31 08:44:10 -0600685__maybe_unused static int video_destroy(struct uclass *uc)
686{
687 struct video_uc_priv *uc_priv = uclass_get_priv(uc);
688
689 if (uc_priv->cyc_active) {
690 cyclic_unregister(&uc_priv->cyc);
691 uc_priv->cyc_active = false;
692 }
693
694 return 0;
695}
696
Simon Glass623d28f2016-01-18 19:52:15 -0700697UCLASS_DRIVER(video) = {
698 .id = UCLASS_VIDEO,
699 .name = "video",
700 .flags = DM_UC_FLAG_SEQ_ALIAS,
701 .post_bind = video_post_bind,
Simon Glass623d28f2016-01-18 19:52:15 -0700702 .post_probe = video_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700703 .priv_auto = sizeof(struct video_uc_priv),
704 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700705 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glasse0337a52024-07-31 08:44:10 -0600706 CONFIG_IS_ENABLED(CYCLIC, (.destroy = video_destroy, ))
Simon Glass623d28f2016-01-18 19:52:15 -0700707};