xref: /netbsd-src/sys/arch/vax/vsa/spx.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: spx.c,v 1.4 2010/12/14 23:31:16 matt 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.4 2010/12/14 23:31:16 matt 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/malloc.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);
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;
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 */
995 			savescr = curscr;
996 			curscr = NULL;
997 			/* clear screen? */
998 		}
999 
1000 		return EPASSTHROUGH;
1001 
1002 	case WSDISPLAYIO_SVIDEO:
1003 		if (*(u_int *)data == WSDISPLAYIO_VIDEO_ON && spx_off) {
1004 			/* Unblank video */
1005 			set_btreg(SPXDAC_REG_CMD0, 0x48);
1006 
1007 			/* Enable sync */
1008 			set_btreg(SPXDAC_REG_CMD2, 0xc0);
1009 
1010 			spx_off = 0;
1011 		} else if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF && !spx_off) {
1012 			/* Blank video */
1013 			set_btreg(SPXDAC_REG_CMD0, 68);
1014 
1015 			/* Disable sync */
1016 			set_btreg(SPXDAC_REG_CMD2, 0x40);
1017 			spx_off = 1;
1018 		}
1019 		break;
1020 
1021 	case WSDISPLAYIO_GVIDEO:
1022 		*(u_int *)data = spx_off == 0 ?
1023 		WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
1024 		break;
1025 
1026 	default:
1027 		return EPASSTHROUGH;
1028 	}
1029 	return 0;
1030 }
1031 
1032 /*
1033  * Bad idea on SPXg/gt due to windowed framebuffer access
1034  */
1035 static paddr_t
1036 spx_mmap(void *v, void *vs, off_t offset, int prot)
1037 {
1038 	if (fb_type != FB_IS_SPX)
1039 		return -1;
1040 
1041 	if (offset >= spx_fb_size || offset < 0)
1042 		return -1;
1043 
1044 	return (SPX_FB_ADDR + offset) >> PGSHIFT;
1045 }
1046 
1047 static int
1048 spx_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
1049 		 int *curxp, int *curyp, long *defattrp)
1050 {
1051 	int i;
1052 	struct spx_screen *ss;
1053 
1054 	ss = malloc(sizeof(struct spx_screen), M_DEVBUF, M_WAITOK | M_ZERO);
1055 
1056 	*cookiep = ss;
1057 	*curxp = *curyp = 0;
1058 	*defattrp = (SPX_BG_COLOR << 4) | SPX_FG_COLOR;
1059 
1060 	for (i = 0; i < spx_cols * spx_rows; ++i)
1061 		ss->ss_image[i].attr = *defattrp;
1062 
1063 	return 0;
1064 }
1065 
1066 static void
1067 spx_free_screen(void *v, void *cookie)
1068 {
1069 /* FIXME add something to actually free malloc()ed screen? */
1070 }
1071 
1072 static int
1073 spx_show_screen(void *v, void *cookie, int waitok,
1074 		void (*cb)(void *, int, int), void *cbarg)
1075 {
1076 	struct spx_screen *ss = cookie;
1077 	u_int row, col, attr;
1078 	u_char c;
1079 
1080 	if (ss == curscr)
1081 		return (0);
1082 
1083 	prevscr = curscr;
1084 	curscr = ss;
1085 
1086 	for (row = 0; row < spx_rows; row++) {
1087 		for (col = 0; col < spx_cols; col++) {
1088 			u_int idx = row * spx_cols + col;
1089 			attr = ss->ss_image[idx].attr;
1090 			c = ss->ss_image[idx].data;
1091 
1092 			/* check if cell requires updating */
1093 			if ((c != prevscr->ss_image[idx].data) ||
1094 			    (attr != prevscr->ss_image[idx].attr)) {
1095 				if (c < 32) c = 32;
1096 				spx_putchar(ss, row, col, c, attr);
1097 			}
1098 		}
1099 	}
1100 
1101 	row = ss->ss_cury; col = ss->ss_curx;
1102 
1103 	spx_cursor(ss, cur_on, row, col);
1104 
1105 	cur_fg = ss->ss_cur_fg;
1106 	cur_bg = ss->ss_cur_bg;
1107 
1108 	return (0);
1109 }
1110 
1111 static int
1112 spx_get_cmap(struct wsdisplay_cmap *p)
1113 {
1114 	u_int index = p->index, count = p->count;
1115 	int error;
1116 
1117 	if (index >= (1 << spx_depth) || count > (1 << spx_depth) - index)
1118 		return (EINVAL);
1119 
1120 	error = copyout(&spx_cmap.r[index], p->red, count);
1121 	if (error)
1122 		return error;
1123 
1124 	error = copyout(&spx_cmap.g[index], p->green, count);
1125 	if (error)
1126 		return error;
1127 
1128 	error = copyout(&spx_cmap.b[index], p->blue, count);
1129 	return error;
1130 }
1131 
1132 static int
1133 spx_set_cmap(struct wsdisplay_cmap *p)
1134 {
1135 	u_int index = p->index, count = p->count;
1136 	int error, s;
1137 
1138 	if (index >= (1 << spx_depth) || count > (1 << spx_depth) - index)
1139 		return (EINVAL);
1140 
1141 	error = copyin(p->red, &spx_cmap.r[index], count);
1142 	if (error)
1143 		return error;
1144 
1145 	error = copyin(p->green, &spx_cmap.g[index], count);
1146 	if (error)
1147 		return error;
1148 
1149 	error = copyin(p->blue, &spx_cmap.b[index], count);
1150 
1151 	if (error)
1152 		return error;
1153 
1154 	s = spltty();
1155 	while (count-- > 0) {
1156 		spx_update_cmap(index,
1157 				spx_cmap.r[index],
1158 				spx_cmap.g[index],
1159 				spx_cmap.b[index]);
1160 		index++;
1161 	}
1162 	splx(s);
1163 	return (0);
1164 }
1165 
1166 cons_decl(spx);
1167 
1168 void
1169 spxcninit(struct consdev *cndev)
1170 {
1171 	int i;
1172 
1173 	spx_blkset(0, 0, spx_xsize, spx_ysize, SPX_BG_COLOR);
1174 
1175 	curscr = &spx_conscreen;
1176 
1177 	for (i = 0; i < spx_cols * spx_rows; i++)
1178 		spx_conscreen.ss_image[i].attr =
1179 			(SPX_BG_COLOR << 4) | SPX_FG_COLOR;
1180 	wsdisplay_cnattach(&spx_stdscreen, &spx_conscreen, 0, 0,
1181 			   (SPX_BG_COLOR << 4) | SPX_FG_COLOR);
1182 	cn_tab->cn_pri = CN_INTERNAL;
1183 
1184 #if NDZKBD > 0
1185 	dzkbd_cnattach(0); /* Connect keyboard and screen together */
1186 #endif
1187 }
1188 
1189 /*
1190  * Called very early to setup the glass tty as console.
1191  * Because it's called before the VM system is inited, virtual memory
1192  * for the framebuffer can be stolen directly without disturbing anything.
1193  */
1194 void
1195 spxcnprobe(struct consdev *cndev)
1196 {
1197 	extern const struct cdevsw wsdisplay_cdevsw;
1198 
1199 	/* Only for VS 4000/90, 90A and 96 with LCSPX or SPXg/gt*/
1200 	if ((vax_boardtype != VAX_BTYP_49) ||
1201 	    ((vax_confdata & (CONF_LCSPX | CONF_SPXg)) == 0))
1202 		return;
1203 
1204 	if (((vax_confdata & 8) && (vax_boardtype == VAX_BTYP_49)) ||
1205 	    (((vax_confdata & KA420_CFG_L3CON) ||
1206 	      (vax_confdata & KA420_CFG_MULTU)) &&
1207 	     ((vax_boardtype == VAX_BTYP_420) ||
1208 	      (vax_boardtype == VAX_BTYP_43)))) {
1209 		aprint_normal("spxcnprobe: Diagnostic console\n");
1210 		return; /* Diagnostic console */
1211 	}
1212 
1213 	spx_init_common(NULL, NULL);
1214 
1215 	cndev->cn_pri = CN_INTERNAL;
1216 	cndev->cn_dev = makedev(cdevsw_lookup_major(&wsdisplay_cdevsw), 0);
1217 }
1218 
1219 static int
1220 spx_map_regs(device_t self,
1221 	     u_int raddr, u_int rsize, u_int r1addr, u_int r1size)
1222 {
1223 	extern vaddr_t virtual_avail;
1224 
1225 	if (!self) {
1226 		regaddr = (long*)virtual_avail;
1227 		virtual_avail += rsize;
1228 		ioaccess((vaddr_t)regaddr, raddr, rsize/VAX_NBPG);
1229 
1230 		regaddr1 = (long*)virtual_avail;
1231 		virtual_avail += r1size;
1232 		ioaccess((vaddr_t)regaddr1, r1addr, r1size/VAX_NBPG);
1233 
1234 		return 1;
1235 	}
1236 
1237 	regaddr = (long*)vax_map_physmem(raddr, rsize/VAX_NBPG);
1238 	if (regaddr == 0) {
1239 		aprint_error_dev(self,
1240 				 "unable to allocate register memory (1).\n");
1241 		return 0;
1242 	}
1243 
1244 	regaddr1 = (long*)vax_map_physmem(r1addr, r1size/VAX_NBPG);
1245 	if (regaddr1 == 0) {
1246 		aprint_error_dev(self,
1247 				 "unable to allocate register memory (2).\n");
1248 		return 0;
1249 	}
1250 
1251 	return 1;
1252 }
1253 
1254 static int
1255 SPX_map_fb(device_t self, struct vsbus_attach_args *va)
1256 {
1257 	int fbsize = (spx_fb_size + FONT_STORAGE_SIZE)/VAX_NBPG;
1258 	extern vaddr_t virtual_avail;
1259 
1260 	if (!self) {
1261 		spxaddr = (char *)virtual_avail;
1262 		virtual_avail += (spx_fb_size + FONT_STORAGE_SIZE);
1263 		ioaccess((vaddr_t)spxaddr, SPX_FB_ADDR, fbsize);
1264 
1265 		bt_addrl = (char *)virtual_avail;
1266 		virtual_avail += VAX_NBPG;
1267 		ioaccess((vaddr_t)bt_addrl, SPX_BT459_ADDRL, 1);
1268 
1269 		bt_addrh = (char *)virtual_avail;
1270 		virtual_avail += VAX_NBPG;
1271 		ioaccess((vaddr_t)bt_addrh, SPX_BT459_ADDRH, 1);
1272 
1273 		bt_reg = (char *)virtual_avail;
1274 		virtual_avail += VAX_NBPG;
1275 		ioaccess((vaddr_t)bt_reg, SPX_BT459_REG, 1);
1276 
1277 		bt_cmap = (char *)virtual_avail;
1278 		virtual_avail += VAX_NBPG;
1279 		ioaccess((vaddr_t)bt_cmap, SPX_BT459_CMAP, 1);
1280 
1281 		return 1;
1282 	}
1283 
1284 	spxaddr = (char *)vax_map_physmem(va->va_paddr, fbsize);
1285 	if (spxaddr == 0) {
1286 		aprint_error_dev(self, "unable to map framebuffer memory.\n");
1287 		return 0;
1288 	}
1289 
1290 	/* map all four RAMDAC registers */
1291 	bt_addrl = (char *)vax_map_physmem(SPX_BT459_ADDRL, 1);
1292 	if (bt_addrl == 0) {
1293 		aprint_error_dev(self,
1294 				 "unable to map BT459 Low Address register.\n");
1295 		return 0;
1296 	}
1297 
1298 	bt_addrh = (char *)vax_map_physmem(SPX_BT459_ADDRH, 1);
1299 	if (bt_addrh == 0) {
1300 		aprint_error_dev(self,
1301 				 "unable to map BT459 High Address register.\n");
1302 		return 0;
1303 	}
1304 
1305 	bt_reg = (char *)vax_map_physmem(SPX_BT459_REG, 1);
1306 	if (bt_reg == 0) {
1307 		aprint_error_dev(self,
1308 				 "unable to map BT459 I/O Register.\n");
1309 		return 0;
1310 	}
1311 
1312 	bt_cmap = (char *)vax_map_physmem(SPX_BT459_CMAP, 1);
1313 	if (bt_cmap == 0) {
1314 		aprint_error_dev(self,
1315 				 "unable to map BT459 Color Map register.\n");
1316 		return 0;
1317 	}
1318 
1319 	return 1;
1320 }
1321 
1322 static int
1323 SPXg_map_fb(device_t self, struct vsbus_attach_args *va)
1324 {
1325 	int fbsize = SPXg_WIN_SIZE/VAX_NBPG;
1326 	extern vaddr_t virtual_avail;
1327 
1328 	if (!self) {
1329 		spxaddr = (char *)virtual_avail;
1330 		virtual_avail += SPXg_WIN_SIZE;
1331 		ioaccess((vaddr_t)spxaddr, SPXg_FB_ADDR, fbsize);
1332 
1333 		bt_addrl = (char *)virtual_avail;
1334 		virtual_avail += VAX_NBPG;
1335 		ioaccess((vaddr_t)bt_addrl, SPXg_BT460_BASE, 1);
1336 
1337 		bt_addrh = bt_addrl + 0x04;
1338 		bt_reg = bt_addrl + 0x08;
1339 		bt_cmap = bt_addrl + 0x0c;
1340 		bt_wait = bt_addrl + 0x34;
1341 
1342 		return 1;
1343 	}
1344 
1345 	spxaddr = (char *)vax_map_physmem(va->va_paddr, fbsize);
1346 	if (spxaddr == 0) {
1347 		aprint_error_dev(self,
1348 				 "unable to map framebuffer memory window.\n");
1349 		return 0;
1350 	}
1351 
1352 	bt_addrl = (char *)vax_map_physmem(SPXg_BT460_BASE, 1);
1353 	if (bt_addrl == 0) {
1354 		aprint_error_dev(self,
1355 				 "unable to map BT460 Low Address register.\n");
1356 		return 0;
1357 	}
1358 	bt_addrh = bt_addrl + 0x04;
1359 	bt_reg   = bt_addrl + 0x08;
1360 	bt_cmap  = bt_addrl + 0x0c;
1361 	bt_wait  = bt_addrl + 0x34;
1362 
1363 	return 1;
1364 }
1365 
1366 static void
1367 SPX_render_font(void)
1368 {
1369 	volatile char *fontaddr = spxaddr + FONT_STORAGE_START;
1370 	int i, j, k, ch, pixel;
1371 
1372 	for (i = 0; i < 256; i++) for (j = 0; j < spx_font.fontheight; j++) {
1373 		ch = QFONT(i, j);
1374 
1375 		/* regular characters */
1376 		pixel = ((i * spx_font.fontheight)+ j) * spx_font.fontwidth;
1377 		for (k = 0; k < spx_font.fontwidth; k++)
1378 			if (ch & (1 << k))
1379 				fontaddr[pixel++] = 0xff;
1380 			else
1381 				fontaddr[pixel++] = 0x00;
1382 
1383 		/* underlined characters */
1384 		pixel = (((i + 256) * spx_font.fontheight) + j)
1385 			* spx_font.fontwidth;
1386 		for (k = 0; k < spx_font.fontwidth; k++)
1387 			if ((ch & (1 << k)) || (j == (spx_font.fontheight - 1)))
1388 				fontaddr[pixel++] = 0xff;
1389 			else
1390 				fontaddr[pixel++] = 0x00;
1391 	}
1392 }
1393 
1394 static void
1395 SPXg_render_font(void)
1396 {
1397 	int i, j, k, ch, pixel;
1398 
1399 	for (i = 0; i < 256; i++) for (j = 0; j < spx_font.fontheight; j++) {
1400 		ch = QFONT(i, j);
1401 		/* regular characters */
1402 		for (k = 0; k < spx_font.fontwidth; k++) {
1403 			pixel = FONT_STORAGE_START
1404 				+ (((i * spx_font.fontheight) + j)
1405 				   * spx_font.fontwidth) + k;
1406 			if ((pixel / SPXg_WIN_LINEAR) != spxg_current_page) {
1407 				spxg_switch_page(pixel / SPXg_WIN_LINEAR);
1408 				spxg_current_page = (pixel / SPXg_WIN_LINEAR);
1409 			}
1410 			SPXg_ADDR(pixel) = ch & (1 << k) ? 0xff : 0x00;
1411 			spxg_delay();
1412 		}
1413 
1414 		/* underlined characters */
1415 		for (k = 0; k < spx_font.fontwidth; k++) {
1416 			pixel = FONT_STORAGE_START
1417 				+ ((((i + 256) * spx_font.fontheight) + j)
1418 				   * spx_font.fontwidth) + k;
1419 			if ((pixel / SPXg_WIN_LINEAR) != spxg_current_page) {
1420 				spxg_switch_page(pixel / SPXg_WIN_LINEAR);
1421 				spxg_current_page = (pixel / SPXg_WIN_LINEAR);
1422 			}
1423 			if ((ch & (1 << k)) || (j == (spx_font.fontheight - 1)))
1424 				SPXg_ADDR(pixel) = 0xff;
1425 			else
1426 				SPXg_ADDR(pixel) = 0x00;
1427 			spxg_delay();
1428 		}
1429 	}
1430 }
1431 
1432 static void
1433 spx_init_common(device_t self, struct vsbus_attach_args *va)
1434 {
1435 	u_int i, j, k;
1436 	int cookie;
1437 	struct wsdisplay_font *wf;
1438 	static int init_done;
1439 
1440 	if (init_done)
1441 		return;
1442 
1443 	/* KA49: Identify framebuffer type */
1444 	switch (vax_confdata & (CONF_LCSPX | CONF_SPXg)) {
1445 	case CONF_LCSPX:
1446 		fb_type = FB_IS_SPX;
1447 		spx_blkcpy_func = SPX_blkcpy;
1448 		spx_blkset_func = SPX_blkset;
1449 		spx_putchar_func = SPX_putchar;
1450 		break;
1451 	case CONF_SPXg:
1452 		fb_type = FB_IS_SPXg;
1453 		spx_blkcpy_func = SPXg_blkcpy;
1454 		spx_blkset_func = SPXg_blkset;
1455 		spx_putchar_func = SPXg_putchar;
1456 		break;
1457 	case CONF_LCSPX | CONF_SPXg:
1458 		panic("spxcnprobe: incorrect FB configuration\n");
1459 		break;
1460 	}
1461 
1462 	/* map SPX registers first */
1463 	if (fb_type == FB_IS_SPX) {
1464 		SPX_MAP_REGS(self, SPX);
1465 	} else if(fb_type == FB_IS_SPXg) {
1466 		SPX_MAP_REGS(self, SPXg);
1467 	}
1468 
1469 	if (fb_type == FB_IS_SPXg)
1470 		spxg_switch_page(0);
1471 
1472 	/* any KA42/43 SPX resolution detection code should be placed here */
1473 	spx_depth = 8;
1474 	spx_xsize = 1280;
1475 	spx_ysize = 1024;
1476 
1477 	wsfont_init();
1478 
1479 	cookie = wsfont_find(NULL, 12, 21, 0, WSDISPLAY_FONTORDER_R2L,
1480 			     WSDISPLAY_FONTORDER_L2R);
1481 	if (cookie == -1)
1482 		cookie = wsfont_find(NULL, 16, 0, 0, WSDISPLAY_FONTORDER_R2L, 0);
1483 	if (cookie == -1)
1484 		cookie = wsfont_find(NULL, 12, 0, 0, WSDISPLAY_FONTORDER_R2L, 0);
1485 	if (cookie == -1)
1486 		cookie = wsfont_find(NULL, 8, 0, 0, WSDISPLAY_FONTORDER_R2L, 0);
1487 	if (cookie == -1)
1488 		cookie = wsfont_find(NULL, 0, 0, 0, WSDISPLAY_FONTORDER_R2L,
1489 				     WSDISPLAY_FONTORDER_L2R);
1490 
1491 	if (cookie == -1 || wsfont_lock(cookie, &wf))
1492 		panic("spx_common_init: unable to load console font");
1493 
1494 	spx_font = *wf;
1495 
1496 	if (self != NULL) {
1497 		aprint_normal_dev(self, "Using %s %dx%d font\n", wf->name,
1498 				  spx_font.fontwidth, spx_font.fontheight);
1499 	}
1500 
1501 	spx_cols = spx_xsize / spx_font.fontwidth;
1502 	spx_rows = spx_ysize / spx_font.fontheight;
1503 	spx_fb_size = spx_xsize * spx_ysize;
1504 	spx_stdscreen.ncols = spx_cols;
1505 	spx_stdscreen.nrows = spx_rows;
1506 	spx_stdscreen.fontwidth = spx_font.fontwidth;
1507 	spx_stdscreen.fontheight = spx_font.fontheight;
1508 	sprintf(spx_stdscreen_name, "%dx%d", spx_cols, spx_rows);
1509 
1510 	/* for SPXg spx_fb_size represents FB window size, not FB length */
1511 	if (fb_type == FB_IS_SPXg)
1512 		spx_fb_size = SPXg_WIN_SIZE;
1513 
1514 	qf = spx_font.data;
1515 	qf2 = (u_short *)spx_font.data;
1516 
1517 	if (fb_type == FB_IS_SPX) {
1518 		SPX_MAP_FB(self, va, SPX);
1519 	} else if (fb_type == FB_IS_SPXg) {
1520 		SPX_MAP_FB(self, va, SPXg);
1521 	}
1522 
1523 	/* render font glyphs to off-screen memory */
1524 	if (fb_type == FB_IS_SPX)
1525 		SPX_render_font();
1526 	else if (fb_type == FB_IS_SPXg)
1527 		SPXg_render_font();
1528 
1529 	/* set up default console color palette */
1530 	for (i = 0; i < 8; i++) {
1531 		spx_cmap.r[i] = spx_default_cmap.r[i];
1532 		spx_cmap.g[i] = spx_default_cmap.g[i];
1533 		spx_cmap.b[i] = spx_default_cmap.b[i];
1534 		spx_update_cmap(i, spx_cmap.r[i], spx_cmap.g[i], spx_cmap.b[i]);
1535 	}
1536 
1537 	/*
1538 	 * Build 64x64 console cursor bitmap based on font dimensions.
1539 	 * Palette colors are not used, but 4 pixels 2bpp of cursor sprite,
1540 	 * fg/bg respectively.
1541 	 */
1542 	for (i = 0; i < 1024; i++)
1543 		set_btreg(SPXDAC_REG_CRAM_BASE + i, 0);
1544 
1545 	/* build cursor line, 1 to 3 pixels high depending on font height */
1546 	if (spx_font.fontheight > 26)
1547 		i = spx_font.fontheight - 3;
1548 	else if (spx_font.fontheight > 16)
1549 		i = spx_font.fontheight - 2;
1550 	else
1551 		i = spx_font.fontheight - 1;
1552 
1553 	for (; i <= spx_font.fontheight - 1; i++) {
1554 		for (j = 0; j < (spx_font.fontwidth >> 2); j++)
1555 			set_btreg(SPXDAC_REG_CRAM_BASE + i*16 + j, 0xff);
1556 		k = (spx_font.fontwidth & 3) << 1;
1557 		set_btreg(SPXDAC_REG_CRAM_BASE + i*16 + j, (1 << k) - 1);
1558 	}
1559 
1560 	if (fb_type == FB_IS_SPX) {
1561 		/* set SPX write/read plane masks */
1562 		SPX_REG(0x3170) = 0xffffffff;
1563 		SPX_REG(0x3174) = 0xffffffff;
1564 	}
1565 
1566 	/* RAMDAC type-specific configuration */
1567 	if (fb_type == FB_IS_SPX)
1568 		set_btreg(SPXDAC_REG_CMD2, 0xc0);
1569 
1570 	/* common configuration */
1571 	set_btreg(SPXDAC_REG_CMD0, 0x48);
1572 	set_btreg(SPXDAC_REG_CMD1, 0x00);
1573 	set_btreg(SPXDAC_REG_PRM, 0xff);
1574 
1575 	set_btreg(SPXDAC_REG_CXLO, 0);
1576 	set_btreg(SPXDAC_REG_CXHI, 0);
1577 	set_btreg(SPXDAC_REG_CYLO, 0);
1578 	set_btreg(SPXDAC_REG_CYHI, 0);
1579 
1580 	set_btreg(SPXDAC_REG_WXLO, 0);
1581 	set_btreg(SPXDAC_REG_WXHI, 0);
1582 	set_btreg(SPXDAC_REG_WYLO, 0);
1583 	set_btreg(SPXDAC_REG_WYHI, 0);
1584 
1585 	set_btreg(SPXDAC_REG_WWLO, 0);
1586 	set_btreg(SPXDAC_REG_WWHI, 0);
1587 	set_btreg(SPXDAC_REG_WHLO, 0);
1588 	set_btreg(SPXDAC_REG_WHHI, 0);
1589 
1590 	init_done = 1;
1591 }
1592