Home | History | Annotate | Line # | Download | only in dev
summitfb.c revision 1.9
      1 /*	$NetBSD: summitfb.c,v 1.9 2024/11/27 14:09:10 riastradh 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/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: summitfb.c,v 1.9 2024/11/27 14:09:10 riastradh Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/kmem.h>
     34 #include <sys/device.h>
     35 #include <sys/mutex.h>
     36 
     37 #include <dev/pci/pcivar.h>
     38 #include <dev/pci/pcireg.h>
     39 #include <dev/pci/pcidevs.h>
     40 #include <dev/pci/pciio.h>
     41 
     42 #include <dev/wscons/wsdisplayvar.h>
     43 #include <dev/wscons/wsconsio.h>
     44 #include <dev/wsfont/wsfont.h>
     45 #include <dev/rasops/rasops.h>
     46 #include <dev/wscons/wsdisplay_vconsvar.h>
     47 #include <dev/pci/wsdisplay_pci.h>
     48 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     49 
     50 #include <dev/ic/stireg.h>
     51 #include <dev/ic/summitreg.h>
     52 #include <dev/ic/stivar.h>
     53 
     54 #include "opt_summitfb.h"
     55 
     56 #ifdef SUMMITFB_DEBUG
     57 #define	DPRINTF(s) printf s
     58 #else
     59 #define	DPRINTF(s) __nothing
     60 #endif
     61 
     62 int	summitfb_match(device_t, cfdata_t, void *);
     63 void	summitfb_attach(device_t, device_t, void *);
     64 
     65 struct	summitfb_softc {
     66 	device_t		sc_dev;
     67 	pci_chipset_tag_t	sc_pc;
     68 	pcitag_t		sc_tag;
     69 
     70 	/* stuff we need in order to use the STI ROM */
     71 	struct sti_softc	sc_base;
     72 	struct sti_screen 	sc_scr;
     73 	bus_space_handle_t	sc_romh;
     74 
     75 	int sc_width, sc_height;
     76 	int sc_locked;
     77 	struct vcons_screen sc_console_screen;
     78 	struct wsscreen_descr sc_defaultscreen_descr;
     79 	const struct wsscreen_descr *sc_screens[1];
     80 	struct wsscreen_list sc_screenlist;
     81 	struct vcons_data vd;
     82 	int sc_mode;
     83 	void (*sc_putchar)(void *, int, int, u_int, long);
     84 	u_char sc_cmap_red[256];
     85 	u_char sc_cmap_green[256];
     86 	u_char sc_cmap_blue[256];
     87 	uint32_t sc_write_mode;
     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(summitfb, sizeof(struct summitfb_softc),
     96     summitfb_match, summitfb_attach, NULL, NULL);
     97 
     98 int	summitfb_readbar(struct sti_softc *, struct pci_attach_args *, u_int,
     99 	    int);
    100 int	summitfb_check_rom(struct summitfb_softc *, struct pci_attach_args *);
    101 void	summitfb_enable_rom(struct sti_softc *);
    102 void	summitfb_disable_rom(struct sti_softc *);
    103 void	summitfb_enable_rom_internal(struct summitfb_softc *);
    104 void	summitfb_disable_rom_internal(struct summitfb_softc *);
    105 
    106 void 	summitfb_setup(struct summitfb_softc *);
    107 
    108 /* XXX these really need to go into their own header */
    109 int	sti_pci_is_console(struct pci_attach_args *, bus_addr_t *);
    110 int	sti_rom_setup(struct sti_rom *, bus_space_tag_t, bus_space_tag_t,
    111 	    bus_space_handle_t, bus_addr_t *, u_int);
    112 int	sti_screen_setup(struct sti_screen *, int);
    113 void	sti_describe_screen(struct sti_softc *, struct sti_screen *);
    114 
    115 #define PCI_ROM_SIZE(mr)						      \
    116 	(PCI_MAPREG_ROM_ADDR(mr) & -PCI_MAPREG_ROM_ADDR(mr))
    117 
    118 /* wsdisplay stuff */
    119 static int	summitfb_ioctl(void *, void *, u_long, void *, int,
    120 		    struct lwp *);
    121 static paddr_t	summitfb_mmap(void *, void *, off_t, int);
    122 static void	summitfb_init_screen(void *, struct vcons_screen *, int,
    123 		    long *);
    124 
    125 static int	summitfb_putcmap(struct summitfb_softc *,
    126 		    struct wsdisplay_cmap *);
    127 static int 	summitfb_getcmap(struct summitfb_softc *,
    128 		    struct wsdisplay_cmap *);
    129 static void	summitfb_restore_palette(struct summitfb_softc *);
    130 static int 	summitfb_putpalreg(struct summitfb_softc *, uint8_t, uint8_t,
    131 		    uint8_t, uint8_t);
    132 
    133 static inline void summitfb_setup_fb(struct summitfb_softc *);
    134 
    135 static void	summitfb_rectfill(struct summitfb_softc *, int, int, int, int,
    136 		    uint32_t);
    137 static void	summitfb_bitblt(void *, int, int, int, int, int,
    138 		    int, int);
    139 
    140 #if 0
    141 static void	summitfb_cursor(void *, int, int, int);
    142 #endif
    143 static void	summitfb_putchar(void *, int, int, u_int, long);
    144 static void	summitfb_putchar_aa(void *, int, int, u_int, long);
    145 static void	summitfb_copycols(void *, int, int, int, int);
    146 static void	summitfb_erasecols(void *, int, int, int, long);
    147 static void	summitfb_copyrows(void *, int, int, int);
    148 static void	summitfb_eraserows(void *, int, int, long);
    149 
    150 static void	summitfb_move_cursor(struct summitfb_softc *, int, int);
    151 static int	summitfb_do_cursor(struct summitfb_softc *,
    152 		    struct wsdisplay_cursor *);
    153 
    154 static void	summitfb_set_video(struct summitfb_softc *, int);
    155 
    156 struct wsdisplay_accessops summitfb_accessops = {
    157 	.ioctl = summitfb_ioctl,
    158 	.mmap = summitfb_mmap,
    159 	.alloc_screen = NULL,
    160 	.free_screen = NULL,
    161 	.show_screen = NULL,
    162 	.load_font = NULL,
    163 	.pollc = NULL,
    164 	.scroll = NULL,
    165 };
    166 
    167 static inline void summitfb_wait_fifo(struct summitfb_softc *, uint32_t);
    168 static inline void summitfb_wait(struct summitfb_softc *);
    169 
    170 int
    171 summitfb_match(device_t parent, cfdata_t cf, void *aux)
    172 {
    173 	struct pci_attach_args *paa = aux;
    174 
    175 	if (PCI_VENDOR(paa->pa_id) != PCI_VENDOR_HP)
    176 		return 0;
    177 
    178 	if (PCI_PRODUCT(paa->pa_id) == PCI_PRODUCT_HP_VISUALIZE_FX4)
    179 		return 10;	/* beat out sti at pci */
    180 
    181 	return 0;
    182 }
    183 
    184 static inline uint32_t
    185 summitfb_read4(struct summitfb_softc *sc, uint32_t offset)
    186 {
    187 	struct sti_rom *rom = sc->sc_base.sc_rom;
    188 	bus_space_tag_t memt = rom->memt;
    189 	bus_space_handle_t memh = rom->regh[2];
    190 
    191 	return bus_space_read_stream_4(memt, memh, offset - 0x400000);
    192 }
    193 
    194 static inline void
    195 summitfb_write4(struct summitfb_softc *sc, uint32_t offset, uint32_t val)
    196 {
    197 	struct sti_rom *rom = sc->sc_base.sc_rom;
    198 	bus_space_tag_t memt = rom->memt;
    199 	bus_space_handle_t memh = rom->regh[2];
    200 
    201 	bus_space_write_stream_4(memt, memh, offset - 0x400000, val);
    202 }
    203 
    204 void
    205 summitfb_attach(device_t parent, device_t self, void *aux)
    206 {
    207 	struct summitfb_softc *sc = device_private(self);
    208 	struct pci_attach_args *paa = aux;
    209 	struct sti_rom *rom;
    210 	struct rasops_info *ri;
    211 	struct wsemuldisplaydev_attach_args aa;
    212 	unsigned long defattr = 0;
    213 	int ret, is_console = 0;
    214 
    215 	sc->sc_dev = self;
    216 
    217 	sc->sc_pc = paa->pa_pc;
    218 	sc->sc_tag = paa->pa_tag;
    219 	sc->sc_base.sc_dev = self;
    220 	sc->sc_base.sc_enable_rom = summitfb_enable_rom;
    221 	sc->sc_base.sc_disable_rom = summitfb_disable_rom;
    222 
    223 	aprint_normal("\n");
    224 
    225 	if (summitfb_check_rom(sc, paa) != 0)
    226 		return;
    227 
    228 	ret = sti_pci_is_console(paa, sc->sc_base. bases);
    229 	if (ret != 0) {
    230 		sc->sc_base.sc_flags |= STI_CONSOLE;
    231 		is_console = 1;
    232 	}
    233 	rom = kmem_zalloc(sizeof(*rom), KM_SLEEP);
    234 	rom->rom_softc = &sc->sc_base;
    235 	ret = sti_rom_setup(rom, paa->pa_iot, paa->pa_memt, sc->sc_romh,
    236 	    sc->sc_base.bases, STI_CODEBASE_MAIN);
    237 	if (ret != 0) {
    238 		kmem_free(rom, sizeof(*rom));
    239 		return;
    240 	}
    241 
    242 	sc->sc_base.sc_rom = rom;
    243 
    244 	sc->sc_scr.scr_rom = sc->sc_base.sc_rom;
    245 	ret = sti_screen_setup(&sc->sc_scr, STI_FBMODE);
    246 
    247 	sc->sc_width = sc->sc_scr.scr_cfg.scr_width;
    248 	sc->sc_height = sc->sc_scr.scr_cfg.scr_height;
    249 	sc->sc_write_mode = 0xffffffff;
    250 
    251 	aprint_normal_dev(sc->sc_dev, "%s at %dx%d\n", sc->sc_scr.name,
    252 	    sc->sc_width, sc->sc_height);
    253 	summitfb_setup(sc);
    254 
    255 #ifdef SUMMITFB_DEBUG
    256 	sc->sc_height -= 200;
    257 #endif
    258 
    259 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    260 		.name = "default",
    261 		.ncols = 0, .nrows = 0,
    262 		.textops = NULL,
    263 		.fontwidth = 8, .fontheight = 16,
    264 		.capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT |
    265 		    WSSCREEN_UNDERLINE | WSSCREEN_RESIZE,
    266 		.modecookie = NULL,
    267 	};
    268 
    269 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    270 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    271 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    272 	sc->sc_locked = 0;
    273 
    274 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    275 	    &summitfb_accessops);
    276 	sc->vd.init_screen = summitfb_init_screen;
    277 	sc->vd.show_screen_cookie = &sc->sc_gc;
    278 	sc->vd.show_screen_cb = glyphcache_adapt;
    279 
    280 	ri = &sc->sc_console_screen.scr_ri;
    281 
    282 	sc->sc_gc.gc_bitblt = summitfb_bitblt;
    283 	sc->sc_gc.gc_blitcookie = sc;
    284 	sc->sc_gc.gc_rop = RopSrc;
    285 
    286 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, &defattr);
    287 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    288 
    289 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    290 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    291 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    292 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    293 
    294 	glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    295 	    sc->sc_scr.fbheight - sc->sc_height - 5,
    296 	    sc->sc_scr.fbwidth,
    297 	    ri->ri_font->fontwidth,
    298 	    ri->ri_font->fontheight,
    299 	    defattr);
    300 
    301 	summitfb_restore_palette(sc);
    302 	summitfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    303 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    304 	summitfb_setup_fb(sc);
    305 
    306 	if (is_console) {
    307 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    308 		    defattr);
    309 
    310 		vcons_replay_msgbuf(&sc->sc_console_screen);
    311 	}
    312 
    313 	/* no suspend/resume support yet */
    314 	pmf_device_register(sc->sc_dev, NULL, NULL);
    315 
    316 	aa.console = is_console;
    317 	aa.scrdata = &sc->sc_screenlist;
    318 	aa.accessops = &summitfb_accessops;
    319 	aa.accesscookie = &sc->vd;
    320 
    321 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
    322 }
    323 
    324 /*
    325  * Grovel the STI ROM image.
    326  */
    327 int
    328 summitfb_check_rom(struct summitfb_softc *spc, struct pci_attach_args *pa)
    329 {
    330 	struct sti_softc *sc = &spc->sc_base;
    331 	pcireg_t address, mask;
    332 	bus_space_handle_t romh;
    333 	bus_size_t romsize, subsize, stiromsize;
    334 	bus_addr_t selected, offs, suboffs;
    335 	uint32_t tmp;
    336 	int i;
    337 	int rc;
    338 
    339 	/* sort of inline sti_pci_enable_rom(sc) */
    340 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
    341 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
    342 	    ~PCI_MAPREG_ROM_ENABLE);
    343 	mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
    344 	address |= PCI_MAPREG_ROM_ENABLE;
    345 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, address);
    346 	sc->sc_flags |= STI_ROM_ENABLED;
    347 
    348 	/*
    349 	 * Map the complete ROM for now.
    350 	 */
    351 	romsize = PCI_ROM_SIZE(mask);
    352 	DPRINTF(("%s: mapping rom @ %lx for %lx\n", __func__,
    353 	    (long)PCI_MAPREG_ROM_ADDR(address), (long)romsize));
    354 
    355 	rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address), romsize,
    356 	    0, &romh);
    357 	if (rc != 0) {
    358 		aprint_error_dev(sc->sc_dev, "can't map PCI ROM (%d)\n", rc);
    359 		goto fail2;
    360 	}
    361 
    362 	summitfb_disable_rom_internal(spc);
    363 	/*
    364 	 * Iterate over the ROM images, pick the best candidate.
    365 	 */
    366 
    367 	selected = (bus_addr_t)-1;
    368 	for (offs = 0; offs < romsize; offs += subsize) {
    369 		summitfb_enable_rom_internal(spc);
    370 		/*
    371 		 * Check for a valid ROM header.
    372 		 */
    373 		tmp = bus_space_read_4(pa->pa_memt, romh, offs + 0);
    374 		tmp = le32toh(tmp);
    375 		if (tmp != 0x55aa0000) {
    376 			summitfb_disable_rom_internal(spc);
    377 			if (offs == 0) {
    378 				aprint_error_dev(sc->sc_dev,
    379 				    "invalid PCI ROM header signature"
    380 				    " (%08x)\n", tmp);
    381 				rc = EINVAL;
    382 			}
    383 			break;
    384 		}
    385 
    386 		/*
    387 		 * Check ROM type.
    388 		 */
    389 		tmp = bus_space_read_4(pa->pa_memt, romh, offs + 4);
    390 		tmp = le32toh(tmp);
    391 		if (tmp != 0x00000001) {	/* 1 == STI ROM */
    392 			summitfb_disable_rom_internal(spc);
    393 			if (offs == 0) {
    394 				aprint_error_dev(sc->sc_dev,
    395 				    "invalid PCI ROM type (%08x)\n", tmp);
    396 				rc = EINVAL;
    397 			}
    398 			break;
    399 		}
    400 
    401 		subsize = (bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
    402 		    offs + 0x0c);
    403 		subsize <<= 9;
    404 
    405 #ifdef SUMMITFB_DEBUG
    406 		summitfb_disable_rom_internal(spc);
    407 		DPRINTF(("ROM offset %08x size %08x type %08x",
    408 		    (u_int)offs, (u_int)subsize, tmp));
    409 		summitfb_enable_rom_internal(spc);
    410 #endif
    411 
    412 		/*
    413 		 * Check for a valid ROM data structure.
    414 		 * We do not need it except to know what architecture the ROM
    415 		 * code is for.
    416 		 */
    417 
    418 		suboffs = offs +(bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
    419 		    offs + 0x18);
    420 		tmp = bus_space_read_4(pa->pa_memt, romh, suboffs + 0);
    421 		tmp = le32toh(tmp);
    422 		if (tmp != 0x50434952) {	/* PCIR */
    423 			summitfb_disable_rom_internal(spc);
    424 			if (offs == 0) {
    425 				aprint_error_dev(sc->sc_dev, "invalid PCI data"
    426 				    " signature (%08x)\n", tmp);
    427 				rc = EINVAL;
    428 			} else {
    429 				DPRINTF((" invalid PCI data signature %08x\n",
    430 				    tmp));
    431 				continue;
    432 			}
    433 		}
    434 
    435 		tmp = bus_space_read_1(pa->pa_memt, romh, suboffs + 0x14);
    436 		summitfb_disable_rom_internal(spc);
    437 		DPRINTF((" code %02x", tmp));
    438 
    439 		switch (tmp) {
    440 #ifdef __hppa__
    441 		case 0x10:
    442 			if (selected == (bus_addr_t)-1)
    443 				selected = offs;
    444 			break;
    445 #endif
    446 #ifdef __i386__
    447 		case 0x00:
    448 			if (selected == (bus_addr_t)-1)
    449 				selected = offs;
    450 			break;
    451 #endif
    452 		default:
    453 			DPRINTF((" (wrong architecture)"));
    454 			break;
    455 		}
    456 		DPRINTF(("%s\n", selected == offs ? " -> SELECTED" : ""));
    457 	}
    458 
    459 	if (selected == (bus_addr_t)-1) {
    460 		if (rc == 0) {
    461 			aprint_error_dev(sc->sc_dev, "found no ROM with "
    462 			    "correct microcode architecture\n");
    463 			rc = ENOEXEC;
    464 		}
    465 		goto fail;
    466 	}
    467 
    468 	/*
    469 	 * Read the STI region BAR assignments.
    470 	 */
    471 
    472 	summitfb_enable_rom_internal(spc);
    473 	offs = selected +
    474 	    (bus_addr_t)bus_space_read_2(pa->pa_memt, romh, selected + 0x0e);
    475 	for (i = 0; i < STI_REGION_MAX; i++) {
    476 		rc = summitfb_readbar(sc, pa, i,
    477 		    bus_space_read_1(pa->pa_memt, romh, offs + i));
    478 		if (rc != 0)
    479 			goto fail;
    480 	}
    481 
    482 	/*
    483 	 * Find out where the STI ROM itself lies, and its size.
    484 	 */
    485 
    486 	offs = selected +
    487 	    (bus_addr_t)bus_space_read_4(pa->pa_memt, romh, selected + 0x08);
    488 	stiromsize = (bus_addr_t)bus_space_read_4(pa->pa_memt, romh,
    489 	    offs + 0x18);
    490 	stiromsize = le32toh(stiromsize);
    491 	summitfb_disable_rom_internal(spc);
    492 
    493 	/*
    494 	 * Replace our mapping with a smaller mapping of only the area
    495 	 * we are interested in.
    496 	 */
    497 
    498 	DPRINTF(("remapping rom @ %lx for %lx\n",
    499 	    (long)(PCI_MAPREG_ROM_ADDR(address) + offs), (long)stiromsize));
    500 	bus_space_unmap(pa->pa_memt, romh, romsize);
    501 	rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address) + offs,
    502 	    stiromsize, 0, &spc->sc_romh);
    503 	if (rc != 0) {
    504 		aprint_error_dev(sc->sc_dev, "can't map STI ROM (%d)\n",
    505 		    rc);
    506 		goto fail2;
    507 	}
    508  	summitfb_disable_rom_internal(spc);
    509 	sc->sc_flags &= ~STI_ROM_ENABLED;
    510 
    511 	return 0;
    512 
    513 fail:
    514 	bus_space_unmap(pa->pa_memt, romh, romsize);
    515 fail2:
    516 	summitfb_disable_rom_internal(spc);
    517 
    518 	return rc;
    519 }
    520 
    521 /*
    522  * Decode a BAR register.
    523  */
    524 int
    525 summitfb_readbar(struct sti_softc *sc, struct pci_attach_args *pa,
    526     u_int region, int bar)
    527 {
    528 	bus_addr_t addr;
    529 	bus_size_t size;
    530 	uint32_t cf;
    531 	int rc;
    532 
    533 	if (bar == 0) {
    534 		sc->bases[region] = 0;
    535 		return 0;
    536 	}
    537 
    538 #ifdef DIAGNOSTIC
    539 	if (bar < PCI_MAPREG_START || bar > PCI_MAPREG_PPB_END) {
    540 		summitfb_disable_rom(sc);
    541 		printf("%s: unexpected bar %02x for region %d\n",
    542 		    device_xname(sc->sc_dev), bar, region);
    543 		summitfb_enable_rom(sc);
    544 	}
    545 #endif
    546 
    547 	cf = pci_conf_read(pa->pa_pc, pa->pa_tag, bar);
    548 
    549 	rc = pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, PCI_MAPREG_TYPE(cf),
    550 	    &addr, &size, NULL);
    551 
    552 	if (rc != 0) {
    553 		summitfb_disable_rom(sc);
    554 		aprint_error_dev(sc->sc_dev, "invalid bar %02x"
    555 		    " for region %d\n",
    556 		    bar, region);
    557 		summitfb_enable_rom(sc);
    558 		return rc;
    559 	}
    560 
    561 	sc->bases[region] = addr;
    562 	return 0;
    563 }
    564 
    565 /*
    566  * Enable PCI ROM.
    567  */
    568 void
    569 summitfb_enable_rom_internal(struct summitfb_softc *spc)
    570 {
    571 	pcireg_t address;
    572 
    573 	KASSERT(spc != NULL);
    574 
    575 	address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
    576 	address |= PCI_MAPREG_ROM_ENABLE;
    577 	pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
    578 }
    579 
    580 void
    581 summitfb_enable_rom(struct sti_softc *sc)
    582 {
    583 	struct summitfb_softc *spc = device_private(sc->sc_dev);
    584 
    585 	if (!ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
    586 		summitfb_enable_rom_internal(spc);
    587 	}
    588 	SET(sc->sc_flags, STI_ROM_ENABLED);
    589 }
    590 
    591 /*
    592  * Disable PCI ROM.
    593  */
    594 void
    595 summitfb_disable_rom_internal(struct summitfb_softc *spc)
    596 {
    597 	pcireg_t address;
    598 
    599 	KASSERT(spc != NULL);
    600 
    601 	address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
    602 	address &= ~PCI_MAPREG_ROM_ENABLE;
    603 	pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
    604 }
    605 
    606 void
    607 summitfb_disable_rom(struct sti_softc *sc)
    608 {
    609 	struct summitfb_softc *spc = device_private(sc->sc_dev);
    610 
    611 	if (ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
    612 		summitfb_disable_rom_internal(spc);
    613 	}
    614 	CLR(sc->sc_flags, STI_ROM_ENABLED);
    615 }
    616 
    617 static inline void
    618 summitfb_wait(struct summitfb_softc *sc)
    619 {
    620 
    621 	while (summitfb_read4(sc, VISFX_STATUS) != 0)
    622 		continue;
    623 }
    624 
    625 static inline void
    626 summitfb_write_mode(struct summitfb_softc *sc, uint32_t mode)
    627 {
    628 	if (sc->sc_write_mode == mode) return;
    629 	summitfb_wait(sc);
    630 	summitfb_write4(sc, VISFX_VRAM_WRITE_MODE, mode);
    631 	sc->sc_write_mode = mode;
    632 }
    633 
    634 static inline void
    635 summitfb_setup_fb(struct summitfb_softc *sc)
    636 {
    637 
    638 	summitfb_write_mode(sc, VISFX_WRITE_MODE_PLAIN);
    639 }
    640 
    641 void
    642 summitfb_setup(struct summitfb_softc *sc)
    643 {
    644 
    645 	sc->sc_hot_x = 0;
    646 	sc->sc_hot_y = 0;
    647 	sc->sc_enabled = 0;
    648 	sc->sc_video_on = 1;
    649 
    650 	/*
    651 	 * STI hands us the frame buffer in 32bit access mode,
    652 	 * one of these puts us into 8bit FB access mode
    653 	 */
    654 #if 1
    655 	summitfb_write4(sc, 0xb08044, 0x1b);
    656 	summitfb_write4(sc, 0xb08048, 0x1b);
    657 	summitfb_write4(sc, 0x920860, 0xe4);
    658 	summitfb_write4(sc, 0xa00818, 0);
    659 	summitfb_write4(sc, 0xa00404, 0);
    660 	summitfb_write4(sc, 0x921110, 0);
    661 	summitfb_write4(sc, 0x9211d8, 0);
    662 	summitfb_write4(sc, 0xa0086c, 0);
    663 	summitfb_write4(sc, 0x921114, 0);
    664 	summitfb_write4(sc, 0xac1050, 0);
    665 	summitfb_write4(sc, 0xa00858, 0xb0);	/* 0 on fx4 */
    666 	summitfb_write4(sc, 0xa00850, 0);	/* fx4 */
    667 	summitfb_write4(sc, 0xa0081c, 0);	/* fx4 */
    668 #endif
    669 
    670 	summitfb_wait(sc);
    671 	summitfb_write4(sc, VISFX_PIXEL_MASK, 0xffffffff);
    672 	summitfb_write4(sc, VISFX_PLANE_MASK, 0xffffffff);
    673 	summitfb_write_mode(sc, VISFX_WRITE_MODE_PLAIN);
    674 	summitfb_write4(sc, VISFX_VRAM_READ_MODE, VISFX_READ_MODE_COPY);
    675 
    676 	summitfb_setup_fb(sc);
    677 }
    678 
    679 static int
    680 summitfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    681     struct lwp *l)
    682 {
    683 	struct vcons_data *vd = v;
    684 	struct summitfb_softc *sc = vd->cookie;
    685 	struct wsdisplay_fbinfo *wdf;
    686 	struct vcons_screen *ms = vd->active;
    687 
    688 	switch (cmd) {
    689 	case WSDISPLAYIO_GTYPE:
    690 		*(u_int *)data = WSDISPLAY_TYPE_STI;
    691 		return 0;
    692 
    693 	case GCID:
    694 		*(u_int *)data = sc->sc_scr.scr_rom->rom_dd.dd_grid[0];
    695 		return 0;
    696 
    697 	/* PCI config read/write passthrough. */
    698 	case PCI_IOC_CFGREAD:
    699 	case PCI_IOC_CFGWRITE:
    700 		return pci_devioctl(sc->sc_pc, sc->sc_tag,
    701 		    cmd, data, flag, l);
    702 
    703 	case WSDISPLAYIO_GET_BUSID:
    704 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    705 		    sc->sc_tag, data);
    706 
    707 	case WSDISPLAYIO_GINFO:
    708 		if (ms == NULL)
    709 			return ENODEV;
    710 		wdf = data;
    711 		wdf->height = ms->scr_ri.ri_height;
    712 		wdf->width = ms->scr_ri.ri_width;
    713 		wdf->depth = ms->scr_ri.ri_depth;
    714 		wdf->cmsize = 256;
    715 		return 0;
    716 
    717 	case WSDISPLAYIO_GETCMAP:
    718 		return summitfb_getcmap(sc,
    719 		    (struct wsdisplay_cmap *)data);
    720 
    721 	case WSDISPLAYIO_PUTCMAP:
    722 		return summitfb_putcmap(sc,
    723 		    (struct wsdisplay_cmap *)data);
    724 
    725 	case WSDISPLAYIO_LINEBYTES:
    726 		*(u_int *)data = 2048;
    727 		return 0;
    728 
    729 	case WSDISPLAYIO_SMODE: {
    730 		int new_mode = *(int *)data;
    731 
    732 		if (new_mode != sc->sc_mode) {
    733 			sc->sc_mode = new_mode;
    734 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    735 				summitfb_setup(sc);
    736 				summitfb_restore_palette(sc);
    737 				//glyphcache_wipe(&sc->sc_gc);
    738 				summitfb_rectfill(sc, 0, 0, sc->sc_width,
    739 				    sc->sc_height, ms->scr_ri.ri_devcmap[
    740 				    (ms->scr_defattr >> 16) & 0xff]);
    741 				vcons_redraw_screen(ms);
    742 				summitfb_set_video(sc, 1);
    743 			}
    744 			summitfb_setup_fb(sc);
    745 		}
    746 		return 0;
    747 	}
    748 
    749 	case WSDISPLAYIO_GET_FBINFO: {
    750 		struct wsdisplayio_fbinfo *fbi = data;
    751 		int ret;
    752 
    753 		ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    754 		fbi->fbi_fbsize = sc->sc_scr.fbheight * 2048;
    755 		return ret;
    756 	}
    757 
    758 	case WSDISPLAYIO_GCURPOS: {
    759 		struct wsdisplay_curpos *cp = data;
    760 
    761 		cp->x = sc->sc_cursor_x;
    762 		cp->y = sc->sc_cursor_y;
    763 		return 0;
    764 	}
    765 
    766 	case WSDISPLAYIO_SCURPOS: {
    767 		struct wsdisplay_curpos *cp = data;
    768 
    769 		summitfb_move_cursor(sc, cp->x, cp->y);
    770 		return 0;
    771 	}
    772 
    773 	case WSDISPLAYIO_GCURMAX: {
    774 		struct wsdisplay_curpos *cp = data;
    775 
    776 		cp->x = 64;
    777 		cp->y = 64;
    778 		return 0;
    779 	}
    780 
    781 	case WSDISPLAYIO_SCURSOR: {
    782 		struct wsdisplay_cursor *cursor = data;
    783 
    784 		return summitfb_do_cursor(sc, cursor);
    785 	}
    786 
    787 	case WSDISPLAYIO_SVIDEO:
    788 		summitfb_set_video(sc, *(int *)data);
    789 		return 0;
    790 	case WSDISPLAYIO_GVIDEO:
    791 		*(u_int *)data = sc->sc_video_on ?
    792 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    793 		return 0;
    794 	}
    795 	return EPASSTHROUGH;
    796 }
    797 
    798 static paddr_t
    799 summitfb_mmap(void *v, void *vs, off_t offset, int prot)
    800 {
    801 	struct vcons_data *vd = v;
    802 	struct summitfb_softc *sc = vd->cookie;
    803 	struct sti_rom *rom = sc->sc_base.sc_rom;
    804 	paddr_t pa = -1;
    805 
    806 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
    807 		return -1;
    808 
    809 	if (offset >= 0 && offset < sc->sc_scr.fblen) {
    810 		/* framebuffer */
    811 		pa = bus_space_mmap(rom->memt, sc->sc_scr.fbaddr, offset,
    812 		    prot, BUS_SPACE_MAP_LINEAR);
    813 	} else if (offset >= 0x80000000 && offset < 0x80400000) {
    814 		/* blitter registers etc. */
    815 		pa = bus_space_mmap(rom->memt, rom->regh[0],
    816 		    offset - 0x80000000, prot, BUS_SPACE_MAP_LINEAR);
    817 	}
    818 
    819 	return pa;
    820 }
    821 
    822 static void
    823 summitfb_init_screen(void *cookie, struct vcons_screen *scr,
    824     int existing, long *defattr)
    825 {
    826 	struct summitfb_softc *sc = cookie;
    827 	struct rasops_info *ri = &scr->scr_ri;
    828 
    829 	ri->ri_depth = 8;
    830 	ri->ri_width = sc->sc_width;
    831 	ri->ri_height = sc->sc_height;
    832 	ri->ri_stride = 2048;
    833 	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB /*|
    834 		     RI_ENABLE_ALPHA | RI_PREFER_ALPHA*/;
    835 
    836 	ri->ri_bits = (void *)sc->sc_scr.fbaddr;
    837 	rasops_init(ri, 0, 0);
    838 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    839 	    WSSCREEN_RESIZE;
    840 	scr->scr_flags |= VCONS_LOADFONT | VCONS_NO_CURSOR;
    841 
    842 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    843 	    sc->sc_width / ri->ri_font->fontwidth);
    844 
    845 	ri->ri_hw = scr;
    846 
    847 	sc->sc_putchar = ri->ri_ops.putchar;
    848 
    849 	ri->ri_ops.copyrows = summitfb_copyrows;
    850 	ri->ri_ops.copycols = summitfb_copycols;
    851 	ri->ri_ops.eraserows = summitfb_eraserows;
    852 	ri->ri_ops.erasecols = summitfb_erasecols;
    853 	//ri->ri_ops.cursor = summitfb_cursor;
    854 	if (FONT_IS_ALPHA(ri->ri_font)) {
    855 		ri->ri_ops.putchar = summitfb_putchar_aa;
    856 	} else
    857 		ri->ri_ops.putchar = summitfb_putchar;
    858 }
    859 
    860 static int
    861 summitfb_putcmap(struct summitfb_softc *sc, struct wsdisplay_cmap *cm)
    862 {
    863 	u_char *r, *g, *b;
    864 	u_int index = cm->index;
    865 	u_int count = cm->count;
    866 	int i, error;
    867 	u_char rbuf[256], gbuf[256], bbuf[256];
    868 
    869 	if (cm->index >= 256 || cm->count > 256 ||
    870 	    (cm->index + cm->count) > 256)
    871 		return EINVAL;
    872 	error = copyin(cm->red, &rbuf[index], count);
    873 	if (error)
    874 		return error;
    875 	error = copyin(cm->green, &gbuf[index], count);
    876 	if (error)
    877 		return error;
    878 	error = copyin(cm->blue, &bbuf[index], count);
    879 	if (error)
    880 		return error;
    881 
    882 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    883 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    884 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    885 
    886 	r = &sc->sc_cmap_red[index];
    887 	g = &sc->sc_cmap_green[index];
    888 	b = &sc->sc_cmap_blue[index];
    889 
    890 	for (i = 0; i < count; i++) {
    891 		summitfb_putpalreg(sc, index, *r, *g, *b);
    892 		index++;
    893 		r++, g++, b++;
    894 	}
    895 	return 0;
    896 }
    897 
    898 static int
    899 summitfb_getcmap(struct summitfb_softc *sc, struct wsdisplay_cmap *cm)
    900 {
    901 	u_int index = cm->index;
    902 	u_int count = cm->count;
    903 	int error;
    904 
    905 	if (index >= 255 || count > 256 || index + count > 256)
    906 		return EINVAL;
    907 
    908 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    909 	if (error)
    910 		return error;
    911 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    912 	if (error)
    913 		return error;
    914 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    915 	if (error)
    916 		return error;
    917 
    918 	return 0;
    919 }
    920 
    921 static void
    922 summitfb_restore_palette(struct summitfb_softc *sc)
    923 {
    924 	uint8_t cmap[768];
    925 	int i, j;
    926 
    927 	j = 0;
    928 	rasops_get_cmap(&sc->sc_console_screen.scr_ri, cmap, sizeof(cmap));
    929 	for (i = 0; i < 256; i++) {
    930 		sc->sc_cmap_red[i] = cmap[j];
    931 		sc->sc_cmap_green[i] = cmap[j + 1];
    932 		sc->sc_cmap_blue[i] = cmap[j + 2];
    933 		summitfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    934 		j += 3;
    935 	}
    936 }
    937 
    938 static int
    939 summitfb_putpalreg(struct summitfb_softc *sc, uint8_t idx,
    940     uint8_t r, uint8_t g, uint8_t b)
    941 {
    942 
    943 	summitfb_write4(sc, VISFX_COLOR_INDEX, 0xc0005100 + idx);
    944 	summitfb_write4(sc, VISFX_COLOR_VALUE, (r << 16) | ( g << 8) | b);
    945 	summitfb_write4(sc, VISFX_COLOR_MASK, 0xff);
    946 	summitfb_write4(sc, 0x80004c, 0xc);
    947 	summitfb_write4(sc, 0x800000, 0);
    948 	return 0;
    949 }
    950 
    951 static inline void
    952 summitfb_wait_fifo(struct summitfb_softc *sc, uint32_t slots)
    953 {
    954 #if 0
    955 	uint32_t reg;
    956 
    957 	do {
    958 		reg = summitfb_read4(sc, NGLE_REG_34);
    959 	} while (reg < slots);
    960 #endif
    961 }
    962 
    963 static void
    964 summitfb_rectfill(struct summitfb_softc *sc, int x, int y, int wi, int he,
    965     uint32_t bg)
    966 {
    967 
    968 	summitfb_write_mode(sc, VISFX_WRITE_MODE_FILL);
    969 	summitfb_write4(sc, VISFX_FG_COLOUR, bg);
    970 	summitfb_write4(sc, VISFX_START, (x << 16) | y);
    971 	summitfb_write4(sc, VISFX_SIZE, (wi << 16) | he);
    972 }
    973 
    974 static void
    975 summitfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
    976     int he, int rop)
    977 {
    978 	struct summitfb_softc *sc = cookie;
    979 
    980 	/* XXX no ROP support yet */
    981 	summitfb_write_mode(sc, VISFX_WRITE_MODE_PLAIN);
    982 	summitfb_write4(sc, VISFX_COPY_SRC, (xs << 16) | ys);
    983 	summitfb_write4(sc, VISFX_COPY_WH, (wi << 16) | he);
    984 	summitfb_write4(sc, VISFX_COPY_DST, (xd << 16) | yd);
    985 
    986 }
    987 
    988 #if 0
    989 static void
    990 summitfb_nuke_cursor(struct rasops_info *ri)
    991 {
    992 	struct vcons_screen *scr = ri->ri_hw;
    993 	struct summitfb_softc *sc = scr->scr_cookie;
    994 	int wi, he, x, y;
    995 
    996 	if (ri->ri_flg & RI_CURSOR) {
    997 		wi = ri->ri_font->fontwidth;
    998 		he = ri->ri_font->fontheight;
    999 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1000 		y = ri->ri_crow * he + ri->ri_yorigin;
   1001 		if (0) summitfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1002 		ri->ri_flg &= ~RI_CURSOR;
   1003 	}
   1004 }
   1005 
   1006 static void
   1007 summitfb_cursor(void *cookie, int on, int row, int col)
   1008 {
   1009 	struct rasops_info *ri = cookie;
   1010 	struct vcons_screen *scr = ri->ri_hw;
   1011 	struct summitfb_softc *sc = scr->scr_cookie;
   1012 	int x, y, wi, he;
   1013 
   1014 	wi = ri->ri_font->fontwidth;
   1015 	he = ri->ri_font->fontheight;
   1016 
   1017 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1018 		if (on) {
   1019 			if (ri->ri_flg & RI_CURSOR) {
   1020 				summitfb_nuke_cursor(ri);
   1021 			}
   1022 			x = col * wi + ri->ri_xorigin;
   1023 			y = row * he + ri->ri_yorigin;
   1024 			summitfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1025 			ri->ri_flg |= RI_CURSOR;
   1026 		}
   1027 		ri->ri_crow = row;
   1028 		ri->ri_ccol = col;
   1029 	} else {
   1030 		ri->ri_crow = row;
   1031 		ri->ri_ccol = col;
   1032 		ri->ri_flg &= ~RI_CURSOR;
   1033 	}
   1034 
   1035 }
   1036 #endif
   1037 
   1038 static void
   1039 summitfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1040 {
   1041 	struct rasops_info *ri = cookie;
   1042 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1043 	struct vcons_screen *scr = ri->ri_hw;
   1044 	struct summitfb_softc *sc = scr->scr_cookie;
   1045 	void *data;
   1046 	int i, x, y, wi, he/*, rv = GC_NOPE*/;
   1047 	uint32_t bg, fg, mask;
   1048 
   1049 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1050 		return;
   1051 
   1052 	if (!CHAR_IN_FONT(c, font))
   1053 		return;
   1054 
   1055 #if 0
   1056 	if (row == ri->ri_crow && col == ri->ri_ccol) {
   1057 		ri->ri_flg &= ~RI_CURSOR;
   1058 	}
   1059 #endif
   1060 	wi = font->fontwidth;
   1061 	he = font->fontheight;
   1062 
   1063 	x = ri->ri_xorigin + col * wi;
   1064 	y = ri->ri_yorigin + row * he;
   1065 
   1066 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1067 
   1068 	/* if we're drawing a space we're done here */
   1069 	if (c == 0x20) {
   1070 		summitfb_rectfill(sc, x, y, wi, he, bg);
   1071 		return;
   1072 	}
   1073 
   1074 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
   1075 
   1076 #if 0
   1077 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1078 	if (rv == GC_OK)
   1079 		return;
   1080 #endif
   1081 
   1082 	summitfb_write_mode(sc, VISFX_WRITE_MODE_EXPAND);
   1083 	summitfb_write4(sc, VISFX_FG_COLOUR, fg);
   1084 	summitfb_write4(sc, VISFX_BG_COLOUR, bg);
   1085 	mask = 0xffffffff << (32 - wi);
   1086 	summitfb_write4(sc, VISFX_PIXEL_MASK, mask);
   1087 	/* not a tpyo, coordinates *are* backwards for this register */
   1088 	summitfb_write4(sc, VISFX_VRAM_WRITE_DEST, (y << 16) | x);
   1089 
   1090 	data = WSFONT_GLYPH(c, font);
   1091 
   1092 	if (ri->ri_font->stride == 1) {
   1093 		uint8_t *data8 = data;
   1094 		for (i = 0; i < he; i++) {
   1095 			mask = *data8;
   1096 			summitfb_write4(sc, VISFX_VRAM_WRITE_DATA_INCRY,
   1097 			    mask << 24);
   1098 			data8++;
   1099 		}
   1100 	} else {
   1101 		uint16_t *data16 = data;
   1102 		for (i = 0; i < he; i++) {
   1103 			mask = *data16;
   1104 			summitfb_write4(sc, VISFX_VRAM_WRITE_DATA_INCRY,
   1105 			    mask << 16);
   1106 			data16++;
   1107 		}
   1108 	}
   1109 #if 0
   1110 	if (rv == GC_ADD)
   1111 		glyphcache_add(&sc->sc_gc, c, x, y);
   1112 #endif
   1113 }
   1114 
   1115 static void
   1116 summitfb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
   1117 {
   1118 	struct rasops_info *ri = cookie;
   1119 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1120 	struct vcons_screen *scr = ri->ri_hw;
   1121 	struct summitfb_softc *sc = scr->scr_cookie;
   1122 	int x, y, wi, he, rv = GC_NOPE;
   1123 	uint32_t bg;
   1124 
   1125 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1126 		return;
   1127 
   1128 	if (!CHAR_IN_FONT(c, font))
   1129 		return;
   1130 
   1131 	if (row == ri->ri_crow && col == ri->ri_ccol) {
   1132 		ri->ri_flg &= ~RI_CURSOR;
   1133 	}
   1134 
   1135 	wi = font->fontwidth;
   1136 	he = font->fontheight;
   1137 
   1138 	x = ri->ri_xorigin + col * wi;
   1139 	y = ri->ri_yorigin + row * he;
   1140 
   1141 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1142 
   1143 	if (c == 0x20) {
   1144 		summitfb_rectfill(sc, x, y, wi, he, bg);
   1145 		return;
   1146 	}
   1147 
   1148 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1149 	if (rv == GC_OK)
   1150 		return;
   1151 
   1152 	summitfb_setup_fb(sc);
   1153 	sc->sc_putchar(cookie, row, col, c, attr);
   1154 
   1155 	if (rv == GC_ADD)
   1156 		glyphcache_add(&sc->sc_gc, c, x, y);
   1157 }
   1158 
   1159 static void
   1160 summitfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1161 {
   1162 	struct rasops_info *ri = cookie;
   1163 	struct vcons_screen *scr = ri->ri_hw;
   1164 	struct summitfb_softc *sc = scr->scr_cookie;
   1165 	int32_t xs, xd, y, width, height;
   1166 
   1167 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1168 #if 0
   1169 		if (ri->ri_crow == row &&
   1170 		    ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols) &&
   1171 		    (ri->ri_flg & RI_CURSOR)) {
   1172 			summitfb_nuke_cursor(ri);
   1173 		}
   1174 #endif
   1175 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1176 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1177 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1178 		width = ri->ri_font->fontwidth * ncols;
   1179 		height = ri->ri_font->fontheight;
   1180 		summitfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
   1181 
   1182 		if (ri->ri_crow == row &&
   1183 		    ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols))
   1184 			ri->ri_flg &= ~RI_CURSOR;
   1185 	}
   1186 }
   1187 
   1188 static void
   1189 summitfb_erasecols(void *cookie, int row, int startcol, int ncols,
   1190     long fillattr)
   1191 {
   1192 	struct rasops_info *ri = cookie;
   1193 	struct vcons_screen *scr = ri->ri_hw;
   1194 	struct summitfb_softc *sc = scr->scr_cookie;
   1195 	int32_t x, y, width, height, fg, bg, ul;
   1196 
   1197 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1198 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1199 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1200 		width = ri->ri_font->fontwidth * ncols;
   1201 		height = ri->ri_font->fontheight;
   1202 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1203 
   1204 		summitfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1205 
   1206 		if (ri->ri_crow == row &&
   1207 		    ri->ri_ccol >= startcol &&
   1208 		    ri->ri_ccol < (startcol + ncols))
   1209 			ri->ri_flg &= ~RI_CURSOR;
   1210 	}
   1211 }
   1212 
   1213 static void
   1214 summitfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1215 {
   1216 	struct rasops_info *ri = cookie;
   1217 	struct vcons_screen *scr = ri->ri_hw;
   1218 	struct summitfb_softc *sc = scr->scr_cookie;
   1219 	int32_t x, ys, yd, width, height;
   1220 
   1221 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1222 #if 0
   1223 		if (ri->ri_crow >= srcrow && ri->ri_crow < (srcrow + nrows) &&
   1224 		    (ri->ri_flg & RI_CURSOR)) {
   1225 			summitfb_nuke_cursor(ri);
   1226 		}
   1227 #endif
   1228 		x = ri->ri_xorigin;
   1229 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1230 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1231 		width = ri->ri_emuwidth;
   1232 		height = ri->ri_font->fontheight * nrows;
   1233 		summitfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
   1234 
   1235 		if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
   1236 			ri->ri_flg &= ~RI_CURSOR;
   1237 	}
   1238 }
   1239 
   1240 static void
   1241 summitfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1242 {
   1243 	struct rasops_info *ri = cookie;
   1244 	struct vcons_screen *scr = ri->ri_hw;
   1245 	struct summitfb_softc *sc = scr->scr_cookie;
   1246 	int32_t x, y, width, height, fg, bg, ul;
   1247 
   1248 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1249 		x = ri->ri_xorigin;
   1250 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1251 		width = ri->ri_emuwidth;
   1252 		height = ri->ri_font->fontheight * nrows;
   1253 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1254 
   1255 		summitfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1256 
   1257 		if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
   1258 			ri->ri_flg &= ~RI_CURSOR;
   1259 	}
   1260 }
   1261 
   1262 static void
   1263 summitfb_move_cursor(struct summitfb_softc *sc, int x, int y)
   1264 {
   1265 	uint32_t pos;
   1266 
   1267 	sc->sc_cursor_x = x;
   1268 	x -= sc->sc_hot_x;
   1269 	sc->sc_cursor_y = y;
   1270 	y -= sc->sc_hot_y;
   1271 
   1272 	if (x < 0) x = 0x1000 - x;
   1273 	if (y < 0) y = 0x1000 - y;
   1274 	pos = (x << 16) | y;
   1275 	if (sc->sc_enabled) pos |= 0x80000000;
   1276 	summitfb_write4(sc, VISFX_CURSOR_POS, pos);
   1277 }
   1278 
   1279 static int
   1280 summitfb_do_cursor(struct summitfb_softc *sc, struct wsdisplay_cursor *cur)
   1281 {
   1282 
   1283 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1284 		sc->sc_enabled = cur->enable;
   1285 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1286 	}
   1287 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1288 		sc->sc_hot_x = cur->hot.x;
   1289 		sc->sc_hot_y = cur->hot.y;
   1290 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1291 	}
   1292 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1293 		summitfb_move_cursor(sc, cur->pos.x, cur->pos.y);
   1294 	}
   1295 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1296 		uint32_t rgb;
   1297 		uint8_t r[2], g[2], b[2];
   1298 
   1299 		copyin(cur->cmap.blue, b, 2);
   1300 		copyin(cur->cmap.green, g, 2);
   1301 		copyin(cur->cmap.red, r, 2);
   1302 		summitfb_write4(sc, VISFX_CURSOR_INDEX, 0);
   1303 		rgb = r[0] << 16 | g[0] << 8 | b[0];
   1304 		summitfb_write4(sc, VISFX_CURSOR_COLOR, rgb);
   1305 		rgb = r[1] << 16 | g[1] << 8 | b[1];
   1306 		/* this isn't right */
   1307 		summitfb_write4(sc, VISFX_CURSOR_COLOR + 4, rgb);
   1308 
   1309 	}
   1310 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1311 
   1312 		uint32_t buffer[128], latch, tmp;
   1313 		int i;
   1314 
   1315 		copyin(cur->mask, buffer, 512);
   1316 		summitfb_write4(sc, VISFX_CURSOR_INDEX, 0);
   1317 		for (i = 0; i < 128; i += 2) {
   1318 			latch = 0;
   1319 			tmp = buffer[i] & 0x80808080;
   1320 			latch |= tmp >> 7;
   1321 			tmp = buffer[i] & 0x40404040;
   1322 			latch |= tmp >> 5;
   1323 			tmp = buffer[i] & 0x20202020;
   1324 			latch |= tmp >> 3;
   1325 			tmp = buffer[i] & 0x10101010;
   1326 			latch |= tmp >> 1;
   1327 			tmp = buffer[i] & 0x08080808;
   1328 			latch |= tmp << 1;
   1329 			tmp = buffer[i] & 0x04040404;
   1330 			latch |= tmp << 3;
   1331 			tmp = buffer[i] & 0x02020202;
   1332 			latch |= tmp << 5;
   1333 			tmp = buffer[i] & 0x01010101;
   1334 			latch |= tmp << 7;
   1335 			summitfb_write4(sc, VISFX_CURSOR_DATA, latch);
   1336 			latch = 0;
   1337 			tmp = buffer[i + 1] & 0x80808080;
   1338 			latch |= tmp >> 7;
   1339 			tmp = buffer[i + 1] & 0x40404040;
   1340 			latch |= tmp >> 5;
   1341 			tmp = buffer[i + 1] & 0x20202020;
   1342 			latch |= tmp >> 3;
   1343 			tmp = buffer[i + 1] & 0x10101010;
   1344 			latch |= tmp >> 1;
   1345 			tmp = buffer[i + 1] & 0x08080808;
   1346 			latch |= tmp << 1;
   1347 			tmp = buffer[i + 1] & 0x04040404;
   1348 			latch |= tmp << 3;
   1349 			tmp = buffer[i + 1] & 0x02020202;
   1350 			latch |= tmp << 5;
   1351 			tmp = buffer[i + 1] & 0x01010101;
   1352 			latch |= tmp << 7;
   1353 			summitfb_write4(sc, VISFX_CURSOR_DATA, latch);
   1354 		}
   1355 
   1356 		summitfb_write4(sc, VISFX_CURSOR_INDEX, 0x80);
   1357 		copyin(cur->image, buffer, 512);
   1358 		for (i = 0; i < 128; i += 2) {
   1359 			latch = 0;
   1360 			tmp = buffer[i] & 0x80808080;
   1361 			latch |= tmp >> 7;
   1362 			tmp = buffer[i] & 0x40404040;
   1363 			latch |= tmp >> 5;
   1364 			tmp = buffer[i] & 0x20202020;
   1365 			latch |= tmp >> 3;
   1366 			tmp = buffer[i] & 0x10101010;
   1367 			latch |= tmp >> 1;
   1368 			tmp = buffer[i] & 0x08080808;
   1369 			latch |= tmp << 1;
   1370 			tmp = buffer[i] & 0x04040404;
   1371 			latch |= tmp << 3;
   1372 			tmp = buffer[i] & 0x02020202;
   1373 			latch |= tmp << 5;
   1374 			tmp = buffer[i] & 0x01010101;
   1375 			latch |= tmp << 7;
   1376 			summitfb_write4(sc, VISFX_CURSOR_DATA, latch);
   1377 			latch = 0;
   1378 			tmp = buffer[i + 1] & 0x80808080;
   1379 			latch |= tmp >> 7;
   1380 			tmp = buffer[i + 1] & 0x40404040;
   1381 			latch |= tmp >> 5;
   1382 			tmp = buffer[i + 1] & 0x20202020;
   1383 			latch |= tmp >> 3;
   1384 			tmp = buffer[i + 1] & 0x10101010;
   1385 			latch |= tmp >> 1;
   1386 			tmp = buffer[i + 1] & 0x08080808;
   1387 			latch |= tmp << 1;
   1388 			tmp = buffer[i + 1] & 0x04040404;
   1389 			latch |= tmp << 3;
   1390 			tmp = buffer[i + 1] & 0x02020202;
   1391 			latch |= tmp << 5;
   1392 			tmp = buffer[i + 1] & 0x01010101;
   1393 			latch |= tmp << 7;
   1394 			summitfb_write4(sc, VISFX_CURSOR_DATA, latch);
   1395 		}
   1396 		summitfb_setup_fb(sc);
   1397 	}
   1398 
   1399 	return 0;
   1400 }
   1401 
   1402 static void
   1403 summitfb_set_video(struct summitfb_softc *sc, int on)
   1404 {
   1405 
   1406 	if (sc->sc_video_on == on)
   1407 		return;
   1408 
   1409 	sc->sc_video_on = on;
   1410 
   1411 	summitfb_wait(sc);
   1412 	if (on) {
   1413 	} else {
   1414 	}
   1415 }
   1416