video: Add support for copying to a hardware framebuffer
[platform/kernel/u-boot.git] / drivers / video / video-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  */
5
6 #include <common.h>
7 #include <console.h>
8 #include <cpu_func.h>
9 #include <dm.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <stdio_dev.h>
14 #include <video.h>
15 #include <video_console.h>
16 #include <asm/cache.h>
17 #include <dm/lists.h>
18 #include <dm/device-internal.h>
19 #include <dm/uclass-internal.h>
20 #ifdef CONFIG_SANDBOX
21 #include <asm/sdl.h>
22 #endif
23
24 /*
25  * Theory of operation:
26  *
27  * Before relocation each device is bound. The driver for each device must
28  * set the @align and @size values in struct video_uc_platdata. This
29  * information represents the requires size and alignment of the frame buffer
30  * for the device. The values can be an over-estimate but cannot be too
31  * small. The actual values will be suppled (in the same manner) by the bind()
32  * method after relocation.
33  *
34  * This information is then picked up by video_reserve() which works out how
35  * much memory is needed for all devices. This is allocated between
36  * gd->video_bottom and gd->video_top.
37  *
38  * After relocation the same process occurs. The driver supplies the same
39  * @size and @align information and this time video_post_bind() checks that
40  * the drivers does not overflow the allocated memory.
41  *
42  * The frame buffer address is actually set (to plat->base) in
43  * video_post_probe(). This function also clears the frame buffer and
44  * allocates a suitable text console device. This can then be used to write
45  * text to the video device.
46  */
47 DECLARE_GLOBAL_DATA_PTR;
48
49 void video_set_flush_dcache(struct udevice *dev, bool flush)
50 {
51         struct video_priv *priv = dev_get_uclass_priv(dev);
52
53         priv->flush_dcache = flush;
54 }
55
56 static ulong alloc_fb(struct udevice *dev, ulong *addrp)
57 {
58         struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
59         ulong base, align, size;
60
61         if (!plat->size)
62                 return 0;
63
64         align = plat->align ? plat->align : 1 << 20;
65         base = *addrp - plat->size;
66         base &= ~(align - 1);
67         plat->base = base;
68         size = *addrp - base;
69         *addrp = base;
70
71         return size;
72 }
73
74 int video_reserve(ulong *addrp)
75 {
76         struct udevice *dev;
77         ulong size;
78
79         gd->video_top = *addrp;
80         for (uclass_find_first_device(UCLASS_VIDEO, &dev);
81              dev;
82              uclass_find_next_device(&dev)) {
83                 size = alloc_fb(dev, addrp);
84                 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
85                       __func__, size, *addrp, dev->name);
86         }
87         gd->video_bottom = *addrp;
88         gd->fb_base = *addrp;
89         debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
90               gd->video_top);
91
92         return 0;
93 }
94
95 int video_clear(struct udevice *dev)
96 {
97         struct video_priv *priv = dev_get_uclass_priv(dev);
98
99         switch (priv->bpix) {
100         case VIDEO_BPP16:
101                 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
102                         u16 *ppix = priv->fb;
103                         u16 *end = priv->fb + priv->fb_size;
104
105                         while (ppix < end)
106                                 *ppix++ = priv->colour_bg;
107                         break;
108                 }
109         case VIDEO_BPP32:
110                 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
111                         u32 *ppix = priv->fb;
112                         u32 *end = priv->fb + priv->fb_size;
113
114                         while (ppix < end)
115                                 *ppix++ = priv->colour_bg;
116                         break;
117                 }
118         default:
119                 memset(priv->fb, priv->colour_bg, priv->fb_size);
120                 break;
121         }
122
123         return 0;
124 }
125
126 void video_set_default_colors(struct udevice *dev, bool invert)
127 {
128         struct video_priv *priv = dev_get_uclass_priv(dev);
129         int fore, back;
130
131         if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
132                 /* White is used when switching to bold, use light gray here */
133                 fore = VID_LIGHT_GRAY;
134                 back = VID_BLACK;
135         } else {
136                 fore = VID_BLACK;
137                 back = VID_WHITE;
138         }
139         if (invert) {
140                 int temp;
141
142                 temp = fore;
143                 fore = back;
144                 back = temp;
145         }
146         priv->fg_col_idx = fore;
147         priv->bg_col_idx = back;
148         priv->colour_fg = vid_console_color(priv, fore);
149         priv->colour_bg = vid_console_color(priv, back);
150 }
151
152 /* Flush video activity to the caches */
153 void video_sync(struct udevice *vid, bool force)
154 {
155         /*
156          * flush_dcache_range() is declared in common.h but it seems that some
157          * architectures do not actually implement it. Is there a way to find
158          * out whether it exists? For now, ARM is safe.
159          */
160 #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
161         struct video_priv *priv = dev_get_uclass_priv(vid);
162
163         if (priv->flush_dcache) {
164                 flush_dcache_range((ulong)priv->fb,
165                                    ALIGN((ulong)priv->fb + priv->fb_size,
166                                          CONFIG_SYS_CACHELINE_SIZE));
167         }
168 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
169         struct video_priv *priv = dev_get_uclass_priv(vid);
170         static ulong last_sync;
171
172         if (force || get_timer(last_sync) > 10) {
173                 sandbox_sdl_sync(priv->fb);
174                 last_sync = get_timer(0);
175         }
176 #endif
177 }
178
179 void video_sync_all(void)
180 {
181         struct udevice *dev;
182
183         for (uclass_find_first_device(UCLASS_VIDEO, &dev);
184              dev;
185              uclass_find_next_device(&dev)) {
186                 if (device_active(dev))
187                         video_sync(dev, true);
188         }
189 }
190
191 int video_get_xsize(struct udevice *dev)
192 {
193         struct video_priv *priv = dev_get_uclass_priv(dev);
194
195         return priv->xsize;
196 }
197
198 int video_get_ysize(struct udevice *dev)
199 {
200         struct video_priv *priv = dev_get_uclass_priv(dev);
201
202         return priv->ysize;
203 }
204
205 #ifdef CONFIG_VIDEO_COPY
206 int video_sync_copy(struct udevice *dev, void *from, void *to)
207 {
208         struct video_priv *priv = dev_get_uclass_priv(dev);
209
210         if (priv->copy_fb) {
211                 long offset, size;
212
213                 /* Find the offset of the first byte to copy */
214                 if ((ulong)to > (ulong)from) {
215                         size = to - from;
216                         offset = from - priv->fb;
217                 } else {
218                         size = from - to;
219                         offset = to - priv->fb;
220                 }
221
222                 /*
223                  * Allow a bit of leeway for valid requests somewhere near the
224                  * frame buffer
225                  */
226                 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
227 #ifdef DEBUG
228                         char str[80];
229
230                         snprintf(str, sizeof(str),
231                                  "[sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
232                                  priv->fb, from, to, offset);
233                         console_puts_select_stderr(true, str);
234 #endif
235                         return -EFAULT;
236                 }
237
238                 /*
239                  * Silently crop the memcpy. This allows callers to avoid doing
240                  * this themselves. It is common for the end pointer to go a
241                  * few lines after the end of the frame buffer, since most of
242                  * the update algorithms terminate a line after their last write
243                  */
244                 if (offset + size > priv->fb_size) {
245                         size = priv->fb_size - offset;
246                 } else if (offset < 0) {
247                         size += offset;
248                         offset = 0;
249                 }
250
251                 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
252         }
253
254         return 0;
255 }
256 #endif
257
258 /* Set up the colour map */
259 static int video_pre_probe(struct udevice *dev)
260 {
261         struct video_priv *priv = dev_get_uclass_priv(dev);
262
263         priv->cmap = calloc(256, sizeof(ushort));
264         if (!priv->cmap)
265                 return -ENOMEM;
266
267         return 0;
268 }
269
270 static int video_pre_remove(struct udevice *dev)
271 {
272         struct video_priv *priv = dev_get_uclass_priv(dev);
273
274         free(priv->cmap);
275
276         return 0;
277 }
278
279 /* Set up the display ready for use */
280 static int video_post_probe(struct udevice *dev)
281 {
282         struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
283         struct video_priv *priv = dev_get_uclass_priv(dev);
284         char name[30], drv[15], *str;
285         const char *drv_name = drv;
286         struct udevice *cons;
287         int ret;
288
289         /* Set up the line and display size */
290         priv->fb = map_sysmem(plat->base, plat->size);
291         if (!priv->line_length)
292                 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
293
294         priv->fb_size = priv->line_length * priv->ysize;
295
296         /* Set up colors  */
297         video_set_default_colors(dev, false);
298
299         if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
300                 video_clear(dev);
301
302         /*
303          * Create a text console device. For now we always do this, although
304          * it might be useful to support only bitmap drawing on the device
305          * for boards that don't need to display text. We create a TrueType
306          * console if enabled, a rotated console if the video driver requests
307          * it, otherwise a normal console.
308          *
309          * The console can be override by setting vidconsole_drv_name before
310          * probing this video driver, or in the probe() method.
311          *
312          * TrueType does not support rotation at present so fall back to the
313          * rotated console in that case.
314          */
315         if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
316                 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
317                 strcpy(drv, "vidconsole_tt");
318         } else {
319                 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
320                          priv->rot);
321                 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
322         }
323
324         str = strdup(name);
325         if (!str)
326                 return -ENOMEM;
327         if (priv->vidconsole_drv_name)
328                 drv_name = priv->vidconsole_drv_name;
329         ret = device_bind_driver(dev, drv_name, str, &cons);
330         if (ret) {
331                 debug("%s: Cannot bind console driver\n", __func__);
332                 return ret;
333         }
334
335         ret = device_probe(cons);
336         if (ret) {
337                 debug("%s: Cannot probe console driver\n", __func__);
338                 return ret;
339         }
340
341         return 0;
342 };
343
344 /* Post-relocation, allocate memory for the frame buffer */
345 static int video_post_bind(struct udevice *dev)
346 {
347         ulong addr = gd->video_top;
348         ulong size;
349
350         /* Before relocation there is nothing to do here */
351         if (!(gd->flags & GD_FLG_RELOC))
352                 return 0;
353         size = alloc_fb(dev, &addr);
354         if (addr < gd->video_bottom) {
355                 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
356                  * 'u-boot,dm-pre-proper' tag
357                  */
358                 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
359                        dev->name);
360                 return -ENOSPC;
361         }
362         debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
363               __func__, size, addr, dev->name);
364         gd->video_bottom = addr;
365
366         return 0;
367 }
368
369 UCLASS_DRIVER(video) = {
370         .id             = UCLASS_VIDEO,
371         .name           = "video",
372         .flags          = DM_UC_FLAG_SEQ_ALIAS,
373         .post_bind      = video_post_bind,
374         .pre_probe      = video_pre_probe,
375         .post_probe     = video_post_probe,
376         .pre_remove     = video_pre_remove,
377         .per_device_auto_alloc_size     = sizeof(struct video_priv),
378         .per_device_platdata_auto_alloc_size = sizeof(struct video_uc_platdata),
379 };