Home | History | Annotate | Line # | Download | only in pci
pm2fb.c revision 1.26
      1 /*	$NetBSD: pm2fb.c,v 1.26 2014/12/09 07:42:50 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009, 2012 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * A console driver for Permedia 2 graphics controllers
     30  * tested on sparc64 only so far
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: pm2fb.c,v 1.26 2014/12/09 07:42:50 macallan Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/lwp.h>
     42 #include <sys/kauth.h>
     43 #include <sys/atomic.h>
     44 
     45 #include <dev/videomode/videomode.h>
     46 
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcidevs.h>
     50 #include <dev/pci/pciio.h>
     51 #include <dev/pci/pm2reg.h>
     52 
     53 #include <dev/wscons/wsdisplayvar.h>
     54 #include <dev/wscons/wsconsio.h>
     55 #include <dev/wsfont/wsfont.h>
     56 #include <dev/rasops/rasops.h>
     57 #include <dev/wscons/wsdisplay_vconsvar.h>
     58 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     59 #include <dev/pci/wsdisplay_pci.h>
     60 
     61 #include <dev/i2c/i2cvar.h>
     62 #include <dev/i2c/i2c_bitbang.h>
     63 #include <dev/i2c/ddcvar.h>
     64 #include <dev/videomode/videomode.h>
     65 #include <dev/videomode/edidvar.h>
     66 #include <dev/videomode/edidreg.h>
     67 
     68 #include "opt_pm2fb.h"
     69 
     70 #ifdef PM2FB_DEBUG
     71 #define DPRINTF aprint_error
     72 #else
     73 #define DPRINTF while (0) printf
     74 #endif
     75 
     76 struct pm2fb_softc {
     77 	device_t sc_dev;
     78 
     79 	pci_chipset_tag_t sc_pc;
     80 	pcitag_t sc_pcitag;
     81 
     82 	bus_space_tag_t sc_memt;
     83 	bus_space_tag_t sc_iot;
     84 
     85 	bus_space_handle_t sc_regh;
     86 	bus_addr_t sc_fb, sc_reg;
     87 	bus_size_t sc_fbsize, sc_regsize;
     88 
     89 	int sc_width, sc_height, sc_depth, sc_stride;
     90 	int sc_locked;
     91 	struct vcons_screen sc_console_screen;
     92 	struct wsscreen_descr sc_defaultscreen_descr;
     93 	const struct wsscreen_descr *sc_screens[1];
     94 	struct wsscreen_list sc_screenlist;
     95 	struct vcons_data vd;
     96 	int sc_mode;
     97 	u_char sc_cmap_red[256];
     98 	u_char sc_cmap_green[256];
     99 	u_char sc_cmap_blue[256];
    100 	/* engine stuff */
    101 	uint32_t sc_pprod;
    102 	int sc_is_pm2;
    103 	/* i2c stuff */
    104 	struct i2c_controller sc_i2c;
    105 	uint8_t sc_edid_data[128];
    106 	struct edid_info sc_ei;
    107 	const struct videomode *sc_videomode;
    108 	glyphcache sc_gc;
    109 };
    110 
    111 static int	pm2fb_match(device_t, cfdata_t, void *);
    112 static void	pm2fb_attach(device_t, device_t, void *);
    113 
    114 CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
    115     pm2fb_match, pm2fb_attach, NULL, NULL);
    116 
    117 extern const u_char rasops_cmap[768];
    118 
    119 static int	pm2fb_ioctl(void *, void *, u_long, void *, int,
    120 			     struct lwp *);
    121 static paddr_t	pm2fb_mmap(void *, void *, off_t, int);
    122 static void	pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
    123 
    124 static int	pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    125 static int 	pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    126 static void	pm2fb_init_palette(struct pm2fb_softc *);
    127 static int 	pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
    128 			    uint8_t, uint8_t);
    129 
    130 static void	pm2fb_init(struct pm2fb_softc *);
    131 static inline void pm2fb_wait(struct pm2fb_softc *, int);
    132 static void	pm2fb_flush_engine(struct pm2fb_softc *);
    133 static void	pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
    134 			    uint32_t);
    135 static void	pm2fb_rectfill_a(void *, int, int, int, int, long);
    136 static void	pm2fb_bitblt(void *, int, int, int, int, int, int, int);
    137 
    138 static void	pm2fb_cursor(void *, int, int, int);
    139 static void	pm2fb_putchar(void *, int, int, u_int, long);
    140 static void	pm2fb_putchar_aa(void *, int, int, u_int, long);
    141 static void	pm2fb_copycols(void *, int, int, int, int);
    142 static void	pm2fb_erasecols(void *, int, int, int, long);
    143 static void	pm2fb_copyrows(void *, int, int, int);
    144 static void	pm2fb_eraserows(void *, int, int, long);
    145 
    146 struct wsdisplay_accessops pm2fb_accessops = {
    147 	pm2fb_ioctl,
    148 	pm2fb_mmap,
    149 	NULL,	/* alloc_screen */
    150 	NULL,	/* free_screen */
    151 	NULL,	/* show_screen */
    152 	NULL, 	/* load_font */
    153 	NULL,	/* pollc */
    154 	NULL	/* scroll */
    155 };
    156 
    157 /* I2C glue */
    158 static int pm2fb_i2c_acquire_bus(void *, int);
    159 static void pm2fb_i2c_release_bus(void *, int);
    160 static int pm2fb_i2c_send_start(void *, int);
    161 static int pm2fb_i2c_send_stop(void *, int);
    162 static int pm2fb_i2c_initiate_xfer(void *, i2c_addr_t, int);
    163 static int pm2fb_i2c_read_byte(void *, uint8_t *, int);
    164 static int pm2fb_i2c_write_byte(void *, uint8_t, int);
    165 
    166 /* I2C bitbang glue */
    167 static void pm2fb_i2cbb_set_bits(void *, uint32_t);
    168 static void pm2fb_i2cbb_set_dir(void *, uint32_t);
    169 static uint32_t pm2fb_i2cbb_read(void *);
    170 
    171 static void pm2_setup_i2c(struct pm2fb_softc *);
    172 
    173 static const struct i2c_bitbang_ops pm2fb_i2cbb_ops = {
    174 	pm2fb_i2cbb_set_bits,
    175 	pm2fb_i2cbb_set_dir,
    176 	pm2fb_i2cbb_read,
    177 	{
    178 		PM2_DD_SDA_IN,
    179 		PM2_DD_SCL_IN,
    180 		0,
    181 		0
    182 	}
    183 };
    184 
    185 /* mode setting stuff */
    186 static int pm2fb_set_pll(struct pm2fb_softc *, int);
    187 static uint8_t pm2fb_read_dac(struct pm2fb_softc *, int);
    188 static void pm2fb_write_dac(struct pm2fb_softc *, int, uint8_t);
    189 static void pm2fb_set_mode(struct pm2fb_softc *, const struct videomode *);
    190 
    191 /* this table is from xf86-video-glint */
    192 #define PARTPROD(a,b,c) (((a)<<6) | ((b)<<3) | (c))
    193 int partprodPermedia[] = {
    194 	-1,
    195 	PARTPROD(0,0,1), PARTPROD(0,1,1), PARTPROD(1,1,1), PARTPROD(1,1,2),
    196 	PARTPROD(1,2,2), PARTPROD(2,2,2), PARTPROD(1,2,3), PARTPROD(2,2,3),
    197 	PARTPROD(1,3,3), PARTPROD(2,3,3), PARTPROD(1,2,4), PARTPROD(3,3,3),
    198 	PARTPROD(1,3,4), PARTPROD(2,3,4),              -1, PARTPROD(3,3,4),
    199 	PARTPROD(1,4,4), PARTPROD(2,4,4),              -1, PARTPROD(3,4,4),
    200 	             -1, PARTPROD(2,3,5),              -1, PARTPROD(4,4,4),
    201 	PARTPROD(1,4,5), PARTPROD(2,4,5), PARTPROD(3,4,5),              -1,
    202 	             -1,              -1,              -1, PARTPROD(4,4,5),
    203 	PARTPROD(1,5,5), PARTPROD(2,5,5),              -1, PARTPROD(3,5,5),
    204 	             -1,              -1,              -1, PARTPROD(4,5,5),
    205 	             -1,              -1,              -1, PARTPROD(3,4,6),
    206 	             -1,              -1,              -1, PARTPROD(5,5,5),
    207 	PARTPROD(1,5,6), PARTPROD(2,5,6),              -1, PARTPROD(3,5,6),
    208 	             -1,              -1,              -1, PARTPROD(4,5,6),
    209 	             -1,              -1,              -1,              -1,
    210 	             -1,              -1,              -1, PARTPROD(5,5,6),
    211 	             -1,              -1,              -1,              -1,
    212 	             -1,              -1,              -1,              -1,
    213 	             -1,              -1,              -1,              -1,
    214 	             -1,              -1,              -1,              -1,
    215 	             -1,              -1,              -1,              -1,
    216 	             -1,              -1,              -1,              -1,
    217 	             -1,              -1,              -1,              -1,
    218 	             -1,              -1,              -1,              -1,
    219 	             -1,              -1,              -1,              -1,
    220 	             -1,              -1,              -1,              -1,
    221 	             -1,              -1,              -1,              -1,
    222 	             -1,              -1,              -1,              -1,
    223 	             -1,              -1,              -1,              -1,
    224 	             -1,              -1,              -1,              -1,
    225 	             -1,              -1,              -1,              -1,
    226 	             -1,              -1,              -1,              -1,
    227 		     0};
    228 
    229 static inline void
    230 pm2fb_wait(struct pm2fb_softc *sc, int slots)
    231 {
    232 	uint32_t reg;
    233 
    234 	do {
    235 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    236 			PM2_INPUT_FIFO_SPACE);
    237 	} while (reg <= slots);
    238 }
    239 
    240 static void
    241 pm2fb_flush_engine(struct pm2fb_softc *sc)
    242 {
    243 
    244 	pm2fb_wait(sc, 2);
    245 
    246 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
    247 	    PM2FLT_PASS_SYNC);
    248 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
    249 	do {
    250 		while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    251 			PM2_OUTPUT_FIFO_WORDS) == 0);
    252 	} while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
    253 	    0x188);
    254 }
    255 
    256 static int
    257 pm2fb_match(device_t parent, cfdata_t match, void *aux)
    258 {
    259 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    260 
    261 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    262 		return 0;
    263 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3DLABS)
    264 		return 0;
    265 
    266 	/*
    267 	 * only card tested on so far is a TechSource Raptor GFX 8P /
    268 	 * Sun PGX32, which happens to be a Permedia 2v
    269 	 */
    270 	if (/*(PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2) ||*/
    271 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2V))
    272 		return 100;
    273 	return (0);
    274 }
    275 
    276 static void
    277 pm2fb_attach(device_t parent, device_t self, void *aux)
    278 {
    279 	struct pm2fb_softc	*sc = device_private(self);
    280 	struct pci_attach_args	*pa = aux;
    281 	struct rasops_info	*ri;
    282 	struct wsemuldisplaydev_attach_args aa;
    283 	prop_dictionary_t	dict;
    284 	unsigned long		defattr;
    285 	bool			is_console;
    286 	uint32_t		flags;
    287 
    288 	sc->sc_pc = pa->pa_pc;
    289 	sc->sc_pcitag = pa->pa_tag;
    290 	sc->sc_memt = pa->pa_memt;
    291 	sc->sc_iot = pa->pa_iot;
    292 	sc->sc_dev = self;
    293 	sc->sc_is_pm2 = (PCI_PRODUCT(pa->pa_id) ==
    294 	    PCI_PRODUCT_3DLABS_PERMEDIA2);
    295 	pci_aprint_devinfo(pa, NULL);
    296 
    297 	/*
    298 	 * fill in parameters from properties
    299 	 * if we can't get a usable mode via DDC2 we'll use this to pick one,
    300 	 * which is why we fill them in with some conservative values that
    301 	 * hopefully work as a last resort
    302 	 */
    303 	dict = device_properties(self);
    304 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    305 		aprint_error("%s: no width property\n", device_xname(self));
    306 		sc->sc_width = 1024;
    307 	}
    308 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    309 		aprint_error("%s: no height property\n", device_xname(self));
    310 		sc->sc_height = 768;
    311 	}
    312 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    313 		aprint_error("%s: no depth property\n", device_xname(self));
    314 		sc->sc_depth = 8;
    315 	}
    316 
    317 	/*
    318 	 * don't look at the linebytes property - The Raptor firmware lies
    319 	 * about it. Get it from width * depth >> 3 instead.
    320 	 */
    321 
    322 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    323 
    324 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    325 
    326 	pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
    327 	    &sc->sc_fb, &sc->sc_fbsize, &flags);
    328 
    329 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    330 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    331 		aprint_error("%s: failed to map registers.\n",
    332 		    device_xname(sc->sc_dev));
    333 	}
    334 
    335 	/*
    336 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    337 	 * but as far as I know there are no PM2 with 64bit BARs
    338 	 */
    339 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    340 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    341 
    342 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    343 		"default",
    344 		0, 0,
    345 		NULL,
    346 		8, 16,
    347 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    348 		NULL
    349 	};
    350 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    351 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    352 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    353 	sc->sc_locked = 0;
    354 
    355 	pm2_setup_i2c(sc);
    356 
    357 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    358 	    &pm2fb_accessops);
    359 	sc->vd.init_screen = pm2fb_init_screen;
    360 
    361 	/* init engine here */
    362 	pm2fb_init(sc);
    363 
    364 	ri = &sc->sc_console_screen.scr_ri;
    365 
    366 	sc->sc_gc.gc_bitblt = pm2fb_bitblt;
    367 	sc->sc_gc.gc_rectfill = pm2fb_rectfill_a;
    368 	sc->sc_gc.gc_blitcookie = sc;
    369 	sc->sc_gc.gc_rop = 3;
    370 
    371 #ifdef PM2FB_DEBUG
    372 	/*
    373 	 * leave some room at the bottom of the screen for various blitter
    374 	 * tests and in order to make the glyph cache visible
    375 	 */
    376 	sc->sc_height -= 200;
    377 #endif
    378 
    379 	if (is_console) {
    380 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    381 		    &defattr);
    382 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    383 
    384 		pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    385 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    386 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    387 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    388 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    389 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    390 
    391 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    392 			min(2047, (sc->sc_fbsize / sc->sc_stride))
    393 			 - sc->sc_height - 5,
    394 			sc->sc_width,
    395 			ri->ri_font->fontwidth,
    396 			ri->ri_font->fontheight,
    397 			defattr);
    398 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    399 		    defattr);
    400 		vcons_replay_msgbuf(&sc->sc_console_screen);
    401 	} else {
    402 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    403 			/* do some minimal setup to avoid weirdnesses later */
    404 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    405 			   &defattr);
    406 		} else
    407 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    408 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    409 			   min(2047, (sc->sc_fbsize / sc->sc_stride))
    410 			    - sc->sc_height - 5,
    411 			   sc->sc_width,
    412 			   ri->ri_font->fontwidth,
    413 			   ri->ri_font->fontheight,
    414 			   defattr);
    415 	}
    416 
    417 	pm2fb_init_palette(sc);
    418 
    419 	aa.console = is_console;
    420 	aa.scrdata = &sc->sc_screenlist;
    421 	aa.accessops = &pm2fb_accessops;
    422 	aa.accesscookie = &sc->vd;
    423 
    424 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    425 
    426 #ifdef PM2FB_DEBUG
    427 	/*
    428 	 * draw a pattern to check if pm2fb_bitblt() gets the alignment stuff
    429 	 * right
    430 	 */
    431 	pm2fb_rectfill(sc, 0, sc->sc_height, sc->sc_width, 200, 0xffffffff);
    432 	pm2fb_rectfill(sc, 0, sc->sc_height, 300, 10, 0);
    433 	pm2fb_rectfill(sc, 10, sc->sc_height, 200, 10, 0xe0e0e0e0);
    434 	for (i = 1; i < 20; i++) {
    435 		pm2fb_bitblt(sc, 0, sc->sc_height,
    436 			i, sc->sc_height + 10 * i,
    437 			300, 10, 3);
    438 		pm2fb_bitblt(sc, i, sc->sc_height,
    439 			400, sc->sc_height + 10 * i,
    440 			300, 10, 3);
    441 	}
    442 #endif
    443 }
    444 
    445 static int
    446 pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    447 	struct lwp *l)
    448 {
    449 	struct vcons_data *vd = v;
    450 	struct pm2fb_softc *sc = vd->cookie;
    451 	struct wsdisplay_fbinfo *wdf;
    452 	struct vcons_screen *ms = vd->active;
    453 
    454 	switch (cmd) {
    455 	case WSDISPLAYIO_GTYPE:
    456 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    457 		return 0;
    458 
    459 	/* PCI config read/write passthrough. */
    460 	case PCI_IOC_CFGREAD:
    461 	case PCI_IOC_CFGWRITE:
    462 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    463 		    cmd, data, flag, l);
    464 
    465 	case WSDISPLAYIO_GET_BUSID:
    466 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    467 		    sc->sc_pcitag, data);
    468 
    469 	case WSDISPLAYIO_GINFO:
    470 		if (ms == NULL)
    471 			return ENODEV;
    472 		wdf = (void *)data;
    473 		wdf->height = ms->scr_ri.ri_height;
    474 		wdf->width = ms->scr_ri.ri_width;
    475 		wdf->depth = ms->scr_ri.ri_depth;
    476 		wdf->cmsize = 256;
    477 		return 0;
    478 
    479 	case WSDISPLAYIO_GETCMAP:
    480 		return pm2fb_getcmap(sc,
    481 		    (struct wsdisplay_cmap *)data);
    482 
    483 	case WSDISPLAYIO_PUTCMAP:
    484 		return pm2fb_putcmap(sc,
    485 		    (struct wsdisplay_cmap *)data);
    486 
    487 	case WSDISPLAYIO_LINEBYTES:
    488 		*(u_int *)data = sc->sc_stride;
    489 		return 0;
    490 
    491 	case WSDISPLAYIO_SMODE: {
    492 		int new_mode = *(int*)data;
    493 		if (new_mode != sc->sc_mode) {
    494 			sc->sc_mode = new_mode;
    495 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    496 				/* first set the video mode */
    497 				if (sc->sc_videomode != NULL) {
    498 					pm2fb_set_mode(sc, sc->sc_videomode);
    499 				}
    500 				/* then initialize the drawing engine */
    501 				pm2fb_init(sc);
    502 				pm2fb_init_palette(sc);
    503 				/* clean out the glyph cache */
    504 				glyphcache_wipe(&sc->sc_gc);
    505 				/* and redraw everything */
    506 				vcons_redraw_screen(ms);
    507 			} else
    508 				pm2fb_flush_engine(sc);
    509 		}
    510 		}
    511 		return 0;
    512 	case WSDISPLAYIO_GET_EDID: {
    513 		struct wsdisplayio_edid_info *d = data;
    514 		d->data_size = 128;
    515 		if (d->buffer_size < 128)
    516 			return EAGAIN;
    517 		return copyout(sc->sc_edid_data, d->edid_data, 128);
    518 	}
    519 
    520 	case WSDISPLAYIO_GET_FBINFO: {
    521 		struct wsdisplayio_fbinfo *fbi = data;
    522 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    523 	}
    524 	}
    525 	return EPASSTHROUGH;
    526 }
    527 
    528 static paddr_t
    529 pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
    530 {
    531 	struct vcons_data *vd = v;
    532 	struct pm2fb_softc *sc = vd->cookie;
    533 	paddr_t pa;
    534 
    535 	/* 'regular' framebuffer mmap()ing */
    536 	if (offset < sc->sc_fbsize) {
    537 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    538 		    BUS_SPACE_MAP_LINEAR);
    539 		return pa;
    540 	}
    541 
    542 	/*
    543 	 * restrict all other mappings to processes with superuser privileges
    544 	 * or the kernel itself
    545 	 */
    546 	if (kauth_authorize_machdep(kauth_cred_get(),
    547 	    KAUTH_MACHDEP_UNMANAGEDMEM,
    548 	    NULL, NULL, NULL, NULL) != 0) {
    549 		aprint_normal("%s: mmap() rejected.\n",
    550 		    device_xname(sc->sc_dev));
    551 		return -1;
    552 	}
    553 
    554 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    555 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    556 		    BUS_SPACE_MAP_LINEAR);
    557 		return pa;
    558 	}
    559 
    560 	if ((offset >= sc->sc_reg) &&
    561 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    562 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    563 		    BUS_SPACE_MAP_LINEAR);
    564 		return pa;
    565 	}
    566 
    567 #ifdef PCI_MAGIC_IO_RANGE
    568 	/* allow mapping of IO space */
    569 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    570 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    571 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    572 		    0, prot, BUS_SPACE_MAP_LINEAR);
    573 		return pa;
    574 	}
    575 #endif
    576 
    577 	return -1;
    578 }
    579 
    580 static void
    581 pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
    582     int existing, long *defattr)
    583 {
    584 	struct pm2fb_softc *sc = cookie;
    585 	struct rasops_info *ri = &scr->scr_ri;
    586 
    587 	ri->ri_depth = sc->sc_depth;
    588 	ri->ri_width = sc->sc_width;
    589 	ri->ri_height = sc->sc_height;
    590 	ri->ri_stride = sc->sc_stride;
    591 	ri->ri_flg = RI_CENTER;
    592 	if (sc->sc_depth == 8)
    593 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    594 
    595 	rasops_init(ri, 0, 0);
    596 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_UNDERLINE;
    597 
    598 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    599 		    sc->sc_width / ri->ri_font->fontwidth);
    600 
    601 	ri->ri_hw = scr;
    602 	ri->ri_ops.copyrows = pm2fb_copyrows;
    603 	ri->ri_ops.copycols = pm2fb_copycols;
    604 	ri->ri_ops.cursor = pm2fb_cursor;
    605 	ri->ri_ops.eraserows = pm2fb_eraserows;
    606 	ri->ri_ops.erasecols = pm2fb_erasecols;
    607 	if (FONT_IS_ALPHA(ri->ri_font)) {
    608 		ri->ri_ops.putchar = pm2fb_putchar_aa;
    609 	} else
    610 		ri->ri_ops.putchar = pm2fb_putchar;
    611 }
    612 
    613 static int
    614 pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    615 {
    616 	u_char *r, *g, *b;
    617 	u_int index = cm->index;
    618 	u_int count = cm->count;
    619 	int i, error;
    620 	u_char rbuf[256], gbuf[256], bbuf[256];
    621 
    622 #ifdef PM2FB_DEBUG
    623 	aprint_debug("putcmap: %d %d\n",index, count);
    624 #endif
    625 	if (cm->index >= 256 || cm->count > 256 ||
    626 	    (cm->index + cm->count) > 256)
    627 		return EINVAL;
    628 	error = copyin(cm->red, &rbuf[index], count);
    629 	if (error)
    630 		return error;
    631 	error = copyin(cm->green, &gbuf[index], count);
    632 	if (error)
    633 		return error;
    634 	error = copyin(cm->blue, &bbuf[index], count);
    635 	if (error)
    636 		return error;
    637 
    638 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    639 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    640 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    641 
    642 	r = &sc->sc_cmap_red[index];
    643 	g = &sc->sc_cmap_green[index];
    644 	b = &sc->sc_cmap_blue[index];
    645 
    646 	for (i = 0; i < count; i++) {
    647 		pm2fb_putpalreg(sc, index, *r, *g, *b);
    648 		index++;
    649 		r++, g++, b++;
    650 	}
    651 	return 0;
    652 }
    653 
    654 static int
    655 pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    656 {
    657 	u_int index = cm->index;
    658 	u_int count = cm->count;
    659 	int error;
    660 
    661 	if (index >= 255 || count > 256 || index + count > 256)
    662 		return EINVAL;
    663 
    664 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    665 	if (error)
    666 		return error;
    667 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    668 	if (error)
    669 		return error;
    670 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    671 	if (error)
    672 		return error;
    673 
    674 	return 0;
    675 }
    676 
    677 static void
    678 pm2fb_init_palette(struct pm2fb_softc *sc)
    679 {
    680 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri;
    681 	int i, j = 0;
    682 	uint8_t cmap[768];
    683 
    684 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    685 	for (i = 0; i < 256; i++) {
    686 		sc->sc_cmap_red[i] = cmap[j];
    687 		sc->sc_cmap_green[i] = cmap[j + 1];
    688 		sc->sc_cmap_blue[i] = cmap[j + 2];
    689 		pm2fb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    690 		j += 3;
    691 	}
    692 }
    693 
    694 static int
    695 pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    696     uint8_t b)
    697 {
    698 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
    699 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
    700 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
    701 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
    702 	return 0;
    703 }
    704 
    705 static uint8_t
    706 pm2fb_read_dac(struct pm2fb_softc *sc, int reg)
    707 {
    708 	if (sc->sc_is_pm2) {
    709 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    710 		    PM2_DAC_PAL_WRITE_IDX, reg);
    711 		return bus_space_read_1(sc->sc_memt, sc->sc_regh,
    712 		    PM2_DAC_INDEX_DATA);
    713 	} else {
    714 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    715 		    PM2V_DAC_INDEX_LOW, reg & 0xff);
    716 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    717 		    PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
    718 		return bus_space_read_1(sc->sc_memt, sc->sc_regh,
    719 		    PM2V_DAC_INDEX_DATA);
    720 	}
    721 }
    722 
    723 static void
    724 pm2fb_write_dac(struct pm2fb_softc *sc, int reg, uint8_t data)
    725 {
    726 	pm2fb_wait(sc, 3);
    727 	if (sc->sc_is_pm2) {
    728 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    729 		    PM2_DAC_PAL_WRITE_IDX, reg);
    730 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    731 		    PM2_DAC_INDEX_DATA, data);
    732 	} else {
    733 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    734 		    PM2V_DAC_INDEX_LOW, reg & 0xff);
    735 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    736 		    PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
    737 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    738 		    PM2V_DAC_INDEX_DATA, data);
    739 	}
    740 }
    741 
    742 static void
    743 pm2fb_init(struct pm2fb_softc *sc)
    744 {
    745 	pm2fb_flush_engine(sc);
    746 
    747 	pm2fb_wait(sc, 9);
    748 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
    749 	/* set aperture endianness */
    750 #if BYTE_ORDER == BIG_ENDIAN
    751 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_APERTURE1_CONTROL,
    752 		PM2_AP_BYTESWAP | PM2_AP_HALFWORDSWAP);
    753 #else
    754 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_APERTURE1_CONTROL, 0);
    755 #endif
    756 #if 0
    757 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
    758 		0xffffffff);
    759 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
    760 		0xffffffff);
    761 #endif
    762 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
    763 		0xffffffff);
    764 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
    765 		0xffffffff);
    766 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
    767 		PM2WM_WRITE_EN);
    768 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
    769 	    (sc->sc_height << 16) | sc->sc_width);
    770 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
    771 	    PM2SC_SCREEN_EN);
    772 	pm2fb_wait(sc, 8);
    773 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
    774 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
    775 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    776 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
    777 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
    778 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
    779 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
    780 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
    781 	pm2fb_wait(sc, 8);
    782 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
    783 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
    784 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
    785 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
    786 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
    787 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
    788 #if 0
    789 	sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    790 	    PM2_FB_READMODE) &
    791 	    (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
    792 #endif
    793 	sc->sc_pprod = partprodPermedia[sc->sc_stride >> 5];
    794 
    795 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
    796 	    sc->sc_pprod);
    797 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
    798 	    sc->sc_pprod);
    799 	pm2fb_wait(sc, 9);
    800 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
    801 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
    802 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
    803 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
    804 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
    805 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
    806 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
    807 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
    808 	    0x0fff0fff);
    809 	/*
    810 	 * another scissor we need to disable in order to blit into off-screen
    811 	 * memory
    812 	 */
    813 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
    814 	    0x0fff0fff);
    815 
    816 	switch(sc->sc_depth) {
    817 		case 8:
    818 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    819 			    PM2_RE_PIXEL_SIZE, PM2PS_8BIT);
    820 			break;
    821 		case 16:
    822 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    823 			    PM2_RE_PIXEL_SIZE, PM2PS_16BIT);
    824 			break;
    825 		case 32:
    826 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    827 			    PM2_RE_PIXEL_SIZE, PM2PS_32BIT);
    828 			break;
    829 	}
    830 	pm2fb_flush_engine(sc);
    831 	DPRINTF("pixel size: %08x\n",
    832 	    bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_RE_PIXEL_SIZE));
    833 }
    834 
    835 static void
    836 pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
    837      uint32_t colour)
    838 {
    839 
    840 	pm2fb_wait(sc, 7);
    841 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    842 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    843 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    844 	    PM2RECFG_WRITE_EN);
    845 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
    846 	    colour);
    847 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    848 	    (y << 16) | x);
    849 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    850 	    (he << 16) | wi);
    851 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    852 	    PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    853 }
    854 
    855 static void
    856 pm2fb_rectfill_a(void *cookie, int x, int y, int wi, int he, long attr)
    857 {
    858 	struct pm2fb_softc *sc = cookie;
    859 
    860 	pm2fb_rectfill(sc, x, y, wi, he,
    861 	    sc->vd.active->scr_ri.ri_devcmap[(attr >> 24 & 0xf)]);
    862 }
    863 
    864 static void
    865 pm2fb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    866     int wi, int he, int rop)
    867 {
    868 	struct pm2fb_softc *sc = cookie;
    869 	uint32_t dir = 0;
    870 	int rxd, rwi, rxdelta;
    871 
    872 	if (yd <= ys) {
    873 		dir |= PM2RE_INC_Y;
    874 	}
    875 	if (xd <= xs) {
    876 		dir |= PM2RE_INC_X;
    877 	}
    878 	pm2fb_wait(sc, 8);
    879 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    880 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    881 	if (sc->sc_depth == 8) {
    882 		int adjust;
    883 		/*
    884 		 * use packed mode for some extra speed
    885 		 * this copies 32bit quantities even in 8 bit mode, so we need
    886 		 * to adjust for cases where the lower two bits in source and
    887 		 * destination X don't align, and/or where the width isn't a
    888 		 * multiple of 4
    889 		 */
    890 		if (rop == 3) {
    891 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    892 			    PM2_RE_CONFIG,
    893 			    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
    894 			    PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
    895 		} else {
    896 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    897 			    PM2_RE_CONFIG,
    898 			    PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
    899 			    PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
    900 			    PM2RECFG_ROP_EN | (rop << 6));
    901 		}
    902 		rxd = xd >> 2;
    903 		rwi = (wi + 7) >> 2;
    904 		rxdelta = (xs & 0xffc) - (xd & 0xffc);
    905 		/* adjust for non-aligned x */
    906 		adjust = ((xd & 3) - (xs & 3));
    907 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
    908 		    PM2_RE_PACKEDDATA_LIMIT,
    909 		    (xd << 16) | (xd + wi) | (adjust << 29));
    910 
    911 	} else {
    912 		/* we're in 16 or 32bit mode */
    913 		if (rop == 3) {
    914 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    915 			    PM2_RE_CONFIG,
    916 			    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
    917 			    PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
    918 		} else {
    919 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    920 			    PM2_RE_CONFIG,
    921 			    PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
    922 			    PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
    923 			    PM2RECFG_ROP_EN | (rop << 6));
    924 		}
    925 		rxd = xd;
    926 		rwi = wi;
    927 		rxdelta = xs - xd;
    928 	}
    929 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    930 	    (yd << 16) | rxd);
    931 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    932 	    (he << 16) | rwi);
    933 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
    934 	    (((ys - yd) & 0xfff) << 16) | (rxdelta & 0xfff));
    935 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    936 	    PM2RE_RECTANGLE | dir);
    937 }
    938 
    939 static void
    940 pm2fb_cursor(void *cookie, int on, int row, int col)
    941 {
    942 	struct rasops_info *ri = cookie;
    943 	struct vcons_screen *scr = ri->ri_hw;
    944 	struct pm2fb_softc *sc = scr->scr_cookie;
    945 	int x, y, wi, he;
    946 
    947 	wi = ri->ri_font->fontwidth;
    948 	he = ri->ri_font->fontheight;
    949 
    950 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    951 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    952 		y = ri->ri_crow * he + ri->ri_yorigin;
    953 		if (ri->ri_flg & RI_CURSOR) {
    954 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    955 			ri->ri_flg &= ~RI_CURSOR;
    956 		}
    957 		ri->ri_crow = row;
    958 		ri->ri_ccol = col;
    959 		if (on) {
    960 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    961 			y = ri->ri_crow * he + ri->ri_yorigin;
    962 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    963 			ri->ri_flg |= RI_CURSOR;
    964 		}
    965 	} else {
    966 		scr->scr_ri.ri_crow = row;
    967 		scr->scr_ri.ri_ccol = col;
    968 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    969 	}
    970 
    971 }
    972 
    973 static void
    974 pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    975 {
    976 	struct rasops_info *ri = cookie;
    977 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    978 	struct vcons_screen *scr = ri->ri_hw;
    979 	struct pm2fb_softc *sc = scr->scr_cookie;
    980 	uint32_t mode;
    981 
    982 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    983 		void *data;
    984 		uint32_t fg, bg;
    985 		int uc, i;
    986 		int x, y, wi, he;
    987 
    988 		wi = font->fontwidth;
    989 		he = font->fontheight;
    990 
    991 		if (!CHAR_IN_FONT(c, font))
    992 			return;
    993 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    994 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    995 		x = ri->ri_xorigin + col * wi;
    996 		y = ri->ri_yorigin + row * he;
    997 		if (c == 0x20) {
    998 			pm2fb_rectfill(sc, x, y, wi, he, bg);
    999 		} else {
   1000 			uc = c - font->firstchar;
   1001 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
   1002 
   1003 			mode = PM2RM_MASK_MIRROR;
   1004 			switch (ri->ri_font->stride) {
   1005 				case 1:
   1006 					mode |= 3 << 7;
   1007 					break;
   1008 				case 2:
   1009 					mode |= 2 << 7;
   1010 					break;
   1011 			}
   1012 
   1013 			pm2fb_wait(sc, 8);
   1014 
   1015 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1016 			    PM2_RE_MODE, mode);
   1017 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1018 			    PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
   1019 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1020 			    PM2_RE_BLOCK_COLOUR, bg);
   1021 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1022 			    PM2_RE_RECT_START, (y << 16) | x);
   1023 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1024 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
   1025 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1026 			    PM2_RE_RENDER,
   1027 			    PM2RE_RECTANGLE |
   1028 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
   1029 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1030 			    PM2_RE_BLOCK_COLOUR, fg);
   1031 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1032 			    PM2_RE_RENDER,
   1033 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
   1034 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
   1035 
   1036 			pm2fb_wait(sc, he);
   1037 			switch (ri->ri_font->stride) {
   1038 			case 1: {
   1039 				uint8_t *data8 = data;
   1040 				uint32_t reg;
   1041 				for (i = 0; i < he; i++) {
   1042 					reg = *data8;
   1043 					bus_space_write_4(sc->sc_memt,
   1044 					    sc->sc_regh,
   1045 					    PM2_RE_BITMASK, reg);
   1046 					data8++;
   1047 				}
   1048 				break;
   1049 				}
   1050 			case 2: {
   1051 				uint16_t *data16 = data;
   1052 				uint32_t reg;
   1053 				for (i = 0; i < he; i++) {
   1054 					reg = *data16;
   1055 					bus_space_write_4(sc->sc_memt,
   1056 					    sc->sc_regh,
   1057 					    PM2_RE_BITMASK, reg);
   1058 					data16++;
   1059 				}
   1060 				break;
   1061 			}
   1062 			}
   1063 		}
   1064 		if (attr & 1)
   1065 			pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
   1066 	}
   1067 }
   1068 
   1069 static void
   1070 pm2fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
   1071 {
   1072 	struct rasops_info *ri = cookie;
   1073 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1074 	struct vcons_screen *scr = ri->ri_hw;
   1075 	struct pm2fb_softc *sc = scr->scr_cookie;
   1076 	uint32_t bg, fg, /*latch = 0,*/ bg8, fg8, pixel;
   1077 	int i, x, y, wi, he, r, g, b, aval;
   1078 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
   1079 	uint8_t *data8;
   1080 	int rv = GC_NOPE, cnt = 0;
   1081 
   1082 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1083 		return;
   1084 
   1085 	if (!CHAR_IN_FONT(c, font))
   1086 		return;
   1087 
   1088 	wi = font->fontwidth;
   1089 	he = font->fontheight;
   1090 
   1091 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1092 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
   1093 	x = ri->ri_xorigin + col * wi;
   1094 	y = ri->ri_yorigin + row * he;
   1095 	if (c == 0x20) {
   1096 		pm2fb_rectfill(sc, x, y, wi, he, bg);
   1097 		if (attr & 1)
   1098 			pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
   1099 		return;
   1100 	}
   1101 
   1102 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1103 	if (rv == GC_OK)
   1104 		return;
   1105 
   1106 	data8 = WSFONT_GLYPH(c, font);
   1107 
   1108 	pm2fb_wait(sc, 5);
   1109 #if 0
   1110 	/*
   1111 	 * TODO:
   1112 	 * - use packed mode here as well, instead of writing each pixel separately
   1113 	 * - see if we can trick the chip into doing the alpha blending for us
   1114 	 */
   1115 	x = x >> 2;
   1116 	wi = (wi + 3) >> 2;
   1117 #endif
   1118 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
   1119 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
   1120 			    PM2RECFG_WRITE_EN /*| PM2RECFG_PACKED*/);
   1121 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1122 			    PM2_RE_RECT_START, (y << 16) | x);
   1123 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1124 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
   1125 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1126 			    PM2_RE_RENDER,
   1127 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_HOST |
   1128 			    PM2RE_INC_X | PM2RE_INC_Y);
   1129 	/*
   1130 	 * we need the RGB colours here, so get offsets into rasops_cmap
   1131 	 */
   1132 	fgo = ((attr >> 24) & 0xf) * 3;
   1133 	bgo = ((attr >> 16) & 0xf) * 3;
   1134 
   1135 	r0 = rasops_cmap[bgo];
   1136 	r1 = rasops_cmap[fgo];
   1137 	g0 = rasops_cmap[bgo + 1];
   1138 	g1 = rasops_cmap[fgo + 1];
   1139 	b0 = rasops_cmap[bgo + 2];
   1140 	b1 = rasops_cmap[fgo + 2];
   1141 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
   1142 	bg8 = R3G3B2(r0, g0, b0);
   1143 	fg8 = R3G3B2(r1, g1, b1);
   1144 
   1145 	pm2fb_wait(sc, 200);
   1146 
   1147 	for (i = 0; i < ri->ri_fontscale; i++) {
   1148 		aval = *data8;
   1149 		if (aval == 0) {
   1150 			pixel = bg8;
   1151 		} else if (aval == 255) {
   1152 			pixel = fg8;
   1153 		} else {
   1154 			r = aval * r1 + (255 - aval) * r0;
   1155 			g = aval * g1 + (255 - aval) * g0;
   1156 			b = aval * b1 + (255 - aval) * b0;
   1157 			pixel = ((r & 0xe000) >> 8) |
   1158 				((g & 0xe000) >> 11) |
   1159 				((b & 0xc000) >> 14);
   1160 		}
   1161 #if 0
   1162 		latch = (latch << 8) | pixel;
   1163 		/* write in 32bit chunks */
   1164 		if ((i & 3) == 3) {
   1165 			bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1166 			    PM2_RE_DATA, latch);
   1167 			/*
   1168 			 * not strictly necessary, old data should be shifted
   1169 			 * out
   1170 			 */
   1171 			latch = 0;
   1172 			cnt++;
   1173 			if (cnt > 190) {
   1174 				pm2fb_wait(sc, 200);
   1175 				cnt = 0;
   1176 			}
   1177 		}
   1178 #else
   1179 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1180 			    PM2_RE_COLOUR, pixel);
   1181 
   1182 		if (cnt > 190) {
   1183 			pm2fb_wait(sc, 200);
   1184 			cnt = 0;
   1185 		}
   1186 #endif
   1187 		data8++;
   1188 	}
   1189 #if 0
   1190 	/* if we have pixels left in latch write them out */
   1191 	if ((i & 3) != 0) {
   1192 		latch = latch << ((4 - (i & 3)) << 3);
   1193 		bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1194 				    PM2_RE_DATA, latch);
   1195 	}
   1196 #endif
   1197 	/*
   1198 	 * XXX
   1199 	 * occasionally characters end up in the cache only partially drawn
   1200 	 * apparently the blitter might end up grabbing them before they're
   1201 	 * completely flushed out into video memory
   1202 	 * so we let the pipeline drain a little bit before continuing
   1203 	 */
   1204 	pm2fb_wait(sc, 20);
   1205 
   1206 	if (rv == GC_ADD) {
   1207 		glyphcache_add(&sc->sc_gc, c, x, y);
   1208 	} else if (attr & 1)
   1209 		pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
   1210 }
   1211 
   1212 static void
   1213 pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1214 {
   1215 	struct rasops_info *ri = cookie;
   1216 	struct vcons_screen *scr = ri->ri_hw;
   1217 	struct pm2fb_softc *sc = scr->scr_cookie;
   1218 	int32_t xs, xd, y, width, height;
   1219 
   1220 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1221 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1222 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1223 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1224 		width = ri->ri_font->fontwidth * ncols;
   1225 		height = ri->ri_font->fontheight;
   1226 		pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
   1227 	}
   1228 }
   1229 
   1230 static void
   1231 pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1232 {
   1233 	struct rasops_info *ri = cookie;
   1234 	struct vcons_screen *scr = ri->ri_hw;
   1235 	struct pm2fb_softc *sc = scr->scr_cookie;
   1236 	int32_t x, y, width, height, fg, bg, ul;
   1237 
   1238 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1239 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1240 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1241 		width = ri->ri_font->fontwidth * ncols;
   1242 		height = ri->ri_font->fontheight;
   1243 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1244 
   1245 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1246 	}
   1247 }
   1248 
   1249 static void
   1250 pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1251 {
   1252 	struct rasops_info *ri = cookie;
   1253 	struct vcons_screen *scr = ri->ri_hw;
   1254 	struct pm2fb_softc *sc = scr->scr_cookie;
   1255 	int32_t x, ys, yd, width, height;
   1256 
   1257 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1258 		x = ri->ri_xorigin;
   1259 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1260 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1261 		width = ri->ri_emuwidth;
   1262 		height = ri->ri_font->fontheight*nrows;
   1263 		pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
   1264 	}
   1265 }
   1266 
   1267 static void
   1268 pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1269 {
   1270 	struct rasops_info *ri = cookie;
   1271 	struct vcons_screen *scr = ri->ri_hw;
   1272 	struct pm2fb_softc *sc = scr->scr_cookie;
   1273 	int32_t x, y, width, height, fg, bg, ul;
   1274 
   1275 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1276 		x = ri->ri_xorigin;
   1277 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1278 		width = ri->ri_emuwidth;
   1279 		height = ri->ri_font->fontheight * nrows;
   1280 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1281 
   1282 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1283 	}
   1284 }
   1285 
   1286 /*
   1287  * Permedia2 can't blit outside of 2048x2048, so reject anything higher
   1288  * max. dot clock is probably too high
   1289  */
   1290 
   1291 #define MODE_IS_VALID(m) (((m)->hdisplay < 2048) && \
   1292 	((m)->dot_clock < 230000))
   1293 
   1294 static void
   1295 pm2_setup_i2c(struct pm2fb_softc *sc)
   1296 {
   1297 	int i;
   1298 #ifdef PM2FB_DEBUG
   1299 	int j;
   1300 #endif
   1301 
   1302 	/* Fill in the i2c tag */
   1303 	sc->sc_i2c.ic_cookie = sc;
   1304 	sc->sc_i2c.ic_acquire_bus = pm2fb_i2c_acquire_bus;
   1305 	sc->sc_i2c.ic_release_bus = pm2fb_i2c_release_bus;
   1306 	sc->sc_i2c.ic_send_start = pm2fb_i2c_send_start;
   1307 	sc->sc_i2c.ic_send_stop = pm2fb_i2c_send_stop;
   1308 	sc->sc_i2c.ic_initiate_xfer = pm2fb_i2c_initiate_xfer;
   1309 	sc->sc_i2c.ic_read_byte = pm2fb_i2c_read_byte;
   1310 	sc->sc_i2c.ic_write_byte = pm2fb_i2c_write_byte;
   1311 	sc->sc_i2c.ic_exec = NULL;
   1312 
   1313 	DPRINTF("data: %08x\n", bus_space_read_4(sc->sc_memt, sc->sc_regh,
   1314 		PM2_DISPLAY_DATA));
   1315 
   1316 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, 0);
   1317 
   1318 	/* zero out the EDID buffer */
   1319 	memset(sc->sc_edid_data, 0, 128);
   1320 
   1321 	/* Some monitors don't respond first time */
   1322 	i = 0;
   1323 	while (sc->sc_edid_data[1] == 0 && i < 10) {
   1324 		ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
   1325 		i++;
   1326 	}
   1327 #ifdef PM2FB_DEBUG
   1328 	printf("i = %d\n", i);
   1329 	for (i = 0; i < 128; i += 16) {
   1330 		printf("%02x:", i);
   1331 		for (j = 0; j < 16; j++)
   1332 			printf(" %02x", sc->sc_edid_data[i + j]);
   1333 		printf("\n");
   1334 	}
   1335 #endif
   1336 
   1337 	if (edid_parse(&sc->sc_edid_data[0], &sc->sc_ei) != -1) {
   1338 #ifdef PM2FB_DEBUG
   1339 		edid_print(&sc->sc_ei);
   1340 #endif
   1341 
   1342 		/*
   1343 		 * Now pick a mode.
   1344 		 */
   1345 		if ((sc->sc_ei.edid_preferred_mode != NULL)) {
   1346 			struct videomode *m = sc->sc_ei.edid_preferred_mode;
   1347 			if (MODE_IS_VALID(m)) {
   1348 				sc->sc_videomode = m;
   1349 			} else {
   1350 				aprint_error_dev(sc->sc_dev,
   1351 				    "unable to use preferred mode\n");
   1352 			}
   1353 		}
   1354 		/*
   1355 		 * if we can't use the preferred mode go look for the
   1356 		 * best one we can support
   1357 		 */
   1358 		if (sc->sc_videomode == NULL) {
   1359 			struct videomode *m = sc->sc_ei.edid_modes;
   1360 
   1361 			sort_modes(sc->sc_ei.edid_modes,
   1362 			    &sc->sc_ei.edid_preferred_mode,
   1363 			    sc->sc_ei.edid_nmodes);
   1364 			if (sc->sc_videomode == NULL)
   1365 				for (int n = 0; n < sc->sc_ei.edid_nmodes; n++)
   1366 					if (MODE_IS_VALID(&m[n])) {
   1367 						sc->sc_videomode = &m[n];
   1368 						break;
   1369 					}
   1370 		}
   1371 	}
   1372 	if (sc->sc_videomode == NULL) {
   1373 		/* no EDID data? */
   1374 		sc->sc_videomode = pick_mode_by_ref(sc->sc_width,
   1375 		    sc->sc_height, 60);
   1376 	}
   1377 	if (sc->sc_videomode != NULL) {
   1378 		pm2fb_set_mode(sc, sc->sc_videomode);
   1379 	}
   1380 }
   1381 
   1382 /* I2C bitbanging */
   1383 static void pm2fb_i2cbb_set_bits(void *cookie, uint32_t bits)
   1384 {
   1385 	struct pm2fb_softc *sc = cookie;
   1386 	uint32_t out;
   1387 
   1388 	out = bits << 2;	/* bitmasks match the IN bits */
   1389 
   1390 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, out);
   1391 	delay(100);
   1392 }
   1393 
   1394 static void pm2fb_i2cbb_set_dir(void *cookie, uint32_t dir)
   1395 {
   1396 	/* Nothing to do */
   1397 }
   1398 
   1399 static uint32_t pm2fb_i2cbb_read(void *cookie)
   1400 {
   1401 	struct pm2fb_softc *sc = cookie;
   1402 	uint32_t bits;
   1403 
   1404 	bits = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA);
   1405 	return bits;
   1406 }
   1407 
   1408 /* higher level I2C stuff */
   1409 static int
   1410 pm2fb_i2c_acquire_bus(void *cookie, int flags)
   1411 {
   1412 	/* private bus */
   1413 	return (0);
   1414 }
   1415 
   1416 static void
   1417 pm2fb_i2c_release_bus(void *cookie, int flags)
   1418 {
   1419 	/* private bus */
   1420 }
   1421 
   1422 static int
   1423 pm2fb_i2c_send_start(void *cookie, int flags)
   1424 {
   1425 	return (i2c_bitbang_send_start(cookie, flags, &pm2fb_i2cbb_ops));
   1426 }
   1427 
   1428 static int
   1429 pm2fb_i2c_send_stop(void *cookie, int flags)
   1430 {
   1431 
   1432 	return (i2c_bitbang_send_stop(cookie, flags, &pm2fb_i2cbb_ops));
   1433 }
   1434 
   1435 static int
   1436 pm2fb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
   1437 {
   1438 
   1439 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
   1440 	    &pm2fb_i2cbb_ops));
   1441 }
   1442 
   1443 static int
   1444 pm2fb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
   1445 {
   1446 	return (i2c_bitbang_read_byte(cookie, valp, flags, &pm2fb_i2cbb_ops));
   1447 }
   1448 
   1449 static int
   1450 pm2fb_i2c_write_byte(void *cookie, uint8_t val, int flags)
   1451 {
   1452 	return (i2c_bitbang_write_byte(cookie, val, flags, &pm2fb_i2cbb_ops));
   1453 }
   1454 
   1455 #define RefClk 14318	/* all frequencies are in kHz */
   1456 static int
   1457 pm2fb_set_pll(struct pm2fb_softc *sc, int freq)
   1458 {
   1459 	int m, n, p, diff, out_freq, bm = 1, bn = 3, bp = 0,
   1460 	    bdiff = 1000000 /* , bfreq */;
   1461 	int fi;
   1462 	uint8_t temp;
   1463 
   1464 	/*
   1465 	 * this should work on PM2V, PM2 needs something slightly different
   1466 	 */
   1467 	for (m = 1; m < 128; m++) {
   1468 		for (n = 2 * m + 1; n < 256; n++) {
   1469 			fi = RefClk * n / m;
   1470 			for (p = 0; p < 2; p++) {
   1471 				out_freq = fi >> (p + 1);
   1472 				diff = abs(out_freq - freq);
   1473 				if (diff < bdiff) {
   1474 					bdiff = diff;
   1475 					/* bfreq = out_freq; */
   1476 					bm = m;
   1477 					bn = n;
   1478 					bp = p;
   1479 				}
   1480 			}
   1481 		}
   1482 	}
   1483 #if 0
   1484 	/*
   1485 	 * XXX
   1486 	 * output between switching modes and attaching a wsdisplay will
   1487 	 * go through firmware calls on sparc64 and potentially mess up
   1488 	 * our drawing engine state
   1489 	 */
   1490 	DPRINTF("best: %d kHz ( %d off ), %d %d %d\n", bfreq, bdiff, bm, bn, bp);
   1491 #endif
   1492 	temp = pm2fb_read_dac(sc, PM2V_DAC_CLOCK_CONTROL) & 0xfc;
   1493 	pm2fb_write_dac(sc, PM2V_DAC_CONTROL, 0);
   1494 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_M, bm);
   1495 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_N, bn);
   1496 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_P, bp);
   1497 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_CONTROL, temp | 3);
   1498 	return 0;
   1499 }
   1500 
   1501 /*
   1502  * most of the following was adapted from the xf86-video-glint driver's
   1503  * pm2v_dac.c
   1504  */
   1505 static void
   1506 pm2fb_set_mode(struct pm2fb_softc *sc, const struct videomode *mode)
   1507 {
   1508 	int t1, t2, t3, t4, stride;
   1509 	uint32_t vclk;
   1510 	uint8_t sync = 0;
   1511 
   1512 	t1 = mode->hsync_start - mode->hdisplay;
   1513 	t2 = mode->vsync_start - mode->vdisplay;
   1514 	t3 = mode->hsync_end - mode->hsync_start;
   1515 	t4 = mode->vsync_end - mode->vsync_start;
   1516 
   1517 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HTOTAL,
   1518 	    ((mode->htotal) >> 3) - 1);
   1519 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_END,
   1520 	    (t1 + t3) >> 3);
   1521 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_START,
   1522 	    (t1 >> 3) - 1);
   1523 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HBLANK_END,
   1524 	    (mode->htotal - mode->hdisplay) >> 3);
   1525 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HGATE_END,
   1526 	    (mode->htotal - mode->hdisplay) >> 3);
   1527 
   1528 	/* first round up to the next multiple of 32 */
   1529 	stride = (mode->hdisplay + 31) & ~31;
   1530 	/* then find the next bigger one that we have partial products for */
   1531 	while ((partprodPermedia[stride >> 5] == -1) && (stride < 2048)) {
   1532 		stride += 32;
   1533 	}
   1534 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_STRIDE,
   1535 	    stride >> 3);
   1536 
   1537 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VTOTAL,
   1538 	    mode->vtotal - 1);
   1539 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_END,
   1540 	    t2 + t4);
   1541 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_START,
   1542 	    t2);
   1543 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VBLANK_END,
   1544 	    mode->vtotal - mode->vdisplay);
   1545 
   1546 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VIDEO_CONTROL,
   1547 	    PM2_VC_VIDEO_ENABLE | PM2_VC_RAMDAC_64BIT |
   1548 	    PM2_VC_HSYNC_ACT_HIGH | PM2_VC_VSYNC_ACT_HIGH);
   1549 
   1550 	vclk = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL);
   1551 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL,
   1552 	    vclk & 0xfffffffc);
   1553 
   1554 	pm2fb_set_pll(sc, mode->dot_clock / 2);
   1555 	pm2fb_write_dac(sc, PM2V_DAC_MISC_CONTROL, PM2V_DAC_8BIT);
   1556 
   1557 	if (mode->flags & VID_PHSYNC)
   1558 		sync |= PM2V_DAC_HSYNC_INV;
   1559 	if (mode->flags & VID_PVSYNC)
   1560 		sync |= PM2V_DAC_VSYNC_INV;
   1561 	pm2fb_write_dac(sc, PM2V_DAC_SYNC_CONTROL, sync);
   1562 
   1563 	pm2fb_write_dac(sc, PM2V_DAC_COLOR_FORMAT, PM2V_DAC_PALETTE);
   1564 	pm2fb_write_dac(sc, PM2V_DAC_PIXEL_SIZE, PM2V_PS_8BIT);
   1565 	sc->sc_width = mode->hdisplay;
   1566 	sc->sc_height = mode->vdisplay;
   1567 	sc->sc_depth = 8;
   1568 	sc->sc_stride = stride;
   1569 	aprint_normal_dev(sc->sc_dev, "using %d x %d in 8 bit, stride %d\n",
   1570 	    sc->sc_width, sc->sc_height, stride);
   1571 }
   1572