Home | History | Annotate | Line # | Download | only in dev
crmfb.c revision 1.15
      1 /* $NetBSD: crmfb.c,v 1.15 2008/02/17 00:51:15 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.15 2008/02/17 00:51:15 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 static void crmfb_scroll(struct crmfb_softc *, int, int, int, int, int, int);
    184 
    185 static void	crmfb_copycols(void *, int, int, int, int);
    186 static void	crmfb_erasecols(void *, int, int, int, long);
    187 static void	crmfb_copyrows(void *, int, int, int);
    188 static void	crmfb_eraserows(void *, int, 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 		aprint_error("%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 	aprint_normal("%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 	aprint_normal("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 	aprint_normal("%s: allocated %d byte fb @ %p (%p)\n",
    316 	    sc->sc_dev.dv_xname,
    317 	    sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
    318 
    319 	sc->sc_current_cell = 0;
    320 
    321 	ri = &crmfb_console_screen.scr_ri;
    322 	memset(ri, 0, sizeof(struct rasops_info));
    323 
    324 	vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
    325 	sc->sc_vd.init_screen = crmfb_init_screen;
    326 	crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    327 	vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
    328 
    329 	crmfb_defaultscreen.ncols = ri->ri_cols;
    330 	crmfb_defaultscreen.nrows = ri->ri_rows;
    331 	crmfb_defaultscreen.textops = &ri->ri_ops;
    332 	crmfb_defaultscreen.capabilities = ri->ri_caps;
    333 	crmfb_defaultscreen.modecookie = NULL;
    334 
    335 	crmfb_setup_video(sc, 8);
    336 	crmfb_setup_palette(sc);
    337 	crmfb_fill_rect(sc, 0, 0, sc->sc_width, sc->sc_height,
    338 	    (defattr >> 16) & 0xff);
    339 
    340 	consdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut");
    341 	if (consdev != NULL && strcmp(consdev, "video()") == 0) {
    342 		wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
    343 		aa.console = 1;
    344 	} else
    345 		aa.console = 0;
    346 	aa.scrdata = &crmfb_screenlist;
    347 	aa.accessops = &crmfb_accessops;
    348 	aa.accesscookie = &sc->sc_vd;
    349 
    350 	config_found(self, &aa, wsemuldisplaydevprint);
    351 
    352 	sc->sc_cur_x = 0;
    353 	sc->sc_cur_y = 0;
    354 	sc->sc_hot_x = 0;
    355 	sc->sc_hot_y = 0;
    356 
    357 #ifdef CRMFB_DEBUG
    358 	crmfb_test_mte(sc);
    359 #endif
    360 	return;
    361 }
    362 
    363 int
    364 crmfb_probe(void)
    365 {
    366 
    367         if (mach_type != MACH_SGI_IP32)
    368                 return 0;
    369 
    370 	return 1;
    371 }
    372 
    373 static int
    374 crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    375 {
    376 	struct vcons_data *vd;
    377 	struct crmfb_softc *sc;
    378 	struct vcons_screen *ms;
    379 	struct wsdisplay_fbinfo *wdf;
    380 	int nmode;
    381 
    382 	vd = (struct vcons_data *)v;
    383 	sc = (struct crmfb_softc *)vd->cookie;
    384 	ms = (struct vcons_screen *)vd->active;
    385 
    386 	switch (cmd) {
    387 	case WSDISPLAYIO_GTYPE:
    388 		/* not really, but who cares? */
    389 		/* wsfb does */
    390 		*(u_int *)data = WSDISPLAY_TYPE_CRIME;
    391 		return 0;
    392 	case WSDISPLAYIO_GINFO:
    393 		if (vd->active != NULL) {
    394 			wdf = (void *)data;
    395 			wdf->height = sc->sc_height;
    396 			wdf->width = sc->sc_width;
    397 			wdf->depth = 32;
    398 			wdf->cmsize = 256;
    399 			return 0;
    400 		} else
    401 			return ENODEV;
    402 	case WSDISPLAYIO_GETCMAP:
    403 		if (sc->sc_depth == 8)
    404 			return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
    405 		else
    406 			return EINVAL;
    407 	case WSDISPLAYIO_PUTCMAP:
    408 		if (sc->sc_depth == 8)
    409 			return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
    410 		else
    411 			return EINVAL;
    412 	case WSDISPLAYIO_LINEBYTES:
    413 		*(u_int *)data = sc->sc_width * sc->sc_depth / 8;
    414 		return 0;
    415 	case WSDISPLAYIO_SMODE:
    416 		nmode = *(int *)data;
    417 		if (nmode != sc->sc_wsmode) {
    418 			sc->sc_wsmode = nmode;
    419 			if (nmode == WSDISPLAYIO_MODE_EMUL) {
    420 				crmfb_setup_video(sc, 8);
    421 				crmfb_setup_palette(sc);
    422 				vcons_redraw_screen(vd->active);
    423 			} else {
    424 				crmfb_setup_video(sc, 32);
    425 			}
    426 		}
    427 		return 0;
    428 	case WSDISPLAYIO_SVIDEO:
    429 	case WSDISPLAYIO_GVIDEO:
    430 		return ENODEV;	/* not supported yet */
    431 
    432 	case WSDISPLAYIO_GCURPOS:
    433 		{
    434 			struct wsdisplay_curpos *pos;
    435 
    436 			pos = (struct wsdisplay_curpos *)data;
    437 			pos->x = sc->sc_cur_x;
    438 			pos->y = sc->sc_cur_y;
    439 		}
    440 		return 0;
    441 	case WSDISPLAYIO_SCURPOS:
    442 		{
    443 			struct wsdisplay_curpos *pos;
    444 
    445 			pos = (struct wsdisplay_curpos *)data;
    446 			crmfb_set_curpos(sc, pos->x, pos->y);
    447 		}
    448 		return 0;
    449 	case WSDISPLAYIO_GCURMAX:
    450 		{
    451 			struct wsdisplay_curpos *pos;
    452 
    453 			pos = (struct wsdisplay_curpos *)data;
    454 			pos->x = 32;
    455 			pos->y = 32;
    456 		}
    457 		return 0;
    458 	case WSDISPLAYIO_GCURSOR:
    459 		{
    460 			struct wsdisplay_cursor *cu;
    461 
    462 			cu = (struct wsdisplay_cursor *)data;
    463 			return crmfb_gcursor(sc, cu);
    464 		}
    465 	case WSDISPLAYIO_SCURSOR:
    466 		{
    467 			struct wsdisplay_cursor *cu;
    468 
    469 			cu = (struct wsdisplay_cursor *)data;
    470 			return crmfb_scursor(sc, cu);
    471 		}
    472 	}
    473 	return EPASSTHROUGH;
    474 }
    475 
    476 static paddr_t
    477 crmfb_mmap(void *v, void *vs, off_t offset, int prot)
    478 {
    479 	struct vcons_data *vd;
    480 	struct crmfb_softc *sc;
    481 	paddr_t pa;
    482 
    483 	vd = (struct vcons_data *)v;
    484 	sc = (struct crmfb_softc *)vd->cookie;
    485 
    486 	/* we probably shouldn't let anyone mmap the framebuffer */
    487 #if 1
    488 	if (offset >= 0 && offset < sc->sc_fbsize) {
    489 		pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
    490 		    sc->sc_dma.nsegs, offset, prot,
    491 		    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    492 		return pa;
    493 	}
    494 #endif
    495 	/*
    496 	 * here would the TLBs be but we don't want to show them to userland
    497 	 * so we return the page containing the status register
    498 	 */
    499 	if ((offset >= 0x15000000) && (offset < 0x15002000))
    500 		return bus_space_mmap(sc->sc_iot, 0x15004000, 0, prot, 0);
    501 	/* now the actual engine registers */
    502 	if ((offset >= 0x15002000) && (offset < 0x15005000))
    503 		return bus_space_mmap(sc->sc_iot, offset, 0, prot, 0);
    504 	/* and now the scratch area */
    505 	if ((offset >= 0x15010000) && (offset < 0x15020000))
    506 		return bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
    507 		   sc->sc_dma.nsegs, offset - 0x15010000 + sc->sc_fbsize,
    508 		   prot, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    509 	return -1;
    510 }
    511 
    512 static void
    513 crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
    514     long *defattr)
    515 {
    516 	struct crmfb_softc *sc;
    517 	struct rasops_info *ri;
    518 
    519 	sc = (struct crmfb_softc *)c;
    520 	ri = &scr->scr_ri;
    521 
    522 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    523 	ri->ri_depth = sc->sc_depth;
    524 	ri->ri_width = sc->sc_width;
    525 	ri->ri_height = sc->sc_height;
    526 	ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
    527 
    528 	switch (ri->ri_depth) {
    529 	case 16:
    530 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
    531 		ri->ri_rpos = 10;
    532 		ri->ri_gpos = 5;
    533 		ri->ri_bpos = 0;
    534 		break;
    535 	case 32:
    536 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    537 		ri->ri_rpos = 8;
    538 		ri->ri_gpos = 16;
    539 		ri->ri_bpos = 24;
    540 		break;
    541 	}
    542 
    543 	ri->ri_bits = KERNADDR(sc->sc_dma);
    544 
    545 	if (existing)
    546 		ri->ri_flg |= RI_CLEAR;
    547 
    548 	rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
    549 	ri->ri_caps = WSSCREEN_WSCOLORS;
    550 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    551 	    ri->ri_width / ri->ri_font->fontwidth);
    552 	ri->ri_hw = scr;
    553 
    554 	/* now make a fake rasops_info for drawing into the scratch tile */
    555 	memcpy(&sc->sc_rasops, ri, sizeof(struct rasops_info));
    556 	sc->sc_rasops.ri_width = 512;	/* assume we're always in 8bit here */
    557 	sc->sc_rasops.ri_stride = 512;
    558 	sc->sc_rasops.ri_height = 128;
    559 	sc->sc_rasops.ri_xorigin = 0;
    560 	sc->sc_rasops.ri_yorigin = 0;
    561 	sc->sc_rasops.ri_bits = sc->sc_scratch;
    562 	sc->sc_cells = 512 / ri->ri_font->fontwidth;
    563 
    564 	ri->ri_ops.cursor    = crmfb_cursor;
    565 	ri->ri_ops.copyrows  = crmfb_copyrows;
    566 	ri->ri_ops.eraserows = crmfb_eraserows;
    567 	ri->ri_ops.copycols  = crmfb_copycols;
    568 	ri->ri_ops.erasecols = crmfb_erasecols;
    569 	ri->ri_ops.putchar   = crmfb_putchar;
    570 
    571 	return;
    572 }
    573 
    574 static int
    575 crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    576 {
    577 	u_int idx, cnt;
    578 	u_char r[256], g[256], b[256];
    579 	u_char *rp, *gp, *bp;
    580 	int rv, i;
    581 
    582 	idx = cm->index;
    583 	cnt = cm->count;
    584 
    585 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    586 		return EINVAL;
    587 
    588 	rv = copyin(cm->red, &r[idx], cnt);
    589 	if (rv)
    590 		return rv;
    591 	rv = copyin(cm->green, &g[idx], cnt);
    592 	if (rv)
    593 		return rv;
    594 	rv = copyin(cm->blue, &b[idx], cnt);
    595 	if (rv)
    596 		return rv;
    597 
    598 	memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
    599 	memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
    600 	memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
    601 
    602 	rp = &sc->sc_cmap_red[idx];
    603 	gp = &sc->sc_cmap_green[idx];
    604 	bp = &sc->sc_cmap_blue[idx];
    605 
    606 	for (i = 0; i < cnt; i++) {
    607 		crmfb_set_palette(sc, idx, *rp, *gp, *bp);
    608 		idx++;
    609 		rp++, gp++, bp++;
    610 	}
    611 
    612 	return 0;
    613 }
    614 
    615 static int
    616 crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    617 {
    618 	u_int idx, cnt;
    619 	int rv;
    620 
    621 	idx = cm->index;
    622 	cnt = cm->count;
    623 
    624 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    625 		return EINVAL;
    626 
    627 	rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
    628 	if (rv)
    629 		return rv;
    630 	rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
    631 	if (rv)
    632 		return rv;
    633 	rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
    634 	if (rv)
    635 		return rv;
    636 
    637 	return 0;
    638 }
    639 
    640 static void
    641 crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
    642     uint8_t b)
    643 {
    644 	uint32_t val;
    645 
    646 	if (reg > 255 || sc->sc_depth != 8)
    647 		return;
    648 
    649 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
    650 		DELAY(10);
    651 
    652 	val = (r << 24) | (g << 16) | (b << 8);
    653 	crmfb_write_reg(sc, CRMFB_CMAP + (reg * 4), val);
    654 
    655 	return;
    656 }
    657 
    658 static int
    659 crmfb_set_curpos(struct crmfb_softc *sc, int x, int y)
    660 {
    661 	uint32_t val;
    662 
    663 	sc->sc_cur_x = x;
    664 	sc->sc_cur_y = y;
    665 
    666 	val = ((x - sc->sc_hot_x) & 0xffff) | ((y - sc->sc_hot_y) << 16);
    667 	crmfb_write_reg(sc, CRMFB_CURSOR_POS, val);
    668 
    669 	return 0;
    670 }
    671 
    672 static int
    673 crmfb_gcursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    674 {
    675 	/* do nothing for now */
    676 	return 0;
    677 }
    678 
    679 static int
    680 crmfb_scursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    681 {
    682 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
    683 
    684 		crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, cur->enable ? 1 : 0);
    685 	}
    686 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
    687 
    688 		sc->sc_hot_x = cur->hot.x;
    689 		sc->sc_hot_y = cur->hot.y;
    690 	}
    691 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
    692 
    693 		crmfb_set_curpos(sc, cur->pos.x, cur->pos.y);
    694 	}
    695 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
    696 		int i;
    697 		uint32_t val;
    698 
    699 		for (i = 0; i < cur->cmap.count; i++) {
    700 			val = (cur->cmap.red[i] << 24) |
    701 			      (cur->cmap.green[i] << 16) |
    702 			      (cur->cmap.blue[i] << 8);
    703 			crmfb_write_reg(sc, CRMFB_CURSOR_CMAP0 +
    704 			    ((i + cur->cmap.index) << 2), val);
    705 		}
    706 	}
    707 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
    708 
    709 		int i, j, cnt = 0;
    710 		uint32_t latch = 0, omask;
    711 		uint8_t imask;
    712 		for (i = 0; i < 64; i++) {
    713 			omask = 0x80000000;
    714 			imask = 0x01;
    715 			cur->image[cnt] &= cur->mask[cnt];
    716 			for (j = 0; j < 8; j++) {
    717 				if (cur->image[cnt] & imask)
    718 					latch |= omask;
    719 				omask >>= 1;
    720 				if (cur->mask[cnt] & imask)
    721 					latch |= omask;
    722 				omask >>= 1;
    723 				imask <<= 1;
    724 			}
    725 			cnt++;
    726 			imask = 0x01;
    727 			cur->image[cnt] &= cur->mask[cnt];
    728 			for (j = 0; j < 8; j++) {
    729 				if (cur->image[cnt] & imask)
    730 					latch |= omask;
    731 				omask >>= 1;
    732 				if (cur->mask[cnt] & imask)
    733 					latch |= omask;
    734 				omask >>= 1;
    735 				imask <<= 1;
    736 			}
    737 			cnt++;
    738 			crmfb_write_reg(sc, CRMFB_CURSOR_BITMAP + (i << 2),
    739 			    latch);
    740 			latch = 0;
    741 		}
    742 	}
    743 	return 0;
    744 }
    745 
    746 static inline void
    747 crmfb_write_reg(struct crmfb_softc *sc, int offset, uint32_t val)
    748 {
    749 
    750 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
    751 	wbflush();
    752 }
    753 
    754 static inline uint32_t
    755 crmfb_read_reg(struct crmfb_softc *sc, int offset)
    756 {
    757 
    758 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
    759 }
    760 
    761 static int
    762 crmfb_wait_dma_idle(struct crmfb_softc *sc)
    763 {
    764 	int bail = 100000, idle;
    765 
    766 	do {
    767 		idle = ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    768 		         CRMFB_OVR_CONTROL) & 1) == 0) &&
    769 		       ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    770 		         CRMFB_FRM_CONTROL) & 1) == 0) &&
    771 		       ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    772 		         CRMFB_DID_CONTROL) & 1) == 0);
    773 		if (!idle)
    774 			delay(10);
    775 		bail--;
    776 	} while ((!idle) && (bail > 0));
    777 	return idle;
    778 }
    779 
    780 static int
    781 crmfb_setup_video(struct crmfb_softc *sc, int depth)
    782 {
    783 	uint64_t reg;
    784 	uint32_t d, h, mode;
    785 	int i, bail, tile_width, tlbptr, j, tx, shift;
    786 	const char *wantsync;
    787 	uint16_t v;
    788 
    789 	/* disable DMA */
    790 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
    791 	d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
    792 	crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d);
    793 	DELAY(50000);
    794 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    795 	d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    796 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    797 	DELAY(50000);
    798 	crmfb_write_reg(sc, CRMFB_DID_CONTROL, 0);
    799 	DELAY(50000);
    800 
    801 	if (!crmfb_wait_dma_idle(sc))
    802 		aprint_error("crmfb: crmfb_wait_dma_idle timed out\n");
    803 
    804 	/* ensure that CRM starts drawing at the top left of the screen
    805 	 * when we re-enable DMA later
    806 	 */
    807 	d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
    808 	crmfb_write_reg(sc, CRMFB_VT_XY, d);
    809 	delay(1000);
    810 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    811 	d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    812 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
    813 
    814 	/* wait for dotclock to turn off */
    815 	bail = 10000;
    816 	while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK) &
    817 	    (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT)) && (bail > 0)) {
    818 		delay(10);
    819 		bail--;
    820 	}
    821 
    822 	/* reset FIFO */
    823 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    824 	d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    825 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    826 	d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    827 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    828 
    829 	/* setup colour mode */
    830 	switch (depth) {
    831 	case 8:
    832 		h = CRMFB_MODE_TYP_I8;
    833 		tile_width = 512;
    834 		break;
    835 	case 16:
    836 		h = CRMFB_MODE_TYP_ARGB5;
    837 		tile_width = 256;
    838 		break;
    839 	case 32:
    840 		h = CRMFB_MODE_TYP_RGB8;
    841 		tile_width = 128;
    842 		break;
    843 	default:
    844 		panic("Unsupported depth");
    845 	}
    846 	d = h << CRMFB_MODE_TYP_SHIFT;
    847 	d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
    848 	for (i = 0; i < (32 * 4); i += 4)
    849 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
    850 	wbflush();
    851 
    852 	/* setup tile pointer, but don't turn on DMA yet! */
    853 	h = DMAADDR(sc->sc_dmai);
    854 	d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
    855 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    856 
    857 	/* init framebuffer width and pixel size */
    858 	/*d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);*/
    859 
    860 	d = ((int)(sc->sc_width / tile_width)) <<
    861 	    CRMFB_FRM_TILESIZE_WIDTH_SHIFT;
    862 	if ((sc->sc_width & (tile_width - 1)) != 0)
    863 		d |= sc->sc_tiles_y;
    864 
    865 	switch (depth) {
    866 	case 8:
    867 		h = CRMFB_FRM_TILESIZE_DEPTH_8;
    868 		break;
    869 	case 16:
    870 		h = CRMFB_FRM_TILESIZE_DEPTH_16;
    871 		break;
    872 	case 32:
    873 		h = CRMFB_FRM_TILESIZE_DEPTH_32;
    874 		break;
    875 	default:
    876 		panic("Unsupported depth");
    877 	}
    878 	d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
    879 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    880 
    881 	/*h = sc->sc_width * sc->sc_height / (512 / (depth >> 3));*/
    882 	h = sc->sc_height;
    883 	d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
    884 	crmfb_write_reg(sc, CRMFB_FRM_PIXSIZE, d);
    885 
    886 	/* turn off firmware overlay and hardware cursor */
    887 	crmfb_write_reg(sc, CRMFB_OVR_WIDTH_TILE, 0);
    888 	crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, 0);
    889 
    890 	/* enable drawing again */
    891 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    892 	d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    893 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
    894 	crmfb_write_reg(sc, CRMFB_VT_XY, 0);
    895 
    896 	/* turn on DMA for the framebuffer */
    897 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    898 	d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    899 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    900 
    901 	/* turn off sync-on-green */
    902 
    903 	wantsync = ARCBIOS->GetEnvironmentVariable("SyncOnGreen");
    904 	if ( (wantsync != NULL) && (wantsync[0] == 'n') ) {
    905 		d = ( 1 << CRMFB_VT_FLAGS_SYNC_LOW_LSB) &
    906 		    CRMFB_REG_MASK(CRMFB_VT_FLAGS_SYNC_LOW_MSB,
    907 		    CRMFB_VT_FLAGS_SYNC_LOW_LSB);
    908 		crmfb_write_reg(sc, CRMFB_VT_FLAGS, d);
    909 	}
    910 
    911 	sc->sc_depth = depth;
    912 
    913 	/* finally set up the drawing engine's TLB A */
    914 	v = (DMAADDR(sc->sc_dma) >> 16) & 0xffff;
    915 	tlbptr = 0;
    916 	tx = ((sc->sc_width + (tile_width - 1)) & ~(tile_width - 1)) /
    917 	    tile_width;
    918 
    919 	for (i = 0; i < sc->sc_tiles_y; i++) {
    920 		reg = 0;
    921 		shift = 64;
    922 		for (j = 0; j < tx; j++) {
    923 			shift -= 16;
    924 			reg |= (((uint64_t)(v | 0x8000)) << shift);
    925 			if (shift == 0) {
    926 				shift = 64;
    927 				bus_space_write_8(sc->sc_iot, sc->sc_reh,
    928 				    CRIME_RE_TLB_A + tlbptr + (j & 0xffc) * 2,
    929 				    reg);
    930 			}
    931 			v++;
    932 		}
    933 		if (shift != 64) {
    934 			bus_space_write_8(sc->sc_iot, sc->sc_reh,
    935 			    CRIME_RE_TLB_A + tlbptr + (j & 0xffc) * 2, reg);
    936 		}
    937 		tlbptr += 32;
    938 	}
    939 
    940 	/* put the scratch page into the lower left of the fb */
    941 	reg = (((uint64_t)(DMAADDR(sc->sc_dma) + sc->sc_fbsize) | 0x80000000) &
    942 	    0xffff0000) << 32;
    943 	bus_space_write_8(sc->sc_iot, sc->sc_reh, CRIME_RE_TLB_A + 0x1e0, reg);
    944 
    945 	wbflush();
    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,
    952 	    0xffffffff);
    953 
    954 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x20, 0);
    955 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x28, 0);
    956 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x30, 0);
    957 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x38, 0);
    958 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x40, 0);
    959 
    960 	switch (depth) {
    961 		case 8:
    962 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_8 |
    963 			    DE_MODE_TYPE_CI | DE_MODE_PIXDEPTH_8;
    964 			break;
    965 		case 16:
    966 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_16 |
    967 			    DE_MODE_TYPE_RGB | DE_MODE_PIXDEPTH_16;
    968 			break;
    969 		case 32:
    970 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_32 |
    971 			    DE_MODE_TYPE_RGBA | DE_MODE_PIXDEPTH_32;
    972 			break;
    973 		default:
    974 			panic("%s: unsuported colour depth %d\n", __func__,
    975 			    depth);
    976 	}
    977 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_DST, mode);
    978 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC, mode);
    979 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_X, 1);
    980 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_Y, 1);
    981 
    982 	/* initialize memory transfer engine */
    983 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
    984 	    MTE_MODE_DST_ECC |
    985 	    (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
    986 	    (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
    987 	    (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
    988 	    MTE_MODE_COPY);
    989 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_STRIDE, 1);
    990 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_STRIDE, 1);
    991 
    992 	return 0;
    993 }
    994 
    995 static void
    996 crmfb_setup_palette(struct crmfb_softc *sc)
    997 {
    998 	int i;
    999 
   1000 	for (i = 0; i < 256; i++) {
   1001 		crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
   1002 		    rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
   1003 		sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
   1004 		sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
   1005 		sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
   1006 	}
   1007 }
   1008 
   1009 static inline void
   1010 crmfb_wait_idle(struct crmfb_softc *sc)
   1011 {
   1012 	int i = 0;
   1013 
   1014 	do {
   1015 		i++;
   1016 	} while (((bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STATUS) &
   1017 		   CRIME_DE_IDLE) == 0) && (i < 100000000));
   1018 	if (i >= 100000000)
   1019 		aprint_error("crmfb_wait_idle() timed out\n");
   1020 }
   1021 
   1022 static void
   1023 crmfb_fill_rect(struct crmfb_softc *sc, int x, int y, int width, int height,
   1024     uint32_t colour)
   1025 {
   1026 	crmfb_wait_idle(sc);
   1027 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
   1028 	    DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK);
   1029 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FG, colour);
   1030 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
   1031 		DE_PRIM_RECTANGLE | DE_PRIM_TB);
   1032 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
   1033 	    (x << 16) | (y & 0xffff));
   1034 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1035 	    CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
   1036 	    ((x + width - 1) << 16) | ((y + height - 1) & 0xffff));
   1037 }
   1038 
   1039 static void
   1040 crmfb_bitblt(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
   1041     int wi, int he, uint32_t rop)
   1042 {
   1043 	uint32_t prim = DE_PRIM_RECTANGLE;
   1044 	int rxa, rya, rxe, rye, rxs, rys;
   1045 	crmfb_wait_idle(sc);
   1046 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
   1047 	    DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK | DE_DRAWMODE_ROP |
   1048 	    DE_DRAWMODE_XFER_EN);
   1049 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, rop);
   1050 	if (xs < xd) {
   1051 		prim |= DE_PRIM_RL;
   1052 		rxe = xd;
   1053 		rxa = xd + wi - 1;
   1054 		rxs = xs + wi - 1;
   1055 	} else {
   1056 		prim |= DE_PRIM_LR;
   1057 		rxe = xd + wi - 1;
   1058 		rxa = xd;
   1059 		rxs = xs;
   1060 	}
   1061 	if (ys < yd) {
   1062 		prim |= DE_PRIM_BT;
   1063 		rye = yd;
   1064 		rya = yd + he - 1;
   1065 		rys = ys + he - 1;
   1066 	} else {
   1067 		prim |= DE_PRIM_TB;
   1068 		rye = yd + he - 1;
   1069 		rya = yd;
   1070 		rys = ys;
   1071 	}
   1072 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE, prim);
   1073 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC,
   1074 	    (rxs << 16) | (rys & 0xffff));
   1075 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
   1076 	    (rxa << 16) | (rya & 0xffff));
   1077 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1078 	    CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
   1079 	    (rxe << 16) | (rye & 0xffff));
   1080 }
   1081 
   1082 static void
   1083 crmfb_scroll(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
   1084     int wi, int he)
   1085 {
   1086 	int rxa, rya, rxe, rye, rxd, ryd, rxde, ryde;
   1087 
   1088 	crmfb_wait_idle(sc);
   1089 	if (ys < yd) {
   1090 		/* bottom to top */
   1091 		/*
   1092 		 * XXX this doesn't work right, there must be another way to
   1093 		 * control in which direction the MTE copies data
   1094 		 */
   1095 		rye = ys;
   1096 		rya = ys + he - 1;
   1097 		ryd = yd + he - 1;
   1098 		ryde = yd;
   1099 		rxe = xs;
   1100 		rxa = xs + wi - 1;
   1101 		rxd = xd + wi - 1;
   1102 		rxde = xd;
   1103 		bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1104 		    CRIME_MTE_DST_STRIDE, -1);
   1105 		bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1106 		    CRIME_MTE_SRC_STRIDE, -1);
   1107 	} else {
   1108 		/* top to bottom */
   1109 		rye = ys + he - 1;
   1110 		rya = ys;
   1111 		ryd = yd;
   1112 		ryde = yd + he - 1;
   1113 		rxe = xs + wi - 1;
   1114 		rxa = xs;
   1115 		rxd = xd;
   1116 		rxde = xd + wi - 1;
   1117 		bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1118 		    CRIME_MTE_DST_STRIDE, 1);
   1119 		bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1120 		    CRIME_MTE_SRC_STRIDE, 1);
   1121 	}
   1122 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC0,
   1123 	    (rxa << 16) | rya);
   1124 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC1,
   1125 	    (rxe << 16) | rye);
   1126 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1127 	    CRIME_MTE_DST0,
   1128 	    (rxd << 16) | ryd);
   1129 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST1 |
   1130 	    CRIME_DE_START,
   1131 	    (rxde << 16) | ryde);
   1132 }
   1133 
   1134 #ifdef CRMFB_DEBUG
   1135 void
   1136 crmfb_test_mte(struct crmfb_softc *sc)
   1137 {
   1138 	uint64_t addr, reg;
   1139 	uint32_t addrs[256];
   1140 	int i, j, boo;
   1141 
   1142 	crmfb_wait_idle(sc);
   1143 	addr = (uint64_t)(DMAADDR(sc->sc_dma) + sc->sc_fbsize);
   1144 	addr = addr >> 12;
   1145 	for (i = 0; i < 64; i += 8) {
   1146 #if 1
   1147 		reg = (addr << 32) | (addr + 1) | 0x8000000080000000LL;
   1148 		bus_space_write_8(sc->sc_iot, sc->sc_reh,
   1149 		    CRIME_RE_LINEAR_A + i, reg);
   1150 		printf(" %08x", (uint32_t)(addr & 0xffffffff));
   1151 #endif
   1152 		addr += 2;
   1153 	}
   1154 	printf("\n");
   1155 	memset(sc->sc_scratch, 4, 0x10000);
   1156 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC,
   1157 	    DE_MODE_LIN_A | DE_MODE_BUFDEPTH_8 | DE_MODE_TYPE_CI |
   1158 	    DE_MODE_PIXDEPTH_8);
   1159 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STRD_SRC, 1);
   1160 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
   1161 	    DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK | DE_DRAWMODE_ROP |
   1162 	    DE_DRAWMODE_XFER_EN);
   1163 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, 3);
   1164 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
   1165 	    DE_PRIM_RECTANGLE | DE_PRIM_TB);
   1166 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC, 0);
   1167 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
   1168 	    0x02000000);
   1169 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1170 	    CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
   1171 	    0x04000100);
   1172 
   1173 	crmfb_wait_idle(sc);
   1174 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC,
   1175 	    DE_MODE_TLB_A | DE_MODE_BUFDEPTH_8 | DE_MODE_TYPE_CI |
   1176 	    DE_MODE_PIXDEPTH_8);
   1177 
   1178 #if 1
   1179 	delay(4000000);
   1180 
   1181 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_BG, 0x15151515);
   1182 	crmfb_wait_idle(sc);
   1183 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
   1184 	    MTE_MODE_DST_ECC |
   1185 	    (MTE_TLB_LIN_A << MTE_DST_TLB_SHIFT) |
   1186 	    (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
   1187 	    (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
   1188 	    0/*MTE_MODE_COPY*/);
   1189 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_STRIDE, 1);
   1190 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_STRIDE, 1);
   1191 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC0, 0x00000000);
   1192 	//bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC1, 0x01000100);
   1193 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST0, 0x00000000);
   1194 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST1 |
   1195 	    CRIME_DE_START, 0x00010000);
   1196 	//status[9] = bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STATUS);
   1197 	crmfb_wait_idle(sc);
   1198 	/* now look for 0x05050505 in RAM */
   1199 
   1200 	boo = 0;
   1201 	for (i = 0xA0000000; i < 0xB0000000; i += 0x1000)
   1202 		if (*((uint32_t *)i) == 0x15151515) {
   1203 			/* see if there's more */
   1204 			j = 4;
   1205 			while ((j < 0x100) && (*((uint32_t *)(i + j)) ==
   1206 			    0x15151515))
   1207 				j += 4;
   1208 			if (j > 0x20) {
   1209 				addrs[boo] = i;
   1210 				boo++;
   1211 			}
   1212 		}
   1213 	printf("...");
   1214 	for (i = 0; i < boo; i++)
   1215 		printf(" %08x", addrs[i]);
   1216 	printf("\n");
   1217 #endif
   1218 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC,
   1219 	    DE_MODE_LIN_A | DE_MODE_BUFDEPTH_8 | DE_MODE_TYPE_CI |
   1220 	    DE_MODE_PIXDEPTH_8);
   1221 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STRD_SRC, 1);
   1222 	crmfb_bitblt(sc, 0, 0, 400, 0, 512, 64, 3);
   1223 	crmfb_wait_idle(sc);
   1224 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC,
   1225 	    DE_MODE_TLB_A | DE_MODE_BUFDEPTH_8 | DE_MODE_TYPE_CI |
   1226 	    DE_MODE_PIXDEPTH_8);
   1227 #if 0
   1228 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
   1229 	    MTE_MODE_DST_ECC |
   1230 	    (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
   1231 	    (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
   1232 	    (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
   1233 	    0/*MTE_MODE_COPY*/);
   1234 #endif
   1235 	delay(4000000);
   1236 #if 0
   1237 	for (i = 0; i < 128; i+=8)
   1238 		printf("%016llx\n", bus_space_read_8(sc->sc_iot, sc->sc_reh,
   1239 		    CRIME_RE_LINEAR_B + i));
   1240 #endif
   1241 #if 0
   1242 	printf("flush: %08x\n",
   1243 	    bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FLUSH));
   1244 #endif
   1245 }
   1246 #endif /* CRMFB_DEBUG */
   1247 
   1248 static void
   1249 crmfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1250 {
   1251 	struct rasops_info *ri = cookie;
   1252 	struct vcons_screen *scr = ri->ri_hw;
   1253 	int32_t xs, xd, y, width, height;
   1254 
   1255 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1256 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1257 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1258 	width = ri->ri_font->fontwidth * ncols;
   1259 	height = ri->ri_font->fontheight;
   1260 	crmfb_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, 3);
   1261 }
   1262 
   1263 static void
   1264 crmfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1265 {
   1266 	struct rasops_info *ri = cookie;
   1267 	struct vcons_screen *scr = ri->ri_hw;
   1268 	int32_t x, y, width, height, bg;
   1269 
   1270 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1271 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1272 	width = ri->ri_font->fontwidth * ncols;
   1273 	height = ri->ri_font->fontheight;
   1274 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1275 	crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
   1276 }
   1277 
   1278 static void
   1279 crmfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1280 {
   1281 	struct rasops_info *ri = cookie;
   1282 	struct vcons_screen *scr = ri->ri_hw;
   1283 	int32_t x, ys, yd, width, height;
   1284 
   1285 	x = ri->ri_xorigin;
   1286 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1287 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1288 	width = ri->ri_emuwidth;
   1289 	height = ri->ri_font->fontheight * nrows;
   1290 
   1291 	/* for now we can only use the MTE for scrolling up */
   1292 	if (ys > yd) {
   1293 		crmfb_scroll(scr->scr_cookie, x, ys, x, yd, width, height);
   1294 	} else {
   1295 		crmfb_bitblt(scr->scr_cookie, x, ys, x, yd, width, height, 3);
   1296 	}
   1297 }
   1298 
   1299 static void
   1300 crmfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1301 {
   1302 	struct rasops_info *ri = cookie;
   1303 	struct vcons_screen *scr = ri->ri_hw;
   1304 	int32_t x, y, width, height, bg;
   1305 
   1306 	if ((row == 0) && (nrows == ri->ri_rows)) {
   1307 		x = y = 0;
   1308 		width = ri->ri_width;
   1309 		height = ri->ri_height;
   1310 	} else {
   1311 		x = ri->ri_xorigin;
   1312 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1313 		width = ri->ri_emuwidth;
   1314 		height = ri->ri_font->fontheight * nrows;
   1315 	}
   1316 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1317 	crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
   1318 }
   1319 
   1320 static void
   1321 crmfb_cursor(void *cookie, int on, int row, int col)
   1322 {
   1323 	struct rasops_info *ri = cookie;
   1324 	struct vcons_screen *scr = ri->ri_hw;
   1325 	struct crmfb_softc *sc = scr->scr_cookie;
   1326 	int x, y, wi,he;
   1327 
   1328 	wi = ri->ri_font->fontwidth;
   1329 	he = ri->ri_font->fontheight;
   1330 
   1331 	if (ri->ri_flg & RI_CURSOR) {
   1332 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1333 		y = ri->ri_crow * he + ri->ri_yorigin;
   1334 		crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
   1335 		ri->ri_flg &= ~RI_CURSOR;
   1336 	}
   1337 
   1338 	ri->ri_crow = row;
   1339 	ri->ri_ccol = col;
   1340 
   1341 	if (on)
   1342 	{
   1343 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1344 		y = ri->ri_crow * he + ri->ri_yorigin;
   1345 		crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
   1346 		ri->ri_flg |= RI_CURSOR;
   1347 	}
   1348 }
   1349 
   1350 static void
   1351 crmfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1352 {
   1353 	struct rasops_info *ri = cookie;
   1354 	struct vcons_screen *scr = ri->ri_hw;
   1355 	struct crmfb_softc *sc = scr->scr_cookie;
   1356 	struct rasops_info *fri = &sc->sc_rasops;
   1357 	int bg;
   1358 	int x, y, wi, he, xs;
   1359 
   1360 	wi = ri->ri_font->fontwidth;
   1361 	he = ri->ri_font->fontheight;
   1362 
   1363 	x = ri->ri_xorigin + col * wi;
   1364 	y = ri->ri_yorigin + row * he;
   1365 
   1366 	bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xff];
   1367 	if (c == 0x20) {
   1368 		crmfb_fill_rect(sc, x, y, wi, he, bg);
   1369 	} else {
   1370 		/*
   1371 		 * we rotate over all available character cells in the scratch
   1372 		 * tile. The idea is to have more cells than there's room for
   1373 		 * drawing commands in the engine's pipeline so we don't have
   1374 		 * to wait for the engine until we're done drawing the
   1375 		 * character and ready to blit it into place
   1376 		 */
   1377 		fri->ri_ops.putchar(fri, 0, sc->sc_current_cell, c, attr);
   1378 		xs = sc->sc_current_cell * wi;
   1379 		sc->sc_current_cell++;
   1380 		if (sc->sc_current_cell >= sc->sc_cells)
   1381 			sc->sc_current_cell = 0;
   1382 		crmfb_bitblt(sc, xs, 2048-128, x, y, wi, he, 3);
   1383 	}
   1384 }
   1385