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