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