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