1 /*
2 * Copyright (c) 2015 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <mapmem.h>
10 #include <stdio_dev.h>
11 #include <video.h>
12 #include <video_console.h>
13 #ifdef CONFIG_DRM_ROCKCHIP
14 #include <video_rockchip.h>
15 #endif
16 #include <dm/lists.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
19 #ifdef CONFIG_SANDBOX
20 #include <asm/sdl.h>
21 #endif
22
23 /*
24 * Theory of operation:
25 *
26 * Before relocation each device is bound. The driver for each device must
27 * set the @align and @size values in struct video_uc_platdata. This
28 * information represents the requires size and alignment of the frame buffer
29 * for the device. The values can be an over-estimate but cannot be too
30 * small. The actual values will be suppled (in the same manner) by the bind()
31 * method after relocation.
32 *
33 * This information is then picked up by video_reserve() which works out how
34 * much memory is needed for all devices. This is allocated between
35 * gd->video_bottom and gd->video_top.
36 *
37 * After relocation the same process occurs. The driver supplies the same
38 * @size and @align information and this time video_post_bind() checks that
39 * the drivers does not overflow the allocated memory.
40 *
41 * The frame buffer address is actually set (to plat->base) in
42 * video_post_probe(). This function also clears the frame buffer and
43 * allocates a suitable text console device. This can then be used to write
44 * text to the video device.
45 */
46 DECLARE_GLOBAL_DATA_PTR;
47
video_set_flush_dcache(struct udevice * dev,bool flush)48 void video_set_flush_dcache(struct udevice *dev, bool flush)
49 {
50 struct video_priv *priv = dev_get_uclass_priv(dev);
51
52 priv->flush_dcache = flush;
53 }
54
alloc_fb(struct udevice * dev,ulong * addrp)55 static ulong alloc_fb(struct udevice *dev, ulong *addrp)
56 {
57 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
58 ulong base, align, size;
59
60 if (!plat->size)
61 return 0;
62
63 align = plat->align ? plat->align : 1 << 20;
64 base = *addrp - plat->size;
65 base &= ~(align - 1);
66 plat->base = base;
67 size = *addrp - base;
68 *addrp = base;
69
70 return size;
71 }
72
video_reserve(ulong * addrp)73 int video_reserve(ulong *addrp)
74 {
75 #ifndef CONFIG_DRM_ROCKCHIP
76 struct udevice *dev;
77 #endif
78 ulong size;
79
80 gd->video_top = *addrp;
81 #ifdef CONFIG_DRM_ROCKCHIP
82 int cubic_lut_step = CONFIG_ROCKCHIP_CUBIC_LUT_SIZE;
83 /* This is depend on IC designed */
84 ulong cubic_lut_size = (cubic_lut_step * cubic_lut_step * cubic_lut_step + 1) / 2 * 16;
85 /* Max support 4 cubic lut */
86 cubic_lut_size = roundup(cubic_lut_size, PAGE_SIZE) << 2;
87
88 size = DRM_ROCKCHIP_FB_SIZE + MEMORY_POOL_SIZE + cubic_lut_size;
89 *addrp = *addrp - size;
90 *addrp &= ~((1 << 20) - 1);
91 debug("Reserving %lx Bytes for video at: %lx\n", size, *addrp);
92 #else
93 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
94 dev;
95 uclass_find_next_device(&dev)) {
96 size = alloc_fb(dev, addrp);
97 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
98 __func__, size, *addrp, dev->name);
99 }
100 #endif
101 gd->video_bottom = *addrp;
102 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
103 gd->video_top);
104
105 return 0;
106 }
107
video_clear(struct udevice * dev)108 static int video_clear(struct udevice *dev)
109 {
110 struct video_priv *priv = dev_get_uclass_priv(dev);
111
112 if (priv->bpix == VIDEO_BPP32) {
113 u32 *ppix = priv->fb;
114 u32 *end = priv->fb + priv->fb_size;
115
116 while (ppix < end)
117 *ppix++ = priv->colour_bg;
118 } else {
119 memset(priv->fb, priv->colour_bg, priv->fb_size);
120 }
121
122 return 0;
123 }
124
125 /* Flush video activity to the caches */
video_sync(struct udevice * vid)126 void video_sync(struct udevice *vid)
127 {
128 /*
129 * flush_dcache_range() is declared in common.h but it seems that some
130 * architectures do not actually implement it. Is there a way to find
131 * out whether it exists? For now, ARM is safe.
132 */
133 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
134 struct video_priv *priv = dev_get_uclass_priv(vid);
135
136 if (priv->flush_dcache) {
137 flush_dcache_range((ulong)priv->fb,
138 ALIGN((ulong)priv->fb + priv->fb_size,
139 CONFIG_SYS_CACHELINE_SIZE));
140 }
141 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
142 struct video_priv *priv = dev_get_uclass_priv(vid);
143 static ulong last_sync;
144
145 if (get_timer(last_sync) > 10) {
146 sandbox_sdl_sync(priv->fb);
147 last_sync = get_timer(0);
148 }
149 #endif
150 }
151
video_sync_all(void)152 void video_sync_all(void)
153 {
154 struct udevice *dev;
155
156 for (uclass_find_first_device(UCLASS_VIDEO, &dev);
157 dev;
158 uclass_find_next_device(&dev)) {
159 if (device_active(dev))
160 video_sync(dev);
161 }
162 }
163
video_get_xsize(struct udevice * dev)164 int video_get_xsize(struct udevice *dev)
165 {
166 struct video_priv *priv = dev_get_uclass_priv(dev);
167
168 return priv->xsize;
169 }
170
video_get_ysize(struct udevice * dev)171 int video_get_ysize(struct udevice *dev)
172 {
173 struct video_priv *priv = dev_get_uclass_priv(dev);
174
175 return priv->ysize;
176 }
177
178 /* Set up the colour map */
video_pre_probe(struct udevice * dev)179 static int video_pre_probe(struct udevice *dev)
180 {
181 struct video_priv *priv = dev_get_uclass_priv(dev);
182
183 priv->cmap = calloc(256, sizeof(ushort));
184 if (!priv->cmap)
185 return -ENOMEM;
186
187 return 0;
188 }
189
video_pre_remove(struct udevice * dev)190 static int video_pre_remove(struct udevice *dev)
191 {
192 struct video_priv *priv = dev_get_uclass_priv(dev);
193
194 free(priv->cmap);
195
196 return 0;
197 }
198
199 /* Set up the display ready for use */
video_post_probe(struct udevice * dev)200 static int video_post_probe(struct udevice *dev)
201 {
202 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
203 struct video_priv *priv = dev_get_uclass_priv(dev);
204 char name[30], drv[15], *str;
205 const char *drv_name = drv;
206 struct udevice *cons;
207 int ret;
208
209 /* Set up the line and display size */
210 priv->fb = map_sysmem(plat->base, plat->size);
211 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
212 priv->fb_size = priv->line_length * priv->ysize;
213
214 /* Set up colours - we could in future support other colours */
215 #ifdef CONFIG_SYS_WHITE_ON_BLACK
216 priv->colour_fg = 0xffffff;
217 #else
218 priv->colour_bg = 0xffffff;
219 #endif
220 video_clear(dev);
221
222 /*
223 * Create a text console device. For now we always do this, although
224 * it might be useful to support only bitmap drawing on the device
225 * for boards that don't need to display text. We create a TrueType
226 * console if enabled, a rotated console if the video driver requests
227 * it, otherwise a normal console.
228 *
229 * The console can be override by setting vidconsole_drv_name before
230 * probing this video driver, or in the probe() method.
231 *
232 * TrueType does not support rotation at present so fall back to the
233 * rotated console in that case.
234 */
235 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
236 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
237 strcpy(drv, "vidconsole_tt");
238 } else {
239 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
240 priv->rot);
241 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
242 }
243
244 str = strdup(name);
245 if (!str)
246 return -ENOMEM;
247 if (priv->vidconsole_drv_name)
248 drv_name = priv->vidconsole_drv_name;
249 ret = device_bind_driver(dev, drv_name, str, &cons);
250 if (ret) {
251 debug("%s: Cannot bind console driver\n", __func__);
252 return ret;
253 }
254
255 ret = device_probe(cons);
256 if (ret) {
257 debug("%s: Cannot probe console driver\n", __func__);
258 return ret;
259 }
260
261 return 0;
262 };
263
264 /* Post-relocation, allocate memory for the frame buffer */
video_post_bind(struct udevice * dev)265 static int video_post_bind(struct udevice *dev)
266 {
267 ulong addr = gd->video_top;
268 ulong size;
269
270 /* Before relocation there is nothing to do here */
271 if ((!gd->flags & GD_FLG_RELOC))
272 return 0;
273 size = alloc_fb(dev, &addr);
274 if (addr < gd->video_bottom) {
275 /* Device tree node may need the 'u-boot,dm-pre-reloc' tag */
276 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
277 dev->name);
278 return -ENOSPC;
279 }
280 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
281 __func__, size, addr, dev->name);
282 gd->video_bottom = addr;
283
284 return 0;
285 }
286
287 UCLASS_DRIVER(video) = {
288 .id = UCLASS_VIDEO,
289 .name = "video",
290 .flags = DM_UC_FLAG_SEQ_ALIAS,
291 .post_bind = video_post_bind,
292 .pre_probe = video_pre_probe,
293 .post_probe = video_post_probe,
294 .pre_remove = video_pre_remove,
295 .per_device_auto_alloc_size = sizeof(struct video_priv),
296 .per_device_platdata_auto_alloc_size = sizeof(struct video_uc_platdata),
297 };
298