Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c24x0_lcd.c revision 1.3.6.1
      1  1.3.6.1  simonb /* $NetBSD: s3c24x0_lcd.c,v 1.3.6.1 2006/04/22 11:37:17 simonb Exp $ */
      2      1.1     bsh 
      3      1.1     bsh /*
      4      1.1     bsh  * Copyright (c) 2004  Genetec Corporation.  All rights reserved.
      5      1.1     bsh  * Written by Hiroyuki Bessho for Genetec Corporation.
      6      1.1     bsh  *
      7      1.1     bsh  * Redistribution and use in source and binary forms, with or without
      8      1.1     bsh  * modification, are permitted provided that the following conditions
      9      1.1     bsh  * are met:
     10      1.1     bsh  * 1. Redistributions of source code must retain the above copyright
     11      1.1     bsh  *    notice, this list of conditions and the following disclaimer.
     12      1.1     bsh  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1     bsh  *    notice, this list of conditions and the following disclaimer in the
     14      1.1     bsh  *    documentation and/or other materials provided with the distribution.
     15      1.1     bsh  * 3. The name of Genetec Corporation may not be used to endorse or
     16      1.1     bsh  *    promote products derived from this software without specific prior
     17      1.1     bsh  *    written permission.
     18      1.1     bsh  *
     19      1.1     bsh  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     20      1.1     bsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1     bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1     bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     23      1.1     bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1     bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1     bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1     bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1     bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1     bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1     bsh  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1     bsh  */
     31      1.1     bsh 
     32      1.1     bsh /*
     33      1.1     bsh  * Support S3C24[10]0's integrated LCD controller.
     34      1.1     bsh  */
     35      1.1     bsh 
     36      1.1     bsh #include <sys/cdefs.h>
     37  1.3.6.1  simonb __KERNEL_RCSID(0, "$NetBSD: s3c24x0_lcd.c,v 1.3.6.1 2006/04/22 11:37:17 simonb Exp $");
     38      1.1     bsh 
     39      1.1     bsh #include <sys/param.h>
     40      1.1     bsh #include <sys/systm.h>
     41      1.1     bsh #include <sys/conf.h>
     42      1.1     bsh #include <sys/uio.h>
     43      1.1     bsh #include <sys/malloc.h>
     44      1.1     bsh #include <sys/kernel.h>			/* for cold */
     45      1.1     bsh 
     46      1.1     bsh #include <uvm/uvm_extern.h>
     47      1.1     bsh 
     48      1.1     bsh #include <dev/cons.h>
     49      1.1     bsh #include <dev/wscons/wsconsio.h>
     50      1.1     bsh #include <dev/wscons/wsdisplayvar.h>
     51      1.1     bsh #include <dev/wscons/wscons_callbacks.h>
     52      1.1     bsh #include <dev/rasops/rasops.h>
     53      1.1     bsh #include <dev/wsfont/wsfont.h>
     54      1.1     bsh 
     55      1.1     bsh #include <machine/bus.h>
     56      1.1     bsh #include <machine/cpu.h>
     57      1.1     bsh #include <arm/cpufunc.h>
     58      1.1     bsh 
     59      1.1     bsh #include <arm/s3c2xx0/s3c24x0var.h>
     60      1.1     bsh #include <arm/s3c2xx0/s3c24x0reg.h>
     61      1.1     bsh #include <arm/s3c2xx0/s3c24x0_lcd.h>
     62      1.1     bsh 
     63      1.1     bsh #include "wsdisplay.h"
     64      1.1     bsh 
     65      1.1     bsh int lcdintr(void *);
     66      1.1     bsh static void init_palette(struct s3c24x0_lcd_softc *,
     67      1.1     bsh     struct s3c24x0_lcd_screen *);
     68      1.1     bsh 
     69      1.1     bsh #ifdef LCD_DEBUG
     70      1.1     bsh static void
     71      1.1     bsh dump_lcdcon(const char *title, bus_space_tag_t iot, bus_space_handle_t ioh)
     72      1.1     bsh {
     73      1.1     bsh 	int i;
     74      1.1     bsh 
     75      1.1     bsh 	printf("%s\n", title);
     76      1.1     bsh 	for(i=LCDC_LCDCON1; i <= LCDC_LCDSADDR3; i+=4) {
     77      1.1     bsh 		if (i%16 == 0)
     78      1.1     bsh 			printf("\n%03x: ", i);
     79      1.1     bsh 		printf("%08x ", bus_space_read_4(iot, ioh, i));
     80      1.1     bsh 	}
     81      1.1     bsh 
     82      1.1     bsh 	printf("\n");
     83      1.1     bsh }
     84      1.1     bsh 
     85      1.1     bsh void draw_test_pattern(struct s3c24x0_lcd_softc *,
     86      1.1     bsh 	    struct s3c24x0_lcd_screen *scr);
     87      1.1     bsh 
     88      1.1     bsh #endif
     89      1.1     bsh 
     90      1.1     bsh void
     91      1.1     bsh s3c24x0_set_lcd_panel_info(struct s3c24x0_lcd_softc *sc,
     92      1.1     bsh     const struct s3c24x0_lcd_panel_info *info)
     93      1.1     bsh {
     94      1.1     bsh 	bus_space_tag_t iot = sc->iot;
     95      1.1     bsh 	bus_space_handle_t ioh = sc->ioh;
     96      1.1     bsh 	uint32_t reg;
     97      1.1     bsh 	int clkval;
     98      1.1     bsh 	int tft = s3c24x0_lcd_panel_tft(info);
     99      1.1     bsh 	int hclk = s3c2xx0_softc->sc_hclk;
    100      1.1     bsh 
    101      1.1     bsh 	sc->panel_info = info;
    102      1.1     bsh 
    103      1.1     bsh 	/* Set LCDCON1. BPPMODE and ENVID are set later */
    104      1.1     bsh 	if (tft)
    105      1.1     bsh 		clkval = (hclk / info->pixel_clock  / 2) - 1;
    106      1.1     bsh 	else {
    107      1.1     bsh 		/* STN display */
    108      1.1     bsh 		clkval = max(2, hclk / info->pixel_clock / 2);
    109      1.1     bsh 	}
    110      1.1     bsh 
    111      1.1     bsh 	reg =  (info->lcdcon1 & ~LCDCON1_CLKVAL_MASK) |
    112      1.1     bsh 		(clkval << LCDCON1_CLKVAL_SHIFT);
    113      1.1     bsh 	reg &= ~LCDCON1_ENVID;
    114      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON1, reg);
    115      1.1     bsh 
    116      1.1     bsh #if 0
    117      1.1     bsh 	printf("hclk=%d pixel clock=%d, clkval = %x lcdcon1=%x\n",
    118      1.1     bsh 	    hclk, info->pixel_clock, clkval, reg);
    119      1.1     bsh #endif
    120      1.1     bsh 
    121      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON2, info->lcdcon2);
    122      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON3, info->lcdcon3);
    123      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON4, info->lcdcon4);
    124      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON5, info->lcdcon5);
    125      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LPCSEL, info->lpcsel);
    126      1.1     bsh }
    127      1.1     bsh 
    128      1.1     bsh void
    129      1.1     bsh s3c24x0_lcd_attach_sub(struct s3c24x0_lcd_softc *sc,
    130      1.1     bsh     struct s3c2xx0_attach_args *sa,
    131      1.1     bsh     const struct s3c24x0_lcd_panel_info *panel_info)
    132      1.1     bsh {
    133      1.1     bsh 	bus_space_tag_t iot = sa->sa_iot;
    134      1.1     bsh 	bus_space_handle_t ioh;
    135      1.1     bsh 	int error;
    136      1.1     bsh 
    137      1.1     bsh 	sc->n_screens = 0;
    138      1.1     bsh 	LIST_INIT(&sc->screens);
    139      1.1     bsh 
    140      1.1     bsh 	/* map controller registers */
    141      1.1     bsh 	error = bus_space_map(iot, sa->sa_addr, S3C24X0_LCDC_SIZE, 0, &ioh);
    142      1.1     bsh 	if (error) {
    143      1.1     bsh 		printf(": failed to map registers %d", error);
    144      1.1     bsh 		return;
    145      1.1     bsh 	}
    146      1.1     bsh 
    147      1.1     bsh 	sc->iot = iot;
    148      1.1     bsh 	sc->ioh = ioh;
    149      1.1     bsh 	sc->dma_tag = sa->sa_dmat;
    150      1.1     bsh 
    151      1.1     bsh #ifdef notyet
    152      1.1     bsh 	sc->ih = s3c24x0_intr_establish(sa->sa_intr, IPL_BIO, lcdintr, sc);
    153      1.1     bsh 	if (sc->ih == NULL)
    154      1.1     bsh 		printf("%s: unable to establish interrupt at irq %d",
    155      1.1     bsh 		    sc->dev.dv_xname, sa->sa_intr);
    156      1.1     bsh #endif
    157      1.1     bsh 
    158      1.1     bsh 	/* mask LCD interrupts */
    159      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDINTMSK, LCDINT_FICNT|LCDINT_FRSYN);
    160      1.1     bsh 
    161      1.1     bsh 	/* Initialize controller registers based on panel geometry*/
    162      1.1     bsh 	s3c24x0_set_lcd_panel_info(sc, panel_info);
    163      1.1     bsh 
    164      1.1     bsh 	/* XXX: enable clock to LCD controller */
    165      1.1     bsh }
    166      1.1     bsh 
    167      1.1     bsh 
    168      1.1     bsh #ifdef notyet
    169      1.1     bsh int
    170      1.1     bsh lcdintr(void *arg)
    171      1.1     bsh {
    172      1.1     bsh 	struct s3c24x0_lcd_softc *sc = arg;
    173      1.1     bsh 	bus_space_tag_t iot = sc->iot;
    174      1.1     bsh 	bus_space_handle_t ioh = sc->ioh;
    175      1.1     bsh 
    176      1.1     bsh 	static uint32_t status;
    177      1.1     bsh 
    178      1.1     bsh 	return 1;
    179      1.1     bsh }
    180      1.1     bsh #endif
    181      1.1     bsh 
    182      1.1     bsh int
    183      1.1     bsh s3c24x0_lcd_start_dma(struct s3c24x0_lcd_softc *sc,
    184      1.1     bsh     struct s3c24x0_lcd_screen *scr)
    185      1.1     bsh {
    186      1.1     bsh 	bus_space_tag_t iot = sc->iot;
    187      1.1     bsh 	bus_space_handle_t ioh = sc->ioh;
    188      1.1     bsh 	const struct s3c24x0_lcd_panel_info *info = sc->panel_info;
    189      1.1     bsh 	int tft = s3c24x0_lcd_panel_tft(info);
    190      1.1     bsh 	int dual_panel =
    191      1.1     bsh 	    (info->lcdcon1 & LCDCON1_PNRMODE_MASK) == LCDCON1_PNRMODE_DUALSTN4;
    192      1.1     bsh 	uint32_t lcdcon1, val;
    193      1.1     bsh 	paddr_t pa;
    194      1.1     bsh 	int depth = scr->depth;
    195      1.1     bsh 	int stride = scr->stride;
    196      1.1     bsh 	int panel_height = info->panel_height;
    197      1.1     bsh 	int panel_width = info->panel_width;
    198      1.1     bsh 	int offsize;
    199      1.1     bsh 
    200      1.1     bsh 	switch (depth) {
    201      1.1     bsh 	case 1: val = LCDCON1_BPPMODE_STN1; break;
    202      1.1     bsh 	case 2: val = LCDCON1_BPPMODE_STN2; break;
    203      1.1     bsh 	case 4: val = LCDCON1_BPPMODE_STN4; break;
    204      1.1     bsh 	case 8: val = LCDCON1_BPPMODE_STN8; break;
    205      1.1     bsh 	case 12:
    206      1.1     bsh 		if (tft)
    207      1.1     bsh 			return -1;
    208      1.1     bsh 		val = LCDCON1_BPPMODE_STN12;
    209      1.1     bsh 		break;
    210      1.1     bsh 	case 16:
    211      1.1     bsh 		if (!tft)
    212      1.1     bsh 			return -1;
    213      1.1     bsh 		val = LCDCON1_BPPMODE_TFT16;
    214      1.1     bsh 		break;
    215      1.1     bsh 	case 24:
    216      1.1     bsh 		if (!tft)
    217      1.1     bsh 			return -1;
    218      1.1     bsh 		val = LCDCON1_BPPMODE_TFT24;
    219      1.1     bsh 		break;
    220      1.1     bsh 	default:
    221      1.1     bsh 		return -1;
    222      1.1     bsh 	}
    223      1.1     bsh 
    224      1.1     bsh 	if (tft)
    225      1.1     bsh 		val |= LCDCON1_BPPMODE_TFTX;
    226      1.1     bsh 
    227      1.1     bsh 	lcdcon1 = bus_space_read_4(iot, ioh, LCDC_LCDCON1);
    228      1.1     bsh 	lcdcon1 &= ~(LCDCON1_BPPMODE_MASK|LCDCON1_ENVID);
    229      1.1     bsh 	lcdcon1 |= val;
    230      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON1, lcdcon1);
    231      1.1     bsh 
    232      1.1     bsh 	/* Adjust LCDCON3.HOZVAL to meet with restriction */
    233      1.1     bsh 	val = roundup(panel_width, 16 / depth);
    234      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON3,
    235      1.1     bsh 	    (info->lcdcon3 & ~LCDCON3_HOZVAL_MASK) |
    236      1.1     bsh 	    (val - 1) << LCDCON3_HOZVAL_SHIFT);
    237      1.1     bsh 
    238      1.1     bsh 	pa = scr->segs[0].ds_addr;
    239      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDSADDR1, pa >> 1);
    240      1.1     bsh 
    241      1.1     bsh 	if (dual_panel) {
    242      1.1     bsh 		/* XXX */
    243      1.1     bsh 	}
    244      1.1     bsh 	else {
    245      1.1     bsh 		pa += stride * panel_height;
    246      1.1     bsh 		bus_space_write_4(iot, ioh, LCDC_LCDSADDR2, pa >> 1);
    247      1.1     bsh 	}
    248      1.1     bsh 
    249      1.1     bsh 	offsize = stride / sizeof (uint16_t) - (panel_width * depth / 16);
    250      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDSADDR3,
    251      1.1     bsh 	    (offsize << LCDSADDR3_OFFSIZE_SHIFT) |
    252      1.1     bsh 	    (panel_width * depth / 16));
    253      1.1     bsh 
    254      1.1     bsh 	/* set byte- or halfword- swap based on the depth */
    255      1.1     bsh 	val = bus_space_read_4(iot, ioh, LCDC_LCDCON5);
    256      1.1     bsh 	val &= ~(LCDCON5_BSWP|LCDCON5_HWSWP);
    257      1.1     bsh 	switch(depth) {
    258      1.1     bsh 	case 2:
    259      1.1     bsh 	case 4:
    260      1.1     bsh 	case 8:
    261      1.1     bsh 		val |= LCDCON5_BSWP;
    262      1.1     bsh 		break;
    263      1.1     bsh 	case 16:
    264      1.1     bsh 		val |= LCDCON5_HWSWP;
    265      1.1     bsh 		break;
    266      1.1     bsh 	}
    267      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON5, val);
    268      1.1     bsh 
    269      1.1     bsh 
    270      1.1     bsh 	init_palette(sc, scr);
    271      1.1     bsh 
    272      1.1     bsh #if 0
    273      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_TPAL, TPAL_TPALEN|
    274      1.1     bsh 	    (0xff<<TPAL_BLUE_SHIFT));
    275      1.1     bsh #endif
    276      1.1     bsh 
    277      1.1     bsh 	/* Enable LCDC */
    278      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON1, lcdcon1 | LCDCON1_ENVID);
    279      1.1     bsh 
    280      1.1     bsh 	sc->lcd_on = 1;
    281      1.1     bsh 
    282      1.1     bsh #ifdef LCD_DEBUG
    283      1.1     bsh 	dump_lcdcon(__FUNCTION__, iot, ioh);
    284      1.1     bsh #endif
    285      1.1     bsh 
    286      1.1     bsh 	return 0;
    287      1.1     bsh }
    288      1.1     bsh 
    289      1.1     bsh void
    290      1.1     bsh s3c24x0_lcd_power(struct s3c24x0_lcd_softc *sc, int on)
    291      1.1     bsh {
    292      1.1     bsh 	bus_space_tag_t iot = sc->iot;
    293      1.1     bsh 	bus_space_handle_t ioh = sc->ioh;
    294      1.1     bsh 	uint32_t reg;
    295      1.1     bsh 
    296      1.1     bsh 	reg = bus_space_read_4(iot, ioh, LCDC_LCDCON5);
    297      1.1     bsh 
    298      1.1     bsh 	if (on)
    299      1.1     bsh 		reg |= LCDCON5_PWREN;
    300      1.1     bsh 	else
    301      1.1     bsh 		reg &= ~LCDCON5_PWREN;
    302      1.1     bsh 
    303      1.1     bsh 	bus_space_write_4(iot, ioh, LCDC_LCDCON5, reg);
    304      1.1     bsh }
    305      1.1     bsh 
    306      1.1     bsh struct s3c24x0_lcd_screen *
    307      1.1     bsh s3c24x0_lcd_new_screen(struct s3c24x0_lcd_softc *sc,
    308      1.1     bsh     int virtual_width, int virtual_height, int depth)
    309      1.1     bsh {
    310      1.1     bsh 	struct s3c24x0_lcd_screen *scr = NULL;
    311      1.1     bsh 	int width, height;
    312      1.1     bsh 	bus_size_t size;
    313      1.1     bsh         int error, pallet_size;
    314      1.1     bsh 	int busdma_flag = (cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
    315      1.1     bsh 	    BUS_DMA_WRITE;
    316      1.1     bsh 	paddr_t align;
    317      1.1     bsh 	const struct s3c24x0_lcd_panel_info *panel_info = sc->panel_info;
    318      1.1     bsh 
    319      1.1     bsh 
    320      1.1     bsh #ifdef DIAGNOSTIC
    321      1.1     bsh 	if (size > 1 << 22) {
    322      1.1     bsh 		aprint_error("%s: too big screen size\n", sc->dev.dv_xname);
    323      1.1     bsh 		return NULL;
    324      1.1     bsh 	}
    325      1.1     bsh #endif
    326      1.1     bsh 
    327      1.1     bsh 	width = panel_info->panel_width;
    328      1.1     bsh 	height = panel_info->panel_height;
    329      1.1     bsh 	pallet_size = 0;
    330      1.1     bsh 
    331      1.1     bsh 	switch (depth) {
    332      1.1     bsh 	case 1: case 2: case 4: case 8:
    333      1.1     bsh 		virtual_width = roundup(virtual_width, 16 / depth);
    334      1.1     bsh 		break;
    335      1.1     bsh 	case 16:
    336      1.1     bsh 		break;
    337      1.1     bsh 	case 12: case 24:
    338      1.1     bsh 	default:
    339      1.1     bsh 		aprint_error("%s: Unknown depth (%d)\n",
    340      1.1     bsh 		    sc->dev.dv_xname, depth);
    341      1.1     bsh 		return NULL;
    342      1.1     bsh 	}
    343      1.1     bsh 
    344      1.1     bsh 	scr = malloc(sizeof *scr, M_DEVBUF,
    345      1.1     bsh 	    M_ZERO | (cold ? M_NOWAIT : M_WAITOK));
    346      1.1     bsh 
    347      1.1     bsh 	if (scr == NULL)
    348      1.1     bsh 		return NULL;
    349      1.1     bsh 
    350      1.1     bsh 	scr->nsegs = 0;
    351      1.1     bsh 	scr->depth = depth;
    352      1.1     bsh 	scr->stride = virtual_width * depth / 8;
    353      1.1     bsh 	scr->buf_size = size = scr->stride * virtual_height;
    354      1.1     bsh 	scr->buf_va = NULL;
    355      1.1     bsh 
    356      1.1     bsh 	/* calculate the alignment for LCD frame buffer.
    357      1.1     bsh 	   the buffer can't across 4MB boundary */
    358      1.1     bsh 	align = 1 << 20;
    359      1.1     bsh 	while (align < size)
    360      1.1     bsh 		align <<= 1;
    361      1.1     bsh 
    362      1.1     bsh         error = bus_dmamem_alloc(sc->dma_tag, size, align, 0,
    363      1.1     bsh 	    scr->segs, 1, &(scr->nsegs), busdma_flag);
    364      1.1     bsh 
    365      1.1     bsh         if (error || scr->nsegs != 1)
    366      1.1     bsh 		goto bad;
    367      1.1     bsh 
    368      1.1     bsh 	error = bus_dmamem_map(sc->dma_tag, scr->segs, scr->nsegs,
    369      1.1     bsh 	    size, (caddr_t *)&(scr->buf_va), busdma_flag | BUS_DMA_COHERENT);
    370      1.1     bsh 	if (error)
    371      1.1     bsh 		goto bad;
    372      1.1     bsh 
    373      1.1     bsh 
    374      1.1     bsh 	memset (scr->buf_va, 0, scr->buf_size);
    375      1.1     bsh 
    376      1.1     bsh 	/* map memory for DMA */
    377      1.1     bsh 	if (bus_dmamap_create(sc->dma_tag, 1024*1024*2, 1,
    378      1.1     bsh 	    1024*1024*2, 0,  busdma_flag, &scr->dma))
    379      1.1     bsh 		goto bad;
    380      1.1     bsh 	error = bus_dmamap_load(sc->dma_tag, scr->dma,
    381      1.1     bsh 	    scr->buf_va, size, NULL, busdma_flag);
    382      1.1     bsh 	if (error)
    383      1.1     bsh 		goto bad;
    384      1.1     bsh 
    385      1.1     bsh 	LIST_INSERT_HEAD(&(sc->screens), scr, link);
    386      1.1     bsh 	sc->n_screens++;
    387      1.1     bsh 
    388      1.1     bsh #ifdef LCD_DEBUG
    389      1.1     bsh 	draw_test_pattern(sc, scr);
    390      1.1     bsh 	dump_lcdcon(__FUNCTION__, sc->iot, sc->ioh);
    391      1.1     bsh #endif
    392      1.1     bsh 	return scr;
    393      1.1     bsh 
    394      1.1     bsh  bad:
    395      1.1     bsh 	if (scr) {
    396      1.1     bsh 		if (scr->buf_va)
    397      1.1     bsh 			bus_dmamem_unmap(sc->dma_tag, scr->buf_va, size);
    398      1.1     bsh 		if (scr->nsegs)
    399      1.1     bsh 			bus_dmamem_free(sc->dma_tag, scr->segs, scr->nsegs);
    400      1.1     bsh 		free(scr, M_DEVBUF);
    401      1.1     bsh 	}
    402      1.1     bsh 	return NULL;
    403      1.1     bsh }
    404      1.1     bsh 
    405      1.1     bsh 
    406      1.1     bsh #define _rgb(r,g,b)	(((r)<<11) | ((g)<<5) | b)
    407      1.1     bsh #define rgb(r,g,b)	_rgb((r)>>1,g,(b)>>1)
    408      1.1     bsh 
    409      1.1     bsh #define L	0x30			/* low intensity */
    410      1.1     bsh #define H	0x3f			/* hight intensity */
    411      1.1     bsh 
    412      1.1     bsh static const uint16_t basic_color_map[] = {
    413      1.1     bsh 	rgb(	0,   0,   0),		/* black */
    414      1.1     bsh 	rgb(	L,   0,   0),		/* red */
    415      1.1     bsh 	rgb(	0,   L,   0),		/* green */
    416      1.1     bsh 	rgb(	L,   L,   0),		/* brown */
    417      1.1     bsh 	rgb(	0,   0,   L),		/* blue */
    418      1.1     bsh 	rgb(	L,   0,   L),		/* magenta */
    419      1.1     bsh 	rgb(	0,   L,   L),		/* cyan */
    420      1.1     bsh 	_rgb(0x1c,0x38,0x1c),		/* white */
    421      1.1     bsh 
    422      1.1     bsh 	rgb(	L,   L,   L),		/* black */
    423      1.1     bsh 	rgb(	H,   0,   0),		/* red */
    424      1.1     bsh 	rgb(	0,   H,   0),		/* green */
    425      1.1     bsh 	rgb(	H,   H,   0),		/* brown */
    426      1.1     bsh 	rgb(	0,   0,   H),		/* blue */
    427      1.1     bsh 	rgb(	H,   0,   H),		/* magenta */
    428      1.1     bsh 	rgb(	0,   H,   H),		/* cyan */
    429      1.1     bsh 	rgb(	H,   H,   H),		/* white */
    430      1.1     bsh };
    431      1.1     bsh 
    432      1.1     bsh #define COLORMAP_LEN (sizeof basic_color_map / sizeof basic_color_map[0])
    433      1.1     bsh 
    434      1.1     bsh #undef H
    435      1.1     bsh #undef L
    436      1.1     bsh 
    437      1.1     bsh static void
    438      1.1     bsh init_palette(struct s3c24x0_lcd_softc *sc, struct s3c24x0_lcd_screen *scr)
    439      1.1     bsh {
    440      1.1     bsh 	int depth = scr->depth;
    441      1.1     bsh 	bus_space_tag_t iot = sc->iot;
    442      1.1     bsh 	bus_space_handle_t ioh = sc->ioh;
    443      1.1     bsh 	int i;
    444      1.1     bsh 
    445      1.1     bsh 	i = 0;
    446      1.1     bsh 
    447      1.1     bsh 	switch(depth) {
    448      1.1     bsh 	default:
    449      1.1     bsh 	case 16:		/* not using palette */
    450      1.1     bsh 		return;
    451      1.1     bsh 	case 8:
    452      1.1     bsh 		while (i < COLORMAP_LEN) {
    453      1.1     bsh 			bus_space_write_4(iot, ioh, LCDC_PALETTE + 4*i,
    454      1.1     bsh 			    basic_color_map[i]);
    455      1.1     bsh 			++i;
    456      1.1     bsh 		}
    457      1.1     bsh 		break;
    458      1.1     bsh 	case 4:
    459      1.1     bsh 	case 2:
    460      1.1     bsh 		/* XXX */
    461      1.1     bsh 		break;
    462      1.1     bsh 	case 1:
    463      1.1     bsh 		bus_space_write_4(iot, ioh, LCDC_PALETTE + 4 * i,
    464      1.1     bsh 		    basic_color_map[i]); /* black */
    465      1.1     bsh 		++i;
    466      1.1     bsh 		bus_space_write_4(iot, ioh, LCDC_PALETTE + 4 * i,
    467      1.1     bsh 		    basic_color_map[7]); /* white */
    468      1.1     bsh 		break;
    469      1.1     bsh 	}
    470      1.1     bsh 
    471      1.1     bsh #ifdef DIAGNOSTIC
    472      1.1     bsh 	/* Fill unused entries */
    473      1.1     bsh 	for ( ; i < 256; ++i )
    474      1.1     bsh 		bus_space_write_4(iot, ioh, LCDC_PALETTE + 4 * i,
    475      1.1     bsh 		    basic_color_map[1]); /* red */
    476      1.1     bsh #endif
    477      1.1     bsh }
    478      1.1     bsh 
    479      1.1     bsh 
    480      1.1     bsh #if NWSDISPLAY > 0
    481      1.1     bsh 
    482      1.1     bsh static void
    483      1.1     bsh s3c24x0_lcd_stop_dma(struct s3c24x0_lcd_softc *sc)
    484      1.1     bsh {
    485      1.1     bsh 	/* Stop LCD output */
    486      1.1     bsh 	bus_space_write_4(sc->iot, sc->ioh, LCDC_LCDCON1,
    487      1.1     bsh 	    ~LCDCON1_ENVID &
    488      1.1     bsh 	    bus_space_read_4(sc->iot, sc->ioh, LCDC_LCDCON1));
    489      1.1     bsh 
    490      1.1     bsh 
    491      1.1     bsh 	sc->lcd_on = 0;
    492      1.1     bsh }
    493      1.1     bsh 
    494      1.1     bsh int
    495      1.1     bsh s3c24x0_lcd_show_screen(void *v, void *cookie, int waitok,
    496      1.1     bsh     void (*cb)(void *, int, int), void *cbarg)
    497      1.1     bsh {
    498      1.1     bsh 	struct s3c24x0_lcd_softc *sc = v;
    499      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie, *old;
    500      1.1     bsh 
    501      1.1     bsh 	/* XXX: make sure the clock is provided for LCD controller */
    502      1.1     bsh 
    503      1.1     bsh 	old = sc->active;
    504      1.1     bsh 	if (old == scr && sc->lcd_on)
    505      1.1     bsh 		return 0;
    506      1.1     bsh 
    507      1.1     bsh 	if (old)
    508      1.1     bsh 		s3c24x0_lcd_stop_dma(sc);
    509      1.1     bsh 
    510      1.1     bsh 	s3c24x0_lcd_start_dma(sc, scr);
    511      1.1     bsh 	sc->active = scr;
    512      1.1     bsh 	s3c24x0_lcd_power(sc, 1);
    513      1.1     bsh 
    514      1.1     bsh 	/* XXX: callback */
    515      1.1     bsh 
    516      1.1     bsh 	return 0;
    517      1.1     bsh }
    518      1.1     bsh 
    519      1.1     bsh int
    520      1.1     bsh s3c24x0_lcd_alloc_screen(void *v, const struct wsscreen_descr *_type,
    521      1.1     bsh     void **cookiep, int *curxp, int *curyp, long *attrp)
    522      1.1     bsh {
    523      1.1     bsh 	struct s3c24x0_lcd_softc *sc = v;
    524      1.1     bsh 	struct s3c24x0_lcd_screen *scr;
    525      1.2      he 	const struct s3c24x0_wsscreen_descr *type =
    526      1.2      he 	    (const struct s3c24x0_wsscreen_descr *)_type;
    527      1.1     bsh 
    528      1.1     bsh 	int width, height;
    529      1.1     bsh 
    530      1.1     bsh 	width = type->c.ncols * type->c.fontwidth;
    531      1.1     bsh 	height = type->c.nrows * type->c.fontwidth;
    532      1.1     bsh 
    533      1.1     bsh 	if (width < sc->panel_info->panel_width)
    534      1.1     bsh 		width =   sc->panel_info->panel_width;
    535      1.1     bsh 	if (height < sc->panel_info->panel_height)
    536      1.1     bsh 		height =   sc->panel_info->panel_height;
    537      1.1     bsh 
    538      1.1     bsh 
    539      1.1     bsh 	scr = s3c24x0_lcd_new_screen(sc, width, height, type->depth);
    540      1.1     bsh 	if (scr == NULL)
    541      1.1     bsh 		return -1;
    542      1.1     bsh 
    543      1.1     bsh 	/*
    544      1.1     bsh 	 * initialize raster operation for this screen.
    545      1.1     bsh 	 */
    546      1.1     bsh 	scr->rinfo.ri_flg = 0;
    547      1.1     bsh 	scr->rinfo.ri_depth = type->depth;
    548      1.1     bsh 	scr->rinfo.ri_bits = scr->buf_va;
    549      1.1     bsh 	scr->rinfo.ri_width = width;
    550      1.1     bsh 	scr->rinfo.ri_height = height;
    551      1.1     bsh 	scr->rinfo.ri_stride = scr->stride;
    552      1.1     bsh 
    553      1.1     bsh 	if (type->c.fontwidth || type->c.fontheight) {
    554      1.1     bsh 		/*
    555      1.1     bsh 		 * find a font with specified size
    556      1.1     bsh 		 */
    557      1.1     bsh 		int cookie;
    558      1.1     bsh 
    559      1.1     bsh 		wsfont_init();
    560      1.1     bsh 
    561      1.1     bsh 		cookie = wsfont_find(NULL, type->c.fontwidth,
    562      1.1     bsh 		    type->c.fontheight, 0, WSDISPLAY_FONTORDER_L2R,
    563      1.1     bsh 		    WSDISPLAY_FONTORDER_L2R);
    564      1.1     bsh 
    565      1.1     bsh 		if (cookie > 0) {
    566      1.1     bsh 			if (wsfont_lock(cookie, &scr->rinfo.ri_font))
    567      1.1     bsh 				scr->rinfo.ri_wsfcookie = cookie;
    568      1.1     bsh 		}
    569      1.1     bsh 	}
    570      1.1     bsh 
    571      1.1     bsh 	rasops_init(&scr->rinfo, type->c.nrows, type->c.ncols);
    572      1.1     bsh 
    573      1.1     bsh 	(* scr->rinfo.ri_ops.allocattr)(&scr->rinfo, 0, 0, 0, attrp);
    574      1.1     bsh 
    575      1.1     bsh 	if (type->c.nrows != scr->rinfo.ri_rows ||
    576      1.1     bsh 	    type->c.ncols != scr->rinfo.ri_cols) {
    577      1.1     bsh 
    578      1.1     bsh 		aprint_error("%s: can't allocate a screen with requested size:"
    579      1.1     bsh 		    "%d x %d -> %d x %d\n",
    580      1.1     bsh 		    sc->dev.dv_xname,
    581      1.1     bsh 		    type->c.ncols, type->c.nrows,
    582      1.1     bsh 		    scr->rinfo.ri_cols, scr->rinfo.ri_rows);
    583      1.1     bsh 	}
    584      1.1     bsh 
    585      1.1     bsh 	*cookiep = scr;
    586      1.1     bsh 	*curxp = 0;
    587      1.1     bsh 	*curyp = 0;
    588      1.1     bsh 
    589      1.1     bsh 	return 0;
    590      1.1     bsh }
    591      1.1     bsh 
    592      1.1     bsh 
    593      1.1     bsh void
    594      1.1     bsh s3c24x0_lcd_free_screen(void *v, void *cookie)
    595      1.1     bsh {
    596      1.1     bsh 	struct s3c24x0_lcd_softc *sc = v;
    597      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    598      1.1     bsh 
    599      1.1     bsh 	LIST_REMOVE(scr, link);
    600      1.1     bsh 	sc->n_screens--;
    601      1.1     bsh 	if (scr == sc->active) {
    602      1.1     bsh 		sc->active = NULL;
    603      1.1     bsh 
    604      1.1     bsh 		/* XXX: We need a good procedure to shutdown the LCD. */
    605      1.1     bsh 
    606      1.1     bsh 		s3c24x0_lcd_stop_dma(sc);
    607      1.1     bsh 		s3c24x0_lcd_power(sc, 0);
    608      1.1     bsh 	}
    609      1.1     bsh 
    610      1.1     bsh 	if (scr->buf_va)
    611      1.1     bsh 		bus_dmamem_unmap(sc->dma_tag, scr->buf_va, scr->map_size);
    612      1.1     bsh 
    613      1.1     bsh 	if (scr->nsegs > 0)
    614      1.1     bsh 		bus_dmamem_free(sc->dma_tag, scr->segs, scr->nsegs);
    615      1.1     bsh 
    616      1.1     bsh 	free(scr, M_DEVBUF);
    617      1.1     bsh }
    618      1.1     bsh 
    619      1.1     bsh int
    620  1.3.6.1  simonb s3c24x0_lcd_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
    621  1.3.6.1  simonb     struct lwp *l)
    622      1.1     bsh {
    623      1.1     bsh 	struct s3c24x0_lcd_softc *sc = v;
    624      1.1     bsh 	struct wsdisplay_fbinfo *wsdisp_info;
    625      1.1     bsh 	struct s3c24x0_lcd_screen *scr;
    626      1.1     bsh 
    627      1.1     bsh 
    628      1.1     bsh 	switch (cmd) {
    629      1.1     bsh 	case WSDISPLAYIO_GTYPE:
    630      1.1     bsh 		*(u_int *)data = WSDISPLAY_TYPE_UNKNOWN; /* XXX */
    631      1.1     bsh 		return 0;
    632      1.1     bsh 
    633      1.1     bsh 	case WSDISPLAYIO_GINFO:
    634      1.1     bsh 		wsdisp_info = (struct wsdisplay_fbinfo *)data;
    635      1.1     bsh 
    636      1.1     bsh 		wsdisp_info->height = sc->panel_info->panel_height;
    637      1.1     bsh 		wsdisp_info->width = sc->panel_info->panel_width;
    638      1.1     bsh 		wsdisp_info->depth = 16; /* XXX */
    639      1.1     bsh 		wsdisp_info->cmsize = 0;
    640      1.1     bsh 		return 0;
    641      1.1     bsh 
    642      1.1     bsh 	case WSDISPLAYIO_GETCMAP:
    643      1.1     bsh 	case WSDISPLAYIO_PUTCMAP:
    644      1.1     bsh 		return EPASSTHROUGH;	/* XXX Colormap */
    645      1.1     bsh 
    646      1.1     bsh 	case WSDISPLAYIO_SVIDEO:
    647      1.1     bsh 		if (*(int *)data == WSDISPLAYIO_VIDEO_ON) {
    648      1.1     bsh 			scr = sc->active;
    649      1.1     bsh 			if (scr == NULL)
    650      1.1     bsh 				scr = LIST_FIRST(&sc->screens);
    651      1.1     bsh 
    652      1.1     bsh 			if (scr == NULL)
    653      1.1     bsh 				return ENXIO;
    654      1.1     bsh 
    655      1.1     bsh 			s3c24x0_lcd_show_screen(sc, scr, 1, NULL, NULL);
    656      1.1     bsh 		}
    657      1.1     bsh 		else {
    658      1.1     bsh 			s3c24x0_lcd_stop_dma(sc);
    659      1.1     bsh 			s3c24x0_lcd_power(sc, 0);
    660      1.1     bsh 		}
    661      1.1     bsh 		return 0;
    662      1.1     bsh 
    663      1.1     bsh 	case WSDISPLAYIO_GVIDEO:
    664      1.1     bsh 		*(u_int *)data = sc->lcd_on;
    665      1.1     bsh 		return 0;
    666      1.1     bsh 
    667      1.1     bsh 	case WSDISPLAYIO_GCURPOS:
    668      1.1     bsh 	case WSDISPLAYIO_SCURPOS:
    669      1.1     bsh 	case WSDISPLAYIO_GCURMAX:
    670      1.1     bsh 	case WSDISPLAYIO_GCURSOR:
    671      1.1     bsh 	case WSDISPLAYIO_SCURSOR:
    672      1.1     bsh 		return EPASSTHROUGH;	/* XXX */
    673      1.1     bsh 	}
    674      1.1     bsh 
    675      1.1     bsh 	return EPASSTHROUGH;
    676      1.1     bsh }
    677      1.1     bsh 
    678      1.1     bsh paddr_t
    679  1.3.6.1  simonb s3c24x0_lcd_mmap(void *v, void *vs, off_t offset, int prot)
    680      1.1     bsh {
    681      1.1     bsh 	struct s3c24x0_lcd_softc *sc = v;
    682      1.1     bsh 	struct s3c24x0_lcd_screen *screen = sc->active;  /* ??? */
    683      1.1     bsh 
    684      1.1     bsh 	if (screen == NULL)
    685      1.1     bsh 		return -1;
    686      1.1     bsh 
    687      1.1     bsh 	return bus_dmamem_mmap(sc->dma_tag, screen->segs, screen->nsegs,
    688      1.1     bsh 	    offset, prot, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    689      1.1     bsh 	return -1;
    690      1.1     bsh }
    691      1.1     bsh 
    692      1.1     bsh 
    693      1.1     bsh static void
    694      1.1     bsh s3c24x0_lcd_cursor(void *cookie, int on, int row, int col)
    695      1.1     bsh {
    696      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    697      1.1     bsh 
    698      1.1     bsh 	(* scr->rinfo.ri_ops.cursor)(&scr->rinfo, on, row, col);
    699      1.1     bsh }
    700      1.1     bsh 
    701      1.1     bsh static int
    702      1.1     bsh s3c24x0_lcd_mapchar(void *cookie, int c, unsigned int *cp)
    703      1.1     bsh {
    704      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    705      1.1     bsh 
    706      1.1     bsh 	return (* scr->rinfo.ri_ops.mapchar)(&scr->rinfo, c, cp);
    707      1.1     bsh }
    708      1.1     bsh 
    709      1.1     bsh static void
    710      1.1     bsh s3c24x0_lcd_putchar(void *cookie, int row, int col, u_int uc, long attr)
    711      1.1     bsh {
    712      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    713      1.1     bsh 
    714      1.1     bsh 	(* scr->rinfo.ri_ops.putchar)(&scr->rinfo,
    715      1.1     bsh 	    row, col, uc, attr);
    716      1.1     bsh }
    717      1.1     bsh 
    718      1.1     bsh static void
    719      1.1     bsh s3c24x0_lcd_copycols(void *cookie, int row, int src, int dst, int num)
    720      1.1     bsh {
    721      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    722      1.1     bsh 
    723      1.1     bsh 	(* scr->rinfo.ri_ops.copycols)(&scr->rinfo,
    724      1.1     bsh 	    row, src, dst, num);
    725      1.1     bsh }
    726      1.1     bsh 
    727      1.1     bsh static void
    728      1.1     bsh s3c24x0_lcd_erasecols(void *cookie, int row, int col, int num, long attr)
    729      1.1     bsh {
    730      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    731      1.1     bsh 
    732      1.1     bsh 	(* scr->rinfo.ri_ops.erasecols)(&scr->rinfo,
    733      1.1     bsh 	    row, col, num, attr);
    734      1.1     bsh }
    735      1.1     bsh 
    736      1.1     bsh static void
    737      1.1     bsh s3c24x0_lcd_copyrows(void *cookie, int src, int dst, int num)
    738      1.1     bsh {
    739      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    740      1.1     bsh 
    741      1.1     bsh 	(* scr->rinfo.ri_ops.copyrows)(&scr->rinfo,
    742      1.1     bsh 	    src, dst, num);
    743      1.1     bsh }
    744      1.1     bsh 
    745      1.1     bsh static void
    746      1.1     bsh s3c24x0_lcd_eraserows(void *cookie, int row, int num, long attr)
    747      1.1     bsh {
    748      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    749      1.1     bsh 
    750      1.1     bsh 	(* scr->rinfo.ri_ops.eraserows)(&scr->rinfo,
    751      1.1     bsh 	    row, num, attr);
    752      1.1     bsh }
    753      1.1     bsh 
    754      1.1     bsh static int
    755      1.1     bsh s3c24x0_lcd_alloc_attr(void *cookie, int fg, int bg, int flg, long *attr)
    756      1.1     bsh {
    757      1.1     bsh 	struct s3c24x0_lcd_screen *scr = cookie;
    758      1.1     bsh 
    759      1.1     bsh 	return (* scr->rinfo.ri_ops.allocattr)(&scr->rinfo,
    760      1.1     bsh 	    fg, bg, flg, attr);
    761      1.1     bsh }
    762      1.1     bsh 
    763      1.1     bsh 
    764      1.1     bsh const struct wsdisplay_emulops s3c24x0_lcd_emulops = {
    765      1.1     bsh 	s3c24x0_lcd_cursor,
    766      1.1     bsh 	s3c24x0_lcd_mapchar,
    767      1.1     bsh 	s3c24x0_lcd_putchar,
    768      1.1     bsh 	s3c24x0_lcd_copycols,
    769      1.1     bsh 	s3c24x0_lcd_erasecols,
    770      1.1     bsh 	s3c24x0_lcd_copyrows,
    771      1.1     bsh 	s3c24x0_lcd_eraserows,
    772      1.1     bsh 	s3c24x0_lcd_alloc_attr
    773      1.1     bsh };
    774      1.1     bsh 
    775      1.1     bsh #endif /* NWSDISPLAY > 0 */
    776