blob: 96ec6f80af3b78bdbbb1b5df033bd6d58961225a [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
6#include <common.h>
Simon Glass73c9c372020-07-02 21:12:20 -06007#include <console.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass623d28f2016-01-18 19:52:15 -07009#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Simon Glass623d28f2016-01-18 19:52:15 -070012#include <mapmem.h>
13#include <stdio_dev.h>
14#include <video.h>
15#include <video_console.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass623d28f2016-01-18 19:52:15 -070018#include <dm/lists.h>
Michal Simek632e3d42020-12-14 08:47:52 +010019#include <dm/device_compat.h>
Simon Glass623d28f2016-01-18 19:52:15 -070020#include <dm/device-internal.h>
21#include <dm/uclass-internal.h>
22#ifdef CONFIG_SANDBOX
23#include <asm/sdl.h>
24#endif
25
26/*
27 * Theory of operation:
28 *
29 * Before relocation each device is bound. The driver for each device must
Simon Glassb75b15b2020-12-03 16:55:23 -070030 * set the @align and @size values in struct video_uc_plat. This
Simon Glass623d28f2016-01-18 19:52:15 -070031 * information represents the requires size and alignment of the frame buffer
32 * for the device. The values can be an over-estimate but cannot be too
33 * small. The actual values will be suppled (in the same manner) by the bind()
34 * method after relocation.
35 *
36 * This information is then picked up by video_reserve() which works out how
37 * much memory is needed for all devices. This is allocated between
38 * gd->video_bottom and gd->video_top.
39 *
40 * After relocation the same process occurs. The driver supplies the same
41 * @size and @align information and this time video_post_bind() checks that
42 * the drivers does not overflow the allocated memory.
43 *
44 * The frame buffer address is actually set (to plat->base) in
45 * video_post_probe(). This function also clears the frame buffer and
46 * allocates a suitable text console device. This can then be used to write
47 * text to the video device.
48 */
49DECLARE_GLOBAL_DATA_PTR;
50
Simon Glass951255d2020-07-02 21:12:32 -060051/**
52 * struct video_uc_priv - Information for the video uclass
53 *
54 * @video_ptr: Current allocation position of the video framebuffer pointer.
55 * While binding devices after relocation, this points to the next
56 * available address to use for a device's framebuffer. It starts at
57 * gd->video_top and works downwards, running out of space when it hits
58 * gd->video_bottom.
59 */
60struct video_uc_priv {
61 ulong video_ptr;
62};
63
Simon Glass64635382016-01-21 19:44:52 -070064void video_set_flush_dcache(struct udevice *dev, bool flush)
65{
66 struct video_priv *priv = dev_get_uclass_priv(dev);
67
68 priv->flush_dcache = flush;
69}
70
Simon Glass623d28f2016-01-18 19:52:15 -070071static ulong alloc_fb(struct udevice *dev, ulong *addrp)
72{
Simon Glassb75b15b2020-12-03 16:55:23 -070073 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -070074 ulong base, align, size;
75
Bin Meng755623d2016-10-09 04:14:17 -070076 if (!plat->size)
77 return 0;
78
Simon Glass623d28f2016-01-18 19:52:15 -070079 align = plat->align ? plat->align : 1 << 20;
80 base = *addrp - plat->size;
81 base &= ~(align - 1);
82 plat->base = base;
83 size = *addrp - base;
84 *addrp = base;
85
86 return size;
87}
88
89int video_reserve(ulong *addrp)
90{
91 struct udevice *dev;
92 ulong size;
93
94 gd->video_top = *addrp;
95 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
96 dev;
97 uclass_find_next_device(&dev)) {
98 size = alloc_fb(dev, addrp);
99 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
100 __func__, size, *addrp, dev->name);
101 }
Simon Glassc3d2f352020-07-02 21:12:33 -0600102
103 /* Allocate space for PCI video devices in case there were not bound */
104 if (*addrp == gd->video_top)
105 *addrp -= CONFIG_VIDEO_PCI_DEFAULT_FB_SIZE;
106
Simon Glass623d28f2016-01-18 19:52:15 -0700107 gd->video_bottom = *addrp;
Simon Glassb2b29612020-05-10 14:17:01 -0600108 gd->fb_base = *addrp;
Simon Glass623d28f2016-01-18 19:52:15 -0700109 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
110 gd->video_top);
111
112 return 0;
113}
114
Simon Glass55343122018-10-01 12:22:26 -0600115int video_clear(struct udevice *dev)
Simon Glass623d28f2016-01-18 19:52:15 -0700116{
117 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf8ec6212020-07-02 21:12:22 -0600118 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700119
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100120 switch (priv->bpix) {
Simon Glass05c17d62019-12-20 18:10:37 -0700121 case VIDEO_BPP16:
122 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
123 u16 *ppix = priv->fb;
124 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100125
Simon Glass05c17d62019-12-20 18:10:37 -0700126 while (ppix < end)
127 *ppix++ = priv->colour_bg;
128 break;
129 }
130 case VIDEO_BPP32:
131 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
132 u32 *ppix = priv->fb;
133 u32 *end = priv->fb + priv->fb_size;
Simon Glass623d28f2016-01-18 19:52:15 -0700134
Simon Glass05c17d62019-12-20 18:10:37 -0700135 while (ppix < end)
136 *ppix++ = priv->colour_bg;
137 break;
138 }
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100139 default:
Simon Glass623d28f2016-01-18 19:52:15 -0700140 memset(priv->fb, priv->colour_bg, priv->fb_size);
Heinrich Schuchardt5e4947e2018-02-08 21:47:10 +0100141 break;
Simon Glass623d28f2016-01-18 19:52:15 -0700142 }
Simon Glassf8ec6212020-07-02 21:12:22 -0600143 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
144 if (ret)
145 return ret;
Simon Glass55343122018-10-01 12:22:26 -0600146
Michal Simeka1e136d2020-12-15 15:12:09 +0100147 return video_sync(dev, false);
Simon Glass623d28f2016-01-18 19:52:15 -0700148}
149
Simon Glass2b063b82018-11-06 15:21:36 -0700150void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100151{
Simon Glass2b063b82018-11-06 15:21:36 -0700152 struct video_priv *priv = dev_get_uclass_priv(dev);
153 int fore, back;
154
Simon Glass05c17d62019-12-20 18:10:37 -0700155 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
156 /* White is used when switching to bold, use light gray here */
157 fore = VID_LIGHT_GRAY;
158 back = VID_BLACK;
159 } else {
160 fore = VID_BLACK;
161 back = VID_WHITE;
162 }
Simon Glass2b063b82018-11-06 15:21:36 -0700163 if (invert) {
164 int temp;
165
166 temp = fore;
167 fore = back;
168 back = temp;
169 }
170 priv->fg_col_idx = fore;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000171 priv->bg_col_idx = back;
Simon Glass2b063b82018-11-06 15:21:36 -0700172 priv->colour_fg = vid_console_color(priv, fore);
173 priv->colour_bg = vid_console_color(priv, back);
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100174}
175
Simon Glass623d28f2016-01-18 19:52:15 -0700176/* Flush video activity to the caches */
Michal Simek632e3d42020-12-14 08:47:52 +0100177int video_sync(struct udevice *vid, bool force)
Simon Glass623d28f2016-01-18 19:52:15 -0700178{
Michal Simek8ae95df2020-12-03 09:30:00 +0100179 struct video_ops *ops = video_get_ops(vid);
180 int ret;
181
182 if (ops && ops->video_sync) {
183 ret = ops->video_sync(vid);
184 if (ret)
185 return ret;
186 }
187
Simon Glass623d28f2016-01-18 19:52:15 -0700188 /*
189 * flush_dcache_range() is declared in common.h but it seems that some
190 * architectures do not actually implement it. Is there a way to find
191 * out whether it exists? For now, ARM is safe.
192 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400193#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass623d28f2016-01-18 19:52:15 -0700194 struct video_priv *priv = dev_get_uclass_priv(vid);
195
196 if (priv->flush_dcache) {
197 flush_dcache_range((ulong)priv->fb,
Simon Glasseebc3fd2016-11-13 14:22:06 -0700198 ALIGN((ulong)priv->fb + priv->fb_size,
199 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass623d28f2016-01-18 19:52:15 -0700200 }
201#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
202 struct video_priv *priv = dev_get_uclass_priv(vid);
203 static ulong last_sync;
204
Simon Glass0806dcc2018-10-01 11:55:14 -0600205 if (force || get_timer(last_sync) > 10) {
Simon Glass623d28f2016-01-18 19:52:15 -0700206 sandbox_sdl_sync(priv->fb);
207 last_sync = get_timer(0);
208 }
209#endif
Michal Simek632e3d42020-12-14 08:47:52 +0100210 return 0;
Simon Glass623d28f2016-01-18 19:52:15 -0700211}
212
213void video_sync_all(void)
214{
215 struct udevice *dev;
Michal Simek632e3d42020-12-14 08:47:52 +0100216 int ret;
Simon Glass623d28f2016-01-18 19:52:15 -0700217
218 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
219 dev;
220 uclass_find_next_device(&dev)) {
Michal Simek632e3d42020-12-14 08:47:52 +0100221 if (device_active(dev)) {
222 ret = video_sync(dev, true);
223 if (ret)
224 dev_dbg(dev, "Video sync failed\n");
225 }
Simon Glass623d28f2016-01-18 19:52:15 -0700226 }
227}
228
229int video_get_xsize(struct udevice *dev)
230{
231 struct video_priv *priv = dev_get_uclass_priv(dev);
232
233 return priv->xsize;
234}
235
236int video_get_ysize(struct udevice *dev)
237{
238 struct video_priv *priv = dev_get_uclass_priv(dev);
239
240 return priv->ysize;
241}
242
Simon Glass73c9c372020-07-02 21:12:20 -0600243#ifdef CONFIG_VIDEO_COPY
244int video_sync_copy(struct udevice *dev, void *from, void *to)
245{
246 struct video_priv *priv = dev_get_uclass_priv(dev);
247
248 if (priv->copy_fb) {
249 long offset, size;
250
251 /* Find the offset of the first byte to copy */
252 if ((ulong)to > (ulong)from) {
253 size = to - from;
254 offset = from - priv->fb;
255 } else {
256 size = from - to;
257 offset = to - priv->fb;
258 }
259
260 /*
261 * Allow a bit of leeway for valid requests somewhere near the
262 * frame buffer
263 */
264 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
265#ifdef DEBUG
266 char str[80];
267
268 snprintf(str, sizeof(str),
269 "[sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
270 priv->fb, from, to, offset);
271 console_puts_select_stderr(true, str);
272#endif
273 return -EFAULT;
274 }
275
276 /*
277 * Silently crop the memcpy. This allows callers to avoid doing
278 * this themselves. It is common for the end pointer to go a
279 * few lines after the end of the frame buffer, since most of
280 * the update algorithms terminate a line after their last write
281 */
282 if (offset + size > priv->fb_size) {
283 size = priv->fb_size - offset;
284 } else if (offset < 0) {
285 size += offset;
286 offset = 0;
287 }
288
289 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
290 }
291
292 return 0;
293}
Simon Glass62b535e2021-01-13 20:29:46 -0700294
295int video_sync_copy_all(struct udevice *dev)
296{
297 struct video_priv *priv = dev_get_uclass_priv(dev);
298
299 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
300
301 return 0;
302}
303
Simon Glass73c9c372020-07-02 21:12:20 -0600304#endif
305
Simon Glass623d28f2016-01-18 19:52:15 -0700306/* Set up the colour map */
307static int video_pre_probe(struct udevice *dev)
308{
309 struct video_priv *priv = dev_get_uclass_priv(dev);
310
311 priv->cmap = calloc(256, sizeof(ushort));
312 if (!priv->cmap)
313 return -ENOMEM;
314
315 return 0;
316}
317
318static int video_pre_remove(struct udevice *dev)
319{
320 struct video_priv *priv = dev_get_uclass_priv(dev);
321
322 free(priv->cmap);
323
324 return 0;
325}
326
327/* Set up the display ready for use */
328static int video_post_probe(struct udevice *dev)
329{
Simon Glassb75b15b2020-12-03 16:55:23 -0700330 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700331 struct video_priv *priv = dev_get_uclass_priv(dev);
332 char name[30], drv[15], *str;
Simon Glassb3a72b32016-01-14 18:10:48 -0700333 const char *drv_name = drv;
Simon Glass623d28f2016-01-18 19:52:15 -0700334 struct udevice *cons;
335 int ret;
336
337 /* Set up the line and display size */
338 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass7d186732018-11-29 15:08:52 -0700339 if (!priv->line_length)
340 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
341
Simon Glass623d28f2016-01-18 19:52:15 -0700342 priv->fb_size = priv->line_length * priv->ysize;
343
Simon Glassb4928e52020-07-02 21:12:21 -0600344 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
345 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
346
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100347 /* Set up colors */
Simon Glass2b063b82018-11-06 15:21:36 -0700348 video_set_default_colors(dev, false);
Rob Clarkf1411882017-08-03 12:47:01 -0400349
350 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
351 video_clear(dev);
Simon Glass623d28f2016-01-18 19:52:15 -0700352
Simon Glass84c7fb32016-01-18 19:52:17 -0700353 /*
Simon Glassb3a72b32016-01-14 18:10:48 -0700354 * Create a text console device. For now we always do this, although
Simon Glass84c7fb32016-01-18 19:52:17 -0700355 * it might be useful to support only bitmap drawing on the device
Simon Glassb3a72b32016-01-14 18:10:48 -0700356 * for boards that don't need to display text. We create a TrueType
357 * console if enabled, a rotated console if the video driver requests
358 * it, otherwise a normal console.
359 *
360 * The console can be override by setting vidconsole_drv_name before
361 * probing this video driver, or in the probe() method.
362 *
363 * TrueType does not support rotation at present so fall back to the
364 * rotated console in that case.
Simon Glass84c7fb32016-01-18 19:52:17 -0700365 */
Simon Glassb3a72b32016-01-14 18:10:48 -0700366 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glass2ef353e2016-01-14 18:10:42 -0700367 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
368 strcpy(drv, "vidconsole_tt");
369 } else {
370 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
371 priv->rot);
372 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
373 }
374
Simon Glass84c7fb32016-01-18 19:52:17 -0700375 str = strdup(name);
376 if (!str)
377 return -ENOMEM;
Simon Glassb3a72b32016-01-14 18:10:48 -0700378 if (priv->vidconsole_drv_name)
379 drv_name = priv->vidconsole_drv_name;
380 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass84c7fb32016-01-18 19:52:17 -0700381 if (ret) {
382 debug("%s: Cannot bind console driver\n", __func__);
383 return ret;
384 }
Simon Glassb3a72b32016-01-14 18:10:48 -0700385
Simon Glass84c7fb32016-01-18 19:52:17 -0700386 ret = device_probe(cons);
387 if (ret) {
388 debug("%s: Cannot probe console driver\n", __func__);
389 return ret;
390 }
391
Simon Glass623d28f2016-01-18 19:52:15 -0700392 return 0;
393};
394
395/* Post-relocation, allocate memory for the frame buffer */
396static int video_post_bind(struct udevice *dev)
397{
Simon Glass951255d2020-07-02 21:12:32 -0600398 struct video_uc_priv *uc_priv;
399 ulong addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700400 ulong size;
401
402 /* Before relocation there is nothing to do here */
Tom Rini6985a722018-04-22 09:47:48 -0400403 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass623d28f2016-01-18 19:52:15 -0700404 return 0;
Simon Glass951255d2020-07-02 21:12:32 -0600405
406 /* Set up the video pointer, if this is the first device */
Simon Glass95588622020-12-22 19:30:28 -0700407 uc_priv = uclass_get_priv(dev->uclass);
Simon Glass951255d2020-07-02 21:12:32 -0600408 if (!uc_priv->video_ptr)
409 uc_priv->video_ptr = gd->video_top;
410
411 /* Allocate framebuffer space for this device */
412 addr = uc_priv->video_ptr;
Simon Glass623d28f2016-01-18 19:52:15 -0700413 size = alloc_fb(dev, &addr);
414 if (addr < gd->video_bottom) {
Patrick Delaunayd1937182019-05-21 19:19:12 +0200415 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
416 * 'u-boot,dm-pre-proper' tag
417 */
Simon Glass623d28f2016-01-18 19:52:15 -0700418 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
419 dev->name);
420 return -ENOSPC;
421 }
422 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
423 __func__, size, addr, dev->name);
Simon Glass951255d2020-07-02 21:12:32 -0600424 uc_priv->video_ptr = addr;
Simon Glass623d28f2016-01-18 19:52:15 -0700425
426 return 0;
427}
428
429UCLASS_DRIVER(video) = {
430 .id = UCLASS_VIDEO,
431 .name = "video",
432 .flags = DM_UC_FLAG_SEQ_ALIAS,
433 .post_bind = video_post_bind,
434 .pre_probe = video_pre_probe,
435 .post_probe = video_post_probe,
436 .pre_remove = video_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700437 .priv_auto = sizeof(struct video_uc_priv),
438 .per_device_auto = sizeof(struct video_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700439 .per_device_plat_auto = sizeof(struct video_uc_plat),
Simon Glass623d28f2016-01-18 19:52:15 -0700440};