xref: /netbsd-src/sys/arch/sparc/dev/cgfourteen.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /*	$NetBSD: cgfourteen.c,v 1.88 2019/08/11 06:04:16 macallan Exp $ */
2 
3 /*
4  * Copyright (c) 1996
5  *	The President and Fellows of Harvard College. All rights reserved.
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  *	This product includes software developed by Harvard University.
16  *	This product includes software developed by the University of
17  *	California, Lawrence Berkeley Laboratory.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. All advertising materials mentioning features or use of this software
28  *    must display the following acknowledgement:
29  *	This product includes software developed by the University of
30  *	California, Berkeley and its contributors.
31  *	This product includes software developed by Harvard University and
32  *	its contributors.
33  * 4. Neither the name of the University nor the names of its contributors
34  *    may be used to endorse or promote products derived from this software
35  *    without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  *   Based on:
50  *	NetBSD: cgthree.c,v 1.28 1996/05/31 09:59:22 pk Exp
51  *	NetBSD: cgsix.c,v 1.25 1996/04/01 17:30:00 christos Exp
52  */
53 
54 /*
55  * Driver for Campus-II on-board mbus-based video (cgfourteen).
56  *
57  * Does not handle interrupts, even though they can occur.
58  *
59  * XXX should defer colormap updates to vertical retrace interrupts
60  */
61 
62 #include "opt_wsemul.h"
63 #include "sx.h"
64 
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/buf.h>
68 #include <sys/device.h>
69 #include <sys/ioctl.h>
70 #include <sys/malloc.h>
71 #include <sys/kmem.h>
72 #include <sys/mman.h>
73 #include <sys/tty.h>
74 #include <sys/conf.h>
75 #include <dev/pci/pciio.h>
76 
77 #include <uvm/uvm_extern.h>
78 
79 #include <dev/sun/fbio.h>
80 #include <machine/autoconf.h>
81 #include <machine/pmap.h>
82 #include <dev/sun/fbvar.h>
83 #include <machine/cpu.h>
84 #include <dev/sbus/sbusvar.h>
85 
86 #include "wsdisplay.h"
87 #include <dev/wscons/wsconsio.h>
88 #include <dev/wsfont/wsfont.h>
89 #include <dev/rasops/rasops.h>
90 
91 #include <dev/wscons/wsdisplay_vconsvar.h>
92 #include <dev/wscons/wsdisplay_glyphcachevar.h>
93 
94 #include <sparc/sparc/asm.h>
95 #include <sparc/dev/cgfourteenreg.h>
96 #include <sparc/dev/cgfourteenvar.h>
97 #include <sparc/dev/sxreg.h>
98 #include <sparc/dev/sxvar.h>
99 
100 /* autoconfiguration driver */
101 static int	cgfourteenmatch(device_t, struct cfdata *, void *);
102 static void	cgfourteenattach(device_t, device_t, void *);
103 static void	cgfourteenunblank(device_t);
104 
105 CFATTACH_DECL_NEW(cgfourteen, sizeof(struct cgfourteen_softc),
106     cgfourteenmatch, cgfourteenattach, NULL, NULL);
107 
108 extern struct cfdriver cgfourteen_cd;
109 
110 dev_type_open(cgfourteenopen);
111 dev_type_close(cgfourteenclose);
112 dev_type_ioctl(cgfourteenioctl);
113 dev_type_mmap(cgfourteenmmap);
114 dev_type_poll(cgfourteenpoll);
115 
116 const struct cdevsw cgfourteen_cdevsw = {
117         .d_open = cgfourteenopen,
118 	.d_close = cgfourteenclose,
119 	.d_read = noread,
120 	.d_write = nowrite,
121 	.d_ioctl = cgfourteenioctl,
122         .d_stop = nostop,
123 	.d_tty = notty,
124 	.d_poll = cgfourteenpoll,
125 	.d_mmap = cgfourteenmmap,
126 	.d_kqfilter = nokqfilter,
127 	.d_discard = nodiscard,
128 	.d_flag = 0
129 };
130 
131 /* frame buffer generic driver */
132 static struct fbdriver cgfourteenfbdriver = {
133 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
134 	cgfourteenpoll, cgfourteenmmap, nokqfilter
135 };
136 
137 static void cg14_set_video(struct cgfourteen_softc *, int);
138 static int  cg14_get_video(struct cgfourteen_softc *);
139 static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
140 static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
141 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
142 static void cg14_init(struct cgfourteen_softc *);
143 static void cg14_reset(struct cgfourteen_softc *);
144 
145 #if NWSDISPLAY > 0
146 static void cg14_setup_wsdisplay(struct cgfourteen_softc *, int);
147 static void cg14_init_cmap(struct cgfourteen_softc *);
148 static int  cg14_putcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
149 static int  cg14_getcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
150 static void cg14_set_depth(struct cgfourteen_softc *, int);
151 static void cg14_move_cursor(struct cgfourteen_softc *, int, int);
152 static int  cg14_do_cursor(struct cgfourteen_softc *,
153                            struct wsdisplay_cursor *);
154 
155 #if NSX > 0
156 static void cg14_wait_idle(struct cgfourteen_softc *);
157 static void cg14_rectfill(struct cgfourteen_softc *, int, int, int, int,
158     uint32_t);
159 static void cg14_rectfill_a(void *, int, int, int, int, long);
160 static void cg14_invert(struct cgfourteen_softc *, int, int, int, int);
161 static void cg14_bitblt(void *, int, int, int, int, int, int, int);
162 static void cg14_bitblt_gc(void *, int, int, int, int, int, int, int);
163 
164 static void cg14_putchar_aa(void *, int, int, u_int, long);
165 static void cg14_cursor(void *, int, int, int);
166 static void cg14_putchar(void *, int, int, u_int, long);
167 static void cg14_copycols(void *, int, int, int, int);
168 static void cg14_erasecols(void *, int, int, int, long);
169 static void cg14_copyrows(void *, int, int, int);
170 static void cg14_eraserows(void *, int, int, long);
171 #endif /* NSX > 0 */
172 
173 #endif
174 
175 /*
176  * Match a cgfourteen.
177  */
178 int
179 cgfourteenmatch(device_t parent, struct cfdata *cf, void *aux)
180 {
181 	union obio_attach_args *uoba = aux;
182 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
183 
184 	/*
185 	 * The cgfourteen is a local-bus video adaptor, accessed directly
186 	 * via the processor, and not through device space or an external
187 	 * bus. Thus we look _only_ at the obio bus.
188 	 * Additionally, these things exist only on the Sun4m.
189 	 */
190 
191 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
192 		return (0);
193 
194 	/* Check driver name */
195 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
196 }
197 
198 #if NWSDISPLAY > 0
199 static int	cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
200 static paddr_t	cg14_mmap(void *, void *, off_t, int);
201 static void	cg14_init_screen(void *, struct vcons_screen *, int, long *);
202 
203 
204 struct wsdisplay_accessops cg14_accessops = {
205 	cg14_ioctl,
206 	cg14_mmap,
207 	NULL,	/* alloc_screen */
208 	NULL,	/* free_screen */
209 	NULL,	/* show_screen */
210 	NULL, 	/* load_font */
211 	NULL,	/* pollc */
212 	NULL	/* scroll */
213 };
214 #endif
215 
216 /*
217  * Attach a display.  We need to notice if it is the console, too.
218  */
219 void
220 cgfourteenattach(device_t parent, device_t self, void *aux)
221 {
222 	union obio_attach_args *uoba = aux;
223 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
224 	struct cgfourteen_softc *sc = device_private(self);
225 	struct fbdevice *fb = &sc->sc_fb;
226 	bus_space_handle_t bh;
227 	int node;
228 	volatile uint32_t *lut;
229 	int i, isconsole, items;
230 	uint32_t fbva[2] = {0, 0};
231 	uint32_t *ptr = fbva;
232 #if NSX > 0
233 	device_t dv;
234 	deviter_t di;
235 #endif
236 
237 	sc->sc_dev = self;
238 	sc->sc_opens = 0;
239 	node = sa->sa_node;
240 
241 	/* Remember cookies for cgfourteenmmap() */
242 	sc->sc_bustag = sa->sa_bustag;
243 
244 	fb->fb_driver = &cgfourteenfbdriver;
245 	fb->fb_device = sc->sc_dev;
246 	/* Mask out invalid flags from the user. */
247 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
248 
249 	fb->fb_type.fb_type = FBTYPE_MDICOLOR;
250 	fb->fb_type.fb_depth = 32;
251 
252 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
253 
254 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
255 
256 	if (sa->sa_nreg < 2) {
257 		printf("%s: only %d register sets\n",
258 			device_xname(self), sa->sa_nreg);
259 		return;
260 	}
261 	memcpy(sc->sc_physadr, sa->sa_reg,
262 	      sa->sa_nreg * sizeof(struct sbus_reg));
263 
264 	sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
265 	fb->fb_type.fb_size = sc->sc_vramsize;
266 
267 	printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
268 	/*
269 	 * Now map in the 8 useful pages of registers
270 	 */
271 	if (sa->sa_size < 0x10000) {
272 #ifdef DIAGNOSTIC
273 		printf("warning: can't find all cgfourteen registers...\n");
274 #endif
275 		sa->sa_size = 0x10000;
276 	}
277 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
278 			 sa->sa_offset,
279 			 sa->sa_size,
280 			 BUS_SPACE_MAP_LINEAR,
281 			 &bh) != 0) {
282 		printf("%s: cannot map control registers\n",
283 		    device_xname(self));
284 		return;
285 	}
286 	sc->sc_regh = bh;
287 	sc->sc_regaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
288 	sc->sc_fbaddr = BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
289 				sc->sc_physadr[CG14_PXL_IDX].sbr_offset);
290 
291 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
292 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
293 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
294 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
295 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
296 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
297 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
298 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
299 
300 	/*
301 	 * Let the user know that we're here
302 	 */
303 	printf(": %dx%d",
304 		fb->fb_type.fb_width, fb->fb_type.fb_height);
305 
306 	/*
307 	 * Enable the video.
308 	 */
309 	cg14_set_video(sc, 1);
310 
311 	/*
312 	 * Grab the initial colormap
313 	 */
314 	lut = sc->sc_clut1->clut_lut;
315 	for (i = 0; i < CG14_CLUT_SIZE; i++)
316 		sc->sc_cmap.cm_chip[i] = lut[i];
317 
318 	/* See if we're the console */
319         isconsole = fb_is_console(node);
320 
321 #if NWSDISPLAY > 0
322 	prom_getprop(sa->sa_node, "address", 4, &items, &ptr);
323 	if (fbva[1] == 0) {
324 		if (sbus_bus_map( sc->sc_bustag,
325 		    sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
326 		    sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
327 		    sc->sc_vramsize, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
328 		    &bh) != 0) {
329 			printf("%s: cannot map pixels\n",
330 				device_xname(sc->sc_dev));
331 			return;
332 		}
333 		sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
334 	} else {
335 		sc->sc_fb.fb_pixels = (void *)fbva[1];
336 	}
337 
338 	if (isconsole)
339 		printf(" (console)\n");
340 	else
341 		printf("\n");
342 
343 	sc->sc_depth = 8;
344 
345 #if NSX > 0
346 	/* see if we've got an SX to help us */
347 	sc->sc_sx = NULL;
348 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
349 	    dv != NULL;
350 	    dv = deviter_next(&di)) {
351 		if (device_is_a(dv, "sx")) {
352 			sc->sc_sx = device_private(dv);
353 		}
354 	}
355 	deviter_release(&di);
356 	if (sc->sc_sx != NULL) {
357 		sc->sc_fb_paddr = bus_space_mmap(sc->sc_bustag,
358 		    sc->sc_fbaddr, 0, 0, 0) & 0xfffff000;
359 		aprint_normal_dev(sc->sc_dev, "using %s\n",
360 		    device_xname(sc->sc_sx->sc_dev));
361 		aprint_debug_dev(sc->sc_dev, "fb paddr: %08x\n",
362 		    sc->sc_fb_paddr);
363 		sx_write(sc->sc_sx, SX_PAGE_BOUND_LOWER, sc->sc_fb_paddr);
364 		sx_write(sc->sc_sx, SX_PAGE_BOUND_UPPER,
365 		    sc->sc_fb_paddr + 0x03ffffff);
366 	}
367 	cg14_wait_idle(sc);
368 #endif
369 	cg14_setup_wsdisplay(sc, isconsole);
370 #endif
371 
372 	/* Attach to /dev/fb */
373 	fb_attach(&sc->sc_fb, isconsole);
374 }
375 
376 /*
377  * Keep track of the number of opens made. In the 24-bit driver, we need to
378  * switch to 24-bit mode on the first open, and switch back to 8-bit on
379  * the last close. This kind of nonsense is needed to give screenblank
380  * a fighting chance of working.
381  */
382 
383 int
384 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
385 {
386 	struct cgfourteen_softc *sc;
387 	int oldopens;
388 
389 	sc = device_lookup_private(&cgfourteen_cd, minor(dev));
390 	if (sc == NULL)
391 		return(ENXIO);
392 	oldopens = sc->sc_opens++;
393 
394 	/* Setup the cg14 as we want it, and save the original PROM state */
395 	if (oldopens == 0)	/* first open only, to make screenblank work */
396 		cg14_init(sc);
397 
398 	return (0);
399 }
400 
401 int
402 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
403 {
404 	struct cgfourteen_softc *sc =
405 	    device_lookup_private(&cgfourteen_cd, minor(dev));
406 	int opens;
407 
408 	opens = --sc->sc_opens;
409 	if (sc->sc_opens < 0)
410 		opens = sc->sc_opens = 0;
411 
412 	/*
413 	 * Restore video state to make the PROM happy, on last close.
414 	 */
415 	if (opens == 0) {
416 		cg14_reset(sc);
417 #if NSX > 0
418 		if (sc->sc_sx)
419 			glyphcache_wipe(&sc->sc_gc);
420 #endif
421 	}
422 	return (0);
423 }
424 
425 int
426 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
427 {
428 	struct cgfourteen_softc *sc =
429 	    device_lookup_private(&cgfourteen_cd, minor(dev));
430 	struct fbgattr *fba;
431 	int error;
432 
433 	switch (cmd) {
434 
435 	case FBIOGTYPE:
436 		*(struct fbtype *)data = sc->sc_fb.fb_type;
437 		break;
438 
439 	case FBIOGATTR:
440 		fba = (struct fbgattr *)data;
441 		fba->real_type = FBTYPE_MDICOLOR;
442 		fba->owner = 0;		/* XXX ??? */
443 		fba->fbtype = sc->sc_fb.fb_type;
444 		fba->sattr.flags = 0;
445 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
446 		fba->sattr.dev_specific[0] = -1;
447 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
448 		fba->emu_types[1] = -1;
449 		break;
450 
451 	case FBIOGETCMAP:
452 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
453 				     CG14_CLUT_SIZE));
454 
455 	case FBIOPUTCMAP:
456 		/* copy to software map */
457 #define p ((struct fbcmap *)data)
458 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
459 		if (error)
460 			return (error);
461 		/* now blast them into the chip */
462 		/* XXX should use retrace interrupt */
463 		cg14_load_hwcmap(sc, p->index, p->count);
464 #undef p
465 		break;
466 
467 	case FBIOGVIDEO:
468 		*(int *)data = cg14_get_video(sc);
469 		break;
470 
471 	case FBIOSVIDEO:
472 		cg14_set_video(sc, *(int *)data);
473 		break;
474 
475 	case CG14_SET_PIXELMODE: {
476 		int depth = *(int *)data;
477 
478 		if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
479 			return EINVAL;
480 
481 		cg14_set_depth(sc, depth);
482 		}
483 		break;
484 	default:
485 		return (ENOTTY);
486 	}
487 	return (0);
488 }
489 
490 /*
491  * Undo the effect of an FBIOSVIDEO that turns the video off.
492  */
493 static void
494 cgfourteenunblank(device_t dev)
495 {
496 	struct cgfourteen_softc *sc = device_private(dev);
497 
498 	cg14_set_video(sc, 1);
499 #if NWSDISPLAY > 0
500 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
501 		cg14_set_depth(sc, 8);
502 		cg14_init_cmap(sc);
503 		vcons_redraw_screen(sc->sc_vd.active);
504 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
505 	}
506 #endif
507 }
508 
509 /*
510  * Return the address that would map the given device at the given
511  * offset, allowing for the given protection, or return -1 for error.
512   */
513 paddr_t
514 cgfourteenmmap(dev_t dev, off_t off, int prot)
515 {
516 	struct cgfourteen_softc *sc =
517 	    device_lookup_private(&cgfourteen_cd, minor(dev));
518 	off_t offset = -1;
519 
520 	if (off & PGOFSET)
521 		panic("cgfourteenmmap");
522 
523 	if (off < 0)
524 		return (-1);
525 
526 	if (off >= 0 && off < 0x10000) {
527 		offset = sc->sc_regaddr;
528 	} else if (off >= CG14_CURSOR_VOFF &&
529 		   off < (CG14_CURSOR_VOFF + 0x1000)) {
530 		offset = sc->sc_regaddr + CG14_OFFSET_CURS;
531 		off -= CG14_CURSOR_VOFF;
532 	} else if (off >= CG14_DIRECT_VOFF &&
533 		   off < (CG14_DIRECT_VOFF + sc->sc_vramsize)) {
534 		offset = sc->sc_fbaddr + CG14_FB_VRAM;
535 		off -= CG14_DIRECT_VOFF;
536 	} else if (off >= CG14_BGR_VOFF &&
537 		   off < (CG14_BGR_VOFF + sc->sc_vramsize)) {
538 		offset = sc->sc_fbaddr + CG14_FB_CBGR;
539 		off -= CG14_BGR_VOFF;
540 	} else if (off >= CG14_X32_VOFF &&
541 		   off < (CG14_X32_VOFF + (sc->sc_vramsize >> 2))) {
542 		offset = sc->sc_fbaddr + CG14_FB_PX32;
543 		off -= CG14_X32_VOFF;
544 	} else if (off >= CG14_B32_VOFF &&
545 		   off < (CG14_B32_VOFF + (sc->sc_vramsize >> 2))) {
546 		offset = sc->sc_fbaddr + CG14_FB_PB32;
547 		off -= CG14_B32_VOFF;
548 	} else if (off >= CG14_G32_VOFF &&
549 		   off < (CG14_G32_VOFF + (sc->sc_vramsize >> 2))) {
550 		offset = sc->sc_fbaddr + CG14_FB_PG32;
551 		off -= CG14_G32_VOFF;
552 	} else if (off >= CG14_R32_VOFF &&
553 		   off < CG14_R32_VOFF + (sc->sc_vramsize >> 2)) {
554 		offset = sc->sc_fbaddr + CG14_FB_PR32;
555 		off -= CG14_R32_VOFF;
556 #if NSX > 0
557 	/*
558 	 * for convenience we also map the SX ranges here:
559 	 * - one page userland registers
560 	 * - CG14-sized IO space at 0x800000000 ( not a typo, it's above 4GB )
561 	 */
562 	} else if (sc->sc_sx == NULL) {
563 		return -1;
564 	} else if (off >= CG14_SXREG_VOFF &&
565 		   off < (CG14_SXREG_VOFF + 0x400)) {
566 		return (bus_space_mmap(sc->sc_sx->sc_tag, sc->sc_sx->sc_uregs,
567 			0, prot, BUS_SPACE_MAP_LINEAR));
568 	} else if (off >= CG14_SXIO_VOFF &&
569 		   off < (CG14_SXIO_VOFF + 0x03ffffff)) {
570 		return (bus_space_mmap(sc->sc_sx->sc_tag, 0x800000000LL,
571 			sc->sc_fb_paddr + (off - CG14_SXIO_VOFF),
572 			prot, BUS_SPACE_MAP_LINEAR));
573 #endif
574 	} else
575 		return -1;
576 
577 	return (bus_space_mmap(sc->sc_bustag, offset, off, prot,
578 		    BUS_SPACE_MAP_LINEAR));
579 }
580 
581 int
582 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
583 {
584 
585 	return (seltrue(dev, events, l));
586 }
587 
588 /*
589  * Miscellaneous helper functions
590  */
591 
592 /* Initialize the framebuffer, storing away useful state for later reset */
593 static void
594 cg14_init(struct cgfourteen_softc *sc)
595 {
596 	cg14_set_depth(sc, 32);
597 }
598 
599 static void
600 /* Restore the state saved on cg14_init */
601 cg14_reset(struct cgfourteen_softc *sc)
602 {
603 	cg14_set_depth(sc, 8);
604 }
605 
606 /* Enable/disable video display; power down monitor if DPMS-capable */
607 static void
608 cg14_set_video(struct cgfourteen_softc *sc, int enable)
609 {
610 	/*
611 	 * We can only use DPMS to power down the display if the chip revision
612 	 * is greater than 0.
613 	 */
614 	if (enable) {
615 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
616 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
617 						 CG14_MCTL_POWERCTL);
618 		else
619 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
620 	} else {
621 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
622 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
623 						  CG14_MCTL_POWERCTL);
624 		else
625 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
626 	}
627 }
628 
629 /* Get status of video display */
630 static int
631 cg14_get_video(struct cgfourteen_softc *sc)
632 {
633 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
634 }
635 
636 /* Read the software shadow colormap */
637 static int
638 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
639 {
640         u_int i, start, count;
641         u_char *cp;
642         int error;
643 
644         start = p->index;
645         count = p->count;
646         if (start >= cmsize || count > cmsize - start)
647                 return (EINVAL);
648 
649         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
650                 error = copyout(&cp[3], &p->red[i], 1);
651                 if (error)
652                         return error;
653                 error = copyout(&cp[2], &p->green[i], 1);
654                 if (error)
655                         return error;
656                 error = copyout(&cp[1], &p->blue[i], 1);
657                 if (error)
658                         return error;
659         }
660         return (0);
661 }
662 
663 /* Write the software shadow colormap */
664 static int
665 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
666 {
667         u_int i, start, count;
668         u_char *cp;
669         u_char cmap[256][4];
670         int error;
671 
672         start = p->index;
673         count = p->count;
674         if (start >= cmsize || count > cmsize - start)
675                 return (EINVAL);
676 
677         memcpy(&cmap, &cm->cm_map, sizeof cmap);
678         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
679                 error = copyin(&p->red[i], &cp[3], 1);
680                 if (error)
681                         return error;
682                 error = copyin(&p->green[i], &cp[2], 1);
683                 if (error)
684                         return error;
685                 error = copyin(&p->blue[i], &cp[1], 1);
686                 if (error)
687                         return error;
688                 cp[0] = 0;      /* no alpha channel */
689         }
690         memcpy(&cm->cm_map, &cmap, sizeof cmap);
691         return (0);
692 }
693 
694 static void
695 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
696 {
697 	/* XXX switch to auto-increment, and on retrace intr */
698 
699 	/* Setup pointers to source and dest */
700 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
701 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
702 
703 	/* Copy by words */
704 	while (--ncolors >= 0)
705 		*lutp++ = *colp++;
706 }
707 
708 static void
709 cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
710 {
711 	struct wsemuldisplaydev_attach_args aa;
712 	struct rasops_info *ri;
713 	long defattr;
714 
715  	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
716 		"default",
717 		0, 0,
718 		NULL,
719 		8, 16,
720 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
721 		WSSCREEN_RESIZE,
722 		NULL
723 	};
724 	cg14_set_depth(sc, 8);
725 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
726 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
727 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
728 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
729 	    &cg14_accessops);
730 	sc->sc_vd.init_screen = cg14_init_screen;
731 	sc->sc_vd.show_screen_cookie = &sc->sc_gc;
732 	sc->sc_vd.show_screen_cb = glyphcache_adapt;
733 
734 	ri = &sc->sc_console_screen.scr_ri;
735 
736 	sc->sc_gc.gc_bitblt = cg14_bitblt_gc;
737 	sc->sc_gc.gc_blitcookie = sc;
738 	sc->sc_gc.gc_rectfill = cg14_rectfill_a;
739 	sc->sc_gc.gc_rop = 0xc;
740 
741 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
742 		    &defattr);
743 
744 		/* clear the screen with the default background colour */
745 		if (sc->sc_sx != NULL) {
746 			cg14_rectfill(sc, 0, 0, ri->ri_width, ri->ri_height,
747 				ri->ri_devcmap[(defattr >> 16) & 0xf]);
748 		} else {
749 			memset(sc->sc_fb.fb_pixels,
750 			       ri->ri_devcmap[(defattr >> 16) & 0xf],
751 			       ri->ri_stride * ri->ri_height);
752 		}
753 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
754 
755 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
756 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
757 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
758 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
759 		glyphcache_init(&sc->sc_gc, sc->sc_fb.fb_type.fb_height + 5,
760 			(sc->sc_vramsize / sc->sc_fb.fb_type.fb_width) -
761 			 sc->sc_fb.fb_type.fb_height - 5,
762 			sc->sc_fb.fb_type.fb_width,
763 			ri->ri_font->fontwidth,
764 			ri->ri_font->fontheight,
765 			defattr);
766 	if (is_cons) {
767 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
768 		    defattr);
769 		vcons_replay_msgbuf(&sc->sc_console_screen);
770 	}
771 
772 	cg14_init_cmap(sc);
773 
774 	aa.console = is_cons;
775 	aa.scrdata = &sc->sc_screenlist;
776 	aa.accessops = &cg14_accessops;
777 	aa.accesscookie = &sc->sc_vd;
778 
779 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
780 }
781 
782 static void
783 cg14_init_cmap(struct cgfourteen_softc *sc)
784 {
785 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri;
786 	int i, j = 0;
787 	uint8_t cmap[768];
788 
789 	rasops_get_cmap(ri, cmap, sizeof(cmap));
790 
791 	for (i = 0; i < 256; i++) {
792 
793 		sc->sc_cmap.cm_map[i][3] = cmap[j];
794 		sc->sc_cmap.cm_map[i][2] = cmap[j + 1];
795 		sc->sc_cmap.cm_map[i][1] = cmap[j + 2];
796 		j += 3;
797 	}
798 	cg14_load_hwcmap(sc, 0, 256);
799 }
800 
801 static int
802 cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
803 {
804 	u_int index = cm->index;
805 	u_int count = cm->count;
806 	int i, error;
807 	u_char rbuf[256], gbuf[256], bbuf[256];
808 
809 	if (cm->index >= 256 || cm->count > 256 ||
810 	    (cm->index + cm->count) > 256)
811 		return EINVAL;
812 	error = copyin(cm->red, &rbuf[index], count);
813 	if (error)
814 		return error;
815 	error = copyin(cm->green, &gbuf[index], count);
816 	if (error)
817 		return error;
818 	error = copyin(cm->blue, &bbuf[index], count);
819 	if (error)
820 		return error;
821 
822 	for (i = 0; i < count; i++) {
823 		sc->sc_cmap.cm_map[index][3] = rbuf[index];
824 		sc->sc_cmap.cm_map[index][2] = gbuf[index];
825 		sc->sc_cmap.cm_map[index][1] = bbuf[index];
826 
827 		index++;
828 	}
829 	cg14_load_hwcmap(sc, 0, 256);
830 	return 0;
831 }
832 
833 static int
834 cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
835 {
836 	uint8_t rbuf[256], gbuf[256], bbuf[256];
837 	u_int index = cm->index;
838 	u_int count = cm->count;
839 	int error, i;
840 
841 	if (index >= 255 || count > 256 || index + count > 256)
842 		return EINVAL;
843 
844 
845 	for (i = 0; i < count; i++) {
846 		rbuf[i] = sc->sc_cmap.cm_map[index][3];
847 		gbuf[i] = sc->sc_cmap.cm_map[index][2];
848 		bbuf[i] = sc->sc_cmap.cm_map[index][1];
849 
850 		index++;
851 	}
852 	error = copyout(rbuf,   cm->red,   count);
853 	if (error)
854 		return error;
855 	error = copyout(gbuf, cm->green, count);
856 	if (error)
857 		return error;
858 	error = copyout(bbuf,  cm->blue,  count);
859 	if (error)
860 		return error;
861 
862 	return 0;
863 }
864 
865 static int
866 cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
867 	struct lwp *l)
868 {
869 	struct vcons_data *vd = v;
870 	struct cgfourteen_softc *sc = vd->cookie;
871 	struct wsdisplay_fbinfo *wdf;
872 	struct vcons_screen *ms = vd->active;
873 
874 	switch (cmd) {
875 
876 		case WSDISPLAYIO_GTYPE:
877 			*(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
878 			return 0;
879 
880 		case WSDISPLAYIO_GINFO:
881 			wdf = (void *)data;
882 			wdf->height = ms->scr_ri.ri_height;
883 			wdf->width = ms->scr_ri.ri_width;
884 			wdf->depth = 32;
885 			wdf->cmsize = 256;
886 			return 0;
887 
888 		case WSDISPLAYIO_GETCMAP:
889 			return cg14_getcmap(sc,
890 			    (struct wsdisplay_cmap *)data);
891 
892 		case WSDISPLAYIO_PUTCMAP:
893 			return cg14_putcmap(sc,
894 			    (struct wsdisplay_cmap *)data);
895 
896 		case WSDISPLAYIO_LINEBYTES:
897 			*(u_int *)data = ms->scr_ri.ri_stride << 2;
898 			return 0;
899 
900 		case WSDISPLAYIO_SMODE:
901 			{
902 				int new_mode = *(int*)data;
903 				if (new_mode != sc->sc_mode) {
904 					sc->sc_mode = new_mode;
905 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
906 						bus_space_write_1(sc->sc_bustag,
907 						    sc->sc_regh,
908 						    CG14_CURSOR_CONTROL, 0);
909 
910 						cg14_set_depth(sc, 8);
911 						cg14_init_cmap(sc);
912 #if NSX > 0
913 						if (sc->sc_sx)
914 							glyphcache_wipe(&sc->sc_gc);
915 #endif
916 						vcons_redraw_screen(ms);
917 					} else {
918 
919 						cg14_set_depth(sc, 32);
920 					}
921 				}
922 			}
923 			return 0;
924 		case WSDISPLAYIO_SVIDEO:
925 			cg14_set_video(sc, *(int *)data);
926 			return 0;
927 		case WSDISPLAYIO_GVIDEO:
928 			return cg14_get_video(sc) ?
929 			    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
930 		case WSDISPLAYIO_GCURPOS:
931 			{
932 				struct wsdisplay_curpos *cp = (void *)data;
933 
934 				cp->x = sc->sc_cursor.cc_pos.x;
935 				cp->y = sc->sc_cursor.cc_pos.y;
936 			}
937 			return 0;
938 		case WSDISPLAYIO_SCURPOS:
939 			{
940 				struct wsdisplay_curpos *cp = (void *)data;
941 
942 				cg14_move_cursor(sc, cp->x, cp->y);
943 			}
944 			return 0;
945 		case WSDISPLAYIO_GCURMAX:
946 			{
947 				struct wsdisplay_curpos *cp = (void *)data;
948 
949 				cp->x = 32;
950 				cp->y = 32;
951 			}
952 			return 0;
953 		case WSDISPLAYIO_SCURSOR:
954 			{
955 				struct wsdisplay_cursor *cursor = (void *)data;
956 
957 				return cg14_do_cursor(sc, cursor);
958 			}
959 		case PCI_IOC_CFGREAD:
960 		case PCI_IOC_CFGWRITE:
961 			return EINVAL;
962 
963 	}
964 	return EPASSTHROUGH;
965 }
966 
967 static paddr_t
968 cg14_mmap(void *v, void *vs, off_t offset, int prot)
969 {
970 	struct vcons_data *vd = v;
971 	struct cgfourteen_softc *sc = vd->cookie;
972 
973 	/* allow mmap()ing the full framebuffer, not just what we use */
974 	if (offset < sc->sc_vramsize)
975 		return bus_space_mmap(sc->sc_bustag,
976 		    BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
977 		      sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
978 		    offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
979 
980 	return -1;
981 }
982 
983 static void
984 cg14_init_screen(void *cookie, struct vcons_screen *scr,
985     int existing, long *defattr)
986 {
987 	struct cgfourteen_softc *sc = cookie;
988 	struct rasops_info *ri = &scr->scr_ri;
989 
990 	ri->ri_depth = 8;
991 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
992 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
993 	ri->ri_stride = ri->ri_width;
994 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
995 
996 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
997 
998 	scr->scr_flags |= VCONS_LOADFONT;
999 #if NSX > 0
1000 	ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
1001 
1002 	/*
1003 	 * unaligned copies with horizontal overlap are slow, so don't bother
1004 	 * handling them in cg14_bitblt() and use putchar() instead
1005 	 */
1006 	if (sc->sc_sx != NULL) {
1007 		scr->scr_flags |= VCONS_NO_COPYCOLS;
1008 	} else
1009 #endif
1010 		scr->scr_flags |= VCONS_DONT_READ;
1011 
1012 	if (existing) {
1013 		ri->ri_flg |= RI_CLEAR;
1014 	}
1015 
1016 	rasops_init(ri, 0, 0);
1017 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
1018 		      WSSCREEN_RESIZE;
1019 
1020 	rasops_reconfig(ri,
1021 	    sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
1022 	    sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
1023 
1024 	ri->ri_hw = scr;
1025 #if NSX > 0
1026 	if (sc->sc_sx != NULL) {
1027 		ri->ri_ops.copyrows = cg14_copyrows;
1028 		ri->ri_ops.copycols = cg14_copycols;
1029 		ri->ri_ops.eraserows = cg14_eraserows;
1030 		ri->ri_ops.erasecols = cg14_erasecols;
1031 		ri->ri_ops.cursor = cg14_cursor;
1032 		if (FONT_IS_ALPHA(ri->ri_font)) {
1033 			ri->ri_ops.putchar = cg14_putchar_aa;
1034 		} else
1035 			ri->ri_ops.putchar = cg14_putchar;
1036 	}
1037 #endif /* NSX > 0 */
1038 }
1039 
1040 static void
1041 cg14_set_depth(struct cgfourteen_softc *sc, int depth)
1042 {
1043 	int i;
1044 
1045 	if (sc->sc_depth == depth)
1046 		return;
1047 
1048 	switch (depth) {
1049 		case 8:
1050 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1051 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
1052 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
1053 			sc->sc_depth = 8;
1054 			/* everything is CLUT1 */
1055 			for (i = 0; i < CG14_CLUT_SIZE; i++)
1056 			     sc->sc_xlut->xlut_lut[i] = 0;
1057 			break;
1058 		case 32:
1059 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1060 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
1061 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
1062 			sc->sc_depth = 32;
1063 			for (i = 0; i < CG14_CLUT_SIZE; i++)
1064 			     sc->sc_xlut->xlut_lut[i] = 0;
1065 			break;
1066 		default:
1067 			printf("%s: can't change to depth %d\n",
1068 			    device_xname(sc->sc_dev), depth);
1069 	}
1070 }
1071 
1072 static void
1073 cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
1074 {
1075 	uint32_t pos;
1076 
1077 	sc->sc_cursor.cc_pos.x = x;
1078 	sc->sc_cursor.cc_pos.y = y;
1079 	pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
1080 	      ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
1081 	bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
1082 }
1083 
1084 static int
1085 cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
1086 {
1087 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1088 
1089 		bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1090 		    CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
1091 	}
1092 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1093 
1094 		sc->sc_cursor.cc_hot.x = cur->hot.x;
1095 		sc->sc_cursor.cc_hot.y = cur->hot.y;
1096 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
1097 	}
1098 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1099 
1100 		cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
1101 	}
1102 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1103 		int i;
1104 		uint32_t val;
1105 
1106 		if ((cur->cmap.index > 2) || (cur->cmap.count > 3) ||
1107 		    (cur->cmap.index + cur->cmap.count > 3))
1108 			return EINVAL;
1109 
1110 		for (i = 0; i < uimin(cur->cmap.count, 3); i++) {
1111 			val = (cur->cmap.red[i] ) |
1112 			      (cur->cmap.green[i] << 8) |
1113 			      (cur->cmap.blue[i] << 16);
1114 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1115 			    CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
1116 			    val);
1117 		}
1118 	}
1119 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1120 		uint32_t buffer[32], latch, tmp;
1121 		int i;
1122 
1123 		copyin(cur->mask, buffer, 128);
1124 		for (i = 0; i < 32; i++) {
1125 			latch = 0;
1126 			tmp = buffer[i] & 0x80808080;
1127 			latch |= tmp >> 7;
1128 			tmp = buffer[i] & 0x40404040;
1129 			latch |= tmp >> 5;
1130 			tmp = buffer[i] & 0x20202020;
1131 			latch |= tmp >> 3;
1132 			tmp = buffer[i] & 0x10101010;
1133 			latch |= tmp >> 1;
1134 			tmp = buffer[i] & 0x08080808;
1135 			latch |= tmp << 1;
1136 			tmp = buffer[i] & 0x04040404;
1137 			latch |= tmp << 3;
1138 			tmp = buffer[i] & 0x02020202;
1139 			latch |= tmp << 5;
1140 			tmp = buffer[i] & 0x01010101;
1141 			latch |= tmp << 7;
1142 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1143 			    CG14_CURSOR_PLANE0 + (i << 2), latch);
1144 		}
1145 		copyin(cur->image, buffer, 128);
1146 		for (i = 0; i < 32; i++) {
1147 			latch = 0;
1148 			tmp = buffer[i] & 0x80808080;
1149 			latch |= tmp >> 7;
1150 			tmp = buffer[i] & 0x40404040;
1151 			latch |= tmp >> 5;
1152 			tmp = buffer[i] & 0x20202020;
1153 			latch |= tmp >> 3;
1154 			tmp = buffer[i] & 0x10101010;
1155 			latch |= tmp >> 1;
1156 			tmp = buffer[i] & 0x08080808;
1157 			latch |= tmp << 1;
1158 			tmp = buffer[i] & 0x04040404;
1159 			latch |= tmp << 3;
1160 			tmp = buffer[i] & 0x02020202;
1161 			latch |= tmp << 5;
1162 			tmp = buffer[i] & 0x01010101;
1163 			latch |= tmp << 7;
1164 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1165 			    CG14_CURSOR_PLANE1 + (i << 2), latch);
1166 		}
1167 	}
1168 	return 0;
1169 }
1170 
1171 #if NSX > 0
1172 
1173 static void
1174 cg14_wait_idle(struct cgfourteen_softc *sc)
1175 {
1176 }
1177 
1178 static void
1179 cg14_rectfill(struct cgfourteen_softc *sc, int x, int y, int wi, int he,
1180      uint32_t colour)
1181 {
1182 	uint32_t addr, pptr;
1183 	int line, cnt, pre, words;
1184 	int stride = sc->sc_fb.fb_type.fb_width;
1185 
1186 	addr = sc->sc_fb_paddr + x + stride * y;
1187 	sx_write(sc->sc_sx, SX_QUEUED(8), colour);
1188 	sx_write(sc->sc_sx, SX_QUEUED(9), colour);
1189 	/*
1190 	 * Calculate the number of pixels we need to do one by one
1191 	 * until we're 32bit aligned, then do the rest in 32bit
1192 	 * mode. Assumes that stride is always a multiple of 4.
1193 	 */
1194 	/* TODO: use 32bit writes with byte mask instead */
1195 	pre = addr & 3;
1196 	if (pre != 0) pre = 4 - pre;
1197 	for (line = 0; line < he; line++) {
1198 		pptr = addr;
1199 		cnt = wi;
1200 		if (pre) {
1201 			sta(pptr & ~7, ASI_SX, SX_STBS(8, pre - 1, pptr & 7));
1202 			pptr += pre;
1203 			cnt -= pre;
1204 		}
1205 		/* now do the aligned pixels in 32bit chunks */
1206 		while(cnt > 3) {
1207 			words = uimin(32, cnt >> 2);
1208 			sta(pptr & ~7, ASI_SX, SX_STS(8, words - 1, pptr & 7));
1209 			pptr += words << 2;
1210 			cnt -= words << 2;
1211 		}
1212 		/* do any remaining pixels byte-wise again */
1213 		if (cnt > 0)
1214 			sta(pptr & ~7, ASI_SX, SX_STBS(8, cnt - 1, pptr & 7));
1215 		addr += stride;
1216 	}
1217 }
1218 
1219 static void
1220 cg14_rectfill_a(void *cookie, int dstx, int dsty,
1221     int width, int height, long attr)
1222 {
1223 	struct cgfourteen_softc *sc = cookie;
1224 
1225 	cg14_rectfill(sc, dstx, dsty, width, height,
1226 	    sc->sc_vd.active->scr_ri.ri_devcmap[(attr >> 24 & 0xf)]);
1227 }
1228 
1229 static void
1230 cg14_invert(struct cgfourteen_softc *sc, int x, int y, int wi, int he)
1231 {
1232 	uint32_t addr, pptr;
1233 	int line, cnt, pre, words;
1234 	int stride = sc->sc_fb.fb_type.fb_width;
1235 
1236 	addr = sc->sc_fb_paddr + x + stride * y;
1237 	sx_write(sc->sc_sx, SX_ROP_CONTROL, 0x33); /* ~src a */
1238 	/*
1239 	 * Calculate the number of pixels we need to do one by one
1240 	 * until we're 32bit aligned, then do the rest in 32bit
1241 	 * mode. Assumes that stride is always a multiple of 4.
1242 	 */
1243 	/* TODO: use 32bit writes with byte mask instead */
1244 	pre = addr & 3;
1245 	if (pre != 0) pre = 4 - pre;
1246 	for (line = 0; line < he; line++) {
1247 		pptr = addr;
1248 		cnt = wi;
1249 		if (pre) {
1250 			sta(pptr & ~7, ASI_SX, SX_LDB(8, pre - 1, pptr & 7));
1251 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
1252 			    SX_ROP(8, 8, 32, pre - 1));
1253 			sta(pptr & ~7, ASI_SX, SX_STB(32, pre - 1, pptr & 7));
1254 			pptr += pre;
1255 			cnt -= pre;
1256 		}
1257 		/* now do the aligned pixels in 32bit chunks */
1258 		while(cnt > 15) {
1259 			words = uimin(16, cnt >> 2);
1260 			sta(pptr & ~7, ASI_SX, SX_LD(8, words - 1, pptr & 7));
1261 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
1262 			    SX_ROP(8, 8, 32, words - 1));
1263 			sta(pptr & ~7, ASI_SX, SX_ST(32, words - 1, pptr & 7));
1264 			pptr += words << 2;
1265 			cnt -= words << 2;
1266 		}
1267 		/* do any remaining pixels byte-wise again */
1268 		if (cnt > 0)
1269 			sta(pptr & ~7, ASI_SX, SX_LDB(8, cnt - 1, pptr & 7));
1270 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
1271 			    SX_ROP(8, 8, 32, cnt - 1));
1272 			sta(pptr & ~7, ASI_SX, SX_STB(32, cnt - 1, pptr & 7));
1273 		addr += stride;
1274 	}
1275 }
1276 
1277 static inline void
1278 cg14_slurp(int reg, uint32_t addr, int cnt)
1279 {
1280 	int num;
1281 	while (cnt > 0) {
1282 		num = uimin(32, cnt);
1283 		sta(addr & ~7, ASI_SX, SX_LD(reg, num - 1, addr & 7));
1284 		cnt -= num;
1285 		reg += num;
1286 		addr += (num << 2);
1287 	}
1288 }
1289 
1290 static inline void
1291 cg14_spit(int reg, uint32_t addr, int cnt)
1292 {
1293 	int num;
1294 	while (cnt > 0) {
1295 		num = uimin(32, cnt);
1296 		sta(addr & ~7, ASI_SX, SX_ST(reg, num - 1, addr & 7));
1297 		cnt -= num;
1298 		reg += num;
1299 		addr += (num << 2);
1300 	}
1301 }
1302 
1303 static void
1304 cg14_bitblt(void *cookie, int xs, int ys, int xd, int yd,
1305     int wi, int he, int rop)
1306 {
1307 	struct cgfourteen_softc *sc = cookie;
1308 	uint32_t saddr, daddr, sptr, dptr;
1309 	int line, cnt, stride = sc->sc_fb.fb_type.fb_width;
1310 	int num, words, skip;
1311 
1312 	if (ys < yd) {
1313 		/* need to go bottom-up */
1314 		saddr = sc->sc_fb_paddr + xs + stride * (ys + he - 1);
1315 		daddr = sc->sc_fb_paddr + xd + stride * (yd + he - 1);
1316 		skip = -stride;
1317 	} else {
1318 		saddr = sc->sc_fb_paddr + xs + stride * ys;
1319 		daddr = sc->sc_fb_paddr + xd + stride * yd;
1320 		skip = stride;
1321 	}
1322 
1323 	if ((saddr & 3) == (daddr & 3)) {
1324 		int pre = saddr & 3;	/* pixels to copy byte-wise */
1325 		if (pre != 0) pre = 4 - pre;
1326 		for (line = 0; line < he; line++) {
1327 			sptr = saddr;
1328 			dptr = daddr;
1329 			cnt = wi;
1330 			if (pre > 0) {
1331 				sta(sptr & ~7, ASI_SX,
1332 				    SX_LDB(32, pre - 1, sptr & 7));
1333 				sta(dptr & ~7, ASI_SX,
1334 				    SX_STB(32, pre - 1, dptr & 7));
1335 				cnt -= pre;
1336 				sptr += pre;
1337 				dptr += pre;
1338 			}
1339 			words = cnt >> 2;
1340 			while(cnt > 3) {
1341 				num = uimin(120, words);
1342 				cg14_slurp(8, sptr, num);
1343 				cg14_spit(8, dptr, num);
1344 				sptr += num << 2;
1345 				dptr += num << 2;
1346 				cnt -= num << 2;
1347 			}
1348 			if (cnt > 0) {
1349 				sta(sptr & ~7, ASI_SX,
1350 				    SX_LDB(32, cnt - 1, sptr & 7));
1351 				sta(dptr & ~7, ASI_SX,
1352 				    SX_STB(32, cnt - 1, dptr & 7));
1353 			}
1354 			saddr += skip;
1355 			daddr += skip;
1356 		}
1357 	} else {
1358 		/* unaligned, have to use byte mode */
1359 		/* funnel shifter & byte mask trickery? */
1360 		for (line = 0; line < he; line++) {
1361 			sptr = saddr;
1362 			dptr = daddr;
1363 			cnt = wi;
1364 			while(cnt > 31) {
1365 				sta(sptr & ~7, ASI_SX, SX_LDB(32, 31, sptr & 7));
1366 				sta(dptr & ~7, ASI_SX, SX_STB(32, 31, dptr & 7));
1367 				sptr += 32;
1368 				dptr += 32;
1369 				cnt -= 32;
1370 			}
1371 			if (cnt > 0) {
1372 				sta(sptr & ~7, ASI_SX,
1373 				    SX_LDB(32, cnt - 1, sptr & 7));
1374 				sta(dptr & ~7, ASI_SX,
1375 				    SX_STB(32, cnt - 1, dptr & 7));
1376 			}
1377 			saddr += skip;
1378 			daddr += skip;
1379 		}
1380 	}
1381 }
1382 
1383 /*
1384  * for copying glyphs around
1385  * - uses all quads for reads
1386  * - uses quads for writes as far as possible
1387  * - limited by number of registers - won't do more than 120 wide
1388  * - doesn't handle overlaps
1389  */
1390 static void
1391 cg14_bitblt_gc(void *cookie, int xs, int ys, int xd, int yd,
1392     int wi, int he, int rop)
1393 {
1394 	struct cgfourteen_softc *sc = cookie;
1395 	uint32_t saddr, daddr;
1396 	int line, cnt = wi, stride = sc->sc_fb.fb_type.fb_width;
1397 	int dreg = 8, swi = wi, dd;
1398 	int in = 0, q = 0, out = 0, r;
1399 
1400 	saddr = sc->sc_fb_paddr + xs + stride * ys;
1401 	daddr = sc->sc_fb_paddr + xd + stride * yd;
1402 
1403 	if (saddr & 3) {
1404 		swi += saddr & 3;
1405 		dreg += saddr & 3;
1406 		saddr &= ~3;
1407 	}
1408 	swi = (swi + 3) >> 2;	/* round up, number of quads to read */
1409 
1410 	if (daddr & 3) {
1411 		in = 4 - (daddr & 3); /* pixels to write in byte mode */
1412 		cnt -= in;
1413 	}
1414 
1415 	q = cnt >> 2;
1416 	out = cnt & 3;
1417 
1418 	for (line = 0; line < he; line++) {
1419 		/* read source line, in all quads */
1420 		sta(saddr & ~7, ASI_SX, SX_LDUQ0(8, swi - 1, saddr & 7));
1421 		/* now write it out */
1422 		dd = daddr;
1423 		r = dreg;
1424 		if (in > 0) {
1425 			sta(dd & ~7, ASI_SX, SX_STB(r, in - 1, dd & 7));
1426 			dd += in;
1427 			r += in;
1428 		}
1429 		if (q > 0) {
1430 			sta(dd & ~7, ASI_SX, SX_STUQ0(r, q - 1, dd & 7));
1431 			r += q << 2;
1432 			dd += q << 2;
1433 		}
1434 		if (out > 0) {
1435 			sta(dd & ~7, ASI_SX, SX_STB(r, out - 1, dd & 7));
1436 		}
1437 		saddr += stride;
1438 		daddr += stride;
1439 	}
1440 }
1441 
1442 static void
1443 cg14_putchar(void *cookie, int row, int col, u_int c, long attr)
1444 {
1445 	struct rasops_info *ri = cookie;
1446 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1447 	struct vcons_screen *scr = ri->ri_hw;
1448 	struct cgfourteen_softc *sc = scr->scr_cookie;
1449 	void *data;
1450 	uint32_t fg, bg;
1451 	int i, x, y, wi, he;
1452 	uint32_t addr;
1453 	int stride = sc->sc_fb.fb_type.fb_width;
1454 
1455 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1456 		return;
1457 
1458 	if (!CHAR_IN_FONT(c, font))
1459 		return;
1460 
1461 	wi = font->fontwidth;
1462 	he = font->fontheight;
1463 
1464 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1465 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
1466 
1467 	x = ri->ri_xorigin + col * wi;
1468 	y = ri->ri_yorigin + row * he;
1469 
1470 	if (c == 0x20) {
1471 		cg14_rectfill(sc, x, y, wi, he, bg);
1472 		if (attr & 1)
1473 			cg14_rectfill(sc, x, y + he - 2, wi, 1, fg);
1474 		return;
1475 	}
1476 
1477 	sx_write(sc->sc_sx, SX_QUEUED(8), bg);
1478 	sx_write(sc->sc_sx, SX_QUEUED(9), fg);
1479 
1480 	data = WSFONT_GLYPH(c, font);
1481 	addr = sc->sc_fb_paddr + x + stride * y;
1482 
1483 	switch (font->stride) {
1484 		case 1: {
1485 			uint8_t *data8 = data;
1486 			uint32_t reg;
1487 			for (i = 0; i < he; i++) {
1488 				reg = *data8;
1489 				sx_write(sc->sc_sx, SX_QUEUED(R_MASK),
1490 				    reg << 24);
1491 				sta(addr & ~7, ASI_SX, SX_STBS(8, wi - 1, addr & 7));
1492 				data8++;
1493 				addr += stride;
1494 			}
1495 			break;
1496 		}
1497 		case 2: {
1498 			uint16_t *data16 = data;
1499 			uint32_t reg;
1500 			for (i = 0; i < he; i++) {
1501 				reg = *data16;
1502 				sx_write(sc->sc_sx, SX_QUEUED(R_MASK),
1503 				    reg << 16);
1504 				sta(addr & ~7, ASI_SX, SX_STBS(8, wi - 1, addr & 7));
1505 				data16++;
1506 				addr += stride;
1507 			}
1508 			break;
1509 		}
1510 	}
1511 	if (attr & 1)
1512 		cg14_rectfill(sc, x, y + he - 2, wi, 1, fg);
1513 }
1514 
1515 static void
1516 cg14_cursor(void *cookie, int on, int row, int col)
1517 {
1518 	struct rasops_info *ri = cookie;
1519 	struct vcons_screen *scr = ri->ri_hw;
1520 	struct cgfourteen_softc *sc = scr->scr_cookie;
1521 	int x, y, wi, he;
1522 
1523 	wi = ri->ri_font->fontwidth;
1524 	he = ri->ri_font->fontheight;
1525 
1526 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1527 		x = ri->ri_ccol * wi + ri->ri_xorigin;
1528 		y = ri->ri_crow * he + ri->ri_yorigin;
1529 		if (ri->ri_flg & RI_CURSOR) {
1530 			cg14_invert(sc, x, y, wi, he);
1531 			ri->ri_flg &= ~RI_CURSOR;
1532 		}
1533 		ri->ri_crow = row;
1534 		ri->ri_ccol = col;
1535 		if (on) {
1536 			x = ri->ri_ccol * wi + ri->ri_xorigin;
1537 			y = ri->ri_crow * he + ri->ri_yorigin;
1538 			cg14_invert(sc, x, y, wi, he);
1539 			ri->ri_flg |= RI_CURSOR;
1540 		}
1541 	} else {
1542 		scr->scr_ri.ri_crow = row;
1543 		scr->scr_ri.ri_ccol = col;
1544 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
1545 	}
1546 
1547 }
1548 
1549 static void
1550 cg14_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1551 {
1552 	struct rasops_info *ri = cookie;
1553 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1554 	struct vcons_screen *scr = ri->ri_hw;
1555 	struct cgfourteen_softc *sc = scr->scr_cookie;
1556 	int stride = sc->sc_fb.fb_type.fb_width;
1557 	uint32_t bg, fg, addr, bg8, fg8, pixel, in, q, next;
1558 	int i, j, x, y, wi, he, r, g, b, aval, cnt, reg;
1559 	int r1, g1, b1, r0, g0, b0, fgo, bgo, rv;
1560 	uint8_t *data8;
1561 
1562 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1563 		return;
1564 
1565 	if (!CHAR_IN_FONT(c, font))
1566 		return;
1567 
1568 	wi = font->fontwidth;
1569 	he = font->fontheight;
1570 
1571 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1572 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
1573 	x = ri->ri_xorigin + col * wi;
1574 	y = ri->ri_yorigin + row * he;
1575 	if (c == 0x20) {
1576 		cg14_rectfill(sc, x, y, wi, he, bg);
1577 		if (attr & 1)
1578 			cg14_rectfill(sc, x, y + he - 2, wi, 1, fg);
1579 		return;
1580 	}
1581 
1582 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1583 	if (rv == GC_OK)
1584 		return;
1585 
1586 	addr = sc->sc_fb_paddr + x + stride * y;
1587 	data8 = WSFONT_GLYPH(c, font);
1588 
1589 	/*
1590 	 * we need the RGB colours here, so get offsets into rasops_cmap
1591 	 */
1592 	fgo = ((attr >> 24) & 0xf) * 3;
1593 	bgo = ((attr >> 16) & 0xf) * 3;
1594 
1595 	r0 = rasops_cmap[bgo];
1596 	r1 = rasops_cmap[fgo];
1597 	g0 = rasops_cmap[bgo + 1];
1598 	g1 = rasops_cmap[fgo + 1];
1599 	b0 = rasops_cmap[bgo + 2];
1600 	b1 = rasops_cmap[fgo + 2];
1601 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
1602 	bg8 = R3G3B2(r0, g0, b0);
1603 	fg8 = R3G3B2(r1, g1, b1);
1604 
1605 	for (i = 0; i < he; i++) {
1606 		/* calculate one line of pixels */
1607 		for (j = 0; j < wi; j++) {
1608 			aval = *data8;
1609 			if (aval == 0) {
1610 				pixel = bg8;
1611 			} else if (aval == 255) {
1612 				pixel = fg8;
1613 			} else {
1614 				r = aval * r1 + (255 - aval) * r0;
1615 				g = aval * g1 + (255 - aval) * g0;
1616 				b = aval * b1 + (255 - aval) * b0;
1617 				pixel = ((r & 0xe000) >> 8) |
1618 					((g & 0xe000) >> 11) |
1619 					((b & 0xc000) >> 14);
1620 			}
1621 			/*
1622 			 * stick them into SX registers and hope we never have
1623 			 * to deal with fonts more than 120 pixels wide
1624 			 */
1625 			sx_write(sc->sc_sx, SX_QUEUED(j + 8), pixel);
1626 			data8++;
1627 		}
1628 		/* now write them into video memory */
1629 		in = (addr & 3);
1630 		next = addr;
1631 		reg = 8;
1632 		cnt = wi;
1633 		if (in != 0) {
1634 			in = 4 - in;	/* pixels to write until aligned */
1635 			sta(next & ~7, ASI_SX, SX_STB(8, in - 1, next & 7));
1636 			next += in;
1637 			reg = 8 + in;
1638 			cnt -= in;
1639 		}
1640 		q = cnt >> 2;	/* number of writes we can do in quads */
1641 		if (q > 0) {
1642 			sta(next & ~7, ASI_SX, SX_STUQ0(reg, q - 1, next & 7));
1643 			next += (q << 2);
1644 			cnt -= (q << 2);
1645 			reg += (q << 2);
1646 		}
1647 		if (cnt > 0) {
1648 			sta(next & ~7, ASI_SX, SX_STB(reg, cnt - 1, next & 7));
1649 		}
1650 
1651 		addr += stride;
1652 	}
1653 
1654 	if (rv == GC_ADD) {
1655 		glyphcache_add(&sc->sc_gc, c, x, y);
1656 	} else if (attr & 1)
1657 		cg14_rectfill(sc, x, y + he - 2, wi, 1, fg);
1658 
1659 }
1660 
1661 static void
1662 cg14_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1663 {
1664 	struct rasops_info *ri = cookie;
1665 	struct vcons_screen *scr = ri->ri_hw;
1666 	struct cgfourteen_softc *sc = scr->scr_cookie;
1667 	int32_t xs, xd, y, width, height;
1668 
1669 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1670 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1671 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1672 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1673 		width = ri->ri_font->fontwidth * ncols;
1674 		height = ri->ri_font->fontheight;
1675 		cg14_bitblt(sc, xs, y, xd, y, width, height, 0x0c);
1676 	}
1677 }
1678 
1679 static void
1680 cg14_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1681 {
1682 	struct rasops_info *ri = cookie;
1683 	struct vcons_screen *scr = ri->ri_hw;
1684 	struct cgfourteen_softc *sc = scr->scr_cookie;
1685 	int32_t x, y, width, height, fg, bg, ul;
1686 
1687 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1688 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1689 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1690 		width = ri->ri_font->fontwidth * ncols;
1691 		height = ri->ri_font->fontheight;
1692 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1693 
1694 		cg14_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1695 	}
1696 }
1697 
1698 static void
1699 cg14_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1700 {
1701 	struct rasops_info *ri = cookie;
1702 	struct vcons_screen *scr = ri->ri_hw;
1703 	struct cgfourteen_softc *sc = scr->scr_cookie;
1704 	int32_t x, ys, yd, width, height;
1705 
1706 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1707 		x = ri->ri_xorigin;
1708 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1709 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1710 		width = ri->ri_emuwidth;
1711 		height = ri->ri_font->fontheight * nrows;
1712 		cg14_bitblt(sc, x, ys, x, yd, width, height, 0x0c);
1713 	}
1714 }
1715 
1716 static void
1717 cg14_eraserows(void *cookie, int row, int nrows, long fillattr)
1718 {
1719 	struct rasops_info *ri = cookie;
1720 	struct vcons_screen *scr = ri->ri_hw;
1721 	struct cgfourteen_softc *sc = scr->scr_cookie;
1722 	int32_t x, y, width, height, fg, bg, ul;
1723 
1724 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1725 		x = ri->ri_xorigin;
1726 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1727 		width = ri->ri_emuwidth;
1728 		height = ri->ri_font->fontheight * nrows;
1729 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1730 
1731 		cg14_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1732 	}
1733 }
1734 
1735 #endif /* NSX > 0 */
1736 
1737