1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
11 #include <stdio_dev.h>
13 #include <video_console.h>
14 #include <asm/cache.h>
16 #include <dm/device-internal.h>
17 #include <dm/uclass-internal.h>
23 * Theory of operation:
25 * Before relocation each device is bound. The driver for each device must
26 * set the @align and @size values in struct video_uc_platdata. This
27 * information represents the requires size and alignment of the frame buffer
28 * for the device. The values can be an over-estimate but cannot be too
29 * small. The actual values will be suppled (in the same manner) by the bind()
30 * method after relocation.
32 * This information is then picked up by video_reserve() which works out how
33 * much memory is needed for all devices. This is allocated between
34 * gd->video_bottom and gd->video_top.
36 * After relocation the same process occurs. The driver supplies the same
37 * @size and @align information and this time video_post_bind() checks that
38 * the drivers does not overflow the allocated memory.
40 * The frame buffer address is actually set (to plat->base) in
41 * video_post_probe(). This function also clears the frame buffer and
42 * allocates a suitable text console device. This can then be used to write
43 * text to the video device.
45 DECLARE_GLOBAL_DATA_PTR;
47 void video_set_flush_dcache(struct udevice *dev, bool flush)
49 struct video_priv *priv = dev_get_uclass_priv(dev);
51 priv->flush_dcache = flush;
54 static ulong alloc_fb(struct udevice *dev, ulong *addrp)
56 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
57 ulong base, align, size;
62 align = plat->align ? plat->align : 1 << 20;
63 base = *addrp - plat->size;
72 int video_reserve(ulong *addrp)
77 gd->video_top = *addrp;
78 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
80 uclass_find_next_device(&dev)) {
81 size = alloc_fb(dev, addrp);
82 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
83 __func__, size, *addrp, dev->name);
85 gd->video_bottom = *addrp;
86 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
92 int video_clear(struct udevice *dev)
94 struct video_priv *priv = dev_get_uclass_priv(dev);
98 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
100 u16 *end = priv->fb + priv->fb_size;
103 *ppix++ = priv->colour_bg;
107 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
108 u32 *ppix = priv->fb;
109 u32 *end = priv->fb + priv->fb_size;
112 *ppix++ = priv->colour_bg;
116 memset(priv->fb, priv->colour_bg, priv->fb_size);
123 void video_set_default_colors(struct udevice *dev, bool invert)
125 struct video_priv *priv = dev_get_uclass_priv(dev);
128 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
129 /* White is used when switching to bold, use light gray here */
130 fore = VID_LIGHT_GRAY;
143 priv->fg_col_idx = fore;
144 priv->bg_col_idx = back;
145 priv->colour_fg = vid_console_color(priv, fore);
146 priv->colour_bg = vid_console_color(priv, back);
149 /* Flush video activity to the caches */
150 void video_sync(struct udevice *vid, bool force)
153 * flush_dcache_range() is declared in common.h but it seems that some
154 * architectures do not actually implement it. Is there a way to find
155 * out whether it exists? For now, ARM is safe.
157 #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
158 struct video_priv *priv = dev_get_uclass_priv(vid);
160 if (priv->flush_dcache) {
161 flush_dcache_range((ulong)priv->fb,
162 ALIGN((ulong)priv->fb + priv->fb_size,
163 CONFIG_SYS_CACHELINE_SIZE));
165 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
166 struct video_priv *priv = dev_get_uclass_priv(vid);
167 static ulong last_sync;
169 if (force || get_timer(last_sync) > 10) {
170 sandbox_sdl_sync(priv->fb);
171 last_sync = get_timer(0);
176 void video_sync_all(void)
180 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
182 uclass_find_next_device(&dev)) {
183 if (device_active(dev))
184 video_sync(dev, true);
188 int video_get_xsize(struct udevice *dev)
190 struct video_priv *priv = dev_get_uclass_priv(dev);
195 int video_get_ysize(struct udevice *dev)
197 struct video_priv *priv = dev_get_uclass_priv(dev);
202 /* Set up the colour map */
203 static int video_pre_probe(struct udevice *dev)
205 struct video_priv *priv = dev_get_uclass_priv(dev);
207 priv->cmap = calloc(256, sizeof(ushort));
214 static int video_pre_remove(struct udevice *dev)
216 struct video_priv *priv = dev_get_uclass_priv(dev);
223 /* Set up the display ready for use */
224 static int video_post_probe(struct udevice *dev)
226 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
227 struct video_priv *priv = dev_get_uclass_priv(dev);
228 char name[30], drv[15], *str;
229 const char *drv_name = drv;
230 struct udevice *cons;
233 /* Set up the line and display size */
234 priv->fb = map_sysmem(plat->base, plat->size);
235 if (!priv->line_length)
236 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
238 priv->fb_size = priv->line_length * priv->ysize;
241 video_set_default_colors(dev, false);
243 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
247 * Create a text console device. For now we always do this, although
248 * it might be useful to support only bitmap drawing on the device
249 * for boards that don't need to display text. We create a TrueType
250 * console if enabled, a rotated console if the video driver requests
251 * it, otherwise a normal console.
253 * The console can be override by setting vidconsole_drv_name before
254 * probing this video driver, or in the probe() method.
256 * TrueType does not support rotation at present so fall back to the
257 * rotated console in that case.
259 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
260 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
261 strcpy(drv, "vidconsole_tt");
263 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
265 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
271 if (priv->vidconsole_drv_name)
272 drv_name = priv->vidconsole_drv_name;
273 ret = device_bind_driver(dev, drv_name, str, &cons);
275 debug("%s: Cannot bind console driver\n", __func__);
279 ret = device_probe(cons);
281 debug("%s: Cannot probe console driver\n", __func__);
288 /* Post-relocation, allocate memory for the frame buffer */
289 static int video_post_bind(struct udevice *dev)
291 ulong addr = gd->video_top;
294 /* Before relocation there is nothing to do here */
295 if (!(gd->flags & GD_FLG_RELOC))
297 size = alloc_fb(dev, &addr);
298 if (addr < gd->video_bottom) {
299 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
300 * 'u-boot,dm-pre-proper' tag
302 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
306 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
307 __func__, size, addr, dev->name);
308 gd->video_bottom = addr;
313 UCLASS_DRIVER(video) = {
316 .flags = DM_UC_FLAG_SEQ_ALIAS,
317 .post_bind = video_post_bind,
318 .pre_probe = video_pre_probe,
319 .post_probe = video_post_probe,
320 .pre_remove = video_pre_remove,
321 .per_device_auto_alloc_size = sizeof(struct video_priv),
322 .per_device_platdata_auto_alloc_size = sizeof(struct video_uc_platdata),