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