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>
18 #include <dm/device_compat.h>
19 #include <dm/device-internal.h>
20 #include <dm/uclass-internal.h>
26 * Theory of operation:
28 * Before relocation each device is bound. The driver for each device must
29 * set the @align and @size values in struct video_uc_plat. This
30 * information represents the requires size and alignment of the frame buffer
31 * for the device. The values can be an over-estimate but cannot be too
32 * small. The actual values will be suppled (in the same manner) by the bind()
33 * method after relocation.
35 * This information is then picked up by video_reserve() which works out how
36 * much memory is needed for all devices. This is allocated between
37 * gd->video_bottom and gd->video_top.
39 * After relocation the same process occurs. The driver supplies the same
40 * @size and @align information and this time video_post_bind() checks that
41 * the drivers does not overflow the allocated memory.
43 * The frame buffer address is actually set (to plat->base) in
44 * video_post_probe(). This function also clears the frame buffer and
45 * allocates a suitable text console device. This can then be used to write
46 * text to the video device.
48 DECLARE_GLOBAL_DATA_PTR;
51 * struct video_uc_priv - Information for the video uclass
53 * @video_ptr: Current allocation position of the video framebuffer pointer.
54 * While binding devices after relocation, this points to the next
55 * available address to use for a device's framebuffer. It starts at
56 * gd->video_top and works downwards, running out of space when it hits
59 struct video_uc_priv {
63 void video_set_flush_dcache(struct udevice *dev, bool flush)
65 struct video_priv *priv = dev_get_uclass_priv(dev);
67 priv->flush_dcache = flush;
70 static ulong alloc_fb(struct udevice *dev, ulong *addrp)
72 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
73 ulong base, align, size;
78 align = plat->align ? plat->align : 1 << 20;
79 base = *addrp - plat->size;
88 int video_reserve(ulong *addrp)
93 gd->video_top = *addrp;
94 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
96 uclass_find_next_device(&dev)) {
97 size = alloc_fb(dev, addrp);
98 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
99 __func__, size, *addrp, dev->name);
102 /* Allocate space for PCI video devices in case there were not bound */
103 if (*addrp == gd->video_top)
104 *addrp -= CONFIG_VIDEO_PCI_DEFAULT_FB_SIZE;
106 gd->video_bottom = *addrp;
107 gd->fb_base = *addrp;
108 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
114 int video_clear(struct udevice *dev)
116 struct video_priv *priv = dev_get_uclass_priv(dev);
119 switch (priv->bpix) {
121 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
122 u16 *ppix = priv->fb;
123 u16 *end = priv->fb + priv->fb_size;
126 *ppix++ = priv->colour_bg;
130 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
131 u32 *ppix = priv->fb;
132 u32 *end = priv->fb + priv->fb_size;
135 *ppix++ = priv->colour_bg;
139 memset(priv->fb, priv->colour_bg, priv->fb_size);
142 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
146 return video_sync(dev, false);
149 void video_set_default_colors(struct udevice *dev, bool invert)
151 struct video_priv *priv = dev_get_uclass_priv(dev);
154 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
155 /* White is used when switching to bold, use light gray here */
156 fore = VID_LIGHT_GRAY;
169 priv->fg_col_idx = fore;
170 priv->bg_col_idx = back;
171 priv->colour_fg = vid_console_color(priv, fore);
172 priv->colour_bg = vid_console_color(priv, back);
175 /* Flush video activity to the caches */
176 int video_sync(struct udevice *vid, bool force)
178 struct video_ops *ops = video_get_ops(vid);
181 if (ops && ops->video_sync) {
182 ret = ops->video_sync(vid);
188 * flush_dcache_range() is declared in common.h but it seems that some
189 * architectures do not actually implement it. Is there a way to find
190 * out whether it exists? For now, ARM is safe.
192 #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
193 struct video_priv *priv = dev_get_uclass_priv(vid);
195 if (priv->flush_dcache) {
196 flush_dcache_range((ulong)priv->fb,
197 ALIGN((ulong)priv->fb + priv->fb_size,
198 CONFIG_SYS_CACHELINE_SIZE));
200 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
201 struct video_priv *priv = dev_get_uclass_priv(vid);
202 static ulong last_sync;
204 if (force || get_timer(last_sync) > 10) {
205 sandbox_sdl_sync(priv->fb);
206 last_sync = get_timer(0);
212 void video_sync_all(void)
217 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
219 uclass_find_next_device(&dev)) {
220 if (device_active(dev)) {
221 ret = video_sync(dev, true);
223 dev_dbg(dev, "Video sync failed\n");
228 int video_get_xsize(struct udevice *dev)
230 struct video_priv *priv = dev_get_uclass_priv(dev);
235 int video_get_ysize(struct udevice *dev)
237 struct video_priv *priv = dev_get_uclass_priv(dev);
242 #ifdef CONFIG_VIDEO_COPY
243 int video_sync_copy(struct udevice *dev, void *from, void *to)
245 struct video_priv *priv = dev_get_uclass_priv(dev);
250 /* Find the offset of the first byte to copy */
251 if ((ulong)to > (ulong)from) {
253 offset = from - priv->fb;
256 offset = to - priv->fb;
260 * Allow a bit of leeway for valid requests somewhere near the
263 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
267 snprintf(str, sizeof(str),
268 "[sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
269 priv->fb, from, to, offset);
270 console_puts_select_stderr(true, str);
276 * Silently crop the memcpy. This allows callers to avoid doing
277 * this themselves. It is common for the end pointer to go a
278 * few lines after the end of the frame buffer, since most of
279 * the update algorithms terminate a line after their last write
281 if (offset + size > priv->fb_size) {
282 size = priv->fb_size - offset;
283 } else if (offset < 0) {
288 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
294 int video_sync_copy_all(struct udevice *dev)
296 struct video_priv *priv = dev_get_uclass_priv(dev);
298 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
305 /* Set up the colour map */
306 static int video_pre_probe(struct udevice *dev)
308 struct video_priv *priv = dev_get_uclass_priv(dev);
310 priv->cmap = calloc(256, sizeof(ushort));
317 static int video_pre_remove(struct udevice *dev)
319 struct video_priv *priv = dev_get_uclass_priv(dev);
326 /* Set up the display ready for use */
327 static int video_post_probe(struct udevice *dev)
329 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
330 struct video_priv *priv = dev_get_uclass_priv(dev);
331 char name[30], drv[15], *str;
332 const char *drv_name = drv;
333 struct udevice *cons;
336 /* Set up the line and display size */
337 priv->fb = map_sysmem(plat->base, plat->size);
338 if (!priv->line_length)
339 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
341 priv->fb_size = priv->line_length * priv->ysize;
343 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
344 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
347 video_set_default_colors(dev, false);
349 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
353 * Create a text console device. For now we always do this, although
354 * it might be useful to support only bitmap drawing on the device
355 * for boards that don't need to display text. We create a TrueType
356 * console if enabled, a rotated console if the video driver requests
357 * it, otherwise a normal console.
359 * The console can be override by setting vidconsole_drv_name before
360 * probing this video driver, or in the probe() method.
362 * TrueType does not support rotation at present so fall back to the
363 * rotated console in that case.
365 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
366 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
367 strcpy(drv, "vidconsole_tt");
369 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
371 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
377 if (priv->vidconsole_drv_name)
378 drv_name = priv->vidconsole_drv_name;
379 ret = device_bind_driver(dev, drv_name, str, &cons);
381 debug("%s: Cannot bind console driver\n", __func__);
385 ret = device_probe(cons);
387 debug("%s: Cannot probe console driver\n", __func__);
394 /* Post-relocation, allocate memory for the frame buffer */
395 static int video_post_bind(struct udevice *dev)
397 struct video_uc_priv *uc_priv;
401 /* Before relocation there is nothing to do here */
402 if (!(gd->flags & GD_FLG_RELOC))
405 /* Set up the video pointer, if this is the first device */
406 uc_priv = uclass_get_priv(dev->uclass);
407 if (!uc_priv->video_ptr)
408 uc_priv->video_ptr = gd->video_top;
410 /* Allocate framebuffer space for this device */
411 addr = uc_priv->video_ptr;
412 size = alloc_fb(dev, &addr);
413 if (addr < gd->video_bottom) {
414 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
415 * 'u-boot,dm-pre-proper' tag
417 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
421 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
422 __func__, size, addr, dev->name);
423 uc_priv->video_ptr = addr;
428 UCLASS_DRIVER(video) = {
431 .flags = DM_UC_FLAG_SEQ_ALIAS,
432 .post_bind = video_post_bind,
433 .pre_probe = video_pre_probe,
434 .post_probe = video_post_probe,
435 .pre_remove = video_pre_remove,
436 .priv_auto = sizeof(struct video_uc_priv),
437 .per_device_auto = sizeof(struct video_priv),
438 .per_device_plat_auto = sizeof(struct video_uc_plat),