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