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