Home | History | Annotate | Line # | Download | only in dev
crmfb.c revision 1.10
      1 /* $NetBSD: crmfb.c,v 1.10 2008/02/02 04:38:37 sekiya Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *        This product includes software developed by Jared D. McNeill.
     18  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19  *    contributors may be used to endorse or promote products derived
     20  *    from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * SGI-CRM (O2) Framebuffer driver
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.10 2008/02/02 04:38:37 sekiya Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 #include <sys/malloc.h>
     46 
     47 #define _SGIMIPS_BUS_DMA_PRIVATE
     48 #include <machine/autoconf.h>
     49 #include <machine/bus.h>
     50 #include <machine/machtype.h>
     51 #include <machine/vmparam.h>
     52 
     53 #include <dev/arcbios/arcbios.h>
     54 #include <dev/arcbios/arcbiosvar.h>
     55 
     56 #include <dev/wscons/wsdisplayvar.h>
     57 #include <dev/wscons/wsconsio.h>
     58 #include <dev/wsfont/wsfont.h>
     59 #include <dev/rasops/rasops.h>
     60 #include <dev/wscons/wsdisplay_vconsvar.h>
     61 
     62 #include <arch/sgimips/dev/crmfbreg.h>
     63 
     64 #define CRMFB_SHADOWFB
     65 
     66 struct wsscreen_descr crmfb_defaultscreen = {
     67 	"default",
     68 	0, 0,
     69 	NULL,
     70 	8, 16,
     71 	WSSCREEN_WSCOLORS,
     72 	NULL,
     73 };
     74 
     75 const struct wsscreen_descr *_crmfb_scrlist[] = {
     76 	&crmfb_defaultscreen,
     77 };
     78 
     79 struct wsscreen_list crmfb_screenlist = {
     80 	sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
     81 	_crmfb_scrlist
     82 };
     83 
     84 static struct vcons_screen crmfb_console_screen;
     85 
     86 static int	crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     87 static paddr_t	crmfb_mmap(void *, void *, off_t, int);
     88 static void	crmfb_init_screen(void *, struct vcons_screen *, int, long *);
     89 
     90 struct wsdisplay_accessops crmfb_accessops = {
     91 	crmfb_ioctl,
     92 	crmfb_mmap,
     93 	NULL,	/* alloc_screen */
     94 	NULL,	/* free_screen */
     95 	NULL,	/* show_screen */
     96 	NULL,	/* load_font */
     97 	NULL,	/* pollc */
     98 	NULL,	/* scroll */
     99 };
    100 
    101 /* Memory to allocate to SGI-CRM -- remember, this is stolen from
    102  * host memory!
    103  */
    104 #define CRMFB_TILESIZE	(512*128)
    105 
    106 static int	crmfb_match(struct device *, struct cfdata *, void *);
    107 static void	crmfb_attach(struct device *, struct device *, void *);
    108 int		crmfb_probe(void);
    109 
    110 #define KERNADDR(p)	((void *)((p).addr))
    111 #define DMAADDR(p)	((p).map->dm_segs[0].ds_addr)
    112 
    113 #define CRMFB_REG_MASK(msb, lsb) \
    114 	( (((uint32_t) 1 << ((msb)-(lsb)+1)) - 1) << (lsb) )
    115 
    116 
    117 struct crmfb_dma {
    118 	bus_dmamap_t		map;
    119 	void			*addr;
    120 	bus_dma_segment_t	segs[1];
    121 	int			nsegs;
    122 	size_t			size;
    123 };
    124 
    125 struct crmfb_softc {
    126 	struct device		sc_dev;
    127 	struct vcons_data	sc_vd;
    128 
    129 	bus_space_tag_t		sc_iot;
    130 	bus_space_handle_t	sc_ioh;
    131 
    132 	bus_dma_tag_t		sc_dmat;
    133 
    134 	struct crmfb_dma	sc_dma;
    135 	struct crmfb_dma	sc_dmai;
    136 
    137 	int			sc_width;
    138 	int			sc_height;
    139 	int			sc_depth;
    140 	uint32_t		sc_fbsize;
    141 	uint8_t			*sc_shadowfb;
    142 
    143 	int			sc_wsmode;
    144 
    145 	/* cursor stuff */
    146 	int			sc_cur_x;
    147 	int			sc_cur_y;
    148 	int			sc_hot_x;
    149 	int			sc_hot_y;
    150 
    151 	u_char			sc_cmap_red[256];
    152 	u_char			sc_cmap_green[256];
    153 	u_char			sc_cmap_blue[256];
    154 };
    155 
    156 static int	crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    157 static int	crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    158 static void	crmfb_set_palette(struct crmfb_softc *,
    159 				  int, uint8_t, uint8_t, uint8_t);
    160 static int	crmfb_set_curpos(struct crmfb_softc *, int, int);
    161 static int	crmfb_gcursor(struct crmfb_softc *, struct wsdisplay_cursor *);
    162 static int	crmfb_scursor(struct crmfb_softc *, struct wsdisplay_cursor *);
    163 static inline void	crmfb_write_reg(struct crmfb_softc *, int, uint32_t);
    164 
    165 /* setup video hw in given colour depth */
    166 static int	crmfb_setup_video(struct crmfb_softc *, int);
    167 static void	crmfb_setup_palette(struct crmfb_softc *);
    168 
    169 CFATTACH_DECL(crmfb, sizeof(struct crmfb_softc),
    170     crmfb_match, crmfb_attach, NULL, NULL);
    171 
    172 static int
    173 crmfb_match(struct device *parent, struct cfdata *cf, void *opaque)
    174 {
    175 	return crmfb_probe();
    176 }
    177 
    178 static void
    179 crmfb_attach(struct device *parent, struct device *self, void *opaque)
    180 {
    181 	struct mainbus_attach_args *ma;
    182 	struct crmfb_softc *sc;
    183 	struct rasops_info *ri;
    184 	struct wsemuldisplaydev_attach_args aa;
    185 	uint32_t d, h;
    186 	uint16_t *p;
    187 	unsigned long v;
    188 	long defattr;
    189 	const char *consdev;
    190 	int rv, i, tiles_x, tiles_y;
    191 
    192 	sc = (struct crmfb_softc *)self;
    193 	ma = (struct mainbus_attach_args *)opaque;
    194 
    195 	sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME;
    196 	sc->sc_dmat = &sgimips_default_bus_dma_tag;
    197 	sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
    198 
    199 	aprint_normal(": SGI CRIME Graphics Display Engine\n");
    200 	rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
    201 	    BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
    202 	if (rv)
    203 		panic("crmfb_attach: can't map I/O space");
    204 
    205 	/* determine mode configured by firmware */
    206 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
    207 	sc->sc_width = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
    208 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
    209 	sc->sc_height = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
    210 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    211 	h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
    212 	if (h == 0)
    213 		sc->sc_depth = 8;
    214 	else if (h == 1)
    215 		sc->sc_depth = 16;
    216 	else
    217 		sc->sc_depth = 32;
    218 
    219 	if (sc->sc_width == 0 || sc->sc_height == 0) {
    220 		printf("%s: device unusable if not setup by firmware\n",
    221 		    sc->sc_dev.dv_xname);
    222 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0 /* XXX */);
    223 		return;
    224 	}
    225 
    226 	printf("%s: initial resolution %dx%d\n",
    227 	    sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height);
    228 
    229 	/*
    230 	 * first determine how many tiles we need
    231 	 * in 32bit each tile is 128x128 pixels
    232 	 */
    233 	tiles_x = (sc->sc_width + 127) >> 7;
    234 	tiles_y = (sc->sc_height + 127) >> 7;
    235 	sc->sc_fbsize = 0x10000 * tiles_x * tiles_y;
    236 	printf("so we need %d x %d tiles -> %08x\n", tiles_x, tiles_y,
    237 	    sc->sc_fbsize);
    238 
    239 	sc->sc_dmai.size = 256 * sizeof(uint16_t);
    240 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
    241 	    sc->sc_dmai.segs,
    242 	    sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
    243 	    &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
    244 	if (rv)
    245 		panic("crmfb_attach: can't allocate DMA memory");
    246 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
    247 	    sc->sc_dmai.size, &sc->sc_dmai.addr,
    248 	    BUS_DMA_NOWAIT);
    249 	if (rv)
    250 		panic("crmfb_attach: can't map DMA memory");
    251 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
    252 	    sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
    253 	if (rv)
    254 		panic("crmfb_attach: can't create DMA map");
    255 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
    256 	    sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
    257 	if (rv)
    258 		panic("crmfb_attach: can't load DMA map");
    259 
    260 	sc->sc_dma.size = sc->sc_fbsize;
    261 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
    262 	    sc->sc_dma.segs,
    263 	    sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
    264 	    &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
    265 	if (rv)
    266 		panic("crmfb_attach: can't allocate DMA memory");
    267 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
    268 	    sc->sc_dma.size, &sc->sc_dma.addr,
    269 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    270 	if (rv)
    271 		panic("crmfb_attach: can't map DMA memory");
    272 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
    273 	    sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
    274 	if (rv)
    275 		panic("crmfb_attach: can't create DMA map");
    276 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
    277 	    sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
    278 	if (rv)
    279 		panic("crmfb_attach: can't load DMA map");
    280 
    281 	p = KERNADDR(sc->sc_dmai);
    282 	v = (unsigned long)DMAADDR(sc->sc_dma);
    283 	for (i = 0; i < (sc->sc_fbsize >> 16); i++) {
    284 		p[i] = ((uint32_t)v >> 16) + i;
    285 	}
    286 
    287 	memset(KERNADDR(sc->sc_dma), 0x00, sc->sc_fbsize);
    288 
    289 	printf("%s: allocated %d byte fb @ %p (%p)\n", sc->sc_dev.dv_xname,
    290 	    sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
    291 
    292 #ifdef CRMFB_SHADOWFB
    293 	/* setup shadow framebuffer */
    294 	sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_NOWAIT | M_ZERO);
    295 	if (sc->sc_shadowfb == NULL)
    296 		aprint_error("%s: WARNING: couldn't allocate shadow fb\n",
    297 		    sc->sc_dev.dv_xname);
    298 #endif
    299 
    300 	ri = &crmfb_console_screen.scr_ri;
    301 	memset(ri, 0, sizeof(struct rasops_info));
    302 
    303 	vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
    304 	sc->sc_vd.init_screen = crmfb_init_screen;
    305 	crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    306 	vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
    307 
    308 	crmfb_defaultscreen.ncols = ri->ri_cols;
    309 	crmfb_defaultscreen.nrows = ri->ri_rows;
    310 	crmfb_defaultscreen.textops = &ri->ri_ops;
    311 	crmfb_defaultscreen.capabilities = ri->ri_caps;
    312 	crmfb_defaultscreen.modecookie = NULL;
    313 
    314 	crmfb_setup_video(sc, 8);
    315 	crmfb_setup_palette(sc);
    316 
    317 	consdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut");
    318 	if (consdev != NULL && strcmp(consdev, "video()") == 0) {
    319 		wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
    320 		aa.console = 1;
    321 	} else
    322 		aa.console = 0;
    323 	aa.scrdata = &crmfb_screenlist;
    324 	aa.accessops = &crmfb_accessops;
    325 	aa.accesscookie = &sc->sc_vd;
    326 
    327 	config_found(self, &aa, wsemuldisplaydevprint);
    328 
    329 	sc->sc_cur_x = 0;
    330 	sc->sc_cur_y = 0;
    331 	sc->sc_hot_x = 0;
    332 	sc->sc_hot_y = 0;
    333 
    334 	return;
    335 }
    336 
    337 int
    338 crmfb_probe(void)
    339 {
    340 
    341         if (mach_type != MACH_SGI_IP32)
    342                 return 0;
    343 
    344 	return 1;
    345 }
    346 
    347 static int
    348 crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    349 {
    350 	struct vcons_data *vd;
    351 	struct crmfb_softc *sc;
    352 	struct vcons_screen *ms;
    353 	struct wsdisplay_fbinfo *wdf;
    354 	int nmode;
    355 
    356 	vd = (struct vcons_data *)v;
    357 	sc = (struct crmfb_softc *)vd->cookie;
    358 	ms = (struct vcons_screen *)vd->active;
    359 
    360 	switch (cmd) {
    361 	case WSDISPLAYIO_GTYPE:
    362 		/* not really, but who cares? */
    363 		/* wsfb does */
    364 		*(u_int *)data = WSDISPLAY_TYPE_CRIME;
    365 		return 0;
    366 	case WSDISPLAYIO_GINFO:
    367 		if (vd->active != NULL) {
    368 			wdf = (void *)data;
    369 			wdf->height = sc->sc_height;
    370 			wdf->width = sc->sc_width;
    371 			wdf->depth = 32;
    372 			wdf->cmsize = 256;
    373 			return 0;
    374 		} else
    375 			return ENODEV;
    376 	case WSDISPLAYIO_GETCMAP:
    377 		if (sc->sc_depth == 8)
    378 			return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
    379 		else
    380 			return EINVAL;
    381 	case WSDISPLAYIO_PUTCMAP:
    382 		if (sc->sc_depth == 8)
    383 			return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
    384 		else
    385 			return EINVAL;
    386 	case WSDISPLAYIO_LINEBYTES:
    387 		*(u_int *)data = sc->sc_width * sc->sc_depth / 8;
    388 		return 0;
    389 	case WSDISPLAYIO_SMODE:
    390 		nmode = *(int *)data;
    391 		if (nmode != sc->sc_wsmode) {
    392 			sc->sc_wsmode = nmode;
    393 			if (nmode == WSDISPLAYIO_MODE_EMUL) {
    394 				crmfb_setup_video(sc, 8);
    395 				crmfb_setup_palette(sc);
    396 				vcons_redraw_screen(vd->active);
    397 			} else {
    398 				crmfb_setup_video(sc, 32);
    399 			}
    400 		}
    401 		return 0;
    402 	case WSDISPLAYIO_SVIDEO:
    403 	case WSDISPLAYIO_GVIDEO:
    404 		return ENODEV;	/* not supported yet */
    405 
    406 	case WSDISPLAYIO_GCURPOS:
    407 		{
    408 			struct wsdisplay_curpos *pos;
    409 
    410 			pos = (struct wsdisplay_curpos *)data;
    411 			pos->x = sc->sc_cur_x;
    412 			pos->y = sc->sc_cur_y;
    413 		}
    414 		return 0;
    415 	case WSDISPLAYIO_SCURPOS:
    416 		{
    417 			struct wsdisplay_curpos *pos;
    418 
    419 			pos = (struct wsdisplay_curpos *)data;
    420 			crmfb_set_curpos(sc, pos->x, pos->y);
    421 		}
    422 		return 0;
    423 	case WSDISPLAYIO_GCURMAX:
    424 		{
    425 			struct wsdisplay_curpos *pos;
    426 
    427 			pos = (struct wsdisplay_curpos *)data;
    428 			pos->x = 32;
    429 			pos->y = 32;
    430 		}
    431 		return 0;
    432 	case WSDISPLAYIO_GCURSOR:
    433 		{
    434 			struct wsdisplay_cursor *cu;
    435 
    436 			cu = (struct wsdisplay_cursor *)data;
    437 			return crmfb_gcursor(sc, cu);
    438 		}
    439 	case WSDISPLAYIO_SCURSOR:
    440 		{
    441 			struct wsdisplay_cursor *cu;
    442 
    443 			cu = (struct wsdisplay_cursor *)data;
    444 			return crmfb_scursor(sc, cu);
    445 		}
    446 	}
    447 	return EPASSTHROUGH;
    448 }
    449 
    450 static paddr_t
    451 crmfb_mmap(void *v, void *vs, off_t offset, int prot)
    452 {
    453 	struct vcons_data *vd;
    454 	struct crmfb_softc *sc;
    455 	paddr_t pa;
    456 
    457 	vd = (struct vcons_data *)v;
    458 	sc = (struct crmfb_softc *)vd->cookie;
    459 
    460 #if 1
    461 	if (offset >= 0 && offset < sc->sc_fbsize) {
    462 		pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
    463 		    sc->sc_dma.nsegs, offset, prot,
    464 		    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    465 		return pa;
    466 	}
    467 #else
    468 	if (offset >= 0 && offset < sc->sc_fbsize) {
    469 		pa = bus_space_mmap(SGIMIPS_BUS_SPACE_NORMAL, sc->sc_fbh,
    470 		    offset, prot, SGIMIPS_BUS_SPACE_NORMAL);
    471 		printf("%s: %08llx -> %llx\n", __func__, offset, pa);
    472 		return pa;
    473 	}
    474 #endif
    475 	return -1;
    476 }
    477 
    478 static void
    479 crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
    480     long *defattr)
    481 {
    482 	struct crmfb_softc *sc;
    483 	struct rasops_info *ri;
    484 
    485 	sc = (struct crmfb_softc *)c;
    486 	ri = &scr->scr_ri;
    487 
    488 	ri->ri_flg = RI_CENTER;
    489 	ri->ri_depth = sc->sc_depth;
    490 	ri->ri_width = sc->sc_width;
    491 	ri->ri_height = sc->sc_height;
    492 	ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
    493 
    494 	switch (ri->ri_depth) {
    495 	case 16:
    496 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
    497 		ri->ri_rpos = 10;
    498 		ri->ri_gpos = 5;
    499 		ri->ri_bpos = 0;
    500 		break;
    501 	case 32:
    502 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    503 		ri->ri_rpos = 8;
    504 		ri->ri_gpos = 16;
    505 		ri->ri_bpos = 24;
    506 		break;
    507 	}
    508 
    509 	if (sc->sc_shadowfb == NULL)
    510 		ri->ri_bits = KERNADDR(sc->sc_dma);
    511 	else {
    512 		ri->ri_bits = sc->sc_shadowfb;
    513 		ri->ri_hwbits = KERNADDR(sc->sc_dma);
    514 	}
    515 
    516 	if (existing)
    517 		ri->ri_flg |= RI_CLEAR;
    518 
    519 	rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
    520 	ri->ri_caps = WSSCREEN_WSCOLORS;
    521 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    522 	    ri->ri_width / ri->ri_font->fontwidth);
    523 	ri->ri_hw = scr;
    524 
    525 	return;
    526 }
    527 
    528 static int
    529 crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    530 {
    531 	u_int idx, cnt;
    532 	u_char r[256], g[256], b[256];
    533 	u_char *rp, *gp, *bp;
    534 	int rv, i;
    535 
    536 	idx = cm->index;
    537 	cnt = cm->count;
    538 
    539 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    540 		return EINVAL;
    541 
    542 	rv = copyin(cm->red, &r[idx], cnt);
    543 	if (rv)
    544 		return rv;
    545 	rv = copyin(cm->green, &g[idx], cnt);
    546 	if (rv)
    547 		return rv;
    548 	rv = copyin(cm->blue, &b[idx], cnt);
    549 	if (rv)
    550 		return rv;
    551 
    552 	memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
    553 	memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
    554 	memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
    555 
    556 	rp = &sc->sc_cmap_red[idx];
    557 	gp = &sc->sc_cmap_green[idx];
    558 	bp = &sc->sc_cmap_blue[idx];
    559 
    560 	for (i = 0; i < cnt; i++) {
    561 		crmfb_set_palette(sc, idx, *rp, *gp, *bp);
    562 		idx++;
    563 		rp++, gp++, bp++;
    564 	}
    565 
    566 	return 0;
    567 }
    568 
    569 static int
    570 crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    571 {
    572 	u_int idx, cnt;
    573 	int rv;
    574 
    575 	idx = cm->index;
    576 	cnt = cm->count;
    577 
    578 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    579 		return EINVAL;
    580 
    581 	rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
    582 	if (rv)
    583 		return rv;
    584 	rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
    585 	if (rv)
    586 		return rv;
    587 	rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
    588 	if (rv)
    589 		return rv;
    590 
    591 	return 0;
    592 }
    593 
    594 static void
    595 crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
    596     uint8_t b)
    597 {
    598 	uint32_t val;
    599 
    600 	if (reg > 255 || sc->sc_depth != 8)
    601 		return;
    602 
    603 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
    604 		DELAY(10);
    605 
    606 	val = (r << 24) | (g << 16) | (b << 8);
    607 	crmfb_write_reg(sc, CRMFB_CMAP + (reg * 4), val);
    608 
    609 	return;
    610 }
    611 
    612 static int
    613 crmfb_set_curpos(struct crmfb_softc *sc, int x, int y)
    614 {
    615 	uint32_t val;
    616 
    617 	sc->sc_cur_x = x;
    618 	sc->sc_cur_y = y;
    619 
    620 	val = ((x - sc->sc_hot_x) & 0xffff) | ((y - sc->sc_hot_y) << 16);
    621 	crmfb_write_reg(sc, CRMFB_CURSOR_POS, val);
    622 
    623 	return 0;
    624 }
    625 
    626 static int
    627 crmfb_gcursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    628 {
    629 	/* do nothing for now */
    630 	return 0;
    631 }
    632 
    633 static int
    634 crmfb_scursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    635 {
    636 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
    637 
    638 		crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, cur->enable ? 1 : 0);
    639 	}
    640 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
    641 
    642 		sc->sc_hot_x = cur->hot.x;
    643 		sc->sc_hot_y = cur->hot.y;
    644 	}
    645 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
    646 
    647 		crmfb_set_curpos(sc, cur->pos.x, cur->pos.y);
    648 	}
    649 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
    650 		int i;
    651 		uint32_t val;
    652 
    653 		for (i = 0; i < cur->cmap.count; i++) {
    654 			val = (cur->cmap.red[i] << 24) |
    655 			      (cur->cmap.green[i] << 16) |
    656 			      (cur->cmap.blue[i] << 8);
    657 			crmfb_write_reg(sc, CRMFB_CURSOR_CMAP0 +
    658 			    ((i + cur->cmap.index) << 2), val);
    659 		}
    660 	}
    661 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
    662 
    663 		int i, j, cnt = 0;
    664 		uint32_t latch = 0, omask;
    665 		uint8_t imask;
    666 		for (i = 0; i < 64; i++) {
    667 			omask = 0x80000000;
    668 			imask = 0x01;
    669 			cur->image[cnt] &= cur->mask[cnt];
    670 			for (j = 0; j < 8; j++) {
    671 				if (cur->image[cnt] & imask)
    672 					latch |= omask;
    673 				omask >>= 1;
    674 				if (cur->mask[cnt] & imask)
    675 					latch |= omask;
    676 				omask >>= 1;
    677 				imask <<= 1;
    678 			}
    679 			cnt++;
    680 			imask = 0x01;
    681 			cur->image[cnt] &= cur->mask[cnt];
    682 			for (j = 0; j < 8; j++) {
    683 				if (cur->image[cnt] & imask)
    684 					latch |= omask;
    685 				omask >>= 1;
    686 				if (cur->mask[cnt] & imask)
    687 					latch |= omask;
    688 				omask >>= 1;
    689 				imask <<= 1;
    690 			}
    691 			cnt++;
    692 			crmfb_write_reg(sc, CRMFB_CURSOR_BITMAP + (i << 2),
    693 			    latch);
    694 			latch = 0;
    695 		}
    696 	}
    697 	return 0;
    698 }
    699 
    700 static inline void
    701 crmfb_write_reg(struct crmfb_softc *sc, int offset, uint32_t val)
    702 {
    703 
    704 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
    705 	wbflush();
    706 }
    707 
    708 static int
    709 crmfb_setup_video(struct crmfb_softc *sc, int depth)
    710 {
    711 	uint32_t d, h;
    712 	int i;
    713 	const char *wantsync;
    714 
    715 	/* disable DMA */
    716 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
    717 	d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
    718 	crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d);
    719 	DELAY(50000);
    720 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    721 	d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    722 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    723 	DELAY(50000);
    724 	crmfb_write_reg(sc, CRMFB_DID_CONTROL, 0);
    725 	DELAY(50000);
    726 
    727 	/* ensure that CRM starts drawing at the top left of the screen
    728 	 * when we re-enable DMA later
    729 	 */
    730 	d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
    731 	crmfb_write_reg(sc, CRMFB_VT_XY, d);
    732 	delay(1000);
    733 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    734 	d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    735 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
    736 	delay(1000);
    737 
    738 	/* reset FIFO */
    739 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    740 	d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    741 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    742 	d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    743 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    744 
    745 	/* setup colour mode */
    746 	switch (depth) {
    747 	case 8:
    748 		h = CRMFB_MODE_TYP_I8;
    749 		break;
    750 	case 16:
    751 		h = CRMFB_MODE_TYP_ARGB5;
    752 		break;
    753 	case 32:
    754 		h = CRMFB_MODE_TYP_RGB8;
    755 		break;
    756 	default:
    757 		panic("Unsupported depth");
    758 	}
    759 	d = h << CRMFB_MODE_TYP_SHIFT;
    760 	d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
    761 	for (i = 0; i < (32 * 4); i += 4)
    762 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
    763 	wbflush();
    764 
    765 	/* setup tile pointer, but don't turn on DMA yet! */
    766 	h = DMAADDR(sc->sc_dmai);
    767 	d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
    768 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    769 
    770 	/* init framebuffer width and pixel size */
    771 	d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);
    772 	switch (depth) {
    773 	case 8:
    774 		h = CRMFB_FRM_TILESIZE_DEPTH_8;
    775 		break;
    776 	case 16:
    777 		h = CRMFB_FRM_TILESIZE_DEPTH_16;
    778 		break;
    779 	case 32:
    780 		h = CRMFB_FRM_TILESIZE_DEPTH_32;
    781 		break;
    782 	default:
    783 		panic("Unsupported depth");
    784 	}
    785 	d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
    786 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    787 
    788 	/* init framebuffer height, we use the trick that the Linux
    789 	 * driver uses to fool the CRM out of tiled mode and into
    790 	 * linear mode
    791 	 */
    792 	h = sc->sc_width * sc->sc_height / (512 / (depth >> 3));
    793 	d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
    794 	crmfb_write_reg(sc, CRMFB_FRM_PIXSIZE, d);
    795 
    796 	/* turn off firmware overlay and hardware cursor */
    797 	crmfb_write_reg(sc, CRMFB_OVR_WIDTH_TILE, 0);
    798 	crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, 0);
    799 
    800 	/* enable drawing again */
    801 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    802 	d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    803 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
    804 	crmfb_write_reg(sc, CRMFB_VT_XY, 0);
    805 
    806 	/* turn on DMA for the framebuffer */
    807 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    808 	d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    809 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    810 
    811 	/* turn off sync-on-green */
    812 
    813 	wantsync = ARCBIOS->GetEnvironmentVariable("SyncOnGreen");
    814 	if ( (wantsync != NULL) && (wantsync[0] == 'n') ) {
    815 		d = ( 1 << CRMFB_VT_FLAGS_SYNC_LOW_LSB) & CRMFB_REG_MASK(CRMFB_VT_FLAGS_SYNC_LOW_MSB, CRMFB_VT_FLAGS_SYNC_LOW_LSB);
    816 		crmfb_write_reg(sc, CRMFB_VT_FLAGS, d);
    817 	}
    818 
    819 	sc->sc_depth = depth;
    820 	return 0;
    821 }
    822 
    823 static void
    824 crmfb_setup_palette(struct crmfb_softc *sc)
    825 {
    826 	int i;
    827 
    828 	for (i = 0; i < 256; i++) {
    829 		crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
    830 		    rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
    831 		sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
    832 		sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
    833 		sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
    834 	}
    835 }
    836