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