1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
13 #include <stdio_dev.h>
15 #include <video_console.h>
16 #include <asm/cache.h>
17 #include <asm/global_data.h>
19 #include <dm/device_compat.h>
20 #include <dm/device-internal.h>
21 #include <dm/uclass-internal.h>
27 * Theory of operation:
29 * Before relocation each device is bound. The driver for each device must
30 * set the @align and @size values in struct video_uc_plat. This
31 * 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.
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.
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.
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.
49 DECLARE_GLOBAL_DATA_PTR;
52 * struct video_uc_priv - Information for the video uclass
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
60 struct video_uc_priv {
64 void video_set_flush_dcache(struct udevice *dev, bool flush)
66 struct video_priv *priv = dev_get_uclass_priv(dev);
68 priv->flush_dcache = flush;
71 static ulong alloc_fb(struct udevice *dev, ulong *addrp)
73 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
74 ulong base, align, size;
79 align = plat->align ? plat->align : 1 << 20;
80 base = *addrp - plat->size;
89 int video_reserve(ulong *addrp)
94 gd->video_top = *addrp;
95 for (uclass_find_first_device(UCLASS_VIDEO, &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);
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;
107 gd->video_bottom = *addrp;
108 gd->fb_base = *addrp;
109 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
115 int video_clear(struct udevice *dev)
117 struct video_priv *priv = dev_get_uclass_priv(dev);
120 switch (priv->bpix) {
122 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
123 u16 *ppix = priv->fb;
124 u16 *end = priv->fb + priv->fb_size;
127 *ppix++ = priv->colour_bg;
131 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
132 u32 *ppix = priv->fb;
133 u32 *end = priv->fb + priv->fb_size;
136 *ppix++ = priv->colour_bg;
140 memset(priv->fb, priv->colour_bg, priv->fb_size);
143 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
147 return video_sync(dev, false);
150 void video_set_default_colors(struct udevice *dev, bool invert)
152 struct video_priv *priv = dev_get_uclass_priv(dev);
155 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;
170 priv->fg_col_idx = fore;
171 priv->bg_col_idx = back;
172 priv->colour_fg = vid_console_color(priv, fore);
173 priv->colour_bg = vid_console_color(priv, back);
176 /* Flush video activity to the caches */
177 int video_sync(struct udevice *vid, bool force)
179 struct video_ops *ops = video_get_ops(vid);
182 if (ops && ops->video_sync) {
183 ret = ops->video_sync(vid);
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.
193 #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
194 struct video_priv *priv = dev_get_uclass_priv(vid);
196 if (priv->flush_dcache) {
197 flush_dcache_range((ulong)priv->fb,
198 ALIGN((ulong)priv->fb + priv->fb_size,
199 CONFIG_SYS_CACHELINE_SIZE));
201 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
202 struct video_priv *priv = dev_get_uclass_priv(vid);
203 static ulong last_sync;
205 if (force || get_timer(last_sync) > 10) {
206 sandbox_sdl_sync(priv->fb);
207 last_sync = get_timer(0);
213 void video_sync_all(void)
218 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
220 uclass_find_next_device(&dev)) {
221 if (device_active(dev)) {
222 ret = video_sync(dev, true);
224 dev_dbg(dev, "Video sync failed\n");
229 int video_get_xsize(struct udevice *dev)
231 struct video_priv *priv = dev_get_uclass_priv(dev);
236 int video_get_ysize(struct udevice *dev)
238 struct video_priv *priv = dev_get_uclass_priv(dev);
243 #ifdef CONFIG_VIDEO_COPY
244 int video_sync_copy(struct udevice *dev, void *from, void *to)
246 struct video_priv *priv = dev_get_uclass_priv(dev);
251 /* Find the offset of the first byte to copy */
252 if ((ulong)to > (ulong)from) {
254 offset = from - priv->fb;
257 offset = to - priv->fb;
261 * Allow a bit of leeway for valid requests somewhere near the
264 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
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);
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
282 if (offset + size > priv->fb_size) {
283 size = priv->fb_size - offset;
284 } else if (offset < 0) {
289 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
295 int video_sync_copy_all(struct udevice *dev)
297 struct video_priv *priv = dev_get_uclass_priv(dev);
299 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
306 /* Set up the colour map */
307 static int video_pre_probe(struct udevice *dev)
309 struct video_priv *priv = dev_get_uclass_priv(dev);
311 priv->cmap = calloc(256, sizeof(ushort));
318 static int video_pre_remove(struct udevice *dev)
320 struct video_priv *priv = dev_get_uclass_priv(dev);
327 /* Set up the display ready for use */
328 static int video_post_probe(struct udevice *dev)
330 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
331 struct video_priv *priv = dev_get_uclass_priv(dev);
332 char name[30], drv[15], *str;
333 const char *drv_name = drv;
334 struct udevice *cons;
337 /* Set up the line and display size */
338 priv->fb = map_sysmem(plat->base, plat->size);
339 if (!priv->line_length)
340 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
342 priv->fb_size = priv->line_length * priv->ysize;
344 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
345 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
348 video_set_default_colors(dev, false);
350 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
354 * Create a text console device. For now we always do this, although
355 * it might be useful to support only bitmap drawing on the device
356 * 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.
360 * The console can be override by setting vidconsole_drv_name before
361 * probing this video driver, or in the probe() method.
363 * TrueType does not support rotation at present so fall back to the
364 * rotated console in that case.
366 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
367 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
368 strcpy(drv, "vidconsole_tt");
370 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
372 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
378 if (priv->vidconsole_drv_name)
379 drv_name = priv->vidconsole_drv_name;
380 ret = device_bind_driver(dev, drv_name, str, &cons);
382 debug("%s: Cannot bind console driver\n", __func__);
386 ret = device_probe(cons);
388 debug("%s: Cannot probe console driver\n", __func__);
395 /* Post-relocation, allocate memory for the frame buffer */
396 static int video_post_bind(struct udevice *dev)
398 struct video_uc_priv *uc_priv;
402 /* Before relocation there is nothing to do here */
403 if (!(gd->flags & GD_FLG_RELOC))
406 /* Set up the video pointer, if this is the first device */
407 uc_priv = uclass_get_priv(dev->uclass);
408 if (!uc_priv->video_ptr)
409 uc_priv->video_ptr = gd->video_top;
411 /* Allocate framebuffer space for this device */
412 addr = uc_priv->video_ptr;
413 size = alloc_fb(dev, &addr);
414 if (addr < gd->video_bottom) {
415 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
416 * 'u-boot,dm-pre-proper' tag
418 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
422 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
423 __func__, size, addr, dev->name);
424 uc_priv->video_ptr = addr;
429 UCLASS_DRIVER(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,
437 .priv_auto = sizeof(struct video_uc_priv),
438 .per_device_auto = sizeof(struct video_priv),
439 .per_device_plat_auto = sizeof(struct video_uc_plat),