Home | History | Annotate | Line # | Download | only in tc
tfb.c revision 1.29.2.2
      1 /* $NetBSD: tfb.c,v 1.29.2.2 2001/09/21 22:36:18 nathanw 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>			/* RCS ID & Copyright macro defns */
     34 
     35 __KERNEL_RCSID(0, "$NetBSD: tfb.c,v 1.29.2.2 2001/09/21 22:36:18 nathanw Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 #include <sys/buf.h>
     43 #include <sys/ioctl.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/intr.h>
     47 
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wscons/wsdisplayvar.h>
     50 
     51 #include <dev/rasops/rasops.h>
     52 #include <dev/wsfont/wsfont.h>
     53 
     54 #include <dev/tc/tcvar.h>
     55 #include <dev/ic/bt463reg.h>
     56 #include <dev/ic/bt431reg.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 
     60 #if defined(pmax)
     61 #define	machine_btop(x) mips_btop(MIPS_KSEG1_TO_PHYS(x))
     62 
     63 /*
     64  * struct bt463reg {
     65  * 	u_int8_t	bt_lo;
     66  * 	unsigned : 24;
     67  * 	u_int8_t	bt_hi;
     68  * 	unsigned : 24;
     69  * 	u_int8_t	bt_reg;
     70  * 	unsigned : 24;
     71  * 	u_int8_t	bt_cmap;
     72  * };
     73  *
     74  * N.B. a pair of Bt431s are located adjascently.
     75  * 	struct bt431twin {
     76  *		struct {
     77  *			u_int8_t u0;	for sprite mask
     78  *			u_int8_t u1;	for sprite image
     79  *			unsigned :16;
     80  *		} bt_lo;
     81  *		...
     82  *
     83  * struct bt431reg {
     84  * 	u_int16_t	bt_lo;
     85  * 	unsigned : 16;
     86  * 	u_int16_t	bt_hi;
     87  * 	unsigned : 16;
     88  * 	u_int16_t	bt_ram;
     89  * 	unsigned : 16;
     90  * 	u_int16_t	bt_ctl;
     91  * };
     92  */
     93 
     94 #define	BYTE(base, index)	*((u_int8_t *)(base) + ((index)<<2))
     95 #define	HALF(base, index)	*((u_int16_t *)(base) + ((index)<<1))
     96 
     97 #endif
     98 
     99 #if defined(alpha)
    100 #define	machine_btop(x) alpha_btop(ALPHA_K0SEG_TO_PHYS(x))
    101 
    102 /*
    103  * struct bt463reg {
    104  * 	u_int32_t	bt_lo;
    105  * 	u_int32_t	bt_hi;
    106  * 	u_int32_t	bt_reg;
    107  * 	u_int32_t	bt_cmap;
    108  * };
    109  *
    110  * struct bt431reg {
    111  * 	u_int32_t	bt_lo;
    112  * 	u_int32_t	bt_hi;
    113  * 	u_int32_t	bt_ram;
    114  * 	u_int32_t	bt_ctl;
    115  * };
    116  */
    117 
    118 #define	BYTE(base, index)	*((u_int32_t *)(base) + (index))
    119 #define	HALF(base, index)	*((u_int32_t *)(base) + (index))
    120 
    121 #endif
    122 
    123 /* Bt463 hardware registers */
    124 #define	bt_lo	0
    125 #define	bt_hi	1
    126 #define	bt_reg	2
    127 #define	bt_cmap	3
    128 
    129 /* Bt431 hardware registers */
    130 #define	bt_ram	2
    131 #define	bt_ctl	3
    132 
    133 #define	SELECT463(vdac, regno) do {			\
    134 	BYTE(vdac, bt_lo) = (regno) & 0x00ff;		\
    135 	BYTE(vdac, bt_hi) = ((regno)& 0xff00) >> 8;	\
    136 	tc_wmb();					\
    137    } while (0)
    138 
    139 #define	TWIN(x)	   ((x) | ((x) << 8))
    140 #define	TWIN_LO(x) (twin = (x) & 0x00ff, (twin << 8) | twin)
    141 #define	TWIN_HI(x) (twin = (x) & 0xff00, twin | (twin >> 8))
    142 
    143 #define	SELECT431(curs, regno) do {	\
    144 	HALF(curs, bt_lo) = TWIN(regno);\
    145 	HALF(curs, bt_hi) = 0;		\
    146 	tc_wmb();			\
    147    } while (0)
    148 
    149 struct hwcmap256 {
    150 #define	CMAP_SIZE	256	/* R/G/B entries */
    151 	u_int8_t r[CMAP_SIZE];
    152 	u_int8_t g[CMAP_SIZE];
    153 	u_int8_t b[CMAP_SIZE];
    154 };
    155 
    156 struct hwcursor64 {
    157 	struct wsdisplay_curpos cc_pos;
    158 	struct wsdisplay_curpos cc_hot;
    159 	struct wsdisplay_curpos cc_size;
    160 	struct wsdisplay_curpos cc_magic;
    161 #define	CURSOR_MAX_SIZE	64
    162 	u_int8_t cc_color[6];
    163 	u_int64_t cc_image[64 + 64];
    164 };
    165 
    166 struct tfb_softc {
    167 	struct device sc_dev;
    168 	vaddr_t sc_vaddr;
    169 	size_t sc_size;
    170 	struct rasops_info *sc_ri;
    171 	struct hwcmap256 sc_cmap;	/* software copy of colormap */
    172 	struct hwcursor64 sc_cursor;	/* software copy of cursor */
    173 	int sc_blanked;			/* video visibility disabled */
    174 	int sc_curenb;			/* cursor sprite enabled */
    175 	int sc_changed;			/* need update of hardware */
    176 #define	WSDISPLAY_CMAP_DOLUT	0x20
    177 	int nscreens;
    178 };
    179 
    180 #define	TX_MAGIC_X	360
    181 #define	TX_MAGIC_Y	36
    182 
    183 #define	TX_BT463_OFFSET	0x040000
    184 #define	TX_BT431_OFFSET	0x040010
    185 #define	TX_CONTROL	0x040030
    186 #define	TX_MAP_REGISTER	0x040030
    187 #define	TX_PIP_OFFSET	0x0800c0
    188 #define	TX_SELECTION	0x100000
    189 #define	TX_8BPP_OFFSET	0x200000
    190 #define	TX_8BPP_SIZE	0x200000
    191 #define	TX_24BPP_OFFSET	0x400000
    192 #define	TX_24BPP_SIZE	0x600000
    193 #define	TX_VIDEO_ENABLE	0xa00000
    194 
    195 #define	TX_CTL_VIDEO_ON	0x80
    196 #define	TX_CTL_INT_ENA	0x40
    197 #define	TX_CTL_INT_PEND	0x20
    198 #define	TX_CTL_SEG_ENA	0x10
    199 #define	TX_CTL_SEG	0x0f
    200 
    201 static int  tfbmatch __P((struct device *, struct cfdata *, void *));
    202 static void tfbattach __P((struct device *, struct device *, void *));
    203 
    204 const struct cfattach tfb_ca = {
    205 	sizeof(struct tfb_softc), tfbmatch, tfbattach,
    206 };
    207 
    208 static void tfb_common_init __P((struct rasops_info *));
    209 static struct rasops_info tfb_console_ri;
    210 static tc_addr_t tfb_consaddr;
    211 
    212 static struct wsscreen_descr tfb_stdscreen = {
    213 	"std", 0, 0,
    214 	0, /* textops */
    215 	0, 0,
    216 	WSSCREEN_REVERSE
    217 };
    218 
    219 static const struct wsscreen_descr *_tfb_scrlist[] = {
    220 	&tfb_stdscreen,
    221 };
    222 
    223 static const struct wsscreen_list tfb_screenlist = {
    224 	sizeof(_tfb_scrlist) / sizeof(struct wsscreen_descr *), _tfb_scrlist
    225 };
    226 
    227 static int	tfbioctl __P((void *, u_long, caddr_t, int, struct proc *));
    228 static paddr_t	tfbmmap __P((void *, off_t, int));
    229 
    230 static int	tfb_alloc_screen __P((void *, const struct wsscreen_descr *,
    231 				      void **, int *, int *, long *));
    232 static void	tfb_free_screen __P((void *, void *));
    233 static int	tfb_show_screen __P((void *, void *, int,
    234 				     void (*) (void *, int, int), void *));
    235 
    236 static const struct wsdisplay_accessops tfb_accessops = {
    237 	tfbioctl,
    238 	tfbmmap,
    239 	tfb_alloc_screen,
    240 	tfb_free_screen,
    241 	tfb_show_screen,
    242 	0 /* load_font */
    243 };
    244 
    245 int  tfb_cnattach __P((tc_addr_t));
    246 static int  tfbintr __P((void *));
    247 static void tfbhwinit __P((caddr_t));
    248 
    249 static int  get_cmap __P((struct tfb_softc *, struct wsdisplay_cmap *));
    250 static int  set_cmap __P((struct tfb_softc *, struct wsdisplay_cmap *));
    251 static int  set_cursor __P((struct tfb_softc *, struct wsdisplay_cursor *));
    252 static int  get_cursor __P((struct tfb_softc *, struct wsdisplay_cursor *));
    253 static void set_curpos __P((struct tfb_softc *, struct wsdisplay_curpos *));
    254 
    255 /* bit order reverse */
    256 static const u_int8_t flip[256] = {
    257 	0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
    258 	0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
    259 	0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
    260 	0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
    261 	0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
    262 	0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
    263 	0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
    264 	0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
    265 	0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
    266 	0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
    267 	0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
    268 	0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
    269 	0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
    270 	0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
    271 	0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
    272 	0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
    273 	0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
    274 	0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
    275 	0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
    276 	0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
    277 	0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
    278 	0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
    279 	0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
    280 	0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
    281 	0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
    282 	0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
    283 	0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
    284 	0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
    285 	0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
    286 	0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
    287 	0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
    288 	0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
    289 };
    290 
    291 static int
    292 tfbmatch(parent, match, aux)
    293 	struct device *parent;
    294 	struct cfdata *match;
    295 	void *aux;
    296 {
    297 	struct tc_attach_args *ta = aux;
    298 
    299 	if (strncmp("PMAG-RO ", ta->ta_modname, TC_ROM_LLEN) != 0
    300 	    && strncmp("PMAG-JA ", ta->ta_modname, TC_ROM_LLEN) != 0)
    301 		return (0);
    302 
    303 	return (1);
    304 }
    305 
    306 
    307 static void
    308 tfbattach(parent, self, aux)
    309 	struct device *parent, *self;
    310 	void *aux;
    311 {
    312 	struct tfb_softc *sc = (struct tfb_softc *)self;
    313 	struct tc_attach_args *ta = aux;
    314 	struct rasops_info *ri;
    315 	struct wsemuldisplaydev_attach_args waa;
    316 	struct hwcmap256 *cm;
    317 	const u_int8_t *p;
    318 	int console, index;
    319 
    320 	console = (ta->ta_addr == tfb_consaddr);
    321 	if (console) {
    322 		sc->sc_ri = ri = &tfb_console_ri;
    323 		sc->nscreens = 1;
    324 	}
    325 	else {
    326 		MALLOC(ri, struct rasops_info *, sizeof(struct rasops_info),
    327 			M_DEVBUF, M_NOWAIT);
    328 		if (ri == NULL) {
    329 			printf(": can't alloc memory\n");
    330 			return;
    331 		}
    332 		memset(ri, 0, sizeof(struct rasops_info));
    333 
    334 		ri->ri_hw = (void *)ta->ta_addr;
    335 		tfb_common_init(ri);
    336 		sc->sc_ri = ri;
    337 	}
    338 	printf(": %dx%d, 8,24bpp\n", ri->ri_width, ri->ri_height);
    339 
    340 	cm = &sc->sc_cmap;
    341 	p = rasops_cmap;
    342 	for (index = 0; index < CMAP_SIZE; index++, p += 3) {
    343 		cm->r[index] = p[0];
    344 		cm->g[index] = p[1];
    345 		cm->b[index] = p[2];
    346 	}
    347 
    348 	sc->sc_vaddr = ta->ta_addr;
    349 	sc->sc_cursor.cc_magic.x = TX_MAGIC_X;
    350 	sc->sc_cursor.cc_magic.y = TX_MAGIC_Y;
    351 	sc->sc_blanked = sc->sc_curenb = 0;
    352 
    353 	tc_intr_establish(parent, ta->ta_cookie, IPL_TTY, tfbintr, sc);
    354 
    355 	*(u_int8_t *)((caddr_t)ri->ri_hw + TX_CONTROL) &= ~0x40;
    356 	*(u_int8_t *)((caddr_t)ri->ri_hw + TX_CONTROL) |= 0x40;
    357 
    358 	waa.console = console;
    359 	waa.scrdata = &tfb_screenlist;
    360 	waa.accessops = &tfb_accessops;
    361 	waa.accesscookie = sc;
    362 
    363 	config_found(self, &waa, wsemuldisplaydevprint);
    364 }
    365 
    366 static void
    367 tfb_common_init(ri)
    368 	struct rasops_info *ri;
    369 {
    370 	caddr_t base;
    371 	int cookie;
    372 
    373 	base = (caddr_t)ri->ri_hw;
    374 
    375 	/* initialize colormap and cursor hardware */
    376 	tfbhwinit(base);
    377 
    378 	ri->ri_flg = RI_CENTER;
    379 	ri->ri_depth = 8;
    380 	ri->ri_width = 1280;
    381 	ri->ri_height = 1024;
    382 	ri->ri_stride = 1280;
    383 	ri->ri_bits = base + TX_8BPP_OFFSET;
    384 
    385 	/* clear the screen */
    386 	memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
    387 
    388 	wsfont_init();
    389 	/* prefer 12 pixel wide font */
    390 	if ((cookie = wsfont_find(NULL, 12, 0, 0)) <= 0)
    391 		cookie = wsfont_find(NULL, 0, 0, 0);
    392 	if (cookie <= 0) {
    393 		printf("tfb: font table is empty\n");
    394 		return;
    395 	}
    396 
    397 	if (wsfont_lock(cookie, &ri->ri_font,
    398 	    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R) <= 0) {
    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 (ENOTTY);
    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.alloc_attr)(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.alloc_attr)(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 || (index + count) > CMAP_SIZE)
    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 || (index + count) > CMAP_SIZE)
    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 (ENOTTY); /* 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