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. Additionally driver can allocate frame buffer
37 * itself by setting plat->base.
39 * This information is then picked up by video_reserve() which works out how
40 * much memory is needed for all devices. This is allocated between
41 * gd->video_bottom and gd->video_top.
43 * After relocation the same process occurs. The driver supplies the same
44 * @size and @align information and this time video_post_bind() checks that
45 * the drivers does not overflow the allocated memory.
47 * The frame buffer address is actually set (to plat->base) in
48 * video_post_probe(). This function also clears the frame buffer and
49 * allocates a suitable text console device. This can then be used to write
50 * text to the video device.
52 DECLARE_GLOBAL_DATA_PTR;
55 * struct video_uc_priv - Information for the video uclass
57 * @video_ptr: Current allocation position of the video framebuffer pointer.
58 * While binding devices after relocation, this points to the next
59 * available address to use for a device's framebuffer. It starts at
60 * gd->video_top and works downwards, running out of space when it hits
63 struct video_uc_priv {
67 void video_set_flush_dcache(struct udevice *dev, bool flush)
69 struct video_priv *priv = dev_get_uclass_priv(dev);
71 priv->flush_dcache = flush;
74 static ulong alloc_fb(struct udevice *dev, ulong *addrp)
76 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
77 ulong base, align, size;
82 /* Allow drivers to allocate the frame buffer themselves */
86 align = plat->align ? plat->align : 1 << 20;
87 base = *addrp - plat->size;
96 int video_reserve(ulong *addrp)
101 gd->video_top = *addrp;
102 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
104 uclass_find_next_device(&dev)) {
105 size = alloc_fb(dev, addrp);
106 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
107 __func__, size, *addrp, dev->name);
110 /* Allocate space for PCI video devices in case there were not bound */
111 if (*addrp == gd->video_top)
112 *addrp -= CONFIG_VIDEO_PCI_DEFAULT_FB_SIZE;
114 gd->video_bottom = *addrp;
115 gd->fb_base = *addrp;
116 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
122 int video_clear(struct udevice *dev)
124 struct video_priv *priv = dev_get_uclass_priv(dev);
127 switch (priv->bpix) {
129 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
130 u16 *ppix = priv->fb;
131 u16 *end = priv->fb + priv->fb_size;
134 *ppix++ = priv->colour_bg;
138 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
139 u32 *ppix = priv->fb;
140 u32 *end = priv->fb + priv->fb_size;
143 *ppix++ = priv->colour_bg;
147 memset(priv->fb, priv->colour_bg, priv->fb_size);
150 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
154 return video_sync(dev, false);
157 void video_set_default_colors(struct udevice *dev, bool invert)
159 struct video_priv *priv = dev_get_uclass_priv(dev);
162 if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
163 /* White is used when switching to bold, use light gray here */
164 fore = VID_LIGHT_GRAY;
177 priv->fg_col_idx = fore;
178 priv->bg_col_idx = back;
179 priv->colour_fg = vid_console_color(priv, fore);
180 priv->colour_bg = vid_console_color(priv, back);
183 /* Flush video activity to the caches */
184 int video_sync(struct udevice *vid, bool force)
186 struct video_ops *ops = video_get_ops(vid);
189 if (ops && ops->video_sync) {
190 ret = ops->video_sync(vid);
196 * flush_dcache_range() is declared in common.h but it seems that some
197 * architectures do not actually implement it. Is there a way to find
198 * out whether it exists? For now, ARM is safe.
200 #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
201 struct video_priv *priv = dev_get_uclass_priv(vid);
203 if (priv->flush_dcache) {
204 flush_dcache_range((ulong)priv->fb,
205 ALIGN((ulong)priv->fb + priv->fb_size,
206 CONFIG_SYS_CACHELINE_SIZE));
208 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
209 struct video_priv *priv = dev_get_uclass_priv(vid);
210 static ulong last_sync;
212 if (force || get_timer(last_sync) > 100) {
213 sandbox_sdl_sync(priv->fb);
214 last_sync = get_timer(0);
220 void video_sync_all(void)
225 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
227 uclass_find_next_device(&dev)) {
228 if (device_active(dev)) {
229 ret = video_sync(dev, true);
231 dev_dbg(dev, "Video sync failed\n");
236 bool video_is_active(void)
240 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
242 uclass_find_next_device(&dev)) {
243 if (device_active(dev))
250 int video_get_xsize(struct udevice *dev)
252 struct video_priv *priv = dev_get_uclass_priv(dev);
257 int video_get_ysize(struct udevice *dev)
259 struct video_priv *priv = dev_get_uclass_priv(dev);
264 #ifdef CONFIG_VIDEO_COPY
265 int video_sync_copy(struct udevice *dev, void *from, void *to)
267 struct video_priv *priv = dev_get_uclass_priv(dev);
272 /* Find the offset of the first byte to copy */
273 if ((ulong)to > (ulong)from) {
275 offset = from - priv->fb;
278 offset = to - priv->fb;
282 * Allow a bit of leeway for valid requests somewhere near the
285 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
289 snprintf(str, sizeof(str),
290 "[** FAULT sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
291 priv->fb, from, to, offset);
292 console_puts_select_stderr(true, str);
298 * Silently crop the memcpy. This allows callers to avoid doing
299 * this themselves. It is common for the end pointer to go a
300 * few lines after the end of the frame buffer, since most of
301 * the update algorithms terminate a line after their last write
303 if (offset + size > priv->fb_size) {
304 size = priv->fb_size - offset;
305 } else if (offset < 0) {
310 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
316 int video_sync_copy_all(struct udevice *dev)
318 struct video_priv *priv = dev_get_uclass_priv(dev);
320 video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
327 #define SPLASH_DECL(_name) \
328 extern u8 __splash_ ## _name ## _begin[]; \
329 extern u8 __splash_ ## _name ## _end[]
331 #define SPLASH_START(_name) __splash_ ## _name ## _begin
333 SPLASH_DECL(u_boot_logo);
335 static int show_splash(struct udevice *dev)
337 u8 *data = SPLASH_START(u_boot_logo);
340 ret = video_bmp_display(dev, map_to_sysmem(data), -4, 4, true);
345 /* Set up the display ready for use */
346 static int video_post_probe(struct udevice *dev)
348 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
349 struct video_priv *priv = dev_get_uclass_priv(dev);
350 char name[30], drv[15], *str;
351 const char *drv_name = drv;
352 struct udevice *cons;
355 /* Set up the line and display size */
356 priv->fb = map_sysmem(plat->base, plat->size);
357 if (!priv->line_length)
358 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
360 priv->fb_size = priv->line_length * priv->ysize;
362 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
363 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
366 video_set_default_colors(dev, false);
368 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
372 * Create a text console device. For now we always do this, although
373 * it might be useful to support only bitmap drawing on the device
374 * for boards that don't need to display text. We create a TrueType
375 * console if enabled, a rotated console if the video driver requests
376 * it, otherwise a normal console.
378 * The console can be override by setting vidconsole_drv_name before
379 * probing this video driver, or in the probe() method.
381 * TrueType does not support rotation at present so fall back to the
382 * rotated console in that case.
384 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
385 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
386 strcpy(drv, "vidconsole_tt");
388 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
390 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
396 if (priv->vidconsole_drv_name)
397 drv_name = priv->vidconsole_drv_name;
398 ret = device_bind_driver(dev, drv_name, str, &cons);
400 debug("%s: Cannot bind console driver\n", __func__);
404 ret = device_probe(cons);
406 debug("%s: Cannot probe console driver\n", __func__);
410 if (IS_ENABLED(CONFIG_VIDEO_LOGO) && !plat->hide_logo) {
411 ret = show_splash(dev);
413 log_debug("Cannot show splash screen\n");
421 /* Post-relocation, allocate memory for the frame buffer */
422 static int video_post_bind(struct udevice *dev)
424 struct video_uc_priv *uc_priv;
428 /* Before relocation there is nothing to do here */
429 if (!(gd->flags & GD_FLG_RELOC))
432 /* Set up the video pointer, if this is the first device */
433 uc_priv = uclass_get_priv(dev->uclass);
434 if (!uc_priv->video_ptr)
435 uc_priv->video_ptr = gd->video_top;
437 /* Allocate framebuffer space for this device */
438 addr = uc_priv->video_ptr;
439 size = alloc_fb(dev, &addr);
440 if (addr < gd->video_bottom) {
441 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
442 * 'u-boot,dm-pre-proper' tag
444 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
448 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
449 __func__, size, addr, dev->name);
450 uc_priv->video_ptr = addr;
455 UCLASS_DRIVER(video) = {
458 .flags = DM_UC_FLAG_SEQ_ALIAS,
459 .post_bind = video_post_bind,
460 .post_probe = video_post_probe,
461 .priv_auto = sizeof(struct video_uc_priv),
462 .per_device_auto = sizeof(struct video_priv),
463 .per_device_plat_auto = sizeof(struct video_uc_plat),