Home | History | Annotate | Line # | Download | only in dev
crmfb.c revision 1.6
      1  1.6  macallan /* $NetBSD: crmfb.c,v 1.6 2007/07/15 05:42:13 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.6  macallan __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.6 2007/07/15 05:42:13 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.3  jmcneill #define CRMFB_SHADOWFB
     63  1.3  jmcneill 
     64  1.1  jmcneill struct wsscreen_descr crmfb_defaultscreen = {
     65  1.1  jmcneill 	"default",
     66  1.1  jmcneill 	0, 0,
     67  1.1  jmcneill 	NULL,
     68  1.1  jmcneill 	8, 16,
     69  1.1  jmcneill 	WSSCREEN_WSCOLORS,
     70  1.1  jmcneill 	NULL,
     71  1.1  jmcneill };
     72  1.1  jmcneill 
     73  1.1  jmcneill const struct wsscreen_descr *_crmfb_scrlist[] = {
     74  1.1  jmcneill 	&crmfb_defaultscreen,
     75  1.1  jmcneill };
     76  1.1  jmcneill 
     77  1.1  jmcneill struct wsscreen_list crmfb_screenlist = {
     78  1.1  jmcneill 	sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
     79  1.1  jmcneill 	_crmfb_scrlist
     80  1.1  jmcneill };
     81  1.1  jmcneill 
     82  1.1  jmcneill static struct vcons_screen crmfb_console_screen;
     83  1.1  jmcneill 
     84  1.1  jmcneill static int	crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     85  1.1  jmcneill static paddr_t	crmfb_mmap(void *, void *, off_t, int);
     86  1.1  jmcneill static void	crmfb_init_screen(void *, struct vcons_screen *, int, long *);
     87  1.1  jmcneill 
     88  1.1  jmcneill struct wsdisplay_accessops crmfb_accessops = {
     89  1.1  jmcneill 	crmfb_ioctl,
     90  1.1  jmcneill 	crmfb_mmap,
     91  1.1  jmcneill 	NULL,	/* alloc_screen */
     92  1.1  jmcneill 	NULL,	/* free_screen */
     93  1.1  jmcneill 	NULL,	/* show_screen */
     94  1.1  jmcneill 	NULL,	/* load_font */
     95  1.1  jmcneill 	NULL,	/* pollc */
     96  1.1  jmcneill 	NULL,	/* scroll */
     97  1.1  jmcneill };
     98  1.1  jmcneill 
     99  1.1  jmcneill /* Memory to allocate to SGI-CRM -- remember, this is stolen from
    100  1.1  jmcneill  * host memory!
    101  1.1  jmcneill  */
    102  1.1  jmcneill #define CRMFB_TILESIZE	(512*128)
    103  1.1  jmcneill 
    104  1.1  jmcneill #define CRMFB_DOTCLOCK		0x00000004
    105  1.1  jmcneill #define		CRMFB_DOTCLOCK_CLKRUN_SHIFT	20
    106  1.1  jmcneill #define CRMFB_VT_XY		0x00010000
    107  1.1  jmcneill #define		CRMFB_VT_XY_FREEZE_SHIFT	31
    108  1.1  jmcneill #define CRMFB_VT_INTR01		0x00010020
    109  1.1  jmcneill #define		CRMFB_VT_INTR01_EN		0xffffffff
    110  1.1  jmcneill #define CRMFB_VT_INTR23		0x00010024
    111  1.1  jmcneill #define		CRMFB_VT_INTR23_EN		0xffffffff
    112  1.1  jmcneill #define CRMFB_VT_VPIX_EN	0x00010038
    113  1.1  jmcneill #define		CRMFB_VT_VPIX_EN_OFF_SHIFT	0
    114  1.1  jmcneill #define CRMFB_VT_VCMAP		0x0001003c
    115  1.1  jmcneill #define		CRMFB_VT_VCMAP_ON_SHIFT		12
    116  1.1  jmcneill #define CRMFB_VT_HCMAP		0x00010040
    117  1.1  jmcneill #define		CRMFB_VT_HCMAP_ON_SHIFT		12
    118  1.1  jmcneill #define CRMFB_OVR_WIDTH_TILE	0x00020000
    119  1.1  jmcneill #define CRMFB_OVR_CONTROL	0x00020008
    120  1.1  jmcneill #define		CRMFB_OVR_CONTROL_DMAEN_SHIFT	0
    121  1.1  jmcneill #define CRMFB_FRM_TILESIZE	0x00030000
    122  1.1  jmcneill #define		CRMFB_FRM_TILESIZE_RHS_SHIFT	0
    123  1.1  jmcneill #define		CRMFB_FRM_TILESIZE_WIDTH_SHIFT	5
    124  1.1  jmcneill #define		CRMFB_FRM_TILESIZE_DEPTH_SHIFT	13
    125  1.1  jmcneill #define			CRMFB_FRM_TILESIZE_DEPTH_8	0
    126  1.1  jmcneill #define			CRMFB_FRM_TILESIZE_DEPTH_16	1
    127  1.1  jmcneill #define			CRMFB_FRM_TILESIZE_DEPTH_32	2
    128  1.1  jmcneill #define		CRMFB_FRM_TILESIZE_FIFOR_SHIFT	15
    129  1.1  jmcneill #define CRMFB_FRM_PIXSIZE	0x00030004
    130  1.1  jmcneill #define		CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT	16
    131  1.1  jmcneill #define CRMFB_FRM_CONTROL	0x0003000c
    132  1.1  jmcneill #define		CRMFB_FRM_CONTROL_DMAEN_SHIFT	0
    133  1.1  jmcneill #define		CRMFB_FRM_CONTROL_LINEAR_SHIFT	1
    134  1.1  jmcneill #define		CRMFB_FRM_CONTROL_TILEPTR_SHIFT	9
    135  1.1  jmcneill #define CRMFB_DID_CONTROL	0x00040004
    136  1.1  jmcneill #define		CRMFB_DID_CONTROL_DMAEN_SHIFT	0
    137  1.1  jmcneill #define CRMFB_MODE		0x00048000
    138  1.1  jmcneill #define		CRMFB_MODE_TYP_SHIFT		2
    139  1.1  jmcneill #define			CRMFB_MODE_TYP_I8	0
    140  1.1  jmcneill #define			CRMFB_MODE_TYP_ARGB5	4
    141  1.1  jmcneill #define			CRMFB_MODE_TYP_RGB8	5
    142  1.1  jmcneill #define		CRMFB_MODE_BUF_SHIFT		0
    143  1.1  jmcneill #define			CRMFB_MODE_BUF_BOTH	3
    144  1.1  jmcneill #define CRMFB_CMAP		0x00050000
    145  1.1  jmcneill #define CRMFB_CMAP_FIFO		0x00058000
    146  1.1  jmcneill #define CRMFB_GMAP		0x00060000
    147  1.6  macallan #define CRMFB_CURSOR_POS	0x00070000
    148  1.6  macallan /*
    149  1.6  macallan  * upper 16 bit are Y, lower 16 bit are X - both signed so there's no need for
    150  1.6  macallan  * a hotspot register
    151  1.6  macallan  */
    152  1.6  macallan #define CRMFB_CURSOR_CONTROL	0x00070004
    153  1.6  macallan 	#define CRMFB_CURSOR_ON		0x00000001
    154  1.6  macallan #define CRMFB_CURSOR_CMAP0	0x00070008
    155  1.6  macallan #define CRMFB_CURSOR_CMAP1	0x0007000c
    156  1.6  macallan #define CRMFB_CURSOR_CMAP2	0x00070010
    157  1.6  macallan #define CRMFB_CURSOR_BITMAP	0x00078000
    158  1.6  macallan /* two bit deep cursor image, zero is transparent */
    159  1.6  macallan 
    160  1.6  macallan /* rendering engine registers */
    161  1.6  macallan #define CRIME_RE_TLB_A		0x1000
    162  1.6  macallan #define CRIME_RE_TLB_B		0x1200
    163  1.6  macallan #define CRIME_RE_TLB_C		0x1400
    164  1.6  macallan #define CRIME_RE_TEX		0x1600
    165  1.6  macallan #define CRIME_RE_CLIP_IDS	0x16e0
    166  1.6  macallan #define CRIME_RE_LINEAR_A	0x1700
    167  1.6  macallan #define CRIME_RE_LINEAR_B	0x1780
    168  1.6  macallan 
    169  1.1  jmcneill 
    170  1.1  jmcneill static int	crmfb_match(struct device *, struct cfdata *, void *);
    171  1.1  jmcneill static void	crmfb_attach(struct device *, struct device *, void *);
    172  1.1  jmcneill int		crmfb_probe(void);
    173  1.1  jmcneill 
    174  1.1  jmcneill #define KERNADDR(p)	((void *)((p).addr))
    175  1.1  jmcneill #define DMAADDR(p)	((p).map->dm_segs[0].ds_addr)
    176  1.1  jmcneill 
    177  1.1  jmcneill struct crmfb_dma {
    178  1.1  jmcneill 	bus_dmamap_t		map;
    179  1.1  jmcneill 	void			*addr;
    180  1.1  jmcneill 	bus_dma_segment_t	segs[1];
    181  1.1  jmcneill 	int			nsegs;
    182  1.1  jmcneill 	size_t			size;
    183  1.1  jmcneill };
    184  1.1  jmcneill 
    185  1.1  jmcneill struct crmfb_softc {
    186  1.1  jmcneill 	struct device		sc_dev;
    187  1.1  jmcneill 	struct vcons_data	sc_vd;
    188  1.1  jmcneill 
    189  1.1  jmcneill 	bus_space_tag_t		sc_iot;
    190  1.1  jmcneill 	bus_space_handle_t	sc_ioh;
    191  1.6  macallan 	bus_space_handle_t	sc_reh;	/* rendering engine registers */
    192  1.1  jmcneill 	bus_dma_tag_t		sc_dmat;
    193  1.1  jmcneill 
    194  1.1  jmcneill 	struct crmfb_dma	sc_dma;
    195  1.1  jmcneill 	struct crmfb_dma	sc_dmai;
    196  1.1  jmcneill 
    197  1.1  jmcneill 	int			sc_width;
    198  1.1  jmcneill 	int			sc_height;
    199  1.1  jmcneill 	int			sc_depth;
    200  1.1  jmcneill 	uint32_t		sc_fbsize;
    201  1.3  jmcneill 	uint8_t			*sc_shadowfb;
    202  1.1  jmcneill 
    203  1.1  jmcneill 	int			sc_wsmode;
    204  1.6  macallan 
    205  1.6  macallan 	/* cursor stuff */
    206  1.6  macallan 	int			sc_cur_x;
    207  1.6  macallan 	int			sc_cur_y;
    208  1.6  macallan 	int			sc_hot_x;
    209  1.6  macallan 	int			sc_hot_y;
    210  1.6  macallan 
    211  1.1  jmcneill 	u_char			sc_cmap_red[256];
    212  1.1  jmcneill 	u_char			sc_cmap_green[256];
    213  1.1  jmcneill 	u_char			sc_cmap_blue[256];
    214  1.1  jmcneill };
    215  1.1  jmcneill 
    216  1.1  jmcneill static int	crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    217  1.1  jmcneill static int	crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    218  1.1  jmcneill static void	crmfb_set_palette(struct crmfb_softc *,
    219  1.1  jmcneill 				  int, uint8_t, uint8_t, uint8_t);
    220  1.6  macallan static int	crmfb_set_curpos(struct crmfb_softc *, int, int);
    221  1.6  macallan static int	crmfb_gcursor(struct crmfb_softc *, struct wsdisplay_cursor *);
    222  1.6  macallan static int	crmfb_scursor(struct crmfb_softc *, struct wsdisplay_cursor *);
    223  1.1  jmcneill 
    224  1.1  jmcneill CFATTACH_DECL(crmfb, sizeof(struct crmfb_softc),
    225  1.1  jmcneill     crmfb_match, crmfb_attach, NULL, NULL);
    226  1.1  jmcneill 
    227  1.1  jmcneill static int
    228  1.1  jmcneill crmfb_match(struct device *parent, struct cfdata *cf, void *opaque)
    229  1.1  jmcneill {
    230  1.1  jmcneill 	return crmfb_probe();
    231  1.1  jmcneill }
    232  1.1  jmcneill 
    233  1.1  jmcneill static void
    234  1.1  jmcneill crmfb_attach(struct device *parent, struct device *self, void *opaque)
    235  1.1  jmcneill {
    236  1.1  jmcneill 	struct mainbus_attach_args *ma;
    237  1.1  jmcneill 	struct crmfb_softc *sc;
    238  1.1  jmcneill 	struct rasops_info *ri;
    239  1.1  jmcneill 	struct wsemuldisplaydev_attach_args aa;
    240  1.1  jmcneill 	uint32_t d, h;
    241  1.1  jmcneill 	uint16_t *p;
    242  1.1  jmcneill 	unsigned long v;
    243  1.1  jmcneill 	long defattr;
    244  1.2  jmcneill 	const char *consdev;
    245  1.1  jmcneill 	int rv, i;
    246  1.1  jmcneill 
    247  1.1  jmcneill 	sc = (struct crmfb_softc *)self;
    248  1.1  jmcneill 	ma = (struct mainbus_attach_args *)opaque;
    249  1.1  jmcneill 
    250  1.1  jmcneill 	sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME;
    251  1.1  jmcneill 	sc->sc_dmat = &sgimips_default_bus_dma_tag;
    252  1.1  jmcneill 	sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
    253  1.1  jmcneill 
    254  1.1  jmcneill 	aprint_normal(": SGI CRIME Graphics Display Engine\n");
    255  1.1  jmcneill 	rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
    256  1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
    257  1.1  jmcneill 	if (rv)
    258  1.1  jmcneill 		panic("crmfb_attach: can't map I/O space");
    259  1.1  jmcneill 
    260  1.6  macallan 	rv = bus_space_map(SGIMIPS_BUS_SPACE_CRIME, 0x15000000, 0x5000, 0,
    261  1.6  macallan 	    &sc->sc_reh);
    262  1.6  macallan 	if (rv)
    263  1.6  macallan 		panic("%s: unable to map rendering engine registers\n",
    264  1.6  macallan 		    __func__);
    265  1.6  macallan 
    266  1.1  jmcneill 	/* determine mode configured by firmware */
    267  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
    268  1.1  jmcneill 	sc->sc_width = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
    269  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
    270  1.1  jmcneill 	sc->sc_height = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
    271  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    272  1.1  jmcneill 	h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
    273  1.1  jmcneill 	if (h == 0)
    274  1.1  jmcneill 		sc->sc_depth = 8;
    275  1.1  jmcneill 	else if (h == 1)
    276  1.1  jmcneill 		sc->sc_depth = 16;
    277  1.1  jmcneill 	else
    278  1.1  jmcneill 		sc->sc_depth = 32;
    279  1.1  jmcneill 
    280  1.4    martin 	if (sc->sc_width == 0 || sc->sc_height == 0) {
    281  1.4    martin 		printf("%s: device unusable if not setup by firmware\n",
    282  1.4    martin 		    sc->sc_dev.dv_xname);
    283  1.4    martin 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0 /* XXX */);
    284  1.4    martin 		return;
    285  1.4    martin 	}
    286  1.4    martin 
    287  1.1  jmcneill 	printf("%s: initial resolution %dx%d %d bpp\n",
    288  1.1  jmcneill 	    sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
    289  1.1  jmcneill 
    290  1.1  jmcneill #if 0
    291  1.1  jmcneill 	/* Changing the colour depth is easy... */
    292  1.1  jmcneill 	sc->sc_depth = 32;
    293  1.1  jmcneill 	printf("%s: using resolution %dx%d %d bpp\n",
    294  1.1  jmcneill 	    sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
    295  1.1  jmcneill #endif
    296  1.1  jmcneill 
    297  1.1  jmcneill 	sc->sc_fbsize = sc->sc_width * sc->sc_height * sc->sc_depth / 8;
    298  1.2  jmcneill 	sc->sc_fbsize = (sc->sc_fbsize / CRMFB_TILESIZE) * CRMFB_TILESIZE;
    299  1.1  jmcneill 
    300  1.1  jmcneill 	sc->sc_dmai.size = 128 * sizeof(uint16_t);
    301  1.1  jmcneill 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
    302  1.1  jmcneill 	    sc->sc_dmai.segs,
    303  1.1  jmcneill 	    sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
    304  1.1  jmcneill 	    &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
    305  1.1  jmcneill 	if (rv)
    306  1.1  jmcneill 		panic("crmfb_attach: can't allocate DMA memory");
    307  1.1  jmcneill 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
    308  1.1  jmcneill 	    sc->sc_dmai.size, &sc->sc_dmai.addr,
    309  1.1  jmcneill 	    BUS_DMA_NOWAIT);
    310  1.1  jmcneill 	if (rv)
    311  1.1  jmcneill 		panic("crmfb_attach: can't map DMA memory");
    312  1.1  jmcneill 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
    313  1.1  jmcneill 	    sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
    314  1.1  jmcneill 	if (rv)
    315  1.1  jmcneill 		panic("crmfb_attach: can't create DMA map");
    316  1.1  jmcneill 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
    317  1.1  jmcneill 	    sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
    318  1.1  jmcneill 	if (rv)
    319  1.1  jmcneill 		panic("crmfb_attach: can't load DMA map");
    320  1.1  jmcneill 
    321  1.1  jmcneill 	sc->sc_dma.size = sc->sc_fbsize;
    322  1.1  jmcneill 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
    323  1.1  jmcneill 	    sc->sc_dma.segs,
    324  1.1  jmcneill 	    sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
    325  1.1  jmcneill 	    &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
    326  1.1  jmcneill 	if (rv)
    327  1.1  jmcneill 		panic("crmfb_attach: can't allocate DMA memory");
    328  1.1  jmcneill 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
    329  1.1  jmcneill 	    sc->sc_dma.size, &sc->sc_dma.addr,
    330  1.1  jmcneill 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    331  1.1  jmcneill 	if (rv)
    332  1.1  jmcneill 		panic("crmfb_attach: can't map DMA memory");
    333  1.1  jmcneill 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
    334  1.1  jmcneill 	    sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
    335  1.1  jmcneill 	if (rv)
    336  1.1  jmcneill 		panic("crmfb_attach: can't create DMA map");
    337  1.1  jmcneill 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
    338  1.1  jmcneill 	    sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
    339  1.1  jmcneill 	if (rv)
    340  1.1  jmcneill 		panic("crmfb_attach: can't load DMA map");
    341  1.1  jmcneill 
    342  1.1  jmcneill 	p = KERNADDR(sc->sc_dmai);
    343  1.1  jmcneill 	v = (unsigned long)DMAADDR(sc->sc_dma);
    344  1.1  jmcneill 	for (i = 0; i < (sc->sc_fbsize >> 16); i++) {
    345  1.1  jmcneill 		p[i] = ((uint32_t)v >> 16) + i;
    346  1.1  jmcneill 	}
    347  1.1  jmcneill 
    348  1.1  jmcneill 	memset(KERNADDR(sc->sc_dma), 0x00, sc->sc_fbsize);
    349  1.1  jmcneill 
    350  1.1  jmcneill 	printf("%s: allocated %d byte fb @ %p (%p)\n", sc->sc_dev.dv_xname,
    351  1.1  jmcneill 	    sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
    352  1.1  jmcneill 
    353  1.3  jmcneill #ifdef CRMFB_SHADOWFB
    354  1.3  jmcneill 	/* setup shadow framebuffer */
    355  1.3  jmcneill 	sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_NOWAIT | M_ZERO);
    356  1.3  jmcneill 	if (sc->sc_shadowfb == NULL)
    357  1.3  jmcneill 		aprint_error("%s: WARNING: couldn't allocate shadow fb\n",
    358  1.3  jmcneill 		    sc->sc_dev.dv_xname);
    359  1.3  jmcneill #endif
    360  1.3  jmcneill 
    361  1.1  jmcneill 	ri = &crmfb_console_screen.scr_ri;
    362  1.1  jmcneill 	memset(ri, 0, sizeof(struct rasops_info));
    363  1.1  jmcneill 
    364  1.1  jmcneill 	vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
    365  1.1  jmcneill 	sc->sc_vd.init_screen = crmfb_init_screen;
    366  1.1  jmcneill 	crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    367  1.1  jmcneill 	vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
    368  1.1  jmcneill 
    369  1.1  jmcneill 	crmfb_defaultscreen.ncols = ri->ri_cols;
    370  1.1  jmcneill 	crmfb_defaultscreen.nrows = ri->ri_rows;
    371  1.1  jmcneill 	crmfb_defaultscreen.textops = &ri->ri_ops;
    372  1.1  jmcneill 	crmfb_defaultscreen.capabilities = ri->ri_caps;
    373  1.1  jmcneill 	crmfb_defaultscreen.modecookie = NULL;
    374  1.1  jmcneill 
    375  1.1  jmcneill 	/* disable DMA */
    376  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
    377  1.1  jmcneill 	d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
    378  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL, d);
    379  1.1  jmcneill 	DELAY(10000);
    380  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    381  1.1  jmcneill 	d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    382  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
    383  1.1  jmcneill 	DELAY(10000);
    384  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DID_CONTROL, 0);
    385  1.1  jmcneill 	DELAY(10000);
    386  1.1  jmcneill 
    387  1.1  jmcneill 	/* ensure that CRM starts drawing at the top left of the screen
    388  1.1  jmcneill 	 * when we re-enable DMA later
    389  1.1  jmcneill 	 */
    390  1.1  jmcneill 	d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
    391  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, d);
    392  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    393  1.1  jmcneill 	d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    394  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
    395  1.1  jmcneill 
    396  1.1  jmcneill 	/* reset FIFO */
    397  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    398  1.1  jmcneill 	d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    399  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
    400  1.1  jmcneill 	d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    401  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
    402  1.1  jmcneill 
    403  1.1  jmcneill 	/* setup colour mode */
    404  1.1  jmcneill 	switch (sc->sc_depth) {
    405  1.1  jmcneill 	case 8:
    406  1.1  jmcneill 		h = CRMFB_MODE_TYP_I8;
    407  1.1  jmcneill 		break;
    408  1.1  jmcneill 	case 16:
    409  1.1  jmcneill 		h = CRMFB_MODE_TYP_ARGB5;
    410  1.1  jmcneill 		break;
    411  1.1  jmcneill 	case 32:
    412  1.1  jmcneill 		h = CRMFB_MODE_TYP_RGB8;
    413  1.1  jmcneill 		break;
    414  1.1  jmcneill 	default:
    415  1.1  jmcneill 		panic("Unsupported depth");
    416  1.1  jmcneill 	}
    417  1.1  jmcneill 	d = h << CRMFB_MODE_TYP_SHIFT;
    418  1.1  jmcneill 	d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
    419  1.1  jmcneill 	for (i = 0; i < (32 * 4); i += 4)
    420  1.1  jmcneill 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
    421  1.1  jmcneill 
    422  1.1  jmcneill 	/* setup tile pointer, but don't turn on DMA yet! */
    423  1.1  jmcneill 	h = DMAADDR(sc->sc_dmai);
    424  1.1  jmcneill 	d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
    425  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
    426  1.1  jmcneill 
    427  1.1  jmcneill 	/* init framebuffer width and pixel size */
    428  1.1  jmcneill 	d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);
    429  1.1  jmcneill 	switch (sc->sc_depth) {
    430  1.1  jmcneill 	case 8:
    431  1.1  jmcneill 		h = CRMFB_FRM_TILESIZE_DEPTH_8;
    432  1.1  jmcneill 		break;
    433  1.1  jmcneill 	case 16:
    434  1.1  jmcneill 		h = CRMFB_FRM_TILESIZE_DEPTH_16;
    435  1.1  jmcneill 		break;
    436  1.1  jmcneill 	case 32:
    437  1.1  jmcneill 		h = CRMFB_FRM_TILESIZE_DEPTH_32;
    438  1.1  jmcneill 		break;
    439  1.1  jmcneill 	default:
    440  1.1  jmcneill 		panic("Unsupported depth");
    441  1.1  jmcneill 	}
    442  1.1  jmcneill 	d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
    443  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
    444  1.1  jmcneill 
    445  1.1  jmcneill 	/* init framebuffer height, we use the trick that the Linux
    446  1.1  jmcneill 	 * driver uses to fool the CRM out of tiled mode and into
    447  1.1  jmcneill 	 * linear mode
    448  1.1  jmcneill 	 */
    449  1.1  jmcneill 	h = sc->sc_width * sc->sc_height / (512 / (sc->sc_depth / 8));
    450  1.1  jmcneill 	d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
    451  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_PIXSIZE, d);
    452  1.1  jmcneill 
    453  1.1  jmcneill 	/* turn off firmware overlay and hardware cursor */
    454  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_WIDTH_TILE, 0);
    455  1.6  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CURSOR_CONTROL, 0);
    456  1.1  jmcneill 
    457  1.1  jmcneill 	/* enable drawing again */
    458  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    459  1.1  jmcneill 	d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    460  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
    461  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, 0);
    462  1.1  jmcneill 
    463  1.1  jmcneill 	/* turn on DMA for the framebuffer */
    464  1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    465  1.1  jmcneill 	d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    466  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
    467  1.1  jmcneill 
    468  1.1  jmcneill 	for (i = 0; i < 256; i++) {
    469  1.1  jmcneill 		crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
    470  1.1  jmcneill 		    rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
    471  1.1  jmcneill 		sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
    472  1.1  jmcneill 		sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
    473  1.1  jmcneill 		sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
    474  1.1  jmcneill 	}
    475  1.1  jmcneill 
    476  1.2  jmcneill 	consdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut");
    477  1.2  jmcneill 	if (consdev != NULL && strcmp(consdev, "video()") == 0) {
    478  1.2  jmcneill 		wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
    479  1.2  jmcneill 		aa.console = 1;
    480  1.2  jmcneill 	} else
    481  1.2  jmcneill 		aa.console = 0;
    482  1.1  jmcneill 	aa.scrdata = &crmfb_screenlist;
    483  1.1  jmcneill 	aa.accessops = &crmfb_accessops;
    484  1.1  jmcneill 	aa.accesscookie = &sc->sc_vd;
    485  1.1  jmcneill 
    486  1.1  jmcneill 	config_found(self, &aa, wsemuldisplaydevprint);
    487  1.1  jmcneill 
    488  1.6  macallan 	/* fill in RE TLB A */
    489  1.6  macallan 	v = (unsigned long)DMAADDR(sc->sc_dma);
    490  1.6  macallan 	for (i = 0; i < (sc->sc_fbsize >> 16); i++) {
    491  1.6  macallan 		bus_space_write_8(sc->sc_iot, sc->sc_reh,
    492  1.6  macallan 		    CRIME_RE_TLB_A + (i << 3), (uint64_t)v + (i << 16));
    493  1.6  macallan 	}
    494  1.6  macallan 
    495  1.6  macallan 	sc->sc_cur_x = 0;
    496  1.6  macallan 	sc->sc_cur_y = 0;
    497  1.6  macallan 	sc->sc_hot_x = 0;
    498  1.6  macallan 	sc->sc_hot_y = 0;
    499  1.6  macallan 
    500  1.1  jmcneill 	return;
    501  1.1  jmcneill }
    502  1.1  jmcneill 
    503  1.1  jmcneill int
    504  1.1  jmcneill crmfb_probe(void)
    505  1.1  jmcneill {
    506  1.1  jmcneill 
    507  1.1  jmcneill         if (mach_type != MACH_SGI_IP32)
    508  1.1  jmcneill                 return 0;
    509  1.1  jmcneill 
    510  1.1  jmcneill 	return 1;
    511  1.1  jmcneill }
    512  1.1  jmcneill 
    513  1.1  jmcneill static int
    514  1.1  jmcneill crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    515  1.1  jmcneill {
    516  1.1  jmcneill 	struct vcons_data *vd;
    517  1.1  jmcneill 	struct crmfb_softc *sc;
    518  1.1  jmcneill 	struct vcons_screen *ms;
    519  1.1  jmcneill 	struct wsdisplay_fbinfo *wdf;
    520  1.1  jmcneill 	int nmode;
    521  1.1  jmcneill 
    522  1.1  jmcneill 	vd = (struct vcons_data *)v;
    523  1.1  jmcneill 	sc = (struct crmfb_softc *)vd->cookie;
    524  1.1  jmcneill 	ms = (struct vcons_screen *)vd->active;
    525  1.1  jmcneill 
    526  1.1  jmcneill 	switch (cmd) {
    527  1.1  jmcneill 	case WSDISPLAYIO_GTYPE:
    528  1.1  jmcneill 		/* not really, but who cares? */
    529  1.1  jmcneill 		*(u_int *)data = WSDISPLAY_TYPE_NEWPORT;
    530  1.1  jmcneill 		return 0;
    531  1.1  jmcneill 	case WSDISPLAYIO_GINFO:
    532  1.1  jmcneill 		if (vd->active != NULL) {
    533  1.1  jmcneill 			wdf = (void *)data;
    534  1.1  jmcneill 			wdf->height = sc->sc_height;
    535  1.1  jmcneill 			wdf->width = sc->sc_width;
    536  1.1  jmcneill 			wdf->depth = sc->sc_depth;
    537  1.1  jmcneill 			wdf->cmsize = 256;
    538  1.1  jmcneill 			return 0;
    539  1.1  jmcneill 		} else
    540  1.1  jmcneill 			return ENODEV;
    541  1.1  jmcneill 	case WSDISPLAYIO_GETCMAP:
    542  1.1  jmcneill 		if (sc->sc_depth == 8)
    543  1.1  jmcneill 			return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
    544  1.1  jmcneill 		else
    545  1.1  jmcneill 			return EINVAL;
    546  1.1  jmcneill 	case WSDISPLAYIO_PUTCMAP:
    547  1.1  jmcneill 		if (sc->sc_depth == 8)
    548  1.1  jmcneill 			return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
    549  1.1  jmcneill 		else
    550  1.1  jmcneill 			return EINVAL;
    551  1.1  jmcneill 	case WSDISPLAYIO_LINEBYTES:
    552  1.1  jmcneill 		*(u_int *)data = sc->sc_width * sc->sc_depth / 8;
    553  1.1  jmcneill 		return 0;
    554  1.1  jmcneill 	case WSDISPLAYIO_SMODE:
    555  1.1  jmcneill 		nmode = *(int *)data;
    556  1.1  jmcneill 		if (nmode != sc->sc_wsmode) {
    557  1.1  jmcneill 			sc->sc_wsmode = nmode;
    558  1.1  jmcneill 			if (nmode == WSDISPLAYIO_MODE_EMUL)
    559  1.1  jmcneill 				vcons_redraw_screen(vd->active);
    560  1.1  jmcneill 		}
    561  1.1  jmcneill 		return 0;
    562  1.5  jmcneill 	case WSDISPLAYIO_SVIDEO:
    563  1.5  jmcneill 	case WSDISPLAYIO_GVIDEO:
    564  1.5  jmcneill 		return ENODEV;	/* not supported yet */
    565  1.6  macallan 
    566  1.6  macallan 	case WSDISPLAYIO_GCURPOS:
    567  1.6  macallan 		{
    568  1.6  macallan 			struct wsdisplay_curpos *pos;
    569  1.6  macallan 
    570  1.6  macallan 			pos = (struct wsdisplay_curpos *)data;
    571  1.6  macallan 			pos->x = sc->sc_cur_x;
    572  1.6  macallan 			pos->y = sc->sc_cur_y;
    573  1.6  macallan 		}
    574  1.6  macallan 		return 0;
    575  1.6  macallan 	case WSDISPLAYIO_SCURPOS:
    576  1.6  macallan 		{
    577  1.6  macallan 			struct wsdisplay_curpos *pos;
    578  1.6  macallan 
    579  1.6  macallan 			pos = (struct wsdisplay_curpos *)data;
    580  1.6  macallan 			crmfb_set_curpos(sc, pos->x, pos->y);
    581  1.6  macallan 		}
    582  1.6  macallan 		return 0;
    583  1.6  macallan 	case WSDISPLAYIO_GCURMAX:
    584  1.6  macallan 		{
    585  1.6  macallan 			struct wsdisplay_curpos *pos;
    586  1.6  macallan 
    587  1.6  macallan 			pos = (struct wsdisplay_curpos *)data;
    588  1.6  macallan 			pos->x = 32;
    589  1.6  macallan 			pos->y = 32;
    590  1.6  macallan 		}
    591  1.6  macallan 		return 0;
    592  1.6  macallan 	case WSDISPLAYIO_GCURSOR:
    593  1.6  macallan 		{
    594  1.6  macallan 			struct wsdisplay_cursor *cu;
    595  1.6  macallan 
    596  1.6  macallan 			cu = (struct wsdisplay_cursor *)data;
    597  1.6  macallan 			return crmfb_gcursor(sc, cu);
    598  1.6  macallan 		}
    599  1.6  macallan 	case WSDISPLAYIO_SCURSOR:
    600  1.6  macallan 		{
    601  1.6  macallan 			struct wsdisplay_cursor *cu;
    602  1.6  macallan 
    603  1.6  macallan 			cu = (struct wsdisplay_cursor *)data;
    604  1.6  macallan 			return crmfb_scursor(sc, cu);
    605  1.6  macallan 		}
    606  1.1  jmcneill 	}
    607  1.1  jmcneill 	return EPASSTHROUGH;
    608  1.1  jmcneill }
    609  1.1  jmcneill 
    610  1.1  jmcneill static paddr_t
    611  1.1  jmcneill crmfb_mmap(void *v, void *vs, off_t offset, int prot)
    612  1.1  jmcneill {
    613  1.1  jmcneill 	struct vcons_data *vd;
    614  1.1  jmcneill 	struct crmfb_softc *sc;
    615  1.1  jmcneill 	paddr_t pa;
    616  1.1  jmcneill 
    617  1.1  jmcneill 	vd = (struct vcons_data *)v;
    618  1.1  jmcneill 	sc = (struct crmfb_softc *)vd->cookie;
    619  1.1  jmcneill 
    620  1.1  jmcneill 	if (offset >= 0 && offset < sc->sc_fbsize) {
    621  1.1  jmcneill 		pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
    622  1.1  jmcneill 		    sc->sc_dma.nsegs, offset, prot,
    623  1.3  jmcneill 		    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    624  1.1  jmcneill 		return pa;
    625  1.1  jmcneill 	}
    626  1.1  jmcneill 
    627  1.1  jmcneill 	return -1;
    628  1.1  jmcneill }
    629  1.1  jmcneill 
    630  1.1  jmcneill static void
    631  1.1  jmcneill crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
    632  1.1  jmcneill     long *defattr)
    633  1.1  jmcneill {
    634  1.1  jmcneill 	struct crmfb_softc *sc;
    635  1.1  jmcneill 	struct rasops_info *ri;
    636  1.1  jmcneill 
    637  1.1  jmcneill 	sc = (struct crmfb_softc *)c;
    638  1.1  jmcneill 	ri = &scr->scr_ri;
    639  1.1  jmcneill 
    640  1.1  jmcneill 	ri->ri_flg = RI_CENTER;
    641  1.1  jmcneill 	ri->ri_depth = sc->sc_depth;
    642  1.1  jmcneill 	ri->ri_width = sc->sc_width;
    643  1.1  jmcneill 	ri->ri_height = sc->sc_height;
    644  1.1  jmcneill 	ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
    645  1.1  jmcneill 
    646  1.1  jmcneill 	switch (ri->ri_depth) {
    647  1.1  jmcneill 	case 16:
    648  1.1  jmcneill 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
    649  1.1  jmcneill 		ri->ri_rpos = 10;
    650  1.1  jmcneill 		ri->ri_gpos = 5;
    651  1.1  jmcneill 		ri->ri_bpos = 0;
    652  1.1  jmcneill 		break;
    653  1.1  jmcneill 	case 32:
    654  1.1  jmcneill 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    655  1.1  jmcneill 		ri->ri_rpos = 8;
    656  1.1  jmcneill 		ri->ri_gpos = 16;
    657  1.1  jmcneill 		ri->ri_bpos = 24;
    658  1.1  jmcneill 		break;
    659  1.1  jmcneill 	}
    660  1.1  jmcneill 
    661  1.3  jmcneill 	if (sc->sc_shadowfb == NULL)
    662  1.3  jmcneill 		ri->ri_bits = KERNADDR(sc->sc_dma);
    663  1.3  jmcneill 	else {
    664  1.3  jmcneill 		ri->ri_bits = sc->sc_shadowfb;
    665  1.3  jmcneill 		ri->ri_hwbits = KERNADDR(sc->sc_dma);
    666  1.3  jmcneill 	}
    667  1.1  jmcneill 
    668  1.1  jmcneill 	if (existing)
    669  1.1  jmcneill 		ri->ri_flg |= RI_CLEAR;
    670  1.1  jmcneill 
    671  1.1  jmcneill 	rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
    672  1.1  jmcneill 	ri->ri_caps = WSSCREEN_WSCOLORS;
    673  1.1  jmcneill 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    674  1.1  jmcneill 	    ri->ri_width / ri->ri_font->fontwidth);
    675  1.1  jmcneill 	ri->ri_hw = scr;
    676  1.1  jmcneill 
    677  1.1  jmcneill 	return;
    678  1.1  jmcneill }
    679  1.1  jmcneill 
    680  1.1  jmcneill static int
    681  1.1  jmcneill crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    682  1.1  jmcneill {
    683  1.1  jmcneill 	u_int idx, cnt;
    684  1.1  jmcneill 	u_char r[256], g[256], b[256];
    685  1.1  jmcneill 	u_char *rp, *gp, *bp;
    686  1.1  jmcneill 	int rv, i;
    687  1.1  jmcneill 
    688  1.1  jmcneill 	idx = cm->index;
    689  1.1  jmcneill 	cnt = cm->count;
    690  1.1  jmcneill 
    691  1.1  jmcneill 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    692  1.1  jmcneill 		return EINVAL;
    693  1.1  jmcneill 
    694  1.1  jmcneill 	rv = copyin(cm->red, &r[idx], cnt);
    695  1.1  jmcneill 	if (rv)
    696  1.1  jmcneill 		return rv;
    697  1.1  jmcneill 	rv = copyin(cm->green, &g[idx], cnt);
    698  1.1  jmcneill 	if (rv)
    699  1.1  jmcneill 		return rv;
    700  1.1  jmcneill 	rv = copyin(cm->blue, &b[idx], cnt);
    701  1.1  jmcneill 	if (rv)
    702  1.1  jmcneill 		return rv;
    703  1.1  jmcneill 
    704  1.1  jmcneill 	memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
    705  1.1  jmcneill 	memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
    706  1.1  jmcneill 	memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
    707  1.1  jmcneill 
    708  1.1  jmcneill 	rp = &sc->sc_cmap_red[idx];
    709  1.1  jmcneill 	gp = &sc->sc_cmap_green[idx];
    710  1.1  jmcneill 	bp = &sc->sc_cmap_blue[idx];
    711  1.1  jmcneill 
    712  1.1  jmcneill 	for (i = 0; i < cnt; i++) {
    713  1.1  jmcneill 		crmfb_set_palette(sc, idx, *rp, *gp, *bp);
    714  1.1  jmcneill 		idx++;
    715  1.1  jmcneill 		rp++, gp++, bp++;
    716  1.1  jmcneill 	}
    717  1.1  jmcneill 
    718  1.1  jmcneill 	return 0;
    719  1.1  jmcneill }
    720  1.1  jmcneill 
    721  1.1  jmcneill static int
    722  1.1  jmcneill crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    723  1.1  jmcneill {
    724  1.1  jmcneill 	u_int idx, cnt;
    725  1.1  jmcneill 	int rv;
    726  1.1  jmcneill 
    727  1.1  jmcneill 	idx = cm->index;
    728  1.1  jmcneill 	cnt = cm->count;
    729  1.1  jmcneill 
    730  1.1  jmcneill 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    731  1.1  jmcneill 		return EINVAL;
    732  1.1  jmcneill 
    733  1.1  jmcneill 	rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
    734  1.1  jmcneill 	if (rv)
    735  1.1  jmcneill 		return rv;
    736  1.1  jmcneill 	rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
    737  1.1  jmcneill 	if (rv)
    738  1.1  jmcneill 		return rv;
    739  1.1  jmcneill 	rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
    740  1.1  jmcneill 	if (rv)
    741  1.1  jmcneill 		return rv;
    742  1.1  jmcneill 
    743  1.1  jmcneill 	return 0;
    744  1.1  jmcneill }
    745  1.1  jmcneill 
    746  1.1  jmcneill static void
    747  1.1  jmcneill crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
    748  1.1  jmcneill     uint8_t b)
    749  1.1  jmcneill {
    750  1.1  jmcneill 	uint32_t val;
    751  1.1  jmcneill 
    752  1.1  jmcneill 	if (reg > 255 || sc->sc_depth != 8)
    753  1.1  jmcneill 		return;
    754  1.1  jmcneill 
    755  1.1  jmcneill 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
    756  1.1  jmcneill 		DELAY(10);
    757  1.1  jmcneill 
    758  1.1  jmcneill 	val = (r << 24) | (g << 16) | (b << 8);
    759  1.1  jmcneill 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP + (reg * 4), val);
    760  1.1  jmcneill 
    761  1.1  jmcneill 	return;
    762  1.1  jmcneill }
    763  1.6  macallan 
    764  1.6  macallan static int
    765  1.6  macallan crmfb_set_curpos(struct crmfb_softc *sc, int x, int y)
    766  1.6  macallan {
    767  1.6  macallan 	uint32_t val;
    768  1.6  macallan 
    769  1.6  macallan 	sc->sc_cur_x = x;
    770  1.6  macallan 	sc->sc_cur_y = y;
    771  1.6  macallan 
    772  1.6  macallan 	val = ((x - sc->sc_hot_x) & 0xffff) | ((y - sc->sc_hot_y) << 16);
    773  1.6  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CURSOR_POS, val);
    774  1.6  macallan 
    775  1.6  macallan 	return 0;
    776  1.6  macallan }
    777  1.6  macallan 
    778  1.6  macallan static int
    779  1.6  macallan crmfb_gcursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    780  1.6  macallan {
    781  1.6  macallan 	/* do nothing for now */
    782  1.6  macallan 	return 0;
    783  1.6  macallan }
    784  1.6  macallan 
    785  1.6  macallan static int
    786  1.6  macallan crmfb_scursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    787  1.6  macallan {
    788  1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
    789  1.6  macallan 
    790  1.6  macallan 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CURSOR_CONTROL,
    791  1.6  macallan 		    cur->enable ? 1 : 0);
    792  1.6  macallan 	}
    793  1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
    794  1.6  macallan 
    795  1.6  macallan 		sc->sc_hot_x = cur->hot.x;
    796  1.6  macallan 		sc->sc_hot_y = cur->hot.y;
    797  1.6  macallan 	}
    798  1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
    799  1.6  macallan 
    800  1.6  macallan 		crmfb_set_curpos(sc, cur->pos.x, cur->pos.y);
    801  1.6  macallan 	}
    802  1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
    803  1.6  macallan 		int i;
    804  1.6  macallan 		uint32_t val;
    805  1.6  macallan 
    806  1.6  macallan 		for (i = 0; i < cur->cmap.count; i++) {
    807  1.6  macallan 			val = (cur->cmap.red[i] << 24) |
    808  1.6  macallan 			      (cur->cmap.green[i] << 16) |
    809  1.6  macallan 			      (cur->cmap.blue[i] << 8);
    810  1.6  macallan 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    811  1.6  macallan 			    CRMFB_CURSOR_CMAP0 + ((i + cur->cmap.index) << 2),
    812  1.6  macallan 			    val);
    813  1.6  macallan 		}
    814  1.6  macallan 	}
    815  1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
    816  1.6  macallan 
    817  1.6  macallan 		int i, j, cnt = 0;
    818  1.6  macallan 		uint32_t latch = 0, omask;
    819  1.6  macallan 		uint8_t imask;
    820  1.6  macallan 		for (i = 0; i < 64; i++) {
    821  1.6  macallan 			omask = 0x80000000;
    822  1.6  macallan 			imask = 0x01;
    823  1.6  macallan 			cur->image[cnt] &= cur->mask[cnt];
    824  1.6  macallan 			for (j = 0; j < 8; j++) {
    825  1.6  macallan 				if (cur->image[cnt] & imask)
    826  1.6  macallan 					latch |= omask;
    827  1.6  macallan 				omask >>= 1;
    828  1.6  macallan 				if (cur->mask[cnt] & imask)
    829  1.6  macallan 					latch |= omask;
    830  1.6  macallan 				omask >>= 1;
    831  1.6  macallan 				imask <<= 1;
    832  1.6  macallan 			}
    833  1.6  macallan 			cnt++;
    834  1.6  macallan 			imask = 0x01;
    835  1.6  macallan 			cur->image[cnt] &= cur->mask[cnt];
    836  1.6  macallan 			for (j = 0; j < 8; j++) {
    837  1.6  macallan 				if (cur->image[cnt] & imask)
    838  1.6  macallan 					latch |= omask;
    839  1.6  macallan 				omask >>= 1;
    840  1.6  macallan 				if (cur->mask[cnt] & imask)
    841  1.6  macallan 					latch |= omask;
    842  1.6  macallan 				omask >>= 1;
    843  1.6  macallan 				imask <<= 1;
    844  1.6  macallan 			}
    845  1.6  macallan 			cnt++;
    846  1.6  macallan 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    847  1.6  macallan 			    CRMFB_CURSOR_BITMAP + (i << 2), latch);
    848  1.6  macallan 			latch = 0;
    849  1.6  macallan 		}
    850  1.6  macallan 	}
    851  1.6  macallan 	return 0;
    852  1.6  macallan }
    853