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