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