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