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