Home | History | Annotate | Line # | Download | only in dev
hyperfb.c revision 1.8
      1 /*	$NetBSD: hyperfb.c,v 1.8 2024/08/01 00:20:22 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2024 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     26  * THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * a native driver for HCRX / hyperdrive cards
     31  * tested on a HCRX24Z in a C360 only so far
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: hyperfb.c,v 1.8 2024/08/01 00:20:22 macallan Exp $");
     36 
     37 #include "opt_cputype.h"
     38 #include "opt_hyperfb.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 
     44 #include <sys/bus.h>
     45 #include <machine/cpu.h>
     46 #include <machine/iomod.h>
     47 #include <machine/autoconf.h>
     48 
     49 #include <dev/wscons/wsdisplayvar.h>
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wsfont/wsfont.h>
     52 #include <dev/rasops/rasops.h>
     53 #include <dev/wscons/wsdisplay_vconsvar.h>
     54 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     55 
     56 #include <dev/ic/stireg.h>
     57 #include <dev/ic/stivar.h>
     58 
     59 #include <hppa/dev/cpudevs.h>
     60 #include <hppa/hppa/machdep.h>
     61 
     62 #ifdef HYPERFB_DEBUG
     63 #define	DPRINTF printf
     64 #else
     65 #define DPRINTF if (0) printf
     66 #endif
     67 
     68 #define	STI_ROMSIZE	(sizeof(struct sti_dd) * 4)
     69 
     70 #define HCRX_FBOFFSET	0x01000000
     71 #define HCRX_FBLEN	0x01000000
     72 #define HCRX_REGOFFSET	0x00100000
     73 #define HCRX_REGLEN	0x00280000
     74 
     75 #define HCRX_CONFIG_24BIT	0x100
     76 
     77 int hyperfb_match(device_t, cfdata_t, void *);
     78 void hyperfb_attach(device_t, device_t, void *);
     79 
     80 struct	hyperfb_softc {
     81 	device_t		sc_dev;
     82 	bus_space_tag_t		sc_iot;
     83 	bus_addr_t		sc_base;
     84 	bus_space_handle_t	sc_hfb, sc_hreg;
     85 	void 			*sc_fb;
     86 
     87 	int sc_width, sc_height;
     88 	int sc_locked, sc_is_console, sc_24bit;
     89 	struct vcons_screen sc_console_screen;
     90 	struct wsscreen_descr sc_defaultscreen_descr;
     91 	const struct wsscreen_descr *sc_screens[1];
     92 	struct wsscreen_list sc_screenlist;
     93 	struct vcons_data vd;
     94 	int sc_mode;
     95 	void (*sc_putchar)(void *, int, int, u_int, long);
     96 	u_char sc_cmap_red[256];
     97 	u_char sc_cmap_green[256];
     98 	u_char sc_cmap_blue[256];
     99 	kmutex_t sc_hwlock;
    100 	uint32_t sc_hwmode;
    101 #define HW_FB		0
    102 #define HW_FILL		1
    103 #define HW_BLIT		2
    104 #define HW_SFILL	3
    105 	/* cursor stuff */
    106 	int sc_cursor_x, sc_cursor_y;
    107 	int sc_hot_x, sc_hot_y, sc_enabled;
    108 	int sc_video_on;
    109 	glyphcache sc_gc;
    110 };
    111 
    112 extern struct cfdriver hyperfb_cd;
    113 
    114 CFATTACH_DECL_NEW(hyperfb, sizeof(struct hyperfb_softc), hyperfb_match,
    115     hyperfb_attach, NULL, NULL);
    116 
    117 static inline void  hyperfb_setup_fb(struct hyperfb_softc *);
    118 static inline void  hyperfb_setup_fb24(struct hyperfb_softc *);
    119 static void 	hyperfb_init_screen(void *, struct vcons_screen *,
    120 			    int, long *);
    121 static int	hyperfb_ioctl(void *, void *, u_long, void *, int,
    122 			     struct lwp *);
    123 static paddr_t	hyperfb_mmap(void *, void *, off_t, int);
    124 
    125 static int	hyperfb_putcmap(struct hyperfb_softc *,
    126 		    struct wsdisplay_cmap *);
    127 static int 	hyperfb_getcmap(struct hyperfb_softc *,
    128 		    struct wsdisplay_cmap *);
    129 static void	hyperfb_restore_palette(struct hyperfb_softc *);
    130 static int 	hyperfb_putpalreg(struct hyperfb_softc *, uint8_t, uint8_t,
    131 			    uint8_t, uint8_t);
    132 void 	hyperfb_setup(struct hyperfb_softc *);
    133 static void	hyperfb_set_video(struct hyperfb_softc *, int);
    134 
    135 static void	hyperfb_rectfill(struct hyperfb_softc *, int, int, int, int,
    136 			    uint32_t);
    137 static void	hyperfb_bitblt(void *, int, int, int, int, int,
    138 			    int, int);
    139 
    140 static void	hyperfb_cursor(void *, int, int, int);
    141 static void	hyperfb_putchar(void *, int, int, u_int, long);
    142 static void	hyperfb_copycols(void *, int, int, int, int);
    143 static void	hyperfb_erasecols(void *, int, int, int, long);
    144 static void	hyperfb_copyrows(void *, int, int, int);
    145 static void	hyperfb_eraserows(void *, int, int, long);
    146 
    147 static void	hyperfb_move_cursor(struct hyperfb_softc *, int, int);
    148 static int	hyperfb_do_cursor(struct hyperfb_softc *,
    149 		    struct wsdisplay_cursor *);
    150 
    151 static inline void hyperfb_wait_fifo(struct hyperfb_softc *, uint32_t);
    152 
    153 #define	ngle_bt458_write(sc, r, v) \
    154 	hyperfb_write4(sc, NGLE_REG_RAMDAC + ((r) << 2), (v) << 24)
    155 
    156 struct wsdisplay_accessops hyperfb_accessops = {
    157 	hyperfb_ioctl,
    158 	hyperfb_mmap,
    159 	NULL,	/* alloc_screen */
    160 	NULL,	/* free_screen */
    161 	NULL,	/* show_screen */
    162 	NULL, 	/* load_font */
    163 	NULL,	/* pollc */
    164 	NULL	/* scroll */
    165 };
    166 
    167 static inline uint32_t
    168 hyperfb_read4(struct hyperfb_softc *sc, uint32_t offset)
    169 {
    170 	return bus_space_read_4(sc->sc_iot, sc->sc_hreg, offset);
    171 }
    172 
    173 static inline uint8_t
    174 hyperfb_read1(struct hyperfb_softc *sc, uint32_t offset)
    175 {
    176 	return bus_space_read_1(sc->sc_iot, sc->sc_hreg, offset);
    177 }
    178 
    179 static inline void
    180 hyperfb_write4(struct hyperfb_softc *sc, uint32_t offset, uint32_t val)
    181 {
    182 	bus_space_write_4(sc->sc_iot, sc->sc_hreg, offset, val);
    183 }
    184 
    185 static inline void
    186 hyperfb_write1(struct hyperfb_softc *sc, uint32_t offset, uint8_t val)
    187 {
    188 	bus_space_write_1(sc->sc_iot, sc->sc_hreg, offset, val);
    189 }
    190 
    191 static inline void
    192 hyperfb_wait(struct hyperfb_softc *sc)
    193 {
    194 	uint8_t stat;
    195 
    196 	do {
    197 		stat = hyperfb_read1(sc, NGLE_REG_15b0);
    198 		if (stat == 0)
    199 			stat = hyperfb_read1(sc, NGLE_REG_15b0);
    200 	} while (stat != 0);
    201 }
    202 
    203 static inline void
    204 hyperfb_wait_fifo(struct hyperfb_softc *sc, uint32_t slots)
    205 {
    206 	uint32_t reg;
    207 
    208 	do {
    209 		reg = hyperfb_read4(sc, NGLE_REG_34);
    210 	} while (reg < slots);
    211 }
    212 
    213 static inline void
    214 hyperfb_setup_fb(struct hyperfb_softc *sc)
    215 {
    216 
    217 	/*
    218 	 * turns out the plane mask is applied to everything, including
    219 	 * direct framebuffer writes, so make sure we always set it
    220 	 */
    221 	hyperfb_wait(sc);
    222 	if ((sc->sc_mode != WSDISPLAYIO_MODE_EMUL) && sc->sc_24bit) {
    223 		hyperfb_write4(sc, NGLE_REG_10, 0xBBA0A000);	/* 24bit */
    224 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    225 	} else {
    226 		hyperfb_write4(sc, NGLE_REG_10, 0x13602000);	/* 8bit */
    227 		hyperfb_write4(sc, NGLE_REG_13, 0xff);
    228 	}
    229 	hyperfb_write4(sc, NGLE_REG_14, 0x83000300);
    230 	hyperfb_wait(sc);
    231 	hyperfb_write1(sc, NGLE_REG_16b1, 1);
    232 	sc->sc_hwmode = HW_FB;
    233 }
    234 
    235 static inline void
    236 hyperfb_setup_fb24(struct hyperfb_softc *sc)
    237 {
    238 
    239 	hyperfb_wait(sc);
    240 	hyperfb_write4(sc, NGLE_REG_10, 0xBBA0A000);	/* 24bit */
    241 	hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    242 	hyperfb_write4(sc, NGLE_REG_14, 0x83000300);
    243 	//IBOvals(RopSrc,0,BitmapExtent08,0,DataDynamic,MaskDynamic,0,0)
    244 	hyperfb_wait(sc);
    245 	hyperfb_write1(sc, NGLE_REG_16b1, 1);
    246 	sc->sc_hwmode = HW_FB;
    247 }
    248 
    249 int
    250 hyperfb_match(device_t parent, cfdata_t cf, void *aux)
    251 {
    252 	struct confargs *ca = aux;
    253 	bus_space_handle_t romh;
    254 	paddr_t rom;
    255 	uint32_t id = 0;
    256 	u_char devtype;
    257 	int rv = 0, romunmapped = 0;
    258 
    259 	if (ca->ca_type.iodc_type != HPPA_TYPE_FIO)
    260 		return 0;
    261 
    262 	/* these need further checking for the graphics id */
    263 	if (ca->ca_type.iodc_sv_model != HPPA_FIO_GSGC &&
    264 	    ca->ca_type.iodc_sv_model != HPPA_FIO_SGC)
    265 		return 0;
    266 
    267 	if (ca->ca_naddrs > 0)
    268 		rom = ca->ca_addrs[0].addr;
    269 	else
    270 		rom = ca->ca_hpa;
    271 
    272 	DPRINTF("%s: hpa=%x, rom=%x\n", __func__, (uint)ca->ca_hpa,
    273 	    (uint)rom);
    274 
    275 	/* if it does not map, probably part of the lasi space */
    276 	if (bus_space_map(ca->ca_iot, rom, STI_ROMSIZE, 0, &romh)) {
    277 		DPRINTF("%s: can't map rom space (%d)\n", __func__, rv);
    278 
    279 		if ((rom & HPPA_IOBEGIN) == HPPA_IOBEGIN) {
    280 			romh = rom;
    281 			romunmapped++;
    282 		} else {
    283 			/* in this case nobody has no freaking idea */
    284 			return 0;
    285 		}
    286 	}
    287 
    288 	devtype = bus_space_read_1(ca->ca_iot, romh, 3);
    289 	DPRINTF("%s: devtype=%d\n", __func__, devtype);
    290 	rv = 1;
    291 	switch (devtype) {
    292 	case STI_DEVTYPE4:
    293 		id = bus_space_read_4(ca->ca_iot, romh, STI_DEV4_DD_GRID);
    294 		break;
    295 	case STI_DEVTYPE1:
    296 		id = (bus_space_read_1(ca->ca_iot, romh, STI_DEV1_DD_GRID
    297 		    +  3) << 24) |
    298 		    (bus_space_read_1(ca->ca_iot, romh, STI_DEV1_DD_GRID
    299 		    +  7) << 16) |
    300 		    (bus_space_read_1(ca->ca_iot, romh, STI_DEV1_DD_GRID
    301 		    + 11) <<  8) |
    302 		    (bus_space_read_1(ca->ca_iot, romh, STI_DEV1_DD_GRID
    303 		    + 15));
    304 		break;
    305 	default:
    306 		DPRINTF("%s: unknown type (%x)\n", __func__, devtype);
    307 		rv = 0;
    308 	}
    309 
    310 	if (id == STI_DD_HCRX)
    311 		rv = 100;	/* beat out sti */
    312 
    313 	ca->ca_addrs[ca->ca_naddrs].addr = rom;
    314 	ca->ca_addrs[ca->ca_naddrs].size = sti_rom_size(ca->ca_iot, romh);
    315 	ca->ca_naddrs++;
    316 
    317 	if (!romunmapped)
    318 		bus_space_unmap(ca->ca_iot, romh, STI_ROMSIZE);
    319 	return rv;
    320 }
    321 
    322 void
    323 hyperfb_attach(device_t parent, device_t self, void *aux)
    324 {
    325 	struct hyperfb_softc *sc = device_private(self);
    326 	struct confargs *ca = aux;
    327 	struct rasops_info *ri;
    328 	struct wsemuldisplaydev_attach_args aa;
    329 	bus_space_handle_t hrom;
    330 	hppa_hpa_t consaddr;
    331 	long defattr;
    332 	int pagezero_cookie;
    333 	paddr_t rom;
    334 	uint32_t config;
    335 
    336 	pagezero_cookie = hppa_pagezero_map();
    337 	consaddr = (hppa_hpa_t)PAGE0->mem_cons.pz_hpa;
    338 	hppa_pagezero_unmap(pagezero_cookie);
    339 
    340 	sc->sc_dev = self;
    341 	sc->sc_base = ca->ca_hpa;
    342 	sc->sc_iot = ca->ca_iot;
    343 	sc->sc_is_console =(ca->ca_hpa == consaddr);
    344 	sc->sc_width = 1280;
    345 	sc->sc_height = 1024;
    346 
    347 	/* we can *not* be interrupted when doing colour map accesses */
    348 	mutex_init(&sc->sc_hwlock, MUTEX_DEFAULT, IPL_HIGH);
    349 
    350 	/* we stashed rom addr/len into the last slot during probe */
    351 	rom = ca->ca_addrs[ca->ca_naddrs - 1].addr;
    352 
    353 	if (bus_space_map(sc->sc_iot,
    354 	    sc->sc_base + HCRX_FBOFFSET, HCRX_FBLEN,
    355 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
    356 	    &sc->sc_hfb)) {
    357 	    	aprint_error_dev(sc->sc_dev,
    358 		    "failed to map the framebuffer\n");
    359 	    	return;
    360 	}
    361 	sc->sc_fb = bus_space_vaddr(sc->sc_iot, sc->sc_hfb);
    362 
    363 	if (bus_space_map(sc->sc_iot,
    364 	    sc->sc_base + HCRX_REGOFFSET, HCRX_REGLEN, 0, &sc->sc_hreg)) {
    365 	    	aprint_error_dev(sc->sc_dev, "failed to map registers\n");
    366 	    	return;
    367 	}
    368 
    369 	/*
    370 	 * we really only need the first word so we can grab the config bits
    371 	 * between the bytes
    372 	 */
    373 	if (bus_space_map(sc->sc_iot, rom, 4, 0, &hrom)) {
    374 	    	aprint_error_dev(sc->sc_dev,
    375 		    "failed to map ROM, assuming 8bit\n");
    376 	    	config = 0;
    377 	} else {
    378 		/* alright, we got the ROM. now do the idle dance. */
    379 		volatile uint32_t r = hyperfb_read4(sc, NGLE_REG_15);
    380 		__USE(r);
    381 		hyperfb_wait(sc);
    382 		config = bus_space_read_4(sc->sc_iot, hrom, 0);
    383 		bus_space_unmap(sc->sc_iot, hrom, 4);
    384 	}
    385 	sc->sc_24bit = ((config & HCRX_CONFIG_24BIT) != 0);
    386 
    387 	printf(" %s\n", sc->sc_24bit ? "HCRX24" : "HCRX");
    388 #ifdef HP7300LC_CPU
    389 	/*
    390 	 * PCXL2: enable accel I/O for this space, see PCX-L2 ERS "ACCEL_IO".
    391 	 * "pcxl2_ers.{ps,pdf}", (section / chapter . rel. page / abs. page)
    392 	 * 8.7.4 / 8-12 / 92, 11.3.14 / 11-14 / 122 and 14.8 / 14-5 / 203.
    393 	 */
    394 	if (hppa_cpu_info->hci_cputype == hpcxl2
    395 	    && ca->ca_hpa >= PCXL2_ACCEL_IO_START
    396 	    && ca->ca_hpa <= PCXL2_ACCEL_IO_END)
    397 		eaio_l2(PCXL2_ACCEL_IO_ADDR2MASK(ca->ca_hpa));
    398 #endif /* HP7300LC_CPU */
    399 
    400 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    401 	sc->sc_locked = 0;
    402 
    403 	hyperfb_setup(sc);
    404 	hyperfb_setup_fb(sc);
    405 
    406 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    407 		"default",
    408 		0, 0,
    409 		NULL,
    410 		8, 16,
    411 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    412 		      WSSCREEN_RESIZE,
    413 		NULL
    414 	};
    415 
    416 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    417 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    418 
    419 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    420 	    &hyperfb_accessops);
    421 	sc->vd.init_screen = hyperfb_init_screen;
    422 	sc->vd.show_screen_cookie = &sc->sc_gc;
    423 	sc->vd.show_screen_cb = glyphcache_adapt;
    424 
    425 	ri = &sc->sc_console_screen.scr_ri;
    426 
    427 	//sc->sc_gc.gc_bitblt = hyperfb_bitblt;
    428 	//sc->sc_gc.gc_blitcookie = sc;
    429 	//sc->sc_gc.gc_rop = RopSrc;
    430 
    431 	if (sc->sc_is_console) {
    432 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    433 		    &defattr);
    434 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    435 
    436 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    437 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    438 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    439 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    440 
    441 #if 0
    442 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    443 				sc->sc_scr.fbheight - sc->sc_height - 5,
    444 				sc->sc_scr.fbwidth,
    445 				ri->ri_font->fontwidth,
    446 				ri->ri_font->fontheight,
    447 				defattr);
    448 #endif
    449 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    450 		    defattr);
    451 
    452 		hyperfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    453 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    454 
    455 		vcons_replay_msgbuf(&sc->sc_console_screen);
    456 	} else {
    457 		/*
    458 		 * since we're not the console we can postpone the rest
    459 		 * until someone actually allocates a screen for us
    460 		 */
    461 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    462 			/* do some minimal setup to avoid weirdnesses later */
    463 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    464 			    &defattr);
    465 		} else
    466 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    467 
    468 #if 0
    469 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    470 				sc->sc_scr.fbheight - sc->sc_height - 5,
    471 				sc->sc_scr.fbwidth,
    472 				ri->ri_font->fontwidth,
    473 				ri->ri_font->fontheight,
    474 				defattr);
    475 #endif
    476 	}
    477 
    478 	hyperfb_restore_palette(sc);
    479 
    480 	/* no suspend/resume support yet */
    481 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
    482 		aprint_error_dev(sc->sc_dev,
    483 		    "couldn't establish power handler\n");
    484 
    485 	aa.console = sc->sc_is_console;
    486 	aa.scrdata = &sc->sc_screenlist;
    487 	aa.accessops = &hyperfb_accessops;
    488 	aa.accesscookie = &sc->vd;
    489 
    490 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
    491 
    492 	hyperfb_setup_fb(sc);
    493 }
    494 
    495 static void
    496 hyperfb_init_screen(void *cookie, struct vcons_screen *scr,
    497     int existing, long *defattr)
    498 {
    499 	struct hyperfb_softc *sc = cookie;
    500 	struct rasops_info *ri = &scr->scr_ri;
    501 
    502 	ri->ri_depth = 8;
    503 	ri->ri_width = 1280;
    504 	ri->ri_height = 1024;
    505 	ri->ri_stride = 2048;
    506 	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB /*|
    507 		     RI_ENABLE_ALPHA | RI_PREFER_ALPHA*/;
    508 
    509 	ri->ri_bits = (void *)sc->sc_fb;
    510 	rasops_init(ri, 0, 0);
    511 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    512 		      WSSCREEN_RESIZE;
    513 	scr->scr_flags |= VCONS_LOADFONT;
    514 
    515 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    516 		    sc->sc_width / ri->ri_font->fontwidth);
    517 
    518 	ri->ri_hw = scr;
    519 
    520 	sc->sc_putchar = ri->ri_ops.putchar;
    521 	ri->ri_ops.copyrows = hyperfb_copyrows;
    522 	ri->ri_ops.copycols = hyperfb_copycols;
    523 	ri->ri_ops.eraserows = hyperfb_eraserows;
    524 	ri->ri_ops.erasecols = hyperfb_erasecols;
    525 	ri->ri_ops.cursor = hyperfb_cursor;
    526 	ri->ri_ops.putchar = hyperfb_putchar;
    527 }
    528 
    529 static int
    530 hyperfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    531     struct lwp *l)
    532 {
    533 	struct vcons_data *vd = v;
    534 	struct hyperfb_softc *sc = vd->cookie;
    535 	struct wsdisplay_fbinfo *wdf;
    536 	struct vcons_screen *ms = vd->active;
    537 
    538 	switch (cmd) {
    539 	case WSDISPLAYIO_GTYPE:
    540 		*(u_int *)data = WSDISPLAY_TYPE_STI;
    541 		return 0;
    542 
    543 	case WSDISPLAYIO_GINFO:
    544 		if (ms == NULL)
    545 			return ENODEV;
    546 		wdf = (void *)data;
    547 		wdf->height = ms->scr_ri.ri_height;
    548 		wdf->width = sc->sc_24bit ? ms->scr_ri.ri_width << 2
    549 		    : ms->scr_ri.ri_width;
    550 		wdf->depth = ms->scr_ri.ri_depth;
    551 		wdf->cmsize = 256;
    552 		return 0;
    553 
    554 	case WSDISPLAYIO_GETCMAP:
    555 		return hyperfb_getcmap(sc,
    556 		    (struct wsdisplay_cmap *)data);
    557 
    558 	case WSDISPLAYIO_PUTCMAP:
    559 		return hyperfb_putcmap(sc,
    560 		    (struct wsdisplay_cmap *)data);
    561 	case WSDISPLAYIO_LINEBYTES:
    562 		*(u_int *)data = sc->sc_24bit ? 8192 : 2048;
    563 		return 0;
    564 
    565 	case WSDISPLAYIO_SMODE: {
    566 		int new_mode = *(int*)data;
    567 		if (new_mode != sc->sc_mode) {
    568 			sc->sc_mode = new_mode;
    569 			if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    570 				hyperfb_setup(sc);
    571 				hyperfb_restore_palette(sc);
    572 #if 0
    573 				glyphcache_wipe(&sc->sc_gc);
    574 #endif
    575 				hyperfb_rectfill(sc, 0, 0, sc->sc_width,
    576 				    sc->sc_height, ms->scr_ri.ri_devcmap[
    577 				    (ms->scr_defattr >> 16) & 0xff]);
    578 				vcons_redraw_screen(ms);
    579 				hyperfb_set_video(sc, 1);
    580 			} else {
    581 				hyperfb_setup(sc);
    582 				hyperfb_rectfill(sc, 0, 0, sc->sc_width,
    583 				    sc->sc_height, 0xff);
    584 				hyperfb_setup_fb24(sc);
    585 			}
    586 		}
    587 		}
    588 		return 0;
    589 
    590 	case WSDISPLAYIO_GET_FBINFO: {
    591 		struct wsdisplayio_fbinfo *fbi = data;
    592 		int ret;
    593 
    594 		ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    595 		fbi->fbi_fbsize = sc->sc_height * 2048;
    596 		if (sc->sc_24bit) {
    597 			fbi->fbi_stride = 8192;
    598 			fbi->fbi_bitsperpixel = 32;
    599 			fbi->fbi_pixeltype = WSFB_RGB;
    600 			fbi->fbi_subtype.fbi_rgbmasks.red_offset = 16;
    601 			fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
    602 			fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
    603 			fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
    604 			fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
    605 			fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
    606 			fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
    607 				fbi->fbi_fbsize = sc->sc_height * 8192;
    608 		}
    609 		return ret;
    610 	}
    611 
    612 	case WSDISPLAYIO_GCURPOS: {
    613 		struct wsdisplay_curpos *cp = (void *)data;
    614 
    615 		cp->x = sc->sc_cursor_x;
    616 		cp->y = sc->sc_cursor_y;
    617 	}
    618 		return 0;
    619 
    620 	case WSDISPLAYIO_SCURPOS: {
    621 		struct wsdisplay_curpos *cp = (void *)data;
    622 
    623 		hyperfb_move_cursor(sc, cp->x, cp->y);
    624 	}
    625 		return 0;
    626 
    627 	case WSDISPLAYIO_GCURMAX: {
    628 		struct wsdisplay_curpos *cp = (void *)data;
    629 
    630 		cp->x = 64;
    631 		cp->y = 64;
    632 	}
    633 		return 0;
    634 
    635 	case WSDISPLAYIO_SCURSOR: {
    636 		struct wsdisplay_cursor *cursor = (void *)data;
    637 
    638 		return hyperfb_do_cursor(sc, cursor);
    639 	}
    640 
    641 	case WSDISPLAYIO_SVIDEO:
    642 		hyperfb_set_video(sc, *(int *)data);
    643 		return 0;
    644 	case WSDISPLAYIO_GVIDEO:
    645 		return sc->sc_video_on ?
    646 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    647 	}
    648 	return EPASSTHROUGH;
    649 }
    650 
    651 static paddr_t
    652 hyperfb_mmap(void *v, void *vs, off_t offset, int prot)
    653 {
    654 	struct vcons_data *vd = v;
    655 	struct hyperfb_softc *sc = vd->cookie;
    656 	paddr_t pa = -1;
    657 
    658 
    659 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
    660 		return -1;
    661 
    662 	/* GSC framebuffer space is 16MB */
    663 	if (offset >= 0 && offset < 0x1000000) {
    664 		/* framebuffer */
    665 		pa = bus_space_mmap(sc->sc_iot, sc->sc_base + HCRX_FBOFFSET,
    666 		    offset, prot,
    667 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    668 	} else if (offset >= 0x80000000 && offset < 0x8040000) {
    669 		/* blitter registers etc. */
    670 		pa = bus_space_mmap(sc->sc_iot, sc->sc_base + HCRX_REGOFFSET,
    671 		    offset - 0x80000000, prot, BUS_SPACE_MAP_LINEAR);
    672 	}
    673 
    674 	return pa;
    675 }
    676 
    677 static int
    678 hyperfb_putcmap(struct hyperfb_softc *sc, struct wsdisplay_cmap *cm)
    679 {
    680 	u_char *r, *g, *b;
    681 	u_int index = cm->index;
    682 	u_int count = cm->count;
    683 	int i, error;
    684 	u_char rbuf[256], gbuf[256], bbuf[256];
    685 
    686 	if (cm->index >= 256 || cm->count > 256 ||
    687 	    (cm->index + cm->count) > 256)
    688 		return EINVAL;
    689 	error = copyin(cm->red, &rbuf[index], count);
    690 	if (error)
    691 		return error;
    692 	error = copyin(cm->green, &gbuf[index], count);
    693 	if (error)
    694 		return error;
    695 	error = copyin(cm->blue, &bbuf[index], count);
    696 	if (error)
    697 		return error;
    698 
    699 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    700 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    701 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    702 
    703 	r = &sc->sc_cmap_red[index];
    704 	g = &sc->sc_cmap_green[index];
    705 	b = &sc->sc_cmap_blue[index];
    706 
    707 	for (i = 0; i < count; i++) {
    708 		hyperfb_putpalreg(sc, index, *r, *g, *b);
    709 		index++;
    710 		r++, g++, b++;
    711 	}
    712 	return 0;
    713 }
    714 
    715 static int
    716 hyperfb_getcmap(struct hyperfb_softc *sc, struct wsdisplay_cmap *cm)
    717 {
    718 	u_int index = cm->index;
    719 	u_int count = cm->count;
    720 	int error;
    721 
    722 	if (index >= 255 || count > 256 || index + count > 256)
    723 		return EINVAL;
    724 
    725 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    726 	if (error)
    727 		return error;
    728 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    729 	if (error)
    730 		return error;
    731 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    732 	if (error)
    733 		return error;
    734 
    735 	return 0;
    736 }
    737 
    738 static void
    739 hyperfb_restore_palette(struct hyperfb_softc *sc)
    740 {
    741 	uint8_t cmap[768];
    742 	int i, j;
    743 
    744 	j = 0;
    745 	rasops_get_cmap(&sc->sc_console_screen.scr_ri, cmap, sizeof(cmap));
    746 	for (i = 0; i < 256; i++) {
    747 		sc->sc_cmap_red[i] = cmap[j];
    748 		sc->sc_cmap_green[i] = cmap[j + 1];
    749 		sc->sc_cmap_blue[i] = cmap[j + 2];
    750 		hyperfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    751 		j += 3;
    752 	}
    753 }
    754 
    755 static int
    756 hyperfb_putpalreg(struct hyperfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    757     uint8_t b)
    758 {
    759 
    760 	mutex_enter(&sc->sc_hwlock);
    761 	hyperfb_wait(sc);
    762 	hyperfb_write4(sc, NGLE_REG_10, 0xbbe0f000);
    763 	hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
    764 	hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    765 
    766 	hyperfb_wait(sc);
    767 	hyperfb_write4(sc, NGLE_REG_3, 0x400 | (idx << 2));
    768 	hyperfb_write4(sc, NGLE_REG_4, (r << 16) | (g << 8) | b);
    769 
    770 	hyperfb_write4(sc, NGLE_REG_2, 0x400);
    771 	hyperfb_write4(sc, NGLE_REG_38, 0x82000100);
    772 	hyperfb_setup_fb(sc);
    773 	mutex_exit(&sc->sc_hwlock);
    774 	return 0;
    775 }
    776 
    777 void
    778 hyperfb_setup(struct hyperfb_softc *sc)
    779 {
    780 	int i;
    781 	uint32_t reg;
    782 
    783 	sc->sc_hwmode = HW_FB;
    784 	sc->sc_hot_x = 0;
    785 	sc->sc_hot_y = 0;
    786 	sc->sc_enabled = 0;
    787 	sc->sc_video_on = 1;
    788 
    789 	/* set Bt458 read mask register to all planes */
    790 	/* XXX I'm not sure HCRX even has one of these */
    791 	hyperfb_wait(sc);
    792 	ngle_bt458_write(sc, 0x08, 0x04);
    793 	ngle_bt458_write(sc, 0x0a, 0xff);
    794 
    795 	reg = hyperfb_read4(sc, NGLE_REG_32);
    796 	DPRINTF("planereg %08x\n", reg);
    797 	hyperfb_write4(sc, NGLE_REG_32, 0xffffffff);
    798 
    799 	/* hyperbowl */
    800 	hyperfb_wait(sc);
    801 	if (sc->sc_24bit) {
    802 		/* write must happen twice because hw bug */
    803 		hyperfb_write4(sc, NGLE_REG_40,
    804 		    HYPERBOWL_MODE01_8_24_LUT0_TRANSPARENT_LUT1_OPAQUE);
    805 		hyperfb_write4(sc, NGLE_REG_40,
    806 		    HYPERBOWL_MODE01_8_24_LUT0_TRANSPARENT_LUT1_OPAQUE);
    807 		hyperfb_write4(sc, NGLE_REG_39, HYPERBOWL_MODE2_8_24);
    808 		/* Set lut 0 to be the direct color */
    809 		hyperfb_write4(sc, NGLE_REG_42, 0x014c0148);
    810 		hyperfb_write4(sc, NGLE_REG_43, 0x404c4048);
    811 		hyperfb_write4(sc, NGLE_REG_44, 0x034c0348);
    812 		hyperfb_write4(sc, NGLE_REG_45, 0x444c4448);
    813 	} else {
    814 		hyperfb_write4(sc, NGLE_REG_40,
    815 		    HYPERBOWL_MODE_FOR_8_OVER_88_LUT0_NO_TRANSPARENCIES);
    816 		hyperfb_write4(sc, NGLE_REG_40,
    817 		    HYPERBOWL_MODE_FOR_8_OVER_88_LUT0_NO_TRANSPARENCIES);
    818 
    819 		hyperfb_write4(sc, NGLE_REG_42, 0);
    820 		hyperfb_write4(sc, NGLE_REG_43, 0);
    821 		hyperfb_write4(sc, NGLE_REG_44, 0);
    822 		hyperfb_write4(sc, NGLE_REG_45, 0x444c4048);
    823 	}
    824 
    825 	/* attr. planes */
    826 	hyperfb_wait(sc);
    827 	hyperfb_write4(sc, NGLE_REG_11,
    828 	    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINattr, 0));
    829 	hyperfb_write4(sc, NGLE_REG_14,
    830 	    IBOvals(RopSrc, 0, BitmapExtent08, 1, DataDynamic, MaskOtc, 1, 0));
    831 	hyperfb_write4(sc, NGLE_REG_12, 0x04000F00/*NGLE_BUFF0_CMAP0*/);
    832 	hyperfb_write4(sc, NGLE_REG_8, 0xffffffff);
    833 
    834 	hyperfb_wait(sc);
    835 	hyperfb_write4(sc, NGLE_REG_6, 0x00000000);
    836 	hyperfb_write4(sc, NGLE_REG_9,
    837 	    (sc->sc_width << 16) | sc->sc_height);
    838 	/*
    839 	 * blit into offscreen memory to force flush previous - apparently
    840 	 * some chips have a bug this works around
    841 	 */
    842 	hyperfb_wait(sc);
    843 	hyperfb_write4(sc, NGLE_REG_6, 0x05000000);
    844 	hyperfb_write4(sc, NGLE_REG_9, 0x00040001);
    845 
    846 	/*
    847 	 * on 24bit-capable hardware we:
    848 	 * - make overlay colour 255 transparent
    849 	 * - blit the 24bit buffer all white
    850 	 * - install a linear ramp in CMAP 0
    851 	 */
    852 	if (sc->sc_24bit) {
    853 		/* overlay transparency */
    854 		hyperfb_wait_fifo(sc, 7);
    855 		hyperfb_write4(sc, NGLE_REG_11, 0x13a02000);
    856 		hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
    857 		hyperfb_write4(sc, NGLE_REG_3, 0x000017f0);
    858 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    859 		hyperfb_write4(sc, NGLE_REG_22, 0xffffffff);
    860 		hyperfb_write4(sc, NGLE_REG_23, 0x0);
    861 
    862 		hyperfb_wait(sc);
    863 		hyperfb_write4(sc, NGLE_REG_12, 0x00000000);
    864 
    865 		/* clear 24bit buffer */
    866 		hyperfb_wait(sc);
    867 		/* plane mask */
    868 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    869 		hyperfb_write4(sc, NGLE_REG_8, 0xffffffff); /* transfer data */
    870 		/* bitmap op */
    871 		hyperfb_write4(sc, NGLE_REG_14,
    872 		    IBOvals(RopSrc, 0, BitmapExtent32, 0, DataDynamic, MaskOtc,
    873 			0, 0));
    874 		/* dst bitmap access */
    875 		hyperfb_write4(sc, NGLE_REG_11,
    876 		    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0F8,
    877 			0));
    878 		hyperfb_wait_fifo(sc, 3);
    879 		hyperfb_write4(sc, NGLE_REG_35, 0x00ffffff);	/* fg colour */
    880 		hyperfb_write4(sc, NGLE_REG_6, 0x00000000);	/* dst xy */
    881 		hyperfb_write4(sc, NGLE_REG_9,
    882 		    (sc->sc_width << 16) | sc->sc_height);
    883 
    884 		/* write a linear ramp into CMAP0 */
    885 		hyperfb_wait(sc);
    886 		hyperfb_write4(sc, NGLE_REG_10, 0xbbe0f000);
    887 		hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
    888 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    889 
    890 		hyperfb_wait(sc);
    891 		hyperfb_write4(sc, NGLE_REG_3, 0);
    892 		for (i = 0; i < 256; i++) {
    893 			hyperfb_wait(sc);
    894 			hyperfb_write4(sc, NGLE_REG_4,
    895 			    (i << 16) | (i << 8) | i);
    896 		}
    897 		hyperfb_write4(sc, NGLE_REG_2, 0x0);
    898 		hyperfb_write4(sc, NGLE_REG_38,
    899 		    LBC_ENABLE | LBC_TYPE_CMAP | 0x100);
    900 		hyperfb_wait(sc);
    901 	}
    902 
    903 	hyperfb_setup_fb(sc);
    904 
    905 	/* make sure video output is enabled */
    906 	hyperfb_wait(sc);
    907 	hyperfb_write4(sc, NGLE_REG_33,
    908 	    hyperfb_read4(sc, NGLE_REG_33) | 0x0a000000);
    909 
    910 	/* cursor mask */
    911 	hyperfb_wait(sc);
    912 	hyperfb_write4(sc, NGLE_REG_30, 0);
    913 	for (i = 0; i < 64; i++) {
    914 		hyperfb_write4(sc, NGLE_REG_31, 0xffffffff);
    915 		hyperfb_write4(sc, NGLE_REG_31, 0xffffffff);
    916 	}
    917 
    918 	/* cursor image */
    919 	hyperfb_wait(sc);
    920 	hyperfb_write4(sc, NGLE_REG_30, 0x80);
    921 	for (i = 0; i < 64; i++) {
    922 		hyperfb_write4(sc, NGLE_REG_31, 0xff00ff00);
    923 		hyperfb_write4(sc, NGLE_REG_31, 0xff00ff00);
    924 	}
    925 
    926 	/* colour map */
    927 	hyperfb_wait(sc);
    928 	hyperfb_write4(sc, NGLE_REG_10, 0xBBE0F000);
    929 	hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
    930 	hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    931 	hyperfb_wait(sc);
    932 	hyperfb_write4(sc, NGLE_REG_3, 0);
    933 	hyperfb_write4(sc, NGLE_REG_4, 0x000000ff);	/* BG */
    934 	hyperfb_write4(sc, NGLE_REG_4, 0x00ff0000);	/* FG */
    935 	hyperfb_wait(sc);
    936 	hyperfb_write4(sc, NGLE_REG_2, 0);
    937 	hyperfb_write4(sc, NGLE_REG_38, LBC_ENABLE | LBC_TYPE_CURSOR | 4);
    938 	hyperfb_setup_fb(sc);
    939 
    940 	hyperfb_move_cursor(sc, 100, 100);
    941 }
    942 
    943 static void
    944 hyperfb_set_video(struct hyperfb_softc *sc, int on)
    945 {
    946 	uint32_t reg;
    947 
    948 	if (sc->sc_video_on == on)
    949 		return;
    950 
    951 	sc->sc_video_on = on;
    952 
    953 	hyperfb_wait(sc);
    954 	reg = hyperfb_read4(sc, NGLE_REG_33);
    955 
    956 	if (on) {
    957 		hyperfb_write4(sc, NGLE_REG_33, reg | HCRX_VIDEO_ENABLE);
    958 	} else {
    959 		hyperfb_write4(sc, NGLE_REG_33, reg & ~HCRX_VIDEO_ENABLE);
    960 	}
    961 }
    962 
    963 static void
    964 hyperfb_rectfill(struct hyperfb_softc *sc, int x, int y, int wi, int he,
    965     uint32_t bg)
    966 {
    967 	/*
    968 	 * XXX
    969 	 * HCRX has the same problem as VisEG drawing rectangles less than 32
    970 	 * pixels wide, but here we don't seem to have any usable offscreen
    971 	 * memory, at least not as long as we're using the overlay planes.
    972 	 * As a workaround, fall back to memset()-based fills for rectangles
    973 	 * less than 32 pixels wide
    974 	 */
    975 	if (wi < 32) {
    976 #if 0
    977 		int i;
    978 		uint8_t *ptr = (uint8_t *)sc->sc_fb + (y << 11) + x;
    979 
    980 		if (sc->sc_hwmode != HW_FB)
    981 			hyperfb_setup_fb(sc);
    982 
    983 		for (i = 0; i < he; i++) {
    984 			memset(ptr, bg, wi);
    985 			ptr += 2048;
    986 		}
    987 #else
    988 		/*
    989 		 * instead of memset() we abuse the blitter - set / clear the
    990 		 * planes we want, select colour by planemask, do two passes
    991 		 * where necessary ( as in, anything not black or white )
    992 		 */
    993 		if (sc->sc_hwmode != HW_SFILL) {
    994 			hyperfb_wait(sc);
    995 			hyperfb_write4(sc, NGLE_REG_10, 0x13a02000);
    996 			sc->sc_hwmode = HW_SFILL;
    997 		}
    998 		bg &= 0xff;
    999 		hyperfb_wait_fifo(sc, 2);
   1000 		hyperfb_write4(sc, NGLE_REG_24, (x << 16) | y);
   1001 		if (bg != 0) {
   1002 			hyperfb_wait_fifo(sc, 4);
   1003 			hyperfb_write4(sc, NGLE_REG_14,
   1004 			    IBOvals(RopSet, 0, BitmapExtent08, 1, DataDynamic,
   1005 				MaskOtc, 0, 0));
   1006 			hyperfb_write4(sc, NGLE_REG_13, bg);
   1007 			hyperfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
   1008 			hyperfb_write4(sc, NGLE_REG_25, (x << 16) | y);
   1009 		}
   1010 		if (bg != 0xff) {
   1011 			hyperfb_wait_fifo(sc, 4);
   1012 			hyperfb_write4(sc, NGLE_REG_14,
   1013 			    IBOvals(RopClr, 0, BitmapExtent08, 1, DataDynamic,
   1014 				MaskOtc, 0, 0));
   1015 			hyperfb_write4(sc, NGLE_REG_13, bg ^ 0xff);
   1016 			hyperfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
   1017 			hyperfb_write4(sc, NGLE_REG_25, (x << 16) | y);
   1018 		}
   1019 #endif
   1020 		return;
   1021 	}
   1022 	if (sc->sc_hwmode != HW_FILL) {
   1023 		hyperfb_wait_fifo(sc, 4);
   1024 		/* transfer data */
   1025 		hyperfb_write4(sc, NGLE_REG_8, 0xffffffff);
   1026 		/* plane mask */
   1027 		hyperfb_write4(sc, NGLE_REG_13, 0xff);
   1028 		/* bitmap op */
   1029 		hyperfb_write4(sc, NGLE_REG_14,
   1030 		    IBOvals(RopSrc, 0, BitmapExtent08, 0, DataDynamic, MaskOtc,
   1031 			0, 0));
   1032 		/* dst bitmap access */
   1033 		hyperfb_write4(sc, NGLE_REG_11,
   1034 		    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINovly,
   1035 			0));
   1036 		sc->sc_hwmode = HW_FILL;
   1037 	}
   1038 	hyperfb_wait_fifo(sc, 3);
   1039 	hyperfb_write4(sc, NGLE_REG_35, bg);
   1040 	/* dst XY */
   1041 	hyperfb_write4(sc, NGLE_REG_6, (x << 16) | y);
   1042 	/* len XY start */
   1043 	hyperfb_write4(sc, NGLE_REG_9, (wi << 16) | he);
   1044 }
   1045 
   1046 static void
   1047 hyperfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
   1048     int he, int rop)
   1049 {
   1050 	struct hyperfb_softc *sc = cookie;
   1051 
   1052 	if (sc->sc_hwmode != HW_BLIT) {
   1053 		hyperfb_wait(sc);
   1054 		hyperfb_write4(sc, NGLE_REG_10, 0x13a02000);
   1055 		hyperfb_write4(sc, NGLE_REG_13, 0xff);
   1056 		sc->sc_hwmode = HW_BLIT;
   1057 	}
   1058 	hyperfb_wait_fifo(sc, 4);
   1059 	hyperfb_write4(sc, NGLE_REG_14, ((rop << 8) & 0xf00) | 0x23000000);
   1060 	/* IBOvals(rop, 0, BitmapExtent08, 1, DataDynamic, MaskOtc, 0, 0) */
   1061 	hyperfb_write4(sc, NGLE_REG_24, (xs << 16) | ys);
   1062 	hyperfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
   1063 	hyperfb_write4(sc, NGLE_REG_25, (xd << 16) | yd);
   1064 }
   1065 
   1066 static void
   1067 hyperfb_nuke_cursor(struct rasops_info *ri)
   1068 {
   1069 	struct vcons_screen *scr = ri->ri_hw;
   1070 	struct hyperfb_softc *sc = scr->scr_cookie;
   1071 	int wi, he, x, y;
   1072 
   1073 	if (ri->ri_flg & RI_CURSOR) {
   1074 		wi = ri->ri_font->fontwidth;
   1075 		he = ri->ri_font->fontheight;
   1076 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1077 		y = ri->ri_crow * he + ri->ri_yorigin;
   1078 		hyperfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1079 		ri->ri_flg &= ~RI_CURSOR;
   1080 	}
   1081 }
   1082 
   1083 static void
   1084 hyperfb_cursor(void *cookie, int on, int row, int col)
   1085 {
   1086 	struct rasops_info *ri = cookie;
   1087 	struct vcons_screen *scr = ri->ri_hw;
   1088 	struct hyperfb_softc *sc = scr->scr_cookie;
   1089 	int x, y, wi, he;
   1090 
   1091 	wi = ri->ri_font->fontwidth;
   1092 	he = ri->ri_font->fontheight;
   1093 
   1094 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1095 		if (on) {
   1096 			if (ri->ri_flg & RI_CURSOR) {
   1097 				hyperfb_nuke_cursor(ri);
   1098 			}
   1099 			x = col * wi + ri->ri_xorigin;
   1100 			y = row * he + ri->ri_yorigin;
   1101 			hyperfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1102 			ri->ri_flg |= RI_CURSOR;
   1103 		}
   1104 		ri->ri_crow = row;
   1105 		ri->ri_ccol = col;
   1106 	} else {
   1107 		ri->ri_crow = row;
   1108 		ri->ri_ccol = col;
   1109 		ri->ri_flg &= ~RI_CURSOR;
   1110 	}
   1111 }
   1112 
   1113 static void
   1114 hyperfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1115 {
   1116 	struct rasops_info *ri = cookie;
   1117 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1118 	struct vcons_screen *scr = ri->ri_hw;
   1119 	struct hyperfb_softc *sc = scr->scr_cookie;
   1120 	int x, y, wi, he/*, rv = GC_NOPE*/;
   1121 	uint32_t bg;
   1122 
   1123 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1124 		return;
   1125 
   1126 	if (!CHAR_IN_FONT(c, font))
   1127 		return;
   1128 
   1129 	if (row == ri->ri_crow && col == ri->ri_ccol) {
   1130 		ri->ri_flg &= ~RI_CURSOR;
   1131 	}
   1132 
   1133 	wi = font->fontwidth;
   1134 	he = font->fontheight;
   1135 
   1136 	x = ri->ri_xorigin + col * wi;
   1137 	y = ri->ri_yorigin + row * he;
   1138 
   1139 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1140 
   1141 	if (c == 0x20) {
   1142 		hyperfb_rectfill(sc, x, y, wi, he, bg);
   1143 		return;
   1144 	}
   1145 
   1146 #if 0
   1147 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1148 	if (rv == GC_OK)
   1149 		return;
   1150 #endif
   1151 	if (sc->sc_hwmode != HW_FB) hyperfb_setup_fb(sc);
   1152 	sc->sc_putchar(cookie, row, col, c, attr);
   1153 #if 0
   1154 	if (rv == GC_ADD)
   1155 		glyphcache_add(&sc->sc_gc, c, x, y);
   1156 #endif
   1157 }
   1158 
   1159 static void
   1160 hyperfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1161 {
   1162 	struct rasops_info *ri = cookie;
   1163 	struct vcons_screen *scr = ri->ri_hw;
   1164 	struct hyperfb_softc *sc = scr->scr_cookie;
   1165 	int32_t xs, xd, y, width, height;
   1166 
   1167 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1168 		if (ri->ri_crow == row &&
   1169 		   (ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols)) &&
   1170 		   (ri->ri_flg & RI_CURSOR)) {
   1171 			hyperfb_nuke_cursor(ri);
   1172 		}
   1173 
   1174 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1175 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1176 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1177 		width = ri->ri_font->fontwidth * ncols;
   1178 		height = ri->ri_font->fontheight;
   1179 		hyperfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
   1180 		if (ri->ri_crow == row &&
   1181 		   (ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols)))
   1182 			ri->ri_flg &= ~RI_CURSOR;
   1183 	}
   1184 }
   1185 
   1186 static void
   1187 hyperfb_erasecols(void *cookie, int row, int startcol, int ncols,
   1188     long fillattr)
   1189 {
   1190 	struct rasops_info *ri = cookie;
   1191 	struct vcons_screen *scr = ri->ri_hw;
   1192 	struct hyperfb_softc *sc = scr->scr_cookie;
   1193 	int32_t x, y, width, height, fg, bg, ul;
   1194 
   1195 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1196 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1197 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1198 		width = ri->ri_font->fontwidth * ncols;
   1199 		height = ri->ri_font->fontheight;
   1200 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1201 
   1202 		hyperfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1203 		if (ri->ri_crow == row &&
   1204 		    (ri->ri_ccol >= startcol &&
   1205 			ri->ri_ccol < (startcol + ncols)))
   1206 			ri->ri_flg &= ~RI_CURSOR;
   1207 	}
   1208 }
   1209 
   1210 static void
   1211 hyperfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1212 {
   1213 	struct rasops_info *ri = cookie;
   1214 	struct vcons_screen *scr = ri->ri_hw;
   1215 	struct hyperfb_softc *sc = scr->scr_cookie;
   1216 	int32_t x, ys, yd, width, height;
   1217 
   1218 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1219 		if ((ri->ri_crow >= srcrow &&
   1220 			ri->ri_crow < (srcrow + nrows)) &&
   1221 		    (ri->ri_flg & RI_CURSOR)) {
   1222 			hyperfb_nuke_cursor(ri);
   1223 		}
   1224 		x = ri->ri_xorigin;
   1225 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1226 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1227 		width = ri->ri_emuwidth;
   1228 		height = ri->ri_font->fontheight * nrows;
   1229 		hyperfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
   1230 		if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
   1231 			ri->ri_flg &= ~RI_CURSOR;
   1232 	}
   1233 }
   1234 
   1235 static void
   1236 hyperfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1237 {
   1238 	struct rasops_info *ri = cookie;
   1239 	struct vcons_screen *scr = ri->ri_hw;
   1240 	struct hyperfb_softc *sc = scr->scr_cookie;
   1241 	int32_t x, y, width, height, fg, bg, ul;
   1242 
   1243 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1244 		x = ri->ri_xorigin;
   1245 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1246 		width = ri->ri_emuwidth;
   1247 		height = ri->ri_font->fontheight * nrows;
   1248 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1249 
   1250 		hyperfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1251 
   1252 		if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
   1253 			ri->ri_flg &= ~RI_CURSOR;
   1254 	}
   1255 }
   1256 
   1257 static void
   1258 hyperfb_move_cursor(struct hyperfb_softc *sc, int x, int y)
   1259 {
   1260 	uint32_t pos;
   1261 
   1262 	sc->sc_cursor_x = x;
   1263 	x -= sc->sc_hot_x;
   1264 	sc->sc_cursor_y = y;
   1265 	y -= sc->sc_hot_y;
   1266 
   1267 	if (x < 0) x = 0x1000 - x;
   1268 	if (y < 0) y = 0x1000 - y;
   1269 	pos = (x << 16) | y;
   1270 	if (sc->sc_enabled) pos |= HCRX_ENABLE_CURSOR;
   1271 	hyperfb_wait(sc);
   1272 	hyperfb_write4(sc, NGLE_REG_29, pos);
   1273 }
   1274 
   1275 static int
   1276 hyperfb_do_cursor(struct hyperfb_softc *sc, struct wsdisplay_cursor *cur)
   1277 {
   1278 
   1279 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1280 
   1281 		sc->sc_enabled = cur->enable;
   1282 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1283 	}
   1284 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1285 
   1286 		sc->sc_hot_x = cur->hot.x;
   1287 		sc->sc_hot_y = cur->hot.y;
   1288 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1289 	}
   1290 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1291 
   1292 		hyperfb_move_cursor(sc, cur->pos.x, cur->pos.y);
   1293 	}
   1294 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1295 		uint32_t rgb;
   1296 		uint8_t r[2], g[2], b[2];
   1297 
   1298 		copyin(cur->cmap.blue, b, 2);
   1299 		copyin(cur->cmap.green, g, 2);
   1300 		copyin(cur->cmap.red, r, 2);
   1301 		mutex_enter(&sc->sc_hwlock);
   1302 		hyperfb_wait(sc);
   1303 		hyperfb_write4(sc, NGLE_REG_10, 0xBBE0F000);
   1304 		hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
   1305 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
   1306 		hyperfb_wait(sc);
   1307 		hyperfb_write4(sc, NGLE_REG_3, 0);
   1308 		rgb = (r[0] << 16) | (g[0] << 8) | b[0];
   1309 		hyperfb_write4(sc, NGLE_REG_4, rgb);	/* BG */
   1310 		rgb = (r[1] << 16) | (g[1] << 8) | b[1];
   1311 		hyperfb_write4(sc, NGLE_REG_4, rgb);	/* FG */
   1312 		hyperfb_write4(sc, NGLE_REG_2, 0);
   1313 		hyperfb_write4(sc, NGLE_REG_38,
   1314 		    LBC_ENABLE | LBC_TYPE_CURSOR | 4);
   1315 
   1316 		hyperfb_setup_fb(sc);
   1317 		mutex_exit(&sc->sc_hwlock);
   1318 
   1319 	}
   1320 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1321 		uint32_t buffer[128], latch, tmp;
   1322 		int i;
   1323 
   1324 		copyin(cur->mask, buffer, 512);
   1325 		hyperfb_wait(sc);
   1326 		hyperfb_write4(sc, NGLE_REG_30, 0);
   1327 		for (i = 0; i < 128; i += 2) {
   1328 			latch = 0;
   1329 			tmp = buffer[i] & 0x80808080;
   1330 			latch |= tmp >> 7;
   1331 			tmp = buffer[i] & 0x40404040;
   1332 			latch |= tmp >> 5;
   1333 			tmp = buffer[i] & 0x20202020;
   1334 			latch |= tmp >> 3;
   1335 			tmp = buffer[i] & 0x10101010;
   1336 			latch |= tmp >> 1;
   1337 			tmp = buffer[i] & 0x08080808;
   1338 			latch |= tmp << 1;
   1339 			tmp = buffer[i] & 0x04040404;
   1340 			latch |= tmp << 3;
   1341 			tmp = buffer[i] & 0x02020202;
   1342 			latch |= tmp << 5;
   1343 			tmp = buffer[i] & 0x01010101;
   1344 			latch |= tmp << 7;
   1345 			hyperfb_write4(sc, NGLE_REG_31, latch);
   1346 			latch = 0;
   1347 			tmp = buffer[i + 1] & 0x80808080;
   1348 			latch |= tmp >> 7;
   1349 			tmp = buffer[i + 1] & 0x40404040;
   1350 			latch |= tmp >> 5;
   1351 			tmp = buffer[i + 1] & 0x20202020;
   1352 			latch |= tmp >> 3;
   1353 			tmp = buffer[i + 1] & 0x10101010;
   1354 			latch |= tmp >> 1;
   1355 			tmp = buffer[i + 1] & 0x08080808;
   1356 			latch |= tmp << 1;
   1357 			tmp = buffer[i + 1] & 0x04040404;
   1358 			latch |= tmp << 3;
   1359 			tmp = buffer[i + 1] & 0x02020202;
   1360 			latch |= tmp << 5;
   1361 			tmp = buffer[i + 1] & 0x01010101;
   1362 			latch |= tmp << 7;
   1363 			hyperfb_write4(sc, NGLE_REG_31, latch);
   1364 		}
   1365 
   1366 		copyin(cur->image, buffer, 512);
   1367 		hyperfb_wait(sc);
   1368 		hyperfb_write4(sc, NGLE_REG_30, 0x80);
   1369 		for (i = 0; i < 128; i += 2) {
   1370 			latch = 0;
   1371 			tmp = buffer[i] & 0x80808080;
   1372 			latch |= tmp >> 7;
   1373 			tmp = buffer[i] & 0x40404040;
   1374 			latch |= tmp >> 5;
   1375 			tmp = buffer[i] & 0x20202020;
   1376 			latch |= tmp >> 3;
   1377 			tmp = buffer[i] & 0x10101010;
   1378 			latch |= tmp >> 1;
   1379 			tmp = buffer[i] & 0x08080808;
   1380 			latch |= tmp << 1;
   1381 			tmp = buffer[i] & 0x04040404;
   1382 			latch |= tmp << 3;
   1383 			tmp = buffer[i] & 0x02020202;
   1384 			latch |= tmp << 5;
   1385 			tmp = buffer[i] & 0x01010101;
   1386 			latch |= tmp << 7;
   1387 			hyperfb_write4(sc, NGLE_REG_31, latch);
   1388 			latch = 0;
   1389 			tmp = buffer[i + 1] & 0x80808080;
   1390 			latch |= tmp >> 7;
   1391 			tmp = buffer[i + 1] & 0x40404040;
   1392 			latch |= tmp >> 5;
   1393 			tmp = buffer[i + 1] & 0x20202020;
   1394 			latch |= tmp >> 3;
   1395 			tmp = buffer[i + 1] & 0x10101010;
   1396 			latch |= tmp >> 1;
   1397 			tmp = buffer[i + 1] & 0x08080808;
   1398 			latch |= tmp << 1;
   1399 			tmp = buffer[i + 1] & 0x04040404;
   1400 			latch |= tmp << 3;
   1401 			tmp = buffer[i + 1] & 0x02020202;
   1402 			latch |= tmp << 5;
   1403 			tmp = buffer[i + 1] & 0x01010101;
   1404 			latch |= tmp << 7;
   1405 			hyperfb_write4(sc, NGLE_REG_31, latch);
   1406 		}
   1407 		hyperfb_setup_fb(sc);
   1408 	}
   1409 
   1410 	return 0;
   1411 }
   1412