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