Home | History | Annotate | Line # | Download | only in dev
crmfb.c revision 1.3
      1 /* $NetBSD: crmfb.c,v 1.3 2007/04/13 12:21:00 jmcneill 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.3 2007/04/13 12:21:00 jmcneill 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 #define CRMFB_SHADOWFB
     63 
     64 struct wsscreen_descr crmfb_defaultscreen = {
     65 	"default",
     66 	0, 0,
     67 	NULL,
     68 	8, 16,
     69 	WSSCREEN_WSCOLORS,
     70 	NULL,
     71 };
     72 
     73 const struct wsscreen_descr *_crmfb_scrlist[] = {
     74 	&crmfb_defaultscreen,
     75 };
     76 
     77 struct wsscreen_list crmfb_screenlist = {
     78 	sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
     79 	_crmfb_scrlist
     80 };
     81 
     82 static struct vcons_screen crmfb_console_screen;
     83 
     84 static int	crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     85 static paddr_t	crmfb_mmap(void *, void *, off_t, int);
     86 static void	crmfb_init_screen(void *, struct vcons_screen *, int, long *);
     87 
     88 struct wsdisplay_accessops crmfb_accessops = {
     89 	crmfb_ioctl,
     90 	crmfb_mmap,
     91 	NULL,	/* alloc_screen */
     92 	NULL,	/* free_screen */
     93 	NULL,	/* show_screen */
     94 	NULL,	/* load_font */
     95 	NULL,	/* pollc */
     96 	NULL,	/* scroll */
     97 };
     98 
     99 /* Memory to allocate to SGI-CRM -- remember, this is stolen from
    100  * host memory!
    101  */
    102 #define CRMFB_TILESIZE	(512*128)
    103 
    104 #define CRMFB_DOTCLOCK		0x00000004
    105 #define		CRMFB_DOTCLOCK_CLKRUN_SHIFT	20
    106 #define CRMFB_VT_XY		0x00010000
    107 #define		CRMFB_VT_XY_FREEZE_SHIFT	31
    108 #define CRMFB_VT_INTR01		0x00010020
    109 #define		CRMFB_VT_INTR01_EN		0xffffffff
    110 #define CRMFB_VT_INTR23		0x00010024
    111 #define		CRMFB_VT_INTR23_EN		0xffffffff
    112 #define CRMFB_VT_VPIX_EN	0x00010038
    113 #define		CRMFB_VT_VPIX_EN_OFF_SHIFT	0
    114 #define CRMFB_VT_VCMAP		0x0001003c
    115 #define		CRMFB_VT_VCMAP_ON_SHIFT		12
    116 #define CRMFB_VT_HCMAP		0x00010040
    117 #define		CRMFB_VT_HCMAP_ON_SHIFT		12
    118 #define CRMFB_OVR_WIDTH_TILE	0x00020000
    119 #define CRMFB_OVR_CONTROL	0x00020008
    120 #define		CRMFB_OVR_CONTROL_DMAEN_SHIFT	0
    121 #define CRMFB_FRM_TILESIZE	0x00030000
    122 #define		CRMFB_FRM_TILESIZE_RHS_SHIFT	0
    123 #define		CRMFB_FRM_TILESIZE_WIDTH_SHIFT	5
    124 #define		CRMFB_FRM_TILESIZE_DEPTH_SHIFT	13
    125 #define			CRMFB_FRM_TILESIZE_DEPTH_8	0
    126 #define			CRMFB_FRM_TILESIZE_DEPTH_16	1
    127 #define			CRMFB_FRM_TILESIZE_DEPTH_32	2
    128 #define		CRMFB_FRM_TILESIZE_FIFOR_SHIFT	15
    129 #define CRMFB_FRM_PIXSIZE	0x00030004
    130 #define		CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT	16
    131 #define CRMFB_FRM_CONTROL	0x0003000c
    132 #define		CRMFB_FRM_CONTROL_DMAEN_SHIFT	0
    133 #define		CRMFB_FRM_CONTROL_LINEAR_SHIFT	1
    134 #define		CRMFB_FRM_CONTROL_TILEPTR_SHIFT	9
    135 #define CRMFB_DID_CONTROL	0x00040004
    136 #define		CRMFB_DID_CONTROL_DMAEN_SHIFT	0
    137 #define CRMFB_MODE		0x00048000
    138 #define		CRMFB_MODE_TYP_SHIFT		2
    139 #define			CRMFB_MODE_TYP_I8	0
    140 #define			CRMFB_MODE_TYP_ARGB5	4
    141 #define			CRMFB_MODE_TYP_RGB8	5
    142 #define		CRMFB_MODE_BUF_SHIFT		0
    143 #define			CRMFB_MODE_BUF_BOTH	3
    144 #define CRMFB_CMAP		0x00050000
    145 #define CRMFB_CMAP_FIFO		0x00058000
    146 #define CRMFB_GMAP		0x00060000
    147 #define CRMFB_CRS_CONTROL	0x00070004
    148 
    149 static int	crmfb_match(struct device *, struct cfdata *, void *);
    150 static void	crmfb_attach(struct device *, struct device *, void *);
    151 int		crmfb_probe(void);
    152 
    153 #define KERNADDR(p)	((void *)((p).addr))
    154 #define DMAADDR(p)	((p).map->dm_segs[0].ds_addr)
    155 
    156 struct crmfb_dma {
    157 	bus_dmamap_t		map;
    158 	void			*addr;
    159 	bus_dma_segment_t	segs[1];
    160 	int			nsegs;
    161 	size_t			size;
    162 };
    163 
    164 struct crmfb_softc {
    165 	struct device		sc_dev;
    166 	struct vcons_data	sc_vd;
    167 
    168 	bus_space_tag_t		sc_iot;
    169 	bus_space_handle_t	sc_ioh;
    170 	bus_dma_tag_t		sc_dmat;
    171 
    172 	struct crmfb_dma	sc_dma;
    173 	struct crmfb_dma	sc_dmai;
    174 
    175 	int			sc_width;
    176 	int			sc_height;
    177 	int			sc_depth;
    178 	uint32_t		sc_fbsize;
    179 	uint8_t			*sc_shadowfb;
    180 
    181 	int			sc_wsmode;
    182 	u_char			sc_cmap_red[256];
    183 	u_char			sc_cmap_green[256];
    184 	u_char			sc_cmap_blue[256];
    185 };
    186 
    187 static int	crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    188 static int	crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    189 static void	crmfb_set_palette(struct crmfb_softc *,
    190 				  int, uint8_t, uint8_t, uint8_t);
    191 
    192 CFATTACH_DECL(crmfb, sizeof(struct crmfb_softc),
    193     crmfb_match, crmfb_attach, NULL, NULL);
    194 
    195 static int
    196 crmfb_match(struct device *parent, struct cfdata *cf, void *opaque)
    197 {
    198 	return crmfb_probe();
    199 }
    200 
    201 static void
    202 crmfb_attach(struct device *parent, struct device *self, void *opaque)
    203 {
    204 	struct mainbus_attach_args *ma;
    205 	struct crmfb_softc *sc;
    206 	struct rasops_info *ri;
    207 	struct wsemuldisplaydev_attach_args aa;
    208 	uint32_t d, h;
    209 	uint16_t *p;
    210 	unsigned long v;
    211 	long defattr;
    212 	const char *consdev;
    213 	int rv, i;
    214 
    215 	sc = (struct crmfb_softc *)self;
    216 	ma = (struct mainbus_attach_args *)opaque;
    217 
    218 	sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME;
    219 	sc->sc_dmat = &sgimips_default_bus_dma_tag;
    220 	sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
    221 
    222 	aprint_normal(": SGI CRIME Graphics Display Engine\n");
    223 	rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
    224 	    BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
    225 	if (rv)
    226 		panic("crmfb_attach: can't map I/O space");
    227 
    228 	/* determine mode configured by firmware */
    229 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
    230 	sc->sc_width = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
    231 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
    232 	sc->sc_height = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
    233 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    234 	h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
    235 	if (h == 0)
    236 		sc->sc_depth = 8;
    237 	else if (h == 1)
    238 		sc->sc_depth = 16;
    239 	else
    240 		sc->sc_depth = 32;
    241 
    242 	printf("%s: initial resolution %dx%d %d bpp\n",
    243 	    sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
    244 
    245 #if 0
    246 	/* Changing the colour depth is easy... */
    247 	sc->sc_depth = 32;
    248 	printf("%s: using resolution %dx%d %d bpp\n",
    249 	    sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
    250 #endif
    251 
    252 	sc->sc_fbsize = sc->sc_width * sc->sc_height * sc->sc_depth / 8;
    253 	sc->sc_fbsize = (sc->sc_fbsize / CRMFB_TILESIZE) * CRMFB_TILESIZE;
    254 
    255 	sc->sc_dmai.size = 128 * sizeof(uint16_t);
    256 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
    257 	    sc->sc_dmai.segs,
    258 	    sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
    259 	    &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
    260 	if (rv)
    261 		panic("crmfb_attach: can't allocate DMA memory");
    262 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
    263 	    sc->sc_dmai.size, &sc->sc_dmai.addr,
    264 	    BUS_DMA_NOWAIT);
    265 	if (rv)
    266 		panic("crmfb_attach: can't map DMA memory");
    267 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
    268 	    sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
    269 	if (rv)
    270 		panic("crmfb_attach: can't create DMA map");
    271 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
    272 	    sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
    273 	if (rv)
    274 		panic("crmfb_attach: can't load DMA map");
    275 
    276 	sc->sc_dma.size = sc->sc_fbsize;
    277 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
    278 	    sc->sc_dma.segs,
    279 	    sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
    280 	    &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
    281 	if (rv)
    282 		panic("crmfb_attach: can't allocate DMA memory");
    283 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
    284 	    sc->sc_dma.size, &sc->sc_dma.addr,
    285 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    286 	if (rv)
    287 		panic("crmfb_attach: can't map DMA memory");
    288 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
    289 	    sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
    290 	if (rv)
    291 		panic("crmfb_attach: can't create DMA map");
    292 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
    293 	    sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
    294 	if (rv)
    295 		panic("crmfb_attach: can't load DMA map");
    296 
    297 	p = KERNADDR(sc->sc_dmai);
    298 	v = (unsigned long)DMAADDR(sc->sc_dma);
    299 	for (i = 0; i < (sc->sc_fbsize >> 16); i++) {
    300 		p[i] = ((uint32_t)v >> 16) + i;
    301 	}
    302 
    303 	memset(KERNADDR(sc->sc_dma), 0x00, sc->sc_fbsize);
    304 
    305 	printf("%s: allocated %d byte fb @ %p (%p)\n", sc->sc_dev.dv_xname,
    306 	    sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
    307 
    308 #ifdef CRMFB_SHADOWFB
    309 	/* setup shadow framebuffer */
    310 	sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_NOWAIT | M_ZERO);
    311 	if (sc->sc_shadowfb == NULL)
    312 		aprint_error("%s: WARNING: couldn't allocate shadow fb\n",
    313 		    sc->sc_dev.dv_xname);
    314 #endif
    315 
    316 	ri = &crmfb_console_screen.scr_ri;
    317 	memset(ri, 0, sizeof(struct rasops_info));
    318 
    319 	vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
    320 	sc->sc_vd.init_screen = crmfb_init_screen;
    321 	crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    322 	vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
    323 
    324 	crmfb_defaultscreen.ncols = ri->ri_cols;
    325 	crmfb_defaultscreen.nrows = ri->ri_rows;
    326 	crmfb_defaultscreen.textops = &ri->ri_ops;
    327 	crmfb_defaultscreen.capabilities = ri->ri_caps;
    328 	crmfb_defaultscreen.modecookie = NULL;
    329 
    330 	/* disable DMA */
    331 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
    332 	d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
    333 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL, d);
    334 	DELAY(10000);
    335 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    336 	d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    337 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
    338 	DELAY(10000);
    339 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DID_CONTROL, 0);
    340 	DELAY(10000);
    341 
    342 	/* ensure that CRM starts drawing at the top left of the screen
    343 	 * when we re-enable DMA later
    344 	 */
    345 	d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
    346 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, d);
    347 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    348 	d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    349 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
    350 
    351 	/* reset FIFO */
    352 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    353 	d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    354 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
    355 	d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    356 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
    357 
    358 	/* setup colour mode */
    359 	switch (sc->sc_depth) {
    360 	case 8:
    361 		h = CRMFB_MODE_TYP_I8;
    362 		break;
    363 	case 16:
    364 		h = CRMFB_MODE_TYP_ARGB5;
    365 		break;
    366 	case 32:
    367 		h = CRMFB_MODE_TYP_RGB8;
    368 		break;
    369 	default:
    370 		panic("Unsupported depth");
    371 	}
    372 	d = h << CRMFB_MODE_TYP_SHIFT;
    373 	d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
    374 	for (i = 0; i < (32 * 4); i += 4)
    375 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
    376 
    377 	/* setup tile pointer, but don't turn on DMA yet! */
    378 	h = DMAADDR(sc->sc_dmai);
    379 	d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
    380 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
    381 
    382 	/* init framebuffer width and pixel size */
    383 	d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);
    384 	switch (sc->sc_depth) {
    385 	case 8:
    386 		h = CRMFB_FRM_TILESIZE_DEPTH_8;
    387 		break;
    388 	case 16:
    389 		h = CRMFB_FRM_TILESIZE_DEPTH_16;
    390 		break;
    391 	case 32:
    392 		h = CRMFB_FRM_TILESIZE_DEPTH_32;
    393 		break;
    394 	default:
    395 		panic("Unsupported depth");
    396 	}
    397 	d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
    398 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
    399 
    400 	/* init framebuffer height, we use the trick that the Linux
    401 	 * driver uses to fool the CRM out of tiled mode and into
    402 	 * linear mode
    403 	 */
    404 	h = sc->sc_width * sc->sc_height / (512 / (sc->sc_depth / 8));
    405 	d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
    406 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_PIXSIZE, d);
    407 
    408 	/* turn off firmware overlay and hardware cursor */
    409 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_WIDTH_TILE, 0);
    410 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CRS_CONTROL, 0);
    411 
    412 	/* enable drawing again */
    413 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    414 	d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    415 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
    416 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, 0);
    417 
    418 	/* turn on DMA for the framebuffer */
    419 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    420 	d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    421 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
    422 
    423 	for (i = 0; i < 256; i++) {
    424 		crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
    425 		    rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
    426 		sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
    427 		sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
    428 		sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
    429 	}
    430 
    431 	consdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut");
    432 	if (consdev != NULL && strcmp(consdev, "video()") == 0) {
    433 		wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
    434 		aa.console = 1;
    435 	} else
    436 		aa.console = 0;
    437 	aa.scrdata = &crmfb_screenlist;
    438 	aa.accessops = &crmfb_accessops;
    439 	aa.accesscookie = &sc->sc_vd;
    440 
    441 	config_found(self, &aa, wsemuldisplaydevprint);
    442 
    443 	return;
    444 }
    445 
    446 int
    447 crmfb_probe(void)
    448 {
    449 
    450         if (mach_type != MACH_SGI_IP32)
    451                 return 0;
    452 
    453 	return 1;
    454 }
    455 
    456 static int
    457 crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    458 {
    459 	struct vcons_data *vd;
    460 	struct crmfb_softc *sc;
    461 	struct vcons_screen *ms;
    462 	struct wsdisplay_fbinfo *wdf;
    463 	int nmode;
    464 
    465 	vd = (struct vcons_data *)v;
    466 	sc = (struct crmfb_softc *)vd->cookie;
    467 	ms = (struct vcons_screen *)vd->active;
    468 
    469 	switch (cmd) {
    470 	case WSDISPLAYIO_GTYPE:
    471 		/* not really, but who cares? */
    472 		*(u_int *)data = WSDISPLAY_TYPE_NEWPORT;
    473 		return 0;
    474 	case WSDISPLAYIO_GINFO:
    475 		if (vd->active != NULL) {
    476 			wdf = (void *)data;
    477 			wdf->height = sc->sc_height;
    478 			wdf->width = sc->sc_width;
    479 			wdf->depth = sc->sc_depth;
    480 			wdf->cmsize = 256;
    481 			return 0;
    482 		} else
    483 			return ENODEV;
    484 	case WSDISPLAYIO_GETCMAP:
    485 		if (sc->sc_depth == 8)
    486 			return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
    487 		else
    488 			return EINVAL;
    489 	case WSDISPLAYIO_PUTCMAP:
    490 		if (sc->sc_depth == 8)
    491 			return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
    492 		else
    493 			return EINVAL;
    494 	case WSDISPLAYIO_LINEBYTES:
    495 		*(u_int *)data = sc->sc_width * sc->sc_depth / 8;
    496 		return 0;
    497 	case WSDISPLAYIO_SMODE:
    498 		nmode = *(int *)data;
    499 		if (nmode != sc->sc_wsmode) {
    500 			sc->sc_wsmode = nmode;
    501 			if (nmode == WSDISPLAYIO_MODE_EMUL)
    502 				vcons_redraw_screen(vd->active);
    503 		}
    504 		return 0;
    505 	}
    506 
    507 	return EPASSTHROUGH;
    508 }
    509 
    510 static paddr_t
    511 crmfb_mmap(void *v, void *vs, off_t offset, int prot)
    512 {
    513 	struct vcons_data *vd;
    514 	struct crmfb_softc *sc;
    515 	paddr_t pa;
    516 
    517 	vd = (struct vcons_data *)v;
    518 	sc = (struct crmfb_softc *)vd->cookie;
    519 
    520 	if (offset >= 0 && offset < sc->sc_fbsize) {
    521 		pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
    522 		    sc->sc_dma.nsegs, offset, prot,
    523 		    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    524 		return pa;
    525 	}
    526 
    527 	return -1;
    528 }
    529 
    530 static void
    531 crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
    532     long *defattr)
    533 {
    534 	struct crmfb_softc *sc;
    535 	struct rasops_info *ri;
    536 
    537 	sc = (struct crmfb_softc *)c;
    538 	ri = &scr->scr_ri;
    539 
    540 	ri->ri_flg = RI_CENTER;
    541 	ri->ri_depth = sc->sc_depth;
    542 	ri->ri_width = sc->sc_width;
    543 	ri->ri_height = sc->sc_height;
    544 	ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
    545 
    546 	switch (ri->ri_depth) {
    547 	case 16:
    548 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
    549 		ri->ri_rpos = 10;
    550 		ri->ri_gpos = 5;
    551 		ri->ri_bpos = 0;
    552 		break;
    553 	case 32:
    554 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    555 		ri->ri_rpos = 8;
    556 		ri->ri_gpos = 16;
    557 		ri->ri_bpos = 24;
    558 		break;
    559 	}
    560 
    561 	if (sc->sc_shadowfb == NULL)
    562 		ri->ri_bits = KERNADDR(sc->sc_dma);
    563 	else {
    564 		ri->ri_bits = sc->sc_shadowfb;
    565 		ri->ri_hwbits = KERNADDR(sc->sc_dma);
    566 	}
    567 
    568 	if (existing)
    569 		ri->ri_flg |= RI_CLEAR;
    570 
    571 	rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
    572 	ri->ri_caps = WSSCREEN_WSCOLORS;
    573 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    574 	    ri->ri_width / ri->ri_font->fontwidth);
    575 	ri->ri_hw = scr;
    576 
    577 	return;
    578 }
    579 
    580 static int
    581 crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    582 {
    583 	u_int idx, cnt;
    584 	u_char r[256], g[256], b[256];
    585 	u_char *rp, *gp, *bp;
    586 	int rv, i;
    587 
    588 	idx = cm->index;
    589 	cnt = cm->count;
    590 
    591 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    592 		return EINVAL;
    593 
    594 	rv = copyin(cm->red, &r[idx], cnt);
    595 	if (rv)
    596 		return rv;
    597 	rv = copyin(cm->green, &g[idx], cnt);
    598 	if (rv)
    599 		return rv;
    600 	rv = copyin(cm->blue, &b[idx], cnt);
    601 	if (rv)
    602 		return rv;
    603 
    604 	memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
    605 	memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
    606 	memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
    607 
    608 	rp = &sc->sc_cmap_red[idx];
    609 	gp = &sc->sc_cmap_green[idx];
    610 	bp = &sc->sc_cmap_blue[idx];
    611 
    612 	for (i = 0; i < cnt; i++) {
    613 		crmfb_set_palette(sc, idx, *rp, *gp, *bp);
    614 		idx++;
    615 		rp++, gp++, bp++;
    616 	}
    617 
    618 	return 0;
    619 }
    620 
    621 static int
    622 crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    623 {
    624 	u_int idx, cnt;
    625 	int rv;
    626 
    627 	idx = cm->index;
    628 	cnt = cm->count;
    629 
    630 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    631 		return EINVAL;
    632 
    633 	rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
    634 	if (rv)
    635 		return rv;
    636 	rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
    637 	if (rv)
    638 		return rv;
    639 	rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
    640 	if (rv)
    641 		return rv;
    642 
    643 	return 0;
    644 }
    645 
    646 static void
    647 crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
    648     uint8_t b)
    649 {
    650 	uint32_t val;
    651 
    652 	if (reg > 255 || sc->sc_depth != 8)
    653 		return;
    654 
    655 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
    656 		DELAY(10);
    657 
    658 	val = (r << 24) | (g << 16) | (b << 8);
    659 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP + (reg * 4), val);
    660 
    661 	return;
    662 }
    663