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