xref: /freebsd/sys/arm/freescale/vybrid/vf_dcu4.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Vybrid Family Display Control Unit (DCU4)
29  * Chapter 55, Vybrid Reference Manual, Rev. 5, 07/2013
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/rman.h>
42 #include <sys/timeet.h>
43 #include <sys/timetc.h>
44 #include <sys/watchdog.h>
45 #include <sys/fbio.h>
46 #include <sys/consio.h>
47 #include <sys/eventhandler.h>
48 #include <sys/gpio.h>
49 
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
57 
58 #include <dev/vt/vt.h>
59 #include <dev/vt/colors/vt_termcolors.h>
60 
61 #include "gpio_if.h"
62 
63 #include <machine/bus.h>
64 #include <machine/fdt.h>
65 #include <machine/cpu.h>
66 #include <machine/intr.h>
67 
68 #include "fb_if.h"
69 
70 #include <arm/freescale/vybrid/vf_common.h>
71 
72 #define	DCU_CTRLDESCCURSOR1	0x000	/* Control Descriptor Cursor 1 */
73 #define	DCU_CTRLDESCCURSOR2	0x004	/* Control Descriptor Cursor 2 */
74 #define	DCU_CTRLDESCCURSOR3	0x008	/* Control Descriptor Cursor 3 */
75 #define	DCU_CTRLDESCCURSOR4	0x00C	/* Control Descriptor Cursor 4 */
76 #define	DCU_DCU_MODE		0x010	/* DCU4 Mode */
77 #define	 DCU_MODE_M		0x3
78 #define	 DCU_MODE_S		0
79 #define	 DCU_MODE_NORMAL	0x1
80 #define	 DCU_MODE_TEST		0x2
81 #define	 DCU_MODE_COLBAR	0x3
82 #define	 RASTER_EN		(1 << 14)	/* Raster scan of pixel data */
83 #define	 PDI_EN			(1 << 13)
84 #define	 PDI_DE_MODE		(1 << 11)
85 #define	 PDI_MODE_M		2
86 #define	DCU_BGND		0x014	/* Background */
87 #define	DCU_DISP_SIZE		0x018	/* Display Size */
88 #define	 DELTA_M		0x7ff
89 #define	 DELTA_Y_S		16
90 #define	 DELTA_X_S		0
91 #define	DCU_HSYN_PARA		0x01C	/* Horizontal Sync Parameter */
92 #define	 BP_H_SHIFT		22
93 #define	 PW_H_SHIFT		11
94 #define	 FP_H_SHIFT		0
95 #define	DCU_VSYN_PARA		0x020	/* Vertical Sync Parameter */
96 #define	 BP_V_SHIFT		22
97 #define	 PW_V_SHIFT		11
98 #define	 FP_V_SHIFT		0
99 #define	DCU_SYNPOL		0x024	/* Synchronize Polarity */
100 #define	 INV_HS			(1 << 0)
101 #define	 INV_VS			(1 << 1)
102 #define	 INV_PDI_VS		(1 << 8) /* Polarity of PDI input VSYNC. */
103 #define	 INV_PDI_HS		(1 << 9) /* Polarity of PDI input HSYNC. */
104 #define	 INV_PDI_DE		(1 << 10) /* Polarity of PDI input DE. */
105 #define	DCU_THRESHOLD		0x028	/* Threshold */
106 #define	 LS_BF_VS_SHIFT		16
107 #define	 OUT_BUF_HIGH_SHIFT	8
108 #define	 OUT_BUF_LOW_SHIFT	0
109 #define	DCU_INT_STATUS		0x02C	/* Interrupt Status */
110 #define	DCU_INT_MASK		0x030	/* Interrupt Mask */
111 #define	DCU_COLBAR_1		0x034	/* COLBAR_1 */
112 #define	DCU_COLBAR_2		0x038	/* COLBAR_2 */
113 #define	DCU_COLBAR_3		0x03C	/* COLBAR_3 */
114 #define	DCU_COLBAR_4		0x040	/* COLBAR_4 */
115 #define	DCU_COLBAR_5		0x044	/* COLBAR_5 */
116 #define	DCU_COLBAR_6		0x048	/* COLBAR_6 */
117 #define	DCU_COLBAR_7		0x04C	/* COLBAR_7 */
118 #define	DCU_COLBAR_8		0x050	/* COLBAR_8 */
119 #define	DCU_DIV_RATIO		0x054	/* Divide Ratio */
120 #define	DCU_SIGN_CALC_1		0x058	/* Sign Calculation 1 */
121 #define	DCU_SIGN_CALC_2		0x05C	/* Sign Calculation 2 */
122 #define	DCU_CRC_VAL		0x060	/* CRC Value */
123 #define	DCU_PDI_STATUS		0x064	/* PDI Status */
124 #define	DCU_PDI_STA_MSK		0x068	/* PDI Status Mask */
125 #define	DCU_PARR_ERR_STATUS1	0x06C	/* Parameter Error Status 1 */
126 #define	DCU_PARR_ERR_STATUS2	0x070	/* Parameter Error Status 2 */
127 #define	DCU_PARR_ERR_STATUS3	0x07C	/* Parameter Error Status 3 */
128 #define	DCU_MASK_PARR_ERR_ST1	0x080	/* Mask Parameter Error Status 1 */
129 #define	DCU_MASK_PARR_ERR_ST2	0x084	/* Mask Parameter Error Status 2 */
130 #define	DCU_MASK_PARR_ERR_ST3	0x090	/* Mask Parameter Error Status 3 */
131 #define	DCU_THRESHOLD_INP_BUF_1	0x094	/* Threshold Input 1 */
132 #define	DCU_THRESHOLD_INP_BUF_2	0x098	/* Threshold Input 2 */
133 #define	DCU_THRESHOLD_INP_BUF_3	0x09C	/* Threshold Input 3 */
134 #define	DCU_LUMA_COMP		0x0A0	/* LUMA Component */
135 #define	DCU_CHROMA_RED		0x0A4	/* Red Chroma Components */
136 #define	DCU_CHROMA_GREEN	0x0A8	/* Green Chroma Components */
137 #define	DCU_CHROMA_BLUE		0x0AC	/* Blue Chroma Components */
138 #define	DCU_CRC_POS		0x0B0	/* CRC Position */
139 #define	DCU_LYR_INTPOL_EN	0x0B4	/* Layer Interpolation Enable */
140 #define	DCU_LYR_LUMA_COMP	0x0B8	/* Layer Luminance Component */
141 #define	DCU_LYR_CHRM_RED	0x0BC	/* Layer Chroma Red */
142 #define	DCU_LYR_CHRM_GRN	0x0C0	/* Layer Chroma Green */
143 #define	DCU_LYR_CHRM_BLUE	0x0C4	/* Layer Chroma Blue */
144 #define	DCU_COMP_IMSIZE		0x0C8	/* Compression Image Size */
145 #define	DCU_UPDATE_MODE		0x0CC	/* Update Mode */
146 #define	 READREG		(1 << 30)
147 #define	 MODE			(1 << 31)
148 #define	DCU_UNDERRUN		0x0D0	/* Underrun */
149 #define	DCU_GLBL_PROTECT	0x100	/* Global Protection */
150 #define	DCU_SFT_LCK_BIT_L0	0x104	/* Soft Lock Bit Layer 0 */
151 #define	DCU_SFT_LCK_BIT_L1	0x108	/* Soft Lock Bit Layer 1 */
152 #define	DCU_SFT_LCK_DISP_SIZE	0x10C	/* Soft Lock Display Size */
153 #define	DCU_SFT_LCK_HS_VS_PARA	0x110	/* Soft Lock Hsync/Vsync Parameter */
154 #define	DCU_SFT_LCK_POL		0x114	/* Soft Lock POL */
155 #define	DCU_SFT_LCK_L0_TRANSP	0x118	/* Soft Lock L0 Transparency */
156 #define	DCU_SFT_LCK_L1_TRANSP	0x11C	/* Soft Lock L1 Transparency */
157 
158 /* Control Descriptor */
159 #define DCU_CTRLDESCL(n, m)	0x200 + (0x40 * n) + 0x4 * (m - 1)
160 #define DCU_CTRLDESCLn_1(n)	DCU_CTRLDESCL(n, 1)
161 #define DCU_CTRLDESCLn_2(n)	DCU_CTRLDESCL(n, 2)
162 #define DCU_CTRLDESCLn_3(n)	DCU_CTRLDESCL(n, 3)
163 #define	 TRANS_SHIFT		20
164 #define DCU_CTRLDESCLn_4(n)	DCU_CTRLDESCL(n, 4)
165 #define	 BPP_MASK		0xf		/* Bit per pixel Mask */
166 #define	 BPP_SHIFT		16		/* Bit per pixel Shift */
167 #define	 BPP24			0x5
168 #define	 EN_LAYER		(1 << 31)	/* Enable the layer */
169 #define DCU_CTRLDESCLn_5(n)	DCU_CTRLDESCL(n, 5)
170 #define DCU_CTRLDESCLn_6(n)	DCU_CTRLDESCL(n, 6)
171 #define DCU_CTRLDESCLn_7(n)	DCU_CTRLDESCL(n, 7)
172 #define DCU_CTRLDESCLn_8(n)	DCU_CTRLDESCL(n, 8)
173 #define DCU_CTRLDESCLn_9(n)	DCU_CTRLDESCL(n, 9)
174 
175 #define	NUM_LAYERS	64
176 
177 struct panel_info {
178 	uint32_t	width;
179 	uint32_t	height;
180 	uint32_t	h_back_porch;
181 	uint32_t	h_pulse_width;
182 	uint32_t	h_front_porch;
183 	uint32_t	v_back_porch;
184 	uint32_t	v_pulse_width;
185 	uint32_t	v_front_porch;
186 	uint32_t	clk_div;
187 	uint32_t	backlight_pin;
188 };
189 
190 struct dcu_softc {
191 	struct resource		*res[2];
192 	bus_space_tag_t		bst;
193 	bus_space_handle_t	bsh;
194 	void			*ih;
195 	device_t		dev;
196 	device_t		sc_fbd;		/* fbd child */
197 	struct fb_info		sc_info;
198 	struct panel_info	*panel;
199 };
200 
201 static struct resource_spec dcu_spec[] = {
202 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
203 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
204 	{ -1, 0 }
205 };
206 
207 static int
208 dcu_probe(device_t dev)
209 {
210 
211 	if (!ofw_bus_status_okay(dev))
212 		return (ENXIO);
213 
214 	if (!ofw_bus_is_compatible(dev, "fsl,mvf600-dcu4"))
215 		return (ENXIO);
216 
217 	device_set_desc(dev, "Vybrid Family Display Control Unit (DCU4)");
218 	return (BUS_PROBE_DEFAULT);
219 }
220 
221 static void
222 dcu_intr(void *arg)
223 {
224 	struct dcu_softc *sc;
225 	int reg;
226 
227 	sc = arg;
228 
229 	/* Ack interrupts */
230 	reg = READ4(sc, DCU_INT_STATUS);
231 	WRITE4(sc, DCU_INT_STATUS, reg);
232 
233 	/* TODO interrupt handler */
234 }
235 
236 static int
237 get_panel_info(struct dcu_softc *sc, struct panel_info *panel)
238 {
239 	phandle_t node;
240 	pcell_t dts_value[3];
241 	int len;
242 
243 	if ((node = ofw_bus_get_node(sc->dev)) == -1)
244 		return (ENXIO);
245 
246 	/* panel size */
247 	if ((len = OF_getproplen(node, "panel-size")) <= 0)
248 		return (ENXIO);
249 	OF_getprop(node, "panel-size", &dts_value, len);
250 	panel->width = fdt32_to_cpu(dts_value[0]);
251 	panel->height = fdt32_to_cpu(dts_value[1]);
252 
253 	/* hsync */
254 	if ((len = OF_getproplen(node, "panel-hsync")) <= 0)
255 		return (ENXIO);
256 	OF_getprop(node, "panel-hsync", &dts_value, len);
257 	panel->h_back_porch = fdt32_to_cpu(dts_value[0]);
258 	panel->h_pulse_width = fdt32_to_cpu(dts_value[1]);
259 	panel->h_front_porch = fdt32_to_cpu(dts_value[2]);
260 
261 	/* vsync */
262 	if ((len = OF_getproplen(node, "panel-vsync")) <= 0)
263 		return (ENXIO);
264 	OF_getprop(node, "panel-vsync", &dts_value, len);
265 	panel->v_back_porch = fdt32_to_cpu(dts_value[0]);
266 	panel->v_pulse_width = fdt32_to_cpu(dts_value[1]);
267 	panel->v_front_porch = fdt32_to_cpu(dts_value[2]);
268 
269 	/* clk divider */
270 	if ((len = OF_getproplen(node, "panel-clk-div")) <= 0)
271 		return (ENXIO);
272 	OF_getprop(node, "panel-clk-div", &dts_value, len);
273 	panel->clk_div = fdt32_to_cpu(dts_value[0]);
274 
275 	/* backlight pin */
276 	if ((len = OF_getproplen(node, "panel-backlight-pin")) <= 0)
277 		return (ENXIO);
278 	OF_getprop(node, "panel-backlight-pin", &dts_value, len);
279 	panel->backlight_pin = fdt32_to_cpu(dts_value[0]);
280 
281 	return (0);
282 }
283 
284 static int
285 dcu_init(struct dcu_softc *sc)
286 {
287 	struct panel_info *panel;
288 	int reg;
289 	int i;
290 
291 	panel = sc->panel;
292 
293 	/* Configure DCU */
294 	reg = ((sc->sc_info.fb_height) << DELTA_Y_S);
295 	reg |= (sc->sc_info.fb_width / 16);
296 	WRITE4(sc, DCU_DISP_SIZE, reg);
297 
298 	reg = (panel->h_back_porch << BP_H_SHIFT);
299 	reg |= (panel->h_pulse_width << PW_H_SHIFT);
300 	reg |= (panel->h_front_porch << FP_H_SHIFT);
301 	WRITE4(sc, DCU_HSYN_PARA, reg);
302 
303 	reg = (panel->v_back_porch << BP_V_SHIFT);
304 	reg |= (panel->v_pulse_width << PW_V_SHIFT);
305 	reg |= (panel->v_front_porch << FP_V_SHIFT);
306 	WRITE4(sc, DCU_VSYN_PARA, reg);
307 
308 	WRITE4(sc, DCU_BGND, 0);
309 	WRITE4(sc, DCU_DIV_RATIO, panel->clk_div);
310 
311 	reg = (INV_VS | INV_HS);
312 	WRITE4(sc, DCU_SYNPOL, reg);
313 
314 	/* TODO: export to panel info */
315 	reg = (0x3 << LS_BF_VS_SHIFT);
316 	reg |= (0x78 << OUT_BUF_HIGH_SHIFT);
317 	reg |= (0 << OUT_BUF_LOW_SHIFT);
318 	WRITE4(sc, DCU_THRESHOLD, reg);
319 
320 	/* Mask all the interrupts */
321 	WRITE4(sc, DCU_INT_MASK, 0xffffffff);
322 
323 	/* Reset all layers */
324 	for (i = 0; i < NUM_LAYERS; i++) {
325 		WRITE4(sc, DCU_CTRLDESCLn_1(i), 0x0);
326 		WRITE4(sc, DCU_CTRLDESCLn_2(i), 0x0);
327 		WRITE4(sc, DCU_CTRLDESCLn_3(i), 0x0);
328 		WRITE4(sc, DCU_CTRLDESCLn_4(i), 0x0);
329 		WRITE4(sc, DCU_CTRLDESCLn_5(i), 0x0);
330 		WRITE4(sc, DCU_CTRLDESCLn_6(i), 0x0);
331 		WRITE4(sc, DCU_CTRLDESCLn_7(i), 0x0);
332 		WRITE4(sc, DCU_CTRLDESCLn_8(i), 0x0);
333 		WRITE4(sc, DCU_CTRLDESCLn_9(i), 0x0);
334 	}
335 
336 	/* Setup first layer */
337 	reg = (sc->sc_info.fb_width | (sc->sc_info.fb_height << 16));
338 	WRITE4(sc, DCU_CTRLDESCLn_1(0), reg);
339 	WRITE4(sc, DCU_CTRLDESCLn_2(0), 0x0);
340 	WRITE4(sc, DCU_CTRLDESCLn_3(0), sc->sc_info.fb_pbase);
341 	reg = (BPP24 << BPP_SHIFT);
342 	reg |= EN_LAYER;
343 	reg |= (0xFF << TRANS_SHIFT); /* completely opaque */
344 	WRITE4(sc, DCU_CTRLDESCLn_4(0), reg);
345 	WRITE4(sc, DCU_CTRLDESCLn_5(0), 0xffffff);
346 	WRITE4(sc, DCU_CTRLDESCLn_6(0), 0x0);
347 	WRITE4(sc, DCU_CTRLDESCLn_7(0), 0x0);
348 	WRITE4(sc, DCU_CTRLDESCLn_8(0), 0x0);
349 	WRITE4(sc, DCU_CTRLDESCLn_9(0), 0x0);
350 
351 	/* Enable DCU in normal mode */
352 	reg = READ4(sc, DCU_DCU_MODE);
353 	reg &= ~(DCU_MODE_M << DCU_MODE_S);
354 	reg |= (DCU_MODE_NORMAL << DCU_MODE_S);
355 	reg |= (RASTER_EN);
356 	WRITE4(sc, DCU_DCU_MODE, reg);
357 	WRITE4(sc, DCU_UPDATE_MODE, READREG);
358 
359 	return (0);
360 }
361 
362 static int
363 dcu_attach(device_t dev)
364 {
365 	struct panel_info panel;
366 	struct dcu_softc *sc;
367 	device_t gpio_dev;
368 	int err;
369 
370 	sc = device_get_softc(dev);
371 	sc->dev = dev;
372 
373 	if (bus_alloc_resources(dev, dcu_spec, sc->res)) {
374 		device_printf(dev, "could not allocate resources\n");
375 		return (ENXIO);
376 	}
377 
378 	/* Memory interface */
379 	sc->bst = rman_get_bustag(sc->res[0]);
380 	sc->bsh = rman_get_bushandle(sc->res[0]);
381 
382 	/* Setup interrupt handler */
383 	err = bus_setup_intr(dev, sc->res[1], INTR_TYPE_BIO | INTR_MPSAFE,
384 	    NULL, dcu_intr, sc, &sc->ih);
385 	if (err) {
386 		device_printf(dev, "Unable to alloc interrupt resource.\n");
387 		return (ENXIO);
388 	}
389 
390 	if (get_panel_info(sc, &panel)) {
391 		device_printf(dev, "Can't get panel info\n");
392 		return (ENXIO);
393 	}
394 
395 	sc->panel = &panel;
396 
397 	/* Bypass timing control (used for raw lcd panels) */
398 	tcon_bypass();
399 
400 	/* Get the GPIO device, we need this to give power to USB */
401 	gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
402 	if (gpio_dev == NULL) {
403 		device_printf(sc->dev, "Error: failed to get the GPIO dev\n");
404 		return (1);
405 	}
406 
407 	/* Turn on backlight */
408 	/* TODO: Use FlexTimer/PWM */
409 	GPIO_PIN_SETFLAGS(gpio_dev, panel.backlight_pin, GPIO_PIN_OUTPUT);
410 	GPIO_PIN_SET(gpio_dev, panel.backlight_pin, GPIO_PIN_HIGH);
411 
412 	sc->sc_info.fb_width = panel.width;
413 	sc->sc_info.fb_height = panel.height;
414 	sc->sc_info.fb_stride = sc->sc_info.fb_width * 3;
415 	sc->sc_info.fb_bpp = sc->sc_info.fb_depth = 24;
416 	sc->sc_info.fb_size = sc->sc_info.fb_height * sc->sc_info.fb_stride;
417 	sc->sc_info.fb_vbase = (intptr_t)contigmalloc(sc->sc_info.fb_size,
418 	    M_DEVBUF, M_ZERO, 0, ~0, PAGE_SIZE, 0);
419 	sc->sc_info.fb_pbase = (intptr_t)vtophys(sc->sc_info.fb_vbase);
420 
421 #if 0
422 	printf("%dx%d [%d]\n", sc->sc_info.fb_width, sc->sc_info.fb_height,
423 	    sc->sc_info.fb_stride);
424 	printf("pbase == 0x%08x\n", sc->sc_info.fb_pbase);
425 #endif
426 
427 	memset((int8_t *)sc->sc_info.fb_vbase, 0x0, sc->sc_info.fb_size);
428 
429 	dcu_init(sc);
430 
431 	sc->sc_info.fb_name = device_get_nameunit(dev);
432 
433 	/* Ask newbus to attach framebuffer device to me. */
434 	sc->sc_fbd = device_add_child(dev, "fbd", device_get_unit(dev));
435 	if (sc->sc_fbd == NULL)
436 		device_printf(dev, "Can't attach fbd device\n");
437 
438 	if (device_probe_and_attach(sc->sc_fbd) != 0) {
439 		device_printf(sc->dev, "Failed to attach fbd device\n");
440 	}
441 
442 	return (0);
443 }
444 
445 static struct fb_info *
446 dcu4_fb_getinfo(device_t dev)
447 {
448 	struct dcu_softc *sc = device_get_softc(dev);
449 
450 	return (&sc->sc_info);
451 }
452 
453 static device_method_t dcu_methods[] = {
454 	DEVMETHOD(device_probe,		dcu_probe),
455 	DEVMETHOD(device_attach,	dcu_attach),
456 
457 	/* Framebuffer service methods */
458 	DEVMETHOD(fb_getinfo,		dcu4_fb_getinfo),
459 	{ 0, 0 }
460 };
461 
462 static driver_t dcu_driver = {
463 	"fb",
464 	dcu_methods,
465 	sizeof(struct dcu_softc),
466 };
467 
468 static devclass_t dcu_devclass;
469 
470 DRIVER_MODULE(fb, simplebus, dcu_driver, dcu_devclass, 0, 0);
471