blob: d620a29c253787a6c06b5f77f971f751f671d8fc [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
Simon Glass896409c2023-07-30 11:16:05 -0600147 if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(VIDEO_HANDOFF)) {
Nikhil M Jainf7ec5312023-07-18 14:27:31 +0530148 struct video_handoff *ho;
149
150 ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0);
151 if (!ho)
152 return log_msg_ret("blf", -ENOENT);
153 ho->fb = *addrp;
154 ho->size = size;
155 }
156
Simon Glass623d28f2016-01-18 19:52:15 -0700157 return 0;
158}
159
Simon Glass062673b2023-06-01 10:22:33 -0600160int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
161 int yend, u32 colour)
162{
163 struct video_priv *priv = dev_get_uclass_priv(dev);
164 void *start, *line;
165 int pixels = xend - xstart;
166 int row, i, ret;
167
168 start = priv->fb + ystart * priv->line_length;
169 start += xstart * VNBYTES(priv->bpix);
170 line = start;
171 for (row = ystart; row < yend; row++) {
172 switch (priv->bpix) {
173 case VIDEO_BPP8: {
174 u8 *dst = line;
175
176 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
177 for (i = 0; i < pixels; i++)
178 *dst++ = colour;
179 }
180 break;
181 }
182 case VIDEO_BPP16: {
183 u16 *dst = line;
184
185 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
186 for (i = 0; i < pixels; i++)
187 *dst++ = colour;
188 }
189 break;
190 }
191 case VIDEO_BPP32: {
192 u32 *dst = line;
193
194 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
195 for (i = 0; i < pixels; i++)
196 *dst++ = colour;
197 }
198 break;
199 }
200 default:
201 return -ENOSYS;
202 }
203 line += priv->line_length;
204 }
205 ret = video_sync_copy(dev, start, line);
206 if (ret)
207 return ret;
208
209 return 0;
210}
211
Nikhil M Jain76833532023-07-18 14:27:30 +0530212int video_reserve_from_bloblist(struct video_handoff *ho)
213{
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530214 if (!ho->fb || ho->size == 0)
215 return -ENOENT;
216
Nikhil M Jain76833532023-07-18 14:27:30 +0530217 gd->video_bottom = ho->fb;
218 gd->fb_base = ho->fb;
219 gd->video_top = ho->fb + ho->size;
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530220 debug("%s: Reserving %lx bytes at %08x as per bloblist received\n",
221 __func__, (unsigned long)ho->size, (u32)ho->fb);
Nikhil M Jain76833532023-07-18 14:27:30 +0530222
223 return 0;
224}
225
Simon Glass2baa6f82022-10-06 08:36:08 -0600226int video_fill(struct udevice *dev, u32 colour)
Simon Glass623d28f2016-01-18 19:52:15 -0700227{
228 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf8ec6212020-07-02 21:12:22 -0600229 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700230
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100231 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700232 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530233 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700234 u16 *ppix = priv->fb;
235 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100236
Simon Glass05c17d62019-12-20 18:10:37 -0700237 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600238 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700239 break;
240 }
241 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530242 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700243 u32 *ppix = priv->fb;
244 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700245
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 }
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100250 default:
Simon Glass2baa6f82022-10-06 08:36:08 -0600251 memset(priv->fb, colour, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100252 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700253 }
Simon Glassf8ec6212020-07-02 21:12:22 -0600254 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
255 if (ret)
256 return ret;
Simon Glass55343122018-10-01 12:22:26 -0600257
Michal Simeka1e136d2020-12-15 15:12:09 +0100258 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700259}
260
Simon Glass2baa6f82022-10-06 08:36:08 -0600261int video_clear(struct udevice *dev)
262{
263 struct video_priv *priv = dev_get_uclass_priv(dev);
264 int ret;
265
266 ret = video_fill(dev, priv->colour_bg);
267 if (ret)
268 return ret;
269
270 return 0;
271}
272
Simon Glass2a006332022-10-06 08:36:03 -0600273static const struct vid_rgb colours[VID_COLOUR_COUNT] = {
274 { 0x00, 0x00, 0x00 }, /* black */
275 { 0xc0, 0x00, 0x00 }, /* red */
276 { 0x00, 0xc0, 0x00 }, /* green */
277 { 0xc0, 0x60, 0x00 }, /* brown */
278 { 0x00, 0x00, 0xc0 }, /* blue */
279 { 0xc0, 0x00, 0xc0 }, /* magenta */
280 { 0x00, 0xc0, 0xc0 }, /* cyan */
281 { 0xc0, 0xc0, 0xc0 }, /* light gray */
282 { 0x80, 0x80, 0x80 }, /* gray */
283 { 0xff, 0x00, 0x00 }, /* bright red */
284 { 0x00, 0xff, 0x00 }, /* bright green */
285 { 0xff, 0xff, 0x00 }, /* yellow */
286 { 0x00, 0x00, 0xff }, /* bright blue */
287 { 0xff, 0x00, 0xff }, /* bright magenta */
288 { 0x00, 0xff, 0xff }, /* bright cyan */
289 { 0xff, 0xff, 0xff }, /* white */
290};
291
Simon Glassd17a6242023-06-01 10:22:48 -0600292u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx)
Simon Glass2a006332022-10-06 08:36:03 -0600293{
294 switch (priv->bpix) {
295 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530296 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass2a006332022-10-06 08:36:03 -0600297 return ((colours[idx].r >> 3) << 11) |
298 ((colours[idx].g >> 2) << 5) |
299 ((colours[idx].b >> 3) << 0);
300 }
301 break;
302 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530303 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Michal Simek985eac82023-05-17 10:42:07 +0200304 switch (priv->format) {
305 case VIDEO_X2R10G10B10:
Simon Glass2a006332022-10-06 08:36:03 -0600306 return (colours[idx].r << 22) |
307 (colours[idx].g << 12) |
308 (colours[idx].b << 2);
Michal Simek985eac82023-05-17 10:42:07 +0200309 case VIDEO_RGBA8888:
310 return (colours[idx].r << 24) |
311 (colours[idx].g << 16) |
312 (colours[idx].b << 8) | 0xff;
313 default:
Simon Glass2a006332022-10-06 08:36:03 -0600314 return (colours[idx].r << 16) |
315 (colours[idx].g << 8) |
316 (colours[idx].b << 0);
Michal Simek985eac82023-05-17 10:42:07 +0200317 }
Simon Glass2a006332022-10-06 08:36:03 -0600318 }
319 break;
320 default:
321 break;
322 }
323
324 /*
325 * For unknown bit arrangements just support
326 * black and white.
327 */
328 if (idx)
329 return 0xffffff; /* white */
330
331 return 0x000000; /* black */
332}
333
Simon Glass2b063b82018-11-06 15:21:36 -0700334void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100335{
Simon Glass2b063b82018-11-06 15:21:36 -0700336 struct video_priv *priv = dev_get_uclass_priv(dev);
337 int fore, back;
338
Simon Glass05c17d62019-12-20 18:10:37 -0700339 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
340 /* White is used when switching to bold, use light gray here */
341 fore = VID_LIGHT_GRAY;
342 back = VID_BLACK;
343 } else {
344 fore = VID_BLACK;
345 back = VID_WHITE;
346 }
Simon Glass2b063b82018-11-06 15:21:36 -0700347 if (invert) {
348 int temp;
349
350 temp = fore;
351 fore = back;
352 back = temp;
353 }
354 priv->fg_col_idx = fore;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000355 priv->bg_col_idx = back;
Simon Glass2a006332022-10-06 08:36:03 -0600356 priv->colour_fg = video_index_to_colour(priv, fore);
357 priv->colour_bg = video_index_to_colour(priv, back);
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100358}
359
Simon Glass623d28f2016-01-18 19:52:15 -0700360/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100361int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700362{
Michal Simek8ae95df2020-12-03 09:30:00 +0100363 struct video_ops *ops = video_get_ops(vid);
364 int ret;
365
366 if (ops && ops->video_sync) {
367 ret = ops->video_sync(vid);
368 if (ret)
369 return ret;
370 }
371
Simon Glass623d28f2016-01-18 19:52:15 -0700372 /*
373 * flush_dcache_range() is declared in common.h but it seems that some
374 * architectures do not actually implement it. Is there a way to find
375 * out whether it exists? For now, ARM is safe.
376 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400377#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass623d28f2016-01-18 19:52:15 -0700378 struct video_priv *priv = dev_get_uclass_priv(vid);
379
380 if (priv->flush_dcache) {
381 flush_dcache_range((ulong)priv->fb,
Simon Glasseebc3fd2016-11-13 14:22:06 -0700382 ALIGN((ulong)priv->fb + priv->fb_size,
383 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass623d28f2016-01-18 19:52:15 -0700384 }
385#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
386 struct video_priv *priv = dev_get_uclass_priv(vid);
387 static ulong last_sync;
388
Simon Glassea113732022-02-28 15:13:48 -0700389 if (force || get_timer(last_sync) > 100) {
Simon Glass623d28f2016-01-18 19:52:15 -0700390 sandbox_sdl_sync(priv->fb);
391 last_sync = get_timer(0);
392 }
393#endif
Michal Simek632e3d42020-12-14 08:47:52 +0100394 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700395}
396
397void video_sync_all(void)
398{
399 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100400 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700401
402 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
403 dev;
404 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100405 if (device_active(dev)) {
406 ret = video_sync(dev, true);
407 if (ret)
408 dev_dbg(dev, "Video sync failed\n");
409 }
Simon Glass623d28f2016-01-18 19:52:15 -0700410 }
411}
412
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100413bool video_is_active(void)
414{
415 struct udevice *dev;
416
417 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
418 dev;
419 uclass_find_next_device(&dev)) {
420 if (device_active(dev))
421 return true;
422 }
423
424 return false;
425}
426
Simon Glass623d28f2016-01-18 19:52:15 -0700427int video_get_xsize(struct udevice *dev)
428{
429 struct video_priv *priv = dev_get_uclass_priv(dev);
430
431 return priv->xsize;
432}
433
434int video_get_ysize(struct udevice *dev)
435{
436 struct video_priv *priv = dev_get_uclass_priv(dev);
437
438 return priv->ysize;
439}
440
Simon Glass73c9c372020-07-02 21:12:20 -0600441#ifdef CONFIG_VIDEO_COPY
442int video_sync_copy(struct udevice *dev, void *from, void *to)
443{
444 struct video_priv *priv = dev_get_uclass_priv(dev);
445
446 if (priv->copy_fb) {
447 long offset, size;
448
449 /* Find the offset of the first byte to copy */
450 if ((ulong)to > (ulong)from) {
451 size = to - from;
452 offset = from - priv->fb;
453 } else {
454 size = from - to;
455 offset = to - priv->fb;
456 }
457
458 /*
459 * Allow a bit of leeway for valid requests somewhere near the
460 * frame buffer
461 */
462 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
463#ifdef DEBUG
Simon Glass78cdc5d2021-11-19 13:23:51 -0700464 char str[120];
Simon Glass73c9c372020-07-02 21:12:20 -0600465
466 snprintf(str, sizeof(str),
Simon Glass78cdc5d2021-11-19 13:23:51 -0700467 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
Simon Glass73c9c372020-07-02 21:12:20 -0600468 priv->fb, from, to, offset);
469 console_puts_select_stderr(true, str);
470#endif
471 return -EFAULT;
472 }
473
474 /*
475 * Silently crop the memcpy. This allows callers to avoid doing
476 * this themselves. It is common for the end pointer to go a
477 * few lines after the end of the frame buffer, since most of
478 * the update algorithms terminate a line after their last write
479 */
480 if (offset + size > priv->fb_size) {
481 size = priv->fb_size - offset;
482 } else if (offset < 0) {
483 size += offset;
484 offset = 0;
485 }
486
487 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
488 }
489
490 return 0;
491}
Simon Glass62b535e2021-01-13 20:29:46 -0700492
493int video_sync_copy_all(struct udevice *dev)
494{
495 struct video_priv *priv = dev_get_uclass_priv(dev);
496
497 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
498
499 return 0;
500}
501
Simon Glass73c9c372020-07-02 21:12:20 -0600502#endif
503
Simon Glass87a3cd72021-11-19 13:24:03 -0700504#define SPLASH_DECL(_name) \
505 extern u8 __splash_ ## _name ## _begin[]; \
506 extern u8 __splash_ ## _name ## _end[]
507
508#define SPLASH_START(_name) __splash_ ## _name ## _begin
509
510SPLASH_DECL(u_boot_logo);
511
Simon Glass22477422022-10-06 08:36:09 -0600512void *video_get_u_boot_logo(void)
513{
514 return SPLASH_START(u_boot_logo);
515}
516
Simon Glass87a3cd72021-11-19 13:24:03 -0700517static int show_splash(struct udevice *dev)
518{
519 u8 *data = SPLASH_START(u_boot_logo);
520 int ret;
521
522 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
523
524 return 0;
525}
526
Simon Glass70459902022-10-06 08:36:18 -0600527int video_default_font_height(struct udevice *dev)
528{
529 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
530
531 if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
532 return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
533 CONFIG_CONSOLE_TRUETYPE_SIZE);
534
535 return vc_priv->y_charsize;
536}
537
Simon Glass623d28f2016-01-18 19:52:15 -0700538/* Set up the display ready for use */
539static int video_post_probe(struct udevice *dev)
540{
Simon Glassb75b15b2020-12-03 16:55:23 -0700541 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700542 struct video_priv *priv = dev_get_uclass_priv(dev);
543 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700544 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700545 struct udevice *cons;
546 int ret;
547
548 /* Set up the line and display size */
549 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700550 if (!priv->line_length)
551 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
552
Simon Glass623d28f2016-01-18 19:52:15 -0700553 priv->fb_size = priv->line_length * priv->ysize;
554
Simon Glassb4928e52020-07-02 21:12:21 -0600555 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
556 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
557
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100558 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700559 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400560
561 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
562 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700563
Simon Glass84c7fb32016-01-18 19:52:17 -0700564 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700565 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700566 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700567 * for boards that don't need to display text. We create a TrueType
568 * console if enabled, a rotated console if the video driver requests
569 * it, otherwise a normal console.
570 *
571 * The console can be override by setting vidconsole_drv_name before
572 * probing this video driver, or in the probe() method.
573 *
574 * TrueType does not support rotation at present so fall back to the
575 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700576 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700577 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700578 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
579 strcpy(drv, "vidconsole_tt");
580 } else {
581 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
582 priv->rot);
583 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
584 }
585
Simon Glass84c7fb32016-01-18 19:52:17 -0700586 str = strdup(name);
587 if (!str)
588 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700589 if (priv->vidconsole_drv_name)
590 drv_name = priv->vidconsole_drv_name;
591 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700592 if (ret) {
593 debug("%s: Cannot bind console driver\n", __func__);
594 return ret;
595 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700596
Simon Glass84c7fb32016-01-18 19:52:17 -0700597 ret = device_probe(cons);
598 if (ret) {
599 debug("%s: Cannot probe console driver\n", __func__);
600 return ret;
601 }
602
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530603 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
604 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass87a3cd72021-11-19 13:24:03 -0700605 ret = show_splash(dev);
606 if (ret) {
607 log_debug("Cannot show splash screen\n");
608 return ret;
609 }
610 }
611
Simon Glass623d28f2016-01-18 19:52:15 -0700612 return 0;
613};
614
615/* Post-relocation, allocate memory for the frame buffer */
616static int video_post_bind(struct udevice *dev)
617{
Simon Glass951255d2020-07-02 21:12:32 -0600618 struct video_uc_priv *uc_priv;
619 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700620 ulong size;
621
622 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400623 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700624 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600625
626 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700627 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600628 if (!uc_priv->video_ptr)
629 uc_priv->video_ptr = gd->video_top;
630
631 /* Allocate framebuffer space for this device */
632 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700633 size = alloc_fb(dev, &addr);
634 if (addr < gd->video_bottom) {
Bin Mengacbafdd2023-07-23 12:40:24 +0800635 /*
636 * Device tree node may need the 'bootph-all' or
Simon Glassfc1aa352023-02-13 08:56:34 -0700637 * 'bootph-some-ram' tag
Patrick Delaunayd1937182019-05-21 19:19:12 +0200638 */
Bin Mengacbafdd2023-07-23 12:40:24 +0800639 printf("Video device '%s' cannot allocate frame buffer memory "
640 "- ensure the device is set up before relocation\n",
Simon Glass623d28f2016-01-18 19:52:15 -0700641 dev->name);
642 return -ENOSPC;
643 }
644 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
645 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600646 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700647
648 return 0;
649}
650
651UCLASS_DRIVER(video) = {
652 .id = UCLASS_VIDEO,
653 .name = "video",
654 .flags = DM_UC_FLAG_SEQ_ALIAS,
655 .post_bind = video_post_bind,
Simon Glass623d28f2016-01-18 19:52:15 -0700656 .post_probe = video_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700657 .priv_auto = sizeof(struct video_uc_priv),
658 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700659 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glass623d28f2016-01-18 19:52:15 -0700660};