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