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