xref: /netbsd-src/sys/arch/arm/imx/imx51_ipuv3.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: imx51_ipuv3.c,v 1.3 2014/11/07 11:54:18 hkenken Exp $	*/
2 
3 /*
4  * Copyright (c) 2011, 2012  Genetec Corporation.  All rights reserved.
5  * Written by Hashimoto Kenichi for Genetec Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: imx51_ipuv3.c,v 1.3 2014/11/07 11:54:18 hkenken Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/uio.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>			/* for cold */
38 #include <sys/pmf.h>
39 
40 #include <uvm/uvm_extern.h>
41 
42 #include <dev/cons.h>
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/wscons/wsdisplayvar.h>
45 #include <dev/wscons/wscons_callbacks.h>
46 #include <dev/rasops/rasops.h>
47 #include <dev/wsfont/wsfont.h>
48 #include <dev/wscons/wsdisplay_vconsvar.h>
49 
50 #include <sys/bus.h>
51 #include <machine/cpu.h>
52 #include <arm/cpufunc.h>
53 
54 #include <arm/imx/imx51var.h>
55 #include <arm/imx/imx51reg.h>
56 #include <arm/imx/imx51_ipuv3var.h>
57 #include <arm/imx/imx51_ipuv3reg.h>
58 #include <arm/imx/imx51_ccmvar.h>
59 #include <arm/imx/imx51_ccmreg.h>
60 
61 #include "imxccm.h"	/* if CCM driver is configured into the kernel */
62 #include "wsdisplay.h"
63 #include "opt_imx51_ipuv3.h"
64 
65 /*
66  * Console variables. These are necessary since console is setup very early,
67  * before devices get attached.
68  */
69 struct {
70 	int is_console;
71 	struct imx51_wsscreen_descr *descr;
72 	struct wsdisplay_accessops *accessops;
73 	const struct lcd_panel_geometry *geom;
74 } imx51_ipuv3_console;
75 
76 #define	IPUV3_READ(ipuv3, module, reg)					      \
77 	bus_space_read_4((ipuv3)->iot, (ipuv3)->module##_ioh, (reg))
78 #define	IPUV3_WRITE(ipuv3, module, reg, val)				      \
79 	bus_space_write_4((ipuv3)->iot, (ipuv3)->module##_ioh, (reg), (val))
80 
81 #ifdef IPUV3_DEBUG
82 int ipuv3_debug = IPUV3_DEBUG;
83 #define	DPRINTFN(n,x)   if (ipuv3_debug>(n)) printf x; else
84 #else
85 #define	DPRINTFN(n,x)
86 #endif
87 
88 int ipuv3intr(void *);
89 
90 static void imx51_ipuv3_initialize(struct imx51_ipuv3_softc *,
91     const struct lcd_panel_geometry *);
92 #if NWSDISPLAY > 0
93 static void imx51_ipuv3_setup_rasops(struct imx51_ipuv3_softc *,
94     struct rasops_info *, struct imx51_wsscreen_descr *,
95     const struct lcd_panel_geometry *);
96 #endif
97 static void imx51_ipuv3_set_idma_param(uint32_t *, uint32_t, uint32_t);
98 
99 static bool imx51_ipuv3_resume(device_t, const pmf_qual_t *);
100 static bool imx51_ipuv3_suspend(device_t, const pmf_qual_t *);
101 
102 #ifdef IPUV3_DEBUG
103 static void
104 imx51_ipuv3_dump(struct imx51_ipuv3_softc *sc)
105 {
106 	int i;
107 
108 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
109 
110 #define	__DUMP(grp, reg)						\
111 	DPRINTFN(4, ("%-16s = 0x%08X\n", #reg, IPUV3_READ(sc, grp, IPU_##reg)))
112 
113 	__DUMP(cm, CM_CONF);
114 	__DUMP(cm, CM_DISP_GEN);
115 	__DUMP(idmac, IDMAC_CONF);
116 	__DUMP(idmac, IDMAC_CH_EN_1);
117 	__DUMP(idmac, IDMAC_CH_EN_2);
118 	__DUMP(idmac, IDMAC_CH_PRI_1);
119 	__DUMP(idmac, IDMAC_CH_PRI_2);
120 	__DUMP(idmac, IDMAC_BNDM_EN_1);
121 	__DUMP(idmac, IDMAC_BNDM_EN_2);
122 	__DUMP(cm, CM_CH_DB_MODE_SEL_0);
123 	__DUMP(cm, CM_CH_DB_MODE_SEL_1);
124 	__DUMP(dmfc, DMFC_WR_CHAN);
125 	__DUMP(dmfc, DMFC_WR_CHAN_DEF);
126 	__DUMP(dmfc, DMFC_DP_CHAN);
127 	__DUMP(dmfc, DMFC_DP_CHAN_DEF);
128 	__DUMP(dmfc, DMFC_IC_CTRL);
129 	__DUMP(cm, CM_FS_PROC_FLOW1);
130 	__DUMP(cm, CM_FS_PROC_FLOW2);
131 	__DUMP(cm, CM_FS_PROC_FLOW3);
132 	__DUMP(cm, CM_FS_DISP_FLOW1);
133 	__DUMP(dc, DC_DISP_CONF1_0);
134 	__DUMP(dc, DC_DISP_CONF2_0);
135 	__DUMP(dc, DC_WR_CH_CONF_5);
136 
137 	printf("*** IPU ***\n");
138 	for (i = 0; i <= 0x17c; i += 4)
139 		DPRINTFN(6, ("0x%08X = 0x%08X\n", i, IPUV3_READ(sc, cm, i)));
140 	printf("*** IDMAC ***\n");
141 	for (i = 0; i <= 0x104; i += 4)
142 		DPRINTFN(6, ("0x%08X = 0x%08X\n", i, IPUV3_READ(sc, idmac, i)));
143 	printf("*** CPMEM ***\n");
144 	for (i = 0x5c0; i <= 0x600; i += 4)
145 		DPRINTFN(6, ("0x%08X = 0x%08X\n", i, IPUV3_READ(sc, cpmem, i)));
146 
147 #undef __DUMP
148 
149 }
150 #endif
151 
152 static void
153 imx51_ipuv3_enable_display(struct imx51_ipuv3_softc *sc)
154 {
155 	uint32_t reg = 0;
156 
157 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
158 
159 	/* enable sub modules */
160 	reg = IPUV3_READ(sc, cm, IPU_CM_CONF);
161 	reg |= CM_CONF_DP_EN |
162 	    CM_CONF_DC_EN |
163 	    CM_CONF_DMFC_EN |
164 	    CM_CONF_DI0_EN;
165 	IPUV3_WRITE(sc, cm, IPU_CM_CONF, reg);
166 }
167 
168 static void
169 imx51_ipuv3_dmfc_init(struct imx51_ipuv3_softc *sc)
170 {
171 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
172 
173 	/* IC channel is disabled */
174 	IPUV3_WRITE(sc, dmfc, IPU_DMFC_IC_CTRL,
175 	    IC_IN_PORT_DISABLE);
176 
177 	IPUV3_WRITE(sc, dmfc, IPU_DMFC_WR_CHAN, 0x00000000);
178 	IPUV3_WRITE(sc, dmfc, IPU_DMFC_WR_CHAN_DEF, 0x20202020);
179 	IPUV3_WRITE(sc, dmfc, IPU_DMFC_DP_CHAN, 0x00000094);
180 	IPUV3_WRITE(sc, dmfc, IPU_DMFC_DP_CHAN_DEF, 0x202020F6);
181 
182 	IPUV3_WRITE(sc, dmfc, IPU_DMFC_GENERAL1,
183 	    DCDP_SYNC_PR_ROUNDROBIN);
184 
185 #ifdef IPUV3_DEBUG
186 	int i;
187 	printf("*** DMFC ***\n");
188 	for (i = 0; i <= 0x34; i += 4)
189 		printf("0x%08X = 0x%08X\n", i, IPUV3_READ(sc, dmfc, i));
190 
191 	printf("%s: DMFC_IC_CTRL         0x%08X\n", __func__,
192 	    IPUV3_READ(sc, dmfc, IPU_DMFC_IC_CTRL));
193 	printf("%s: IPU_DMFC_WR_CHAN     0x%08X\n", __func__,
194 	    IPUV3_READ(sc, dmfc, IPU_DMFC_WR_CHAN));
195 	printf("%s: IPU_DMFC_WR_CHAN_DEF 0x%08X\n", __func__,
196 	    IPUV3_READ(sc, dmfc, IPU_DMFC_WR_CHAN_DEF));
197 	printf("%s: IPU_DMFC_GENERAL1    0x%08X\n", __func__,
198 	    IPUV3_READ(sc, dmfc, IPU_DMFC_GENERAL1));
199 #endif
200 }
201 
202 static void
203 imx51_ipuv3_dc_map_clear(struct imx51_ipuv3_softc *sc, int map)
204 {
205 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
206 
207 	uint32_t reg;
208 	uint32_t addr;
209 
210 	addr = IPU_DC_MAP_CONF_PNTR(map / 2);
211 	reg = IPUV3_READ(sc, dc, addr);
212 	reg &= ~(0xFFFF << (16 * (map & 0x1)));
213 	IPUV3_WRITE(sc, dc, addr, reg);
214 }
215 
216 static void
217 imx51_ipuv3_dc_map_conf(struct imx51_ipuv3_softc *sc,
218     int map, int byte, int offset, uint8_t mask)
219 {
220 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
221 
222 	uint32_t reg;
223 	uint32_t addr;
224 
225 	addr = IPU_DC_MAP_CONF_MASK((map * 3 + byte) / 2);
226 	reg = IPUV3_READ(sc, dc, addr);
227 	reg &= ~(0xFFFF << (16 * ((map * 3 + byte) & 0x1)));
228 	reg |= ((offset << 8) | mask) << (16 * ((map * 3 + byte) & 0x1));
229 	IPUV3_WRITE(sc, dc, addr, reg);
230 #ifdef IPUV3_DEBUG
231 	printf("%s: addr 0x%08X reg 0x%08X\n", __func__, addr, reg);
232 #endif
233 
234 	addr = IPU_DC_MAP_CONF_PNTR(map / 2);
235 	reg = IPUV3_READ(sc, dc, addr);
236 	reg &= ~(0x1F << ((16 * (map & 0x1)) + (5 * byte)));
237 	reg |= ((map * 3) + byte) << ((16 * (map & 0x1)) + (5 * byte));
238 	IPUV3_WRITE(sc, dc, addr, reg);
239 #ifdef IPUV3_DEBUG
240 	printf("%s: addr 0x%08X reg 0x%08X\n", __func__, addr, reg);
241 #endif
242 }
243 
244 static void
245 imx51_ipuv3_dc_template_command(struct imx51_ipuv3_softc *sc,
246     int index, int sync, int gluelogic, int waveform, int mapping,
247     int operand, int opecode, int stop)
248 {
249 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
250 
251 	uint32_t reg;
252 
253 	reg = (sync << 0) |
254 	    (gluelogic << 4) |
255 	    (waveform << 11) |
256 	    (mapping << 15) |
257 	    (operand << 20);
258 	IPUV3_WRITE(sc, dctmpl, index * 8, reg);
259 #ifdef IPUV3_DEBUG
260 	printf("%s: addr 0x%08X reg 0x%08X\n", __func__, index * 8, reg);
261 #endif
262 	reg = (opecode << 0) |
263 	    (stop << 9);
264 	IPUV3_WRITE(sc, dctmpl, index * 8 + 4, reg);
265 #ifdef IPUV3_DEBUG
266 	printf("%s: addr 0x%08X reg 0x%08X\n", __func__, index * 8 + 4, reg);
267 #endif
268 }
269 
270 static void
271 imx51_ipuv3_set_routine_link(struct imx51_ipuv3_softc *sc,
272     int base, int evt, int addr, int pri)
273 {
274 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
275 
276 	uint32_t reg;
277 
278 	reg = IPUV3_READ(sc, dc, IPU_DC_RL(base, evt));
279 	reg &= ~(0xFFFF << (16 * (evt & 0x1)));
280 	reg |= ((addr << 8) | pri) << (16 * (evt & 0x1));
281 	IPUV3_WRITE(sc, dc, IPU_DC_RL(base, evt), reg);
282 #ifdef IPUV3_DEBUG
283 	printf("%s: event %d addr %d priority %d\n", __func__,
284 	    evt, addr, pri);
285 	printf("%s: %p = 0x%08X\n", __func__,
286 	    (void *)IPU_DC_RL(base, evt), reg);
287 #endif
288 }
289 
290 static void
291 imx51_ipuv3_dc_init(struct imx51_ipuv3_softc *sc)
292 {
293 	uint32_t reg;
294 
295 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
296 
297 	imx51_ipuv3_dc_map_clear(sc, 0);
298 	imx51_ipuv3_dc_map_conf(sc, 0, 0,  7, 0xff);
299 	imx51_ipuv3_dc_map_conf(sc, 0, 1, 15, 0xff);
300 	imx51_ipuv3_dc_map_conf(sc, 0, 2, 23, 0xff);
301 	imx51_ipuv3_dc_map_clear(sc, 1);
302 	imx51_ipuv3_dc_map_conf(sc, 1, 0,  5, 0xfc);
303 	imx51_ipuv3_dc_map_conf(sc, 1, 1, 11, 0xfc);
304 	imx51_ipuv3_dc_map_conf(sc, 1, 2, 17, 0xfc);
305 	imx51_ipuv3_dc_map_clear(sc, 2);
306 	imx51_ipuv3_dc_map_conf(sc, 2, 0, 15, 0xff);
307 	imx51_ipuv3_dc_map_conf(sc, 2, 1, 23, 0xff);
308 	imx51_ipuv3_dc_map_conf(sc, 2, 2,  7, 0xff);
309 	imx51_ipuv3_dc_map_clear(sc, 3);
310 	imx51_ipuv3_dc_map_conf(sc, 3, 0,  4, 0xf8);
311 	imx51_ipuv3_dc_map_conf(sc, 3, 1, 10, 0xfc);
312 	imx51_ipuv3_dc_map_conf(sc, 3, 2, 15, 0xf8);
313 	imx51_ipuv3_dc_map_clear(sc, 4);
314 	imx51_ipuv3_dc_map_conf(sc, 4, 0,  5, 0xfc);
315 	imx51_ipuv3_dc_map_conf(sc, 4, 1, 13, 0xfc);
316 	imx51_ipuv3_dc_map_conf(sc, 4, 2, 21, 0xfc);
317 
318 	/* microcode */
319 	imx51_ipuv3_dc_template_command(sc,
320 	    5, 5, 8, 1, 5, 0, 0x180, 1);
321 	imx51_ipuv3_dc_template_command(sc,
322 	    6, 5, 4, 1, 5, 0, 0x180, 1);
323 	imx51_ipuv3_dc_template_command(sc,
324 	    7, 5, 0, 1, 5, 0, 0x180, 1);
325 
326 	reg = (4 << 5) | 0x2;
327 	IPUV3_WRITE(sc, dc, IPU_DC_WR_CH_CONF_5, reg);
328 	IPUV3_WRITE(sc, dc, IPU_DC_WR_CH_CONF_1, 0x4);
329 	IPUV3_WRITE(sc, dc, IPU_DC_WR_CH_ADDR_5, 0x0);
330 	IPUV3_WRITE(sc, dc, IPU_DC_GEN, 0x44);
331 
332 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_NL, 5, 3);
333 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_EOL, 6, 2);
334 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_NEW_DATA, 7, 1);
335 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_NF, 0, 0);
336 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_NFIELD, 0, 0);
337 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_EOF, 0, 0);
338 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_EOFIELD, 0, 0);
339 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_NEW_CHAN, 0, 0);
340 	imx51_ipuv3_set_routine_link(sc, DC_RL_CH_5, DC_RL_EVT_NEW_ADDR, 0, 0);
341 
342 #ifdef IPUV3_DEBUG
343 	int i;
344 	printf("*** DC ***\n");
345 	for (i = 0; i <= 0x1C8; i += 4)
346 		printf("0x%08X = 0x%08X\n", i, IPUV3_READ(sc, dc, i));
347 	printf("*** DCTEMPL ***\n");
348 	for (i = 0; i <= 0x100; i += 4)
349 		printf("0x%08X = 0x%08X\n", i, IPUV3_READ(sc, dctmpl, i));
350 #endif
351 }
352 
353 static void
354 imx51_ipuv3_dc_display_config(struct imx51_ipuv3_softc *sc, int width)
355 {
356 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
357 
358 	IPUV3_WRITE(sc, dc, IPU_DC_DISP_CONF2_0, width);
359 }
360 
361 static void
362 imx51_ipuv3_di_sync_conf(struct imx51_ipuv3_softc *sc, int no,
363     uint32_t reg_gen0, uint32_t reg_gen1, uint32_t repeat)
364 {
365 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
366 
367 	uint32_t reg;
368 
369 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN0(no), reg_gen0);
370 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN1(no), reg_gen1);
371 	reg = IPUV3_READ(sc, di0, IPU_DI_STP_REP(no));
372 	reg &= ~DI_STP_REP(no);
373 	reg |= __SHIFTIN(repeat, DI_STP_REP(no));
374 	IPUV3_WRITE(sc, di0, IPU_DI_STP_REP(no), reg);
375 
376 #ifdef IPUV3_DEBUG
377 	printf("%s: no %d\n", __func__, no);
378 	printf("%s: addr 0x%08X reg_gen0   0x%08X\n", __func__,
379 	    IPU_DI_SW_GEN0(no), reg_gen0);
380 	printf("%s: addr 0x%08X reg_gen1   0x%08X\n", __func__,
381 	    IPU_DI_SW_GEN1(no), reg_gen1);
382 	printf("%s: addr 0x%08X DI_STP_REP 0x%08X\n", __func__,
383 	    IPU_DI_STP_REP(no), reg);
384 #endif
385 }
386 
387 static void
388 imx51_ipuv3_di_init(struct imx51_ipuv3_softc *sc)
389 {
390 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
391 
392 	uint32_t reg;
393 	uint32_t div;
394 	u_int ipuclk;
395 	const struct lcd_panel_geometry *geom = sc->geometry;
396 
397 #if NIMXCCM > 0
398 	ipuclk = imx51_get_clock(IMX51CLK_IPU_HSP_CLK_ROOT);
399 #elif !defined(IMX51_IPU_HSP_CLOCK)
400 #error	IMX51_CPU_HSP_CLOCK need to be defined.
401 #else
402 	ipuclk = IMX51_IPU_HSP_CLOCK;
403 #endif
404 	DPRINTFN(3, ("IPU HSP clock = %d\n", ipuclk));
405 	div = (ipuclk * 16) / geom->pixel_clk;
406 	div = div < 16 ? 16 : div & 0xff8;
407 
408 	/* DI counter */
409 	reg = IPUV3_READ(sc, cm, IPU_CM_DISP_GEN);
410 	reg &= ~(CM_DISP_GEN_DI1_COUNTER_RELEASE |
411 	    CM_DISP_GEN_DI0_COUNTER_RELEASE);
412 	IPUV3_WRITE(sc, cm, IPU_CM_DISP_GEN, reg);
413 
414 	IPUV3_WRITE(sc, di0, IPU_DI_BS_CLKGEN0, div);
415 	IPUV3_WRITE(sc, di0, IPU_DI_BS_CLKGEN1,
416 	    __SHIFTIN(div / 16, DI_BS_CLKGEN1_DOWN));
417 #ifdef IPUV3_DEBUG
418 	printf("%s: IPU_DI_BS_CLKGEN0 = 0x%08X\n", __func__,
419 	    IPUV3_READ(sc, di0, IPU_DI_BS_CLKGEN0));
420 	printf("%s: IPU_DI_BS_CLKGEN1 = 0x%08X\n", __func__,
421 	    IPUV3_READ(sc, di0, IPU_DI_BS_CLKGEN1));
422 #endif
423 	/* Display Time settings */
424 	reg = __SHIFTIN(div / 16 - 1, DI_DW_GEN_ACCESS_SIZE) |
425 	    __SHIFTIN(div / 16 - 1, DI_DW_GEN_COMPONNENT_SIZE) |
426 	    __SHIFTIN(3, DI_DW_GEN_PIN(15));
427 	IPUV3_WRITE(sc, di0, IPU_DI_DW_GEN(0), reg);
428 #ifdef IPUV3_DEBUG
429 	printf("%s: div = %d\n", __func__, div);
430 	printf("%s: IPU_DI_DW_GEN(0) 0x%08X = 0x%08X\n", __func__,
431 	    IPU_DI_DW_GEN(0), reg);
432 #endif
433 
434 	/* Up & Down Data Wave Set */
435 	reg = __SHIFTIN(div / 16 * 2, DI_DW_SET_DOWN);
436 	IPUV3_WRITE(sc, di0, IPU_DI_DW_SET(0, 3), reg);
437 #ifdef IPUV3_DEBUG
438 	printf("%s: IPU_DI_DW_SET(0, 3) 0x%08X = 0x%08X\n", __func__,
439 	    IPU_DI_DW_SET(0, 3), reg);
440 #endif
441 
442 	/* internal HSCYNC */
443 	imx51_ipuv3_di_sync_conf(sc, 1,
444 	    __DI_SW_GEN0(geom->panel_width + geom->hsync_width +
445 		geom->left + geom->right - 1, 1, 0, 0),
446 	    __DI_SW_GEN1(0, 1, 0, 0, 0, 0, 0),
447 	    0);
448 
449 	/* HSYNC */
450 	imx51_ipuv3_di_sync_conf(sc, 2,
451 	    __DI_SW_GEN0(geom->panel_width + geom->hsync_width +
452 		geom->left + geom->right - 1, 1, 0, 1),
453 	    __DI_SW_GEN1(1, 1, 0, geom->hsync_width * 2, 1, 0, 0),
454 	    0);
455 
456 	/* VSYNC */
457 	imx51_ipuv3_di_sync_conf(sc, 3,
458 	    __DI_SW_GEN0(geom->panel_height + geom->vsync_width +
459 		geom->upper + geom->lower - 1, 2, 0, 0),
460 	    __DI_SW_GEN1(1, 1, 0, geom->vsync_width * 2, 2, 0, 0),
461 	    0);
462 
463 	IPUV3_WRITE(sc, di0, IPU_DI_SCR_CONF,
464 	    geom->panel_height + geom->vsync_width + geom->upper +
465 	    geom->lower - 1);
466 
467 	/* Active Lines Start */
468 	imx51_ipuv3_di_sync_conf(sc, 4,
469 	    __DI_SW_GEN0(0, 3, geom->vsync_width + geom->upper, 3),
470 	    __DI_SW_GEN1(0, 0, 4, 0, 0, 0, 0),
471 	    geom->panel_height);
472 
473 	/* Active Clock Start */
474 	imx51_ipuv3_di_sync_conf(sc, 5,
475 	    __DI_SW_GEN0(0, 1, geom->hsync_width + geom->left, 1),
476 	    __DI_SW_GEN1(0, 0, 5, 0, 0, 0, 0),
477 	    geom->panel_width);
478 
479 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN0(6), 0);
480 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN1(6), 0);
481 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN0(7), 0);
482 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN1(7), 0);
483 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN0(8), 0);
484 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN1(8), 0);
485 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN0(9), 0);
486 	IPUV3_WRITE(sc, di0, IPU_DI_SW_GEN1(9), 0);
487 
488 	reg = IPUV3_READ(sc, di0, IPU_DI_STP_REP(6));
489 	reg &= ~DI_STP_REP(6);
490 	IPUV3_WRITE(sc, di0, IPU_DI_STP_REP(6), reg);
491 	IPUV3_WRITE(sc, di0, IPU_DI_STP_REP(7), 0);
492 	IPUV3_WRITE(sc, di0, IPU_DI_STP_REP(9), 0);
493 
494 	IPUV3_WRITE(sc, di0, IPU_DI_GENERAL, 0);
495 	reg = __SHIFTIN(3 - 1, DI_SYNC_AS_GEN_VSYNC_SEL) |
496 	    __SHIFTIN(0x2, DI_SYNC_AS_GEN_SYNC_START);
497 	IPUV3_WRITE(sc, di0, IPU_DI_SYNC_AS_GEN, reg);
498 	IPUV3_WRITE(sc, di0, IPU_DI_POL, DI_POL_DRDY_POLARITY_15);
499 
500 	/* release DI counter */
501 	reg = IPUV3_READ(sc, cm, IPU_CM_DISP_GEN);
502 	reg |= CM_DISP_GEN_DI0_COUNTER_RELEASE;
503 	IPUV3_WRITE(sc, cm, IPU_CM_DISP_GEN, reg);
504 
505 #ifdef IPUV3_DEBUG
506 	int i;
507 	printf("*** DI0 ***\n");
508 	for (i = 0; i <= 0x174; i += 4)
509 		printf("0x%08X = 0x%08X\n", i, IPUV3_READ(sc, di0, i));
510 
511 	printf("%s: IPU_CM_DISP_GEN    : 0x%08X\n", __func__,
512 	    IPUV3_READ(sc, cm, IPU_CM_DISP_GEN));
513 	printf("%s: IPU_DI_SYNC_AS_GEN : 0x%08X\n", __func__,
514 	    IPUV3_READ(sc, di0, IPU_DI_SYNC_AS_GEN));
515 	printf("%s: IPU_DI_GENERAL     : 0x%08X\n", __func__,
516 	    IPUV3_READ(sc, di0, IPU_DI_GENERAL));
517 	printf("%s: IPU_DI_POL         : 0x%08X\n", __func__,
518 	    IPUV3_READ(sc, di0, IPU_DI_POL));
519 #endif
520 }
521 
522 
523 void
524 imx51_ipuv3_geometry(struct imx51_ipuv3_softc *sc,
525     const struct lcd_panel_geometry *geom)
526 {
527 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
528 
529 	sc->geometry = geom;
530 
531 #ifdef IPUV3_DEBUG
532 	printf("%s: screen height = %d\n",__func__ , geom->panel_height);
533 	printf("%s:        width  = %d\n",__func__ , geom->panel_width);
534 	printf("%s: IPU Clock = %d\n", __func__,
535 	    imx51_get_clock(IMX51CLK_IPU_HSP_CLK_ROOT));
536 	printf("%s: Pixel Clock = %d\n", __func__, geom->pixel_clk);
537 #endif
538 
539 	imx51_ipuv3_di_init(sc);
540 
541 #ifdef IPUV3_DEBUG
542 	printf("%s: IPU_CM_DISP_GEN    : 0x%08X\n", __func__,
543 	    IPUV3_READ(sc, cm, IPU_CM_DISP_GEN));
544 #endif
545 
546 	imx51_ipuv3_dc_display_config(sc, geom->panel_width);
547 
548 	return;
549 }
550 
551 /*
552  * Initialize the IPUV3 controller.
553  */
554 static void
555 imx51_ipuv3_initialize(struct imx51_ipuv3_softc *sc,
556     const struct lcd_panel_geometry *geom)
557 {
558 	uint32_t reg;
559 
560 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
561 
562 	IPUV3_WRITE(sc, cm, IPU_CM_CONF, 0);
563 
564 	/* reset */
565 	IPUV3_WRITE(sc, cm, IPU_CM_MEM_RST, CM_MEM_START | CM_MEM_EN);
566 	while (IPUV3_READ(sc, cm, IPU_CM_MEM_RST) & CM_MEM_START)
567 		; /* wait */
568 
569 	imx51_ipuv3_dmfc_init(sc);
570 	imx51_ipuv3_dc_init(sc);
571 
572 	imx51_ipuv3_geometry(sc, geom);
573 
574 	/* set global alpha */
575 	IPUV3_WRITE(sc, dp, IPU_DP_GRAPH_WIND_CTRL_SYNC, 0x80 << 24);
576 	IPUV3_WRITE(sc, dp, IPU_DP_COM_CONF_SYNC, DP_DP_GWAM_SYNC);
577 
578 	reg = IPUV3_READ(sc, cm, IPU_CM_DISP_GEN);
579 	reg |= CM_DISP_GEN_MCU_MAX_BURST_STOP |
580 	    __SHIFTIN(0x8, CM_DISP_GEN_MCU_T);
581 	IPUV3_WRITE(sc, cm, IPU_CM_DISP_GEN, reg);
582 }
583 
584 static void
585 imx51_ipuv3_init_screen(void *cookie, struct vcons_screen *scr,
586 		    int existing, long *defattr)
587 {
588 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
589 
590 	struct imx51_ipuv3_softc *sc = cookie;
591 	struct rasops_info *ri = &scr->scr_ri;
592 	struct imx51_wsscreen_descr *descr = imx51_ipuv3_console.descr;
593 
594 	if ((scr == &sc->console) && (sc->vd.active != NULL))
595 		return;
596 
597 	ri->ri_bits = sc->active->buf_va;
598 
599 	scr->scr_flags |= VCONS_DONT_READ;
600 	if (existing)
601 		ri->ri_flg |= RI_CLEAR;
602 
603 	imx51_ipuv3_setup_rasops(sc, ri, descr, sc->geometry);
604 
605 	ri->ri_caps = WSSCREEN_WSCOLORS;
606 
607 	rasops_reconfig(ri,
608 	    ri->ri_height / ri->ri_font->fontheight,
609 	    ri->ri_width / ri->ri_font->fontwidth);
610 
611 	ri->ri_hw = scr;
612 }
613 
614 /*
615  * Common driver attachment code.
616  */
617 void
618 imx51_ipuv3_attach_sub(struct imx51_ipuv3_softc *sc,
619     struct axi_attach_args *axia, const struct lcd_panel_geometry *geom)
620 {
621 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
622 
623 	bus_space_tag_t iot = axia->aa_iot;
624 	bus_space_handle_t ioh;
625 	int error;
626 
627 	aprint_normal(": i.MX51 IPUV3 controller\n");
628 
629 	sc->n_screens = 0;
630 	LIST_INIT(&sc->screens);
631 
632 	sc->iot = iot;
633 	sc->dma_tag = &imx_bus_dma_tag;
634 
635 	/* map controller registers */
636 	error = bus_space_map(iot, IPU_CM_BASE, IPU_CM_SIZE, 0, &ioh);
637 	if (error)
638 		goto fail_retarn_cm;
639 	sc->cm_ioh = ioh;
640 
641 	/* map Display Multi FIFO Controller registers */
642 	error = bus_space_map(iot, IPU_DMFC_BASE, IPU_DMFC_SIZE, 0, &ioh);
643 	if (error)
644 		goto fail_retarn_dmfc;
645 	sc->dmfc_ioh = ioh;
646 
647 	/* map Display Interface registers */
648 	error = bus_space_map(iot, IPU_DI0_BASE, IPU_DI0_SIZE, 0, &ioh);
649 	if (error)
650 		goto fail_retarn_di0;
651 	sc->di0_ioh = ioh;
652 
653 	/* map Display Processor registers */
654 	error = bus_space_map(iot, IPU_DP_BASE, IPU_DP_SIZE, 0, &ioh);
655 	if (error)
656 		goto fail_retarn_dp;
657 	sc->dp_ioh = ioh;
658 
659 	/* map Display Controller registers */
660 	error = bus_space_map(iot, IPU_DC_BASE, IPU_DC_SIZE, 0, &ioh);
661 	if (error)
662 		goto fail_retarn_dc;
663 	sc->dc_ioh = ioh;
664 
665 	/* map Image DMA Controller registers */
666 	error = bus_space_map(iot, IPU_IDMAC_BASE, IPU_IDMAC_SIZE, 0, &ioh);
667 	if (error)
668 		goto fail_retarn_idmac;
669 	sc->idmac_ioh = ioh;
670 
671 	/* map CPMEM registers */
672 	error = bus_space_map(iot, IPU_CPMEM_BASE, IPU_CPMEM_SIZE, 0, &ioh);
673 	if (error)
674 		goto fail_retarn_cpmem;
675 	sc->cpmem_ioh = ioh;
676 
677 	/* map DCTEMPL registers */
678 	error = bus_space_map(iot, IPU_DCTMPL_BASE, IPU_DCTMPL_SIZE, 0, &ioh);
679 	if (error)
680 		goto fail_retarn_dctmpl;
681 	sc->dctmpl_ioh = ioh;
682 
683 #ifdef notyet
684 	sc->ih = imx51_ipuv3_intr_establish(IMX51_INT_IPUV3, IPL_BIO,
685 	    ipuv3intr, sc);
686 	if (sc->ih == NULL) {
687 		aprint_error_dev(sc->dev,
688 		    "unable to establish interrupt at irq %d\n",
689 		    IMX51_INT_IPUV3);
690 		return;
691 	}
692 #endif
693 
694 	imx51_ipuv3_initialize(sc, geom);
695 
696 #if NWSDISPLAY > 0
697 	struct imx51_wsscreen_descr *descr = imx51_ipuv3_console.descr;
698 	struct imx51_ipuv3_screen *scr;
699 
700 	sc->mode = WSDISPLAYIO_MODE_EMUL;
701 	error = imx51_ipuv3_new_screen(sc, descr->depth, &scr);
702 	if (error) {
703 		aprint_error_dev(sc->dev,
704 		    "unable to create new screen (errno=%d)", error);
705 		return;
706 	}
707 	sc->active = scr;
708 
709 	vcons_init(&sc->vd, sc, &descr->c,
710 	    imx51_ipuv3_console.accessops);
711 	sc->vd.init_screen = imx51_ipuv3_init_screen;
712 
713 #ifdef IPUV3_DEBUG
714 	printf("%s: IPUV3 console ? %d\n", __func__, imx51_ipuv3_console.is_console);
715 #endif
716 
717 	struct rasops_info *ri;
718 	long defattr;
719 	ri = &sc->console.scr_ri;
720 
721 	vcons_init_screen(&sc->vd, &sc->console, 1,
722 	    &defattr);
723 	sc->console.scr_flags |= VCONS_SCREEN_IS_STATIC;
724 
725 	descr->c.nrows = ri->ri_rows;
726 	descr->c.ncols = ri->ri_cols;
727 	descr->c.textops = &ri->ri_ops;
728 	descr->c.capabilities = ri->ri_caps;
729 
730 	if (imx51_ipuv3_console.is_console) {
731 		wsdisplay_cnattach(&descr->c, ri, 0, 0, defattr);
732 		aprint_normal_dev(sc->dev, "console\n");
733 	}
734 
735 	vcons_replay_msgbuf(&sc->console);
736 
737 	imx51_ipuv3_start_dma(sc, scr);
738 #endif
739 
740 	if (!pmf_device_register(sc->dev, imx51_ipuv3_suspend,
741 		imx51_ipuv3_resume)) {
742 		aprint_error_dev(sc->dev, "can't establish power hook\n");
743 	}
744 
745 	return;
746 
747 fail_retarn_dctmpl:
748 	bus_space_unmap(sc->iot, sc->cpmem_ioh, IPU_CPMEM_SIZE);
749 fail_retarn_cpmem:
750 	bus_space_unmap(sc->iot, sc->idmac_ioh, IPU_IDMAC_SIZE);
751 fail_retarn_idmac:
752 	bus_space_unmap(sc->iot, sc->dc_ioh, IPU_DC_SIZE);
753 fail_retarn_dp:
754 	bus_space_unmap(sc->iot, sc->dp_ioh, IPU_DP_SIZE);
755 fail_retarn_dc:
756 	bus_space_unmap(sc->iot, sc->di0_ioh, IPU_DI0_SIZE);
757 fail_retarn_di0:
758 	bus_space_unmap(sc->iot, sc->dmfc_ioh, IPU_DMFC_SIZE);
759 fail_retarn_dmfc:
760 	bus_space_unmap(sc->iot, sc->dc_ioh, IPU_CM_SIZE);
761 fail_retarn_cm:
762 	aprint_error_dev(sc->dev,
763 	    "failed to map registers (errno=%d)\n", error);
764 	return;
765 }
766 
767 int
768 imx51_ipuv3_cnattach(bool isconsole, struct imx51_wsscreen_descr *descr,
769     struct wsdisplay_accessops *accessops,
770     const struct lcd_panel_geometry *geom)
771 {
772 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
773 	imx51_ipuv3_console.descr = descr;
774 	imx51_ipuv3_console.geom = geom;
775 	imx51_ipuv3_console.accessops = accessops;
776 	imx51_ipuv3_console.is_console = isconsole;
777 	return 0;
778 }
779 
780 #ifdef notyet
781 /*
782  * Interrupt handler.
783  */
784 int
785 ipuv3intr(void *arg)
786 {
787 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
788 
789 	struct imx51_ipuv3_softc *sc = (struct imx51_ipuv3_softc *)arg;
790 	bus_space_tag_t iot = sc->iot;
791 	bus_space_handle_t ioh = sc->dc_ioh;
792 	uint32_t status;
793 
794 	status = IPUV3_READ(ioh, V3CR);
795 	/* Clear stickey status bits */
796 	IPUV3_WRITE(ioh, V3CR, status);
797 
798 	return 1;
799 }
800 #endif
801 
802 static void
803 imx51_ipuv3_write_dmaparam(struct imx51_ipuv3_softc *sc,
804     int ch, uint32_t *value, int size)
805 {
806 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
807 
808 	int i;
809 	uint32_t addr = ch * 0x40;
810 
811 	for (i = 0; i < size; i++) {
812 		IPUV3_WRITE(sc, cpmem, addr + ((i % 5) * 0x4) +
813 		    ((i / 5) * 0x20), value[i]);
814 #ifdef IPUV3_DEBUG
815 		printf("%s: addr = 0x%08X, val = 0x%08X\n", __func__,
816 		    addr + ((i % 5) * 0x4) + ((i / 5) * 0x20),
817 		    IPUV3_READ(sc, cpmem, addr + ((i % 5) * 0x4) +
818 			((i / 5) * 0x20)));
819 #endif
820 	}
821 }
822 
823 static void
824 imx51_ipuv3_build_param(struct imx51_ipuv3_softc *sc,
825     struct imx51_ipuv3_screen *scr,
826     uint32_t *params)
827 {
828 	const struct lcd_panel_geometry *geom = sc->geometry;
829 
830 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
831 
832 	imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_FW,
833 	    (geom->panel_width - 1));
834 	imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_FH,
835 	    (geom->panel_height - 1));
836 	imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_EBA0,
837 	    scr->segs[0].ds_addr >> 3);
838 	imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_EBA1,
839 	    scr->segs[0].ds_addr >> 3);
840 	imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_SL,
841 	    (scr->stride - 1));
842 
843 	imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_PFS, 0x7);
844 	imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_NPB, 32 - 1);
845 
846 	switch (scr->depth) {
847 	case 32:
848 		/* ARBG888 */
849 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_BPP, 0x0);
850 
851 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS0, 0);
852 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS1, 8);
853 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS2, 16);
854 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS3, 24);
855 
856 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID0, 8 - 1);
857 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID1, 8 - 1);
858 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID2, 8 - 1);
859 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID3, 8 - 1);
860 		break;
861 	case 24:
862 		/* RBG888 */
863 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_BPP, 0x1);
864 
865 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS0, 0);
866 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS1, 8);
867 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS2, 16);
868 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS3, 24);
869 
870 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID0, 8 - 1);
871 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID1, 8 - 1);
872 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID2, 8 - 1);
873 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID3, 0);
874 		break;
875 	case 16:
876 		/* RBG565 */
877 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_BPP, 0x3);
878 
879 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS0, 0);
880 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS1, 5);
881 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS2, 11);
882 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_OFS3, 16);
883 
884 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID0, 5 - 1);
885 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID1, 6 - 1);
886 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID2, 5 - 1);
887 		imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_WID3, 0);
888 		break;
889 	default:
890 		panic("%s: unsupported depth %d\n", __func__, scr->depth);
891 		break;
892 	}
893 
894 	imx51_ipuv3_set_idma_param(params, IDMAC_Ch_PARAM_ID, 1);
895 }
896 
897 static void
898 imx51_ipuv3_set_idma_param(uint32_t *params, uint32_t name, uint32_t val)
899 {
900 	int word = (name >> 16) & 0xff;
901 	int shift = (name >> 8) & 0xff;
902 	int width = name & 0xff;
903 	int index;
904 
905 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
906 
907 	index = word * 5;
908 	index += shift / 32;
909 	shift = shift % 32;
910 
911 	params[index] |= val << shift;
912 	shift = 32 - shift;
913 
914 	if (width > shift)
915 		params[index+1] |= val >> shift;
916 }
917 
918 /*
919  * Enable DMA to cause the display to be refreshed periodically.
920  * This brings the screen to life...
921  */
922 void
923 imx51_ipuv3_start_dma(struct imx51_ipuv3_softc *sc,
924     struct imx51_ipuv3_screen *scr)
925 {
926 	int save;
927 	uint32_t params[10];
928 	uint32_t reg;
929 
930 	DPRINTFN(3, ("%s: Pixel depth = %d\n", __func__, scr->depth));
931 
932 	reg = IPUV3_READ(sc, idmac, IPU_IDMAC_CH_EN_1);
933 	reg &= ~__BIT(CH_PANNEL_BG);
934 	IPUV3_WRITE(sc, idmac, IPU_IDMAC_CH_EN_1, reg);
935 
936 	memset(params, 0, sizeof(params));
937 	imx51_ipuv3_build_param(sc, scr, params);
938 
939 	save = disable_interrupts(I32_bit);
940 
941 	/* IDMAC configuration */
942 	imx51_ipuv3_write_dmaparam(sc, CH_PANNEL_BG, params,
943 	    sizeof(params) / sizeof(params[0]));
944 
945 	IPUV3_WRITE(sc, idmac, IPU_IDMAC_CH_PRI_1, __BIT(CH_PANNEL_BG));
946 
947 	/* double buffer */
948 	reg = IPUV3_READ(sc, cm, IPU_CM_CH_DB_MODE_SEL_0);
949 	reg |= __BIT(CH_PANNEL_BG);
950 	IPUV3_WRITE(sc, cm, IPU_CM_CH_DB_MODE_SEL_0, reg);
951 
952 	reg = IPUV3_READ(sc, idmac, IPU_IDMAC_CH_EN_1);
953 	reg |= __BIT(CH_PANNEL_BG);
954 	IPUV3_WRITE(sc, idmac, IPU_IDMAC_CH_EN_1, reg);
955 
956 	reg = IPUV3_READ(sc, cm, IPU_CM_GPR);
957 	reg &= ~CM_GPR_IPU_CH_BUF0_RDY0_CLR;
958 	IPUV3_WRITE(sc, cm, IPU_CM_GPR, reg);
959 
960 	IPUV3_WRITE(sc, cm, IPU_CM_CUR_BUF_0, __BIT(CH_PANNEL_BG));
961 
962 	restore_interrupts(save);
963 
964 	imx51_ipuv3_enable_display(sc);
965 
966 #ifdef IPUV3_DEBUG
967 	imx51_ipuv3_dump(sc);
968 #endif
969 }
970 
971 /*
972  * Disable screen refresh.
973  */
974 static void
975 imx51_ipuv3_stop_dma(struct imx51_ipuv3_softc *sc)
976 {
977 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
978 
979 	return;
980 }
981 
982 /*
983  * Create and initialize a new screen buffer.
984  */
985 int
986 imx51_ipuv3_new_screen(struct imx51_ipuv3_softc *sc, int depth,
987     struct imx51_ipuv3_screen **scrpp)
988 {
989 	const struct lcd_panel_geometry *geometry;
990 	struct imx51_ipuv3_screen *scr = NULL;
991 	int width, height;
992 	bus_size_t size;
993 	int error;
994 	int busdma_flag = (cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
995 
996 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
997 
998 	geometry = sc->geometry;
999 
1000 	width = geometry->panel_width;
1001 	height = geometry->panel_height;
1002 
1003 	scr = malloc(sizeof(*scr), M_DEVBUF, M_NOWAIT);
1004 	if (scr == NULL)
1005 		return ENOMEM;
1006 
1007 	memset(scr, 0, sizeof(*scr));
1008 
1009 	scr->nsegs = 0;
1010 	scr->depth = depth;
1011 	scr->stride = width * depth / 8;
1012 	scr->buf_size = size = scr->stride * height;
1013 	scr->buf_va = NULL;
1014 
1015 	error = bus_dmamem_alloc(sc->dma_tag, size, 16, 0, scr->segs, 1,
1016 	    &scr->nsegs, busdma_flag);
1017 
1018 	if (error || scr->nsegs != 1) {
1019 		/* XXX:
1020 		 * Actually we can handle nsegs>1 case by means
1021 		 * of multiple DMA descriptors for a panel.  It
1022 		 * will make code here a bit hairly.
1023 		 */
1024 		if (error == 0)
1025 			error = E2BIG;
1026 		goto bad;
1027 	}
1028 
1029 	error = bus_dmamem_map(sc->dma_tag, scr->segs, scr->nsegs, size,
1030 	    (void **)&scr->buf_va, busdma_flag | BUS_DMA_COHERENT);
1031 	if (error)
1032 		goto bad;
1033 
1034 	memset(scr->buf_va, 0, scr->buf_size);
1035 
1036 	/* map memory for DMA */
1037 	error = bus_dmamap_create(sc->dma_tag, 1024*1024*2, 1, 1024*1024*2, 0,
1038 	    busdma_flag, &scr->dma);
1039 	if (error)
1040 		goto bad;
1041 
1042 	error = bus_dmamap_load(sc->dma_tag, scr->dma, scr->buf_va, size,
1043 	    NULL, busdma_flag);
1044 	if (error)
1045 		goto bad;
1046 
1047 	LIST_INSERT_HEAD(&sc->screens, scr, link);
1048 	sc->n_screens++;
1049 
1050 #ifdef IPUV3_DEBUG
1051 	printf("%s: screen buffer width  %d\n", __func__, width);
1052 	printf("%s: screen buffer height %d\n", __func__, height);
1053 	printf("%s: screen buffer depth  %d\n", __func__, depth);
1054 	printf("%s: screen buffer stride %d\n", __func__, scr->stride);
1055 	printf("%s: screen buffer size   0x%08X\n", __func__,
1056 	    (uint32_t)scr->buf_size);
1057 	printf("%s: screen buffer addr virtual  %p\n", __func__, scr->buf_va);
1058 	printf("%s: screen buffer addr physical %p\n", __func__,
1059 	    (void *)scr->segs[0].ds_addr);
1060 #endif
1061 
1062 	scr->map_size = size;		/* used when unmap this. */
1063 
1064 	*scrpp = scr;
1065 
1066 	return 0;
1067 
1068 bad:
1069 #ifdef IPUV3_DEBUG
1070 	printf("%s: error = 0x%08X\n", __func__, error);
1071 #endif
1072 	if (scr) {
1073 		if (scr->buf_va)
1074 			bus_dmamem_unmap(sc->dma_tag, scr->buf_va, size);
1075 		if (scr->nsegs)
1076 			bus_dmamem_free(sc->dma_tag, scr->segs, scr->nsegs);
1077 		free(scr, M_DEVBUF);
1078 	}
1079 	*scrpp = NULL;
1080 	return error;
1081 }
1082 
1083 #if NWSDISPLAY > 0
1084 /*
1085  * Initialize rasops for a screen, as well as struct wsscreen_descr if this
1086  * is the first screen creation.
1087  */
1088 static void
1089 imx51_ipuv3_setup_rasops(struct imx51_ipuv3_softc *sc, struct rasops_info *rinfo,
1090     struct imx51_wsscreen_descr *descr,
1091     const struct lcd_panel_geometry *geom)
1092 {
1093 
1094 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
1095 
1096 	rinfo->ri_flg = descr->flags;
1097 	rinfo->ri_depth = descr->depth;
1098 	rinfo->ri_width = geom->panel_width;
1099 	rinfo->ri_height = geom->panel_height;
1100 	rinfo->ri_stride = rinfo->ri_width * rinfo->ri_depth / 8;
1101 
1102 	/* swap B and R */
1103 	if (descr->depth == 16) {
1104 		rinfo->ri_rnum = 5;
1105 		rinfo->ri_rpos = 11;
1106 		rinfo->ri_gnum = 6;
1107 		rinfo->ri_gpos = 5;
1108 		rinfo->ri_bnum = 5;
1109 		rinfo->ri_bpos = 0;
1110 	}
1111 
1112 	if (descr->c.nrows == 0) {
1113 		/* get rasops to compute screen size the first time */
1114 		rasops_init(rinfo, 100, 100);
1115 	} else {
1116 		rasops_init(rinfo, descr->c.nrows, descr->c.ncols);
1117 	}
1118 
1119 	descr->c.nrows = rinfo->ri_rows;
1120 	descr->c.ncols = rinfo->ri_cols;
1121 	descr->c.capabilities = rinfo->ri_caps;
1122 	descr->c.textops = &rinfo->ri_ops;
1123 }
1124 #endif
1125 /*
1126  * Power management
1127  */
1128 static bool
1129 imx51_ipuv3_suspend(device_t dv, const pmf_qual_t *qual)
1130 {
1131 	struct imx51_ipuv3_softc *sc = device_private(dv);
1132 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
1133 	if (sc->active)
1134 		imx51_ipuv3_stop_dma(sc);
1135 	return true;
1136 }
1137 
1138 static bool
1139 imx51_ipuv3_resume(device_t dv, const pmf_qual_t *qual)
1140 {
1141 	struct imx51_ipuv3_softc *sc = device_private(dv);
1142 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
1143 	if (sc->active) {
1144 		imx51_ipuv3_initialize(sc, sc->geometry);
1145 		imx51_ipuv3_start_dma(sc, sc->active);
1146 	}
1147 	return true;
1148 }
1149 
1150 #if NWSDISPLAY > 0
1151 int
1152 imx51_ipuv3_show_screen(void *v, void *cookie, int waitok,
1153     void (*cb)(void *, int, int), void *cbarg)
1154 {
1155 	struct vcons_data *vd = v;
1156 	struct imx51_ipuv3_softc *sc = vd->cookie;
1157 	struct imx51_ipuv3_screen *scr = cookie, *old;
1158 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
1159 
1160 	old = sc->active;
1161 	if (old == scr)
1162 		return 0;
1163 	if (old)
1164 		imx51_ipuv3_stop_dma(sc);
1165 	imx51_ipuv3_start_dma(sc, scr);
1166 	sc->active = scr;
1167 	return 0;
1168 }
1169 
1170 int
1171 imx51_ipuv3_alloc_screen(void *v, const struct wsscreen_descr *_type,
1172     void **cookiep, int *curxp, int *curyp, long *attrp)
1173 {
1174 	struct vcons_data *vd = v;
1175 	struct imx51_ipuv3_softc *sc = vd->cookie;
1176 	struct imx51_ipuv3_screen *scr;
1177 	const struct imx51_wsscreen_descr *type =
1178 		(const struct imx51_wsscreen_descr *)_type;
1179 	int error;
1180 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
1181 
1182 	error = imx51_ipuv3_new_screen(sc, type->depth, &scr);
1183 	if (error)
1184 		return error;
1185 
1186 	/*
1187 	 * initialize raster operation for this screen.
1188 	 */
1189 	scr->rinfo.ri_flg = 0;
1190 	scr->rinfo.ri_depth = type->depth;
1191 	scr->rinfo.ri_bits = scr->buf_va;
1192 	scr->rinfo.ri_width = sc->geometry->panel_width;
1193 	scr->rinfo.ri_height = sc->geometry->panel_height;
1194 	scr->rinfo.ri_stride = scr->rinfo.ri_width * scr->rinfo.ri_depth / 8;
1195 #ifdef CPU_XSCALE_PXA270
1196 	if (scr->rinfo.ri_depth > 16)
1197 		scr->rinfo.ri_stride = scr->rinfo.ri_width * 4;
1198 #endif
1199 	scr->rinfo.ri_wsfcookie = -1;	/* XXX */
1200 
1201 	rasops_init(&scr->rinfo, type->c.nrows, type->c.ncols);
1202 
1203 	(*scr->rinfo.ri_ops.allocattr)(&scr->rinfo, 0, 0, 0, attrp);
1204 
1205 	*cookiep = scr;
1206 	*curxp = 0;
1207 	*curyp = 0;
1208 
1209 	return 0;
1210 }
1211 
1212 void
1213 imx51_ipuv3_free_screen(void *v, void *cookie)
1214 {
1215 	struct vcons_data *vd = v;
1216 	struct imx51_ipuv3_softc *sc = vd->cookie;
1217 	struct imx51_ipuv3_screen *scr = cookie;
1218 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
1219 
1220 	LIST_REMOVE(scr, link);
1221 	sc->n_screens--;
1222 	if (scr == sc->active) {
1223 		/* at first, we need to stop LCD DMA */
1224 		sc->active = NULL;
1225 
1226 		printf("lcd_free on active screen\n");
1227 
1228 		imx51_ipuv3_stop_dma(sc);
1229 	}
1230 
1231 	if (scr->buf_va)
1232 		bus_dmamem_unmap(sc->dma_tag, scr->buf_va, scr->map_size);
1233 	if (scr->nsegs > 0)
1234 		bus_dmamem_free(sc->dma_tag, scr->segs, scr->nsegs);
1235 	free(scr, M_DEVBUF);
1236 }
1237 
1238 int
1239 imx51_ipuv3_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
1240     struct lwp *l)
1241 {
1242 	struct vcons_data *vd = v;
1243 	struct imx51_ipuv3_softc *sc = vd->cookie;
1244 	struct wsdisplay_fbinfo *wsdisp_info;
1245 	struct vcons_screen *ms = vd->active;
1246 
1247 	DPRINTFN(5, ("%s : cmd 0x%X (%d)\n", __func__, (u_int)cmd, (int)IOCGROUP(cmd)));
1248 
1249 	switch (cmd) {
1250 	case WSDISPLAYIO_GTYPE:
1251 		*(int *)data = WSDISPLAY_TYPE_IMXIPU;
1252 		return 0;
1253 
1254 	case WSDISPLAYIO_GINFO:
1255 		wsdisp_info = (struct wsdisplay_fbinfo *)data;
1256 		wsdisp_info->height = ms->scr_ri.ri_height;
1257 		wsdisp_info->width = ms->scr_ri.ri_width;
1258 		wsdisp_info->depth = ms->scr_ri.ri_depth;
1259 		wsdisp_info->cmsize = 0;
1260 		return 0;
1261 
1262 	case WSDISPLAYIO_LINEBYTES:
1263 		*(u_int *)data = ms->scr_ri.ri_stride;
1264 		return 0;
1265 
1266 	case WSDISPLAYIO_GETCMAP:
1267 	case WSDISPLAYIO_PUTCMAP:
1268 		return EPASSTHROUGH;	/* XXX Colormap */
1269 
1270 	case WSDISPLAYIO_SVIDEO:
1271 		if (*(int *)data == WSDISPLAYIO_VIDEO_ON) {
1272 			/* turn it on */
1273 		}
1274 		else {
1275 			/* start IPUV3 shutdown */
1276 			/* sleep until interrupt */
1277 		}
1278 		return 0;
1279 
1280 	case WSDISPLAYIO_GVIDEO:
1281 		*(u_int *)data =  WSDISPLAYIO_VIDEO_ON;
1282 		return 0;
1283 
1284 	case WSDISPLAYIO_GCURPOS:
1285 	case WSDISPLAYIO_SCURPOS:
1286 	case WSDISPLAYIO_GCURMAX:
1287 	case WSDISPLAYIO_GCURSOR:
1288 	case WSDISPLAYIO_SCURSOR:
1289 		return EPASSTHROUGH;	/* XXX */
1290 	case WSDISPLAYIO_SMODE:
1291 		{
1292 			int new_mode = *(int*)data;
1293 
1294 			/* notify the bus backend */
1295 			if (new_mode != sc->mode) {
1296 				sc->mode = new_mode;
1297 				if(new_mode == WSDISPLAYIO_MODE_EMUL)
1298 					vcons_redraw_screen(ms);
1299 			}
1300 		}
1301 		return 0;
1302 	}
1303 
1304 	return EPASSTHROUGH;
1305 }
1306 
1307 paddr_t
1308 imx51_ipuv3_mmap(void *v, void *vs, off_t offset, int prot)
1309 {
1310 	DPRINTFN(5, ("%s : %d\n", __func__, __LINE__));
1311 
1312 	struct vcons_data *vd = v;
1313 	struct imx51_ipuv3_softc *sc = vd->cookie;
1314 	struct imx51_ipuv3_screen *scr = sc->active;
1315 
1316 	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
1317 	    offset, prot,
1318 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
1319 }
1320 #endif /* NWSDISPLAY > 0 */
1321 
1322