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