Home | History | Annotate | Line # | Download | only in sbus
p9100.c revision 1.56.6.1
      1 /*	$NetBSD: p9100.c,v 1.56.6.1 2012/11/20 03:02:32 tls 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.56.6.1 2012/11/20 03:02:32 tls 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 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     72 
     73 #include "opt_wsemul.h"
     74 #include "rasops_glue.h"
     75 #include "opt_pnozz.h"
     76 
     77 #include "ioconf.h"
     78 
     79 #include "tctrl.h"
     80 #if NTCTRL > 0
     81 #include <machine/tctrl.h>
     82 #include <sparc/dev/tctrlvar.h>	/*XXX*/
     83 #endif
     84 
     85 #ifdef PNOZZ_DEBUG
     86 #define DPRINTF aprint_normal
     87 #else
     88 #define DPRINTF while (0) aprint_normal
     89 #endif
     90 
     91 struct pnozz_cursor {
     92 	short	pc_enable;		/* cursor is enabled */
     93 	struct	fbcurpos pc_pos;	/* position */
     94 	struct	fbcurpos pc_hot;	/* hot-spot */
     95 	struct	fbcurpos pc_size;	/* size of mask & image fields */
     96 	uint32_t pc_bits[0x100];	/* space for mask & image bits */
     97 	unsigned char red[3], green[3];
     98 	unsigned char blue[3];		/* cursor palette */
     99 };
    100 
    101 /* per-display variables */
    102 struct p9100_softc {
    103 	device_t	sc_dev;		/* base device */
    104 	struct fbdevice	sc_fb;		/* frame buffer device */
    105 
    106 	bus_space_tag_t	sc_bustag;
    107 
    108 	bus_addr_t	sc_ctl_paddr;	/* phys address description */
    109 	bus_size_t	sc_ctl_psize;	/*   for device mmap() */
    110 	bus_space_handle_t sc_ctl_memh;	/*   bus space handle */
    111 
    112 	bus_addr_t	sc_fb_paddr;	/* phys address description */
    113 	bus_size_t	sc_fb_psize;	/*   for device mmap() */
    114 #ifdef PNOZZ_USE_LATCH
    115 	bus_space_handle_t sc_fb_memh;	/*   bus space handle */
    116 #endif
    117 	volatile uint32_t sc_junk;
    118 	uint32_t 	sc_mono_width;	/* for setup_mono */
    119 
    120 	uint32_t	sc_width;
    121 	uint32_t	sc_height;	/* panel width / height */
    122 	uint32_t	sc_stride;
    123 	uint32_t	sc_depth;
    124 	int		sc_depthshift;	/* blitter works on bytes not pixels */
    125 
    126 	union	bt_cmap sc_cmap;	/* Brooktree color map */
    127 
    128 	struct pnozz_cursor sc_cursor;
    129 
    130 	int 		sc_mode;
    131 	int 		sc_video, sc_powerstate;
    132 	uint32_t 	sc_bg;
    133 	volatile uint32_t sc_last_offset;
    134 	struct vcons_data vd;
    135 	uint8_t		sc_dac_power;
    136 	glyphcache	sc_gc;
    137 };
    138 
    139 
    140 static struct vcons_screen p9100_console_screen;
    141 
    142 extern const u_char rasops_cmap[768];
    143 
    144 struct wsscreen_descr p9100_defscreendesc = {
    145 	"default",
    146 	0, 0,
    147 	NULL,
    148 	8, 16,
    149 	WSSCREEN_WSCOLORS,
    150 };
    151 
    152 const struct wsscreen_descr *_p9100_scrlist[] = {
    153 	&p9100_defscreendesc,
    154 	/* XXX other formats, graphics screen? */
    155 };
    156 
    157 struct wsscreen_list p9100_screenlist = {
    158 	sizeof(_p9100_scrlist) / sizeof(struct wsscreen_descr *),
    159 	_p9100_scrlist
    160 };
    161 
    162 /* autoconfiguration driver */
    163 static int	p9100_sbus_match(device_t, cfdata_t, void *);
    164 static void	p9100_sbus_attach(device_t, device_t, void *);
    165 
    166 static void	p9100unblank(device_t);
    167 
    168 CFATTACH_DECL_NEW(pnozz, sizeof(struct p9100_softc),
    169     p9100_sbus_match, p9100_sbus_attach, NULL, NULL);
    170 
    171 static dev_type_open(p9100open);
    172 static dev_type_close(p9100close);
    173 static dev_type_ioctl(p9100ioctl);
    174 static dev_type_mmap(p9100mmap);
    175 
    176 const struct cdevsw pnozz_cdevsw = {
    177 	p9100open, nullclose, noread, nowrite, p9100ioctl,
    178 	nostop, notty, nopoll, p9100mmap, nokqfilter,
    179 };
    180 
    181 /* frame buffer generic driver */
    182 static struct fbdriver p9100fbdriver = {
    183 	p9100unblank, p9100open, p9100close, p9100ioctl, nopoll,
    184 	p9100mmap, nokqfilter
    185 };
    186 
    187 static void	p9100loadcmap(struct p9100_softc *, int, int);
    188 static void	p9100_set_video(struct p9100_softc *, int);
    189 static int	p9100_get_video(struct p9100_softc *);
    190 static uint32_t p9100_ctl_read_4(struct p9100_softc *, bus_size_t);
    191 static void	p9100_ctl_write_4(struct p9100_softc *, bus_size_t, uint32_t);
    192 static uint8_t	p9100_ramdac_read(struct p9100_softc *, bus_size_t);
    193 static void	p9100_ramdac_write(struct p9100_softc *, bus_size_t, uint8_t);
    194 
    195 static uint8_t	p9100_ramdac_read_ctl(struct p9100_softc *, int);
    196 static void	p9100_ramdac_write_ctl(struct p9100_softc *, int, uint8_t);
    197 
    198 static void 	p9100_init_engine(struct p9100_softc *);
    199 static int	p9100_set_depth(struct p9100_softc *, int);
    200 
    201 #if NWSDISPLAY > 0
    202 static void	p9100_sync(struct p9100_softc *);
    203 static void	p9100_bitblt(void *, int, int, int, int, int, int, int);
    204 static void 	p9100_rectfill(void *, int, int, int, int, uint32_t);
    205 static void	p9100_clearscreen(struct p9100_softc *);
    206 
    207 static void	p9100_setup_mono(struct p9100_softc *, int, int, int, int,
    208 		    uint32_t, uint32_t);
    209 static void	p9100_feed_line(struct p9100_softc *, int, uint8_t *);
    210 static void	p9100_set_color_reg(struct p9100_softc *, int, int32_t);
    211 
    212 static void	p9100_copycols(void *, int, int, int, int);
    213 static void	p9100_erasecols(void *, int, int, int, long);
    214 static void	p9100_copyrows(void *, int, int, int);
    215 static void	p9100_eraserows(void *, int, int, long);
    216 /*static int	p9100_mapchar(void *, int, u_int *);*/
    217 static void	p9100_putchar(void *, int, int, u_int, long);
    218 static void	p9100_putchar_aa(void *, int, int, u_int, long);
    219 static void	p9100_cursor(void *, int, int, int);
    220 
    221 static int	p9100_putcmap(struct p9100_softc *, struct wsdisplay_cmap *);
    222 static int 	p9100_getcmap(struct p9100_softc *, struct wsdisplay_cmap *);
    223 static int	p9100_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    224 static paddr_t	p9100_mmap(void *, void *, off_t, int);
    225 
    226 /*static int	p9100_load_font(void *, void *, struct wsdisplay_font *);*/
    227 
    228 static void	p9100_init_screen(void *, struct vcons_screen *, int,
    229 		    long *);
    230 #endif
    231 
    232 static void	p9100_init_cursor(struct p9100_softc *);
    233 
    234 static void	p9100_set_fbcursor(struct p9100_softc *);
    235 static void	p9100_setcursorcmap(struct p9100_softc *);
    236 static void	p9100_loadcursor(struct p9100_softc *);
    237 
    238 #if 0
    239 static int	p9100_intr(void *);
    240 #endif
    241 
    242 /* power management stuff */
    243 static bool p9100_suspend(device_t, const pmf_qual_t *);
    244 static bool p9100_resume(device_t, const pmf_qual_t *);
    245 
    246 #if NTCTRL > 0
    247 static void p9100_set_extvga(void *, int);
    248 #endif
    249 
    250 #if NWSDISPLAY > 0
    251 struct wsdisplay_accessops p9100_accessops = {
    252 	p9100_ioctl,
    253 	p9100_mmap,
    254 	NULL,	/* vcons_alloc_screen */
    255 	NULL,	/* vcons_free_screen */
    256 	NULL,	/* vcons_show_screen */
    257 	NULL,	/* load_font */
    258 	NULL,	/* polls */
    259 	NULL,	/* scroll */
    260 };
    261 #endif
    262 
    263 #ifdef PNOZZ_USE_LATCH
    264 #define PNOZZ_LATCH(sc, off) if(sc->sc_last_offset != (off & 0xffffff80)) { \
    265 		sc->sc_junk = bus_space_read_4(sc->sc_bustag, sc->sc_fb_memh, \
    266 		    off); \
    267 		sc->sc_last_offset = off & 0xffffff80; }
    268 #else
    269 #define PNOZZ_LATCH(a, b)
    270 #endif
    271 
    272 /*
    273  * Match a p9100.
    274  */
    275 static int
    276 p9100_sbus_match(device_t parent, cfdata_t cf, void *aux)
    277 {
    278 	struct sbus_attach_args *sa = aux;
    279 
    280 	if (strcmp("p9100", sa->sa_name) == 0)
    281 		return 100;
    282 	return 0;
    283 }
    284 
    285 
    286 /*
    287  * Attach a display.  We need to notice if it is the console, too.
    288  */
    289 static void
    290 p9100_sbus_attach(device_t parent, device_t self, void *args)
    291 {
    292 	struct p9100_softc *sc = device_private(self);
    293 	struct sbus_attach_args *sa = args;
    294 	struct fbdevice *fb = &sc->sc_fb;
    295 	int isconsole;
    296 	int node = sa->sa_node;
    297 	int i, j;
    298 	uint8_t ver, cmap[768];
    299 
    300 #if NWSDISPLAY > 0
    301 	struct wsemuldisplaydev_attach_args aa;
    302 	struct rasops_info *ri;
    303 	unsigned long defattr;
    304 #endif
    305 
    306 	sc->sc_last_offset = 0xffffffff;
    307 	sc->sc_dev = self;
    308 
    309 	/*
    310 	 * When the ROM has mapped in a p9100 display, the address
    311 	 * maps only the video RAM, so in any case we have to map the
    312 	 * registers ourselves.
    313 	 */
    314 
    315 	if (sa->sa_npromvaddrs != 0)
    316 		fb->fb_pixels = (void *)sa->sa_promvaddrs[0];
    317 
    318 	/* Remember cookies for p9100_mmap() */
    319 	sc->sc_bustag = sa->sa_bustag;
    320 
    321 	sc->sc_ctl_paddr = sbus_bus_addr(sa->sa_bustag,
    322 		sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base);
    323 	sc->sc_ctl_psize = 0x8000;/*(bus_size_t)sa->sa_reg[0].oa_size;*/
    324 
    325 	sc->sc_fb_paddr = sbus_bus_addr(sa->sa_bustag,
    326 		sa->sa_reg[2].oa_space, sa->sa_reg[2].oa_base);
    327 	sc->sc_fb_psize = (bus_size_t)sa->sa_reg[2].oa_size;
    328 
    329 	if (sbus_bus_map(sc->sc_bustag,
    330 	    sa->sa_reg[0].oa_space,
    331 	    sa->sa_reg[0].oa_base,
    332 	    /*
    333 	     * XXX for some reason the SBus resources don't cover
    334 	     * all registers, so we just map what we need
    335 	     */
    336 	    0x8000,
    337 	    0, &sc->sc_ctl_memh) != 0) {
    338 		printf("%s: cannot map control registers\n",
    339 		    device_xname(self));
    340 		return;
    341 	}
    342 
    343 	/*
    344 	 * we need to map the framebuffer even though we never write to it,
    345 	 * thanks to some weirdness in the SPARCbook's SBus glue for the
    346 	 * P9100 - all register accesses need to be 'latched in' whenever we
    347 	 * go to another 0x80 aligned 'page' by reading the framebuffer at the
    348 	 * same offset
    349 	 * XXX apparently the latter isn't true - my SP3GX works fine without
    350 	 */
    351 #ifdef PNOZZ_USE_LATCH
    352 	if (fb->fb_pixels == NULL) {
    353 		if (sbus_bus_map(sc->sc_bustag,
    354 		    sa->sa_reg[2].oa_space,
    355 		    sa->sa_reg[2].oa_base,
    356 		    sc->sc_fb_psize,
    357 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    358 		    &sc->sc_fb_memh) != 0) {
    359 			printf("%s: cannot map framebuffer\n",
    360 			    device_xname(self));
    361 			return;
    362 		}
    363 		fb->fb_pixels = (char *)sc->sc_fb_memh;
    364 	} else {
    365 		sc->sc_fb_memh = (bus_space_handle_t) fb->fb_pixels;
    366 	}
    367 #endif
    368 	sc->sc_width = prom_getpropint(node, "width", 800);
    369 	sc->sc_height = prom_getpropint(node, "height", 600);
    370 	sc->sc_depth = prom_getpropint(node, "depth", 8) >> 3;
    371 
    372 	sc->sc_stride = prom_getpropint(node, "linebytes",
    373 	    sc->sc_width * sc->sc_depth);
    374 
    375 	fb->fb_driver = &p9100fbdriver;
    376 	fb->fb_device = sc->sc_dev;
    377 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
    378 #ifdef PNOZZ_EMUL_CG3
    379 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
    380 #else
    381 	fb->fb_type.fb_type = FBTYPE_P9100;
    382 #endif
    383 	fb->fb_pixels = NULL;
    384 
    385 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    386 
    387 	isconsole = fb_is_console(node);
    388 #if 0
    389 	if (!isconsole) {
    390 		aprint_normal("\n");
    391 		aprint_error_dev(self, "fatal error: PROM didn't configure device\n");
    392 		return;
    393 	}
    394 #endif
    395 
    396     	fb->fb_type.fb_depth = 8;
    397 	sc->sc_depth = 1;
    398 	sc->sc_depthshift = 0;
    399 
    400 	/* check the RAMDAC */
    401 	ver = p9100_ramdac_read_ctl(sc, DAC_VERSION);
    402 
    403 	p9100_init_engine(sc);
    404 	p9100_set_depth(sc, 8);
    405 
    406 	fb_setsize_obp(fb, fb->fb_type.fb_depth, sc->sc_width, sc->sc_height,
    407 	    node);
    408 
    409 #if 0
    410 	bus_intr_establish(sc->sc_bustag, sa->sa_pri, IPL_BIO,
    411 	    p9100_intr, sc);
    412 #endif
    413 
    414 	fb->fb_type.fb_cmsize = prom_getpropint(node, "cmsize", 256);
    415 	if ((1 << fb->fb_type.fb_depth) != fb->fb_type.fb_cmsize)
    416 		printf(", %d entry colormap", fb->fb_type.fb_cmsize);
    417 
    418 	/* make sure we are not blanked */
    419 	if (isconsole)
    420 		p9100_set_video(sc, 1);
    421 
    422 	/* register with power management */
    423 	sc->sc_video = 1;
    424 	sc->sc_powerstate = PWR_RESUME;
    425 	if (!pmf_device_register(self, p9100_suspend, p9100_resume)) {
    426 		panic("%s: could not register with PMF",
    427 		      device_xname(sc->sc_dev));
    428 	}
    429 
    430 	if (isconsole) {
    431 		printf(" (console)\n");
    432 #ifdef RASTERCONSOLE
    433 		/*p9100loadcmap(sc, 255, 1);*/
    434 		fbrcons_init(fb);
    435 #endif
    436 	} else
    437 		printf("\n");
    438 
    439 #if NWSDISPLAY > 0
    440 	wsfont_init();
    441 
    442 #ifdef PNOZZ_DEBUG
    443 	/* make the glyph cache visible */
    444 	sc->sc_height -= 100;
    445 #endif
    446 
    447 	sc->sc_gc.gc_bitblt = p9100_bitblt;
    448 	sc->sc_gc.gc_blitcookie = sc;
    449 	sc->sc_gc.gc_rop = ROP_SRC;
    450 
    451 	vcons_init(&sc->vd, sc, &p9100_defscreendesc, &p9100_accessops);
    452 	sc->vd.init_screen = p9100_init_screen;
    453 
    454 	vcons_init_screen(&sc->vd, &p9100_console_screen, 1, &defattr);
    455 	p9100_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    456 
    457 	/* Initialize the default color map. */
    458 	rasops_get_cmap(&p9100_console_screen.scr_ri, cmap, 768);
    459 
    460 	j = 0;
    461 	for (i = 0; i < 256; i++) {
    462 		sc->sc_cmap.cm_map[i][0] = cmap[j];
    463 		j++;
    464 		sc->sc_cmap.cm_map[i][1] = cmap[j];
    465 		j++;
    466 		sc->sc_cmap.cm_map[i][2] = cmap[j];
    467 		j++;
    468 	}
    469 	p9100loadcmap(sc, 0, 256);
    470 
    471 	sc->sc_bg = (defattr >> 16) & 0xff;
    472 	p9100_clearscreen(sc);
    473 
    474 	ri = &p9100_console_screen.scr_ri;
    475 
    476 	p9100_defscreendesc.nrows = ri->ri_rows;
    477 	p9100_defscreendesc.ncols = ri->ri_cols;
    478 	p9100_defscreendesc.textops = &ri->ri_ops;
    479 	p9100_defscreendesc.capabilities = ri->ri_caps;
    480 
    481 	glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    482 			(0x200000 / sc->sc_stride) - sc->sc_height - 5,
    483 			sc->sc_width,
    484 			ri->ri_font->fontwidth,
    485 			ri->ri_font->fontheight,
    486 			defattr);
    487 
    488 	if(isconsole) {
    489 		wsdisplay_cnattach(&p9100_defscreendesc, ri, 0, 0, defattr);
    490 		vcons_replay_msgbuf(&p9100_console_screen);
    491 	}
    492 
    493 	aa.console = isconsole;
    494 	aa.scrdata = &p9100_screenlist;
    495 	aa.accessops = &p9100_accessops;
    496 	aa.accesscookie = &sc->vd;
    497 
    498 	config_found(self, &aa, wsemuldisplaydevprint);
    499 #endif
    500 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    501 	printf("%s: rev %d / %x, %dx%d, depth %d mem %x\n",
    502 		device_xname(self),
    503 		(i & 7), ver, fb->fb_type.fb_width, fb->fb_type.fb_height,
    504 		fb->fb_type.fb_depth, (unsigned int)sc->sc_fb_psize);
    505 	/* cursor sprite handling */
    506 	p9100_init_cursor(sc);
    507 
    508 	/* attach the fb */
    509 	fb_attach(fb, isconsole);
    510 
    511 #if NTCTRL > 0
    512 	/* register callback for external monitor status change */
    513 	tadpole_register_callback(p9100_set_extvga, sc);
    514 #endif
    515 }
    516 
    517 int
    518 p9100open(dev_t dev, int flags, int mode, struct lwp *l)
    519 {
    520 	int unit = minor(dev);
    521 
    522 	if (device_lookup(&pnozz_cd, unit) == NULL)
    523 		return (ENXIO);
    524 	return (0);
    525 }
    526 
    527 int
    528 p9100close(dev_t dev, int flags, int mode, struct lwp *l)
    529 {
    530 	struct p9100_softc *sc = device_lookup_private(&pnozz_cd, minor(dev));
    531 
    532 #if NWSDISPLAY > 0
    533 	p9100_init_engine(sc);
    534 	p9100_set_depth(sc, 8);
    535 	p9100loadcmap(sc, 0, 256);
    536 	p9100_clearscreen(sc);
    537 	glyphcache_wipe(&sc->sc_gc);
    538 	vcons_redraw_screen(sc->vd.active);
    539 #endif
    540 	return 0;
    541 }
    542 
    543 int
    544 p9100ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    545 {
    546 	struct p9100_softc *sc = device_lookup_private(&pnozz_cd, minor(dev));
    547 	struct fbgattr *fba;
    548 	int error, v;
    549 
    550 	switch (cmd) {
    551 
    552 	case FBIOGTYPE:
    553 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    554 		break;
    555 
    556 	case FBIOGATTR:
    557 		fba = (struct fbgattr *)data;
    558 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    559 		fba->owner = 0;		/* XXX ??? */
    560 		fba->fbtype = sc->sc_fb.fb_type;
    561 		fba->sattr.flags = 0;
    562 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    563 		fba->sattr.dev_specific[0] = -1;
    564 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    565 		fba->emu_types[1] = -1;
    566 		break;
    567 
    568 	case FBIOGETCMAP:
    569 #define p ((struct fbcmap *)data)
    570 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    571 
    572 	case FBIOPUTCMAP:
    573 		/* copy to software map */
    574 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    575 		if (error)
    576 			return (error);
    577 		/* now blast them into the chip */
    578 		/* XXX should use retrace interrupt */
    579 		p9100loadcmap(sc, p->index, p->count);
    580 #undef p
    581 		break;
    582 
    583 	case FBIOGVIDEO:
    584 		*(int *)data = p9100_get_video(sc);
    585 		break;
    586 
    587 	case FBIOSVIDEO:
    588 		p9100_set_video(sc, *(int *)data);
    589 		break;
    590 
    591 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
    592 #define p ((struct fbcursor *)data)
    593 #define pc (&sc->sc_cursor)
    594 
    595 	case FBIOGCURSOR:
    596 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
    597 		p->enable = pc->pc_enable;
    598 		p->pos = pc->pc_pos;
    599 		p->hot = pc->pc_hot;
    600 		p->size = pc->pc_size;
    601 
    602 		if (p->image != NULL) {
    603 			error = copyout(pc->pc_bits, p->image, 0x200);
    604 			if (error)
    605 				return error;
    606 			error = copyout(&pc->pc_bits[0x80], p->mask, 0x200);
    607 			if (error)
    608 				return error;
    609 		}
    610 
    611 		p->cmap.index = 0;
    612 		p->cmap.count = 3;
    613 		if (p->cmap.red != NULL) {
    614 			copyout(pc->red, p->cmap.red, 3);
    615 			copyout(pc->green, p->cmap.green, 3);
    616 			copyout(pc->blue, p->cmap.blue, 3);
    617 		}
    618 		break;
    619 
    620 	case FBIOSCURSOR:
    621 	{
    622 		int count;
    623 		uint32_t image[0x80], mask[0x80];
    624 		uint8_t red[3], green[3], blue[3];
    625 
    626 		v = p->set;
    627 		if (v & FB_CUR_SETCMAP) {
    628 			error = copyin(p->cmap.red, red, 3);
    629 			error |= copyin(p->cmap.green, green, 3);
    630 			error |= copyin(p->cmap.blue, blue, 3);
    631 			if (error)
    632 				return error;
    633 		}
    634 		if (v & FB_CUR_SETSHAPE) {
    635 			if (p->size.x > 64 || p->size.y > 64)
    636 				return EINVAL;
    637 			memset(&mask, 0, 0x200);
    638 			memset(&image, 0, 0x200);
    639 			count = p->size.y * 8;
    640 			error = copyin(p->image, image, count);
    641 			if (error)
    642 				return error;
    643 			error = copyin(p->mask, mask, count);
    644 			if (error)
    645 				return error;
    646 		}
    647 
    648 		/* parameters are OK; do it */
    649 		if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
    650 			if (v & FB_CUR_SETCUR)
    651 				pc->pc_enable = p->enable;
    652 			if (v & FB_CUR_SETPOS)
    653 				pc->pc_pos = p->pos;
    654 			if (v & FB_CUR_SETHOT)
    655 				pc->pc_hot = p->hot;
    656 			p9100_set_fbcursor(sc);
    657 		}
    658 
    659 		if (v & FB_CUR_SETCMAP) {
    660 			memcpy(pc->red, red, 3);
    661 			memcpy(pc->green, green, 3);
    662 			memcpy(pc->blue, blue, 3);
    663 			p9100_setcursorcmap(sc);
    664 		}
    665 
    666 		if (v & FB_CUR_SETSHAPE) {
    667 			memcpy(pc->pc_bits, image, 0x200);
    668 			memcpy(&pc->pc_bits[0x80], mask, 0x200);
    669 			p9100_loadcursor(sc);
    670 		}
    671 	}
    672 	break;
    673 
    674 #undef p
    675 #undef cc
    676 
    677 	case FBIOGCURPOS:
    678 		*(struct fbcurpos *)data = sc->sc_cursor.pc_pos;
    679 		break;
    680 
    681 	case FBIOSCURPOS:
    682 		sc->sc_cursor.pc_pos = *(struct fbcurpos *)data;
    683 		p9100_set_fbcursor(sc);
    684 		break;
    685 
    686 	case FBIOGCURMAX:
    687 		/* max cursor size is 64x64 */
    688 		((struct fbcurpos *)data)->x = 64;
    689 		((struct fbcurpos *)data)->y = 64;
    690 		break;
    691 
    692 	default:
    693 		return (ENOTTY);
    694 	}
    695 	return (0);
    696 }
    697 
    698 static uint32_t
    699 p9100_ctl_read_4(struct p9100_softc *sc, bus_size_t off)
    700 {
    701 
    702 	PNOZZ_LATCH(sc, off);
    703 	return bus_space_read_4(sc->sc_bustag, sc->sc_ctl_memh, off);
    704 }
    705 
    706 static void
    707 p9100_ctl_write_4(struct p9100_softc *sc, bus_size_t off, uint32_t v)
    708 {
    709 
    710 	PNOZZ_LATCH(sc, off);
    711 	bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off, v);
    712 }
    713 
    714 /* initialize the drawing engine */
    715 static void
    716 p9100_init_engine(struct p9100_softc *sc)
    717 {
    718 	/* reset clipping rectangles */
    719 	uint32_t rmax = ((sc->sc_width & 0x3fff) << 16) |
    720 	    (sc->sc_height & 0x3fff);
    721 
    722 	sc->sc_last_offset = 0xffffffff;
    723 
    724 	p9100_ctl_write_4(sc, WINDOW_OFFSET, 0);
    725 	p9100_ctl_write_4(sc, WINDOW_MIN, 0);
    726 	p9100_ctl_write_4(sc, WINDOW_MAX, rmax);
    727 	p9100_ctl_write_4(sc, BYTE_CLIP_MIN, 0);
    728 	p9100_ctl_write_4(sc, BYTE_CLIP_MAX, 0x3fff3fff);
    729 	p9100_ctl_write_4(sc, DRAW_MODE, 0);
    730 	p9100_ctl_write_4(sc, PLANE_MASK, 0xffffffff);
    731 	p9100_ctl_write_4(sc, PATTERN0, 0xffffffff);
    732 	p9100_ctl_write_4(sc, PATTERN1, 0xffffffff);
    733 	p9100_ctl_write_4(sc, PATTERN2, 0xffffffff);
    734 	p9100_ctl_write_4(sc, PATTERN3, 0xffffffff);
    735 
    736 }
    737 
    738 /* we only need these in the wsdisplay case */
    739 #if NWSDISPLAY > 0
    740 
    741 /* wait until the engine is idle */
    742 static void
    743 p9100_sync(struct p9100_softc *sc)
    744 {
    745 	while((p9100_ctl_read_4(sc, ENGINE_STATUS) &
    746 	    (ENGINE_BUSY | BLITTER_BUSY)) != 0);
    747 }
    748 
    749 static void
    750 p9100_set_color_reg(struct p9100_softc *sc, int reg, int32_t col)
    751 {
    752 	uint32_t out;
    753 
    754 	switch(sc->sc_depth)
    755 	{
    756 		case 1:	/* 8 bit */
    757 			out = (col << 8) | col;
    758 			out |= out << 16;
    759 			break;
    760 		case 2: /* 16 bit */
    761 			out = col | (col << 16);
    762 			break;
    763 		default:
    764 			out = col;
    765 	}
    766 	p9100_ctl_write_4(sc, reg, out);
    767 }
    768 
    769 /* screen-to-screen blit */
    770 static void
    771 p9100_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
    772     int he, int rop)
    773 {
    774 	struct p9100_softc *sc = cookie;
    775 	uint32_t src, dst, srcw, dstw;
    776 
    777 	sc->sc_last_offset = 0xffffffff;
    778 
    779 	src = ((xs & 0x3fff) << 16) | (ys & 0x3fff);
    780 	dst = ((xd & 0x3fff) << 16) | (yd & 0x3fff);
    781 	srcw = (((xs + wi - 1) & 0x3fff) << 16) | ((ys + he - 1) & 0x3fff);
    782 	dstw = (((xd + wi - 1) & 0x3fff) << 16) | ((yd + he - 1) & 0x3fff);
    783 
    784 	p9100_sync(sc);
    785 
    786 	p9100_ctl_write_4(sc, RASTER_OP, rop);
    787 	p9100_ctl_write_4(sc, BYTE_CLIP_MAX, 0x3fff3fff);
    788 
    789 	p9100_ctl_write_4(sc, ABS_XY0, src << sc->sc_depthshift);
    790 	p9100_ctl_write_4(sc, ABS_XY1, srcw << sc->sc_depthshift);
    791 	p9100_ctl_write_4(sc, ABS_XY2, dst << sc->sc_depthshift);
    792 	p9100_ctl_write_4(sc, ABS_XY3, dstw << sc->sc_depthshift);
    793 
    794 	sc->sc_junk = p9100_ctl_read_4(sc, COMMAND_BLIT);
    795 }
    796 
    797 /* solid rectangle fill */
    798 static void
    799 p9100_rectfill(void *cookie, int xs, int ys, int wi, int he, uint32_t col)
    800 {
    801 	struct p9100_softc *sc = cookie;
    802 	uint32_t src, srcw;
    803 
    804 	sc->sc_last_offset = 0xffffffff;
    805 
    806 	src = ((xs & 0x3fff) << 16) | (ys & 0x3fff);
    807 	srcw = (((xs + wi) & 0x3fff) << 16) | ((ys + he) & 0x3fff);
    808 	p9100_sync(sc);
    809 	p9100_ctl_write_4(sc, BYTE_CLIP_MAX, 0x3fff3fff);
    810 	p9100_set_color_reg(sc, FOREGROUND_COLOR, col);
    811 	p9100_set_color_reg(sc, BACKGROUND_COLOR, col);
    812 	p9100_ctl_write_4(sc, RASTER_OP, ROP_PAT);
    813 	p9100_ctl_write_4(sc, COORD_INDEX, 0);
    814 	p9100_ctl_write_4(sc, RECT_RTW_XY, src);
    815 	p9100_ctl_write_4(sc, RECT_RTW_XY, srcw);
    816 	sc->sc_junk = p9100_ctl_read_4(sc, COMMAND_QUAD);
    817 }
    818 
    819 /* setup for mono->colour expansion */
    820 static void
    821 p9100_setup_mono(struct p9100_softc *sc, int x, int y, int wi, int he,
    822     uint32_t fg, uint32_t bg)
    823 {
    824 
    825 	sc->sc_last_offset = 0xffffffff;
    826 
    827 	p9100_sync(sc);
    828 	/*
    829 	 * this doesn't make any sense to me either, but for some reason the
    830 	 * chip applies the foreground colour to 0 pixels
    831 	 */
    832 
    833 	p9100_set_color_reg(sc,FOREGROUND_COLOR,bg);
    834 	p9100_set_color_reg(sc,BACKGROUND_COLOR,fg);
    835 
    836 	p9100_ctl_write_4(sc, BYTE_CLIP_MAX, 0x3fff3fff);
    837 	p9100_ctl_write_4(sc, RASTER_OP, ROP_SRC);
    838 	p9100_ctl_write_4(sc, ABS_X0, x);
    839 	p9100_ctl_write_4(sc, ABS_XY1, (x << 16) | (y & 0xFFFFL));
    840 	p9100_ctl_write_4(sc, ABS_X2, (x + wi));
    841 	p9100_ctl_write_4(sc, ABS_Y3, he);
    842 	/* now feed the data into the chip */
    843 	sc->sc_mono_width = wi;
    844 }
    845 
    846 /* write monochrome data to the screen through the blitter */
    847 static void
    848 p9100_feed_line(struct p9100_softc *sc, int count, uint8_t *data)
    849 {
    850 	int i;
    851 	uint32_t latch = 0, bork;
    852 	int shift = 24;
    853 	int to_go = sc->sc_mono_width;
    854 
    855 	PNOZZ_LATCH(sc, PIXEL_1);
    856 
    857 	for (i = 0; i < count; i++) {
    858 		bork = data[i];
    859 		latch |= (bork << shift);
    860 		if (shift == 0) {
    861 			/* check how many bits are significant */
    862 			if (to_go > 31) {
    863 				bus_space_write_4(sc->sc_bustag,
    864 				    sc->sc_ctl_memh,
    865 				    (PIXEL_1 + (31 << 2)), latch);
    866 				to_go -= 32;
    867 			} else
    868 			{
    869 				bus_space_write_4(sc->sc_bustag,
    870 				    sc->sc_ctl_memh,
    871 				    (PIXEL_1 + ((to_go - 1) << 2)), latch);
    872 				to_go = 0;
    873 			}
    874 			latch = 0;
    875 			shift = 24;
    876 		} else
    877 			shift -= 8;
    878 		}
    879 	if (shift != 24)
    880 		p9100_ctl_write_4(sc, (PIXEL_1 + ((to_go - 1) << 2)), latch);
    881 }
    882 
    883 static void
    884 p9100_clearscreen(struct p9100_softc *sc)
    885 {
    886 
    887 	p9100_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
    888 }
    889 #endif /* NWSDISPLAY > 0 */
    890 
    891 static uint8_t
    892 p9100_ramdac_read(struct p9100_softc *sc, bus_size_t off)
    893 {
    894 
    895 	sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
    896 	return ((bus_space_read_4(sc->sc_bustag,
    897 	    sc->sc_ctl_memh, off) >> 16) & 0xff);
    898 }
    899 
    900 static void
    901 p9100_ramdac_write(struct p9100_softc *sc, bus_size_t off, uint8_t v)
    902 {
    903 
    904 	sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
    905 	bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off,
    906 	    ((uint32_t)v) << 16);
    907 }
    908 
    909 static uint8_t
    910 p9100_ramdac_read_ctl(struct p9100_softc *sc, int off)
    911 {
    912 	p9100_ramdac_write(sc, DAC_INDX_LO, off & 0xff);
    913 	p9100_ramdac_write(sc, DAC_INDX_HI, (off & 0xff00) >> 8);
    914 	return p9100_ramdac_read(sc, DAC_INDX_DATA);
    915 }
    916 
    917 static void
    918 p9100_ramdac_write_ctl(struct p9100_softc *sc, int off, uint8_t val)
    919 {
    920 	p9100_ramdac_write(sc, DAC_INDX_LO, off & 0xff);
    921 	p9100_ramdac_write(sc, DAC_INDX_HI, (off & 0xff00) >> 8);
    922 	p9100_ramdac_write(sc, DAC_INDX_DATA, val);
    923 }
    924 
    925 /*
    926  * Undo the effect of an FBIOSVIDEO that turns the video off.
    927  */
    928 static void
    929 p9100unblank(device_t dev)
    930 {
    931 	struct p9100_softc *sc = device_private(dev);
    932 
    933 	p9100_set_video(sc, 1);
    934 
    935 	/*
    936 	 * Check if we're in terminal mode. If not force the console screen
    937 	 * to front so we can see ddb, panic messages and so on
    938 	 */
    939 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
    940 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    941 		if (sc->vd.active != &p9100_console_screen) {
    942 			SCREEN_INVISIBLE(sc->vd.active);
    943 			sc->vd.active = &p9100_console_screen;
    944 			SCREEN_VISIBLE(&p9100_console_screen);
    945 		}
    946 		p9100_init_engine(sc);
    947 		p9100_set_depth(sc, 8);
    948 		vcons_redraw_screen(&p9100_console_screen);
    949 	}
    950 }
    951 
    952 static void
    953 p9100_set_video(struct p9100_softc *sc, int enable)
    954 {
    955 	uint32_t v = p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1);
    956 
    957 	if (enable)
    958 		v |= VIDEO_ENABLED;
    959 	else
    960 		v &= ~VIDEO_ENABLED;
    961 	p9100_ctl_write_4(sc, SCRN_RPNT_CTL_1, v);
    962 #if NTCTRL > 0
    963 	/* Turn On/Off the TFT if we know how.
    964 	 */
    965 	tadpole_set_video(enable);
    966 #endif
    967 }
    968 
    969 static int
    970 p9100_get_video(struct p9100_softc *sc)
    971 {
    972 	return (p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1) & VIDEO_ENABLED) != 0;
    973 }
    974 
    975 static bool
    976 p9100_suspend(device_t dev, const pmf_qual_t *qual)
    977 {
    978 	struct p9100_softc *sc = device_private(dev);
    979 
    980 	if (sc->sc_powerstate == PWR_SUSPEND)
    981 		return TRUE;
    982 
    983 	sc->sc_video = p9100_get_video(sc);
    984 	sc->sc_dac_power = p9100_ramdac_read_ctl(sc, DAC_POWER_MGT);
    985 	p9100_ramdac_write_ctl(sc, DAC_POWER_MGT,
    986 		DAC_POWER_SCLK_DISABLE |
    987 		DAC_POWER_DDOT_DISABLE |
    988 		DAC_POWER_SYNC_DISABLE |
    989 		DAC_POWER_ICLK_DISABLE |
    990 		DAC_POWER_IPWR_DISABLE);
    991 	p9100_set_video(sc, 0);
    992 	sc->sc_powerstate = PWR_SUSPEND;
    993 	return TRUE;
    994 }
    995 
    996 static bool
    997 p9100_resume(device_t dev, const pmf_qual_t *qual)
    998 {
    999 	struct p9100_softc *sc = device_private(dev);
   1000 
   1001 	if (sc->sc_powerstate == PWR_RESUME)
   1002 		return TRUE;
   1003 
   1004 	p9100_ramdac_write_ctl(sc, DAC_POWER_MGT, sc->sc_dac_power);
   1005 	p9100_set_video(sc, sc->sc_video);
   1006 
   1007 	sc->sc_powerstate = PWR_RESUME;
   1008 	return TRUE;
   1009 }
   1010 
   1011 /*
   1012  * Load a subset of the current (new) colormap into the IBM RAMDAC.
   1013  */
   1014 static void
   1015 p9100loadcmap(struct p9100_softc *sc, int start, int ncolors)
   1016 {
   1017 	int i;
   1018 	sc->sc_last_offset = 0xffffffff;
   1019 
   1020 	p9100_ramdac_write(sc, DAC_CMAP_WRIDX, start);
   1021 
   1022 	for (i=0;i<ncolors;i++) {
   1023 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
   1024 		    sc->sc_cmap.cm_map[i + start][0]);
   1025 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
   1026 		    sc->sc_cmap.cm_map[i + start][1]);
   1027 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
   1028 		    sc->sc_cmap.cm_map[i + start][2]);
   1029 	}
   1030 }
   1031 
   1032 /*
   1033  * Return the address that would map the given device at the given
   1034  * offset, allowing for the given protection, or return -1 for error.
   1035  */
   1036 static paddr_t
   1037 p9100mmap(dev_t dev, off_t off, int prot)
   1038 {
   1039 	struct p9100_softc *sc = device_lookup_private(&pnozz_cd, minor(dev));
   1040 
   1041 	if (off & PGOFSET)
   1042 		panic("p9100mmap");
   1043 	if (off < 0)
   1044 		return (-1);
   1045 
   1046 #ifdef PNOZZ_EMUL_CG3
   1047 #define CG3_MMAP_OFFSET	0x04000000
   1048 	/* Make Xsun think we are a CG3 (SUN3COLOR)
   1049 	 */
   1050 	if (off >= CG3_MMAP_OFFSET && off < CG3_MMAP_OFFSET + sc->sc_fb_psize) {
   1051 		off -= CG3_MMAP_OFFSET;
   1052 		return (bus_space_mmap(sc->sc_bustag,
   1053 			sc->sc_fb_paddr,
   1054 			off,
   1055 			prot,
   1056 			BUS_SPACE_MAP_LINEAR));
   1057 	}
   1058 #endif
   1059 
   1060 	if (off >= sc->sc_fb_psize + sc->sc_ctl_psize/* + sc->sc_cmd_psize*/)
   1061 		return (-1);
   1062 
   1063 	if (off < sc->sc_fb_psize) {
   1064 		return (bus_space_mmap(sc->sc_bustag,
   1065 			sc->sc_fb_paddr,
   1066 			off,
   1067 			prot,
   1068 			BUS_SPACE_MAP_LINEAR));
   1069 	}
   1070 
   1071 	off -= sc->sc_fb_psize;
   1072 	if (off < sc->sc_ctl_psize) {
   1073 		return (bus_space_mmap(sc->sc_bustag,
   1074 			sc->sc_ctl_paddr,
   1075 			off,
   1076 			prot,
   1077 			BUS_SPACE_MAP_LINEAR));
   1078 	}
   1079 
   1080 	return EINVAL;
   1081 }
   1082 
   1083 /* wscons stuff */
   1084 #if NWSDISPLAY > 0
   1085 
   1086 static void
   1087 p9100_cursor(void *cookie, int on, int row, int col)
   1088 {
   1089 	struct rasops_info *ri = cookie;
   1090 	struct vcons_screen *scr = ri->ri_hw;
   1091 	struct p9100_softc *sc = scr->scr_cookie;
   1092 	int x, y, wi,he;
   1093 
   1094 	wi = ri->ri_font->fontwidth;
   1095 	he = ri->ri_font->fontheight;
   1096 
   1097 	if (ri->ri_flg & RI_CURSOR) {
   1098 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1099 		y = ri->ri_crow * he + ri->ri_yorigin;
   1100 		p9100_bitblt(sc, x, y, x, y, wi, he, ROP_SRC ^ 0xff);
   1101 		ri->ri_flg &= ~RI_CURSOR;
   1102 	}
   1103 
   1104 	ri->ri_crow = row;
   1105 	ri->ri_ccol = col;
   1106 
   1107 	if (on)
   1108 	{
   1109 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1110 		y = ri->ri_crow * he + ri->ri_yorigin;
   1111 		p9100_bitblt(sc, x, y, x, y, wi, he, ROP_SRC ^ 0xff);
   1112 		ri->ri_flg |= RI_CURSOR;
   1113 	}
   1114 }
   1115 
   1116 #if 0
   1117 static int
   1118 p9100_mapchar(void *cookie, int uni, u_int *index)
   1119 {
   1120 	return 0;
   1121 }
   1122 #endif
   1123 
   1124 static void
   1125 p9100_putchar(void *cookie, int row, int col, u_int c, long attr)
   1126 {
   1127 	struct rasops_info *ri = cookie;
   1128 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1129 	struct vcons_screen *scr = ri->ri_hw;
   1130 	struct p9100_softc *sc = scr->scr_cookie;
   1131 
   1132 	int fg, bg, i;
   1133 	uint8_t *data;
   1134 	int x, y, wi, he;
   1135 
   1136 	wi = font->fontwidth;
   1137 	he = font->fontheight;
   1138 
   1139 	if (!CHAR_IN_FONT(c, font))
   1140 		return;
   1141 
   1142 	bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xff];
   1143 	fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xff];
   1144 	x = ri->ri_xorigin + col * wi;
   1145 	y = ri->ri_yorigin + row * he;
   1146 
   1147 	if (c == 0x20) {
   1148 		p9100_rectfill(sc, x, y, wi, he, bg);
   1149 	} else {
   1150 		data = WSFONT_GLYPH(c, font);
   1151 
   1152 		p9100_setup_mono(sc, x, y, wi, 1, fg, bg);
   1153 		for (i = 0; i < he; i++) {
   1154 			p9100_feed_line(sc, font->stride,
   1155 			    data);
   1156 			data += font->stride;
   1157 		}
   1158 	}
   1159 }
   1160 
   1161 static void
   1162 p9100_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
   1163 {
   1164 	struct rasops_info *ri = cookie;
   1165 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1166 	struct vcons_screen *scr = ri->ri_hw;
   1167 	struct p9100_softc *sc = scr->scr_cookie;
   1168 	uint32_t bg, latch = 0, bg8, fg8, pixel;
   1169 	int i, j, x, y, wi, he, r, g, b, aval, rwi;
   1170 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
   1171 	uint8_t *data8;
   1172 	int rv;
   1173 
   1174 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1175 		return;
   1176 
   1177 	if (!CHAR_IN_FONT(c, font))
   1178 		return;
   1179 
   1180 	wi = font->fontwidth;
   1181 	rwi = (wi + 3) & ~3;
   1182 	he = font->fontheight;
   1183 
   1184 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1185 	x = ri->ri_xorigin + col * wi;
   1186 	y = ri->ri_yorigin + row * he;
   1187 
   1188 	if (c == 0x20) {
   1189 		p9100_rectfill(sc, x, y, wi, he, bg);
   1190 		return;
   1191 	}
   1192 
   1193 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1194 	if (rv == GC_OK)
   1195 		return;
   1196 
   1197 	data8 = WSFONT_GLYPH(c, font);
   1198 
   1199 	p9100_sync(sc);
   1200 
   1201 	p9100_ctl_write_4(sc, RASTER_OP, ROP_SRC);
   1202 	p9100_ctl_write_4(sc, ABS_X0, x);
   1203 	p9100_ctl_write_4(sc, ABS_XY1, (x << 16) | (y & 0xFFFFL));
   1204 	p9100_ctl_write_4(sc, ABS_X2, (x + rwi));
   1205 	p9100_ctl_write_4(sc, ABS_Y3, 1);
   1206 	p9100_ctl_write_4(sc, BYTE_CLIP_MAX, ((x + wi - 1) << 16) | 0x3fff);
   1207 
   1208 	/*
   1209 	 * we need the RGB colours here, so get offsets into rasops_cmap
   1210 	 */
   1211 	fgo = ((attr >> 24) & 0xf) * 3;
   1212 	bgo = ((attr >> 16) & 0xf) * 3;
   1213 
   1214 	r0 = rasops_cmap[bgo];
   1215 	r1 = rasops_cmap[fgo];
   1216 	g0 = rasops_cmap[bgo + 1];
   1217 	g1 = rasops_cmap[fgo + 1];
   1218 	b0 = rasops_cmap[bgo + 2];
   1219 	b1 = rasops_cmap[fgo + 2];
   1220 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
   1221 	bg8 = R3G3B2(r0, g0, b0);
   1222 	fg8 = R3G3B2(r1, g1, b1);
   1223 
   1224 	//r128fb_wait(sc, 16);
   1225 
   1226 	for (i = 0; i < he; i++) {
   1227 		for (j = 0; j < wi; j++) {
   1228 			aval = *data8;
   1229 			if (aval == 0) {
   1230 				pixel = bg8;
   1231 			} else if (aval == 255) {
   1232 				pixel = fg8;
   1233 			} else {
   1234 			r = aval * r1 + (255 - aval) * r0;
   1235 				g = aval * g1 + (255 - aval) * g0;
   1236 				b = aval * b1 + (255 - aval) * b0;
   1237 				pixel = ((r & 0xe000) >> 8) |
   1238 					((g & 0xe000) >> 11) |
   1239 					((b & 0xc000) >> 14);
   1240 			}
   1241 			latch = (latch << 8) | pixel;
   1242 			/* write in 32bit chunks */
   1243 			if ((j & 3) == 3) {
   1244 				bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh,
   1245 				    COMMAND_PIXEL8, latch);
   1246 				latch = 0;
   1247 			}
   1248 			data8++;
   1249 		}
   1250 		/* if we have pixels left in latch write them out */
   1251 		if ((j & 3) != 0) {
   1252 			latch = latch << ((4 - (j & 3)) << 3);
   1253 			bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh,
   1254 			    COMMAND_PIXEL8, latch);
   1255 		}
   1256 	}
   1257 	if (rv == GC_ADD) {
   1258 		glyphcache_add(&sc->sc_gc, c, x, y);
   1259 	}
   1260 }
   1261 
   1262 /*
   1263  * wsdisplay_accessops
   1264  */
   1265 
   1266 int
   1267 p9100_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
   1268 	struct lwp *l)
   1269 {
   1270 	struct vcons_data *vd = v;
   1271 	struct p9100_softc *sc = vd->cookie;
   1272 	struct wsdisplay_fbinfo *wdf;
   1273 	struct vcons_screen *ms = vd->active;
   1274 
   1275 	switch (cmd) {
   1276 		case WSDISPLAYIO_GTYPE:
   1277 			*(u_int *)data = WSDISPLAY_TYPE_SB_P9100;
   1278 			return 0;
   1279 
   1280 		case FBIOGVIDEO:
   1281 		case WSDISPLAYIO_GVIDEO:
   1282 			*(int *)data = p9100_get_video(sc);
   1283 			return 0;
   1284 
   1285 		case WSDISPLAYIO_SVIDEO:
   1286 		case FBIOSVIDEO:
   1287 			p9100_set_video(sc, *(int *)data);
   1288 			return 0;
   1289 
   1290 		case WSDISPLAYIO_GINFO:
   1291 			wdf = (void *)data;
   1292 			wdf->height = ms->scr_ri.ri_height;
   1293 			wdf->width = ms->scr_ri.ri_width;
   1294 			wdf->depth = ms->scr_ri.ri_depth;
   1295 			wdf->cmsize = 256;
   1296 			return 0;
   1297 
   1298 		case WSDISPLAYIO_GETCMAP:
   1299 			return p9100_getcmap(sc, (struct wsdisplay_cmap *)data);
   1300 
   1301 		case WSDISPLAYIO_PUTCMAP:
   1302 			return p9100_putcmap(sc, (struct wsdisplay_cmap *)data);
   1303 
   1304 		case WSDISPLAYIO_SMODE:
   1305 			{
   1306 				int new_mode = *(int*)data;
   1307 				if (new_mode != sc->sc_mode)
   1308 				{
   1309 					sc->sc_mode = new_mode;
   1310 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
   1311 					{
   1312 						p9100_init_engine(sc);
   1313 						p9100_set_depth(sc, 8);
   1314 						p9100loadcmap(sc, 0, 256);
   1315 						p9100_clearscreen(sc);
   1316 						glyphcache_wipe(&sc->sc_gc);
   1317 						vcons_redraw_screen(ms);
   1318 					}
   1319 				}
   1320 			}
   1321 	}
   1322 	return EPASSTHROUGH;
   1323 }
   1324 
   1325 static paddr_t
   1326 p9100_mmap(void *v, void *vs, off_t offset, int prot)
   1327 {
   1328 	struct vcons_data *vd = v;
   1329 	struct p9100_softc *sc = vd->cookie;
   1330 	paddr_t pa;
   1331 
   1332 	/* 'regular' framebuffer mmap()ing */
   1333 	if (offset < sc->sc_fb_psize) {
   1334 		pa = bus_space_mmap(sc->sc_bustag, sc->sc_fb_paddr + offset, 0,
   1335 		    prot, BUS_SPACE_MAP_LINEAR);
   1336 		return pa;
   1337 	}
   1338 
   1339 	if ((offset >= sc->sc_fb_paddr) && (offset < (sc->sc_fb_paddr +
   1340 	    sc->sc_fb_psize))) {
   1341 		pa = bus_space_mmap(sc->sc_bustag, offset, 0, prot,
   1342 		    BUS_SPACE_MAP_LINEAR);
   1343 		return pa;
   1344 	}
   1345 
   1346 	if ((offset >= sc->sc_ctl_paddr) && (offset < (sc->sc_ctl_paddr +
   1347 	    sc->sc_ctl_psize))) {
   1348 		pa = bus_space_mmap(sc->sc_bustag, offset, 0, prot,
   1349 		    BUS_SPACE_MAP_LINEAR);
   1350 		return pa;
   1351 	}
   1352 
   1353 	return -1;
   1354 }
   1355 
   1356 static void
   1357 p9100_init_screen(void *cookie, struct vcons_screen *scr,
   1358     int existing, long *defattr)
   1359 {
   1360 	struct p9100_softc *sc = cookie;
   1361 	struct rasops_info *ri = &scr->scr_ri;
   1362 
   1363 	ri->ri_depth = sc->sc_depth << 3;
   1364 	ri->ri_width = sc->sc_width;
   1365 	ri->ri_height = sc->sc_height;
   1366 	ri->ri_stride = sc->sc_stride;
   1367 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
   1368 	if (ri->ri_depth == 8)
   1369 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
   1370 
   1371 #ifdef PNOZZ_USE_LATCH
   1372 	ri->ri_bits = bus_space_vaddr(sc->sc_bustag, sc->sc_fb_memh);
   1373 	DPRINTF("addr: %08lx\n",(ulong)ri->ri_bits);
   1374 #endif
   1375 
   1376 	rasops_init(ri, 0, 0);
   1377 	ri->ri_caps = WSSCREEN_WSCOLORS;
   1378 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
   1379 		    sc->sc_width / ri->ri_font->fontwidth);
   1380 
   1381 	/* enable acceleration */
   1382 	ri->ri_ops.cursor    = p9100_cursor;
   1383 	ri->ri_ops.copyrows  = p9100_copyrows;
   1384 	ri->ri_ops.eraserows = p9100_eraserows;
   1385 	ri->ri_ops.copycols  = p9100_copycols;
   1386 	ri->ri_ops.erasecols = p9100_erasecols;
   1387 	if (FONT_IS_ALPHA(ri->ri_font)) {
   1388 		ri->ri_ops.putchar = p9100_putchar_aa;
   1389 	} else
   1390 		ri->ri_ops.putchar = p9100_putchar;
   1391 }
   1392 
   1393 static int
   1394 p9100_putcmap(struct p9100_softc *sc, struct wsdisplay_cmap *cm)
   1395 {
   1396 	u_int index = cm->index;
   1397 	u_int count = cm->count;
   1398 	int i, error;
   1399 	u_char rbuf[256], gbuf[256], bbuf[256];
   1400 	u_char *r, *g, *b;
   1401 
   1402 	if (cm->index >= 256 || cm->count > 256 ||
   1403 	    (cm->index + cm->count) > 256)
   1404 		return EINVAL;
   1405 	error = copyin(cm->red, &rbuf[index], count);
   1406 	if (error)
   1407 		return error;
   1408 	error = copyin(cm->green, &gbuf[index], count);
   1409 	if (error)
   1410 		return error;
   1411 	error = copyin(cm->blue, &bbuf[index], count);
   1412 	if (error)
   1413 		return error;
   1414 
   1415 	r = &rbuf[index];
   1416 	g = &gbuf[index];
   1417 	b = &bbuf[index];
   1418 
   1419 	for (i = 0; i < count; i++) {
   1420 		sc->sc_cmap.cm_map[index][0] = *r;
   1421 		sc->sc_cmap.cm_map[index][1] = *g;
   1422 		sc->sc_cmap.cm_map[index][2] = *b;
   1423 		index++;
   1424 		r++, g++, b++;
   1425 	}
   1426 	p9100loadcmap(sc, 0, 256);
   1427 	return 0;
   1428 }
   1429 
   1430 static int
   1431 p9100_getcmap(struct p9100_softc *sc, struct wsdisplay_cmap *cm)
   1432 {
   1433 	u_int index = cm->index;
   1434 	u_int count = cm->count;
   1435 	int error, i;
   1436 	uint8_t red[256], green[256], blue[256];
   1437 
   1438 	if (index >= 255 || count > 256 || index + count > 256)
   1439 		return EINVAL;
   1440 
   1441 	i = index;
   1442 	while (i < (index + count)) {
   1443 		red[i] = sc->sc_cmap.cm_map[i][0];
   1444 		green[i] = sc->sc_cmap.cm_map[i][1];
   1445 		blue[i] = sc->sc_cmap.cm_map[i][2];
   1446 		i++;
   1447 	}
   1448 	error = copyout(&red[index],   cm->red,   count);
   1449 	if (error)
   1450 		return error;
   1451 	error = copyout(&green[index], cm->green, count);
   1452 	if (error)
   1453 		return error;
   1454 	error = copyout(&blue[index],  cm->blue,  count);
   1455 	if (error)
   1456 		return error;
   1457 
   1458 	return 0;
   1459 }
   1460 
   1461 static void
   1462 p9100_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1463 {
   1464 	struct rasops_info *ri = cookie;
   1465 	struct vcons_screen *scr = ri->ri_hw;
   1466 	int32_t xs, xd, y, width, height;
   1467 
   1468 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1469 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1470 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1471 	width = ri->ri_font->fontwidth * ncols;
   1472 	height = ri->ri_font->fontheight;
   1473 	p9100_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, ROP_SRC);
   1474 }
   1475 
   1476 static void
   1477 p9100_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1478 {
   1479 	struct rasops_info *ri = cookie;
   1480 	struct vcons_screen *scr = ri->ri_hw;
   1481 	int32_t x, y, width, height, bg;
   1482 
   1483 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1484 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1485 	width = ri->ri_font->fontwidth * ncols;
   1486 	height = ri->ri_font->fontheight;
   1487 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1488 	p9100_rectfill(scr->scr_cookie, x, y, width, height, bg);
   1489 }
   1490 
   1491 static void
   1492 p9100_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1493 {
   1494 	struct rasops_info *ri = cookie;
   1495 	struct vcons_screen *scr = ri->ri_hw;
   1496 	int32_t x, ys, yd, width, height;
   1497 
   1498 	x = ri->ri_xorigin;
   1499 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1500 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1501 	width = ri->ri_emuwidth;
   1502 	height = ri->ri_font->fontheight * nrows;
   1503 	p9100_bitblt(scr->scr_cookie, x, ys, x, yd, width, height, ROP_SRC);
   1504 }
   1505 
   1506 static void
   1507 p9100_eraserows(void *cookie, int row, int nrows, long fillattr)
   1508 {
   1509 	struct rasops_info *ri = cookie;
   1510 	struct vcons_screen *scr = ri->ri_hw;
   1511 	int32_t x, y, width, height, bg;
   1512 
   1513 	if ((row == 0) && (nrows == ri->ri_rows)) {
   1514 		x = y = 0;
   1515 		width = ri->ri_width;
   1516 		height = ri->ri_height;
   1517 	} else {
   1518 		x = ri->ri_xorigin;
   1519 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1520 		width = ri->ri_emuwidth;
   1521 		height = ri->ri_font->fontheight * nrows;
   1522 	}
   1523 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1524 	p9100_rectfill(scr->scr_cookie, x, y, width, height, bg);
   1525 }
   1526 
   1527 #if 0
   1528 static int
   1529 p9100_load_font(void *v, void *cookie, struct wsdisplay_font *data)
   1530 {
   1531 
   1532 	return 0;
   1533 }
   1534 #endif
   1535 
   1536 #endif /* NWSDISPLAY > 0 */
   1537 
   1538 #if 0
   1539 static int
   1540 p9100_intr(void *arg)
   1541 {
   1542 	/*p9100_softc *sc=arg;*/
   1543 	DPRINTF(".");
   1544 	return 1;
   1545 }
   1546 #endif
   1547 
   1548 static void
   1549 p9100_init_cursor(struct p9100_softc *sc)
   1550 {
   1551 
   1552 	memset(&sc->sc_cursor, 0, sizeof(struct pnozz_cursor));
   1553 	sc->sc_cursor.pc_size.x = 64;
   1554 	sc->sc_cursor.pc_size.y = 64;
   1555 
   1556 }
   1557 
   1558 static void
   1559 p9100_set_fbcursor(struct p9100_softc *sc)
   1560 {
   1561 #ifdef PNOZZ_PARANOID
   1562 	int s;
   1563 
   1564 	s = splhigh();	/* just in case... */
   1565 #endif
   1566 	sc->sc_last_offset = 0xffffffff;
   1567 
   1568 	/* set position and hotspot */
   1569 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
   1570 	p9100_ramdac_write(sc, DAC_INDX_HI, 0);
   1571 	p9100_ramdac_write(sc, DAC_INDX_LO, DAC_CURSOR_CTL);
   1572 	if (sc->sc_cursor.pc_enable) {
   1573 		p9100_ramdac_write(sc, DAC_INDX_DATA, DAC_CURSOR_X11 |
   1574 		    DAC_CURSOR_64);
   1575 	} else
   1576 		p9100_ramdac_write(sc, DAC_INDX_DATA, DAC_CURSOR_OFF);
   1577 	/* next two registers - x low, high, y low, high */
   1578 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_pos.x & 0xff);
   1579 	p9100_ramdac_write(sc, DAC_INDX_DATA, (sc->sc_cursor.pc_pos.x >> 8) &
   1580 	    0xff);
   1581 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_pos.y & 0xff);
   1582 	p9100_ramdac_write(sc, DAC_INDX_DATA, (sc->sc_cursor.pc_pos.y >> 8) &
   1583 	    0xff);
   1584 	/* hotspot */
   1585 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_hot.x & 0xff);
   1586 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_hot.y & 0xff);
   1587 
   1588 #ifdef PNOZZ_PARANOID
   1589 	splx(s);
   1590 #endif
   1591 
   1592 }
   1593 
   1594 static void
   1595 p9100_setcursorcmap(struct p9100_softc *sc)
   1596 {
   1597 	int i;
   1598 
   1599 #ifdef PNOZZ_PARANOID
   1600 	int s;
   1601 	s = splhigh();	/* just in case... */
   1602 #endif
   1603 	sc->sc_last_offset = 0xffffffff;
   1604 
   1605 	/* set cursor colours */
   1606 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
   1607 	p9100_ramdac_write(sc, DAC_INDX_HI, 0);
   1608 	p9100_ramdac_write(sc, DAC_INDX_LO, DAC_CURSOR_COL_1);
   1609 
   1610 	for (i = 0; i < 3; i++) {
   1611 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.red[i]);
   1612 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.green[i]);
   1613 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.blue[i]);
   1614 	}
   1615 
   1616 #ifdef PNOZZ_PARANOID
   1617 	splx(s);
   1618 #endif
   1619 }
   1620 
   1621 static void
   1622 p9100_loadcursor(struct p9100_softc *sc)
   1623 {
   1624 	uint32_t *image, *mask;
   1625 	uint32_t bit, bbit, im, ma;
   1626 	int i, j, k;
   1627 	uint8_t latch1, latch2;
   1628 
   1629 #ifdef PNOZZ_PARANOID
   1630 	int s;
   1631 	s = splhigh();	/* just in case... */
   1632 #endif
   1633 	sc->sc_last_offset = 0xffffffff;
   1634 
   1635 	/* set cursor shape */
   1636 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
   1637 	p9100_ramdac_write(sc, DAC_INDX_HI, 1);
   1638 	p9100_ramdac_write(sc, DAC_INDX_LO, 0);
   1639 
   1640 	image = sc->sc_cursor.pc_bits;
   1641 	mask = &sc->sc_cursor.pc_bits[0x80];
   1642 
   1643 	for (i = 0; i < 0x80; i++) {
   1644 		bit = 0x80000000;
   1645 		im = image[i];
   1646 		ma = mask[i];
   1647 		for (k = 0; k < 4; k++) {
   1648 			bbit = 0x1;
   1649 			latch1 = 0;
   1650 			for (j = 0; j < 4; j++) {
   1651 				if (im & bit)
   1652 					latch1 |= bbit;
   1653 				bbit <<= 1;
   1654 				if (ma & bit)
   1655 					latch1 |= bbit;
   1656 				bbit <<= 1;
   1657 				bit >>= 1;
   1658 			}
   1659 			bbit = 0x1;
   1660 			latch2 = 0;
   1661 			for (j = 0; j < 4; j++) {
   1662 				if (im & bit)
   1663 					latch2 |= bbit;
   1664 				bbit <<= 1;
   1665 				if (ma & bit)
   1666 					latch2 |= bbit;
   1667 				bbit <<= 1;
   1668 				bit >>= 1;
   1669 			}
   1670 			p9100_ramdac_write(sc, DAC_INDX_DATA, latch1);
   1671 			p9100_ramdac_write(sc, DAC_INDX_DATA, latch2);
   1672 		}
   1673 	}
   1674 #ifdef PNOZZ_DEBUG_CURSOR
   1675 	printf("image:\n");
   1676 	for (i=0;i<0x80;i+=2)
   1677 		printf("%08x %08x\n", image[i], image[i+1]);
   1678 	printf("mask:\n");
   1679 	for (i=0;i<0x80;i+=2)
   1680 		printf("%08x %08x\n", mask[i], mask[i+1]);
   1681 #endif
   1682 #ifdef PNOZZ_PARANOID
   1683 	splx(s);
   1684 #endif
   1685 }
   1686 
   1687 #if NTCTRL > 0
   1688 static void
   1689 p9100_set_extvga(void *cookie, int status)
   1690 {
   1691 	struct p9100_softc *sc = cookie;
   1692 #ifdef PNOZZ_PARANOID
   1693 	int s;
   1694 
   1695 	s = splhigh();
   1696 #endif
   1697 
   1698 #ifdef PNOZZ_DEBUG
   1699 	printf("%s: external VGA %s\n", device_xname(sc->sc_dev),
   1700 	    status ? "on" : "off");
   1701 #endif
   1702 
   1703 	sc->sc_last_offset = 0xffffffff;
   1704 
   1705 	if (status) {
   1706 		p9100_ramdac_write_ctl(sc, DAC_POWER_MGT,
   1707 		    p9100_ramdac_read_ctl(sc, DAC_POWER_MGT) &
   1708 		    ~DAC_POWER_IPWR_DISABLE);
   1709 	} else {
   1710 		p9100_ramdac_write_ctl(sc, DAC_POWER_MGT,
   1711 		    p9100_ramdac_read_ctl(sc, DAC_POWER_MGT) |
   1712 		    DAC_POWER_IPWR_DISABLE);
   1713 	}
   1714 #ifdef PNOZZ_PARANOID
   1715 	splx(s);
   1716 #endif
   1717 }
   1718 #endif /* NTCTRL > 0 */
   1719 
   1720 static int
   1721 upper_bit(uint32_t b)
   1722 {
   1723         uint32_t mask=0x80000000;
   1724         int cnt = 31;
   1725         if (b == 0)
   1726                 return -1;
   1727         while ((mask != 0) && ((b & mask) == 0)) {
   1728                 mask = mask >> 1;
   1729                 cnt--;
   1730         }
   1731         return cnt;
   1732 }
   1733 
   1734 static int
   1735 p9100_set_depth(struct p9100_softc *sc, int depth)
   1736 {
   1737 	int new_sls;
   1738 	uint32_t bits, scr, memctl, mem;
   1739 	int s0, s1, s2, s3, ps, crtcline;
   1740 	uint8_t pf, mc3, es;
   1741 
   1742 	switch (depth) {
   1743 		case 8:
   1744 			sc->sc_depthshift = 0;
   1745 			ps = 2;
   1746 			pf = 3;
   1747 			mc3 = 0;
   1748 			es = 0;	/* no swapping */
   1749 			memctl = 3;
   1750 			break;
   1751 		case 16:
   1752 			sc->sc_depthshift = 1;
   1753 			ps = 3;
   1754 			pf = 4;
   1755 			mc3 = 0;
   1756 			es = 2;	/* swap bytes in 16bit words */
   1757 			memctl = 2;
   1758 			break;
   1759 		case 24:
   1760 			/* boo */
   1761 			printf("We don't DO 24bit pixels dammit!\n");
   1762 			return 0;
   1763 		case 32:
   1764 			sc->sc_depthshift = 2;
   1765 			ps = 5;
   1766 			pf = 6;
   1767 			mc3 = 0;
   1768 			es = 6;	/* swap both half-words and bytes */
   1769 			memctl = 1;	/* 0 */
   1770 			break;
   1771 		default:
   1772 			aprint_error("%s: bogus colour depth (%d)\n",
   1773 			    __func__, depth);
   1774 			return FALSE;
   1775 	}
   1776 	/*
   1777 	 * this could be done a lot shorter and faster but then nobody would
   1778 	 * understand what the hell we're doing here without getting a major
   1779 	 * headache. Scanline size is encoded as 4 shift values, 3 of them 3 bits
   1780 	 * wide, 16 << n for n>0, one 2 bits, 512 << n for n>0. n==0 means 0
   1781 	 */
   1782 	new_sls = sc->sc_width << sc->sc_depthshift;
   1783 	sc->sc_stride = new_sls;
   1784 	bits = new_sls;
   1785 	s3 = upper_bit(bits);
   1786 	if (s3 > 9) {
   1787 		bits &= ~(1 << s3);
   1788 		s3 -= 9;
   1789 	} else
   1790 		s3 = 0;
   1791 	s2 = upper_bit(bits);
   1792 	if (s2 > 0) {
   1793 		bits &= ~(1 << s2);
   1794 		s2 -= 4;
   1795 	} else
   1796 		s2 = 0;
   1797 	s1 = upper_bit(bits);
   1798 	if (s1 > 0) {
   1799 	        bits &= ~(1 << s1);
   1800 	        s1 -= 4;
   1801 	} else
   1802 		s1 = 0;
   1803 	s0 = upper_bit(bits);
   1804 	if (s0 > 0) {
   1805 	        bits &= ~(1 << s0);
   1806 	        s0 -= 4;
   1807 	} else
   1808 		s0 = 0;
   1809 
   1810 
   1811 	DPRINTF("sls: %x sh: %d %d %d %d leftover: %x\n", new_sls, s0, s1,
   1812 	    s2, s3, bits);
   1813 
   1814 	/*
   1815 	 * now let's put these values into the System Config Register. No need to
   1816 	 * read it here since we (hopefully) just saved the content
   1817 	 */
   1818 	scr = p9100_ctl_read_4(sc, SYS_CONF);
   1819 	scr = (s0 << SHIFT_0) | (s1 << SHIFT_1) | (s2 << SHIFT_2) |
   1820 	        (s3 << SHIFT_3) | (ps << PIXEL_SHIFT) | (es << SWAP_SHIFT);
   1821 
   1822 	DPRINTF("new scr: %x DAC %x %x\n", scr, pf, mc3);
   1823 
   1824 	mem = p9100_ctl_read_4(sc, VID_MEM_CONFIG);
   1825 
   1826 	DPRINTF("old memctl: %08x\n", mem);
   1827 
   1828 	/* set shift and crtc clock */
   1829 	mem &= ~(0x0000fc00);
   1830 	mem |= (memctl << 10) | (memctl << 13);
   1831 	p9100_ctl_write_4(sc, VID_MEM_CONFIG, mem);
   1832 
   1833 	DPRINTF("new memctl: %08x\n", mem);
   1834 
   1835 	/* whack the engine... */
   1836 	p9100_ctl_write_4(sc, SYS_CONF, scr);
   1837 
   1838 	/* ok, whack the DAC */
   1839 	p9100_ramdac_write_ctl(sc, DAC_MISC_1, 0x11);
   1840 	p9100_ramdac_write_ctl(sc, DAC_MISC_2, 0x45);
   1841 	p9100_ramdac_write_ctl(sc, DAC_MISC_3, mc3);
   1842 	/*
   1843 	 * despite the 3GX manual saying otherwise we don't need to mess with
   1844 	 * any clock dividers here
   1845 	 */
   1846 	p9100_ramdac_write_ctl(sc, DAC_MISC_CLK, 1);
   1847 	p9100_ramdac_write_ctl(sc, 3, 0);
   1848 	p9100_ramdac_write_ctl(sc, 4, 0);
   1849 
   1850 	p9100_ramdac_write_ctl(sc, DAC_POWER_MGT, 0);
   1851 	p9100_ramdac_write_ctl(sc, DAC_OPERATION, 0);
   1852 	p9100_ramdac_write_ctl(sc, DAC_PALETTE_CTRL, 0);
   1853 
   1854 	p9100_ramdac_write_ctl(sc, DAC_PIXEL_FMT, pf);
   1855 
   1856 	/* TODO: distinguish between 15 and 16 bit */
   1857 	p9100_ramdac_write_ctl(sc, DAC_8BIT_CTRL, 0);
   1858 	/* direct colour, linear, 565 */
   1859 	p9100_ramdac_write_ctl(sc, DAC_16BIT_CTRL, 0xc6);
   1860 	/* direct colour */
   1861 	p9100_ramdac_write_ctl(sc, DAC_32BIT_CTRL, 3);
   1862 
   1863 	/* From the 3GX manual. Needs magic number reduction */
   1864 	p9100_ramdac_write_ctl(sc, 0x10, 2);
   1865 	p9100_ramdac_write_ctl(sc, 0x11, 0);
   1866 	p9100_ramdac_write_ctl(sc, 0x14, 5);
   1867 	p9100_ramdac_write_ctl(sc, 0x08, 1);
   1868 	p9100_ramdac_write_ctl(sc, 0x15, 5);
   1869 	p9100_ramdac_write_ctl(sc, 0x16, 0x63);
   1870 
   1871 	/* whack the CRTC */
   1872 	/* we always transfer 64bit in one go */
   1873 	crtcline = sc->sc_stride >> 3;
   1874 
   1875 	DPRINTF("crtcline: %d\n", crtcline);
   1876 
   1877 	p9100_ctl_write_4(sc, VID_HTOTAL, (24 << sc->sc_depthshift) + crtcline);
   1878 	p9100_ctl_write_4(sc, VID_HSRE, 8 << sc->sc_depthshift);
   1879 	p9100_ctl_write_4(sc, VID_HBRE, 18 << sc->sc_depthshift);
   1880 	p9100_ctl_write_4(sc, VID_HBFE, (18 << sc->sc_depthshift) + crtcline);
   1881 
   1882 #ifdef PNOZZ_DEBUG
   1883 	{
   1884 		uint32_t sscr;
   1885 		sscr = p9100_ctl_read_4(sc, SYS_CONF);
   1886 		printf("scr: %x\n", sscr);
   1887 	}
   1888 #endif
   1889 	return TRUE;
   1890 }
   1891