tga.c revision 1.21 1 /* $NetBSD: tga.c,v 1.21 2000/03/12 05:32:29 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/device.h>
34 #include <sys/conf.h>
35 #include <sys/malloc.h>
36 #include <sys/buf.h>
37 #include <sys/ioctl.h>
38
39 #include <vm/vm.h>
40
41 #include <machine/bus.h>
42 #include <machine/intr.h>
43
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcidevs.h>
47 #include <dev/pci/tgareg.h>
48 #include <dev/pci/tgavar.h>
49 #include <dev/ic/bt485reg.h>
50 #include <dev/ic/bt485var.h>
51
52 #include <dev/rcons/raster.h>
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/wscons/wscons_raster.h>
55 #include <dev/wscons/wsdisplayvar.h>
56
57 #ifdef __alpha__
58 #include <machine/pte.h>
59 #endif
60
61 int tgamatch __P((struct device *, struct cfdata *, void *));
62 void tgaattach __P((struct device *, struct device *, void *));
63 int tgaprint __P((void *, const char *));
64
65 struct cfattach tga_ca = {
66 sizeof(struct tga_softc), tgamatch, tgaattach,
67 };
68
69 int tga_identify __P((struct tga_devconfig *));
70 const struct tga_conf *tga_getconf __P((int));
71 void tga_getdevconfig __P((bus_space_tag_t memt, pci_chipset_tag_t pc,
72 pcitag_t tag, struct tga_devconfig *dc));
73
74 struct tga_devconfig tga_console_dc;
75
76 int tga_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
77 int tga_mmap __P((void *, off_t, int));
78 static void tga_copyrows __P((void *, int, int, int));
79 static void tga_copycols __P((void *, int, int, int, int));
80 static int tga_alloc_screen __P((void *, const struct wsscreen_descr *,
81 void **, int *, int *, long *));
82 static void tga_free_screen __P((void *, void *));
83 static int tga_show_screen __P((void *, void *, int,
84 void (*) (void *, int, int), void *));
85 static int tga_rop __P((struct raster *, int, int, int, int, int,
86 struct raster *, int, int));
87 static int tga_rop_nosrc __P((struct raster *, int, int, int, int, int));
88 static int tga_rop_htov __P((struct raster *, int, int, int, int,
89 int, struct raster *, int, int ));
90 static int tga_rop_vtov __P((struct raster *, int, int, int, int,
91 int, struct raster *, int, int ));
92 void tga2_init __P((struct tga_devconfig *, int));
93
94 /* RAMDAC interface functions */
95 int tga_sched_update __P((void *, void (*)(void *)));
96 void tga_ramdac_wr __P((void *, u_int, u_int8_t));
97 u_int8_t tga_ramdac_rd __P((void *, u_int));
98 void tga2_ramdac_wr __P((void *, u_int, u_int8_t));
99 u_int8_t tga2_ramdac_rd __P((void *, u_int));
100
101 /* Interrupt handler */
102 int tga_intr __P((void *));
103
104 struct wsdisplay_emulops tga_emulops = {
105 rcons_cursor, /* could use hardware cursor; punt */
106 rcons_mapchar,
107 rcons_putchar,
108 tga_copycols,
109 rcons_erasecols,
110 tga_copyrows,
111 rcons_eraserows,
112 rcons_alloc_attr
113 };
114
115 struct wsscreen_descr tga_stdscreen = {
116 "std",
117 0, 0, /* will be filled in -- XXX shouldn't, it's global */
118 &tga_emulops,
119 0, 0,
120 WSSCREEN_REVERSE
121 };
122
123 const struct wsscreen_descr *_tga_scrlist[] = {
124 &tga_stdscreen,
125 /* XXX other formats, graphics screen? */
126 };
127
128 struct wsscreen_list tga_screenlist = {
129 sizeof(_tga_scrlist) / sizeof(struct wsscreen_descr *), _tga_scrlist
130 };
131
132 struct wsdisplay_accessops tga_accessops = {
133 tga_ioctl,
134 tga_mmap,
135 tga_alloc_screen,
136 tga_free_screen,
137 tga_show_screen,
138 0 /* load_font */
139 };
140
141 void tga_blank __P((struct tga_devconfig *));
142 void tga_unblank __P((struct tga_devconfig *));
143
144 int
145 tgamatch(parent, match, aux)
146 struct device *parent;
147 struct cfdata *match;
148 void *aux;
149 {
150 struct pci_attach_args *pa = aux;
151
152 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_DEC)
153 return (0);
154
155 switch (PCI_PRODUCT(pa->pa_id)) {
156 case PCI_PRODUCT_DEC_21030:
157 case PCI_PRODUCT_DEC_PBXGB:
158 return 10;
159 default:
160 return 0;
161 }
162 return (0);
163 }
164
165 void
166 tga_getdevconfig(memt, pc, tag, dc)
167 bus_space_tag_t memt;
168 pci_chipset_tag_t pc;
169 pcitag_t tag;
170 struct tga_devconfig *dc;
171 {
172 const struct tga_conf *tgac;
173 struct raster *rap;
174 struct rcons *rcp;
175 bus_size_t pcisize;
176 int i, flags;
177
178 dc->dc_memt = memt;
179
180 dc->dc_pcitag = tag;
181
182 /* XXX magic number */
183 if (pci_mapreg_info(pc, tag, 0x10,
184 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
185 &dc->dc_pcipaddr, &pcisize, &flags))
186 return;
187 if ((flags & BUS_SPACE_MAP_PREFETCHABLE) == 0) /* XXX */
188 panic("tga memory not prefetchable");
189
190 if (bus_space_map(memt, dc->dc_pcipaddr, pcisize,
191 BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR, &dc->dc_memh))
192 return;
193 dc->dc_vaddr = dc->dc_memh; /* XXX Cheat-o-matic */
194 #ifdef __alpha__
195 dc->dc_paddr = ALPHA_K0SEG_TO_PHYS(dc->dc_vaddr); /* XXX */
196 #endif
197
198 bus_space_subregion(dc->dc_memt, dc->dc_memh,
199 TGA_MEM_CREGS, TGA_CREGS_SIZE,
200 &dc->dc_regs);
201 dc->dc_tga_type = tga_identify(dc);
202
203 tgac = dc->dc_tgaconf = tga_getconf(dc->dc_tga_type);
204 if (tgac == NULL)
205 return;
206
207 #if 0
208 /* XXX on the Alpha, pcisize = 4 * cspace_size. */
209 if (tgac->tgac_cspace_size != pcisize) /* sanity */
210 panic("tga_getdevconfig: memory size mismatch?");
211 #endif
212
213 switch (TGARREG(dc, TGA_REG_GREV) & 0xff) {
214 case 0x01:
215 case 0x02:
216 case 0x03:
217 case 0x04:
218 dc->dc_tga2 = 0;
219 break;
220 case 0x20:
221 case 0x21:
222 case 0x22:
223 dc->dc_tga2 = 1;
224 break;
225 default:
226 panic("tga_getdevconfig: TGA Revision not recognized");
227 }
228
229 if (dc->dc_tga2) {
230 int monitor;
231
232 monitor = (~TGARREG(dc, TGA_REG_GREV) >> 16) & 0x0f;
233 tga2_init(dc, monitor);
234 }
235
236 switch (TGARREG(dc, TGA_REG_VHCR) & 0x1ff) { /* XXX */
237 case 0:
238 dc->dc_wid = 8192;
239 break;
240
241 case 1:
242 dc->dc_wid = 8196;
243 break;
244
245 default:
246 dc->dc_wid = (TGARREG(dc, TGA_REG_VHCR) & 0x1ff) * 4; /* XXX */
247 break;
248 }
249
250 dc->dc_rowbytes = dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
251
252 if ((TGARREG(dc, TGA_REG_VHCR) & 0x00000001) != 0 && /* XXX */
253 (TGARREG(dc, TGA_REG_VHCR) & 0x80000000) != 0) { /* XXX */
254 dc->dc_wid -= 4;
255 /*
256 * XXX XXX turning off 'odd' shouldn't be necesssary,
257 * XXX XXX but i can't make X work with the weird size.
258 */
259 TGAWREG(dc, TGA_REG_VHCR, TGARREG(dc, TGA_REG_VHCR) & ~0x80000001);
260 dc->dc_rowbytes =
261 dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
262 }
263
264 dc->dc_ht = (TGARREG(dc, TGA_REG_VVCR) & 0x7ff); /* XXX */
265
266 /* XXX this seems to be what DEC does */
267 TGAWREG(dc, TGA_REG_CCBR, 0);
268 TGAWREG(dc, TGA_REG_VVBR, 1);
269 dc->dc_videobase = dc->dc_vaddr + tgac->tgac_dbuf[0] +
270 1 * tgac->tgac_vvbr_units;
271 dc->dc_blanked = 1;
272 tga_unblank(dc);
273
274 /*
275 * Set all bits in the pixel mask, to enable writes to all pixels.
276 * It seems that the console firmware clears some of them
277 * under some circumstances, which causes cute vertical stripes.
278 */
279 TGAWREG(dc, TGA_REG_GPXR_P, 0xffffffff);
280
281 /* clear the screen */
282 for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(u_int32_t))
283 *(u_int32_t *)(dc->dc_videobase + i) = 0;
284
285 /* initialize the raster */
286 rap = &dc->dc_raster;
287 rap->width = dc->dc_wid;
288 rap->height = dc->dc_ht;
289 rap->depth = tgac->tgac_phys_depth;
290 rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
291 rap->pixels = (u_int32_t *)dc->dc_videobase;
292 rap->data = (caddr_t)dc;
293
294 /* initialize the raster console blitter */
295 rcp = &dc->dc_rcons;
296 rcp->rc_sp = rap;
297 rcp->rc_crow = rcp->rc_ccol = -1;
298 rcp->rc_crowp = &rcp->rc_crow;
299 rcp->rc_ccolp = &rcp->rc_ccol;
300 rcons_init(rcp, 34, 80);
301
302 tga_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
303 tga_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
304 }
305
306 void
307 tgaattach(parent, self, aux)
308 struct device *parent, *self;
309 void *aux;
310 {
311 struct pci_attach_args *pa = aux;
312 struct tga_softc *sc = (struct tga_softc *)self;
313 struct wsemuldisplaydev_attach_args aa;
314 pci_intr_handle_t intrh;
315 const char *intrstr;
316 u_int8_t rev;
317 int console;
318
319 #ifdef __alpha__
320 console = (pa->pa_tag == tga_console_dc.dc_pcitag);
321 #else
322 console = 0;
323 #endif
324 if (console) {
325 sc->sc_dc = &tga_console_dc;
326 sc->nscreens = 1;
327 } else {
328 sc->sc_dc = (struct tga_devconfig *)
329 malloc(sizeof(struct tga_devconfig), M_DEVBUF, M_WAITOK);
330 bzero(sc->sc_dc, sizeof(struct tga_devconfig));
331 tga_getdevconfig(pa->pa_memt, pa->pa_pc, pa->pa_tag,
332 sc->sc_dc);
333 }
334 if (sc->sc_dc->dc_vaddr == NULL) {
335 printf(": couldn't map memory space; punt!\n");
336 return;
337 }
338
339 /* XXX say what's going on. */
340 intrstr = NULL;
341 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
342 pa->pa_intrline, &intrh)) {
343 printf(": couldn't map interrupt");
344 return;
345 }
346 intrstr = pci_intr_string(pa->pa_pc, intrh);
347 sc->sc_intr = pci_intr_establish(pa->pa_pc, intrh, IPL_TTY, tga_intr,
348 sc->sc_dc);
349 if (sc->sc_intr == NULL) {
350 printf(": couldn't establish interrupt");
351 if (intrstr != NULL)
352 printf("at %s", intrstr);
353 printf("\n");
354 return;
355 }
356
357 rev = PCI_REVISION(pa->pa_class);
358 switch (rev) {
359 case 0x1:
360 case 0x2:
361 case 0x3:
362 printf(": DC21030 step %c", 'A' + rev - 1);
363 break;
364 case 0x20:
365 printf(": TGA2 abstract software model");
366 break;
367 case 0x21:
368 case 0x22:
369 printf(": TGA2 pass %d", rev - 0x20);
370 break;
371
372 default:
373 printf("unknown stepping (0x%x)", rev);
374 break;
375 }
376 printf(", ");
377
378 /*
379 * Get RAMDAC function vectors and call the RAMDAC functions
380 * to allocate its private storage and pass that back to us.
381 */
382 sc->sc_dc->dc_ramdac_funcs = bt485_funcs();
383 if (!sc->sc_dc->dc_tga2) {
384 sc->sc_dc->dc_ramdac_cookie = bt485_register(
385 sc->sc_dc, tga_sched_update, tga_ramdac_wr,
386 tga_ramdac_rd);
387 } else {
388 sc->sc_dc->dc_ramdac_cookie = bt485_register(
389 sc->sc_dc, tga_sched_update, tga2_ramdac_wr,
390 tga2_ramdac_rd);
391 }
392
393 /*
394 * Initialize the RAMDAC. Initialization includes disabling
395 * cursor, setting a sane colormap, etc.
396 */
397 (*sc->sc_dc->dc_ramdac_funcs->ramdac_init)(sc->sc_dc->dc_ramdac_cookie);
398 TGAWREG(sc->sc_dc, TGA_REG_SISR, 0x00000001); /* XXX */
399
400 if (sc->sc_dc->dc_tgaconf == NULL) {
401 printf("unknown board configuration\n");
402 return;
403 }
404 printf("board type %s\n", sc->sc_dc->dc_tgaconf->tgac_name);
405 printf("%s: %d x %d, %dbpp, %s RAMDAC\n", sc->sc_dev.dv_xname,
406 sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
407 sc->sc_dc->dc_tgaconf->tgac_phys_depth,
408 sc->sc_dc->dc_ramdac_funcs->ramdac_name);
409
410 if (intrstr != NULL)
411 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
412 intrstr);
413
414 aa.console = console;
415 aa.scrdata = &tga_screenlist;
416 aa.accessops = &tga_accessops;
417 aa.accesscookie = sc;
418
419 config_found(self, &aa, wsemuldisplaydevprint);
420 }
421
422 int
423 tga_ioctl(v, cmd, data, flag, p)
424 void *v;
425 u_long cmd;
426 caddr_t data;
427 int flag;
428 struct proc *p;
429 {
430 struct tga_softc *sc = v;
431 struct tga_devconfig *dc = sc->sc_dc;
432 struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
433 struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
434
435 switch (cmd) {
436 case WSDISPLAYIO_GTYPE:
437 *(u_int *)data = WSDISPLAY_TYPE_TGA;
438 return (0);
439
440 case WSDISPLAYIO_GINFO:
441 #define wsd_fbip ((struct wsdisplay_fbinfo *)data)
442 wsd_fbip->height = sc->sc_dc->dc_ht;
443 wsd_fbip->width = sc->sc_dc->dc_wid;
444 wsd_fbip->depth = sc->sc_dc->dc_tgaconf->tgac_phys_depth;
445 wsd_fbip->cmsize = 256; /* XXX ??? */
446 #undef wsd_fbip
447 return (0);
448
449 case WSDISPLAYIO_GETCMAP:
450 return (*dcrf->ramdac_get_cmap)(dcrc,
451 (struct wsdisplay_cmap *)data);
452
453 case WSDISPLAYIO_PUTCMAP:
454 return (*dcrf->ramdac_set_cmap)(dcrc,
455 (struct wsdisplay_cmap *)data);
456
457 case WSDISPLAYIO_SVIDEO:
458 if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
459 tga_blank(sc->sc_dc);
460 else
461 tga_unblank(sc->sc_dc);
462 return (0);
463
464 case WSDISPLAYIO_GVIDEO:
465 *(u_int *)data = dc->dc_blanked ?
466 WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
467 return (0);
468
469 case WSDISPLAYIO_GCURPOS:
470 return (*dcrf->ramdac_get_curpos)(dcrc,
471 (struct wsdisplay_curpos *)data);
472
473 case WSDISPLAYIO_SCURPOS:
474 return (*dcrf->ramdac_set_curpos)(dcrc,
475 (struct wsdisplay_curpos *)data);
476
477 case WSDISPLAYIO_GCURMAX:
478 return (*dcrf->ramdac_get_curmax)(dcrc,
479 (struct wsdisplay_curpos *)data);
480
481 case WSDISPLAYIO_GCURSOR:
482 return (*dcrf->ramdac_get_cursor)(dcrc,
483 (struct wsdisplay_cursor *)data);
484
485 case WSDISPLAYIO_SCURSOR:
486 return (*dcrf->ramdac_set_cursor)(dcrc,
487 (struct wsdisplay_cursor *)data);
488 }
489 return (-1);
490 }
491
492 int
493 tga_sched_update(v, f)
494 void *v;
495 void (*f) __P((void *));
496 {
497 struct tga_devconfig *dc = v;
498
499 TGAWREG(dc, TGA_REG_SISR, 0x00010000);
500 dc->dc_ramdac_intr = f;
501 return 0;
502 }
503
504 int
505 tga_intr(v)
506 void *v;
507 {
508 struct tga_devconfig *dc = v;
509 struct ramdac_cookie *dcrc= dc->dc_ramdac_cookie;
510
511 if ((TGARREG(dc, TGA_REG_SISR) & 0x00010001) != 0x00010001)
512 return 0;
513 dc->dc_ramdac_intr(dcrc);
514 dc->dc_ramdac_intr = NULL;
515 TGAWREG(dc, TGA_REG_SISR, 0x00000001);
516 return (1);
517 }
518
519 int
520 tga_mmap(v, offset, prot)
521 void *v;
522 off_t offset;
523 int prot;
524 {
525
526 /* XXX NEW MAPPING CODE... */
527
528 #ifdef __alpha__
529 struct tga_softc *sc = v;
530
531 if (offset >= sc->sc_dc->dc_tgaconf->tgac_cspace_size || offset < 0)
532 return -1;
533 return alpha_btop(sc->sc_dc->dc_paddr + offset);
534 #else
535 return (-1);
536 #endif
537 }
538
539 int
540 tga_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
541 void *v;
542 const struct wsscreen_descr *type;
543 void **cookiep;
544 int *curxp, *curyp;
545 long *attrp;
546 {
547 struct tga_softc *sc = v;
548 long defattr;
549
550 if (sc->nscreens > 0)
551 return (ENOMEM);
552
553 *cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
554 *curxp = 0;
555 *curyp = 0;
556 rcons_alloc_attr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
557 *attrp = defattr;
558 sc->nscreens++;
559 return (0);
560 }
561
562 void
563 tga_free_screen(v, cookie)
564 void *v;
565 void *cookie;
566 {
567 struct tga_softc *sc = v;
568
569 if (sc->sc_dc == &tga_console_dc)
570 panic("tga_free_screen: console");
571
572 sc->nscreens--;
573 }
574
575 int
576 tga_show_screen(v, cookie, waitok, cb, cbarg)
577 void *v;
578 void *cookie;
579 int waitok;
580 void (*cb) __P((void *, int, int));
581 void *cbarg;
582 {
583
584 return (0);
585 }
586
587 int
588 tga_cnattach(iot, memt, pc, bus, device, function)
589 bus_space_tag_t iot, memt;
590 pci_chipset_tag_t pc;
591 int bus, device, function;
592 {
593 struct tga_devconfig *dcp = &tga_console_dc;
594 long defattr;
595
596 tga_getdevconfig(memt, pc,
597 pci_make_tag(pc, bus, device, function), dcp);
598
599 /* sanity checks */
600 if (dcp->dc_vaddr == NULL)
601 panic("tga_console(%d, %d): couldn't map memory space",
602 device, function);
603 if (dcp->dc_tgaconf == NULL)
604 panic("tga_console(%d, %d): unknown board configuration",
605 device, function);
606
607 /*
608 * Initialize the RAMDAC but DO NOT allocate any private storage.
609 * Initialization includes disabling cursor, setting a sane
610 * colormap, etc. It will be reinitialized in tgaattach().
611 */
612
613 /* XXX -- this only works for bt485, but then we only support that,
614 * currently.
615 */
616 if (dcp->dc_tga2)
617 bt485_cninit(dcp, tga_sched_update, tga2_ramdac_wr,
618 tga2_ramdac_rd);
619 else
620 bt485_cninit(dcp, tga_sched_update, tga_ramdac_wr,
621 tga_ramdac_rd);
622
623 rcons_alloc_attr(&dcp->dc_rcons, 0, 0, 0, &defattr);
624
625 wsdisplay_cnattach(&tga_stdscreen, &dcp->dc_rcons,
626 0, 0, defattr);
627
628 return(0);
629 }
630
631 /*
632 * Functions to blank and unblank the display.
633 */
634 void
635 tga_blank(dc)
636 struct tga_devconfig *dc;
637 {
638
639 if (!dc->dc_blanked) {
640 dc->dc_blanked = 1;
641 /* XXX */
642 TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | VVR_BLANK);
643 }
644 }
645
646 void
647 tga_unblank(dc)
648 struct tga_devconfig *dc;
649 {
650
651 if (dc->dc_blanked) {
652 dc->dc_blanked = 0;
653 /* XXX */
654 TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~VVR_BLANK);
655 }
656 }
657
658 /*
659 * Functions to manipulate the built-in cursor handing hardware.
660 */
661 int
662 tga_builtin_set_cursor(dc, cursorp)
663 struct tga_devconfig *dc;
664 struct wsdisplay_cursor *cursorp;
665 {
666 struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
667 struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
668 int count, error, v;
669
670 v = cursorp->which;
671 if (v & WSDISPLAY_CURSOR_DOCMAP) {
672 error = dcrf->ramdac_check_curcmap(dcrc, cursorp);
673 if (error)
674 return (error);
675 }
676 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
677 if ((u_int)cursorp->size.x != 64 ||
678 (u_int)cursorp->size.y > 64)
679 return (EINVAL);
680 /* The cursor is 2 bits deep, and there is no mask */
681 count = (cursorp->size.y * 64 * 2) / NBBY;
682 if (!uvm_useracc(cursorp->image, count, B_READ))
683 return (EFAULT);
684 }
685 if (v & WSDISPLAY_CURSOR_DOHOT) /* not supported */
686 return EINVAL;
687
688 /* parameters are OK; do it */
689 if (v & WSDISPLAY_CURSOR_DOCUR) {
690 if (cursorp->enable)
691 /* XXX */
692 TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 0x04);
693 else
694 /* XXX */
695 TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~0x04);
696 }
697 if (v & WSDISPLAY_CURSOR_DOPOS) {
698 TGAWREG(dc, TGA_REG_CXYR,
699 ((cursorp->pos.y & 0xfff) << 12) | (cursorp->pos.x & 0xfff));
700 }
701 if (v & WSDISPLAY_CURSOR_DOCMAP) {
702 /* can't fail. */
703 dcrf->ramdac_set_curcmap(dcrc, cursorp);
704 }
705 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
706 count = ((64 * 2) / NBBY) * cursorp->size.y;
707 TGAWREG(dc, TGA_REG_CCBR,
708 (TGARREG(dc, TGA_REG_CCBR) & ~0xfc00) | (cursorp->size.y << 10));
709 copyin(cursorp->image, (char *)(dc->dc_vaddr +
710 (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
711 count); /* can't fail. */
712 }
713 return (0);
714 }
715
716 int
717 tga_builtin_get_cursor(dc, cursorp)
718 struct tga_devconfig *dc;
719 struct wsdisplay_cursor *cursorp;
720 {
721 struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
722 struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
723 int count, error;
724
725 cursorp->which = WSDISPLAY_CURSOR_DOALL &
726 ~(WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOCMAP);
727 cursorp->enable = (TGARREG(dc, TGA_REG_VVVR) & 0x04) != 0;
728 cursorp->pos.x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
729 cursorp->pos.y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
730 cursorp->size.x = 64;
731 cursorp->size.y = (TGARREG(dc, TGA_REG_CCBR) >> 10) & 0x3f;
732
733 if (cursorp->image != NULL) {
734 count = (cursorp->size.y * 64 * 2) / NBBY;
735 error = copyout((char *)(dc->dc_vaddr +
736 (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
737 cursorp->image, count);
738 if (error)
739 return (error);
740 /* No mask */
741 }
742 error = dcrf->ramdac_get_curcmap(dcrc, cursorp);
743 return (error);
744 }
745
746 int
747 tga_builtin_set_curpos(dc, curposp)
748 struct tga_devconfig *dc;
749 struct wsdisplay_curpos *curposp;
750 {
751
752 TGAWREG(dc, TGA_REG_CXYR,
753 ((curposp->y & 0xfff) << 12) | (curposp->x & 0xfff));
754 return (0);
755 }
756
757 int
758 tga_builtin_get_curpos(dc, curposp)
759 struct tga_devconfig *dc;
760 struct wsdisplay_curpos *curposp;
761 {
762
763 curposp->x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
764 curposp->y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
765 return (0);
766 }
767
768 int
769 tga_builtin_get_curmax(dc, curposp)
770 struct tga_devconfig *dc;
771 struct wsdisplay_curpos *curposp;
772 {
773
774 curposp->x = curposp->y = 64;
775 return (0);
776 }
777
778 /*
779 * Copy columns (characters) in a row (line).
780 */
781 void
782 tga_copycols(id, row, srccol, dstcol, ncols)
783 void *id;
784 int row, srccol, dstcol, ncols;
785 {
786 struct rcons *rc = id;
787 int y, srcx, dstx, nx;
788
789 y = rc->rc_yorigin + rc->rc_font->height * row;
790 srcx = rc->rc_xorigin + rc->rc_font->width * srccol;
791 dstx = rc->rc_xorigin + rc->rc_font->width * dstcol;
792 nx = rc->rc_font->width * ncols;
793
794 tga_rop(rc->rc_sp, dstx, y,
795 nx, rc->rc_font->height, RAS_SRC,
796 rc->rc_sp, srcx, y);
797 }
798
799 /*
800 * Copy rows (lines).
801 */
802 void
803 tga_copyrows(id, srcrow, dstrow, nrows)
804 void *id;
805 int srcrow, dstrow, nrows;
806 {
807 struct rcons *rc = id;
808 int srcy, dsty, ny;
809
810 srcy = rc->rc_yorigin + rc->rc_font->height * srcrow;
811 dsty = rc->rc_yorigin + rc->rc_font->height * dstrow;
812 ny = rc->rc_font->height * nrows;
813
814 tga_rop(rc->rc_sp, rc->rc_xorigin, dsty,
815 rc->rc_raswidth, ny, RAS_SRC,
816 rc->rc_sp, rc->rc_xorigin, srcy);
817 }
818
819 /* Do we need the src? */
820 static int needsrc[16] = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 };
821
822 /* A mapping between our API and the TGA card */
823 static int map_rop[16] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6,
824 0xe, 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
825 };
826
827 /*
828 * Generic TGA raster op.
829 * This covers all possible raster ops, and
830 * clips the sizes and all of that.
831 */
832 static int
833 tga_rop(dst, dx, dy, w, h, rop, src, sx, sy)
834 struct raster *dst;
835 int dx, dy, w, h, rop;
836 struct raster *src;
837 int sx, sy;
838 {
839 if (!dst)
840 return -1;
841 if (dst->data == NULL)
842 return -1; /* we should be writing to a screen */
843 if (needsrc[RAS_GETOP(rop)]) {
844 if (src == (struct raster *) 0)
845 return -1; /* We want a src */
846 /* Clip against src */
847 if (sx < 0) {
848 w += sx;
849 sx = 0;
850 }
851 if (sy < 0) {
852 h += sy;
853 sy = 0;
854 }
855 if (sx + w > src->width)
856 w = src->width - sx;
857 if (sy + h > src->height)
858 h = src->height - sy;
859 } else {
860 if (src != (struct raster *) 0)
861 return -1; /* We need no src */
862 }
863 /* Clip against dst. We modify src regardless of using it,
864 * since it really doesn't matter.
865 */
866 if (dx < 0) {
867 w += dx;
868 sx -= dx;
869 dx = 0;
870 }
871 if (dy < 0) {
872 h += dy;
873 sy -= dy;
874 dy = 0;
875 }
876 if (dx + w > dst->width)
877 w = dst->width - dx;
878 if (dy + h > dst->height)
879 h = dst->height - dy;
880 if (w <= 0 || h <= 0)
881 return 0; /* Vacuously true; */
882 if (!src)
883 return tga_rop_nosrc(dst, dx, dy, w, h, rop);
884 if (src->data == NULL)
885 return tga_rop_htov(dst, dx, dy, w, h, rop, src, sx, sy);
886 else
887 return tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy);
888 }
889
890 /*
891 * No source raster ops.
892 * This function deals with all raster ops that don't require a src.
893 */
894 static int
895 tga_rop_nosrc(dst, dx, dy, w, h, rop)
896 struct raster *dst;
897 int dx, dy, w, h, rop;
898 {
899 return raster_op(dst, dx, dy, w, h, rop, NULL, 0, 0);
900 }
901
902 /*
903 * Host to Video raster ops.
904 * This function deals with all raster ops that have a src that is host memory.
905 */
906 static int
907 tga_rop_htov(dst, dx, dy, w, h, rop, src, sx, sy)
908 struct raster *dst;
909 int dx, dy, w, h, rop;
910 struct raster *src;
911 int sx, sy;
912 {
913 return raster_op(dst, dx, dy, w, h, rop, src, sx, sy);
914 }
915
916 /*
917 * Video to Video raster ops.
918 * This function deals with all raster ops that have a src and dst
919 * that are on the card.
920 */
921 static int
922 tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy)
923 struct raster *dst;
924 int dx, dy, w, h, rop;
925 struct raster *src;
926 int sx, sy;
927 {
928 struct tga_devconfig *dc = (struct tga_devconfig *)dst->data;
929 int srcb, dstb;
930 int x, y;
931 int xstart, xend, xdir, xinc;
932 int ystart, yend, ydir, yinc;
933 int offset = 1 * dc->dc_tgaconf->tgac_vvbr_units;
934
935 /*
936 * I don't yet want to deal with unaligned guys, really. And we don't
937 * deal with copies from one card to another.
938 */
939 if (dx % 8 != 0 || sx % 8 != 0 || src != dst)
940 return raster_op(dst, dx, dy, w, h, rop, src, sx, sy);
941
942 if (sy >= dy) {
943 ystart = 0;
944 yend = h;
945 ydir = 1;
946 } else {
947 ystart = h;
948 yend = 0;
949 ydir = -1;
950 }
951 if (sx >= dx) {
952 xstart = 0;
953 xend = w * (dst->depth / 8);
954 xdir = 1;
955 } else {
956 xstart = w * (dst->depth / 8);
957 xend = 0;
958 xdir = -1;
959 }
960 xinc = xdir * 4 * 64;
961 yinc = ydir * dst->linelongs * 4;
962 ystart *= dst->linelongs * 4;
963 yend *= dst->linelongs * 4;
964 srcb = offset + sy * src->linelongs * 4 + sx;
965 dstb = offset + dy * dst->linelongs * 4 + dx;
966 TGAWALREG(dc, TGA_REG_GMOR, 3, 0x0007); /* Copy mode */
967 TGAWALREG(dc, TGA_REG_GOPR, 3, map_rop[rop]); /* Set up the op */
968 for (y = ystart; (ydir * y) < (ydir * yend); y += yinc) {
969 for (x = xstart; (xdir * x) < (xdir * xend); x += xinc) {
970 /* XXX XXX Eight writes to different addresses should fill
971 * XXX XXX up the write buffers on 21064 and 21164 chips,
972 * XXX XXX but later CPUs might have larger write buffers which
973 * XXX XXX require further unrolling of this loop, or the
974 * XXX XXX insertion of memory barriers.
975 */
976 TGAWALREG(dc, TGA_REG_GCSR, 0, srcb + y + x + 3 * 64);
977 TGAWALREG(dc, TGA_REG_GCDR, 0, dstb + y + x + 3 * 64);
978 TGAWALREG(dc, TGA_REG_GCSR, 1, srcb + y + x + 2 * 64);
979 TGAWALREG(dc, TGA_REG_GCDR, 1, dstb + y + x + 2 * 64);
980 TGAWALREG(dc, TGA_REG_GCSR, 2, srcb + y + x + 1 * 64);
981 TGAWALREG(dc, TGA_REG_GCDR, 2, dstb + y + x + 1 * 64);
982 TGAWALREG(dc, TGA_REG_GCSR, 3, srcb + y + x + 0 * 64);
983 TGAWALREG(dc, TGA_REG_GCDR, 3, dstb + y + x + 0 * 64);
984 }
985 }
986 TGAWALREG(dc, TGA_REG_GOPR, 0, 0x0003); /* op -> dst = src */
987 TGAWALREG(dc, TGA_REG_GMOR, 0, 0x0000); /* Simple mode */
988 return 0;
989 }
990
991 void
992 tga_ramdac_wr(v, btreg, val)
993 void *v;
994 u_int btreg;
995 u_int8_t val;
996 {
997 struct tga_devconfig *dc = v;
998
999 if (btreg > BT485_REG_MAX)
1000 panic("tga_ramdac_wr: reg %d out of range\n", btreg);
1001
1002 TGAWREG(dc, TGA_REG_EPDR, (btreg << 9) | (0 << 8 ) | val); /* XXX */
1003 TGAREGWB(dc, TGA_REG_EPDR, 1);
1004 }
1005
1006 void
1007 tga2_ramdac_wr(v, btreg, val)
1008 void *v;
1009 u_int btreg;
1010 u_int8_t val;
1011 {
1012 struct tga_devconfig *dc = v;
1013 bus_space_handle_t ramdac;
1014
1015 if (btreg > BT485_REG_MAX)
1016 panic("tga_ramdac_wr: reg %d out of range\n", btreg);
1017
1018 bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
1019 (0xe << 12) + (btreg << 8), 4, &ramdac);
1020 bus_space_write_4(dc->dc_memt, ramdac, 0, val & 0xff);
1021 bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_WRITE);
1022 }
1023
1024 u_int8_t
1025 tga_ramdac_rd(v, btreg)
1026 void *v;
1027 u_int btreg;
1028 {
1029 struct tga_devconfig *dc = v;
1030 tga_reg_t rdval;
1031
1032 if (btreg > BT485_REG_MAX)
1033 panic("tga_ramdac_rd: reg %d out of range\n", btreg);
1034
1035 TGAWREG(dc, TGA_REG_EPSR, (btreg << 1) | 0x1); /* XXX */
1036 TGAREGWB(dc, TGA_REG_EPSR, 1);
1037
1038 rdval = TGARREG(dc, TGA_REG_EPDR);
1039 return (rdval >> 16) & 0xff; /* XXX */
1040 }
1041
1042 u_int8_t
1043 tga2_ramdac_rd(v, btreg)
1044 void *v;
1045 u_int btreg;
1046 {
1047 struct tga_devconfig *dc = v;
1048 bus_space_handle_t ramdac;
1049 u_int8_t retval;
1050
1051 if (btreg > BT485_REG_MAX)
1052 panic("tga_ramdac_rd: reg %d out of range\n", btreg);
1053
1054 bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
1055 (0xe << 12) + (btreg << 8), 4, &ramdac);
1056 retval = bus_space_read_4(dc->dc_memt, ramdac, 0) & 0xff;
1057 bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_READ);
1058 return retval;
1059 }
1060
1061 #include <dev/ic/decmonitors.c>
1062 void tga2_ics9110_wr __P((
1063 struct tga_devconfig *dc,
1064 int dotclock
1065 ));
1066
1067 void
1068 tga2_init(dc, m)
1069 struct tga_devconfig *dc;
1070 int m;
1071 {
1072
1073 tga2_ics9110_wr(dc, decmonitors[m].dotclock);
1074 #if 0
1075 TGAWREG(dc, TGA_REG_VHCR,
1076 ((decmonitors[m].hbp / 4) << 21) |
1077 ((decmonitors[m].hsync / 4) << 14) |
1078 (((decmonitors[m].hfp - 4) / 4) << 9) |
1079 ((decmonitors[m].cols + 4) / 4));
1080 #else
1081 TGAWREG(dc, TGA_REG_VHCR,
1082 ((decmonitors[m].hbp / 4) << 21) |
1083 ((decmonitors[m].hsync / 4) << 14) |
1084 (((decmonitors[m].hfp) / 4) << 9) |
1085 ((decmonitors[m].cols) / 4));
1086 #endif
1087 TGAWREG(dc, TGA_REG_VVCR,
1088 (decmonitors[m].vbp << 22) |
1089 (decmonitors[m].vsync << 16) |
1090 (decmonitors[m].vfp << 11) |
1091 (decmonitors[m].rows));
1092 TGAWREG(dc, TGA_REG_VVBR, 1);
1093 TGAREGRWB(dc, TGA_REG_VHCR, 3);
1094 TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 1);
1095 TGAREGRWB(dc, TGA_REG_VVVR, 1);
1096 TGAWREG(dc, TGA_REG_GPMR, 0xffffffff);
1097 TGAREGRWB(dc, TGA_REG_GPMR, 1);
1098 }
1099
1100 void
1101 tga2_ics9110_wr(dc, dotclock)
1102 struct tga_devconfig *dc;
1103 int dotclock;
1104 {
1105 bus_space_handle_t clock;
1106 u_int32_t valU;
1107 int N, M, R, V, X;
1108 int i;
1109
1110 switch (dotclock) {
1111 case 130808000:
1112 N = 0x40; M = 0x7; V = 0x0; X = 0x1; R = 0x1; break;
1113 case 119840000:
1114 N = 0x2d; M = 0x2b; V = 0x1; X = 0x1; R = 0x1; break;
1115 case 108180000:
1116 N = 0x11; M = 0x9; V = 0x1; X = 0x1; R = 0x2; break;
1117 case 103994000:
1118 N = 0x6d; M = 0xf; V = 0x0; X = 0x1; R = 0x1; break;
1119 case 175000000:
1120 N = 0x5F; M = 0x3E; V = 0x1; X = 0x1; R = 0x1; break;
1121 case 75000000:
1122 N = 0x6e; M = 0x15; V = 0x0; X = 0x1; R = 0x1; break;
1123 case 74000000:
1124 N = 0x2a; M = 0x41; V = 0x1; X = 0x1; R = 0x1; break;
1125 case 69000000:
1126 N = 0x35; M = 0xb; V = 0x0; X = 0x1; R = 0x1; break;
1127 case 65000000:
1128 N = 0x6d; M = 0x0c; V = 0x0; X = 0x1; R = 0x2; break;
1129 case 50000000:
1130 N = 0x37; M = 0x3f; V = 0x1; X = 0x1; R = 0x2; break;
1131 case 40000000:
1132 N = 0x5f; M = 0x11; V = 0x0; X = 0x1; R = 0x2; break;
1133 case 31500000:
1134 N = 0x16; M = 0x05; V = 0x0; X = 0x1; R = 0x2; break;
1135 case 25175000:
1136 N = 0x66; M = 0x1d; V = 0x0; X = 0x1; R = 0x2; break;
1137 case 135000000:
1138 N = 0x42; M = 0x07; V = 0x0; X = 0x1; R = 0x1; break;
1139 case 110000000:
1140 N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
1141 case 202500000:
1142 N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
1143 default:
1144 panic("unrecognized clock rate %d\n", dotclock);
1145 }
1146
1147 /* XXX -- hard coded, bad */
1148 valU = N | ( M << 7 ) | (V << 14);
1149 valU |= (X << 15) | (R << 17);
1150 valU |= 0x17 << 19;
1151
1152 bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
1153 TGA2_MEM_CLOCK + (0xe << 12), 4, &clock); /* XXX */
1154
1155 for (i=24; i>0; i--) {
1156 u_int32_t writeval;
1157
1158 writeval = valU & 0x1;
1159 if (i == 1)
1160 writeval |= 0x2;
1161 valU >>= 1;
1162 bus_space_write_4(dc->dc_memt, clock, 0, writeval);
1163 bus_space_barrier(dc->dc_memt, clock, 0, 4, BUS_SPACE_BARRIER_WRITE);
1164 }
1165 bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
1166 TGA2_MEM_CLOCK + (0xe << 12) + (0x1 << 11) + (0x1 << 11), 4,
1167 &clock); /* XXX */
1168 bus_space_write_4(dc->dc_memt, clock, 0, 0x0);
1169 bus_space_barrier(dc->dc_memt, clock, 0, 0, BUS_SPACE_BARRIER_WRITE);
1170 }
1171