blob: ff1382f4a43b76869956ca971f5c77fe9c3e871a [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 Glass623d28f2016-01-18 19:52:15 -070011#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass623d28f2016-01-18 19:52:15 -070014#include <mapmem.h>
Nikhil M Jainf7ec5312023-07-18 14:27:31 +053015#include <spl.h>
Simon Glass623d28f2016-01-18 19:52:15 -070016#include <stdio_dev.h>
17#include <video.h>
18#include <video_console.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Simon Glass623d28f2016-01-18 19:52:15 -070021#include <dm/lists.h>
Michal Simek632e3d42020-12-14 08:47:52 +010022#include <dm/device_compat.h>
Simon Glass623d28f2016-01-18 19:52:15 -070023#include <dm/device-internal.h>
24#include <dm/uclass-internal.h>
25#ifdef CONFIG_SANDBOX
26#include <asm/sdl.h>
27#endif
28
29/*
30 * Theory of operation:
31 *
32 * Before relocation each device is bound. The driver for each device must
Simon Glassb75b15b2020-12-03 16:55:23 -070033 * set the @align and @size values in struct video_uc_plat. This
Simon Glass623d28f2016-01-18 19:52:15 -070034 * information represents the requires size and alignment of the frame buffer
35 * for the device. The values can be an over-estimate but cannot be too
36 * small. The actual values will be suppled (in the same manner) by the bind()
Pali Rohárf204fce2022-03-09 20:46:00 +010037 * method after relocation. Additionally driver can allocate frame buffer
38 * itself by setting plat->base.
Simon Glass623d28f2016-01-18 19:52:15 -070039 *
40 * This information is then picked up by video_reserve() which works out how
41 * much memory is needed for all devices. This is allocated between
42 * gd->video_bottom and gd->video_top.
43 *
44 * After relocation the same process occurs. The driver supplies the same
45 * @size and @align information and this time video_post_bind() checks that
46 * the drivers does not overflow the allocated memory.
47 *
48 * The frame buffer address is actually set (to plat->base) in
49 * video_post_probe(). This function also clears the frame buffer and
50 * allocates a suitable text console device. This can then be used to write
51 * text to the video device.
52 */
53DECLARE_GLOBAL_DATA_PTR;
54
Simon Glass951255d2020-07-02 21:12:32 -060055/**
56 * struct video_uc_priv - Information for the video uclass
57 *
58 * @video_ptr: Current allocation position of the video framebuffer pointer.
59 * While binding devices after relocation, this points to the next
60 * available address to use for a device's framebuffer. It starts at
61 * gd->video_top and works downwards, running out of space when it hits
62 * gd->video_bottom.
63 */
64struct video_uc_priv {
65 ulong video_ptr;
66};
67
Simon Glass2a006332022-10-06 08:36:03 -060068/** struct vid_rgb - Describes a video colour */
69struct vid_rgb {
70 u32 r;
71 u32 g;
72 u32 b;
73};
74
Simon Glass64635382016-01-21 19:44:52 -070075void video_set_flush_dcache(struct udevice *dev, bool flush)
76{
77 struct video_priv *priv = dev_get_uclass_priv(dev);
78
79 priv->flush_dcache = flush;
80}
81
Simon Glassdf865d32023-03-10 12:47:17 -080082static ulong alloc_fb_(ulong align, ulong size, ulong *addrp)
83{
84 ulong base;
85
86 align = align ? align : 1 << 20;
87 base = *addrp - size;
88 base &= ~(align - 1);
89 size = *addrp - base;
90 *addrp = base;
91
92 return size;
93}
94
Simon Glass623d28f2016-01-18 19:52:15 -070095static ulong alloc_fb(struct udevice *dev, ulong *addrp)
96{
Simon Glassb75b15b2020-12-03 16:55:23 -070097 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glassdf865d32023-03-10 12:47:17 -080098 ulong size;
Simon Glass623d28f2016-01-18 19:52:15 -070099
Simon Glassdf865d32023-03-10 12:47:17 -0800100 if (!plat->size) {
101 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_size) {
102 size = alloc_fb_(plat->align, plat->copy_size, addrp);
103 plat->copy_base = *addrp;
104 return size;
105 }
106
Bin Meng755623d2016-10-09 04:14:17 -0700107 return 0;
Simon Glassdf865d32023-03-10 12:47:17 -0800108 }
Bin Meng755623d2016-10-09 04:14:17 -0700109
Pali Rohárf204fce2022-03-09 20:46:00 +0100110 /* Allow drivers to allocate the frame buffer themselves */
111 if (plat->base)
112 return 0;
113
Simon Glassdf865d32023-03-10 12:47:17 -0800114 size = alloc_fb_(plat->align, plat->size, addrp);
115 plat->base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700116
117 return size;
118}
119
120int video_reserve(ulong *addrp)
121{
122 struct udevice *dev;
123 ulong size;
124
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530125 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() == PHASE_BOARD_F)
126 return 0;
127
Simon Glass623d28f2016-01-18 19:52:15 -0700128 gd->video_top = *addrp;
129 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
130 dev;
131 uclass_find_next_device(&dev)) {
132 size = alloc_fb(dev, addrp);
133 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
134 __func__, size, *addrp, dev->name);
135 }
Simon Glassc3d2f352020-07-02 21:12:33 -0600136
137 /* Allocate space for PCI video devices in case there were not bound */
138 if (*addrp == gd->video_top)
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530139 *addrp -= CONFIG_VAL(VIDEO_PCI_DEFAULT_FB_SIZE);
Simon Glassc3d2f352020-07-02 21:12:33 -0600140
Simon Glass623d28f2016-01-18 19:52:15 -0700141 gd->video_bottom = *addrp;
Simon Glassb2b29612020-05-10 14:17:01 -0600142 gd->fb_base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700143 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
144 gd->video_top);
145
146 return 0;
147}
148
Simon Glass062673b2023-06-01 10:22:33 -0600149int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
150 int yend, u32 colour)
151{
152 struct video_priv *priv = dev_get_uclass_priv(dev);
153 void *start, *line;
154 int pixels = xend - xstart;
155 int row, i, ret;
156
157 start = priv->fb + ystart * priv->line_length;
158 start += xstart * VNBYTES(priv->bpix);
159 line = start;
160 for (row = ystart; row < yend; row++) {
161 switch (priv->bpix) {
162 case VIDEO_BPP8: {
163 u8 *dst = line;
164
165 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
166 for (i = 0; i < pixels; i++)
167 *dst++ = colour;
168 }
169 break;
170 }
171 case VIDEO_BPP16: {
172 u16 *dst = line;
173
174 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
175 for (i = 0; i < pixels; i++)
176 *dst++ = colour;
177 }
178 break;
179 }
180 case VIDEO_BPP32: {
181 u32 *dst = line;
182
183 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
184 for (i = 0; i < pixels; i++)
185 *dst++ = colour;
186 }
187 break;
188 }
189 default:
190 return -ENOSYS;
191 }
192 line += priv->line_length;
193 }
194 ret = video_sync_copy(dev, start, line);
195 if (ret)
196 return ret;
197
198 return 0;
199}
200
Nikhil M Jain76833532023-07-18 14:27:30 +0530201int video_reserve_from_bloblist(struct video_handoff *ho)
202{
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530203 if (!ho->fb || ho->size == 0)
204 return -ENOENT;
205
Nikhil M Jain76833532023-07-18 14:27:30 +0530206 gd->video_bottom = ho->fb;
207 gd->fb_base = ho->fb;
208 gd->video_top = ho->fb + ho->size;
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530209 debug("%s: Reserving %lx bytes at %08x as per bloblist received\n",
210 __func__, (unsigned long)ho->size, (u32)ho->fb);
Nikhil M Jain76833532023-07-18 14:27:30 +0530211
212 return 0;
213}
214
Simon Glass2baa6f82022-10-06 08:36:08 -0600215int video_fill(struct udevice *dev, u32 colour)
Simon Glass623d28f2016-01-18 19:52:15 -0700216{
217 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf8ec6212020-07-02 21:12:22 -0600218 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700219
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100220 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700221 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530222 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700223 u16 *ppix = priv->fb;
224 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100225
Simon Glass05c17d62019-12-20 18:10:37 -0700226 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600227 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700228 break;
229 }
230 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530231 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700232 u32 *ppix = priv->fb;
233 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700234
Simon Glass05c17d62019-12-20 18:10:37 -0700235 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600236 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700237 break;
238 }
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100239 default:
Simon Glass2baa6f82022-10-06 08:36:08 -0600240 memset(priv->fb, colour, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100241 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700242 }
Simon Glassf8ec6212020-07-02 21:12:22 -0600243 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
244 if (ret)
245 return ret;
Simon Glass55343122018-10-01 12:22:26 -0600246
Michal Simeka1e136d2020-12-15 15:12:09 +0100247 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700248}
249
Simon Glass2baa6f82022-10-06 08:36:08 -0600250int video_clear(struct udevice *dev)
251{
252 struct video_priv *priv = dev_get_uclass_priv(dev);
253 int ret;
254
255 ret = video_fill(dev, priv->colour_bg);
256 if (ret)
257 return ret;
258
259 return 0;
260}
261
Simon Glass2a006332022-10-06 08:36:03 -0600262static const struct vid_rgb colours[VID_COLOUR_COUNT] = {
263 { 0x00, 0x00, 0x00 }, /* black */
264 { 0xc0, 0x00, 0x00 }, /* red */
265 { 0x00, 0xc0, 0x00 }, /* green */
266 { 0xc0, 0x60, 0x00 }, /* brown */
267 { 0x00, 0x00, 0xc0 }, /* blue */
268 { 0xc0, 0x00, 0xc0 }, /* magenta */
269 { 0x00, 0xc0, 0xc0 }, /* cyan */
270 { 0xc0, 0xc0, 0xc0 }, /* light gray */
271 { 0x80, 0x80, 0x80 }, /* gray */
272 { 0xff, 0x00, 0x00 }, /* bright red */
273 { 0x00, 0xff, 0x00 }, /* bright green */
274 { 0xff, 0xff, 0x00 }, /* yellow */
275 { 0x00, 0x00, 0xff }, /* bright blue */
276 { 0xff, 0x00, 0xff }, /* bright magenta */
277 { 0x00, 0xff, 0xff }, /* bright cyan */
278 { 0xff, 0xff, 0xff }, /* white */
279};
280
Simon Glassd17a6242023-06-01 10:22:48 -0600281u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx)
Simon Glass2a006332022-10-06 08:36:03 -0600282{
283 switch (priv->bpix) {
284 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530285 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass2a006332022-10-06 08:36:03 -0600286 return ((colours[idx].r >> 3) << 11) |
287 ((colours[idx].g >> 2) << 5) |
288 ((colours[idx].b >> 3) << 0);
289 }
290 break;
291 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530292 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Michal Simek985eac82023-05-17 10:42:07 +0200293 switch (priv->format) {
294 case VIDEO_X2R10G10B10:
Simon Glass2a006332022-10-06 08:36:03 -0600295 return (colours[idx].r << 22) |
296 (colours[idx].g << 12) |
297 (colours[idx].b << 2);
Michal Simek985eac82023-05-17 10:42:07 +0200298 case VIDEO_RGBA8888:
299 return (colours[idx].r << 24) |
300 (colours[idx].g << 16) |
301 (colours[idx].b << 8) | 0xff;
302 default:
Simon Glass2a006332022-10-06 08:36:03 -0600303 return (colours[idx].r << 16) |
304 (colours[idx].g << 8) |
305 (colours[idx].b << 0);
Michal Simek985eac82023-05-17 10:42:07 +0200306 }
Simon Glass2a006332022-10-06 08:36:03 -0600307 }
308 break;
309 default:
310 break;
311 }
312
313 /*
314 * For unknown bit arrangements just support
315 * black and white.
316 */
317 if (idx)
318 return 0xffffff; /* white */
319
320 return 0x000000; /* black */
321}
322
Simon Glass2b063b82018-11-06 15:21:36 -0700323void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100324{
Simon Glass2b063b82018-11-06 15:21:36 -0700325 struct video_priv *priv = dev_get_uclass_priv(dev);
326 int fore, back;
327
Simon Glass05c17d62019-12-20 18:10:37 -0700328 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
329 /* White is used when switching to bold, use light gray here */
330 fore = VID_LIGHT_GRAY;
331 back = VID_BLACK;
332 } else {
333 fore = VID_BLACK;
334 back = VID_WHITE;
335 }
Simon Glass2b063b82018-11-06 15:21:36 -0700336 if (invert) {
337 int temp;
338
339 temp = fore;
340 fore = back;
341 back = temp;
342 }
343 priv->fg_col_idx = fore;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000344 priv->bg_col_idx = back;
Simon Glass2a006332022-10-06 08:36:03 -0600345 priv->colour_fg = video_index_to_colour(priv, fore);
346 priv->colour_bg = video_index_to_colour(priv, back);
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100347}
348
Simon Glass623d28f2016-01-18 19:52:15 -0700349/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100350int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700351{
Michal Simek8ae95df2020-12-03 09:30:00 +0100352 struct video_ops *ops = video_get_ops(vid);
353 int ret;
354
355 if (ops && ops->video_sync) {
356 ret = ops->video_sync(vid);
357 if (ret)
358 return ret;
359 }
360
Simon Glass623d28f2016-01-18 19:52:15 -0700361 /*
362 * flush_dcache_range() is declared in common.h but it seems that some
363 * architectures do not actually implement it. Is there a way to find
364 * out whether it exists? For now, ARM is safe.
365 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400366#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass623d28f2016-01-18 19:52:15 -0700367 struct video_priv *priv = dev_get_uclass_priv(vid);
368
369 if (priv->flush_dcache) {
370 flush_dcache_range((ulong)priv->fb,
Simon Glasseebc3fd2016-11-13 14:22:06 -0700371 ALIGN((ulong)priv->fb + priv->fb_size,
372 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass623d28f2016-01-18 19:52:15 -0700373 }
374#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
375 struct video_priv *priv = dev_get_uclass_priv(vid);
376 static ulong last_sync;
377
Simon Glassea113732022-02-28 15:13:48 -0700378 if (force || get_timer(last_sync) > 100) {
Simon Glass623d28f2016-01-18 19:52:15 -0700379 sandbox_sdl_sync(priv->fb);
380 last_sync = get_timer(0);
381 }
382#endif
Michal Simek632e3d42020-12-14 08:47:52 +0100383 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700384}
385
386void video_sync_all(void)
387{
388 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100389 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700390
391 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
392 dev;
393 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100394 if (device_active(dev)) {
395 ret = video_sync(dev, true);
396 if (ret)
397 dev_dbg(dev, "Video sync failed\n");
398 }
Simon Glass623d28f2016-01-18 19:52:15 -0700399 }
400}
401
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100402bool video_is_active(void)
403{
404 struct udevice *dev;
405
Devarsh Thakkar42e41062024-02-22 18:38:09 +0530406 /* Assume video to be active if SPL passed video hand-off to U-boot */
407 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() > PHASE_SPL)
408 return true;
409
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100410 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
411 dev;
412 uclass_find_next_device(&dev)) {
413 if (device_active(dev))
414 return true;
415 }
416
417 return false;
418}
419
Simon Glass623d28f2016-01-18 19:52:15 -0700420int video_get_xsize(struct udevice *dev)
421{
422 struct video_priv *priv = dev_get_uclass_priv(dev);
423
424 return priv->xsize;
425}
426
427int video_get_ysize(struct udevice *dev)
428{
429 struct video_priv *priv = dev_get_uclass_priv(dev);
430
431 return priv->ysize;
432}
433
Simon Glass73c9c372020-07-02 21:12:20 -0600434#ifdef CONFIG_VIDEO_COPY
435int video_sync_copy(struct udevice *dev, void *from, void *to)
436{
437 struct video_priv *priv = dev_get_uclass_priv(dev);
438
439 if (priv->copy_fb) {
440 long offset, size;
441
442 /* Find the offset of the first byte to copy */
443 if ((ulong)to > (ulong)from) {
444 size = to - from;
445 offset = from - priv->fb;
446 } else {
447 size = from - to;
448 offset = to - priv->fb;
449 }
450
451 /*
452 * Allow a bit of leeway for valid requests somewhere near the
453 * frame buffer
454 */
455 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
456#ifdef DEBUG
Simon Glass78cdc5d2021-11-19 13:23:51 -0700457 char str[120];
Simon Glass73c9c372020-07-02 21:12:20 -0600458
459 snprintf(str, sizeof(str),
Simon Glass78cdc5d2021-11-19 13:23:51 -0700460 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
Simon Glass73c9c372020-07-02 21:12:20 -0600461 priv->fb, from, to, offset);
462 console_puts_select_stderr(true, str);
463#endif
464 return -EFAULT;
465 }
466
467 /*
468 * Silently crop the memcpy. This allows callers to avoid doing
469 * this themselves. It is common for the end pointer to go a
470 * few lines after the end of the frame buffer, since most of
471 * the update algorithms terminate a line after their last write
472 */
473 if (offset + size > priv->fb_size) {
474 size = priv->fb_size - offset;
475 } else if (offset < 0) {
476 size += offset;
477 offset = 0;
478 }
479
480 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
481 }
482
483 return 0;
484}
Simon Glass62b535e2021-01-13 20:29:46 -0700485
486int video_sync_copy_all(struct udevice *dev)
487{
488 struct video_priv *priv = dev_get_uclass_priv(dev);
489
490 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
491
492 return 0;
493}
494
Simon Glass73c9c372020-07-02 21:12:20 -0600495#endif
496
Simon Glass87a3cd72021-11-19 13:24:03 -0700497#define SPLASH_DECL(_name) \
498 extern u8 __splash_ ## _name ## _begin[]; \
499 extern u8 __splash_ ## _name ## _end[]
500
501#define SPLASH_START(_name) __splash_ ## _name ## _begin
502
503SPLASH_DECL(u_boot_logo);
504
Simon Glass22477422022-10-06 08:36:09 -0600505void *video_get_u_boot_logo(void)
506{
507 return SPLASH_START(u_boot_logo);
508}
509
Simon Glass87a3cd72021-11-19 13:24:03 -0700510static int show_splash(struct udevice *dev)
511{
512 u8 *data = SPLASH_START(u_boot_logo);
513 int ret;
514
515 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
516
517 return 0;
518}
519
Simon Glass70459902022-10-06 08:36:18 -0600520int video_default_font_height(struct udevice *dev)
521{
522 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
523
524 if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
525 return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
526 CONFIG_CONSOLE_TRUETYPE_SIZE);
527
528 return vc_priv->y_charsize;
529}
530
Simon Glass623d28f2016-01-18 19:52:15 -0700531/* Set up the display ready for use */
532static int video_post_probe(struct udevice *dev)
533{
Simon Glassb75b15b2020-12-03 16:55:23 -0700534 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700535 struct video_priv *priv = dev_get_uclass_priv(dev);
536 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700537 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700538 struct udevice *cons;
539 int ret;
540
541 /* Set up the line and display size */
542 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700543 if (!priv->line_length)
544 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
545
Simon Glass623d28f2016-01-18 19:52:15 -0700546 priv->fb_size = priv->line_length * priv->ysize;
547
Devarsh Thakkarf5254cc2023-12-05 21:25:21 +0530548 /*
549 * Set up video handoff fields for passing video blob to next stage
550 * NOTE:
551 * This assumes that reserved video memory only uses a single framebuffer
552 */
553 if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(BLOBLIST)) {
554 struct video_handoff *ho;
555
556 ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0);
557 if (!ho)
558 return log_msg_ret("blf", -ENOENT);
559 ho->fb = gd->video_bottom;
560 /* Fill aligned size here as calculated in video_reserve() */
561 ho->size = gd->video_top - gd->video_bottom;
562 ho->xsize = priv->xsize;
563 ho->ysize = priv->ysize;
564 ho->line_length = priv->line_length;
565 ho->bpix = priv->bpix;
566 }
567
Simon Glassb4928e52020-07-02 21:12:21 -0600568 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
569 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
570
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100571 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700572 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400573
574 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
575 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700576
Simon Glass84c7fb32016-01-18 19:52:17 -0700577 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700578 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700579 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700580 * for boards that don't need to display text. We create a TrueType
581 * console if enabled, a rotated console if the video driver requests
582 * it, otherwise a normal console.
583 *
584 * The console can be override by setting vidconsole_drv_name before
585 * probing this video driver, or in the probe() method.
586 *
587 * TrueType does not support rotation at present so fall back to the
588 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700589 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700590 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700591 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
592 strcpy(drv, "vidconsole_tt");
593 } else {
594 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
595 priv->rot);
596 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
597 }
598
Simon Glass84c7fb32016-01-18 19:52:17 -0700599 str = strdup(name);
600 if (!str)
601 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700602 if (priv->vidconsole_drv_name)
603 drv_name = priv->vidconsole_drv_name;
604 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700605 if (ret) {
606 debug("%s: Cannot bind console driver\n", __func__);
607 return ret;
608 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700609
Simon Glass84c7fb32016-01-18 19:52:17 -0700610 ret = device_probe(cons);
611 if (ret) {
612 debug("%s: Cannot probe console driver\n", __func__);
613 return ret;
614 }
615
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530616 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
617 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass87a3cd72021-11-19 13:24:03 -0700618 ret = show_splash(dev);
619 if (ret) {
620 log_debug("Cannot show splash screen\n");
621 return ret;
622 }
623 }
624
Simon Glass623d28f2016-01-18 19:52:15 -0700625 return 0;
626};
627
628/* Post-relocation, allocate memory for the frame buffer */
629static int video_post_bind(struct udevice *dev)
630{
Simon Glass951255d2020-07-02 21:12:32 -0600631 struct video_uc_priv *uc_priv;
632 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700633 ulong size;
634
635 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400636 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700637 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600638
639 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700640 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600641 if (!uc_priv->video_ptr)
642 uc_priv->video_ptr = gd->video_top;
643
644 /* Allocate framebuffer space for this device */
645 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700646 size = alloc_fb(dev, &addr);
647 if (addr < gd->video_bottom) {
Bin Mengacbafdd2023-07-23 12:40:24 +0800648 /*
649 * Device tree node may need the 'bootph-all' or
Simon Glassfc1aa352023-02-13 08:56:34 -0700650 * 'bootph-some-ram' tag
Patrick Delaunayd1937182019-05-21 19:19:12 +0200651 */
Bin Mengacbafdd2023-07-23 12:40:24 +0800652 printf("Video device '%s' cannot allocate frame buffer memory "
653 "- ensure the device is set up before relocation\n",
Simon Glass623d28f2016-01-18 19:52:15 -0700654 dev->name);
655 return -ENOSPC;
656 }
657 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
658 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600659 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700660
661 return 0;
662}
663
664UCLASS_DRIVER(video) = {
665 .id = UCLASS_VIDEO,
666 .name = "video",
667 .flags = DM_UC_FLAG_SEQ_ALIAS,
668 .post_bind = video_post_bind,
Simon Glass623d28f2016-01-18 19:52:15 -0700669 .post_probe = video_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700670 .priv_auto = sizeof(struct video_uc_priv),
671 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700672 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glass623d28f2016-01-18 19:52:15 -0700673};