Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.69
      1 /*	$NetBSD: cgfourteen.c,v 1.69 2013/02/05 21:45:39 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 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/buf.h>
     74 #include <sys/device.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/malloc.h>
     77 #include <sys/kmem.h>
     78 #include <sys/mman.h>
     79 #include <sys/tty.h>
     80 #include <sys/conf.h>
     81 #include <dev/pci/pciio.h>
     82 
     83 #include <uvm/uvm_extern.h>
     84 
     85 #include <dev/sun/fbio.h>
     86 #include <machine/autoconf.h>
     87 #include <machine/pmap.h>
     88 #include <dev/sun/fbvar.h>
     89 #include <machine/cpu.h>
     90 #include <dev/sbus/sbusvar.h>
     91 
     92 #include "wsdisplay.h"
     93 #include <dev/wscons/wsconsio.h>
     94 #include <dev/wsfont/wsfont.h>
     95 #include <dev/rasops/rasops.h>
     96 
     97 #include <dev/wscons/wsdisplay_vconsvar.h>
     98 
     99 #include <sparc/dev/cgfourteenreg.h>
    100 #include <sparc/dev/cgfourteenvar.h>
    101 
    102 #include "opt_wsemul.h"
    103 
    104 /* autoconfiguration driver */
    105 static int	cgfourteenmatch(device_t, struct cfdata *, void *);
    106 static void	cgfourteenattach(device_t, device_t, void *);
    107 static void	cgfourteenunblank(device_t);
    108 
    109 CFATTACH_DECL_NEW(cgfourteen, sizeof(struct cgfourteen_softc),
    110     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    111 
    112 extern struct cfdriver cgfourteen_cd;
    113 
    114 dev_type_open(cgfourteenopen);
    115 dev_type_close(cgfourteenclose);
    116 dev_type_ioctl(cgfourteenioctl);
    117 dev_type_mmap(cgfourteenmmap);
    118 dev_type_poll(cgfourteenpoll);
    119 
    120 const struct cdevsw cgfourteen_cdevsw = {
    121         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    122         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
    123 };
    124 
    125 /* frame buffer generic driver */
    126 static struct fbdriver cgfourteenfbdriver = {
    127 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    128 	cgfourteenpoll, cgfourteenmmap, nokqfilter
    129 };
    130 
    131 static void cg14_set_video(struct cgfourteen_softc *, int);
    132 static int  cg14_get_video(struct cgfourteen_softc *);
    133 static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
    134 static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
    135 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
    136 static void cg14_init(struct cgfourteen_softc *);
    137 static void cg14_reset(struct cgfourteen_softc *);
    138 
    139 #if NWSDISPLAY > 0
    140 static void cg14_setup_wsdisplay(struct cgfourteen_softc *, int);
    141 static void cg14_init_cmap(struct cgfourteen_softc *);
    142 static int  cg14_putcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    143 static int  cg14_getcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    144 static void cg14_set_depth(struct cgfourteen_softc *, int);
    145 static void cg14_move_cursor(struct cgfourteen_softc *, int, int);
    146 static int  cg14_do_cursor(struct cgfourteen_softc *,
    147                            struct wsdisplay_cursor *);
    148 #endif
    149 
    150 /*
    151  * Match a cgfourteen.
    152  */
    153 int
    154 cgfourteenmatch(device_t parent, struct cfdata *cf, void *aux)
    155 {
    156 	union obio_attach_args *uoba = aux;
    157 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    158 
    159 	/*
    160 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    161 	 * via the processor, and not through device space or an external
    162 	 * bus. Thus we look _only_ at the obio bus.
    163 	 * Additionally, these things exist only on the Sun4m.
    164 	 */
    165 
    166 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    167 		return (0);
    168 
    169 	/* Check driver name */
    170 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    171 }
    172 
    173 /*
    174  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
    175  *  space for faked overlay junk for the cg8 emulation.
    176  *
    177  * As it happens, this value is correct for both cg3 and cg8 emulation!
    178  */
    179 #define COLOUR_OFFSET (256*1024)
    180 
    181 #if NWSDISPLAY > 0
    182 static int	cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    183 static paddr_t	cg14_mmap(void *, void *, off_t, int);
    184 static void	cg14_init_screen(void *, struct vcons_screen *, int, long *);
    185 
    186 
    187 struct wsdisplay_accessops cg14_accessops = {
    188 	cg14_ioctl,
    189 	cg14_mmap,
    190 	NULL,	/* alloc_screen */
    191 	NULL,	/* free_screen */
    192 	NULL,	/* show_screen */
    193 	NULL, 	/* load_font */
    194 	NULL,	/* pollc */
    195 	NULL	/* scroll */
    196 };
    197 #endif
    198 
    199 /*
    200  * Attach a display.  We need to notice if it is the console, too.
    201  */
    202 void
    203 cgfourteenattach(device_t parent, device_t self, void *aux)
    204 {
    205 	union obio_attach_args *uoba = aux;
    206 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    207 	struct cgfourteen_softc *sc = device_private(self);
    208 	struct fbdevice *fb = &sc->sc_fb;
    209 	bus_space_handle_t bh;
    210 	int node, ramsize;
    211 	volatile uint32_t *lut;
    212 	int i, isconsole, items;
    213 	uint32_t fbva[2] = {0, 0};
    214 	uint32_t *ptr = fbva;
    215 
    216 	sc->sc_dev = self;
    217 	sc->sc_opens = 0;
    218 	node = sa->sa_node;
    219 
    220 	/* Remember cookies for cgfourteenmmap() */
    221 	sc->sc_bustag = sa->sa_bustag;
    222 
    223 	fb->fb_driver = &cgfourteenfbdriver;
    224 	fb->fb_device = sc->sc_dev;
    225 	/* Mask out invalid flags from the user. */
    226 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
    227 
    228 	fb->fb_type.fb_type = FBTYPE_MDICOLOR;
    229 	fb->fb_type.fb_depth = 32;
    230 
    231 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    232 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
    233 
    234 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    235 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
    236 
    237 	if (sa->sa_nreg < 2) {
    238 		printf("%s: only %d register sets\n",
    239 			device_xname(self), sa->sa_nreg);
    240 		return;
    241 	}
    242 	memcpy(sc->sc_physadr, sa->sa_reg,
    243 	      sa->sa_nreg * sizeof(struct sbus_reg));
    244 
    245 	sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
    246 
    247 	printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
    248 	/*
    249 	 * Now map in the 8 useful pages of registers
    250 	 */
    251 	if (sa->sa_size < 0x10000) {
    252 #ifdef DIAGNOSTIC
    253 		printf("warning: can't find all cgfourteen registers...\n");
    254 #endif
    255 		sa->sa_size = 0x10000;
    256 	}
    257 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    258 			 sa->sa_offset,
    259 			 sa->sa_size,
    260 			 0 /*BUS_SPACE_MAP_LINEAR*/,
    261 			 &bh) != 0) {
    262 		printf("%s: cannot map control registers\n", device_xname(self));
    263 		return;
    264 	}
    265 	sc->sc_regh = bh;
    266 	sc->sc_regaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
    267 	sc->sc_fbaddr = BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    268 				sc->sc_physadr[CG14_PXL_IDX].sbr_offset);
    269 
    270 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    271 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    272 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    273 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    274 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    275 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    276 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    277 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    278 
    279 	/*
    280 	 * Let the user know that we're here
    281 	 */
    282 	printf(": %dx%d",
    283 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    284 
    285 	/*
    286 	 * Enable the video.
    287 	 */
    288 	cg14_set_video(sc, 1);
    289 
    290 	/*
    291 	 * Grab the initial colormap
    292 	 */
    293 	lut = sc->sc_clut1->clut_lut;
    294 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    295 		sc->sc_cmap.cm_chip[i] = lut[i];
    296 
    297 	/* See if we're the console */
    298         isconsole = fb_is_console(node);
    299 
    300 #if NWSDISPLAY > 0
    301 	prom_getprop(sa->sa_node, "address", 4, &items, &ptr);
    302 	if (fbva[1] == 0) {
    303 		if (sbus_bus_map( sc->sc_bustag,
    304 		    sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    305 		    sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
    306 		    ramsize, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    307 		    &bh) != 0) {
    308 			printf("%s: cannot map pixels\n",
    309 				device_xname(sc->sc_dev));
    310 			return;
    311 		}
    312 		sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
    313 	} else {
    314 		sc->sc_fb.fb_pixels = (void *)fbva[1];
    315 	}
    316 
    317 	if (isconsole)
    318 		printf(" (console)\n");
    319 	else
    320 		printf("\n");
    321 
    322 	sc->sc_depth = 8;
    323 	cg14_setup_wsdisplay(sc, isconsole);
    324 #endif
    325 
    326 	/* Attach to /dev/fb */
    327 	fb_attach(&sc->sc_fb, isconsole);
    328 }
    329 
    330 /*
    331  * Keep track of the number of opens made. In the 24-bit driver, we need to
    332  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    333  * the last close. This kind of nonsense is needed to give screenblank
    334  * a fighting chance of working.
    335  */
    336 
    337 int
    338 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
    339 {
    340 	struct cgfourteen_softc *sc;
    341 	int oldopens;
    342 
    343 	sc = device_lookup_private(&cgfourteen_cd, minor(dev));
    344 	if (sc == NULL)
    345 		return(ENXIO);
    346 	oldopens = sc->sc_opens++;
    347 
    348 	/* Setup the cg14 as we want it, and save the original PROM state */
    349 	if (oldopens == 0)	/* first open only, to make screenblank work */
    350 		cg14_init(sc);
    351 
    352 	return (0);
    353 }
    354 
    355 int
    356 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
    357 {
    358 	struct cgfourteen_softc *sc =
    359 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    360 	int opens;
    361 
    362 	opens = --sc->sc_opens;
    363 	if (sc->sc_opens < 0)
    364 		opens = sc->sc_opens = 0;
    365 
    366 	/*
    367 	 * Restore video state to make the PROM happy, on last close.
    368 	 */
    369 	if (opens == 0)
    370 		cg14_reset(sc);
    371 
    372 	return (0);
    373 }
    374 
    375 int
    376 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    377 {
    378 	struct cgfourteen_softc *sc =
    379 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    380 	struct fbgattr *fba;
    381 	int error;
    382 
    383 	switch (cmd) {
    384 
    385 	case FBIOGTYPE:
    386 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    387 		break;
    388 
    389 	case FBIOGATTR:
    390 		fba = (struct fbgattr *)data;
    391 		fba->real_type = FBTYPE_MDICOLOR;
    392 		fba->owner = 0;		/* XXX ??? */
    393 		fba->fbtype = sc->sc_fb.fb_type;
    394 		fba->sattr.flags = 0;
    395 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    396 		fba->sattr.dev_specific[0] = -1;
    397 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    398 		fba->emu_types[1] = -1;
    399 		break;
    400 
    401 	case FBIOGETCMAP:
    402 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    403 				     CG14_CLUT_SIZE));
    404 
    405 	case FBIOPUTCMAP:
    406 		/* copy to software map */
    407 #define p ((struct fbcmap *)data)
    408 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    409 		if (error)
    410 			return (error);
    411 		/* now blast them into the chip */
    412 		/* XXX should use retrace interrupt */
    413 		cg14_load_hwcmap(sc, p->index, p->count);
    414 #undef p
    415 		break;
    416 
    417 	case FBIOGVIDEO:
    418 		*(int *)data = cg14_get_video(sc);
    419 		break;
    420 
    421 	case FBIOSVIDEO:
    422 		cg14_set_video(sc, *(int *)data);
    423 		break;
    424 
    425 	case CG14_SET_PIXELMODE: {
    426 		int depth = *(int *)data;
    427 
    428 		if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
    429 			return EINVAL;
    430 
    431 		cg14_set_depth(sc, depth);
    432 		}
    433 		break;
    434 	default:
    435 		return (ENOTTY);
    436 	}
    437 	return (0);
    438 }
    439 
    440 /*
    441  * Undo the effect of an FBIOSVIDEO that turns the video off.
    442  */
    443 static void
    444 cgfourteenunblank(device_t dev)
    445 {
    446 	struct cgfourteen_softc *sc = device_private(dev);
    447 
    448 	cg14_set_video(sc, 1);
    449 #if NWSDISPLAY > 0
    450 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
    451 		cg14_set_depth(sc, 8);
    452 		cg14_init_cmap(sc);
    453 		vcons_redraw_screen(sc->sc_vd.active);
    454 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    455 	}
    456 #endif
    457 }
    458 
    459 /*
    460  * Return the address that would map the given device at the given
    461  * offset, allowing for the given protection, or return -1 for error.
    462   */
    463 paddr_t
    464 cgfourteenmmap(dev_t dev, off_t off, int prot)
    465 {
    466 	struct cgfourteen_softc *sc =
    467 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    468 	off_t offset = -1;
    469 
    470 	if (off & PGOFSET)
    471 		panic("cgfourteenmmap");
    472 
    473 	if (off < 0)
    474 		return (-1);
    475 
    476 	if (off >= 0 && off < 0x10000) {
    477 		offset = sc->sc_regaddr;
    478 	} else if (off >= CG14_CURSOR_VOFF &&
    479 		   off < (CG14_CURSOR_VOFF + 0x1000)) {
    480 		offset = sc->sc_regaddr + CG14_OFFSET_CURS;
    481 		off -= CG14_CURSOR_VOFF;
    482 	} else if (off >= CG14_DIRECT_VOFF &&
    483 		   off < (CG14_DIRECT_VOFF + sc->sc_vramsize)) {
    484 		offset = sc->sc_fbaddr + CG14_FB_VRAM;
    485 		off -= CG14_DIRECT_VOFF;
    486 	} else if (off >= CG14_BGR_VOFF &&
    487 		   off < (CG14_BGR_VOFF + sc->sc_vramsize)) {
    488 		offset = sc->sc_fbaddr + CG14_FB_CBGR;
    489 		off -= CG14_BGR_VOFF;
    490 	} else if (off >= CG14_X32_VOFF &&
    491 		   off < (CG14_X32_VOFF + (sc->sc_vramsize >> 2))) {
    492 		offset = sc->sc_fbaddr + CG14_FB_PX32;
    493 		off -= CG14_X32_VOFF;
    494 	} else if (off >= CG14_B32_VOFF &&
    495 		   off < (CG14_B32_VOFF + (sc->sc_vramsize >> 2))) {
    496 		offset = sc->sc_fbaddr + CG14_FB_PB32;
    497 		off -= CG14_B32_VOFF;
    498 	} else if (off >= CG14_G32_VOFF &&
    499 		   off < (CG14_G32_VOFF + (sc->sc_vramsize >> 2))) {
    500 		offset = sc->sc_fbaddr + CG14_FB_PG32;
    501 		off -= CG14_G32_VOFF;
    502 	} else if (off >= CG14_R32_VOFF &&
    503 		   off < CG14_R32_VOFF + (sc->sc_vramsize >> 2)) {
    504 		offset = sc->sc_fbaddr + CG14_FB_PR32;
    505 		off -= CG14_R32_VOFF;
    506 	} else
    507 		return -1;
    508 	return (bus_space_mmap(sc->sc_bustag, offset, off, prot,
    509 		    BUS_SPACE_MAP_LINEAR));
    510 }
    511 
    512 int
    513 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
    514 {
    515 
    516 	return (seltrue(dev, events, l));
    517 }
    518 
    519 /*
    520  * Miscellaneous helper functions
    521  */
    522 
    523 /* Initialize the framebuffer, storing away useful state for later reset */
    524 static void
    525 cg14_init(struct cgfourteen_softc *sc)
    526 {
    527 #if 0
    528 	volatile uint32_t *clut;
    529 	volatile uint8_t  *xlut;
    530 	int i;
    531 
    532 	/*
    533 	 * We stash away the following to restore on close:
    534 	 *
    535 	 * 	color look-up table 1 	(sc->sc_saveclut)
    536 	 *	x look-up table		(sc->sc_savexlut)
    537 	 *	control register	(sc->sc_savectl)
    538 	 *	cursor control register (sc->sc_savehwc)
    539 	 */
    540 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    541 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    542 
    543 	clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
    544 	xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
    545 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    546 		sc->sc_saveclut.cm_chip[i] = clut[i];
    547 		sc->sc_savexlut[i] = xlut[i];
    548 	}
    549 
    550 	/*
    551 	 * Enable the video and put it in 8 bit mode
    552 	 */
    553 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    554 		CG14_MCTL_POWERCTL;
    555 #else
    556 	cg14_set_depth(sc, 32);
    557 #endif
    558 }
    559 
    560 static void
    561 /* Restore the state saved on cg14_init */
    562 cg14_reset(struct cgfourteen_softc *sc)
    563 {
    564 #if 0
    565 	volatile uint32_t *clut;
    566 	volatile uint8_t  *xlut;
    567 	int i;
    568 
    569 	/*
    570 	 * We restore the following, saved in cg14_init:
    571 	 *
    572 	 * 	color look-up table 1 	(sc->sc_saveclut)
    573 	 *	x look-up table		(sc->sc_savexlut)
    574 	 *	control register	(sc->sc_savectl)
    575 	 *	cursor control register (sc->sc_savehwc)
    576 	 *
    577 	 * Note that we don't touch the video enable bits in the
    578 	 * control register; otherwise, screenblank wouldn't work.
    579 	 */
    580 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    581 							CG14_MCTL_POWERCTL)) |
    582 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    583 						    CG14_MCTL_POWERCTL));
    584 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    585 
    586 	clut = sc->sc_clut1->clut_lut;
    587 	xlut = sc->sc_xlut->xlut_lut;
    588 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    589 		clut[i] = sc->sc_saveclut.cm_chip[i];
    590 		xlut[i] = sc->sc_savexlut[i];
    591 	}
    592 #else
    593 	cg14_set_depth(sc, 8);
    594 #endif
    595 }
    596 
    597 /* Enable/disable video display; power down monitor if DPMS-capable */
    598 static void
    599 cg14_set_video(struct cgfourteen_softc *sc, int enable)
    600 {
    601 	/*
    602 	 * We can only use DPMS to power down the display if the chip revision
    603 	 * is greater than 0.
    604 	 */
    605 	if (enable) {
    606 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    607 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    608 						 CG14_MCTL_POWERCTL);
    609 		else
    610 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    611 	} else {
    612 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    613 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    614 						  CG14_MCTL_POWERCTL);
    615 		else
    616 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    617 	}
    618 }
    619 
    620 /* Get status of video display */
    621 static int
    622 cg14_get_video(struct cgfourteen_softc *sc)
    623 {
    624 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    625 }
    626 
    627 /* Read the software shadow colormap */
    628 static int
    629 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    630 {
    631         u_int i, start, count;
    632         u_char *cp;
    633         int error;
    634 
    635         start = p->index;
    636         count = p->count;
    637         if (start >= cmsize || count > cmsize - start)
    638                 return (EINVAL);
    639 
    640         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    641                 error = copyout(&cp[3], &p->red[i], 1);
    642                 if (error)
    643                         return error;
    644                 error = copyout(&cp[2], &p->green[i], 1);
    645                 if (error)
    646                         return error;
    647                 error = copyout(&cp[1], &p->blue[i], 1);
    648                 if (error)
    649                         return error;
    650         }
    651         return (0);
    652 }
    653 
    654 /* Write the software shadow colormap */
    655 static int
    656 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    657 {
    658         u_int i, start, count;
    659         u_char *cp;
    660         u_char cmap[256][4];
    661         int error;
    662 
    663         start = p->index;
    664         count = p->count;
    665         if (start >= cmsize || count > cmsize - start)
    666                 return (EINVAL);
    667 
    668         memcpy(&cmap, &cm->cm_map, sizeof cmap);
    669         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    670                 error = copyin(&p->red[i], &cp[3], 1);
    671                 if (error)
    672                         return error;
    673                 error = copyin(&p->green[i], &cp[2], 1);
    674                 if (error)
    675                         return error;
    676                 error = copyin(&p->blue[i], &cp[1], 1);
    677                 if (error)
    678                         return error;
    679                 cp[0] = 0;      /* no alpha channel */
    680         }
    681         memcpy(&cm->cm_map, &cmap, sizeof cmap);
    682         return (0);
    683 }
    684 
    685 static void
    686 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
    687 {
    688 	/* XXX switch to auto-increment, and on retrace intr */
    689 
    690 	/* Setup pointers to source and dest */
    691 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
    692 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
    693 
    694 	/* Copy by words */
    695 	while (--ncolors >= 0)
    696 		*lutp++ = *colp++;
    697 }
    698 
    699 #if NWSDISPLAY > 0
    700 static void
    701 cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
    702 {
    703 	struct wsemuldisplaydev_attach_args aa;
    704 	struct rasops_info *ri;
    705 	long defattr;
    706 
    707  	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    708 		"default",
    709 		0, 0,
    710 		NULL,
    711 		8, 16,
    712 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    713 		NULL
    714 	};
    715 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    716 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    717 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    718 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
    719 	    &cg14_accessops);
    720 	sc->sc_vd.init_screen = cg14_init_screen;
    721 
    722 	ri = &sc->sc_console_screen.scr_ri;
    723 
    724 	if (is_cons) {
    725 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
    726 		    &defattr);
    727 
    728 		/* clear the screen with the default background colour */
    729 		memset(sc->sc_fb.fb_pixels,
    730 		       (defattr >> 16) & 0xff,
    731 		       ri->ri_stride * ri->ri_height);
    732 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    733 
    734 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    735 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    736 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    737 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    738 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    739 		    defattr);
    740 		vcons_replay_msgbuf(&sc->sc_console_screen);
    741 	} else {
    742 		/*
    743 		 * since we're not the console we can postpone the rest
    744 		 * until someone actually allocates a screen for us
    745 		 */
    746 	}
    747 
    748 	cg14_init_cmap(sc);
    749 
    750 	aa.console = is_cons;
    751 	aa.scrdata = &sc->sc_screenlist;
    752 	aa.accessops = &cg14_accessops;
    753 	aa.accesscookie = &sc->sc_vd;
    754 
    755 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    756 }
    757 
    758 static void
    759 cg14_init_cmap(struct cgfourteen_softc *sc)
    760 {
    761 	int i, j = 0;
    762 
    763 	for (i = 0; i < 256; i++) {
    764 
    765 		sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
    766 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
    767 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
    768 		j += 3;
    769 	}
    770 	cg14_load_hwcmap(sc, 0, 256);
    771 }
    772 
    773 static int
    774 cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    775 {
    776 	u_int index = cm->index;
    777 	u_int count = cm->count;
    778 	int i, error;
    779 	u_char rbuf[256], gbuf[256], bbuf[256];
    780 
    781 	if (cm->index >= 256 || cm->count > 256 ||
    782 	    (cm->index + cm->count) > 256)
    783 		return EINVAL;
    784 	error = copyin(cm->red, &rbuf[index], count);
    785 	if (error)
    786 		return error;
    787 	error = copyin(cm->green, &gbuf[index], count);
    788 	if (error)
    789 		return error;
    790 	error = copyin(cm->blue, &bbuf[index], count);
    791 	if (error)
    792 		return error;
    793 
    794 	for (i = 0; i < count; i++) {
    795 		sc->sc_cmap.cm_map[index][3] = rbuf[index];
    796 		sc->sc_cmap.cm_map[index][2] = gbuf[index];
    797 		sc->sc_cmap.cm_map[index][1] = bbuf[index];
    798 
    799 		index++;
    800 	}
    801 	cg14_load_hwcmap(sc, 0, 256);
    802 	return 0;
    803 }
    804 
    805 static int
    806 cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    807 {
    808 	uint8_t rbuf[256], gbuf[256], bbuf[256];
    809 	u_int index = cm->index;
    810 	u_int count = cm->count;
    811 	int error, i;
    812 
    813 	if (index >= 255 || count > 256 || index + count > 256)
    814 		return EINVAL;
    815 
    816 
    817 	for (i = 0; i < count; i++) {
    818 		rbuf[i] = sc->sc_cmap.cm_map[index][3];
    819 		gbuf[i] = sc->sc_cmap.cm_map[index][2];
    820 		bbuf[i] = sc->sc_cmap.cm_map[index][1];
    821 
    822 		index++;
    823 	}
    824 	error = copyout(rbuf,   cm->red,   count);
    825 	if (error)
    826 		return error;
    827 	error = copyout(gbuf, cm->green, count);
    828 	if (error)
    829 		return error;
    830 	error = copyout(bbuf,  cm->blue,  count);
    831 	if (error)
    832 		return error;
    833 
    834 	return 0;
    835 }
    836 
    837 static int
    838 cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    839 	struct lwp *l)
    840 {
    841 	struct vcons_data *vd = v;
    842 	struct cgfourteen_softc *sc = vd->cookie;
    843 	struct wsdisplay_fbinfo *wdf;
    844 	struct vcons_screen *ms = vd->active;
    845 
    846 	switch (cmd) {
    847 
    848 		case WSDISPLAYIO_GTYPE:
    849 			*(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
    850 			return 0;
    851 
    852 		case WSDISPLAYIO_GINFO:
    853 			wdf = (void *)data;
    854 			wdf->height = ms->scr_ri.ri_height;
    855 			wdf->width = ms->scr_ri.ri_width;
    856 			wdf->depth = 32;
    857 			wdf->cmsize = 256;
    858 			return 0;
    859 
    860 		case WSDISPLAYIO_GETCMAP:
    861 			return cg14_getcmap(sc,
    862 			    (struct wsdisplay_cmap *)data);
    863 
    864 		case WSDISPLAYIO_PUTCMAP:
    865 			return cg14_putcmap(sc,
    866 			    (struct wsdisplay_cmap *)data);
    867 
    868 		case WSDISPLAYIO_LINEBYTES:
    869 			*(u_int *)data = ms->scr_ri.ri_stride << 2;
    870 			return 0;
    871 
    872 		case WSDISPLAYIO_SMODE:
    873 			{
    874 				int new_mode = *(int*)data;
    875 				if (new_mode != sc->sc_mode) {
    876 					sc->sc_mode = new_mode;
    877 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    878 						bus_space_write_1(sc->sc_bustag,
    879 						    sc->sc_regh,
    880 						    CG14_CURSOR_CONTROL, 0);
    881 
    882 						cg14_set_depth(sc, 8);
    883 						cg14_init_cmap(sc);
    884 						vcons_redraw_screen(ms);
    885 					} else {
    886 
    887 						cg14_set_depth(sc, 32);
    888 					}
    889 				}
    890 			}
    891 			return 0;
    892 		case WSDISPLAYIO_SVIDEO:
    893 			cg14_set_video(sc, *(int *)data);
    894 			return 0;
    895 		case WSDISPLAYIO_GVIDEO:
    896 			return cg14_get_video(sc) ?
    897 			    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    898 		case WSDISPLAYIO_GCURPOS:
    899 			{
    900 				struct wsdisplay_curpos *cp = (void *)data;
    901 
    902 				cp->x = sc->sc_cursor.cc_pos.x;
    903 				cp->y = sc->sc_cursor.cc_pos.y;
    904 			}
    905 			return 0;
    906 		case WSDISPLAYIO_SCURPOS:
    907 			{
    908 				struct wsdisplay_curpos *cp = (void *)data;
    909 
    910 				cg14_move_cursor(sc, cp->x, cp->y);
    911 			}
    912 			return 0;
    913 		case WSDISPLAYIO_GCURMAX:
    914 			{
    915 				struct wsdisplay_curpos *cp = (void *)data;
    916 
    917 				cp->x = 32;
    918 				cp->y = 32;
    919 			}
    920 			return 0;
    921 		case WSDISPLAYIO_SCURSOR:
    922 			{
    923 				struct wsdisplay_cursor *cursor = (void *)data;
    924 
    925 				return cg14_do_cursor(sc, cursor);
    926 			}
    927 		case PCI_IOC_CFGREAD:
    928 		case PCI_IOC_CFGWRITE:
    929 			return EINVAL;
    930 
    931 	}
    932 	return EPASSTHROUGH;
    933 }
    934 
    935 static paddr_t
    936 cg14_mmap(void *v, void *vs, off_t offset, int prot)
    937 {
    938 	struct vcons_data *vd = v;
    939 	struct cgfourteen_softc *sc = vd->cookie;
    940 
    941 	/* allow mmap()ing the full framebuffer, not just what we use */
    942 	if (offset < sc->sc_vramsize)
    943 		return bus_space_mmap(sc->sc_bustag,
    944 		    BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    945 		      sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    946 		    offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
    947 
    948 	return -1;
    949 }
    950 
    951 static void
    952 cg14_init_screen(void *cookie, struct vcons_screen *scr,
    953     int existing, long *defattr)
    954 {
    955 	struct cgfourteen_softc *sc = cookie;
    956 	struct rasops_info *ri = &scr->scr_ri;
    957 
    958 	ri->ri_depth = 8;
    959 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
    960 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
    961 	ri->ri_stride = ri->ri_width;
    962 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    963 
    964 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
    965 	scr->scr_flags |= VCONS_DONT_READ;
    966 
    967 	if (existing) {
    968 		ri->ri_flg |= RI_CLEAR;
    969 	}
    970 
    971 	rasops_init(ri, 0, 0);
    972 	ri->ri_caps = WSSCREEN_WSCOLORS;
    973 
    974 	rasops_reconfig(ri,
    975 	    sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
    976 	    sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
    977 
    978 	ri->ri_hw = scr;
    979 }
    980 
    981 static void
    982 cg14_set_depth(struct cgfourteen_softc *sc, int depth)
    983 {
    984 	int i;
    985 
    986 	if (sc->sc_depth == depth)
    987 		return;
    988 
    989 	switch (depth) {
    990 		case 8:
    991 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
    992 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
    993 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
    994 			sc->sc_depth = 8;
    995 			/* everything is CLUT1 */
    996 			for (i = 0; i < CG14_CLUT_SIZE; i++)
    997 			     sc->sc_xlut->xlut_lut[i] = 0;
    998 			break;
    999 		case 32:
   1000 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1001 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1002 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
   1003 			sc->sc_depth = 32;
   1004 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1005 			     sc->sc_xlut->xlut_lut[i] = 0;
   1006 			break;
   1007 		default:
   1008 			printf("%s: can't change to depth %d\n",
   1009 			    device_xname(sc->sc_dev), depth);
   1010 	}
   1011 }
   1012 
   1013 static void
   1014 cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
   1015 {
   1016 	uint32_t pos;
   1017 
   1018 	sc->sc_cursor.cc_pos.x = x;
   1019 	sc->sc_cursor.cc_pos.y = y;
   1020 	pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
   1021 	      ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
   1022 	bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
   1023 }
   1024 
   1025 static int
   1026 cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
   1027 {
   1028 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1029 
   1030 		bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1031 		    CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
   1032 	}
   1033 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1034 
   1035 		sc->sc_cursor.cc_hot.x = cur->hot.x;
   1036 		sc->sc_cursor.cc_hot.y = cur->hot.y;
   1037 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1038 	}
   1039 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1040 
   1041 		cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
   1042 	}
   1043 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1044 		int i;
   1045 		uint32_t val;
   1046 
   1047 		for (i = 0; i < min(cur->cmap.count, 3); i++) {
   1048 			val = (cur->cmap.red[i] ) |
   1049 			      (cur->cmap.green[i] << 8) |
   1050 			      (cur->cmap.blue[i] << 16);
   1051 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1052 			    CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
   1053 			    val);
   1054 		}
   1055 	}
   1056 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1057 		uint32_t buffer[32], latch, tmp;
   1058 		int i;
   1059 
   1060 		copyin(cur->mask, buffer, 128);
   1061 		for (i = 0; i < 32; i++) {
   1062 			latch = 0;
   1063 			tmp = buffer[i] & 0x80808080;
   1064 			latch |= tmp >> 7;
   1065 			tmp = buffer[i] & 0x40404040;
   1066 			latch |= tmp >> 5;
   1067 			tmp = buffer[i] & 0x20202020;
   1068 			latch |= tmp >> 3;
   1069 			tmp = buffer[i] & 0x10101010;
   1070 			latch |= tmp >> 1;
   1071 			tmp = buffer[i] & 0x08080808;
   1072 			latch |= tmp << 1;
   1073 			tmp = buffer[i] & 0x04040404;
   1074 			latch |= tmp << 3;
   1075 			tmp = buffer[i] & 0x02020202;
   1076 			latch |= tmp << 5;
   1077 			tmp = buffer[i] & 0x01010101;
   1078 			latch |= tmp << 7;
   1079 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1080 			    CG14_CURSOR_PLANE0 + (i << 2), latch);
   1081 		}
   1082 		copyin(cur->image, buffer, 128);
   1083 		for (i = 0; i < 32; i++) {
   1084 			latch = 0;
   1085 			tmp = buffer[i] & 0x80808080;
   1086 			latch |= tmp >> 7;
   1087 			tmp = buffer[i] & 0x40404040;
   1088 			latch |= tmp >> 5;
   1089 			tmp = buffer[i] & 0x20202020;
   1090 			latch |= tmp >> 3;
   1091 			tmp = buffer[i] & 0x10101010;
   1092 			latch |= tmp >> 1;
   1093 			tmp = buffer[i] & 0x08080808;
   1094 			latch |= tmp << 1;
   1095 			tmp = buffer[i] & 0x04040404;
   1096 			latch |= tmp << 3;
   1097 			tmp = buffer[i] & 0x02020202;
   1098 			latch |= tmp << 5;
   1099 			tmp = buffer[i] & 0x01010101;
   1100 			latch |= tmp << 7;
   1101 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1102 			    CG14_CURSOR_PLANE1 + (i << 2), latch);
   1103 		}
   1104 	}
   1105 	return 0;
   1106 }
   1107 #endif
   1108