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