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