Home | History | Annotate | Line # | Download | only in xscale
pxa2x0_lcd.c revision 1.21
      1 /* $NetBSD: pxa2x0_lcd.c,v 1.21 2007/06/28 14:41:49 nonaka Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002  Genetec Corporation.  All rights reserved.
      5  * Written by Hiroyuki Bessho for Genetec Corporation.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project by
     18  *	Genetec Corporation.
     19  * 4. The name of Genetec Corporation may not be used to endorse or
     20  *    promote products derived from this software without specific prior
     21  *    written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Support PXA2[15]0's integrated LCD controller.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: pxa2x0_lcd.c,v 1.21 2007/06/28 14:41:49 nonaka Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/conf.h>
     46 #include <sys/uio.h>
     47 #include <sys/malloc.h>
     48 #include <sys/kernel.h>			/* for cold */
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #include <dev/cons.h>
     53 #include <dev/wscons/wsconsio.h>
     54 #include <dev/wscons/wsdisplayvar.h>
     55 #include <dev/wscons/wscons_callbacks.h>
     56 #include <dev/rasops/rasops.h>
     57 #include <dev/wsfont/wsfont.h>
     58 
     59 #include <machine/bus.h>
     60 #include <machine/cpu.h>
     61 #include <arm/cpufunc.h>
     62 
     63 #include <arm/xscale/pxa2x0cpu.h>
     64 #include <arm/xscale/pxa2x0var.h>
     65 #include <arm/xscale/pxa2x0reg.h>
     66 #include <arm/xscale/pxa2x0_lcd.h>
     67 #include <arm/xscale/pxa2x0_gpio.h>
     68 
     69 #include "wsdisplay.h"
     70 
     71 /*
     72  * Console variables. These are necessary since console is setup very early,
     73  * before devices get attached.
     74  */
     75 struct {
     76 	int				 is_console;
     77 	struct pxa2x0_wsscreen_descr	*descr;
     78 	const struct lcd_panel_geometry *geom;
     79 } pxa2x0_lcd_console;
     80 
     81 int		lcdintr(void *);
     82 
     83 static void	pxa2x0_lcd_initialize(struct pxa2x0_lcd_softc *,
     84 		    const struct lcd_panel_geometry *);
     85 static void	pxa2x0_lcd_setup_rasops(struct pxa2x0_lcd_softc *,
     86 		    struct rasops_info *, struct pxa2x0_wsscreen_descr *,
     87 		    const struct lcd_panel_geometry *);
     88 
     89 void
     90 pxa2x0_lcd_geometry(struct pxa2x0_lcd_softc *sc,
     91     const struct lcd_panel_geometry *info)
     92 {
     93 	bus_space_tag_t iot;
     94 	bus_space_handle_t ioh;
     95 	uint32_t ccr0;
     96 	int lines;
     97 
     98 	iot = sc->iot;
     99 	ioh = sc->ioh;
    100 	sc->geometry = info;
    101 
    102 	ccr0 = LCCR0_IMASK;
    103 	if (info->panel_info & LCDPANEL_ACTIVE)
    104 		ccr0 |= LCCR0_PAS;	/* active mode */
    105 	if ((info->panel_info & (LCDPANEL_DUAL|LCDPANEL_ACTIVE))
    106 	    == LCDPANEL_DUAL)
    107 		ccr0 |= LCCR0_SDS; /* dual panel */
    108 	if (info->panel_info & LCDPANEL_MONOCHROME)
    109 		ccr0 |= LCCR0_CMS;
    110 	bus_space_write_4(iot, ioh, LCDC_LCCR0, ccr0);
    111 
    112 	bus_space_write_4(iot, ioh, LCDC_LCCR1,
    113 	    (info->panel_width-1)
    114 	    | ((info->hsync_pulse_width-1)<<10)
    115 	    | ((info->end_line_wait-1)<<16)
    116 	    | ((info->beg_line_wait-1)<<24));
    117 
    118 	if (info->panel_info & LCDPANEL_DUAL)
    119 		lines = info->panel_height/2 + info->extra_lines;
    120 	else
    121 		lines = info->panel_height + info->extra_lines;
    122 
    123 	bus_space_write_4(iot, ioh, LCDC_LCCR2,
    124 	    (lines-1)
    125 	    | (info->vsync_pulse_width<<10)
    126 	    | (info->end_frame_wait<<16)
    127 	    | (info->beg_frame_wait<<24));
    128 
    129 	bus_space_write_4(iot, ioh, LCDC_LCCR3,
    130 	    (info->pixel_clock_div<<0)
    131 	    | (info->ac_bias << 8)
    132 	    | ((info->panel_info &
    133 		(LCDPANEL_VSP|LCDPANEL_HSP|LCDPANEL_PCP|LCDPANEL_OEP))
    134 		<<20)
    135 	    | (4 << 24) /* 16bpp */
    136 	    | ((info->panel_info & LCDPANEL_DPC) ? (1<<27) : 0)
    137 	    );
    138 }
    139 
    140 /*
    141  * Initialize the LCD controller.
    142  */
    143 void
    144 pxa2x0_lcd_initialize(struct pxa2x0_lcd_softc *sc,
    145     const struct lcd_panel_geometry *geom)
    146 {
    147 	bus_space_tag_t iot;
    148 	bus_space_handle_t ioh;
    149 	uint32_t lccr0, lscr;
    150 	int nldd;
    151 
    152 	iot = sc->iot;
    153 	ioh = sc->ioh;
    154 
    155 	/* Check if LCD is enabled before programming, it should not
    156 	 * be enabled while it is being reprogrammed, therefore disable
    157 	 * it first.
    158 	 */
    159 	lccr0 = bus_space_read_4(iot, ioh, LCDC_LCCR0);
    160 	if (lccr0 & LCCR0_ENB) {
    161 		lccr0 |= LCCR0_LDM;
    162 		bus_space_write_4(iot, ioh, LCDC_LCCR0, lccr0);
    163 		lccr0 = bus_space_read_4(iot, ioh, LCDC_LCCR0); /* paranoia */
    164 		lccr0 |= LCCR0_DIS;
    165 		bus_space_write_4(iot, ioh, LCDC_LCCR0, lccr0);
    166 		do {
    167 			lscr = bus_space_read_4(iot, ioh, LCDC_LCSR);
    168 		} while (!(lscr & LCSR_LDD));
    169 	}
    170 
    171 	/* enable clock */
    172 	pxa2x0_clkman_config(CKEN_LCD, 1);
    173 
    174 	bus_space_write_4(iot, ioh, LCDC_LCCR0, LCCR0_IMASK);
    175 
    176 	/*
    177 	 * setup GP[77:58] for LCD
    178 	 */
    179 	/* Always use [FLP]CLK, ACBIAS */
    180 	pxa2x0_gpio_set_function(74, GPIO_ALT_FN_2_OUT);
    181 	pxa2x0_gpio_set_function(75, GPIO_ALT_FN_2_OUT);
    182 	pxa2x0_gpio_set_function(76, GPIO_ALT_FN_2_OUT);
    183 	pxa2x0_gpio_set_function(77, GPIO_ALT_FN_2_OUT);
    184 
    185 	if ((geom->panel_info & LCDPANEL_ACTIVE) ||
    186 	    ((geom->panel_info & (LCDPANEL_MONOCHROME|LCDPANEL_DUAL)) ==
    187 	    LCDPANEL_DUAL)) {
    188 		/* active and color dual panel need L_DD[15:0] */
    189 		nldd = 16;
    190 	} else if ((geom->panel_info & LCDPANEL_DUAL) ||
    191 	    !(geom->panel_info & LCDPANEL_MONOCHROME)) {
    192 		/* dual or color need L_DD[7:0] */
    193 		nldd = 8;
    194 	} else {
    195 		/* Otherwise just L_DD[3:0] */
    196 		nldd = 4;
    197 	}
    198 
    199 	while (nldd--)
    200 		pxa2x0_gpio_set_function(58 + nldd, GPIO_ALT_FN_2_OUT);
    201 
    202 	pxa2x0_lcd_geometry(sc, geom);
    203 }
    204 
    205 /*
    206  * Common driver attachment code.
    207  */
    208 void
    209 pxa2x0_lcd_attach_sub(struct pxa2x0_lcd_softc *sc,
    210     struct pxaip_attach_args *pxa, const struct lcd_panel_geometry *geom)
    211 {
    212 	bus_space_tag_t iot = pxa->pxa_iot;
    213 	bus_space_handle_t ioh;
    214 	int error;
    215 
    216 	sc->n_screens = 0;
    217 	LIST_INIT(&sc->screens);
    218 
    219 	/* map controller registers */
    220 	error = bus_space_map(iot, PXA2X0_LCDC_BASE,
    221 			       PXA2X0_LCDC_SIZE, 0, &ioh);
    222 	if (error) {
    223 		printf(": failed to map registers (errno=%d)\n", error);
    224 		return;
    225 	}
    226 
    227 	sc->iot = iot;
    228 	sc->ioh = ioh;
    229 	sc->dma_tag = &pxa2x0_bus_dma_tag;
    230 
    231 	sc->ih = pxa2x0_intr_establish(PXA2X0_INT_LCD, IPL_BIO, lcdintr, sc);
    232 	if (sc->ih == NULL) {
    233 		printf(": unable to establish interrupt at irq %d\n",
    234 		    PXA2X0_INT_LCD);
    235 		return;
    236 	}
    237 
    238 	printf(": PXA2x0 LCD controller\n");
    239 
    240 	pxa2x0_lcd_initialize(sc, geom);
    241 
    242 	if (pxa2x0_lcd_console.is_console) {
    243 		struct pxa2x0_wsscreen_descr *descr = pxa2x0_lcd_console.descr;
    244 		struct pxa2x0_lcd_screen *scr;
    245 		struct rasops_info *ri;
    246 		long defattr;
    247 
    248 		error = pxa2x0_lcd_new_screen(sc, descr->depth, &scr);
    249 		if (error) {
    250 			printf("%s: unable to create new screen (errno=%d)",
    251 			    sc->dev.dv_xname, error);
    252 			return;
    253 		}
    254 
    255 		ri = &scr->rinfo;
    256 		ri->ri_hw = (void *)scr;
    257 		ri->ri_bits = scr->buf_va;
    258 		pxa2x0_lcd_setup_rasops(sc, ri, descr, geom);
    259 
    260 		/* assumes 16 bpp */
    261 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    262 
    263 		pxa2x0_lcd_start_dma(sc, scr);
    264 		sc->active = scr;
    265 
    266 		wsdisplay_cnattach(&descr->c, ri, ri->ri_ccol, ri->ri_crow,
    267 		    defattr);
    268 
    269 		printf("%s: console\n", sc->dev.dv_xname);
    270 	}
    271 }
    272 
    273 int
    274 pxa2x0_lcd_cnattach(struct pxa2x0_wsscreen_descr *descr,
    275     const struct lcd_panel_geometry *geom)
    276 {
    277 
    278 	pxa2x0_lcd_console.descr = descr;
    279 	pxa2x0_lcd_console.geom = geom;
    280 	pxa2x0_lcd_console.is_console = 1;
    281 
    282 	return 0;
    283 }
    284 
    285 /*
    286  * Interrupt handler.
    287  */
    288 int
    289 lcdintr(void *arg)
    290 {
    291 	struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)arg;
    292 	bus_space_tag_t iot = sc->iot;
    293 	bus_space_handle_t ioh = sc->ioh;
    294 	uint32_t status;
    295 
    296 	status = bus_space_read_4(iot, ioh, LCDC_LCSR);
    297 	/* Clear stickey status bits */
    298 	bus_space_write_4(iot, ioh, LCDC_LCSR, status);
    299 
    300 	return 1;
    301 }
    302 
    303 /*
    304  * Enable DMA to cause the display to be refreshed periodically.
    305  * This brings the screen to life...
    306  */
    307 void
    308 pxa2x0_lcd_start_dma(struct pxa2x0_lcd_softc *sc,
    309     struct pxa2x0_lcd_screen *scr)
    310 {
    311 	bus_space_tag_t iot;
    312 	bus_space_handle_t ioh;
    313 	uint32_t tmp;
    314 	int val, save;
    315 
    316 	iot = sc->iot;
    317 	ioh = sc->ioh;
    318 
    319 	save = disable_interrupts(I32_bit);
    320 
    321 	switch (scr->depth) {
    322 	case 1: val = 0; break;
    323 	case 2: val = 1; break;
    324 	case 4: val = 2; break;
    325 	case 8: val = 3; break;
    326 	case 16: val = 4; break;
    327 	case 18: val = 5; break;
    328 	case 24: val = 33; break;
    329 	default:
    330 		val = 4; break;
    331 	}
    332 
    333 	tmp = bus_space_read_4(iot, ioh, LCDC_LCCR3);
    334 	if (CPU_IS_PXA270) {
    335 		bus_space_write_4(iot, ioh, LCDC_LCCR3,
    336 		  (tmp & ~(LCCR3_BPP|(1<<29))) | (val << LCCR3_BPP_SHIFT));
    337 	} else {
    338 		bus_space_write_4(iot, ioh, LCDC_LCCR3,
    339 		    (tmp & ~LCCR3_BPP) | (val << LCCR3_BPP_SHIFT));
    340 	}
    341 
    342 	bus_space_write_4(iot, ioh, LCDC_FDADR0,
    343 	    scr->depth >= 16 ? scr->dma_desc_pa :
    344 	    scr->dma_desc_pa + 2 * sizeof (struct lcd_dma_descriptor));
    345 	bus_space_write_4(iot, ioh, LCDC_FDADR1,
    346 	    scr->dma_desc_pa + 1 * sizeof (struct lcd_dma_descriptor));
    347 
    348 	/* clear status */
    349 	bus_space_write_4(iot, ioh, LCDC_LCSR, 0);
    350 
    351 	delay(1000);			/* ??? */
    352 
    353 	/* Enable LCDC */
    354 	tmp = bus_space_read_4(iot, ioh, LCDC_LCCR0);
    355 	/*tmp &= ~LCCR0_SFM;*/
    356 	bus_space_write_4(iot, ioh, LCDC_LCCR0, tmp | LCCR0_ENB);
    357 
    358 	restore_interrupts(save);
    359 }
    360 
    361 #if NWSDISPLAY > 0
    362 /*
    363  * Disable screen refresh.
    364  */
    365 static void
    366 pxa2x0_lcd_stop_dma(struct pxa2x0_lcd_softc *sc)
    367 {
    368 
    369 	/* Stop LCD DMA after current frame */
    370 	bus_space_write_4(sc->iot, sc->ioh, LCDC_LCCR0,
    371 	    LCCR0_DIS |
    372 	    bus_space_read_4(sc->iot, sc->ioh, LCDC_LCCR0));
    373 
    374 	/* wait for disabling done.
    375 	   XXX: use interrupt. */
    376 	while (LCCR0_ENB &
    377 	    bus_space_read_4(sc->iot, sc->ioh, LCDC_LCCR0))
    378 		continue;
    379 
    380 	bus_space_write_4(sc->iot, sc->ioh, LCDC_LCCR0,
    381 	    ~LCCR0_DIS &
    382 	    bus_space_read_4(sc->iot, sc->ioh, LCDC_LCCR0));
    383 }
    384 #endif
    385 
    386 #define _rgb(r,g,b)	(((r)<<11) | ((g)<<5) | b)
    387 #define rgb(r,g,b)	_rgb((r)>>1,g,(b)>>1)
    388 
    389 #define L	0x1f			/* low intensity */
    390 #define H	0x3f			/* hight intensity */
    391 
    392 static uint16_t basic_color_map[] = {
    393 	rgb(	0,   0,   0),		/* black */
    394 	rgb(	L,   0,   0),		/* red */
    395 	rgb(	0,   L,   0),		/* green */
    396 	rgb(	L,   L,   0),		/* brown */
    397 	rgb(	0,   0,   L),		/* blue */
    398 	rgb(	L,   0,   L),		/* magenta */
    399 	rgb(	0,   L,   L),		/* cyan */
    400 	_rgb(0x1c,0x38,0x1c),		/* white */
    401 
    402 	rgb(	L,   L,   L),		/* black */
    403 	rgb(	H,   0,   0),		/* red */
    404 	rgb(	0,   H,   0),		/* green */
    405 	rgb(	H,   H,   0),		/* brown */
    406 	rgb(	0,   0,   H),		/* blue */
    407 	rgb(	H,   0,   H),		/* magenta */
    408 	rgb(	0,   H,   H),		/* cyan */
    409 	rgb(	H,   H,   H)
    410 };
    411 
    412 #undef H
    413 #undef L
    414 
    415 static void
    416 init_pallet(uint16_t *buf, int depth)
    417 {
    418 	int i;
    419 
    420 	/* convert RGB332 to RGB565 */
    421 	switch (depth) {
    422 	case 8:
    423 	case 4:
    424 #if 0
    425 		for (i=0; i <= 255; ++i) {
    426 			buf[i] = ((9 * ((i>>5) & 0x07)) <<11) |
    427 			    ((9 * ((i>>2) & 0x07)) << 5) |
    428 			    ((21 * (i & 0x03))/2);
    429 		}
    430 #else
    431 		memcpy(buf, basic_color_map, sizeof basic_color_map);
    432 		for (i=16; i < (1<<depth); ++i)
    433 			buf[i] = 0xffff;
    434 #endif
    435 		break;
    436 	case 16:
    437 		/* pallet is not needed */
    438 		break;
    439 	default:
    440 		/* other depths are not supported */
    441 		break;
    442 	}
    443 }
    444 
    445 /*
    446  * Create and initialize a new screen buffer.
    447  */
    448 int
    449 pxa2x0_lcd_new_screen(struct pxa2x0_lcd_softc *sc, int depth,
    450      struct pxa2x0_lcd_screen **scrpp)
    451 {
    452 	bus_space_tag_t iot;
    453 	bus_space_handle_t ioh;
    454 	bus_dma_tag_t dma_tag;
    455 	const struct lcd_panel_geometry *geometry;
    456 	struct pxa2x0_lcd_screen *scr = NULL;
    457 	int width, height;
    458 	bus_size_t size;
    459 	int error, pallet_size;
    460 	int busdma_flag = (cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    461 	struct lcd_dma_descriptor *desc;
    462 	paddr_t buf_pa, desc_pa;
    463 
    464 	iot = sc->iot;
    465 	ioh = sc->ioh;
    466 	dma_tag = sc->dma_tag;
    467 	geometry = sc->geometry;
    468 
    469 	width = geometry->panel_width;
    470 	height = geometry->panel_height;
    471 	pallet_size = 0;
    472 
    473 	switch (depth) {
    474 	case 1:
    475 	case 2:
    476 	case 4:
    477 	case 8:
    478 		pallet_size = (1<<depth) * sizeof (uint16_t);
    479 		/* FALLTHROUGH */
    480 	case 16:
    481 		size = roundup(width,4)*depth/8 * height;
    482 		break;
    483 	case 18:
    484 	case 24:
    485 		size = roundup(width,4) * 4 * height;
    486 		break;
    487 	case 19:
    488 	case 25:
    489 		printf("%s: Not supported depth (%d)\n",
    490 		    sc->dev.dv_xname, depth);
    491 		return EINVAL;
    492 	default:
    493 		printf("%s: Unknown depth (%d)\n",
    494 		    sc->dev.dv_xname, depth);
    495 		return EINVAL;
    496 	}
    497 
    498 	scr = malloc(sizeof(*scr), M_DEVBUF, M_NOWAIT);
    499 	if (scr == NULL)
    500 		return ENOMEM;
    501 
    502 	memset(scr, 0, sizeof(*scr));
    503 
    504 	scr->nsegs = 0;
    505 	scr->depth = depth;
    506 	scr->buf_size = size;
    507 	scr->buf_va = NULL;
    508 	size = roundup(size,16) + 3 * sizeof (struct lcd_dma_descriptor)
    509 	    + pallet_size;
    510 
    511 	error = bus_dmamem_alloc(dma_tag, size, 16, 0, scr->segs, 1,
    512 	    &scr->nsegs, busdma_flag);
    513 
    514 	if (error || scr->nsegs != 1) {
    515 		/* XXX:
    516 		 * Actually we can handle nsegs>1 case by means
    517 		 * of multiple DMA descriptors for a panel.  It
    518 		 * will make code here a bit hairly.
    519 		 */
    520 		if (error == 0)
    521 			error = E2BIG;
    522 		goto bad;
    523 	}
    524 
    525 	error = bus_dmamem_map(dma_tag, scr->segs, scr->nsegs, size,
    526 	    (void **)&scr->buf_va, busdma_flag | BUS_DMA_COHERENT);
    527 	if (error)
    528 		goto bad;
    529 
    530 	memset(scr->buf_va, 0, scr->buf_size);
    531 
    532 	/* map memory for DMA */
    533 	error = bus_dmamap_create(dma_tag, 1024*1024*2, 1, 1024*1024*2, 0,
    534 	    busdma_flag, &scr->dma);
    535 	if (error)
    536 		goto bad;
    537 
    538 	error = bus_dmamap_load(dma_tag, scr->dma, scr->buf_va, size,
    539 	    NULL, busdma_flag);
    540 	if (error)
    541 		goto bad;
    542 
    543 	buf_pa = scr->segs[0].ds_addr;
    544 	desc_pa = buf_pa + roundup(size, PAGE_SIZE) - 3*sizeof *desc;
    545 
    546 	/* make descriptors at the top of mapped memory */
    547 	desc = (struct lcd_dma_descriptor *)(
    548 		(char *)(scr->buf_va) + roundup(size, PAGE_SIZE) -
    549 			  3*sizeof *desc);
    550 
    551 	desc[0].fdadr = desc_pa;
    552 	desc[0].fsadr = buf_pa;
    553 	desc[0].ldcmd = scr->buf_size;
    554 
    555 	if (pallet_size) {
    556 		init_pallet((uint16_t *)((char *)desc - pallet_size), depth);
    557 
    558 		desc[2].fdadr = desc_pa; /* chain to panel 0 */
    559 		desc[2].fsadr = desc_pa - pallet_size;
    560 		desc[2].ldcmd = pallet_size | LDCMD_PAL;
    561 	}
    562 
    563 	if (geometry->panel_info & LCDPANEL_DUAL) {
    564 		/* Dual panel */
    565 		desc[1].fdadr = desc_pa + sizeof *desc;
    566 		desc[1].fsadr = buf_pa + scr->buf_size/2;
    567 		desc[0].ldcmd = desc[1].ldcmd = scr->buf_size/2;
    568 
    569 	}
    570 
    571 #if 0
    572 	desc[0].ldcmd |= LDCMD_SOFINT;
    573 	desc[1].ldcmd |= LDCMD_SOFINT;
    574 #endif
    575 
    576 	scr->dma_desc = desc;
    577 	scr->dma_desc_pa = desc_pa;
    578 	scr->map_size = size;		/* used when unmap this. */
    579 
    580 	LIST_INSERT_HEAD(&sc->screens, scr, link);
    581 	sc->n_screens++;
    582 
    583 	*scrpp = scr;
    584 
    585 	return 0;
    586 
    587  bad:
    588 	if (scr) {
    589 		if (scr->buf_va)
    590 			bus_dmamem_unmap(dma_tag, scr->buf_va, size);
    591 		if (scr->nsegs)
    592 			bus_dmamem_free(dma_tag, scr->segs, scr->nsegs);
    593 		free(scr, M_DEVBUF);
    594 	}
    595 	*scrpp = NULL;
    596 	return error;
    597 }
    598 
    599 /*
    600  * Initialize rasops for a screen, as well as struct wsscreen_descr if this
    601  * is the first screen creation.
    602  */
    603 static void
    604 pxa2x0_lcd_setup_rasops(struct pxa2x0_lcd_softc *sc, struct rasops_info *rinfo,
    605     struct pxa2x0_wsscreen_descr *descr,
    606     const struct lcd_panel_geometry *geom)
    607 {
    608 
    609 	rinfo->ri_flg = descr->flags;
    610 	rinfo->ri_depth = descr->depth;
    611 	rinfo->ri_width = geom->panel_width;
    612 	rinfo->ri_height = geom->panel_height;
    613 	rinfo->ri_stride = rinfo->ri_width * rinfo->ri_depth / 8;
    614 #ifdef notyet
    615 	rinfo->ri_wsfcookie = -1;	/* XXX */
    616 #endif
    617 
    618 	/* swap B and R */
    619 	if (descr->depth == 16) {
    620 		rinfo->ri_rnum = 5;
    621 		rinfo->ri_rpos = 11;
    622 		rinfo->ri_gnum = 6;
    623 		rinfo->ri_gpos = 5;
    624 		rinfo->ri_bnum = 5;
    625 		rinfo->ri_bpos = 0;
    626 	}
    627 
    628 	if (descr->c.nrows == 0) {
    629 		/* get rasops to compute screen size the first time */
    630 		rasops_init(rinfo, 100, 100);
    631 	} else {
    632 		rasops_init(rinfo, descr->c.nrows, descr->c.ncols);
    633 	}
    634 
    635 	descr->c.nrows = rinfo->ri_rows;
    636 	descr->c.ncols = rinfo->ri_cols;
    637 	descr->c.capabilities = rinfo->ri_caps;
    638 	descr->c.textops = &rinfo->ri_ops;
    639 }
    640 
    641 /*
    642  * Power management
    643  */
    644 void
    645 pxa2x0_lcd_suspend(struct pxa2x0_lcd_softc *sc)
    646 {
    647 
    648 	if (sc->active) {
    649 		pxa2x0_lcd_stop_dma(sc);
    650 		pxa2x0_clkman_config(CKEN_LCD, 0);
    651 	}
    652 }
    653 
    654 void
    655 pxa2x0_lcd_resume(struct pxa2x0_lcd_softc *sc)
    656 {
    657 
    658 	if (sc->active) {
    659 		pxa2x0_lcd_initialize(sc, sc->geometry);
    660 		pxa2x0_lcd_start_dma(sc, sc->active);
    661 	}
    662 }
    663 
    664 void
    665 pxa2x0_lcd_power(int why, void *v)
    666 {
    667 	struct pxa2x0_lcd_softc *sc = v;
    668 
    669 	switch (why) {
    670 	case PWR_SUSPEND:
    671 	case PWR_STANDBY:
    672 		pxa2x0_lcd_suspend(sc);
    673 		break;
    674 
    675 	case PWR_RESUME:
    676 		pxa2x0_lcd_resume(sc);
    677 		break;
    678 	}
    679 }
    680 
    681 #if NWSDISPLAY > 0
    682 /*
    683  * Initialize struct wsscreen_descr based on values calculated by
    684  * raster operation subsystem.
    685  */
    686 int
    687 pxa2x0_lcd_setup_wsscreen(struct pxa2x0_wsscreen_descr *descr,
    688     const struct lcd_panel_geometry *geom,
    689     const char *fontname)
    690 {
    691 	int width = geom->panel_width;
    692 	int height = geom->panel_height;
    693 	int cookie = -1;
    694 	struct rasops_info rinfo;
    695 
    696 	memset(&rinfo, 0, sizeof rinfo);
    697 
    698 	if (fontname) {
    699 		wsfont_init();
    700 		cookie = wsfont_find(fontname, 0, 0, 0,
    701 		    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R);
    702 		if (cookie < 0 ||
    703 		    wsfont_lock(cookie, &rinfo.ri_font))
    704 			return -1;
    705 	}
    706 	else {
    707 		/* let rasops_init() choose any font */
    708 	}
    709 
    710 	/* let rasops_init calculate # of cols and rows in character */
    711 	rinfo.ri_flg = 0;
    712 	rinfo.ri_depth = descr->depth;
    713 	rinfo.ri_bits = NULL;
    714 	rinfo.ri_width = width;
    715 	rinfo.ri_height = height;
    716 	rinfo.ri_stride = width * rinfo.ri_depth / 8;
    717 #ifdef	CPU_XSCALE_PXA270
    718 	if (rinfo.ri_depth > 16)
    719 		rinfo.ri_stride = width * 4;
    720 #endif
    721 	rinfo.ri_wsfcookie = cookie;
    722 
    723 	rasops_init(&rinfo, 100, 100);
    724 
    725 	descr->c.nrows = rinfo.ri_rows;
    726 	descr->c.ncols = rinfo.ri_cols;
    727 	descr->c.capabilities = rinfo.ri_caps;
    728 
    729 	return cookie;
    730 }
    731 
    732 int
    733 pxa2x0_lcd_show_screen(void *v, void *cookie, int waitok,
    734     void (*cb)(void *, int, int), void *cbarg)
    735 {
    736 	struct pxa2x0_lcd_softc *sc = v;
    737 	struct pxa2x0_lcd_screen *scr = cookie, *old;
    738 
    739 	old = sc->active;
    740 	if (old == scr)
    741 		return 0;
    742 
    743 	if (old)
    744 		pxa2x0_lcd_stop_dma(sc);
    745 
    746 	pxa2x0_lcd_start_dma(sc, scr);
    747 
    748 	sc->active = scr;
    749 	return 0;
    750 }
    751 
    752 int
    753 pxa2x0_lcd_alloc_screen(void *v, const struct wsscreen_descr *_type,
    754     void **cookiep, int *curxp, int *curyp, long *attrp)
    755 {
    756 	struct pxa2x0_lcd_softc *sc = v;
    757 	struct pxa2x0_lcd_screen *scr;
    758 	const struct pxa2x0_wsscreen_descr *type =
    759 		(const struct pxa2x0_wsscreen_descr *)_type;
    760 	int error;
    761 
    762 	error = pxa2x0_lcd_new_screen(sc, type->depth, &scr);
    763 	if (error)
    764 		return error;
    765 
    766 	/*
    767 	 * initialize raster operation for this screen.
    768 	 */
    769 	scr->rinfo.ri_flg = 0;
    770 	scr->rinfo.ri_depth = type->depth;
    771 	scr->rinfo.ri_bits = scr->buf_va;
    772 	scr->rinfo.ri_width = sc->geometry->panel_width;
    773 	scr->rinfo.ri_height = sc->geometry->panel_height;
    774 	scr->rinfo.ri_stride = scr->rinfo.ri_width * scr->rinfo.ri_depth / 8;
    775 #ifdef CPU_XSCALE_PXA270
    776 	if (scr->rinfo.ri_depth > 16)
    777 		scr->rinfo.ri_stride = scr->rinfo.ri_width * 4;
    778 #endif
    779 	scr->rinfo.ri_wsfcookie = -1;	/* XXX */
    780 
    781 	rasops_init(&scr->rinfo, type->c.nrows, type->c.ncols);
    782 
    783 	(* scr->rinfo.ri_ops.allocattr)(&scr->rinfo, 0, 0, 0, attrp);
    784 
    785 	*cookiep = scr;
    786 	*curxp = 0;
    787 	*curyp = 0;
    788 
    789 	return 0;
    790 }
    791 
    792 void
    793 pxa2x0_lcd_free_screen(void *v, void *cookie)
    794 {
    795 	struct pxa2x0_lcd_softc *sc = v;
    796 	struct pxa2x0_lcd_screen *scr = cookie;
    797 
    798 	LIST_REMOVE(scr, link);
    799 	sc->n_screens--;
    800 	if (scr == sc->active) {
    801 		/* at first, we need to stop LCD DMA */
    802 		sc->active = NULL;
    803 
    804 		printf("lcd_free on active screen\n");
    805 
    806 		pxa2x0_lcd_stop_dma(sc);
    807 	}
    808 
    809 	if (scr->buf_va)
    810 		bus_dmamem_unmap(sc->dma_tag, scr->buf_va, scr->map_size);
    811 	if (scr->nsegs > 0)
    812 		bus_dmamem_free(sc->dma_tag, scr->segs, scr->nsegs);
    813 	free(scr, M_DEVBUF);
    814 }
    815 
    816 int
    817 pxa2x0_lcd_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    818 	struct lwp *l)
    819 {
    820 	struct pxa2x0_lcd_softc *sc = v;
    821 	struct pxa2x0_lcd_screen *scr = sc->active;  /* ??? */
    822 	struct wsdisplay_fbinfo *wsdisp_info;
    823 	uint32_t ccr0;
    824 
    825 	switch (cmd) {
    826 	case WSDISPLAYIO_GTYPE:
    827 		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
    828 		return 0;
    829 
    830 	case WSDISPLAYIO_GINFO:
    831 		wsdisp_info = (struct wsdisplay_fbinfo *)data;
    832 		wsdisp_info->height = sc->geometry->panel_height;
    833 		wsdisp_info->width = sc->geometry->panel_width;
    834 		wsdisp_info->depth = scr->depth;
    835 		wsdisp_info->cmsize = 0;
    836 		return 0;
    837 
    838 	case WSDISPLAYIO_LINEBYTES:
    839 		*(u_int *)data = scr->rinfo.ri_stride;
    840 		return 0;
    841 
    842 	case WSDISPLAYIO_GETCMAP:
    843 	case WSDISPLAYIO_PUTCMAP:
    844 		return EPASSTHROUGH;	/* XXX Colormap */
    845 
    846 	case WSDISPLAYIO_SVIDEO:
    847 		if (*(int *)data == WSDISPLAYIO_VIDEO_ON) {
    848 		  /* turn it on */
    849 		}
    850 		else {
    851 		  /* start LCD shutdown */
    852 		  /* sleep until interrupt */
    853 		}
    854 		return 0;
    855 
    856 	case WSDISPLAYIO_GVIDEO:
    857 		ccr0 = bus_space_read_4(sc->iot, sc->ioh, LCDC_LCCR0);
    858 		*(u_int *)data = (ccr0 & (LCCR0_ENB|LCCR0_DIS)) == LCCR0_ENB ?
    859 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    860 		return 0;
    861 
    862 	case WSDISPLAYIO_GCURPOS:
    863 	case WSDISPLAYIO_SCURPOS:
    864 	case WSDISPLAYIO_GCURMAX:
    865 	case WSDISPLAYIO_GCURSOR:
    866 	case WSDISPLAYIO_SCURSOR:
    867 		return EPASSTHROUGH;	/* XXX */
    868 	}
    869 
    870 	return EPASSTHROUGH;
    871 }
    872 
    873 paddr_t
    874 pxa2x0_lcd_mmap(void *v, void *vs, off_t offset, int prot)
    875 {
    876 	struct pxa2x0_lcd_softc *sc = v;
    877 	struct pxa2x0_lcd_screen *scr = sc->active;  /* ??? */
    878 
    879 	if (scr == NULL)
    880 		return -1;
    881 
    882 	if (offset < 0 ||
    883 	    offset >= scr->rinfo.ri_stride * scr->rinfo.ri_height)
    884 		return -1;
    885 
    886 	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
    887 	    offset, prot, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    888 }
    889 
    890 
    891 static void
    892 pxa2x0_lcd_cursor(void *cookie, int on, int row, int col)
    893 {
    894 	struct pxa2x0_lcd_screen *scr = cookie;
    895 
    896 	(*scr->rinfo.ri_ops.cursor)(&scr->rinfo, on, row, col);
    897 }
    898 
    899 static int
    900 pxa2x0_lcd_mapchar(void *cookie, int c, unsigned int *cp)
    901 {
    902 	struct pxa2x0_lcd_screen *scr = cookie;
    903 
    904 	return (*scr->rinfo.ri_ops.mapchar)(&scr->rinfo, c, cp);
    905 }
    906 
    907 static void
    908 pxa2x0_lcd_putchar(void *cookie, int row, int col, u_int uc, long attr)
    909 {
    910 	struct pxa2x0_lcd_screen *scr = cookie;
    911 
    912 	(*scr->rinfo.ri_ops.putchar)(&scr->rinfo, row, col, uc, attr);
    913 }
    914 
    915 static void
    916 pxa2x0_lcd_copycols(void *cookie, int row, int src, int dst, int num)
    917 {
    918 	struct pxa2x0_lcd_screen *scr = cookie;
    919 
    920 	(*scr->rinfo.ri_ops.copycols)(&scr->rinfo, row, src, dst, num);
    921 }
    922 
    923 static void
    924 pxa2x0_lcd_erasecols(void *cookie, int row, int col, int num, long attr)
    925 {
    926 	struct pxa2x0_lcd_screen *scr = cookie;
    927 
    928 	(*scr->rinfo.ri_ops.erasecols)(&scr->rinfo, row, col, num, attr);
    929 }
    930 
    931 static void
    932 pxa2x0_lcd_copyrows(void *cookie, int src, int dst, int num)
    933 {
    934 	struct pxa2x0_lcd_screen *scr = cookie;
    935 
    936 	(*scr->rinfo.ri_ops.copyrows)(&scr->rinfo, src, dst, num);
    937 }
    938 
    939 static void
    940 pxa2x0_lcd_eraserows(void *cookie, int row, int num, long attr)
    941 {
    942 	struct pxa2x0_lcd_screen *scr = cookie;
    943 
    944 	(*scr->rinfo.ri_ops.eraserows)(&scr->rinfo, row, num, attr);
    945 }
    946 
    947 static int
    948 pxa2x0_lcd_alloc_attr(void *cookie, int fg, int bg, int flg, long *attr)
    949 {
    950 	struct pxa2x0_lcd_screen *scr = cookie;
    951 
    952 	return (*scr->rinfo.ri_ops.allocattr)(&scr->rinfo, fg, bg, flg, attr);
    953 }
    954 
    955 const struct wsdisplay_emulops pxa2x0_lcd_emulops = {
    956 	pxa2x0_lcd_cursor,
    957 	pxa2x0_lcd_mapchar,
    958 	pxa2x0_lcd_putchar,
    959 	pxa2x0_lcd_copycols,
    960 	pxa2x0_lcd_erasecols,
    961 	pxa2x0_lcd_copyrows,
    962 	pxa2x0_lcd_eraserows,
    963 	pxa2x0_lcd_alloc_attr
    964 };
    965 
    966 #endif /* NWSDISPLAY > 0 */
    967