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