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