Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.49
      1 /*	$NetBSD: cgfourteen.c,v 1.49 2007/07/30 18:20:09 macallan Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996
      5  *	The President and Fellows of Harvard College. All rights reserved.
      6  * Copyright (c) 1992, 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * This software was developed by the Computer Systems Engineering group
     10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     11  * contributed to Berkeley.
     12  *
     13  * All advertising materials mentioning features or use of this software
     14  * must display the following acknowledgement:
     15  *	This product includes software developed by Harvard University.
     16  *	This product includes software developed by the University of
     17  *	California, Lawrence Berkeley Laboratory.
     18  *
     19  * Redistribution and use in source and binary forms, with or without
     20  * modification, are permitted provided that the following conditions
     21  * are met:
     22  * 1. Redistributions of source code must retain the above copyright
     23  *    notice, this list of conditions and the following disclaimer.
     24  * 2. Redistributions in binary form must reproduce the above copyright
     25  *    notice, this list of conditions and the following disclaimer in the
     26  *    documentation and/or other materials provided with the distribution.
     27  * 3. All advertising materials mentioning features or use of this software
     28  *    must display the following acknowledgement:
     29  *	This product includes software developed by the University of
     30  *	California, Berkeley and its contributors.
     31  *	This product includes software developed by Harvard University and
     32  *	its contributors.
     33  * 4. Neither the name of the University nor the names of its contributors
     34  *    may be used to endorse or promote products derived from this software
     35  *    without specific prior written permission.
     36  *
     37  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     47  * SUCH DAMAGE.
     48  *
     49  *   Based on:
     50  *	NetBSD: cgthree.c,v 1.28 1996/05/31 09:59:22 pk Exp
     51  *	NetBSD: cgsix.c,v 1.25 1996/04/01 17:30:00 christos Exp
     52  */
     53 
     54 /*
     55  * Driver for Campus-II on-board mbus-based video (cgfourteen).
     56  * Provides minimum emulation of a Sun cgthree 8-bit framebuffer to
     57  * allow X to run.
     58  *
     59  * Does not handle interrupts, even though they can occur.
     60  *
     61  * XXX should defer colormap updates to vertical retrace interrupts
     62  */
     63 
     64 /*
     65  * The following is for debugging only; it opens up a security hole
     66  * enabled by allowing any user to map the control registers for the
     67  * cg14 into their space.
     68  */
     69 #undef CG14_MAP_REGS
     70 
     71 /*
     72  * The following enables 24-bit operation: when opened, the framebuffer
     73  * will switch to 24-bit mode (actually 32-bit mode), and provide a
     74  * simple cg8 emulation.
     75  */
     76 #define CG14_CG8
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/buf.h>
     81 #include <sys/device.h>
     82 #include <sys/ioctl.h>
     83 #include <sys/malloc.h>
     84 #include <sys/mman.h>
     85 #include <sys/tty.h>
     86 #include <sys/conf.h>
     87 
     88 #include <uvm/uvm_extern.h>
     89 
     90 #include <dev/sun/fbio.h>
     91 #include <machine/autoconf.h>
     92 #include <machine/pmap.h>
     93 #include <dev/sun/fbvar.h>
     94 #include <machine/cpu.h>
     95 #include <dev/sbus/sbusvar.h>
     96 
     97 #include "wsdisplay.h"
     98 #include <dev/wscons/wsconsio.h>
     99 #include <dev/wsfont/wsfont.h>
    100 #include <dev/rasops/rasops.h>
    101 
    102 #include <dev/wscons/wsdisplay_vconsvar.h>
    103 
    104 #include <sparc/dev/cgfourteenreg.h>
    105 #include <sparc/dev/cgfourteenvar.h>
    106 
    107 #include "opt_wsemul.h"
    108 
    109 /* autoconfiguration driver */
    110 static int	cgfourteenmatch(struct device *, struct cfdata *, void *);
    111 static void	cgfourteenattach(struct device *, struct device *, void *);
    112 static void	cgfourteenunblank(struct device *);
    113 
    114 CFATTACH_DECL(cgfourteen, sizeof(struct cgfourteen_softc),
    115     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    116 
    117 extern struct cfdriver cgfourteen_cd;
    118 
    119 dev_type_open(cgfourteenopen);
    120 dev_type_close(cgfourteenclose);
    121 dev_type_ioctl(cgfourteenioctl);
    122 dev_type_mmap(cgfourteenmmap);
    123 dev_type_poll(cgfourteenpoll);
    124 
    125 const struct cdevsw cgfourteen_cdevsw = {
    126         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    127         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
    128 };
    129 
    130 /* frame buffer generic driver */
    131 static struct fbdriver cgfourteenfbdriver = {
    132 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    133 	cgfourteenpoll, cgfourteenmmap, nokqfilter
    134 };
    135 
    136 extern struct tty *fbconstty;
    137 
    138 static void cg14_set_video(struct cgfourteen_softc *, int);
    139 static int  cg14_get_video(struct cgfourteen_softc *);
    140 static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
    141 static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
    142 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
    143 static void cg14_init(struct cgfourteen_softc *);
    144 static void cg14_reset(struct cgfourteen_softc *);
    145 
    146 #if NWSDISPLAY > 0
    147 static void cg14_setup_wsdisplay(struct cgfourteen_softc *, int);
    148 static void cg14_init_cmap(struct cgfourteen_softc *);
    149 static int  cg14_putcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    150 static int  cg14_getcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    151 static void cg14_set_depth(struct cgfourteen_softc *, int);
    152 #endif
    153 
    154 #if defined(RASTERCONSOLE) && (NWSDISPLAY > 0)
    155 #error You can't have it both ways - either RASTERCONSOLE or wsdisplay
    156 #endif
    157 
    158 /*
    159  * Match a cgfourteen.
    160  */
    161 int
    162 cgfourteenmatch(struct device *parent, struct cfdata *cf, void *aux)
    163 {
    164 	union obio_attach_args *uoba = aux;
    165 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    166 
    167 	/*
    168 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    169 	 * via the processor, and not through device space or an external
    170 	 * bus. Thus we look _only_ at the obio bus.
    171 	 * Additionally, these things exist only on the Sun4m.
    172 	 */
    173 
    174 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    175 		return (0);
    176 
    177 	/* Check driver name */
    178 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    179 }
    180 
    181 /*
    182  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
    183  *  space for faked overlay junk for the cg8 emulation.
    184  *
    185  * As it happens, this value is correct for both cg3 and cg8 emulation!
    186  */
    187 #define COLOUR_OFFSET (256*1024)
    188 
    189 #ifdef RASTERCONSOLE
    190 static void cg14_set_rcons_luts(struct cgfourteen_softc *sc)
    191 {
    192 	int i;
    193 
    194 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_xlut->xlut_lut[i] = 0x22;
    195 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_clut2->clut_lut[i] = 0x00ffffff;
    196 	sc->sc_clut2->clut_lut[0] = 0x00ffffff;
    197 	sc->sc_clut2->clut_lut[255] = 0;
    198 }
    199 #endif /* RASTERCONSOLE */
    200 
    201 #if NWSDISPLAY > 0
    202 static int	cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    203 static paddr_t	cg14_mmap(void *, void *, off_t, int);
    204 static void	cg14_init_screen(void *, struct vcons_screen *, int, long *);
    205 
    206 
    207 struct wsdisplay_accessops cg14_accessops = {
    208 	cg14_ioctl,
    209 	cg14_mmap,
    210 	NULL,	/* alloc_screen */
    211 	NULL,	/* free_screen */
    212 	NULL,	/* show_screen */
    213 	NULL, 	/* load_font */
    214 	NULL,	/* pollc */
    215 	NULL	/* scroll */
    216 };
    217 #endif
    218 
    219 /*
    220  * Attach a display.  We need to notice if it is the console, too.
    221  */
    222 void
    223 cgfourteenattach(struct device *parent, struct device *self, void *aux)
    224 {
    225 	union obio_attach_args *uoba = aux;
    226 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    227 	struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
    228 	struct fbdevice *fb = &sc->sc_fb;
    229 	bus_space_handle_t bh;
    230 	int node, ramsize;
    231 	volatile uint32_t *lut;
    232 	int i, isconsole;
    233 
    234 	node = sa->sa_node;
    235 
    236 	/* Remember cookies for cgfourteenmmap() */
    237 	sc->sc_bustag = sa->sa_bustag;
    238 
    239 	fb->fb_driver = &cgfourteenfbdriver;
    240 	fb->fb_device = &sc->sc_dev;
    241 	/* Mask out invalid flags from the user. */
    242 	fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
    243 
    244 	/*
    245 	 * We're emulating a cg3/8, so represent ourselves as one
    246 	 */
    247 #ifdef CG14_CG8
    248 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
    249 	fb->fb_type.fb_depth = 32;
    250 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    251 	ramsize = roundup(fb->fb_type.fb_height * 1152 * 4, NBPG);
    252 #else
    253 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
    254 	fb->fb_type.fb_depth = 8;
    255 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    256 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
    257 #endif
    258 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    259 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
    260 
    261 	if (sa->sa_nreg < 2) {
    262 		printf("%s: only %d register sets\n",
    263 			self->dv_xname, sa->sa_nreg);
    264 		return;
    265 	}
    266 	bcopy(sa->sa_reg, sc->sc_physadr,
    267 	      sa->sa_nreg * sizeof(struct sbus_reg));
    268 
    269 	sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
    270 
    271 	printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
    272 	/*
    273 	 * Now map in the 8 useful pages of registers
    274 	 */
    275 	if (sa->sa_size < 0x10000) {
    276 #ifdef DIAGNOSTIC
    277 		printf("warning: can't find all cgfourteen registers...\n");
    278 #endif
    279 		sa->sa_size = 0x10000;
    280 	}
    281 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    282 			 sa->sa_offset,
    283 			 sa->sa_size,
    284 			 BUS_SPACE_MAP_LINEAR,
    285 			 &bh) != 0) {
    286 		printf("%s: cannot map control registers\n", self->dv_xname);
    287 		return;
    288 	}
    289 	sc->sc_regh = bh;
    290 
    291 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    292 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    293 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    294 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    295 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    296 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    297 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    298 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    299 
    300 	/*
    301 	 * Let the user know that we're here
    302 	 */
    303 #ifdef CG14_CG8
    304 	printf(": cgeight emulated at %dx%dx24bpp",
    305 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    306 #else
    307 	printf(": cgthree emulated at %dx%dx8bpp",
    308 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    309 #endif
    310 	/*
    311 	 * Enable the video.
    312 	 */
    313 	cg14_set_video(sc, 1);
    314 
    315 	/*
    316 	 * Grab the initial colormap
    317 	 */
    318 	lut = sc->sc_clut1->clut_lut;
    319 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    320 		sc->sc_cmap.cm_chip[i] = lut[i];
    321 
    322 	/* See if we're the console */
    323         isconsole = fb_is_console(node);
    324 
    325 #if defined(RASTERCONSOLE)
    326 	if (isconsole) {
    327 		printf(" (console)\n");
    328 		/* *sbus*_bus_map?  but that's how we map the regs... */
    329 		if (sbus_bus_map( sc->sc_bustag,
    330 				  sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    331 				  sc->sc_physadr[CG14_PXL_IDX].sbr_offset +
    332 				    0x03800000,
    333 				  1152 * 900, BUS_SPACE_MAP_LINEAR,
    334 				  &bh) != 0) {
    335 			printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
    336 			return;
    337 		}
    338 		sc->sc_rcfb = sc->sc_fb;
    339 		sc->sc_rcfb.fb_type.fb_type = FBTYPE_SUN3COLOR;
    340 		sc->sc_rcfb.fb_type.fb_depth = 8;
    341 		sc->sc_rcfb.fb_linebytes = 1152;
    342 		sc->sc_rcfb.fb_type.fb_size = roundup(1152*900,NBPG);
    343 		sc->sc_rcfb.fb_pixels = (void *)bh;
    344 
    345 		printf("vram at %p\n",(void *)bh);
    346 		/* XXX should use actual screen size */
    347 
    348 		for (i = 0; i < 1152 * 900; i++)
    349 		    ((unsigned char *)bh)[i] = 0;
    350 		fbrcons_init(&sc->sc_rcfb);
    351 		cg14_set_rcons_luts(sc);
    352 		sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID |
    353 		    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL;
    354 	} else
    355 		printf("\n");
    356 #endif
    357 
    358 #if NWSDISPLAY > 0
    359 	if (sbus_bus_map( sc->sc_bustag,
    360 	    sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    361 	    sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
    362 	    ramsize, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    363 		printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
    364 		return;
    365 	}
    366 
    367 	sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
    368 	if (isconsole)
    369 		printf(" (console)\n");
    370 
    371 	sc->sc_depth = 8;
    372 	cg14_setup_wsdisplay(sc, isconsole);
    373 #endif
    374 
    375 	/* Attach to /dev/fb */
    376 	fb_attach(&sc->sc_fb, isconsole);
    377 }
    378 
    379 /*
    380  * Keep track of the number of opens made. In the 24-bit driver, we need to
    381  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    382  * the last close. This kind of nonsense is needed to give screenblank
    383  * a fighting chance of working.
    384  */
    385 static int cg14_opens = 0;
    386 
    387 int
    388 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
    389 {
    390 	struct cgfourteen_softc *sc;
    391 	int unit;
    392 	int s, oldopens;
    393 
    394 	unit = minor(dev);
    395 	if (unit >= cgfourteen_cd.cd_ndevs)
    396 		return(ENXIO);
    397 	sc = cgfourteen_cd.cd_devs[minor(dev)];
    398 	if (sc == NULL)
    399 		return(ENXIO);
    400 	s = splhigh();
    401 	oldopens = cg14_opens++;
    402 	splx(s);
    403 
    404 	/* Setup the cg14 as we want it, and save the original PROM state */
    405 	if (oldopens == 0)	/* first open only, to make screenblank work */
    406 		cg14_init(sc);
    407 
    408 	return (0);
    409 }
    410 
    411 int
    412 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
    413 {
    414 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    415 	int s, opens;
    416 
    417 	s = splhigh();
    418 	opens = --cg14_opens;
    419 	if (cg14_opens < 0)
    420 		opens = cg14_opens = 0;
    421 	splx(s);
    422 
    423 	/*
    424 	 * Restore video state to make the PROM happy, on last close.
    425 	 */
    426 	if (opens == 0)
    427 		cg14_reset(sc);
    428 
    429 	return (0);
    430 }
    431 
    432 int
    433 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    434 {
    435 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    436 	struct fbgattr *fba;
    437 	int error;
    438 
    439 	switch (cmd) {
    440 
    441 	case FBIOGTYPE:
    442 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    443 		break;
    444 
    445 	case FBIOGATTR:
    446 		fba = (struct fbgattr *)data;
    447 		fba->real_type = FBTYPE_MDICOLOR;
    448 		fba->owner = 0;		/* XXX ??? */
    449 		fba->fbtype = sc->sc_fb.fb_type;
    450 		fba->sattr.flags = 0;
    451 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    452 		fba->sattr.dev_specific[0] = -1;
    453 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    454 		fba->emu_types[1] = -1;
    455 		break;
    456 
    457 	case FBIOGETCMAP:
    458 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    459 				     CG14_CLUT_SIZE));
    460 
    461 	case FBIOPUTCMAP:
    462 		/* copy to software map */
    463 #define p ((struct fbcmap *)data)
    464 #ifdef CG14_CG8
    465 		p->index &= 0xffffff;
    466 #endif
    467 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    468 		if (error)
    469 			return (error);
    470 		/* now blast them into the chip */
    471 		/* XXX should use retrace interrupt */
    472 		cg14_load_hwcmap(sc, p->index, p->count);
    473 #undef p
    474 		break;
    475 
    476 	case FBIOGVIDEO:
    477 		*(int *)data = cg14_get_video(sc);
    478 		break;
    479 
    480 	case FBIOSVIDEO:
    481 		cg14_set_video(sc, *(int *)data);
    482 		break;
    483 
    484 	default:
    485 		return (ENOTTY);
    486 	}
    487 	return (0);
    488 }
    489 
    490 /*
    491  * Undo the effect of an FBIOSVIDEO that turns the video off.
    492  */
    493 static void
    494 cgfourteenunblank(struct device *dev)
    495 {
    496 
    497 	cg14_set_video((struct cgfourteen_softc *)dev, 1);
    498 }
    499 
    500 /*
    501  * Return the address that would map the given device at the given
    502  * offset, allowing for the given protection, or return -1 for error.
    503  *
    504  * Since we're pretending to be a cg8, we put the main video RAM at the
    505  *  same place the cg8 does, at offset 256k.  The cg8 has an enable
    506  *  plane in the 256k space; our "enable" plane works differently.  We
    507  *  can't emulate the enable plane very well, but all X uses it for is
    508  *  to clear it at startup - so we map the first page of video RAM over
    509  *  and over to fill that 256k space.  We also map some other views of
    510  *  the video RAM space.
    511  *
    512  * Our memory map thus looks like
    513  *
    514  *	mmap range		space	base offset
    515  *	00000000-00040000	vram	0 (multi-mapped - see above)
    516  *	00040000-00434800	vram	00000000
    517  *	01000000-01400000	vram	01000000
    518  *	02000000-02200000	vram	02000000
    519  *	02800000-02a00000	vram	02800000
    520  *	03000000-03100000	vram	03000000
    521  *	03400000-03500000	vram	03400000
    522  *	03800000-03900000	vram	03800000
    523  *	03c00000-03d00000	vram	03c00000
    524  *	10000000-10010000	regs	00000000 (only if CG14_MAP_REGS)
    525  */
    526 paddr_t
    527 cgfourteenmmap(dev_t dev, off_t off, int prot)
    528 {
    529 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    530 
    531 	if (off & PGOFSET)
    532 		panic("cgfourteenmmap");
    533 
    534 	if (off < 0)
    535 		return (-1);
    536 
    537 #if defined(CG14_MAP_REGS) /* XXX: security hole */
    538 	/*
    539 	 * Map the control registers into user space. Should only be
    540 	 * used for debugging!
    541 	 */
    542 	if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
    543 		off -= 0x10000000;
    544 		return (bus_space_mmap(sc->sc_bustag,
    545 			BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
    546 				   sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
    547 			off, prot, BUS_SPACE_MAP_LINEAR));
    548 	}
    549 #endif
    550 
    551 	if (off < COLOUR_OFFSET)
    552 		off = 0;
    553 	else if (off < COLOUR_OFFSET+(1152*900*4))
    554 		off -= COLOUR_OFFSET;
    555 	else {
    556 		switch (off >> 20) {
    557 			case 0x010: case 0x011: case 0x012: case 0x013:
    558 			case 0x020: case 0x021:
    559 			case 0x028: case 0x029:
    560 			case 0x030:
    561 			case 0x034:
    562 			case 0x038:
    563 			case 0x03c:
    564 				break;
    565 			default:
    566 				return(-1);
    567 		}
    568 	}
    569 
    570 	return (bus_space_mmap(sc->sc_bustag,
    571 		BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    572 			   sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    573 		off, prot, BUS_SPACE_MAP_LINEAR));
    574 }
    575 
    576 int
    577 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
    578 {
    579 
    580 	return (seltrue(dev, events, l));
    581 }
    582 
    583 /*
    584  * Miscellaneous helper functions
    585  */
    586 
    587 /* Initialize the framebuffer, storing away useful state for later reset */
    588 static void
    589 cg14_init(struct cgfourteen_softc *sc)
    590 {
    591 	volatile uint32_t *clut;
    592 	volatile uint8_t  *xlut;
    593 	int i;
    594 
    595 	/*
    596 	 * We stash away the following to restore on close:
    597 	 *
    598 	 * 	color look-up table 1 	(sc->sc_saveclut)
    599 	 *	x look-up table		(sc->sc_savexlut)
    600 	 *	control register	(sc->sc_savectl)
    601 	 *	cursor control register (sc->sc_savehwc)
    602 	 */
    603 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    604 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    605 
    606 	clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
    607 	xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
    608 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    609 		sc->sc_saveclut.cm_chip[i] = clut[i];
    610 		sc->sc_savexlut[i] = xlut[i];
    611 	}
    612 
    613 #ifdef CG14_CG8
    614 	/*
    615 	 * Enable the video, and put in 24 bit mode.
    616 	 */
    617 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
    618 		CG14_MCTL_POWERCTL;
    619 
    620 	/*
    621 	 * Zero the xlut to enable direct-color mode
    622 	 */
    623 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    624 		sc->sc_xlut->xlut_lut[i] = 0;
    625 #else
    626 	/*
    627 	 * Enable the video and put it in 8 bit mode
    628 	 */
    629 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    630 		CG14_MCTL_POWERCTL;
    631 #endif
    632 }
    633 
    634 static void
    635 /* Restore the state saved on cg14_init */
    636 cg14_reset(struct cgfourteen_softc *sc)
    637 {
    638 	volatile uint32_t *clut;
    639 	volatile uint8_t  *xlut;
    640 	int i;
    641 
    642 	/*
    643 	 * We restore the following, saved in cg14_init:
    644 	 *
    645 	 * 	color look-up table 1 	(sc->sc_saveclut)
    646 	 *	x look-up table		(sc->sc_savexlut)
    647 	 *	control register	(sc->sc_savectl)
    648 	 *	cursor control register (sc->sc_savehwc)
    649 	 *
    650 	 * Note that we don't touch the video enable bits in the
    651 	 * control register; otherwise, screenblank wouldn't work.
    652 	 */
    653 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    654 							CG14_MCTL_POWERCTL)) |
    655 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    656 						    CG14_MCTL_POWERCTL));
    657 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    658 
    659 	clut = sc->sc_clut1->clut_lut;
    660 	xlut = sc->sc_xlut->xlut_lut;
    661 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    662 		clut[i] = sc->sc_saveclut.cm_chip[i];
    663 		xlut[i] = sc->sc_savexlut[i];
    664 	}
    665 }
    666 
    667 /* Enable/disable video display; power down monitor if DPMS-capable */
    668 static void
    669 cg14_set_video(struct cgfourteen_softc *sc, int enable)
    670 {
    671 	/*
    672 	 * We can only use DPMS to power down the display if the chip revision
    673 	 * is greater than 0.
    674 	 */
    675 	if (enable) {
    676 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    677 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    678 						 CG14_MCTL_POWERCTL);
    679 		else
    680 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    681 	} else {
    682 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    683 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    684 						  CG14_MCTL_POWERCTL);
    685 		else
    686 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    687 	}
    688 }
    689 
    690 /* Get status of video display */
    691 static int
    692 cg14_get_video(struct cgfourteen_softc *sc)
    693 {
    694 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    695 }
    696 
    697 /* Read the software shadow colormap */
    698 static int
    699 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    700 {
    701         u_int i, start, count;
    702         u_char *cp;
    703         int error;
    704 
    705         start = p->index;
    706         count = p->count;
    707         if (start >= cmsize || count > cmsize - start)
    708                 return (EINVAL);
    709 
    710         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    711                 error = copyout(&cp[3], &p->red[i], 1);
    712                 if (error)
    713                         return error;
    714                 error = copyout(&cp[2], &p->green[i], 1);
    715                 if (error)
    716                         return error;
    717                 error = copyout(&cp[1], &p->blue[i], 1);
    718                 if (error)
    719                         return error;
    720         }
    721         return (0);
    722 }
    723 
    724 /* Write the software shadow colormap */
    725 static int
    726 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    727 {
    728         u_int i, start, count;
    729         u_char *cp;
    730         u_char cmap[256][4];
    731         int error;
    732 
    733         start = p->index;
    734         count = p->count;
    735         if (start >= cmsize || count > cmsize - start)
    736                 return (EINVAL);
    737 
    738         memcpy(&cmap, &cm->cm_map, sizeof cmap);
    739         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    740                 error = copyin(&p->red[i], &cp[3], 1);
    741                 if (error)
    742                         return error;
    743                 error = copyin(&p->green[i], &cp[2], 1);
    744                 if (error)
    745                         return error;
    746                 error = copyin(&p->blue[i], &cp[1], 1);
    747                 if (error)
    748                         return error;
    749                 cp[0] = 0;      /* no alpha channel */
    750         }
    751         memcpy(&cm->cm_map, &cmap, sizeof cmap);
    752         return (0);
    753 }
    754 
    755 static void
    756 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
    757 {
    758 	/* XXX switch to auto-increment, and on retrace intr */
    759 
    760 	/* Setup pointers to source and dest */
    761 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
    762 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
    763 
    764 	/* Copy by words */
    765 	while (--ncolors >= 0)
    766 		*lutp++ = *colp++;
    767 }
    768 
    769 #if NWSDISPLAY > 0
    770 static void
    771 cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
    772 {
    773 	struct wsemuldisplaydev_attach_args aa;
    774 	struct rasops_info *ri;
    775 	long defattr;
    776 
    777  	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    778 		"default",
    779 		0, 0,
    780 		NULL,
    781 		8, 16,
    782 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    783 		NULL
    784 	};
    785 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    786 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    787 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    788 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
    789 	    &cg14_accessops);
    790 	sc->sc_vd.init_screen = cg14_init_screen;
    791 
    792 	ri = &sc->sc_console_screen.scr_ri;
    793 
    794 	if (is_cons) {
    795 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
    796 		    &defattr);
    797 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    798 
    799 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    800 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    801 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    802 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    803 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    804 		    defattr);
    805 	} else {
    806 		/*
    807 		 * since we're not the console we can postpone the rest
    808 		 * until someone actually allocates a screen for us
    809 		 */
    810 	}
    811 
    812 	cg14_init_cmap(sc);
    813 
    814 	aa.console = is_cons;
    815 	aa.scrdata = &sc->sc_screenlist;
    816 	aa.accessops = &cg14_accessops;
    817 	aa.accesscookie = &sc->sc_vd;
    818 
    819 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    820 }
    821 
    822 static void
    823 cg14_init_cmap(struct cgfourteen_softc *sc)
    824 {
    825 	int i, j = 0;
    826 
    827 	for (i = 0; i < 256; i++) {
    828 
    829 		sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
    830 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
    831 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
    832 		j += 3;
    833 	}
    834 	cg14_load_hwcmap(sc, 0, 256);
    835 }
    836 
    837 static int
    838 cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    839 {
    840 	u_int index = cm->index;
    841 	u_int count = cm->count;
    842 	int i, error;
    843 	u_char rbuf[256], gbuf[256], bbuf[256];
    844 
    845 	if (cm->index >= 256 || cm->count > 256 ||
    846 	    (cm->index + cm->count) > 256)
    847 		return EINVAL;
    848 	error = copyin(cm->red, &rbuf[index], count);
    849 	if (error)
    850 		return error;
    851 	error = copyin(cm->green, &gbuf[index], count);
    852 	if (error)
    853 		return error;
    854 	error = copyin(cm->blue, &bbuf[index], count);
    855 	if (error)
    856 		return error;
    857 
    858 	for (i = 0; i < count; i++) {
    859 		sc->sc_cmap.cm_map[index][3] = rbuf[index];
    860 		sc->sc_cmap.cm_map[index][2] = gbuf[index];
    861 		sc->sc_cmap.cm_map[index][1] = bbuf[index];
    862 
    863 		index++;
    864 	}
    865 	cg14_load_hwcmap(sc, 0, 256);
    866 	return 0;
    867 }
    868 
    869 static int
    870 cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    871 {
    872 	uint8_t rbuf[256], gbuf[256], bbuf[256];
    873 	u_int index = cm->index;
    874 	u_int count = cm->count;
    875 	int error, i;
    876 
    877 	if (index >= 255 || count > 256 || index + count > 256)
    878 		return EINVAL;
    879 
    880 
    881 	for (i = 0; i < count; i++) {
    882 		rbuf[i] = sc->sc_cmap.cm_map[index][3];
    883 		gbuf[i] = sc->sc_cmap.cm_map[index][2];
    884 		bbuf[i] = sc->sc_cmap.cm_map[index][1];
    885 
    886 		index++;
    887 	}
    888 	error = copyout(rbuf,   cm->red,   count);
    889 	if (error)
    890 		return error;
    891 	error = copyout(gbuf, cm->green, count);
    892 	if (error)
    893 		return error;
    894 	error = copyout(bbuf,  cm->blue,  count);
    895 	if (error)
    896 		return error;
    897 
    898 	return 0;
    899 }
    900 static int
    901 cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    902 	struct lwp *l)
    903 {
    904 	struct vcons_data *vd = v;
    905 	struct cgfourteen_softc *sc = vd->cookie;
    906 	struct wsdisplay_fbinfo *wdf;
    907 	struct vcons_screen *ms = vd->active;
    908 
    909 	switch (cmd) {
    910 
    911 		case WSDISPLAYIO_GTYPE:
    912 			*(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
    913 			return 0;
    914 
    915 		case WSDISPLAYIO_GINFO:
    916 			wdf = (void *)data;
    917 			wdf->height = ms->scr_ri.ri_height;
    918 			wdf->width = ms->scr_ri.ri_width;
    919 			wdf->depth = 32;
    920 			wdf->cmsize = 256;
    921 			return 0;
    922 
    923 		case WSDISPLAYIO_GETCMAP:
    924 			return cg14_getcmap(sc,
    925 			    (struct wsdisplay_cmap *)data);
    926 
    927 		case WSDISPLAYIO_PUTCMAP:
    928 			return cg14_putcmap(sc,
    929 			    (struct wsdisplay_cmap *)data);
    930 
    931 		case WSDISPLAYIO_LINEBYTES:
    932 			*(u_int *)data = ms->scr_ri.ri_width << 2;
    933 			return 0;
    934 
    935 		case WSDISPLAYIO_SMODE:
    936 			{
    937 				int new_mode = *(int*)data;
    938 				if (new_mode != sc->sc_mode) {
    939 					sc->sc_mode = new_mode;
    940 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    941 						cg14_set_depth(sc, 8);
    942 						cg14_init_cmap(sc);
    943 						vcons_redraw_screen(ms);
    944 					} else
    945 						cg14_set_depth(sc, 32);
    946 				}
    947 			}
    948 			return 0;
    949 	}
    950 	return EPASSTHROUGH;
    951 }
    952 
    953 static paddr_t
    954 cg14_mmap(void *v, void *vs, off_t offset, int prot)
    955 {
    956 	struct vcons_data *vd = v;
    957 	struct cgfourteen_softc *sc = vd->cookie;
    958 
    959 	/* allow mmap()ing the full framebuffer, not just what we use */
    960 	if (offset < sc->sc_vramsize)
    961 		return bus_space_mmap(sc->sc_bustag,
    962 		    BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    963 		      sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    964 		    offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
    965 
    966 	return -1;
    967 }
    968 
    969 static void
    970 cg14_init_screen(void *cookie, struct vcons_screen *scr,
    971     int existing, long *defattr)
    972 {
    973 	struct cgfourteen_softc *sc = cookie;
    974 	struct rasops_info *ri = &scr->scr_ri;
    975 
    976 	ri->ri_depth = 8;
    977 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
    978 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
    979 	ri->ri_stride = ri->ri_width;
    980 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    981 
    982 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
    983 
    984 	if (existing) {
    985 		ri->ri_flg |= RI_CLEAR;
    986 	}
    987 
    988 	rasops_init(ri, sc->sc_fb.fb_type.fb_height / 8,
    989 	     sc->sc_fb.fb_type.fb_width / 8);
    990 	ri->ri_caps = WSSCREEN_WSCOLORS;
    991 
    992 	rasops_reconfig(ri,
    993 	    sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
    994 	    sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
    995 
    996 	ri->ri_hw = scr;
    997 }
    998 
    999 static void
   1000 cg14_set_depth(struct cgfourteen_softc *sc, int depth)
   1001 {
   1002 	int i;
   1003 
   1004 	if (sc->sc_depth == depth)
   1005 		return;
   1006 	switch (depth) {
   1007 		case 8:
   1008 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1009 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1010 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
   1011 			sc->sc_depth = 8;
   1012 			/* everything is CLUT1 */
   1013 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1014 			     sc->sc_xlut->xlut_lut[i] = 0;
   1015 			break;
   1016 		case 32:
   1017 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1018 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1019 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
   1020 			sc->sc_depth = 32;
   1021 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1022 			     sc->sc_xlut->xlut_lut[i] = 0;
   1023 			break;
   1024 		default:
   1025 			printf("%s: can't change to depth %d\n",
   1026 			    sc->sc_dev.dv_xname, depth);
   1027 	}
   1028 }
   1029 #endif
   1030