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