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