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