xref: /rk3399_rockchip-uboot/drivers/video/video-uclass.c (revision d1e7b9e1d9259b6a26a1dc310b724936b8d5e55e)
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 
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 
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 : 2 << 20;	/* kernel cma alloc alignment. */
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 
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 &= ~((2 << 20) - 1);	/* kernel cma alloc alignment. */
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 		*addrp &= ~((2 << 20) - 1);	/* kernel cma alloc alignment. */
98 		debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
99 		      __func__, size, *addrp, dev->name);
100 	}
101 #endif
102 	gd->video_bottom = *addrp;
103 	debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
104 	      gd->video_top);
105 
106 	return 0;
107 }
108 
109 static int video_clear(struct udevice *dev)
110 {
111 	struct video_priv *priv = dev_get_uclass_priv(dev);
112 
113 	if (priv->bpix == VIDEO_BPP32) {
114 		u32 *ppix = priv->fb;
115 		u32 *end = priv->fb + priv->fb_size;
116 
117 		while (ppix < end)
118 			*ppix++ = priv->colour_bg;
119 	} else {
120 		memset(priv->fb, priv->colour_bg, priv->fb_size);
121 	}
122 
123 	return 0;
124 }
125 
126 /* Flush video activity to the caches */
127 void video_sync(struct udevice *vid)
128 {
129 	/*
130 	 * flush_dcache_range() is declared in common.h but it seems that some
131 	 * architectures do not actually implement it. Is there a way to find
132 	 * out whether it exists? For now, ARM is safe.
133 	 */
134 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF)
135 	struct video_priv *priv = dev_get_uclass_priv(vid);
136 
137 	if (priv->flush_dcache) {
138 		flush_dcache_range((ulong)priv->fb,
139 				   ALIGN((ulong)priv->fb + priv->fb_size,
140 					 CONFIG_SYS_CACHELINE_SIZE));
141 	}
142 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
143 	struct video_priv *priv = dev_get_uclass_priv(vid);
144 	static ulong last_sync;
145 
146 	if (get_timer(last_sync) > 10) {
147 		sandbox_sdl_sync(priv->fb);
148 		last_sync = get_timer(0);
149 	}
150 #endif
151 }
152 
153 void video_sync_all(void)
154 {
155 	struct udevice *dev;
156 
157 	for (uclass_find_first_device(UCLASS_VIDEO, &dev);
158 	     dev;
159 	     uclass_find_next_device(&dev)) {
160 		if (device_active(dev))
161 			video_sync(dev);
162 	}
163 }
164 
165 int video_get_xsize(struct udevice *dev)
166 {
167 	struct video_priv *priv = dev_get_uclass_priv(dev);
168 
169 	return priv->xsize;
170 }
171 
172 int video_get_ysize(struct udevice *dev)
173 {
174 	struct video_priv *priv = dev_get_uclass_priv(dev);
175 
176 	return priv->ysize;
177 }
178 
179 /* Set up the colour map */
180 static int video_pre_probe(struct udevice *dev)
181 {
182 	struct video_priv *priv = dev_get_uclass_priv(dev);
183 
184 	priv->cmap = calloc(256, sizeof(ushort));
185 	if (!priv->cmap)
186 		return -ENOMEM;
187 
188 	return 0;
189 }
190 
191 static int video_pre_remove(struct udevice *dev)
192 {
193 	struct video_priv *priv = dev_get_uclass_priv(dev);
194 
195 	free(priv->cmap);
196 
197 	return 0;
198 }
199 
200 /* Set up the display ready for use */
201 static int video_post_probe(struct udevice *dev)
202 {
203 	struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
204 	struct video_priv *priv = dev_get_uclass_priv(dev);
205 	char name[30], drv[15], *str;
206 	const char *drv_name = drv;
207 	struct udevice *cons;
208 	int ret;
209 
210 	/* Set up the line and display size */
211 	priv->fb = map_sysmem(plat->base, plat->size);
212 	priv->line_length = priv->xsize * VNBYTES(priv->bpix);
213 	priv->fb_size = priv->line_length * priv->ysize;
214 
215 	/* Set up colours - we could in future support other colours */
216 #ifdef CONFIG_SYS_WHITE_ON_BLACK
217 	priv->colour_fg = 0xffffff;
218 #else
219 	priv->colour_bg = 0xffffff;
220 #endif
221 	video_clear(dev);
222 
223 	/*
224 	 * Create a text console device. For now we always do this, although
225 	 * it might be useful to support only bitmap drawing on the device
226 	 * for boards that don't need to display text. We create a TrueType
227 	 * console if enabled, a rotated console if the video driver requests
228 	 * it, otherwise a normal console.
229 	 *
230 	 * The console can be override by setting vidconsole_drv_name before
231 	 * probing this video driver, or in the probe() method.
232 	 *
233 	 * TrueType does not support rotation at present so fall back to the
234 	 * rotated console in that case.
235 	 */
236 	if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
237 		snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
238 		strcpy(drv, "vidconsole_tt");
239 	} else {
240 		snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
241 			 priv->rot);
242 		snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
243 	}
244 
245 	str = strdup(name);
246 	if (!str)
247 		return -ENOMEM;
248 	if (priv->vidconsole_drv_name)
249 		drv_name = priv->vidconsole_drv_name;
250 	ret = device_bind_driver(dev, drv_name, str, &cons);
251 	if (ret) {
252 		debug("%s: Cannot bind console driver\n", __func__);
253 		return ret;
254 	}
255 
256 	ret = device_probe(cons);
257 	if (ret) {
258 		debug("%s: Cannot probe console driver\n", __func__);
259 		return ret;
260 	}
261 
262 	return 0;
263 };
264 
265 /* Post-relocation, allocate memory for the frame buffer */
266 static int video_post_bind(struct udevice *dev)
267 {
268 	ulong addr = gd->video_top;
269 	ulong size;
270 
271 	/* Before relocation there is nothing to do here */
272 	if ((!gd->flags & GD_FLG_RELOC))
273 		return 0;
274 	size = alloc_fb(dev, &addr);
275 	if (addr < gd->video_bottom) {
276 		/* Device tree node may need the 'u-boot,dm-pre-reloc' tag */
277 		printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
278 		       dev->name);
279 		return -ENOSPC;
280 	}
281 	debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
282 	      __func__, size, addr, dev->name);
283 	gd->video_bottom = addr;
284 
285 	return 0;
286 }
287 
288 UCLASS_DRIVER(video) = {
289 	.id		= UCLASS_VIDEO,
290 	.name		= "video",
291 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
292 	.post_bind	= video_post_bind,
293 	.pre_probe	= video_pre_probe,
294 	.post_probe	= video_post_probe,
295 	.pre_remove	= video_pre_remove,
296 	.per_device_auto_alloc_size	= sizeof(struct video_priv),
297 	.per_device_platdata_auto_alloc_size = sizeof(struct video_uc_platdata),
298 };
299