Home | History | Annotate | Line # | Download | only in xscale
pxa2x0_lcd.c revision 1.1
      1  1.1  bsh /* $Id: pxa2x0_lcd.c,v 1.1 2002/10/19 19:31:40 bsh Exp $ */
      2  1.1  bsh 
      3  1.1  bsh /*
      4  1.1  bsh  * Copyright (c) 2002  Genetec Corporation.  All rights reserved.
      5  1.1  bsh  * Written by Hiroyuki Bessho 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  * 3. All advertising materials mentioning features or use of this software
     16  1.1  bsh  *    must display the following acknowledgement:
     17  1.1  bsh  *	This product includes software developed for the NetBSD Project by
     18  1.1  bsh  *	Genetec Corporation.
     19  1.1  bsh  * 4. The name of Genetec Corporation may not be used to endorse or
     20  1.1  bsh  *    promote products derived from this software without specific prior
     21  1.1  bsh  *    written permission.
     22  1.1  bsh  *
     23  1.1  bsh  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     24  1.1  bsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1  bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1  bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     27  1.1  bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1  bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1  bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1  bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  bsh  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  bsh  */
     35  1.1  bsh 
     36  1.1  bsh /*
     37  1.1  bsh  * Support PXA2[15]0's integrated LCD controller.
     38  1.1  bsh  */
     39  1.1  bsh #include <sys/param.h>
     40  1.1  bsh #include <sys/systm.h>
     41  1.1  bsh #include <sys/conf.h>
     42  1.1  bsh #include <sys/uio.h>
     43  1.1  bsh #include <sys/malloc.h>
     44  1.1  bsh #include <sys/kernel.h>			/* for cold */
     45  1.1  bsh 
     46  1.1  bsh #include <dev/cons.h>
     47  1.1  bsh #include <dev/wscons/wsconsio.h>
     48  1.1  bsh #include <dev/wscons/wsdisplayvar.h>
     49  1.1  bsh #include <dev/wscons/wscons_callbacks.h>
     50  1.1  bsh #include <dev/rasops/rasops.h>
     51  1.1  bsh #include <dev/wsfont/wsfont.h>
     52  1.1  bsh 
     53  1.1  bsh #include <machine/bus.h>
     54  1.1  bsh #include <machine/cpu.h>
     55  1.1  bsh #include <arm/cpufunc.h>
     56  1.1  bsh 
     57  1.1  bsh #include <arm/xscale/pxa2x0var.h>
     58  1.1  bsh #include <arm/xscale/pxa2x0reg.h>
     59  1.1  bsh #include <arm/xscale/pxa2x0_lcd.h>
     60  1.1  bsh 
     61  1.1  bsh #include "wsdisplay.h"
     62  1.1  bsh 
     63  1.1  bsh int lcdintr(void *);
     64  1.1  bsh 
     65  1.1  bsh void
     66  1.1  bsh pxa2x0_lcd_geometry( struct pxa2x0_lcd_softc *sc,
     67  1.1  bsh     const struct lcd_panel_geometry *info )
     68  1.1  bsh {
     69  1.1  bsh 	int lines;
     70  1.1  bsh 	bus_space_tag_t iot = sc->iot;
     71  1.1  bsh 	bus_space_handle_t ioh = sc->ioh;
     72  1.1  bsh 	uint32_t ccr0;
     73  1.1  bsh 
     74  1.1  bsh 	sc->geometry = info;
     75  1.1  bsh 
     76  1.1  bsh 	ccr0 = LCCR0_IMASK;
     77  1.1  bsh 	if( info->panel_info & LCDPANEL_ACTIVE )
     78  1.1  bsh 		ccr0 |= LCCR0_PAS;	/* passive mode */
     79  1.1  bsh 	if( (info->panel_info & (LCDPANEL_DUAL|LCDPANEL_ACTIVE))
     80  1.1  bsh 	    == LCDPANEL_DUAL )
     81  1.1  bsh 		ccr0 |= LCCR0_SDS; /* dual panel */
     82  1.1  bsh 	if( info->panel_info & LCDPANEL_MONOCHROME )
     83  1.1  bsh 		ccr0 |= LCCR0_CMS;
     84  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCCR0, ccr0 );
     85  1.1  bsh 
     86  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCCR1,
     87  1.1  bsh 	    (info->panel_width-1)
     88  1.1  bsh 	    | ((info->hsync_pulse_width-1)<<10)
     89  1.1  bsh 	    | ((info->end_line_wait-1)<<16)
     90  1.1  bsh 	    | ((info->beg_line_wait-1)<<24) );
     91  1.1  bsh 
     92  1.1  bsh 	if( info->panel_info & LCDPANEL_DUAL )
     93  1.1  bsh 		lines = info->panel_height/2 + info->extra_lines;
     94  1.1  bsh 	else
     95  1.1  bsh 		lines = info->panel_height + info->extra_lines;
     96  1.1  bsh 
     97  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCCR2,
     98  1.1  bsh 	    (lines-1)
     99  1.1  bsh 	    | (info->vsync_pulse_width<<10)
    100  1.1  bsh 	    | (info->end_frame_wait<<16)
    101  1.1  bsh 	    | (info->beg_frame_wait<<24) );
    102  1.1  bsh 
    103  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCCR3,
    104  1.1  bsh 	    (info->pixel_clock_div<<0)
    105  1.1  bsh 	    | (info->ac_bias << 8)
    106  1.1  bsh 	    | ((info->panel_info &
    107  1.1  bsh 		(LCDPANEL_VSP|LCDPANEL_HSP|LCDPANEL_PCP|LCDPANEL_OEP))
    108  1.1  bsh 		<<20)
    109  1.1  bsh 	    | ( 4 << 24 ) /* 16bpp */
    110  1.1  bsh 	    | ((info->panel_info & LCDPANEL_DPC) ? (1<<27) : 0)
    111  1.1  bsh 	    );
    112  1.1  bsh }
    113  1.1  bsh 
    114  1.1  bsh void
    115  1.1  bsh pxa2x0_lcd_attach_sub( struct pxa2x0_softc *parent,
    116  1.1  bsh     struct pxa2x0_lcd_softc *sc,
    117  1.1  bsh     const struct lcd_panel_geometry *geom )
    118  1.1  bsh {
    119  1.1  bsh 	bus_space_tag_t iot;
    120  1.1  bsh 	bus_space_handle_t ioh;
    121  1.1  bsh 	uint32_t tmp;
    122  1.1  bsh 	int error;
    123  1.1  bsh 
    124  1.1  bsh 
    125  1.1  bsh 	iot = parent->saip.sc_iot;
    126  1.1  bsh 
    127  1.1  bsh 	sc->n_screens = 0;
    128  1.1  bsh 	LIST_INIT(&sc->screens);
    129  1.1  bsh 
    130  1.1  bsh 	/* map controller registers */
    131  1.1  bsh 	error = bus_space_map( iot, PXA2X0_LCDC_BASE,
    132  1.1  bsh 			       PXA2X0_LCDC_SIZE, 0, &ioh );
    133  1.1  bsh 	if( error ){
    134  1.1  bsh 		printf( ": failed to map registers %d", error );
    135  1.1  bsh 		return;
    136  1.1  bsh 	}
    137  1.1  bsh 
    138  1.1  bsh 	sc->iot = iot;
    139  1.1  bsh 	sc->ioh = ioh;
    140  1.1  bsh 	sc->dma_tag = &pxa2x0_bus_dma_tag;
    141  1.1  bsh 
    142  1.1  bsh 	sc->ih = pxa2x0_intr_establish(17, IPL_BIO, lcdintr, sc);
    143  1.1  bsh 	if (sc->ih == NULL)
    144  1.1  bsh 		printf("%s: unable to establish interrupt at irq %d",
    145  1.1  bsh 		    sc->dev.dv_xname, 17);
    146  1.1  bsh 
    147  1.1  bsh 	/* Initialize LCD controller */
    148  1.1  bsh 
    149  1.1  bsh 	/* enable clock */
    150  1.1  bsh 	tmp = bus_space_read_4( iot, parent->sc_clkman_ioh, CLKMAN_CKEN );
    151  1.1  bsh 	bus_space_write_4( iot, parent->sc_clkman_ioh, CLKMAN_CKEN,
    152  1.1  bsh 			   tmp | (1U<<16) );
    153  1.1  bsh 
    154  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCCR0, LCCR0_IMASK );
    155  1.1  bsh 
    156  1.1  bsh 	/*
    157  1.1  bsh 	 * setup GP[77:58] for LCD
    158  1.1  bsh 	 */
    159  1.1  bsh 	{
    160  1.1  bsh 		bus_space_handle_t gpioh = parent->saip.sc_gpioh;
    161  1.1  bsh 		uint32_t  gafr1, gafr2, gpdr1, gpdr2;
    162  1.1  bsh 		int panelinfo = geom->panel_info;
    163  1.1  bsh 
    164  1.1  bsh 		gpdr1 = bus_space_read_4( iot, gpioh, GPIO_GPDR1 );
    165  1.1  bsh 		gpdr2 = bus_space_read_4( iot, gpioh, GPIO_GPDR2 );
    166  1.1  bsh 		gafr1 = bus_space_read_4( iot, gpioh, GPIO_GAFR1_U );
    167  1.1  bsh 		gafr2 = bus_space_read_4( iot, gpioh, GPIO_GAFR2_L );
    168  1.1  bsh 
    169  1.1  bsh 		/* Always use L_DD[3:0], [FLP]CLK, ACBIAS */
    170  1.1  bsh 		gafr1 = (gafr1 & ~(0xff<<20)) | (0xaa<<20);
    171  1.1  bsh 		gafr2 = (gafr2 & ~(0xff<<20)) | (0xaa<<20);
    172  1.1  bsh 		gpdr1 |= 0x0f<<26;
    173  1.1  bsh 		gpdr2 |= 0x0f<<10;
    174  1.1  bsh 
    175  1.1  bsh 		if( (panelinfo & LCDPANEL_ACTIVE) ||
    176  1.1  bsh 		    ((panelinfo & (LCDPANEL_MONOCHROME|LCDPANEL_DUAL))
    177  1.1  bsh 			== LCDPANEL_DUAL ) ){
    178  1.1  bsh 			/* active and color dual panel need L_DD[15:0] */
    179  1.1  bsh 			gafr1 = (gafr1 & ~(0x0f<<28)) | (0x0a<<28);
    180  1.1  bsh 			gafr2 = (gafr2 & ~0xfffff) | 0xaaaaa;
    181  1.1  bsh 			gpdr1 |= 0x03<<30;
    182  1.1  bsh 			gpdr2 |= 0x3ff;
    183  1.1  bsh 		}
    184  1.1  bsh 		else if( (panelinfo & LCDPANEL_DUAL) ||
    185  1.1  bsh 		    !(panelinfo & LCDPANEL_MONOCHROME ) ){
    186  1.1  bsh 			/* dual or color need L_DD[7:0] */
    187  1.1  bsh 
    188  1.1  bsh 			gafr1 = (gafr1 & ~(0x0f<<28)) | (0x0a<<28);
    189  1.1  bsh 			gafr2 = (gafr2 & ~0x0f) | 0x0a;
    190  1.1  bsh 			gpdr1 |= 0x03<<30;
    191  1.1  bsh 			gpdr2 |= 0x03;
    192  1.1  bsh 		}
    193  1.1  bsh 
    194  1.1  bsh 
    195  1.1  bsh 		bus_space_write_4( iot, gpioh, GPIO_GAFR1_U, gafr1 );
    196  1.1  bsh 		bus_space_write_4( iot, gpioh, GPIO_GAFR2_L, gafr2 );
    197  1.1  bsh 		bus_space_write_4( iot, gpioh, GPIO_GPDR1, gpdr1 );
    198  1.1  bsh 		bus_space_write_4( iot, gpioh, GPIO_GPDR2, gpdr2 );
    199  1.1  bsh 	}
    200  1.1  bsh 
    201  1.1  bsh 
    202  1.1  bsh 
    203  1.1  bsh 
    204  1.1  bsh 	pxa2x0_lcd_geometry( sc, geom );
    205  1.1  bsh }
    206  1.1  bsh 
    207  1.1  bsh 
    208  1.1  bsh int
    209  1.1  bsh lcdintr(void *arg)
    210  1.1  bsh {
    211  1.1  bsh 	struct pxa2x0_lcd_softc *sc = arg;
    212  1.1  bsh 	bus_space_tag_t iot = sc->iot;
    213  1.1  bsh 	bus_space_handle_t ioh = sc->ioh;
    214  1.1  bsh 
    215  1.1  bsh 	static uint32_t status;
    216  1.1  bsh 
    217  1.1  bsh 	status = bus_space_read_4( iot, ioh, LCDC_LCSR );
    218  1.1  bsh 	/* Clear stickey status bits */
    219  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCSR, status );
    220  1.1  bsh 
    221  1.1  bsh 	return 1;
    222  1.1  bsh }
    223  1.1  bsh 
    224  1.1  bsh void
    225  1.1  bsh pxa2x0_lcd_start_dma( struct pxa2x0_lcd_softc *sc,
    226  1.1  bsh     struct pxa2x0_lcd_screen *scr )
    227  1.1  bsh {
    228  1.1  bsh 	uint32_t tmp;
    229  1.1  bsh 	bus_space_tag_t iot = sc->iot;
    230  1.1  bsh 	bus_space_handle_t ioh = sc->ioh;
    231  1.1  bsh 	int val, save;
    232  1.1  bsh 
    233  1.1  bsh 	save = disable_interrupts( I32_bit );
    234  1.1  bsh 
    235  1.1  bsh 	switch( scr->depth ){
    236  1.1  bsh 	case 1: val = 0; break;
    237  1.1  bsh 	case 2: val = 1; break;
    238  1.1  bsh 	case 4: val = 2; break;
    239  1.1  bsh 	case 8: val = 3; break;
    240  1.1  bsh 	case 16:    /* FALLTHROUGH */
    241  1.1  bsh 	default:
    242  1.1  bsh 		val = 4; break;
    243  1.1  bsh 	}
    244  1.1  bsh 
    245  1.1  bsh 	tmp = bus_space_read_4( iot, ioh, LCDC_LCCR3 );
    246  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCCR3,
    247  1.1  bsh 	    (tmp & ~LCCR3_BPP) | (val << LCCR3_BPP_SHIFT) );
    248  1.1  bsh 
    249  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_FDADR0,
    250  1.1  bsh 	    scr->depth == 16 ? scr->dma_desc_pa :
    251  1.1  bsh 	    scr->dma_desc_pa + 2 * sizeof (struct lcd_dma_descriptor) );
    252  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_FDADR1,
    253  1.1  bsh 	    scr->dma_desc_pa + 1 * sizeof (struct lcd_dma_descriptor) );
    254  1.1  bsh 
    255  1.1  bsh 	/* clear status */
    256  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCSR, 0 );
    257  1.1  bsh 
    258  1.1  bsh 	delay(1000);			/* ??? */
    259  1.1  bsh 
    260  1.1  bsh 	/* Enable LCDC */
    261  1.1  bsh 	tmp = bus_space_read_4( iot, ioh, LCDC_LCCR0 );
    262  1.1  bsh 	/*tmp &= ~LCCR0_SFM;*/
    263  1.1  bsh 	bus_space_write_4( iot, ioh, LCDC_LCCR0, tmp | LCCR0_ENB );
    264  1.1  bsh 
    265  1.1  bsh 	restore_interrupts( save );
    266  1.1  bsh 
    267  1.1  bsh }
    268  1.1  bsh 
    269  1.1  bsh 
    270  1.1  bsh static void
    271  1.1  bsh pxa2x0_lcd_stop_dma( struct pxa2x0_lcd_softc *sc )
    272  1.1  bsh {
    273  1.1  bsh 	/* Stop LCD DMA after current frame */
    274  1.1  bsh 	bus_space_write_4( sc->iot, sc->ioh, LCDC_LCCR0,
    275  1.1  bsh 	    LCCR0_DIS |
    276  1.1  bsh 	    bus_space_read_4( sc->iot, sc->ioh, LCDC_LCCR0 ) );
    277  1.1  bsh 
    278  1.1  bsh 	/* wait for disabling done.
    279  1.1  bsh 	   XXX: use interrupt. */
    280  1.1  bsh 	while( LCCR0_ENB &
    281  1.1  bsh 	    bus_space_read_4( sc->iot, sc->ioh, LCDC_LCCR0 ) )
    282  1.1  bsh 		;
    283  1.1  bsh 
    284  1.1  bsh 	bus_space_write_4( sc->iot, sc->ioh, LCDC_LCCR0,
    285  1.1  bsh 	    ~LCCR0_DIS &
    286  1.1  bsh 	    bus_space_read_4( sc->iot, sc->ioh, LCDC_LCCR0 ) );
    287  1.1  bsh }
    288  1.1  bsh 
    289  1.1  bsh #define _rgb(r,g,b)	(((r)<<11) | ((g)<<5) | b)
    290  1.1  bsh #define rgb(r,g,b)	_rgb((r)>>1,g,(b)>>1)
    291  1.1  bsh 
    292  1.1  bsh #define L	0x1f			/* low intensity */
    293  1.1  bsh #define H	0x3f			/* hight intensity */
    294  1.1  bsh 
    295  1.1  bsh static uint16_t basic_color_map[] = {
    296  1.1  bsh 	rgb(	0,   0,   0),		/* black */
    297  1.1  bsh 	rgb(	L,   0,   0),		/* red */
    298  1.1  bsh 	rgb(	0,   L,   0),		/* green */
    299  1.1  bsh 	rgb(	L,   L,   0),		/* brown */
    300  1.1  bsh 	rgb(	0,   0,   L),		/* blue */
    301  1.1  bsh 	rgb(	L,   0,   L),		/* magenta */
    302  1.1  bsh 	rgb(	0,   L,   L),		/* cyan */
    303  1.1  bsh 	_rgb(0x1c,0x38,0x1c),		/* white */
    304  1.1  bsh 
    305  1.1  bsh 	rgb(	L,   L,   L),		/* black */
    306  1.1  bsh 	rgb(	H,   0,   0),		/* red */
    307  1.1  bsh 	rgb(	0,   H,   0),		/* green */
    308  1.1  bsh 	rgb(	H,   H,   0),		/* brown */
    309  1.1  bsh 	rgb(	0,   0,   H),		/* blue */
    310  1.1  bsh 	rgb(	H,   0,   H),		/* magenta */
    311  1.1  bsh 	rgb(	0,   H,   H),		/* cyan */
    312  1.1  bsh 	rgb(	H,   H,   H)
    313  1.1  bsh };
    314  1.1  bsh 
    315  1.1  bsh #undef H
    316  1.1  bsh #undef L
    317  1.1  bsh 
    318  1.1  bsh static void
    319  1.1  bsh init_pallet( uint16_t *buf, int depth )
    320  1.1  bsh {
    321  1.1  bsh 	int i;
    322  1.1  bsh 
    323  1.1  bsh 	/* convert RGB332 to RGB565 */
    324  1.1  bsh 	switch( depth ){
    325  1.1  bsh 	case 8:
    326  1.1  bsh 	case 4:
    327  1.1  bsh #if 0
    328  1.1  bsh 		for( i=0; i <= 255; ++i ){
    329  1.1  bsh 			buf[i] = ((9 * ((i>>5) & 0x07)) <<11) |
    330  1.1  bsh 			    ((9 * ((i>>2) & 0x07)) << 5) |
    331  1.1  bsh 			    ((21 * (i & 0x03))/2 );
    332  1.1  bsh 		}
    333  1.1  bsh #else
    334  1.1  bsh 		memcpy( buf, basic_color_map, sizeof basic_color_map );
    335  1.1  bsh 		for( i=16; i < (1<<depth); ++i )
    336  1.1  bsh 			buf[i] = 0xffff;
    337  1.1  bsh #endif
    338  1.1  bsh 		break;
    339  1.1  bsh 	default:
    340  1.1  bsh 	}
    341  1.1  bsh }
    342  1.1  bsh 
    343  1.1  bsh struct pxa2x0_lcd_screen *
    344  1.1  bsh pxa2x0_lcd_new_screen( struct pxa2x0_lcd_softc *sc,
    345  1.1  bsh     int depth )
    346  1.1  bsh {
    347  1.1  bsh 	struct pxa2x0_lcd_screen *scr = NULL;
    348  1.1  bsh 	int width, height;
    349  1.1  bsh 	bus_size_t size;
    350  1.1  bsh         int error, pallet_size;
    351  1.1  bsh 	int busdma_flag = (cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    352  1.1  bsh 	struct lcd_dma_descriptor *desc;
    353  1.1  bsh 	paddr_t buf_pa, desc_pa;
    354  1.1  bsh 
    355  1.1  bsh 	width = sc->geometry->panel_width;
    356  1.1  bsh 	height = sc->geometry->panel_height;
    357  1.1  bsh 	pallet_size = 0;
    358  1.1  bsh 
    359  1.1  bsh 	switch( depth ){
    360  1.1  bsh 	case 1:
    361  1.1  bsh 	case 2:
    362  1.1  bsh 	case 4:
    363  1.1  bsh 	case 8:
    364  1.1  bsh 		pallet_size = (1<<depth) * sizeof (uint16_t);
    365  1.1  bsh 		/* FALLTHROUGH */
    366  1.1  bsh 	case 16:
    367  1.1  bsh 		size = roundup(width,4)*depth/8 * height;
    368  1.1  bsh 		break;
    369  1.1  bsh 	default:
    370  1.1  bsh 		printf( "%s: Unknown depth (%d)\n", sc->dev.dv_xname, depth );
    371  1.1  bsh 		return NULL;
    372  1.1  bsh 	}
    373  1.1  bsh 
    374  1.1  bsh 	scr = malloc( sizeof *scr, M_DEVBUF,
    375  1.1  bsh 	    M_ZERO | (cold ? M_NOWAIT : M_WAITOK) );
    376  1.1  bsh 
    377  1.1  bsh 	if( scr == NULL )
    378  1.1  bsh 		return NULL;
    379  1.1  bsh 
    380  1.1  bsh 	scr->nsegs = 0;
    381  1.1  bsh 	scr->depth = depth;
    382  1.1  bsh 	scr->buf_size = size;
    383  1.1  bsh 	scr->buf_va = NULL;
    384  1.1  bsh 	size = roundup(size,16) + 3 * sizeof (struct lcd_dma_descriptor)
    385  1.1  bsh 	    + pallet_size;
    386  1.1  bsh 
    387  1.1  bsh         error = bus_dmamem_alloc( sc->dma_tag, size, 16, 0,
    388  1.1  bsh 	    scr->segs, 1, &(scr->nsegs), busdma_flag );
    389  1.1  bsh 
    390  1.1  bsh         if( error || scr->nsegs != 1 ){
    391  1.1  bsh 		/* XXX: Actually we can handle nsegs>1 case by means
    392  1.1  bsh                    of multiple DMA descriptors for a panel.  it will
    393  1.1  bsh                    makes code here a bit hairly */
    394  1.1  bsh 		goto bad;
    395  1.1  bsh         }
    396  1.1  bsh 
    397  1.1  bsh 	error = bus_dmamem_map( sc->dma_tag, scr->segs, scr->nsegs,
    398  1.1  bsh 	    size, (caddr_t *)&(scr->buf_va), busdma_flag | BUS_DMA_COHERENT );
    399  1.1  bsh 	if( error )
    400  1.1  bsh 		goto bad;
    401  1.1  bsh 
    402  1.1  bsh 
    403  1.1  bsh 	memset( scr->buf_va, 0, scr->buf_size );
    404  1.1  bsh 
    405  1.1  bsh 	/* map memory for DMA */
    406  1.1  bsh 	if( bus_dmamap_create( sc->dma_tag, 1024*1024*2, 1,
    407  1.1  bsh 	    1024*1024*2, 0,  busdma_flag, &scr->dma ) )
    408  1.1  bsh 		goto bad;
    409  1.1  bsh 	error = bus_dmamap_load( sc->dma_tag, scr->dma,
    410  1.1  bsh 	    scr->buf_va, size, NULL, busdma_flag );
    411  1.1  bsh 	if( error ){
    412  1.1  bsh 		goto bad;
    413  1.1  bsh 	}
    414  1.1  bsh 
    415  1.1  bsh 	buf_pa = scr->segs[0].ds_addr;
    416  1.1  bsh 	desc_pa = buf_pa + roundup(size, NBPG) - 3*sizeof *desc;
    417  1.1  bsh 
    418  1.1  bsh 	/* make descriptors at the top of mapped memory */
    419  1.1  bsh 	desc = (struct lcd_dma_descriptor *)(
    420  1.1  bsh 		(caddr_t)(scr->buf_va) + roundup(size, NBPG) - 3*sizeof *desc);
    421  1.1  bsh 
    422  1.1  bsh 	desc[0].fdadr = desc_pa;
    423  1.1  bsh 	desc[0].fsadr = buf_pa;
    424  1.1  bsh 	desc[0].ldcmd = scr->buf_size;
    425  1.1  bsh 
    426  1.1  bsh 	if( pallet_size ){
    427  1.1  bsh 		init_pallet( (uint16_t *)((char *)desc - pallet_size), depth );
    428  1.1  bsh 
    429  1.1  bsh 		desc[2].fdadr = desc_pa; /* chain to panel 0 */
    430  1.1  bsh 		desc[2].fsadr = desc_pa - pallet_size;
    431  1.1  bsh 		desc[2].ldcmd = pallet_size | LDCMD_PAL;
    432  1.1  bsh 	}
    433  1.1  bsh 
    434  1.1  bsh 	if( sc->geometry->panel_info & LCDPANEL_DUAL ){
    435  1.1  bsh 		/* Dual panel */
    436  1.1  bsh 		desc[1].fdadr = desc_pa + sizeof *desc;
    437  1.1  bsh 		desc[1].fsadr = buf_pa + scr->buf_size/2;
    438  1.1  bsh 		desc[0].ldcmd = desc[1].ldcmd = scr->buf_size/2;
    439  1.1  bsh 
    440  1.1  bsh 	}
    441  1.1  bsh 
    442  1.1  bsh #if 0
    443  1.1  bsh 	desc[0].ldcmd |= LDCMD_SOFINT;
    444  1.1  bsh 	desc[1].ldcmd |= LDCMD_SOFINT;
    445  1.1  bsh #endif
    446  1.1  bsh 
    447  1.1  bsh 	scr->dma_desc = desc;
    448  1.1  bsh 	scr->dma_desc_pa = desc_pa;
    449  1.1  bsh 	scr->map_size = size;		/* used when unmap this. */
    450  1.1  bsh 
    451  1.1  bsh 	LIST_INSERT_HEAD( &(sc->screens), scr, link );
    452  1.1  bsh 	sc->n_screens++;
    453  1.1  bsh 
    454  1.1  bsh 	return scr;
    455  1.1  bsh 
    456  1.1  bsh  bad:
    457  1.1  bsh 	if( scr ){
    458  1.1  bsh 		if( scr->buf_va )
    459  1.1  bsh 			bus_dmamem_unmap( sc->dma_tag, scr->buf_va, size );
    460  1.1  bsh 		if( scr->nsegs )
    461  1.1  bsh 			bus_dmamem_free( sc->dma_tag, scr->segs, scr->nsegs );
    462  1.1  bsh 		free( scr, M_DEVBUF );
    463  1.1  bsh 	}
    464  1.1  bsh 	return NULL;
    465  1.1  bsh }
    466  1.1  bsh 
    467  1.1  bsh 
    468  1.1  bsh #if NWSDISPLAY > 0
    469  1.1  bsh 
    470  1.1  bsh /*
    471  1.1  bsh  * Initialize struct wsscreen_descr based on values calculated by
    472  1.1  bsh  * raster operation subsystem.
    473  1.1  bsh  */
    474  1.1  bsh int
    475  1.1  bsh pxa2x0_lcd_setup_wsscreen(struct pxa2x0_wsscreen_descr *descr,
    476  1.1  bsh     const struct lcd_panel_geometry *geom,
    477  1.1  bsh     const char *fontname )
    478  1.1  bsh {
    479  1.1  bsh 	int width = geom->panel_width;
    480  1.1  bsh 	int height = geom->panel_height;
    481  1.1  bsh 	int cookie = -1;
    482  1.1  bsh 	struct rasops_info rinfo;
    483  1.1  bsh 
    484  1.1  bsh 	memset( &rinfo, 0, sizeof rinfo );
    485  1.1  bsh 
    486  1.1  bsh 	if( fontname ){
    487  1.1  bsh 		wsfont_init();
    488  1.1  bsh 		cookie = wsfont_find( (char *)fontname, 0, 0, 0,
    489  1.1  bsh 		    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R );
    490  1.1  bsh 		if( cookie < 0 ||
    491  1.1  bsh 		    wsfont_lock(cookie, &rinfo.ri_font) )
    492  1.1  bsh 			return -1;
    493  1.1  bsh 	}
    494  1.1  bsh 	else {
    495  1.1  bsh 		/* let rasops_init() choose any font */
    496  1.1  bsh 	}
    497  1.1  bsh 
    498  1.1  bsh 	/* let rasops_init calculate # of cols and rows in character */
    499  1.1  bsh 	rinfo.ri_flg = 0;
    500  1.1  bsh 	rinfo.ri_depth = descr->depth;
    501  1.1  bsh 	rinfo.ri_bits = NULL;
    502  1.1  bsh 	rinfo.ri_width = width;
    503  1.1  bsh 	rinfo.ri_height = height;
    504  1.1  bsh 	rinfo.ri_stride = width * rinfo.ri_depth / 8;
    505  1.1  bsh 	rinfo.ri_wsfcookie = cookie;
    506  1.1  bsh 
    507  1.1  bsh 	rasops_init( &rinfo, 100, 100 );
    508  1.1  bsh 
    509  1.1  bsh 	descr->c.nrows = rinfo.ri_rows;
    510  1.1  bsh 	descr->c.ncols = rinfo.ri_cols;
    511  1.1  bsh 	descr->c.capabilities = rinfo.ri_caps;
    512  1.1  bsh 
    513  1.1  bsh 	return cookie;
    514  1.1  bsh }
    515  1.1  bsh 
    516  1.1  bsh 
    517  1.1  bsh int
    518  1.1  bsh pxa2x0_lcd_show_screen(void *v, void *cookie, int waitok,
    519  1.1  bsh     void (*cb)(void *, int, int), void *cbarg)
    520  1.1  bsh {
    521  1.1  bsh 	struct pxa2x0_lcd_softc *sc = v;
    522  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie, *old;
    523  1.1  bsh 
    524  1.1  bsh 	old = sc->active;
    525  1.1  bsh 	if( old == scr )
    526  1.1  bsh 		return 0;
    527  1.1  bsh 
    528  1.1  bsh 	if( old )
    529  1.1  bsh 		pxa2x0_lcd_stop_dma( sc );
    530  1.1  bsh 
    531  1.1  bsh 	pxa2x0_lcd_start_dma( sc, scr );
    532  1.1  bsh 
    533  1.1  bsh 	sc->active = scr;
    534  1.1  bsh 	return 0;
    535  1.1  bsh }
    536  1.1  bsh 
    537  1.1  bsh int
    538  1.1  bsh pxa2x0_lcd_alloc_screen(void *v, const struct wsscreen_descr *_type,
    539  1.1  bsh     void **cookiep, int *curxp, int *curyp, long *attrp)
    540  1.1  bsh {
    541  1.1  bsh 	struct pxa2x0_lcd_softc *sc = v;
    542  1.1  bsh 	struct pxa2x0_lcd_screen *scr;
    543  1.1  bsh 	struct pxa2x0_wsscreen_descr *type = (struct pxa2x0_wsscreen_descr *)_type;
    544  1.1  bsh 
    545  1.1  bsh 	scr = pxa2x0_lcd_new_screen( sc, type->depth );
    546  1.1  bsh 	if( scr == NULL )
    547  1.1  bsh 		return -1;
    548  1.1  bsh 
    549  1.1  bsh 	/*
    550  1.1  bsh 	 * initialize raster operation for this screen.
    551  1.1  bsh 	 */
    552  1.1  bsh 	scr->rinfo.ri_flg = 0;
    553  1.1  bsh 	scr->rinfo.ri_depth = type->depth;
    554  1.1  bsh 	scr->rinfo.ri_bits = scr->buf_va;
    555  1.1  bsh 	scr->rinfo.ri_width = sc->geometry->panel_width;
    556  1.1  bsh 	scr->rinfo.ri_height = sc->geometry->panel_height;
    557  1.1  bsh 	scr->rinfo.ri_stride = scr->rinfo.ri_width * scr->rinfo.ri_depth / 8;
    558  1.1  bsh 	scr->rinfo.ri_wsfcookie = -1;	/* XXX */
    559  1.1  bsh 
    560  1.1  bsh 	rasops_init( &scr->rinfo, type->c.nrows, type->c.ncols );
    561  1.1  bsh 
    562  1.1  bsh 	(* scr->rinfo.ri_ops.allocattr)(&scr->rinfo, 0, 0, 0, attrp );
    563  1.1  bsh 
    564  1.1  bsh 	*cookiep = scr;
    565  1.1  bsh 	*curxp = 0;
    566  1.1  bsh 	*curyp = 0;
    567  1.1  bsh 
    568  1.1  bsh 	return 0;
    569  1.1  bsh }
    570  1.1  bsh 
    571  1.1  bsh 
    572  1.1  bsh void
    573  1.1  bsh pxa2x0_lcd_free_screen(void *v, void *cookie)
    574  1.1  bsh {
    575  1.1  bsh 	struct pxa2x0_lcd_softc *sc = v;
    576  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    577  1.1  bsh 
    578  1.1  bsh 	LIST_REMOVE( scr, link );
    579  1.1  bsh 	sc->n_screens--;
    580  1.1  bsh 	if( scr == sc->active ){
    581  1.1  bsh 		/* at first, we need to stop LCD DMA */
    582  1.1  bsh 		sc->active = NULL;
    583  1.1  bsh 
    584  1.1  bsh 		printf( "lcd_free on active screen\n" );
    585  1.1  bsh 
    586  1.1  bsh 		pxa2x0_lcd_stop_dma(sc);
    587  1.1  bsh 	}
    588  1.1  bsh 
    589  1.1  bsh 	if( scr->buf_va )
    590  1.1  bsh 		bus_dmamem_unmap( sc->dma_tag, scr->buf_va, scr->map_size );
    591  1.1  bsh 
    592  1.1  bsh 	if( scr->nsegs > 0 )
    593  1.1  bsh 		bus_dmamem_free( sc->dma_tag, scr->segs, scr->nsegs );
    594  1.1  bsh 
    595  1.1  bsh 	free( scr, M_DEVBUF );
    596  1.1  bsh }
    597  1.1  bsh 
    598  1.1  bsh int
    599  1.1  bsh pxa2x0_lcd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    600  1.1  bsh {
    601  1.1  bsh 	struct pxa2x0_lcd_softc *sc = v;
    602  1.1  bsh 	struct wsdisplay_fbinfo *wsdisp_info;
    603  1.1  bsh 	uint32_t ccr0;
    604  1.1  bsh 
    605  1.1  bsh 	switch (cmd) {
    606  1.1  bsh 	case WSDISPLAYIO_GTYPE:
    607  1.1  bsh 		*(u_int *)data = WSDISPLAY_TYPE_UNKNOWN; /* XXX */
    608  1.1  bsh 		return 0;
    609  1.1  bsh 
    610  1.1  bsh 	case WSDISPLAYIO_GINFO:
    611  1.1  bsh 		wsdisp_info = (struct wsdisplay_fbinfo *)data;
    612  1.1  bsh 
    613  1.1  bsh 		wsdisp_info->height = sc->geometry->panel_height;
    614  1.1  bsh 		wsdisp_info->width = sc->geometry->panel_width;
    615  1.1  bsh 		wsdisp_info->depth = 16; /* XXX */
    616  1.1  bsh 		wsdisp_info->cmsize = 0;
    617  1.1  bsh 		return 0;
    618  1.1  bsh 
    619  1.1  bsh 	case WSDISPLAYIO_GETCMAP:
    620  1.1  bsh 	case WSDISPLAYIO_PUTCMAP:
    621  1.1  bsh 		return EPASSTHROUGH;	/* XXX Colormap */
    622  1.1  bsh 
    623  1.1  bsh 	case WSDISPLAYIO_SVIDEO:
    624  1.1  bsh 		if( *(int *)data == WSDISPLAYIO_VIDEO_ON ){
    625  1.1  bsh 		  /* turn it on */
    626  1.1  bsh 		}
    627  1.1  bsh 		else {
    628  1.1  bsh 		  /* start LCD shutdown */
    629  1.1  bsh 		  /* sleep until interrupt */
    630  1.1  bsh 		}
    631  1.1  bsh 		return 0;
    632  1.1  bsh 
    633  1.1  bsh 	case WSDISPLAYIO_GVIDEO:
    634  1.1  bsh 		ccr0 = bus_space_read_4( sc->iot, sc->ioh, LCDC_LCCR0 );
    635  1.1  bsh 		*(u_int *)data = (ccr0 & (LCCR0_ENB|LCCR0_DIS)) == LCCR0_ENB ?
    636  1.1  bsh 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    637  1.1  bsh 		return 0;
    638  1.1  bsh 
    639  1.1  bsh 
    640  1.1  bsh 
    641  1.1  bsh 	case WSDISPLAYIO_GCURPOS:
    642  1.1  bsh 	case WSDISPLAYIO_SCURPOS:
    643  1.1  bsh 	case WSDISPLAYIO_GCURMAX:
    644  1.1  bsh 	case WSDISPLAYIO_GCURSOR:
    645  1.1  bsh 	case WSDISPLAYIO_SCURSOR:
    646  1.1  bsh 		return EPASSTHROUGH;	/* XXX */
    647  1.1  bsh 	}
    648  1.1  bsh 
    649  1.1  bsh 	return EPASSTHROUGH;
    650  1.1  bsh }
    651  1.1  bsh 
    652  1.1  bsh paddr_t
    653  1.1  bsh pxa2x0_lcd_mmap(void *v, off_t offset, int prot)
    654  1.1  bsh {
    655  1.1  bsh 	struct pxa2x0_lcd_softc *sc = v;
    656  1.1  bsh 	struct pxa2x0_lcd_screen *screen = sc->active;  /* ??? */
    657  1.1  bsh 
    658  1.1  bsh 	if( screen == NULL )
    659  1.1  bsh 		return -1;
    660  1.1  bsh 
    661  1.1  bsh 	return bus_dmamem_mmap( sc->dma_tag, screen->segs, screen->nsegs,
    662  1.1  bsh 	    offset, prot, BUS_DMA_WAITOK|BUS_DMA_COHERENT );
    663  1.1  bsh 	return -1;
    664  1.1  bsh }
    665  1.1  bsh 
    666  1.1  bsh 
    667  1.1  bsh static void
    668  1.1  bsh pxa2x0_lcd_cursor( void *cookie, int on, int row, int col )
    669  1.1  bsh {
    670  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    671  1.1  bsh 
    672  1.1  bsh 	(* scr->rinfo.ri_ops.cursor)( &scr->rinfo, on, row, col );
    673  1.1  bsh }
    674  1.1  bsh 
    675  1.1  bsh static int
    676  1.1  bsh pxa2x0_lcd_mapchar( void *cookie, int c, unsigned int *cp )
    677  1.1  bsh {
    678  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    679  1.1  bsh 
    680  1.1  bsh 	return (* scr->rinfo.ri_ops.mapchar)( &scr->rinfo, c, cp );
    681  1.1  bsh }
    682  1.1  bsh 
    683  1.1  bsh static void
    684  1.1  bsh pxa2x0_lcd_putchar( void *cookie, int row, int col, u_int uc, long attr )
    685  1.1  bsh {
    686  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    687  1.1  bsh 
    688  1.1  bsh 	(* scr->rinfo.ri_ops.putchar)( &scr->rinfo,
    689  1.1  bsh 	    row, col, uc, attr );
    690  1.1  bsh }
    691  1.1  bsh 
    692  1.1  bsh static void
    693  1.1  bsh pxa2x0_lcd_copycols( void *cookie, int row, int src, int dst, int num)
    694  1.1  bsh {
    695  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    696  1.1  bsh 
    697  1.1  bsh 	(* scr->rinfo.ri_ops.copycols)( &scr->rinfo,
    698  1.1  bsh 	    row, src, dst, num );
    699  1.1  bsh }
    700  1.1  bsh 
    701  1.1  bsh static void
    702  1.1  bsh pxa2x0_lcd_erasecols( void *cookie, int row, int col, int num, long attr)
    703  1.1  bsh {
    704  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    705  1.1  bsh 
    706  1.1  bsh 	(* scr->rinfo.ri_ops.erasecols)( &scr->rinfo,
    707  1.1  bsh 	    row, col, num, attr );
    708  1.1  bsh }
    709  1.1  bsh 
    710  1.1  bsh static void
    711  1.1  bsh pxa2x0_lcd_copyrows( void *cookie, int src, int dst, int num )
    712  1.1  bsh {
    713  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    714  1.1  bsh 
    715  1.1  bsh 	(* scr->rinfo.ri_ops.copyrows)( &scr->rinfo,
    716  1.1  bsh 	    src, dst, num );
    717  1.1  bsh }
    718  1.1  bsh 
    719  1.1  bsh static void
    720  1.1  bsh pxa2x0_lcd_eraserows( void *cookie, int row, int num, long attr )
    721  1.1  bsh {
    722  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    723  1.1  bsh 
    724  1.1  bsh 	(* scr->rinfo.ri_ops.eraserows)( &scr->rinfo,
    725  1.1  bsh 	    row, num, attr );
    726  1.1  bsh }
    727  1.1  bsh 
    728  1.1  bsh static int
    729  1.1  bsh pxa2x0_lcd_alloc_attr( void *cookie, int fg, int bg, int flg, long *attr)
    730  1.1  bsh {
    731  1.1  bsh 	struct pxa2x0_lcd_screen *scr = cookie;
    732  1.1  bsh 
    733  1.1  bsh 	return (* scr->rinfo.ri_ops.allocattr)( &scr->rinfo,
    734  1.1  bsh 	    fg, bg, flg, attr );
    735  1.1  bsh }
    736  1.1  bsh 
    737  1.1  bsh 
    738  1.1  bsh const struct wsdisplay_emulops pxa2x0_lcd_emulops = {
    739  1.1  bsh 	pxa2x0_lcd_cursor,
    740  1.1  bsh 	pxa2x0_lcd_mapchar,
    741  1.1  bsh 	pxa2x0_lcd_putchar,
    742  1.1  bsh 	pxa2x0_lcd_copycols,
    743  1.1  bsh 	pxa2x0_lcd_erasecols,
    744  1.1  bsh 	pxa2x0_lcd_copyrows,
    745  1.1  bsh 	pxa2x0_lcd_eraserows,
    746  1.1  bsh 	pxa2x0_lcd_alloc_attr
    747  1.1  bsh };
    748  1.1  bsh 
    749  1.1  bsh #endif /* NWSDISPLAY > 0 */
    750