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