Home | History | Annotate | Line # | Download | only in dev
crmfb.c revision 1.12
      1 /* $NetBSD: crmfb.c,v 1.12 2008/02/05 21:42:16 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  *               2008 Michael Lorenz <macallan (at) netbsd.org>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *        This product includes software developed by Jared D. McNeill.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * SGI-CRM (O2) Framebuffer driver
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.12 2008/02/05 21:42:16 macallan Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/device.h>
     46 #include <sys/malloc.h>
     47 
     48 #define _SGIMIPS_BUS_DMA_PRIVATE
     49 #include <machine/autoconf.h>
     50 #include <machine/bus.h>
     51 #include <machine/machtype.h>
     52 #include <machine/vmparam.h>
     53 
     54 #include <dev/arcbios/arcbios.h>
     55 #include <dev/arcbios/arcbiosvar.h>
     56 
     57 #include <dev/wscons/wsdisplayvar.h>
     58 #include <dev/wscons/wsconsio.h>
     59 #include <dev/wsfont/wsfont.h>
     60 #include <dev/rasops/rasops.h>
     61 #include <dev/wscons/wsdisplay_vconsvar.h>
     62 
     63 #include <arch/sgimips/dev/crmfbreg.h>
     64 
     65 /* #define CRMFB_DEBUG */
     66 
     67 struct wsscreen_descr crmfb_defaultscreen = {
     68 	"default",
     69 	0, 0,
     70 	NULL,
     71 	8, 16,
     72 	WSSCREEN_WSCOLORS,
     73 	NULL,
     74 };
     75 
     76 const struct wsscreen_descr *_crmfb_scrlist[] = {
     77 	&crmfb_defaultscreen,
     78 };
     79 
     80 struct wsscreen_list crmfb_screenlist = {
     81 	sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
     82 	_crmfb_scrlist
     83 };
     84 
     85 static struct vcons_screen crmfb_console_screen;
     86 
     87 static int	crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     88 static paddr_t	crmfb_mmap(void *, void *, off_t, int);
     89 static void	crmfb_init_screen(void *, struct vcons_screen *, int, long *);
     90 
     91 struct wsdisplay_accessops crmfb_accessops = {
     92 	crmfb_ioctl,
     93 	crmfb_mmap,
     94 	NULL,	/* alloc_screen */
     95 	NULL,	/* free_screen */
     96 	NULL,	/* show_screen */
     97 	NULL,	/* load_font */
     98 	NULL,	/* pollc */
     99 	NULL,	/* scroll */
    100 };
    101 
    102 /* Memory to allocate to SGI-CRM -- remember, this is stolen from
    103  * host memory!
    104  */
    105 #define CRMFB_TILESIZE	(512*128)
    106 
    107 static int	crmfb_match(struct device *, struct cfdata *, void *);
    108 static void	crmfb_attach(struct device *, struct device *, void *);
    109 int		crmfb_probe(void);
    110 
    111 #define KERNADDR(p)	((void *)((p).addr))
    112 #define DMAADDR(p)	((p).map->dm_segs[0].ds_addr)
    113 
    114 #define CRMFB_REG_MASK(msb, lsb) \
    115 	( (((uint32_t) 1 << ((msb)-(lsb)+1)) - 1) << (lsb) )
    116 
    117 
    118 struct crmfb_dma {
    119 	bus_dmamap_t		map;
    120 	void			*addr;
    121 	bus_dma_segment_t	segs[1];
    122 	int			nsegs;
    123 	size_t			size;
    124 };
    125 
    126 struct crmfb_softc {
    127 	struct device		sc_dev;
    128 	struct vcons_data	sc_vd;
    129 
    130 	bus_space_tag_t		sc_iot;
    131 	bus_space_handle_t	sc_ioh;
    132 	bus_space_handle_t	sc_reh;
    133 
    134 	bus_dma_tag_t		sc_dmat;
    135 
    136 	struct crmfb_dma	sc_dma;
    137 	struct crmfb_dma	sc_dmai;
    138 
    139 	int			sc_width;
    140 	int			sc_height;
    141 	int			sc_depth;
    142 	int			sc_tiles_x, sc_tiles_y;
    143 	uint32_t		sc_fbsize;
    144 	uint8_t			*sc_scratch;
    145 	struct rasops_info	sc_rasops;
    146 	int 			sc_cells;
    147 	int			sc_current_cell;
    148 	int			sc_wsmode;
    149 
    150 	/* cursor stuff */
    151 	int			sc_cur_x;
    152 	int			sc_cur_y;
    153 	int			sc_hot_x;
    154 	int			sc_hot_y;
    155 
    156 	u_char			sc_cmap_red[256];
    157 	u_char			sc_cmap_green[256];
    158 	u_char			sc_cmap_blue[256];
    159 };
    160 
    161 static int	crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    162 static int	crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    163 static void	crmfb_set_palette(struct crmfb_softc *,
    164 				  int, uint8_t, uint8_t, uint8_t);
    165 static int	crmfb_set_curpos(struct crmfb_softc *, int, int);
    166 static int	crmfb_gcursor(struct crmfb_softc *, struct wsdisplay_cursor *);
    167 static int	crmfb_scursor(struct crmfb_softc *, struct wsdisplay_cursor *);
    168 static inline void	crmfb_write_reg(struct crmfb_softc *, int, uint32_t);
    169 static inline uint32_t	crmfb_read_reg(struct crmfb_softc *, int);
    170 static int	crmfb_wait_dma_idle(struct crmfb_softc *);
    171 
    172 /* setup video hw in given colour depth */
    173 static int	crmfb_setup_video(struct crmfb_softc *, int);
    174 static void	crmfb_setup_palette(struct crmfb_softc *);
    175 
    176 #ifdef CRMFB_DEBUG
    177 void crmfb_test_mte(struct crmfb_softc *);
    178 #endif
    179 
    180 static void crmfb_fill_rect(struct crmfb_softc *, int, int, int, int, uint32_t);
    181 static void crmfb_bitblt(struct crmfb_softc *, int, int, int, int, int, int,
    182 			 uint32_t);
    183 
    184 static void	crmfb_copycols(void *, int, int, int, int);
    185 static void	crmfb_erasecols(void *, int, int, int, long);
    186 static void	crmfb_copyrows(void *, int, int, int);
    187 static void	crmfb_eraserows(void *, int, int, long);
    188 //static void	crmfb_putchar(void *, int, int, u_int, long);
    189 static void	crmfb_cursor(void *, int, int, int);
    190 static void	crmfb_putchar(void *, int, int, u_int, long);
    191 
    192 CFATTACH_DECL(crmfb, sizeof(struct crmfb_softc),
    193     crmfb_match, crmfb_attach, NULL, NULL);
    194 
    195 static int
    196 crmfb_match(struct device *parent, struct cfdata *cf, void *opaque)
    197 {
    198 	return crmfb_probe();
    199 }
    200 
    201 static void
    202 crmfb_attach(struct device *parent, struct device *self, void *opaque)
    203 {
    204 	struct mainbus_attach_args *ma;
    205 	struct crmfb_softc *sc;
    206 	struct rasops_info *ri;
    207 	struct wsemuldisplaydev_attach_args aa;
    208 	uint32_t d, h;
    209 	uint16_t *p;
    210 	unsigned long v;
    211 	long defattr;
    212 	const char *consdev;
    213 	int rv, i;
    214 
    215 	sc = (struct crmfb_softc *)self;
    216 	ma = (struct mainbus_attach_args *)opaque;
    217 
    218 	sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME;
    219 	sc->sc_dmat = &sgimips_default_bus_dma_tag;
    220 	sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
    221 
    222 	aprint_normal(": SGI CRIME Graphics Display Engine\n");
    223 	rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
    224 	    BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
    225 	if (rv)
    226 		panic("crmfb_attach: can't map I/O space");
    227 	rv = bus_space_map(sc->sc_iot, 0x15000000, 0x6000, 0, &sc->sc_reh);
    228 	if (rv)
    229 		panic("crmfb_attach: can't map rendering engine");
    230 
    231 	/* determine mode configured by firmware */
    232 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
    233 	sc->sc_width = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
    234 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
    235 	sc->sc_height = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
    236 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    237 	h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
    238 	if (h == 0)
    239 		sc->sc_depth = 8;
    240 	else if (h == 1)
    241 		sc->sc_depth = 16;
    242 	else
    243 		sc->sc_depth = 32;
    244 
    245 	if (sc->sc_width == 0 || sc->sc_height == 0) {
    246 		printf("%s: device unusable if not setup by firmware\n",
    247 		    sc->sc_dev.dv_xname);
    248 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0 /* XXX */);
    249 		return;
    250 	}
    251 
    252 	printf("%s: initial resolution %dx%d\n",
    253 	    sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height);
    254 
    255 	/*
    256 	 * first determine how many tiles we need
    257 	 * in 32bit each tile is 128x128 pixels
    258 	 */
    259 	sc->sc_tiles_x = (sc->sc_width + 127) >> 7;
    260 	sc->sc_tiles_y = (sc->sc_height + 127) >> 7;
    261 	sc->sc_fbsize = 0x10000 * sc->sc_tiles_x * sc->sc_tiles_y;
    262 	printf("so we need %d x %d tiles -> %08x\n", sc->sc_tiles_x,
    263 	    sc->sc_tiles_y, sc->sc_fbsize);
    264 
    265 	sc->sc_dmai.size = 256 * sizeof(uint16_t);
    266 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
    267 	    sc->sc_dmai.segs,
    268 	    sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
    269 	    &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
    270 	if (rv)
    271 		panic("crmfb_attach: can't allocate DMA memory");
    272 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
    273 	    sc->sc_dmai.size, &sc->sc_dmai.addr,
    274 	    BUS_DMA_NOWAIT);
    275 	if (rv)
    276 		panic("crmfb_attach: can't map DMA memory");
    277 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
    278 	    sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
    279 	if (rv)
    280 		panic("crmfb_attach: can't create DMA map");
    281 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
    282 	    sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
    283 	if (rv)
    284 		panic("crmfb_attach: can't load DMA map");
    285 
    286 	/* allocate an extra tile for character drawing */
    287 	sc->sc_dma.size = sc->sc_fbsize + 0x10000;
    288 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
    289 	    sc->sc_dma.segs,
    290 	    sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
    291 	    &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
    292 	if (rv)
    293 		panic("crmfb_attach: can't allocate DMA memory");
    294 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
    295 	    sc->sc_dma.size, &sc->sc_dma.addr,
    296 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    297 	if (rv)
    298 		panic("crmfb_attach: can't map DMA memory");
    299 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
    300 	    sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
    301 	if (rv)
    302 		panic("crmfb_attach: can't create DMA map");
    303 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
    304 	    sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
    305 	if (rv)
    306 		panic("crmfb_attach: can't load DMA map");
    307 
    308 	p = KERNADDR(sc->sc_dmai);
    309 	v = (unsigned long)DMAADDR(sc->sc_dma);
    310 	for (i = 0; i < (sc->sc_tiles_x * sc->sc_tiles_y); i++) {
    311 		p[i] = ((uint32_t)v >> 16) + i;
    312 	}
    313 	sc->sc_scratch = (char *)KERNADDR(sc->sc_dma) + sc->sc_fbsize;
    314 
    315 	printf("%s: allocated %d byte fb @ %p (%p)\n", sc->sc_dev.dv_xname,
    316 	    sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
    317 
    318 	sc->sc_current_cell = 0;
    319 
    320 	ri = &crmfb_console_screen.scr_ri;
    321 	memset(ri, 0, sizeof(struct rasops_info));
    322 
    323 	vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
    324 	sc->sc_vd.init_screen = crmfb_init_screen;
    325 	crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    326 	vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
    327 
    328 	crmfb_defaultscreen.ncols = ri->ri_cols;
    329 	crmfb_defaultscreen.nrows = ri->ri_rows;
    330 	crmfb_defaultscreen.textops = &ri->ri_ops;
    331 	crmfb_defaultscreen.capabilities = ri->ri_caps;
    332 	crmfb_defaultscreen.modecookie = NULL;
    333 
    334 	crmfb_setup_video(sc, 8);
    335 	crmfb_fill_rect(sc, 0, 0, sc->sc_width, sc->sc_height, 0xf);
    336 
    337 	crmfb_setup_palette(sc);
    338 
    339 	consdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut");
    340 	if (consdev != NULL && strcmp(consdev, "video()") == 0) {
    341 		wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
    342 		aa.console = 1;
    343 	} else
    344 		aa.console = 0;
    345 	aa.scrdata = &crmfb_screenlist;
    346 	aa.accessops = &crmfb_accessops;
    347 	aa.accesscookie = &sc->sc_vd;
    348 
    349 	config_found(self, &aa, wsemuldisplaydevprint);
    350 
    351 	sc->sc_cur_x = 0;
    352 	sc->sc_cur_y = 0;
    353 	sc->sc_hot_x = 0;
    354 	sc->sc_hot_y = 0;
    355 
    356 #ifdef CRMFB_DEBUG
    357 	crmfb_test_mte(sc);
    358 #endif
    359 	return;
    360 }
    361 
    362 int
    363 crmfb_probe(void)
    364 {
    365 
    366         if (mach_type != MACH_SGI_IP32)
    367                 return 0;
    368 
    369 	return 1;
    370 }
    371 
    372 static int
    373 crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    374 {
    375 	struct vcons_data *vd;
    376 	struct crmfb_softc *sc;
    377 	struct vcons_screen *ms;
    378 	struct wsdisplay_fbinfo *wdf;
    379 	int nmode;
    380 
    381 	vd = (struct vcons_data *)v;
    382 	sc = (struct crmfb_softc *)vd->cookie;
    383 	ms = (struct vcons_screen *)vd->active;
    384 
    385 	switch (cmd) {
    386 	case WSDISPLAYIO_GTYPE:
    387 		/* not really, but who cares? */
    388 		/* wsfb does */
    389 		*(u_int *)data = WSDISPLAY_TYPE_CRIME;
    390 		return 0;
    391 	case WSDISPLAYIO_GINFO:
    392 		if (vd->active != NULL) {
    393 			wdf = (void *)data;
    394 			wdf->height = sc->sc_height;
    395 			wdf->width = sc->sc_width;
    396 			wdf->depth = 32;
    397 			wdf->cmsize = 256;
    398 			return 0;
    399 		} else
    400 			return ENODEV;
    401 	case WSDISPLAYIO_GETCMAP:
    402 		if (sc->sc_depth == 8)
    403 			return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
    404 		else
    405 			return EINVAL;
    406 	case WSDISPLAYIO_PUTCMAP:
    407 		if (sc->sc_depth == 8)
    408 			return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
    409 		else
    410 			return EINVAL;
    411 	case WSDISPLAYIO_LINEBYTES:
    412 		*(u_int *)data = sc->sc_width * sc->sc_depth / 8;
    413 		return 0;
    414 	case WSDISPLAYIO_SMODE:
    415 		nmode = *(int *)data;
    416 		if (nmode != sc->sc_wsmode) {
    417 			sc->sc_wsmode = nmode;
    418 			if (nmode == WSDISPLAYIO_MODE_EMUL) {
    419 				crmfb_setup_video(sc, 8);
    420 				crmfb_setup_palette(sc);
    421 				vcons_redraw_screen(vd->active);
    422 			} else {
    423 				crmfb_setup_video(sc, 32);
    424 			}
    425 		}
    426 		return 0;
    427 	case WSDISPLAYIO_SVIDEO:
    428 	case WSDISPLAYIO_GVIDEO:
    429 		return ENODEV;	/* not supported yet */
    430 
    431 	case WSDISPLAYIO_GCURPOS:
    432 		{
    433 			struct wsdisplay_curpos *pos;
    434 
    435 			pos = (struct wsdisplay_curpos *)data;
    436 			pos->x = sc->sc_cur_x;
    437 			pos->y = sc->sc_cur_y;
    438 		}
    439 		return 0;
    440 	case WSDISPLAYIO_SCURPOS:
    441 		{
    442 			struct wsdisplay_curpos *pos;
    443 
    444 			pos = (struct wsdisplay_curpos *)data;
    445 			crmfb_set_curpos(sc, pos->x, pos->y);
    446 		}
    447 		return 0;
    448 	case WSDISPLAYIO_GCURMAX:
    449 		{
    450 			struct wsdisplay_curpos *pos;
    451 
    452 			pos = (struct wsdisplay_curpos *)data;
    453 			pos->x = 32;
    454 			pos->y = 32;
    455 		}
    456 		return 0;
    457 	case WSDISPLAYIO_GCURSOR:
    458 		{
    459 			struct wsdisplay_cursor *cu;
    460 
    461 			cu = (struct wsdisplay_cursor *)data;
    462 			return crmfb_gcursor(sc, cu);
    463 		}
    464 	case WSDISPLAYIO_SCURSOR:
    465 		{
    466 			struct wsdisplay_cursor *cu;
    467 
    468 			cu = (struct wsdisplay_cursor *)data;
    469 			return crmfb_scursor(sc, cu);
    470 		}
    471 	}
    472 	return EPASSTHROUGH;
    473 }
    474 
    475 static paddr_t
    476 crmfb_mmap(void *v, void *vs, off_t offset, int prot)
    477 {
    478 	struct vcons_data *vd;
    479 	struct crmfb_softc *sc;
    480 	paddr_t pa;
    481 
    482 	vd = (struct vcons_data *)v;
    483 	sc = (struct crmfb_softc *)vd->cookie;
    484 
    485 #if 1
    486 	if (offset >= 0 && offset < sc->sc_fbsize) {
    487 		pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
    488 		    sc->sc_dma.nsegs, offset, prot,
    489 		    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    490 		return pa;
    491 	}
    492 #else
    493 	if (offset >= 0 && offset < sc->sc_fbsize) {
    494 		pa = bus_space_mmap(SGIMIPS_BUS_SPACE_NORMAL, sc->sc_fbh,
    495 		    offset, prot, SGIMIPS_BUS_SPACE_NORMAL);
    496 		printf("%s: %08llx -> %llx\n", __func__, offset, pa);
    497 		return pa;
    498 	}
    499 #endif
    500 	return -1;
    501 }
    502 
    503 static void
    504 crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
    505     long *defattr)
    506 {
    507 	struct crmfb_softc *sc;
    508 	struct rasops_info *ri;
    509 
    510 	sc = (struct crmfb_softc *)c;
    511 	ri = &scr->scr_ri;
    512 
    513 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    514 	ri->ri_depth = sc->sc_depth;
    515 	ri->ri_width = sc->sc_width;
    516 	ri->ri_height = sc->sc_height;
    517 	ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
    518 
    519 	switch (ri->ri_depth) {
    520 	case 16:
    521 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
    522 		ri->ri_rpos = 10;
    523 		ri->ri_gpos = 5;
    524 		ri->ri_bpos = 0;
    525 		break;
    526 	case 32:
    527 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    528 		ri->ri_rpos = 8;
    529 		ri->ri_gpos = 16;
    530 		ri->ri_bpos = 24;
    531 		break;
    532 	}
    533 
    534 	ri->ri_bits = KERNADDR(sc->sc_dma);
    535 
    536 	if (existing)
    537 		ri->ri_flg |= RI_CLEAR;
    538 
    539 	rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
    540 	ri->ri_caps = WSSCREEN_WSCOLORS;
    541 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    542 	    ri->ri_width / ri->ri_font->fontwidth);
    543 	ri->ri_hw = scr;
    544 
    545 	/* now make a fake rasops_info for drawing into the scratch tile */
    546 	memcpy(&sc->sc_rasops, ri, sizeof(struct rasops_info));
    547 	sc->sc_rasops.ri_width = 512;	/* assume we're always in 8bit here */
    548 	sc->sc_rasops.ri_stride = 512;
    549 	sc->sc_rasops.ri_height = 128;
    550 	sc->sc_rasops.ri_xorigin = 0;
    551 	sc->sc_rasops.ri_yorigin = 0;
    552 	sc->sc_rasops.ri_bits = sc->sc_scratch;
    553 	sc->sc_cells = 512 / ri->ri_font->fontwidth;
    554 
    555 	ri->ri_ops.cursor    = crmfb_cursor;
    556 	ri->ri_ops.copyrows  = crmfb_copyrows;
    557 	ri->ri_ops.eraserows = crmfb_eraserows;
    558 	ri->ri_ops.copycols  = crmfb_copycols;
    559 	ri->ri_ops.erasecols = crmfb_erasecols;
    560 	ri->ri_ops.putchar   = crmfb_putchar;
    561 
    562 	return;
    563 }
    564 
    565 static int
    566 crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    567 {
    568 	u_int idx, cnt;
    569 	u_char r[256], g[256], b[256];
    570 	u_char *rp, *gp, *bp;
    571 	int rv, i;
    572 
    573 	idx = cm->index;
    574 	cnt = cm->count;
    575 
    576 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    577 		return EINVAL;
    578 
    579 	rv = copyin(cm->red, &r[idx], cnt);
    580 	if (rv)
    581 		return rv;
    582 	rv = copyin(cm->green, &g[idx], cnt);
    583 	if (rv)
    584 		return rv;
    585 	rv = copyin(cm->blue, &b[idx], cnt);
    586 	if (rv)
    587 		return rv;
    588 
    589 	memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
    590 	memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
    591 	memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
    592 
    593 	rp = &sc->sc_cmap_red[idx];
    594 	gp = &sc->sc_cmap_green[idx];
    595 	bp = &sc->sc_cmap_blue[idx];
    596 
    597 	for (i = 0; i < cnt; i++) {
    598 		crmfb_set_palette(sc, idx, *rp, *gp, *bp);
    599 		idx++;
    600 		rp++, gp++, bp++;
    601 	}
    602 
    603 	return 0;
    604 }
    605 
    606 static int
    607 crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    608 {
    609 	u_int idx, cnt;
    610 	int rv;
    611 
    612 	idx = cm->index;
    613 	cnt = cm->count;
    614 
    615 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    616 		return EINVAL;
    617 
    618 	rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
    619 	if (rv)
    620 		return rv;
    621 	rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
    622 	if (rv)
    623 		return rv;
    624 	rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
    625 	if (rv)
    626 		return rv;
    627 
    628 	return 0;
    629 }
    630 
    631 static void
    632 crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
    633     uint8_t b)
    634 {
    635 	uint32_t val;
    636 
    637 	if (reg > 255 || sc->sc_depth != 8)
    638 		return;
    639 
    640 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
    641 		DELAY(10);
    642 
    643 	val = (r << 24) | (g << 16) | (b << 8);
    644 	crmfb_write_reg(sc, CRMFB_CMAP + (reg * 4), val);
    645 
    646 	return;
    647 }
    648 
    649 static int
    650 crmfb_set_curpos(struct crmfb_softc *sc, int x, int y)
    651 {
    652 	uint32_t val;
    653 
    654 	sc->sc_cur_x = x;
    655 	sc->sc_cur_y = y;
    656 
    657 	val = ((x - sc->sc_hot_x) & 0xffff) | ((y - sc->sc_hot_y) << 16);
    658 	crmfb_write_reg(sc, CRMFB_CURSOR_POS, val);
    659 
    660 	return 0;
    661 }
    662 
    663 static int
    664 crmfb_gcursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    665 {
    666 	/* do nothing for now */
    667 	return 0;
    668 }
    669 
    670 static int
    671 crmfb_scursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    672 {
    673 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
    674 
    675 		crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, cur->enable ? 1 : 0);
    676 	}
    677 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
    678 
    679 		sc->sc_hot_x = cur->hot.x;
    680 		sc->sc_hot_y = cur->hot.y;
    681 	}
    682 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
    683 
    684 		crmfb_set_curpos(sc, cur->pos.x, cur->pos.y);
    685 	}
    686 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
    687 		int i;
    688 		uint32_t val;
    689 
    690 		for (i = 0; i < cur->cmap.count; i++) {
    691 			val = (cur->cmap.red[i] << 24) |
    692 			      (cur->cmap.green[i] << 16) |
    693 			      (cur->cmap.blue[i] << 8);
    694 			crmfb_write_reg(sc, CRMFB_CURSOR_CMAP0 +
    695 			    ((i + cur->cmap.index) << 2), val);
    696 		}
    697 	}
    698 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
    699 
    700 		int i, j, cnt = 0;
    701 		uint32_t latch = 0, omask;
    702 		uint8_t imask;
    703 		for (i = 0; i < 64; i++) {
    704 			omask = 0x80000000;
    705 			imask = 0x01;
    706 			cur->image[cnt] &= cur->mask[cnt];
    707 			for (j = 0; j < 8; j++) {
    708 				if (cur->image[cnt] & imask)
    709 					latch |= omask;
    710 				omask >>= 1;
    711 				if (cur->mask[cnt] & imask)
    712 					latch |= omask;
    713 				omask >>= 1;
    714 				imask <<= 1;
    715 			}
    716 			cnt++;
    717 			imask = 0x01;
    718 			cur->image[cnt] &= cur->mask[cnt];
    719 			for (j = 0; j < 8; j++) {
    720 				if (cur->image[cnt] & imask)
    721 					latch |= omask;
    722 				omask >>= 1;
    723 				if (cur->mask[cnt] & imask)
    724 					latch |= omask;
    725 				omask >>= 1;
    726 				imask <<= 1;
    727 			}
    728 			cnt++;
    729 			crmfb_write_reg(sc, CRMFB_CURSOR_BITMAP + (i << 2),
    730 			    latch);
    731 			latch = 0;
    732 		}
    733 	}
    734 	return 0;
    735 }
    736 
    737 static inline void
    738 crmfb_write_reg(struct crmfb_softc *sc, int offset, uint32_t val)
    739 {
    740 
    741 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
    742 	wbflush();
    743 }
    744 
    745 static inline uint32_t
    746 crmfb_read_reg(struct crmfb_softc *sc, int offset)
    747 {
    748 
    749 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
    750 }
    751 
    752 static int
    753 crmfb_wait_dma_idle(struct crmfb_softc *sc)
    754 {
    755 	int bail = 100000, idle;
    756 
    757 	do {
    758 		idle = ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    759 		         CRMFB_OVR_CONTROL) & 1) == 0) &&
    760 		       ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    761 		         CRMFB_FRM_CONTROL) & 1) == 0) &&
    762 		       ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    763 		         CRMFB_DID_CONTROL) & 1) == 0);
    764 		if (!idle)
    765 			delay(10);
    766 		bail--;
    767 	} while ((!idle) && (bail > 0));
    768 	return idle;
    769 }
    770 
    771 static int
    772 crmfb_setup_video(struct crmfb_softc *sc, int depth)
    773 {
    774 	uint64_t reg;
    775 	uint32_t d, h, mode;
    776 	int i, bail, tile_width, tlbptr, j, tx, shift;
    777 	const char *wantsync;
    778 	uint16_t v;
    779 
    780 	/* disable DMA */
    781 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
    782 	d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
    783 	crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d);
    784 	DELAY(50000);
    785 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    786 	d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    787 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    788 	DELAY(50000);
    789 	crmfb_write_reg(sc, CRMFB_DID_CONTROL, 0);
    790 	DELAY(50000);
    791 
    792 	if (!crmfb_wait_dma_idle(sc))
    793 		aprint_error("crmfb: crmfb_wait_dma_idle timed out\n");
    794 
    795 	/* ensure that CRM starts drawing at the top left of the screen
    796 	 * when we re-enable DMA later
    797 	 */
    798 	d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
    799 	crmfb_write_reg(sc, CRMFB_VT_XY, d);
    800 	delay(1000);
    801 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    802 	d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    803 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
    804 
    805 	/* wait for dotclock to turn off */
    806 	bail = 10000;
    807 	while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK) &
    808 	    (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT)) && (bail > 0)) {
    809 		delay(10);
    810 		bail--;
    811 	}
    812 
    813 	/* reset FIFO */
    814 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    815 	d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    816 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    817 	d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    818 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    819 
    820 	/* setup colour mode */
    821 	switch (depth) {
    822 	case 8:
    823 		h = CRMFB_MODE_TYP_I8;
    824 		tile_width = 512;
    825 		break;
    826 	case 16:
    827 		h = CRMFB_MODE_TYP_ARGB5;
    828 		tile_width = 256;
    829 		break;
    830 	case 32:
    831 		h = CRMFB_MODE_TYP_RGB8;
    832 		tile_width = 128;
    833 		break;
    834 	default:
    835 		panic("Unsupported depth");
    836 	}
    837 	d = h << CRMFB_MODE_TYP_SHIFT;
    838 	d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
    839 	for (i = 0; i < (32 * 4); i += 4)
    840 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
    841 	wbflush();
    842 
    843 	/* setup tile pointer, but don't turn on DMA yet! */
    844 	h = DMAADDR(sc->sc_dmai);
    845 	d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
    846 	printf("CRMFB_FRM_CONTROL %08x -> %08x", crmfb_read_reg(sc, CRMFB_FRM_CONTROL), d);
    847 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    848 
    849 	/* init framebuffer width and pixel size */
    850 	/*d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);*/
    851 
    852 	d = ((int)(sc->sc_width / tile_width)) <<
    853 	    CRMFB_FRM_TILESIZE_WIDTH_SHIFT;
    854 	if ((sc->sc_width & (tile_width - 1)) != 0)
    855 		d |= sc->sc_tiles_y;
    856 
    857 	switch (depth) {
    858 	case 8:
    859 		h = CRMFB_FRM_TILESIZE_DEPTH_8;
    860 		break;
    861 	case 16:
    862 		h = CRMFB_FRM_TILESIZE_DEPTH_16;
    863 		break;
    864 	case 32:
    865 		h = CRMFB_FRM_TILESIZE_DEPTH_32;
    866 		break;
    867 	default:
    868 		panic("Unsupported depth");
    869 	}
    870 	d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
    871 	printf("CRMFB_FRM_TILESIZE %08x -> %08x", crmfb_read_reg(sc, CRMFB_FRM_TILESIZE), d);
    872 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    873 
    874 	/* init framebuffer height, we use the trick that the Linux
    875 	 * driver uses to fool the CRM out of tiled mode and into
    876 	 * linear mode
    877 	 */
    878 	/*h = sc->sc_width * sc->sc_height / (512 / (depth >> 3));*/
    879 	h = sc->sc_height;
    880 	d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
    881 	printf("CRMFB_FRM_PIXSIZE %08x -> %08x", crmfb_read_reg(sc, CRMFB_FRM_PIXSIZE), d);
    882 	crmfb_write_reg(sc, CRMFB_FRM_PIXSIZE, d);
    883 
    884 	/* turn off firmware overlay and hardware cursor */
    885 	crmfb_write_reg(sc, CRMFB_OVR_WIDTH_TILE, 0);
    886 	crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, 0);
    887 
    888 	/* enable drawing again */
    889 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    890 	d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    891 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
    892 	crmfb_write_reg(sc, CRMFB_VT_XY, 0);
    893 
    894 	/* turn on DMA for the framebuffer */
    895 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    896 	d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    897 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    898 
    899 	/* turn off sync-on-green */
    900 
    901 	wantsync = ARCBIOS->GetEnvironmentVariable("SyncOnGreen");
    902 	if ( (wantsync != NULL) && (wantsync[0] == 'n') ) {
    903 		d = ( 1 << CRMFB_VT_FLAGS_SYNC_LOW_LSB) & CRMFB_REG_MASK(CRMFB_VT_FLAGS_SYNC_LOW_MSB, CRMFB_VT_FLAGS_SYNC_LOW_LSB);
    904 		crmfb_write_reg(sc, CRMFB_VT_FLAGS, d);
    905 	}
    906 
    907 	sc->sc_depth = depth;
    908 
    909 	/* finally set up the drawing engine's TLB A */
    910 	v = (DMAADDR(sc->sc_dma) >> 16) & 0xffff;
    911 	tlbptr = 0;
    912 	tx = ((sc->sc_width + (tile_width - 1)) & ~(tile_width - 1)) / tile_width;
    913 	printf("tx: %d\n", tx);
    914 	for (i = 0; i < sc->sc_tiles_y; i++) {
    915 		reg = 0;
    916 		shift = 64;
    917 		for (j = 0; j < tx; j++) {
    918 			shift -= 16;
    919 			reg |= (((uint64_t)(v | 0x8000)) << shift);
    920 			if (shift == 0) {
    921 				shift = 64;
    922 				bus_space_write_8(sc->sc_iot, sc->sc_reh,
    923 				    CRIME_RE_TLB_A + tlbptr + (j & 0xffc) * 2, reg);
    924 				printf("%08x: %016llx\n", tlbptr + (j & 0xffc) * 2, reg);
    925 			}
    926 			v++;
    927 		}
    928 		if (shift != 64) {
    929 			bus_space_write_8(sc->sc_iot, sc->sc_reh,
    930 			    CRIME_RE_TLB_A + tlbptr + (j & 0xffc) * 2, reg);
    931 			printf("%08x: %016llx\n", tlbptr + (j & 0xffc) * 2, reg);
    932 		}
    933 		tlbptr += 32;
    934 	}
    935 
    936 	/* put the scratch page into the lower left of the fb */
    937 	reg = (((uint64_t)(DMAADDR(sc->sc_dma) + sc->sc_fbsize) | 0x80000000) &
    938 	    0xffff0000) << 32;
    939 	bus_space_write_8(sc->sc_iot, sc->sc_reh, CRIME_RE_TLB_A + 0x1e0, reg);
    940 
    941 	wbflush();
    942 
    943 	for (i = 0; i < 0x80; i += 8)
    944 		printf("%04x: %016llx\n", i, bus_space_read_8(sc->sc_iot, sc->sc_reh,
    945 			    CRIME_RE_TLB_A + i));
    946 
    947 	/* do some very basic engine setup */
    948 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_CLIPMODE, 0);
    949 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_SRC, 0);
    950 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_DST, 0);
    951 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PLANEMASK, 0xffffffff);
    952 
    953 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x20, 0);
    954 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x28, 0);
    955 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x30, 0);
    956 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x38, 0);
    957 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x40, 0);
    958 
    959 	switch (depth) {
    960 		case 8:
    961 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_8 |
    962 			    DE_MODE_TYPE_CI | DE_MODE_PIXDEPTH_8;
    963 			break;
    964 		case 16:
    965 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_16 |
    966 			    DE_MODE_TYPE_RGB | DE_MODE_PIXDEPTH_16;
    967 		case 32:
    968 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_32 |
    969 			    DE_MODE_TYPE_RGBA | DE_MODE_PIXDEPTH_32;
    970 		default:
    971 			panic("%s: unsuported colour depth %d\n", __func__,
    972 			    depth);
    973 	}
    974 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_DST, mode);
    975 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC, mode);
    976 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_X, 1);
    977 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_Y, 1);
    978 	return 0;
    979 }
    980 
    981 static void
    982 crmfb_setup_palette(struct crmfb_softc *sc)
    983 {
    984 	int i;
    985 
    986 	for (i = 0; i < 256; i++) {
    987 		crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
    988 		    rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
    989 		sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
    990 		sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
    991 		sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
    992 	}
    993 }
    994 
    995 static inline void
    996 crmfb_wait_idle(struct crmfb_softc *sc)
    997 {
    998 	int i = 0;
    999 
   1000 	do {
   1001 		i++;
   1002 		delay(1);
   1003 	} while (((bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STATUS) &
   1004 		   CRIME_DE_IDLE) == 0) && (i < 100000000));
   1005 	if (i >= 100000000)
   1006 		aprint_error("crmfb_wait_idle() timed out\n");
   1007 }
   1008 
   1009 static void
   1010 crmfb_fill_rect(struct crmfb_softc *sc, int x, int y, int width, int height,
   1011     uint32_t colour)
   1012 {
   1013 	crmfb_wait_idle(sc);
   1014 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
   1015 	    DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK);
   1016 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FG, colour);
   1017 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
   1018 		DE_PRIM_RECTANGLE | DE_PRIM_TB);
   1019 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
   1020 	    (x << 16) | (y & 0xffff));
   1021 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1022 	    CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
   1023 	    ((x + width - 1) << 16) | ((y + height - 1) & 0xffff));
   1024 }
   1025 
   1026 static void
   1027 crmfb_bitblt(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
   1028     int wi, int he, uint32_t rop)
   1029 {
   1030 	uint32_t prim = DE_PRIM_RECTANGLE;
   1031 	int rxa, rya, rxe, rye, rxs, rys;
   1032 	crmfb_wait_idle(sc);
   1033 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
   1034 	    DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK | DE_DRAWMODE_ROP |
   1035 	    DE_DRAWMODE_XFER_EN);
   1036 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, rop);
   1037 	if (xs < xd) {
   1038 		prim |= DE_PRIM_RL;
   1039 		rxe = xd;
   1040 		rxa = xd + wi - 1;
   1041 		rxs = xs + wi - 1;
   1042 	} else {
   1043 		prim |= DE_PRIM_LR;
   1044 		rxe = xd + wi - 1;
   1045 		rxa = xd;
   1046 		rxs = xs;
   1047 	}
   1048 	if (ys < yd) {
   1049 		prim |= DE_PRIM_BT;
   1050 		rye = yd;
   1051 		rya = yd + he - 1;
   1052 		rys = ys + he - 1;
   1053 	} else {
   1054 		prim |= DE_PRIM_TB;
   1055 		rye = yd + he - 1;
   1056 		rya = yd;
   1057 		rys = ys;
   1058 	}
   1059 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE, prim);
   1060 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC,
   1061 	    (rxs << 16) | (rys & 0xffff));
   1062 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
   1063 	    (rxa << 16) | (rya & 0xffff));
   1064 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1065 	    CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
   1066 	    (rxe << 16) | (rye & 0xffff));
   1067 }
   1068 
   1069 #ifdef CRMFB_DEBUG
   1070 void
   1071 crmfb_test_mte(struct crmfb_softc *sc)
   1072 {
   1073 	uint32_t status[10];
   1074 	int i;
   1075 
   1076 	crmfb_fill_rect(sc, 1, 1, 100, 100, 1);
   1077 	crmfb_fill_rect(sc, 102, 1, 100, 100, 2);
   1078 	crmfb_fill_rect(sc, 203, 1, 100, 100, 3);
   1079 	crmfb_fill_rect(sc, 1024, 1, 100, 100, 4);
   1080 
   1081 	crmfb_bitblt(sc, 10, 10, 400, 0, 300, 300, 12);
   1082 	crmfb_wait_idle(sc);
   1083 	printf("ROP: %08x\n", bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP));
   1084 #if 0
   1085 	delay(4000000);
   1086 
   1087 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_NULL, 0x0);
   1088 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_BG, 0x05050505);
   1089 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
   1090 	    MTE_MODE_DST_ECC |
   1091 	    (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
   1092 	    (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
   1093 	    (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
   1094 	    0/*MTE_MODE_COPY*/);
   1095 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_STRIDE, 1);
   1096 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_STRIDE, 1);
   1097 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC0, 0x00000000);
   1098 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC1, 0x02000200);
   1099 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST0, 0x03000000);
   1100 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST1 | CRIME_DE_START, 0x02000200);
   1101 	status[9] = bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STATUS);
   1102 #endif
   1103 #if 1
   1104 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
   1105 	    MTE_MODE_DST_ECC |
   1106 	    (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
   1107 	    (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
   1108 	    (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
   1109 	    0/*MTE_MODE_COPY*/);
   1110 #endif
   1111 	delay(4000000);
   1112 #if 0
   1113 	printf("flush: %08x\n",
   1114 	    bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FLUSH));
   1115 #endif
   1116 
   1117 	for (i = 0; i < 10; i++)
   1118 		printf("%08x ", status[i]);
   1119 	printf("\n");
   1120 }
   1121 #endif /* CRMFB_DEBUG */
   1122 
   1123 static void
   1124 crmfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1125 {
   1126 	struct rasops_info *ri = cookie;
   1127 	struct vcons_screen *scr = ri->ri_hw;
   1128 	int32_t xs, xd, y, width, height;
   1129 
   1130 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1131 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1132 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1133 	width = ri->ri_font->fontwidth * ncols;
   1134 	height = ri->ri_font->fontheight;
   1135 	crmfb_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, 3);
   1136 }
   1137 
   1138 static void
   1139 crmfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1140 {
   1141 	struct rasops_info *ri = cookie;
   1142 	struct vcons_screen *scr = ri->ri_hw;
   1143 	int32_t x, y, width, height, bg;
   1144 
   1145 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1146 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1147 	width = ri->ri_font->fontwidth * ncols;
   1148 	height = ri->ri_font->fontheight;
   1149 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1150 	crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
   1151 }
   1152 
   1153 static void
   1154 crmfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1155 {
   1156 	struct rasops_info *ri = cookie;
   1157 	struct vcons_screen *scr = ri->ri_hw;
   1158 	int32_t x, ys, yd, width, height;
   1159 
   1160 	x = ri->ri_xorigin;
   1161 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1162 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1163 	width = ri->ri_emuwidth;
   1164 	height = ri->ri_font->fontheight * nrows;
   1165 	crmfb_bitblt(scr->scr_cookie, x, ys, x, yd, width, height, 3);
   1166 }
   1167 
   1168 static void
   1169 crmfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1170 {
   1171 	struct rasops_info *ri = cookie;
   1172 	struct vcons_screen *scr = ri->ri_hw;
   1173 	int32_t x, y, width, height, bg;
   1174 
   1175 	if ((row == 0) && (nrows == ri->ri_rows)) {
   1176 		x = y = 0;
   1177 		width = ri->ri_width;
   1178 		height = ri->ri_height;
   1179 	} else {
   1180 		x = ri->ri_xorigin;
   1181 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1182 		width = ri->ri_emuwidth;
   1183 		height = ri->ri_font->fontheight * nrows;
   1184 	}
   1185 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1186 	crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
   1187 }
   1188 
   1189 static void
   1190 crmfb_cursor(void *cookie, int on, int row, int col)
   1191 {
   1192 	struct rasops_info *ri = cookie;
   1193 	struct vcons_screen *scr = ri->ri_hw;
   1194 	struct crmfb_softc *sc = scr->scr_cookie;
   1195 	int x, y, wi,he;
   1196 
   1197 	wi = ri->ri_font->fontwidth;
   1198 	he = ri->ri_font->fontheight;
   1199 
   1200 	if (ri->ri_flg & RI_CURSOR) {
   1201 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1202 		y = ri->ri_crow * he + ri->ri_yorigin;
   1203 		crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
   1204 		ri->ri_flg &= ~RI_CURSOR;
   1205 	}
   1206 
   1207 	ri->ri_crow = row;
   1208 	ri->ri_ccol = col;
   1209 
   1210 	if (on)
   1211 	{
   1212 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1213 		y = ri->ri_crow * he + ri->ri_yorigin;
   1214 		crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
   1215 		ri->ri_flg |= RI_CURSOR;
   1216 	}
   1217 }
   1218 
   1219 static void
   1220 crmfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1221 {
   1222 	struct rasops_info *ri = cookie;
   1223 	struct vcons_screen *scr = ri->ri_hw;
   1224 	struct crmfb_softc *sc = scr->scr_cookie;
   1225 	struct rasops_info *fri = &sc->sc_rasops;
   1226 	int bg;
   1227 	int x, y, wi, he, xs;
   1228 
   1229 	wi = ri->ri_font->fontwidth;
   1230 	he = ri->ri_font->fontheight;
   1231 
   1232 	x = ri->ri_xorigin + col * wi;
   1233 	y = ri->ri_yorigin + row * he;
   1234 
   1235 	bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xff];
   1236 	if (c == 0x20) {
   1237 		crmfb_fill_rect(sc, x, y, wi, he, bg);
   1238 	} else {
   1239 		/*
   1240 		 * we rotate over all available character cells in the scratch
   1241 		 * tile. The idea is to have more cells than there's room for
   1242 		 * drawing commands in the engine's pipeline so we don't have
   1243 		 * to wait for the engine until we're done drawing the
   1244 		 * character and ready to blit it into place
   1245 		 */
   1246 		fri->ri_ops.putchar(fri, 0, sc->sc_current_cell, c, attr);
   1247 		xs = sc->sc_current_cell * wi;
   1248 		sc->sc_current_cell++;
   1249 		if (sc->sc_current_cell >= sc->sc_cells)
   1250 			sc->sc_current_cell = 0;
   1251 		crmfb_bitblt(sc, xs, 2048-128, x, y, wi, he, 3);
   1252 	}
   1253 }
   1254