blob: 7b5d1dfbb3bdb2ea0d35137c73674c040b6b8fca [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
Tom Riniabb9a042024-05-18 20:20:43 -06008#include <common.h>
Nikhil M Jainf7ec5312023-07-18 14:27:31 +05309#include <bloblist.h>
Simon Glass73c9c372020-07-02 21:12:20 -060010#include <console.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.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 Glass951255d2020-07-02 21:12:32 -060056/**
57 * struct video_uc_priv - Information for the video uclass
58 *
59 * @video_ptr: Current allocation position of the video framebuffer pointer.
60 * While binding devices after relocation, this points to the next
61 * available address to use for a device's framebuffer. It starts at
62 * gd->video_top and works downwards, running out of space when it hits
63 * gd->video_bottom.
64 */
65struct video_uc_priv {
66 ulong video_ptr;
67};
68
Simon Glass2a006332022-10-06 08:36:03 -060069/** struct vid_rgb - Describes a video colour */
70struct vid_rgb {
71 u32 r;
72 u32 g;
73 u32 b;
74};
75
Simon Glass64635382016-01-21 19:44:52 -070076void video_set_flush_dcache(struct udevice *dev, bool flush)
77{
78 struct video_priv *priv = dev_get_uclass_priv(dev);
79
80 priv->flush_dcache = flush;
81}
82
Simon Glassdf865d32023-03-10 12:47:17 -080083static ulong alloc_fb_(ulong align, ulong size, ulong *addrp)
84{
85 ulong base;
86
87 align = align ? align : 1 << 20;
88 base = *addrp - size;
89 base &= ~(align - 1);
90 size = *addrp - base;
91 *addrp = base;
92
93 return size;
94}
95
Simon Glass623d28f2016-01-18 19:52:15 -070096static ulong alloc_fb(struct udevice *dev, ulong *addrp)
97{
Simon Glassb75b15b2020-12-03 16:55:23 -070098 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glassdf865d32023-03-10 12:47:17 -080099 ulong size;
Simon Glass623d28f2016-01-18 19:52:15 -0700100
Simon Glassdf865d32023-03-10 12:47:17 -0800101 if (!plat->size) {
102 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_size) {
103 size = alloc_fb_(plat->align, plat->copy_size, addrp);
104 plat->copy_base = *addrp;
105 return size;
106 }
107
Bin Meng755623d2016-10-09 04:14:17 -0700108 return 0;
Simon Glassdf865d32023-03-10 12:47:17 -0800109 }
Bin Meng755623d2016-10-09 04:14:17 -0700110
Pali Rohárf204fce2022-03-09 20:46:00 +0100111 /* Allow drivers to allocate the frame buffer themselves */
112 if (plat->base)
113 return 0;
114
Simon Glassdf865d32023-03-10 12:47:17 -0800115 size = alloc_fb_(plat->align, plat->size, addrp);
116 plat->base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700117
118 return size;
119}
120
121int video_reserve(ulong *addrp)
122{
123 struct udevice *dev;
124 ulong size;
125
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530126 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() == PHASE_BOARD_F)
127 return 0;
128
Simon Glass623d28f2016-01-18 19:52:15 -0700129 gd->video_top = *addrp;
130 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
131 dev;
132 uclass_find_next_device(&dev)) {
133 size = alloc_fb(dev, addrp);
134 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
135 __func__, size, *addrp, dev->name);
136 }
Simon Glassc3d2f352020-07-02 21:12:33 -0600137
138 /* Allocate space for PCI video devices in case there were not bound */
139 if (*addrp == gd->video_top)
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530140 *addrp -= CONFIG_VAL(VIDEO_PCI_DEFAULT_FB_SIZE);
Simon Glassc3d2f352020-07-02 21:12:33 -0600141
Simon Glass623d28f2016-01-18 19:52:15 -0700142 gd->video_bottom = *addrp;
Simon Glassb2b29612020-05-10 14:17:01 -0600143 gd->fb_base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700144 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
145 gd->video_top);
146
147 return 0;
148}
149
Simon Glass062673b2023-06-01 10:22:33 -0600150int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
151 int yend, u32 colour)
152{
153 struct video_priv *priv = dev_get_uclass_priv(dev);
154 void *start, *line;
155 int pixels = xend - xstart;
156 int row, i, ret;
157
158 start = priv->fb + ystart * priv->line_length;
159 start += xstart * VNBYTES(priv->bpix);
160 line = start;
161 for (row = ystart; row < yend; row++) {
162 switch (priv->bpix) {
163 case VIDEO_BPP8: {
164 u8 *dst = line;
165
166 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
167 for (i = 0; i < pixels; i++)
168 *dst++ = colour;
169 }
170 break;
171 }
172 case VIDEO_BPP16: {
173 u16 *dst = line;
174
175 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
176 for (i = 0; i < pixels; i++)
177 *dst++ = colour;
178 }
179 break;
180 }
181 case VIDEO_BPP32: {
182 u32 *dst = line;
183
184 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
185 for (i = 0; i < pixels; i++)
186 *dst++ = colour;
187 }
188 break;
189 }
190 default:
191 return -ENOSYS;
192 }
193 line += priv->line_length;
194 }
195 ret = video_sync_copy(dev, start, line);
196 if (ret)
197 return ret;
198
199 return 0;
200}
201
Nikhil M Jain76833532023-07-18 14:27:30 +0530202int video_reserve_from_bloblist(struct video_handoff *ho)
203{
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530204 if (!ho->fb || ho->size == 0)
205 return -ENOENT;
206
Nikhil M Jain76833532023-07-18 14:27:30 +0530207 gd->video_bottom = ho->fb;
208 gd->fb_base = ho->fb;
209 gd->video_top = ho->fb + ho->size;
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530210 debug("%s: Reserving %lx bytes at %08x as per bloblist received\n",
211 __func__, (unsigned long)ho->size, (u32)ho->fb);
Nikhil M Jain76833532023-07-18 14:27:30 +0530212
213 return 0;
214}
215
Simon Glass2baa6f82022-10-06 08:36:08 -0600216int video_fill(struct udevice *dev, u32 colour)
Simon Glass623d28f2016-01-18 19:52:15 -0700217{
218 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf8ec6212020-07-02 21:12:22 -0600219 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700220
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100221 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700222 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530223 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700224 u16 *ppix = priv->fb;
225 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100226
Simon Glass05c17d62019-12-20 18:10:37 -0700227 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600228 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700229 break;
230 }
231 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530232 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700233 u32 *ppix = priv->fb;
234 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700235
Simon Glass05c17d62019-12-20 18:10:37 -0700236 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600237 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700238 break;
239 }
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100240 default:
Simon Glass2baa6f82022-10-06 08:36:08 -0600241 memset(priv->fb, colour, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100242 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700243 }
Simon Glassf8ec6212020-07-02 21:12:22 -0600244 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
245 if (ret)
246 return ret;
Simon Glass55343122018-10-01 12:22:26 -0600247
Michal Simeka1e136d2020-12-15 15:12:09 +0100248 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700249}
250
Simon Glass2baa6f82022-10-06 08:36:08 -0600251int video_clear(struct udevice *dev)
252{
253 struct video_priv *priv = dev_get_uclass_priv(dev);
254 int ret;
255
256 ret = video_fill(dev, priv->colour_bg);
257 if (ret)
258 return ret;
259
260 return 0;
261}
262
Simon Glass2a006332022-10-06 08:36:03 -0600263static const struct vid_rgb colours[VID_COLOUR_COUNT] = {
264 { 0x00, 0x00, 0x00 }, /* black */
265 { 0xc0, 0x00, 0x00 }, /* red */
266 { 0x00, 0xc0, 0x00 }, /* green */
267 { 0xc0, 0x60, 0x00 }, /* brown */
268 { 0x00, 0x00, 0xc0 }, /* blue */
269 { 0xc0, 0x00, 0xc0 }, /* magenta */
270 { 0x00, 0xc0, 0xc0 }, /* cyan */
271 { 0xc0, 0xc0, 0xc0 }, /* light gray */
272 { 0x80, 0x80, 0x80 }, /* gray */
273 { 0xff, 0x00, 0x00 }, /* bright red */
274 { 0x00, 0xff, 0x00 }, /* bright green */
275 { 0xff, 0xff, 0x00 }, /* yellow */
276 { 0x00, 0x00, 0xff }, /* bright blue */
277 { 0xff, 0x00, 0xff }, /* bright magenta */
278 { 0x00, 0xff, 0xff }, /* bright cyan */
279 { 0xff, 0xff, 0xff }, /* white */
280};
281
Simon Glassd17a6242023-06-01 10:22:48 -0600282u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx)
Simon Glass2a006332022-10-06 08:36:03 -0600283{
284 switch (priv->bpix) {
285 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530286 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass2a006332022-10-06 08:36:03 -0600287 return ((colours[idx].r >> 3) << 11) |
288 ((colours[idx].g >> 2) << 5) |
289 ((colours[idx].b >> 3) << 0);
290 }
291 break;
292 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530293 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Michal Simek985eac82023-05-17 10:42:07 +0200294 switch (priv->format) {
295 case VIDEO_X2R10G10B10:
Simon Glass2a006332022-10-06 08:36:03 -0600296 return (colours[idx].r << 22) |
297 (colours[idx].g << 12) |
298 (colours[idx].b << 2);
Michal Simek985eac82023-05-17 10:42:07 +0200299 case VIDEO_RGBA8888:
300 return (colours[idx].r << 24) |
301 (colours[idx].g << 16) |
302 (colours[idx].b << 8) | 0xff;
303 default:
Simon Glass2a006332022-10-06 08:36:03 -0600304 return (colours[idx].r << 16) |
305 (colours[idx].g << 8) |
306 (colours[idx].b << 0);
Michal Simek985eac82023-05-17 10:42:07 +0200307 }
Simon Glass2a006332022-10-06 08:36:03 -0600308 }
309 break;
310 default:
311 break;
312 }
313
314 /*
315 * For unknown bit arrangements just support
316 * black and white.
317 */
318 if (idx)
319 return 0xffffff; /* white */
320
321 return 0x000000; /* black */
322}
323
Simon Glass2b063b82018-11-06 15:21:36 -0700324void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100325{
Simon Glass2b063b82018-11-06 15:21:36 -0700326 struct video_priv *priv = dev_get_uclass_priv(dev);
327 int fore, back;
328
Simon Glass05c17d62019-12-20 18:10:37 -0700329 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
330 /* White is used when switching to bold, use light gray here */
331 fore = VID_LIGHT_GRAY;
332 back = VID_BLACK;
333 } else {
334 fore = VID_BLACK;
335 back = VID_WHITE;
336 }
Simon Glass2b063b82018-11-06 15:21:36 -0700337 if (invert) {
338 int temp;
339
340 temp = fore;
341 fore = back;
342 back = temp;
343 }
344 priv->fg_col_idx = fore;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000345 priv->bg_col_idx = back;
Simon Glass2a006332022-10-06 08:36:03 -0600346 priv->colour_fg = video_index_to_colour(priv, fore);
347 priv->colour_bg = video_index_to_colour(priv, back);
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100348}
349
Simon Glass623d28f2016-01-18 19:52:15 -0700350/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100351int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700352{
Michal Simek8ae95df2020-12-03 09:30:00 +0100353 struct video_ops *ops = video_get_ops(vid);
354 int ret;
355
356 if (ops && ops->video_sync) {
357 ret = ops->video_sync(vid);
358 if (ret)
359 return ret;
360 }
361
Simon Glass623d28f2016-01-18 19:52:15 -0700362 /*
363 * flush_dcache_range() is declared in common.h but it seems that some
364 * architectures do not actually implement it. Is there a way to find
365 * out whether it exists? For now, ARM is safe.
366 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400367#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass623d28f2016-01-18 19:52:15 -0700368 struct video_priv *priv = dev_get_uclass_priv(vid);
369
370 if (priv->flush_dcache) {
371 flush_dcache_range((ulong)priv->fb,
Simon Glasseebc3fd2016-11-13 14:22:06 -0700372 ALIGN((ulong)priv->fb + priv->fb_size,
373 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass623d28f2016-01-18 19:52:15 -0700374 }
375#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
376 struct video_priv *priv = dev_get_uclass_priv(vid);
377 static ulong last_sync;
378
Simon Glassea113732022-02-28 15:13:48 -0700379 if (force || get_timer(last_sync) > 100) {
Simon Glass623d28f2016-01-18 19:52:15 -0700380 sandbox_sdl_sync(priv->fb);
381 last_sync = get_timer(0);
382 }
383#endif
Michal Simek632e3d42020-12-14 08:47:52 +0100384 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700385}
386
387void video_sync_all(void)
388{
389 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100390 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700391
392 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
393 dev;
394 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100395 if (device_active(dev)) {
396 ret = video_sync(dev, true);
397 if (ret)
398 dev_dbg(dev, "Video sync failed\n");
399 }
Simon Glass623d28f2016-01-18 19:52:15 -0700400 }
401}
402
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100403bool video_is_active(void)
404{
405 struct udevice *dev;
406
Devarsh Thakkar42e41062024-02-22 18:38:09 +0530407 /* Assume video to be active if SPL passed video hand-off to U-boot */
408 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() > PHASE_SPL)
409 return true;
410
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100411 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
412 dev;
413 uclass_find_next_device(&dev)) {
414 if (device_active(dev))
415 return true;
416 }
417
418 return false;
419}
420
Simon Glass623d28f2016-01-18 19:52:15 -0700421int video_get_xsize(struct udevice *dev)
422{
423 struct video_priv *priv = dev_get_uclass_priv(dev);
424
425 return priv->xsize;
426}
427
428int video_get_ysize(struct udevice *dev)
429{
430 struct video_priv *priv = dev_get_uclass_priv(dev);
431
432 return priv->ysize;
433}
434
Simon Glass73c9c372020-07-02 21:12:20 -0600435#ifdef CONFIG_VIDEO_COPY
436int video_sync_copy(struct udevice *dev, void *from, void *to)
437{
438 struct video_priv *priv = dev_get_uclass_priv(dev);
439
440 if (priv->copy_fb) {
441 long offset, size;
442
443 /* Find the offset of the first byte to copy */
444 if ((ulong)to > (ulong)from) {
445 size = to - from;
446 offset = from - priv->fb;
447 } else {
448 size = from - to;
449 offset = to - priv->fb;
450 }
451
452 /*
453 * Allow a bit of leeway for valid requests somewhere near the
454 * frame buffer
455 */
456 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
457#ifdef DEBUG
Simon Glass78cdc5d2021-11-19 13:23:51 -0700458 char str[120];
Simon Glass73c9c372020-07-02 21:12:20 -0600459
460 snprintf(str, sizeof(str),
Simon Glass78cdc5d2021-11-19 13:23:51 -0700461 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
Simon Glass73c9c372020-07-02 21:12:20 -0600462 priv->fb, from, to, offset);
463 console_puts_select_stderr(true, str);
464#endif
465 return -EFAULT;
466 }
467
468 /*
469 * Silently crop the memcpy. This allows callers to avoid doing
470 * this themselves. It is common for the end pointer to go a
471 * few lines after the end of the frame buffer, since most of
472 * the update algorithms terminate a line after their last write
473 */
474 if (offset + size > priv->fb_size) {
475 size = priv->fb_size - offset;
476 } else if (offset < 0) {
477 size += offset;
478 offset = 0;
479 }
480
481 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
482 }
483
484 return 0;
485}
Simon Glass62b535e2021-01-13 20:29:46 -0700486
487int video_sync_copy_all(struct udevice *dev)
488{
489 struct video_priv *priv = dev_get_uclass_priv(dev);
490
491 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
492
493 return 0;
494}
495
Simon Glass73c9c372020-07-02 21:12:20 -0600496#endif
497
Simon Glass87a3cd72021-11-19 13:24:03 -0700498#define SPLASH_DECL(_name) \
499 extern u8 __splash_ ## _name ## _begin[]; \
500 extern u8 __splash_ ## _name ## _end[]
501
502#define SPLASH_START(_name) __splash_ ## _name ## _begin
503
504SPLASH_DECL(u_boot_logo);
505
Simon Glass22477422022-10-06 08:36:09 -0600506void *video_get_u_boot_logo(void)
507{
508 return SPLASH_START(u_boot_logo);
509}
510
Simon Glass87a3cd72021-11-19 13:24:03 -0700511static int show_splash(struct udevice *dev)
512{
513 u8 *data = SPLASH_START(u_boot_logo);
514 int ret;
515
516 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
517
518 return 0;
519}
520
Simon Glass70459902022-10-06 08:36:18 -0600521int video_default_font_height(struct udevice *dev)
522{
523 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
524
525 if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
526 return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
527 CONFIG_CONSOLE_TRUETYPE_SIZE);
528
529 return vc_priv->y_charsize;
530}
531
Simon Glass623d28f2016-01-18 19:52:15 -0700532/* Set up the display ready for use */
533static int video_post_probe(struct udevice *dev)
534{
Simon Glassb75b15b2020-12-03 16:55:23 -0700535 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700536 struct video_priv *priv = dev_get_uclass_priv(dev);
537 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700538 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700539 struct udevice *cons;
540 int ret;
541
542 /* Set up the line and display size */
543 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700544 if (!priv->line_length)
545 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
546
Simon Glass623d28f2016-01-18 19:52:15 -0700547 priv->fb_size = priv->line_length * priv->ysize;
548
Devarsh Thakkarf5254cc2023-12-05 21:25:21 +0530549 /*
550 * Set up video handoff fields for passing video blob to next stage
551 * NOTE:
552 * This assumes that reserved video memory only uses a single framebuffer
553 */
554 if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(BLOBLIST)) {
555 struct video_handoff *ho;
556
557 ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0);
558 if (!ho)
559 return log_msg_ret("blf", -ENOENT);
560 ho->fb = gd->video_bottom;
561 /* Fill aligned size here as calculated in video_reserve() */
562 ho->size = gd->video_top - gd->video_bottom;
563 ho->xsize = priv->xsize;
564 ho->ysize = priv->ysize;
565 ho->line_length = priv->line_length;
566 ho->bpix = priv->bpix;
567 }
568
Simon Glassb4928e52020-07-02 21:12:21 -0600569 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
570 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
571
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100572 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700573 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400574
575 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
576 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700577
Simon Glass84c7fb32016-01-18 19:52:17 -0700578 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700579 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700580 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700581 * for boards that don't need to display text. We create a TrueType
582 * console if enabled, a rotated console if the video driver requests
583 * it, otherwise a normal console.
584 *
585 * The console can be override by setting vidconsole_drv_name before
586 * probing this video driver, or in the probe() method.
587 *
588 * TrueType does not support rotation at present so fall back to the
589 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700590 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700591 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700592 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
593 strcpy(drv, "vidconsole_tt");
594 } else {
595 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
596 priv->rot);
597 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
598 }
599
Simon Glass84c7fb32016-01-18 19:52:17 -0700600 str = strdup(name);
601 if (!str)
602 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700603 if (priv->vidconsole_drv_name)
604 drv_name = priv->vidconsole_drv_name;
605 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700606 if (ret) {
607 debug("%s: Cannot bind console driver\n", __func__);
608 return ret;
609 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700610
Simon Glass84c7fb32016-01-18 19:52:17 -0700611 ret = device_probe(cons);
612 if (ret) {
613 debug("%s: Cannot probe console driver\n", __func__);
614 return ret;
615 }
616
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530617 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
618 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass87a3cd72021-11-19 13:24:03 -0700619 ret = show_splash(dev);
620 if (ret) {
621 log_debug("Cannot show splash screen\n");
622 return ret;
623 }
624 }
625
Simon Glass623d28f2016-01-18 19:52:15 -0700626 return 0;
627};
628
629/* Post-relocation, allocate memory for the frame buffer */
630static int video_post_bind(struct udevice *dev)
631{
Simon Glass951255d2020-07-02 21:12:32 -0600632 struct video_uc_priv *uc_priv;
633 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700634 ulong size;
635
636 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400637 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700638 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600639
640 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700641 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600642 if (!uc_priv->video_ptr)
643 uc_priv->video_ptr = gd->video_top;
644
645 /* Allocate framebuffer space for this device */
646 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700647 size = alloc_fb(dev, &addr);
648 if (addr < gd->video_bottom) {
Bin Mengacbafdd2023-07-23 12:40:24 +0800649 /*
650 * Device tree node may need the 'bootph-all' or
Simon Glassfc1aa352023-02-13 08:56:34 -0700651 * 'bootph-some-ram' tag
Patrick Delaunayd1937182019-05-21 19:19:12 +0200652 */
Bin Mengacbafdd2023-07-23 12:40:24 +0800653 printf("Video device '%s' cannot allocate frame buffer memory "
654 "- ensure the device is set up before relocation\n",
Simon Glass623d28f2016-01-18 19:52:15 -0700655 dev->name);
656 return -ENOSPC;
657 }
658 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
659 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600660 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700661
662 return 0;
663}
664
665UCLASS_DRIVER(video) = {
666 .id = UCLASS_VIDEO,
667 .name = "video",
668 .flags = DM_UC_FLAG_SEQ_ALIAS,
669 .post_bind = video_post_bind,
Simon Glass623d28f2016-01-18 19:52:15 -0700670 .post_probe = video_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700671 .priv_auto = sizeof(struct video_uc_priv),
672 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700673 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glass623d28f2016-01-18 19:52:15 -0700674};