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