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