blob: 8f268fc4063f89e4d2d3260cc81d235244cc1172 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass1acafc72016-01-18 19:52:15 -07002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glass1acafc72016-01-18 19:52:15 -07004 */
5
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_VIDEO
7
Simon Glass1acafc72016-01-18 19:52:15 -07008#include <common.h>
Nikhil M Jain5bc610a2023-07-18 14:27:31 +05309#include <bloblist.h>
Simon Glass9beac5d2020-07-02 21:12:20 -060010#include <console.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass1acafc72016-01-18 19:52:15 -070012#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glass1acafc72016-01-18 19:52:15 -070015#include <mapmem.h>
Nikhil M Jain5bc610a2023-07-18 14:27:31 +053016#include <spl.h>
Simon Glass1acafc72016-01-18 19:52:15 -070017#include <stdio_dev.h>
18#include <video.h>
19#include <video_console.h>
Simon Glass90526e92020-05-10 11:39:56 -060020#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glass1acafc72016-01-18 19:52:15 -070022#include <dm/lists.h>
Michal Simek9de731f2020-12-14 08:47:52 +010023#include <dm/device_compat.h>
Simon Glass1acafc72016-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 Glass8a8d24b2020-12-03 16:55:23 -070034 * set the @align and @size values in struct video_uc_plat. This
Simon Glass1acafc72016-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árbd0df822022-03-09 20:46:00 +010038 * method after relocation. Additionally driver can allocate frame buffer
39 * itself by setting plat->base.
Simon Glass1acafc72016-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 Glass7812bbd2020-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 Glassa032e4b2022-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 Glass68dcdc92016-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 Glass315e3672023-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 Glass1acafc72016-01-18 19:52:15 -070096static ulong alloc_fb(struct udevice *dev, ulong *addrp)
97{
Simon Glass8a8d24b2020-12-03 16:55:23 -070098 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass315e3672023-03-10 12:47:17 -080099 ulong size;
Simon Glass1acafc72016-01-18 19:52:15 -0700100
Simon Glass315e3672023-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 Meng39683982016-10-09 04:14:17 -0700108 return 0;
Simon Glass315e3672023-03-10 12:47:17 -0800109 }
Bin Meng39683982016-10-09 04:14:17 -0700110
Pali Rohárbd0df822022-03-09 20:46:00 +0100111 /* Allow drivers to allocate the frame buffer themselves */
112 if (plat->base)
113 return 0;
114
Simon Glass315e3672023-03-10 12:47:17 -0800115 size = alloc_fb_(plat->align, plat->size, addrp);
116 plat->base = *addrp;
Simon Glass1acafc72016-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
126 gd->video_top = *addrp;
127 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
128 dev;
129 uclass_find_next_device(&dev)) {
130 size = alloc_fb(dev, addrp);
131 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
132 __func__, size, *addrp, dev->name);
133 }
Simon Glass551ca0e2020-07-02 21:12:33 -0600134
135 /* Allocate space for PCI video devices in case there were not bound */
136 if (*addrp == gd->video_top)
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530137 *addrp -= CONFIG_VAL(VIDEO_PCI_DEFAULT_FB_SIZE);
Simon Glass551ca0e2020-07-02 21:12:33 -0600138
Simon Glass1acafc72016-01-18 19:52:15 -0700139 gd->video_bottom = *addrp;
Simon Glassaef43ea2020-05-10 14:17:01 -0600140 gd->fb_base = *addrp;
Simon Glass1acafc72016-01-18 19:52:15 -0700141 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
142 gd->video_top);
143
Nikhil M Jain5bc610a2023-07-18 14:27:31 +0530144 if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(BLOBLIST)) {
145 struct video_handoff *ho;
146
147 ho = bloblist_add(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho), 0);
148 if (!ho)
149 return log_msg_ret("blf", -ENOENT);
150 ho->fb = *addrp;
151 ho->size = size;
152 }
153
Simon Glass1acafc72016-01-18 19:52:15 -0700154 return 0;
155}
156
Simon Glass0ab4f912023-06-01 10:22:33 -0600157int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
158 int yend, u32 colour)
159{
160 struct video_priv *priv = dev_get_uclass_priv(dev);
161 void *start, *line;
162 int pixels = xend - xstart;
163 int row, i, ret;
164
165 start = priv->fb + ystart * priv->line_length;
166 start += xstart * VNBYTES(priv->bpix);
167 line = start;
168 for (row = ystart; row < yend; row++) {
169 switch (priv->bpix) {
170 case VIDEO_BPP8: {
171 u8 *dst = line;
172
173 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
174 for (i = 0; i < pixels; i++)
175 *dst++ = colour;
176 }
177 break;
178 }
179 case VIDEO_BPP16: {
180 u16 *dst = line;
181
182 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
183 for (i = 0; i < pixels; i++)
184 *dst++ = colour;
185 }
186 break;
187 }
188 case VIDEO_BPP32: {
189 u32 *dst = line;
190
191 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
192 for (i = 0; i < pixels; i++)
193 *dst++ = colour;
194 }
195 break;
196 }
197 default:
198 return -ENOSYS;
199 }
200 line += priv->line_length;
201 }
202 ret = video_sync_copy(dev, start, line);
203 if (ret)
204 return ret;
205
206 return 0;
207}
208
Nikhil M Jainccd21ee2023-07-18 14:27:30 +0530209int video_reserve_from_bloblist(struct video_handoff *ho)
210{
211 gd->video_bottom = ho->fb;
212 gd->fb_base = ho->fb;
213 gd->video_top = ho->fb + ho->size;
214 debug("Reserving %luk for video using blob at: %08x\n",
215 ((unsigned long)ho->size) >> 10, (u32)ho->fb);
216
217 return 0;
218}
219
Simon Glass50d562c2022-10-06 08:36:08 -0600220int video_fill(struct udevice *dev, u32 colour)
Simon Glass1acafc72016-01-18 19:52:15 -0700221{
222 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glass138dfea2020-07-02 21:12:22 -0600223 int ret;
Simon Glass1acafc72016-01-18 19:52:15 -0700224
Heinrich Schuchardtd7a75d32018-02-08 21:47:10 +0100225 switch (priv->bpix) {
Simon Glass0c20aaf2019-12-20 18:10:37 -0700226 case VIDEO_BPP16:
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530227 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass0c20aaf2019-12-20 18:10:37 -0700228 u16 *ppix = priv->fb;
229 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardtd7a75d32018-02-08 21:47:10 +0100230
Simon Glass0c20aaf2019-12-20 18:10:37 -0700231 while (ppix < end)
Simon Glass50d562c2022-10-06 08:36:08 -0600232 *ppix++ = colour;
Simon Glass0c20aaf2019-12-20 18:10:37 -0700233 break;
234 }
235 case VIDEO_BPP32:
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530236 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Simon Glass0c20aaf2019-12-20 18:10:37 -0700237 u32 *ppix = priv->fb;
238 u32 *end = priv->fb + priv->fb_size;
Simon Glass1acafc72016-01-18 19:52:15 -0700239
Simon Glass0c20aaf2019-12-20 18:10:37 -0700240 while (ppix < end)
Simon Glass50d562c2022-10-06 08:36:08 -0600241 *ppix++ = colour;
Simon Glass0c20aaf2019-12-20 18:10:37 -0700242 break;
243 }
Heinrich Schuchardtd7a75d32018-02-08 21:47:10 +0100244 default:
Simon Glass50d562c2022-10-06 08:36:08 -0600245 memset(priv->fb, colour, priv->fb_size);
Heinrich Schuchardtd7a75d32018-02-08 21:47:10 +0100246 break;
Simon Glass1acafc72016-01-18 19:52:15 -0700247 }
Simon Glass138dfea2020-07-02 21:12:22 -0600248 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
249 if (ret)
250 return ret;
Simon Glassc6ebd012018-10-01 12:22:26 -0600251
Michal Simek53376632020-12-15 15:12:09 +0100252 return video_sync(dev, false);
Simon Glass1acafc72016-01-18 19:52:15 -0700253}
254
Simon Glass50d562c2022-10-06 08:36:08 -0600255int video_clear(struct udevice *dev)
256{
257 struct video_priv *priv = dev_get_uclass_priv(dev);
258 int ret;
259
260 ret = video_fill(dev, priv->colour_bg);
261 if (ret)
262 return ret;
263
264 return 0;
265}
266
Simon Glassa032e4b2022-10-06 08:36:03 -0600267static const struct vid_rgb colours[VID_COLOUR_COUNT] = {
268 { 0x00, 0x00, 0x00 }, /* black */
269 { 0xc0, 0x00, 0x00 }, /* red */
270 { 0x00, 0xc0, 0x00 }, /* green */
271 { 0xc0, 0x60, 0x00 }, /* brown */
272 { 0x00, 0x00, 0xc0 }, /* blue */
273 { 0xc0, 0x00, 0xc0 }, /* magenta */
274 { 0x00, 0xc0, 0xc0 }, /* cyan */
275 { 0xc0, 0xc0, 0xc0 }, /* light gray */
276 { 0x80, 0x80, 0x80 }, /* gray */
277 { 0xff, 0x00, 0x00 }, /* bright red */
278 { 0x00, 0xff, 0x00 }, /* bright green */
279 { 0xff, 0xff, 0x00 }, /* yellow */
280 { 0x00, 0x00, 0xff }, /* bright blue */
281 { 0xff, 0x00, 0xff }, /* bright magenta */
282 { 0x00, 0xff, 0xff }, /* bright cyan */
283 { 0xff, 0xff, 0xff }, /* white */
284};
285
Simon Glass2d6ee922023-06-01 10:22:48 -0600286u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx)
Simon Glassa032e4b2022-10-06 08:36:03 -0600287{
288 switch (priv->bpix) {
289 case VIDEO_BPP16:
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530290 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glassa032e4b2022-10-06 08:36:03 -0600291 return ((colours[idx].r >> 3) << 11) |
292 ((colours[idx].g >> 2) << 5) |
293 ((colours[idx].b >> 3) << 0);
294 }
295 break;
296 case VIDEO_BPP32:
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530297 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Michal Simeke9500ba2023-05-17 10:42:07 +0200298 switch (priv->format) {
299 case VIDEO_X2R10G10B10:
Simon Glassa032e4b2022-10-06 08:36:03 -0600300 return (colours[idx].r << 22) |
301 (colours[idx].g << 12) |
302 (colours[idx].b << 2);
Michal Simeke9500ba2023-05-17 10:42:07 +0200303 case VIDEO_RGBA8888:
304 return (colours[idx].r << 24) |
305 (colours[idx].g << 16) |
306 (colours[idx].b << 8) | 0xff;
307 default:
Simon Glassa032e4b2022-10-06 08:36:03 -0600308 return (colours[idx].r << 16) |
309 (colours[idx].g << 8) |
310 (colours[idx].b << 0);
Michal Simeke9500ba2023-05-17 10:42:07 +0200311 }
Simon Glassa032e4b2022-10-06 08:36:03 -0600312 }
313 break;
314 default:
315 break;
316 }
317
318 /*
319 * For unknown bit arrangements just support
320 * black and white.
321 */
322 if (idx)
323 return 0xffffff; /* white */
324
325 return 0x000000; /* black */
326}
327
Simon Glassb9f210a2018-11-06 15:21:36 -0700328void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100329{
Simon Glassb9f210a2018-11-06 15:21:36 -0700330 struct video_priv *priv = dev_get_uclass_priv(dev);
331 int fore, back;
332
Simon Glass0c20aaf2019-12-20 18:10:37 -0700333 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
334 /* White is used when switching to bold, use light gray here */
335 fore = VID_LIGHT_GRAY;
336 back = VID_BLACK;
337 } else {
338 fore = VID_BLACK;
339 back = VID_WHITE;
340 }
Simon Glassb9f210a2018-11-06 15:21:36 -0700341 if (invert) {
342 int temp;
343
344 temp = fore;
345 fore = back;
346 back = temp;
347 }
348 priv->fg_col_idx = fore;
Andre Przywaraeabb0722019-03-23 01:29:56 +0000349 priv->bg_col_idx = back;
Simon Glassa032e4b2022-10-06 08:36:03 -0600350 priv->colour_fg = video_index_to_colour(priv, fore);
351 priv->colour_bg = video_index_to_colour(priv, back);
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100352}
353
Simon Glass1acafc72016-01-18 19:52:15 -0700354/* Flush video activity to the caches */
Michal Simek9de731f2020-12-14 08:47:52 +0100355int video_sync(struct udevice *vid, bool force)
Simon Glass1acafc72016-01-18 19:52:15 -0700356{
Michal Simek9d69c2d2020-12-03 09:30:00 +0100357 struct video_ops *ops = video_get_ops(vid);
358 int ret;
359
360 if (ops && ops->video_sync) {
361 ret = ops->video_sync(vid);
362 if (ret)
363 return ret;
364 }
365
Simon Glass1acafc72016-01-18 19:52:15 -0700366 /*
367 * flush_dcache_range() is declared in common.h but it seems that some
368 * architectures do not actually implement it. Is there a way to find
369 * out whether it exists? For now, ARM is safe.
370 */
Trevor Woerner10015022019-05-03 09:41:00 -0400371#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass1acafc72016-01-18 19:52:15 -0700372 struct video_priv *priv = dev_get_uclass_priv(vid);
373
374 if (priv->flush_dcache) {
375 flush_dcache_range((ulong)priv->fb,
Simon Glass79813942016-11-13 14:22:06 -0700376 ALIGN((ulong)priv->fb + priv->fb_size,
377 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass1acafc72016-01-18 19:52:15 -0700378 }
379#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
380 struct video_priv *priv = dev_get_uclass_priv(vid);
381 static ulong last_sync;
382
Simon Glass7c19e4c2022-02-28 15:13:48 -0700383 if (force || get_timer(last_sync) > 100) {
Simon Glass1acafc72016-01-18 19:52:15 -0700384 sandbox_sdl_sync(priv->fb);
385 last_sync = get_timer(0);
386 }
387#endif
Michal Simek9de731f2020-12-14 08:47:52 +0100388 return 0;
Simon Glass1acafc72016-01-18 19:52:15 -0700389}
390
391void video_sync_all(void)
392{
393 struct udevice *dev;
Michal Simek9de731f2020-12-14 08:47:52 +0100394 int ret;
Simon Glass1acafc72016-01-18 19:52:15 -0700395
396 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
397 dev;
398 uclass_find_next_device(&dev)) {
Michal Simek9de731f2020-12-14 08:47:52 +0100399 if (device_active(dev)) {
400 ret = video_sync(dev, true);
401 if (ret)
402 dev_dbg(dev, "Video sync failed\n");
403 }
Simon Glass1acafc72016-01-18 19:52:15 -0700404 }
405}
406
Patrick Delaunay2e2e6d82021-11-15 16:32:20 +0100407bool video_is_active(void)
408{
409 struct udevice *dev;
410
411 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 Glass1acafc72016-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 Glass9beac5d2020-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 Glass6a19e932021-11-19 13:23:51 -0700458 char str[120];
Simon Glass9beac5d2020-07-02 21:12:20 -0600459
460 snprintf(str, sizeof(str),
Simon Glass6a19e932021-11-19 13:23:51 -0700461 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
Simon Glass9beac5d2020-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 Glass7d701162021-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 Glass9beac5d2020-07-02 21:12:20 -0600496#endif
497
Simon Glass84e63ab2021-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 Glass0d389012022-10-06 08:36:09 -0600506void *video_get_u_boot_logo(void)
507{
508 return SPLASH_START(u_boot_logo);
509}
510
Simon Glass84e63ab2021-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 Glassc830e282022-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 Glass1acafc72016-01-18 19:52:15 -0700532/* Set up the display ready for use */
533static int video_post_probe(struct udevice *dev)
534{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700535 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass1acafc72016-01-18 19:52:15 -0700536 struct video_priv *priv = dev_get_uclass_priv(dev);
537 char name[30], drv[15], *str;
Simon Glass826f35f2016-01-14 18:10:48 -0700538 const char *drv_name = drv;
Simon Glass1acafc72016-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 Glass06696eb2018-11-29 15:08:52 -0700544 if (!priv->line_length)
545 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
546
Simon Glass1acafc72016-01-18 19:52:15 -0700547 priv->fb_size = priv->line_length * priv->ysize;
548
Simon Glass6efa8092020-07-02 21:12:21 -0600549 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
550 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
551
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100552 /* Set up colors */
Simon Glassb9f210a2018-11-06 15:21:36 -0700553 video_set_default_colors(dev, false);
Rob Clark8ef05352017-08-03 12:47:01 -0400554
555 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
556 video_clear(dev);
Simon Glass1acafc72016-01-18 19:52:15 -0700557
Simon Glass83510762016-01-18 19:52:17 -0700558 /*
Simon Glass826f35f2016-01-14 18:10:48 -0700559 * Create a text console device. For now we always do this, although
Simon Glass83510762016-01-18 19:52:17 -0700560 * it might be useful to support only bitmap drawing on the device
Simon Glass826f35f2016-01-14 18:10:48 -0700561 * for boards that don't need to display text. We create a TrueType
562 * console if enabled, a rotated console if the video driver requests
563 * it, otherwise a normal console.
564 *
565 * The console can be override by setting vidconsole_drv_name before
566 * probing this video driver, or in the probe() method.
567 *
568 * TrueType does not support rotation at present so fall back to the
569 * rotated console in that case.
Simon Glass83510762016-01-18 19:52:17 -0700570 */
Simon Glass826f35f2016-01-14 18:10:48 -0700571 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glassa29b0122016-01-14 18:10:42 -0700572 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
573 strcpy(drv, "vidconsole_tt");
574 } else {
575 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
576 priv->rot);
577 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
578 }
579
Simon Glass83510762016-01-18 19:52:17 -0700580 str = strdup(name);
581 if (!str)
582 return -ENOMEM;
Simon Glass826f35f2016-01-14 18:10:48 -0700583 if (priv->vidconsole_drv_name)
584 drv_name = priv->vidconsole_drv_name;
585 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass83510762016-01-18 19:52:17 -0700586 if (ret) {
587 debug("%s: Cannot bind console driver\n", __func__);
588 return ret;
589 }
Simon Glass826f35f2016-01-14 18:10:48 -0700590
Simon Glass83510762016-01-18 19:52:17 -0700591 ret = device_probe(cons);
592 if (ret) {
593 debug("%s: Cannot probe console driver\n", __func__);
594 return ret;
595 }
596
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530597 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
598 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass84e63ab2021-11-19 13:24:03 -0700599 ret = show_splash(dev);
600 if (ret) {
601 log_debug("Cannot show splash screen\n");
602 return ret;
603 }
604 }
605
Simon Glass1acafc72016-01-18 19:52:15 -0700606 return 0;
607};
608
609/* Post-relocation, allocate memory for the frame buffer */
610static int video_post_bind(struct udevice *dev)
611{
Simon Glass7812bbd2020-07-02 21:12:32 -0600612 struct video_uc_priv *uc_priv;
613 ulong addr;
Simon Glass1acafc72016-01-18 19:52:15 -0700614 ulong size;
615
616 /* Before relocation there is nothing to do here */
Tom Rini75164182018-04-22 09:47:48 -0400617 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass1acafc72016-01-18 19:52:15 -0700618 return 0;
Simon Glass7812bbd2020-07-02 21:12:32 -0600619
620 /* Set up the video pointer, if this is the first device */
Simon Glass0fd3d912020-12-22 19:30:28 -0700621 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass7812bbd2020-07-02 21:12:32 -0600622 if (!uc_priv->video_ptr)
623 uc_priv->video_ptr = gd->video_top;
624
625 /* Allocate framebuffer space for this device */
626 addr = uc_priv->video_ptr;
Simon Glass1acafc72016-01-18 19:52:15 -0700627 size = alloc_fb(dev, &addr);
628 if (addr < gd->video_bottom) {
Bin Meng08ece5b2023-07-23 12:40:24 +0800629 /*
630 * Device tree node may need the 'bootph-all' or
Simon Glasse316fba2023-02-13 08:56:34 -0700631 * 'bootph-some-ram' tag
Patrick Delaunay69989742019-05-21 19:19:12 +0200632 */
Bin Meng08ece5b2023-07-23 12:40:24 +0800633 printf("Video device '%s' cannot allocate frame buffer memory "
634 "- ensure the device is set up before relocation\n",
Simon Glass1acafc72016-01-18 19:52:15 -0700635 dev->name);
636 return -ENOSPC;
637 }
638 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
639 __func__, size, addr, dev->name);
Simon Glass7812bbd2020-07-02 21:12:32 -0600640 uc_priv->video_ptr = addr;
Simon Glass1acafc72016-01-18 19:52:15 -0700641
642 return 0;
643}
644
645UCLASS_DRIVER(video) = {
646 .id = UCLASS_VIDEO,
647 .name = "video",
648 .flags = DM_UC_FLAG_SEQ_ALIAS,
649 .post_bind = video_post_bind,
Simon Glass1acafc72016-01-18 19:52:15 -0700650 .post_probe = video_post_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700651 .priv_auto = sizeof(struct video_uc_priv),
652 .per_device_auto = sizeof(struct video_priv),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700653 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glass1acafc72016-01-18 19:52:15 -0700654};