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