tga.c revision 1.70 1 /* $NetBSD: tga.c,v 1.70 2009/03/14 15:36:19 dsl 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/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: tga.c,v 1.70 2009/03/14 15:36:19 dsl Exp $");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/conf.h>
38 #include <sys/malloc.h>
39 #include <sys/buf.h>
40 #include <sys/ioctl.h>
41
42 #include <sys/bus.h>
43 #include <sys/intr.h>
44
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcidevs.h>
48 #include <dev/pci/pciio.h>
49 #include <dev/pci/tgareg.h>
50 #include <dev/pci/tgavar.h>
51 #include <dev/ic/bt485reg.h>
52 #include <dev/ic/bt485var.h>
53 #include <dev/ic/bt463reg.h>
54 #include <dev/ic/bt463var.h>
55 #include <dev/ic/ibm561var.h>
56
57 #include <dev/wscons/wsconsio.h>
58 #include <dev/wscons/wscons_raster.h>
59 #include <dev/rasops/rasops.h>
60 #include <dev/wsfont/wsfont.h>
61 #include <uvm/uvm_extern.h>
62
63 int tgamatch(struct device *, struct cfdata *, void *);
64 void tgaattach(struct device *, struct device *, void *);
65 int tgaprint(void *, const char *);
66
67 CFATTACH_DECL(tga, sizeof(struct tga_softc),
68 tgamatch, tgaattach, NULL, NULL);
69
70 static void tga_init(bus_space_tag_t memt, pci_chipset_tag_t pc,
71 pcitag_t tag, struct tga_devconfig *dc);
72
73 static int tga_matchcommon(bus_space_tag_t, pci_chipset_tag_t, pcitag_t);
74 static void tga_mapaddrs(bus_space_tag_t memt, pci_chipset_tag_t pc,
75 pcitag_t, bus_size_t *pcisize, struct tga_devconfig *dc);
76 unsigned tga_getdotclock(struct tga_devconfig *dc);
77
78 struct tga_devconfig tga_console_dc;
79
80 int tga_ioctl(void *, void *, u_long, void *, int, struct lwp *);
81 paddr_t tga_mmap(void *, void *, off_t, int);
82 static void tga_copyrows(void *, int, int, int);
83 static void tga_copycols(void *, int, int, int, int);
84 static int tga_alloc_screen(void *, const struct wsscreen_descr *,
85 void **, int *, int *, long *);
86 static void tga_free_screen(void *, void *);
87 static int tga_show_screen(void *, void *, int,
88 void (*) (void *, int, int), void *);
89 static int tga_rop(struct rasops_info *, int, int, int, int, int,
90 struct rasops_info *, int, int);
91 static int tga_rop_vtov(struct rasops_info *, int, int, int, int,
92 int, struct rasops_info *, int, int);
93 static void tga_putchar(void *c, int row, int col,
94 u_int uc, long attr);
95 static void tga_eraserows(void *, int, int, long);
96 static void tga_erasecols(void *, int, int, int, long);
97 void tga2_init(struct tga_devconfig *);
98
99 static void tga_config_interrupts(struct device *);
100
101 /* RAMDAC interface functions */
102 static int tga_sched_update(void *, void (*)(void *));
103 static void tga_ramdac_wr(void *, u_int, u_int8_t);
104 static u_int8_t tga_ramdac_rd(void *, u_int);
105 static void tga_bt463_wr(void *, u_int, u_int8_t);
106 static u_int8_t tga_bt463_rd(void *, u_int);
107 static void tga2_ramdac_wr(void *, u_int, u_int8_t);
108 static u_int8_t tga2_ramdac_rd(void *, u_int);
109
110 /* Interrupt handler */
111 static int tga_intr(void *);
112
113 /* The NULL entries will get filled in by rasops_init().
114 * XXX and the non-NULL ones will be overwritten; reset after calling it.
115 */
116 struct wsdisplay_emulops tga_emulops = {
117 NULL,
118 NULL,
119 tga_putchar,
120 tga_copycols,
121 tga_erasecols,
122 tga_copyrows,
123 tga_eraserows,
124 NULL,
125 NULL,
126 };
127
128 struct wsscreen_descr tga_stdscreen = {
129 "std",
130 0, 0, /* will be filled in -- XXX shouldn't, it's global */
131 &tga_emulops,
132 0, 0,
133 WSSCREEN_REVERSE,
134 NULL,
135 };
136
137 const struct wsscreen_descr *_tga_scrlist[] = {
138 &tga_stdscreen,
139 /* XXX other formats, graphics screen? */
140 };
141
142 struct wsscreen_list tga_screenlist = {
143 sizeof(_tga_scrlist) / sizeof(struct wsscreen_descr *), _tga_scrlist
144 };
145
146 struct wsdisplay_accessops tga_accessops = {
147 tga_ioctl,
148 tga_mmap,
149 tga_alloc_screen,
150 tga_free_screen,
151 tga_show_screen,
152 NULL, /* load_font */
153 NULL,
154 NULL,
155 };
156
157 static void tga_blank(struct tga_devconfig *);
158 static void tga_unblank(struct tga_devconfig *);
159
160 int
161 tga_cnmatch(iot, memt, pc, tag)
162 bus_space_tag_t iot, memt;
163 pci_chipset_tag_t pc;
164 pcitag_t tag;
165 {
166 return tga_matchcommon(memt, pc, tag);
167 }
168
169 int
170 tgamatch(struct device *parent, struct cfdata *match, void *aux)
171 {
172 struct pci_attach_args *pa = aux;
173
174 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_DEC)
175 return (0);
176
177 switch (PCI_PRODUCT(pa->pa_id)) {
178 case PCI_PRODUCT_DEC_21030:
179 case PCI_PRODUCT_DEC_PBXGB:
180 break;
181 default:
182 return 0;
183 }
184
185 #if defined(__alpha__) || defined(arc)
186 /* short-circuit the following test, as we
187 * already have the memory mapped and hence
188 * cannot perform it---and we are the console
189 * anyway.
190 */
191 if (pa->pa_tag == tga_console_dc.dc_pcitag)
192 return 10;
193 #endif
194 return tga_matchcommon(pa->pa_memt, pa->pa_pc, pa->pa_tag);
195 }
196
197 static int
198 tga_matchcommon(bus_space_tag_t memt, pci_chipset_tag_t pc, pcitag_t tag)
199 {
200 struct tga_devconfig tmp_dc;
201 struct tga_devconfig *dc = &tmp_dc;
202 bus_size_t pcisize;
203
204 tga_mapaddrs(memt, pc, tag, &pcisize, dc);
205 dc->dc_tga_type = tga_identify(dc);
206
207 dc->dc_tgaconf = tga_getconf(dc->dc_tga_type);
208 bus_space_unmap(memt, dc->dc_memh, pcisize);
209 if (dc->dc_tgaconf)
210 return 10;
211 return 0;
212 }
213
214 static void
215 tga_mapaddrs(bus_space_tag_t memt, pci_chipset_tag_t pc, pcitag_t tag, bus_size_t *pcisize, struct tga_devconfig *dc)
216 {
217 int flags;
218
219 dc->dc_memt = memt;
220 dc->dc_tgaconf = NULL;
221
222 /* XXX magic number */
223 if (pci_mapreg_info(pc, tag, 0x10,
224 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
225 &dc->dc_pcipaddr, pcisize, &flags))
226 panic("tga_mapaddrs: pci_mapreg_info() failed");
227 if ((flags & BUS_SPACE_MAP_PREFETCHABLE) == 0) /* XXX */
228 panic("tga memory not prefetchable");
229
230 if (bus_space_map(memt, dc->dc_pcipaddr, *pcisize,
231 BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR, &dc->dc_memh))
232 panic("tga_mapaddrs: could not map TGA address space");
233 dc->dc_vaddr = (vaddr_t) bus_space_vaddr(memt, dc->dc_memh);
234
235 bus_space_subregion(dc->dc_memt, dc->dc_memh,
236 TGA_MEM_CREGS, TGA_CREGS_SIZE,
237 &dc->dc_regs);
238 }
239
240 static void
241 tga_init(bus_space_tag_t memt, pci_chipset_tag_t pc, pcitag_t tag, struct tga_devconfig *dc)
242 {
243 const struct tga_conf *tgac;
244 struct rasops_info *rip;
245 int cookie;
246 bus_size_t pcisize;
247 int i;
248
249 dc->dc_pc = pc;
250 dc->dc_pcitag = tag;
251 tga_mapaddrs(memt, pc, tag, &pcisize, dc);
252 dc->dc_tga_type = tga_identify(dc);
253 tgac = dc->dc_tgaconf = tga_getconf(dc->dc_tga_type);
254 #if 0
255 /* XXX on the Alpha, pcisize = 4 * cspace_size. */
256 if (tgac->tgac_cspace_size != pcisize) /* sanity */
257 panic("tga_init: memory size mismatch?");
258 #endif
259
260 switch (TGARREG(dc, TGA_REG_GREV) & 0xff) {
261 case 0x01:
262 case 0x02:
263 case 0x03:
264 case 0x04:
265 dc->dc_tga2 = 0;
266 break;
267 case 0x20:
268 case 0x21:
269 case 0x22:
270 dc->dc_tga2 = 1;
271 break;
272 default:
273 panic("tga_init: TGA Revision not recognized");
274 }
275
276 if (dc->dc_tga2)
277 tga2_init(dc);
278
279 switch (TGARREG(dc, TGA_REG_VHCR) & 0x1ff) { /* XXX */
280 case 0:
281 dc->dc_wid = 8192;
282 break;
283
284 case 1:
285 dc->dc_wid = 8196;
286 break;
287
288 default:
289 dc->dc_wid = (TGARREG(dc, TGA_REG_VHCR) & 0x1ff) * 4; /* XXX */
290 break;
291 }
292
293 /*
294 * XXX XXX Turning off "odd" shouldn't be necessary,
295 * XXX XXX but I can't make X work with the weird size.
296 */
297 if ((TGARREG(dc, TGA_REG_VHCR) & 0x00000001) != 0 && /* XXX */
298 (TGARREG(dc, TGA_REG_VHCR) & 0x80000000) != 0) { /* XXX */
299 TGAWREG(dc, TGA_REG_VHCR,
300 (TGARREG(dc, TGA_REG_VHCR) & ~0x80000001));
301 dc->dc_wid -= 4;
302 }
303
304 dc->dc_rowbytes = dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
305 dc->dc_ht = (TGARREG(dc, TGA_REG_VVCR) & 0x7ff); /* XXX */
306
307 /* XXX this seems to be what DEC does */
308 TGAWREG(dc, TGA_REG_CCBR, 0);
309 TGAWREG(dc, TGA_REG_VVBR, 1);
310 dc->dc_videobase = dc->dc_vaddr + tgac->tgac_dbuf[0] +
311 1 * tgac->tgac_vvbr_units;
312 dc->dc_blanked = 1;
313 tga_unblank(dc);
314
315 /*
316 * Set all bits in the pixel mask, to enable writes to all pixels.
317 * It seems that the console firmware clears some of them
318 * under some circumstances, which causes cute vertical stripes.
319 */
320 TGAWREG(dc, TGA_REG_GPXR_P, 0xffffffff);
321
322 /* clear the screen */
323 for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(u_int32_t))
324 *(u_int32_t *)(dc->dc_videobase + i) = 0;
325
326 /* Initialize rasops descriptor */
327 rip = &dc->dc_rinfo;
328 rip->ri_flg = RI_CENTER;
329 rip->ri_depth = tgac->tgac_phys_depth;
330 rip->ri_bits = (void *)dc->dc_videobase;
331 rip->ri_width = dc->dc_wid;
332 rip->ri_height = dc->dc_ht;
333 rip->ri_stride = dc->dc_rowbytes;
334 rip->ri_hw = dc;
335
336 if (tgac->tgac_phys_depth == 32) {
337 rip->ri_rnum = 8;
338 rip->ri_gnum = 8;
339 rip->ri_bnum = 8;
340 rip->ri_rpos = 16;
341 rip->ri_gpos = 8;
342 rip->ri_bpos = 0;
343 }
344
345 wsfont_init();
346 /* prefer 8 pixel wide font */
347 cookie = wsfont_find(NULL, 8, 0, 0, WSDISPLAY_FONTORDER_R2L,
348 WSDISPLAY_FONTORDER_L2R);
349 if (cookie <= 0)
350 cookie = wsfont_find(NULL, 0, 0, 0, WSDISPLAY_FONTORDER_R2L,
351 WSDISPLAY_FONTORDER_L2R);
352 if (cookie <= 0) {
353 printf("tga: no appropriate fonts.\n");
354 return;
355 }
356
357 /* the accelerated tga_putchar() needs LSbit left */
358 if (wsfont_lock(cookie, &dc->dc_rinfo.ri_font)) {
359 printf("tga: couldn't lock font\n");
360 return;
361 }
362 dc->dc_rinfo.ri_wsfcookie = cookie;
363
364 rasops_init(rip, 34, 80);
365
366 /* add our accelerated functions */
367 /* XXX shouldn't have to do this; rasops should leave non-NULL
368 * XXX entries alone.
369 */
370 dc->dc_rinfo.ri_ops.copyrows = tga_copyrows;
371 dc->dc_rinfo.ri_ops.eraserows = tga_eraserows;
372 dc->dc_rinfo.ri_ops.erasecols = tga_erasecols;
373 dc->dc_rinfo.ri_ops.copycols = tga_copycols;
374 dc->dc_rinfo.ri_ops.putchar = tga_putchar;
375
376 tga_stdscreen.nrows = dc->dc_rinfo.ri_rows;
377 tga_stdscreen.ncols = dc->dc_rinfo.ri_cols;
378 tga_stdscreen.textops = &dc->dc_rinfo.ri_ops;
379 tga_stdscreen.capabilities = dc->dc_rinfo.ri_caps;
380
381
382 dc->dc_intrenabled = 0;
383 }
384
385 void
386 tgaattach(parent, self, aux)
387 struct device *parent, *self;
388 void *aux;
389 {
390 struct pci_attach_args *pa = aux;
391 struct tga_softc *sc = (struct tga_softc *)self;
392 struct wsemuldisplaydev_attach_args aa;
393 pci_intr_handle_t intrh;
394 const char *intrstr;
395 u_int8_t rev;
396 int console;
397
398 #if defined(__alpha__) || defined(arc)
399 console = (pa->pa_tag == tga_console_dc.dc_pcitag);
400 #else
401 console = 0;
402 #endif
403 if (console) {
404 sc->sc_dc = &tga_console_dc;
405 sc->nscreens = 1;
406 } else {
407 sc->sc_dc = (struct tga_devconfig *)
408 malloc(sizeof(struct tga_devconfig), M_DEVBUF,
409 M_WAITOK|M_ZERO);
410 tga_init(pa->pa_memt, pa->pa_pc, pa->pa_tag, sc->sc_dc);
411 }
412 if (sc->sc_dc->dc_vaddr == 0) {
413 printf(": couldn't map memory space; punt!\n");
414 return;
415 }
416
417 /* XXX say what's going on. */
418 intrstr = NULL;
419 if (pci_intr_map(pa, &intrh)) {
420 printf(": couldn't map interrupt");
421 return;
422 }
423 intrstr = pci_intr_string(pa->pa_pc, intrh);
424 sc->sc_intr = pci_intr_establish(pa->pa_pc, intrh, IPL_TTY, tga_intr,
425 sc->sc_dc);
426 if (sc->sc_intr == NULL) {
427 printf(": couldn't establish interrupt");
428 if (intrstr != NULL)
429 printf("at %s", intrstr);
430 printf("\n");
431 return;
432 }
433
434 rev = PCI_REVISION(pa->pa_class);
435 switch (rev) {
436 case 0x1:
437 case 0x2:
438 case 0x3:
439 printf(": DC21030 step %c", 'A' + rev - 1);
440 break;
441 case 0x20:
442 printf(": TGA2 abstract software model");
443 break;
444 case 0x21:
445 case 0x22:
446 printf(": TGA2 pass %d", rev - 0x20);
447 break;
448
449 default:
450 printf("unknown stepping (0x%x)", rev);
451 break;
452 }
453 printf(", ");
454
455 /*
456 * Get RAMDAC function vectors and call the RAMDAC functions
457 * to allocate its private storage and pass that back to us.
458 */
459
460 sc->sc_dc->dc_ramdac_funcs = sc->sc_dc->dc_tgaconf->ramdac_funcs();
461 if (!sc->sc_dc->dc_tga2) {
462 if (sc->sc_dc->dc_tgaconf->ramdac_funcs == bt485_funcs)
463 sc->sc_dc->dc_ramdac_cookie =
464 sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
465 tga_sched_update, tga_ramdac_wr, tga_ramdac_rd);
466 else
467 sc->sc_dc->dc_ramdac_cookie =
468 sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
469 tga_sched_update, tga_bt463_wr, tga_bt463_rd);
470 } else {
471 sc->sc_dc->dc_ramdac_cookie =
472 sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
473 tga_sched_update, tga2_ramdac_wr, tga2_ramdac_rd);
474
475 /* XXX this is a bit of a hack, setting the dotclock here */
476 if (sc->sc_dc->dc_tgaconf->ramdac_funcs != bt485_funcs)
477 (*sc->sc_dc->dc_ramdac_funcs->ramdac_set_dotclock)
478 (sc->sc_dc->dc_ramdac_cookie,
479 tga_getdotclock(sc->sc_dc));
480 }
481
482 /*
483 * Initialize the RAMDAC. Initialization includes disabling
484 * cursor, setting a sane colormap, etc. We presume that we've
485 * filled in the necessary dot clock for PowerStorm 4d20.
486 */
487 (*sc->sc_dc->dc_ramdac_funcs->ramdac_init)(sc->sc_dc->dc_ramdac_cookie);
488 TGAWREG(sc->sc_dc, TGA_REG_SISR, 0x00000001); /* XXX */
489
490 if (sc->sc_dc->dc_tgaconf == NULL) {
491 printf("unknown board configuration\n");
492 return;
493 }
494 printf("board type %s\n", sc->sc_dc->dc_tgaconf->tgac_name);
495 printf("%s: %d x %d, %dbpp, %s RAMDAC\n", device_xname(&sc->sc_dev),
496 sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
497 sc->sc_dc->dc_tgaconf->tgac_phys_depth,
498 sc->sc_dc->dc_ramdac_funcs->ramdac_name);
499
500 if (intrstr != NULL)
501 printf("%s: interrupting at %s\n", device_xname(&sc->sc_dev),
502 intrstr);
503
504 aa.console = console;
505 aa.scrdata = &tga_screenlist;
506 aa.accessops = &tga_accessops;
507 aa.accesscookie = sc;
508
509 config_found(self, &aa, wsemuldisplaydevprint);
510
511 config_interrupts(self, tga_config_interrupts);
512 }
513
514 static void
515 tga_config_interrupts (d)
516 struct device *d;
517 {
518 struct tga_softc *sc = (struct tga_softc *)d;
519 sc->sc_dc->dc_intrenabled = 1;
520 }
521
522 int
523 tga_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
524 {
525 struct tga_softc *sc = v;
526 struct tga_devconfig *dc = sc->sc_dc;
527 struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
528 struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
529
530 switch (cmd) {
531 case WSDISPLAYIO_GTYPE:
532 *(u_int *)data = WSDISPLAY_TYPE_TGA;
533 return (0);
534
535 case WSDISPLAYIO_GINFO:
536 #define wsd_fbip ((struct wsdisplay_fbinfo *)data)
537 wsd_fbip->height = sc->sc_dc->dc_ht;
538 wsd_fbip->width = sc->sc_dc->dc_wid;
539 wsd_fbip->depth = sc->sc_dc->dc_tgaconf->tgac_phys_depth;
540 #if 0
541 wsd_fbip->cmsize = 256; /* XXX ??? */
542 #else
543 wsd_fbip->cmsize = 1024; /* XXX ??? */
544 #endif
545 #undef wsd_fbip
546 return (0);
547
548 case WSDISPLAYIO_GETCMAP:
549 return (*dcrf->ramdac_get_cmap)(dcrc,
550 (struct wsdisplay_cmap *)data);
551
552 case WSDISPLAYIO_PUTCMAP:
553 return (*dcrf->ramdac_set_cmap)(dcrc,
554 (struct wsdisplay_cmap *)data);
555
556 case WSDISPLAYIO_SVIDEO:
557 if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
558 tga_blank(sc->sc_dc);
559 else
560 tga_unblank(sc->sc_dc);
561 return (0);
562
563 case WSDISPLAYIO_GVIDEO:
564 *(u_int *)data = dc->dc_blanked ?
565 WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
566 return (0);
567
568 case WSDISPLAYIO_GCURPOS:
569 return (*dcrf->ramdac_get_curpos)(dcrc,
570 (struct wsdisplay_curpos *)data);
571
572 case WSDISPLAYIO_SCURPOS:
573 return (*dcrf->ramdac_set_curpos)(dcrc,
574 (struct wsdisplay_curpos *)data);
575
576 case WSDISPLAYIO_GCURMAX:
577 return (*dcrf->ramdac_get_curmax)(dcrc,
578 (struct wsdisplay_curpos *)data);
579
580 case WSDISPLAYIO_GCURSOR:
581 return (*dcrf->ramdac_get_cursor)(dcrc,
582 (struct wsdisplay_cursor *)data);
583
584 case WSDISPLAYIO_SCURSOR:
585 return (*dcrf->ramdac_set_cursor)(dcrc,
586 (struct wsdisplay_cursor *)data);
587
588 case WSDISPLAYIO_LINEBYTES:
589 *(u_int *)data = dc->dc_rowbytes;
590 return (0);
591
592 /* PCI config read/write passthrough. */
593 case PCI_IOC_CFGREAD:
594 case PCI_IOC_CFGWRITE:
595 return (pci_devioctl(dc->dc_pc, dc->dc_pcitag,
596 cmd, data, flag, l));
597 }
598 return (EPASSTHROUGH);
599 }
600
601 static int
602 tga_sched_update(v, f)
603 void *v;
604 void (*f)(void *);
605 {
606 struct tga_devconfig *dc = v;
607
608 if (dc->dc_intrenabled) {
609 /* Arrange for f to be called at the next end-of-frame interrupt */
610 dc->dc_ramdac_intr = f;
611 TGAWREG(dc, TGA_REG_SISR, 0x00010000);
612 } else {
613 /* Spin until the end-of-frame, then call f */
614 TGAWREG(dc, TGA_REG_SISR, 0x00010001);
615 TGAREGWB(dc, TGA_REG_SISR, 1);
616 while ((TGARREG(dc, TGA_REG_SISR) & 0x00000001) == 0)
617 ;
618 f(dc->dc_ramdac_cookie);
619 TGAWREG(dc, TGA_REG_SISR, 0x00000001);
620 TGAREGWB(dc, TGA_REG_SISR, 1);
621 }
622
623 return 0;
624 }
625
626 static int
627 tga_intr(void *v)
628 {
629 struct tga_devconfig *dc = v;
630 struct ramdac_cookie *dcrc= dc->dc_ramdac_cookie;
631
632 u_int32_t reg;
633
634 reg = TGARREG(dc, TGA_REG_SISR);
635 if (( reg & 0x00010001) != 0x00010001) {
636 /* Odd. We never set any of the other interrupt enables. */
637 if ((reg & 0x1f) != 0) {
638 /* Clear the mysterious pending interrupts. */
639 TGAWREG(dc, TGA_REG_SISR, (reg & 0x1f));
640 TGAREGWB(dc, TGA_REG_SISR, 1);
641 /* This was our interrupt, even if we're puzzled as to why
642 * we got it. Don't make the interrupt handler think it
643 * was a stray.
644 */
645 return -1;
646 } else {
647 return 0;
648 }
649 }
650 /* if we have something to do, do it */
651 if (dc->dc_ramdac_intr) {
652 dc->dc_ramdac_intr(dcrc);
653 dc->dc_ramdac_intr = NULL;
654 }
655 TGAWREG(dc, TGA_REG_SISR, 0x00000001);
656 TGAREGWB(dc, TGA_REG_SISR, 1);
657 return (1);
658 }
659
660 paddr_t
661 tga_mmap(void *v, void *vs, off_t offset, int prot)
662 {
663 struct tga_softc *sc = v;
664
665 if (offset >= sc->sc_dc->dc_tgaconf->tgac_cspace_size || offset < 0)
666 return -1;
667
668 return (bus_space_mmap(sc->sc_dc->dc_memt, sc->sc_dc->dc_pcipaddr,
669 offset, prot, BUS_SPACE_MAP_LINEAR));
670 }
671
672 static int
673 tga_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
674 void *v;
675 const struct wsscreen_descr *type;
676 void **cookiep;
677 int *curxp, *curyp;
678 long *attrp;
679 {
680 struct tga_softc *sc = v;
681 long defattr;
682
683 if (sc->nscreens > 0)
684 return (ENOMEM);
685
686 *cookiep = &sc->sc_dc->dc_rinfo; /* one and only for now */
687 *curxp = 0;
688 *curyp = 0;
689 sc->sc_dc->dc_rinfo.ri_ops.allocattr(&sc->sc_dc->dc_rinfo,
690 0, 0, 0, &defattr);
691 *attrp = defattr;
692 sc->nscreens++;
693 return (0);
694 }
695
696 static void
697 tga_free_screen(void *v, void *cookie)
698 {
699 struct tga_softc *sc = v;
700
701 if (sc->sc_dc == &tga_console_dc)
702 panic("tga_free_screen: console");
703
704 sc->nscreens--;
705 }
706
707 static int
708 tga_show_screen(v, cookie, waitok, cb, cbarg)
709 void *v;
710 void *cookie;
711 int waitok;
712 void (*cb)(void *, int, int);
713 void *cbarg;
714 {
715
716 return (0);
717 }
718
719 int
720 tga_cnattach(iot, memt, pc, bus, device, function)
721 bus_space_tag_t iot, memt;
722 pci_chipset_tag_t pc;
723 int bus, device, function;
724 {
725 struct tga_devconfig *dcp = &tga_console_dc;
726 long defattr;
727
728 tga_init(memt, pc, pci_make_tag(pc, bus, device, function), dcp);
729
730 /* sanity checks */
731 if (dcp->dc_vaddr == 0)
732 panic("tga_console(%d, %d): couldn't map memory space",
733 device, function);
734 if (dcp->dc_tgaconf == NULL)
735 panic("tga_console(%d, %d): unknown board configuration",
736 device, function);
737
738 /*
739 * Initialize the RAMDAC but DO NOT allocate any private storage.
740 * Initialization includes disabling cursor, setting a sane
741 * colormap, etc. It will be reinitialized in tgaattach().
742 */
743 if (dcp->dc_tga2) {
744 if (dcp->dc_tgaconf->ramdac_funcs == bt485_funcs)
745 bt485_cninit(dcp, tga_sched_update, tga2_ramdac_wr,
746 tga2_ramdac_rd);
747 else
748 ibm561_cninit(dcp, tga_sched_update, tga2_ramdac_wr,
749 tga2_ramdac_rd, tga_getdotclock(dcp));
750 } else {
751 if (dcp->dc_tgaconf->ramdac_funcs == bt485_funcs)
752 bt485_cninit(dcp, tga_sched_update, tga_ramdac_wr,
753 tga_ramdac_rd);
754 else {
755 bt463_cninit(dcp, tga_sched_update, tga_bt463_wr,
756 tga_bt463_rd);
757 }
758 }
759 dcp->dc_rinfo.ri_ops.allocattr(&dcp->dc_rinfo, 0, 0, 0, &defattr);
760 wsdisplay_cnattach(&tga_stdscreen, &dcp->dc_rinfo, 0, 0, defattr);
761
762 return(0);
763 }
764
765 /*
766 * Functions to blank and unblank the display.
767 */
768 static void
769 tga_blank(struct tga_devconfig *dc)
770 {
771
772 if (!dc->dc_blanked) {
773 dc->dc_blanked = 1;
774 /* XXX */
775 TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | VVR_BLANK);
776 }
777 }
778
779 static void
780 tga_unblank(struct tga_devconfig *dc)
781 {
782
783 if (dc->dc_blanked) {
784 dc->dc_blanked = 0;
785 /* XXX */
786 TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~VVR_BLANK);
787 }
788 }
789
790 /*
791 * Functions to manipulate the built-in cursor handing hardware.
792 */
793 int
794 tga_builtin_set_cursor(struct tga_devconfig *dc, struct wsdisplay_cursor *cursorp)
795 {
796 struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
797 struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
798 u_char image[512];
799 u_int count, v;
800 int error;
801
802 v = cursorp->which;
803 if (v & WSDISPLAY_CURSOR_DOCMAP) {
804 error = dcrf->ramdac_check_curcmap(dcrc, cursorp);
805 if (error)
806 return (error);
807 }
808 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
809 if ((u_int)cursorp->size.x != 64 ||
810 (u_int)cursorp->size.y > 64)
811 return (EINVAL);
812 /* The cursor is 2 bits deep, and there is no mask */
813 count = (cursorp->size.y * 64 * 2) / NBBY;
814 error = copyin(cursorp->image, image, count);
815 if (error)
816 return error;
817 }
818 if (v & WSDISPLAY_CURSOR_DOHOT) /* not supported */
819 return EINVAL;
820
821 /* parameters are OK; do it */
822 if (v & WSDISPLAY_CURSOR_DOCUR) {
823 if (cursorp->enable)
824 /* XXX */
825 TGAWREG(dc, TGA_REG_VVVR,
826 TGARREG(dc, TGA_REG_VVVR) | 0x04);
827 else
828 /* XXX */
829 TGAWREG(dc, TGA_REG_VVVR,
830 TGARREG(dc, TGA_REG_VVVR) & ~0x04);
831 }
832 if (v & WSDISPLAY_CURSOR_DOPOS) {
833 TGAWREG(dc, TGA_REG_CXYR, ((cursorp->pos.y & 0xfff) << 12) |
834 (cursorp->pos.x & 0xfff));
835 }
836 if (v & WSDISPLAY_CURSOR_DOCMAP) {
837 dcrf->ramdac_set_curcmap(dcrc, cursorp);
838 }
839 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
840 count = ((64 * 2) / NBBY) * cursorp->size.y;
841 TGAWREG(dc, TGA_REG_CCBR,
842 (TGARREG(dc, TGA_REG_CCBR) & ~0xfc00) |
843 (cursorp->size.y << 10));
844 memcpy((char *)(dc->dc_vaddr +
845 (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
846 image, count);
847 }
848 return (0);
849 }
850
851 int
852 tga_builtin_get_cursor(struct tga_devconfig *dc, struct wsdisplay_cursor *cursorp)
853 {
854 struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
855 struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
856 int count, error;
857
858 cursorp->which = WSDISPLAY_CURSOR_DOALL &
859 ~(WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOCMAP);
860 cursorp->enable = (TGARREG(dc, TGA_REG_VVVR) & 0x04) != 0;
861 cursorp->pos.x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
862 cursorp->pos.y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
863 cursorp->size.x = 64;
864 cursorp->size.y = (TGARREG(dc, TGA_REG_CCBR) >> 10) & 0x3f;
865
866 if (cursorp->image != NULL) {
867 count = (cursorp->size.y * 64 * 2) / NBBY;
868 error = copyout((char *)(dc->dc_vaddr +
869 (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
870 cursorp->image, count);
871 if (error)
872 return (error);
873 /* No mask */
874 }
875 error = dcrf->ramdac_get_curcmap(dcrc, cursorp);
876 return (error);
877 }
878
879 int
880 tga_builtin_set_curpos(struct tga_devconfig *dc, struct wsdisplay_curpos *curposp)
881 {
882
883 TGAWREG(dc, TGA_REG_CXYR,
884 ((curposp->y & 0xfff) << 12) | (curposp->x & 0xfff));
885 return (0);
886 }
887
888 int
889 tga_builtin_get_curpos(struct tga_devconfig *dc, struct wsdisplay_curpos *curposp)
890 {
891
892 curposp->x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
893 curposp->y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
894 return (0);
895 }
896
897 int
898 tga_builtin_get_curmax(struct tga_devconfig *dc, struct wsdisplay_curpos *curposp)
899 {
900
901 curposp->x = curposp->y = 64;
902 return (0);
903 }
904
905 /*
906 * Copy columns (characters) in a row (line).
907 */
908 static void
909 tga_copycols(id, row, srccol, dstcol, ncols)
910 void *id;
911 int row, srccol, dstcol, ncols;
912 {
913 struct rasops_info *ri = id;
914 int y, srcx, dstx, nx;
915
916 y = ri->ri_font->fontheight * row;
917 srcx = ri->ri_font->fontwidth * srccol;
918 dstx = ri->ri_font->fontwidth * dstcol;
919 nx = ri->ri_font->fontwidth * ncols;
920
921 tga_rop(ri, dstx, y,
922 nx, ri->ri_font->fontheight, RAS_SRC,
923 ri, srcx, y);
924 }
925
926 /*
927 * Copy rows (lines).
928 */
929 static void
930 tga_copyrows(id, srcrow, dstrow, nrows)
931 void *id;
932 int srcrow, dstrow, nrows;
933 {
934 struct rasops_info *ri = id;
935 int srcy, dsty, ny;
936
937 srcy = ri->ri_font->fontheight * srcrow;
938 dsty = ri->ri_font->fontheight * dstrow;
939 ny = ri->ri_font->fontheight * nrows;
940
941 tga_rop(ri, 0, dsty,
942 ri->ri_emuwidth, ny, RAS_SRC,
943 ri, 0, srcy);
944 }
945
946 /* Do we need the src? */
947 static int needsrc[16] = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 };
948
949 /* A mapping between our API and the TGA card */
950 static int map_rop[16] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6,
951 0xe, 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
952 };
953
954 /*
955 * Generic TGA raster op.
956 * This covers all possible raster ops, and
957 * clips the sizes and all of that.
958 */
959 static int
960 tga_rop(dst, dx, dy, w, h, rop, src, sx, sy)
961 struct rasops_info *dst;
962 int dx, dy, w, h, rop;
963 struct rasops_info *src;
964 int sx, sy;
965 {
966 if (!dst)
967 return -1;
968 if (needsrc[RAS_GETOP(rop)]) {
969 if (src == NULL)
970 return -1; /* We want a src */
971 /* Clip against src */
972 if (sx < 0) {
973 w += sx;
974 sx = 0;
975 }
976 if (sy < 0) {
977 h += sy;
978 sy = 0;
979 }
980 if (sx + w > src->ri_emuwidth)
981 w = src->ri_emuwidth - sx;
982 if (sy + h > src->ri_emuheight)
983 h = src->ri_emuheight - sy;
984 } else {
985 if (src != NULL)
986 return -1; /* We need no src */
987 }
988 /* Clip against dst. We modify src regardless of using it,
989 * since it really doesn't matter.
990 */
991 if (dx < 0) {
992 w += dx;
993 sx -= dx;
994 dx = 0;
995 }
996 if (dy < 0) {
997 h += dy;
998 sy -= dy;
999 dy = 0;
1000 }
1001 if (dx + w > dst->ri_emuwidth)
1002 w = dst->ri_emuwidth - dx;
1003 if (dy + h > dst->ri_emuheight)
1004 h = dst->ri_emuheight - dy;
1005 if (w <= 0 || h <= 0)
1006 return 0; /* Vacuously true; */
1007 if (!src) {
1008 /* XXX Punt! */
1009 return -1;
1010 }
1011 return tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy);
1012 }
1013
1014
1015
1016 /*
1017 * Video to Video raster ops.
1018 * This function deals with all raster ops that have a src and dst
1019 * that are on the card.
1020 */
1021 static int
1022 tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy)
1023 struct rasops_info *dst;
1024 int dx, dy, w, h, rop;
1025 struct rasops_info *src;
1026 int sx, sy;
1027 {
1028 struct tga_devconfig *dc = (struct tga_devconfig *)dst->ri_hw;
1029 int srcb, dstb, tga_srcb, tga_dstb;
1030 int x, y, wb;
1031 int xstart, xend, xdir;
1032 int ystart, yend, ydir, yinc;
1033 int xleft, lastx, lastleft;
1034 int offset = 1 * dc->dc_tgaconf->tgac_vvbr_units;
1035
1036 /*
1037 * I don't yet want to deal with unaligned guys, really. And we don't
1038 * deal with copies from one card to another.
1039 */
1040 if (dx % 8 != 0 || sx % 8 != 0 || src != dst) {
1041 /* XXX Punt! */
1042 /* XXX should never happen, since it's only being used to
1043 * XXX copy 8-pixel-wide characters.
1044 */
1045 return -1;
1046 }
1047
1048 srcb = sy * src->ri_stride + sx * (src->ri_depth/8);
1049 dstb = dy * dst->ri_stride + dx * (dst->ri_depth/8);
1050 tga_srcb = offset + (sy + src->ri_yorigin) * src->ri_stride +
1051 (sx + src->ri_xorigin) * (src->ri_depth/8);
1052 tga_dstb = offset + (dy + dst->ri_yorigin) * dst->ri_stride +
1053 (dx + dst->ri_xorigin) * (dst->ri_depth/8);
1054
1055 if (sy >= dy) {
1056 ystart = 0;
1057 yend = (h - 1) * dst->ri_stride;
1058 ydir = 1;
1059 } else {
1060 ystart = (h - 1) * dst->ri_stride;
1061 yend = 0;
1062 ydir = -1;
1063 }
1064 yinc = ydir * dst->ri_stride;
1065
1066 wb = w * (dst->ri_depth / 8);
1067 if (sx >= dx || (sx + w) <= dx) { /* copy forwards */
1068 xstart = 0;
1069 xend = wb;
1070 xdir = 1;
1071 } else { /* copy backwards */
1072 xstart = wb;
1073 xend = 0;
1074 xdir = -1;
1075 }
1076
1077 TGAWALREG(dc, TGA_REG_GMOR, 3, 0x0007); /* Copy mode */
1078 TGAWALREG(dc, TGA_REG_GOPR, 3, map_rop[rop]); /* Set up the op */
1079 TGAWALREG(dc, TGA_REG_GPSR, 3, 0); /* No shift */
1080
1081 /*
1082 * we have 3 sizes of pixels to move in X direction:
1083 * 4 * 64 (unrolled TGA ops)
1084 * 64 (single TGA op)
1085 * 4 (CPU, using long word)
1086 */
1087
1088 if (xdir == 1) { /* move to the left */
1089
1090 if (wb & ~63)
1091 for (y = ystart; (ydir * y) <= (ydir * yend); y += yinc) {
1092 /* 4*64 byte chunks */
1093 for (xleft = wb, x = xstart; xleft >= 4*64;
1094 x += 4*64, xleft -= 4*64) {
1095
1096 /* XXX XXX Eight writes to different addresses should fill
1097 * XXX XXX up the write buffers on 21064 and 21164 chips,
1098 * XXX XXX but later CPUs might have larger write buffers which
1099 * XXX XXX require further unrolling of this loop, or the
1100 * XXX XXX insertion of memory barriers.
1101 */
1102 TGAWALREG(dc, TGA_REG_GCSR, 0, tga_srcb + y + x + 0 * 64);
1103 TGAWALREG(dc, TGA_REG_GCDR, 0, tga_dstb + y + x + 0 * 64);
1104 TGAWALREG(dc, TGA_REG_GCSR, 1, tga_srcb + y + x + 1 * 64);
1105 TGAWALREG(dc, TGA_REG_GCDR, 1, tga_dstb + y + x + 1 * 64);
1106 TGAWALREG(dc, TGA_REG_GCSR, 2, tga_srcb + y + x + 2 * 64);
1107 TGAWALREG(dc, TGA_REG_GCDR, 2, tga_dstb + y + x + 2 * 64);
1108 TGAWALREG(dc, TGA_REG_GCSR, 3, tga_srcb + y + x + 3 * 64);
1109 TGAWALREG(dc, TGA_REG_GCDR, 3, tga_dstb + y + x + 3 * 64);
1110 }
1111
1112 /* 64 byte chunks */
1113 for (; xleft >= 64; x += 64, xleft -= 64) {
1114 TGAWALREG(dc, TGA_REG_GCSR, 0, tga_srcb + y + x + 0 * 64);
1115 TGAWALREG(dc, TGA_REG_GCDR, 0, tga_dstb + y + x + 0 * 64);
1116 }
1117 }
1118
1119 TGAWALREG(dc, TGA_REG_GOPR, 0, 0x0003); /* op -> dst = src */
1120 TGAWALREG(dc, TGA_REG_GMOR, 0, 0x0000); /* Simple mode */
1121
1122 lastleft = wb & 63;
1123 if (lastleft) {
1124 lastx = xstart + (wb & ~63);
1125 for (y = ystart; (ydir * y) <= (ydir * yend); y += yinc) {
1126 /* 4 byte granularity */
1127 for (x = lastx, xleft = lastleft; xleft >= 4;
1128 x += 4, xleft -= 4) {
1129 *(uint32_t *)(dst->ri_bits + dstb + y + x + 0 * 4) =
1130 *(uint32_t *)(dst->ri_bits + srcb + y + x + 0 * 4);
1131 }
1132 }
1133 }
1134 }
1135 else { /* above move to the left, below move to the right */
1136
1137 if (wb & ~63)
1138 for (y = ystart; (ydir * y) <= (ydir * yend); y += yinc) {
1139 /* 4*64 byte chunks */
1140 for (xleft = wb, x = xstart; xleft >= 4*64;
1141 x -= 4*64, xleft -= 4*64) {
1142
1143 /* XXX XXX Eight writes to different addresses should fill
1144 * XXX XXX up the write buffers on 21064 and 21164 chips,
1145 * XXX XXX but later CPUs might have larger write buffers which
1146 * XXX XXX require further unrolling of this loop, or the
1147 * XXX XXX insertion of memory barriers.
1148 */
1149 TGAWALREG(dc, TGA_REG_GCSR, 0, tga_srcb + y + x - 1 * 64);
1150 TGAWALREG(dc, TGA_REG_GCDR, 0, tga_dstb + y + x - 1 * 64);
1151 TGAWALREG(dc, TGA_REG_GCSR, 1, tga_srcb + y + x - 2 * 64);
1152 TGAWALREG(dc, TGA_REG_GCDR, 1, tga_dstb + y + x - 2 * 64);
1153 TGAWALREG(dc, TGA_REG_GCSR, 2, tga_srcb + y + x - 3 * 64);
1154 TGAWALREG(dc, TGA_REG_GCDR, 2, tga_dstb + y + x - 3 * 64);
1155 TGAWALREG(dc, TGA_REG_GCSR, 3, tga_srcb + y + x - 4 * 64);
1156 TGAWALREG(dc, TGA_REG_GCDR, 3, tga_dstb + y + x - 4 * 64);
1157 }
1158
1159 /* 64 byte chunks */
1160 for (; xleft >= 64; x -= 64, xleft -= 64) {
1161 TGAWALREG(dc, TGA_REG_GCSR, 0, tga_srcb + y + x - 1 * 64);
1162 TGAWALREG(dc, TGA_REG_GCDR, 0, tga_dstb + y + x - 1 * 64);
1163 }
1164 }
1165
1166 TGAWALREG(dc, TGA_REG_GOPR, 0, 0x0003); /* op -> dst = src */
1167 TGAWALREG(dc, TGA_REG_GMOR, 0, 0x0000); /* Simple mode */
1168
1169 lastleft = wb & 63;
1170 if (lastleft) {
1171 lastx = xstart - (wb & ~63);
1172 for (y = ystart; (ydir * y) <= (ydir * yend); y += yinc) {
1173 /* 4 byte granularity */
1174 for (x = lastx, xleft = lastleft; xleft >= 4;
1175 x -= 4, xleft -= 4) {
1176 *(uint32_t *)(dst->ri_bits + dstb + y + x - 1 * 4) =
1177 *(uint32_t *)(dst->ri_bits + srcb + y + x - 1 * 4);
1178 }
1179 }
1180 }
1181 }
1182 return 0;
1183 }
1184
1185
1186 void tga_putchar (c, row, col, uc, attr)
1187 void *c;
1188 int row, col;
1189 u_int uc;
1190 long attr;
1191 {
1192 struct rasops_info *ri = c;
1193 struct tga_devconfig *dc = ri->ri_hw;
1194 int fs, height, width;
1195 u_char *fr;
1196 int32_t *rp;
1197
1198 rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
1199
1200 height = ri->ri_font->fontheight;
1201 width = ri->ri_font->fontwidth;
1202
1203 uc -= ri->ri_font->firstchar;
1204 fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
1205 fs = ri->ri_font->stride;
1206
1207 /* Set foreground and background color. XXX memoize this somehow?
1208 * The rasops code has already expanded the color entry to 32 bits
1209 * for us, even for 8-bit displays, so we don't have to do anything.
1210 */
1211 TGAWREG(dc, TGA_REG_GFGR, ri->ri_devcmap[(attr >> 24) & 15]);
1212 TGAWREG(dc, TGA_REG_GBGR, ri->ri_devcmap[(attr >> 16) & 15]);
1213
1214 /* Set raster operation to "copy"... */
1215 if (ri->ri_depth == 8)
1216 TGAWREG(dc, TGA_REG_GOPR, 0x3);
1217 else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
1218 TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
1219
1220 /* Set which pixels we're drawing (of a possible 32). */
1221 TGAWREG(dc, TGA_REG_GPXR_P, (1 << width) - 1);
1222
1223 /* Set drawing mode to opaque stipple. */
1224 TGAWREG(dc, TGA_REG_GMOR, 0x1);
1225
1226 /* Insert write barrier before actually sending data */
1227 /* XXX Abuses the fact that there is only one write barrier on Alphas */
1228 TGAREGWB(dc, TGA_REG_GMOR, 1);
1229
1230 while(height--) {
1231 /* The actual stipple write */
1232 *rp = fr[0] | (fr[1] << 8) | (fr[2] << 16) | (fr[3] << 24);
1233
1234 fr += fs;
1235 rp = (int32_t *)((char *)rp + ri->ri_stride);
1236 }
1237
1238 /* Do underline */
1239 if ((attr & 1) != 0) {
1240 rp = (int32_t *)((char *)rp - (ri->ri_stride << 1));
1241 *rp = 0xffffffff;
1242 }
1243
1244 /* Set grapics mode back to normal. */
1245 TGAWREG(dc, TGA_REG_GMOR, 0);
1246 TGAWREG(dc, TGA_REG_GPXR_P, 0xffffffff);
1247
1248 }
1249
1250 static void
1251 tga_eraserows(c, row, num, attr)
1252 void *c;
1253 int row, num;
1254 long attr;
1255 {
1256 struct rasops_info *ri = c;
1257 struct tga_devconfig *dc = ri->ri_hw;
1258 int32_t color, lines, pixels;
1259 int32_t *rp;
1260
1261 color = ri->ri_devcmap[(attr >> 16) & 15];
1262 rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale);
1263 lines = num * ri->ri_font->fontheight;
1264 pixels = ri->ri_emuwidth - 1;
1265
1266 /* Set fill color in block-color registers */
1267 TGAWREG(dc, TGA_REG_GBCR0, color);
1268 TGAWREG(dc, TGA_REG_GBCR1, color);
1269 if (ri->ri_depth != 8) {
1270 TGAWREG(dc, TGA_REG_GBCR2, color);
1271 TGAWREG(dc, TGA_REG_GBCR3, color);
1272 TGAWREG(dc, TGA_REG_GBCR4, color);
1273 TGAWREG(dc, TGA_REG_GBCR5, color);
1274 TGAWREG(dc, TGA_REG_GBCR6, color);
1275 TGAWREG(dc, TGA_REG_GBCR7, color);
1276 }
1277
1278 /* Set raster operation to "copy"... */
1279 if (ri->ri_depth == 8)
1280 TGAWREG(dc, TGA_REG_GOPR, 0x3);
1281 else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
1282 TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
1283
1284 /* Set which pixels we're drawing (of a possible 32). */
1285 TGAWREG(dc, TGA_REG_GDAR, 0xffffffff);
1286
1287 /* Set drawing mode to block fill. */
1288 TGAWREG(dc, TGA_REG_GMOR, 0x2d);
1289
1290 /* Insert write barrier before actually sending data */
1291 /* XXX Abuses the fact that there is only one write barrier on Alphas */
1292 TGAREGWB(dc, TGA_REG_GMOR, 1);
1293
1294 while (lines--) {
1295 *rp = pixels;
1296 rp = (int32_t *)((char *)rp + ri->ri_stride);
1297 }
1298
1299 /* Set grapics mode back to normal. */
1300 TGAWREG(dc, TGA_REG_GMOR, 0);
1301
1302 }
1303
1304 static void
1305 tga_erasecols (c, row, col, num, attr)
1306 void *c;
1307 int row, col, num;
1308 long attr;
1309 {
1310 struct rasops_info *ri = c;
1311 struct tga_devconfig *dc = ri->ri_hw;
1312 int32_t color, lines, pixels;
1313 int32_t *rp;
1314
1315 color = ri->ri_devcmap[(attr >> 16) & 15];
1316 rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
1317 lines = ri->ri_font->fontheight;
1318 pixels = (num * ri->ri_font->fontwidth) - 1;
1319
1320 /* Set fill color in block-color registers */
1321 TGAWREG(dc, TGA_REG_GBCR0, color);
1322 TGAWREG(dc, TGA_REG_GBCR1, color);
1323 if (ri->ri_depth != 8) {
1324 TGAWREG(dc, TGA_REG_GBCR2, color);
1325 TGAWREG(dc, TGA_REG_GBCR3, color);
1326 TGAWREG(dc, TGA_REG_GBCR4, color);
1327 TGAWREG(dc, TGA_REG_GBCR5, color);
1328 TGAWREG(dc, TGA_REG_GBCR6, color);
1329 TGAWREG(dc, TGA_REG_GBCR7, color);
1330 }
1331
1332 /* Set raster operation to "copy"... */
1333 if (ri->ri_depth == 8)
1334 TGAWREG(dc, TGA_REG_GOPR, 0x3);
1335 else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
1336 TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
1337
1338 /* Set which pixels we're drawing (of a possible 32). */
1339 TGAWREG(dc, TGA_REG_GDAR, 0xffffffff);
1340
1341 /* Set drawing mode to block fill. */
1342 TGAWREG(dc, TGA_REG_GMOR, 0x2d);
1343
1344 /* Insert write barrier before actually sending data */
1345 /* XXX Abuses the fact that there is only one write barrier on Alphas */
1346 TGAREGWB(dc, TGA_REG_GMOR, 1);
1347
1348 while (lines--) {
1349 *rp = pixels;
1350 rp = (int32_t *)((char *)rp + ri->ri_stride);
1351 }
1352
1353 /* Set grapics mode back to normal. */
1354 TGAWREG(dc, TGA_REG_GMOR, 0);
1355 }
1356
1357
1358 static void
1359 tga_ramdac_wr(void *v, u_int btreg, u_int8_t val)
1360 {
1361 struct tga_devconfig *dc = v;
1362
1363 if (btreg > BT485_REG_MAX)
1364 panic("tga_ramdac_wr: reg %d out of range", btreg);
1365
1366 TGAWREG(dc, TGA_REG_EPDR, (btreg << 9) | (0 << 8 ) | val); /* XXX */
1367 TGAREGWB(dc, TGA_REG_EPDR, 1);
1368 }
1369
1370 static void
1371 tga2_ramdac_wr(void *v, u_int btreg, u_int8_t val)
1372 {
1373 struct tga_devconfig *dc = v;
1374 bus_space_handle_t ramdac;
1375
1376 if (btreg > BT485_REG_MAX)
1377 panic("tga_ramdac_wr: reg %d out of range", btreg);
1378
1379 bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
1380 (0xe << 12) + (btreg << 8), 4, &ramdac);
1381 bus_space_write_4(dc->dc_memt, ramdac, 0, val & 0xff);
1382 bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_WRITE);
1383 }
1384
1385 static u_int8_t
1386 tga_bt463_rd(void *v, u_int btreg)
1387 {
1388 struct tga_devconfig *dc = v;
1389 tga_reg_t rdval;
1390
1391 /*
1392 * Strobe CE# (high->low->high) since status and data are latched on
1393 * the falling and rising edges (repsectively) of this active-low signal.
1394 */
1395
1396 TGAREGWB(dc, TGA_REG_EPSR, 1);
1397 TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 1);
1398 TGAREGWB(dc, TGA_REG_EPSR, 1);
1399 TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 0);
1400
1401 TGAREGRB(dc, TGA_REG_EPSR, 1);
1402
1403 rdval = TGARREG(dc, TGA_REG_EPDR);
1404 TGAREGWB(dc, TGA_REG_EPSR, 1);
1405 TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 1);
1406
1407 return (rdval >> 16) & 0xff;
1408 }
1409
1410 static void
1411 tga_bt463_wr(void *v, u_int btreg, u_int8_t val)
1412 {
1413 struct tga_devconfig *dc = v;
1414
1415 /*
1416 * In spite of the 21030 documentation, to set the MPU bus bits for
1417 * a write, you set them in the upper bits of EPDR, not EPSR.
1418 */
1419
1420 /*
1421 * Strobe CE# (high->low->high) since status and data are latched on
1422 * the falling and rising edges of this active-low signal.
1423 */
1424
1425 TGAREGWB(dc, TGA_REG_EPDR, 1);
1426 TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x100 | val);
1427 TGAREGWB(dc, TGA_REG_EPDR, 1);
1428 TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x000 | val);
1429 TGAREGWB(dc, TGA_REG_EPDR, 1);
1430 TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x100 | val);
1431
1432 }
1433
1434 static u_int8_t
1435 tga_ramdac_rd(void *v, u_int btreg)
1436 {
1437 struct tga_devconfig *dc = v;
1438 tga_reg_t rdval;
1439
1440 if (btreg > BT485_REG_MAX)
1441 panic("tga_ramdac_rd: reg %d out of range", btreg);
1442
1443 TGAWREG(dc, TGA_REG_EPSR, (btreg << 1) | 0x1); /* XXX */
1444 TGAREGWB(dc, TGA_REG_EPSR, 1);
1445
1446 rdval = TGARREG(dc, TGA_REG_EPDR);
1447 return (rdval >> 16) & 0xff; /* XXX */
1448 }
1449
1450 static u_int8_t
1451 tga2_ramdac_rd(void *v, u_int btreg)
1452 {
1453 struct tga_devconfig *dc = v;
1454 bus_space_handle_t ramdac;
1455 u_int8_t retval;
1456
1457 if (btreg > BT485_REG_MAX)
1458 panic("tga_ramdac_rd: reg %d out of range", btreg);
1459
1460 bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
1461 (0xe << 12) + (btreg << 8), 4, &ramdac);
1462 retval = bus_space_read_4(dc->dc_memt, ramdac, 0) & 0xff;
1463 bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_READ);
1464 return retval;
1465 }
1466
1467 #include <dev/ic/decmonitors.c>
1468 void tga2_ics9110_wr(struct tga_devconfig *dc, int dotclock);
1469
1470 struct monitor *tga_getmonitor(struct tga_devconfig *dc);
1471
1472 void
1473 tga2_init(struct tga_devconfig *dc)
1474 {
1475 struct monitor *m = tga_getmonitor(dc);
1476
1477 /* Deal with the dot clocks.
1478 */
1479 if (dc->dc_tga_type == TGA_TYPE_POWERSTORM_4D20) {
1480 /* Set this up as a reference clock for the
1481 * ibm561's PLL.
1482 */
1483 tga2_ics9110_wr(dc, 14300000);
1484 /* XXX Can't set up the dotclock properly, until such time
1485 * as the RAMDAC is configured.
1486 */
1487 } else {
1488 /* otherwise the ics9110 is our clock. */
1489 tga2_ics9110_wr(dc, m->dotclock);
1490 }
1491 #if 0
1492 TGAWREG(dc, TGA_REG_VHCR,
1493 ((m->hbp / 4) << 21) |
1494 ((m->hsync / 4) << 14) |
1495 (((m->hfp - 4) / 4) << 9) |
1496 ((m->cols + 4) / 4));
1497 #else
1498 TGAWREG(dc, TGA_REG_VHCR,
1499 ((m->hbp / 4) << 21) |
1500 ((m->hsync / 4) << 14) |
1501 (((m->hfp) / 4) << 9) |
1502 ((m->cols) / 4));
1503 #endif
1504 TGAWREG(dc, TGA_REG_VVCR,
1505 (m->vbp << 22) |
1506 (m->vsync << 16) |
1507 (m->vfp << 11) |
1508 (m->rows));
1509 TGAWREG(dc, TGA_REG_VVBR, 1);
1510 TGAREGRWB(dc, TGA_REG_VHCR, 3);
1511 TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 1);
1512 TGAREGRWB(dc, TGA_REG_VVVR, 1);
1513 TGAWREG(dc, TGA_REG_GPMR, 0xffffffff);
1514 TGAREGRWB(dc, TGA_REG_GPMR, 1);
1515 }
1516
1517 void
1518 tga2_ics9110_wr(struct tga_devconfig *dc, int dotclock)
1519 {
1520 bus_space_handle_t clock;
1521 u_int32_t valU;
1522 int N, M, R, V, X;
1523 int i;
1524
1525 switch (dotclock) {
1526 case 130808000:
1527 N = 0x40; M = 0x7; V = 0x0; X = 0x1; R = 0x1; break;
1528 case 119840000:
1529 N = 0x2d; M = 0x2b; V = 0x1; X = 0x1; R = 0x1; break;
1530 case 108180000:
1531 N = 0x11; M = 0x9; V = 0x1; X = 0x1; R = 0x2; break;
1532 case 103994000:
1533 N = 0x6d; M = 0xf; V = 0x0; X = 0x1; R = 0x1; break;
1534 case 175000000:
1535 N = 0x5F; M = 0x3E; V = 0x1; X = 0x1; R = 0x1; break;
1536 case 75000000:
1537 N = 0x6e; M = 0x15; V = 0x0; X = 0x1; R = 0x1; break;
1538 case 74000000:
1539 N = 0x2a; M = 0x41; V = 0x1; X = 0x1; R = 0x1; break;
1540 case 69000000:
1541 N = 0x35; M = 0xb; V = 0x0; X = 0x1; R = 0x1; break;
1542 case 65000000:
1543 N = 0x6d; M = 0x0c; V = 0x0; X = 0x1; R = 0x2; break;
1544 case 50000000:
1545 N = 0x37; M = 0x3f; V = 0x1; X = 0x1; R = 0x2; break;
1546 case 40000000:
1547 N = 0x5f; M = 0x11; V = 0x0; X = 0x1; R = 0x2; break;
1548 case 31500000:
1549 N = 0x16; M = 0x05; V = 0x0; X = 0x1; R = 0x2; break;
1550 case 25175000:
1551 N = 0x66; M = 0x1d; V = 0x0; X = 0x1; R = 0x2; break;
1552 case 135000000:
1553 N = 0x42; M = 0x07; V = 0x0; X = 0x1; R = 0x1; break;
1554 case 110000000:
1555 N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
1556 case 202500000:
1557 N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
1558 case 14300000: /* this one is just a ref clock */
1559 N = 0x03; M = 0x03; V = 0x1; X = 0x1; R = 0x3; break;
1560 default:
1561 panic("unrecognized clock rate %d", dotclock);
1562 }
1563
1564 /* XXX -- hard coded, bad */
1565 valU = N | ( M << 7 ) | (V << 14);
1566 valU |= (X << 15) | (R << 17);
1567 valU |= 0x17 << 19;
1568
1569 bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
1570 TGA2_MEM_CLOCK + (0xe << 12), 4, &clock); /* XXX */
1571
1572 for (i=24; i>0; i--) {
1573 u_int32_t writeval;
1574
1575 writeval = valU & 0x1;
1576 if (i == 1)
1577 writeval |= 0x2;
1578 valU >>= 1;
1579 bus_space_write_4(dc->dc_memt, clock, 0, writeval);
1580 bus_space_barrier(dc->dc_memt, clock, 0, 4, BUS_SPACE_BARRIER_WRITE);
1581 }
1582 bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
1583 TGA2_MEM_CLOCK + (0xe << 12) + (0x1 << 11) + (0x1 << 11), 4,
1584 &clock); /* XXX */
1585 bus_space_write_4(dc->dc_memt, clock, 0, 0x0);
1586 bus_space_barrier(dc->dc_memt, clock, 0, 0, BUS_SPACE_BARRIER_WRITE);
1587 }
1588
1589 struct monitor *
1590 tga_getmonitor(struct tga_devconfig *dc)
1591 {
1592 return &decmonitors[(~TGARREG(dc, TGA_REG_GREV) >> 16) & 0x0f];
1593 }
1594
1595 unsigned
1596 tga_getdotclock(struct tga_devconfig *dc)
1597 {
1598 return tga_getmonitor(dc)->dotclock;
1599 }
1600