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