xref: /netbsd/sys/dev/tc/tfb.c (revision c4a72b64)
1 /* $NetBSD: tfb.c,v 1.38 2002/10/02 16:53:08 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1998, 1999 Tohru Nishimura.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Tohru Nishimura
17  *	for the NetBSD Project.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: tfb.c,v 1.38 2002/10/02 16:53:08 thorpej Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/buf.h>
42 #include <sys/ioctl.h>
43 
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wsdisplayvar.h>
49 
50 #include <dev/rasops/rasops.h>
51 #include <dev/wsfont/wsfont.h>
52 
53 #include <dev/tc/tcvar.h>
54 #include <dev/ic/bt463reg.h>
55 #include <dev/ic/bt431reg.h>
56 
57 #include <uvm/uvm_extern.h>
58 
59 #if defined(pmax)
60 #define	machine_btop(x) mips_btop(MIPS_KSEG1_TO_PHYS(x))
61 
62 /*
63  * struct bt463reg {
64  * 	u_int8_t	bt_lo;
65  * 	unsigned : 24;
66  * 	u_int8_t	bt_hi;
67  * 	unsigned : 24;
68  * 	u_int8_t	bt_reg;
69  * 	unsigned : 24;
70  * 	u_int8_t	bt_cmap;
71  * };
72  *
73  * N.B. a pair of Bt431s are located adjascently.
74  * 	struct bt431twin {
75  *		struct {
76  *			u_int8_t u0;	for sprite mask
77  *			u_int8_t u1;	for sprite image
78  *			unsigned :16;
79  *		} bt_lo;
80  *		...
81  *
82  * struct bt431reg {
83  * 	u_int16_t	bt_lo;
84  * 	unsigned : 16;
85  * 	u_int16_t	bt_hi;
86  * 	unsigned : 16;
87  * 	u_int16_t	bt_ram;
88  * 	unsigned : 16;
89  * 	u_int16_t	bt_ctl;
90  * };
91  */
92 
93 #define	BYTE(base, index)	*((u_int8_t *)(base) + ((index)<<2))
94 #define	HALF(base, index)	*((u_int16_t *)(base) + ((index)<<1))
95 
96 #endif
97 
98 #if defined(alpha)
99 #define	machine_btop(x) alpha_btop(ALPHA_K0SEG_TO_PHYS(x))
100 
101 /*
102  * struct bt463reg {
103  * 	u_int32_t	bt_lo;
104  * 	u_int32_t	bt_hi;
105  * 	u_int32_t	bt_reg;
106  * 	u_int32_t	bt_cmap;
107  * };
108  *
109  * struct bt431reg {
110  * 	u_int32_t	bt_lo;
111  * 	u_int32_t	bt_hi;
112  * 	u_int32_t	bt_ram;
113  * 	u_int32_t	bt_ctl;
114  * };
115  */
116 
117 #define	BYTE(base, index)	*((u_int32_t *)(base) + (index))
118 #define	HALF(base, index)	*((u_int32_t *)(base) + (index))
119 
120 #endif
121 
122 /* Bt463 hardware registers */
123 #define	bt_lo	0
124 #define	bt_hi	1
125 #define	bt_reg	2
126 #define	bt_cmap	3
127 
128 /* Bt431 hardware registers */
129 #define	bt_ram	2
130 #define	bt_ctl	3
131 
132 #define	SELECT463(vdac, regno) do {			\
133 	BYTE(vdac, bt_lo) = (regno) & 0x00ff;		\
134 	BYTE(vdac, bt_hi) = ((regno)& 0xff00) >> 8;	\
135 	tc_wmb();					\
136    } while (0)
137 
138 #define	TWIN(x)	   ((x) | ((x) << 8))
139 #define	TWIN_LO(x) (twin = (x) & 0x00ff, (twin << 8) | twin)
140 #define	TWIN_HI(x) (twin = (x) & 0xff00, twin | (twin >> 8))
141 
142 #define	SELECT431(curs, regno) do {	\
143 	HALF(curs, bt_lo) = TWIN(regno);\
144 	HALF(curs, bt_hi) = 0;		\
145 	tc_wmb();			\
146    } while (0)
147 
148 struct hwcmap256 {
149 #define	CMAP_SIZE	256	/* R/G/B entries */
150 	u_int8_t r[CMAP_SIZE];
151 	u_int8_t g[CMAP_SIZE];
152 	u_int8_t b[CMAP_SIZE];
153 };
154 
155 struct hwcursor64 {
156 	struct wsdisplay_curpos cc_pos;
157 	struct wsdisplay_curpos cc_hot;
158 	struct wsdisplay_curpos cc_size;
159 	struct wsdisplay_curpos cc_magic;
160 #define	CURSOR_MAX_SIZE	64
161 	u_int8_t cc_color[6];
162 	u_int64_t cc_image[64 + 64];
163 };
164 
165 struct tfb_softc {
166 	struct device sc_dev;
167 	vaddr_t sc_vaddr;
168 	size_t sc_size;
169 	struct rasops_info *sc_ri;
170 	struct hwcmap256 sc_cmap;	/* software copy of colormap */
171 	struct hwcursor64 sc_cursor;	/* software copy of cursor */
172 	int sc_blanked;			/* video visibility disabled */
173 	int sc_curenb;			/* cursor sprite enabled */
174 	int sc_changed;			/* need update of hardware */
175 #define	WSDISPLAY_CMAP_DOLUT	0x20
176 	int nscreens;
177 };
178 
179 #define	TX_MAGIC_X	360
180 #define	TX_MAGIC_Y	36
181 
182 #define	TX_BT463_OFFSET	0x040000
183 #define	TX_BT431_OFFSET	0x040010
184 #define	TX_CONTROL	0x040030
185 #define	TX_MAP_REGISTER	0x040030
186 #define	TX_PIP_OFFSET	0x0800c0
187 #define	TX_SELECTION	0x100000
188 #define	TX_8BPP_OFFSET	0x200000
189 #define	TX_8BPP_SIZE	0x200000
190 #define	TX_24BPP_OFFSET	0x400000
191 #define	TX_24BPP_SIZE	0x600000
192 #define	TX_VIDEO_ENABLE	0xa00000
193 
194 #define	TX_CTL_VIDEO_ON	0x80
195 #define	TX_CTL_INT_ENA	0x40
196 #define	TX_CTL_INT_PEND	0x20
197 #define	TX_CTL_SEG_ENA	0x10
198 #define	TX_CTL_SEG	0x0f
199 
200 static int  tfbmatch __P((struct device *, struct cfdata *, void *));
201 static void tfbattach __P((struct device *, struct device *, void *));
202 
203 CFATTACH_DECL(tfb, sizeof(struct tfb_softc),
204     tfbmatch, tfbattach, NULL, NULL);
205 
206 static void tfb_common_init __P((struct rasops_info *));
207 static struct rasops_info tfb_console_ri;
208 static tc_addr_t tfb_consaddr;
209 
210 static struct wsscreen_descr tfb_stdscreen = {
211 	"std", 0, 0,
212 	0, /* textops */
213 	0, 0,
214 	WSSCREEN_REVERSE
215 };
216 
217 static const struct wsscreen_descr *_tfb_scrlist[] = {
218 	&tfb_stdscreen,
219 };
220 
221 static const struct wsscreen_list tfb_screenlist = {
222 	sizeof(_tfb_scrlist) / sizeof(struct wsscreen_descr *), _tfb_scrlist
223 };
224 
225 static int	tfbioctl __P((void *, u_long, caddr_t, int, struct proc *));
226 static paddr_t	tfbmmap __P((void *, off_t, int));
227 
228 static int	tfb_alloc_screen __P((void *, const struct wsscreen_descr *,
229 				      void **, int *, int *, long *));
230 static void	tfb_free_screen __P((void *, void *));
231 static int	tfb_show_screen __P((void *, void *, int,
232 				     void (*) (void *, int, int), void *));
233 
234 static const struct wsdisplay_accessops tfb_accessops = {
235 	tfbioctl,
236 	tfbmmap,
237 	tfb_alloc_screen,
238 	tfb_free_screen,
239 	tfb_show_screen,
240 	0 /* load_font */
241 };
242 
243 int  tfb_cnattach __P((tc_addr_t));
244 static int  tfbintr __P((void *));
245 static void tfbhwinit __P((caddr_t));
246 
247 static int  get_cmap __P((struct tfb_softc *, struct wsdisplay_cmap *));
248 static int  set_cmap __P((struct tfb_softc *, struct wsdisplay_cmap *));
249 static int  set_cursor __P((struct tfb_softc *, struct wsdisplay_cursor *));
250 static int  get_cursor __P((struct tfb_softc *, struct wsdisplay_cursor *));
251 static void set_curpos __P((struct tfb_softc *, struct wsdisplay_curpos *));
252 
253 /* bit order reverse */
254 static const u_int8_t flip[256] = {
255 	0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
256 	0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
257 	0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
258 	0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
259 	0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
260 	0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
261 	0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
262 	0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
263 	0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
264 	0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
265 	0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
266 	0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
267 	0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
268 	0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
269 	0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
270 	0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
271 	0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
272 	0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
273 	0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
274 	0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
275 	0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
276 	0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
277 	0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
278 	0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
279 	0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
280 	0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
281 	0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
282 	0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
283 	0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
284 	0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
285 	0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
286 	0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
287 };
288 
289 static int
290 tfbmatch(parent, match, aux)
291 	struct device *parent;
292 	struct cfdata *match;
293 	void *aux;
294 {
295 	struct tc_attach_args *ta = aux;
296 
297 	if (strncmp("PMAG-RO ", ta->ta_modname, TC_ROM_LLEN) != 0
298 	    && strncmp("PMAG-JA ", ta->ta_modname, TC_ROM_LLEN) != 0)
299 		return (0);
300 
301 	return (1);
302 }
303 
304 
305 static void
306 tfbattach(parent, self, aux)
307 	struct device *parent, *self;
308 	void *aux;
309 {
310 	struct tfb_softc *sc = (struct tfb_softc *)self;
311 	struct tc_attach_args *ta = aux;
312 	struct rasops_info *ri;
313 	struct wsemuldisplaydev_attach_args waa;
314 	struct hwcmap256 *cm;
315 	const u_int8_t *p;
316 	int console, index;
317 
318 	console = (ta->ta_addr == tfb_consaddr);
319 	if (console) {
320 		sc->sc_ri = ri = &tfb_console_ri;
321 		sc->nscreens = 1;
322 	}
323 	else {
324 		MALLOC(ri, struct rasops_info *, sizeof(struct rasops_info),
325 			M_DEVBUF, M_NOWAIT);
326 		if (ri == NULL) {
327 			printf(": can't alloc memory\n");
328 			return;
329 		}
330 		memset(ri, 0, sizeof(struct rasops_info));
331 
332 		ri->ri_hw = (void *)ta->ta_addr;
333 		tfb_common_init(ri);
334 		sc->sc_ri = ri;
335 	}
336 	printf(": %dx%d, 8,24bpp\n", ri->ri_width, ri->ri_height);
337 
338 	cm = &sc->sc_cmap;
339 	p = rasops_cmap;
340 	for (index = 0; index < CMAP_SIZE; index++, p += 3) {
341 		cm->r[index] = p[0];
342 		cm->g[index] = p[1];
343 		cm->b[index] = p[2];
344 	}
345 
346 	sc->sc_vaddr = ta->ta_addr;
347 	sc->sc_cursor.cc_magic.x = TX_MAGIC_X;
348 	sc->sc_cursor.cc_magic.y = TX_MAGIC_Y;
349 	sc->sc_blanked = sc->sc_curenb = 0;
350 
351 	tc_intr_establish(parent, ta->ta_cookie, IPL_TTY, tfbintr, sc);
352 
353 	*(u_int8_t *)((caddr_t)ri->ri_hw + TX_CONTROL) &= ~0x40;
354 	*(u_int8_t *)((caddr_t)ri->ri_hw + TX_CONTROL) |= 0x40;
355 
356 	waa.console = console;
357 	waa.scrdata = &tfb_screenlist;
358 	waa.accessops = &tfb_accessops;
359 	waa.accesscookie = sc;
360 
361 	config_found(self, &waa, wsemuldisplaydevprint);
362 }
363 
364 static void
365 tfb_common_init(ri)
366 	struct rasops_info *ri;
367 {
368 	caddr_t base;
369 	int cookie;
370 
371 	base = (caddr_t)ri->ri_hw;
372 
373 	/* initialize colormap and cursor hardware */
374 	tfbhwinit(base);
375 
376 	ri->ri_flg = RI_CENTER;
377 	ri->ri_depth = 8;
378 	ri->ri_width = 1280;
379 	ri->ri_height = 1024;
380 	ri->ri_stride = 1280;
381 	ri->ri_bits = base + TX_8BPP_OFFSET;
382 
383 	/* clear the screen */
384 	memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
385 
386 	wsfont_init();
387 	/* prefer 12 pixel wide font */
388 	cookie = wsfont_find(NULL, 12, 0, 0, WSDISPLAY_FONTORDER_L2R,
389 	    WSDISPLAY_FONTORDER_L2R);
390 	if (cookie <= 0)
391 		cookie = wsfont_find(NULL, 0, 0, 0, WSDISPLAY_FONTORDER_L2R,
392 		    WSDISPLAY_FONTORDER_L2R);
393 	if (cookie <= 0) {
394 		printf("tfb: font table is empty\n");
395 		return;
396 	}
397 
398 	if (wsfont_lock(cookie, &ri->ri_font)) {
399 		printf("tfb: couldn't lock font\n");
400 		return;
401 	}
402 	ri->ri_wsfcookie = cookie;
403 
404 	rasops_init(ri, 34, 80);
405 
406 	/* XXX shouldn't be global */
407 	tfb_stdscreen.nrows = ri->ri_rows;
408 	tfb_stdscreen.ncols = ri->ri_cols;
409 	tfb_stdscreen.textops = &ri->ri_ops;
410 	tfb_stdscreen.capabilities = ri->ri_caps;
411 }
412 
413 static int
414 tfbioctl(v, cmd, data, flag, p)
415 	void *v;
416 	u_long cmd;
417 	caddr_t data;
418 	int flag;
419 	struct proc *p;
420 {
421 	struct tfb_softc *sc = v;
422 	struct rasops_info *ri = sc->sc_ri;
423 	int turnoff;
424 
425 	switch (cmd) {
426 	case WSDISPLAYIO_GTYPE:
427 		*(u_int *)data = WSDISPLAY_TYPE_TX;
428 		return (0);
429 
430 	case WSDISPLAYIO_GINFO:
431 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
432 		wsd_fbip->height = ri->ri_height;
433 		wsd_fbip->width = ri->ri_width;
434 		wsd_fbip->depth = ri->ri_depth;
435 		wsd_fbip->cmsize = CMAP_SIZE;
436 #undef fbt
437 		return (0);
438 
439 	case WSDISPLAYIO_GETCMAP:
440 		return get_cmap(sc, (struct wsdisplay_cmap *)data);
441 
442 	case WSDISPLAYIO_PUTCMAP:
443 		return set_cmap(sc, (struct wsdisplay_cmap *)data);
444 
445 	case WSDISPLAYIO_SVIDEO:
446 		turnoff = *(int *)data == WSDISPLAYIO_VIDEO_OFF;
447 		if ((sc->sc_blanked == 0) ^ turnoff) {
448 			sc->sc_blanked = turnoff;
449 #if 0	/* XXX later XXX */
450 	To turn off;
451 	- clear the MSB of TX control register; &= ~0x80,
452 	- assign Bt431 register #0 with value 0x4 to hide sprite cursor.
453 #endif	/* XXX XXX XXX */
454 		}
455 		return (0);
456 
457 	case WSDISPLAYIO_GVIDEO:
458 		*(u_int *)data = sc->sc_blanked ?
459 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
460 		return (0);
461 
462 	case WSDISPLAYIO_GCURPOS:
463 		*(struct wsdisplay_curpos *)data = sc->sc_cursor.cc_pos;
464 		return (0);
465 
466 	case WSDISPLAYIO_SCURPOS:
467 		set_curpos(sc, (struct wsdisplay_curpos *)data);
468 		sc->sc_changed = WSDISPLAY_CURSOR_DOPOS;
469 		return (0);
470 
471 	case WSDISPLAYIO_GCURMAX:
472 		((struct wsdisplay_curpos *)data)->x =
473 		((struct wsdisplay_curpos *)data)->y = CURSOR_MAX_SIZE;
474 		return (0);
475 
476 	case WSDISPLAYIO_GCURSOR:
477 		return get_cursor(sc, (struct wsdisplay_cursor *)data);
478 
479 	case WSDISPLAYIO_SCURSOR:
480 		return set_cursor(sc, (struct wsdisplay_cursor *)data);
481 	}
482 	return (EPASSTHROUGH);
483 }
484 
485 static paddr_t
486 tfbmmap(v, offset, prot)
487 	void *v;
488 	off_t offset;
489 	int prot;
490 {
491 	struct tfb_softc *sc = v;
492 
493 	if (offset >= TX_8BPP_SIZE || offset < 0) /* XXX 24bpp XXX */
494 		return (-1);
495 	return machine_btop(sc->sc_vaddr + TX_8BPP_OFFSET + offset);
496 }
497 
498 static int
499 tfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
500 	void *v;
501 	const struct wsscreen_descr *type;
502 	void **cookiep;
503 	int *curxp, *curyp;
504 	long *attrp;
505 {
506 	struct tfb_softc *sc = v;
507 	struct rasops_info *ri = sc->sc_ri;
508 	long defattr;
509 
510 	if (sc->nscreens > 0)
511 		return (ENOMEM);
512 
513 	*cookiep = ri; /* one and only for now */
514 	*curxp = 0;
515 	*curyp = 0;
516 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
517 	*attrp = defattr;
518 	sc->nscreens++;
519 	return (0);
520 }
521 
522 static void
523 tfb_free_screen(v, cookie)
524 	void *v;
525 	void *cookie;
526 {
527 	struct tfb_softc *sc = v;
528 
529 	if (sc->sc_ri == &tfb_console_ri)
530 		panic("tfb_free_screen: console");
531 
532 	sc->nscreens--;
533 }
534 
535 static int
536 tfb_show_screen(v, cookie, waitok, cb, cbarg)
537 	void *v;
538 	void *cookie;
539 	int waitok;
540 	void (*cb) __P((void *, int, int));
541 	void *cbarg;
542 {
543 
544 	return (0);
545 }
546 
547 /* EXPORT */ int
548 tfb_cnattach(addr)
549 	tc_addr_t addr;
550 {
551 	struct rasops_info *ri;
552 	long defattr;
553 
554 	ri = &tfb_console_ri;
555 	ri->ri_hw = (void *)addr;
556 	tfb_common_init(ri);
557 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
558 	wsdisplay_cnattach(&tfb_stdscreen, ri, 0, 0, defattr);
559 	tfb_consaddr = addr;
560 	return (0);
561 }
562 
563 static int
564 tfbintr(arg)
565 	void *arg;
566 {
567 	struct tfb_softc *sc = arg;
568 	caddr_t base, vdac, curs;
569 	int v;
570 
571 	base = (caddr_t)sc->sc_ri->ri_hw;
572 	*(u_int8_t *)(base + TX_CONTROL) &= ~0x40;
573 	if (sc->sc_changed == 0)
574 		goto done;
575 
576 	vdac = base + TX_BT463_OFFSET;
577 	curs = base + TX_BT431_OFFSET;
578 	v = sc->sc_changed;
579 	if (v & WSDISPLAY_CURSOR_DOCUR) {
580 		SELECT431(curs, BT431_REG_COMMAND);
581 		HALF(curs, bt_ctl) = (sc->sc_curenb) ? 0x4444 : 0x0404;
582 	}
583 	if (v & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
584 		int x, y;
585 		u_int16_t twin;
586 
587 		x = sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x;
588 		y = sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y;
589 
590 		x += sc->sc_cursor.cc_magic.x;
591 		y += sc->sc_cursor.cc_magic.y;
592 
593 		SELECT431(curs, BT431_REG_CURSOR_X_LOW);
594 		HALF(curs, bt_ctl) = TWIN_LO(x);	tc_wmb();
595 		HALF(curs, bt_ctl) = TWIN_HI(x);	tc_wmb();
596 		HALF(curs, bt_ctl) = TWIN_LO(y);	tc_wmb();
597 		HALF(curs, bt_ctl) = TWIN_HI(y);	tc_wmb();
598 	}
599 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
600 		u_int8_t *cp = sc->sc_cursor.cc_color;
601 
602 		SELECT463(vdac, BT463_IREG_CURSOR_COLOR_0);
603 		BYTE(vdac, bt_reg) = cp[1]; tc_wmb();
604 		BYTE(vdac, bt_reg) = cp[3]; tc_wmb();
605 		BYTE(vdac, bt_reg) = cp[5]; tc_wmb();
606 
607 		BYTE(vdac, bt_reg) = cp[0]; tc_wmb();
608 		BYTE(vdac, bt_reg) = cp[2]; tc_wmb();
609 		BYTE(vdac, bt_reg) = cp[4]; tc_wmb();
610 
611 		BYTE(vdac, bt_reg) = cp[1]; tc_wmb();
612 		BYTE(vdac, bt_reg) = cp[3]; tc_wmb();
613 		BYTE(vdac, bt_reg) = cp[5]; tc_wmb();
614 
615 		BYTE(vdac, bt_reg) = cp[1]; tc_wmb();
616 		BYTE(vdac, bt_reg) = cp[3]; tc_wmb();
617 		BYTE(vdac, bt_reg) = cp[5]; tc_wmb();
618 	}
619 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
620 		u_int8_t *ip, *mp, img, msk;
621 		int bcnt;
622 
623 		ip = (u_int8_t *)sc->sc_cursor.cc_image;
624 		mp = (u_int8_t *)(sc->sc_cursor.cc_image + CURSOR_MAX_SIZE);
625 		bcnt = 0;
626 		SELECT431(curs, BT431_REG_CRAM_BASE);
627 
628 		/* 64 pixel scan line is consisted with 16 byte cursor ram */
629 		while (bcnt < sc->sc_cursor.cc_size.y * 16) {
630 			/* pad right half 32 pixel when smaller than 33 */
631 			if ((bcnt & 0x8) && sc->sc_cursor.cc_size.x < 33) {
632 				HALF(curs, bt_ram) = 0;
633 				tc_wmb();
634 			}
635 			else {
636 				img = *ip++;
637 				msk = *mp++;
638 				img &= msk;	/* cookie off image */
639 				HALF(curs, bt_ram)
640 				    = (flip[img] << 8) | flip[msk];
641 				tc_wmb();
642 			}
643 			bcnt += 2;
644 		}
645 		/* pad unoccupied scan lines */
646 		while (bcnt < CURSOR_MAX_SIZE * 16) {
647 			HALF(curs, bt_ram) = 0;
648 			tc_wmb();
649 			bcnt += 2;
650 		}
651 	}
652 	if (v & WSDISPLAY_CMAP_DOLUT) {
653 		struct hwcmap256 *cm = &sc->sc_cmap;
654 		int index;
655 
656 		SELECT463(vdac, BT463_IREG_CPALETTE_RAM);
657 		for (index = 0; index < CMAP_SIZE; index++) {
658 			BYTE(vdac, bt_cmap) = cm->r[index];
659 			BYTE(vdac, bt_cmap) = cm->g[index];
660 			BYTE(vdac, bt_cmap) = cm->b[index];
661 		}
662 	}
663 	sc->sc_changed = 0;
664 done:
665 	*(u_int8_t *)(base + TX_CONTROL) &= ~0x40;	/* !? Eeeh !? */
666 	*(u_int8_t *)(base + TX_CONTROL) |= 0x40;
667 	return (1);
668 }
669 
670 static void
671 tfbhwinit(tfbbase)
672 	caddr_t tfbbase;
673 {
674 	caddr_t vdac, curs;
675 	const u_int8_t *p;
676 	int i;
677 
678 	vdac = tfbbase + TX_BT463_OFFSET;
679 	curs = tfbbase + TX_BT431_OFFSET;
680 	SELECT463(vdac, BT463_IREG_COMMAND_0);
681 	BYTE(vdac, bt_reg) = 0x40;	tc_wmb();	/* CMD 0 */
682 	BYTE(vdac, bt_reg) = 0x46;	tc_wmb();	/* CMD 1 */
683 	BYTE(vdac, bt_reg) = 0xc0;	tc_wmb();	/* CMD 2 */
684 	BYTE(vdac, bt_reg) = 0;		tc_wmb();	/* !? 204 !? */
685 	BYTE(vdac, bt_reg) = 0xff;	tc_wmb();	/* plane  0:7  */
686 	BYTE(vdac, bt_reg) = 0xff;	tc_wmb();	/* plane  8:15 */
687 	BYTE(vdac, bt_reg) = 0xff;	tc_wmb();	/* plane 16:23 */
688 	BYTE(vdac, bt_reg) = 0xff;	tc_wmb();	/* plane 24:27 */
689 	BYTE(vdac, bt_reg) = 0x00;	tc_wmb();	/* blink  0:7  */
690 	BYTE(vdac, bt_reg) = 0x00;	tc_wmb();	/* blink  8:15 */
691 	BYTE(vdac, bt_reg) = 0x00;	tc_wmb();	/* blink 16:23 */
692 	BYTE(vdac, bt_reg) = 0x00;	tc_wmb();	/* blink 24:27 */
693 	BYTE(vdac, bt_reg) = 0x00;	tc_wmb();
694 
695 #if 0 /* XXX ULTRIX does initialize 16 entry window type here XXX */
696   {
697 	static u_int32_t windowtype[BT463_IREG_WINDOW_TYPE_TABLE] = {
698 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
699 	};
700 
701 	SELECT463(vdac, BT463_IREG_WINDOW_TYPE_TABLE);
702 	for (i = 0; i < BT463_NWTYPE_ENTRIES; i++) {
703 		BYTE(vdac, bt_reg) = windowtype[i];	  /*   0:7  */
704 		BYTE(vdac, bt_reg) = windowtype[i] >> 8;  /*   8:15 */
705 		BYTE(vdac, bt_reg) = windowtype[i] >> 16; /*  16:23 */
706 	}
707   }
708 #endif
709 
710 	SELECT463(vdac, BT463_IREG_CPALETTE_RAM);
711 	p = rasops_cmap;
712 	for (i = 0; i < 256; i++, p += 3) {
713 		BYTE(vdac, bt_cmap) = p[0];	tc_wmb();
714 		BYTE(vdac, bt_cmap) = p[1];	tc_wmb();
715 		BYTE(vdac, bt_cmap) = p[2];	tc_wmb();
716 	}
717 
718 	/* !? Eeeh !? */
719 	SELECT463(vdac, 0x0100 /* BT463_IREG_CURSOR_COLOR_0 */);
720 	for (i = 0; i < 256; i++) {
721 		BYTE(vdac, bt_cmap) = i;	tc_wmb();
722 		BYTE(vdac, bt_cmap) = i;	tc_wmb();
723 		BYTE(vdac, bt_cmap) = i;	tc_wmb();
724 	}
725 
726 	SELECT431(curs, BT431_REG_COMMAND);
727 	HALF(curs, bt_ctl) = 0x0404;		tc_wmb();
728 	HALF(curs, bt_ctl) = 0; /* XLO */	tc_wmb();
729 	HALF(curs, bt_ctl) = 0; /* XHI */	tc_wmb();
730 	HALF(curs, bt_ctl) = 0; /* YLO */	tc_wmb();
731 	HALF(curs, bt_ctl) = 0; /* YHI */	tc_wmb();
732 	HALF(curs, bt_ctl) = 0; /* XWLO */	tc_wmb();
733 	HALF(curs, bt_ctl) = 0; /* XWHI */	tc_wmb();
734 	HALF(curs, bt_ctl) = 0; /* WYLO */	tc_wmb();
735 	HALF(curs, bt_ctl) = 0; /* WYLO */	tc_wmb();
736 	HALF(curs, bt_ctl) = 0; /* WWLO */	tc_wmb();
737 	HALF(curs, bt_ctl) = 0; /* WWHI */	tc_wmb();
738 	HALF(curs, bt_ctl) = 0; /* WHLO */	tc_wmb();
739 	HALF(curs, bt_ctl) = 0; /* WHHI */	tc_wmb();
740 
741 	SELECT431(curs, BT431_REG_CRAM_BASE);
742 	for (i = 0; i < 512; i++) {
743 		HALF(curs, bt_ram) = 0;	tc_wmb();
744 	}
745 }
746 
747 static int
748 get_cmap(sc, p)
749 	struct tfb_softc *sc;
750 	struct wsdisplay_cmap *p;
751 {
752 	u_int index = p->index, count = p->count;
753 
754 	if (index >= CMAP_SIZE || count > CMAP_SIZE - index)
755 		return (EINVAL);
756 
757 	if (!uvm_useracc(p->red, count, B_WRITE) ||
758 	    !uvm_useracc(p->green, count, B_WRITE) ||
759 	    !uvm_useracc(p->blue, count, B_WRITE))
760 		return (EFAULT);
761 
762 	copyout(&sc->sc_cmap.r[index], p->red, count);
763 	copyout(&sc->sc_cmap.g[index], p->green, count);
764 	copyout(&sc->sc_cmap.b[index], p->blue, count);
765 
766 	return (0);
767 }
768 
769 static int
770 set_cmap(sc, p)
771 	struct tfb_softc *sc;
772 	struct wsdisplay_cmap *p;
773 {
774 	u_int index = p->index, count = p->count;
775 
776 	if (index >= CMAP_SIZE || count > CMAP_SIZE - index)
777 		return (EINVAL);
778 
779 	if (!uvm_useracc(p->red, count, B_READ) ||
780 	    !uvm_useracc(p->green, count, B_READ) ||
781 	    !uvm_useracc(p->blue, count, B_READ))
782 		return (EFAULT);
783 
784 	copyin(p->red, &sc->sc_cmap.r[index], count);
785 	copyin(p->green, &sc->sc_cmap.g[index], count);
786 	copyin(p->blue, &sc->sc_cmap.b[index], count);
787 
788 	sc->sc_changed |= WSDISPLAY_CMAP_DOLUT;
789 
790 	return (0);
791 }
792 
793 static int
794 set_cursor(sc, p)
795 	struct tfb_softc *sc;
796 	struct wsdisplay_cursor *p;
797 {
798 #define	cc (&sc->sc_cursor)
799 	u_int v, index, count, icount;
800 
801 	v = p->which;
802 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
803 		index = p->cmap.index;
804 		count = p->cmap.count;
805 		if (index >= 2 || (index + count) > 2)
806 			return (EINVAL);
807 		if (!uvm_useracc(p->cmap.red, count, B_READ) ||
808 		    !uvm_useracc(p->cmap.green, count, B_READ) ||
809 		    !uvm_useracc(p->cmap.blue, count, B_READ))
810 			return (EFAULT);
811 	}
812 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
813 		if (p->size.x > CURSOR_MAX_SIZE || p->size.y > CURSOR_MAX_SIZE)
814 			return (EINVAL);
815 		icount = ((p->size.x < 33) ? 4 : 8) * p->size.y;
816 		if (!uvm_useracc(p->image, icount, B_READ) ||
817 		    !uvm_useracc(p->mask, icount, B_READ))
818 			return (EFAULT);
819 	}
820 
821 	if (v & WSDISPLAY_CURSOR_DOCUR)
822 		sc->sc_curenb = p->enable;
823 	if (v & WSDISPLAY_CURSOR_DOPOS)
824 		set_curpos(sc, &p->pos);
825 	if (v & WSDISPLAY_CURSOR_DOHOT)
826 		cc->cc_hot = p->hot;
827 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
828 		copyin(p->cmap.red, &cc->cc_color[index], count);
829 		copyin(p->cmap.green, &cc->cc_color[index + 2], count);
830 		copyin(p->cmap.blue, &cc->cc_color[index + 4], count);
831 	}
832 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
833 		cc->cc_size = p->size;
834 		memset(cc->cc_image, 0, sizeof cc->cc_image);
835 		copyin(p->image, cc->cc_image, icount);
836 		copyin(p->mask, cc->cc_image+CURSOR_MAX_SIZE, icount);
837 	}
838 	sc->sc_changed = v;
839 
840 	return (0);
841 #undef cc
842 }
843 
844 static int
845 get_cursor(sc, p)
846 	struct tfb_softc *sc;
847 	struct wsdisplay_cursor *p;
848 {
849 	return (EPASSTHROUGH); /* XXX */
850 }
851 
852 static void
853 set_curpos(sc, curpos)
854 	struct tfb_softc *sc;
855 	struct wsdisplay_curpos *curpos;
856 {
857 	struct rasops_info *ri = sc->sc_ri;
858 	int x = curpos->x, y = curpos->y;
859 
860 	if (y < 0)
861 		y = 0;
862 	else if (y > ri->ri_height)
863 		y = ri->ri_height;
864 	if (x < 0)
865 		x = 0;
866 	else if (x > ri->ri_width)
867 		x = ri->ri_width;
868 	sc->sc_cursor.cc_pos.x = x;
869 	sc->sc_cursor.cc_pos.y = y;
870 }
871