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