Home | History | Annotate | Line # | Download | only in sbus
p9100.c revision 1.31
      1 /*	$NetBSD: p9100.c,v 1.31 2006/04/12 19:38:24 jmmv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2005, 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * color display (p9100) driver.
     41  *
     42  * Does not handle interrupts, even though they can occur.
     43  *
     44  * XXX should defer colormap updates to vertical retrace interrupts
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: p9100.c,v 1.31 2006/04/12 19:38:24 jmmv Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/buf.h>
     53 #include <sys/device.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/malloc.h>
     56 #include <sys/mman.h>
     57 #include <sys/tty.h>
     58 #include <sys/conf.h>
     59 
     60 #include <machine/bus.h>
     61 #include <machine/autoconf.h>
     62 
     63 #include <dev/sun/fbio.h>
     64 #include <dev/sun/fbvar.h>
     65 #include <dev/sun/btreg.h>
     66 #include <dev/sun/btvar.h>
     67 
     68 #include <dev/sbus/p9100reg.h>
     69 
     70 #include <dev/sbus/sbusvar.h>
     71 
     72 /*#include <dev/wscons/wsdisplayvar.h>*/
     73 #include <dev/wscons/wsconsio.h>
     74 #include <dev/wsfont/wsfont.h>
     75 #include <dev/rasops/rasops.h>
     76 
     77 #include <dev/wscons/wsdisplay_vconsvar.h>
     78 
     79 #include "opt_wsemul.h"
     80 #include "rasops_glue.h"
     81 
     82 #include "tctrl.h"
     83 #if NTCTRL > 0
     84 #include <machine/tctrl.h>
     85 #include <sparc/dev/tctrlvar.h>/*XXX*/
     86 #endif
     87 
     88 struct pnozz_cursor {
     89 	short	pc_enable;		/* cursor is enabled */
     90 	struct	fbcurpos pc_pos;	/* position */
     91 	struct	fbcurpos pc_hot;	/* hot-spot */
     92 	struct	fbcurpos pc_size;	/* size of mask & image fields */
     93 	uint32_t pc_bits[0x100];	/* space for mask & image bits */
     94 	unsigned char red[3], green[3];
     95 	unsigned char blue[3];		/* cursor palette */
     96 };
     97 
     98 /* per-display variables */
     99 struct p9100_softc {
    100 	struct device	sc_dev;		/* base device */
    101 	struct sbusdev	sc_sd;		/* sbus device */
    102 	struct fbdevice	sc_fb;		/* frame buffer device */
    103 
    104 	bus_space_tag_t	sc_bustag;
    105 
    106 	bus_addr_t	sc_ctl_paddr;	/* phys address description */
    107 	bus_size_t	sc_ctl_psize;	/*   for device mmap() */
    108 	bus_space_handle_t sc_ctl_memh;	/*   bus space handle */
    109 
    110 	bus_addr_t	sc_cmd_paddr;	/* phys address description */
    111 	bus_size_t	sc_cmd_psize;	/*   for device mmap() */
    112 	bus_space_handle_t sc_cmd_memh;	/*   bus space handle */
    113 
    114 	bus_addr_t	sc_fb_paddr;	/* phys address description */
    115 	bus_size_t	sc_fb_psize;	/*   for device mmap() */
    116 	bus_space_handle_t sc_fb_memh;	/*   bus space handle */
    117 
    118 	volatile uint32_t sc_junk;
    119 	uint32_t sc_mono_width;	/* for setup_mono */
    120 
    121 	uint32_t sc_width;
    122 	uint32_t sc_height;	/* panel width / height */
    123 	uint32_t sc_stride;
    124 	uint32_t sc_depth;
    125 	union	bt_cmap sc_cmap;	/* Brooktree color map */
    126 
    127 	struct pnozz_cursor sc_cursor;
    128 
    129 	int sc_mode;
    130 	int sc_video, sc_powerstate;
    131 	uint32_t sc_bg;
    132 	volatile uint32_t sc_last_offset;
    133 	struct vcons_data vd;
    134 };
    135 
    136 
    137 static struct vcons_screen p9100_console_screen;
    138 
    139 extern const u_char rasops_cmap[768];
    140 
    141 struct wsscreen_descr p9100_defscreendesc = {
    142 	"default",
    143 	0, 0,
    144 	NULL,
    145 	8, 16,
    146 	WSSCREEN_WSCOLORS,
    147 };
    148 
    149 const struct wsscreen_descr *_p9100_scrlist[] = {
    150 	&p9100_defscreendesc,
    151 	/* XXX other formats, graphics screen? */
    152 };
    153 
    154 struct wsscreen_list p9100_screenlist = {
    155 	sizeof(_p9100_scrlist) / sizeof(struct wsscreen_descr *), _p9100_scrlist
    156 };
    157 
    158 /* autoconfiguration driver */
    159 static int	p9100_sbus_match(struct device *, struct cfdata *, void *);
    160 static void	p9100_sbus_attach(struct device *, struct device *, void *);
    161 
    162 static void	p9100unblank(struct device *);
    163 static void	p9100_shutdown(void *);
    164 
    165 CFATTACH_DECL(pnozz, sizeof(struct p9100_softc),
    166     p9100_sbus_match, p9100_sbus_attach, NULL, NULL);
    167 
    168 extern struct cfdriver pnozz_cd;
    169 
    170 static dev_type_open(p9100open);
    171 static dev_type_ioctl(p9100ioctl);
    172 static dev_type_mmap(p9100mmap);
    173 
    174 const struct cdevsw pnozz_cdevsw = {
    175 	p9100open, nullclose, noread, nowrite, p9100ioctl,
    176 	nostop, notty, nopoll, p9100mmap, nokqfilter,
    177 };
    178 
    179 /* frame buffer generic driver */
    180 static struct fbdriver p9100fbdriver = {
    181 	p9100unblank, p9100open, nullclose, p9100ioctl, nopoll,
    182 	p9100mmap, nokqfilter
    183 };
    184 
    185 static void	p9100loadcmap(struct p9100_softc *, int, int);
    186 static void	p9100_set_video(struct p9100_softc *, int);
    187 static int	p9100_get_video(struct p9100_softc *);
    188 static uint32_t p9100_ctl_read_4(struct p9100_softc *, bus_size_t);
    189 static void	p9100_ctl_write_4(struct p9100_softc *, bus_size_t, uint32_t);
    190 static uint8_t	p9100_ramdac_read(struct p9100_softc *, bus_size_t);
    191 static void	p9100_ramdac_write(struct p9100_softc *, bus_size_t, uint8_t);
    192 
    193 static uint8_t	p9100_ramdac_read_ctl(struct p9100_softc *, int);
    194 static void	p9100_ramdac_write_ctl(struct p9100_softc *, int, uint8_t);
    195 
    196 static void 	p9100_init_engine(struct p9100_softc *);
    197 
    198 #if NWSDISPLAY > 0
    199 static void	p9100_sync(struct p9100_softc *);
    200 static void	p9100_bitblt(void *, int, int, int, int, int, int, uint32_t);
    201 static void 	p9100_rectfill(void *, int, int, int, int, uint32_t);
    202 static void	p9100_clearscreen(struct p9100_softc *);
    203 
    204 static void	p9100_setup_mono(struct p9100_softc *, int, int, int, int,
    205 		    uint32_t, uint32_t);
    206 static void	p9100_feed_line(struct p9100_softc *, int, uint8_t *);
    207 static void	p9100_set_color_reg(struct p9100_softc *, int, int32_t);
    208 
    209 static void	p9100_copycols(void *, int, int, int, int);
    210 static void	p9100_erasecols(void *, int, int, int, long);
    211 static void	p9100_copyrows(void *, int, int, int);
    212 static void	p9100_eraserows(void *, int, int, long);
    213 /*static int	p9100_mapchar(void *, int, u_int *);*/
    214 static void	p9100_putchar(void *, int, int, u_int, long);
    215 static void	p9100_cursor(void *, int, int, int);
    216 static int	p9100_allocattr(void *, int, int, int, long *);
    217 
    218 /*static void	p9100_scroll(void *, void *, int);*/
    219 
    220 static int	p9100_putcmap(struct p9100_softc *, struct wsdisplay_cmap *);
    221 static int 	p9100_getcmap(struct p9100_softc *, struct wsdisplay_cmap *);
    222 static int	p9100_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
    223 static paddr_t	p9100_mmap(void *, void *, off_t, int);
    224 
    225 /*static int	p9100_load_font(void *, void *, struct wsdisplay_font *);*/
    226 
    227 static void	p9100_init_screen(void *, struct vcons_screen *, int,
    228 	    long *);
    229 #endif
    230 
    231 static void	p9100_init_cursor(struct p9100_softc *);
    232 
    233 static void	p9100_set_fbcursor(struct p9100_softc *);
    234 static void	p9100_setcursorcmap(struct p9100_softc *);
    235 static void	p9100_loadcursor(struct p9100_softc *);
    236 
    237 static int	p9100_intr(void *);
    238 
    239 /* power management stuff */
    240 static void p9100_power_hook(int, void *);
    241 
    242 static void p9100_set_extvga(void *, int);
    243 
    244 #if NWSDISPLAY > 0
    245 struct wsdisplay_accessops p9100_accessops = {
    246 	p9100_ioctl,
    247 	p9100_mmap,
    248 	NULL,	/* vcons_alloc_screen */
    249 	NULL,	/* vcons_free_screen */
    250 	NULL,	/* vcons_show_screen */
    251 	NULL,	/* load_font */
    252 	NULL,	/* polls */
    253 	NULL,	/* getwschar */
    254 	NULL,	/* putwschar */
    255 	NULL,	/* scroll */
    256 };
    257 #endif
    258 
    259 #define PNOZZ_LATCH(sc, off) if(sc->sc_last_offset == (off & 0xffffff80)) { \
    260 		sc->sc_junk = bus_space_read_4(sc->sc_bustag, sc->sc_fb_memh, \
    261 		    off); \
    262 		sc->sc_last_offset = off & 0xffffff80; }
    263 
    264 /*
    265  * Match a p9100.
    266  */
    267 static int
    268 p9100_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
    269 {
    270 	struct sbus_attach_args *sa = aux;
    271 
    272 	return (strcmp("p9100", sa->sa_name) == 0);
    273 }
    274 
    275 
    276 /*
    277  * Attach a display.  We need to notice if it is the console, too.
    278  */
    279 static void
    280 p9100_sbus_attach(struct device *parent, struct device *self, void *args)
    281 {
    282 	struct p9100_softc *sc = (struct p9100_softc *)self;
    283 	struct sbus_attach_args *sa = args;
    284 	struct fbdevice *fb = &sc->sc_fb;
    285 	int isconsole;
    286 	int node;
    287 	int i, j;
    288 	uint8_t ver;
    289 
    290 #if NWSDISPLAY > 0
    291 	struct wsemuldisplaydev_attach_args aa;
    292 	struct rasops_info *ri;
    293 	unsigned long defattr;
    294 #endif
    295 
    296 	sc->sc_last_offset = 0xffffffff;
    297 
    298 	/* Remember cookies for p9100_mmap() */
    299 	sc->sc_bustag = sa->sa_bustag;
    300 	sc->sc_ctl_paddr = sbus_bus_addr(sa->sa_bustag,
    301 		sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base);
    302 	sc->sc_ctl_psize = 0x8000;/*(bus_size_t)sa->sa_reg[0].oa_size;*/
    303 
    304 	sc->sc_cmd_paddr = sbus_bus_addr(sa->sa_bustag,
    305 		sa->sa_reg[1].oa_space, sa->sa_reg[1].oa_base);
    306 	sc->sc_cmd_psize = (bus_size_t)sa->sa_reg[1].oa_size;
    307 
    308 	sc->sc_fb_paddr = sbus_bus_addr(sa->sa_bustag,
    309 		sa->sa_reg[2].oa_space, sa->sa_reg[2].oa_base);
    310 	sc->sc_fb_psize = (bus_size_t)sa->sa_reg[2].oa_size;
    311 
    312 	fb->fb_driver = &p9100fbdriver;
    313 	fb->fb_device = &sc->sc_dev;
    314 	fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
    315 #ifdef PNOZZ_EMUL_CG3
    316 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
    317 #else
    318 	fb->fb_type.fb_type = FBTYPE_P9100;
    319 #endif
    320 	fb->fb_pixels = NULL;
    321 
    322 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    323 
    324 	node = sa->sa_node;
    325 	isconsole = fb_is_console(node);
    326 	if (!isconsole) {
    327 		printf("\n%s: fatal error: PROM didn't configure device\n",
    328 		    self->dv_xname);
    329 		return;
    330 	}
    331 
    332 	/*
    333 	 * When the ROM has mapped in a p9100 display, the address
    334 	 * maps only the video RAM, so in any case we have to map the
    335 	 * registers ourselves.  We only need the video RAM if we are
    336 	 * going to print characters via rconsole.
    337 	 */
    338 	if (sbus_bus_map(sc->sc_bustag,
    339 			 sa->sa_reg[0].oa_space,
    340 			 sa->sa_reg[0].oa_base,
    341 			 /*
    342 			  * XXX for some reason the SBus resources don't cover
    343 			  * all registers, so we just map what we need
    344 			  */
    345 			 /*sc->sc_ctl_psize*/ 0x8000,
    346 			 /*BUS_SPACE_MAP_LINEAR*/0, &sc->sc_ctl_memh) != 0) {
    347 		printf("%s: cannot map control registers\n", self->dv_xname);
    348 		return;
    349 	}
    350 
    351 	if (sa->sa_npromvaddrs != 0)
    352 		fb->fb_pixels = (caddr_t)sa->sa_promvaddrs[0];
    353 
    354 	if (fb->fb_pixels == NULL) {
    355 		if (sbus_bus_map(sc->sc_bustag,
    356 				sa->sa_reg[2].oa_space,
    357 				sa->sa_reg[2].oa_base,
    358 				sc->sc_fb_psize,
    359 				BUS_SPACE_MAP_LINEAR, &sc->sc_fb_memh) != 0) {
    360 			printf("%s: cannot map framebuffer\n", self->dv_xname);
    361 			return;
    362 		}
    363 		fb->fb_pixels = (char *)sc->sc_fb_memh;
    364 	} else {
    365 		sc->sc_fb_memh = (bus_space_handle_t) fb->fb_pixels;
    366 	}
    367 
    368 	i = p9100_ctl_read_4(sc, 0x0004);
    369 	switch ((i >> 26) & 7) {
    370 	    case 5: fb->fb_type.fb_depth = 32; break;
    371 	    case 7: fb->fb_type.fb_depth = 24; break;
    372 	    case 3: fb->fb_type.fb_depth = 16; break;
    373 	    case 2: fb->fb_type.fb_depth = 8; break;
    374 	    default: {
    375 		panic("pnozz: can't determine screen depth (0x%02x)", i);
    376 	    }
    377 	}
    378 	sc->sc_depth = (fb->fb_type.fb_depth >> 3);
    379 
    380 	/* XXX for some reason I get a kernel trap with this */
    381 	sc->sc_width = prom_getpropint(node, "width", 800);
    382 	sc->sc_height = prom_getpropint(node, "height", 600);
    383 
    384 	sc->sc_stride = prom_getpropint(node, "linebytes", sc->sc_width *
    385 	    (fb->fb_type.fb_depth >> 3));
    386 
    387 	/* check the RAMDAC */
    388 	ver = p9100_ramdac_read_ctl(sc, DAC_VERSION);
    389 
    390 	p9100_init_engine(sc);
    391 
    392 	fb_setsize_obp(fb, fb->fb_type.fb_depth, sc->sc_width, sc->sc_height,
    393 	    node);
    394 
    395 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
    396 	bus_intr_establish(sc->sc_bustag, sa->sa_pri, IPL_BIO,
    397 	    p9100_intr, sc);
    398 
    399 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    400 	printf(": rev %d / %x, %dx%d, depth %d mem %x",
    401 	       (i & 7), ver, fb->fb_type.fb_width, fb->fb_type.fb_height,
    402 	       fb->fb_type.fb_depth, (unsigned int)sc->sc_fb_psize);
    403 
    404 	fb->fb_type.fb_cmsize = prom_getpropint(node, "cmsize", 256);
    405 	if ((1 << fb->fb_type.fb_depth) != fb->fb_type.fb_cmsize)
    406 		printf(", %d entry colormap", fb->fb_type.fb_cmsize);
    407 
    408 	/* Initialize the default color map. */
    409 	/*bt_initcmap(&sc->sc_cmap, 256);*/
    410 	j = 0;
    411 	for (i = 0; i < 256; i++) {
    412 		sc->sc_cmap.cm_map[i][0] = rasops_cmap[j];
    413 		j++;
    414 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j];
    415 		j++;
    416 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j];
    417 		j++;
    418 	}
    419 	p9100loadcmap(sc, 0, 256);
    420 
    421 	/* make sure we are not blanked */
    422 	if (isconsole)
    423 		p9100_set_video(sc, 1);
    424 
    425 	if (shutdownhook_establish(p9100_shutdown, sc) == NULL) {
    426 		panic("%s: could not establish shutdown hook",
    427 		      sc->sc_dev.dv_xname);
    428 	}
    429 
    430 	if (isconsole) {
    431 		printf(" (console)\n");
    432 #ifdef RASTERCONSOLE
    433 		/*p9100loadcmap(sc, 255, 1);*/
    434 		fbrcons_init(fb);
    435 #endif
    436 	} else
    437 		printf("\n");
    438 
    439 #if NWSDISPLAY > 0
    440 	wsfont_init();
    441 
    442 	vcons_init(&sc->vd, sc, &p9100_defscreendesc, &p9100_accessops);
    443 	sc->vd.init_screen = p9100_init_screen;
    444 
    445 	vcons_init_screen(&sc->vd, &p9100_console_screen, 1, &defattr);
    446 	p9100_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    447 
    448 	sc->sc_bg = (defattr >> 16) & 0xff;
    449 	p9100_clearscreen(sc);
    450 
    451 	ri = &p9100_console_screen.scr_ri;
    452 
    453 	p9100_defscreendesc.nrows = ri->ri_rows;
    454 	p9100_defscreendesc.ncols = ri->ri_cols;
    455 	p9100_defscreendesc.textops = &ri->ri_ops;
    456 	p9100_defscreendesc.capabilities = ri->ri_caps;
    457 
    458 	if(isconsole) {
    459 		wsdisplay_cnattach(&p9100_defscreendesc, ri, 0, 0, defattr);
    460 	}
    461 
    462 	aa.console = isconsole;
    463 	aa.scrdata = &p9100_screenlist;
    464 	aa.accessops = &p9100_accessops;
    465 	aa.accesscookie = &sc->vd;
    466 
    467 	config_found(self, &aa, wsemuldisplaydevprint);
    468 #endif
    469 	/* cursor sprite handling */
    470 	p9100_init_cursor(sc);
    471 
    472 	/* attach the fb */
    473 	fb_attach(fb, isconsole);
    474 
    475 	/* register with power management */
    476 	sc->sc_video = 1;
    477 	sc->sc_powerstate = PWR_RESUME;
    478 	powerhook_establish(p9100_power_hook, sc);
    479 
    480 #if NTCTRL > 0
    481 	/* register callback for external monitor status change */
    482 	tadpole_register_callback(p9100_set_extvga, sc);
    483 #endif
    484 }
    485 
    486 static void
    487 p9100_shutdown(arg)
    488 	void *arg;
    489 {
    490 	struct p9100_softc *sc = arg;
    491 
    492 #ifdef RASTERCONSOLE
    493 	sc->sc_cmap.cm_map[0][0] = 0xff;
    494 	sc->sc_cmap.cm_map[0][1] = 0xff;
    495 	sc->sc_cmap.cm_map[0][2] = 0xff;
    496 	sc->sc_cmap.cm_map[1][0] = 0;
    497 	sc->sc_cmap.cm_map[1][1] = 0;
    498 	sc->sc_cmap.cm_map[1][2] = 0x00;
    499 	p9100loadcmap(sc, 0, 2);
    500 	sc->sc_cmap.cm_map[255][0] = 0;
    501 	sc->sc_cmap.cm_map[255][1] = 0;
    502 	sc->sc_cmap.cm_map[255][2] = 0;
    503 	p9100loadcmap(sc, 255, 1);
    504 #endif
    505 	p9100_set_video(sc, 1);
    506 }
    507 
    508 int
    509 p9100open(dev_t dev, int flags, int mode, struct lwp *l)
    510 {
    511 	int unit = minor(dev);
    512 
    513 	if (unit >= pnozz_cd.cd_ndevs || pnozz_cd.cd_devs[unit] == NULL)
    514 		return (ENXIO);
    515 	return (0);
    516 }
    517 
    518 int
    519 p9100ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
    520 {
    521 	struct p9100_softc *sc = pnozz_cd.cd_devs[minor(dev)];
    522 	struct fbgattr *fba;
    523 	int error, v;
    524 
    525 	switch (cmd) {
    526 
    527 	case FBIOGTYPE:
    528 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    529 		break;
    530 
    531 	case FBIOGATTR:
    532 		fba = (struct fbgattr *)data;
    533 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    534 		fba->owner = 0;		/* XXX ??? */
    535 		fba->fbtype = sc->sc_fb.fb_type;
    536 		fba->sattr.flags = 0;
    537 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    538 		fba->sattr.dev_specific[0] = -1;
    539 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    540 		fba->emu_types[1] = -1;
    541 		break;
    542 
    543 	case FBIOGETCMAP:
    544 #define p ((struct fbcmap *)data)
    545 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    546 
    547 	case FBIOPUTCMAP:
    548 		/* copy to software map */
    549 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    550 		if (error)
    551 			return (error);
    552 		/* now blast them into the chip */
    553 		/* XXX should use retrace interrupt */
    554 		p9100loadcmap(sc, p->index, p->count);
    555 #undef p
    556 		break;
    557 
    558 	case FBIOGVIDEO:
    559 		*(int *)data = p9100_get_video(sc);
    560 		break;
    561 
    562 	case FBIOSVIDEO:
    563 		p9100_set_video(sc, *(int *)data);
    564 		break;
    565 
    566 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
    567 #define p ((struct fbcursor *)data)
    568 #define pc (&sc->sc_cursor)
    569 
    570 	case FBIOGCURSOR:
    571 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
    572 		p->enable = pc->pc_enable;
    573 		p->pos = pc->pc_pos;
    574 		p->hot = pc->pc_hot;
    575 		p->size = pc->pc_size;
    576 
    577 		if (p->image != NULL) {
    578 			error = copyout(pc->pc_bits, p->image, 0x200);
    579 			if (error)
    580 				return error;
    581 			error = copyout(&pc->pc_bits[0x80], p->mask, 0x200);
    582 			if (error)
    583 				return error;
    584 		}
    585 
    586 		p->cmap.index = 0;
    587 		p->cmap.count = 3;
    588 		if (p->cmap.red != NULL) {
    589 			copyout(pc->red, p->cmap.red, 3);
    590 			copyout(pc->green, p->cmap.green, 3);
    591 			copyout(pc->blue, p->cmap.blue, 3);
    592 		}
    593 		break;
    594 
    595 	case FBIOSCURSOR:
    596 	{
    597 		int count;
    598 		uint32_t image[0x80], mask[0x80];
    599 		uint8_t red[3], green[3], blue[3];
    600 
    601 		v = p->set;
    602 		if (v & FB_CUR_SETCMAP) {
    603 			error = copyin(p->cmap.red, red, 3);
    604 			error |= copyin(p->cmap.green, green, 3);
    605 			error |= copyin(p->cmap.blue, blue, 3);
    606 			if (error)
    607 				return error;
    608 		}
    609 		if (v & FB_CUR_SETSHAPE) {
    610 			if (p->size.x > 64 || p->size.y > 64)
    611 				return EINVAL;
    612 			memset(&mask, 0, 0x200);
    613 			memset(&image, 0, 0x200);
    614 			count = p->size.y * 8;
    615 			error = copyin(p->image, image, count);
    616 			if (error)
    617 				return error;
    618 			error = copyin(p->mask, mask, count);
    619 			if (error)
    620 				return error;
    621 		}
    622 
    623 		/* parameters are OK; do it */
    624 		if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
    625 			if (v & FB_CUR_SETCUR)
    626 				pc->pc_enable = p->enable;
    627 			if (v & FB_CUR_SETPOS)
    628 				pc->pc_pos = p->pos;
    629 			if (v & FB_CUR_SETHOT)
    630 				pc->pc_hot = p->hot;
    631 			p9100_set_fbcursor(sc);
    632 		}
    633 
    634 		if (v & FB_CUR_SETCMAP) {
    635 			memcpy(pc->red, red, 3);
    636 			memcpy(pc->green, green, 3);
    637 			memcpy(pc->blue, blue, 3);
    638 			p9100_setcursorcmap(sc);
    639 		}
    640 
    641 		if (v & FB_CUR_SETSHAPE) {
    642 			memcpy(pc->pc_bits, image, 0x200);
    643 			memcpy(&pc->pc_bits[0x80], mask, 0x200);
    644 			p9100_loadcursor(sc);
    645 		}
    646 	}
    647 	break;
    648 
    649 #undef p
    650 #undef cc
    651 
    652 	case FBIOGCURPOS:
    653 		*(struct fbcurpos *)data = sc->sc_cursor.pc_pos;
    654 		break;
    655 
    656 	case FBIOSCURPOS:
    657 		sc->sc_cursor.pc_pos = *(struct fbcurpos *)data;
    658 		p9100_set_fbcursor(sc);
    659 		break;
    660 
    661 	case FBIOGCURMAX:
    662 		/* max cursor size is 64x64 */
    663 		((struct fbcurpos *)data)->x = 64;
    664 		((struct fbcurpos *)data)->y = 64;
    665 		break;
    666 
    667 	default:
    668 		return (ENOTTY);
    669 	}
    670 	return (0);
    671 }
    672 
    673 static uint32_t
    674 p9100_ctl_read_4(struct p9100_softc *sc, bus_size_t off)
    675 {
    676 
    677 	PNOZZ_LATCH(sc, off);
    678 	return bus_space_read_4(sc->sc_bustag, sc->sc_ctl_memh, off);
    679 }
    680 
    681 static void
    682 p9100_ctl_write_4(struct p9100_softc *sc, bus_size_t off, uint32_t v)
    683 {
    684 
    685 	PNOZZ_LATCH(sc, off);
    686 	bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off, v);
    687 }
    688 
    689 /* initialize the drawing engine */
    690 static void
    691 p9100_init_engine(struct p9100_softc *sc)
    692 {
    693 	/* reset clipping rectangles */
    694 	uint32_t rmax = ((sc->sc_width & 0x3fff) << 16) |
    695 	    (sc->sc_height & 0x3fff);
    696 
    697 	sc->sc_last_offset = 0xffffffff;
    698 
    699 	p9100_ctl_write_4(sc, WINDOW_OFFSET, 0);
    700 	p9100_ctl_write_4(sc, WINDOW_MIN, 0);
    701 	p9100_ctl_write_4(sc, WINDOW_MAX, rmax);
    702 	p9100_ctl_write_4(sc, BYTE_CLIP_MIN, 0);
    703 	p9100_ctl_write_4(sc, BYTE_CLIP_MAX, rmax);
    704 	p9100_ctl_write_4(sc, DRAW_MODE, 0);
    705 	p9100_ctl_write_4(sc, PLANE_MASK, 0xffffffff);
    706 	p9100_ctl_write_4(sc, PATTERN0, 0xffffffff);
    707 	p9100_ctl_write_4(sc, PATTERN1, 0xffffffff);
    708 	p9100_ctl_write_4(sc, PATTERN2, 0xffffffff);
    709 	p9100_ctl_write_4(sc, PATTERN3, 0xffffffff);
    710 }
    711 
    712 /* we only need these in the wsdisplay case */
    713 #if NWSDISPLAY > 0
    714 
    715 /* wait until the engine is idle */
    716 static void
    717 p9100_sync(struct p9100_softc *sc)
    718 {
    719 	while((p9100_ctl_read_4(sc, ENGINE_STATUS) &
    720 	    (ENGINE_BUSY | BLITTER_BUSY)) != 0);
    721 }
    722 
    723 static void
    724 p9100_set_color_reg(struct p9100_softc *sc, int reg, int32_t col)
    725 {
    726 	uint32_t out;
    727 
    728 	switch(sc->sc_depth)
    729 	{
    730 		case 1:	/* 8 bit */
    731 			out = (col << 8) | col;
    732 			out |= out << 16;
    733 			break;
    734 		case 2: /* 16 bit */
    735 			out = col | (col << 16);
    736 			break;
    737 		default:
    738 			out = col;
    739 	}
    740 	p9100_ctl_write_4(sc, reg, out);
    741 }
    742 
    743 /* screen-to-screen blit */
    744 static void
    745 p9100_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
    746     int he, uint32_t rop)
    747 {
    748 	struct p9100_softc *sc = cookie;
    749 	uint32_t src, dst, srcw, dstw;
    750 
    751 	sc->sc_last_offset = 0xffffffff;
    752 
    753 	src = ((xs & 0x3fff) << 16) | (ys & 0x3fff);
    754 	dst = ((xd & 0x3fff) << 16) | (yd & 0x3fff);
    755 	srcw = (((xs + wi - 1) & 0x3fff) << 16) | ((ys + he - 1) & 0x3fff);
    756 	dstw = (((xd + wi - 1) & 0x3fff) << 16) | ((yd + he - 1) & 0x3fff);
    757 	p9100_sync(sc);
    758 	p9100_ctl_write_4(sc, RASTER_OP, rop);
    759 
    760 	p9100_ctl_write_4(sc, ABS_XY0, src);
    761 
    762 	p9100_ctl_write_4(sc, ABS_XY1, srcw);
    763 	p9100_ctl_write_4(sc, ABS_XY2, dst);
    764 	p9100_ctl_write_4(sc, ABS_XY3, dstw);
    765 	sc->sc_junk = p9100_ctl_read_4(sc, COMMAND_BLIT);
    766 }
    767 
    768 /* solid rectangle fill */
    769 static void
    770 p9100_rectfill(void *cookie, int xs, int ys, int wi, int he, uint32_t col)
    771 {
    772 	struct p9100_softc *sc = cookie;
    773 	uint32_t src, srcw;
    774 
    775 	sc->sc_last_offset = 0xffffffff;
    776 
    777 	src = ((xs & 0x3fff) << 16) | (ys & 0x3fff);
    778 	srcw = (((xs + wi) & 0x3fff) << 16) | ((ys + he) & 0x3fff);
    779 	p9100_sync(sc);
    780 	p9100_set_color_reg(sc, FOREGROUND_COLOR, col);
    781 	p9100_set_color_reg(sc, BACKGROUND_COLOR, col);
    782 	p9100_ctl_write_4(sc, RASTER_OP, ROP_PAT);
    783 	p9100_ctl_write_4(sc, COORD_INDEX, 0);
    784 	p9100_ctl_write_4(sc, RECT_RTW_XY, src);
    785 	p9100_ctl_write_4(sc, RECT_RTW_XY, srcw);
    786 	sc->sc_junk = p9100_ctl_read_4(sc, COMMAND_QUAD);
    787 }
    788 
    789 /* setup for mono->colour expansion */
    790 static void
    791 p9100_setup_mono(struct p9100_softc *sc, int x, int y, int wi, int he,
    792     uint32_t fg, uint32_t bg)
    793 {
    794 
    795 	sc->sc_last_offset = 0xffffffff;
    796 
    797 	p9100_sync(sc);
    798 	/*
    799 	 * this doesn't make any sense to me either, but for some reason the
    800 	 * chip applies the foreground colour to 0 pixels
    801 	 */
    802 
    803 	p9100_set_color_reg(sc,FOREGROUND_COLOR,bg);
    804 	p9100_set_color_reg(sc,BACKGROUND_COLOR,fg);
    805 
    806 	p9100_ctl_write_4(sc, RASTER_OP, ROP_SRC);
    807 	p9100_ctl_write_4(sc, ABS_X0, x);
    808 	p9100_ctl_write_4(sc, ABS_XY1, (x << 16) | (y & 0xFFFFL));
    809 	p9100_ctl_write_4(sc, ABS_X2, (x + wi));
    810 	p9100_ctl_write_4(sc, ABS_Y3, he);
    811 	/* now feed the data into the chip */
    812 	sc->sc_mono_width = wi;
    813 }
    814 
    815 /* write monochrome data to the screen through the blitter */
    816 static void
    817 p9100_feed_line(struct p9100_softc *sc, int count, uint8_t *data)
    818 {
    819 	int i;
    820 	uint32_t latch = 0, bork;
    821 	int shift = 24;
    822 	int to_go = sc->sc_mono_width;
    823 
    824 	PNOZZ_LATCH(sc, PIXEL_1);
    825 
    826 	for (i = 0; i < count; i++) {
    827 		bork = data[i];
    828 		latch |= (bork << shift);
    829 		if (shift == 0) {
    830 			/* check how many bits are significant */
    831 			if (to_go > 31) {
    832 				bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh,
    833 				    (PIXEL_1 + (31 << 2)), latch);
    834 				/*p9100_ctl_write_4(sc, (PIXEL_1 +
    835 				    (31 << 2)), latch);*/
    836 				to_go -= 32;
    837 			} else
    838 			{
    839 				bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh,
    840 				    (PIXEL_1 + ((to_go - 1) << 2)), latch);
    841 				/*p9100_ctl_write_4(sc, (PIXEL_1 +
    842 				    ((to_go - 1) << 2)), latch);*/
    843 				to_go = 0;
    844 			}
    845 			latch = 0;
    846 			shift = 24;
    847 		} else
    848 			shift -= 8;
    849 		}
    850 	if (shift != 24)
    851 		p9100_ctl_write_4(sc, (PIXEL_1 + ((to_go - 1) << 2)), latch);
    852 }
    853 
    854 static void
    855 p9100_clearscreen(struct p9100_softc *sc)
    856 {
    857 
    858 	p9100_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
    859 }
    860 #endif /* NWSDISPLAY > 0 */
    861 
    862 static uint8_t
    863 p9100_ramdac_read(struct p9100_softc *sc, bus_size_t off)
    864 {
    865 
    866 	sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
    867 	return ((bus_space_read_4(sc->sc_bustag,
    868 	    sc->sc_ctl_memh, off) >> 16) & 0xff);
    869 }
    870 
    871 static void
    872 p9100_ramdac_write(struct p9100_softc *sc, bus_size_t off, uint8_t v)
    873 {
    874 
    875 	sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
    876 	bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off,
    877 	    ((uint32_t)v) << 16);
    878 }
    879 
    880 static uint8_t
    881 p9100_ramdac_read_ctl(struct p9100_softc *sc, int off)
    882 {
    883 	p9100_ramdac_write(sc, DAC_INDX_LO, off & 0xff);
    884 	p9100_ramdac_write(sc, DAC_INDX_HI, (off & 0xff00) >> 8);
    885 	return p9100_ramdac_read(sc, DAC_INDX_DATA);
    886 }
    887 
    888 static void
    889 p9100_ramdac_write_ctl(struct p9100_softc *sc, int off, uint8_t val)
    890 {
    891 	p9100_ramdac_write(sc, DAC_INDX_LO, off & 0xff);
    892 	p9100_ramdac_write(sc, DAC_INDX_HI, (off & 0xff00) >> 8);
    893 	p9100_ramdac_write(sc, DAC_INDX_DATA, val);
    894 }
    895 
    896 /*
    897  * Undo the effect of an FBIOSVIDEO that turns the video off.
    898  */
    899 static void
    900 p9100unblank(struct device *dev)
    901 {
    902 	struct p9100_softc *sc = (struct p9100_softc *)dev;
    903 
    904 	p9100_set_video((struct p9100_softc *)dev, 1);
    905 
    906 	/*
    907 	 * Check if we're in terminal mode. If not force the console screen
    908 	 * to front so we can see ddb, panic messages and so on
    909 	 */
    910 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
    911 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    912 		if (sc->vd.active != &p9100_console_screen) {
    913 			SCREEN_INVISIBLE(sc->vd.active);
    914 			sc->vd.active = &p9100_console_screen;
    915 			SCREEN_VISIBLE(&p9100_console_screen);
    916 		}
    917 		vcons_redraw_screen(&p9100_console_screen);
    918 	}
    919 }
    920 
    921 static void
    922 p9100_set_video(struct p9100_softc *sc, int enable)
    923 {
    924 	u_int32_t v = p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1);
    925 
    926 	if (enable)
    927 		v |= VIDEO_ENABLED;
    928 	else
    929 		v &= ~VIDEO_ENABLED;
    930 	p9100_ctl_write_4(sc, SCRN_RPNT_CTL_1, v);
    931 #if NTCTRL > 0
    932 	/* Turn On/Off the TFT if we know how.
    933 	 */
    934 	tadpole_set_video(enable);
    935 #endif
    936 }
    937 
    938 static int
    939 p9100_get_video(struct p9100_softc *sc)
    940 {
    941 	return (p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1) & VIDEO_ENABLED) != 0;
    942 }
    943 
    944 static void
    945 p9100_power_hook(int why, void *cookie)
    946 {
    947 	struct p9100_softc *sc = cookie;
    948 
    949 	if (why == sc->sc_powerstate)
    950 		return;
    951 
    952 	switch(why)
    953 	{
    954 		case PWR_SUSPEND:
    955 		case PWR_STANDBY:
    956 			sc->sc_video = p9100_get_video(sc);
    957 			p9100_set_video(sc, 0);
    958 			sc->sc_powerstate = why;
    959 			break;
    960 		case PWR_RESUME:
    961 			p9100_set_video(sc, sc->sc_video);
    962 			sc->sc_powerstate = why;
    963 			break;
    964 	}
    965 }
    966 
    967 /*
    968  * Load a subset of the current (new) colormap into the IBM RAMDAC.
    969  */
    970 static void
    971 p9100loadcmap(struct p9100_softc *sc, int start, int ncolors)
    972 {
    973 	int i;
    974 
    975 	sc->sc_last_offset = 0xffffffff;
    976 
    977 	p9100_ramdac_write(sc, DAC_CMAP_WRIDX, start);
    978 
    979 	for (i=0;i<ncolors;i++) {
    980 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
    981 		    sc->sc_cmap.cm_map[i + start][0]);
    982 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
    983 		    sc->sc_cmap.cm_map[i + start][1]);
    984 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
    985 		    sc->sc_cmap.cm_map[i + start][2]);
    986 	}
    987 }
    988 
    989 /*
    990  * Return the address that would map the given device at the given
    991  * offset, allowing for the given protection, or return -1 for error.
    992  */
    993 static paddr_t
    994 p9100mmap(dev_t dev, off_t off, int prot)
    995 {
    996 	struct p9100_softc *sc = pnozz_cd.cd_devs[minor(dev)];
    997 
    998 	if (off & PGOFSET)
    999 		panic("p9100mmap");
   1000 	if (off < 0)
   1001 		return (-1);
   1002 
   1003 #ifdef PNOZZ_EMUL_CG3
   1004 #define CG3_MMAP_OFFSET	0x04000000
   1005 	/* Make Xsun think we are a CG3 (SUN3COLOR)
   1006 	 */
   1007 	if (off >= CG3_MMAP_OFFSET && off < CG3_MMAP_OFFSET + sc->sc_fb_psize) {
   1008 		off -= CG3_MMAP_OFFSET;
   1009 		return (bus_space_mmap(sc->sc_bustag,
   1010 			sc->sc_fb_paddr,
   1011 			off,
   1012 			prot,
   1013 			BUS_SPACE_MAP_LINEAR));
   1014 	}
   1015 #endif
   1016 
   1017 	if (off >= sc->sc_fb_psize + sc->sc_ctl_psize + sc->sc_cmd_psize)
   1018 		return (-1);
   1019 
   1020 	if (off < sc->sc_fb_psize) {
   1021 		return (bus_space_mmap(sc->sc_bustag,
   1022 			sc->sc_fb_paddr,
   1023 			off,
   1024 			prot,
   1025 			BUS_SPACE_MAP_LINEAR));
   1026 	}
   1027 	off -= sc->sc_fb_psize;
   1028 	if (off < sc->sc_ctl_psize) {
   1029 		return (bus_space_mmap(sc->sc_bustag,
   1030 			sc->sc_ctl_paddr,
   1031 			off,
   1032 			prot,
   1033 			BUS_SPACE_MAP_LINEAR));
   1034 	}
   1035 	off -= sc->sc_ctl_psize;
   1036 
   1037 	return (bus_space_mmap(sc->sc_bustag,
   1038 		sc->sc_cmd_paddr,
   1039 		off,
   1040 		prot,
   1041 		BUS_SPACE_MAP_LINEAR));
   1042 }
   1043 
   1044 /* wscons stuff */
   1045 #if NWSDISPLAY > 0
   1046 
   1047 static void
   1048 p9100_cursor(void *cookie, int on, int row, int col)
   1049 {
   1050 	struct rasops_info *ri = cookie;
   1051 	struct vcons_screen *scr = ri->ri_hw;
   1052 	struct p9100_softc *sc = scr->scr_cookie;
   1053 	int x, y, wi,he;
   1054 
   1055 	wi = ri->ri_font->fontwidth;
   1056 	he = ri->ri_font->fontheight;
   1057 
   1058 	if (ri->ri_flg & RI_CURSOR) {
   1059 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1060 		y = ri->ri_crow * he + ri->ri_yorigin;
   1061 		p9100_bitblt(sc, x, y, x, y, wi, he, ROP_SRC ^ 0xff);
   1062 		ri->ri_flg &= ~RI_CURSOR;
   1063 	}
   1064 	ri->ri_crow = row;
   1065 	ri->ri_ccol = col;
   1066 	if (on)
   1067 	{
   1068 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1069 		y = ri->ri_crow * he + ri->ri_yorigin;
   1070 		p9100_bitblt(sc, x, y, x, y, wi, he, ROP_SRC ^ 0xff);
   1071 		ri->ri_flg |= RI_CURSOR;
   1072 	}
   1073 }
   1074 
   1075 #if 0
   1076 static int
   1077 p9100_mapchar(void *cookie, int uni, u_int *index)
   1078 {
   1079 	return 0;
   1080 }
   1081 #endif
   1082 
   1083 static void
   1084 p9100_putchar(void *cookie, int row, int col, u_int c, long attr)
   1085 {
   1086 	struct rasops_info *ri = cookie;
   1087 	struct vcons_screen *scr = ri->ri_hw;
   1088 	struct p9100_softc *sc = scr->scr_cookie;
   1089 
   1090 	int fg, bg, uc, i;
   1091 	uint8_t *data;
   1092 	int x, y, wi,he;
   1093 
   1094 	wi = ri->ri_font->fontwidth;
   1095 	he = ri->ri_font->fontheight;
   1096 
   1097 	if (!CHAR_IN_FONT(c, ri->ri_font))
   1098 		return;
   1099 	bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xff];
   1100 	fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xff];
   1101 	x = ri->ri_xorigin + col * wi;
   1102 	y = ri->ri_yorigin + row * he;
   1103 	if (c == 0x20) {
   1104 		p9100_rectfill(sc, x, y, wi, he, bg);
   1105 	} else {
   1106 		uc = c-ri->ri_font->firstchar;
   1107 		data = (uint8_t *)ri->ri_font->data + uc *
   1108 		    ri->ri_fontscale;
   1109 
   1110 		p9100_setup_mono(sc, x, y, wi, 1, fg, bg);
   1111 		for (i = 0; i < he; i++) {
   1112 			p9100_feed_line(sc, ri->ri_font->stride,
   1113 			    data);
   1114 			data += ri->ri_font->stride;
   1115 		}
   1116 		/*p9100_sync(sc);*/
   1117 	}
   1118 }
   1119 
   1120 /*
   1121  * wsdisplay_accessops
   1122  */
   1123 
   1124 int
   1125 p9100_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
   1126 	struct lwp *l)
   1127 {
   1128 	struct vcons_data *vd = v;
   1129 	struct p9100_softc *sc = vd->cookie;
   1130 	struct wsdisplay_fbinfo *wdf;
   1131 	struct vcons_screen *ms = vd->active;
   1132 
   1133 	switch (cmd) {
   1134 		case WSDISPLAYIO_GTYPE:
   1135 			*(u_int *)data = WSDISPLAY_TYPE_SB_P9100;
   1136 			return 0;
   1137 
   1138 		case FBIOGVIDEO:
   1139 		case WSDISPLAYIO_GVIDEO:
   1140 			*(int *)data = p9100_get_video(sc);
   1141 			return 0;
   1142 
   1143 		case WSDISPLAYIO_SVIDEO:
   1144 		case FBIOSVIDEO:
   1145 			p9100_set_video(sc, *(int *)data);
   1146 			return 0;
   1147 
   1148 		case WSDISPLAYIO_GINFO:
   1149 			wdf = (void *)data;
   1150 			wdf->height = ms->scr_ri.ri_height;
   1151 			wdf->width = ms->scr_ri.ri_width;
   1152 			wdf->depth = ms->scr_ri.ri_depth;
   1153 			wdf->cmsize = 256;
   1154 			return 0;
   1155 
   1156 		case WSDISPLAYIO_GETCMAP:
   1157 			return p9100_getcmap(sc, (struct wsdisplay_cmap *)data);
   1158 
   1159 		case WSDISPLAYIO_PUTCMAP:
   1160 			return p9100_putcmap(sc, (struct wsdisplay_cmap *)data);
   1161 
   1162 		case WSDISPLAYIO_SMODE:
   1163 			{
   1164 				int new_mode = *(int*)data;
   1165 				if (new_mode != sc->sc_mode)
   1166 				{
   1167 					sc->sc_mode = new_mode;
   1168 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
   1169 					{
   1170 						p9100_init_engine(sc);
   1171 						p9100loadcmap(sc, 0, 256);
   1172 						p9100_clearscreen(sc);
   1173 						vcons_redraw_screen(ms);
   1174 					}
   1175 				}
   1176 			}
   1177 	}
   1178 	return EPASSTHROUGH;
   1179 }
   1180 
   1181 static paddr_t
   1182 p9100_mmap(void *v, void *vs, off_t offset, int prot)
   1183 {
   1184 	struct vcons_data *vd = v;
   1185 	struct p9100_softc *sc = vd->cookie;
   1186 	paddr_t pa;
   1187 
   1188 	/* 'regular' framebuffer mmap()ing */
   1189 	if (offset < sc->sc_fb_psize) {
   1190 		pa = bus_space_mmap(sc->sc_bustag, sc->sc_fb_paddr + offset, 0,
   1191 		    prot, BUS_SPACE_MAP_LINEAR);
   1192 		return pa;
   1193 	}
   1194 
   1195 	if ((offset >= sc->sc_fb_paddr) && (offset < (sc->sc_fb_paddr +
   1196 	    sc->sc_fb_psize))) {
   1197 		pa = bus_space_mmap(sc->sc_bustag, offset, 0, prot,
   1198 		    BUS_SPACE_MAP_LINEAR);
   1199 		return pa;
   1200 	}
   1201 
   1202 	if ((offset >= sc->sc_ctl_paddr) && (offset < (sc->sc_ctl_paddr +
   1203 	    sc->sc_ctl_psize))) {
   1204 		pa = bus_space_mmap(sc->sc_bustag, offset, 0, prot,
   1205 		    BUS_SPACE_MAP_LINEAR);
   1206 		return pa;
   1207 	}
   1208 
   1209 	return -1;
   1210 }
   1211 
   1212 static void
   1213 p9100_init_screen(void *cookie, struct vcons_screen *scr,
   1214     int existing, long *defattr)
   1215 {
   1216 	struct p9100_softc *sc = cookie;
   1217 	struct rasops_info *ri = &scr->scr_ri;
   1218 
   1219 	ri->ri_depth = sc->sc_depth << 3;
   1220 	ri->ri_width = sc->sc_width;
   1221 	ri->ri_height = sc->sc_height;
   1222 	ri->ri_stride = sc->sc_stride;
   1223 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
   1224 
   1225 	ri->ri_bits = bus_space_vaddr(sc->sc_bustag, sc->sc_fb_memh);
   1226 
   1227 #ifdef DEBUG_P9100
   1228 	printf("addr: %08lx\n",(ulong)ri->ri_bits);
   1229 #endif
   1230 	rasops_init(ri, sc->sc_height/8, sc->sc_width/8);
   1231 	ri->ri_caps = WSSCREEN_WSCOLORS;
   1232 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
   1233 		    sc->sc_width / ri->ri_font->fontwidth);
   1234 
   1235 	/* enable acceleration */
   1236 	ri->ri_ops.cursor    = p9100_cursor;
   1237 	ri->ri_ops.copyrows  = p9100_copyrows;
   1238 	ri->ri_ops.eraserows = p9100_eraserows;
   1239 	ri->ri_ops.copycols  = p9100_copycols;
   1240 	ri->ri_ops.erasecols = p9100_erasecols;
   1241 	ri->ri_ops.putchar   = p9100_putchar;
   1242 	ri->ri_ops.allocattr = p9100_allocattr;
   1243 }
   1244 
   1245 static int
   1246 p9100_putcmap(struct p9100_softc *sc, struct wsdisplay_cmap *cm)
   1247 {
   1248 	u_int index = cm->index;
   1249 	u_int count = cm->count;
   1250 	int i, error;
   1251 	u_char rbuf[256], gbuf[256], bbuf[256];
   1252 	u_char *r, *g, *b;
   1253 
   1254 	if (cm->index >= 256 || cm->count > 256 ||
   1255 	    (cm->index + cm->count) > 256)
   1256 		return EINVAL;
   1257 	error = copyin(cm->red, &rbuf[index], count);
   1258 	if (error)
   1259 		return error;
   1260 	error = copyin(cm->green, &gbuf[index], count);
   1261 	if (error)
   1262 		return error;
   1263 	error = copyin(cm->blue, &bbuf[index], count);
   1264 	if (error)
   1265 		return error;
   1266 
   1267 	r = &rbuf[index];
   1268 	g = &gbuf[index];
   1269 	b = &bbuf[index];
   1270 
   1271 	for (i = 0; i < count; i++) {
   1272 		sc->sc_cmap.cm_map[index][0] = *r;
   1273 		sc->sc_cmap.cm_map[index][1] = *g;
   1274 		sc->sc_cmap.cm_map[index][2] = *b;
   1275 		index++;
   1276 		r++, g++, b++;
   1277 	}
   1278 	p9100loadcmap(sc, 0, 256);
   1279 	return 0;
   1280 }
   1281 
   1282 static int
   1283 p9100_getcmap(struct p9100_softc *sc, struct wsdisplay_cmap *cm)
   1284 {
   1285 	u_int index = cm->index;
   1286 	u_int count = cm->count;
   1287 	int error, i;
   1288 	uint8_t red[256], green[256], blue[256];
   1289 
   1290 	if (index >= 255 || count > 256 || index + count > 256)
   1291 		return EINVAL;
   1292 
   1293 	i = index;
   1294 	while (i < (index + count)) {
   1295 		red[i] = sc->sc_cmap.cm_map[i][0];
   1296 		green[i] = sc->sc_cmap.cm_map[i][1];
   1297 		blue[i] = sc->sc_cmap.cm_map[i][2];
   1298 		i++;
   1299 	}
   1300 	error = copyout(&red[index],   cm->red,   count);
   1301 	if (error)
   1302 		return error;
   1303 	error = copyout(&green[index], cm->green, count);
   1304 	if (error)
   1305 		return error;
   1306 	error = copyout(&blue[index],  cm->blue,  count);
   1307 	if (error)
   1308 		return error;
   1309 
   1310 	return 0;
   1311 }
   1312 
   1313 static void
   1314 p9100_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1315 {
   1316 	struct rasops_info *ri = cookie;
   1317 	struct vcons_screen *scr = ri->ri_hw;
   1318 	int32_t xs, xd, y, width, height;
   1319 
   1320 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1321 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1322 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1323 	width = ri->ri_font->fontwidth * ncols;
   1324 	height = ri->ri_font->fontheight;
   1325 	p9100_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, ROP_SRC);
   1326 }
   1327 
   1328 static void
   1329 p9100_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1330 {
   1331 	struct rasops_info *ri = cookie;
   1332 	struct vcons_screen *scr = ri->ri_hw;
   1333 	int32_t x, y, width, height, bg;
   1334 
   1335 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1336 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1337 	width = ri->ri_font->fontwidth * ncols;
   1338 	height = ri->ri_font->fontheight;
   1339 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1340 	p9100_rectfill(scr->scr_cookie, x, y, width, height, bg);
   1341 }
   1342 
   1343 static void
   1344 p9100_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1345 {
   1346 	struct rasops_info *ri = cookie;
   1347 	struct vcons_screen *scr = ri->ri_hw;
   1348 	int32_t x, ys, yd, width, height;
   1349 
   1350 	x = ri->ri_xorigin;
   1351 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1352 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1353 	width = ri->ri_emuwidth;
   1354 	height = ri->ri_font->fontheight * nrows;
   1355 	p9100_bitblt(scr->scr_cookie, x, ys, x, yd, width, height, ROP_SRC);
   1356 }
   1357 
   1358 static void
   1359 p9100_eraserows(void *cookie, int row, int nrows, long fillattr)
   1360 {
   1361 	struct rasops_info *ri = cookie;
   1362 	struct vcons_screen *scr = ri->ri_hw;
   1363 	int32_t x, y, width, height, bg;
   1364 
   1365 	if ((row == 0) && (nrows == ri->ri_rows)) {
   1366 		x = y = 0;
   1367 		width = ri->ri_width;
   1368 		height = ri->ri_height;
   1369 	} else {
   1370 		x = ri->ri_xorigin;
   1371 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1372 		width = ri->ri_emuwidth;
   1373 		height = ri->ri_font->fontheight * nrows;
   1374 	}
   1375 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1376 	p9100_rectfill(scr->scr_cookie, x, y, width, height, bg);
   1377 }
   1378 
   1379 
   1380 static int
   1381 p9100_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
   1382 {
   1383 	if ((fg == 0) && (bg == 0))
   1384 	{
   1385 		fg = WS_DEFAULT_FG;
   1386 		bg = WS_DEFAULT_BG;
   1387 	}
   1388 	if (flags & WSATTR_REVERSE) {
   1389 		*attrp = (bg & 0xff) << 24 | (fg & 0xff) << 16 |
   1390 		    (flags & 0xff) << 8;
   1391 	} else
   1392 		*attrp = (fg & 0xff) << 24 | (bg & 0xff) << 16 |
   1393 		    (flags & 0xff) << 8;
   1394 	return 0;
   1395 }
   1396 
   1397 #if 0
   1398 static int
   1399 p9100_load_font(void *v, void *cookie, struct wsdisplay_font *data)
   1400 {
   1401 
   1402 	return 0;
   1403 }
   1404 #endif
   1405 
   1406 #endif /* NWSDISPLAY > 0 */
   1407 
   1408 static int
   1409 p9100_intr(void *arg)
   1410 {
   1411 	/*p9100_softc *sc=arg;
   1412 	printf(".");*/
   1413 	return 1;
   1414 }
   1415 
   1416 static void
   1417 p9100_init_cursor(struct p9100_softc *sc)
   1418 {
   1419 
   1420 	memset(&sc->sc_cursor, 0, sizeof(struct pnozz_cursor));
   1421 	sc->sc_cursor.pc_size.x = 64;
   1422 	sc->sc_cursor.pc_size.y = 64;
   1423 
   1424 }
   1425 
   1426 static void
   1427 p9100_set_fbcursor(struct p9100_softc *sc)
   1428 {
   1429 #ifdef PNOZZ_PARANOID
   1430 	int s;
   1431 
   1432 	s = splhigh();	/* just in case... */
   1433 #endif
   1434 	sc->sc_last_offset = 0xffffffff;
   1435 
   1436 	/* set position and hotspot */
   1437 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
   1438 	p9100_ramdac_write(sc, DAC_INDX_HI, 0);
   1439 	p9100_ramdac_write(sc, DAC_INDX_LO, DAC_CURSOR_CTL);
   1440 	if (sc->sc_cursor.pc_enable) {
   1441 		p9100_ramdac_write(sc, DAC_INDX_DATA, DAC_CURSOR_X11 |
   1442 		    DAC_CURSOR_64);
   1443 	} else
   1444 		p9100_ramdac_write(sc, DAC_INDX_DATA, DAC_CURSOR_OFF);
   1445 	/* next two registers - x low, high, y low, high */
   1446 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_pos.x & 0xff);
   1447 	p9100_ramdac_write(sc, DAC_INDX_DATA, (sc->sc_cursor.pc_pos.x >> 8) &
   1448 	    0xff);
   1449 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_pos.y & 0xff);
   1450 	p9100_ramdac_write(sc, DAC_INDX_DATA, (sc->sc_cursor.pc_pos.y >> 8) &
   1451 	    0xff);
   1452 	/* hotspot */
   1453 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_hot.x & 0xff);
   1454 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_hot.y & 0xff);
   1455 
   1456 #ifdef PNOZZ_PARANOID
   1457 	splx(s);
   1458 #endif
   1459 
   1460 }
   1461 
   1462 static void
   1463 p9100_setcursorcmap(struct p9100_softc *sc)
   1464 {
   1465 	int i;
   1466 
   1467 #ifdef PNOZZ_PARANOID
   1468 	int s;
   1469 	s = splhigh();	/* just in case... */
   1470 #endif
   1471 	sc->sc_last_offset = 0xffffffff;
   1472 
   1473 	/* set cursor colours */
   1474 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
   1475 	p9100_ramdac_write(sc, DAC_INDX_HI, 0);
   1476 	p9100_ramdac_write(sc, DAC_INDX_LO, DAC_CURSOR_COL_1);
   1477 
   1478 	for (i = 0; i < 3; i++) {
   1479 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.red[i]);
   1480 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.green[i]);
   1481 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.blue[i]);
   1482 	}
   1483 
   1484 #ifdef PNOZZ_PARANOID
   1485 	splx(s);
   1486 #endif
   1487 }
   1488 
   1489 static void
   1490 p9100_loadcursor(struct p9100_softc *sc)
   1491 {
   1492 	uint32_t *image, *mask;
   1493 	uint32_t bit, bbit, im, ma;
   1494 	int i, j, k;
   1495 	uint8_t latch1, latch2;
   1496 
   1497 #ifdef PNOZZ_PARANOID
   1498 	int s;
   1499 	s = splhigh();	/* just in case... */
   1500 #endif
   1501 	sc->sc_last_offset = 0xffffffff;
   1502 
   1503 	/* set cursor shape */
   1504 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
   1505 	p9100_ramdac_write(sc, DAC_INDX_HI, 1);
   1506 	p9100_ramdac_write(sc, DAC_INDX_LO, 0);
   1507 
   1508 	image = sc->sc_cursor.pc_bits;
   1509 	mask = &sc->sc_cursor.pc_bits[0x80];
   1510 
   1511 	for (i = 0; i < 0x80; i++) {
   1512 		bit = 0x80000000;
   1513 		im = image[i];
   1514 		ma = mask[i];
   1515 		for (k = 0; k < 4; k++) {
   1516 			bbit = 0x1;
   1517 			latch1 = 0;
   1518 			for (j = 0; j < 4; j++) {
   1519 				if (im & bit)
   1520 					latch1 |= bbit;
   1521 				bbit <<= 1;
   1522 				if (ma & bit)
   1523 					latch1 |= bbit;
   1524 				bbit <<= 1;
   1525 				bit >>= 1;
   1526 			}
   1527 			bbit = 0x1;
   1528 			latch2 = 0;
   1529 			for (j = 0; j < 4; j++) {
   1530 				if (im & bit)
   1531 					latch2 |= bbit;
   1532 				bbit <<= 1;
   1533 				if (ma & bit)
   1534 					latch2 |= bbit;
   1535 				bbit <<= 1;
   1536 				bit >>= 1;
   1537 			}
   1538 			p9100_ramdac_write(sc, DAC_INDX_DATA, latch1);
   1539 			p9100_ramdac_write(sc, DAC_INDX_DATA, latch2);
   1540 		}
   1541 	}
   1542 #ifdef DEBUG_CURSOR
   1543 	printf("image:\n");
   1544 	for (i=0;i<0x80;i+=2)
   1545 		printf("%08x %08x\n", image[i], image[i+1]);
   1546 	printf("mask:\n");
   1547 	for (i=0;i<0x80;i+=2)
   1548 		printf("%08x %08x\n", mask[i], mask[i+1]);
   1549 #endif
   1550 #ifdef PNOZZ_PARANOID
   1551 	splx(s);
   1552 #endif
   1553 }
   1554 
   1555 static void
   1556 p9100_set_extvga(void *cookie, int status)
   1557 {
   1558 	struct p9100_softc *sc = cookie;
   1559 #ifdef PNOZZ_PARANOID
   1560 	int s;
   1561 
   1562 	s = splhigh();
   1563 #endif
   1564 #ifdef DEBUG
   1565 	printf("%s: external VGA %s\n", sc->sc_dev.dv_xname,
   1566 	    status ? "on" : "off");
   1567 #endif
   1568 	sc->sc_last_offset = 0xffffffff;
   1569 
   1570 	if (status) {
   1571 		p9100_ramdac_write_ctl(sc, DAC_POWER,
   1572 		    p9100_ramdac_read_ctl(sc, DAC_POWER) &
   1573 		    ~DAC_POWER_IPWR_DISABLE);
   1574 	} else {
   1575 		p9100_ramdac_write_ctl(sc, DAC_POWER,
   1576 		    p9100_ramdac_read_ctl(sc, DAC_POWER) |
   1577 		    DAC_POWER_IPWR_DISABLE);
   1578 	}
   1579 #ifdef PNOZZ_PARANOID
   1580 	splx(s);
   1581 #endif
   1582 }
   1583