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