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