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