Home | History | Annotate | Line # | Download | only in sun
cgthree.c revision 1.5
      1 /*	$NetBSD: cgthree.c,v 1.5 2002/09/06 13:18:43 gehenna Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      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 /*
     40  * color display (cgthree) driver.
     41  *
     42  * Does not handle interrupts, even though they can occur.
     43  *
     44  * XXX should defer colormap updates to vertical retrace interrupts
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: cgthree.c,v 1.5 2002/09/06 13:18:43 gehenna Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/buf.h>
     53 #include <sys/device.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/malloc.h>
     56 #include <sys/mman.h>
     57 #include <sys/tty.h>
     58 #include <sys/conf.h>
     59 
     60 #include <machine/bus.h>
     61 #include <machine/autoconf.h>
     62 
     63 #include <dev/sun/fbio.h>
     64 #include <dev/sun/fbvar.h>
     65 
     66 #include <dev/sun/btreg.h>
     67 #include <dev/sun/btvar.h>
     68 #include <dev/sun/cgthreereg.h>
     69 #include <dev/sun/cgthreevar.h>
     70 
     71 static void	cgthreeunblank(struct device *);
     72 static void	cgthreeloadcmap(struct cgthree_softc *, int, int);
     73 static void	cgthree_set_video(struct cgthree_softc *, int);
     74 static int	cgthree_get_video(struct cgthree_softc *);
     75 
     76 extern struct cfdriver cgthree_cd;
     77 
     78 dev_type_open(cgthreeopen);
     79 dev_type_ioctl(cgthreeioctl);
     80 dev_type_mmap(cgthreemmap);
     81 
     82 const struct cdevsw cgthree_cdevsw = {
     83 	cgthreeopen, nullclose, noread, nowrite, cgthreeioctl,
     84 	nostop, notty, nopoll, cgthreemmap,
     85 };
     86 
     87 /* frame buffer generic driver */
     88 static struct fbdriver cgthreefbdriver = {
     89 	cgthreeunblank, cgthreeopen, nullclose, cgthreeioctl, nopoll,
     90 	cgthreemmap
     91 };
     92 
     93 /* Video control parameters */
     94 struct cg3_videoctrl {
     95 	unsigned char	sense;		/* Monitor sense value */
     96 	unsigned char	vctrl[12];
     97 } cg3_videoctrl[] = {
     98 /* Missing entries: sense 0x10, 0x30, 0x50 */
     99 	{ 0x40, /* this happens to be my 19'' 1152x900 gray-scale monitor */
    100 	   {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
    101 	},
    102 	{ 0x00, /* default? must be last */
    103 	   {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
    104 	}
    105 };
    106 
    107 
    108 void
    109 cgthreeattach(sc, name, isconsole)
    110 	struct cgthree_softc *sc;
    111 	char *name;
    112 	int isconsole;
    113 {
    114 	int i;
    115 	struct fbdevice *fb = &sc->sc_fb;
    116 	volatile struct fbcontrol *fbc = sc->sc_fbc;
    117 	volatile struct bt_regs *bt = &fbc->fbc_dac;
    118 
    119 	fb->fb_driver = &cgthreefbdriver;
    120 	fb->fb_type.fb_cmsize = 256;
    121 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    122 	printf(": %s, %d x %d", name,
    123 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    124 
    125 	/* Transfer video magic to board, if it's not running */
    126 	if ((fbc->fbc_ctrl & FBC_TIMING) == 0) {
    127 		int sense = (fbc->fbc_status & FBS_MSENSE);
    128 		/* Search table for video timings fitting this monitor */
    129 		for (i = 0; i < sizeof(cg3_videoctrl)/sizeof(cg3_videoctrl[0]);
    130 		     i++) {
    131 			int j;
    132 			if (sense != cg3_videoctrl[i].sense)
    133 				continue;
    134 
    135 			printf(" setting video ctrl");
    136 			for (j = 0; j < 12; j++)
    137 				fbc->fbc_vcontrol[j] =
    138 					cg3_videoctrl[i].vctrl[j];
    139 			fbc->fbc_ctrl |= FBC_TIMING;
    140 			break;
    141 		}
    142 	}
    143 
    144 	/* Initialize the default color map. */
    145 	bt_initcmap(&sc->sc_cmap, 256);
    146 	cgthreeloadcmap(sc, 0, 256);
    147 
    148 	/* make sure we are not blanked */
    149 	cgthree_set_video(sc, 1);
    150 	BT_INIT(bt, 0);
    151 
    152 	if (isconsole) {
    153 		printf(" (console)\n");
    154 #ifdef RASTERCONSOLE
    155 		fbrcons_init(fb);
    156 #endif
    157 	} else
    158 		printf("\n");
    159 
    160 	fb_attach(fb, isconsole);
    161 }
    162 
    163 
    164 int
    165 cgthreeopen(dev, flags, mode, p)
    166 	dev_t dev;
    167 	int flags, mode;
    168 	struct proc *p;
    169 {
    170 	int unit = minor(dev);
    171 
    172 	if (unit >= cgthree_cd.cd_ndevs || cgthree_cd.cd_devs[unit] == NULL)
    173 		return (ENXIO);
    174 	return (0);
    175 }
    176 
    177 int
    178 cgthreeioctl(dev, cmd, data, flags, p)
    179 	dev_t dev;
    180 	u_long cmd;
    181 	caddr_t data;
    182 	int flags;
    183 	struct proc *p;
    184 {
    185 	struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
    186 	struct fbgattr *fba;
    187 	int error;
    188 
    189 	switch (cmd) {
    190 
    191 	case FBIOGTYPE:
    192 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    193 		break;
    194 
    195 	case FBIOGATTR:
    196 		fba = (struct fbgattr *)data;
    197 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    198 		fba->owner = 0;		/* XXX ??? */
    199 		fba->fbtype = sc->sc_fb.fb_type;
    200 		fba->sattr.flags = 0;
    201 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    202 		fba->sattr.dev_specific[0] = -1;
    203 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    204 		fba->emu_types[1] = -1;
    205 		break;
    206 
    207 	case FBIOGETCMAP:
    208 #define p ((struct fbcmap *)data)
    209 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    210 
    211 	case FBIOPUTCMAP:
    212 		/* copy to software map */
    213 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    214 		if (error)
    215 			return (error);
    216 		/* now blast them into the chip */
    217 		/* XXX should use retrace interrupt */
    218 		cgthreeloadcmap(sc, p->index, p->count);
    219 #undef p
    220 		break;
    221 
    222 	case FBIOGVIDEO:
    223 		*(int *)data = cgthree_get_video(sc);
    224 		break;
    225 
    226 	case FBIOSVIDEO:
    227 		cgthree_set_video(sc, *(int *)data);
    228 		break;
    229 
    230 	default:
    231 		return (ENOTTY);
    232 	}
    233 	return (0);
    234 }
    235 
    236 /*
    237  * Undo the effect of an FBIOSVIDEO that turns the video off.
    238  */
    239 static void
    240 cgthreeunblank(dev)
    241 	struct device *dev;
    242 {
    243 
    244 	cgthree_set_video((struct cgthree_softc *)dev, 1);
    245 }
    246 
    247 static void
    248 cgthree_set_video(sc, enable)
    249 	struct cgthree_softc *sc;
    250 	int enable;
    251 {
    252 
    253 	if (enable)
    254 		sc->sc_fbc->fbc_ctrl |= FBC_VENAB;
    255 	else
    256 		sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB;
    257 }
    258 
    259 static int
    260 cgthree_get_video(sc)
    261 	struct cgthree_softc *sc;
    262 {
    263 
    264 	return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0);
    265 }
    266 
    267 /*
    268  * Load a subset of the current (new) colormap into the Brooktree DAC.
    269  */
    270 static void
    271 cgthreeloadcmap(sc, start, ncolors)
    272 	struct cgthree_softc *sc;
    273 	int start, ncolors;
    274 {
    275 	volatile struct bt_regs *bt;
    276 	u_int *ip;
    277 	int count;
    278 
    279 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    280 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    281 	bt = &sc->sc_fbc->fbc_dac;
    282 	bt->bt_addr = BT_D4M4(start);
    283 	while (--count >= 0)
    284 		bt->bt_cmap = *ip++;
    285 }
    286 
    287 /*
    288  * Return the address that would map the given device at the given
    289  * offset, allowing for the given protection, or return -1 for error.
    290  *
    291  * The cg3 is mapped starting at 256KB, for pseudo-compatibility with
    292  * the cg4 (which had an overlay plane in the first 128K and an enable
    293  * plane in the next 128K).  X11 uses only 256k+ region but tries to
    294  * map the whole thing, so we repeatedly map the first 256K to the
    295  * first page of the color screen.  If someone tries to use the overlay
    296  * and enable regions, they will get a surprise....
    297  *
    298  * As well, mapping at an offset of 0x04000000 causes the cg3 to be
    299  * mapped in flat mode without the cg4 emulation.
    300  */
    301 paddr_t
    302 cgthreemmap(dev, off, prot)
    303 	dev_t dev;
    304 	off_t off;
    305 	int prot;
    306 {
    307 	struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
    308 
    309 #define START		(128*1024 + 128*1024)
    310 #define NOOVERLAY	(0x04000000)
    311 
    312 	if (off & PGOFSET)
    313 		panic("cgthreemmap");
    314 	if (off < 0)
    315 		return (-1);
    316 	if ((u_int)off >= NOOVERLAY)
    317 		off -= NOOVERLAY;
    318 	else if ((u_int)off >= START)
    319 		off -= START;
    320 	else
    321 		off = 0;
    322 
    323 	if (off >= sc->sc_fb.fb_type.fb_size)
    324 		return (-1);
    325 
    326 	return (bus_space_mmap(sc->sc_bustag,
    327 		sc->sc_paddr, CG3REG_MEM + off,
    328 		prot, BUS_SPACE_MAP_LINEAR));
    329 }
    330