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