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