1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
6 #define LOG_CATEGORY UCLASS_VIDEO
15 #include <stdio_dev.h>
17 #include <video_console.h>
18 #include <asm/cache.h>
19 #include <asm/global_data.h>
21 #include <dm/device_compat.h>
22 #include <dm/device-internal.h>
23 #include <dm/uclass-internal.h>
29 * Theory of operation:
31 * Before relocation each device is bound. The driver for each device must
32 * set the @align and @size values in struct video_uc_plat. This
33 * 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.
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.
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.
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.
51 DECLARE_GLOBAL_DATA_PTR;
54 * struct video_uc_priv - Information for the video uclass
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
62 struct video_uc_priv {
66 void video_set_flush_dcache(struct udevice *dev, bool flush)
68 struct video_priv *priv = dev_get_uclass_priv(dev);
70 priv->flush_dcache = flush;
73 static ulong alloc_fb(struct udevice *dev, ulong *addrp)
75 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
76 ulong base, align, size;
81 align = plat->align ? plat->align : 1 << 20;
82 base = *addrp - plat->size;
91 int video_reserve(ulong *addrp)
96 gd->video_top = *addrp;
97 for (uclass_find_first_device(UCLASS_VIDEO, &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);
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;
109 gd->video_bottom = *addrp;
110 gd->fb_base = *addrp;
111 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
117 int video_clear(struct udevice *dev)
119 struct video_priv *priv = dev_get_uclass_priv(dev);
122 switch (priv->bpix) {
124 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
125 u16 *ppix = priv->fb;
126 u16 *end = priv->fb + priv->fb_size;
129 *ppix++ = priv->colour_bg;
133 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
134 u32 *ppix = priv->fb;
135 u32 *end = priv->fb + priv->fb_size;
138 *ppix++ = priv->colour_bg;
142 memset(priv->fb, priv->colour_bg, priv->fb_size);
145 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
149 return video_sync(dev, false);
152 void video_set_default_colors(struct udevice *dev, bool invert)
154 struct video_priv *priv = dev_get_uclass_priv(dev);
157 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;
172 priv->fg_col_idx = fore;
173 priv->bg_col_idx = back;
174 priv->colour_fg = vid_console_color(priv, fore);
175 priv->colour_bg = vid_console_color(priv, back);
178 /* Flush video activity to the caches */
179 int video_sync(struct udevice *vid, bool force)
181 struct video_ops *ops = video_get_ops(vid);
184 if (ops && ops->video_sync) {
185 ret = ops->video_sync(vid);
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.
195 #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
196 struct video_priv *priv = dev_get_uclass_priv(vid);
198 if (priv->flush_dcache) {
199 flush_dcache_range((ulong)priv->fb,
200 ALIGN((ulong)priv->fb + priv->fb_size,
201 CONFIG_SYS_CACHELINE_SIZE));
203 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
204 struct video_priv *priv = dev_get_uclass_priv(vid);
205 static ulong last_sync;
207 if (force || get_timer(last_sync) > 10) {
208 sandbox_sdl_sync(priv->fb);
209 last_sync = get_timer(0);
215 void video_sync_all(void)
220 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
222 uclass_find_next_device(&dev)) {
223 if (device_active(dev)) {
224 ret = video_sync(dev, true);
226 dev_dbg(dev, "Video sync failed\n");
231 int video_get_xsize(struct udevice *dev)
233 struct video_priv *priv = dev_get_uclass_priv(dev);
238 int video_get_ysize(struct udevice *dev)
240 struct video_priv *priv = dev_get_uclass_priv(dev);
245 #ifdef CONFIG_VIDEO_COPY
246 int video_sync_copy(struct udevice *dev, void *from, void *to)
248 struct video_priv *priv = dev_get_uclass_priv(dev);
253 /* Find the offset of the first byte to copy */
254 if ((ulong)to > (ulong)from) {
256 offset = from - priv->fb;
259 offset = to - priv->fb;
263 * Allow a bit of leeway for valid requests somewhere near the
266 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
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);
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
284 if (offset + size > priv->fb_size) {
285 size = priv->fb_size - offset;
286 } else if (offset < 0) {
291 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
297 int video_sync_copy_all(struct udevice *dev)
299 struct video_priv *priv = dev_get_uclass_priv(dev);
301 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
308 /* Set up the colour map */
309 static int video_pre_probe(struct udevice *dev)
311 struct video_priv *priv = dev_get_uclass_priv(dev);
313 priv->cmap = calloc(256, sizeof(ushort));
320 static int video_pre_remove(struct udevice *dev)
322 struct video_priv *priv = dev_get_uclass_priv(dev);
329 /* Set up the display ready for use */
330 static int video_post_probe(struct udevice *dev)
332 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
333 struct video_priv *priv = dev_get_uclass_priv(dev);
334 char name[30], drv[15], *str;
335 const char *drv_name = drv;
336 struct udevice *cons;
339 /* Set up the line and display size */
340 priv->fb = map_sysmem(plat->base, plat->size);
341 if (!priv->line_length)
342 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
344 priv->fb_size = priv->line_length * priv->ysize;
346 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
347 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
350 video_set_default_colors(dev, false);
352 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
356 * Create a text console device. For now we always do this, although
357 * it might be useful to support only bitmap drawing on the device
358 * 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.
362 * The console can be override by setting vidconsole_drv_name before
363 * probing this video driver, or in the probe() method.
365 * TrueType does not support rotation at present so fall back to the
366 * rotated console in that case.
368 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
369 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
370 strcpy(drv, "vidconsole_tt");
372 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
374 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
380 if (priv->vidconsole_drv_name)
381 drv_name = priv->vidconsole_drv_name;
382 ret = device_bind_driver(dev, drv_name, str, &cons);
384 debug("%s: Cannot bind console driver\n", __func__);
388 ret = device_probe(cons);
390 debug("%s: Cannot probe console driver\n", __func__);
397 /* Post-relocation, allocate memory for the frame buffer */
398 static int video_post_bind(struct udevice *dev)
400 struct video_uc_priv *uc_priv;
404 /* Before relocation there is nothing to do here */
405 if (!(gd->flags & GD_FLG_RELOC))
408 /* Set up the video pointer, if this is the first device */
409 uc_priv = uclass_get_priv(dev->uclass);
410 if (!uc_priv->video_ptr)
411 uc_priv->video_ptr = gd->video_top;
413 /* Allocate framebuffer space for this device */
414 addr = uc_priv->video_ptr;
415 size = alloc_fb(dev, &addr);
416 if (addr < gd->video_bottom) {
417 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
418 * 'u-boot,dm-pre-proper' tag
420 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
424 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
425 __func__, size, addr, dev->name);
426 uc_priv->video_ptr = addr;
431 UCLASS_DRIVER(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,
439 .priv_auto = sizeof(struct video_uc_priv),
440 .per_device_auto = sizeof(struct video_priv),
441 .per_device_plat_auto = sizeof(struct video_uc_plat),