Home | History | Annotate | Line # | Download | only in sun
cgthree.c revision 1.2.6.3
      1  1.2.6.3  jdolecek /*	$NetBSD: cgthree.c,v 1.2.6.3 2002/06/28 08:04:12 jdolecek Exp $ */
      2      1.1        pk 
      3      1.1        pk /*-
      4      1.1        pk  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5      1.1        pk  * All rights reserved.
      6      1.1        pk  *
      7      1.1        pk  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1        pk  * by Paul Kranenburg.
      9      1.1        pk  *
     10      1.1        pk  * Redistribution and use in source and binary forms, with or without
     11      1.1        pk  * modification, are permitted provided that the following conditions
     12      1.1        pk  * are met:
     13      1.1        pk  * 1. Redistributions of source code must retain the above copyright
     14      1.1        pk  *    notice, this list of conditions and the following disclaimer.
     15      1.1        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1        pk  *    notice, this list of conditions and the following disclaimer in the
     17      1.1        pk  *    documentation and/or other materials provided with the distribution.
     18      1.1        pk  * 3. All advertising materials mentioning features or use of this software
     19      1.1        pk  *    must display the following acknowledgement:
     20      1.1        pk  *        This product includes software developed by the NetBSD
     21      1.1        pk  *        Foundation, Inc. and its contributors.
     22      1.1        pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1        pk  *    contributors may be used to endorse or promote products derived
     24      1.1        pk  *    from this software without specific prior written permission.
     25      1.1        pk  *
     26      1.1        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1        pk  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1        pk  */
     38      1.1        pk 
     39      1.1        pk /*
     40      1.1        pk  * color display (cgthree) driver.
     41      1.1        pk  *
     42      1.1        pk  * Does not handle interrupts, even though they can occur.
     43      1.1        pk  *
     44      1.1        pk  * XXX should defer colormap updates to vertical retrace interrupts
     45      1.1        pk  */
     46      1.1        pk 
     47  1.2.6.1   thorpej #include <sys/cdefs.h>
     48  1.2.6.3  jdolecek __KERNEL_RCSID(0, "$NetBSD: cgthree.c,v 1.2.6.3 2002/06/28 08:04:12 jdolecek Exp $");
     49  1.2.6.1   thorpej 
     50      1.1        pk #include <sys/param.h>
     51      1.1        pk #include <sys/systm.h>
     52      1.1        pk #include <sys/buf.h>
     53      1.1        pk #include <sys/device.h>
     54      1.1        pk #include <sys/ioctl.h>
     55      1.1        pk #include <sys/malloc.h>
     56      1.1        pk #include <sys/mman.h>
     57      1.1        pk #include <sys/tty.h>
     58      1.1        pk #include <sys/conf.h>
     59      1.1        pk 
     60      1.1        pk #include <machine/bus.h>
     61      1.1        pk #include <machine/autoconf.h>
     62      1.1        pk 
     63      1.1        pk #include <dev/sun/fbio.h>
     64      1.1        pk #include <dev/sun/fbvar.h>
     65      1.1        pk 
     66      1.1        pk #include <dev/sun/btreg.h>
     67      1.1        pk #include <dev/sun/btvar.h>
     68      1.1        pk #include <dev/sun/cgthreereg.h>
     69      1.1        pk #include <dev/sun/cgthreevar.h>
     70      1.1        pk 
     71      1.1        pk #include <machine/conf.h>
     72      1.1        pk 
     73      1.2        pk static void	cgthreeunblank(struct device *);
     74      1.2        pk static void	cgthreeloadcmap(struct cgthree_softc *, int, int);
     75      1.2        pk static void	cgthree_set_video(struct cgthree_softc *, int);
     76      1.2        pk static int	cgthree_get_video(struct cgthree_softc *);
     77      1.2        pk 
     78      1.1        pk /* cdevsw prototypes */
     79      1.1        pk cdev_decl(cgthree);
     80      1.1        pk 
     81      1.1        pk extern struct cfdriver cgthree_cd;
     82      1.1        pk 
     83      1.1        pk /* frame buffer generic driver */
     84      1.1        pk static struct fbdriver cgthreefbdriver = {
     85      1.1        pk 	cgthreeunblank, cgthreeopen, cgthreeclose, cgthreeioctl, cgthreepoll,
     86  1.2.6.3  jdolecek 	cgthreemmap, cgthreekqfilter
     87      1.1        pk };
     88      1.1        pk 
     89      1.1        pk /* Video control parameters */
     90      1.1        pk struct cg3_videoctrl {
     91      1.1        pk 	unsigned char	sense;		/* Monitor sense value */
     92      1.1        pk 	unsigned char	vctrl[12];
     93      1.1        pk } cg3_videoctrl[] = {
     94      1.1        pk /* Missing entries: sense 0x10, 0x30, 0x50 */
     95      1.1        pk 	{ 0x40, /* this happens to be my 19'' 1152x900 gray-scale monitor */
     96      1.1        pk 	   {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
     97      1.1        pk 	},
     98      1.1        pk 	{ 0x00, /* default? must be last */
     99      1.1        pk 	   {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
    100      1.1        pk 	}
    101      1.1        pk };
    102      1.1        pk 
    103      1.1        pk 
    104      1.1        pk void
    105      1.1        pk cgthreeattach(sc, name, isconsole)
    106      1.1        pk 	struct cgthree_softc *sc;
    107      1.1        pk 	char *name;
    108      1.1        pk 	int isconsole;
    109      1.1        pk {
    110      1.1        pk 	int i;
    111      1.1        pk 	struct fbdevice *fb = &sc->sc_fb;
    112      1.1        pk 	volatile struct fbcontrol *fbc = sc->sc_fbc;
    113      1.1        pk 	volatile struct bt_regs *bt = &fbc->fbc_dac;
    114      1.1        pk 
    115      1.1        pk 	fb->fb_driver = &cgthreefbdriver;
    116      1.1        pk 	fb->fb_type.fb_cmsize = 256;
    117      1.1        pk 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    118      1.1        pk 	printf(": %s, %d x %d", name,
    119      1.1        pk 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    120      1.1        pk 
    121      1.1        pk 	/* Transfer video magic to board, if it's not running */
    122      1.1        pk 	if ((fbc->fbc_ctrl & FBC_TIMING) == 0) {
    123      1.1        pk 		int sense = (fbc->fbc_status & FBS_MSENSE);
    124      1.1        pk 		/* Search table for video timings fitting this monitor */
    125      1.1        pk 		for (i = 0; i < sizeof(cg3_videoctrl)/sizeof(cg3_videoctrl[0]);
    126      1.1        pk 		     i++) {
    127      1.1        pk 			int j;
    128      1.1        pk 			if (sense != cg3_videoctrl[i].sense)
    129      1.1        pk 				continue;
    130      1.1        pk 
    131      1.1        pk 			printf(" setting video ctrl");
    132      1.1        pk 			for (j = 0; j < 12; j++)
    133      1.1        pk 				fbc->fbc_vcontrol[j] =
    134      1.1        pk 					cg3_videoctrl[i].vctrl[j];
    135      1.1        pk 			fbc->fbc_ctrl |= FBC_TIMING;
    136      1.1        pk 			break;
    137      1.1        pk 		}
    138      1.1        pk 	}
    139      1.1        pk 
    140      1.1        pk 	/* Initialize the default color map. */
    141      1.1        pk 	bt_initcmap(&sc->sc_cmap, 256);
    142      1.1        pk 	cgthreeloadcmap(sc, 0, 256);
    143      1.1        pk 
    144      1.1        pk 	/* make sure we are not blanked */
    145      1.1        pk 	cgthree_set_video(sc, 1);
    146      1.1        pk 	BT_INIT(bt, 0);
    147      1.1        pk 
    148      1.1        pk 	if (isconsole) {
    149      1.1        pk 		printf(" (console)\n");
    150      1.1        pk #ifdef RASTERCONSOLE
    151      1.1        pk 		fbrcons_init(fb);
    152      1.1        pk #endif
    153      1.1        pk 	} else
    154      1.1        pk 		printf("\n");
    155      1.1        pk 
    156      1.1        pk 	fb_attach(fb, isconsole);
    157      1.1        pk }
    158      1.1        pk 
    159      1.1        pk 
    160      1.1        pk int
    161      1.1        pk cgthreeopen(dev, flags, mode, p)
    162      1.1        pk 	dev_t dev;
    163      1.1        pk 	int flags, mode;
    164      1.1        pk 	struct proc *p;
    165      1.1        pk {
    166      1.1        pk 	int unit = minor(dev);
    167      1.1        pk 
    168      1.1        pk 	if (unit >= cgthree_cd.cd_ndevs || cgthree_cd.cd_devs[unit] == NULL)
    169      1.1        pk 		return (ENXIO);
    170      1.1        pk 	return (0);
    171      1.1        pk }
    172      1.1        pk 
    173      1.1        pk int
    174      1.1        pk cgthreeclose(dev, flags, mode, p)
    175      1.1        pk 	dev_t dev;
    176      1.1        pk 	int flags, mode;
    177      1.1        pk 	struct proc *p;
    178      1.1        pk {
    179      1.1        pk 
    180      1.1        pk 	return (0);
    181      1.1        pk }
    182      1.1        pk 
    183      1.1        pk int
    184      1.1        pk cgthreeioctl(dev, cmd, data, flags, p)
    185      1.1        pk 	dev_t dev;
    186      1.1        pk 	u_long cmd;
    187      1.1        pk 	caddr_t data;
    188      1.1        pk 	int flags;
    189      1.1        pk 	struct proc *p;
    190      1.1        pk {
    191      1.1        pk 	struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
    192      1.1        pk 	struct fbgattr *fba;
    193      1.1        pk 	int error;
    194      1.1        pk 
    195      1.1        pk 	switch (cmd) {
    196      1.1        pk 
    197      1.1        pk 	case FBIOGTYPE:
    198      1.1        pk 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    199      1.1        pk 		break;
    200      1.1        pk 
    201      1.1        pk 	case FBIOGATTR:
    202      1.1        pk 		fba = (struct fbgattr *)data;
    203      1.1        pk 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    204      1.1        pk 		fba->owner = 0;		/* XXX ??? */
    205      1.1        pk 		fba->fbtype = sc->sc_fb.fb_type;
    206      1.1        pk 		fba->sattr.flags = 0;
    207      1.1        pk 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    208      1.1        pk 		fba->sattr.dev_specific[0] = -1;
    209      1.1        pk 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    210      1.1        pk 		fba->emu_types[1] = -1;
    211      1.1        pk 		break;
    212      1.1        pk 
    213      1.1        pk 	case FBIOGETCMAP:
    214      1.1        pk #define p ((struct fbcmap *)data)
    215      1.1        pk 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    216      1.1        pk 
    217      1.1        pk 	case FBIOPUTCMAP:
    218      1.1        pk 		/* copy to software map */
    219      1.1        pk 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    220      1.1        pk 		if (error)
    221      1.1        pk 			return (error);
    222      1.1        pk 		/* now blast them into the chip */
    223      1.1        pk 		/* XXX should use retrace interrupt */
    224      1.1        pk 		cgthreeloadcmap(sc, p->index, p->count);
    225      1.1        pk #undef p
    226      1.1        pk 		break;
    227      1.1        pk 
    228      1.1        pk 	case FBIOGVIDEO:
    229      1.1        pk 		*(int *)data = cgthree_get_video(sc);
    230      1.1        pk 		break;
    231      1.1        pk 
    232      1.1        pk 	case FBIOSVIDEO:
    233      1.1        pk 		cgthree_set_video(sc, *(int *)data);
    234      1.1        pk 		break;
    235      1.1        pk 
    236      1.1        pk 	default:
    237      1.1        pk 		return (ENOTTY);
    238      1.1        pk 	}
    239      1.1        pk 	return (0);
    240      1.1        pk }
    241      1.1        pk 
    242      1.1        pk int
    243      1.1        pk cgthreepoll(dev, events, p)
    244      1.1        pk 	dev_t dev;
    245      1.1        pk 	int events;
    246      1.1        pk 	struct proc *p;
    247      1.1        pk {
    248      1.1        pk 
    249      1.1        pk 	return (seltrue(dev, events, p));
    250  1.2.6.2  jdolecek }
    251  1.2.6.2  jdolecek 
    252  1.2.6.2  jdolecek static void
    253  1.2.6.2  jdolecek filt_cgthreedetach(struct knote *kn)
    254  1.2.6.2  jdolecek {
    255  1.2.6.2  jdolecek 	/* Nothing to do */
    256  1.2.6.2  jdolecek }
    257  1.2.6.2  jdolecek 
    258  1.2.6.2  jdolecek static const struct filterops cgthree_filtops =
    259  1.2.6.2  jdolecek 	{ 1, NULL, filt_cgthreedetach, filt_seltrue };
    260  1.2.6.2  jdolecek 
    261  1.2.6.2  jdolecek int
    262  1.2.6.2  jdolecek cgthreekqfilter(dev_t dev, struct knote *kn)
    263  1.2.6.2  jdolecek {
    264  1.2.6.2  jdolecek 	switch (kn->kn_filter) {
    265  1.2.6.2  jdolecek 	case EVFILT_READ:
    266  1.2.6.2  jdolecek 	case EVFILT_WRITE:
    267  1.2.6.2  jdolecek 		kn->kn_fop = &cgthree_filtops;
    268  1.2.6.2  jdolecek 		break;
    269  1.2.6.2  jdolecek 	default:
    270  1.2.6.2  jdolecek 		return (1);
    271  1.2.6.2  jdolecek 	}
    272  1.2.6.2  jdolecek 
    273  1.2.6.2  jdolecek 	/* Nothing more to do */
    274  1.2.6.2  jdolecek 	return (0);
    275      1.1        pk }
    276      1.1        pk 
    277      1.1        pk /*
    278      1.1        pk  * Undo the effect of an FBIOSVIDEO that turns the video off.
    279      1.1        pk  */
    280      1.1        pk static void
    281      1.1        pk cgthreeunblank(dev)
    282      1.1        pk 	struct device *dev;
    283      1.1        pk {
    284      1.1        pk 
    285      1.1        pk 	cgthree_set_video((struct cgthree_softc *)dev, 1);
    286      1.1        pk }
    287      1.1        pk 
    288      1.1        pk static void
    289      1.1        pk cgthree_set_video(sc, enable)
    290      1.1        pk 	struct cgthree_softc *sc;
    291      1.1        pk 	int enable;
    292      1.1        pk {
    293      1.1        pk 
    294      1.1        pk 	if (enable)
    295      1.1        pk 		sc->sc_fbc->fbc_ctrl |= FBC_VENAB;
    296      1.1        pk 	else
    297      1.1        pk 		sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB;
    298      1.1        pk }
    299      1.1        pk 
    300      1.1        pk static int
    301      1.1        pk cgthree_get_video(sc)
    302      1.1        pk 	struct cgthree_softc *sc;
    303      1.1        pk {
    304      1.1        pk 
    305      1.1        pk 	return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0);
    306      1.1        pk }
    307      1.1        pk 
    308      1.1        pk /*
    309      1.1        pk  * Load a subset of the current (new) colormap into the Brooktree DAC.
    310      1.1        pk  */
    311      1.1        pk static void
    312      1.1        pk cgthreeloadcmap(sc, start, ncolors)
    313      1.1        pk 	struct cgthree_softc *sc;
    314      1.1        pk 	int start, ncolors;
    315      1.1        pk {
    316      1.1        pk 	volatile struct bt_regs *bt;
    317      1.1        pk 	u_int *ip;
    318      1.1        pk 	int count;
    319      1.1        pk 
    320      1.1        pk 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    321      1.1        pk 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    322      1.1        pk 	bt = &sc->sc_fbc->fbc_dac;
    323      1.1        pk 	bt->bt_addr = BT_D4M4(start);
    324      1.1        pk 	while (--count >= 0)
    325      1.1        pk 		bt->bt_cmap = *ip++;
    326      1.1        pk }
    327      1.1        pk 
    328      1.1        pk /*
    329      1.1        pk  * Return the address that would map the given device at the given
    330      1.1        pk  * offset, allowing for the given protection, or return -1 for error.
    331      1.1        pk  *
    332      1.1        pk  * The cg3 is mapped starting at 256KB, for pseudo-compatibility with
    333      1.1        pk  * the cg4 (which had an overlay plane in the first 128K and an enable
    334      1.1        pk  * plane in the next 128K).  X11 uses only 256k+ region but tries to
    335      1.1        pk  * map the whole thing, so we repeatedly map the first 256K to the
    336      1.1        pk  * first page of the color screen.  If someone tries to use the overlay
    337      1.1        pk  * and enable regions, they will get a surprise....
    338      1.1        pk  *
    339      1.1        pk  * As well, mapping at an offset of 0x04000000 causes the cg3 to be
    340      1.1        pk  * mapped in flat mode without the cg4 emulation.
    341      1.1        pk  */
    342      1.1        pk paddr_t
    343      1.1        pk cgthreemmap(dev, off, prot)
    344      1.1        pk 	dev_t dev;
    345      1.1        pk 	off_t off;
    346      1.1        pk 	int prot;
    347      1.1        pk {
    348      1.1        pk 	struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
    349      1.1        pk 
    350      1.1        pk #define START		(128*1024 + 128*1024)
    351      1.1        pk #define NOOVERLAY	(0x04000000)
    352      1.1        pk 
    353      1.1        pk 	if (off & PGOFSET)
    354      1.1        pk 		panic("cgthreemmap");
    355      1.1        pk 	if (off < 0)
    356      1.1        pk 		return (-1);
    357      1.1        pk 	if ((u_int)off >= NOOVERLAY)
    358      1.1        pk 		off -= NOOVERLAY;
    359      1.1        pk 	else if ((u_int)off >= START)
    360      1.1        pk 		off -= START;
    361      1.1        pk 	else
    362      1.1        pk 		off = 0;
    363      1.1        pk 
    364      1.1        pk 	if (off >= sc->sc_fb.fb_type.fb_size)
    365      1.1        pk 		return (-1);
    366      1.1        pk 
    367  1.2.6.1   thorpej 	return (bus_space_mmap(sc->sc_bustag,
    368  1.2.6.1   thorpej 		sc->sc_paddr, CG3REG_MEM + off,
    369  1.2.6.1   thorpej 		prot, BUS_SPACE_MAP_LINEAR));
    370      1.1        pk }
    371