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