blob: 3571e62ba2d9937d8b05150d98f015387d1a39f7 [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
Simon Glass623d28f2016-01-18 19:52:15 -07008#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
407 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
408 dev;
409 uclass_find_next_device(&dev)) {
410 if (device_active(dev))
411 return true;
412 }
413
414 return false;
415}
416
Simon Glass623d28f2016-01-18 19:52:15 -0700417int video_get_xsize(struct udevice *dev)
418{
419 struct video_priv *priv = dev_get_uclass_priv(dev);
420
421 return priv->xsize;
422}
423
424int video_get_ysize(struct udevice *dev)
425{
426 struct video_priv *priv = dev_get_uclass_priv(dev);
427
428 return priv->ysize;
429}
430
Simon Glass73c9c372020-07-02 21:12:20 -0600431#ifdef CONFIG_VIDEO_COPY
432int video_sync_copy(struct udevice *dev, void *from, void *to)
433{
434 struct video_priv *priv = dev_get_uclass_priv(dev);
435
436 if (priv->copy_fb) {
437 long offset, size;
438
439 /* Find the offset of the first byte to copy */
440 if ((ulong)to > (ulong)from) {
441 size = to - from;
442 offset = from - priv->fb;
443 } else {
444 size = from - to;
445 offset = to - priv->fb;
446 }
447
448 /*
449 * Allow a bit of leeway for valid requests somewhere near the
450 * frame buffer
451 */
452 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
453#ifdef DEBUG
Simon Glass78cdc5d2021-11-19 13:23:51 -0700454 char str[120];
Simon Glass73c9c372020-07-02 21:12:20 -0600455
456 snprintf(str, sizeof(str),
Simon Glass78cdc5d2021-11-19 13:23:51 -0700457 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
Simon Glass73c9c372020-07-02 21:12:20 -0600458 priv->fb, from, to, offset);
459 console_puts_select_stderr(true, str);
460#endif
461 return -EFAULT;
462 }
463
464 /*
465 * Silently crop the memcpy. This allows callers to avoid doing
466 * this themselves. It is common for the end pointer to go a
467 * few lines after the end of the frame buffer, since most of
468 * the update algorithms terminate a line after their last write
469 */
470 if (offset + size > priv->fb_size) {
471 size = priv->fb_size - offset;
472 } else if (offset < 0) {
473 size += offset;
474 offset = 0;
475 }
476
477 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
478 }
479
480 return 0;
481}
Simon Glass62b535e2021-01-13 20:29:46 -0700482
483int video_sync_copy_all(struct udevice *dev)
484{
485 struct video_priv *priv = dev_get_uclass_priv(dev);
486
487 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
488
489 return 0;
490}
491
Simon Glass73c9c372020-07-02 21:12:20 -0600492#endif
493
Simon Glass87a3cd72021-11-19 13:24:03 -0700494#define SPLASH_DECL(_name) \
495 extern u8 __splash_ ## _name ## _begin[]; \
496 extern u8 __splash_ ## _name ## _end[]
497
498#define SPLASH_START(_name) __splash_ ## _name ## _begin
499
500SPLASH_DECL(u_boot_logo);
501
Simon Glass22477422022-10-06 08:36:09 -0600502void *video_get_u_boot_logo(void)
503{
504 return SPLASH_START(u_boot_logo);
505}
506
Simon Glass87a3cd72021-11-19 13:24:03 -0700507static int show_splash(struct udevice *dev)
508{
509 u8 *data = SPLASH_START(u_boot_logo);
510 int ret;
511
512 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
513
514 return 0;
515}
516
Simon Glass70459902022-10-06 08:36:18 -0600517int video_default_font_height(struct udevice *dev)
518{
519 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
520
521 if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
522 return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
523 CONFIG_CONSOLE_TRUETYPE_SIZE);
524
525 return vc_priv->y_charsize;
526}
527
Simon Glass623d28f2016-01-18 19:52:15 -0700528/* Set up the display ready for use */
529static int video_post_probe(struct udevice *dev)
530{
Simon Glassb75b15b2020-12-03 16:55:23 -0700531 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700532 struct video_priv *priv = dev_get_uclass_priv(dev);
533 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700534 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700535 struct udevice *cons;
536 int ret;
537
538 /* Set up the line and display size */
539 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700540 if (!priv->line_length)
541 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
542
Simon Glass623d28f2016-01-18 19:52:15 -0700543 priv->fb_size = priv->line_length * priv->ysize;
544
Devarsh Thakkarf5254cc2023-12-05 21:25:21 +0530545 /*
546 * Set up video handoff fields for passing video blob to next stage
547 * NOTE:
548 * This assumes that reserved video memory only uses a single framebuffer
549 */
550 if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(BLOBLIST)) {
551 struct video_handoff *ho;
552
553 ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0);
554 if (!ho)
555 return log_msg_ret("blf", -ENOENT);
556 ho->fb = gd->video_bottom;
557 /* Fill aligned size here as calculated in video_reserve() */
558 ho->size = gd->video_top - gd->video_bottom;
559 ho->xsize = priv->xsize;
560 ho->ysize = priv->ysize;
561 ho->line_length = priv->line_length;
562 ho->bpix = priv->bpix;
563 }
564
Simon Glassb4928e52020-07-02 21:12:21 -0600565 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
566 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
567
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100568 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700569 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400570
571 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
572 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700573
Simon Glass84c7fb32016-01-18 19:52:17 -0700574 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700575 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700576 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700577 * for boards that don't need to display text. We create a TrueType
578 * console if enabled, a rotated console if the video driver requests
579 * it, otherwise a normal console.
580 *
581 * The console can be override by setting vidconsole_drv_name before
582 * probing this video driver, or in the probe() method.
583 *
584 * TrueType does not support rotation at present so fall back to the
585 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700586 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700587 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700588 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
589 strcpy(drv, "vidconsole_tt");
590 } else {
591 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
592 priv->rot);
593 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
594 }
595
Simon Glass84c7fb32016-01-18 19:52:17 -0700596 str = strdup(name);
597 if (!str)
598 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700599 if (priv->vidconsole_drv_name)
600 drv_name = priv->vidconsole_drv_name;
601 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700602 if (ret) {
603 debug("%s: Cannot bind console driver\n", __func__);
604 return ret;
605 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700606
Simon Glass84c7fb32016-01-18 19:52:17 -0700607 ret = device_probe(cons);
608 if (ret) {
609 debug("%s: Cannot probe console driver\n", __func__);
610 return ret;
611 }
612
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530613 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
614 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass87a3cd72021-11-19 13:24:03 -0700615 ret = show_splash(dev);
616 if (ret) {
617 log_debug("Cannot show splash screen\n");
618 return ret;
619 }
620 }
621
Simon Glass623d28f2016-01-18 19:52:15 -0700622 return 0;
623};
624
625/* Post-relocation, allocate memory for the frame buffer */
626static int video_post_bind(struct udevice *dev)
627{
Simon Glass951255d2020-07-02 21:12:32 -0600628 struct video_uc_priv *uc_priv;
629 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700630 ulong size;
631
632 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400633 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700634 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600635
636 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700637 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600638 if (!uc_priv->video_ptr)
639 uc_priv->video_ptr = gd->video_top;
640
641 /* Allocate framebuffer space for this device */
642 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700643 size = alloc_fb(dev, &addr);
644 if (addr < gd->video_bottom) {
Bin Mengacbafdd2023-07-23 12:40:24 +0800645 /*
646 * Device tree node may need the 'bootph-all' or
Simon Glassfc1aa352023-02-13 08:56:34 -0700647 * 'bootph-some-ram' tag
Patrick Delaunayd1937182019-05-21 19:19:12 +0200648 */
Bin Mengacbafdd2023-07-23 12:40:24 +0800649 printf("Video device '%s' cannot allocate frame buffer memory "
650 "- ensure the device is set up before relocation\n",
Simon Glass623d28f2016-01-18 19:52:15 -0700651 dev->name);
652 return -ENOSPC;
653 }
654 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
655 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600656 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700657
658 return 0;
659}
660
661UCLASS_DRIVER(video) = {
662 .id = UCLASS_VIDEO,
663 .name = "video",
664 .flags = DM_UC_FLAG_SEQ_ALIAS,
665 .post_bind = video_post_bind,
Simon Glass623d28f2016-01-18 19:52:15 -0700666 .post_probe = video_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700667 .priv_auto = sizeof(struct video_uc_priv),
668 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700669 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glass623d28f2016-01-18 19:52:15 -0700670};