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