blob: 435dab1f195c40c32659150345add3754a516781 [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>
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>
15#include <stdio_dev.h>
16#include <video.h>
17#include <video_console.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glass623d28f2016-01-18 19:52:15 -070020#include <dm/lists.h>
Michal Simek632e3d42020-12-14 08:47:52 +010021#include <dm/device_compat.h>
Simon Glass623d28f2016-01-18 19:52:15 -070022#include <dm/device-internal.h>
23#include <dm/uclass-internal.h>
24#ifdef CONFIG_SANDBOX
25#include <asm/sdl.h>
26#endif
27
28/*
29 * Theory of operation:
30 *
31 * Before relocation each device is bound. The driver for each device must
Simon Glassb75b15b2020-12-03 16:55:23 -070032 * set the @align and @size values in struct video_uc_plat. This
Simon Glass623d28f2016-01-18 19:52:15 -070033 * information represents the requires size and alignment of the frame buffer
34 * for the device. The values can be an over-estimate but cannot be too
35 * small. The actual values will be suppled (in the same manner) by the bind()
Pali Rohárf204fce2022-03-09 20:46:00 +010036 * method after relocation. Additionally driver can allocate frame buffer
37 * itself by setting plat->base.
Simon Glass623d28f2016-01-18 19:52:15 -070038 *
39 * This information is then picked up by video_reserve() which works out how
40 * much memory is needed for all devices. This is allocated between
41 * gd->video_bottom and gd->video_top.
42 *
43 * After relocation the same process occurs. The driver supplies the same
44 * @size and @align information and this time video_post_bind() checks that
45 * the drivers does not overflow the allocated memory.
46 *
47 * The frame buffer address is actually set (to plat->base) in
48 * video_post_probe(). This function also clears the frame buffer and
49 * allocates a suitable text console device. This can then be used to write
50 * text to the video device.
51 */
52DECLARE_GLOBAL_DATA_PTR;
53
Simon Glass951255d2020-07-02 21:12:32 -060054/**
55 * struct video_uc_priv - Information for the video uclass
56 *
57 * @video_ptr: Current allocation position of the video framebuffer pointer.
58 * While binding devices after relocation, this points to the next
59 * available address to use for a device's framebuffer. It starts at
60 * gd->video_top and works downwards, running out of space when it hits
61 * gd->video_bottom.
62 */
63struct video_uc_priv {
64 ulong video_ptr;
65};
66
Simon Glass2a006332022-10-06 08:36:03 -060067/** struct vid_rgb - Describes a video colour */
68struct vid_rgb {
69 u32 r;
70 u32 g;
71 u32 b;
72};
73
Simon Glass64635382016-01-21 19:44:52 -070074void video_set_flush_dcache(struct udevice *dev, bool flush)
75{
76 struct video_priv *priv = dev_get_uclass_priv(dev);
77
78 priv->flush_dcache = flush;
79}
80
Simon Glassdf865d32023-03-10 12:47:17 -080081static ulong alloc_fb_(ulong align, ulong size, ulong *addrp)
82{
83 ulong base;
84
85 align = align ? align : 1 << 20;
86 base = *addrp - size;
87 base &= ~(align - 1);
88 size = *addrp - base;
89 *addrp = base;
90
91 return size;
92}
93
Simon Glass623d28f2016-01-18 19:52:15 -070094static ulong alloc_fb(struct udevice *dev, ulong *addrp)
95{
Simon Glassb75b15b2020-12-03 16:55:23 -070096 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glassdf865d32023-03-10 12:47:17 -080097 ulong size;
Simon Glass623d28f2016-01-18 19:52:15 -070098
Simon Glassdf865d32023-03-10 12:47:17 -080099 if (!plat->size) {
100 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_size) {
101 size = alloc_fb_(plat->align, plat->copy_size, addrp);
102 plat->copy_base = *addrp;
103 return size;
104 }
105
Bin Meng755623d2016-10-09 04:14:17 -0700106 return 0;
Simon Glassdf865d32023-03-10 12:47:17 -0800107 }
Bin Meng755623d2016-10-09 04:14:17 -0700108
Pali Rohárf204fce2022-03-09 20:46:00 +0100109 /* Allow drivers to allocate the frame buffer themselves */
110 if (plat->base)
111 return 0;
112
Simon Glassdf865d32023-03-10 12:47:17 -0800113 size = alloc_fb_(plat->align, plat->size, addrp);
114 plat->base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700115
116 return size;
117}
118
119int video_reserve(ulong *addrp)
120{
121 struct udevice *dev;
122 ulong size;
123
124 gd->video_top = *addrp;
125 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
126 dev;
127 uclass_find_next_device(&dev)) {
128 size = alloc_fb(dev, addrp);
129 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
130 __func__, size, *addrp, dev->name);
131 }
Simon Glassc3d2f352020-07-02 21:12:33 -0600132
133 /* Allocate space for PCI video devices in case there were not bound */
134 if (*addrp == gd->video_top)
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530135 *addrp -= CONFIG_VAL(VIDEO_PCI_DEFAULT_FB_SIZE);
Simon Glassc3d2f352020-07-02 21:12:33 -0600136
Simon Glass623d28f2016-01-18 19:52:15 -0700137 gd->video_bottom = *addrp;
Simon Glassb2b29612020-05-10 14:17:01 -0600138 gd->fb_base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700139 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
140 gd->video_top);
141
142 return 0;
143}
144
Simon Glass062673b2023-06-01 10:22:33 -0600145int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
146 int yend, u32 colour)
147{
148 struct video_priv *priv = dev_get_uclass_priv(dev);
149 void *start, *line;
150 int pixels = xend - xstart;
151 int row, i, ret;
152
153 start = priv->fb + ystart * priv->line_length;
154 start += xstart * VNBYTES(priv->bpix);
155 line = start;
156 for (row = ystart; row < yend; row++) {
157 switch (priv->bpix) {
158 case VIDEO_BPP8: {
159 u8 *dst = line;
160
161 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
162 for (i = 0; i < pixels; i++)
163 *dst++ = colour;
164 }
165 break;
166 }
167 case VIDEO_BPP16: {
168 u16 *dst = line;
169
170 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
171 for (i = 0; i < pixels; i++)
172 *dst++ = colour;
173 }
174 break;
175 }
176 case VIDEO_BPP32: {
177 u32 *dst = line;
178
179 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
180 for (i = 0; i < pixels; i++)
181 *dst++ = colour;
182 }
183 break;
184 }
185 default:
186 return -ENOSYS;
187 }
188 line += priv->line_length;
189 }
190 ret = video_sync_copy(dev, start, line);
191 if (ret)
192 return ret;
193
194 return 0;
195}
196
Nikhil M Jain76833532023-07-18 14:27:30 +0530197int video_reserve_from_bloblist(struct video_handoff *ho)
198{
199 gd->video_bottom = ho->fb;
200 gd->fb_base = ho->fb;
201 gd->video_top = ho->fb + ho->size;
202 debug("Reserving %luk for video using blob at: %08x\n",
203 ((unsigned long)ho->size) >> 10, (u32)ho->fb);
204
205 return 0;
206}
207
Simon Glass2baa6f82022-10-06 08:36:08 -0600208int video_fill(struct udevice *dev, u32 colour)
Simon Glass623d28f2016-01-18 19:52:15 -0700209{
210 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf8ec6212020-07-02 21:12:22 -0600211 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700212
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100213 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700214 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530215 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700216 u16 *ppix = priv->fb;
217 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100218
Simon Glass05c17d62019-12-20 18:10:37 -0700219 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600220 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700221 break;
222 }
223 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530224 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Simon Glass05c17d62019-12-20 18:10:37 -0700225 u32 *ppix = priv->fb;
226 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700227
Simon Glass05c17d62019-12-20 18:10:37 -0700228 while (ppix < end)
Simon Glass2baa6f82022-10-06 08:36:08 -0600229 *ppix++ = colour;
Simon Glass05c17d62019-12-20 18:10:37 -0700230 break;
231 }
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100232 default:
Simon Glass2baa6f82022-10-06 08:36:08 -0600233 memset(priv->fb, colour, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100234 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700235 }
Simon Glassf8ec6212020-07-02 21:12:22 -0600236 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
237 if (ret)
238 return ret;
Simon Glass55343122018-10-01 12:22:26 -0600239
Michal Simeka1e136d2020-12-15 15:12:09 +0100240 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700241}
242
Simon Glass2baa6f82022-10-06 08:36:08 -0600243int video_clear(struct udevice *dev)
244{
245 struct video_priv *priv = dev_get_uclass_priv(dev);
246 int ret;
247
248 ret = video_fill(dev, priv->colour_bg);
249 if (ret)
250 return ret;
251
252 return 0;
253}
254
Simon Glass2a006332022-10-06 08:36:03 -0600255static const struct vid_rgb colours[VID_COLOUR_COUNT] = {
256 { 0x00, 0x00, 0x00 }, /* black */
257 { 0xc0, 0x00, 0x00 }, /* red */
258 { 0x00, 0xc0, 0x00 }, /* green */
259 { 0xc0, 0x60, 0x00 }, /* brown */
260 { 0x00, 0x00, 0xc0 }, /* blue */
261 { 0xc0, 0x00, 0xc0 }, /* magenta */
262 { 0x00, 0xc0, 0xc0 }, /* cyan */
263 { 0xc0, 0xc0, 0xc0 }, /* light gray */
264 { 0x80, 0x80, 0x80 }, /* gray */
265 { 0xff, 0x00, 0x00 }, /* bright red */
266 { 0x00, 0xff, 0x00 }, /* bright green */
267 { 0xff, 0xff, 0x00 }, /* yellow */
268 { 0x00, 0x00, 0xff }, /* bright blue */
269 { 0xff, 0x00, 0xff }, /* bright magenta */
270 { 0x00, 0xff, 0xff }, /* bright cyan */
271 { 0xff, 0xff, 0xff }, /* white */
272};
273
Simon Glassd17a6242023-06-01 10:22:48 -0600274u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx)
Simon Glass2a006332022-10-06 08:36:03 -0600275{
276 switch (priv->bpix) {
277 case VIDEO_BPP16:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530278 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
Simon Glass2a006332022-10-06 08:36:03 -0600279 return ((colours[idx].r >> 3) << 11) |
280 ((colours[idx].g >> 2) << 5) |
281 ((colours[idx].b >> 3) << 0);
282 }
283 break;
284 case VIDEO_BPP32:
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530285 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
Michal Simek985eac82023-05-17 10:42:07 +0200286 switch (priv->format) {
287 case VIDEO_X2R10G10B10:
Simon Glass2a006332022-10-06 08:36:03 -0600288 return (colours[idx].r << 22) |
289 (colours[idx].g << 12) |
290 (colours[idx].b << 2);
Michal Simek985eac82023-05-17 10:42:07 +0200291 case VIDEO_RGBA8888:
292 return (colours[idx].r << 24) |
293 (colours[idx].g << 16) |
294 (colours[idx].b << 8) | 0xff;
295 default:
Simon Glass2a006332022-10-06 08:36:03 -0600296 return (colours[idx].r << 16) |
297 (colours[idx].g << 8) |
298 (colours[idx].b << 0);
Michal Simek985eac82023-05-17 10:42:07 +0200299 }
Simon Glass2a006332022-10-06 08:36:03 -0600300 }
301 break;
302 default:
303 break;
304 }
305
306 /*
307 * For unknown bit arrangements just support
308 * black and white.
309 */
310 if (idx)
311 return 0xffffff; /* white */
312
313 return 0x000000; /* black */
314}
315
Simon Glass2b063b82018-11-06 15:21:36 -0700316void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100317{
Simon Glass2b063b82018-11-06 15:21:36 -0700318 struct video_priv *priv = dev_get_uclass_priv(dev);
319 int fore, back;
320
Simon Glass05c17d62019-12-20 18:10:37 -0700321 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
322 /* White is used when switching to bold, use light gray here */
323 fore = VID_LIGHT_GRAY;
324 back = VID_BLACK;
325 } else {
326 fore = VID_BLACK;
327 back = VID_WHITE;
328 }
Simon Glass2b063b82018-11-06 15:21:36 -0700329 if (invert) {
330 int temp;
331
332 temp = fore;
333 fore = back;
334 back = temp;
335 }
336 priv->fg_col_idx = fore;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000337 priv->bg_col_idx = back;
Simon Glass2a006332022-10-06 08:36:03 -0600338 priv->colour_fg = video_index_to_colour(priv, fore);
339 priv->colour_bg = video_index_to_colour(priv, back);
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100340}
341
Simon Glass623d28f2016-01-18 19:52:15 -0700342/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100343int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700344{
Michal Simek8ae95df2020-12-03 09:30:00 +0100345 struct video_ops *ops = video_get_ops(vid);
346 int ret;
347
348 if (ops && ops->video_sync) {
349 ret = ops->video_sync(vid);
350 if (ret)
351 return ret;
352 }
353
Simon Glass623d28f2016-01-18 19:52:15 -0700354 /*
355 * flush_dcache_range() is declared in common.h but it seems that some
356 * architectures do not actually implement it. Is there a way to find
357 * out whether it exists? For now, ARM is safe.
358 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400359#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass623d28f2016-01-18 19:52:15 -0700360 struct video_priv *priv = dev_get_uclass_priv(vid);
361
362 if (priv->flush_dcache) {
363 flush_dcache_range((ulong)priv->fb,
Simon Glasseebc3fd2016-11-13 14:22:06 -0700364 ALIGN((ulong)priv->fb + priv->fb_size,
365 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass623d28f2016-01-18 19:52:15 -0700366 }
367#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
368 struct video_priv *priv = dev_get_uclass_priv(vid);
369 static ulong last_sync;
370
Simon Glassea113732022-02-28 15:13:48 -0700371 if (force || get_timer(last_sync) > 100) {
Simon Glass623d28f2016-01-18 19:52:15 -0700372 sandbox_sdl_sync(priv->fb);
373 last_sync = get_timer(0);
374 }
375#endif
Michal Simek632e3d42020-12-14 08:47:52 +0100376 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700377}
378
379void video_sync_all(void)
380{
381 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100382 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700383
384 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
385 dev;
386 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100387 if (device_active(dev)) {
388 ret = video_sync(dev, true);
389 if (ret)
390 dev_dbg(dev, "Video sync failed\n");
391 }
Simon Glass623d28f2016-01-18 19:52:15 -0700392 }
393}
394
Patrick Delaunayefcc84b2021-11-15 16:32:20 +0100395bool video_is_active(void)
396{
397 struct udevice *dev;
398
399 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
400 dev;
401 uclass_find_next_device(&dev)) {
402 if (device_active(dev))
403 return true;
404 }
405
406 return false;
407}
408
Simon Glass623d28f2016-01-18 19:52:15 -0700409int video_get_xsize(struct udevice *dev)
410{
411 struct video_priv *priv = dev_get_uclass_priv(dev);
412
413 return priv->xsize;
414}
415
416int video_get_ysize(struct udevice *dev)
417{
418 struct video_priv *priv = dev_get_uclass_priv(dev);
419
420 return priv->ysize;
421}
422
Simon Glass73c9c372020-07-02 21:12:20 -0600423#ifdef CONFIG_VIDEO_COPY
424int video_sync_copy(struct udevice *dev, void *from, void *to)
425{
426 struct video_priv *priv = dev_get_uclass_priv(dev);
427
428 if (priv->copy_fb) {
429 long offset, size;
430
431 /* Find the offset of the first byte to copy */
432 if ((ulong)to > (ulong)from) {
433 size = to - from;
434 offset = from - priv->fb;
435 } else {
436 size = from - to;
437 offset = to - priv->fb;
438 }
439
440 /*
441 * Allow a bit of leeway for valid requests somewhere near the
442 * frame buffer
443 */
444 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
445#ifdef DEBUG
Simon Glass78cdc5d2021-11-19 13:23:51 -0700446 char str[120];
Simon Glass73c9c372020-07-02 21:12:20 -0600447
448 snprintf(str, sizeof(str),
Simon Glass78cdc5d2021-11-19 13:23:51 -0700449 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
Simon Glass73c9c372020-07-02 21:12:20 -0600450 priv->fb, from, to, offset);
451 console_puts_select_stderr(true, str);
452#endif
453 return -EFAULT;
454 }
455
456 /*
457 * Silently crop the memcpy. This allows callers to avoid doing
458 * this themselves. It is common for the end pointer to go a
459 * few lines after the end of the frame buffer, since most of
460 * the update algorithms terminate a line after their last write
461 */
462 if (offset + size > priv->fb_size) {
463 size = priv->fb_size - offset;
464 } else if (offset < 0) {
465 size += offset;
466 offset = 0;
467 }
468
469 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
470 }
471
472 return 0;
473}
Simon Glass62b535e2021-01-13 20:29:46 -0700474
475int video_sync_copy_all(struct udevice *dev)
476{
477 struct video_priv *priv = dev_get_uclass_priv(dev);
478
479 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
480
481 return 0;
482}
483
Simon Glass73c9c372020-07-02 21:12:20 -0600484#endif
485
Simon Glass87a3cd72021-11-19 13:24:03 -0700486#define SPLASH_DECL(_name) \
487 extern u8 __splash_ ## _name ## _begin[]; \
488 extern u8 __splash_ ## _name ## _end[]
489
490#define SPLASH_START(_name) __splash_ ## _name ## _begin
491
492SPLASH_DECL(u_boot_logo);
493
Simon Glass22477422022-10-06 08:36:09 -0600494void *video_get_u_boot_logo(void)
495{
496 return SPLASH_START(u_boot_logo);
497}
498
Simon Glass87a3cd72021-11-19 13:24:03 -0700499static int show_splash(struct udevice *dev)
500{
501 u8 *data = SPLASH_START(u_boot_logo);
502 int ret;
503
504 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
505
506 return 0;
507}
508
Simon Glass70459902022-10-06 08:36:18 -0600509int video_default_font_height(struct udevice *dev)
510{
511 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
512
513 if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
514 return IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
515 CONFIG_CONSOLE_TRUETYPE_SIZE);
516
517 return vc_priv->y_charsize;
518}
519
Simon Glass623d28f2016-01-18 19:52:15 -0700520/* Set up the display ready for use */
521static int video_post_probe(struct udevice *dev)
522{
Simon Glassb75b15b2020-12-03 16:55:23 -0700523 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700524 struct video_priv *priv = dev_get_uclass_priv(dev);
525 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700526 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700527 struct udevice *cons;
528 int ret;
529
530 /* Set up the line and display size */
531 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700532 if (!priv->line_length)
533 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
534
Simon Glass623d28f2016-01-18 19:52:15 -0700535 priv->fb_size = priv->line_length * priv->ysize;
536
Simon Glassb4928e52020-07-02 21:12:21 -0600537 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
538 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
539
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100540 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700541 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400542
543 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
544 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700545
Simon Glass84c7fb32016-01-18 19:52:17 -0700546 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700547 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700548 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700549 * for boards that don't need to display text. We create a TrueType
550 * console if enabled, a rotated console if the video driver requests
551 * it, otherwise a normal console.
552 *
553 * The console can be override by setting vidconsole_drv_name before
554 * probing this video driver, or in the probe() method.
555 *
556 * TrueType does not support rotation at present so fall back to the
557 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700558 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700559 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700560 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
561 strcpy(drv, "vidconsole_tt");
562 } else {
563 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
564 priv->rot);
565 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
566 }
567
Simon Glass84c7fb32016-01-18 19:52:17 -0700568 str = strdup(name);
569 if (!str)
570 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700571 if (priv->vidconsole_drv_name)
572 drv_name = priv->vidconsole_drv_name;
573 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700574 if (ret) {
575 debug("%s: Cannot bind console driver\n", __func__);
576 return ret;
577 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700578
Simon Glass84c7fb32016-01-18 19:52:17 -0700579 ret = device_probe(cons);
580 if (ret) {
581 debug("%s: Cannot probe console driver\n", __func__);
582 return ret;
583 }
584
Nikhil M Jain9e3301d2023-04-20 17:41:08 +0530585 if (CONFIG_IS_ENABLED(VIDEO_LOGO) &&
586 !CONFIG_IS_ENABLED(SPLASH_SCREEN) && !plat->hide_logo) {
Simon Glass87a3cd72021-11-19 13:24:03 -0700587 ret = show_splash(dev);
588 if (ret) {
589 log_debug("Cannot show splash screen\n");
590 return ret;
591 }
592 }
593
Simon Glass623d28f2016-01-18 19:52:15 -0700594 return 0;
595};
596
597/* Post-relocation, allocate memory for the frame buffer */
598static int video_post_bind(struct udevice *dev)
599{
Simon Glass951255d2020-07-02 21:12:32 -0600600 struct video_uc_priv *uc_priv;
601 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700602 ulong size;
603
604 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400605 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700606 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600607
608 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700609 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600610 if (!uc_priv->video_ptr)
611 uc_priv->video_ptr = gd->video_top;
612
613 /* Allocate framebuffer space for this device */
614 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700615 size = alloc_fb(dev, &addr);
616 if (addr < gd->video_bottom) {
Simon Glassfc1aa352023-02-13 08:56:34 -0700617 /* Device tree node may need the 'bootph-all' or
618 * 'bootph-some-ram' tag
Patrick Delaunayd1937182019-05-21 19:19:12 +0200619 */
Simon Glass623d28f2016-01-18 19:52:15 -0700620 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
621 dev->name);
622 return -ENOSPC;
623 }
624 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
625 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600626 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700627
628 return 0;
629}
630
631UCLASS_DRIVER(video) = {
632 .id = UCLASS_VIDEO,
633 .name = "video",
634 .flags = DM_UC_FLAG_SEQ_ALIAS,
635 .post_bind = video_post_bind,
Simon Glass623d28f2016-01-18 19:52:15 -0700636 .post_probe = video_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700637 .priv_auto = sizeof(struct video_uc_priv),
638 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700639 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glass623d28f2016-01-18 19:52:15 -0700640};