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