Home | History | Annotate | Line # | Download | only in dev
crmfb.c revision 1.26.16.2
      1  1.26.16.1     rmind /* $NetBSD: crmfb.c,v 1.26.16.2 2011/04/21 01:41:23 rmind 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.12  macallan  *               2008 Michael Lorenz <macallan (at) netbsd.org>
      6        1.1  jmcneill  * All rights reserved.
      7        1.1  jmcneill  *
      8        1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      9        1.1  jmcneill  * modification, are permitted provided that the following conditions
     10        1.1  jmcneill  * are met:
     11        1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     12        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     13        1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     15        1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     16        1.1  jmcneill  *
     17        1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18        1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19        1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20        1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21        1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22        1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23        1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24        1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25        1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26        1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27        1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     28        1.1  jmcneill  */
     29        1.1  jmcneill 
     30        1.1  jmcneill /*
     31        1.1  jmcneill  * SGI-CRM (O2) Framebuffer driver
     32        1.1  jmcneill  */
     33        1.1  jmcneill 
     34        1.1  jmcneill #include <sys/cdefs.h>
     35  1.26.16.1     rmind __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.26.16.2 2011/04/21 01:41:23 rmind Exp $");
     36        1.1  jmcneill 
     37        1.1  jmcneill #include <sys/param.h>
     38        1.1  jmcneill #include <sys/systm.h>
     39        1.1  jmcneill #include <sys/device.h>
     40        1.1  jmcneill #include <sys/malloc.h>
     41        1.1  jmcneill 
     42        1.1  jmcneill #define _SGIMIPS_BUS_DMA_PRIVATE
     43        1.1  jmcneill #include <machine/autoconf.h>
     44        1.1  jmcneill #include <machine/bus.h>
     45        1.1  jmcneill #include <machine/machtype.h>
     46        1.1  jmcneill #include <machine/vmparam.h>
     47        1.1  jmcneill 
     48        1.2  jmcneill #include <dev/arcbios/arcbios.h>
     49        1.2  jmcneill #include <dev/arcbios/arcbiosvar.h>
     50        1.2  jmcneill 
     51        1.1  jmcneill #include <dev/wscons/wsdisplayvar.h>
     52        1.1  jmcneill #include <dev/wscons/wsconsio.h>
     53        1.1  jmcneill #include <dev/wsfont/wsfont.h>
     54        1.1  jmcneill #include <dev/rasops/rasops.h>
     55        1.1  jmcneill #include <dev/wscons/wsdisplay_vconsvar.h>
     56        1.1  jmcneill 
     57  1.26.16.2     rmind #include <dev/i2c/i2cvar.h>
     58  1.26.16.2     rmind #include <dev/i2c/i2c_bitbang.h>
     59  1.26.16.2     rmind #include <dev/i2c/ddcvar.h>
     60  1.26.16.2     rmind #include <dev/videomode/videomode.h>
     61  1.26.16.2     rmind #include <dev/videomode/edidvar.h>
     62  1.26.16.2     rmind 
     63        1.7  macallan #include <arch/sgimips/dev/crmfbreg.h>
     64        1.7  macallan 
     65  1.26.16.2     rmind #include "opt_crmfb.h"
     66  1.26.16.2     rmind 
     67  1.26.16.2     rmind #ifdef CRMFB_DEBUG
     68  1.26.16.2     rmind #define DPRINTF printf
     69  1.26.16.2     rmind #else
     70  1.26.16.2     rmind #define DPRINTF while (0) printf
     71  1.26.16.2     rmind #endif
     72        1.3  jmcneill 
     73        1.1  jmcneill struct wsscreen_descr crmfb_defaultscreen = {
     74        1.1  jmcneill 	"default",
     75        1.1  jmcneill 	0, 0,
     76        1.1  jmcneill 	NULL,
     77        1.1  jmcneill 	8, 16,
     78        1.1  jmcneill 	WSSCREEN_WSCOLORS,
     79        1.1  jmcneill 	NULL,
     80        1.1  jmcneill };
     81        1.1  jmcneill 
     82        1.1  jmcneill const struct wsscreen_descr *_crmfb_scrlist[] = {
     83        1.1  jmcneill 	&crmfb_defaultscreen,
     84        1.1  jmcneill };
     85        1.1  jmcneill 
     86        1.1  jmcneill struct wsscreen_list crmfb_screenlist = {
     87        1.1  jmcneill 	sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
     88        1.1  jmcneill 	_crmfb_scrlist
     89        1.1  jmcneill };
     90        1.1  jmcneill 
     91        1.1  jmcneill static struct vcons_screen crmfb_console_screen;
     92        1.1  jmcneill 
     93        1.1  jmcneill static int	crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     94        1.1  jmcneill static paddr_t	crmfb_mmap(void *, void *, off_t, int);
     95        1.1  jmcneill static void	crmfb_init_screen(void *, struct vcons_screen *, int, long *);
     96        1.1  jmcneill 
     97        1.1  jmcneill struct wsdisplay_accessops crmfb_accessops = {
     98        1.1  jmcneill 	crmfb_ioctl,
     99        1.1  jmcneill 	crmfb_mmap,
    100        1.1  jmcneill 	NULL,	/* alloc_screen */
    101        1.1  jmcneill 	NULL,	/* free_screen */
    102        1.1  jmcneill 	NULL,	/* show_screen */
    103        1.1  jmcneill 	NULL,	/* load_font */
    104        1.1  jmcneill 	NULL,	/* pollc */
    105        1.1  jmcneill 	NULL,	/* scroll */
    106        1.1  jmcneill };
    107        1.1  jmcneill 
    108        1.1  jmcneill /* Memory to allocate to SGI-CRM -- remember, this is stolen from
    109        1.1  jmcneill  * host memory!
    110        1.1  jmcneill  */
    111        1.1  jmcneill #define CRMFB_TILESIZE	(512*128)
    112        1.1  jmcneill 
    113  1.26.16.2     rmind static int	crmfb_match(device_t, struct cfdata *, void *);
    114  1.26.16.2     rmind static void	crmfb_attach(device_t, device_t, void *);
    115        1.1  jmcneill int		crmfb_probe(void);
    116        1.1  jmcneill 
    117        1.1  jmcneill #define KERNADDR(p)	((void *)((p).addr))
    118        1.1  jmcneill #define DMAADDR(p)	((p).map->dm_segs[0].ds_addr)
    119        1.1  jmcneill 
    120       1.10    sekiya #define CRMFB_REG_MASK(msb, lsb) \
    121       1.10    sekiya 	( (((uint32_t) 1 << ((msb)-(lsb)+1)) - 1) << (lsb) )
    122       1.10    sekiya 
    123       1.10    sekiya 
    124        1.1  jmcneill struct crmfb_dma {
    125        1.1  jmcneill 	bus_dmamap_t		map;
    126        1.1  jmcneill 	void			*addr;
    127        1.1  jmcneill 	bus_dma_segment_t	segs[1];
    128        1.1  jmcneill 	int			nsegs;
    129        1.1  jmcneill 	size_t			size;
    130        1.1  jmcneill };
    131        1.1  jmcneill 
    132        1.1  jmcneill struct crmfb_softc {
    133  1.26.16.2     rmind 	device_t		sc_dev;
    134        1.1  jmcneill 	struct vcons_data	sc_vd;
    135  1.26.16.2     rmind 	struct i2c_controller	sc_i2c;
    136  1.26.16.2     rmind 	int sc_dir;
    137        1.1  jmcneill 
    138        1.1  jmcneill 	bus_space_tag_t		sc_iot;
    139        1.1  jmcneill 	bus_space_handle_t	sc_ioh;
    140       1.11  macallan 	bus_space_handle_t	sc_reh;
    141        1.7  macallan 
    142        1.1  jmcneill 	bus_dma_tag_t		sc_dmat;
    143        1.1  jmcneill 
    144        1.1  jmcneill 	struct crmfb_dma	sc_dma;
    145        1.1  jmcneill 	struct crmfb_dma	sc_dmai;
    146        1.1  jmcneill 
    147        1.1  jmcneill 	int			sc_width;
    148        1.1  jmcneill 	int			sc_height;
    149        1.1  jmcneill 	int			sc_depth;
    150       1.11  macallan 	int			sc_tiles_x, sc_tiles_y;
    151        1.1  jmcneill 	uint32_t		sc_fbsize;
    152       1.18  macallan 	int			sc_mte_direction;
    153       1.11  macallan 	uint8_t			*sc_scratch;
    154       1.21  macallan 	paddr_t			sc_linear;
    155        1.1  jmcneill 	int			sc_wsmode;
    156  1.26.16.2     rmind 	struct edid_info sc_edid_info;
    157        1.6  macallan 
    158        1.6  macallan 	/* cursor stuff */
    159        1.6  macallan 	int			sc_cur_x;
    160        1.6  macallan 	int			sc_cur_y;
    161        1.6  macallan 	int			sc_hot_x;
    162        1.6  macallan 	int			sc_hot_y;
    163        1.6  macallan 
    164        1.1  jmcneill 	u_char			sc_cmap_red[256];
    165        1.1  jmcneill 	u_char			sc_cmap_green[256];
    166        1.1  jmcneill 	u_char			sc_cmap_blue[256];
    167        1.1  jmcneill };
    168        1.1  jmcneill 
    169        1.1  jmcneill static int	crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    170        1.1  jmcneill static int	crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
    171        1.1  jmcneill static void	crmfb_set_palette(struct crmfb_softc *,
    172        1.1  jmcneill 				  int, uint8_t, uint8_t, uint8_t);
    173        1.6  macallan static int	crmfb_set_curpos(struct crmfb_softc *, int, int);
    174        1.6  macallan static int	crmfb_gcursor(struct crmfb_softc *, struct wsdisplay_cursor *);
    175        1.6  macallan static int	crmfb_scursor(struct crmfb_softc *, struct wsdisplay_cursor *);
    176        1.7  macallan static inline void	crmfb_write_reg(struct crmfb_softc *, int, uint32_t);
    177       1.11  macallan static inline uint32_t	crmfb_read_reg(struct crmfb_softc *, int);
    178       1.11  macallan static int	crmfb_wait_dma_idle(struct crmfb_softc *);
    179        1.7  macallan 
    180        1.7  macallan /* setup video hw in given colour depth */
    181        1.7  macallan static int	crmfb_setup_video(struct crmfb_softc *, int);
    182        1.7  macallan static void	crmfb_setup_palette(struct crmfb_softc *);
    183        1.1  jmcneill 
    184       1.11  macallan static void crmfb_fill_rect(struct crmfb_softc *, int, int, int, int, uint32_t);
    185       1.11  macallan static void crmfb_bitblt(struct crmfb_softc *, int, int, int, int, int, int,
    186       1.11  macallan 			 uint32_t);
    187       1.15  macallan static void crmfb_scroll(struct crmfb_softc *, int, int, int, int, int, int);
    188       1.11  macallan 
    189       1.11  macallan static void	crmfb_copycols(void *, int, int, int, int);
    190       1.11  macallan static void	crmfb_erasecols(void *, int, int, int, long);
    191       1.11  macallan static void	crmfb_copyrows(void *, int, int, int);
    192       1.11  macallan static void	crmfb_eraserows(void *, int, int, long);
    193       1.11  macallan static void	crmfb_cursor(void *, int, int, int);
    194       1.11  macallan static void	crmfb_putchar(void *, int, int, u_int, long);
    195       1.11  macallan 
    196  1.26.16.2     rmind /* I2C glue */
    197  1.26.16.2     rmind static int crmfb_i2c_acquire_bus(void *, int);
    198  1.26.16.2     rmind static void crmfb_i2c_release_bus(void *, int);
    199  1.26.16.2     rmind static int crmfb_i2c_send_start(void *, int);
    200  1.26.16.2     rmind static int crmfb_i2c_send_stop(void *, int);
    201  1.26.16.2     rmind static int crmfb_i2c_initiate_xfer(void *, i2c_addr_t, int);
    202  1.26.16.2     rmind static int crmfb_i2c_read_byte(void *, uint8_t *, int);
    203  1.26.16.2     rmind static int crmfb_i2c_write_byte(void *, uint8_t, int);
    204  1.26.16.2     rmind 
    205  1.26.16.2     rmind /* I2C bitbang glue */
    206  1.26.16.2     rmind static void crmfb_i2cbb_set_bits(void *, uint32_t);
    207  1.26.16.2     rmind static void crmfb_i2cbb_set_dir(void *, uint32_t);
    208  1.26.16.2     rmind static uint32_t crmfb_i2cbb_read(void *);
    209  1.26.16.2     rmind 
    210  1.26.16.2     rmind static const struct i2c_bitbang_ops crmfb_i2cbb_ops = {
    211  1.26.16.2     rmind 	crmfb_i2cbb_set_bits,
    212  1.26.16.2     rmind 	crmfb_i2cbb_set_dir,
    213  1.26.16.2     rmind 	crmfb_i2cbb_read,
    214  1.26.16.2     rmind 	{
    215  1.26.16.2     rmind 		CRMFB_I2C_SDA,
    216  1.26.16.2     rmind 		CRMFB_I2C_SCL,
    217  1.26.16.2     rmind 		0,
    218  1.26.16.2     rmind 		1
    219  1.26.16.2     rmind 	}
    220  1.26.16.2     rmind };
    221  1.26.16.2     rmind static void crmfb_setup_ddc(struct crmfb_softc *);
    222  1.26.16.2     rmind 
    223  1.26.16.2     rmind /* mode setting stuff */
    224  1.26.16.2     rmind static uint32_t calc_pll(int);	/* frequency in kHz */
    225  1.26.16.2     rmind static int crmfb_set_mode(struct crmfb_softc *, const struct videomode *);
    226  1.26.16.2     rmind 
    227  1.26.16.2     rmind CFATTACH_DECL_NEW(crmfb, sizeof(struct crmfb_softc),
    228        1.1  jmcneill     crmfb_match, crmfb_attach, NULL, NULL);
    229        1.1  jmcneill 
    230        1.1  jmcneill static int
    231  1.26.16.2     rmind crmfb_match(device_t parent, struct cfdata *cf, void *opaque)
    232        1.1  jmcneill {
    233        1.1  jmcneill 	return crmfb_probe();
    234        1.1  jmcneill }
    235        1.1  jmcneill 
    236        1.1  jmcneill static void
    237  1.26.16.2     rmind crmfb_attach(device_t parent, device_t self, void *opaque)
    238        1.1  jmcneill {
    239        1.1  jmcneill 	struct mainbus_attach_args *ma;
    240        1.1  jmcneill 	struct crmfb_softc *sc;
    241        1.1  jmcneill 	struct rasops_info *ri;
    242        1.1  jmcneill 	struct wsemuldisplaydev_attach_args aa;
    243        1.1  jmcneill 	uint32_t d, h;
    244        1.1  jmcneill 	uint16_t *p;
    245        1.1  jmcneill 	unsigned long v;
    246        1.1  jmcneill 	long defattr;
    247        1.2  jmcneill 	const char *consdev;
    248       1.11  macallan 	int rv, i;
    249        1.1  jmcneill 
    250  1.26.16.2     rmind 	sc = device_private(self);
    251  1.26.16.2     rmind 	sc->sc_dev = self;
    252  1.26.16.2     rmind 
    253        1.1  jmcneill 	ma = (struct mainbus_attach_args *)opaque;
    254        1.1  jmcneill 
    255        1.1  jmcneill 	sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME;
    256        1.1  jmcneill 	sc->sc_dmat = &sgimips_default_bus_dma_tag;
    257        1.1  jmcneill 	sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
    258        1.1  jmcneill 
    259        1.1  jmcneill 	aprint_normal(": SGI CRIME Graphics Display Engine\n");
    260        1.1  jmcneill 	rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
    261        1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
    262        1.1  jmcneill 	if (rv)
    263        1.1  jmcneill 		panic("crmfb_attach: can't map I/O space");
    264       1.11  macallan 	rv = bus_space_map(sc->sc_iot, 0x15000000, 0x6000, 0, &sc->sc_reh);
    265       1.11  macallan 	if (rv)
    266       1.11  macallan 		panic("crmfb_attach: can't map rendering engine");
    267        1.1  jmcneill 
    268        1.1  jmcneill 	/* determine mode configured by firmware */
    269        1.9  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
    270        1.9  jmcneill 	sc->sc_width = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
    271        1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
    272        1.9  jmcneill 	sc->sc_height = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
    273        1.1  jmcneill 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    274        1.1  jmcneill 	h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
    275        1.1  jmcneill 	if (h == 0)
    276        1.1  jmcneill 		sc->sc_depth = 8;
    277        1.1  jmcneill 	else if (h == 1)
    278        1.1  jmcneill 		sc->sc_depth = 16;
    279        1.1  jmcneill 	else
    280        1.1  jmcneill 		sc->sc_depth = 32;
    281        1.1  jmcneill 
    282        1.4    martin 	if (sc->sc_width == 0 || sc->sc_height == 0) {
    283  1.26.16.2     rmind 		aprint_error_dev(sc->sc_dev,
    284  1.26.16.2     rmind 		    "device unusable if not setup by firmware\n");
    285        1.4    martin 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0 /* XXX */);
    286        1.4    martin 		return;
    287        1.4    martin 	}
    288        1.4    martin 
    289  1.26.16.2     rmind 	aprint_normal_dev(sc->sc_dev, "initial resolution %dx%d\n",
    290  1.26.16.2     rmind 	    sc->sc_width, sc->sc_height);
    291        1.1  jmcneill 
    292  1.26.16.2     rmind 	crmfb_setup_ddc(sc);
    293  1.26.16.2     rmind 	if ((sc->sc_edid_info.edid_preferred_mode != NULL)) {
    294  1.26.16.2     rmind 		if (crmfb_set_mode(sc, sc->sc_edid_info.edid_preferred_mode))
    295  1.26.16.2     rmind 			aprint_normal_dev(sc->sc_dev, "using %dx%d\n",
    296  1.26.16.2     rmind 			    sc->sc_width, sc->sc_height);
    297  1.26.16.2     rmind 	}
    298        1.7  macallan 	/*
    299        1.7  macallan 	 * first determine how many tiles we need
    300        1.7  macallan 	 * in 32bit each tile is 128x128 pixels
    301        1.7  macallan 	 */
    302       1.11  macallan 	sc->sc_tiles_x = (sc->sc_width + 127) >> 7;
    303       1.11  macallan 	sc->sc_tiles_y = (sc->sc_height + 127) >> 7;
    304       1.11  macallan 	sc->sc_fbsize = 0x10000 * sc->sc_tiles_x * sc->sc_tiles_y;
    305        1.1  jmcneill 
    306        1.7  macallan 	sc->sc_dmai.size = 256 * sizeof(uint16_t);
    307        1.1  jmcneill 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
    308        1.1  jmcneill 	    sc->sc_dmai.segs,
    309        1.1  jmcneill 	    sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
    310        1.1  jmcneill 	    &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
    311        1.1  jmcneill 	if (rv)
    312        1.1  jmcneill 		panic("crmfb_attach: can't allocate DMA memory");
    313        1.1  jmcneill 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
    314        1.1  jmcneill 	    sc->sc_dmai.size, &sc->sc_dmai.addr,
    315        1.1  jmcneill 	    BUS_DMA_NOWAIT);
    316        1.1  jmcneill 	if (rv)
    317        1.1  jmcneill 		panic("crmfb_attach: can't map DMA memory");
    318        1.1  jmcneill 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
    319        1.1  jmcneill 	    sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
    320        1.1  jmcneill 	if (rv)
    321        1.1  jmcneill 		panic("crmfb_attach: can't create DMA map");
    322        1.1  jmcneill 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
    323        1.1  jmcneill 	    sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
    324        1.1  jmcneill 	if (rv)
    325        1.1  jmcneill 		panic("crmfb_attach: can't load DMA map");
    326        1.1  jmcneill 
    327       1.21  macallan 	/* allocate an extra 64Kb for a linear buffer */
    328       1.21  macallan 	sc->sc_dma.size = 0x10000 * (16 * sc->sc_tiles_x + 1);
    329        1.1  jmcneill 	rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
    330        1.1  jmcneill 	    sc->sc_dma.segs,
    331        1.1  jmcneill 	    sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
    332        1.1  jmcneill 	    &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
    333        1.1  jmcneill 	if (rv)
    334        1.1  jmcneill 		panic("crmfb_attach: can't allocate DMA memory");
    335        1.1  jmcneill 	rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
    336        1.1  jmcneill 	    sc->sc_dma.size, &sc->sc_dma.addr,
    337        1.1  jmcneill 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    338        1.1  jmcneill 	if (rv)
    339        1.1  jmcneill 		panic("crmfb_attach: can't map DMA memory");
    340        1.1  jmcneill 	rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
    341        1.1  jmcneill 	    sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
    342        1.1  jmcneill 	if (rv)
    343        1.1  jmcneill 		panic("crmfb_attach: can't create DMA map");
    344        1.1  jmcneill 	rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
    345        1.1  jmcneill 	    sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
    346        1.1  jmcneill 	if (rv)
    347        1.1  jmcneill 		panic("crmfb_attach: can't load DMA map");
    348        1.1  jmcneill 
    349        1.1  jmcneill 	p = KERNADDR(sc->sc_dmai);
    350        1.1  jmcneill 	v = (unsigned long)DMAADDR(sc->sc_dma);
    351       1.11  macallan 	for (i = 0; i < (sc->sc_tiles_x * sc->sc_tiles_y); i++) {
    352        1.1  jmcneill 		p[i] = ((uint32_t)v >> 16) + i;
    353        1.1  jmcneill 	}
    354       1.26   tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmai.map, 0, sc->sc_dmai.size,
    355       1.26   tsutsui 	    BUS_DMASYNC_PREWRITE);
    356       1.21  macallan 	sc->sc_scratch = (char *)KERNADDR(sc->sc_dma) + (0xf0000 * sc->sc_tiles_x);
    357       1.21  macallan 	sc->sc_linear = (paddr_t)DMAADDR(sc->sc_dma) + 0x100000 * sc->sc_tiles_x;
    358        1.1  jmcneill 
    359  1.26.16.2     rmind 	aprint_normal_dev(sc->sc_dev, "allocated %d byte fb @ %p (%p)\n",
    360        1.1  jmcneill 	    sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
    361        1.1  jmcneill 
    362       1.21  macallan 	crmfb_setup_video(sc, 8);
    363        1.1  jmcneill 	ri = &crmfb_console_screen.scr_ri;
    364        1.1  jmcneill 	memset(ri, 0, sizeof(struct rasops_info));
    365        1.1  jmcneill 
    366        1.1  jmcneill 	vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
    367        1.1  jmcneill 	sc->sc_vd.init_screen = crmfb_init_screen;
    368        1.1  jmcneill 	crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    369        1.1  jmcneill 	vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
    370        1.1  jmcneill 
    371        1.1  jmcneill 	crmfb_defaultscreen.ncols = ri->ri_cols;
    372        1.1  jmcneill 	crmfb_defaultscreen.nrows = ri->ri_rows;
    373        1.1  jmcneill 	crmfb_defaultscreen.textops = &ri->ri_ops;
    374        1.1  jmcneill 	crmfb_defaultscreen.capabilities = ri->ri_caps;
    375        1.1  jmcneill 	crmfb_defaultscreen.modecookie = NULL;
    376        1.1  jmcneill 
    377        1.7  macallan 	crmfb_setup_palette(sc);
    378       1.14  macallan 	crmfb_fill_rect(sc, 0, 0, sc->sc_width, sc->sc_height,
    379       1.17  macallan 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    380        1.1  jmcneill 
    381  1.26.16.1     rmind 	consdev = arcbios_GetEnvironmentVariable("ConsoleOut");
    382        1.2  jmcneill 	if (consdev != NULL && strcmp(consdev, "video()") == 0) {
    383        1.2  jmcneill 		wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
    384  1.26.16.1     rmind 		vcons_replay_msgbuf(&crmfb_console_screen);
    385        1.2  jmcneill 		aa.console = 1;
    386        1.2  jmcneill 	} else
    387        1.2  jmcneill 		aa.console = 0;
    388        1.1  jmcneill 	aa.scrdata = &crmfb_screenlist;
    389        1.1  jmcneill 	aa.accessops = &crmfb_accessops;
    390        1.1  jmcneill 	aa.accesscookie = &sc->sc_vd;
    391        1.1  jmcneill 
    392        1.1  jmcneill 	config_found(self, &aa, wsemuldisplaydevprint);
    393        1.1  jmcneill 
    394        1.6  macallan 	sc->sc_cur_x = 0;
    395        1.6  macallan 	sc->sc_cur_y = 0;
    396        1.6  macallan 	sc->sc_hot_x = 0;
    397        1.6  macallan 	sc->sc_hot_y = 0;
    398        1.6  macallan 
    399        1.1  jmcneill 	return;
    400        1.1  jmcneill }
    401        1.1  jmcneill 
    402        1.1  jmcneill int
    403        1.1  jmcneill crmfb_probe(void)
    404        1.1  jmcneill {
    405        1.1  jmcneill 
    406        1.1  jmcneill         if (mach_type != MACH_SGI_IP32)
    407        1.1  jmcneill                 return 0;
    408        1.1  jmcneill 
    409        1.1  jmcneill 	return 1;
    410        1.1  jmcneill }
    411        1.1  jmcneill 
    412        1.1  jmcneill static int
    413        1.1  jmcneill crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    414        1.1  jmcneill {
    415        1.1  jmcneill 	struct vcons_data *vd;
    416        1.1  jmcneill 	struct crmfb_softc *sc;
    417        1.1  jmcneill 	struct vcons_screen *ms;
    418        1.1  jmcneill 	struct wsdisplay_fbinfo *wdf;
    419        1.1  jmcneill 	int nmode;
    420        1.1  jmcneill 
    421        1.1  jmcneill 	vd = (struct vcons_data *)v;
    422        1.1  jmcneill 	sc = (struct crmfb_softc *)vd->cookie;
    423        1.1  jmcneill 	ms = (struct vcons_screen *)vd->active;
    424        1.1  jmcneill 
    425        1.1  jmcneill 	switch (cmd) {
    426        1.1  jmcneill 	case WSDISPLAYIO_GTYPE:
    427        1.1  jmcneill 		/* not really, but who cares? */
    428        1.7  macallan 		/* wsfb does */
    429        1.7  macallan 		*(u_int *)data = WSDISPLAY_TYPE_CRIME;
    430        1.1  jmcneill 		return 0;
    431        1.1  jmcneill 	case WSDISPLAYIO_GINFO:
    432        1.1  jmcneill 		if (vd->active != NULL) {
    433        1.1  jmcneill 			wdf = (void *)data;
    434        1.1  jmcneill 			wdf->height = sc->sc_height;
    435        1.1  jmcneill 			wdf->width = sc->sc_width;
    436        1.7  macallan 			wdf->depth = 32;
    437        1.1  jmcneill 			wdf->cmsize = 256;
    438        1.1  jmcneill 			return 0;
    439        1.1  jmcneill 		} else
    440        1.1  jmcneill 			return ENODEV;
    441        1.1  jmcneill 	case WSDISPLAYIO_GETCMAP:
    442        1.1  jmcneill 		if (sc->sc_depth == 8)
    443        1.1  jmcneill 			return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
    444        1.1  jmcneill 		else
    445        1.1  jmcneill 			return EINVAL;
    446        1.1  jmcneill 	case WSDISPLAYIO_PUTCMAP:
    447        1.1  jmcneill 		if (sc->sc_depth == 8)
    448        1.1  jmcneill 			return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
    449        1.1  jmcneill 		else
    450        1.1  jmcneill 			return EINVAL;
    451        1.1  jmcneill 	case WSDISPLAYIO_LINEBYTES:
    452        1.1  jmcneill 		*(u_int *)data = sc->sc_width * sc->sc_depth / 8;
    453        1.1  jmcneill 		return 0;
    454        1.1  jmcneill 	case WSDISPLAYIO_SMODE:
    455        1.1  jmcneill 		nmode = *(int *)data;
    456        1.1  jmcneill 		if (nmode != sc->sc_wsmode) {
    457        1.1  jmcneill 			sc->sc_wsmode = nmode;
    458        1.7  macallan 			if (nmode == WSDISPLAYIO_MODE_EMUL) {
    459        1.7  macallan 				crmfb_setup_video(sc, 8);
    460        1.7  macallan 				crmfb_setup_palette(sc);
    461        1.1  jmcneill 				vcons_redraw_screen(vd->active);
    462        1.7  macallan 			} else {
    463        1.7  macallan 				crmfb_setup_video(sc, 32);
    464        1.7  macallan 			}
    465        1.1  jmcneill 		}
    466        1.1  jmcneill 		return 0;
    467        1.5  jmcneill 	case WSDISPLAYIO_SVIDEO:
    468        1.5  jmcneill 	case WSDISPLAYIO_GVIDEO:
    469        1.5  jmcneill 		return ENODEV;	/* not supported yet */
    470        1.6  macallan 
    471        1.6  macallan 	case WSDISPLAYIO_GCURPOS:
    472        1.6  macallan 		{
    473        1.6  macallan 			struct wsdisplay_curpos *pos;
    474        1.6  macallan 
    475        1.6  macallan 			pos = (struct wsdisplay_curpos *)data;
    476        1.6  macallan 			pos->x = sc->sc_cur_x;
    477        1.6  macallan 			pos->y = sc->sc_cur_y;
    478        1.6  macallan 		}
    479        1.6  macallan 		return 0;
    480        1.6  macallan 	case WSDISPLAYIO_SCURPOS:
    481        1.6  macallan 		{
    482        1.6  macallan 			struct wsdisplay_curpos *pos;
    483        1.6  macallan 
    484        1.6  macallan 			pos = (struct wsdisplay_curpos *)data;
    485        1.6  macallan 			crmfb_set_curpos(sc, pos->x, pos->y);
    486        1.6  macallan 		}
    487        1.6  macallan 		return 0;
    488        1.6  macallan 	case WSDISPLAYIO_GCURMAX:
    489        1.6  macallan 		{
    490        1.6  macallan 			struct wsdisplay_curpos *pos;
    491        1.6  macallan 
    492        1.6  macallan 			pos = (struct wsdisplay_curpos *)data;
    493        1.6  macallan 			pos->x = 32;
    494        1.6  macallan 			pos->y = 32;
    495        1.6  macallan 		}
    496        1.6  macallan 		return 0;
    497        1.6  macallan 	case WSDISPLAYIO_GCURSOR:
    498        1.6  macallan 		{
    499        1.6  macallan 			struct wsdisplay_cursor *cu;
    500        1.6  macallan 
    501        1.6  macallan 			cu = (struct wsdisplay_cursor *)data;
    502        1.6  macallan 			return crmfb_gcursor(sc, cu);
    503        1.6  macallan 		}
    504        1.6  macallan 	case WSDISPLAYIO_SCURSOR:
    505        1.6  macallan 		{
    506        1.6  macallan 			struct wsdisplay_cursor *cu;
    507        1.6  macallan 
    508        1.6  macallan 			cu = (struct wsdisplay_cursor *)data;
    509        1.6  macallan 			return crmfb_scursor(sc, cu);
    510        1.6  macallan 		}
    511        1.1  jmcneill 	}
    512        1.1  jmcneill 	return EPASSTHROUGH;
    513        1.1  jmcneill }
    514        1.1  jmcneill 
    515        1.1  jmcneill static paddr_t
    516        1.1  jmcneill crmfb_mmap(void *v, void *vs, off_t offset, int prot)
    517        1.1  jmcneill {
    518        1.1  jmcneill 	struct vcons_data *vd;
    519        1.1  jmcneill 	struct crmfb_softc *sc;
    520        1.1  jmcneill 	paddr_t pa;
    521        1.1  jmcneill 
    522        1.1  jmcneill 	vd = (struct vcons_data *)v;
    523        1.1  jmcneill 	sc = (struct crmfb_softc *)vd->cookie;
    524        1.1  jmcneill 
    525       1.14  macallan 	/* we probably shouldn't let anyone mmap the framebuffer */
    526        1.7  macallan #if 1
    527       1.22  macallan 	if (offset >= 0 && offset < (0x100000 * sc->sc_tiles_x)) {
    528        1.1  jmcneill 		pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
    529        1.1  jmcneill 		    sc->sc_dma.nsegs, offset, prot,
    530        1.3  jmcneill 		    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    531        1.1  jmcneill 		return pa;
    532        1.1  jmcneill 	}
    533        1.7  macallan #endif
    534       1.14  macallan 	/*
    535       1.14  macallan 	 * here would the TLBs be but we don't want to show them to userland
    536       1.14  macallan 	 * so we return the page containing the status register
    537       1.14  macallan 	 */
    538       1.14  macallan 	if ((offset >= 0x15000000) && (offset < 0x15002000))
    539       1.14  macallan 		return bus_space_mmap(sc->sc_iot, 0x15004000, 0, prot, 0);
    540       1.14  macallan 	/* now the actual engine registers */
    541       1.14  macallan 	if ((offset >= 0x15002000) && (offset < 0x15005000))
    542       1.14  macallan 		return bus_space_mmap(sc->sc_iot, offset, 0, prot, 0);
    543       1.14  macallan 	/* and now the scratch area */
    544       1.14  macallan 	if ((offset >= 0x15010000) && (offset < 0x15020000))
    545       1.14  macallan 		return bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
    546       1.21  macallan 		     sc->sc_dma.nsegs,
    547       1.21  macallan 		     offset + (0x100000 * sc->sc_tiles_x) - 0x15010000,
    548       1.21  macallan 		     prot, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    549        1.1  jmcneill 	return -1;
    550        1.1  jmcneill }
    551        1.1  jmcneill 
    552        1.1  jmcneill static void
    553        1.1  jmcneill crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
    554        1.1  jmcneill     long *defattr)
    555        1.1  jmcneill {
    556        1.1  jmcneill 	struct crmfb_softc *sc;
    557        1.1  jmcneill 	struct rasops_info *ri;
    558        1.1  jmcneill 
    559        1.1  jmcneill 	sc = (struct crmfb_softc *)c;
    560        1.1  jmcneill 	ri = &scr->scr_ri;
    561        1.1  jmcneill 
    562       1.11  macallan 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    563        1.1  jmcneill 	ri->ri_depth = sc->sc_depth;
    564        1.1  jmcneill 	ri->ri_width = sc->sc_width;
    565        1.1  jmcneill 	ri->ri_height = sc->sc_height;
    566        1.1  jmcneill 	ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
    567        1.1  jmcneill 
    568        1.1  jmcneill 	switch (ri->ri_depth) {
    569        1.1  jmcneill 	case 16:
    570        1.1  jmcneill 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
    571        1.1  jmcneill 		ri->ri_rpos = 10;
    572        1.1  jmcneill 		ri->ri_gpos = 5;
    573        1.1  jmcneill 		ri->ri_bpos = 0;
    574        1.1  jmcneill 		break;
    575        1.1  jmcneill 	case 32:
    576        1.1  jmcneill 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    577        1.1  jmcneill 		ri->ri_rpos = 8;
    578        1.1  jmcneill 		ri->ri_gpos = 16;
    579        1.1  jmcneill 		ri->ri_bpos = 24;
    580        1.1  jmcneill 		break;
    581        1.1  jmcneill 	}
    582        1.1  jmcneill 
    583       1.11  macallan 	ri->ri_bits = KERNADDR(sc->sc_dma);
    584        1.1  jmcneill 
    585        1.1  jmcneill 	if (existing)
    586        1.1  jmcneill 		ri->ri_flg |= RI_CLEAR;
    587        1.1  jmcneill 
    588        1.1  jmcneill 	rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
    589        1.1  jmcneill 	ri->ri_caps = WSSCREEN_WSCOLORS;
    590        1.1  jmcneill 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    591        1.1  jmcneill 	    ri->ri_width / ri->ri_font->fontwidth);
    592        1.1  jmcneill 	ri->ri_hw = scr;
    593        1.1  jmcneill 
    594       1.11  macallan 	ri->ri_ops.cursor    = crmfb_cursor;
    595       1.11  macallan 	ri->ri_ops.copyrows  = crmfb_copyrows;
    596       1.11  macallan 	ri->ri_ops.eraserows = crmfb_eraserows;
    597       1.11  macallan 	ri->ri_ops.copycols  = crmfb_copycols;
    598       1.11  macallan 	ri->ri_ops.erasecols = crmfb_erasecols;
    599       1.11  macallan 	ri->ri_ops.putchar   = crmfb_putchar;
    600       1.11  macallan 
    601        1.1  jmcneill 	return;
    602        1.1  jmcneill }
    603        1.1  jmcneill 
    604        1.1  jmcneill static int
    605        1.1  jmcneill crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    606        1.1  jmcneill {
    607        1.1  jmcneill 	u_int idx, cnt;
    608        1.1  jmcneill 	u_char r[256], g[256], b[256];
    609        1.1  jmcneill 	u_char *rp, *gp, *bp;
    610        1.1  jmcneill 	int rv, i;
    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 = copyin(cm->red, &r[idx], cnt);
    619        1.1  jmcneill 	if (rv)
    620        1.1  jmcneill 		return rv;
    621        1.1  jmcneill 	rv = copyin(cm->green, &g[idx], cnt);
    622        1.1  jmcneill 	if (rv)
    623        1.1  jmcneill 		return rv;
    624        1.1  jmcneill 	rv = copyin(cm->blue, &b[idx], cnt);
    625        1.1  jmcneill 	if (rv)
    626        1.1  jmcneill 		return rv;
    627        1.1  jmcneill 
    628        1.1  jmcneill 	memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
    629        1.1  jmcneill 	memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
    630        1.1  jmcneill 	memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
    631        1.1  jmcneill 
    632        1.1  jmcneill 	rp = &sc->sc_cmap_red[idx];
    633        1.1  jmcneill 	gp = &sc->sc_cmap_green[idx];
    634        1.1  jmcneill 	bp = &sc->sc_cmap_blue[idx];
    635        1.1  jmcneill 
    636        1.1  jmcneill 	for (i = 0; i < cnt; i++) {
    637        1.1  jmcneill 		crmfb_set_palette(sc, idx, *rp, *gp, *bp);
    638        1.1  jmcneill 		idx++;
    639        1.1  jmcneill 		rp++, gp++, bp++;
    640        1.1  jmcneill 	}
    641        1.1  jmcneill 
    642        1.1  jmcneill 	return 0;
    643        1.1  jmcneill }
    644        1.1  jmcneill 
    645        1.1  jmcneill static int
    646        1.1  jmcneill crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
    647        1.1  jmcneill {
    648        1.1  jmcneill 	u_int idx, cnt;
    649        1.1  jmcneill 	int rv;
    650        1.1  jmcneill 
    651        1.1  jmcneill 	idx = cm->index;
    652        1.1  jmcneill 	cnt = cm->count;
    653        1.1  jmcneill 
    654        1.1  jmcneill 	if (idx >= 255 || cnt > 256 || idx + cnt > 256)
    655        1.1  jmcneill 		return EINVAL;
    656        1.1  jmcneill 
    657        1.1  jmcneill 	rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
    658        1.1  jmcneill 	if (rv)
    659        1.1  jmcneill 		return rv;
    660        1.1  jmcneill 	rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
    661        1.1  jmcneill 	if (rv)
    662        1.1  jmcneill 		return rv;
    663        1.1  jmcneill 	rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
    664        1.1  jmcneill 	if (rv)
    665        1.1  jmcneill 		return rv;
    666        1.1  jmcneill 
    667        1.1  jmcneill 	return 0;
    668        1.1  jmcneill }
    669        1.1  jmcneill 
    670        1.1  jmcneill static void
    671        1.1  jmcneill crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
    672        1.1  jmcneill     uint8_t b)
    673        1.1  jmcneill {
    674        1.1  jmcneill 	uint32_t val;
    675        1.1  jmcneill 
    676        1.1  jmcneill 	if (reg > 255 || sc->sc_depth != 8)
    677        1.1  jmcneill 		return;
    678        1.1  jmcneill 
    679        1.1  jmcneill 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
    680        1.1  jmcneill 		DELAY(10);
    681        1.1  jmcneill 
    682       1.23  jmcneill 	val = (r << 8) | (g << 16) | (b << 24);
    683        1.7  macallan 	crmfb_write_reg(sc, CRMFB_CMAP + (reg * 4), val);
    684        1.1  jmcneill 
    685        1.1  jmcneill 	return;
    686        1.1  jmcneill }
    687        1.6  macallan 
    688        1.6  macallan static int
    689        1.6  macallan crmfb_set_curpos(struct crmfb_softc *sc, int x, int y)
    690        1.6  macallan {
    691        1.6  macallan 	uint32_t val;
    692        1.6  macallan 
    693        1.6  macallan 	sc->sc_cur_x = x;
    694        1.6  macallan 	sc->sc_cur_y = y;
    695        1.6  macallan 
    696        1.6  macallan 	val = ((x - sc->sc_hot_x) & 0xffff) | ((y - sc->sc_hot_y) << 16);
    697        1.7  macallan 	crmfb_write_reg(sc, CRMFB_CURSOR_POS, val);
    698        1.6  macallan 
    699        1.6  macallan 	return 0;
    700        1.6  macallan }
    701        1.6  macallan 
    702        1.6  macallan static int
    703        1.6  macallan crmfb_gcursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    704        1.6  macallan {
    705        1.6  macallan 	/* do nothing for now */
    706        1.6  macallan 	return 0;
    707        1.6  macallan }
    708        1.6  macallan 
    709        1.6  macallan static int
    710        1.6  macallan crmfb_scursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
    711        1.6  macallan {
    712        1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
    713        1.6  macallan 
    714        1.7  macallan 		crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, cur->enable ? 1 : 0);
    715        1.6  macallan 	}
    716        1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
    717        1.6  macallan 
    718        1.6  macallan 		sc->sc_hot_x = cur->hot.x;
    719        1.6  macallan 		sc->sc_hot_y = cur->hot.y;
    720        1.6  macallan 	}
    721        1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
    722        1.6  macallan 
    723        1.6  macallan 		crmfb_set_curpos(sc, cur->pos.x, cur->pos.y);
    724        1.6  macallan 	}
    725        1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
    726        1.6  macallan 		int i;
    727        1.6  macallan 		uint32_t val;
    728        1.6  macallan 
    729        1.6  macallan 		for (i = 0; i < cur->cmap.count; i++) {
    730        1.6  macallan 			val = (cur->cmap.red[i] << 24) |
    731        1.6  macallan 			      (cur->cmap.green[i] << 16) |
    732        1.6  macallan 			      (cur->cmap.blue[i] << 8);
    733        1.7  macallan 			crmfb_write_reg(sc, CRMFB_CURSOR_CMAP0 +
    734        1.7  macallan 			    ((i + cur->cmap.index) << 2), val);
    735        1.6  macallan 		}
    736        1.6  macallan 	}
    737        1.6  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
    738        1.6  macallan 
    739        1.6  macallan 		int i, j, cnt = 0;
    740        1.6  macallan 		uint32_t latch = 0, omask;
    741        1.6  macallan 		uint8_t imask;
    742        1.6  macallan 		for (i = 0; i < 64; i++) {
    743        1.6  macallan 			omask = 0x80000000;
    744        1.6  macallan 			imask = 0x01;
    745        1.6  macallan 			cur->image[cnt] &= cur->mask[cnt];
    746        1.6  macallan 			for (j = 0; j < 8; j++) {
    747        1.6  macallan 				if (cur->image[cnt] & imask)
    748        1.6  macallan 					latch |= omask;
    749        1.6  macallan 				omask >>= 1;
    750        1.6  macallan 				if (cur->mask[cnt] & imask)
    751        1.6  macallan 					latch |= omask;
    752        1.6  macallan 				omask >>= 1;
    753        1.6  macallan 				imask <<= 1;
    754        1.6  macallan 			}
    755        1.6  macallan 			cnt++;
    756        1.6  macallan 			imask = 0x01;
    757        1.6  macallan 			cur->image[cnt] &= cur->mask[cnt];
    758        1.6  macallan 			for (j = 0; j < 8; j++) {
    759        1.6  macallan 				if (cur->image[cnt] & imask)
    760        1.6  macallan 					latch |= omask;
    761        1.6  macallan 				omask >>= 1;
    762        1.6  macallan 				if (cur->mask[cnt] & imask)
    763        1.6  macallan 					latch |= omask;
    764        1.6  macallan 				omask >>= 1;
    765        1.6  macallan 				imask <<= 1;
    766        1.6  macallan 			}
    767        1.6  macallan 			cnt++;
    768        1.7  macallan 			crmfb_write_reg(sc, CRMFB_CURSOR_BITMAP + (i << 2),
    769        1.7  macallan 			    latch);
    770        1.6  macallan 			latch = 0;
    771        1.6  macallan 		}
    772        1.6  macallan 	}
    773        1.6  macallan 	return 0;
    774        1.6  macallan }
    775        1.7  macallan 
    776        1.7  macallan static inline void
    777        1.7  macallan crmfb_write_reg(struct crmfb_softc *sc, int offset, uint32_t val)
    778        1.7  macallan {
    779        1.7  macallan 
    780        1.7  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
    781        1.7  macallan 	wbflush();
    782        1.7  macallan }
    783        1.7  macallan 
    784       1.11  macallan static inline uint32_t
    785       1.11  macallan crmfb_read_reg(struct crmfb_softc *sc, int offset)
    786       1.11  macallan {
    787       1.11  macallan 
    788       1.11  macallan 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
    789       1.11  macallan }
    790       1.11  macallan 
    791       1.11  macallan static int
    792       1.11  macallan crmfb_wait_dma_idle(struct crmfb_softc *sc)
    793       1.11  macallan {
    794       1.11  macallan 	int bail = 100000, idle;
    795       1.11  macallan 
    796       1.11  macallan 	do {
    797       1.11  macallan 		idle = ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    798       1.11  macallan 		         CRMFB_OVR_CONTROL) & 1) == 0) &&
    799       1.11  macallan 		       ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    800       1.11  macallan 		         CRMFB_FRM_CONTROL) & 1) == 0) &&
    801       1.11  macallan 		       ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    802       1.11  macallan 		         CRMFB_DID_CONTROL) & 1) == 0);
    803       1.11  macallan 		if (!idle)
    804       1.11  macallan 			delay(10);
    805       1.11  macallan 		bail--;
    806       1.11  macallan 	} while ((!idle) && (bail > 0));
    807       1.11  macallan 	return idle;
    808       1.11  macallan }
    809       1.11  macallan 
    810        1.7  macallan static int
    811        1.7  macallan crmfb_setup_video(struct crmfb_softc *sc, int depth)
    812        1.7  macallan {
    813       1.11  macallan 	uint64_t reg;
    814       1.21  macallan 	uint32_t d, h, mode, page;
    815  1.26.16.2     rmind 	int i, bail, tile_width, tlbptr, lptr, j, tx, shift, overhang;
    816       1.10    sekiya 	const char *wantsync;
    817       1.11  macallan 	uint16_t v;
    818       1.11  macallan 
    819        1.7  macallan 	/* disable DMA */
    820        1.7  macallan 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
    821        1.7  macallan 	d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
    822        1.7  macallan 	crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d);
    823        1.7  macallan 	DELAY(50000);
    824        1.7  macallan 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    825        1.7  macallan 	d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    826        1.7  macallan 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    827        1.7  macallan 	DELAY(50000);
    828  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_DID_CONTROL, d);
    829        1.7  macallan 	DELAY(50000);
    830        1.7  macallan 
    831       1.11  macallan 	if (!crmfb_wait_dma_idle(sc))
    832       1.11  macallan 		aprint_error("crmfb: crmfb_wait_dma_idle timed out\n");
    833       1.11  macallan 
    834        1.7  macallan 	/* ensure that CRM starts drawing at the top left of the screen
    835        1.7  macallan 	 * when we re-enable DMA later
    836        1.7  macallan 	 */
    837        1.7  macallan 	d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
    838        1.7  macallan 	crmfb_write_reg(sc, CRMFB_VT_XY, d);
    839        1.7  macallan 	delay(1000);
    840        1.7  macallan 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    841        1.7  macallan 	d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    842        1.7  macallan 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
    843       1.11  macallan 
    844       1.11  macallan 	/* wait for dotclock to turn off */
    845       1.11  macallan 	bail = 10000;
    846       1.11  macallan 	while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK) &
    847       1.11  macallan 	    (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT)) && (bail > 0)) {
    848       1.11  macallan 		delay(10);
    849       1.11  macallan 		bail--;
    850       1.11  macallan 	}
    851        1.7  macallan 
    852        1.7  macallan 	/* reset FIFO */
    853        1.7  macallan 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
    854        1.7  macallan 	d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    855        1.7  macallan 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    856        1.7  macallan 	d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
    857        1.7  macallan 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    858        1.7  macallan 
    859        1.7  macallan 	/* setup colour mode */
    860        1.7  macallan 	switch (depth) {
    861        1.7  macallan 	case 8:
    862        1.7  macallan 		h = CRMFB_MODE_TYP_I8;
    863       1.11  macallan 		tile_width = 512;
    864        1.7  macallan 		break;
    865        1.7  macallan 	case 16:
    866        1.7  macallan 		h = CRMFB_MODE_TYP_ARGB5;
    867       1.11  macallan 		tile_width = 256;
    868        1.7  macallan 		break;
    869        1.7  macallan 	case 32:
    870        1.7  macallan 		h = CRMFB_MODE_TYP_RGB8;
    871       1.11  macallan 		tile_width = 128;
    872        1.7  macallan 		break;
    873        1.7  macallan 	default:
    874        1.7  macallan 		panic("Unsupported depth");
    875        1.7  macallan 	}
    876        1.7  macallan 	d = h << CRMFB_MODE_TYP_SHIFT;
    877        1.7  macallan 	d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
    878        1.7  macallan 	for (i = 0; i < (32 * 4); i += 4)
    879        1.7  macallan 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
    880        1.7  macallan 	wbflush();
    881        1.7  macallan 
    882        1.7  macallan 	/* setup tile pointer, but don't turn on DMA yet! */
    883        1.7  macallan 	h = DMAADDR(sc->sc_dmai);
    884        1.7  macallan 	d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
    885        1.7  macallan 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    886        1.7  macallan 
    887        1.7  macallan 	/* init framebuffer width and pixel size */
    888       1.11  macallan 	/*d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);*/
    889       1.11  macallan 
    890       1.11  macallan 	d = ((int)(sc->sc_width / tile_width)) <<
    891       1.11  macallan 	    CRMFB_FRM_TILESIZE_WIDTH_SHIFT;
    892  1.26.16.2     rmind 	overhang = sc->sc_width % tile_width;
    893  1.26.16.2     rmind 	if (overhang != 0) {
    894  1.26.16.2     rmind 		uint32_t val;
    895  1.26.16.2     rmind 		DPRINTF("tile width: %d\n", tile_width);
    896  1.26.16.2     rmind 		DPRINTF("overhang: %d\n", overhang);
    897  1.26.16.2     rmind 		val = (overhang * (depth >> 3)) >> 5;
    898  1.26.16.2     rmind 		DPRINTF("reg: %08x\n", val);
    899  1.26.16.2     rmind 		d |= (val & 0x1f);
    900  1.26.16.2     rmind 		DPRINTF("d: %08x\n", d);
    901  1.26.16.2     rmind 	}
    902       1.11  macallan 
    903        1.7  macallan 	switch (depth) {
    904        1.7  macallan 	case 8:
    905        1.7  macallan 		h = CRMFB_FRM_TILESIZE_DEPTH_8;
    906        1.7  macallan 		break;
    907        1.7  macallan 	case 16:
    908        1.7  macallan 		h = CRMFB_FRM_TILESIZE_DEPTH_16;
    909        1.7  macallan 		break;
    910        1.7  macallan 	case 32:
    911        1.7  macallan 		h = CRMFB_FRM_TILESIZE_DEPTH_32;
    912        1.7  macallan 		break;
    913        1.7  macallan 	default:
    914        1.7  macallan 		panic("Unsupported depth");
    915        1.7  macallan 	}
    916        1.7  macallan 	d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
    917        1.7  macallan 	crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
    918        1.7  macallan 
    919       1.11  macallan 	/*h = sc->sc_width * sc->sc_height / (512 / (depth >> 3));*/
    920       1.11  macallan 	h = sc->sc_height;
    921        1.7  macallan 	d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
    922        1.7  macallan 	crmfb_write_reg(sc, CRMFB_FRM_PIXSIZE, d);
    923        1.7  macallan 
    924        1.7  macallan 	/* turn off firmware overlay and hardware cursor */
    925        1.7  macallan 	crmfb_write_reg(sc, CRMFB_OVR_WIDTH_TILE, 0);
    926        1.7  macallan 	crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, 0);
    927        1.7  macallan 
    928        1.7  macallan 	/* turn on DMA for the framebuffer */
    929        1.7  macallan 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
    930        1.7  macallan 	d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
    931        1.7  macallan 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
    932        1.7  macallan 
    933  1.26.16.2     rmind 	/* enable drawing again */
    934  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_XY, 0);
    935  1.26.16.2     rmind 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
    936  1.26.16.2     rmind 	d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
    937  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
    938  1.26.16.2     rmind 
    939       1.10    sekiya 	/* turn off sync-on-green */
    940       1.10    sekiya 
    941  1.26.16.1     rmind 	wantsync = arcbios_GetEnvironmentVariable("SyncOnGreen");
    942       1.10    sekiya 	if ( (wantsync != NULL) && (wantsync[0] == 'n') ) {
    943       1.15  macallan 		d = ( 1 << CRMFB_VT_FLAGS_SYNC_LOW_LSB) &
    944       1.15  macallan 		    CRMFB_REG_MASK(CRMFB_VT_FLAGS_SYNC_LOW_MSB,
    945       1.15  macallan 		    CRMFB_VT_FLAGS_SYNC_LOW_LSB);
    946       1.10    sekiya 		crmfb_write_reg(sc, CRMFB_VT_FLAGS, d);
    947       1.10    sekiya 	}
    948       1.10    sekiya 
    949        1.7  macallan 	sc->sc_depth = depth;
    950       1.11  macallan 
    951       1.11  macallan 	/* finally set up the drawing engine's TLB A */
    952       1.11  macallan 	v = (DMAADDR(sc->sc_dma) >> 16) & 0xffff;
    953       1.11  macallan 	tlbptr = 0;
    954       1.15  macallan 	tx = ((sc->sc_width + (tile_width - 1)) & ~(tile_width - 1)) /
    955       1.15  macallan 	    tile_width;
    956       1.14  macallan 
    957  1.26.16.2     rmind 	DPRINTF("tx: %d\n", tx);
    958       1.21  macallan 
    959       1.21  macallan 	for (i = 0; i < 16; i++) {
    960       1.11  macallan 		reg = 0;
    961       1.11  macallan 		shift = 64;
    962       1.21  macallan 		lptr = 0;
    963       1.11  macallan 		for (j = 0; j < tx; j++) {
    964       1.11  macallan 			shift -= 16;
    965       1.11  macallan 			reg |= (((uint64_t)(v | 0x8000)) << shift);
    966       1.11  macallan 			if (shift == 0) {
    967       1.11  macallan 				shift = 64;
    968       1.11  macallan 				bus_space_write_8(sc->sc_iot, sc->sc_reh,
    969       1.21  macallan 				    CRIME_RE_TLB_A + tlbptr + lptr,
    970       1.15  macallan 				    reg);
    971  1.26.16.2     rmind 				DPRINTF("%04x: %016"PRIx64"\n", tlbptr + lptr, reg);
    972       1.21  macallan 				reg = 0;
    973       1.21  macallan 				lptr += 8;
    974       1.11  macallan 			}
    975       1.11  macallan 			v++;
    976       1.11  macallan 		}
    977       1.11  macallan 		if (shift != 64) {
    978       1.11  macallan 			bus_space_write_8(sc->sc_iot, sc->sc_reh,
    979       1.21  macallan 			    CRIME_RE_TLB_A + tlbptr + lptr, reg);
    980  1.26.16.2     rmind 			DPRINTF("%04x: %016"PRIx64"\n", tlbptr + lptr, reg);
    981       1.11  macallan 		}
    982       1.11  macallan 		tlbptr += 32;
    983       1.11  macallan 	}
    984       1.21  macallan 	sc->sc_scratch = (char *)KERNADDR(sc->sc_dma) + (0xf0000 * tx);
    985       1.11  macallan 
    986       1.21  macallan 	/* now put the last 64kB into the 1st linear TLB */
    987       1.21  macallan 	page = (sc->sc_linear >> 12) | 0x80000000;
    988       1.21  macallan 	tlbptr = 0;
    989       1.21  macallan 	for (i = 0; i < 8; i++) {
    990       1.21  macallan 		reg = ((uint64_t)page << 32) | (page + 1);
    991       1.21  macallan 		bus_space_write_8(sc->sc_iot, sc->sc_reh,
    992       1.21  macallan 		    CRIME_RE_LINEAR_A + tlbptr, reg);
    993       1.21  macallan 		page += 2;
    994       1.21  macallan 		tlbptr += 8;
    995       1.21  macallan 	}
    996       1.11  macallan 	wbflush();
    997       1.11  macallan 
    998       1.11  macallan 	/* do some very basic engine setup */
    999       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_CLIPMODE, 0);
   1000       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_SRC, 0);
   1001       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_DST, 0);
   1002       1.15  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PLANEMASK,
   1003       1.15  macallan 	    0xffffffff);
   1004       1.11  macallan 
   1005       1.11  macallan 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x20, 0);
   1006       1.11  macallan 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x28, 0);
   1007       1.11  macallan 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x30, 0);
   1008       1.11  macallan 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x38, 0);
   1009       1.11  macallan 	bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x40, 0);
   1010       1.11  macallan 
   1011       1.11  macallan 	switch (depth) {
   1012       1.11  macallan 		case 8:
   1013       1.11  macallan 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_8 |
   1014       1.11  macallan 			    DE_MODE_TYPE_CI | DE_MODE_PIXDEPTH_8;
   1015       1.11  macallan 			break;
   1016       1.11  macallan 		case 16:
   1017       1.11  macallan 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_16 |
   1018       1.11  macallan 			    DE_MODE_TYPE_RGB | DE_MODE_PIXDEPTH_16;
   1019       1.14  macallan 			break;
   1020       1.11  macallan 		case 32:
   1021       1.11  macallan 			mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_32 |
   1022       1.11  macallan 			    DE_MODE_TYPE_RGBA | DE_MODE_PIXDEPTH_32;
   1023       1.14  macallan 			break;
   1024       1.11  macallan 		default:
   1025       1.11  macallan 			panic("%s: unsuported colour depth %d\n", __func__,
   1026       1.11  macallan 			    depth);
   1027       1.11  macallan 	}
   1028       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_DST, mode);
   1029       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC, mode);
   1030       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_X, 1);
   1031       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_Y, 1);
   1032       1.15  macallan 
   1033       1.15  macallan 	/* initialize memory transfer engine */
   1034       1.15  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
   1035       1.15  macallan 	    MTE_MODE_DST_ECC |
   1036       1.15  macallan 	    (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
   1037       1.15  macallan 	    (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
   1038       1.15  macallan 	    (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
   1039       1.15  macallan 	    MTE_MODE_COPY);
   1040       1.18  macallan 	sc->sc_mte_direction = 1;
   1041       1.16  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_Y_STEP, 1);
   1042       1.16  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_Y_STEP, 1);
   1043       1.15  macallan 
   1044        1.7  macallan 	return 0;
   1045        1.7  macallan }
   1046        1.7  macallan 
   1047       1.19  macallan static void
   1048       1.18  macallan crmfb_set_mte_direction(struct crmfb_softc *sc, int dir)
   1049       1.18  macallan {
   1050       1.18  macallan 	if (dir == sc->sc_mte_direction)
   1051       1.18  macallan 		return;
   1052       1.18  macallan 
   1053       1.18  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_Y_STEP, dir);
   1054       1.18  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_Y_STEP, dir);
   1055       1.20  macallan 	sc->sc_mte_direction = dir;
   1056       1.18  macallan }
   1057       1.18  macallan 
   1058        1.7  macallan static void
   1059        1.7  macallan crmfb_setup_palette(struct crmfb_softc *sc)
   1060        1.7  macallan {
   1061        1.7  macallan 	int i;
   1062        1.7  macallan 
   1063        1.7  macallan 	for (i = 0; i < 256; i++) {
   1064        1.7  macallan 		crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
   1065        1.7  macallan 		    rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
   1066        1.7  macallan 		sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
   1067        1.7  macallan 		sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
   1068        1.7  macallan 		sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
   1069        1.7  macallan 	}
   1070        1.7  macallan }
   1071       1.11  macallan 
   1072       1.11  macallan static inline void
   1073       1.11  macallan crmfb_wait_idle(struct crmfb_softc *sc)
   1074       1.11  macallan {
   1075       1.11  macallan 	int i = 0;
   1076       1.11  macallan 
   1077       1.11  macallan 	do {
   1078       1.11  macallan 		i++;
   1079       1.11  macallan 	} while (((bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STATUS) &
   1080       1.11  macallan 		   CRIME_DE_IDLE) == 0) && (i < 100000000));
   1081       1.11  macallan 	if (i >= 100000000)
   1082       1.11  macallan 		aprint_error("crmfb_wait_idle() timed out\n");
   1083       1.11  macallan }
   1084       1.11  macallan 
   1085       1.11  macallan static void
   1086       1.11  macallan crmfb_fill_rect(struct crmfb_softc *sc, int x, int y, int width, int height,
   1087       1.11  macallan     uint32_t colour)
   1088       1.11  macallan {
   1089       1.11  macallan 	crmfb_wait_idle(sc);
   1090       1.18  macallan 	crmfb_set_mte_direction(sc, 1);
   1091       1.17  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
   1092       1.17  macallan 	    MTE_MODE_DST_ECC |
   1093       1.17  macallan 	    (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
   1094       1.17  macallan 	    (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
   1095       1.17  macallan 	    (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
   1096       1.17  macallan 	    0);
   1097       1.17  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_BG, colour);
   1098       1.17  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST0,
   1099       1.11  macallan 	    (x << 16) | (y & 0xffff));
   1100       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1101       1.17  macallan 	    CRIME_MTE_DST1 | CRIME_DE_START,
   1102       1.11  macallan 	    ((x + width - 1) << 16) | ((y + height - 1) & 0xffff));
   1103       1.11  macallan }
   1104       1.11  macallan 
   1105       1.11  macallan static void
   1106       1.11  macallan crmfb_bitblt(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
   1107       1.11  macallan     int wi, int he, uint32_t rop)
   1108       1.11  macallan {
   1109       1.11  macallan 	uint32_t prim = DE_PRIM_RECTANGLE;
   1110       1.11  macallan 	int rxa, rya, rxe, rye, rxs, rys;
   1111       1.11  macallan 	crmfb_wait_idle(sc);
   1112       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
   1113       1.11  macallan 	    DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK | DE_DRAWMODE_ROP |
   1114       1.11  macallan 	    DE_DRAWMODE_XFER_EN);
   1115       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, rop);
   1116       1.11  macallan 	if (xs < xd) {
   1117       1.11  macallan 		prim |= DE_PRIM_RL;
   1118       1.11  macallan 		rxe = xd;
   1119       1.11  macallan 		rxa = xd + wi - 1;
   1120       1.11  macallan 		rxs = xs + wi - 1;
   1121       1.11  macallan 	} else {
   1122       1.11  macallan 		prim |= DE_PRIM_LR;
   1123       1.11  macallan 		rxe = xd + wi - 1;
   1124       1.11  macallan 		rxa = xd;
   1125       1.11  macallan 		rxs = xs;
   1126       1.11  macallan 	}
   1127       1.11  macallan 	if (ys < yd) {
   1128       1.11  macallan 		prim |= DE_PRIM_BT;
   1129       1.11  macallan 		rye = yd;
   1130       1.11  macallan 		rya = yd + he - 1;
   1131       1.11  macallan 		rys = ys + he - 1;
   1132       1.11  macallan 	} else {
   1133       1.11  macallan 		prim |= DE_PRIM_TB;
   1134       1.11  macallan 		rye = yd + he - 1;
   1135       1.11  macallan 		rya = yd;
   1136       1.11  macallan 		rys = ys;
   1137       1.11  macallan 	}
   1138       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE, prim);
   1139       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC,
   1140       1.11  macallan 	    (rxs << 16) | (rys & 0xffff));
   1141       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
   1142       1.11  macallan 	    (rxa << 16) | (rya & 0xffff));
   1143       1.11  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1144       1.11  macallan 	    CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
   1145       1.11  macallan 	    (rxe << 16) | (rye & 0xffff));
   1146       1.11  macallan }
   1147       1.11  macallan 
   1148       1.15  macallan static void
   1149       1.15  macallan crmfb_scroll(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
   1150       1.15  macallan     int wi, int he)
   1151       1.15  macallan {
   1152       1.15  macallan 	int rxa, rya, rxe, rye, rxd, ryd, rxde, ryde;
   1153       1.15  macallan 
   1154       1.16  macallan 	rxa = xs;
   1155       1.16  macallan 	rxe = xs + wi - 1;
   1156       1.16  macallan 	rxd = xd;
   1157       1.16  macallan 	rxde = xd + wi - 1;
   1158       1.16  macallan 
   1159       1.15  macallan 	crmfb_wait_idle(sc);
   1160       1.16  macallan 
   1161       1.17  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
   1162       1.17  macallan 	    MTE_MODE_DST_ECC |
   1163       1.17  macallan 	    (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
   1164       1.17  macallan 	    (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
   1165       1.17  macallan 	    (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
   1166       1.17  macallan 	    MTE_MODE_COPY);
   1167       1.17  macallan 
   1168       1.15  macallan 	if (ys < yd) {
   1169       1.15  macallan 		/* bottom to top */
   1170       1.15  macallan 		rye = ys;
   1171       1.15  macallan 		rya = ys + he - 1;
   1172       1.15  macallan 		ryd = yd + he - 1;
   1173       1.15  macallan 		ryde = yd;
   1174       1.18  macallan 		crmfb_set_mte_direction(sc, -1);
   1175       1.15  macallan 	} else {
   1176       1.15  macallan 		/* top to bottom */
   1177       1.15  macallan 		rye = ys + he - 1;
   1178       1.15  macallan 		rya = ys;
   1179       1.15  macallan 		ryd = yd;
   1180       1.15  macallan 		ryde = yd + he - 1;
   1181       1.18  macallan 		crmfb_set_mte_direction(sc, 1);
   1182       1.15  macallan 	}
   1183       1.15  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC0,
   1184       1.15  macallan 	    (rxa << 16) | rya);
   1185       1.15  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC1,
   1186       1.15  macallan 	    (rxe << 16) | rye);
   1187       1.15  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1188       1.15  macallan 	    CRIME_MTE_DST0,
   1189       1.15  macallan 	    (rxd << 16) | ryd);
   1190       1.15  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST1 |
   1191       1.15  macallan 	    CRIME_DE_START,
   1192       1.15  macallan 	    (rxde << 16) | ryde);
   1193       1.15  macallan }
   1194       1.15  macallan 
   1195       1.11  macallan static void
   1196       1.11  macallan crmfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1197       1.11  macallan {
   1198       1.11  macallan 	struct rasops_info *ri = cookie;
   1199       1.11  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1200       1.11  macallan 	int32_t xs, xd, y, width, height;
   1201       1.11  macallan 
   1202       1.11  macallan 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1203       1.11  macallan 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1204       1.11  macallan 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1205       1.11  macallan 	width = ri->ri_font->fontwidth * ncols;
   1206       1.11  macallan 	height = ri->ri_font->fontheight;
   1207       1.11  macallan 	crmfb_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, 3);
   1208       1.11  macallan }
   1209       1.11  macallan 
   1210       1.11  macallan static void
   1211       1.11  macallan crmfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1212       1.11  macallan {
   1213       1.11  macallan 	struct rasops_info *ri = cookie;
   1214       1.11  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1215       1.11  macallan 	int32_t x, y, width, height, bg;
   1216       1.11  macallan 
   1217       1.11  macallan 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1218       1.11  macallan 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1219       1.11  macallan 	width = ri->ri_font->fontwidth * ncols;
   1220       1.11  macallan 	height = ri->ri_font->fontheight;
   1221       1.11  macallan 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1222       1.11  macallan 	crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
   1223       1.11  macallan }
   1224       1.11  macallan 
   1225       1.11  macallan static void
   1226       1.11  macallan crmfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1227       1.11  macallan {
   1228       1.11  macallan 	struct rasops_info *ri = cookie;
   1229       1.11  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1230       1.11  macallan 	int32_t x, ys, yd, width, height;
   1231       1.11  macallan 
   1232       1.11  macallan 	x = ri->ri_xorigin;
   1233       1.11  macallan 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1234       1.11  macallan 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1235       1.11  macallan 	width = ri->ri_emuwidth;
   1236       1.11  macallan 	height = ri->ri_font->fontheight * nrows;
   1237       1.15  macallan 
   1238       1.16  macallan 	crmfb_scroll(scr->scr_cookie, x, ys, x, yd, width, height);
   1239       1.11  macallan }
   1240       1.11  macallan 
   1241       1.11  macallan static void
   1242       1.11  macallan crmfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1243       1.11  macallan {
   1244       1.11  macallan 	struct rasops_info *ri = cookie;
   1245       1.11  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1246       1.11  macallan 	int32_t x, y, width, height, bg;
   1247       1.11  macallan 
   1248       1.11  macallan 	if ((row == 0) && (nrows == ri->ri_rows)) {
   1249       1.11  macallan 		x = y = 0;
   1250       1.11  macallan 		width = ri->ri_width;
   1251       1.11  macallan 		height = ri->ri_height;
   1252       1.11  macallan 	} else {
   1253       1.11  macallan 		x = ri->ri_xorigin;
   1254       1.11  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1255       1.11  macallan 		width = ri->ri_emuwidth;
   1256       1.11  macallan 		height = ri->ri_font->fontheight * nrows;
   1257       1.11  macallan 	}
   1258       1.11  macallan 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
   1259       1.11  macallan 	crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
   1260       1.11  macallan }
   1261       1.11  macallan 
   1262       1.11  macallan static void
   1263       1.11  macallan crmfb_cursor(void *cookie, int on, int row, int col)
   1264       1.11  macallan {
   1265       1.11  macallan 	struct rasops_info *ri = cookie;
   1266       1.11  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1267       1.11  macallan 	struct crmfb_softc *sc = scr->scr_cookie;
   1268       1.11  macallan 	int x, y, wi,he;
   1269       1.11  macallan 
   1270       1.11  macallan 	wi = ri->ri_font->fontwidth;
   1271       1.11  macallan 	he = ri->ri_font->fontheight;
   1272       1.11  macallan 
   1273       1.11  macallan 	if (ri->ri_flg & RI_CURSOR) {
   1274       1.11  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1275       1.11  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
   1276       1.11  macallan 		crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
   1277       1.11  macallan 		ri->ri_flg &= ~RI_CURSOR;
   1278       1.11  macallan 	}
   1279       1.11  macallan 
   1280       1.11  macallan 	ri->ri_crow = row;
   1281       1.11  macallan 	ri->ri_ccol = col;
   1282       1.11  macallan 
   1283       1.11  macallan 	if (on)
   1284       1.11  macallan 	{
   1285       1.11  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1286       1.11  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
   1287       1.11  macallan 		crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
   1288       1.11  macallan 		ri->ri_flg |= RI_CURSOR;
   1289       1.11  macallan 	}
   1290       1.11  macallan }
   1291       1.11  macallan 
   1292       1.11  macallan static void
   1293       1.11  macallan crmfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1294       1.11  macallan {
   1295       1.11  macallan 	struct rasops_info *ri = cookie;
   1296       1.11  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1297       1.11  macallan 	struct crmfb_softc *sc = scr->scr_cookie;
   1298  1.26.16.1     rmind 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1299  1.26.16.1     rmind 	uint32_t bg, fg;
   1300  1.26.16.1     rmind 	int x, y, wi, he, i, uc;
   1301  1.26.16.1     rmind 	uint8_t *fd8;
   1302  1.26.16.1     rmind 	uint16_t *fd16;
   1303  1.26.16.1     rmind 	void *fd;
   1304       1.11  macallan 
   1305  1.26.16.1     rmind 	wi = font->fontwidth;
   1306  1.26.16.1     rmind 	he = font->fontheight;
   1307       1.11  macallan 
   1308       1.11  macallan 	x = ri->ri_xorigin + col * wi;
   1309       1.11  macallan 	y = ri->ri_yorigin + row * he;
   1310       1.11  macallan 
   1311       1.17  macallan 	bg = ri->ri_devcmap[(attr >> 16) & 0xff];
   1312  1.26.16.1     rmind 	fg = ri->ri_devcmap[(attr >> 24) & 0xff];
   1313  1.26.16.1     rmind 	uc = c - font->firstchar;
   1314  1.26.16.1     rmind 	fd = (uint8_t *)font->data + uc * ri->ri_fontscale;
   1315       1.11  macallan 	if (c == 0x20) {
   1316       1.11  macallan 		crmfb_fill_rect(sc, x, y, wi, he, bg);
   1317       1.11  macallan 	} else {
   1318  1.26.16.1     rmind 		crmfb_wait_idle(sc);
   1319  1.26.16.1     rmind 		/* setup */
   1320  1.26.16.1     rmind 		bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, 3);
   1321  1.26.16.1     rmind 		bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FG, fg);
   1322  1.26.16.1     rmind 		bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_BG, bg);
   1323  1.26.16.1     rmind 		bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
   1324  1.26.16.1     rmind 		    DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK |
   1325  1.26.16.1     rmind 		    DE_DRAWMODE_ROP |
   1326  1.26.16.1     rmind 		    DE_DRAWMODE_OPAQUE_STIP | DE_DRAWMODE_POLY_STIP);
   1327  1.26.16.1     rmind 		bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
   1328  1.26.16.1     rmind 		    DE_PRIM_RECTANGLE | DE_PRIM_LR | DE_PRIM_TB);
   1329  1.26.16.1     rmind 		bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STIPPLE_MODE,
   1330  1.26.16.1     rmind 		    0x001f0000);
   1331  1.26.16.1     rmind 		/* now let's feed the engine */
   1332  1.26.16.1     rmind 		if (font->stride == 1) {
   1333  1.26.16.1     rmind 			/* shovel in 8 bit quantities */
   1334  1.26.16.1     rmind 			fd8 = fd;
   1335  1.26.16.1     rmind 			for (i = 0; i < he; i++) {
   1336  1.26.16.1     rmind 				/*
   1337  1.26.16.1     rmind 				 * the pipeline should be long enough to
   1338  1.26.16.1     rmind 				 * draw any character without having to wait
   1339  1.26.16.1     rmind 				 */
   1340  1.26.16.1     rmind 				bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1341  1.26.16.1     rmind 				    CRIME_DE_STIPPLE_PAT, *fd8 << 24);
   1342  1.26.16.1     rmind 				bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1343  1.26.16.1     rmind 				    CRIME_DE_X_VERTEX_0, (x << 16) | y);
   1344  1.26.16.1     rmind 				bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1345  1.26.16.1     rmind 				    CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
   1346  1.26.16.1     rmind 				    ((x + wi) << 16) | y);
   1347  1.26.16.1     rmind 				y++;
   1348  1.26.16.1     rmind 				fd8++;
   1349  1.26.16.1     rmind 			}
   1350  1.26.16.1     rmind 		} else if (font->stride == 2) {
   1351  1.26.16.1     rmind 			/* shovel in 16 bit quantities */
   1352  1.26.16.1     rmind 			fd16 = fd;
   1353  1.26.16.1     rmind 			for (i = 0; i < he; i++) {
   1354  1.26.16.1     rmind 				/*
   1355  1.26.16.1     rmind 				 * the pipeline should be long enough to
   1356  1.26.16.1     rmind 				 * draw any character without having to wait
   1357  1.26.16.1     rmind 				 */
   1358  1.26.16.1     rmind 				bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1359  1.26.16.1     rmind 				    CRIME_DE_STIPPLE_PAT, *fd16 << 16);
   1360  1.26.16.1     rmind 				bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1361  1.26.16.1     rmind 				    CRIME_DE_X_VERTEX_0, (x << 16) | y);
   1362  1.26.16.1     rmind 				bus_space_write_4(sc->sc_iot, sc->sc_reh,
   1363  1.26.16.1     rmind 				    CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
   1364  1.26.16.1     rmind 				    ((x + wi) << 16) | y);
   1365  1.26.16.1     rmind 				y++;
   1366  1.26.16.1     rmind 				fd16++;
   1367  1.26.16.1     rmind 			}
   1368  1.26.16.1     rmind 		}
   1369       1.11  macallan 	}
   1370       1.11  macallan }
   1371  1.26.16.2     rmind 
   1372  1.26.16.2     rmind static void
   1373  1.26.16.2     rmind crmfb_setup_ddc(struct crmfb_softc *sc)
   1374  1.26.16.2     rmind {
   1375  1.26.16.2     rmind 	int i;
   1376  1.26.16.2     rmind 	char edid_data[128];
   1377  1.26.16.2     rmind 
   1378  1.26.16.2     rmind 	memset(edid_data, 0, 128);
   1379  1.26.16.2     rmind 	sc->sc_i2c.ic_cookie = sc;
   1380  1.26.16.2     rmind 	sc->sc_i2c.ic_acquire_bus = crmfb_i2c_acquire_bus;
   1381  1.26.16.2     rmind 	sc->sc_i2c.ic_release_bus = crmfb_i2c_release_bus;
   1382  1.26.16.2     rmind 	sc->sc_i2c.ic_send_start = crmfb_i2c_send_start;
   1383  1.26.16.2     rmind 	sc->sc_i2c.ic_send_stop = crmfb_i2c_send_stop;
   1384  1.26.16.2     rmind 	sc->sc_i2c.ic_initiate_xfer = crmfb_i2c_initiate_xfer;
   1385  1.26.16.2     rmind 	sc->sc_i2c.ic_read_byte = crmfb_i2c_read_byte;
   1386  1.26.16.2     rmind 	sc->sc_i2c.ic_write_byte = crmfb_i2c_write_byte;
   1387  1.26.16.2     rmind 	sc->sc_i2c.ic_exec = NULL;
   1388  1.26.16.2     rmind 	i = 0;
   1389  1.26.16.2     rmind 	while (edid_data[1] == 0 && i++ < 10)
   1390  1.26.16.2     rmind 		ddc_read_edid(&sc->sc_i2c, edid_data, 128);
   1391  1.26.16.2     rmind 	if (i > 1)
   1392  1.26.16.2     rmind 		aprint_debug_dev(sc->sc_dev,
   1393  1.26.16.2     rmind 		    "had to try %d times to get EDID data\n", i);
   1394  1.26.16.2     rmind 	if (i < 11) {
   1395  1.26.16.2     rmind 		edid_parse(edid_data, &sc->sc_edid_info);
   1396  1.26.16.2     rmind 		edid_print(&sc->sc_edid_info);
   1397  1.26.16.2     rmind 	}
   1398  1.26.16.2     rmind }
   1399  1.26.16.2     rmind 
   1400  1.26.16.2     rmind /* I2C bitbanging */
   1401  1.26.16.2     rmind static void
   1402  1.26.16.2     rmind crmfb_i2cbb_set_bits(void *cookie, uint32_t bits)
   1403  1.26.16.2     rmind {
   1404  1.26.16.2     rmind 	struct crmfb_softc *sc = cookie;
   1405  1.26.16.2     rmind 
   1406  1.26.16.2     rmind 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_I2C_VGA, bits ^ 3);
   1407  1.26.16.2     rmind }
   1408  1.26.16.2     rmind 
   1409  1.26.16.2     rmind static void
   1410  1.26.16.2     rmind crmfb_i2cbb_set_dir(void *cookie, uint32_t dir)
   1411  1.26.16.2     rmind {
   1412  1.26.16.2     rmind 
   1413  1.26.16.2     rmind 	/* Nothing to do */
   1414  1.26.16.2     rmind }
   1415  1.26.16.2     rmind 
   1416  1.26.16.2     rmind static uint32_t
   1417  1.26.16.2     rmind crmfb_i2cbb_read(void *cookie)
   1418  1.26.16.2     rmind {
   1419  1.26.16.2     rmind 	struct crmfb_softc *sc = cookie;
   1420  1.26.16.2     rmind 
   1421  1.26.16.2     rmind 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_I2C_VGA) ^ 3;
   1422  1.26.16.2     rmind }
   1423  1.26.16.2     rmind 
   1424  1.26.16.2     rmind /* higher level I2C stuff */
   1425  1.26.16.2     rmind static int
   1426  1.26.16.2     rmind crmfb_i2c_acquire_bus(void *cookie, int flags)
   1427  1.26.16.2     rmind {
   1428  1.26.16.2     rmind 
   1429  1.26.16.2     rmind 	/* private bus */
   1430  1.26.16.2     rmind 	return 0;
   1431  1.26.16.2     rmind }
   1432  1.26.16.2     rmind 
   1433  1.26.16.2     rmind static void
   1434  1.26.16.2     rmind crmfb_i2c_release_bus(void *cookie, int flags)
   1435  1.26.16.2     rmind {
   1436  1.26.16.2     rmind 
   1437  1.26.16.2     rmind 	/* private bus */
   1438  1.26.16.2     rmind }
   1439  1.26.16.2     rmind 
   1440  1.26.16.2     rmind static int
   1441  1.26.16.2     rmind crmfb_i2c_send_start(void *cookie, int flags)
   1442  1.26.16.2     rmind {
   1443  1.26.16.2     rmind 
   1444  1.26.16.2     rmind 	return i2c_bitbang_send_start(cookie, flags, &crmfb_i2cbb_ops);
   1445  1.26.16.2     rmind }
   1446  1.26.16.2     rmind 
   1447  1.26.16.2     rmind static int
   1448  1.26.16.2     rmind crmfb_i2c_send_stop(void *cookie, int flags)
   1449  1.26.16.2     rmind {
   1450  1.26.16.2     rmind 
   1451  1.26.16.2     rmind 	return i2c_bitbang_send_stop(cookie, flags, &crmfb_i2cbb_ops);
   1452  1.26.16.2     rmind }
   1453  1.26.16.2     rmind 
   1454  1.26.16.2     rmind static int
   1455  1.26.16.2     rmind crmfb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
   1456  1.26.16.2     rmind {
   1457  1.26.16.2     rmind 
   1458  1.26.16.2     rmind 	return i2c_bitbang_initiate_xfer(cookie, addr, flags,
   1459  1.26.16.2     rmind 	    &crmfb_i2cbb_ops);
   1460  1.26.16.2     rmind }
   1461  1.26.16.2     rmind 
   1462  1.26.16.2     rmind static int
   1463  1.26.16.2     rmind crmfb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
   1464  1.26.16.2     rmind {
   1465  1.26.16.2     rmind 
   1466  1.26.16.2     rmind 	return i2c_bitbang_read_byte(cookie, valp, flags, &crmfb_i2cbb_ops);
   1467  1.26.16.2     rmind }
   1468  1.26.16.2     rmind 
   1469  1.26.16.2     rmind static int
   1470  1.26.16.2     rmind crmfb_i2c_write_byte(void *cookie, uint8_t val, int flags)
   1471  1.26.16.2     rmind {
   1472  1.26.16.2     rmind 
   1473  1.26.16.2     rmind 	return i2c_bitbang_write_byte(cookie, val, flags, &crmfb_i2cbb_ops);
   1474  1.26.16.2     rmind }
   1475  1.26.16.2     rmind 
   1476  1.26.16.2     rmind /* mode setting stuff */
   1477  1.26.16.2     rmind static uint32_t
   1478  1.26.16.2     rmind calc_pll(int f_out)
   1479  1.26.16.2     rmind {
   1480  1.26.16.2     rmind 	uint32_t ret;
   1481  1.26.16.2     rmind 	int f_in = 20000;	/* 20MHz in */
   1482  1.26.16.2     rmind 	int M, N, P;
   1483  1.26.16.2     rmind 	int error, div, best = 9999999;
   1484  1.26.16.2     rmind 	int ff1, ff2;
   1485  1.26.16.2     rmind 	int MM = 0, NN = 0, PP = 0, ff = 0;
   1486  1.26.16.2     rmind 
   1487  1.26.16.2     rmind 	/* f_out = M * f_in / (N * (1 << P) */
   1488  1.26.16.2     rmind 
   1489  1.26.16.2     rmind 	for (P = 0; P < 4; P++) {
   1490  1.26.16.2     rmind 		for (N = 64; N > 0; N--) {
   1491  1.26.16.2     rmind 			div = N * (1 << P);
   1492  1.26.16.2     rmind 			M = f_out * div / f_in;
   1493  1.26.16.2     rmind 			if ((M < 257) && (M > 100)) {
   1494  1.26.16.2     rmind 				ff1 = M * f_in / div;
   1495  1.26.16.2     rmind 				ff2 = (M + 1) * f_in / div;
   1496  1.26.16.2     rmind 				error = abs(ff1 - f_out);
   1497  1.26.16.2     rmind 				if (error < best) {
   1498  1.26.16.2     rmind 					MM = M;
   1499  1.26.16.2     rmind 					NN = N;
   1500  1.26.16.2     rmind 					PP = P;
   1501  1.26.16.2     rmind 					ff = ff1;
   1502  1.26.16.2     rmind 					best = error;
   1503  1.26.16.2     rmind 				}
   1504  1.26.16.2     rmind 				error = abs(ff2 - f_out);
   1505  1.26.16.2     rmind 				if ((error < best) && ( M < 256)){
   1506  1.26.16.2     rmind 					MM = M + 1;
   1507  1.26.16.2     rmind 					NN = N;
   1508  1.26.16.2     rmind 					PP = P;
   1509  1.26.16.2     rmind 					ff = ff2;
   1510  1.26.16.2     rmind 					best = error;
   1511  1.26.16.2     rmind 				}
   1512  1.26.16.2     rmind 			}
   1513  1.26.16.2     rmind 		}
   1514  1.26.16.2     rmind 	}
   1515  1.26.16.2     rmind 	DPRINTF("%d: M %d N %d P %d -> %d\n", f_out, MM, NN, PP, ff);
   1516  1.26.16.2     rmind 	/* now shove the parameters into the register's format */
   1517  1.26.16.2     rmind 	ret = (MM - 1) | ((NN - 1) << 8) | (P << 14);
   1518  1.26.16.2     rmind 	return ret;
   1519  1.26.16.2     rmind }
   1520  1.26.16.2     rmind 
   1521  1.26.16.2     rmind static int
   1522  1.26.16.2     rmind crmfb_set_mode(struct crmfb_softc *sc, const struct videomode *mode)
   1523  1.26.16.2     rmind {
   1524  1.26.16.2     rmind 	uint32_t d, dc;
   1525  1.26.16.2     rmind 	int tmp, diff;
   1526  1.26.16.2     rmind 
   1527  1.26.16.2     rmind 	if ((mode->hdisplay % 32) != 0) {
   1528  1.26.16.2     rmind 		aprint_error_dev(sc->sc_dev, "hdisplay (%d) is not a multiple of 32\n", mode->hdisplay);
   1529  1.26.16.2     rmind 		return FALSE;
   1530  1.26.16.2     rmind 	}
   1531  1.26.16.2     rmind 	if (mode->dot_clock > 140000) {
   1532  1.26.16.2     rmind 		aprint_error_dev(sc->sc_dev, "requested dot clock is too high ( %d MHz )\n", mode->dot_clock / 1000);
   1533  1.26.16.2     rmind 		return FALSE;
   1534  1.26.16.2     rmind 	}
   1535  1.26.16.2     rmind 
   1536  1.26.16.2     rmind 	/* disable DMA */
   1537  1.26.16.2     rmind 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
   1538  1.26.16.2     rmind 	d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
   1539  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d);
   1540  1.26.16.2     rmind 	DELAY(50000);
   1541  1.26.16.2     rmind 	d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
   1542  1.26.16.2     rmind 	d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
   1543  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
   1544  1.26.16.2     rmind 	DELAY(50000);
   1545  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_DID_CONTROL, d);
   1546  1.26.16.2     rmind 	DELAY(50000);
   1547  1.26.16.2     rmind 
   1548  1.26.16.2     rmind 	if (!crmfb_wait_dma_idle(sc))
   1549  1.26.16.2     rmind 		aprint_error("crmfb: crmfb_wait_dma_idle timed out\n");
   1550  1.26.16.2     rmind 
   1551  1.26.16.2     rmind 	/* ok, now we're good to go */
   1552  1.26.16.2     rmind 	dc = calc_pll(mode->dot_clock);
   1553  1.26.16.2     rmind 
   1554  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_XY, 1 << CRMFB_VT_XY_FREEZE_SHIFT);
   1555  1.26.16.2     rmind 	delay(1000);
   1556  1.26.16.2     rmind 
   1557  1.26.16.2     rmind 	/* set the dot clock pll but don't start it yet */
   1558  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_DOTCLOCK, dc);
   1559  1.26.16.2     rmind 	delay(10000);
   1560  1.26.16.2     rmind 
   1561  1.26.16.2     rmind 	/* pixel counter */
   1562  1.26.16.2     rmind 	d = mode->htotal | (mode->vtotal << 12);
   1563  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_XYMAX, d);
   1564  1.26.16.2     rmind 
   1565  1.26.16.2     rmind 	/* video timings */
   1566  1.26.16.2     rmind 	d = mode->vsync_end | (mode->vsync_start << 12);
   1567  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_VSYNC, d);
   1568  1.26.16.2     rmind 
   1569  1.26.16.2     rmind 	d = mode->hsync_end | (mode->hsync_start << 12);
   1570  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_HSYNC, d);
   1571  1.26.16.2     rmind 
   1572  1.26.16.2     rmind 	d = mode->vtotal | (mode->vdisplay << 12);
   1573  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_VBLANK, d);
   1574  1.26.16.2     rmind 
   1575  1.26.16.2     rmind 	d = (mode->htotal - 5) | ((mode->hdisplay - 5) << 12);
   1576  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_HBLANK, d);
   1577  1.26.16.2     rmind 
   1578  1.26.16.2     rmind 	d = mode->vtotal | (mode->vdisplay << 12);
   1579  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_VCMAP, d);
   1580  1.26.16.2     rmind 	d = mode->htotal | (mode->hdisplay << 12);
   1581  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_HCMAP, d);
   1582  1.26.16.2     rmind 
   1583  1.26.16.2     rmind 	d = 0;
   1584  1.26.16.2     rmind 	if (mode->flags & VID_NHSYNC) d |= CRMFB_VT_FLAGS_HDRV_INVERT;
   1585  1.26.16.2     rmind 	if (mode->flags & VID_NVSYNC) d |= CRMFB_VT_FLAGS_VDRV_INVERT;
   1586  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_FLAGS, d);
   1587  1.26.16.2     rmind 
   1588  1.26.16.2     rmind 	diff = -abs(mode->vtotal - mode->vdisplay - 1);
   1589  1.26.16.2     rmind 	d = ((uint32_t)diff << 12) & 0x00fff000;
   1590  1.26.16.2     rmind 	d |= (mode->htotal - 20);
   1591  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_DID_STARTXY, d);
   1592  1.26.16.2     rmind 
   1593  1.26.16.2     rmind 	d = ((uint32_t)(diff + 1) << 12) & 0x00fff000;
   1594  1.26.16.2     rmind 	d |= (mode->htotal - 54);
   1595  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_CRS_STARTXY, d);
   1596  1.26.16.2     rmind 
   1597  1.26.16.2     rmind 	d = ((uint32_t)diff << 12) & 0x00fff000;
   1598  1.26.16.2     rmind 	d |= (mode->htotal - 4);
   1599  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_VC_STARTXY, d);
   1600  1.26.16.2     rmind 
   1601  1.26.16.2     rmind 	tmp = mode->htotal - 19;
   1602  1.26.16.2     rmind 	d = tmp << 12;
   1603  1.26.16.2     rmind 	d |= ((tmp + mode->hdisplay - 2) % mode->htotal);
   1604  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_HPIX_EN, d);
   1605  1.26.16.2     rmind 
   1606  1.26.16.2     rmind 	d = mode->vdisplay | (mode->vtotal << 12);
   1607  1.26.16.2     rmind 	crmfb_write_reg(sc, CRMFB_VT_VPIX_EN, d);
   1608  1.26.16.2     rmind 
   1609  1.26.16.2     rmind 	sc->sc_width = mode->hdisplay;
   1610  1.26.16.2     rmind 	sc->sc_height = mode->vdisplay;
   1611  1.26.16.2     rmind 
   1612  1.26.16.2     rmind 	return TRUE;
   1613  1.26.16.2     rmind }
   1614  1.26.16.2     rmind 
   1615