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