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