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