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