Home | History | Annotate | Line # | Download | only in dev
gftfb.c revision 1.12
      1 /*	$NetBSD: gftfb.c,v 1.12 2024/03/28 12:50:31 macallan Exp $	*/
      2 
      3 /*	$OpenBSD: sti_pci.c,v 1.7 2009/02/06 22:51:04 miod Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 2006, 2007 Miodrag Vallat.
      7  ^                     2024 Michael Lorenz
      8  *
      9  * Permission to use, copy, modify, and distribute this software for any
     10  * purpose with or without fee is hereby granted, provided that the above
     11  * copyright notice, this permission notice, and the disclaimer below
     12  * appear in all copies.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     21  */
     22 
     23 /*
     24  * a native driver for HP Visualize EG PCI graphics cards
     25  * STI portions are from Miodrag Vallat's sti_pci.c
     26  */
     27 
     28 #include <sys/param.h>
     29 #include <sys/systm.h>
     30 #include <sys/kmem.h>
     31 #include <sys/device.h>
     32 #include <sys/mutex.h>
     33 
     34 #include <dev/pci/pcivar.h>
     35 #include <dev/pci/pcireg.h>
     36 #include <dev/pci/pcidevs.h>
     37 #include <dev/pci/pciio.h>
     38 
     39 #include <dev/wscons/wsdisplayvar.h>
     40 #include <dev/wscons/wsconsio.h>
     41 #include <dev/wsfont/wsfont.h>
     42 #include <dev/rasops/rasops.h>
     43 #include <dev/wscons/wsdisplay_vconsvar.h>
     44 #include <dev/pci/wsdisplay_pci.h>
     45 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     46 
     47 #include <dev/ic/stireg.h>
     48 #include <dev/ic/stivar.h>
     49 
     50 #ifdef STIDEBUG
     51 #define	DPRINTF(s)	do {	\
     52 	if (stidebug)		\
     53 		printf s;	\
     54 } while(0)
     55 
     56 extern int stidebug;
     57 #else
     58 #define	DPRINTF(s)	/* */
     59 #endif
     60 
     61 int	gftfb_match(device_t, cfdata_t, void *);
     62 void	gftfb_attach(device_t, device_t, void *);
     63 
     64 struct	gftfb_softc {
     65 	device_t		sc_dev;
     66 	pci_chipset_tag_t	sc_pc;
     67 	pcitag_t		sc_tag;
     68 
     69 	/* stuff we need in order to use the STI ROM */
     70 	struct sti_softc	sc_base;
     71 	struct sti_screen 	sc_scr;
     72 	bus_space_handle_t	sc_romh;
     73 
     74 	int sc_width, sc_height;
     75 	int sc_locked;
     76 	struct vcons_screen sc_console_screen;
     77 	struct wsscreen_descr sc_defaultscreen_descr;
     78 	const struct wsscreen_descr *sc_screens[1];
     79 	struct wsscreen_list sc_screenlist;
     80 	struct vcons_data vd;
     81 	int sc_mode;
     82 	void (*sc_putchar)(void *, int, int, u_int, long);
     83 	u_char sc_cmap_red[256];
     84 	u_char sc_cmap_green[256];
     85 	u_char sc_cmap_blue[256];
     86 	kmutex_t sc_hwlock;
     87 	uint32_t sc_hwmode;
     88 #define HW_FB	0
     89 #define HW_FILL	1
     90 #define HW_BLIT	2
     91 	uint32_t sc_rect_colour, sc_rect_height;
     92 	/* cursor stuff */
     93 	int sc_cursor_x, sc_cursor_y;
     94 	int sc_hot_x, sc_hot_y, sc_enabled;
     95 	int sc_video_on;
     96 	glyphcache sc_gc;
     97 };
     98 
     99 CFATTACH_DECL_NEW(gftfb, sizeof(struct gftfb_softc),
    100     gftfb_match, gftfb_attach, NULL, NULL);
    101 
    102 int	gftfb_readbar(struct sti_softc *, struct pci_attach_args *, u_int, int);
    103 int	gftfb_check_rom(struct gftfb_softc *, struct pci_attach_args *);
    104 void	gftfb_enable_rom(struct sti_softc *);
    105 void	gftfb_disable_rom(struct sti_softc *);
    106 void	gftfb_enable_rom_internal(struct gftfb_softc *);
    107 void	gftfb_disable_rom_internal(struct gftfb_softc *);
    108 
    109 void 	gftfb_setup(struct gftfb_softc *);
    110 
    111 #define	ngle_bt458_write(memt, memh, r, v) \
    112 	bus_space_write_stream_4(memt, memh, NGLE_REG_RAMDAC + ((r) << 2), (v) << 24)
    113 
    114 
    115 /* XXX these really need to go into their own header */
    116 int	sti_pci_is_console(struct pci_attach_args *, bus_addr_t *);
    117 int	sti_rom_setup(struct sti_rom *, bus_space_tag_t, bus_space_tag_t,
    118 	    bus_space_handle_t, bus_addr_t *, u_int);
    119 int	sti_screen_setup(struct sti_screen *, int);
    120 void	sti_describe_screen(struct sti_softc *, struct sti_screen *);
    121 
    122 #define PCI_ROM_SIZE(mr)                                                \
    123             (PCI_MAPREG_ROM_ADDR(mr) & -PCI_MAPREG_ROM_ADDR(mr))
    124 
    125 /* wsdisplay stuff */
    126 static int	gftfb_ioctl(void *, void *, u_long, void *, int,
    127 			     struct lwp *);
    128 static paddr_t	gftfb_mmap(void *, void *, off_t, int);
    129 static void	gftfb_init_screen(void *, struct vcons_screen *, int, long *);
    130 
    131 static int	gftfb_putcmap(struct gftfb_softc *, struct wsdisplay_cmap *);
    132 static int 	gftfb_getcmap(struct gftfb_softc *, struct wsdisplay_cmap *);
    133 static void	gftfb_restore_palette(struct gftfb_softc *);
    134 static int 	gftfb_putpalreg(struct gftfb_softc *, uint8_t, uint8_t,
    135 			    uint8_t, uint8_t);
    136 
    137 static void	gftfb_rectfill(struct gftfb_softc *, int, int, int, int,
    138 			    uint32_t);
    139 static void	gftfb_bitblt(void *, int, int, int, int, int,
    140 			    int, int);
    141 
    142 static void	gftfb_cursor(void *, int, int, int);
    143 static void	gftfb_putchar(void *, int, int, u_int, long);
    144 static void	gftfb_copycols(void *, int, int, int, int);
    145 static void	gftfb_erasecols(void *, int, int, int, long);
    146 static void	gftfb_copyrows(void *, int, int, int);
    147 static void	gftfb_eraserows(void *, int, int, long);
    148 
    149 static void	gftfb_move_cursor(struct gftfb_softc *, int, int);
    150 static int	gftfb_do_cursor(struct gftfb_softc *, struct wsdisplay_cursor *);
    151 
    152 static void	gftfb_set_video(struct gftfb_softc *, int);
    153 
    154 struct wsdisplay_accessops gftfb_accessops = {
    155 	gftfb_ioctl,
    156 	gftfb_mmap,
    157 	NULL,	/* alloc_screen */
    158 	NULL,	/* free_screen */
    159 	NULL,	/* show_screen */
    160 	NULL, 	/* load_font */
    161 	NULL,	/* pollc */
    162 	NULL	/* scroll */
    163 };
    164 
    165 #define BA(F,C,S,A,J,B,I)						\
    166 	(((F)<<31)|((C)<<27)|((S)<<24)|((A)<<21)|((J)<<16)|((B)<<12)|(I))
    167 
    168 #define IBOvals(R,M,X,S,D,L,B,F)					\
    169 	(((R)<<8)|((M)<<16)|((X)<<24)|((S)<<29)|((D)<<28)|((L)<<31)|((B)<<1)|(F))
    170 
    171 #define	    IndexedDcd	0	/* Pixel data is indexed (pseudo) color */
    172 #define	    Otc04	2	/* Pixels in each longword transfer (4) */
    173 #define	    Otc32	5	/* Pixels in each longword transfer (32) */
    174 #define	    Ots08	3	/* Each pixel is size (8)d transfer (1) */
    175 #define	    OtsIndirect	6	/* Each bit goes through FG/BG color(8) */
    176 #define	    AddrLong	5	/* FB address is Long aligned (pixel) */
    177 #define	    BINovly	0x2	/* 8 bit overlay */
    178 #define	    BINapp0I	0x0	/* Application Buffer 0, Indexed */
    179 #define	    BINapp1I	0x1	/* Application Buffer 1, Indexed */
    180 #define	    BINapp0F8	0xa	/* Application Buffer 0, Fractional 8-8-8 */
    181 #define	    BINattr	0xd	/* Attribute Bitmap */
    182 #define	    RopSrc 	0x3
    183 #define	    RopInv 	0xc
    184 #define	    BitmapExtent08  3	/* Each write hits ( 8) bits in depth */
    185 #define	    BitmapExtent32  5	/* Each write hits (32) bits in depth */
    186 #define	    DataDynamic	    0	/* Data register reloaded by direct access */
    187 #define	    MaskDynamic	    1	/* Mask register reloaded by direct access */
    188 #define	    MaskOtc	    0	/* Mask contains Object Count valid bits */
    189 
    190 static inline void gftfb_wait_fifo(struct gftfb_softc *, uint32_t);
    191 
    192 int
    193 gftfb_match(device_t parent, cfdata_t cf, void *aux)
    194 {
    195 	struct pci_attach_args *paa = aux;
    196 
    197 	if (PCI_VENDOR(paa->pa_id) != PCI_VENDOR_HP)
    198 		return 0;
    199 
    200 	if (PCI_PRODUCT(paa->pa_id) == PCI_PRODUCT_HP_VISUALIZE_EG)
    201 		return 10;	/* beat out sti at pci */
    202 
    203 	return 0;
    204 }
    205 
    206 void
    207 gftfb_attach(device_t parent, device_t self, void *aux)
    208 {
    209 	struct gftfb_softc *sc = device_private(self);
    210 	struct pci_attach_args *paa = aux;
    211 	struct sti_rom *rom;
    212 	struct rasops_info *ri;
    213 	struct wsemuldisplaydev_attach_args aa;
    214 	unsigned long defattr = 0;
    215 	int ret, is_console = 0, i, j;
    216 	uint8_t cmap[768];
    217 
    218 	sc->sc_dev = self;
    219 
    220 	sc->sc_pc = paa->pa_pc;
    221 	sc->sc_tag = paa->pa_tag;
    222 	sc->sc_base.sc_dev = self;
    223 	sc->sc_base.sc_enable_rom = gftfb_enable_rom;
    224 	sc->sc_base.sc_disable_rom = gftfb_disable_rom;
    225 
    226 	/* we can *not* be interrupted when doing colour map accesses */
    227 	mutex_init(&sc->sc_hwlock, MUTEX_DEFAULT, IPL_HIGH);
    228 
    229 	aprint_normal("\n");
    230 
    231 	if (gftfb_check_rom(sc, paa) != 0)
    232 		return;
    233 
    234 	ret = sti_pci_is_console(paa, sc->sc_base. bases);
    235 	if (ret != 0) {
    236 		sc->sc_base.sc_flags |= STI_CONSOLE;
    237 		is_console = 1;
    238 	}
    239 	rom = (struct sti_rom *)kmem_zalloc(sizeof(*rom), KM_SLEEP);
    240 	rom->rom_softc = &sc->sc_base;
    241 	ret = sti_rom_setup(rom, paa->pa_iot, paa->pa_memt, sc->sc_romh, sc->sc_base.bases, STI_CODEBASE_MAIN);
    242 	if (ret != 0) {
    243 		kmem_free(rom, sizeof(*rom));
    244 		return;
    245 	}
    246 
    247 	sc->sc_base.sc_rom = rom;
    248 
    249 	sc->sc_scr.scr_rom = sc->sc_base.sc_rom;
    250 	ret = sti_screen_setup(&sc->sc_scr, STI_FBMODE);
    251 
    252 	sc->sc_width = sc->sc_scr.scr_cfg.scr_width;
    253 	sc->sc_height = sc->sc_scr.scr_cfg.scr_height;
    254 	sc->sc_rect_colour = 0xf0000000;
    255 	sc->sc_rect_height = 0;
    256 
    257 	aprint_normal_dev(sc->sc_dev, "%s at %dx%d\n", sc->sc_scr.name,
    258 	    sc->sc_width, sc->sc_height);
    259 	gftfb_setup(sc);
    260 
    261 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    262 		"default",
    263 		0, 0,
    264 		NULL,
    265 		8, 16,
    266 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    267 		      WSSCREEN_RESIZE,
    268 		NULL
    269 	};
    270 
    271 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    272 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    273 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    274 	sc->sc_locked = 0;
    275 
    276 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    277 	    &gftfb_accessops);
    278 	sc->vd.init_screen = gftfb_init_screen;
    279 	sc->vd.show_screen_cookie = &sc->sc_gc;
    280 	sc->vd.show_screen_cb = glyphcache_adapt;
    281 
    282 	ri = &sc->sc_console_screen.scr_ri;
    283 
    284 	sc->sc_gc.gc_bitblt = gftfb_bitblt;
    285 	sc->sc_gc.gc_blitcookie = sc;
    286 	sc->sc_gc.gc_rop = RopSrc;
    287 
    288 	if (is_console) {
    289 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    290 		    &defattr);
    291 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    292 
    293 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    294 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    295 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    296 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    297 
    298 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    299 				sc->sc_scr.fbheight - sc->sc_height - 5,
    300 				sc->sc_scr.fbwidth,
    301 				ri->ri_font->fontwidth,
    302 				ri->ri_font->fontheight,
    303 				defattr);
    304 
    305 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    306 		    defattr);
    307 
    308 		gftfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    309 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    310 
    311 		vcons_replay_msgbuf(&sc->sc_console_screen);
    312 	} else {
    313 		/*
    314 		 * since we're not the console we can postpone the rest
    315 		 * until someone actually allocates a screen for us
    316 		 */
    317 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    318 			/* do some minimal setup to avoid weirdnesses later */
    319 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    320 			    &defattr);
    321 		} else
    322 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    323 
    324 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    325 				sc->sc_scr.fbheight - sc->sc_height - 5,
    326 				sc->sc_scr.fbwidth,
    327 				ri->ri_font->fontwidth,
    328 				ri->ri_font->fontheight,
    329 				defattr);
    330 	}
    331 
    332 	j = 0;
    333 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    334 	for (i = 0; i < 256; i++) {
    335 		sc->sc_cmap_red[i] = cmap[j];
    336 		sc->sc_cmap_green[i] = cmap[j + 1];
    337 		sc->sc_cmap_blue[i] = cmap[j + 2];
    338 		gftfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    339 		j += 3;
    340 	}
    341 
    342 	/* no suspend/resume support yet */
    343 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
    344 		aprint_error_dev(sc->sc_dev,
    345 		    "couldn't establish power handler\n");
    346 
    347 	aa.console = is_console;
    348 	aa.scrdata = &sc->sc_screenlist;
    349 	aa.accessops = &gftfb_accessops;
    350 	aa.accesscookie = &sc->vd;
    351 
    352 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
    353 }
    354 
    355 /*
    356  * Grovel the STI ROM image.
    357  */
    358 int
    359 gftfb_check_rom(struct gftfb_softc *spc, struct pci_attach_args *pa)
    360 {
    361 	struct sti_softc *sc = &spc->sc_base;
    362 	pcireg_t address, mask;
    363 	bus_space_handle_t romh;
    364 	bus_size_t romsize, subsize, stiromsize;
    365 	bus_addr_t selected, offs, suboffs;
    366 	uint32_t tmp;
    367 	int i;
    368 	int rc;
    369 
    370 	/* sort of inline sti_pci_enable_rom(sc) */
    371 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
    372 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
    373 	    ~PCI_MAPREG_ROM_ENABLE);
    374 	mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
    375 	address |= PCI_MAPREG_ROM_ENABLE;
    376 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, address);
    377 	sc->sc_flags |= STI_ROM_ENABLED;
    378 	/*
    379 	 * Map the complete ROM for now.
    380 	 */
    381 
    382 	romsize = PCI_ROM_SIZE(mask);
    383 	DPRINTF(("%s: mapping rom @ %lx for %lx\n", __func__,
    384 	    (long)PCI_MAPREG_ROM_ADDR(address), (long)romsize));
    385 
    386 	rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address), romsize,
    387 	    0, &romh);
    388 	if (rc != 0) {
    389 		aprint_error_dev(sc->sc_dev, "can't map PCI ROM (%d)\n", rc);
    390 		goto fail2;
    391 	}
    392 
    393 	gftfb_disable_rom_internal(spc);
    394 	/*
    395 	 * Iterate over the ROM images, pick the best candidate.
    396 	 */
    397 
    398 	selected = (bus_addr_t)-1;
    399 	for (offs = 0; offs < romsize; offs += subsize) {
    400 		gftfb_enable_rom_internal(spc);
    401 		/*
    402 		 * Check for a valid ROM header.
    403 		 */
    404 		tmp = bus_space_read_4(pa->pa_memt, romh, offs + 0);
    405 		tmp = le32toh(tmp);
    406 		if (tmp != 0x55aa0000) {
    407 			gftfb_disable_rom_internal(spc);
    408 			if (offs == 0) {
    409 				aprint_error_dev(sc->sc_dev,
    410 				    "invalid PCI ROM header signature (%08x)\n",
    411 				     tmp);
    412 				rc = EINVAL;
    413 			}
    414 			break;
    415 		}
    416 
    417 		/*
    418 		 * Check ROM type.
    419 		 */
    420 		tmp = bus_space_read_4(pa->pa_memt, romh, offs + 4);
    421 		tmp = le32toh(tmp);
    422 		if (tmp != 0x00000001) {	/* 1 == STI ROM */
    423 			gftfb_disable_rom_internal(spc);
    424 			if (offs == 0) {
    425 				aprint_error_dev(sc->sc_dev,
    426 				    "invalid PCI ROM type (%08x)\n", tmp);
    427 				rc = EINVAL;
    428 			}
    429 			break;
    430 		}
    431 
    432 		subsize = (bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
    433 		    offs + 0x0c);
    434 		subsize <<= 9;
    435 
    436 #ifdef STIDEBUG
    437 		gftfb_disable_rom_internal(spc);
    438 		DPRINTF(("ROM offset %08x size %08x type %08x",
    439 		    (u_int)offs, (u_int)subsize, tmp));
    440 		gftfb_enable_rom_internal(spc);
    441 #endif
    442 
    443 		/*
    444 		 * Check for a valid ROM data structure.
    445 		 * We do not need it except to know what architecture the ROM
    446 		 * code is for.
    447 		 */
    448 
    449 		suboffs = offs +(bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
    450 		    offs + 0x18);
    451 		tmp = bus_space_read_4(pa->pa_memt, romh, suboffs + 0);
    452 		tmp = le32toh(tmp);
    453 		if (tmp != 0x50434952) {	/* PCIR */
    454 			gftfb_disable_rom_internal(spc);
    455 			if (offs == 0) {
    456 				aprint_error_dev(sc->sc_dev, "invalid PCI data"
    457 				    " signature (%08x)\n", tmp);
    458 				rc = EINVAL;
    459 			} else {
    460 				DPRINTF((" invalid PCI data signature %08x\n",
    461 				    tmp));
    462 				continue;
    463 			}
    464 		}
    465 
    466 		tmp = bus_space_read_1(pa->pa_memt, romh, suboffs + 0x14);
    467 		gftfb_disable_rom_internal(spc);
    468 		DPRINTF((" code %02x", tmp));
    469 
    470 		switch (tmp) {
    471 #ifdef __hppa__
    472 		case 0x10:
    473 			if (selected == (bus_addr_t)-1)
    474 				selected = offs;
    475 			break;
    476 #endif
    477 #ifdef __i386__
    478 		case 0x00:
    479 			if (selected == (bus_addr_t)-1)
    480 				selected = offs;
    481 			break;
    482 #endif
    483 		default:
    484 #ifdef STIDEBUG
    485 			DPRINTF((" (wrong architecture)"));
    486 #endif
    487 			break;
    488 		}
    489 		DPRINTF(("%s\n", selected == offs ? " -> SELECTED" : ""));
    490 	}
    491 
    492 	if (selected == (bus_addr_t)-1) {
    493 		if (rc == 0) {
    494 			aprint_error_dev(sc->sc_dev, "found no ROM with "
    495 			    "correct microcode architecture\n");
    496 			rc = ENOEXEC;
    497 		}
    498 		goto fail;
    499 	}
    500 
    501 	/*
    502 	 * Read the STI region BAR assignments.
    503 	 */
    504 
    505 	gftfb_enable_rom_internal(spc);
    506 	offs = selected +
    507 	    (bus_addr_t)bus_space_read_2(pa->pa_memt, romh, selected + 0x0e);
    508 	for (i = 0; i < STI_REGION_MAX; i++) {
    509 		rc = gftfb_readbar(sc, pa, i,
    510 		    bus_space_read_1(pa->pa_memt, romh, offs + i));
    511 		if (rc != 0)
    512 			goto fail;
    513 	}
    514 
    515 	/*
    516 	 * Find out where the STI ROM itself lies, and its size.
    517 	 */
    518 
    519 	offs = selected +
    520 	    (bus_addr_t)bus_space_read_4(pa->pa_memt, romh, selected + 0x08);
    521 	stiromsize = (bus_addr_t)bus_space_read_4(pa->pa_memt, romh,
    522 	    offs + 0x18);
    523 	stiromsize = le32toh(stiromsize);
    524 	gftfb_disable_rom_internal(spc);
    525 
    526 	/*
    527 	 * Replace our mapping with a smaller mapping of only the area
    528 	 * we are interested in.
    529 	 */
    530 
    531 	DPRINTF(("remapping rom @ %lx for %lx\n",
    532 	    (long)(PCI_MAPREG_ROM_ADDR(address) + offs), (long)stiromsize));
    533 	bus_space_unmap(pa->pa_memt, romh, romsize);
    534 	rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address) + offs,
    535 	    stiromsize, 0, &spc->sc_romh);
    536 	if (rc != 0) {
    537 		aprint_error_dev(sc->sc_dev, "can't map STI ROM (%d)\n",
    538 		    rc);
    539 		goto fail2;
    540 	}
    541  	gftfb_disable_rom_internal(spc);
    542 	sc->sc_flags &= ~STI_ROM_ENABLED;
    543 
    544 	return 0;
    545 
    546 fail:
    547 	bus_space_unmap(pa->pa_memt, romh, romsize);
    548 fail2:
    549 	gftfb_disable_rom_internal(spc);
    550 
    551 	return rc;
    552 }
    553 
    554 /*
    555  * Decode a BAR register.
    556  */
    557 int
    558 gftfb_readbar(struct sti_softc *sc, struct pci_attach_args *pa, u_int region,
    559     int bar)
    560 {
    561 	bus_addr_t addr;
    562 	bus_size_t size;
    563 	uint32_t cf;
    564 	int rc;
    565 
    566 	if (bar == 0) {
    567 		sc->bases[region] = 0;
    568 		return (0);
    569 	}
    570 
    571 #ifdef DIAGNOSTIC
    572 	if (bar < PCI_MAPREG_START || bar > PCI_MAPREG_PPB_END) {
    573 		gftfb_disable_rom(sc);
    574 		printf("%s: unexpected bar %02x for region %d\n",
    575 		    device_xname(sc->sc_dev), bar, region);
    576 		gftfb_enable_rom(sc);
    577 	}
    578 #endif
    579 
    580 	cf = pci_conf_read(pa->pa_pc, pa->pa_tag, bar);
    581 
    582 	rc = pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, PCI_MAPREG_TYPE(cf),
    583 	    &addr, &size, NULL);
    584 
    585 	if (rc != 0) {
    586 		gftfb_disable_rom(sc);
    587 		aprint_error_dev(sc->sc_dev, "invalid bar %02x for region %d\n",
    588 		    bar, region);
    589 		gftfb_enable_rom(sc);
    590 		return (rc);
    591 	}
    592 
    593 	sc->bases[region] = addr;
    594 	return (0);
    595 }
    596 
    597 /*
    598  * Enable PCI ROM.
    599  */
    600 void
    601 gftfb_enable_rom_internal(struct gftfb_softc *spc)
    602 {
    603 	pcireg_t address;
    604 
    605 	KASSERT(spc != NULL);
    606 
    607 	address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
    608 	address |= PCI_MAPREG_ROM_ENABLE;
    609 	pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
    610 }
    611 
    612 void
    613 gftfb_enable_rom(struct sti_softc *sc)
    614 {
    615 	struct gftfb_softc *spc = device_private(sc->sc_dev);
    616 
    617 	if (!ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
    618 		gftfb_enable_rom_internal(spc);
    619 	}
    620 	SET(sc->sc_flags, STI_ROM_ENABLED);
    621 }
    622 
    623 /*
    624  * Disable PCI ROM.
    625  */
    626 void
    627 gftfb_disable_rom_internal(struct gftfb_softc *spc)
    628 {
    629 	pcireg_t address;
    630 
    631 	KASSERT(spc != NULL);
    632 
    633 	address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
    634 	address &= ~PCI_MAPREG_ROM_ENABLE;
    635 	pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
    636 }
    637 
    638 void
    639 gftfb_disable_rom(struct sti_softc *sc)
    640 {
    641 	struct gftfb_softc *spc = device_private(sc->sc_dev);
    642 
    643 	if (ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
    644 		gftfb_disable_rom_internal(spc);
    645 	}
    646 	CLR(sc->sc_flags, STI_ROM_ENABLED);
    647 }
    648 
    649 static inline void
    650 gftfb_wait(struct gftfb_softc *sc)
    651 {
    652 	struct sti_rom *rom = sc->sc_base.sc_rom;
    653 	bus_space_tag_t memt = rom->memt;
    654 	bus_space_handle_t memh = rom->regh[2];
    655 	uint8_t stat;
    656 
    657 	do {
    658 		stat = bus_space_read_1(memt, memh, NGLE_REG_15b0);
    659 		if (stat == 0)
    660 			stat = bus_space_read_1(memt, memh, NGLE_REG_15b0);
    661 	} while (stat != 0);
    662 }
    663 
    664 static inline void
    665 gftfb_setup_fb(struct gftfb_softc *sc)
    666 {
    667 	struct sti_rom *rom = sc->sc_base.sc_rom;
    668 	bus_space_tag_t memt = rom->memt;
    669 	bus_space_handle_t memh = rom->regh[2];
    670 
    671 	gftfb_wait(sc);
    672 	bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0x13601000);
    673 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x83000300);
    674 	gftfb_wait(sc);
    675 	bus_space_write_1(memt, memh, NGLE_REG_16b1, 1);
    676 	sc->sc_hwmode = HW_FB;
    677 }
    678 
    679 void
    680 gftfb_setup(struct gftfb_softc *sc)
    681 {
    682 	struct sti_rom *rom = sc->sc_base.sc_rom;
    683 	bus_space_tag_t memt = rom->memt;
    684 	bus_space_handle_t memh = rom->regh[2];
    685 	int i;
    686 
    687 	sc->sc_hwmode = HW_FB;
    688 	sc->sc_hot_x = 0;
    689 	sc->sc_hot_y = 0;
    690 	sc->sc_enabled = 0;
    691 	sc->sc_video_on = 1;
    692 
    693 	sc->sc_rect_colour = 0xf0000000;
    694 	sc->sc_rect_height = 0;
    695 
    696 	/* set Bt458 read mask register to all planes */
    697 	gftfb_wait(sc);
    698 	ngle_bt458_write(memt, memh, 0x08, 0x04);
    699 	ngle_bt458_write(memt, memh, 0x0a, 0xff);
    700 
    701 	gftfb_setup_fb(sc);
    702 
    703 	/* attr. planes */
    704 	gftfb_wait(sc);
    705 	bus_space_write_stream_4(memt, memh, NGLE_REG_11, 0x2ea0d000);
    706 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x23000302);
    707 	bus_space_write_stream_4(memt, memh, NGLE_REG_12, NGLE_ARTIST_CMAP0);
    708 	bus_space_write_stream_4(memt, memh, NGLE_REG_8, 0xffffffff);
    709 
    710 	gftfb_wait(sc);
    711 	bus_space_write_stream_4(memt, memh, NGLE_REG_6, 0x00000000);
    712 	bus_space_write_stream_4(memt, memh, NGLE_REG_9,
    713 	    (sc->sc_scr.scr_cfg.scr_width << 16) | sc->sc_scr.scr_cfg.scr_height);
    714 	/*
    715 	 * blit into offscreen memory to force flush previous - apparently
    716 	 * some chips have a bug this works around
    717 	 */
    718 	bus_space_write_stream_4(memt, memh, NGLE_REG_6, 0x05000000);
    719 	bus_space_write_stream_4(memt, memh, NGLE_REG_9, 0x00040001);
    720 
    721 	gftfb_wait(sc);
    722 	bus_space_write_stream_4(memt, memh, NGLE_REG_12, 0x00000000);
    723 
    724 	gftfb_setup_fb(sc);
    725 
    726 	/* make sure video output is enabled */
    727 	gftfb_wait(sc);
    728 	bus_space_write_stream_4(memt, memh, NGLE_REG_21,
    729 	    bus_space_read_stream_4(memt, memh, NGLE_REG_21) | 0x0a000000);
    730 	bus_space_write_stream_4(memt, memh, NGLE_REG_27,
    731 	    bus_space_read_stream_4(memt, memh, NGLE_REG_27) | 0x00800000);
    732 
    733 	/* initialize cursor sprite */
    734 	gftfb_wait(sc);
    735 
    736 	/* cursor mask */
    737 	gftfb_wait(sc);
    738 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x300);
    739 	bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
    740 	bus_space_write_stream_4(memt, memh, NGLE_REG_11, 0x28A07000);
    741 	bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
    742 	for (i = 0; i < 64; i++) {
    743 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0xffffffff);
    744 		bus_space_write_stream_4(memt, memh, NGLE_REG_5, 0xffffffff);
    745 	}
    746 
    747 	/* cursor image */
    748 	gftfb_wait(sc);
    749 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x300);
    750 	bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
    751 	bus_space_write_stream_4(memt, memh, NGLE_REG_11, 0x28A06000);
    752 	bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
    753 	for (i = 0; i < 64; i++) {
    754 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0xff00ff00);
    755 		bus_space_write_stream_4(memt, memh, NGLE_REG_5, 0xff00ff00);
    756 	}
    757 
    758 	/* colour map */
    759 	gftfb_wait(sc);
    760 	bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0xBBE0F000);
    761 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x03000300);
    762 	bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
    763 	gftfb_wait(sc);
    764 	bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
    765 	bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0);
    766 	bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0);
    767 	bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0x000000ff);	/* BG */
    768 	bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0x00ff0000);	/* FG */
    769 	gftfb_wait(sc);
    770 	bus_space_write_stream_4(memt, memh, NGLE_REG_2, 0);
    771 	bus_space_write_stream_4(memt, memh, NGLE_REG_26, 0x80008004);
    772 	gftfb_setup_fb(sc);
    773 
    774 	gftfb_move_cursor(sc, 100, 100);
    775 
    776 }
    777 
    778 static int
    779 gftfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    780 	struct lwp *l)
    781 {
    782 	struct vcons_data *vd = v;
    783 	struct gftfb_softc *sc = vd->cookie;
    784 	struct wsdisplay_fbinfo *wdf;
    785 	struct vcons_screen *ms = vd->active;
    786 
    787 	switch (cmd) {
    788 	case WSDISPLAYIO_GTYPE:
    789 		*(u_int *)data = WSDISPLAY_TYPE_STI;
    790 		return 0;
    791 
    792 	/* PCI config read/write passthrough. */
    793 	case PCI_IOC_CFGREAD:
    794 	case PCI_IOC_CFGWRITE:
    795 		return pci_devioctl(sc->sc_pc, sc->sc_tag,
    796 		    cmd, data, flag, l);
    797 
    798 	case WSDISPLAYIO_GET_BUSID:
    799 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    800 		    sc->sc_tag, data);
    801 
    802 	case WSDISPLAYIO_GINFO:
    803 		if (ms == NULL)
    804 			return ENODEV;
    805 		wdf = (void *)data;
    806 		wdf->height = ms->scr_ri.ri_height;
    807 		wdf->width = ms->scr_ri.ri_width;
    808 		wdf->depth = ms->scr_ri.ri_depth;
    809 		wdf->cmsize = 256;
    810 		return 0;
    811 
    812 	case WSDISPLAYIO_GETCMAP:
    813 		return gftfb_getcmap(sc,
    814 		    (struct wsdisplay_cmap *)data);
    815 
    816 	case WSDISPLAYIO_PUTCMAP:
    817 		return gftfb_putcmap(sc,
    818 		    (struct wsdisplay_cmap *)data);
    819 
    820 	case WSDISPLAYIO_LINEBYTES:
    821 		*(u_int *)data = 2048;
    822 		return 0;
    823 
    824 	case WSDISPLAYIO_SMODE: {
    825 		int new_mode = *(int*)data;
    826 		if (new_mode != sc->sc_mode) {
    827 			sc->sc_mode = new_mode;
    828 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    829 				gftfb_setup(sc);
    830 				gftfb_restore_palette(sc);
    831 				glyphcache_wipe(&sc->sc_gc);
    832 				gftfb_rectfill(sc, 0, 0, sc->sc_width,
    833 				    sc->sc_height, ms->scr_ri.ri_devcmap[
    834 				    (ms->scr_defattr >> 16) & 0xff]);
    835 				vcons_redraw_screen(ms);
    836 				gftfb_set_video(sc, 1);
    837 			}
    838 		}
    839 		}
    840 		return 0;
    841 
    842 	case WSDISPLAYIO_GET_FBINFO:
    843 		{
    844 			struct wsdisplayio_fbinfo *fbi = data;
    845 			int ret;
    846 
    847 			ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    848 			fbi->fbi_fbsize = sc->sc_scr.fbheight * 2048;
    849 			return ret;
    850 		}
    851 
    852 	case WSDISPLAYIO_GCURPOS:
    853 		{
    854 			struct wsdisplay_curpos *cp = (void *)data;
    855 
    856 			cp->x = sc->sc_cursor_x;
    857 			cp->y = sc->sc_cursor_y;
    858 		}
    859 		return 0;
    860 
    861 	case WSDISPLAYIO_SCURPOS:
    862 		{
    863 			struct wsdisplay_curpos *cp = (void *)data;
    864 
    865 			gftfb_move_cursor(sc, cp->x, cp->y);
    866 		}
    867 		return 0;
    868 
    869 	case WSDISPLAYIO_GCURMAX:
    870 		{
    871 			struct wsdisplay_curpos *cp = (void *)data;
    872 
    873 			cp->x = 64;
    874 			cp->y = 64;
    875 		}
    876 		return 0;
    877 
    878 	case WSDISPLAYIO_SCURSOR:
    879 		{
    880 			struct wsdisplay_cursor *cursor = (void *)data;
    881 
    882 			return gftfb_do_cursor(sc, cursor);
    883 		}
    884 
    885 	case WSDISPLAYIO_SVIDEO:
    886 		gftfb_set_video(sc, *(int *)data);
    887 		return 0;
    888 	case WSDISPLAYIO_GVIDEO:
    889 		return sc->sc_video_on ?
    890 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    891 	}
    892 	return EPASSTHROUGH;
    893 }
    894 
    895 static paddr_t
    896 gftfb_mmap(void *v, void *vs, off_t offset, int prot)
    897 {
    898 	struct vcons_data *vd = v;
    899 	struct gftfb_softc *sc = vd->cookie;
    900 	struct sti_rom *rom = sc->sc_base.sc_rom;
    901 	paddr_t pa;
    902 
    903 	if (offset < 0 || offset >= sc->sc_scr.fblen)
    904 		return -1;
    905 
    906 	if (sc->sc_mode != WSDISPLAYIO_MODE_DUMBFB)
    907 		return -1;
    908 
    909 	pa = bus_space_mmap(rom->memt, sc->sc_scr.fbaddr, offset, prot,
    910 	    BUS_SPACE_MAP_LINEAR);
    911 	return pa;
    912 }
    913 
    914 static void
    915 gftfb_init_screen(void *cookie, struct vcons_screen *scr,
    916     int existing, long *defattr)
    917 {
    918 	struct gftfb_softc *sc = cookie;
    919 	struct rasops_info *ri = &scr->scr_ri;
    920 
    921 	ri->ri_depth = 8;
    922 	ri->ri_width = sc->sc_width;
    923 	ri->ri_height = sc->sc_height;
    924 	ri->ri_stride = 2048;
    925 	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB |
    926 		     RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
    927 
    928 	ri->ri_bits = (void *)sc->sc_scr.fbaddr;
    929 	rasops_init(ri, 0, 0);
    930 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    931 		      WSSCREEN_RESIZE;
    932 	scr->scr_flags |= VCONS_LOADFONT;
    933 
    934 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    935 		    sc->sc_width / ri->ri_font->fontwidth);
    936 
    937 	ri->ri_hw = scr;
    938 	sc->sc_putchar = ri->ri_ops.putchar;
    939 	ri->ri_ops.copyrows = gftfb_copyrows;
    940 	ri->ri_ops.copycols = gftfb_copycols;
    941 	ri->ri_ops.eraserows = gftfb_eraserows;
    942 	ri->ri_ops.erasecols = gftfb_erasecols;
    943 	ri->ri_ops.cursor = gftfb_cursor;
    944 	ri->ri_ops.putchar = gftfb_putchar;
    945 }
    946 
    947 static int
    948 gftfb_putcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
    949 {
    950 	u_char *r, *g, *b;
    951 	u_int index = cm->index;
    952 	u_int count = cm->count;
    953 	int i, error;
    954 	u_char rbuf[256], gbuf[256], bbuf[256];
    955 
    956 	if (cm->index >= 256 || cm->count > 256 ||
    957 	    (cm->index + cm->count) > 256)
    958 		return EINVAL;
    959 	error = copyin(cm->red, &rbuf[index], count);
    960 	if (error)
    961 		return error;
    962 	error = copyin(cm->green, &gbuf[index], count);
    963 	if (error)
    964 		return error;
    965 	error = copyin(cm->blue, &bbuf[index], count);
    966 	if (error)
    967 		return error;
    968 
    969 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    970 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    971 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    972 
    973 	r = &sc->sc_cmap_red[index];
    974 	g = &sc->sc_cmap_green[index];
    975 	b = &sc->sc_cmap_blue[index];
    976 
    977 	for (i = 0; i < count; i++) {
    978 		gftfb_putpalreg(sc, index, *r, *g, *b);
    979 		index++;
    980 		r++, g++, b++;
    981 	}
    982 	return 0;
    983 }
    984 
    985 static int
    986 gftfb_getcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
    987 {
    988 	u_int index = cm->index;
    989 	u_int count = cm->count;
    990 	int error;
    991 
    992 	if (index >= 255 || count > 256 || index + count > 256)
    993 		return EINVAL;
    994 
    995 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    996 	if (error)
    997 		return error;
    998 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    999 	if (error)
   1000 		return error;
   1001 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
   1002 	if (error)
   1003 		return error;
   1004 
   1005 	return 0;
   1006 }
   1007 
   1008 static void
   1009 gftfb_restore_palette(struct gftfb_softc *sc)
   1010 {
   1011 	int i;
   1012 
   1013 	for (i = 0; i < 256; i++) {
   1014 		gftfb_putpalreg(sc, i, sc->sc_cmap_red[i],
   1015 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
   1016 	}
   1017 }
   1018 
   1019 static int
   1020 gftfb_putpalreg(struct gftfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
   1021     uint8_t b)
   1022 {
   1023 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1024 	bus_space_tag_t memt = rom->memt;
   1025 	bus_space_handle_t memh = rom->regh[2];
   1026 
   1027 	mutex_enter(&sc->sc_hwlock);
   1028 	gftfb_wait(sc);
   1029 	bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0xbbe0f000);
   1030 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x03000300);
   1031 	bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
   1032 
   1033 	gftfb_wait(sc);
   1034 	bus_space_write_stream_4(memt, memh, NGLE_REG_3,
   1035 	    0x400 | (idx << 2));
   1036 	bus_space_write_stream_4(memt, memh, NGLE_REG_4,
   1037 	    (r << 16) | (g << 8) | b);
   1038 
   1039 	bus_space_write_stream_4(memt, memh, NGLE_REG_2, 0x400);
   1040 	bus_space_write_stream_4(memt, memh, NGLE_REG_26, 0x80000100);
   1041 	gftfb_setup_fb(sc);
   1042 	mutex_exit(&sc->sc_hwlock);
   1043 	return 0;
   1044 }
   1045 
   1046 static inline void
   1047 gftfb_wait_fifo(struct gftfb_softc *sc, uint32_t slots)
   1048 {
   1049 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1050 	bus_space_tag_t memt = rom->memt;
   1051 	bus_space_handle_t memh = rom->regh[2];
   1052 	uint32_t reg;
   1053 
   1054 	do {
   1055 		reg = bus_space_read_stream_4(memt, memh, NGLE_REG_34);
   1056 	} while (reg < slots);
   1057 }
   1058 
   1059 static void
   1060 gftfb_real_rectfill(struct gftfb_softc *sc, int x, int y, int wi, int he,
   1061 		      uint32_t bg)
   1062 {
   1063 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1064 	bus_space_tag_t memt = rom->memt;
   1065 	bus_space_handle_t memh = rom->regh[2];
   1066 
   1067 	if (sc->sc_hwmode != HW_FILL) {
   1068 		gftfb_wait_fifo(sc, 4);
   1069 		/* transfer data */
   1070 		bus_space_write_stream_4(memt, memh, NGLE_REG_8, 0xffffffff);
   1071 		/* plane mask */
   1072 		bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xff);
   1073 		/* bitmap op */
   1074 		bus_space_write_stream_4(memt, memh, NGLE_REG_14,
   1075 		    IBOvals(RopSrc, 0, BitmapExtent08, 0, DataDynamic, MaskOtc, 0, 0));
   1076 		/* dst bitmap access */
   1077 		bus_space_write_stream_4(memt, memh, NGLE_REG_11,
   1078 		    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0I, 0));
   1079 		sc->sc_hwmode = HW_FILL;
   1080 	}
   1081 	gftfb_wait_fifo(sc, 3);
   1082 	bus_space_write_stream_4(memt, memh, NGLE_REG_35, bg);
   1083 	/* dst XY */
   1084 	bus_space_write_stream_4(memt, memh, NGLE_REG_6, (x << 16) | y);
   1085 	/* len XY start */
   1086 	bus_space_write_stream_4(memt, memh, NGLE_REG_9, (wi << 16) | he);
   1087 
   1088 }
   1089 
   1090 static void
   1091 gftfb_rectfill(struct gftfb_softc *sc, int x, int y, int wi, int he,
   1092 		      uint32_t bg)
   1093 {
   1094 	/*
   1095 	 * For some reason my 4MB VisEG always draws rectangles at least 32
   1096 	 * pixels wide - no idea why, the bitblt command doesn't have this
   1097 	 * problem.
   1098 	 * So, as a workaround, we draw a 50xFontHeight rectangle to the right
   1099 	 * of the visible fb, keep track of the colour so we don't need to
   1100 	 * redraw every time, and bitblt the portion we need
   1101 	 */
   1102 	if (wi < 50) {
   1103 		if ((bg != sc->sc_rect_colour) ||
   1104 		    (he > sc->sc_rect_height)) {
   1105 			gftfb_real_rectfill(sc, sc->sc_width + 10, 0, 50,
   1106 			    he, bg);
   1107 			sc->sc_rect_colour = bg;
   1108 			sc->sc_rect_height = he;
   1109 		}
   1110 		gftfb_bitblt(sc, sc->sc_width + 10, 0, x, y, wi, he, RopSrc);
   1111 	} else
   1112 		gftfb_real_rectfill(sc, x, y, wi, he, bg);
   1113 }
   1114 
   1115 static void
   1116 gftfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
   1117 			    int he, int rop)
   1118 {
   1119 	struct gftfb_softc *sc = cookie;
   1120 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1121 	bus_space_tag_t memt = rom->memt;
   1122 	bus_space_handle_t memh = rom->regh[2];
   1123 
   1124 	if (sc->sc_hwmode != HW_BLIT) {
   1125 		gftfb_wait(sc);
   1126 		bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0x13a01000);
   1127 		sc->sc_hwmode = HW_BLIT;
   1128 	}
   1129 	gftfb_wait_fifo(sc, 5);
   1130 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, ((rop << 8) & 0xf00) | 0x23000000);
   1131 	bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xff);
   1132 	bus_space_write_stream_4(memt, memh, NGLE_REG_24, (xs << 16) | ys);
   1133 	bus_space_write_stream_4(memt, memh, NGLE_REG_7, (wi << 16) | he);
   1134 	bus_space_write_stream_4(memt, memh, NGLE_REG_25, (xd << 16) | yd);
   1135 }
   1136 
   1137 static void
   1138 gftfb_nuke_cursor(struct rasops_info *ri)
   1139 {
   1140 	struct vcons_screen *scr = ri->ri_hw;
   1141 	struct gftfb_softc *sc = scr->scr_cookie;
   1142 	int wi, he, x, y;
   1143 
   1144 	if (ri->ri_flg & RI_CURSOR) {
   1145 		wi = ri->ri_font->fontwidth;
   1146 		he = ri->ri_font->fontheight;
   1147 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1148 		y = ri->ri_crow * he + ri->ri_yorigin;
   1149 		gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1150 		ri->ri_flg &= ~RI_CURSOR;
   1151 	}
   1152 }
   1153 
   1154 static void
   1155 gftfb_cursor(void *cookie, int on, int row, int col)
   1156 {
   1157 	struct rasops_info *ri = cookie;
   1158 	struct vcons_screen *scr = ri->ri_hw;
   1159 	struct gftfb_softc *sc = scr->scr_cookie;
   1160 	int x, y, wi, he;
   1161 
   1162 	wi = ri->ri_font->fontwidth;
   1163 	he = ri->ri_font->fontheight;
   1164 
   1165 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1166 		if (on) {
   1167 			if (ri->ri_flg & RI_CURSOR) {
   1168 				gftfb_nuke_cursor(ri);
   1169 			}
   1170 			x = col * wi + ri->ri_xorigin;
   1171 			y = row * he + ri->ri_yorigin;
   1172 			gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1173 			ri->ri_flg |= RI_CURSOR;
   1174 		}
   1175 		ri->ri_crow = row;
   1176 		ri->ri_ccol = col;
   1177 	} else
   1178 	{
   1179 		ri->ri_crow = row;
   1180 		ri->ri_ccol = col;
   1181 		ri->ri_flg &= ~RI_CURSOR;
   1182 	}
   1183 
   1184 }
   1185 
   1186 static void
   1187 gftfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1188 {
   1189 	struct rasops_info *ri = cookie;
   1190 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1191 	struct vcons_screen *scr = ri->ri_hw;
   1192 	struct gftfb_softc *sc = scr->scr_cookie;
   1193 	int x, y, wi, he, rv = GC_NOPE;
   1194 	uint32_t bg;
   1195 
   1196 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1197 		return;
   1198 
   1199 	if (!CHAR_IN_FONT(c, font))
   1200 		return;
   1201 
   1202 	if (row == ri->ri_crow && col == ri->ri_ccol) {
   1203 		ri->ri_flg &= ~RI_CURSOR;
   1204 	}
   1205 
   1206 	wi = font->fontwidth;
   1207 	he = font->fontheight;
   1208 
   1209 	x = ri->ri_xorigin + col * wi;
   1210 	y = ri->ri_yorigin + row * he;
   1211 
   1212 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1213 
   1214 	if (c == 0x20) {
   1215 		gftfb_rectfill(sc, x, y, wi, he, bg);
   1216 		return;
   1217 	}
   1218 
   1219 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1220 	if (rv == GC_OK)
   1221 		return;
   1222 
   1223 	if (sc->sc_hwmode != HW_FB) gftfb_setup_fb(sc);
   1224 	sc->sc_putchar(cookie, row, col, c, attr);
   1225 
   1226 	if (rv == GC_ADD)
   1227 		glyphcache_add(&sc->sc_gc, c, x, y);
   1228 }
   1229 
   1230 static void
   1231 gftfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1232 {
   1233 	struct rasops_info *ri = cookie;
   1234 	struct vcons_screen *scr = ri->ri_hw;
   1235 	struct gftfb_softc *sc = scr->scr_cookie;
   1236 	int32_t xs, xd, y, width, height;
   1237 
   1238 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1239 		if (ri->ri_crow == row &&
   1240 		   (ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols)) &&
   1241 		   (ri->ri_flg & RI_CURSOR)) {
   1242 			gftfb_nuke_cursor(ri);
   1243 		}
   1244 
   1245 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1246 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1247 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1248 		width = ri->ri_font->fontwidth * ncols;
   1249 		height = ri->ri_font->fontheight;
   1250 		gftfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
   1251 		if (ri->ri_crow == row &&
   1252 		   (ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols)))
   1253 			ri->ri_flg &= ~RI_CURSOR;
   1254 	}
   1255 }
   1256 
   1257 static void
   1258 gftfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1259 {
   1260 	struct rasops_info *ri = cookie;
   1261 	struct vcons_screen *scr = ri->ri_hw;
   1262 	struct gftfb_softc *sc = scr->scr_cookie;
   1263 	int32_t x, y, width, height, fg, bg, ul;
   1264 
   1265 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1266 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1267 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1268 		width = ri->ri_font->fontwidth * ncols;
   1269 		height = ri->ri_font->fontheight;
   1270 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1271 
   1272 		gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1273 		if (ri->ri_crow == row &&
   1274 		   (ri->ri_ccol >= startcol && ri->ri_ccol < (startcol + ncols)))
   1275 			ri->ri_flg &= ~RI_CURSOR;
   1276 	}
   1277 }
   1278 
   1279 static void
   1280 gftfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1281 {
   1282 	struct rasops_info *ri = cookie;
   1283 	struct vcons_screen *scr = ri->ri_hw;
   1284 	struct gftfb_softc *sc = scr->scr_cookie;
   1285 	int32_t x, ys, yd, width, height;
   1286 
   1287 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1288 		if ((ri->ri_crow >= srcrow && ri->ri_crow < (srcrow + nrows)) &&
   1289 		   (ri->ri_flg & RI_CURSOR)) {
   1290 			gftfb_nuke_cursor(ri);
   1291 		}
   1292 		x = ri->ri_xorigin;
   1293 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1294 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1295 		width = ri->ri_emuwidth;
   1296 		height = ri->ri_font->fontheight * nrows;
   1297 		gftfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
   1298 		if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
   1299 			ri->ri_flg &= ~RI_CURSOR;
   1300 	}
   1301 }
   1302 
   1303 static void
   1304 gftfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1305 {
   1306 	struct rasops_info *ri = cookie;
   1307 	struct vcons_screen *scr = ri->ri_hw;
   1308 	struct gftfb_softc *sc = scr->scr_cookie;
   1309 	int32_t x, y, width, height, fg, bg, ul;
   1310 
   1311 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1312 		x = ri->ri_xorigin;
   1313 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1314 		width = ri->ri_emuwidth;
   1315 		height = ri->ri_font->fontheight * nrows;
   1316 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1317 
   1318 		gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1319 
   1320 		if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
   1321 			ri->ri_flg &= ~RI_CURSOR;
   1322 	}
   1323 }
   1324 
   1325 /*
   1326  * cursor sprite handling
   1327  * like most hw info, xf86 3.3 -> nglehdw.h was used as documentation
   1328  * problem is, the PCI EG doesn't quite behave like an S9000_ID_ARTIST
   1329  * the cursor position register bahaves like the one on HCRX while using
   1330  * the same address as Artist, incuding the enable bit and weird handling
   1331  * of negative coordinates. The rest of it, colour map, sprite image etc.,
   1332  * behave like Artist.
   1333  */
   1334 
   1335 static void
   1336 gftfb_move_cursor(struct gftfb_softc *sc, int x, int y)
   1337 {
   1338 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1339 	bus_space_tag_t memt = rom->memt;
   1340 	bus_space_handle_t memh = rom->regh[2];
   1341 	uint32_t pos;
   1342 
   1343 	sc->sc_cursor_x = x;
   1344 	x -= sc->sc_hot_x;
   1345 	sc->sc_cursor_y = y;
   1346 	y -= sc->sc_hot_y;
   1347 
   1348 	if (x < 0) x = 0x1000 - x;
   1349 	if (y < 0) y = 0x1000 - y;
   1350 	pos = (x << 16) | y;
   1351 	if (sc->sc_enabled) pos |= 0x80000000;
   1352 	gftfb_wait(sc);
   1353 	bus_space_write_stream_4(memt, memh, NGLE_REG_17, pos);
   1354 	bus_space_write_stream_4(memt, memh, NGLE_REG_18, 0x80);
   1355 }
   1356 
   1357 static int
   1358 gftfb_do_cursor(struct gftfb_softc *sc, struct wsdisplay_cursor *cur)
   1359 {
   1360 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1361 	bus_space_tag_t memt = rom->memt;
   1362 	bus_space_handle_t memh = rom->regh[2];
   1363 
   1364 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1365 
   1366 		sc->sc_enabled = cur->enable;
   1367 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1368 	}
   1369 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1370 
   1371 		sc->sc_hot_x = cur->hot.x;
   1372 		sc->sc_hot_y = cur->hot.y;
   1373 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1374 	}
   1375 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1376 
   1377 		gftfb_move_cursor(sc, cur->pos.x, cur->pos.y);
   1378 	}
   1379 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1380 		uint32_t rgb;
   1381 		uint8_t r[2], g[2], b[2];
   1382 
   1383 		copyin(cur->cmap.blue, b, 2);
   1384 		copyin(cur->cmap.green, g, 2);
   1385 		copyin(cur->cmap.red, r, 2);
   1386 		mutex_enter(&sc->sc_hwlock);
   1387 		gftfb_wait(sc);
   1388 		bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0xBBE0F000);
   1389 		bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x03000300);
   1390 		bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
   1391 		gftfb_wait(sc);
   1392 		bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
   1393 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0);
   1394 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0);
   1395 		rgb = (r[0] << 16) | (g[0] << 8) | b[0];
   1396 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, rgb);	/* BG */
   1397 		rgb = (r[1] << 16) | (g[1] << 8) | b[1];
   1398 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, rgb);	/* FG */
   1399 		bus_space_write_stream_4(memt, memh, NGLE_REG_2, 0);
   1400 		bus_space_write_stream_4(memt, memh, NGLE_REG_26, 0x80008004);
   1401 		gftfb_setup_fb(sc);
   1402 		mutex_exit(&sc->sc_hwlock);
   1403 
   1404 	}
   1405 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1406 		uint32_t buffer[128], latch, tmp;
   1407 		int i;
   1408 
   1409 		copyin(cur->mask, buffer, 512);
   1410 		gftfb_wait(sc);
   1411 		bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x300);
   1412 		bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
   1413 		bus_space_write_stream_4(memt, memh, NGLE_REG_11, 0x28A07000);
   1414 		bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
   1415 		for (i = 0; i < 128; i += 2) {
   1416 			latch = 0;
   1417 			tmp = buffer[i] & 0x80808080;
   1418 			latch |= tmp >> 7;
   1419 			tmp = buffer[i] & 0x40404040;
   1420 			latch |= tmp >> 5;
   1421 			tmp = buffer[i] & 0x20202020;
   1422 			latch |= tmp >> 3;
   1423 			tmp = buffer[i] & 0x10101010;
   1424 			latch |= tmp >> 1;
   1425 			tmp = buffer[i] & 0x08080808;
   1426 			latch |= tmp << 1;
   1427 			tmp = buffer[i] & 0x04040404;
   1428 			latch |= tmp << 3;
   1429 			tmp = buffer[i] & 0x02020202;
   1430 			latch |= tmp << 5;
   1431 			tmp = buffer[i] & 0x01010101;
   1432 			latch |= tmp << 7;
   1433 			bus_space_write_stream_4(memt, memh, NGLE_REG_4, latch);
   1434 			latch = 0;
   1435 			tmp = buffer[i + 1] & 0x80808080;
   1436 			latch |= tmp >> 7;
   1437 			tmp = buffer[i + 1] & 0x40404040;
   1438 			latch |= tmp >> 5;
   1439 			tmp = buffer[i + 1] & 0x20202020;
   1440 			latch |= tmp >> 3;
   1441 			tmp = buffer[i + 1] & 0x10101010;
   1442 			latch |= tmp >> 1;
   1443 			tmp = buffer[i + 1] & 0x08080808;
   1444 			latch |= tmp << 1;
   1445 			tmp = buffer[i + 1] & 0x04040404;
   1446 			latch |= tmp << 3;
   1447 			tmp = buffer[i + 1] & 0x02020202;
   1448 			latch |= tmp << 5;
   1449 			tmp = buffer[i + 1] & 0x01010101;
   1450 			latch |= tmp << 7;
   1451 			bus_space_write_stream_4(memt, memh, NGLE_REG_5, latch);
   1452 		}
   1453 
   1454 		copyin(cur->image, buffer, 512);
   1455 		gftfb_wait(sc);
   1456 		bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x300);
   1457 		bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
   1458 		bus_space_write_stream_4(memt, memh, NGLE_REG_11, 0x28A06000);
   1459 		bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
   1460 		for (i = 0; i < 128; i += 2) {
   1461 			latch = 0;
   1462 			tmp = buffer[i] & 0x80808080;
   1463 			latch |= tmp >> 7;
   1464 			tmp = buffer[i] & 0x40404040;
   1465 			latch |= tmp >> 5;
   1466 			tmp = buffer[i] & 0x20202020;
   1467 			latch |= tmp >> 3;
   1468 			tmp = buffer[i] & 0x10101010;
   1469 			latch |= tmp >> 1;
   1470 			tmp = buffer[i] & 0x08080808;
   1471 			latch |= tmp << 1;
   1472 			tmp = buffer[i] & 0x04040404;
   1473 			latch |= tmp << 3;
   1474 			tmp = buffer[i] & 0x02020202;
   1475 			latch |= tmp << 5;
   1476 			tmp = buffer[i] & 0x01010101;
   1477 			latch |= tmp << 7;
   1478 			bus_space_write_stream_4(memt, memh, NGLE_REG_4, latch);
   1479 			latch = 0;
   1480 			tmp = buffer[i + 1] & 0x80808080;
   1481 			latch |= tmp >> 7;
   1482 			tmp = buffer[i + 1] & 0x40404040;
   1483 			latch |= tmp >> 5;
   1484 			tmp = buffer[i + 1] & 0x20202020;
   1485 			latch |= tmp >> 3;
   1486 			tmp = buffer[i + 1] & 0x10101010;
   1487 			latch |= tmp >> 1;
   1488 			tmp = buffer[i + 1] & 0x08080808;
   1489 			latch |= tmp << 1;
   1490 			tmp = buffer[i + 1] & 0x04040404;
   1491 			latch |= tmp << 3;
   1492 			tmp = buffer[i + 1] & 0x02020202;
   1493 			latch |= tmp << 5;
   1494 			tmp = buffer[i + 1] & 0x01010101;
   1495 			latch |= tmp << 7;
   1496 			bus_space_write_stream_4(memt, memh, NGLE_REG_5, latch);
   1497 		}
   1498 		gftfb_setup_fb(sc);
   1499 	}
   1500 
   1501 	return 0;
   1502 }
   1503 
   1504 static void
   1505 gftfb_set_video(struct gftfb_softc *sc, int on)
   1506 {
   1507 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1508 	bus_space_tag_t memt = rom->memt;
   1509 	bus_space_handle_t memh = rom->regh[2];
   1510 
   1511 	if (sc->sc_video_on == on)
   1512 		return;
   1513 
   1514 	sc->sc_video_on = on;
   1515 
   1516 	gftfb_wait(sc);
   1517 	if (on) {
   1518 		bus_space_write_stream_4(memt, memh, NGLE_REG_21,
   1519 		    bus_space_read_stream_4(memt, memh, NGLE_REG_21) | 0x0a000000);
   1520 		bus_space_write_stream_4(memt, memh, NGLE_REG_27,
   1521 		    bus_space_read_stream_4(memt, memh, NGLE_REG_27) | 0x00800000);
   1522 	} else {
   1523 		bus_space_write_stream_4(memt, memh, NGLE_REG_21,
   1524 		    bus_space_read_stream_4(memt, memh, NGLE_REG_21) &  ~0x0a000000);
   1525 		bus_space_write_stream_4(memt, memh, NGLE_REG_27,
   1526 		    bus_space_read_stream_4(memt, memh, NGLE_REG_27) & ~0x00800000);
   1527 	}
   1528 }
   1529