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