Home | History | Annotate | Line # | Download | only in vsa
spx.c revision 1.2.2.3
      1 /*	$NetBSD: spx.c,v 1.2.2.3 2010/08/11 22:52:52 yamt Exp $ */
      2 /*
      3  * SPX/LCSPX/SPXg/SPXgt accelerated framebuffer driver for NetBSD/VAX
      4  * Copyright (c) 2005 Blaz Antonic
      5  * All rights reserved.
      6  *
      7  * This software contains code written by Michael L. Hitch.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the abovementioned copyrights
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: spx.c,v 1.2.2.3 2010/08/11 22:52:52 yamt Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/device.h>
     39 #include <sys/systm.h>
     40 #include <sys/callout.h>
     41 #include <sys/time.h>
     42 #include <sys/malloc.h>
     43 #include <sys/conf.h>
     44 #include <sys/kernel.h>
     45 
     46 #include <uvm/uvm.h>
     47 
     48 #include <machine/vsbus.h>
     49 #include <machine/sid.h>
     50 #include <machine/cpu.h>
     51 #include <machine/ka420.h>
     52 
     53 #include <dev/cons.h>
     54 
     55 #include <dev/dec/dzreg.h>
     56 #include <dev/dec/dzvar.h>
     57 #include <dev/dec/dzkbdvar.h>
     58 
     59 #include <dev/wscons/wsdisplayvar.h>
     60 #include <dev/wscons/wsconsio.h>
     61 #include <dev/wscons/wscons_callbacks.h>
     62 #include <dev/wsfont/wsfont.h>
     63 
     64 #include "machine/scb.h"
     65 
     66 #include "dzkbd.h"
     67 
     68 #define CONF_LCSPX		0x02
     69 #define CONF_SPXg		0x10
     70 
     71 #define FB_IS_SPX		1
     72 #define FB_IS_SPXg		2
     73 
     74 /* Screen hardware defs */
     75 #define	SPX_FB_ADDR		0x38000000	/* Frame buffer */
     76 #define SPXg_FB_ADDR_KA46	0x22200000
     77 #define SPXg_FB_ADDR_KA49	0x28200000
     78 #define SPXg_FB_ADDR		((vax_boardtype == VAX_BTYP_46) ? \
     79 				 SPXg_FB_ADDR_KA46 : SPXg_FB_ADDR_KA49)
     80 
     81 #define SPXg_WIN_SIZE		0x00100000	/* Window size */
     82 /* # of pixels that fit into single window */
     83 #define SPXg_WIN_LINEAR		(SPXg_WIN_SIZE / 2)
     84 
     85 #define SPXg_DELAY		5
     86 
     87 /*
     88  * off-screen font storage space
     89  * 32x16 glyphs, 256 regular and underlined chars
     90  */
     91 #define FONT_STORAGE_START	(spx_xsize * spx_ysize)
     92 #define FONT_STORAGE_SIZE	(32 * 16 * 256 * 2)
     93 
     94 /* register space defines */
     95 #define SPX_REG_SIZE		0x00002000
     96 #define SPX_REG_ADDR		0x39302000	/* 1st set of SPX registers */
     97 
     98 #define SPXg_REG_SIZE		0x00004000
     99 #define SPXg_REG_ADDR_KA46	0x22004000
    100 #define SPXg_REG_ADDR_KA49	0x28004000
    101 #define SPXg_REG_ADDR		((vax_boardtype == VAX_BTYP_46) ? \
    102 				 SPXg_REG_ADDR_KA46 : SPXg_REG_ADDR_KA49)
    103 
    104 #define SPX_REG1_SIZE		0x00001000
    105 #define SPX_REG1_ADDR		0x39b00000	/* 2nd set of SPX registers */
    106 
    107 #define SPXg_REG1_SIZE		0x00001000
    108 #define SPXg_REG1_ADDR_KA46	0x22100000
    109 #define SPXg_REG1_ADDR_KA49	0x28100000
    110 #define SPXg_REG1_ADDR		((vax_boardtype == VAX_BTYP_46) ? \
    111 				 SPXg_REG1_ADDR_KA46 : SPXg_REG1_ADDR_KA49)
    112 #define SPX_REG(reg)		regaddr[(reg - 0x2000) / 4]
    113 #define SPXg_REG(reg)		regaddr[(reg - 0x2000) / 2]
    114 
    115 #define SPX_REG1(reg)		regaddr1[(reg) / 4]
    116 #define SPXg_REG1(reg)		regaddr1[(reg) / 4]
    117 
    118 /* few SPX register names */
    119 #define SPX_COMMAND		0x21ec
    120 #define SPX_XSTART		0x21d0
    121 #define SPX_YSTART		0x21d4
    122 #define SPX_XEND		0x21d8
    123 #define SPX_YEND		0x21dc
    124 #define SPX_DSTPIX		0x21e0
    125 #define SPX_DSTPIX1		0x20e0
    126 #define SPX_SRCPIX		0x21e4
    127 #define SPX_SRCPIX1		0x20e4
    128 #define SPX_MAIN3		0x219c
    129 #define SPX_STRIDE		0x21e8	/* SRC | DST */
    130 #define SPX_SRCMASK		0x21f0
    131 #define SPX_DSTMASK		0x21f4
    132 #define SPX_FG			0x2260
    133 #define SPX_BG			0x2264
    134 #define SPX_DESTLOOP		0x2270
    135 #define SPX_MPC			0x21fc
    136 
    137 /* few SPX opcodes (microcode entry points); rasterop # = opcode ? */
    138 #define SPX_OP_FILLRECT		0x19
    139 #define	SPX_OP_COPYRECT		0x1a
    140 
    141 /* bt459 locations */
    142 #define SPX_BT459_ADDRL		0x39b10000
    143 #define SPX_BT459_ADDRH		0x39b14000
    144 #define SPX_BT459_REG		0x39b18000
    145 #define SPX_BT459_CMAP		0x39b1c000
    146 
    147 /* bt460 locations */
    148 #define SPXg_BT460_BASE_KA46	0x200f0000
    149 #define SPXg_BT460_BASE_KA49	0x2a000000
    150 #define SPXg_BT460_BASE		((vax_boardtype == VAX_BTYP_46) ? \
    151 				 SPXg_BT460_BASE_KA46 : SPXg_BT460_BASE_KA49)
    152 
    153 #define	SPX_BG_COLOR		WS_DEFAULT_BG
    154 #define	SPX_FG_COLOR		WS_DEFAULT_FG
    155 
    156 /*
    157  * cursor X = 0, Y = 0 on-screen bias
    158  * FIXME: BIAS for various HW types
    159  */
    160 #define CUR_XBIAS		360
    161 #define CUR_YBIAS		37
    162 
    163 /* few BT459 & BT460 indirect register defines */
    164 #define SPXDAC_REG_CCOLOR_1	0x181
    165 #define SPXDAC_REG_CCOLOR_2	0x182
    166 #define SPXDAC_REG_CCOLOR_3	0x183
    167 #define SPXDAC_REG_ID		0x200
    168 #define SPXDAC_REG_CMD0		0x201
    169 #define SPXDAC_REG_CMD1		0x202
    170 #define SPXDAC_REG_CMD2		0x203
    171 #define SPXDAC_REG_PRM		0x204
    172 #define SPXDAC_REG_CCR		0x300
    173 #define SPXDAC_REG_CXLO		0x301
    174 #define SPXDAC_REG_CXHI		0x302
    175 #define SPXDAC_REG_CYLO		0x303
    176 #define SPXDAC_REG_CYHI		0x304
    177 #define SPXDAC_REG_WXLO		0x305
    178 #define SPXDAC_REG_WXHI		0x306
    179 #define SPXDAC_REG_WYLO		0x307
    180 #define SPXDAC_REG_WYHI		0x308
    181 #define SPXDAC_REG_WWLO		0x309
    182 #define SPXDAC_REG_WWHI		0x30a
    183 #define SPXDAC_REG_WHLO		0x30b
    184 #define SPXDAC_REG_WHHI		0x30c
    185 #define SPXDAC_REG_CRAM_BASE	0x400
    186 
    187 /*
    188  * used to access top/bottom 8 bits of a 16-bit argument for split RAMDAC
    189  * addressing
    190  */
    191 #define HI(x)	(((x) >> 8) & 0xff)
    192 #define LO(x)	((x) & 0xff)
    193 
    194 static	int	spx_match(device_t, cfdata_t, void *);
    195 static	void	spx_attach(device_t, device_t, void *);
    196 
    197 struct	spx_softc {
    198 	device_t ss_dev;
    199 };
    200 
    201 CFATTACH_DECL_NEW(spx, sizeof(struct spx_softc),
    202 		  spx_match, spx_attach, NULL, NULL);
    203 
    204 static void	spx_cursor(void *, int, int, int);
    205 static int	spx_mapchar(void *, int, unsigned int *);
    206 static void	spx_putchar(void *, int, int, u_int, long);
    207 static void	spx_copycols(void *, int, int, int, int);
    208 static void	spx_erasecols(void *, int, int, int, long);
    209 static void	spx_copyrows(void *, int, int, int);
    210 static void	spx_eraserows(void *, int, int, long);
    211 static int	spx_allocattr(void *, int, int, int, long *);
    212 static int	spx_get_cmap(struct wsdisplay_cmap *);
    213 static int	spx_set_cmap(struct wsdisplay_cmap *);
    214 
    215 static int	spx_map_regs(device_t, u_int, u_int, u_int, u_int);
    216 static void	SPX_render_font(void);
    217 static void	SPXg_render_font(void);
    218 static int	SPX_map_fb(device_t, struct vsbus_attach_args *);
    219 static int	SPXg_map_fb(device_t, struct vsbus_attach_args *);
    220 static void	spx_init_common(device_t, struct vsbus_attach_args *);
    221 
    222 #define SPX_MAP_FB(self, va, type) if (! type ## _map_fb(self, va)) return
    223 #define SPX_MAP_REGS(self, type) if (!spx_map_regs(self,		\
    224 						   type ## _REG_ADDR,	\
    225 						   type ## _REG_SIZE,	\
    226 						   type ## _REG1_ADDR,	\
    227 						   type ## _REG1_SIZE)) \
    228 					return
    229 
    230 const struct wsdisplay_emulops spx_emulops = {
    231 	spx_cursor,
    232 	spx_mapchar,
    233 	spx_putchar,
    234 	spx_copycols,
    235 	spx_erasecols,
    236 	spx_copyrows,
    237 	spx_eraserows,
    238 	spx_allocattr
    239 };
    240 
    241 static char spx_stdscreen_name[10] = "160x128";
    242 struct wsscreen_descr spx_stdscreen = {
    243 	spx_stdscreen_name, 160, 128,		/* dynamically set */
    244 	&spx_emulops,
    245 	8, 15,					/* dynamically set */
    246 	WSSCREEN_UNDERLINE|WSSCREEN_REVERSE|WSSCREEN_WSCOLORS,
    247 };
    248 
    249 const struct wsscreen_descr *_spx_scrlist[] = {
    250 	&spx_stdscreen,
    251 };
    252 
    253 const struct wsscreen_list spx_screenlist = {
    254 	sizeof(_spx_scrlist) / sizeof(struct wsscreen_descr *),
    255 	_spx_scrlist,
    256 };
    257 
    258 static volatile char *spxaddr;
    259 static volatile long *regaddr;
    260 static volatile long *regaddr1;
    261 
    262 static volatile char *bt_addrl;
    263 static volatile char *bt_addrh;
    264 static volatile char *bt_reg;
    265 static volatile char *bt_cmap;
    266 static volatile char *bt_wait; /* SPXg/gt only */
    267 
    268 static int	spx_xsize;
    269 static int	spx_ysize;
    270 static int	spx_depth;
    271 static int	spx_cols;
    272 static int	spx_rows;
    273 static long	spx_fb_size;
    274 
    275 /* Our current hardware colormap */
    276 static struct hwcmap256 {
    277 #define	CMAP_SIZE	256	/* 256 R/G/B entries */
    278 	u_int8_t r[CMAP_SIZE];
    279 	u_int8_t g[CMAP_SIZE];
    280 	u_int8_t b[CMAP_SIZE];
    281 } spx_cmap;
    282 
    283 /* The default colormap */
    284 static struct {
    285 	u_int8_t r[8];
    286 	u_int8_t g[8];
    287 	u_int8_t b[8];
    288 } spx_default_cmap = {
    289 	{ 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff },
    290 	{ 0x00, 0x00, 0xff, 0x7f, 0x00, 0x00, 0xff, 0xff },
    291 	{ 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff }
    292 };
    293 
    294 static char fb_type = 0;
    295 static char spxg_current_page = 0;
    296 #define spxg_switch_page(nr) (SPXg_REG1(0x20) = ((1 << (nr) & 0xff)) << 16)
    297 
    298 struct wsdisplay_font spx_font;
    299 static u_char *qf;
    300 static u_short *qf2;
    301 
    302 #define QCHAR_INVALID(c)     (((c) < spx_font.firstchar) ||		\
    303 			      ((c) >= spx_font.firstchar + spx_font.numchars))
    304 #define QCHAR(c)	     (QCHAR_INVALID(c) ? 0 : (c) - spx_font.firstchar)
    305 
    306 #define QFONT_INDEX(c, line) (QCHAR(c) * spx_font.fontheight + line)
    307 #define QFONT_QF(c, line)    qf[QFONT_INDEX(c, line)]
    308 #define QFONT_QF2(c, line)   qf2[QFONT_INDEX(c, line)]
    309 #define QFONT(c, line)       (spx_font.stride == 2 ? \
    310 			      QFONT_QF2(c, line) :   \
    311 			      QFONT_QF(c, line))
    312 
    313 /* replicate color value */
    314 #define COLOR(x) (((x) << 24) | ((x) << 16) | ((x) << 8) | (x))
    315 
    316 /* convert coordinates into linear offset */
    317 #define LINEAR(x, y) ((y * spx_xsize) + x)
    318 
    319 /* Linear pixel address */
    320 #define SPX_POS(row, col, line, dot)		   \
    321 	((col) * spx_font.fontwidth +		   \
    322 	 (row) * spx_font.fontheight * spx_xsize + \
    323 	 (line) * spx_xsize + dot)
    324 
    325 #define	SPX_ADDR(row, col, line, dot) spxaddr[SPX_POS(row, col, line, dot)]
    326 
    327 /* Recalculate absolute pixel address from linear address */
    328 #define SPXg_ADDR(linear) spxaddr[((((linear) % SPXg_WIN_LINEAR) / 4) * 8) + \
    329 				  (((linear) % SPXg_WIN_LINEAR) % 4)]
    330 
    331 
    332 static int	spx_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    333 static paddr_t	spx_mmap(void *, void *, off_t, int);
    334 static int	spx_alloc_screen(void *, const struct wsscreen_descr *,
    335 				      void **, int *, int *, long *);
    336 static void	spx_free_screen(void *, void *);
    337 static int	spx_show_screen(void *, void *, int, void (*) (void *, int, int),
    338 				void *);
    339 
    340 static void	spx_update_cmap(int entry, char red, char green, char blue);
    341 static void	set_btreg(u_int addr, u_char value);
    342 static u_char	get_btreg(u_int addr);
    343 
    344 /* SPXg/gt delay */
    345 static void	spxg_delay(void);
    346 
    347 /*
    348  * SPX HW accelerated block copy
    349  * common code in spx_blkcpy,
    350  * HW-specific code accessed through spx_blkcpy_func pointer
    351  */
    352 static void	spx_blkcpy(u_int sxpos, u_int sypos,
    353 			   u_int dxpos, u_int dypos,
    354 			   u_int xdim, u_int ydim);
    355 
    356 static void	SPX_blkcpy(u_int sxpos, u_int sypos,
    357 			   u_int dxpos, u_int dypos,
    358 			   u_int xdim, u_int ydim,
    359 			   char direction);
    360 static void	SPXg_blkcpy(u_int sxpos, u_int sypos,
    361 			    u_int dxpos, u_int dypos,
    362 			    u_int xdim, u_int ydim,
    363 			    char direction);
    364 static void	(*spx_blkcpy_func)(u_int, u_int,
    365 				   u_int, u_int,
    366 				   u_int, u_int,
    367 				   char);
    368 
    369 /* SPX HW accelerated block set, no common code */
    370 static void	SPX_blkset(u_int xpos, u_int ypos,
    371 			   u_int xdim, u_int ydim, char color);
    372 static void	SPXg_blkset(u_int xpos, u_int ypos,
    373 			    u_int xdim, u_int ydim, char color);
    374 static void	(*spx_blkset_func)(u_int, u_int, u_int, u_int, char);
    375 #define spx_blkset(x, y, xd, yd, c) spx_blkset_func(x, y, xd, yd, c)
    376 
    377 /* SPX HW accelerated part of spx_putchar */
    378 static void	SPX_putchar(int, int, u_int, char, char);
    379 static void	SPXg_putchar(int, int, u_int, char, char);
    380 static void	(*spx_putchar_func)(int, int, u_int, char, char);
    381 
    382 const struct wsdisplay_accessops spx_accessops = {
    383 	spx_ioctl,
    384 	spx_mmap,
    385 	spx_alloc_screen,
    386 	spx_free_screen,
    387 	spx_show_screen,
    388 	0 /* load_font */
    389 };
    390 
    391 /* TODO allocate ss_image dynamically for consoles beyond first one */
    392 struct	spx_screen {
    393 	int	ss_curx;
    394 	int	ss_cury;
    395 	int	ss_cur_fg;
    396 	int	ss_cur_bg;
    397 	struct {
    398 		u_char	data;	/* Image character */
    399 		u_char	attr;	/* Attribute: 80/70/08/07 */
    400 	} ss_image[160 * 128];	/* allow for maximum possible cell matrix */
    401 };
    402 
    403 #define	SPX_ATTR_UNDERLINE	0x80
    404 #define	SPX_BG_MASK		0x70
    405 #define	SPX_ATTR_REVERSE	0x08
    406 #define	SPX_FG_MASK		0x07
    407 
    408 static	struct spx_screen spx_conscreen;
    409 static	struct spx_screen *prevscr, *curscr;
    410 static	struct spx_screen *savescr;
    411 
    412 static	int cur_fg, cur_bg;
    413 static	int spx_off = 1;
    414 
    415 static void
    416 spx_update_cmap(int entry, char red, char green, char blue)
    417 {
    418 	*bt_addrl = LO(entry);
    419 	*bt_addrh = HI(entry);
    420 	if ((entry >= 0x181) && (entry <= 0x183)) {
    421 		*bt_reg = red;
    422 		*bt_reg = green;
    423 		*bt_reg = blue;
    424 	} else {
    425 		*bt_cmap = red;
    426 		*bt_cmap = green;
    427 		*bt_cmap = blue;
    428 	}
    429 }
    430 
    431 static void
    432 set_btreg(u_int addr, u_char value)
    433 {
    434 	*bt_addrl = LO(addr);
    435 	*bt_addrh = HI(addr);
    436 	*bt_reg = value;
    437 }
    438 
    439 static u_char
    440 get_btreg(u_int addr)
    441 {
    442 	*bt_addrl = LO(addr);
    443 	*bt_addrh = HI(addr);
    444 	return *bt_reg & 0xff;
    445 }
    446 
    447 static void
    448 spxg_delay(void)
    449 {
    450 	char i;
    451 
    452 	for (i = 0; i <= SPXg_DELAY; i++)
    453 		*bt_wait = *bt_wait + i;
    454 }
    455 
    456 static void
    457 SPX_blkcpy(u_int sxpos, u_int sypos,
    458 	   u_int dxpos, u_int dypos,
    459 	   u_int xdim, u_int ydim,
    460 	   char direction)
    461 {
    462 	u_int counter = 0xffffe;
    463 	long temp = 0;
    464 
    465 	SPX_REG(SPX_COMMAND) = 0x84884648 | direction; /* 0x848c4648? */
    466 	SPX_REG(SPX_XSTART) = dxpos << 16;
    467 	SPX_REG(SPX_YSTART) = dypos << 16;
    468 	SPX_REG(SPX_XEND) = (dxpos + xdim) << 16;
    469 	SPX_REG(SPX_YEND) = (dypos + ydim) << 16;
    470 
    471 	temp = ((SPX_REG1(0x10) & 0xc0) << (30 - 6)) |		\
    472 		((SPX_REG1(0x10) & 0x3f) << 24);
    473 
    474 	SPX_REG(SPX_DSTPIX) = temp + LINEAR(dxpos, dypos);
    475 	SPX_REG(SPX_SRCPIX) = temp + LINEAR(sxpos, sypos);
    476 	SPX_REG(SPX_SRCPIX1) = 0;
    477 	SPX_REG(SPX_STRIDE) = (spx_xsize << 16) | spx_xsize;
    478 	SPX_REG(SPX_SRCMASK) = 0xff;
    479 	SPX_REG(SPX_DSTMASK) = 0xff;
    480 
    481 	SPX_REG(SPX_DESTLOOP) = 0x00112003;
    482 	SPX_REG(SPX_MPC) = 0x2000 | SPX_OP_COPYRECT;
    483 
    484 	SPX_REG1(0x18) = 0xffffffff;
    485 	while ((counter) && ((SPX_REG1(0x18) & 2) == 0))
    486 		counter--;
    487 }
    488 
    489 static void
    490 SPXg_blkcpy(u_int sxpos, u_int sypos,
    491 	    u_int dxpos, u_int dypos,
    492 	    u_int xdim, u_int ydim,
    493 	    char direction)
    494 {
    495 	u_int counter = 0xffffe;
    496 	long temp = 0;
    497 
    498 	SPXg_REG(SPX_COMMAND) = 0x84884648 | direction; spxg_delay();
    499 	SPXg_REG(SPX_XSTART) = dxpos << 16; spxg_delay();
    500 	SPXg_REG(SPX_YSTART) = dypos << 16; spxg_delay();
    501 	SPXg_REG(SPX_XEND) = (dxpos + xdim) << 16; spxg_delay();
    502 	SPXg_REG(SPX_YEND) = (dypos + ydim) << 16; spxg_delay();
    503 
    504 	temp = 0x3f << 24;
    505 
    506 	SPXg_REG(SPX_DSTPIX) = temp + LINEAR(dxpos, dypos); spxg_delay();
    507 	SPXg_REG(SPX_DSTPIX1) = 0xff000000; spxg_delay();
    508 	SPXg_REG(SPX_SRCPIX) = temp + LINEAR(sxpos, sypos); spxg_delay();
    509 	SPXg_REG(SPX_SRCPIX1) = 0; spxg_delay();
    510 	SPXg_REG(SPX_STRIDE) = (spx_xsize << 16) | spx_xsize; spxg_delay();
    511 	SPXg_REG(SPX_SRCMASK) = 0xffffffff; spxg_delay();
    512 	SPXg_REG(SPX_DSTMASK) = 0xffffffff; spxg_delay();
    513 
    514 	SPXg_REG(SPX_DESTLOOP) = 0x00112003; spxg_delay();
    515 	SPXg_REG(SPX_MPC) = 0x2000 | SPX_OP_COPYRECT; spxg_delay();
    516 
    517 	while ((counter) && ((SPXg_REG1(0x0) & 0x8000) == 0))
    518 		counter--;
    519 }
    520 
    521 static void
    522 spx_blkcpy(u_int sxpos, u_int sypos,
    523 	   u_int dxpos, u_int dypos,
    524 	   u_int xdim, u_int ydim)
    525 {
    526 	char direction = 0;
    527 
    528 	/* don't waste time checking whether src and dest actually overlap */
    529 	if (sxpos < dxpos) {
    530 		direction |= 0x01; /* X decrement */
    531 		sxpos += xdim;
    532 		dxpos += xdim;
    533 		xdim = -xdim;
    534 	}
    535 	if (sypos < dypos) {
    536 		direction |= 0x02; /* Y decrement */
    537 		sypos += ydim;
    538 		dypos += ydim;
    539 		ydim = -ydim;
    540 	}
    541 
    542 	spx_blkcpy_func(sxpos, sypos, dxpos, dypos, xdim, ydim, direction);
    543 }
    544 
    545 static void
    546 SPX_blkset(u_int xpos, u_int ypos, u_int xdim, u_int ydim, char color)
    547 {
    548 	u_int counter = 0xfffe;
    549 	long temp = 0;
    550 
    551 	SPX_REG(SPX_COMMAND) = 0x84884608;
    552 	SPX_REG(SPX_XSTART) = xpos << 16;
    553 	SPX_REG(SPX_YSTART) = ypos << 16;
    554 	SPX_REG(SPX_XEND) = (xpos + xdim) << 16;
    555 	SPX_REG(SPX_YEND) = (ypos + ydim) << 16;
    556 	SPX_REG(SPX_STRIDE) = (spx_xsize << 16) | spx_xsize;
    557 	SPX_REG(SPX_DESTLOOP) = 0x20102003;
    558 
    559 	temp = ((SPX_REG1(0x10) & 0xc0) << (30 - 6)) |
    560 		((SPX_REG1(0x10) & 0x3f) << 24);
    561 	SPX_REG(SPX_DSTPIX) = temp + LINEAR(xpos, ypos);
    562 
    563 	SPX_REG(SPX_FG) = COLOR(color);
    564 
    565 	SPX_REG(SPX_MPC) = 0x2000 | SPX_OP_FILLRECT;
    566 
    567 	SPX_REG1(0x18) = 0xffffffff;
    568 	while ((counter) && ((SPX_REG1(0x18) & 2) == 0))
    569 		counter--;
    570 }
    571 
    572 static void
    573 SPXg_blkset(u_int xpos, u_int ypos, u_int xdim, u_int ydim, char color)
    574 {
    575 	u_int counter = 0xfffe;
    576 	long temp = 0;
    577 
    578 	SPXg_REG(SPX_COMMAND) = 0x84884608; spxg_delay();
    579 	SPXg_REG(SPX_XSTART) = xpos << 16; spxg_delay();
    580 	SPXg_REG(SPX_YSTART) = ypos << 16; spxg_delay();
    581 	SPXg_REG(SPX_XEND) = (xpos + xdim) << 16; spxg_delay();
    582 	SPXg_REG(SPX_YEND) = (ypos + ydim) << 16; spxg_delay();
    583 	SPXg_REG(SPX_STRIDE) = (spx_xsize << 16) | spx_xsize; spxg_delay();
    584 
    585 	temp = 0x3f << 24;
    586 	SPXg_REG(SPX_DSTPIX) = temp + LINEAR(xpos, ypos); spxg_delay();
    587 	SPXg_REG(SPX_DSTPIX1) = 0xff000000; spxg_delay();
    588 
    589 	SPXg_REG(SPX_FG) = COLOR(color); spxg_delay();
    590 
    591 	SPXg_REG(SPX_DESTLOOP) = 0x20102003; spxg_delay();
    592 	SPXg_REG(SPX_MPC) = 0x2000 | SPX_OP_FILLRECT; spxg_delay();
    593 
    594 	while ((counter) && ((SPXg_REG1(0x0) & 0x8000) == 0))
    595 		counter--;
    596 }
    597 
    598 int
    599 spx_match(device_t parent, cfdata_t match, void *aux)
    600 {
    601 	struct vsbus_softc *sc = device_private(parent);
    602 #if 0
    603 	struct vsbus_attach_args *va = aux;
    604 	char *ch = (char *)va->va_addr;
    605 #endif
    606 
    607 /*
    608  * FIXME:
    609  * add KA46 when/if ever somebody reports SPXg vax_confdata ID on VS 4000/60
    610  * Ditto for SPX ID on KA42 & KA43
    611  */
    612 	if ((vax_boardtype != VAX_BTYP_49) ||
    613 	    ((vax_confdata & (CONF_LCSPX | CONF_SPXg)) == 0))
    614 		return 0;
    615 
    616 /* FIXME add RAMDAC ID code ??? */
    617 #if 0
    618 	/*
    619 	 * FIXME:
    620 	 * are memory addresses besides va->va_addr off limits at this time ?
    621 	 * RAMDAC ID register test, should read 0x4a for LCSPX, 0x4b for SPXg
    622 	 */
    623 	if (get_btreg(SPXDAC_REG_ID) != 0x4a)
    624 		return 0;
    625 #endif
    626 
    627 	sc->sc_mask = 0x04; /* XXX - should be generated */
    628 	scb_fake(0x14c, 0x15);
    629 
    630 	return 20;
    631 }
    632 
    633 static void
    634 spx_attach(device_t parent, device_t self, void *aux)
    635 {
    636 	struct spx_softc *sc = device_private(self);
    637 	struct vsbus_attach_args *va = aux;
    638 	struct wsemuldisplaydev_attach_args aa;
    639 
    640 	sc->ss_dev = self;
    641 
    642 	aprint_normal("\n");
    643 	aa.console = spxaddr != NULL;
    644 
    645 	spx_init_common(self, va);
    646 
    647 	/* display FB type based on RAMDAC ID */
    648 	switch (get_btreg(SPXDAC_REG_ID) & 0xff) {
    649 	case 0x4a:
    650 		aprint_normal_dev(self,
    651 			"RAMDAC ID: 0x%x, Bt459 (SPX/LCSPX) RAMDAC type\n",
    652 			get_btreg(SPXDAC_REG_ID) & 0xff);
    653 		break;
    654 
    655 	case 0x4b:
    656 		aprint_normal_dev(self,
    657 			"RAMDAC ID: 0x%x, Bt460 (SPXg) RAMDAC type\n",
    658 			get_btreg(SPXDAC_REG_ID) & 0xff);
    659 		break;
    660 	default:
    661 		aprint_error_dev(self, "unknown RAMDAC type 0x%x\n",
    662 			get_btreg(SPXDAC_REG_ID) & 0xff);
    663 	}
    664 
    665 	curscr = &spx_conscreen;
    666 	prevscr = curscr;
    667 
    668 	aa.scrdata = &spx_screenlist;
    669 	aa.accessops = &spx_accessops;
    670 
    671 	/* enable cursor */
    672 	set_btreg(SPXDAC_REG_CCR, 0xc1);
    673 
    674 	config_found(self, &aa, wsemuldisplaydevprint);
    675 }
    676 
    677 static	int cur_on;
    678 
    679 static void
    680 spx_cursor(void *id, int on, int row, int col)
    681 {
    682 	struct spx_screen *ss = id;
    683 	int attr, data;
    684 
    685 	attr = ss->ss_image[row * spx_cols + col].attr;
    686 	data = ss->ss_image[row * spx_cols + col].data;
    687 
    688 	if (attr & SPX_ATTR_REVERSE) {
    689 		cur_bg = attr & SPX_FG_MASK;
    690 		cur_fg = (attr & SPX_BG_MASK) >> 4;
    691 	} else {
    692 		cur_fg = attr & SPX_FG_MASK;
    693 		cur_bg = (attr & SPX_BG_MASK) >> 4;
    694 	}
    695 
    696 	if (ss == curscr) {
    697 #ifdef SOFTCURSOR
    698 		if ((cur_on = on))
    699 			spx_putchar_func(row, col, data, cur_bg, cur_fg);
    700 		else
    701 			spx_putchar_func(row, col, data, cur_fg, cur_bg);
    702 #else
    703 		/* change cursor foreground color colormap entry */
    704 		spx_update_cmap(SPXDAC_REG_CCOLOR_3,
    705 				spx_cmap.r[cur_fg],
    706 				spx_cmap.g[cur_fg],
    707 				spx_cmap.b[cur_fg]);
    708 
    709 		/* update cursor position registers */
    710 		set_btreg(SPXDAC_REG_CXLO,
    711 			  LO(col * spx_font.fontwidth + CUR_XBIAS));
    712 		set_btreg(SPXDAC_REG_CXHI,
    713 			  HI(col * spx_font.fontwidth + CUR_XBIAS));
    714 		set_btreg(SPXDAC_REG_CYLO,
    715 			  LO(row * spx_font.fontheight + CUR_YBIAS));
    716 		set_btreg(SPXDAC_REG_CYHI,
    717 			  HI(row * spx_font.fontheight + CUR_YBIAS));
    718 
    719 		if ((cur_on = on))
    720 			/* enable cursor */
    721 			set_btreg(SPXDAC_REG_CCR, 0xc1);
    722 		else
    723 			/* disable cursor */
    724 			set_btreg(SPXDAC_REG_CCR, 0x01);
    725 #endif
    726 	}
    727 
    728 	ss->ss_curx = col;
    729 	ss->ss_cury = row;
    730 	ss->ss_cur_bg = cur_bg;
    731 	ss->ss_cur_fg = cur_fg;
    732 }
    733 
    734 static int
    735 spx_mapchar(void *id, int uni, unsigned int *index)
    736 {
    737 	if (uni < 256) {
    738 		*index = uni;
    739 		return (5);
    740 	}
    741 	*index = ' ';
    742 	return (0);
    743 }
    744 
    745 static void
    746 SPX_putchar(int row, int col, u_int c, char dot_fg, char dot_bg)
    747 {
    748 	u_int counter = 0xffffe;
    749 	long temp = 0;
    750 
    751 	SPX_REG(SPX_FG) = COLOR(dot_fg);
    752 	SPX_REG(SPX_BG) = COLOR(dot_bg);
    753 	SPX_REG(SPX_MAIN3) = 0x01010101;
    754 	SPX_REG(SPX_COMMAND) = 0x84884648;
    755 	SPX_REG(SPX_XSTART) = (col * spx_font.fontwidth) << 16;
    756 	SPX_REG(SPX_YSTART) = (row * spx_font.fontheight) << 16;
    757 	SPX_REG(SPX_XEND) = ((col + 1) * spx_font.fontwidth) << 16;
    758 	SPX_REG(SPX_YEND) = ((row + 1) * spx_font.fontheight) << 16;
    759 
    760 	temp = ((SPX_REG1(0x10) & 0xc0) << (30 - 6)) |	\
    761 		((SPX_REG1(0x10) & 0x3f) << 24);
    762 
    763 	SPX_REG(SPX_DSTPIX) = temp + LINEAR(col * spx_font.fontwidth,
    764 					    row * spx_font.fontheight);
    765 	SPX_REG(SPX_SRCPIX) = temp + FONT_STORAGE_START
    766 		+ (c * spx_font.fontheight * spx_font.fontwidth);
    767 	SPX_REG(SPX_SRCPIX1) = 0;
    768 	SPX_REG(SPX_STRIDE) = (spx_font.fontwidth << 16) | spx_xsize;
    769 	SPX_REG(SPX_SRCMASK) = 0xff;
    770 	SPX_REG(SPX_DSTMASK) = 0xff;
    771 
    772 	SPX_REG(SPX_DESTLOOP) = 0x20142003;
    773 	SPX_REG(SPX_MPC) = 0x2000 | SPX_OP_COPYRECT;
    774 
    775 	SPX_REG1(0x18) = 0xffffffff;
    776 
    777 	while ((counter) && ((SPX_REG1(0x18) & 2) == 0))
    778 		counter--;
    779 }
    780 
    781 static void
    782 SPXg_putchar(int row, int col, u_int c, char dot_fg, char dot_bg)
    783 {
    784 	u_int counter = 0xffffe;
    785 	long temp = 0;
    786 
    787 	SPXg_REG(SPX_FG) = COLOR(dot_fg);
    788 	spxg_delay();
    789 	SPXg_REG(SPX_BG) = COLOR(dot_bg);
    790 	spxg_delay();
    791 	SPXg_REG(SPX_MAIN3) = 0x01010101;
    792 	spxg_delay();
    793 	SPXg_REG(SPX_COMMAND) = 0x84884648;
    794 	spxg_delay();
    795 	SPXg_REG(SPX_XSTART) = (col * spx_font.fontwidth) << 16;
    796 	spxg_delay();
    797 	SPXg_REG(SPX_YSTART) = (row * spx_font.fontheight) << 16;
    798 	spxg_delay();
    799 	SPXg_REG(SPX_XEND) = ((col + 1) * spx_font.fontwidth) << 16;
    800 	spxg_delay();
    801 	SPXg_REG(SPX_YEND) = ((row + 1) * spx_font.fontheight) << 16;
    802 	spxg_delay();
    803 
    804 	temp = 0x3f << 24;
    805 
    806 	SPXg_REG(SPX_DSTPIX) = temp + LINEAR(col * spx_font.fontwidth,
    807 					     row * spx_font.fontheight);
    808 	spxg_delay();
    809 	SPXg_REG(SPX_DSTPIX1) = 0xff000000;
    810 	spxg_delay();
    811 	SPXg_REG(SPX_SRCPIX) = temp + FONT_STORAGE_START +
    812 		(c * spx_font.fontheight * spx_font.fontwidth);
    813 	spxg_delay();
    814 	SPXg_REG(SPX_SRCPIX1) = 0;
    815 	spxg_delay();
    816 	SPXg_REG(SPX_STRIDE) = (spx_font.fontwidth << 16) | spx_xsize;
    817 	spxg_delay();
    818 	SPXg_REG(SPX_SRCMASK) = 0xffffffff;
    819 	spxg_delay();
    820 	SPXg_REG(SPX_DSTMASK) = 0xffffffff;
    821 	spxg_delay();
    822 
    823 	SPXg_REG(SPX_DESTLOOP) = 0x20142003;
    824 	spxg_delay();
    825 	SPXg_REG(SPX_MPC) = 0x2000 | SPX_OP_COPYRECT;
    826 	spxg_delay();
    827 
    828 	while ((counter) && ((SPXg_REG1(0x0) & 0x8000) == 0))
    829 		counter--;
    830 }
    831 
    832 static void
    833 spx_putchar(void *id, int row, int col, u_int c, long attr)
    834 {
    835 	struct spx_screen *ss = id;
    836 	char dot_fg, dot_bg;
    837 
    838 	c &= 0xff;
    839 
    840 	ss->ss_image[row * spx_cols + col].data = c;
    841 	ss->ss_image[row * spx_cols + col].attr = attr;
    842 	if (ss != curscr)
    843 		return;
    844 
    845 	dot_fg = attr & SPX_FG_MASK;
    846 	dot_bg = (attr & SPX_BG_MASK) >> 4;
    847 	if (attr & SPX_ATTR_REVERSE) {
    848 		dot_fg = (attr & SPX_BG_MASK) >> 4;
    849 		dot_bg = attr & SPX_FG_MASK;
    850 	}
    851 
    852 	/* adjust glyph index for underlined text */
    853 	if (attr & SPX_ATTR_UNDERLINE)
    854 		c += 0x100;
    855 
    856 	spx_putchar_func(row, col, c, dot_fg, dot_bg);
    857 }
    858 
    859 /*
    860  * copies columns inside a row.
    861  */
    862 static void
    863 spx_copycols(void *id, int row, int srccol, int dstcol, int ncols)
    864 {
    865 	struct spx_screen *ss = id;
    866 
    867 	bcopy(&ss->ss_image[row * spx_cols + srccol],
    868 	      &ss->ss_image[row * spx_cols + dstcol],
    869 	      ncols * sizeof(ss->ss_image[0]));
    870 
    871 	if (ss == curscr)
    872 		spx_blkcpy(srccol * spx_font.fontwidth,
    873 			   row * spx_font.fontheight,
    874 			   dstcol * spx_font.fontwidth,
    875 			   row * spx_font.fontheight,
    876 			   ncols * spx_font.fontwidth,
    877 			   spx_font.fontheight);
    878 }
    879 
    880 /*
    881  * Erases a bunch of chars inside one row.
    882  */
    883 static void
    884 spx_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
    885 {
    886 	struct spx_screen *ss = id;
    887 	int offset = row * spx_cols + startcol;
    888 	int i;
    889 
    890 	memset(&ss->ss_image[row * spx_cols + startcol], 0,
    891 	      ncols * sizeof(ss->ss_image[0]));
    892 
    893 	for (i = offset; i < offset + ncols; ++i)
    894 		ss->ss_image[i].attr = fillattr;
    895 
    896 	if (ss == curscr)
    897 		spx_blkset(startcol * spx_font.fontwidth,
    898 			   row * spx_font.fontheight,
    899 			   ncols * spx_font.fontwidth,
    900 			   spx_font.fontheight,
    901 			   (fillattr & SPX_BG_MASK) >> 4);
    902 }
    903 
    904 static void
    905 spx_copyrows(void *id, int srcrow, int dstrow, int nrows)
    906 {
    907 	struct spx_screen *ss = id;
    908 
    909 	bcopy(&ss->ss_image[srcrow * spx_cols],
    910 	      &ss->ss_image[dstrow * spx_cols],
    911 	      nrows * spx_cols * sizeof(ss->ss_image[0]));
    912 
    913 	if (ss == curscr)
    914 		spx_blkcpy(0, (srcrow * spx_font.fontheight),
    915 			   0, (dstrow * spx_font.fontheight),
    916 			   spx_xsize, (nrows * spx_font.fontheight));
    917 }
    918 
    919 static void
    920 spx_eraserows(void *id, int startrow, int nrows, long fillattr)
    921 {
    922 	struct spx_screen *ss = id;
    923 	int i;
    924 
    925 	memset(&ss->ss_image[startrow * spx_cols], 0,
    926 	      nrows * spx_cols * sizeof(ss->ss_image[0]));
    927 
    928 	for (i = startrow * spx_cols; i < startrow + nrows * spx_cols; ++i)
    929 		ss->ss_image[i].attr = fillattr;
    930 
    931 	if (ss == curscr)
    932 		spx_blkset(0, (startrow * spx_font.fontheight),
    933 			   spx_xsize, (nrows * spx_font.fontheight),
    934 			   (fillattr & SPX_BG_MASK) >> 4);
    935 }
    936 
    937 static int
    938 spx_allocattr(void *id, int fg, int bg, int flags, long *attrp)
    939 {
    940 	long myattr;
    941 
    942 	if (flags & WSATTR_WSCOLORS)
    943 		myattr = (fg & SPX_FG_MASK) | ((bg << 4) & SPX_BG_MASK);
    944 	else
    945 		myattr = WSCOL_WHITE << 4;	/* XXXX */
    946 	if (flags & WSATTR_REVERSE)
    947 		myattr |= SPX_ATTR_REVERSE;
    948 	if (flags & WSATTR_UNDERLINE)
    949 		myattr |= SPX_ATTR_UNDERLINE;
    950 	*attrp = myattr;
    951 	return 0;
    952 }
    953 
    954 static int
    955 spx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    956 {
    957 	struct wsdisplay_fbinfo *fb = data;
    958 	int i;
    959 
    960 	switch (cmd) {
    961 	case WSDISPLAYIO_GTYPE:
    962 		*(u_int *)data = WSDISPLAY_TYPE_SPX;
    963 		break;
    964 
    965 	case WSDISPLAYIO_GINFO:
    966 		fb->height = spx_ysize;
    967 		fb->width = spx_xsize;
    968 		fb->depth = spx_depth;
    969 		fb->cmsize = 1 << spx_depth;
    970 		break;
    971 
    972 	case WSDISPLAYIO_GETCMAP:
    973 		return spx_get_cmap((struct wsdisplay_cmap *)data);
    974 
    975 	case WSDISPLAYIO_PUTCMAP:
    976 		return spx_set_cmap((struct wsdisplay_cmap *)data);
    977 
    978 	case WSDISPLAYIO_GMODE:
    979 		return EPASSTHROUGH;
    980 
    981 	case WSDISPLAYIO_SMODE:
    982 		/*
    983 		 * if setting WSDISPLAYIO_MODE_EMUL, restore console cmap,
    984 		 * current screen
    985 		 */
    986 		if (*(u_int *)data == WSDISPLAYIO_MODE_EMUL) {
    987 			for (i = 0; i < 8; i++) {
    988 				spx_cmap.r[i] = spx_default_cmap.r[i];
    989 				spx_cmap.g[i] = spx_default_cmap.g[i];
    990 				spx_cmap.b[i] = spx_default_cmap.b[i];
    991 				spx_update_cmap(i, spx_cmap.r[i], spx_cmap.g[i],
    992 						spx_cmap.b[i]);
    993 			}
    994 			if (savescr != NULL)
    995 				spx_show_screen(NULL, savescr, 0, NULL, NULL);
    996 			savescr = NULL;
    997 		} else {		/* WSDISPLAYIO_MODE_MAPPED */
    998 			savescr = curscr;
    999 			curscr = NULL;
   1000 			/* clear screen? */
   1001 		}
   1002 
   1003 		return EPASSTHROUGH;
   1004 
   1005 	case WSDISPLAYIO_SVIDEO:
   1006 		if (*(u_int *)data == WSDISPLAYIO_VIDEO_ON && spx_off) {
   1007 			/* Unblank video */
   1008 			set_btreg(SPXDAC_REG_CMD0, 0x48);
   1009 
   1010 			/* Enable sync */
   1011 			set_btreg(SPXDAC_REG_CMD2, 0xc0);
   1012 
   1013 			spx_off = 0;
   1014 		} else if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF && !spx_off) {
   1015 			/* Blank video */
   1016 			set_btreg(SPXDAC_REG_CMD0, 68);
   1017 
   1018 			/* Disable sync */
   1019 			set_btreg(SPXDAC_REG_CMD2, 0x40);
   1020 			spx_off = 1;
   1021 		}
   1022 		break;
   1023 
   1024 	case WSDISPLAYIO_GVIDEO:
   1025 		*(u_int *)data = spx_off == 0 ?
   1026 		WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
   1027 		break;
   1028 
   1029 	default:
   1030 		return EPASSTHROUGH;
   1031 	}
   1032 	return 0;
   1033 }
   1034 
   1035 /*
   1036  * Bad idea on SPXg/gt due to windowed framebuffer access
   1037  */
   1038 static paddr_t
   1039 spx_mmap(void *v, void *vs, off_t offset, int prot)
   1040 {
   1041 	if (fb_type != FB_IS_SPX)
   1042 		return -1;
   1043 
   1044 	if (offset >= spx_fb_size || offset < 0)
   1045 		return -1;
   1046 
   1047 	return (SPX_FB_ADDR + offset) >> PGSHIFT;
   1048 }
   1049 
   1050 static int
   1051 spx_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
   1052 		 int *curxp, int *curyp, long *defattrp)
   1053 {
   1054 	int i;
   1055 	struct spx_screen *ss;
   1056 
   1057 	ss = malloc(sizeof(struct spx_screen), M_DEVBUF, M_WAITOK | M_ZERO);
   1058 
   1059 	*cookiep = ss;
   1060 	*curxp = *curyp = 0;
   1061 	*defattrp = (SPX_BG_COLOR << 4) | SPX_FG_COLOR;
   1062 
   1063 	for (i = 0; i < spx_cols * spx_rows; ++i)
   1064 		ss->ss_image[i].attr = *defattrp;
   1065 
   1066 	return 0;
   1067 }
   1068 
   1069 static void
   1070 spx_free_screen(void *v, void *cookie)
   1071 {
   1072 /* FIXME add something to actually free malloc()ed screen? */
   1073 }
   1074 
   1075 static int
   1076 spx_show_screen(void *v, void *cookie, int waitok,
   1077 		void (*cb)(void *, int, int), void *cbarg)
   1078 {
   1079 	struct spx_screen *ss = cookie;
   1080 	u_int row, col, attr;
   1081 	u_char c;
   1082 
   1083 	if (ss == curscr)
   1084 		return (0);
   1085 
   1086 	prevscr = curscr;
   1087 	curscr = ss;
   1088 
   1089 	for (row = 0; row < spx_rows; row++) {
   1090 		for (col = 0; col < spx_cols; col++) {
   1091 			u_int idx = row * spx_cols + col;
   1092 			attr = ss->ss_image[idx].attr;
   1093 			c = ss->ss_image[idx].data;
   1094 
   1095 			/* check if cell requires updating */
   1096 			if ((c != prevscr->ss_image[idx].data) ||
   1097 			    (attr != prevscr->ss_image[idx].attr)) {
   1098 				if (c < 32) c = 32;
   1099 				spx_putchar(ss, row, col, c, attr);
   1100 			}
   1101 		}
   1102 	}
   1103 
   1104 	row = ss->ss_cury; col = ss->ss_curx;
   1105 
   1106 	spx_cursor(ss, cur_on, row, col);
   1107 
   1108 	cur_fg = ss->ss_cur_fg;
   1109 	cur_bg = ss->ss_cur_bg;
   1110 
   1111 	return (0);
   1112 }
   1113 
   1114 static int
   1115 spx_get_cmap(struct wsdisplay_cmap *p)
   1116 {
   1117 	u_int index = p->index, count = p->count;
   1118 	int error;
   1119 
   1120 	if (index >= (1 << spx_depth) || count > (1 << spx_depth) - index)
   1121 		return (EINVAL);
   1122 
   1123 	error = copyout(&spx_cmap.r[index], p->red, count);
   1124 	if (error)
   1125 		return error;
   1126 
   1127 	error = copyout(&spx_cmap.g[index], p->green, count);
   1128 	if (error)
   1129 		return error;
   1130 
   1131 	error = copyout(&spx_cmap.b[index], p->blue, count);
   1132 	return error;
   1133 }
   1134 
   1135 static int
   1136 spx_set_cmap(struct wsdisplay_cmap *p)
   1137 {
   1138 	u_int index = p->index, count = p->count;
   1139 	int error, s;
   1140 
   1141 	if (index >= (1 << spx_depth) || count > (1 << spx_depth) - index)
   1142 		return (EINVAL);
   1143 
   1144 	error = copyin(p->red, &spx_cmap.r[index], count);
   1145 	if (error)
   1146 		return error;
   1147 
   1148 	error = copyin(p->green, &spx_cmap.g[index], count);
   1149 	if (error)
   1150 		return error;
   1151 
   1152 	error = copyin(p->blue, &spx_cmap.b[index], count);
   1153 
   1154 	if (error)
   1155 		return error;
   1156 
   1157 	s = spltty();
   1158 	while (count-- > 0) {
   1159 		spx_update_cmap(index,
   1160 				spx_cmap.r[index],
   1161 				spx_cmap.g[index],
   1162 				spx_cmap.b[index]);
   1163 		index++;
   1164 	}
   1165 	splx(s);
   1166 	return (0);
   1167 }
   1168 
   1169 cons_decl(spx);
   1170 
   1171 void
   1172 spxcninit(struct consdev *cndev)
   1173 {
   1174 	int i;
   1175 
   1176 	spx_blkset(0, 0, spx_xsize, spx_ysize, SPX_BG_COLOR);
   1177 
   1178 	curscr = &spx_conscreen;
   1179 
   1180 	for (i = 0; i < spx_cols * spx_rows; i++)
   1181 		spx_conscreen.ss_image[i].attr =
   1182 			(SPX_BG_COLOR << 4) | SPX_FG_COLOR;
   1183 	wsdisplay_cnattach(&spx_stdscreen, &spx_conscreen, 0, 0,
   1184 			   (SPX_BG_COLOR << 4) | SPX_FG_COLOR);
   1185 	cn_tab->cn_pri = CN_INTERNAL;
   1186 
   1187 #if NDZKBD > 0
   1188 	dzkbd_cnattach(0); /* Connect keyboard and screen together */
   1189 #endif
   1190 }
   1191 
   1192 /*
   1193  * Called very early to setup the glass tty as console.
   1194  * Because it's called before the VM system is inited, virtual memory
   1195  * for the framebuffer can be stolen directly without disturbing anything.
   1196  */
   1197 void
   1198 spxcnprobe(struct consdev *cndev)
   1199 {
   1200 	extern const struct cdevsw wsdisplay_cdevsw;
   1201 
   1202 	/* Only for VS 4000/90, 90A and 96 with LCSPX or SPXg/gt*/
   1203 	if ((vax_boardtype != VAX_BTYP_49) ||
   1204 	    ((vax_confdata & (CONF_LCSPX | CONF_SPXg)) == 0))
   1205 		return;
   1206 
   1207 	if (((vax_confdata & 8) && (vax_boardtype == VAX_BTYP_49)) ||
   1208 	    (((vax_confdata & KA420_CFG_L3CON) ||
   1209 	      (vax_confdata & KA420_CFG_MULTU)) &&
   1210 	     ((vax_boardtype == VAX_BTYP_420) ||
   1211 	      (vax_boardtype == VAX_BTYP_43)))) {
   1212 		aprint_normal("spxcnprobe: Diagnostic console\n");
   1213 		return; /* Diagnostic console */
   1214 	}
   1215 
   1216 	spx_init_common(NULL, NULL);
   1217 
   1218 	cndev->cn_pri = CN_INTERNAL;
   1219 	cndev->cn_dev = makedev(cdevsw_lookup_major(&wsdisplay_cdevsw), 0);
   1220 }
   1221 
   1222 static int
   1223 spx_map_regs(device_t self,
   1224 	     u_int raddr, u_int rsize, u_int r1addr, u_int r1size)
   1225 {
   1226 	extern vaddr_t virtual_avail;
   1227 
   1228 	if (!self) {
   1229 		regaddr = (long*)virtual_avail;
   1230 		virtual_avail += rsize;
   1231 		ioaccess((vaddr_t)regaddr, raddr, rsize/VAX_NBPG);
   1232 
   1233 		regaddr1 = (long*)virtual_avail;
   1234 		virtual_avail += r1size;
   1235 		ioaccess((vaddr_t)regaddr1, r1addr, r1size/VAX_NBPG);
   1236 
   1237 		return 1;
   1238 	}
   1239 
   1240 	regaddr = (long*)vax_map_physmem(raddr, rsize/VAX_NBPG);
   1241 	if (regaddr == 0) {
   1242 		aprint_error_dev(self,
   1243 				 "unable to allocate register memory (1).\n");
   1244 		return 0;
   1245 	}
   1246 
   1247 	regaddr1 = (long*)vax_map_physmem(r1addr, r1size/VAX_NBPG);
   1248 	if (regaddr1 == 0) {
   1249 		aprint_error_dev(self,
   1250 				 "unable to allocate register memory (2).\n");
   1251 		return 0;
   1252 	}
   1253 
   1254 	return 1;
   1255 }
   1256 
   1257 static int
   1258 SPX_map_fb(device_t self, struct vsbus_attach_args *va)
   1259 {
   1260 	int fbsize = (spx_fb_size + FONT_STORAGE_SIZE)/VAX_NBPG;
   1261 	extern vaddr_t virtual_avail;
   1262 
   1263 	if (!self) {
   1264 		spxaddr = (char *)virtual_avail;
   1265 		virtual_avail += (spx_fb_size + FONT_STORAGE_SIZE);
   1266 		ioaccess((vaddr_t)spxaddr, SPX_FB_ADDR, fbsize);
   1267 
   1268 		bt_addrl = (char *)virtual_avail;
   1269 		virtual_avail += VAX_NBPG;
   1270 		ioaccess((vaddr_t)bt_addrl, SPX_BT459_ADDRL, 1);
   1271 
   1272 		bt_addrh = (char *)virtual_avail;
   1273 		virtual_avail += VAX_NBPG;
   1274 		ioaccess((vaddr_t)bt_addrh, SPX_BT459_ADDRH, 1);
   1275 
   1276 		bt_reg = (char *)virtual_avail;
   1277 		virtual_avail += VAX_NBPG;
   1278 		ioaccess((vaddr_t)bt_reg, SPX_BT459_REG, 1);
   1279 
   1280 		bt_cmap = (char *)virtual_avail;
   1281 		virtual_avail += VAX_NBPG;
   1282 		ioaccess((vaddr_t)bt_cmap, SPX_BT459_CMAP, 1);
   1283 
   1284 		return 1;
   1285 	}
   1286 
   1287 	spxaddr = (char *)vax_map_physmem(va->va_paddr, fbsize);
   1288 	if (spxaddr == 0) {
   1289 		aprint_error_dev(self, "unable to map framebuffer memory.\n");
   1290 		return 0;
   1291 	}
   1292 
   1293 	/* map all four RAMDAC registers */
   1294 	bt_addrl = (char *)vax_map_physmem(SPX_BT459_ADDRL, 1);
   1295 	if (bt_addrl == 0) {
   1296 		aprint_error_dev(self,
   1297 				 "unable to map BT459 Low Address register.\n");
   1298 		return 0;
   1299 	}
   1300 
   1301 	bt_addrh = (char *)vax_map_physmem(SPX_BT459_ADDRH, 1);
   1302 	if (bt_addrh == 0) {
   1303 		aprint_error_dev(self,
   1304 				 "unable to map BT459 High Address register.\n");
   1305 		return 0;
   1306 	}
   1307 
   1308 	bt_reg = (char *)vax_map_physmem(SPX_BT459_REG, 1);
   1309 	if (bt_reg == 0) {
   1310 		aprint_error_dev(self,
   1311 				 "unable to map BT459 I/O Register.\n");
   1312 		return 0;
   1313 	}
   1314 
   1315 	bt_cmap = (char *)vax_map_physmem(SPX_BT459_CMAP, 1);
   1316 	if (bt_cmap == 0) {
   1317 		aprint_error_dev(self,
   1318 				 "unable to map BT459 Color Map register.\n");
   1319 		return 0;
   1320 	}
   1321 
   1322 	return 1;
   1323 }
   1324 
   1325 static int
   1326 SPXg_map_fb(device_t self, struct vsbus_attach_args *va)
   1327 {
   1328 	int fbsize = SPXg_WIN_SIZE/VAX_NBPG;
   1329 	extern vaddr_t virtual_avail;
   1330 
   1331 	if (!self) {
   1332 		spxaddr = (char *)virtual_avail;
   1333 		virtual_avail += SPXg_WIN_SIZE;
   1334 		ioaccess((vaddr_t)spxaddr, SPXg_FB_ADDR, fbsize);
   1335 
   1336 		bt_addrl = (char *)virtual_avail;
   1337 		virtual_avail += VAX_NBPG;
   1338 		ioaccess((vaddr_t)bt_addrl, SPXg_BT460_BASE, 1);
   1339 
   1340 		bt_addrh = bt_addrl + 0x04;
   1341 		bt_reg = bt_addrl + 0x08;
   1342 		bt_cmap = bt_addrl + 0x0c;
   1343 		bt_wait = bt_addrl + 0x34;
   1344 
   1345 		return 1;
   1346 	}
   1347 
   1348 	spxaddr = (char *)vax_map_physmem(va->va_paddr, fbsize);
   1349 	if (spxaddr == 0) {
   1350 		aprint_error_dev(self,
   1351 				 "unable to map framebuffer memory window.\n");
   1352 		return 0;
   1353 	}
   1354 
   1355 	bt_addrl = (char *)vax_map_physmem(SPXg_BT460_BASE, 1);
   1356 	if (bt_addrl == 0) {
   1357 		aprint_error_dev(self,
   1358 				 "unable to map BT460 Low Address register.\n");
   1359 		return 0;
   1360 	}
   1361 	bt_addrh = bt_addrl + 0x04;
   1362 	bt_reg   = bt_addrl + 0x08;
   1363 	bt_cmap  = bt_addrl + 0x0c;
   1364 	bt_wait  = bt_addrl + 0x34;
   1365 
   1366 	return 1;
   1367 }
   1368 
   1369 static void
   1370 SPX_render_font(void)
   1371 {
   1372 	volatile char *fontaddr = spxaddr + FONT_STORAGE_START;
   1373 	int i, j, k, ch, pixel;
   1374 
   1375 	for (i = 0; i < 256; i++) for (j = 0; j < spx_font.fontheight; j++) {
   1376 		ch = QFONT(i, j);
   1377 
   1378 		/* regular characters */
   1379 		pixel = ((i * spx_font.fontheight)+ j) * spx_font.fontwidth;
   1380 		for (k = 0; k < spx_font.fontwidth; k++)
   1381 			if (ch & (1 << k))
   1382 				fontaddr[pixel++] = 0xff;
   1383 			else
   1384 				fontaddr[pixel++] = 0x00;
   1385 
   1386 		/* underlined characters */
   1387 		pixel = (((i + 256) * spx_font.fontheight) + j)
   1388 			* spx_font.fontwidth;
   1389 		for (k = 0; k < spx_font.fontwidth; k++)
   1390 			if ((ch & (1 << k)) || (j == (spx_font.fontheight - 1)))
   1391 				fontaddr[pixel++] = 0xff;
   1392 			else
   1393 				fontaddr[pixel++] = 0x00;
   1394 	}
   1395 }
   1396 
   1397 static void
   1398 SPXg_render_font(void)
   1399 {
   1400 	int i, j, k, ch, pixel;
   1401 
   1402 	for (i = 0; i < 256; i++) for (j = 0; j < spx_font.fontheight; j++) {
   1403 		ch = QFONT(i, j);
   1404 		/* regular characters */
   1405 		for (k = 0; k < spx_font.fontwidth; k++) {
   1406 			pixel = FONT_STORAGE_START
   1407 				+ (((i * spx_font.fontheight) + j)
   1408 				   * spx_font.fontwidth) + k;
   1409 			if ((pixel / SPXg_WIN_LINEAR) != spxg_current_page) {
   1410 				spxg_switch_page(pixel / SPXg_WIN_LINEAR);
   1411 				spxg_current_page = (pixel / SPXg_WIN_LINEAR);
   1412 			}
   1413 			SPXg_ADDR(pixel) = ch & (1 << k) ? 0xff : 0x00;
   1414 			spxg_delay();
   1415 		}
   1416 
   1417 		/* underlined characters */
   1418 		for (k = 0; k < spx_font.fontwidth; k++) {
   1419 			pixel = FONT_STORAGE_START
   1420 				+ ((((i + 256) * spx_font.fontheight) + j)
   1421 				   * spx_font.fontwidth) + k;
   1422 			if ((pixel / SPXg_WIN_LINEAR) != spxg_current_page) {
   1423 				spxg_switch_page(pixel / SPXg_WIN_LINEAR);
   1424 				spxg_current_page = (pixel / SPXg_WIN_LINEAR);
   1425 			}
   1426 			if ((ch & (1 << k)) || (j == (spx_font.fontheight - 1)))
   1427 				SPXg_ADDR(pixel) = 0xff;
   1428 			else
   1429 				SPXg_ADDR(pixel) = 0x00;
   1430 			spxg_delay();
   1431 		}
   1432 	}
   1433 }
   1434 
   1435 static void
   1436 spx_init_common(device_t self, struct vsbus_attach_args *va)
   1437 {
   1438 	u_int i, j, k;
   1439 	int cookie;
   1440 	struct wsdisplay_font *wf;
   1441 	static int init_done;
   1442 
   1443 	if (init_done)
   1444 		return;
   1445 
   1446 	/* KA49: Identify framebuffer type */
   1447 	switch (vax_confdata & (CONF_LCSPX | CONF_SPXg)) {
   1448 	case CONF_LCSPX:
   1449 		fb_type = FB_IS_SPX;
   1450 		spx_blkcpy_func = SPX_blkcpy;
   1451 		spx_blkset_func = SPX_blkset;
   1452 		spx_putchar_func = SPX_putchar;
   1453 		break;
   1454 	case CONF_SPXg:
   1455 		fb_type = FB_IS_SPXg;
   1456 		spx_blkcpy_func = SPXg_blkcpy;
   1457 		spx_blkset_func = SPXg_blkset;
   1458 		spx_putchar_func = SPXg_putchar;
   1459 		break;
   1460 	case CONF_LCSPX | CONF_SPXg:
   1461 		panic("spxcnprobe: incorrect FB configuration\n");
   1462 		break;
   1463 	}
   1464 
   1465 	/* map SPX registers first */
   1466 	if (fb_type == FB_IS_SPX) {
   1467 		SPX_MAP_REGS(self, SPX);
   1468 	} else if(fb_type == FB_IS_SPXg) {
   1469 		SPX_MAP_REGS(self, SPXg);
   1470 	}
   1471 
   1472 	if (fb_type == FB_IS_SPXg)
   1473 		spxg_switch_page(0);
   1474 
   1475 	/* any KA42/43 SPX resolution detection code should be placed here */
   1476 	spx_depth = 8;
   1477 	spx_xsize = 1280;
   1478 	spx_ysize = 1024;
   1479 
   1480 	wsfont_init();
   1481 
   1482 	cookie = wsfont_find(NULL, 12, 21, 0, WSDISPLAY_FONTORDER_R2L,
   1483 			     WSDISPLAY_FONTORDER_L2R);
   1484 	if (cookie == -1)
   1485 		cookie = wsfont_find(NULL, 16, 0, 0, WSDISPLAY_FONTORDER_R2L, 0);
   1486 	if (cookie == -1)
   1487 		cookie = wsfont_find(NULL, 12, 0, 0, WSDISPLAY_FONTORDER_R2L, 0);
   1488 	if (cookie == -1)
   1489 		cookie = wsfont_find(NULL, 8, 0, 0, WSDISPLAY_FONTORDER_R2L, 0);
   1490 	if (cookie == -1)
   1491 		cookie = wsfont_find(NULL, 0, 0, 0, WSDISPLAY_FONTORDER_R2L,
   1492 				     WSDISPLAY_FONTORDER_L2R);
   1493 
   1494 	if (cookie == -1 || wsfont_lock(cookie, &wf))
   1495 		panic("spx_common_init: unable to load console font");
   1496 
   1497 	spx_font = *wf;
   1498 
   1499 	if (self != NULL) {
   1500 		aprint_normal_dev(self, "Using %s %dx%d font\n", wf->name,
   1501 				  spx_font.fontwidth, spx_font.fontheight);
   1502 	}
   1503 
   1504 	spx_cols = spx_xsize / spx_font.fontwidth;
   1505 	spx_rows = spx_ysize / spx_font.fontheight;
   1506 	spx_fb_size = spx_xsize * spx_ysize;
   1507 	spx_stdscreen.ncols = spx_cols;
   1508 	spx_stdscreen.nrows = spx_rows;
   1509 	spx_stdscreen.fontwidth = spx_font.fontwidth;
   1510 	spx_stdscreen.fontheight = spx_font.fontheight;
   1511 	sprintf(spx_stdscreen_name, "%dx%d", spx_cols, spx_rows);
   1512 
   1513 	/* for SPXg spx_fb_size represents FB window size, not FB length */
   1514 	if (fb_type == FB_IS_SPXg)
   1515 		spx_fb_size = SPXg_WIN_SIZE;
   1516 
   1517 	qf = spx_font.data;
   1518 	qf2 = (u_short *)spx_font.data;
   1519 
   1520 	if (fb_type == FB_IS_SPX) {
   1521 		SPX_MAP_FB(self, va, SPX);
   1522 	} else if (fb_type == FB_IS_SPXg) {
   1523 		SPX_MAP_FB(self, va, SPXg);
   1524 	}
   1525 
   1526 	/* render font glyphs to off-screen memory */
   1527 	if (fb_type == FB_IS_SPX)
   1528 		SPX_render_font();
   1529 	else if (fb_type == FB_IS_SPXg)
   1530 		SPXg_render_font();
   1531 
   1532 	/* set up default console color palette */
   1533 	for (i = 0; i < 8; i++) {
   1534 		spx_cmap.r[i] = spx_default_cmap.r[i];
   1535 		spx_cmap.g[i] = spx_default_cmap.g[i];
   1536 		spx_cmap.b[i] = spx_default_cmap.b[i];
   1537 		spx_update_cmap(i, spx_cmap.r[i], spx_cmap.g[i], spx_cmap.b[i]);
   1538 	}
   1539 
   1540 	/*
   1541 	 * Build 64x64 console cursor bitmap based on font dimensions.
   1542 	 * Palette colors are not used, but 4 pixels 2bpp of cursor sprite,
   1543 	 * fg/bg respectively.
   1544 	 */
   1545 	for (i = 0; i < 1024; i++)
   1546 		set_btreg(SPXDAC_REG_CRAM_BASE + i, 0);
   1547 
   1548 	/* build cursor line, 1 to 3 pixels high depending on font height */
   1549 	if (spx_font.fontheight > 26)
   1550 		i = spx_font.fontheight - 3;
   1551 	else if (spx_font.fontheight > 16)
   1552 		i = spx_font.fontheight - 2;
   1553 	else
   1554 		i = spx_font.fontheight - 1;
   1555 
   1556 	for (; i <= spx_font.fontheight - 1; i++) {
   1557 		for (j = 0; j < (spx_font.fontwidth >> 2); j++)
   1558 			set_btreg(SPXDAC_REG_CRAM_BASE + i*16 + j, 0xff);
   1559 		k = (spx_font.fontwidth & 3) << 1;
   1560 		set_btreg(SPXDAC_REG_CRAM_BASE + i*16 + j, (1 << k) - 1);
   1561 	}
   1562 
   1563 	if (fb_type == FB_IS_SPX) {
   1564 		/* set SPX write/read plane masks */
   1565 		SPX_REG(0x3170) = 0xffffffff;
   1566 		SPX_REG(0x3174) = 0xffffffff;
   1567 	}
   1568 
   1569 	/* RAMDAC type-specific configuration */
   1570 	if (fb_type == FB_IS_SPX)
   1571 		set_btreg(SPXDAC_REG_CMD2, 0xc0);
   1572 
   1573 	/* common configuration */
   1574 	set_btreg(SPXDAC_REG_CMD0, 0x48);
   1575 	set_btreg(SPXDAC_REG_CMD1, 0x00);
   1576 	set_btreg(SPXDAC_REG_PRM, 0xff);
   1577 
   1578 	set_btreg(SPXDAC_REG_CXLO, 0);
   1579 	set_btreg(SPXDAC_REG_CXHI, 0);
   1580 	set_btreg(SPXDAC_REG_CYLO, 0);
   1581 	set_btreg(SPXDAC_REG_CYHI, 0);
   1582 
   1583 	set_btreg(SPXDAC_REG_WXLO, 0);
   1584 	set_btreg(SPXDAC_REG_WXHI, 0);
   1585 	set_btreg(SPXDAC_REG_WYLO, 0);
   1586 	set_btreg(SPXDAC_REG_WYHI, 0);
   1587 
   1588 	set_btreg(SPXDAC_REG_WWLO, 0);
   1589 	set_btreg(SPXDAC_REG_WWHI, 0);
   1590 	set_btreg(SPXDAC_REG_WHLO, 0);
   1591 	set_btreg(SPXDAC_REG_WHHI, 0);
   1592 
   1593 	init_done = 1;
   1594 }
   1595