Home | History | Annotate | Line # | Download | only in pci
pm2fb.c revision 1.5
      1 /*	$NetBSD: pm2fb.c,v 1.5 2010/11/13 13:52:08 uebayasi Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009 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.5 2010/11/13 13:52:08 uebayasi 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 
     44 #include <dev/videomode/videomode.h>
     45 
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pcireg.h>
     48 #include <dev/pci/pcidevs.h>
     49 #include <dev/pci/pciio.h>
     50 #include <dev/pci/pm2reg.h>
     51 
     52 #include <dev/wscons/wsdisplayvar.h>
     53 #include <dev/wscons/wsconsio.h>
     54 #include <dev/wsfont/wsfont.h>
     55 #include <dev/rasops/rasops.h>
     56 #include <dev/wscons/wsdisplay_vconsvar.h>
     57 
     58 #include <dev/i2c/i2cvar.h>
     59 
     60 struct pm2fb_softc {
     61 	device_t sc_dev;
     62 
     63 	pci_chipset_tag_t sc_pc;
     64 	pcitag_t sc_pcitag;
     65 
     66 	bus_space_tag_t sc_memt;
     67 	bus_space_tag_t sc_iot;
     68 
     69 	bus_space_handle_t sc_regh;
     70 	bus_addr_t sc_fb, sc_reg;
     71 	bus_size_t sc_fbsize, sc_regsize;
     72 
     73 	int sc_width, sc_height, sc_depth, sc_stride;
     74 	int sc_locked;
     75 	struct vcons_screen sc_console_screen;
     76 	struct wsscreen_descr sc_defaultscreen_descr;
     77 	const struct wsscreen_descr *sc_screens[1];
     78 	struct wsscreen_list sc_screenlist;
     79 	struct vcons_data vd;
     80 	int sc_mode;
     81 	u_char sc_cmap_red[256];
     82 	u_char sc_cmap_green[256];
     83 	u_char sc_cmap_blue[256];
     84 	/* engine stuff */
     85 	uint32_t sc_pprod;
     86 };
     87 
     88 static int	pm2fb_match(device_t, cfdata_t, void *);
     89 static void	pm2fb_attach(device_t, device_t, void *);
     90 
     91 CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
     92     pm2fb_match, pm2fb_attach, NULL, NULL);
     93 
     94 extern const u_char rasops_cmap[768];
     95 
     96 static int	pm2fb_ioctl(void *, void *, u_long, void *, int,
     97 			     struct lwp *);
     98 static paddr_t	pm2fb_mmap(void *, void *, off_t, int);
     99 static void	pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
    100 
    101 static int	pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    102 static int 	pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    103 static void	pm2fb_restore_palette(struct pm2fb_softc *);
    104 static int 	pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
    105 			    uint8_t, uint8_t);
    106 
    107 static void	pm2fb_init(struct pm2fb_softc *);
    108 static void	pm2fb_flush_engine(struct pm2fb_softc *);
    109 static void	pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
    110 			    uint32_t);
    111 static void	pm2fb_bitblt(struct pm2fb_softc *, int, int, int, int, int,
    112 			    int, int);
    113 
    114 static void	pm2fb_cursor(void *, int, int, int);
    115 static void	pm2fb_putchar(void *, int, int, u_int, long);
    116 static void	pm2fb_copycols(void *, int, int, int, int);
    117 static void	pm2fb_erasecols(void *, int, int, int, long);
    118 static void	pm2fb_copyrows(void *, int, int, int);
    119 static void	pm2fb_eraserows(void *, int, int, long);
    120 
    121 struct wsdisplay_accessops pm2fb_accessops = {
    122 	pm2fb_ioctl,
    123 	pm2fb_mmap,
    124 	NULL,	/* alloc_screen */
    125 	NULL,	/* free_screen */
    126 	NULL,	/* show_screen */
    127 	NULL, 	/* load_font */
    128 	NULL,	/* pollc */
    129 	NULL	/* scroll */
    130 };
    131 
    132 static inline void
    133 pm2fb_wait(struct pm2fb_softc *sc, int slots)
    134 {
    135 	uint32_t reg;
    136 
    137 	do {
    138 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    139 			PM2_INPUT_FIFO_SPACE);
    140 	} while (reg <= slots);
    141 }
    142 
    143 static void
    144 pm2fb_flush_engine(struct pm2fb_softc *sc)
    145 {
    146 
    147 	pm2fb_wait(sc, 2);
    148 
    149 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
    150 	    PM2FLT_PASS_SYNC);
    151 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
    152 	do {
    153 		while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    154 			PM2_OUTPUT_FIFO_WORDS) == 0);
    155 	} while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
    156 	    0x188);
    157 }
    158 
    159 static int
    160 pm2fb_match(device_t parent, cfdata_t match, void *aux)
    161 {
    162 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    163 
    164 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    165 		return 0;
    166 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3DLABS)
    167 		return 0;
    168 
    169 	/* only cards tested on so far - likely need a list */
    170 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2) ||
    171 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2V))
    172 		return 100;
    173 	return (0);
    174 }
    175 
    176 static void
    177 pm2fb_attach(device_t parent, device_t self, void *aux)
    178 {
    179 	struct pm2fb_softc	*sc = device_private(self);
    180 	struct pci_attach_args	*pa = aux;
    181 	struct rasops_info	*ri;
    182 	char devinfo[256];
    183 	struct wsemuldisplaydev_attach_args aa;
    184 	prop_dictionary_t	dict;
    185 	unsigned long		defattr;
    186 	bool			is_console;
    187 	int i, j;
    188 	uint32_t flags;
    189 
    190 	sc->sc_pc = pa->pa_pc;
    191 	sc->sc_pcitag = pa->pa_tag;
    192 	sc->sc_memt = pa->pa_memt;
    193 	sc->sc_iot = pa->pa_iot;
    194 	sc->sc_dev = self;
    195 
    196 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    197 	aprint_normal(": %s\n", devinfo);
    198 
    199 	/* fill in parameters from properties */
    200 	dict = device_properties(self);
    201 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    202 		aprint_error("%s: no width property\n", device_xname(self));
    203 		return;
    204 	}
    205 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    206 		aprint_error("%s: no height property\n", device_xname(self));
    207 		return;
    208 	}
    209 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    210 		aprint_error("%s: no depth property\n", device_xname(self));
    211 		return;
    212 	}
    213 	/*
    214 	 * don't look at the linebytes property - The Raptor firmware lies
    215 	 * about it. Get it from width * depth >> 3 instead.
    216 	 */
    217 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    218 
    219 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    220 
    221 	pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
    222 	    &sc->sc_fb, &sc->sc_fbsize, &flags);
    223 
    224 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    225 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    226 		aprint_error("%s: failed to map registers.\n",
    227 		    device_xname(sc->sc_dev));
    228 	}
    229 
    230 	/*
    231 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    232 	 * but as far as I know there are no PM2 with 64bit BARs
    233 	 */
    234 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    235 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    236 
    237 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    238 		"default",
    239 		0, 0,
    240 		NULL,
    241 		8, 16,
    242 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    243 		NULL
    244 	};
    245 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    246 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    247 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    248 	sc->sc_locked = 0;
    249 
    250 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    251 	    &pm2fb_accessops);
    252 	sc->vd.init_screen = pm2fb_init_screen;
    253 
    254 	/* init engine here */
    255 	pm2fb_init(sc);
    256 
    257 	ri = &sc->sc_console_screen.scr_ri;
    258 
    259 	j = 0;
    260 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    261 
    262 		sc->sc_cmap_red[i] = rasops_cmap[j];
    263 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    264 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    265 		pm2fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    266 		    rasops_cmap[j + 2]);
    267 		j += 3;
    268 	}
    269 
    270 	if (is_console) {
    271 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    272 		    &defattr);
    273 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    274 
    275 		pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    276 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    277 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    278 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    279 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    280 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    281 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    282 		    defattr);
    283 		vcons_replay_msgbuf(&sc->sc_console_screen);
    284 	} else {
    285 		/*
    286 		 * since we're not the console we can postpone the rest
    287 		 * until someone actually allocates a screen for us
    288 		 */
    289 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    290 	}
    291 
    292 	aa.console = is_console;
    293 	aa.scrdata = &sc->sc_screenlist;
    294 	aa.accessops = &pm2fb_accessops;
    295 	aa.accesscookie = &sc->vd;
    296 
    297 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    298 }
    299 
    300 static int
    301 pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    302 	struct lwp *l)
    303 {
    304 	struct vcons_data *vd = v;
    305 	struct pm2fb_softc *sc = vd->cookie;
    306 	struct wsdisplay_fbinfo *wdf;
    307 	struct vcons_screen *ms = vd->active;
    308 
    309 	switch (cmd) {
    310 
    311 		case WSDISPLAYIO_GTYPE:
    312 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    313 			return 0;
    314 
    315 		/* PCI config read/write passthrough. */
    316 		case PCI_IOC_CFGREAD:
    317 		case PCI_IOC_CFGWRITE:
    318 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    319 			    cmd, data, flag, l));
    320 
    321 		case WSDISPLAYIO_GINFO:
    322 			if (ms == NULL)
    323 				return ENODEV;
    324 			wdf = (void *)data;
    325 			wdf->height = ms->scr_ri.ri_height;
    326 			wdf->width = ms->scr_ri.ri_width;
    327 			wdf->depth = ms->scr_ri.ri_depth;
    328 			wdf->cmsize = 256;
    329 			return 0;
    330 
    331 		case WSDISPLAYIO_GETCMAP:
    332 			return pm2fb_getcmap(sc,
    333 			    (struct wsdisplay_cmap *)data);
    334 
    335 		case WSDISPLAYIO_PUTCMAP:
    336 			return pm2fb_putcmap(sc,
    337 			    (struct wsdisplay_cmap *)data);
    338 
    339 		case WSDISPLAYIO_LINEBYTES:
    340 			*(u_int *)data = sc->sc_stride;
    341 			return 0;
    342 
    343 		case WSDISPLAYIO_SMODE:
    344 			{
    345 				int new_mode = *(int*)data;
    346 
    347 				if (new_mode != sc->sc_mode) {
    348 					sc->sc_mode = new_mode;
    349 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    350 						pm2fb_restore_palette(sc);
    351 						vcons_redraw_screen(ms);
    352 					} else
    353 						pm2fb_flush_engine(sc);
    354 				}
    355 			}
    356 			return 0;
    357 	}
    358 	return EPASSTHROUGH;
    359 }
    360 
    361 static paddr_t
    362 pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
    363 {
    364 	struct vcons_data *vd = v;
    365 	struct pm2fb_softc *sc = vd->cookie;
    366 	paddr_t pa;
    367 
    368 	/* 'regular' framebuffer mmap()ing */
    369 	if (offset < sc->sc_fbsize) {
    370 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    371 		    BUS_SPACE_MAP_LINEAR);
    372 		return pa;
    373 	}
    374 
    375 	/*
    376 	 * restrict all other mappings to processes with superuser privileges
    377 	 * or the kernel itself
    378 	 */
    379 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
    380 	    NULL) != 0) {
    381 		aprint_normal("%s: mmap() rejected.\n",
    382 		    device_xname(sc->sc_dev));
    383 		return -1;
    384 	}
    385 
    386 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    387 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    388 		    BUS_SPACE_MAP_LINEAR);
    389 		return pa;
    390 	}
    391 
    392 	if ((offset >= sc->sc_reg) &&
    393 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    394 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    395 		    BUS_SPACE_MAP_LINEAR);
    396 		return pa;
    397 	}
    398 
    399 #ifdef PCI_MAGIC_IO_RANGE
    400 	/* allow mapping of IO space */
    401 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    402 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    403 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    404 		    0, prot, BUS_SPACE_MAP_LINEAR);
    405 		return pa;
    406 	}
    407 #endif
    408 
    409 	return -1;
    410 }
    411 
    412 static void
    413 pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
    414     int existing, long *defattr)
    415 {
    416 	struct pm2fb_softc *sc = cookie;
    417 	struct rasops_info *ri = &scr->scr_ri;
    418 
    419 	ri->ri_depth = sc->sc_depth;
    420 	ri->ri_width = sc->sc_width;
    421 	ri->ri_height = sc->sc_height;
    422 	ri->ri_stride = sc->sc_stride;
    423 	ri->ri_flg = RI_CENTER;
    424 
    425 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    426 	ri->ri_caps = WSSCREEN_WSCOLORS;
    427 
    428 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    429 		    sc->sc_width / ri->ri_font->fontwidth);
    430 
    431 	ri->ri_hw = scr;
    432 	ri->ri_ops.copyrows = pm2fb_copyrows;
    433 	ri->ri_ops.copycols = pm2fb_copycols;
    434 	ri->ri_ops.cursor = pm2fb_cursor;
    435 	ri->ri_ops.eraserows = pm2fb_eraserows;
    436 	ri->ri_ops.erasecols = pm2fb_erasecols;
    437 	ri->ri_ops.putchar = pm2fb_putchar;
    438 }
    439 
    440 static int
    441 pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    442 {
    443 	u_char *r, *g, *b;
    444 	u_int index = cm->index;
    445 	u_int count = cm->count;
    446 	int i, error;
    447 	u_char rbuf[256], gbuf[256], bbuf[256];
    448 
    449 #ifdef PM2FB_DEBUG
    450 	aprint_debug("putcmap: %d %d\n",index, count);
    451 #endif
    452 	if (cm->index >= 256 || cm->count > 256 ||
    453 	    (cm->index + cm->count) > 256)
    454 		return EINVAL;
    455 	error = copyin(cm->red, &rbuf[index], count);
    456 	if (error)
    457 		return error;
    458 	error = copyin(cm->green, &gbuf[index], count);
    459 	if (error)
    460 		return error;
    461 	error = copyin(cm->blue, &bbuf[index], count);
    462 	if (error)
    463 		return error;
    464 
    465 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    466 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    467 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    468 
    469 	r = &sc->sc_cmap_red[index];
    470 	g = &sc->sc_cmap_green[index];
    471 	b = &sc->sc_cmap_blue[index];
    472 
    473 	for (i = 0; i < count; i++) {
    474 		pm2fb_putpalreg(sc, index, *r, *g, *b);
    475 		index++;
    476 		r++, g++, b++;
    477 	}
    478 	return 0;
    479 }
    480 
    481 static int
    482 pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    483 {
    484 	u_int index = cm->index;
    485 	u_int count = cm->count;
    486 	int error;
    487 
    488 	if (index >= 255 || count > 256 || index + count > 256)
    489 		return EINVAL;
    490 
    491 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    492 	if (error)
    493 		return error;
    494 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    495 	if (error)
    496 		return error;
    497 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    498 	if (error)
    499 		return error;
    500 
    501 	return 0;
    502 }
    503 
    504 static void
    505 pm2fb_restore_palette(struct pm2fb_softc *sc)
    506 {
    507 	int i;
    508 
    509 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    510 		pm2fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    511 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    512 	}
    513 }
    514 
    515 static int
    516 pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    517     uint8_t b)
    518 {
    519 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
    520 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
    521 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
    522 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
    523 	return 0;
    524 }
    525 
    526 static void
    527 pm2fb_init(struct pm2fb_softc *sc)
    528 {
    529 	pm2fb_flush_engine(sc);
    530 
    531 	pm2fb_wait(sc, 8);
    532 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
    533 #if 0
    534 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
    535 		0xffffffff);
    536 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
    537 		0xffffffff);
    538 #endif
    539 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
    540 		0xffffffff);
    541 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
    542 		0xffffffff);
    543 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
    544 		PM2WM_WRITE_EN);
    545 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
    546 	    (sc->sc_height << 16) | sc->sc_width);
    547 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
    548 	    PM2SC_SCREEN_EN);
    549 	pm2fb_wait(sc, 8);
    550 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
    551 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
    552 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    553 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
    554 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
    555 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
    556 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
    557 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
    558 	pm2fb_wait(sc, 8);
    559 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
    560 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
    561 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
    562 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
    563 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
    564 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
    565 	sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    566 	    PM2_FB_READMODE) &
    567 	    (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
    568 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
    569 	    sc->sc_pprod);
    570 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
    571 	    sc->sc_pprod);
    572 	pm2fb_wait(sc, 8);
    573 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
    574 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
    575 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
    576 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
    577 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
    578 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
    579 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
    580 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
    581 	    0x0fff0fff);
    582 	pm2fb_flush_engine(sc);
    583 }
    584 
    585 static void
    586 pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
    587      uint32_t colour)
    588 {
    589 
    590 	pm2fb_wait(sc, 7);
    591 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    592 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    593 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    594 	    PM2RECFG_WRITE_EN);
    595 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
    596 	    colour);
    597 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    598 	    (y << 16) | x);
    599 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    600 	    (he << 16) | wi);
    601 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    602 	    PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    603 }
    604 
    605 static void
    606 pm2fb_bitblt(struct pm2fb_softc *sc, int xs, int ys, int xd, int yd,
    607     int wi, int he, int rop)
    608 {
    609 	uint32_t dir = 0;
    610 
    611 	if (yd <= ys) {
    612 		dir |= PM2RE_INC_Y;
    613 	}
    614 	if (xd <= xs) {
    615 		dir |= PM2RE_INC_X;
    616 	}
    617 	pm2fb_wait(sc, 7);
    618 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    619 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    620 	if (rop == 3) {
    621 		bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    622 		    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN | PM2RECFG_ROP_EN |
    623 		    PM2RECFG_PACKED | (rop << 6));
    624 	} else {
    625 		bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    626 		    PM2RECFG_READ_SRC | PM2RECFG_READ_DST | PM2RECFG_WRITE_EN |
    627 		    PM2RECFG_PACKED | PM2RECFG_ROP_EN | (rop << 6));
    628 	}
    629 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    630 	    (yd << 16) | xd);
    631 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    632 	    (he << 16) | wi);
    633 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
    634 	    (((ys - yd) & 0xfff) << 16) | ((xs - xd) & 0xfff));
    635 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    636 	    PM2RE_RECTANGLE | dir);
    637 }
    638 
    639 static void
    640 pm2fb_cursor(void *cookie, int on, int row, int col)
    641 {
    642 	struct rasops_info *ri = cookie;
    643 	struct vcons_screen *scr = ri->ri_hw;
    644 	struct pm2fb_softc *sc = scr->scr_cookie;
    645 	int x, y, wi, he;
    646 
    647 	wi = ri->ri_font->fontwidth;
    648 	he = ri->ri_font->fontheight;
    649 
    650 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    651 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    652 		y = ri->ri_crow * he + ri->ri_yorigin;
    653 		if (ri->ri_flg & RI_CURSOR) {
    654 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    655 			ri->ri_flg &= ~RI_CURSOR;
    656 		}
    657 		ri->ri_crow = row;
    658 		ri->ri_ccol = col;
    659 		if (on) {
    660 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    661 			y = ri->ri_crow * he + ri->ri_yorigin;
    662 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    663 			ri->ri_flg |= RI_CURSOR;
    664 		}
    665 	} else {
    666 		scr->scr_ri.ri_crow = row;
    667 		scr->scr_ri.ri_ccol = col;
    668 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    669 	}
    670 
    671 }
    672 
    673 static void
    674 pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    675 {
    676 	struct rasops_info *ri = cookie;
    677 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    678 	struct vcons_screen *scr = ri->ri_hw;
    679 	struct pm2fb_softc *sc = scr->scr_cookie;
    680 	uint32_t mode;
    681 
    682 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    683 		void *data;
    684 		uint32_t fg, bg;
    685 		int uc, i;
    686 		int x, y, wi, he;
    687 
    688 		wi = font->fontwidth;
    689 		he = font->fontheight;
    690 
    691 		if (!CHAR_IN_FONT(c, font))
    692 			return;
    693 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    694 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    695 		x = ri->ri_xorigin + col * wi;
    696 		y = ri->ri_yorigin + row * he;
    697 		if (c == 0x20) {
    698 			pm2fb_rectfill(sc, x, y, wi, he, bg);
    699 		} else {
    700 			uc = c - font->firstchar;
    701 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
    702 
    703 			mode = PM2RM_MASK_MIRROR;
    704 			switch (ri->ri_font->stride) {
    705 				case 1:
    706 					mode |= 3 << 7;
    707 					break;
    708 				case 2:
    709 					mode |= 2 << 7;
    710 					break;
    711 			}
    712 
    713 			pm2fb_wait(sc, 8);
    714 
    715 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    716 			    PM2_RE_MODE, mode);
    717 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    718 			    PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
    719 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    720 			    PM2_RE_BLOCK_COLOUR, bg);
    721 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    722 			    PM2_RE_RECT_START, (y << 16) | x);
    723 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    724 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
    725 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    726 			    PM2_RE_RENDER,
    727 			    PM2RE_RECTANGLE |
    728 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    729 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    730 			    PM2_RE_BLOCK_COLOUR, fg);
    731 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    732 			    PM2_RE_RENDER,
    733 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
    734 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    735 
    736 			pm2fb_wait(sc, he);
    737 			switch (ri->ri_font->stride) {
    738 			case 1: {
    739 				uint8_t *data8 = data;
    740 				uint32_t reg;
    741 				for (i = 0; i < he; i++) {
    742 					reg = *data8;
    743 					bus_space_write_4(sc->sc_memt,
    744 					    sc->sc_regh,
    745 					    PM2_RE_BITMASK, reg);
    746 					data8++;
    747 				}
    748 				break;
    749 				}
    750 			case 2: {
    751 				uint16_t *data16 = data;
    752 				uint32_t reg;
    753 				for (i = 0; i < he; i++) {
    754 					reg = *data16;
    755 					bus_space_write_4(sc->sc_memt,
    756 					    sc->sc_regh,
    757 					    PM2_RE_BITMASK, reg);
    758 					data16++;
    759 				}
    760 				break;
    761 			}
    762 			}
    763 		}
    764 	}
    765 }
    766 
    767 static void
    768 pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    769 {
    770 	struct rasops_info *ri = cookie;
    771 	struct vcons_screen *scr = ri->ri_hw;
    772 	struct pm2fb_softc *sc = scr->scr_cookie;
    773 	int32_t xs, xd, y, width, height;
    774 
    775 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    776 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    777 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    778 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    779 		width = ri->ri_font->fontwidth * ncols;
    780 		height = ri->ri_font->fontheight;
    781 		pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
    782 	}
    783 }
    784 
    785 static void
    786 pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    787 {
    788 	struct rasops_info *ri = cookie;
    789 	struct vcons_screen *scr = ri->ri_hw;
    790 	struct pm2fb_softc *sc = scr->scr_cookie;
    791 	int32_t x, y, width, height, fg, bg, ul;
    792 
    793 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    794 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    795 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    796 		width = ri->ri_font->fontwidth * ncols;
    797 		height = ri->ri_font->fontheight;
    798 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    799 
    800 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    801 	}
    802 }
    803 
    804 static void
    805 pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    806 {
    807 	struct rasops_info *ri = cookie;
    808 	struct vcons_screen *scr = ri->ri_hw;
    809 	struct pm2fb_softc *sc = scr->scr_cookie;
    810 	int32_t x, ys, yd, width, height;
    811 
    812 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    813 		x = ri->ri_xorigin;
    814 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    815 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    816 		width = ri->ri_emuwidth;
    817 		height = ri->ri_font->fontheight*nrows;
    818 		pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
    819 	}
    820 }
    821 
    822 static void
    823 pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
    824 {
    825 	struct rasops_info *ri = cookie;
    826 	struct vcons_screen *scr = ri->ri_hw;
    827 	struct pm2fb_softc *sc = scr->scr_cookie;
    828 	int32_t x, y, width, height, fg, bg, ul;
    829 
    830 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    831 		x = ri->ri_xorigin;
    832 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    833 		width = ri->ri_emuwidth;
    834 		height = ri->ri_font->fontheight * nrows;
    835 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    836 
    837 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    838 	}
    839 }
    840 
    841