blob: e358a7949e0088c4a1c547664b8c92ff4e9396c0 [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 Glass853e8d22024-08-21 10:18:55 -0600155ulong video_get_fb(void)
156{
157 struct udevice *dev;
158
159 uclass_find_first_device(UCLASS_VIDEO, &dev);
160 if (dev) {
161 const struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
162
163 return uc_plat->base;
164 }
165
166 return 0;
167}
168
Simon Glass062673b2023-06-01 10:22:33 -0600169int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
170 int yend, u32 colour)
171{
172 struct video_priv *priv = dev_get_uclass_priv(dev);
173 void *start, *line;
174 int pixels = xend - xstart;
175 int row, i, ret;
176
177 start = priv->fb + ystart * priv->line_length;
178 start += xstart * VNBYTES(priv->bpix);
179 line = start;
180 for (row = ystart; row < yend; row++) {
181 switch (priv->bpix) {
182 case VIDEO_BPP8: {
183 u8 *dst = line;
184
185 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
186 for (i = 0; i < pixels; i++)
187 *dst++ = colour;
188 }
189 break;
190 }
191 case VIDEO_BPP16: {
192 u16 *dst = line;
193
194 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
195 for (i = 0; i < pixels; i++)
196 *dst++ = colour;
197 }
198 break;
199 }
200 case VIDEO_BPP32: {
201 u32 *dst = line;
202
203 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
204 for (i = 0; i < pixels; i++)
205 *dst++ = colour;
206 }
207 break;
208 }
209 default:
210 return -ENOSYS;
211 }
212 line += priv->line_length;
213 }
214 ret = video_sync_copy(dev, start, line);
215 if (ret)
216 return ret;
217
218 return 0;
219}
220
Nikhil M Jain76833532023-07-18 14:27:30 +0530221int video_reserve_from_bloblist(struct video_handoff *ho)
222{
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530223 if (!ho->fb || ho->size == 0)
224 return -ENOENT;
225
Nikhil M Jain76833532023-07-18 14:27:30 +0530226 gd->video_bottom = ho->fb;
227 gd->fb_base = ho->fb;
228 gd->video_top = ho->fb + ho->size;
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530229 debug("%s: Reserving %lx bytes at %08x as per bloblist received\n",
230 __func__, (unsigned long)ho->size, (u32)ho->fb);
Nikhil M Jain76833532023-07-18 14:27:30 +0530231
232 return 0;
233}
234
Simon Glass2baa6f82022-10-06 08:36:08 -0600235int video_fill(struct udevice *dev, u32 colour)
Simon Glass623d28f2016-01-18 19:52:15 -0700236{
237 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf8ec6212020-07-02 21:12:22 -0600238 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700239
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100240 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700241 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530242 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700243 u16 *ppix = priv->fb;
244 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100245
Simon Glass05c17d62019-12-20 18:10:37 -0700246 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600247 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700248 break;
249 }
250 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530251 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700252 u32 *ppix = priv->fb;
253 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700254
Simon Glass05c17d62019-12-20 18:10:37 -0700255 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600256 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700257 break;
258 }
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100259 default:
Simon Glass2baa6f82022-10-06 08:36:08 -0600260 memset(priv->fb, colour, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100261 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700262 }
Simon Glassf8ec6212020-07-02 21:12:22 -0600263 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
264 if (ret)
265 return ret;
Simon Glass55343122018-10-01 12:22:26 -0600266
Michal Simeka1e136d2020-12-15 15:12:09 +0100267 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700268}
269
Simon Glass2baa6f82022-10-06 08:36:08 -0600270int video_clear(struct udevice *dev)
271{
272 struct video_priv *priv = dev_get_uclass_priv(dev);
273 int ret;
274
275 ret = video_fill(dev, priv->colour_bg);
276 if (ret)
277 return ret;
278
279 return 0;
280}
281
Simon Glass2a006332022-10-06 08:36:03 -0600282static const struct vid_rgb colours[VID_COLOUR_COUNT] = {
283 { 0x00, 0x00, 0x00 }, /* black */
284 { 0xc0, 0x00, 0x00 }, /* red */
285 { 0x00, 0xc0, 0x00 }, /* green */
286 { 0xc0, 0x60, 0x00 }, /* brown */
287 { 0x00, 0x00, 0xc0 }, /* blue */
288 { 0xc0, 0x00, 0xc0 }, /* magenta */
289 { 0x00, 0xc0, 0xc0 }, /* cyan */
290 { 0xc0, 0xc0, 0xc0 }, /* light gray */
291 { 0x80, 0x80, 0x80 }, /* gray */
292 { 0xff, 0x00, 0x00 }, /* bright red */
293 { 0x00, 0xff, 0x00 }, /* bright green */
294 { 0xff, 0xff, 0x00 }, /* yellow */
295 { 0x00, 0x00, 0xff }, /* bright blue */
296 { 0xff, 0x00, 0xff }, /* bright magenta */
297 { 0x00, 0xff, 0xff }, /* bright cyan */
298 { 0xff, 0xff, 0xff }, /* white */
299};
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 Glass05c17d62019-12-20 18:10:37 -0700348 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
349 /* 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
Simon Glass623d28f2016-01-18 19:52:15 -0700369/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100370int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700371{
Simon Glass7f6280f2024-07-31 08:44:09 -0600372 struct video_priv *priv = dev_get_uclass_priv(vid);
Michal Simek8ae95df2020-12-03 09:30:00 +0100373 struct video_ops *ops = video_get_ops(vid);
374 int ret;
375
376 if (ops && ops->video_sync) {
377 ret = ops->video_sync(vid);
378 if (ret)
379 return ret;
380 }
381
Simon Glasse0337a52024-07-31 08:44:10 -0600382 if (CONFIG_IS_ENABLED(CYCLIC) && !force &&
383 get_timer(priv->last_sync) < CONFIG_VIDEO_SYNC_MS)
384 return 0;
385
Simon Glass623d28f2016-01-18 19:52:15 -0700386 /*
387 * flush_dcache_range() is declared in common.h but it seems that some
388 * architectures do not actually implement it. Is there a way to find
389 * out whether it exists? For now, ARM is safe.
390 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400391#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass623d28f2016-01-18 19:52:15 -0700392 if (priv->flush_dcache) {
393 flush_dcache_range((ulong)priv->fb,
Simon Glasseebc3fd2016-11-13 14:22:06 -0700394 ALIGN((ulong)priv->fb + priv->fb_size,
395 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass623d28f2016-01-18 19:52:15 -0700396 }
397#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
Simon Glasse0337a52024-07-31 08:44:10 -0600398 sandbox_sdl_sync(priv->fb);
Simon Glass623d28f2016-01-18 19:52:15 -0700399#endif
Simon Glasse0337a52024-07-31 08:44:10 -0600400 priv->last_sync = get_timer(0);
401
Michal Simek632e3d42020-12-14 08:47:52 +0100402 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700403}
404
405void video_sync_all(void)
406{
407 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100408 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700409
410 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
411 dev;
412 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100413 if (device_active(dev)) {
414 ret = video_sync(dev, true);
415 if (ret)
416 dev_dbg(dev, "Video sync failed\n");
417 }
Simon Glass623d28f2016-01-18 19:52:15 -0700418 }
419}
420
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100421bool video_is_active(void)
422{
423 struct udevice *dev;
424
Devarsh Thakkar42e41062024-02-22 18:38:09 +0530425 /* Assume video to be active if SPL passed video hand-off to U-boot */
426 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() > PHASE_SPL)
427 return true;
428
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100429 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
430 dev;
431 uclass_find_next_device(&dev)) {
432 if (device_active(dev))
433 return true;
434 }
435
436 return false;
437}
438
Simon Glass623d28f2016-01-18 19:52:15 -0700439int video_get_xsize(struct udevice *dev)
440{
441 struct video_priv *priv = dev_get_uclass_priv(dev);
442
443 return priv->xsize;
444}
445
446int video_get_ysize(struct udevice *dev)
447{
448 struct video_priv *priv = dev_get_uclass_priv(dev);
449
450 return priv->ysize;
451}
452
Simon Glass73c9c372020-07-02 21:12:20 -0600453#ifdef CONFIG_VIDEO_COPY
454int video_sync_copy(struct udevice *dev, void *from, void *to)
455{
456 struct video_priv *priv = dev_get_uclass_priv(dev);
457
458 if (priv->copy_fb) {
459 long offset, size;
460
461 /* Find the offset of the first byte to copy */
462 if ((ulong)to > (ulong)from) {
463 size = to - from;
464 offset = from - priv->fb;
465 } else {
466 size = from - to;
467 offset = to - priv->fb;
468 }
469
470 /*
471 * Allow a bit of leeway for valid requests somewhere near the
472 * frame buffer
473 */
474 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
475#ifdef DEBUG
Simon Glass78cdc5d2021-11-19 13:23:51 -0700476 char str[120];
Simon Glass73c9c372020-07-02 21:12:20 -0600477
478 snprintf(str, sizeof(str),
Simon Glass78cdc5d2021-11-19 13:23:51 -0700479 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
Simon Glass73c9c372020-07-02 21:12:20 -0600480 priv->fb, from, to, offset);
481 console_puts_select_stderr(true, str);
482#endif
483 return -EFAULT;
484 }
485
486 /*
487 * Silently crop the memcpy. This allows callers to avoid doing
488 * this themselves. It is common for the end pointer to go a
489 * few lines after the end of the frame buffer, since most of
490 * the update algorithms terminate a line after their last write
491 */
492 if (offset + size > priv->fb_size) {
493 size = priv->fb_size - offset;
494 } else if (offset < 0) {
495 size += offset;
496 offset = 0;
497 }
498
499 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
500 }
501
502 return 0;
503}
Simon Glass62b535e2021-01-13 20:29:46 -0700504
505int video_sync_copy_all(struct udevice *dev)
506{
507 struct video_priv *priv = dev_get_uclass_priv(dev);
508
509 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
510
511 return 0;
512}
513
Simon Glass73c9c372020-07-02 21:12:20 -0600514#endif
515
Simon Glass87a3cd72021-11-19 13:24:03 -0700516#define SPLASH_DECL(_name) \
517 extern u8 __splash_ ## _name ## _begin[]; \
518 extern u8 __splash_ ## _name ## _end[]
519
520#define SPLASH_START(_name) __splash_ ## _name ## _begin
521
522SPLASH_DECL(u_boot_logo);
523
Simon Glass22477422022-10-06 08:36:09 -0600524void *video_get_u_boot_logo(void)
525{
526 return SPLASH_START(u_boot_logo);
527}
528
Simon Glass87a3cd72021-11-19 13:24:03 -0700529static int show_splash(struct udevice *dev)
530{
531 u8 *data = SPLASH_START(u_boot_logo);
532 int ret;
533
534 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
535
536 return 0;
537}
538
Simon Glass70459902022-10-06 08:36:18 -0600539int video_default_font_height(struct udevice *dev)
540{
541 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
542
543 if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
544 return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
545 CONFIG_CONSOLE_TRUETYPE_SIZE);
546
547 return vc_priv->y_charsize;
548}
549
Simon Glasse0337a52024-07-31 08:44:10 -0600550static void video_idle(struct cyclic_info *cyc)
551{
552 video_sync_all();
553}
554
Simon Glass623d28f2016-01-18 19:52:15 -0700555/* Set up the display ready for use */
556static int video_post_probe(struct udevice *dev)
557{
Simon Glassb75b15b2020-12-03 16:55:23 -0700558 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glasse0337a52024-07-31 08:44:10 -0600559 struct video_uc_priv *uc_priv = uclass_get_priv(dev->uclass);
Simon Glass623d28f2016-01-18 19:52:15 -0700560 struct video_priv *priv = dev_get_uclass_priv(dev);
561 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700562 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700563 struct udevice *cons;
564 int ret;
565
566 /* Set up the line and display size */
567 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700568 if (!priv->line_length)
569 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
570
Simon Glass623d28f2016-01-18 19:52:15 -0700571 priv->fb_size = priv->line_length * priv->ysize;
572
Devarsh Thakkarf5254cc2023-12-05 21:25:21 +0530573 /*
574 * Set up video handoff fields for passing video blob to next stage
575 * NOTE:
576 * This assumes that reserved video memory only uses a single framebuffer
577 */
578 if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(BLOBLIST)) {
579 struct video_handoff *ho;
580
581 ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0);
582 if (!ho)
583 return log_msg_ret("blf", -ENOENT);
584 ho->fb = gd->video_bottom;
585 /* Fill aligned size here as calculated in video_reserve() */
586 ho->size = gd->video_top - gd->video_bottom;
587 ho->xsize = priv->xsize;
588 ho->ysize = priv->ysize;
589 ho->line_length = priv->line_length;
590 ho->bpix = priv->bpix;
591 }
592
Simon Glassb4928e52020-07-02 21:12:21 -0600593 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
594 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
595
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100596 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700597 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400598
599 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
600 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700601
Simon Glass84c7fb32016-01-18 19:52:17 -0700602 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700603 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700604 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700605 * for boards that don't need to display text. We create a TrueType
606 * console if enabled, a rotated console if the video driver requests
607 * it, otherwise a normal console.
608 *
609 * The console can be override by setting vidconsole_drv_name before
610 * probing this video driver, or in the probe() method.
611 *
612 * TrueType does not support rotation at present so fall back to the
613 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700614 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700615 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700616 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
617 strcpy(drv, "vidconsole_tt");
618 } else {
619 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
620 priv->rot);
621 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
622 }
623
Simon Glass84c7fb32016-01-18 19:52:17 -0700624 str = strdup(name);
625 if (!str)
626 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700627 if (priv->vidconsole_drv_name)
628 drv_name = priv->vidconsole_drv_name;
629 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700630 if (ret) {
631 debug("%s: Cannot bind console driver\n", __func__);
632 return ret;
633 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700634
Simon Glass84c7fb32016-01-18 19:52:17 -0700635 ret = device_probe(cons);
636 if (ret) {
637 debug("%s: Cannot probe console driver\n", __func__);
638 return ret;
639 }
640
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530641 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
642 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass87a3cd72021-11-19 13:24:03 -0700643 ret = show_splash(dev);
644 if (ret) {
645 log_debug("Cannot show splash screen\n");
646 return ret;
647 }
648 }
649
Simon Glasse0337a52024-07-31 08:44:10 -0600650 /* register cyclic as soon as the first video device is probed */
651 if (CONFIG_IS_ENABLED(CYCLIC) && (gd->flags && GD_FLG_RELOC) &&
652 !uc_priv->cyc_active) {
653 uint ms = CONFIG_IF_ENABLED_INT(CYCLIC, VIDEO_SYNC_CYCLIC_MS);
654
655 cyclic_register(&uc_priv->cyc, video_idle, ms * 1000,
656 "video_init");
657 uc_priv->cyc_active = true;
658 }
659
Simon Glass623d28f2016-01-18 19:52:15 -0700660 return 0;
661};
662
663/* Post-relocation, allocate memory for the frame buffer */
664static int video_post_bind(struct udevice *dev)
665{
Simon Glass951255d2020-07-02 21:12:32 -0600666 struct video_uc_priv *uc_priv;
667 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700668 ulong size;
669
670 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400671 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700672 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600673
674 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700675 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600676 if (!uc_priv->video_ptr)
677 uc_priv->video_ptr = gd->video_top;
678
679 /* Allocate framebuffer space for this device */
680 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700681 size = alloc_fb(dev, &addr);
682 if (addr < gd->video_bottom) {
Bin Mengacbafdd2023-07-23 12:40:24 +0800683 /*
684 * Device tree node may need the 'bootph-all' or
Simon Glassfc1aa352023-02-13 08:56:34 -0700685 * 'bootph-some-ram' tag
Patrick Delaunayd1937182019-05-21 19:19:12 +0200686 */
Bin Mengacbafdd2023-07-23 12:40:24 +0800687 printf("Video device '%s' cannot allocate frame buffer memory "
688 "- ensure the device is set up before relocation\n",
Simon Glass623d28f2016-01-18 19:52:15 -0700689 dev->name);
690 return -ENOSPC;
691 }
692 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
693 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600694 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700695
696 return 0;
697}
698
Simon Glasse0337a52024-07-31 08:44:10 -0600699__maybe_unused static int video_destroy(struct uclass *uc)
700{
701 struct video_uc_priv *uc_priv = uclass_get_priv(uc);
702
703 if (uc_priv->cyc_active) {
704 cyclic_unregister(&uc_priv->cyc);
705 uc_priv->cyc_active = false;
706 }
707
708 return 0;
709}
710
Simon Glass623d28f2016-01-18 19:52:15 -0700711UCLASS_DRIVER(video) = {
712 .id = UCLASS_VIDEO,
713 .name = "video",
714 .flags = DM_UC_FLAG_SEQ_ALIAS,
715 .post_bind = video_post_bind,
Simon Glass623d28f2016-01-18 19:52:15 -0700716 .post_probe = video_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700717 .priv_auto = sizeof(struct video_uc_priv),
718 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700719 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glasse0337a52024-07-31 08:44:10 -0600720 CONFIG_IS_ENABLED(CYCLIC, (.destroy = video_destroy, ))
Simon Glass623d28f2016-01-18 19:52:15 -0700721};