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