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