Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.42
      1  1.42  christos /*	$NetBSD: cgfourteen.c,v 1.42 2006/01/17 04:22:08 christos Exp $ */
      2   1.1    abrown 
      3   1.1    abrown /*
      4  1.12        pk  * Copyright (c) 1996
      5   1.1    abrown  *	The President and Fellows of Harvard College. All rights reserved.
      6   1.1    abrown  * Copyright (c) 1992, 1993
      7   1.1    abrown  *	The Regents of the University of California.  All rights reserved.
      8   1.1    abrown  *
      9   1.1    abrown  * This software was developed by the Computer Systems Engineering group
     10   1.1    abrown  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     11   1.1    abrown  * contributed to Berkeley.
     12   1.1    abrown  *
     13   1.1    abrown  * All advertising materials mentioning features or use of this software
     14   1.1    abrown  * must display the following acknowledgement:
     15   1.1    abrown  *	This product includes software developed by Harvard University.
     16   1.1    abrown  *	This product includes software developed by the University of
     17   1.1    abrown  *	California, Lawrence Berkeley Laboratory.
     18   1.1    abrown  *
     19   1.1    abrown  * Redistribution and use in source and binary forms, with or without
     20   1.1    abrown  * modification, are permitted provided that the following conditions
     21   1.1    abrown  * are met:
     22   1.1    abrown  * 1. Redistributions of source code must retain the above copyright
     23   1.1    abrown  *    notice, this list of conditions and the following disclaimer.
     24   1.1    abrown  * 2. Redistributions in binary form must reproduce the above copyright
     25   1.1    abrown  *    notice, this list of conditions and the following disclaimer in the
     26   1.1    abrown  *    documentation and/or other materials provided with the distribution.
     27   1.1    abrown  * 3. All advertising materials mentioning features or use of this software
     28   1.1    abrown  *    must display the following acknowledgement:
     29   1.1    abrown  *	This product includes software developed by the University of
     30   1.1    abrown  *	California, Berkeley and its contributors.
     31   1.1    abrown  *	This product includes software developed by Harvard University and
     32   1.1    abrown  *	its contributors.
     33   1.1    abrown  * 4. Neither the name of the University nor the names of its contributors
     34   1.1    abrown  *    may be used to endorse or promote products derived from this software
     35   1.1    abrown  *    without specific prior written permission.
     36   1.1    abrown  *
     37   1.1    abrown  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     38   1.1    abrown  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     39   1.1    abrown  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     40   1.1    abrown  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     41   1.1    abrown  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     42   1.1    abrown  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     43   1.1    abrown  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     44   1.1    abrown  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     45   1.1    abrown  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     46   1.1    abrown  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     47   1.1    abrown  * SUCH DAMAGE.
     48   1.1    abrown  *
     49   1.1    abrown  *   Based on:
     50   1.1    abrown  *	NetBSD: cgthree.c,v 1.28 1996/05/31 09:59:22 pk Exp
     51   1.1    abrown  *	NetBSD: cgsix.c,v 1.25 1996/04/01 17:30:00 christos Exp
     52   1.1    abrown  */
     53   1.1    abrown 
     54   1.1    abrown /*
     55   1.1    abrown  * Driver for Campus-II on-board mbus-based video (cgfourteen).
     56   1.1    abrown  * Provides minimum emulation of a Sun cgthree 8-bit framebuffer to
     57   1.1    abrown  * allow X to run.
     58   1.1    abrown  *
     59   1.1    abrown  * Does not handle interrupts, even though they can occur.
     60   1.1    abrown  *
     61   1.1    abrown  * XXX should defer colormap updates to vertical retrace interrupts
     62   1.1    abrown  */
     63  1.10       mrg 
     64  1.12        pk /*
     65   1.1    abrown  * The following is for debugging only; it opens up a security hole
     66  1.12        pk  * enabled by allowing any user to map the control registers for the
     67   1.1    abrown  * cg14 into their space.
     68   1.1    abrown  */
     69  1.42  christos #define CG14_MAP_REGS
     70   1.1    abrown 
     71   1.1    abrown /*
     72   1.1    abrown  * The following enables 24-bit operation: when opened, the framebuffer
     73  1.12        pk  * will switch to 24-bit mode (actually 32-bit mode), and provide a
     74   1.1    abrown  * simple cg8 emulation.
     75   1.1    abrown  */
     76  1.42  christos #define CG14_CG8
     77   1.1    abrown 
     78   1.1    abrown #include <sys/param.h>
     79   1.1    abrown #include <sys/systm.h>
     80   1.1    abrown #include <sys/buf.h>
     81   1.1    abrown #include <sys/device.h>
     82   1.1    abrown #include <sys/ioctl.h>
     83   1.1    abrown #include <sys/malloc.h>
     84   1.1    abrown #include <sys/mman.h>
     85   1.1    abrown #include <sys/tty.h>
     86   1.1    abrown #include <sys/conf.h>
     87   1.1    abrown 
     88  1.18       mrg #include <uvm/uvm_extern.h>
     89   1.1    abrown 
     90  1.42  christos #include <dev/sun/fbio.h>
     91  1.19        pk #include <machine/autoconf.h>
     92  1.42  christos #include <machine/pmap.h>
     93  1.42  christos #include <dev/sun/fbvar.h>
     94  1.42  christos #include <machine/cpu.h>
     95  1.19        pk #include <dev/sbus/sbusvar.h>
     96  1.19        pk 
     97   1.1    abrown #include <sparc/dev/cgfourteenreg.h>
     98   1.1    abrown #include <sparc/dev/cgfourteenvar.h>
     99   1.1    abrown 
    100   1.1    abrown /* autoconfiguration driver */
    101  1.40       uwe static int	cgfourteenmatch(struct device *, struct cfdata *, void *);
    102   1.1    abrown static void	cgfourteenattach(struct device *, struct device *, void *);
    103   1.1    abrown static void	cgfourteenunblank(struct device *);
    104   1.1    abrown 
    105  1.29   thorpej CFATTACH_DECL(cgfourteen, sizeof(struct cgfourteen_softc),
    106  1.30   thorpej     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    107  1.42  christos 
    108   1.8   thorpej extern struct cfdriver cgfourteen_cd;
    109   1.1    abrown 
    110  1.26   gehenna dev_type_open(cgfourteenopen);
    111  1.26   gehenna dev_type_close(cgfourteenclose);
    112  1.26   gehenna dev_type_ioctl(cgfourteenioctl);
    113  1.26   gehenna dev_type_mmap(cgfourteenmmap);
    114  1.42  christos dev_type_poll(cgfourteenpoll);
    115  1.26   gehenna 
    116  1.26   gehenna const struct cdevsw cgfourteen_cdevsw = {
    117  1.42  christos         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    118  1.42  christos         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
    119  1.26   gehenna };
    120  1.26   gehenna 
    121   1.1    abrown /* frame buffer generic driver */
    122   1.1    abrown static struct fbdriver cgfourteenfbdriver = {
    123  1.12        pk 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    124  1.42  christos 	cgfourteenpoll, cgfourteenmmap, nokqfilter
    125   1.1    abrown };
    126   1.1    abrown 
    127  1.42  christos extern struct tty *fbconstty;
    128  1.42  christos 
    129  1.40       uwe static void cg14_set_video(struct cgfourteen_softc *, int);
    130  1.40       uwe static int  cg14_get_video(struct cgfourteen_softc *);
    131  1.40       uwe static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
    132  1.40       uwe static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
    133  1.40       uwe static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
    134  1.40       uwe static void cg14_init(struct cgfourteen_softc *);
    135  1.40       uwe static void cg14_reset(struct cgfourteen_softc *);
    136  1.23   darrenr 
    137  1.23   darrenr /*
    138   1.1    abrown  * Match a cgfourteen.
    139   1.1    abrown  */
    140  1.42  christos int
    141  1.40       uwe cgfourteenmatch(struct device *parent, struct cfdata *cf, void *aux)
    142   1.1    abrown {
    143  1.11        pk 	union obio_attach_args *uoba = aux;
    144  1.11        pk 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    145   1.1    abrown 
    146   1.1    abrown 	/*
    147   1.1    abrown 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    148   1.1    abrown 	 * via the processor, and not through device space or an external
    149   1.1    abrown 	 * bus. Thus we look _only_ at the obio bus.
    150   1.1    abrown 	 * Additionally, these things exist only on the Sun4m.
    151   1.1    abrown 	 */
    152  1.11        pk 
    153  1.11        pk 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    154  1.11        pk 		return (0);
    155  1.11        pk 
    156  1.11        pk 	/* Check driver name */
    157  1.27   thorpej 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    158   1.1    abrown }
    159   1.1    abrown 
    160   1.1    abrown /*
    161  1.42  christos  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
    162  1.42  christos  *  space for faked overlay junk for the cg8 emulation.
    163  1.42  christos  *
    164  1.42  christos  * As it happens, this value is correct for both cg3 and cg8 emulation!
    165  1.42  christos  */
    166  1.42  christos #define COLOUR_OFFSET (256*1024)
    167  1.42  christos 
    168  1.42  christos static void cg14_set_rcons_luts(struct cgfourteen_softc *sc)
    169  1.42  christos {
    170  1.42  christos 	int i;
    171  1.42  christos 
    172  1.42  christos 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_xlut->xlut_lut[i] = 0x22;
    173  1.42  christos 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_clut2->clut_lut[i] = 0x00ffffff;
    174  1.42  christos 	sc->sc_clut2->clut_lut[0] = 0x00ffffff;
    175  1.42  christos 	sc->sc_clut2->clut_lut[255] = 0;
    176  1.42  christos }
    177  1.42  christos 
    178  1.42  christos /*
    179   1.1    abrown  * Attach a display.  We need to notice if it is the console, too.
    180   1.1    abrown  */
    181  1.42  christos void
    182  1.40       uwe cgfourteenattach(struct device *parent, struct device *self, void *aux)
    183   1.1    abrown {
    184  1.11        pk 	union obio_attach_args *uoba = aux;
    185  1.11        pk 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    186  1.11        pk 	struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
    187  1.11        pk 	struct fbdevice *fb = &sc->sc_fb;
    188  1.11        pk 	bus_space_handle_t bh;
    189  1.11        pk 	int node, ramsize;
    190  1.40       uwe 	volatile uint32_t *lut;
    191   1.1    abrown 	int i, isconsole;
    192  1.11        pk 
    193  1.11        pk 	node = sa->sa_node;
    194  1.11        pk 
    195  1.11        pk 	/* Remember cookies for cgfourteenmmap() */
    196  1.11        pk 	sc->sc_bustag = sa->sa_bustag;
    197   1.1    abrown 
    198  1.11        pk 	fb->fb_driver = &cgfourteenfbdriver;
    199  1.11        pk 	fb->fb_device = &sc->sc_dev;
    200  1.11        pk 	/* Mask out invalid flags from the user. */
    201  1.11        pk 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
    202   1.1    abrown 
    203   1.1    abrown 	/*
    204   1.1    abrown 	 * We're emulating a cg3/8, so represent ourselves as one
    205   1.1    abrown 	 */
    206   1.1    abrown #ifdef CG14_CG8
    207  1.11        pk 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
    208  1.11        pk 	fb->fb_type.fb_depth = 32;
    209  1.42  christos 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    210  1.42  christos 	ramsize = roundup(fb->fb_type.fb_height * 1152 * 4, NBPG);
    211   1.1    abrown #else
    212  1.11        pk 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
    213  1.11        pk 	fb->fb_type.fb_depth = 8;
    214  1.11        pk 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    215  1.42  christos 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
    216  1.23   darrenr #endif
    217  1.11        pk 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    218  1.42  christos 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
    219  1.11        pk 
    220  1.13        pk 	if (sa->sa_nreg < 2) {
    221  1.13        pk 		printf("%s: only %d register sets\n",
    222  1.13        pk 			self->dv_xname, sa->sa_nreg);
    223  1.11        pk 		return;
    224  1.11        pk 	}
    225  1.13        pk 	bcopy(sa->sa_reg, sc->sc_physadr,
    226  1.42  christos 	      sa->sa_nreg * sizeof(struct sbus_reg));
    227   1.1    abrown 
    228   1.1    abrown 	/*
    229   1.1    abrown 	 * Now map in the 8 useful pages of registers
    230   1.1    abrown 	 */
    231  1.11        pk 	if (sa->sa_size < 0x10000) {
    232   1.1    abrown #ifdef DIAGNOSTIC
    233   1.5  christos 		printf("warning: can't find all cgfourteen registers...\n");
    234   1.1    abrown #endif
    235  1.11        pk 		sa->sa_size = 0x10000;
    236   1.1    abrown 	}
    237  1.42  christos 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    238  1.42  christos 			 sa->sa_offset,
    239  1.42  christos 			 sa->sa_size,
    240  1.42  christos 			 BUS_SPACE_MAP_LINEAR,
    241  1.42  christos 			 &bh) != 0) {
    242  1.11        pk 		printf("%s: cannot map control registers\n", self->dv_xname);
    243  1.11        pk 		return;
    244  1.11        pk 	}
    245  1.11        pk 
    246  1.11        pk 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    247  1.11        pk 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    248  1.11        pk 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    249  1.11        pk 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    250  1.11        pk 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    251  1.11        pk 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    252  1.11        pk 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    253  1.11        pk 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    254   1.1    abrown 
    255   1.1    abrown 	/*
    256   1.1    abrown 	 * Let the user know that we're here
    257   1.1    abrown 	 */
    258   1.1    abrown #ifdef CG14_CG8
    259   1.5  christos 	printf(": cgeight emulated at %dx%dx24bpp",
    260  1.11        pk 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    261   1.1    abrown #else
    262   1.5  christos 	printf(": cgthree emulated at %dx%dx8bpp",
    263  1.11        pk 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    264   1.1    abrown #endif
    265   1.1    abrown 	/*
    266  1.42  christos 	 * Enable the video.
    267   1.1    abrown 	 */
    268   1.1    abrown 	cg14_set_video(sc, 1);
    269   1.1    abrown 
    270   1.1    abrown 	/*
    271   1.1    abrown 	 * Grab the initial colormap
    272   1.1    abrown 	 */
    273  1.39   tsutsui 	lut = sc->sc_clut1->clut_lut;
    274   1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    275   1.1    abrown 		sc->sc_cmap.cm_chip[i] = lut[i];
    276   1.1    abrown 
    277   1.1    abrown 	/* See if we're the console */
    278  1.42  christos         isconsole = fb_is_console(node);
    279   1.1    abrown 
    280   1.1    abrown 	if (isconsole) {
    281   1.5  christos 		printf(" (console)\n");
    282   1.1    abrown #ifdef RASTERCONSOLE
    283  1.42  christos 		/* *sbus*_bus_map?  but that's how we map the regs... */
    284  1.42  christos 		if (sbus_bus_map( sc->sc_bustag,
    285  1.42  christos 				  sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    286  1.42  christos 				  sc->sc_physadr[CG14_PXL_IDX].sbr_offset+0x03800000,
    287  1.42  christos 				  1152*900, BUS_SPACE_MAP_LINEAR,
    288  1.42  christos 				  &bh) != 0) {
    289  1.42  christos 			printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
    290  1.42  christos 		} else {
    291  1.42  christos 			sc->sc_rcfb = sc->sc_fb;
    292  1.42  christos 			sc->sc_rcfb.fb_type.fb_type = FBTYPE_SUN3COLOR;
    293  1.42  christos 			sc->sc_rcfb.fb_type.fb_depth = 8;
    294  1.42  christos 			sc->sc_rcfb.fb_linebytes = 1152;
    295  1.42  christos 			sc->sc_rcfb.fb_type.fb_size = roundup(1152*900,NBPG);
    296  1.42  christos 			sc->sc_rcfb.fb_pixels = (void *)bh;
    297  1.42  christos 			printf("vram at %p\n",(void *)bh);
    298  1.42  christos 			for (i=0;i<1152*900;i++) ((unsigned char *)bh)[i] = 0;
    299  1.42  christos 			fbrcons_init(&sc->sc_rcfb);
    300  1.42  christos 			cg14_set_rcons_luts(sc);
    301  1.42  christos 			sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL;
    302  1.42  christos 		}
    303   1.1    abrown #endif
    304   1.1    abrown 	} else
    305   1.5  christos 		printf("\n");
    306   1.1    abrown 
    307   1.1    abrown 	/* Attach to /dev/fb */
    308  1.16        pk 	fb_attach(&sc->sc_fb, isconsole);
    309   1.1    abrown }
    310   1.1    abrown 
    311   1.1    abrown /*
    312  1.12        pk  * Keep track of the number of opens made. In the 24-bit driver, we need to
    313   1.1    abrown  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    314   1.1    abrown  * the last close. This kind of nonsense is needed to give screenblank
    315   1.1    abrown  * a fighting chance of working.
    316   1.1    abrown  */
    317   1.1    abrown static int cg14_opens = 0;
    318   1.1    abrown 
    319   1.1    abrown int
    320  1.41  christos cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
    321   1.1    abrown {
    322  1.42  christos 	struct cgfourteen_softc *sc;
    323  1.42  christos 	int unit;
    324   1.1    abrown 	int s, oldopens;
    325   1.1    abrown 
    326  1.42  christos 	unit = minor(dev);
    327  1.42  christos 	if (unit >= cgfourteen_cd.cd_ndevs)
    328  1.42  christos 		return(ENXIO);
    329  1.42  christos 	sc = cgfourteen_cd.cd_devs[minor(dev)];
    330  1.42  christos 	if (sc == NULL)
    331  1.42  christos 		return(ENXIO);
    332   1.1    abrown 	s = splhigh();
    333   1.1    abrown 	oldopens = cg14_opens++;
    334   1.1    abrown 	splx(s);
    335   1.1    abrown 
    336   1.1    abrown 	/* Setup the cg14 as we want it, and save the original PROM state */
    337   1.1    abrown 	if (oldopens == 0)	/* first open only, to make screenblank work */
    338   1.1    abrown 		cg14_init(sc);
    339   1.1    abrown 
    340   1.1    abrown 	return (0);
    341   1.1    abrown }
    342   1.1    abrown 
    343   1.1    abrown int
    344  1.41  christos cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
    345   1.1    abrown {
    346  1.11        pk 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    347   1.1    abrown 	int s, opens;
    348   1.1    abrown 
    349   1.1    abrown 	s = splhigh();
    350   1.1    abrown 	opens = --cg14_opens;
    351   1.1    abrown 	if (cg14_opens < 0)
    352   1.1    abrown 		opens = cg14_opens = 0;
    353   1.1    abrown 	splx(s);
    354   1.1    abrown 
    355   1.1    abrown 	/*
    356   1.1    abrown 	 * Restore video state to make the PROM happy, on last close.
    357   1.1    abrown 	 */
    358   1.1    abrown 	if (opens == 0)
    359   1.1    abrown 		cg14_reset(sc);
    360   1.1    abrown 
    361   1.1    abrown 	return (0);
    362   1.1    abrown }
    363   1.1    abrown 
    364   1.1    abrown int
    365  1.41  christos cgfourteenioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
    366   1.1    abrown {
    367  1.11        pk 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    368  1.11        pk 	struct fbgattr *fba;
    369  1.42  christos 	int error;
    370   1.1    abrown 
    371   1.1    abrown 	switch (cmd) {
    372   1.1    abrown 
    373   1.1    abrown 	case FBIOGTYPE:
    374   1.1    abrown 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    375   1.1    abrown 		break;
    376   1.1    abrown 
    377   1.1    abrown 	case FBIOGATTR:
    378   1.1    abrown 		fba = (struct fbgattr *)data;
    379   1.1    abrown 		fba->real_type = FBTYPE_MDICOLOR;
    380   1.1    abrown 		fba->owner = 0;		/* XXX ??? */
    381   1.1    abrown 		fba->fbtype = sc->sc_fb.fb_type;
    382   1.1    abrown 		fba->sattr.flags = 0;
    383   1.1    abrown 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    384   1.1    abrown 		fba->sattr.dev_specific[0] = -1;
    385   1.1    abrown 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    386   1.1    abrown 		fba->emu_types[1] = -1;
    387   1.1    abrown 		break;
    388   1.1    abrown 
    389   1.1    abrown 	case FBIOGETCMAP:
    390  1.42  christos 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    391   1.1    abrown 				     CG14_CLUT_SIZE));
    392   1.1    abrown 
    393   1.1    abrown 	case FBIOPUTCMAP:
    394   1.1    abrown 		/* copy to software map */
    395   1.1    abrown #define p ((struct fbcmap *)data)
    396   1.1    abrown #ifdef CG14_CG8
    397   1.1    abrown 		p->index &= 0xffffff;
    398   1.1    abrown #endif
    399   1.1    abrown 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    400   1.1    abrown 		if (error)
    401   1.1    abrown 			return (error);
    402   1.1    abrown 		/* now blast them into the chip */
    403   1.1    abrown 		/* XXX should use retrace interrupt */
    404   1.1    abrown 		cg14_load_hwcmap(sc, p->index, p->count);
    405   1.1    abrown #undef p
    406   1.1    abrown 		break;
    407   1.1    abrown 
    408   1.1    abrown 	case FBIOGVIDEO:
    409   1.1    abrown 		*(int *)data = cg14_get_video(sc);
    410   1.1    abrown 		break;
    411   1.1    abrown 
    412   1.1    abrown 	case FBIOSVIDEO:
    413   1.1    abrown 		cg14_set_video(sc, *(int *)data);
    414   1.1    abrown 		break;
    415   1.1    abrown 
    416   1.1    abrown 	default:
    417   1.1    abrown 		return (ENOTTY);
    418   1.1    abrown 	}
    419   1.1    abrown 	return (0);
    420   1.1    abrown }
    421   1.1    abrown 
    422   1.1    abrown /*
    423   1.1    abrown  * Undo the effect of an FBIOSVIDEO that turns the video off.
    424   1.1    abrown  */
    425   1.1    abrown static void
    426  1.40       uwe cgfourteenunblank(struct device *dev)
    427   1.1    abrown {
    428   1.1    abrown 
    429   1.1    abrown 	cg14_set_video((struct cgfourteen_softc *)dev, 1);
    430   1.1    abrown }
    431   1.1    abrown 
    432   1.1    abrown /*
    433   1.1    abrown  * Return the address that would map the given device at the given
    434   1.1    abrown  * offset, allowing for the given protection, or return -1 for error.
    435   1.1    abrown  *
    436  1.42  christos  * Since we're pretending to be a cg8, we put the main video RAM at the
    437  1.42  christos  *  same place the cg8 does, at offset 256k.  The cg8 has an enable
    438  1.42  christos  *  plane in the 256k space; our "enable" plane works differently.  We
    439  1.42  christos  *  can't emulate the enable plane very well, but all X uses it for is
    440  1.42  christos  *  to clear it at startup - so we map the first page of video RAM over
    441  1.42  christos  *  and over to fill that 256k space.  We also map some other views of
    442  1.42  christos  *  the video RAM space.
    443  1.42  christos  *
    444  1.42  christos  * Our memory map thus looks like
    445  1.42  christos  *
    446  1.42  christos  *	mmap range		space	base offset
    447  1.42  christos  *	00000000-00040000	vram	0 (multi-mapped - see above)
    448  1.42  christos  *	00040000-00434800	vram	00000000
    449  1.42  christos  *	01000000-01400000	vram	01000000
    450  1.42  christos  *	02000000-02200000	vram	02000000
    451  1.42  christos  *	02800000-02a00000	vram	02800000
    452  1.42  christos  *	03000000-03100000	vram	03000000
    453  1.42  christos  *	03400000-03500000	vram	03400000
    454  1.42  christos  *	03800000-03900000	vram	03800000
    455  1.42  christos  *	03c00000-03d00000	vram	03c00000
    456  1.42  christos  *	10000000-10010000	regs	00000000 (only if CG14_MAP_REGS)
    457   1.1    abrown  */
    458  1.17    simonb paddr_t
    459  1.40       uwe cgfourteenmmap(dev_t dev, off_t off, int prot)
    460   1.1    abrown {
    461  1.11        pk 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    462  1.12        pk 
    463   1.1    abrown 	if (off & PGOFSET)
    464   1.1    abrown 		panic("cgfourteenmmap");
    465  1.14       mrg 
    466  1.14       mrg 	if (off < 0)
    467  1.14       mrg 		return (-1);
    468   1.1    abrown 
    469  1.42  christos #if defined(CG14_MAP_REGS) /* XXX: security hole */
    470   1.1    abrown 	/*
    471  1.12        pk 	 * Map the control registers into user space. Should only be
    472   1.1    abrown 	 * used for debugging!
    473   1.1    abrown 	 */
    474   1.1    abrown 	if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
    475   1.1    abrown 		off -= 0x10000000;
    476  1.42  christos 		return (bus_space_mmap(sc->sc_bustag,
    477  1.21       eeh 			BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
    478  1.42  christos 				   sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
    479  1.21       eeh 			off, prot, BUS_SPACE_MAP_LINEAR));
    480   1.1    abrown 	}
    481   1.1    abrown #endif
    482  1.40       uwe 
    483  1.42  christos 	if (off < COLOUR_OFFSET)
    484  1.23   darrenr 		off = 0;
    485  1.42  christos 	else if (off < COLOUR_OFFSET+(1152*900*4))
    486  1.42  christos 		off -= COLOUR_OFFSET;
    487  1.42  christos 	else {
    488  1.42  christos 		switch (off >> 20) {
    489  1.42  christos 			case 0x010: case 0x011: case 0x012: case 0x013:
    490  1.42  christos 			case 0x020: case 0x021:
    491  1.42  christos 			case 0x028: case 0x029:
    492  1.42  christos 			case 0x030:
    493  1.42  christos 			case 0x034:
    494  1.42  christos 			case 0x038:
    495  1.42  christos 			case 0x03c:
    496  1.42  christos 				break;
    497  1.42  christos 			default:
    498  1.42  christos 				return(-1);
    499  1.42  christos 		}
    500   1.1    abrown 	}
    501   1.1    abrown 
    502  1.21       eeh 	return (bus_space_mmap(sc->sc_bustag,
    503  1.42  christos 		BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    504  1.42  christos 			   sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    505  1.21       eeh 		off, prot, BUS_SPACE_MAP_LINEAR));
    506  1.12        pk }
    507   1.1    abrown 
    508  1.42  christos int
    509  1.42  christos cgfourteenpoll(dev_t dev, int events, struct lwp *l)
    510  1.42  christos {
    511  1.42  christos 
    512  1.42  christos 	return (seltrue(dev, events, l));
    513  1.42  christos }
    514  1.42  christos 
    515   1.1    abrown /*
    516  1.12        pk  * Miscellaneous helper functions
    517   1.1    abrown  */
    518   1.1    abrown 
    519   1.1    abrown /* Initialize the framebuffer, storing away useful state for later reset */
    520  1.12        pk static void
    521  1.40       uwe cg14_init(struct cgfourteen_softc *sc)
    522   1.1    abrown {
    523  1.42  christos 	u_int32_t *clut;
    524  1.42  christos 	u_int8_t  *xlut;
    525  1.11        pk 	int i;
    526   1.1    abrown 
    527   1.1    abrown 	/*
    528   1.1    abrown 	 * We stash away the following to restore on close:
    529   1.1    abrown 	 *
    530   1.1    abrown 	 * 	color look-up table 1 	(sc->sc_saveclut)
    531   1.1    abrown 	 *	x look-up table		(sc->sc_savexlut)
    532   1.1    abrown 	 *	control register	(sc->sc_savectl)
    533   1.1    abrown 	 *	cursor control register (sc->sc_savehwc)
    534   1.1    abrown 	 */
    535   1.1    abrown 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    536   1.1    abrown 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    537   1.1    abrown 
    538  1.42  christos 	clut = (u_int32_t *) sc->sc_clut1->clut_lut;
    539  1.42  christos 	xlut = (u_int8_t *) sc->sc_xlut->xlut_lut;
    540   1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    541   1.1    abrown 		sc->sc_saveclut.cm_chip[i] = clut[i];
    542   1.1    abrown 		sc->sc_savexlut[i] = xlut[i];
    543   1.1    abrown 	}
    544   1.1    abrown 
    545   1.1    abrown #ifdef CG14_CG8
    546   1.1    abrown 	/*
    547   1.1    abrown 	 * Enable the video, and put in 24 bit mode.
    548   1.1    abrown 	 */
    549   1.1    abrown 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
    550   1.1    abrown 		CG14_MCTL_POWERCTL;
    551   1.1    abrown 
    552  1.12        pk 	/*
    553   1.1    abrown 	 * Zero the xlut to enable direct-color mode
    554   1.1    abrown 	 */
    555  1.42  christos 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    556  1.42  christos 		sc->sc_xlut->xlut_lut[i] = 0;
    557   1.1    abrown #else
    558   1.1    abrown 	/*
    559   1.1    abrown 	 * Enable the video and put it in 8 bit mode
    560   1.1    abrown 	 */
    561   1.1    abrown 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    562   1.1    abrown 		CG14_MCTL_POWERCTL;
    563   1.1    abrown #endif
    564   1.1    abrown }
    565   1.1    abrown 
    566  1.42  christos static void
    567  1.40       uwe /* Restore the state saved on cg14_init */
    568  1.40       uwe cg14_reset(struct cgfourteen_softc *sc)
    569   1.1    abrown {
    570  1.40       uwe 	volatile uint32_t *clut;
    571  1.40       uwe 	volatile uint8_t  *xlut;
    572  1.11        pk 	int i;
    573   1.1    abrown 
    574   1.1    abrown 	/*
    575   1.1    abrown 	 * We restore the following, saved in cg14_init:
    576   1.1    abrown 	 *
    577   1.1    abrown 	 * 	color look-up table 1 	(sc->sc_saveclut)
    578   1.1    abrown 	 *	x look-up table		(sc->sc_savexlut)
    579   1.1    abrown 	 *	control register	(sc->sc_savectl)
    580   1.1    abrown 	 *	cursor control register (sc->sc_savehwc)
    581   1.1    abrown 	 *
    582   1.1    abrown 	 * Note that we don't touch the video enable bits in the
    583   1.1    abrown 	 * control register; otherwise, screenblank wouldn't work.
    584   1.1    abrown 	 */
    585   1.1    abrown 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    586   1.1    abrown 							CG14_MCTL_POWERCTL)) |
    587   1.1    abrown 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    588   1.1    abrown 						    CG14_MCTL_POWERCTL));
    589   1.1    abrown 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    590   1.1    abrown 
    591  1.39   tsutsui 	clut = sc->sc_clut1->clut_lut;
    592  1.39   tsutsui 	xlut = sc->sc_xlut->xlut_lut;
    593   1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    594   1.1    abrown 		clut[i] = sc->sc_saveclut.cm_chip[i];
    595   1.1    abrown 		xlut[i] = sc->sc_savexlut[i];
    596   1.1    abrown 	}
    597   1.1    abrown }
    598   1.1    abrown 
    599   1.1    abrown /* Enable/disable video display; power down monitor if DPMS-capable */
    600   1.1    abrown static void
    601  1.40       uwe cg14_set_video(struct cgfourteen_softc *sc, int enable)
    602   1.1    abrown {
    603  1.12        pk 	/*
    604   1.1    abrown 	 * We can only use DPMS to power down the display if the chip revision
    605   1.1    abrown 	 * is greater than 0.
    606   1.1    abrown 	 */
    607   1.1    abrown 	if (enable) {
    608   1.1    abrown 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    609   1.1    abrown 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    610   1.1    abrown 						 CG14_MCTL_POWERCTL);
    611   1.1    abrown 		else
    612   1.1    abrown 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    613   1.1    abrown 	} else {
    614   1.1    abrown 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    615   1.1    abrown 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    616   1.1    abrown 						  CG14_MCTL_POWERCTL);
    617   1.1    abrown 		else
    618   1.1    abrown 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    619   1.1    abrown 	}
    620   1.1    abrown }
    621   1.1    abrown 
    622   1.1    abrown /* Get status of video display */
    623   1.1    abrown static int
    624  1.40       uwe cg14_get_video(struct cgfourteen_softc *sc)
    625   1.1    abrown {
    626   1.1    abrown 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    627   1.1    abrown }
    628   1.1    abrown 
    629   1.1    abrown /* Read the software shadow colormap */
    630  1.12        pk static int
    631  1.40       uwe cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    632   1.1    abrown {
    633  1.42  christos         u_int i, start, count;
    634  1.42  christos         u_char *cp;
    635  1.42  christos         int error;
    636  1.42  christos 
    637  1.42  christos         start = p->index;
    638  1.42  christos         count = p->count;
    639  1.42  christos         if (start >= cmsize || count > cmsize - start)
    640  1.42  christos                 return (EINVAL);
    641  1.42  christos 
    642  1.42  christos         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    643  1.42  christos                 error = copyout(&cp[3], &p->red[i], 1);
    644  1.42  christos                 if (error)
    645  1.42  christos                         return error;
    646  1.42  christos                 error = copyout(&cp[2], &p->green[i], 1);
    647  1.42  christos                 if (error)
    648  1.42  christos                         return error;
    649  1.42  christos                 error = copyout(&cp[1], &p->blue[i], 1);
    650  1.42  christos                 if (error)
    651  1.42  christos                         return error;
    652  1.42  christos         }
    653  1.42  christos         return (0);
    654   1.1    abrown }
    655   1.1    abrown 
    656   1.1    abrown /* Write the software shadow colormap */
    657   1.1    abrown static int
    658  1.40       uwe cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    659  1.24    itojun {
    660  1.42  christos         u_int i, start, count;
    661  1.42  christos         u_char *cp;
    662  1.42  christos         u_char cmap[256][4];
    663  1.42  christos         int error;
    664  1.42  christos 
    665  1.42  christos         start = p->index;
    666  1.42  christos         count = p->count;
    667  1.42  christos         if (start >= cmsize || count > cmsize - start)
    668  1.42  christos                 return (EINVAL);
    669  1.42  christos 
    670  1.42  christos         memcpy(&cmap, &cm->cm_map, sizeof cmap);
    671  1.42  christos         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    672  1.42  christos                 error = copyin(&p->red[i], &cp[3], 1);
    673  1.42  christos                 if (error)
    674  1.42  christos                         return error;
    675  1.42  christos                 error = copyin(&p->green[i], &cp[2], 1);
    676  1.42  christos                 if (error)
    677  1.42  christos                         return error;
    678  1.42  christos                 error = copyin(&p->blue[i], &cp[1], 1);
    679  1.42  christos                 if (error)
    680  1.42  christos                         return error;
    681  1.42  christos                 cp[0] = 0;      /* no alpha channel */
    682  1.42  christos         }
    683  1.42  christos         memcpy(&cm->cm_map, &cmap, sizeof cmap);
    684  1.42  christos         return (0);
    685   1.1    abrown }
    686   1.1    abrown 
    687   1.1    abrown static void
    688  1.40       uwe cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
    689   1.1    abrown {
    690   1.1    abrown 	/* XXX switch to auto-increment, and on retrace intr */
    691  1.40       uwe 
    692   1.1    abrown 	/* Setup pointers to source and dest */
    693  1.40       uwe 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
    694  1.40       uwe 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
    695   1.1    abrown 
    696   1.1    abrown 	/* Copy by words */
    697   1.1    abrown 	while (--ncolors >= 0)
    698   1.1    abrown 		*lutp++ = *colp++;
    699   1.1    abrown }
    700