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