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