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