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