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