Home | History | Annotate | Line # | Download | only in dev
hyperfb.c revision 1.13
      1 /*	$NetBSD: hyperfb.c,v 1.13 2024/08/28 06:20:30 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.13 2024/08/28 06:20:30 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 GCID:
    547 		*(u_int *)data = STI_DD_HCRX;
    548 		return 0;
    549 
    550 	case WSDISPLAYIO_GINFO:
    551 		if (ms == NULL)
    552 			return ENODEV;
    553 		wdf = (void *)data;
    554 		wdf->height = ms->scr_ri.ri_height;
    555 		wdf->width = sc->sc_24bit ? ms->scr_ri.ri_width << 2
    556 		    : ms->scr_ri.ri_width;
    557 		wdf->depth = ms->scr_ri.ri_depth;
    558 		wdf->cmsize = 256;
    559 		return 0;
    560 
    561 	case WSDISPLAYIO_GETCMAP:
    562 		return hyperfb_getcmap(sc,
    563 		    (struct wsdisplay_cmap *)data);
    564 
    565 	case WSDISPLAYIO_PUTCMAP:
    566 		return hyperfb_putcmap(sc,
    567 		    (struct wsdisplay_cmap *)data);
    568 	case WSDISPLAYIO_LINEBYTES:
    569 		*(u_int *)data = sc->sc_24bit ? 8192 : 2048;
    570 		return 0;
    571 
    572 	case WSDISPLAYIO_SMODE: {
    573 		int new_mode = *(int*)data;
    574 		if (new_mode != sc->sc_mode) {
    575 			sc->sc_mode = new_mode;
    576 			if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    577 				hyperfb_setup(sc);
    578 				hyperfb_restore_palette(sc);
    579 #if 0
    580 				glyphcache_wipe(&sc->sc_gc);
    581 #endif
    582 				hyperfb_rectfill(sc, 0, 0, sc->sc_width,
    583 				    sc->sc_height, ms->scr_ri.ri_devcmap[
    584 				    (ms->scr_defattr >> 16) & 0xff]);
    585 				vcons_redraw_screen(ms);
    586 				hyperfb_set_video(sc, 1);
    587 			} else {
    588 				hyperfb_setup(sc);
    589 				hyperfb_rectfill(sc, 0, 0, sc->sc_width,
    590 				    sc->sc_height, 0xff);
    591 				hyperfb_setup_fb24(sc);
    592 			}
    593 		}
    594 		}
    595 		return 0;
    596 
    597 	case WSDISPLAYIO_GET_FBINFO: {
    598 		struct wsdisplayio_fbinfo *fbi = data;
    599 		int ret;
    600 
    601 		ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    602 		fbi->fbi_fbsize = sc->sc_height * 2048;
    603 		if (sc->sc_24bit) {
    604 			fbi->fbi_stride = 8192;
    605 			fbi->fbi_bitsperpixel = 32;
    606 			fbi->fbi_pixeltype = WSFB_RGB;
    607 			fbi->fbi_subtype.fbi_rgbmasks.red_offset = 16;
    608 			fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
    609 			fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
    610 			fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
    611 			fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
    612 			fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
    613 			fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
    614 				fbi->fbi_fbsize = sc->sc_height * 8192;
    615 		}
    616 		return ret;
    617 	}
    618 
    619 	case WSDISPLAYIO_GCURPOS: {
    620 		struct wsdisplay_curpos *cp = (void *)data;
    621 
    622 		cp->x = sc->sc_cursor_x;
    623 		cp->y = sc->sc_cursor_y;
    624 	}
    625 		return 0;
    626 
    627 	case WSDISPLAYIO_SCURPOS: {
    628 		struct wsdisplay_curpos *cp = (void *)data;
    629 
    630 		hyperfb_move_cursor(sc, cp->x, cp->y);
    631 	}
    632 		return 0;
    633 
    634 	case WSDISPLAYIO_GCURMAX: {
    635 		struct wsdisplay_curpos *cp = (void *)data;
    636 
    637 		cp->x = 64;
    638 		cp->y = 64;
    639 	}
    640 		return 0;
    641 
    642 	case WSDISPLAYIO_SCURSOR: {
    643 		struct wsdisplay_cursor *cursor = (void *)data;
    644 
    645 		return hyperfb_do_cursor(sc, cursor);
    646 	}
    647 
    648 	case WSDISPLAYIO_SVIDEO:
    649 		hyperfb_set_video(sc, *(int *)data);
    650 		return 0;
    651 	case WSDISPLAYIO_GVIDEO:
    652 		*(u_int *)data = sc->sc_video_on ?
    653 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    654 		return 0;
    655 	}
    656 	return EPASSTHROUGH;
    657 }
    658 
    659 static paddr_t
    660 hyperfb_mmap(void *v, void *vs, off_t offset, int prot)
    661 {
    662 	struct vcons_data *vd = v;
    663 	struct hyperfb_softc *sc = vd->cookie;
    664 	paddr_t pa = -1;
    665 
    666 
    667 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
    668 		return -1;
    669 
    670 	/* GSC framebuffer space is 16MB */
    671 	if (offset >= 0 && offset < 0x1000000) {
    672 		/* framebuffer */
    673 		pa = bus_space_mmap(sc->sc_iot, sc->sc_base + HCRX_FBOFFSET,
    674 		    offset, prot,
    675 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    676 	} else if (offset >= 0x80000000 && offset < 0x8040000) {
    677 		/* blitter registers etc. */
    678 		pa = bus_space_mmap(sc->sc_iot, sc->sc_base + HCRX_REGOFFSET,
    679 		    offset - 0x80000000, prot, BUS_SPACE_MAP_LINEAR);
    680 	}
    681 
    682 	return pa;
    683 }
    684 
    685 static int
    686 hyperfb_putcmap(struct hyperfb_softc *sc, struct wsdisplay_cmap *cm)
    687 {
    688 	u_char *r, *g, *b;
    689 	u_int index = cm->index;
    690 	u_int count = cm->count;
    691 	int i, error;
    692 	u_char rbuf[256], gbuf[256], bbuf[256];
    693 
    694 	if (cm->index >= 256 || cm->count > 256 ||
    695 	    (cm->index + cm->count) > 256)
    696 		return EINVAL;
    697 	error = copyin(cm->red, &rbuf[index], count);
    698 	if (error)
    699 		return error;
    700 	error = copyin(cm->green, &gbuf[index], count);
    701 	if (error)
    702 		return error;
    703 	error = copyin(cm->blue, &bbuf[index], count);
    704 	if (error)
    705 		return error;
    706 
    707 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    708 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    709 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    710 
    711 	r = &sc->sc_cmap_red[index];
    712 	g = &sc->sc_cmap_green[index];
    713 	b = &sc->sc_cmap_blue[index];
    714 
    715 	for (i = 0; i < count; i++) {
    716 		hyperfb_putpalreg(sc, index, *r, *g, *b);
    717 		index++;
    718 		r++, g++, b++;
    719 	}
    720 	return 0;
    721 }
    722 
    723 static int
    724 hyperfb_getcmap(struct hyperfb_softc *sc, struct wsdisplay_cmap *cm)
    725 {
    726 	u_int index = cm->index;
    727 	u_int count = cm->count;
    728 	int error;
    729 
    730 	if (index >= 255 || count > 256 || index + count > 256)
    731 		return EINVAL;
    732 
    733 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    734 	if (error)
    735 		return error;
    736 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    737 	if (error)
    738 		return error;
    739 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    740 	if (error)
    741 		return error;
    742 
    743 	return 0;
    744 }
    745 
    746 static void
    747 hyperfb_restore_palette(struct hyperfb_softc *sc)
    748 {
    749 	uint8_t cmap[768];
    750 	int i, j;
    751 
    752 	j = 0;
    753 	rasops_get_cmap(&sc->sc_console_screen.scr_ri, cmap, sizeof(cmap));
    754 	for (i = 0; i < 256; i++) {
    755 		sc->sc_cmap_red[i] = cmap[j];
    756 		sc->sc_cmap_green[i] = cmap[j + 1];
    757 		sc->sc_cmap_blue[i] = cmap[j + 2];
    758 		hyperfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    759 		j += 3;
    760 	}
    761 }
    762 
    763 static int
    764 hyperfb_putpalreg(struct hyperfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    765     uint8_t b)
    766 {
    767 
    768 	mutex_enter(&sc->sc_hwlock);
    769 	hyperfb_wait(sc);
    770 	hyperfb_write4(sc, NGLE_REG_10, 0xbbe0f000);
    771 	hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
    772 	hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    773 
    774 	hyperfb_wait(sc);
    775 	hyperfb_write4(sc, NGLE_REG_3, 0x400 | (idx << 2));
    776 	hyperfb_write4(sc, NGLE_REG_4, (r << 16) | (g << 8) | b);
    777 
    778 	hyperfb_write4(sc, NGLE_REG_2, 0x400);
    779 	hyperfb_write4(sc, NGLE_REG_38, 0x82000100);
    780 	hyperfb_setup_fb(sc);
    781 	mutex_exit(&sc->sc_hwlock);
    782 	return 0;
    783 }
    784 
    785 void
    786 hyperfb_setup(struct hyperfb_softc *sc)
    787 {
    788 	int i;
    789 	uint32_t reg;
    790 
    791 	sc->sc_hwmode = HW_FB;
    792 	sc->sc_hot_x = 0;
    793 	sc->sc_hot_y = 0;
    794 	sc->sc_enabled = 0;
    795 	sc->sc_video_on = 1;
    796 
    797 	/* set Bt458 read mask register to all planes */
    798 	/* XXX I'm not sure HCRX even has one of these */
    799 	hyperfb_wait(sc);
    800 	ngle_bt458_write(sc, 0x08, 0x04);
    801 	ngle_bt458_write(sc, 0x0a, 0xff);
    802 
    803 	reg = hyperfb_read4(sc, NGLE_REG_32);
    804 	DPRINTF("planereg %08x\n", reg);
    805 	hyperfb_write4(sc, NGLE_REG_32, 0xffffffff);
    806 
    807 	/* hyperbowl */
    808 	hyperfb_wait(sc);
    809 	if (sc->sc_24bit) {
    810 		/* write must happen twice because hw bug */
    811 		hyperfb_write4(sc, NGLE_REG_40,
    812 		    HYPERBOWL_MODE01_8_24_LUT0_TRANSPARENT_LUT1_OPAQUE);
    813 		hyperfb_write4(sc, NGLE_REG_40,
    814 		    HYPERBOWL_MODE01_8_24_LUT0_TRANSPARENT_LUT1_OPAQUE);
    815 		hyperfb_write4(sc, NGLE_REG_39, HYPERBOWL_MODE2_8_24);
    816 		/* Set lut 0 to be the direct color */
    817 		hyperfb_write4(sc, NGLE_REG_42, 0x014c0148);
    818 		hyperfb_write4(sc, NGLE_REG_43, 0x404c4048);
    819 		hyperfb_write4(sc, NGLE_REG_44, 0x034c0348);
    820 		hyperfb_write4(sc, NGLE_REG_45, 0x444c4448);
    821 	} else {
    822 		hyperfb_write4(sc, NGLE_REG_40,
    823 		    HYPERBOWL_MODE_FOR_8_OVER_88_LUT0_NO_TRANSPARENCIES);
    824 		hyperfb_write4(sc, NGLE_REG_40,
    825 		    HYPERBOWL_MODE_FOR_8_OVER_88_LUT0_NO_TRANSPARENCIES);
    826 
    827 		hyperfb_write4(sc, NGLE_REG_42, 0);
    828 		hyperfb_write4(sc, NGLE_REG_43, 0);
    829 		hyperfb_write4(sc, NGLE_REG_44, 0);
    830 		hyperfb_write4(sc, NGLE_REG_45, 0x444c4048);
    831 	}
    832 
    833 	/* attr. planes */
    834 	hyperfb_wait(sc);
    835 	hyperfb_write4(sc, NGLE_REG_11,
    836 	    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINattr, 0));
    837 	hyperfb_write4(sc, NGLE_REG_14,
    838 	    IBOvals(RopSrc, 0, BitmapExtent08, 1, DataDynamic, MaskOtc, 1, 0));
    839 	hyperfb_write4(sc, NGLE_REG_12, 0x04000F00/*NGLE_BUFF0_CMAP0*/);
    840 	hyperfb_write4(sc, NGLE_REG_8, 0xffffffff);
    841 
    842 	hyperfb_wait(sc);
    843 	hyperfb_write4(sc, NGLE_REG_6, 0x00000000);
    844 	hyperfb_write4(sc, NGLE_REG_9,
    845 	    (sc->sc_width << 16) | sc->sc_height);
    846 	/*
    847 	 * blit into offscreen memory to force flush previous - apparently
    848 	 * some chips have a bug this works around
    849 	 */
    850 	hyperfb_wait(sc);
    851 	hyperfb_write4(sc, NGLE_REG_6, 0x05000000);
    852 	hyperfb_write4(sc, NGLE_REG_9, 0x00040001);
    853 
    854 	/*
    855 	 * on 24bit-capable hardware we:
    856 	 * - make overlay colour 255 transparent
    857 	 * - blit the 24bit buffer all white
    858 	 * - install a linear ramp in CMAP 0
    859 	 */
    860 	if (sc->sc_24bit) {
    861 		/* overlay transparency */
    862 		hyperfb_wait_fifo(sc, 7);
    863 		hyperfb_write4(sc, NGLE_REG_11,
    864 		    BA(IndexedDcd, Otc04, Ots08, AddrLong, 0, BINovly, 0));
    865 		hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
    866 		hyperfb_write4(sc, NGLE_REG_3, 0x000017f0);
    867 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    868 		hyperfb_write4(sc, NGLE_REG_22, 0xffffffff);
    869 		hyperfb_write4(sc, NGLE_REG_23, 0x0);
    870 
    871 		hyperfb_wait(sc);
    872 		hyperfb_write4(sc, NGLE_REG_12, 0x00000000);
    873 
    874 		/* clear 24bit buffer */
    875 		hyperfb_wait(sc);
    876 		/* plane mask */
    877 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    878 		hyperfb_write4(sc, NGLE_REG_8, 0xffffffff); /* transfer data */
    879 		/* bitmap op */
    880 		hyperfb_write4(sc, NGLE_REG_14,
    881 		    IBOvals(RopSrc, 0, BitmapExtent32, 0, DataDynamic, MaskOtc,
    882 			0, 0));
    883 		/* dst bitmap access */
    884 		hyperfb_write4(sc, NGLE_REG_11,
    885 		    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0F8,
    886 			0));
    887 		hyperfb_wait_fifo(sc, 3);
    888 		hyperfb_write4(sc, NGLE_REG_35, 0x00ffffff);	/* fg colour */
    889 		hyperfb_write4(sc, NGLE_REG_6, 0x00000000);	/* dst xy */
    890 		hyperfb_write4(sc, NGLE_REG_9,
    891 		    (sc->sc_width << 16) | sc->sc_height);
    892 
    893 		/* write a linear ramp into CMAP0 */
    894 		hyperfb_wait(sc);
    895 		hyperfb_write4(sc, NGLE_REG_10, 0xbbe0f000);
    896 		hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
    897 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    898 
    899 		hyperfb_wait(sc);
    900 		hyperfb_write4(sc, NGLE_REG_3, 0);
    901 		for (i = 0; i < 256; i++) {
    902 			hyperfb_wait(sc);
    903 			hyperfb_write4(sc, NGLE_REG_4,
    904 			    (i << 16) | (i << 8) | i);
    905 		}
    906 		hyperfb_write4(sc, NGLE_REG_2, 0x0);
    907 		hyperfb_write4(sc, NGLE_REG_38,
    908 		    LBC_ENABLE | LBC_TYPE_CMAP | 0x100);
    909 		hyperfb_wait(sc);
    910 	}
    911 
    912 	hyperfb_setup_fb(sc);
    913 
    914 	/* make sure video output is enabled */
    915 	hyperfb_wait(sc);
    916 	hyperfb_write4(sc, NGLE_REG_33,
    917 	    hyperfb_read4(sc, NGLE_REG_33) | 0x0a000000);
    918 
    919 	/* cursor mask */
    920 	hyperfb_wait(sc);
    921 	hyperfb_write4(sc, NGLE_REG_30, 0);
    922 	for (i = 0; i < 64; i++) {
    923 		hyperfb_write4(sc, NGLE_REG_31, 0xffffffff);
    924 		hyperfb_write4(sc, NGLE_REG_31, 0xffffffff);
    925 	}
    926 
    927 	/* cursor image */
    928 	hyperfb_wait(sc);
    929 	hyperfb_write4(sc, NGLE_REG_30, 0x80);
    930 	for (i = 0; i < 64; i++) {
    931 		hyperfb_write4(sc, NGLE_REG_31, 0xff00ff00);
    932 		hyperfb_write4(sc, NGLE_REG_31, 0xff00ff00);
    933 	}
    934 
    935 	/* colour map */
    936 	hyperfb_wait(sc);
    937 	hyperfb_write4(sc, NGLE_REG_10, 0xBBE0F000);
    938 	hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
    939 	hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
    940 	hyperfb_wait(sc);
    941 	hyperfb_write4(sc, NGLE_REG_3, 0);
    942 	hyperfb_write4(sc, NGLE_REG_4, 0x000000ff);	/* BG */
    943 	hyperfb_write4(sc, NGLE_REG_4, 0x00ff0000);	/* FG */
    944 	hyperfb_wait(sc);
    945 	hyperfb_write4(sc, NGLE_REG_2, 0);
    946 	hyperfb_write4(sc, NGLE_REG_38, LBC_ENABLE | LBC_TYPE_CURSOR | 4);
    947 	hyperfb_setup_fb(sc);
    948 
    949 	hyperfb_move_cursor(sc, 100, 100);
    950 }
    951 
    952 static void
    953 hyperfb_set_video(struct hyperfb_softc *sc, int on)
    954 {
    955 	uint32_t reg;
    956 
    957 	if (sc->sc_video_on == on)
    958 		return;
    959 
    960 	sc->sc_video_on = on;
    961 
    962 	hyperfb_wait(sc);
    963 	reg = hyperfb_read4(sc, NGLE_REG_33);
    964 
    965 	if (on) {
    966 		hyperfb_write4(sc, NGLE_REG_33, reg | HCRX_VIDEO_ENABLE);
    967 	} else {
    968 		hyperfb_write4(sc, NGLE_REG_33, reg & ~HCRX_VIDEO_ENABLE);
    969 	}
    970 }
    971 
    972 static void
    973 hyperfb_rectfill(struct hyperfb_softc *sc, int x, int y, int wi, int he,
    974     uint32_t bg)
    975 {
    976 	/*
    977 	 * XXX
    978 	 * HCRX has the same problem as VisEG drawing rectangles less than 32
    979 	 * pixels wide, but here we don't seem to have any usable offscreen
    980 	 * memory, at least not as long as we're using the overlay planes.
    981 	 * As a workaround, fall back to memset()-based fills for rectangles
    982 	 * less than 32 pixels wide
    983 	 */
    984 	if (wi < 32) {
    985 #if 0
    986 		int i;
    987 		uint8_t *ptr = (uint8_t *)sc->sc_fb + (y << 11) + x;
    988 
    989 		if (sc->sc_hwmode != HW_FB)
    990 			hyperfb_setup_fb(sc);
    991 
    992 		for (i = 0; i < he; i++) {
    993 			memset(ptr, bg, wi);
    994 			ptr += 2048;
    995 		}
    996 #else
    997 		/*
    998 		 * instead of memset() we abuse the blitter - set / clear the
    999 		 * planes we want, select colour by planemask, do two passes
   1000 		 * where necessary ( as in, anything not black or white )
   1001 		 */
   1002 		if (sc->sc_hwmode != HW_SFILL) {
   1003 			hyperfb_wait(sc);
   1004 			hyperfb_write4(sc, NGLE_REG_10,
   1005 		 	 BA(IndexedDcd, Otc04, Ots08, AddrLong, 0, BINovly, 0));
   1006 			sc->sc_hwmode = HW_SFILL;
   1007 		}
   1008 		bg &= 0xff;
   1009 		hyperfb_wait_fifo(sc, 2);
   1010 		hyperfb_write4(sc, NGLE_REG_24, (x << 16) | y);
   1011 		if (bg != 0) {
   1012 			hyperfb_wait_fifo(sc, 4);
   1013 			hyperfb_write4(sc, NGLE_REG_14,
   1014 			    IBOvals(RopSet, 0, BitmapExtent08, 1, DataDynamic,
   1015 				MaskOtc, 0, 0));
   1016 			hyperfb_write4(sc, NGLE_REG_13, bg);
   1017 			hyperfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
   1018 			hyperfb_write4(sc, NGLE_REG_25, (x << 16) | y);
   1019 		}
   1020 		if (bg != 0xff) {
   1021 			hyperfb_wait_fifo(sc, 4);
   1022 			hyperfb_write4(sc, NGLE_REG_14,
   1023 			    IBOvals(RopClr, 0, BitmapExtent08, 1, DataDynamic,
   1024 				MaskOtc, 0, 0));
   1025 			hyperfb_write4(sc, NGLE_REG_13, bg ^ 0xff);
   1026 			hyperfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
   1027 			hyperfb_write4(sc, NGLE_REG_25, (x << 16) | y);
   1028 		}
   1029 #endif
   1030 		return;
   1031 	}
   1032 	if (sc->sc_hwmode != HW_FILL) {
   1033 		hyperfb_wait_fifo(sc, 4);
   1034 		/* transfer data */
   1035 		hyperfb_write4(sc, NGLE_REG_8, 0xffffffff);
   1036 		/* plane mask */
   1037 		hyperfb_write4(sc, NGLE_REG_13, 0xff);
   1038 		/* bitmap op */
   1039 		hyperfb_write4(sc, NGLE_REG_14,
   1040 		    IBOvals(RopSrc, 0, BitmapExtent08, 0, DataDynamic, MaskOtc,
   1041 			0, 0));
   1042 		/* dst bitmap access */
   1043 		hyperfb_write4(sc, NGLE_REG_11,
   1044 		    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINovly,
   1045 			0));
   1046 		sc->sc_hwmode = HW_FILL;
   1047 	}
   1048 	hyperfb_wait_fifo(sc, 3);
   1049 	hyperfb_write4(sc, NGLE_REG_35, bg);
   1050 	/* dst XY */
   1051 	hyperfb_write4(sc, NGLE_REG_6, (x << 16) | y);
   1052 	/* len XY start */
   1053 	hyperfb_write4(sc, NGLE_REG_9, (wi << 16) | he);
   1054 }
   1055 
   1056 static void
   1057 hyperfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
   1058     int he, int rop)
   1059 {
   1060 	struct hyperfb_softc *sc = cookie;
   1061 
   1062 	if (sc->sc_hwmode != HW_BLIT) {
   1063 		hyperfb_wait(sc);
   1064 		hyperfb_write4(sc, NGLE_REG_10,
   1065 		    BA(IndexedDcd, Otc04, Ots08, AddrLong, 0, BINovly, 0));
   1066 		hyperfb_write4(sc, NGLE_REG_13, 0xff);
   1067 		sc->sc_hwmode = HW_BLIT;
   1068 	}
   1069 	hyperfb_wait_fifo(sc, 4);
   1070 	hyperfb_write4(sc, NGLE_REG_14, ((rop << 8) & 0xf00) | 0x23000000);
   1071 	/* IBOvals(rop, 0, BitmapExtent08, 1, DataDynamic, MaskOtc, 0, 0) */
   1072 	hyperfb_write4(sc, NGLE_REG_24, (xs << 16) | ys);
   1073 	hyperfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
   1074 	hyperfb_write4(sc, NGLE_REG_25, (xd << 16) | yd);
   1075 }
   1076 
   1077 static void
   1078 hyperfb_nuke_cursor(struct rasops_info *ri)
   1079 {
   1080 	struct vcons_screen *scr = ri->ri_hw;
   1081 	struct hyperfb_softc *sc = scr->scr_cookie;
   1082 	int wi, he, x, y;
   1083 
   1084 	if (ri->ri_flg & RI_CURSOR) {
   1085 		wi = ri->ri_font->fontwidth;
   1086 		he = ri->ri_font->fontheight;
   1087 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1088 		y = ri->ri_crow * he + ri->ri_yorigin;
   1089 		hyperfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1090 		ri->ri_flg &= ~RI_CURSOR;
   1091 	}
   1092 }
   1093 
   1094 static void
   1095 hyperfb_cursor(void *cookie, int on, int row, int col)
   1096 {
   1097 	struct rasops_info *ri = cookie;
   1098 	struct vcons_screen *scr = ri->ri_hw;
   1099 	struct hyperfb_softc *sc = scr->scr_cookie;
   1100 	int x, y, wi, he;
   1101 
   1102 	wi = ri->ri_font->fontwidth;
   1103 	he = ri->ri_font->fontheight;
   1104 
   1105 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1106 		if (on) {
   1107 			if (ri->ri_flg & RI_CURSOR) {
   1108 				hyperfb_nuke_cursor(ri);
   1109 			}
   1110 			x = col * wi + ri->ri_xorigin;
   1111 			y = row * he + ri->ri_yorigin;
   1112 			hyperfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
   1113 			ri->ri_flg |= RI_CURSOR;
   1114 		}
   1115 		ri->ri_crow = row;
   1116 		ri->ri_ccol = col;
   1117 	} else {
   1118 		ri->ri_crow = row;
   1119 		ri->ri_ccol = col;
   1120 		ri->ri_flg &= ~RI_CURSOR;
   1121 	}
   1122 }
   1123 
   1124 static void
   1125 hyperfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1126 {
   1127 	struct rasops_info *ri = cookie;
   1128 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1129 	struct vcons_screen *scr = ri->ri_hw;
   1130 	struct hyperfb_softc *sc = scr->scr_cookie;
   1131 	int x, y, wi, he/*, rv = GC_NOPE*/;
   1132 	uint32_t bg;
   1133 
   1134 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1135 		return;
   1136 
   1137 	if (!CHAR_IN_FONT(c, font))
   1138 		return;
   1139 
   1140 	if (row == ri->ri_crow && col == ri->ri_ccol) {
   1141 		ri->ri_flg &= ~RI_CURSOR;
   1142 	}
   1143 
   1144 	wi = font->fontwidth;
   1145 	he = font->fontheight;
   1146 
   1147 	x = ri->ri_xorigin + col * wi;
   1148 	y = ri->ri_yorigin + row * he;
   1149 
   1150 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1151 
   1152 	if (c == 0x20) {
   1153 		hyperfb_rectfill(sc, x, y, wi, he, bg);
   1154 		return;
   1155 	}
   1156 
   1157 #if 0
   1158 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1159 	if (rv == GC_OK)
   1160 		return;
   1161 #endif
   1162 	if (sc->sc_hwmode != HW_FB) hyperfb_setup_fb(sc);
   1163 	sc->sc_putchar(cookie, row, col, c, attr);
   1164 #if 0
   1165 	if (rv == GC_ADD)
   1166 		glyphcache_add(&sc->sc_gc, c, x, y);
   1167 #endif
   1168 }
   1169 
   1170 static void
   1171 hyperfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1172 {
   1173 	struct rasops_info *ri = cookie;
   1174 	struct vcons_screen *scr = ri->ri_hw;
   1175 	struct hyperfb_softc *sc = scr->scr_cookie;
   1176 	int32_t xs, xd, y, width, height;
   1177 
   1178 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1179 		if (ri->ri_crow == row &&
   1180 		   (ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols)) &&
   1181 		   (ri->ri_flg & RI_CURSOR)) {
   1182 			hyperfb_nuke_cursor(ri);
   1183 		}
   1184 
   1185 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1186 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1187 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1188 		width = ri->ri_font->fontwidth * ncols;
   1189 		height = ri->ri_font->fontheight;
   1190 		hyperfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
   1191 		if (ri->ri_crow == row &&
   1192 		   (ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols)))
   1193 			ri->ri_flg &= ~RI_CURSOR;
   1194 	}
   1195 }
   1196 
   1197 static void
   1198 hyperfb_erasecols(void *cookie, int row, int startcol, int ncols,
   1199     long fillattr)
   1200 {
   1201 	struct rasops_info *ri = cookie;
   1202 	struct vcons_screen *scr = ri->ri_hw;
   1203 	struct hyperfb_softc *sc = scr->scr_cookie;
   1204 	int32_t x, y, width, height, fg, bg, ul;
   1205 
   1206 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1207 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1208 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1209 		width = ri->ri_font->fontwidth * ncols;
   1210 		height = ri->ri_font->fontheight;
   1211 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1212 
   1213 		hyperfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1214 		if (ri->ri_crow == row &&
   1215 		    (ri->ri_ccol >= startcol &&
   1216 			ri->ri_ccol < (startcol + ncols)))
   1217 			ri->ri_flg &= ~RI_CURSOR;
   1218 	}
   1219 }
   1220 
   1221 static void
   1222 hyperfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1223 {
   1224 	struct rasops_info *ri = cookie;
   1225 	struct vcons_screen *scr = ri->ri_hw;
   1226 	struct hyperfb_softc *sc = scr->scr_cookie;
   1227 	int32_t x, ys, yd, width, height;
   1228 
   1229 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1230 		if ((ri->ri_crow >= srcrow &&
   1231 			ri->ri_crow < (srcrow + nrows)) &&
   1232 		    (ri->ri_flg & RI_CURSOR)) {
   1233 			hyperfb_nuke_cursor(ri);
   1234 		}
   1235 		x = ri->ri_xorigin;
   1236 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1237 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1238 		width = ri->ri_emuwidth;
   1239 		height = ri->ri_font->fontheight * nrows;
   1240 		hyperfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
   1241 		if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
   1242 			ri->ri_flg &= ~RI_CURSOR;
   1243 	}
   1244 }
   1245 
   1246 static void
   1247 hyperfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1248 {
   1249 	struct rasops_info *ri = cookie;
   1250 	struct vcons_screen *scr = ri->ri_hw;
   1251 	struct hyperfb_softc *sc = scr->scr_cookie;
   1252 	int32_t x, y, width, height, fg, bg, ul;
   1253 
   1254 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1255 		x = ri->ri_xorigin;
   1256 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1257 		width = ri->ri_emuwidth;
   1258 		height = ri->ri_font->fontheight * nrows;
   1259 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1260 
   1261 		hyperfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1262 
   1263 		if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
   1264 			ri->ri_flg &= ~RI_CURSOR;
   1265 	}
   1266 }
   1267 
   1268 static void
   1269 hyperfb_move_cursor(struct hyperfb_softc *sc, int x, int y)
   1270 {
   1271 	uint32_t pos;
   1272 
   1273 	sc->sc_cursor_x = x;
   1274 	x -= sc->sc_hot_x;
   1275 	sc->sc_cursor_y = y;
   1276 	y -= sc->sc_hot_y;
   1277 
   1278 	if (x < 0) x = 0x1000 - x;
   1279 	if (y < 0) y = 0x1000 - y;
   1280 	pos = (x << 16) | y;
   1281 	if (sc->sc_enabled) pos |= HCRX_ENABLE_CURSOR;
   1282 	hyperfb_wait_fifo(sc, 2);
   1283 	hyperfb_write4(sc, NGLE_REG_28, 0);
   1284 	hyperfb_write4(sc, NGLE_REG_29, pos);
   1285 }
   1286 
   1287 static int
   1288 hyperfb_do_cursor(struct hyperfb_softc *sc, struct wsdisplay_cursor *cur)
   1289 {
   1290 
   1291 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1292 
   1293 		sc->sc_enabled = cur->enable;
   1294 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1295 	}
   1296 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1297 
   1298 		sc->sc_hot_x = cur->hot.x;
   1299 		sc->sc_hot_y = cur->hot.y;
   1300 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1301 	}
   1302 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1303 
   1304 		hyperfb_move_cursor(sc, cur->pos.x, cur->pos.y);
   1305 	}
   1306 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1307 		uint32_t rgb;
   1308 		uint8_t r[2], g[2], b[2];
   1309 
   1310 		copyin(cur->cmap.blue, b, 2);
   1311 		copyin(cur->cmap.green, g, 2);
   1312 		copyin(cur->cmap.red, r, 2);
   1313 		mutex_enter(&sc->sc_hwlock);
   1314 		hyperfb_wait(sc);
   1315 		hyperfb_write4(sc, NGLE_REG_10, 0xBBE0F000);
   1316 		hyperfb_write4(sc, NGLE_REG_14, 0x03000300);
   1317 		hyperfb_write4(sc, NGLE_REG_13, 0xffffffff);
   1318 		hyperfb_wait(sc);
   1319 		hyperfb_write4(sc, NGLE_REG_3, 0);
   1320 		rgb = (r[0] << 16) | (g[0] << 8) | b[0];
   1321 		hyperfb_write4(sc, NGLE_REG_4, rgb);	/* BG */
   1322 		rgb = (r[1] << 16) | (g[1] << 8) | b[1];
   1323 		hyperfb_write4(sc, NGLE_REG_4, rgb);	/* FG */
   1324 		hyperfb_write4(sc, NGLE_REG_2, 0);
   1325 		hyperfb_write4(sc, NGLE_REG_38,
   1326 		    LBC_ENABLE | LBC_TYPE_CURSOR | 4);
   1327 
   1328 		hyperfb_setup_fb(sc);
   1329 		mutex_exit(&sc->sc_hwlock);
   1330 
   1331 	}
   1332 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1333 		uint32_t buffer[128], latch, tmp;
   1334 		int i;
   1335 
   1336 		copyin(cur->mask, buffer, 512);
   1337 		hyperfb_wait(sc);
   1338 		hyperfb_write4(sc, NGLE_REG_30, 0);
   1339 		for (i = 0; i < 128; i += 2) {
   1340 			latch = 0;
   1341 			tmp = buffer[i] & 0x80808080;
   1342 			latch |= tmp >> 7;
   1343 			tmp = buffer[i] & 0x40404040;
   1344 			latch |= tmp >> 5;
   1345 			tmp = buffer[i] & 0x20202020;
   1346 			latch |= tmp >> 3;
   1347 			tmp = buffer[i] & 0x10101010;
   1348 			latch |= tmp >> 1;
   1349 			tmp = buffer[i] & 0x08080808;
   1350 			latch |= tmp << 1;
   1351 			tmp = buffer[i] & 0x04040404;
   1352 			latch |= tmp << 3;
   1353 			tmp = buffer[i] & 0x02020202;
   1354 			latch |= tmp << 5;
   1355 			tmp = buffer[i] & 0x01010101;
   1356 			latch |= tmp << 7;
   1357 			hyperfb_write4(sc, NGLE_REG_31, latch);
   1358 			latch = 0;
   1359 			tmp = buffer[i + 1] & 0x80808080;
   1360 			latch |= tmp >> 7;
   1361 			tmp = buffer[i + 1] & 0x40404040;
   1362 			latch |= tmp >> 5;
   1363 			tmp = buffer[i + 1] & 0x20202020;
   1364 			latch |= tmp >> 3;
   1365 			tmp = buffer[i + 1] & 0x10101010;
   1366 			latch |= tmp >> 1;
   1367 			tmp = buffer[i + 1] & 0x08080808;
   1368 			latch |= tmp << 1;
   1369 			tmp = buffer[i + 1] & 0x04040404;
   1370 			latch |= tmp << 3;
   1371 			tmp = buffer[i + 1] & 0x02020202;
   1372 			latch |= tmp << 5;
   1373 			tmp = buffer[i + 1] & 0x01010101;
   1374 			latch |= tmp << 7;
   1375 			hyperfb_write4(sc, NGLE_REG_31, latch);
   1376 		}
   1377 
   1378 		copyin(cur->image, buffer, 512);
   1379 		hyperfb_wait(sc);
   1380 		hyperfb_write4(sc, NGLE_REG_30, 0x80);
   1381 		for (i = 0; i < 128; i += 2) {
   1382 			latch = 0;
   1383 			tmp = buffer[i] & 0x80808080;
   1384 			latch |= tmp >> 7;
   1385 			tmp = buffer[i] & 0x40404040;
   1386 			latch |= tmp >> 5;
   1387 			tmp = buffer[i] & 0x20202020;
   1388 			latch |= tmp >> 3;
   1389 			tmp = buffer[i] & 0x10101010;
   1390 			latch |= tmp >> 1;
   1391 			tmp = buffer[i] & 0x08080808;
   1392 			latch |= tmp << 1;
   1393 			tmp = buffer[i] & 0x04040404;
   1394 			latch |= tmp << 3;
   1395 			tmp = buffer[i] & 0x02020202;
   1396 			latch |= tmp << 5;
   1397 			tmp = buffer[i] & 0x01010101;
   1398 			latch |= tmp << 7;
   1399 			hyperfb_write4(sc, NGLE_REG_31, latch);
   1400 			latch = 0;
   1401 			tmp = buffer[i + 1] & 0x80808080;
   1402 			latch |= tmp >> 7;
   1403 			tmp = buffer[i + 1] & 0x40404040;
   1404 			latch |= tmp >> 5;
   1405 			tmp = buffer[i + 1] & 0x20202020;
   1406 			latch |= tmp >> 3;
   1407 			tmp = buffer[i + 1] & 0x10101010;
   1408 			latch |= tmp >> 1;
   1409 			tmp = buffer[i + 1] & 0x08080808;
   1410 			latch |= tmp << 1;
   1411 			tmp = buffer[i + 1] & 0x04040404;
   1412 			latch |= tmp << 3;
   1413 			tmp = buffer[i + 1] & 0x02020202;
   1414 			latch |= tmp << 5;
   1415 			tmp = buffer[i + 1] & 0x01010101;
   1416 			latch |= tmp << 7;
   1417 			hyperfb_write4(sc, NGLE_REG_31, latch);
   1418 		}
   1419 		hyperfb_setup_fb(sc);
   1420 	}
   1421 
   1422 	return 0;
   1423 }
   1424