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