tx3912video.c revision 1.38       1 /*	$NetBSD: tx3912video.c,v 1.38 2007/03/04 05:59:54 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999-2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: tx3912video.c,v 1.38 2007/03/04 05:59:54 christos Exp $");
     41 
     42 #define TX3912VIDEO_DEBUG
     43 
     44 #include "hpcfb.h"
     45 #include "bivideo.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/device.h>
     50 #include <sys/extent.h>
     51 
     52 #include <sys/ioctl.h>
     53 #include <sys/buf.h>
     54 
     55 #include <uvm/uvm_extern.h>
     56 
     57 #include <dev/cons.h> /* consdev */
     58 
     59 #include <machine/bus.h>
     60 #include <machine/bootinfo.h>
     61 #include <machine/config_hook.h>
     62 
     63 #include <hpcmips/tx/tx39var.h>
     64 #include <hpcmips/tx/tx3912videovar.h>
     65 #include <hpcmips/tx/tx3912videoreg.h>
     66 
     67 /* CLUT */
     68 #include <dev/wscons/wsdisplayvar.h>
     69 #include <dev/rasops/rasops.h>
     70 #include <dev/hpc/video_subr.h>
     71 
     72 #include <dev/wscons/wsconsio.h>
     73 #include <dev/hpc/hpcfbvar.h>
     74 #include <dev/hpc/hpcfbio.h>
     75 #if NBIVIDEO > 0
     76 #include <dev/hpc/bivideovar.h>
     77 #endif
     78 
     79 #ifdef TX3912VIDEO_DEBUG
     80 int	tx3912video_debug = 1;
     81 #define	DPRINTF(arg) if (tx3912video_debug) printf arg;
     82 #define	DPRINTFN(n, arg) if (tx3912video_debug > (n)) printf arg;
     83 #else
     84 #define	DPRINTF(arg)
     85 #define DPRINTFN(n, arg)
     86 #endif
     87 
     88 struct tx3912video_softc {
     89 	struct device sc_dev;
     90 	void *sc_powerhook;	/* power management hook */
     91 	int sc_console;
     92 	struct hpcfb_fbconf sc_fbconf;
     93 	struct hpcfb_dspconf sc_dspconf;
     94 	struct video_chip *sc_chip;
     95 };
     96 
     97 /* TX3912 built-in video chip itself */
     98 static struct video_chip tx3912video_chip;
     99 
    100 int	tx3912video_power(void *, int, long, void *);
    101 void	tx3912video_framebuffer_init(struct video_chip *);
    102 int	tx3912video_framebuffer_alloc(struct video_chip *, paddr_t, paddr_t *);
    103 void	tx3912video_reset(struct video_chip *);
    104 void	tx3912video_resolution_init(struct video_chip *);
    105 int	tx3912video_match(struct device *, struct cfdata *, void *);
    106 void	tx3912video_attach(struct device *, struct device *, void *);
    107 int	tx3912video_print(void *, const char *);
    108 
    109 void	tx3912video_hpcfbinit(struct tx3912video_softc *);
    110 int	tx3912video_ioctl(void *, u_long, void *, int, struct lwp *);
    111 paddr_t	tx3912video_mmap(void *, off_t, int);
    112 
    113 void	tx3912video_clut_init(struct tx3912video_softc *);
    114 void	tx3912video_clut_install(void *, struct rasops_info *);
    115 void	tx3912video_clut_get(struct tx3912video_softc *, u_int32_t *, int,
    116 	    int);
    117 
    118 static int __get_color8(int);
    119 static int __get_color4(int);
    120 
    121 CFATTACH_DECL(tx3912video, sizeof(struct tx3912video_softc),
    122     tx3912video_match, tx3912video_attach, NULL, NULL);
    123 
    124 struct hpcfb_accessops tx3912video_ha = {
    125 	tx3912video_ioctl, tx3912video_mmap, 0, 0, 0, 0,
    126 	tx3912video_clut_install
    127 };
    128 
    129 int
    130 tx3912video_match(struct device *parent, struct cfdata *cf, void *aux)
    131 {
    132 	return (ATTACH_NORMAL);
    133 }
    134 
    135 void
    136 tx3912video_attach(struct device *parent, struct device *self, void *aux)
    137 {
    138 	struct tx3912video_softc *sc = (void *)self;
    139 	struct video_chip *chip;
    140 	static const char *const depth_print[] = {
    141 		[TX3912_VIDEOCTRL1_BITSEL_MONOCHROME] = "monochrome",
    142 		[TX3912_VIDEOCTRL1_BITSEL_2BITGREYSCALE] = "2bit greyscale",
    143 		[TX3912_VIDEOCTRL1_BITSEL_4BITGREYSCALE] = "4bit greyscale",
    144 		[TX3912_VIDEOCTRL1_BITSEL_8BITCOLOR] = "8bit color"
    145 	};
    146 	struct hpcfb_attach_args ha;
    147 	tx_chipset_tag_t tc;
    148 	txreg_t val;
    149 	int console;
    150 
    151 	sc->sc_console = console = cn_tab ? 0 : 1;
    152 	sc->sc_chip = chip = &tx3912video_chip;
    153 
    154 	/* print video module information */
    155 	printf(": %s, frame buffer 0x%08x-0x%08x\n",
    156 	    depth_print[(ffs(chip->vc_fbdepth) - 1) & 0x3],
    157 	    (unsigned)chip->vc_fbpaddr,
    158 	    (unsigned)(chip->vc_fbpaddr + chip->vc_fbsize));
    159 
    160 	/* don't inverse VDAT[3:0] signal */
    161 	tc = chip->vc_v;
    162 	val = tx_conf_read(tc, TX3912_VIDEOCTRL1_REG);
    163 	val &= ~TX3912_VIDEOCTRL1_INVVID;
    164 	tx_conf_write(tc, TX3912_VIDEOCTRL1_REG, val);
    165 
    166 	/* install default CLUT */
    167 	tx3912video_clut_init(sc);
    168 
    169 	/* if serial console, power off video module */
    170 	tx3912video_power(sc, 0, 0, (void *)
    171 	    (console ? PWR_RESUME : PWR_SUSPEND));
    172 
    173 	/* Add a hard power hook to power saving */
    174 	sc->sc_powerhook = config_hook(CONFIG_HOOK_PMEVENT,
    175 	    CONFIG_HOOK_PMEVENT_HARDPOWER, CONFIG_HOOK_SHARE,
    176 	    tx3912video_power, sc);
    177 	if (sc->sc_powerhook == 0)
    178 		printf("WARNING unable to establish hard power hook");
    179 
    180 #ifdef TX3912VIDEO_DEBUG
    181 	/* attach debug draw routine (debugging use) */
    182 	video_attach_drawfunc(sc->sc_chip);
    183 	tx_conf_register_video(tc, sc->sc_chip);
    184 #endif
    185 
    186 	/* Attach frame buffer device */
    187 	tx3912video_hpcfbinit(sc);
    188 
    189 	if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
    190 		panic("tx3912video_attach: can't init fb console");
    191 	}
    192 
    193 	ha.ha_console = console;
    194 	ha.ha_accessops = &tx3912video_ha;
    195 	ha.ha_accessctx = sc;
    196 	ha.ha_curfbconf = 0;
    197 	ha.ha_nfbconf = 1;
    198 	ha.ha_fbconflist = &sc->sc_fbconf;
    199 	ha.ha_curdspconf = 0;
    200 	ha.ha_ndspconf = 1;
    201 	ha.ha_dspconflist = &sc->sc_dspconf;
    202 
    203 	config_found(self, &ha, hpcfbprint);
    204 #if NBIVIDEO > 0
    205 	/* bivideo is no longer need */
    206 	bivideo_dont_attach = 1;
    207 #endif /* NBIVIDEO > 0 */
    208 }
    209 
    210 int
    211 tx3912video_power(void *ctx, int type, long id, void *msg)
    212 {
    213 	struct tx3912video_softc *sc = ctx;
    214 	struct video_chip *chip = sc->sc_chip;
    215 	tx_chipset_tag_t tc = chip->vc_v;
    216 	int why = (int)msg;
    217 	txreg_t val;
    218 
    219 	switch (why) {
    220 	case PWR_RESUME:
    221 		if (!sc->sc_console)
    222 			return (0); /* serial console */
    223 
    224 		DPRINTF(("%s: ON\n", sc->sc_dev.dv_xname));
    225 		val = tx_conf_read(tc, TX3912_VIDEOCTRL1_REG);
    226 		val |= (TX3912_VIDEOCTRL1_DISPON | TX3912_VIDEOCTRL1_ENVID);
    227 		tx_conf_write(tc, TX3912_VIDEOCTRL1_REG, val);
    228 		break;
    229 	case PWR_SUSPEND:
    230 		/* FALLTHROUGH */
    231 	case PWR_STANDBY:
    232 		DPRINTF(("%s: OFF\n", sc->sc_dev.dv_xname));
    233 		val = tx_conf_read(tc, TX3912_VIDEOCTRL1_REG);
    234 		val &= ~(TX3912_VIDEOCTRL1_DISPON | TX3912_VIDEOCTRL1_ENVID);
    235 		tx_conf_write(tc, TX3912_VIDEOCTRL1_REG, val);
    236 		break;
    237 	}
    238 
    239 	return (0);
    240 }
    241 
    242 void
    243 tx3912video_hpcfbinit(sc)
    244 	struct tx3912video_softc *sc;
    245 {
    246 	struct video_chip *chip = sc->sc_chip;
    247 	struct hpcfb_fbconf *fb = &sc->sc_fbconf;
    248 	vaddr_t fbvaddr = (vaddr_t)MIPS_PHYS_TO_KSEG1(chip->vc_fbpaddr);
    249 
    250 	memset(fb, 0, sizeof(struct hpcfb_fbconf));
    251 
    252 	fb->hf_conf_index	= 0;	/* configuration index		*/
    253 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
    254 	strncpy(fb->hf_name, "TX3912 built-in video", HPCFB_MAXNAMELEN);
    255 					/* frame buffer name		*/
    256 	strncpy(fb->hf_conf_name, "LCD", HPCFB_MAXNAMELEN);
    257 					/* configuration name		*/
    258 	fb->hf_height		= chip->vc_fbheight;
    259 	fb->hf_width		= chip->vc_fbwidth;
    260 	fb->hf_baseaddr		= (u_long)fbvaddr;
    261 	fb->hf_offset		= (u_long)fbvaddr -
    262 	    mips_ptob(mips_btop(fbvaddr));
    263 					/* frame buffer start offset   	*/
    264 	fb->hf_bytes_per_line	= (chip->vc_fbwidth * chip->vc_fbdepth)
    265 	    / NBBY;
    266 	fb->hf_nplanes		= 1;
    267 	fb->hf_bytes_per_plane	= chip->vc_fbheight * fb->hf_bytes_per_line;
    268 
    269 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
    270 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
    271 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
    272 	if (video_reverse_color())
    273 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    274 
    275 
    276 	switch (chip->vc_fbdepth) {
    277 	default:
    278 		panic("tx3912video_hpcfbinit: not supported color depth");
    279 		/* NOTREACHED */
    280 	case 2:
    281 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    282 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    283 		fb->hf_pack_width = 8;
    284 		fb->hf_pixels_per_pack = 4;
    285 		fb->hf_pixel_width = 2;
    286 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    287 		/* reserved for future use */
    288 		fb->hf_u.hf_gray.hf_flags = 0;
    289 		break;
    290 	case 8:
    291 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
    292 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    293 		fb->hf_pack_width = 8;
    294 		fb->hf_pixels_per_pack = 1;
    295 		fb->hf_pixel_width = 8;
    296 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
    297 		/* reserved for future use */
    298 		fb->hf_u.hf_indexed.hf_flags = 0;
    299 		break;
    300 	}
    301 }
    302 
    303 int
    304 tx3912video_init(paddr_t fb_start, paddr_t *fb_end)
    305 {
    306 	struct video_chip *chip = &tx3912video_chip;
    307 	tx_chipset_tag_t tc;
    308 	txreg_t reg;
    309 	int fbdepth, reverse, error;
    310 
    311 	reverse = video_reverse_color();
    312 	chip->vc_v = tc = tx_conf_get_tag();
    313 
    314 	reg = tx_conf_read(tc, TX3912_VIDEOCTRL1_REG);
    315 	fbdepth = 1 << (TX3912_VIDEOCTRL1_BITSEL(reg));
    316 
    317 	switch (fbdepth) {
    318 	case 2:
    319 		bootinfo->fb_type = reverse ? BIFB_D2_M2L_3 : BIFB_D2_M2L_0;
    320 		break;
    321 	case 4:
    322 		/* XXX should implement rasops4.c */
    323 		fbdepth = 2;
    324 		bootinfo->fb_type = reverse ? BIFB_D2_M2L_3 : BIFB_D2_M2L_0;
    325 		reg = tx_conf_read(tc, TX3912_VIDEOCTRL1_REG);
    326 		TX3912_VIDEOCTRL1_BITSEL_CLR(reg);
    327 		reg = TX3912_VIDEOCTRL1_BITSEL_SET(reg,
    328 		    TX3912_VIDEOCTRL1_BITSEL_2BITGREYSCALE);
    329 		tx_conf_write(tc, TX3912_VIDEOCTRL1_REG, reg);
    330 		break;
    331 	case 8:
    332 		bootinfo->fb_type = reverse ? BIFB_D8_FF : BIFB_D8_00;
    333 		break;
    334 	}
    335 
    336 	chip->vc_fbdepth = fbdepth;
    337 	chip->vc_fbwidth = bootinfo->fb_width;
    338 	chip->vc_fbheight= bootinfo->fb_height;
    339 
    340 	/* Allocate framebuffer area */
    341 	error = tx3912video_framebuffer_alloc(chip, fb_start, fb_end);
    342 	if (error != 0)
    343 		return (1);
    344 
    345 #if notyet
    346 	tx3912video_resolution_init(chip);
    347 #else
    348 	/* Use Windows CE setting. */
    349 #endif
    350 	/* Set DMA transfer address to VID module */
    351 	tx3912video_framebuffer_init(chip);
    352 
    353 	/* Syncronize framebuffer addr to frame signal */
    354 	tx3912video_reset(chip);
    355 
    356 	bootinfo->fb_line_bytes = (chip->vc_fbwidth * fbdepth) / NBBY;
    357 	bootinfo->fb_addr = (void *)MIPS_PHYS_TO_KSEG1(chip->vc_fbpaddr);
    358 
    359 	return (0);
    360 }
    361 
    362 int
    363 tx3912video_framebuffer_alloc(struct video_chip *chip, paddr_t fb_start,
    364     paddr_t *fb_end /* buffer allocation hint */)
    365 {
    366 	struct extent_fixed ex_fixed[10];
    367 	struct extent *ex;
    368 	u_long addr, size;
    369 	int error;
    370 
    371 	/* calcurate frame buffer size */
    372 	size = (chip->vc_fbwidth * chip->vc_fbheight * chip->vc_fbdepth) /
    373 	    NBBY;
    374 
    375 	/* extent V-RAM region */
    376 	ex = extent_create("Frame buffer address", fb_start, *fb_end,
    377 	    0, (void *)ex_fixed, sizeof ex_fixed,
    378 	    EX_NOWAIT);
    379 	if (ex == 0)
    380 		return (1);
    381 
    382 	/* Allocate V-RAM area */
    383 	error = extent_alloc_subregion(ex, fb_start, fb_start + size - 1,
    384 	    size, TX3912_FRAMEBUFFER_ALIGNMENT,
    385 	    TX3912_FRAMEBUFFER_BOUNDARY, EX_FAST|EX_NOWAIT, &addr);
    386 	extent_destroy(ex);
    387 
    388 	if (error != 0)
    389 		return (1);
    390 
    391 	chip->vc_fbpaddr = addr;
    392 	chip->vc_fbvaddr = MIPS_PHYS_TO_KSEG1(addr);
    393 	chip->vc_fbsize = size;
    394 
    395 	*fb_end = addr + size;
    396 
    397 	return (0);
    398 }
    399 
    400 void
    401 tx3912video_framebuffer_init(struct video_chip *chip)
    402 {
    403 	u_int32_t fb_addr, fb_size, vaddr, bank, base;
    404 	txreg_t reg;
    405 	tx_chipset_tag_t tc = chip->vc_v;
    406 
    407 	fb_addr = chip->vc_fbpaddr;
    408 	fb_size = chip->vc_fbsize;
    409 
    410 	/*  XXX currently I don't set DFVAL, so force DF signal toggled on
    411          *  XXX each frame. */
    412 	reg = tx_conf_read(tc, TX3912_VIDEOCTRL1_REG);
    413 	reg &= ~TX3912_VIDEOCTRL1_DFMODE;
    414 	tx_conf_write(tc, TX3912_VIDEOCTRL1_REG, reg);
    415 
    416 	/* Set DMA transfer start and end address */
    417 
    418 	bank = TX3912_VIDEOCTRL3_VIDBANK(fb_addr);
    419 	base = TX3912_VIDEOCTRL3_VIDBASEHI(fb_addr);
    420 	reg = TX3912_VIDEOCTRL3_VIDBANK_SET(0, bank);
    421 	/* Upper address counter */
    422 	reg = TX3912_VIDEOCTRL3_VIDBASEHI_SET(reg, base);
    423 	tx_conf_write(tc, TX3912_VIDEOCTRL3_REG, reg);
    424 
    425 	/* Lower address counter  */
    426 	base = TX3912_VIDEOCTRL4_VIDBASELO(fb_addr + fb_size);
    427 	reg = TX3912_VIDEOCTRL4_VIDBASELO_SET(0, base);
    428 
    429 	/* Set DF-signal rate */
    430 	reg = TX3912_VIDEOCTRL4_DFVAL_SET(reg, 0); /* XXX not yet*/
    431 
    432 	/* Set VIDDONE signal delay after FRAME signal */
    433 	/* XXX not yet*/
    434 	tx_conf_write(tc, TX3912_VIDEOCTRL4_REG, reg);
    435 
    436 	/* Clear frame buffer */
    437 	vaddr = MIPS_PHYS_TO_KSEG1(fb_addr);
    438 	memset((void*)vaddr, 0, fb_size);
    439 }
    440 
    441 void
    442 tx3912video_resolution_init(struct video_chip *chip)
    443 {
    444 	int h, v, split, bit8, horzval, lineval;
    445 	tx_chipset_tag_t tc = chip->vc_v;
    446 	txreg_t reg;
    447 	u_int32_t val;
    448 
    449 	h = chip->vc_fbwidth;
    450 	v = chip->vc_fbheight;
    451 	reg = tx_conf_read(tc, TX3912_VIDEOCTRL1_REG);
    452 	split = reg & TX3912_VIDEOCTRL1_DISPSPLIT;
    453 	bit8  = (TX3912_VIDEOCTRL1_BITSEL(reg) ==
    454 	    TX3912_VIDEOCTRL1_BITSEL_8BITCOLOR);
    455 	val = TX3912_VIDEOCTRL1_BITSEL(reg);
    456 
    457 	if ((val == TX3912_VIDEOCTRL1_BITSEL_8BITCOLOR) && !split) {
    458 		/* (LCD horizontal pixels / 8bit) * RGB - 1 */
    459 		horzval = (h / 8) * 3 - 1;
    460 	} else {
    461 		horzval = h / 4 - 1;
    462 	}
    463 	lineval = (split ? v / 2 : v) - 1;
    464 
    465 	/* Video rate */
    466 	/* XXX
    467 	 *  probably This value should be determined from DFINT and LCDINT
    468 	 */
    469 	reg = TX3912_VIDEOCTRL2_VIDRATE_SET(0, horzval + 1);
    470 	/* Horizontal size of LCD */
    471 	reg = TX3912_VIDEOCTRL2_HORZVAL_SET(reg, horzval);
    472 	/* # of lines for the LCD */
    473 	reg = TX3912_VIDEOCTRL2_LINEVAL_SET(reg, lineval);
    474 
    475 	tx_conf_write(tc, TX3912_VIDEOCTRL2_REG, reg);
    476 }
    477 
    478 void
    479 tx3912video_reset(struct video_chip *chip)
    480 {
    481 	tx_chipset_tag_t tc = chip->vc_v;
    482 	txreg_t reg;
    483 
    484 	reg = tx_conf_read(tc, TX3912_VIDEOCTRL1_REG);
    485 
    486 	/* Disable video logic at end of this frame */
    487 	reg |= TX3912_VIDEOCTRL1_ENFREEZEFRAME;
    488 	tx_conf_write(tc, TX3912_VIDEOCTRL1_REG, reg);
    489 
    490 	/* Wait for end of frame */
    491 	delay(30 * 1000);
    492 
    493 	/* Make sure to disable video logic */
    494 	reg &= ~TX3912_VIDEOCTRL1_ENVID;
    495 	tx_conf_write(tc, TX3912_VIDEOCTRL1_REG, reg);
    496 
    497 	delay(1000);
    498 
    499 	/* Enable video logic again */
    500 	reg &= ~TX3912_VIDEOCTRL1_ENFREEZEFRAME;
    501 	reg |= TX3912_VIDEOCTRL1_ENVID;
    502 	tx_conf_write(tc, TX3912_VIDEOCTRL1_REG, reg);
    503 
    504 	delay(1000);
    505 }
    506 
    507 int
    508 tx3912video_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    509 {
    510 	struct tx3912video_softc *sc = (struct tx3912video_softc *)v;
    511 	struct hpcfb_fbconf *fbconf;
    512 	struct hpcfb_dspconf *dspconf;
    513 	struct wsdisplay_cmap *cmap;
    514 	u_int8_t *r, *g, *b;
    515 	u_int32_t *rgb;
    516 	int idx, cnt, error;
    517 
    518 	switch (cmd) {
    519 	case WSDISPLAYIO_GETCMAP:
    520 		cmap = (struct wsdisplay_cmap *)data;
    521 		cnt = cmap->count;
    522 		idx = cmap->index;
    523 
    524 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
    525 		    sc->sc_fbconf.hf_pack_width != 8 ||
    526 		    !LEGAL_CLUT_INDEX(idx) ||
    527 		    !LEGAL_CLUT_INDEX(idx + cnt - 1)) {
    528 			return (EINVAL);
    529 		}
    530 
    531 		error = cmap_work_alloc(&r, &g, &b, &rgb, cnt);
    532 		if (error)
    533 			goto out;
    534 		tx3912video_clut_get(sc, rgb, idx, cnt);
    535 		rgb24_decompose(rgb, r, g, b, cnt);
    536 
    537 		error = copyout(r, cmap->red, cnt);
    538 		if (error)
    539 			goto out;
    540 		error = copyout(g, cmap->green,cnt);
    541 		if (error)
    542 			goto out;
    543 		error = copyout(b, cmap->blue, cnt);
    544 
    545 out:
    546 		cmap_work_free(r, g, b, rgb);
    547 		return error;
    548 
    549 	case WSDISPLAYIO_PUTCMAP:
    550 		/*
    551 		 * TX3912 can't change CLUT index. R:G:B = 3:3:2
    552 		 */
    553 		return (0);
    554 
    555 	case HPCFBIO_GCONF:
    556 		fbconf = (struct hpcfb_fbconf *)data;
    557 		if (fbconf->hf_conf_index != 0 &&
    558 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    559 			return (EINVAL);
    560 		}
    561 		*fbconf = sc->sc_fbconf;	/* structure assignment */
    562 		return (0);
    563 
    564 	case HPCFBIO_SCONF:
    565 		fbconf = (struct hpcfb_fbconf *)data;
    566 		if (fbconf->hf_conf_index != 0 &&
    567 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    568 			return (EINVAL);
    569 		}
    570 		/*
    571 		 * nothing to do because we have only one configuration
    572 		 */
    573 		return (0);
    574 
    575 	case HPCFBIO_GDSPCONF:
    576 		dspconf = (struct hpcfb_dspconf *)data;
    577 		if ((dspconf->hd_unit_index != 0 &&
    578 		    dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    579 		    (dspconf->hd_conf_index != 0 &&
    580 			dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    581 			return (EINVAL);
    582 		}
    583 		*dspconf = sc->sc_dspconf;	/* structure assignment */
    584 		return (0);
    585 
    586 	case HPCFBIO_SDSPCONF:
    587 		dspconf = (struct hpcfb_dspconf *)data;
    588 		if ((dspconf->hd_unit_index != 0 &&
    589 		    dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    590 		    (dspconf->hd_conf_index != 0 &&
    591 			dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    592 			return (EINVAL);
    593 		}
    594 		/*
    595 		 * nothing to do
    596 		 * because we have only one unit and one configuration
    597 		 */
    598 		return (0);
    599 
    600 	case HPCFBIO_GOP:
    601 	case HPCFBIO_SOP:
    602 		/* XXX not implemented yet */
    603 		return (EINVAL);
    604 	}
    605 
    606 	return (EPASSTHROUGH);
    607 }
    608 
    609 paddr_t
    610 tx3912video_mmap(void *ctx, off_t offset, int prot)
    611 {
    612 	struct tx3912video_softc *sc = (struct tx3912video_softc *)ctx;
    613 
    614 	if (offset < 0 || (sc->sc_fbconf.hf_bytes_per_plane +
    615 	    sc->sc_fbconf.hf_offset) <  offset) {
    616 		return (-1);
    617 	}
    618 
    619 	return (mips_btop(sc->sc_chip->vc_fbpaddr + offset));
    620 }
    621 
    622 /*
    623  * CLUT staff
    624  */
    625 static const struct {
    626 	int mul, div;
    627 } dither_list [] = {
    628 	[TX3912_VIDEO_DITHER_DUTYCYCLE_1]	= { 1, 1 },
    629 	[TX3912_VIDEO_DITHER_DUTYCYCLE_6_7]	= { 6, 7 },
    630 	[TX3912_VIDEO_DITHER_DUTYCYCLE_4_5]	= { 4, 5 },
    631 	[TX3912_VIDEO_DITHER_DUTYCYCLE_3_4]	= { 3, 4 },
    632 	[TX3912_VIDEO_DITHER_DUTYCYCLE_5_7]	= { 5, 7 },
    633 	[TX3912_VIDEO_DITHER_DUTYCYCLE_2_3]	= { 2, 3 },
    634 	[TX3912_VIDEO_DITHER_DUTYCYCLE_3_5]	= { 3, 5 },
    635 	[TX3912_VIDEO_DITHER_DUTYCYCLE_4_7]	= { 4, 7 },
    636 	[TX3912_VIDEO_DITHER_DUTYCYCLE_2_4]	= { 2, 4 },
    637 	[TX3912_VIDEO_DITHER_DUTYCYCLE_3_7]	= { 3, 7 },
    638 	[TX3912_VIDEO_DITHER_DUTYCYCLE_2_5]	= { 2, 5 },
    639 	[TX3912_VIDEO_DITHER_DUTYCYCLE_1_3]	= { 1, 3 },
    640 	[TX3912_VIDEO_DITHER_DUTYCYCLE_2_7]	= { 2, 7 },
    641 	[TX3912_VIDEO_DITHER_DUTYCYCLE_1_5]	= { 1, 5 },
    642 	[TX3912_VIDEO_DITHER_DUTYCYCLE_1_7]	= { 1, 7 },
    643 	[TX3912_VIDEO_DITHER_DUTYCYCLE_0]	= { 0, 1 }
    644 }, *dlp;
    645 
    646 static const int dither_level8[8] = {
    647 	TX3912_VIDEO_DITHER_DUTYCYCLE_0,
    648 	TX3912_VIDEO_DITHER_DUTYCYCLE_2_7,
    649 	TX3912_VIDEO_DITHER_DUTYCYCLE_2_5,
    650 	TX3912_VIDEO_DITHER_DUTYCYCLE_2_4,
    651 	TX3912_VIDEO_DITHER_DUTYCYCLE_3_5,
    652 	TX3912_VIDEO_DITHER_DUTYCYCLE_5_7,
    653 	TX3912_VIDEO_DITHER_DUTYCYCLE_4_5,
    654 	TX3912_VIDEO_DITHER_DUTYCYCLE_1,
    655 };
    656 
    657 static const int dither_level4[4] = {
    658 	TX3912_VIDEO_DITHER_DUTYCYCLE_0,
    659 	TX3912_VIDEO_DITHER_DUTYCYCLE_1_3,
    660 	TX3912_VIDEO_DITHER_DUTYCYCLE_5_7,
    661 	TX3912_VIDEO_DITHER_DUTYCYCLE_1,
    662 };
    663 
    664 static int
    665 __get_color8(int luti)
    666 {
    667 	KASSERT(luti >=0 && luti < 8);
    668 	dlp = &dither_list[dither_level8[luti]];
    669 
    670 	return ((0xff * dlp->mul) / dlp->div);
    671 }
    672 
    673 static int
    674 __get_color4(int luti)
    675 {
    676 	KASSERT(luti >=0 && luti < 4);
    677 	dlp = &dither_list[dither_level4[luti]];
    678 
    679 	return ((0xff * dlp->mul) / dlp->div);
    680 }
    681 
    682 void
    683 tx3912video_clut_get(struct tx3912video_softc *sc, u_int32_t *rgb, int beg,
    684     int cnt)
    685 {
    686 	int i;
    687 
    688 	KASSERT(rgb);
    689 	KASSERT(LEGAL_CLUT_INDEX(beg));
    690 	KASSERT(LEGAL_CLUT_INDEX(beg + cnt - 1));
    691 
    692 	for (i = beg; i < beg + cnt; i++) {
    693 		*rgb++ =  RGB24(__get_color8((i >> 5) & 0x7),
    694 		    __get_color8((i >> 2) & 0x7),
    695 		    __get_color4(i & 0x3));
    696 	}
    697 }
    698 
    699 void
    700 tx3912video_clut_install(void *ctx, struct rasops_info *ri)
    701 {
    702 	struct tx3912video_softc *sc = ctx;
    703 	static const int system_cmap[0x10] = {
    704 		TX3912VIDEO_BLACK,
    705 		TX3912VIDEO_RED,
    706 		TX3912VIDEO_GREEN,
    707 		TX3912VIDEO_YELLOW,
    708 		TX3912VIDEO_BLUE,
    709 		TX3912VIDEO_MAGENTA,
    710 		TX3912VIDEO_CYAN,
    711 		TX3912VIDEO_WHITE,
    712 		TX3912VIDEO_DARK_BLACK,
    713 		TX3912VIDEO_DARK_RED,
    714 		TX3912VIDEO_DARK_GREEN,
    715 		TX3912VIDEO_DARK_YELLOW,
    716 		TX3912VIDEO_DARK_BLUE,
    717 		TX3912VIDEO_DARK_MAGENTA,
    718 		TX3912VIDEO_DARK_CYAN,
    719 		TX3912VIDEO_DARK_WHITE,
    720 	};
    721 
    722 	KASSERT(ri);
    723 
    724 	if (sc->sc_chip->vc_fbdepth == 8) {
    725 		/* XXX 2bit gray scale LUT not supported */
    726 		memcpy(ri->ri_devcmap, system_cmap, sizeof system_cmap);
    727 	}
    728 }
    729 
    730 void
    731 tx3912video_clut_init(struct tx3912video_softc *sc)
    732 {
    733 	tx_chipset_tag_t tc = sc->sc_chip->vc_v;
    734 
    735 	if (sc->sc_chip->vc_fbdepth != 8) {
    736 		return; /* XXX 2bit gray scale LUT not supported */
    737 	}
    738 
    739 	/*
    740 	 * time-based dithering pattern (TOSHIBA recommended pattern)
    741 	 */
    742 	/* 2/3, 1/3 */
    743 	tx_conf_write(tc, TX3912_VIDEOCTRL8_REG,
    744 	    TX3912_VIDEOCTRL8_PAT2_3_DEFAULT);
    745 	/* 3/4, 2/4 */
    746 	tx_conf_write(tc, TX3912_VIDEOCTRL9_REG,
    747 	    (TX3912_VIDEOCTRL9_PAT3_4_DEFAULT << 16) |
    748 	    TX3912_VIDEOCTRL9_PAT2_4_DEFAULT);
    749 	/* 4/5, 1/5 */
    750 	tx_conf_write(tc, TX3912_VIDEOCTRL10_REG,
    751 	    TX3912_VIDEOCTRL10_PAT4_5_DEFAULT);
    752 	/* 3/5, 2/5 */
    753 	tx_conf_write(tc, TX3912_VIDEOCTRL11_REG,
    754 	    TX3912_VIDEOCTRL11_PAT3_5_DEFAULT);
    755 	/* 6/7, 1/7 */
    756 	tx_conf_write(tc, TX3912_VIDEOCTRL12_REG,
    757 	    TX3912_VIDEOCTRL12_PAT6_7_DEFAULT);
    758 	/* 5/7, 2/7 */
    759 	tx_conf_write(tc, TX3912_VIDEOCTRL13_REG,
    760 	    TX3912_VIDEOCTRL13_PAT5_7_DEFAULT);
    761 	/* 4/7, 3/7 */
    762 	tx_conf_write(tc, TX3912_VIDEOCTRL14_REG,
    763 	    TX3912_VIDEOCTRL14_PAT4_7_DEFAULT);
    764 
    765 	/*
    766 	 * dither-pattern look-up table. (selected by uch)
    767 	 */
    768 	/* red */
    769 	tx_conf_write(tc, TX3912_VIDEOCTRL5_REG,
    770 	    (dither_level8[7] << 28) |
    771 	    (dither_level8[6] << 24) |
    772 	    (dither_level8[5] << 20) |
    773 	    (dither_level8[4] << 16) |
    774 	    (dither_level8[3] << 12) |
    775 	    (dither_level8[2] << 8) |
    776 	    (dither_level8[1] << 4) |
    777 	    (dither_level8[0] << 0));
    778 	/* green */
    779 	tx_conf_write(tc, TX3912_VIDEOCTRL6_REG,
    780 	    (dither_level8[7] << 28) |
    781 	    (dither_level8[6] << 24) |
    782 	    (dither_level8[5] << 20) |
    783 	    (dither_level8[4] << 16) |
    784 	    (dither_level8[3] << 12) |
    785 	    (dither_level8[2] << 8) |
    786 	    (dither_level8[1] << 4) |
    787 	    (dither_level8[0] << 0));
    788 	/* blue (2bit gray scale also use this look-up table) */
    789 	tx_conf_write(tc, TX3912_VIDEOCTRL7_REG,
    790 	    (dither_level4[3] << 12) |
    791 	    (dither_level4[2] << 8) |
    792 	    (dither_level4[1] << 4) |
    793 	    (dither_level4[0] << 0));
    794 
    795 	tx3912video_reset(sc->sc_chip);
    796 }
    797