Home | History | Annotate | Line # | Download | only in dev
gftfb.c revision 1.10
      1 /*	$NetBSD: gftfb.c,v 1.10 2024/03/27 06:52:03 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_STI;
    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 			int ret;
    840 
    841 			ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    842 			fbi->fbi_height = sc->sc_scr.fbheight;
    843 			fbi->fbi_fbsize = sc->sc_scr.fbheight * 2048;
    844 			return ret;
    845 		}
    846 
    847 	case WSDISPLAYIO_GCURPOS:
    848 		{
    849 			struct wsdisplay_curpos *cp = (void *)data;
    850 
    851 			cp->x = sc->sc_cursor_x;
    852 			cp->y = sc->sc_cursor_y;
    853 		}
    854 		return 0;
    855 
    856 	case WSDISPLAYIO_SCURPOS:
    857 		{
    858 			struct wsdisplay_curpos *cp = (void *)data;
    859 
    860 			gftfb_move_cursor(sc, cp->x, cp->y);
    861 		}
    862 		return 0;
    863 
    864 	case WSDISPLAYIO_GCURMAX:
    865 		{
    866 			struct wsdisplay_curpos *cp = (void *)data;
    867 
    868 			cp->x = 64;
    869 			cp->y = 64;
    870 		}
    871 		return 0;
    872 
    873 	case WSDISPLAYIO_SCURSOR:
    874 		{
    875 			struct wsdisplay_cursor *cursor = (void *)data;
    876 
    877 			return gftfb_do_cursor(sc, cursor);
    878 		}
    879 
    880 	case WSDISPLAYIO_SVIDEO:
    881 		gftfb_set_video(sc, *(int *)data);
    882 		return 0;
    883 	case WSDISPLAYIO_GVIDEO:
    884 		return sc->sc_video_on ?
    885 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    886 	}
    887 	return EPASSTHROUGH;
    888 }
    889 
    890 static paddr_t
    891 gftfb_mmap(void *v, void *vs, off_t offset, int prot)
    892 {
    893 	struct vcons_data *vd = v;
    894 	struct gftfb_softc *sc = vd->cookie;
    895 	struct sti_rom *rom = sc->sc_base.sc_rom;
    896 	paddr_t pa;
    897 
    898 	if (offset < 0 || offset >= sc->sc_scr.fblen)
    899 		return -1;
    900 
    901 	if (sc->sc_mode != WSDISPLAYIO_MODE_DUMBFB)
    902 		return -1;
    903 
    904 	pa = bus_space_mmap(rom->memt, sc->sc_scr.fbaddr, offset, prot,
    905 	    BUS_SPACE_MAP_LINEAR);
    906 	return pa;
    907 }
    908 
    909 static void
    910 gftfb_init_screen(void *cookie, struct vcons_screen *scr,
    911     int existing, long *defattr)
    912 {
    913 	struct gftfb_softc *sc = cookie;
    914 	struct rasops_info *ri = &scr->scr_ri;
    915 
    916 	ri->ri_depth = 8;
    917 	ri->ri_width = sc->sc_width;
    918 	ri->ri_height = sc->sc_height;
    919 	ri->ri_stride = 2048;
    920 	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB |
    921 		     RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
    922 
    923 	ri->ri_bits = (void *)sc->sc_scr.fbaddr;
    924 	rasops_init(ri, 0, 0);
    925 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    926 		      WSSCREEN_RESIZE;
    927 	scr->scr_flags |= VCONS_LOADFONT;
    928 
    929 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    930 		    sc->sc_width / ri->ri_font->fontwidth);
    931 
    932 	ri->ri_hw = scr;
    933 	sc->sc_putchar = ri->ri_ops.putchar;
    934 	ri->ri_ops.copyrows = gftfb_copyrows;
    935 	ri->ri_ops.copycols = gftfb_copycols;
    936 	ri->ri_ops.eraserows = gftfb_eraserows;
    937 	ri->ri_ops.erasecols = gftfb_erasecols;
    938 	ri->ri_ops.cursor = gftfb_cursor;
    939 	ri->ri_ops.putchar = gftfb_putchar;
    940 }
    941 
    942 static int
    943 gftfb_putcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
    944 {
    945 	u_char *r, *g, *b;
    946 	u_int index = cm->index;
    947 	u_int count = cm->count;
    948 	int i, error;
    949 	u_char rbuf[256], gbuf[256], bbuf[256];
    950 
    951 	if (cm->index >= 256 || cm->count > 256 ||
    952 	    (cm->index + cm->count) > 256)
    953 		return EINVAL;
    954 	error = copyin(cm->red, &rbuf[index], count);
    955 	if (error)
    956 		return error;
    957 	error = copyin(cm->green, &gbuf[index], count);
    958 	if (error)
    959 		return error;
    960 	error = copyin(cm->blue, &bbuf[index], count);
    961 	if (error)
    962 		return error;
    963 
    964 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    965 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    966 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    967 
    968 	r = &sc->sc_cmap_red[index];
    969 	g = &sc->sc_cmap_green[index];
    970 	b = &sc->sc_cmap_blue[index];
    971 
    972 	for (i = 0; i < count; i++) {
    973 		gftfb_putpalreg(sc, index, *r, *g, *b);
    974 		index++;
    975 		r++, g++, b++;
    976 	}
    977 	return 0;
    978 }
    979 
    980 static int
    981 gftfb_getcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
    982 {
    983 	u_int index = cm->index;
    984 	u_int count = cm->count;
    985 	int error;
    986 
    987 	if (index >= 255 || count > 256 || index + count > 256)
    988 		return EINVAL;
    989 
    990 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    991 	if (error)
    992 		return error;
    993 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    994 	if (error)
    995 		return error;
    996 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    997 	if (error)
    998 		return error;
    999 
   1000 	return 0;
   1001 }
   1002 
   1003 static void
   1004 gftfb_restore_palette(struct gftfb_softc *sc)
   1005 {
   1006 	int i;
   1007 
   1008 	for (i = 0; i < 256; i++) {
   1009 		gftfb_putpalreg(sc, i, sc->sc_cmap_red[i],
   1010 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
   1011 	}
   1012 }
   1013 
   1014 static int
   1015 gftfb_putpalreg(struct gftfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
   1016     uint8_t b)
   1017 {
   1018 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1019 	bus_space_tag_t memt = rom->memt;
   1020 	bus_space_handle_t memh = rom->regh[2];
   1021 
   1022 	mutex_enter(&sc->sc_hwlock);
   1023 	gftfb_wait(sc);
   1024 	bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0xbbe0f000);
   1025 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x03000300);
   1026 	bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
   1027 
   1028 	gftfb_wait(sc);
   1029 	bus_space_write_stream_4(memt, memh, NGLE_REG_3,
   1030 	    0x400 | (idx << 2));
   1031 	bus_space_write_stream_4(memt, memh, NGLE_REG_4,
   1032 	    (r << 16) | (g << 8) | b);
   1033 
   1034 	bus_space_write_stream_4(memt, memh, NGLE_REG_2, 0x400);
   1035 	bus_space_write_stream_4(memt, memh, NGLE_REG_26, 0x80000100);
   1036 	gftfb_setup_fb(sc);
   1037 	mutex_exit(&sc->sc_hwlock);
   1038 	return 0;
   1039 }
   1040 
   1041 static inline void
   1042 gftfb_wait_fifo(struct gftfb_softc *sc, uint32_t slots)
   1043 {
   1044 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1045 	bus_space_tag_t memt = rom->memt;
   1046 	bus_space_handle_t memh = rom->regh[2];
   1047 	uint32_t reg;
   1048 
   1049 	do {
   1050 		reg = bus_space_read_stream_4(memt, memh, NGLE_REG_34);
   1051 	} while (reg < slots);
   1052 }
   1053 
   1054 static void
   1055 gftfb_rectfill(struct gftfb_softc *sc, int x, int y, int wi, int he,
   1056 		      uint32_t bg)
   1057 {
   1058 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1059 	bus_space_tag_t memt = rom->memt;
   1060 	bus_space_handle_t memh = rom->regh[2];
   1061 
   1062 	if (sc->sc_hwmode != HW_FILL) {
   1063 		gftfb_wait_fifo(sc, 4);
   1064 		/* transfer data */
   1065 		bus_space_write_stream_4(memt, memh, NGLE_REG_8, 0xffffffff);
   1066 		/* plane mask */
   1067 		bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xff);
   1068 		/* bitmap op */
   1069 		bus_space_write_stream_4(memt, memh, NGLE_REG_14,
   1070 		    IBOvals(RopSrc, 0, BitmapExtent08, 0, DataDynamic, MaskOtc, 0, 0));
   1071 		/* dst bitmap access */
   1072 		bus_space_write_stream_4(memt, memh, NGLE_REG_11,
   1073 		    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0I, 0));
   1074 		sc->sc_hwmode = HW_FILL;
   1075 	}
   1076 	gftfb_wait_fifo(sc, 3);
   1077 	bus_space_write_stream_4(memt, memh, NGLE_REG_35, bg);
   1078 	/* dst XY */
   1079 	bus_space_write_stream_4(memt, memh, NGLE_REG_6, (x << 16) | y);
   1080 	/* len XY start */
   1081 	bus_space_write_stream_4(memt, memh, NGLE_REG_9, (wi << 16) | he);
   1082 
   1083 }
   1084 
   1085 
   1086 static void
   1087 gftfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
   1088 			    int he, int rop)
   1089 {
   1090 	struct gftfb_softc *sc = cookie;
   1091 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1092 	bus_space_tag_t memt = rom->memt;
   1093 	bus_space_handle_t memh = rom->regh[2];
   1094 
   1095 	if (sc->sc_hwmode != HW_BLIT) {
   1096 		gftfb_wait(sc);
   1097 		bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0x13a01000);
   1098 		sc->sc_hwmode = HW_BLIT;
   1099 	}
   1100 	gftfb_wait_fifo(sc, 5);
   1101 	bus_space_write_stream_4(memt, memh, NGLE_REG_14, ((rop << 8) & 0xf00) | 0x23000000);
   1102 	bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xff);
   1103 	bus_space_write_stream_4(memt, memh, NGLE_REG_24, (xs << 16) | ys);
   1104 	bus_space_write_stream_4(memt, memh, NGLE_REG_7, (wi << 16) | he);
   1105 	bus_space_write_stream_4(memt, memh, NGLE_REG_25, (xd << 16) | yd);
   1106 }
   1107 
   1108 static void
   1109 gftfb_nuke_cursor(struct rasops_info *ri)
   1110 {
   1111 	struct vcons_screen *scr = ri->ri_hw;
   1112 	struct gftfb_softc *sc = scr->scr_cookie;
   1113 	int wi, he, x, y;
   1114 
   1115 	if (ri->ri_flg & RI_CURSOR) {
   1116 		wi = ri->ri_font->fontwidth;
   1117 		he = ri->ri_font->fontheight;
   1118 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1119 		y = ri->ri_crow * he + ri->ri_yorigin;
   1120 		gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1121 		ri->ri_flg &= ~RI_CURSOR;
   1122 	}
   1123 }
   1124 
   1125 static void
   1126 gftfb_cursor(void *cookie, int on, int row, int col)
   1127 {
   1128 	struct rasops_info *ri = cookie;
   1129 	struct vcons_screen *scr = ri->ri_hw;
   1130 	struct gftfb_softc *sc = scr->scr_cookie;
   1131 	int x, y, wi, he;
   1132 
   1133 	wi = ri->ri_font->fontwidth;
   1134 	he = ri->ri_font->fontheight;
   1135 
   1136 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1137 		if (on) {
   1138 			if (ri->ri_flg & RI_CURSOR) {
   1139 				gftfb_nuke_cursor(ri);
   1140 			}
   1141 			x = col * wi + ri->ri_xorigin;
   1142 			y = row * he + ri->ri_yorigin;
   1143 			gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1144 			ri->ri_flg |= RI_CURSOR;
   1145 		}
   1146 		ri->ri_crow = row;
   1147 		ri->ri_ccol = col;
   1148 	} else
   1149 	{
   1150 		ri->ri_crow = row;
   1151 		ri->ri_ccol = col;
   1152 		ri->ri_flg &= ~RI_CURSOR;
   1153 	}
   1154 
   1155 }
   1156 
   1157 static void
   1158 gftfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1159 {
   1160 	struct rasops_info *ri = cookie;
   1161 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1162 	struct vcons_screen *scr = ri->ri_hw;
   1163 	struct gftfb_softc *sc = scr->scr_cookie;
   1164 	int x, y, wi, he, rv = GC_NOPE;
   1165 #if 0
   1166 	uint32_t bg;
   1167 #endif
   1168 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1169 		return;
   1170 
   1171 	if (!CHAR_IN_FONT(c, font))
   1172 		return;
   1173 
   1174 	if (row == ri->ri_crow && col == ri->ri_ccol) {
   1175 		ri->ri_flg &= ~RI_CURSOR;
   1176 	}
   1177 
   1178 	wi = font->fontwidth;
   1179 	he = font->fontheight;
   1180 
   1181 	x = ri->ri_xorigin + col * wi;
   1182 	y = ri->ri_yorigin + row * he;
   1183 #if 0
   1184 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1185 
   1186 	/* XXX
   1187 	 * rectfill currently draws rectangles less than 32 pixels wide as
   1188 	 * 32 pixels wide, no idea why. So until I figure that one out we
   1189 	 * draw blanks by software
   1190 	 * bitblt doesn't seem to have this problem
   1191 	 */
   1192 	if (c == 0x20) {
   1193 		gftfb_rectfill(sc, x, y, wi, he, bg);
   1194 		return;
   1195 	}
   1196 #endif
   1197 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1198 	if (rv == GC_OK)
   1199 		return;
   1200 
   1201 	if (sc->sc_hwmode != HW_FB) gftfb_setup_fb(sc);
   1202 	sc->sc_putchar(cookie, row, col, c, attr);
   1203 
   1204 	if (rv == GC_ADD)
   1205 		glyphcache_add(&sc->sc_gc, c, x, y);
   1206 }
   1207 
   1208 static void
   1209 gftfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1210 {
   1211 	struct rasops_info *ri = cookie;
   1212 	struct vcons_screen *scr = ri->ri_hw;
   1213 	struct gftfb_softc *sc = scr->scr_cookie;
   1214 	int32_t xs, xd, y, width, height;
   1215 
   1216 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1217 		if (ri->ri_crow == row &&
   1218 		   (ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols)) &&
   1219 		   (ri->ri_flg & RI_CURSOR)) {
   1220 			gftfb_nuke_cursor(ri);
   1221 		}
   1222 
   1223 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1224 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1225 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1226 		width = ri->ri_font->fontwidth * ncols;
   1227 		height = ri->ri_font->fontheight;
   1228 		gftfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
   1229 		if (ri->ri_crow == row &&
   1230 		   (ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols)))
   1231 			ri->ri_flg &= ~RI_CURSOR;
   1232 	}
   1233 }
   1234 
   1235 static void
   1236 gftfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1237 {
   1238 	struct rasops_info *ri = cookie;
   1239 	struct vcons_screen *scr = ri->ri_hw;
   1240 	struct gftfb_softc *sc = scr->scr_cookie;
   1241 	int32_t x, y, width, height, fg, bg, ul;
   1242 
   1243 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1244 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1245 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1246 		width = ri->ri_font->fontwidth * ncols;
   1247 		height = ri->ri_font->fontheight;
   1248 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1249 
   1250 		gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1251 		if (ri->ri_crow == row &&
   1252 		   (ri->ri_ccol >= startcol && ri->ri_ccol < (startcol + ncols)))
   1253 			ri->ri_flg &= ~RI_CURSOR;
   1254 	}
   1255 }
   1256 
   1257 static void
   1258 gftfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1259 {
   1260 	struct rasops_info *ri = cookie;
   1261 	struct vcons_screen *scr = ri->ri_hw;
   1262 	struct gftfb_softc *sc = scr->scr_cookie;
   1263 	int32_t x, ys, yd, width, height;
   1264 
   1265 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1266 		if ((ri->ri_crow >= srcrow && ri->ri_crow < (srcrow + nrows)) &&
   1267 		   (ri->ri_flg & RI_CURSOR)) {
   1268 			gftfb_nuke_cursor(ri);
   1269 		}
   1270 		x = ri->ri_xorigin;
   1271 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1272 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1273 		width = ri->ri_emuwidth;
   1274 		height = ri->ri_font->fontheight * nrows;
   1275 		gftfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
   1276 		if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
   1277 			ri->ri_flg &= ~RI_CURSOR;
   1278 	}
   1279 }
   1280 
   1281 static void
   1282 gftfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1283 {
   1284 	struct rasops_info *ri = cookie;
   1285 	struct vcons_screen *scr = ri->ri_hw;
   1286 	struct gftfb_softc *sc = scr->scr_cookie;
   1287 	int32_t x, y, width, height, fg, bg, ul;
   1288 
   1289 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1290 		x = ri->ri_xorigin;
   1291 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1292 		width = ri->ri_emuwidth;
   1293 		height = ri->ri_font->fontheight * nrows;
   1294 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1295 
   1296 		gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1297 
   1298 		if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
   1299 			ri->ri_flg &= ~RI_CURSOR;
   1300 	}
   1301 }
   1302 
   1303 /*
   1304  * cursor sprite handling
   1305  * like most hw info, xf86 3.3 -> nglehdw.h was used as documentation
   1306  * problem is, the PCI EG doesn't quite behave like an S9000_ID_ARTIST
   1307  * the cursor position register bahaves like the one on HCRX while using
   1308  * the same address as Artist, incuding the enable bit and weird handling
   1309  * of negative coordinates. The rest of it, colour map, sprite image etc.,
   1310  * behave like Artist.
   1311  */
   1312 
   1313 static void
   1314 gftfb_move_cursor(struct gftfb_softc *sc, int x, int y)
   1315 {
   1316 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1317 	bus_space_tag_t memt = rom->memt;
   1318 	bus_space_handle_t memh = rom->regh[2];
   1319 	uint32_t pos;
   1320 
   1321 	sc->sc_cursor_x = x;
   1322 	x -= sc->sc_hot_x;
   1323 	sc->sc_cursor_y = y;
   1324 	y -= sc->sc_hot_y;
   1325 
   1326 	if (x < 0) x = 0x1000 - x;
   1327 	if (y < 0) y = 0x1000 - y;
   1328 	pos = (x << 16) | y;
   1329 	if (sc->sc_enabled) pos |= 0x80000000;
   1330 	gftfb_wait(sc);
   1331 	bus_space_write_stream_4(memt, memh, NGLE_REG_17, pos);
   1332 	bus_space_write_stream_4(memt, memh, NGLE_REG_18, 0x80);
   1333 }
   1334 
   1335 static int
   1336 gftfb_do_cursor(struct gftfb_softc *sc, struct wsdisplay_cursor *cur)
   1337 {
   1338 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1339 	bus_space_tag_t memt = rom->memt;
   1340 	bus_space_handle_t memh = rom->regh[2];
   1341 
   1342 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1343 
   1344 		sc->sc_enabled = cur->enable;
   1345 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1346 	}
   1347 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1348 
   1349 		sc->sc_hot_x = cur->hot.x;
   1350 		sc->sc_hot_y = cur->hot.y;
   1351 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1352 	}
   1353 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1354 
   1355 		gftfb_move_cursor(sc, cur->pos.x, cur->pos.y);
   1356 	}
   1357 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1358 		uint32_t rgb;
   1359 		uint8_t r[2], g[2], b[2];
   1360 
   1361 		copyin(cur->cmap.blue, b, 2);
   1362 		copyin(cur->cmap.green, g, 2);
   1363 		copyin(cur->cmap.red, r, 2);
   1364 		mutex_enter(&sc->sc_hwlock);
   1365 		gftfb_wait(sc);
   1366 		bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0xBBE0F000);
   1367 		bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x03000300);
   1368 		bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
   1369 		gftfb_wait(sc);
   1370 		bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
   1371 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0);
   1372 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, 0);
   1373 		rgb = (r[0] << 16) | (g[0] << 8) | b[0];
   1374 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, rgb);	/* BG */
   1375 		rgb = (r[1] << 16) | (g[1] << 8) | b[1];
   1376 		bus_space_write_stream_4(memt, memh, NGLE_REG_4, rgb);	/* FG */
   1377 		bus_space_write_stream_4(memt, memh, NGLE_REG_2, 0);
   1378 		bus_space_write_stream_4(memt, memh, NGLE_REG_26, 0x80008004);
   1379 		gftfb_setup_fb(sc);
   1380 		mutex_exit(&sc->sc_hwlock);
   1381 
   1382 	}
   1383 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1384 		uint32_t buffer[128], latch, tmp;
   1385 		int i;
   1386 
   1387 		copyin(cur->mask, buffer, 512);
   1388 		gftfb_wait(sc);
   1389 		bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x300);
   1390 		bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
   1391 		bus_space_write_stream_4(memt, memh, NGLE_REG_11, 0x28A07000);
   1392 		bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
   1393 		for (i = 0; i < 128; i += 2) {
   1394 			latch = 0;
   1395 			tmp = buffer[i] & 0x80808080;
   1396 			latch |= tmp >> 7;
   1397 			tmp = buffer[i] & 0x40404040;
   1398 			latch |= tmp >> 5;
   1399 			tmp = buffer[i] & 0x20202020;
   1400 			latch |= tmp >> 3;
   1401 			tmp = buffer[i] & 0x10101010;
   1402 			latch |= tmp >> 1;
   1403 			tmp = buffer[i] & 0x08080808;
   1404 			latch |= tmp << 1;
   1405 			tmp = buffer[i] & 0x04040404;
   1406 			latch |= tmp << 3;
   1407 			tmp = buffer[i] & 0x02020202;
   1408 			latch |= tmp << 5;
   1409 			tmp = buffer[i] & 0x01010101;
   1410 			latch |= tmp << 7;
   1411 			bus_space_write_stream_4(memt, memh, NGLE_REG_4, latch);
   1412 			latch = 0;
   1413 			tmp = buffer[i + 1] & 0x80808080;
   1414 			latch |= tmp >> 7;
   1415 			tmp = buffer[i + 1] & 0x40404040;
   1416 			latch |= tmp >> 5;
   1417 			tmp = buffer[i + 1] & 0x20202020;
   1418 			latch |= tmp >> 3;
   1419 			tmp = buffer[i + 1] & 0x10101010;
   1420 			latch |= tmp >> 1;
   1421 			tmp = buffer[i + 1] & 0x08080808;
   1422 			latch |= tmp << 1;
   1423 			tmp = buffer[i + 1] & 0x04040404;
   1424 			latch |= tmp << 3;
   1425 			tmp = buffer[i + 1] & 0x02020202;
   1426 			latch |= tmp << 5;
   1427 			tmp = buffer[i + 1] & 0x01010101;
   1428 			latch |= tmp << 7;
   1429 			bus_space_write_stream_4(memt, memh, NGLE_REG_5, latch);
   1430 		}
   1431 
   1432 		copyin(cur->image, buffer, 512);
   1433 		gftfb_wait(sc);
   1434 		bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x300);
   1435 		bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
   1436 		bus_space_write_stream_4(memt, memh, NGLE_REG_11, 0x28A06000);
   1437 		bus_space_write_stream_4(memt, memh, NGLE_REG_3, 0);
   1438 		for (i = 0; i < 128; i += 2) {
   1439 			latch = 0;
   1440 			tmp = buffer[i] & 0x80808080;
   1441 			latch |= tmp >> 7;
   1442 			tmp = buffer[i] & 0x40404040;
   1443 			latch |= tmp >> 5;
   1444 			tmp = buffer[i] & 0x20202020;
   1445 			latch |= tmp >> 3;
   1446 			tmp = buffer[i] & 0x10101010;
   1447 			latch |= tmp >> 1;
   1448 			tmp = buffer[i] & 0x08080808;
   1449 			latch |= tmp << 1;
   1450 			tmp = buffer[i] & 0x04040404;
   1451 			latch |= tmp << 3;
   1452 			tmp = buffer[i] & 0x02020202;
   1453 			latch |= tmp << 5;
   1454 			tmp = buffer[i] & 0x01010101;
   1455 			latch |= tmp << 7;
   1456 			bus_space_write_stream_4(memt, memh, NGLE_REG_4, latch);
   1457 			latch = 0;
   1458 			tmp = buffer[i + 1] & 0x80808080;
   1459 			latch |= tmp >> 7;
   1460 			tmp = buffer[i + 1] & 0x40404040;
   1461 			latch |= tmp >> 5;
   1462 			tmp = buffer[i + 1] & 0x20202020;
   1463 			latch |= tmp >> 3;
   1464 			tmp = buffer[i + 1] & 0x10101010;
   1465 			latch |= tmp >> 1;
   1466 			tmp = buffer[i + 1] & 0x08080808;
   1467 			latch |= tmp << 1;
   1468 			tmp = buffer[i + 1] & 0x04040404;
   1469 			latch |= tmp << 3;
   1470 			tmp = buffer[i + 1] & 0x02020202;
   1471 			latch |= tmp << 5;
   1472 			tmp = buffer[i + 1] & 0x01010101;
   1473 			latch |= tmp << 7;
   1474 			bus_space_write_stream_4(memt, memh, NGLE_REG_5, latch);
   1475 		}
   1476 		gftfb_setup_fb(sc);
   1477 	}
   1478 
   1479 	return 0;
   1480 }
   1481 
   1482 static void
   1483 gftfb_set_video(struct gftfb_softc *sc, int on)
   1484 {
   1485 	struct sti_rom *rom = sc->sc_base.sc_rom;
   1486 	bus_space_tag_t memt = rom->memt;
   1487 	bus_space_handle_t memh = rom->regh[2];
   1488 
   1489 	if (sc->sc_video_on == on)
   1490 		return;
   1491 
   1492 	sc->sc_video_on = on;
   1493 
   1494 	gftfb_wait(sc);
   1495 	if (on) {
   1496 		bus_space_write_stream_4(memt, memh, NGLE_REG_21,
   1497 		    bus_space_read_stream_4(memt, memh, NGLE_REG_21) | 0x0a000000);
   1498 		bus_space_write_stream_4(memt, memh, NGLE_REG_27,
   1499 		    bus_space_read_stream_4(memt, memh, NGLE_REG_27) | 0x00800000);
   1500 	} else {
   1501 		bus_space_write_stream_4(memt, memh, NGLE_REG_21,
   1502 		    bus_space_read_stream_4(memt, memh, NGLE_REG_21) &  ~0x0a000000);
   1503 		bus_space_write_stream_4(memt, memh, NGLE_REG_27,
   1504 		    bus_space_read_stream_4(memt, memh, NGLE_REG_27) & ~0x00800000);
   1505 	}
   1506 }
   1507