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