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