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