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