blob: 9f8cf6ef2a9f324705a869c6da831b25e389418e [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()
36 * method after relocation.
37 *
38 * This information is then picked up by video_reserve() which works out how
39 * much memory is needed for all devices. This is allocated between
40 * gd->video_bottom and gd->video_top.
41 *
42 * After relocation the same process occurs. The driver supplies the same
43 * @size and @align information and this time video_post_bind() checks that
44 * the drivers does not overflow the allocated memory.
45 *
46 * The frame buffer address is actually set (to plat->base) in
47 * video_post_probe(). This function also clears the frame buffer and
48 * allocates a suitable text console device. This can then be used to write
49 * text to the video device.
50 */
51DECLARE_GLOBAL_DATA_PTR;
52
Simon Glass951255d2020-07-02 21:12:32 -060053/**
54 * struct video_uc_priv - Information for the video uclass
55 *
56 * @video_ptr: Current allocation position of the video framebuffer pointer.
57 * While binding devices after relocation, this points to the next
58 * available address to use for a device's framebuffer. It starts at
59 * gd->video_top and works downwards, running out of space when it hits
60 * gd->video_bottom.
61 */
62struct video_uc_priv {
63 ulong video_ptr;
64};
65
Simon Glass64635382016-01-21 19:44:52 -070066void video_set_flush_dcache(struct udevice *dev, bool flush)
67{
68 struct video_priv *priv = dev_get_uclass_priv(dev);
69
70 priv->flush_dcache = flush;
71}
72
Simon Glass623d28f2016-01-18 19:52:15 -070073static ulong alloc_fb(struct udevice *dev, ulong *addrp)
74{
Simon Glassb75b15b2020-12-03 16:55:23 -070075 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -070076 ulong base, align, size;
77
Bin Meng755623d2016-10-09 04:14:17 -070078 if (!plat->size)
79 return 0;
80
Simon Glass623d28f2016-01-18 19:52:15 -070081 align = plat->align ? plat->align : 1 << 20;
82 base = *addrp - plat->size;
83 base &= ~(align - 1);
84 plat->base = base;
85 size = *addrp - base;
86 *addrp = base;
87
88 return size;
89}
90
91int video_reserve(ulong *addrp)
92{
93 struct udevice *dev;
94 ulong size;
95
96 gd->video_top = *addrp;
97 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
98 dev;
99 uclass_find_next_device(&dev)) {
100 size = alloc_fb(dev, addrp);
101 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
102 __func__, size, *addrp, dev->name);
103 }
Simon Glassc3d2f352020-07-02 21:12:33 -0600104
105 /* Allocate space for PCI video devices in case there were not bound */
106 if (*addrp == gd->video_top)
107 *addrp -= CONFIG_VIDEO_PCI_DEFAULT_FB_SIZE;
108
Simon Glass623d28f2016-01-18 19:52:15 -0700109 gd->video_bottom = *addrp;
Simon Glassb2b29612020-05-10 14:17:01 -0600110 gd->fb_base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700111 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
112 gd->video_top);
113
114 return 0;
115}
116
Simon Glass55343122018-10-01 12:22:26 -0600117int video_clear(struct udevice *dev)
Simon Glass623d28f2016-01-18 19:52:15 -0700118{
119 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf8ec6212020-07-02 21:12:22 -0600120 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700121
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100122 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700123 case VIDEO_BPP16:
124 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
125 u16 *ppix = priv->fb;
126 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100127
Simon Glass05c17d62019-12-20 18:10:37 -0700128 while (ppix < end)
129 *ppix++ = priv->colour_bg;
130 break;
131 }
132 case VIDEO_BPP32:
133 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
134 u32 *ppix = priv->fb;
135 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700136
Simon Glass05c17d62019-12-20 18:10:37 -0700137 while (ppix < end)
138 *ppix++ = priv->colour_bg;
139 break;
140 }
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100141 default:
Simon Glass623d28f2016-01-18 19:52:15 -0700142 memset(priv->fb, priv->colour_bg, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100143 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700144 }
Simon Glassf8ec6212020-07-02 21:12:22 -0600145 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
146 if (ret)
147 return ret;
Simon Glass55343122018-10-01 12:22:26 -0600148
Michal Simeka1e136d2020-12-15 15:12:09 +0100149 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700150}
151
Simon Glass2b063b82018-11-06 15:21:36 -0700152void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100153{
Simon Glass2b063b82018-11-06 15:21:36 -0700154 struct video_priv *priv = dev_get_uclass_priv(dev);
155 int fore, back;
156
Simon Glass05c17d62019-12-20 18:10:37 -0700157 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
158 /* White is used when switching to bold, use light gray here */
159 fore = VID_LIGHT_GRAY;
160 back = VID_BLACK;
161 } else {
162 fore = VID_BLACK;
163 back = VID_WHITE;
164 }
Simon Glass2b063b82018-11-06 15:21:36 -0700165 if (invert) {
166 int temp;
167
168 temp = fore;
169 fore = back;
170 back = temp;
171 }
172 priv->fg_col_idx = fore;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000173 priv->bg_col_idx = back;
Simon Glass2b063b82018-11-06 15:21:36 -0700174 priv->colour_fg = vid_console_color(priv, fore);
175 priv->colour_bg = vid_console_color(priv, back);
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100176}
177
Simon Glass623d28f2016-01-18 19:52:15 -0700178/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100179int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700180{
Michal Simek8ae95df2020-12-03 09:30:00 +0100181 struct video_ops *ops = video_get_ops(vid);
182 int ret;
183
184 if (ops && ops->video_sync) {
185 ret = ops->video_sync(vid);
186 if (ret)
187 return ret;
188 }
189
Simon Glass623d28f2016-01-18 19:52:15 -0700190 /*
191 * flush_dcache_range() is declared in common.h but it seems that some
192 * architectures do not actually implement it. Is there a way to find
193 * out whether it exists? For now, ARM is safe.
194 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400195#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass623d28f2016-01-18 19:52:15 -0700196 struct video_priv *priv = dev_get_uclass_priv(vid);
197
198 if (priv->flush_dcache) {
199 flush_dcache_range((ulong)priv->fb,
Simon Glasseebc3fd2016-11-13 14:22:06 -0700200 ALIGN((ulong)priv->fb + priv->fb_size,
201 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass623d28f2016-01-18 19:52:15 -0700202 }
203#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
204 struct video_priv *priv = dev_get_uclass_priv(vid);
205 static ulong last_sync;
206
Simon Glass0806dcc2018-10-01 11:55:14 -0600207 if (force || get_timer(last_sync) > 10) {
Simon Glass623d28f2016-01-18 19:52:15 -0700208 sandbox_sdl_sync(priv->fb);
209 last_sync = get_timer(0);
210 }
211#endif
Michal Simek632e3d42020-12-14 08:47:52 +0100212 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700213}
214
215void video_sync_all(void)
216{
217 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100218 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700219
220 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
221 dev;
222 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100223 if (device_active(dev)) {
224 ret = video_sync(dev, true);
225 if (ret)
226 dev_dbg(dev, "Video sync failed\n");
227 }
Simon Glass623d28f2016-01-18 19:52:15 -0700228 }
229}
230
231int video_get_xsize(struct udevice *dev)
232{
233 struct video_priv *priv = dev_get_uclass_priv(dev);
234
235 return priv->xsize;
236}
237
238int video_get_ysize(struct udevice *dev)
239{
240 struct video_priv *priv = dev_get_uclass_priv(dev);
241
242 return priv->ysize;
243}
244
Simon Glass73c9c372020-07-02 21:12:20 -0600245#ifdef CONFIG_VIDEO_COPY
246int video_sync_copy(struct udevice *dev, void *from, void *to)
247{
248 struct video_priv *priv = dev_get_uclass_priv(dev);
249
250 if (priv->copy_fb) {
251 long offset, size;
252
253 /* Find the offset of the first byte to copy */
254 if ((ulong)to > (ulong)from) {
255 size = to - from;
256 offset = from - priv->fb;
257 } else {
258 size = from - to;
259 offset = to - priv->fb;
260 }
261
262 /*
263 * Allow a bit of leeway for valid requests somewhere near the
264 * frame buffer
265 */
266 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
267#ifdef DEBUG
268 char str[80];
269
270 snprintf(str, sizeof(str),
271 "[sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
272 priv->fb, from, to, offset);
273 console_puts_select_stderr(true, str);
274#endif
275 return -EFAULT;
276 }
277
278 /*
279 * Silently crop the memcpy. This allows callers to avoid doing
280 * this themselves. It is common for the end pointer to go a
281 * few lines after the end of the frame buffer, since most of
282 * the update algorithms terminate a line after their last write
283 */
284 if (offset + size > priv->fb_size) {
285 size = priv->fb_size - offset;
286 } else if (offset < 0) {
287 size += offset;
288 offset = 0;
289 }
290
291 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
292 }
293
294 return 0;
295}
Simon Glass62b535e2021-01-13 20:29:46 -0700296
297int video_sync_copy_all(struct udevice *dev)
298{
299 struct video_priv *priv = dev_get_uclass_priv(dev);
300
301 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
302
303 return 0;
304}
305
Simon Glass73c9c372020-07-02 21:12:20 -0600306#endif
307
Simon Glass623d28f2016-01-18 19:52:15 -0700308/* Set up the colour map */
309static int video_pre_probe(struct udevice *dev)
310{
311 struct video_priv *priv = dev_get_uclass_priv(dev);
312
313 priv->cmap = calloc(256, sizeof(ushort));
314 if (!priv->cmap)
315 return -ENOMEM;
316
317 return 0;
318}
319
320static int video_pre_remove(struct udevice *dev)
321{
322 struct video_priv *priv = dev_get_uclass_priv(dev);
323
324 free(priv->cmap);
325
326 return 0;
327}
328
329/* Set up the display ready for use */
330static int video_post_probe(struct udevice *dev)
331{
Simon Glassb75b15b2020-12-03 16:55:23 -0700332 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700333 struct video_priv *priv = dev_get_uclass_priv(dev);
334 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700335 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700336 struct udevice *cons;
337 int ret;
338
339 /* Set up the line and display size */
340 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700341 if (!priv->line_length)
342 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
343
Simon Glass623d28f2016-01-18 19:52:15 -0700344 priv->fb_size = priv->line_length * priv->ysize;
345
Simon Glassb4928e52020-07-02 21:12:21 -0600346 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
347 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
348
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100349 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700350 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400351
352 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
353 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700354
Simon Glass84c7fb32016-01-18 19:52:17 -0700355 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700356 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700357 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700358 * for boards that don't need to display text. We create a TrueType
359 * console if enabled, a rotated console if the video driver requests
360 * it, otherwise a normal console.
361 *
362 * The console can be override by setting vidconsole_drv_name before
363 * probing this video driver, or in the probe() method.
364 *
365 * TrueType does not support rotation at present so fall back to the
366 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700367 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700368 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700369 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
370 strcpy(drv, "vidconsole_tt");
371 } else {
372 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
373 priv->rot);
374 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
375 }
376
Simon Glass84c7fb32016-01-18 19:52:17 -0700377 str = strdup(name);
378 if (!str)
379 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700380 if (priv->vidconsole_drv_name)
381 drv_name = priv->vidconsole_drv_name;
382 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700383 if (ret) {
384 debug("%s: Cannot bind console driver\n", __func__);
385 return ret;
386 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700387
Simon Glass84c7fb32016-01-18 19:52:17 -0700388 ret = device_probe(cons);
389 if (ret) {
390 debug("%s: Cannot probe console driver\n", __func__);
391 return ret;
392 }
393
Simon Glass623d28f2016-01-18 19:52:15 -0700394 return 0;
395};
396
397/* Post-relocation, allocate memory for the frame buffer */
398static int video_post_bind(struct udevice *dev)
399{
Simon Glass951255d2020-07-02 21:12:32 -0600400 struct video_uc_priv *uc_priv;
401 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700402 ulong size;
403
404 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400405 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700406 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600407
408 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700409 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600410 if (!uc_priv->video_ptr)
411 uc_priv->video_ptr = gd->video_top;
412
413 /* Allocate framebuffer space for this device */
414 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700415 size = alloc_fb(dev, &addr);
416 if (addr < gd->video_bottom) {
Patrick Delaunayd1937182019-05-21 19:19:12 +0200417 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
418 * 'u-boot,dm-pre-proper' tag
419 */
Simon Glass623d28f2016-01-18 19:52:15 -0700420 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
421 dev->name);
422 return -ENOSPC;
423 }
424 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
425 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600426 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700427
428 return 0;
429}
430
431UCLASS_DRIVER(video) = {
432 .id = UCLASS_VIDEO,
433 .name = "video",
434 .flags = DM_UC_FLAG_SEQ_ALIAS,
435 .post_bind = video_post_bind,
436 .pre_probe = video_pre_probe,
437 .post_probe = video_post_probe,
438 .pre_remove = video_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700439 .priv_auto = sizeof(struct video_uc_priv),
440 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700441 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glass623d28f2016-01-18 19:52:15 -0700442};