blob: 21452a1cda97b90e1cbfa9682b435dcbae282d56 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass1acafc72016-01-18 19:52:15 -07002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glass1acafc72016-01-18 19:52:15 -07004 */
5
6#include <common.h>
Simon Glass9beac5d2020-07-02 21:12:20 -06007#include <console.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass1acafc72016-01-18 19:52:15 -07009#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <malloc.h>
Simon Glass1acafc72016-01-18 19:52:15 -070012#include <mapmem.h>
13#include <stdio_dev.h>
14#include <video.h>
15#include <video_console.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass1acafc72016-01-18 19:52:15 -070017#include <dm/lists.h>
Michal Simek9de731f2020-12-14 08:47:52 +010018#include <dm/device_compat.h>
Simon Glass1acafc72016-01-18 19:52:15 -070019#include <dm/device-internal.h>
20#include <dm/uclass-internal.h>
21#ifdef CONFIG_SANDBOX
22#include <asm/sdl.h>
23#endif
24
25/*
26 * Theory of operation:
27 *
28 * Before relocation each device is bound. The driver for each device must
29 * set the @align and @size values in struct video_uc_platdata. 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.
34 *
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.
38 *
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.
42 *
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.
47 */
48DECLARE_GLOBAL_DATA_PTR;
49
Simon Glass7812bbd2020-07-02 21:12:32 -060050/**
51 * struct video_uc_priv - Information for the video uclass
52 *
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
57 * gd->video_bottom.
58 */
59struct video_uc_priv {
60 ulong video_ptr;
61};
62
Simon Glass68dcdc92016-01-21 19:44:52 -070063void video_set_flush_dcache(struct udevice *dev, bool flush)
64{
65 struct video_priv *priv = dev_get_uclass_priv(dev);
66
67 priv->flush_dcache = flush;
68}
69
Simon Glass1acafc72016-01-18 19:52:15 -070070static ulong alloc_fb(struct udevice *dev, ulong *addrp)
71{
72 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
73 ulong base, align, size;
74
Bin Meng39683982016-10-09 04:14:17 -070075 if (!plat->size)
76 return 0;
77
Simon Glass1acafc72016-01-18 19:52:15 -070078 align = plat->align ? plat->align : 1 << 20;
79 base = *addrp - plat->size;
80 base &= ~(align - 1);
81 plat->base = base;
82 size = *addrp - base;
83 *addrp = base;
84
85 return size;
86}
87
88int video_reserve(ulong *addrp)
89{
90 struct udevice *dev;
91 ulong size;
92
93 gd->video_top = *addrp;
94 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
95 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);
100 }
Simon Glass551ca0e2020-07-02 21:12:33 -0600101
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;
105
Simon Glass1acafc72016-01-18 19:52:15 -0700106 gd->video_bottom = *addrp;
Simon Glassaef43ea2020-05-10 14:17:01 -0600107 gd->fb_base = *addrp;
Simon Glass1acafc72016-01-18 19:52:15 -0700108 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
109 gd->video_top);
110
111 return 0;
112}
113
Simon Glassc6ebd012018-10-01 12:22:26 -0600114int video_clear(struct udevice *dev)
Simon Glass1acafc72016-01-18 19:52:15 -0700115{
116 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glass138dfea2020-07-02 21:12:22 -0600117 int ret;
Simon Glass1acafc72016-01-18 19:52:15 -0700118
Heinrich Schuchardtd7a75d32018-02-08 21:47:10 +0100119 switch (priv->bpix) {
Simon Glass0c20aaf2019-12-20 18:10:37 -0700120 case VIDEO_BPP16:
121 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
122 u16 *ppix = priv->fb;
123 u16 *end = priv->fb + priv->fb_size;
Heinrich Schuchardtd7a75d32018-02-08 21:47:10 +0100124
Simon Glass0c20aaf2019-12-20 18:10:37 -0700125 while (ppix < end)
126 *ppix++ = priv->colour_bg;
127 break;
128 }
129 case VIDEO_BPP32:
130 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
131 u32 *ppix = priv->fb;
132 u32 *end = priv->fb + priv->fb_size;
Simon Glass1acafc72016-01-18 19:52:15 -0700133
Simon Glass0c20aaf2019-12-20 18:10:37 -0700134 while (ppix < end)
135 *ppix++ = priv->colour_bg;
136 break;
137 }
Heinrich Schuchardtd7a75d32018-02-08 21:47:10 +0100138 default:
Simon Glass1acafc72016-01-18 19:52:15 -0700139 memset(priv->fb, priv->colour_bg, priv->fb_size);
Heinrich Schuchardtd7a75d32018-02-08 21:47:10 +0100140 break;
Simon Glass1acafc72016-01-18 19:52:15 -0700141 }
Simon Glass138dfea2020-07-02 21:12:22 -0600142 ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
143 if (ret)
144 return ret;
Simon Glassc6ebd012018-10-01 12:22:26 -0600145
Michal Simek53376632020-12-15 15:12:09 +0100146 return video_sync(dev, false);
Simon Glass1acafc72016-01-18 19:52:15 -0700147}
148
Simon Glassb9f210a2018-11-06 15:21:36 -0700149void video_set_default_colors(struct udevice *dev, bool invert)
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100150{
Simon Glassb9f210a2018-11-06 15:21:36 -0700151 struct video_priv *priv = dev_get_uclass_priv(dev);
152 int fore, back;
153
Simon Glass0c20aaf2019-12-20 18:10:37 -0700154 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;
157 back = VID_BLACK;
158 } else {
159 fore = VID_BLACK;
160 back = VID_WHITE;
161 }
Simon Glassb9f210a2018-11-06 15:21:36 -0700162 if (invert) {
163 int temp;
164
165 temp = fore;
166 fore = back;
167 back = temp;
168 }
169 priv->fg_col_idx = fore;
Andre Przywaraeabb0722019-03-23 01:29:56 +0000170 priv->bg_col_idx = back;
Simon Glassb9f210a2018-11-06 15:21:36 -0700171 priv->colour_fg = vid_console_color(priv, fore);
172 priv->colour_bg = vid_console_color(priv, back);
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100173}
174
Simon Glass1acafc72016-01-18 19:52:15 -0700175/* Flush video activity to the caches */
Michal Simek9de731f2020-12-14 08:47:52 +0100176int video_sync(struct udevice *vid, bool force)
Simon Glass1acafc72016-01-18 19:52:15 -0700177{
Michal Simek9d69c2d2020-12-03 09:30:00 +0100178 struct video_ops *ops = video_get_ops(vid);
179 int ret;
180
181 if (ops && ops->video_sync) {
182 ret = ops->video_sync(vid);
183 if (ret)
184 return ret;
185 }
186
Simon Glass1acafc72016-01-18 19:52:15 -0700187 /*
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.
191 */
Trevor Woerner10015022019-05-03 09:41:00 -0400192#if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass1acafc72016-01-18 19:52:15 -0700193 struct video_priv *priv = dev_get_uclass_priv(vid);
194
195 if (priv->flush_dcache) {
196 flush_dcache_range((ulong)priv->fb,
Simon Glass79813942016-11-13 14:22:06 -0700197 ALIGN((ulong)priv->fb + priv->fb_size,
198 CONFIG_SYS_CACHELINE_SIZE));
Simon Glass1acafc72016-01-18 19:52:15 -0700199 }
200#elif defined(CONFIG_VIDEO_SANDBOX_SDL)
201 struct video_priv *priv = dev_get_uclass_priv(vid);
202 static ulong last_sync;
203
Simon Glass55d39912018-10-01 11:55:14 -0600204 if (force || get_timer(last_sync) > 10) {
Simon Glass1acafc72016-01-18 19:52:15 -0700205 sandbox_sdl_sync(priv->fb);
206 last_sync = get_timer(0);
207 }
208#endif
Michal Simek9de731f2020-12-14 08:47:52 +0100209 return 0;
Simon Glass1acafc72016-01-18 19:52:15 -0700210}
211
212void video_sync_all(void)
213{
214 struct udevice *dev;
Michal Simek9de731f2020-12-14 08:47:52 +0100215 int ret;
Simon Glass1acafc72016-01-18 19:52:15 -0700216
217 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
218 dev;
219 uclass_find_next_device(&dev)) {
Michal Simek9de731f2020-12-14 08:47:52 +0100220 if (device_active(dev)) {
221 ret = video_sync(dev, true);
222 if (ret)
223 dev_dbg(dev, "Video sync failed\n");
224 }
Simon Glass1acafc72016-01-18 19:52:15 -0700225 }
226}
227
228int video_get_xsize(struct udevice *dev)
229{
230 struct video_priv *priv = dev_get_uclass_priv(dev);
231
232 return priv->xsize;
233}
234
235int video_get_ysize(struct udevice *dev)
236{
237 struct video_priv *priv = dev_get_uclass_priv(dev);
238
239 return priv->ysize;
240}
241
Simon Glass9beac5d2020-07-02 21:12:20 -0600242#ifdef CONFIG_VIDEO_COPY
243int video_sync_copy(struct udevice *dev, void *from, void *to)
244{
245 struct video_priv *priv = dev_get_uclass_priv(dev);
246
247 if (priv->copy_fb) {
248 long offset, size;
249
250 /* Find the offset of the first byte to copy */
251 if ((ulong)to > (ulong)from) {
252 size = to - from;
253 offset = from - priv->fb;
254 } else {
255 size = from - to;
256 offset = to - priv->fb;
257 }
258
259 /*
260 * Allow a bit of leeway for valid requests somewhere near the
261 * frame buffer
262 */
263 if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
264#ifdef DEBUG
265 char str[80];
266
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);
271#endif
272 return -EFAULT;
273 }
274
275 /*
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
280 */
281 if (offset + size > priv->fb_size) {
282 size = priv->fb_size - offset;
283 } else if (offset < 0) {
284 size += offset;
285 offset = 0;
286 }
287
288 memcpy(priv->copy_fb + offset, priv->fb + offset, size);
289 }
290
291 return 0;
292}
293#endif
294
Simon Glass1acafc72016-01-18 19:52:15 -0700295/* Set up the colour map */
296static int video_pre_probe(struct udevice *dev)
297{
298 struct video_priv *priv = dev_get_uclass_priv(dev);
299
300 priv->cmap = calloc(256, sizeof(ushort));
301 if (!priv->cmap)
302 return -ENOMEM;
303
304 return 0;
305}
306
307static int video_pre_remove(struct udevice *dev)
308{
309 struct video_priv *priv = dev_get_uclass_priv(dev);
310
311 free(priv->cmap);
312
313 return 0;
314}
315
316/* Set up the display ready for use */
317static int video_post_probe(struct udevice *dev)
318{
319 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
320 struct video_priv *priv = dev_get_uclass_priv(dev);
321 char name[30], drv[15], *str;
Simon Glass826f35f2016-01-14 18:10:48 -0700322 const char *drv_name = drv;
Simon Glass1acafc72016-01-18 19:52:15 -0700323 struct udevice *cons;
324 int ret;
325
326 /* Set up the line and display size */
327 priv->fb = map_sysmem(plat->base, plat->size);
Simon Glass06696eb2018-11-29 15:08:52 -0700328 if (!priv->line_length)
329 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
330
Simon Glass1acafc72016-01-18 19:52:15 -0700331 priv->fb_size = priv->line_length * priv->ysize;
332
Simon Glass6efa8092020-07-02 21:12:21 -0600333 if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
334 priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
335
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100336 /* Set up colors */
Simon Glassb9f210a2018-11-06 15:21:36 -0700337 video_set_default_colors(dev, false);
Rob Clark8ef05352017-08-03 12:47:01 -0400338
339 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
340 video_clear(dev);
Simon Glass1acafc72016-01-18 19:52:15 -0700341
Simon Glass83510762016-01-18 19:52:17 -0700342 /*
Simon Glass826f35f2016-01-14 18:10:48 -0700343 * Create a text console device. For now we always do this, although
Simon Glass83510762016-01-18 19:52:17 -0700344 * it might be useful to support only bitmap drawing on the device
Simon Glass826f35f2016-01-14 18:10:48 -0700345 * for boards that don't need to display text. We create a TrueType
346 * console if enabled, a rotated console if the video driver requests
347 * it, otherwise a normal console.
348 *
349 * The console can be override by setting vidconsole_drv_name before
350 * probing this video driver, or in the probe() method.
351 *
352 * TrueType does not support rotation at present so fall back to the
353 * rotated console in that case.
Simon Glass83510762016-01-18 19:52:17 -0700354 */
Simon Glass826f35f2016-01-14 18:10:48 -0700355 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
Simon Glassa29b0122016-01-14 18:10:42 -0700356 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
357 strcpy(drv, "vidconsole_tt");
358 } else {
359 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
360 priv->rot);
361 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
362 }
363
Simon Glass83510762016-01-18 19:52:17 -0700364 str = strdup(name);
365 if (!str)
366 return -ENOMEM;
Simon Glass826f35f2016-01-14 18:10:48 -0700367 if (priv->vidconsole_drv_name)
368 drv_name = priv->vidconsole_drv_name;
369 ret = device_bind_driver(dev, drv_name, str, &cons);
Simon Glass83510762016-01-18 19:52:17 -0700370 if (ret) {
371 debug("%s: Cannot bind console driver\n", __func__);
372 return ret;
373 }
Simon Glass826f35f2016-01-14 18:10:48 -0700374
Simon Glass83510762016-01-18 19:52:17 -0700375 ret = device_probe(cons);
376 if (ret) {
377 debug("%s: Cannot probe console driver\n", __func__);
378 return ret;
379 }
380
Simon Glass1acafc72016-01-18 19:52:15 -0700381 return 0;
382};
383
384/* Post-relocation, allocate memory for the frame buffer */
385static int video_post_bind(struct udevice *dev)
386{
Simon Glass7812bbd2020-07-02 21:12:32 -0600387 struct video_uc_priv *uc_priv;
388 ulong addr;
Simon Glass1acafc72016-01-18 19:52:15 -0700389 ulong size;
390
391 /* Before relocation there is nothing to do here */
Tom Rini75164182018-04-22 09:47:48 -0400392 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass1acafc72016-01-18 19:52:15 -0700393 return 0;
Simon Glass7812bbd2020-07-02 21:12:32 -0600394
395 /* Set up the video pointer, if this is the first device */
396 uc_priv = dev->uclass->priv;
397 if (!uc_priv->video_ptr)
398 uc_priv->video_ptr = gd->video_top;
399
400 /* Allocate framebuffer space for this device */
401 addr = uc_priv->video_ptr;
Simon Glass1acafc72016-01-18 19:52:15 -0700402 size = alloc_fb(dev, &addr);
403 if (addr < gd->video_bottom) {
Patrick Delaunay69989742019-05-21 19:19:12 +0200404 /* Device tree node may need the 'u-boot,dm-pre-reloc' or
405 * 'u-boot,dm-pre-proper' tag
406 */
Simon Glass1acafc72016-01-18 19:52:15 -0700407 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
408 dev->name);
409 return -ENOSPC;
410 }
411 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
412 __func__, size, addr, dev->name);
Simon Glass7812bbd2020-07-02 21:12:32 -0600413 uc_priv->video_ptr = addr;
Simon Glass1acafc72016-01-18 19:52:15 -0700414
415 return 0;
416}
417
418UCLASS_DRIVER(video) = {
419 .id = UCLASS_VIDEO,
420 .name = "video",
421 .flags = DM_UC_FLAG_SEQ_ALIAS,
422 .post_bind = video_post_bind,
423 .pre_probe = video_pre_probe,
424 .post_probe = video_post_probe,
425 .pre_remove = video_pre_remove,
Simon Glass7812bbd2020-07-02 21:12:32 -0600426 .priv_auto_alloc_size = sizeof(struct video_uc_priv),
Simon Glass1acafc72016-01-18 19:52:15 -0700427 .per_device_auto_alloc_size = sizeof(struct video_priv),
428 .per_device_platdata_auto_alloc_size = sizeof(struct video_uc_platdata),
429};