Home | History | Annotate | Line # | Download | only in sun
cgthree.c revision 1.2.6.3
      1 /*	$NetBSD: cgthree.c,v 1.2.6.3 2002/06/28 08:04:12 jdolecek 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.2.6.3 2002/06/28 08:04:12 jdolecek 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 #include <machine/conf.h>
     72 
     73 static void	cgthreeunblank(struct device *);
     74 static void	cgthreeloadcmap(struct cgthree_softc *, int, int);
     75 static void	cgthree_set_video(struct cgthree_softc *, int);
     76 static int	cgthree_get_video(struct cgthree_softc *);
     77 
     78 /* cdevsw prototypes */
     79 cdev_decl(cgthree);
     80 
     81 extern struct cfdriver cgthree_cd;
     82 
     83 /* frame buffer generic driver */
     84 static struct fbdriver cgthreefbdriver = {
     85 	cgthreeunblank, cgthreeopen, cgthreeclose, cgthreeioctl, cgthreepoll,
     86 	cgthreemmap, cgthreekqfilter
     87 };
     88 
     89 /* Video control parameters */
     90 struct cg3_videoctrl {
     91 	unsigned char	sense;		/* Monitor sense value */
     92 	unsigned char	vctrl[12];
     93 } cg3_videoctrl[] = {
     94 /* Missing entries: sense 0x10, 0x30, 0x50 */
     95 	{ 0x40, /* this happens to be my 19'' 1152x900 gray-scale monitor */
     96 	   {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
     97 	},
     98 	{ 0x00, /* default? must be last */
     99 	   {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
    100 	}
    101 };
    102 
    103 
    104 void
    105 cgthreeattach(sc, name, isconsole)
    106 	struct cgthree_softc *sc;
    107 	char *name;
    108 	int isconsole;
    109 {
    110 	int i;
    111 	struct fbdevice *fb = &sc->sc_fb;
    112 	volatile struct fbcontrol *fbc = sc->sc_fbc;
    113 	volatile struct bt_regs *bt = &fbc->fbc_dac;
    114 
    115 	fb->fb_driver = &cgthreefbdriver;
    116 	fb->fb_type.fb_cmsize = 256;
    117 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    118 	printf(": %s, %d x %d", name,
    119 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    120 
    121 	/* Transfer video magic to board, if it's not running */
    122 	if ((fbc->fbc_ctrl & FBC_TIMING) == 0) {
    123 		int sense = (fbc->fbc_status & FBS_MSENSE);
    124 		/* Search table for video timings fitting this monitor */
    125 		for (i = 0; i < sizeof(cg3_videoctrl)/sizeof(cg3_videoctrl[0]);
    126 		     i++) {
    127 			int j;
    128 			if (sense != cg3_videoctrl[i].sense)
    129 				continue;
    130 
    131 			printf(" setting video ctrl");
    132 			for (j = 0; j < 12; j++)
    133 				fbc->fbc_vcontrol[j] =
    134 					cg3_videoctrl[i].vctrl[j];
    135 			fbc->fbc_ctrl |= FBC_TIMING;
    136 			break;
    137 		}
    138 	}
    139 
    140 	/* Initialize the default color map. */
    141 	bt_initcmap(&sc->sc_cmap, 256);
    142 	cgthreeloadcmap(sc, 0, 256);
    143 
    144 	/* make sure we are not blanked */
    145 	cgthree_set_video(sc, 1);
    146 	BT_INIT(bt, 0);
    147 
    148 	if (isconsole) {
    149 		printf(" (console)\n");
    150 #ifdef RASTERCONSOLE
    151 		fbrcons_init(fb);
    152 #endif
    153 	} else
    154 		printf("\n");
    155 
    156 	fb_attach(fb, isconsole);
    157 }
    158 
    159 
    160 int
    161 cgthreeopen(dev, flags, mode, p)
    162 	dev_t dev;
    163 	int flags, mode;
    164 	struct proc *p;
    165 {
    166 	int unit = minor(dev);
    167 
    168 	if (unit >= cgthree_cd.cd_ndevs || cgthree_cd.cd_devs[unit] == NULL)
    169 		return (ENXIO);
    170 	return (0);
    171 }
    172 
    173 int
    174 cgthreeclose(dev, flags, mode, p)
    175 	dev_t dev;
    176 	int flags, mode;
    177 	struct proc *p;
    178 {
    179 
    180 	return (0);
    181 }
    182 
    183 int
    184 cgthreeioctl(dev, cmd, data, flags, p)
    185 	dev_t dev;
    186 	u_long cmd;
    187 	caddr_t data;
    188 	int flags;
    189 	struct proc *p;
    190 {
    191 	struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
    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(dev, events, p)
    244 	dev_t dev;
    245 	int events;
    246 	struct proc *p;
    247 {
    248 
    249 	return (seltrue(dev, events, p));
    250 }
    251 
    252 static void
    253 filt_cgthreedetach(struct knote *kn)
    254 {
    255 	/* Nothing to do */
    256 }
    257 
    258 static const struct filterops cgthree_filtops =
    259 	{ 1, NULL, filt_cgthreedetach, filt_seltrue };
    260 
    261 int
    262 cgthreekqfilter(dev_t dev, struct knote *kn)
    263 {
    264 	switch (kn->kn_filter) {
    265 	case EVFILT_READ:
    266 	case EVFILT_WRITE:
    267 		kn->kn_fop = &cgthree_filtops;
    268 		break;
    269 	default:
    270 		return (1);
    271 	}
    272 
    273 	/* Nothing more to do */
    274 	return (0);
    275 }
    276 
    277 /*
    278  * Undo the effect of an FBIOSVIDEO that turns the video off.
    279  */
    280 static void
    281 cgthreeunblank(dev)
    282 	struct device *dev;
    283 {
    284 
    285 	cgthree_set_video((struct cgthree_softc *)dev, 1);
    286 }
    287 
    288 static void
    289 cgthree_set_video(sc, enable)
    290 	struct cgthree_softc *sc;
    291 	int enable;
    292 {
    293 
    294 	if (enable)
    295 		sc->sc_fbc->fbc_ctrl |= FBC_VENAB;
    296 	else
    297 		sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB;
    298 }
    299 
    300 static int
    301 cgthree_get_video(sc)
    302 	struct cgthree_softc *sc;
    303 {
    304 
    305 	return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0);
    306 }
    307 
    308 /*
    309  * Load a subset of the current (new) colormap into the Brooktree DAC.
    310  */
    311 static void
    312 cgthreeloadcmap(sc, start, ncolors)
    313 	struct cgthree_softc *sc;
    314 	int start, ncolors;
    315 {
    316 	volatile struct bt_regs *bt;
    317 	u_int *ip;
    318 	int count;
    319 
    320 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    321 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    322 	bt = &sc->sc_fbc->fbc_dac;
    323 	bt->bt_addr = BT_D4M4(start);
    324 	while (--count >= 0)
    325 		bt->bt_cmap = *ip++;
    326 }
    327 
    328 /*
    329  * Return the address that would map the given device at the given
    330  * offset, allowing for the given protection, or return -1 for error.
    331  *
    332  * The cg3 is mapped starting at 256KB, for pseudo-compatibility with
    333  * the cg4 (which had an overlay plane in the first 128K and an enable
    334  * plane in the next 128K).  X11 uses only 256k+ region but tries to
    335  * map the whole thing, so we repeatedly map the first 256K to the
    336  * first page of the color screen.  If someone tries to use the overlay
    337  * and enable regions, they will get a surprise....
    338  *
    339  * As well, mapping at an offset of 0x04000000 causes the cg3 to be
    340  * mapped in flat mode without the cg4 emulation.
    341  */
    342 paddr_t
    343 cgthreemmap(dev, off, prot)
    344 	dev_t dev;
    345 	off_t off;
    346 	int prot;
    347 {
    348 	struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
    349 
    350 #define START		(128*1024 + 128*1024)
    351 #define NOOVERLAY	(0x04000000)
    352 
    353 	if (off & PGOFSET)
    354 		panic("cgthreemmap");
    355 	if (off < 0)
    356 		return (-1);
    357 	if ((u_int)off >= NOOVERLAY)
    358 		off -= NOOVERLAY;
    359 	else if ((u_int)off >= START)
    360 		off -= START;
    361 	else
    362 		off = 0;
    363 
    364 	if (off >= sc->sc_fb.fb_type.fb_size)
    365 		return (-1);
    366 
    367 	return (bus_space_mmap(sc->sc_bustag,
    368 		sc->sc_paddr, CG3REG_MEM + off,
    369 		prot, BUS_SPACE_MAP_LINEAR));
    370 }
    371