xref: /netbsd-src/sys/arch/vax/uba/qv.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*$Header: /cvsroot/src/sys/arch/vax/uba/qv.c,v 1.33 2017/05/22 17:15:45 ragge Exp $*/
2 /*
3  * Copyright (c) 2015 Charles H. Dickman. All rights reserved.
4  * Derived from smg.c
5  * Copyright (c) 1998 Ludd, University of Lule}, Sweden.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*       1         2         3         4         5         6         7        */
31 /*3456789012345678901234567890123456789012345678901234567890123456789012345678*/
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$Header: /cvsroot/src/sys/arch/vax/uba/qv.c,v 1.33 2017/05/22 17:15:45 ragge Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/callout.h>
39 #include <sys/conf.h>
40 #include <sys/cpu.h>
41 #include <sys/device.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/extent.h>		/***/
45 #include <sys/time.h>
46 #include <sys/bus.h>
47 #include <vax/include/pte.h>                /* temporary */
48 #include <machine/sid.h>
49 #include <dev/cons.h>
50 #include <dev/qbus/ubavar.h>
51 #include <dev/wscons/wsdisplayvar.h>
52 #include <dev/wscons/wsconsio.h>
53 #include <dev/wscons/wscons_callbacks.h>
54 #include <dev/wsfont/wsfont.h>
55 #include <dev/wsfb/genfbvar.h>
56 #include <vax/include/sgmap.h> /***/
57 
58 #include "uba_common.h"		/***/
59 #include "qv.h"
60 #include "qv_ic.h"
61 #include "qvaux.h"
62 #include "opt_wsfont.h"
63 
64 #define QMEMBASE        0x30000000
65 #define QVSIZE          0x40000
66 #define QV_SCANMAP      0x3F800
67 #define QV_CURSRAM      0x3FFE0
68 
69 #define QV_CSR          0
70 #define QV_CSR_1        (1 << 2)
71 #define QV_CSR_2        (1 << 3)
72 #define QV_CSR_BANK     (15 << 11)
73 #define QV_CUR_X        2
74 #define QV_CRTC_AR      8
75 #define QV_CRTC_DR      10
76 #define QV_IC           12
77 #define QV_ICDR         QV_ICDR
78 #define QV_ICSR         (QV_ICDR + 2)
79 
80 /* Screen hardware defs */
81 #define QV_COLS		128	/* char width of screen */
82 #define QV_ROWS		57	/* rows of char on screen */
83 #define QV_CHEIGHT	15	/* lines a char consists of */
84 #define QV_NEXTROW	(QV_COLS * QV_CHEIGHT)
85 #define	QV_YWIDTH	864
86 #define QV_XWIDTH	1024
87 
88 /* hardware cursor */
89 #define CUR_BLINKN      0x00
90 #define CUR_BLANK       0x20
91 #define CUR_BLINKS      0x40
92 #define CUR_BLINKF      0x60
93 #define CUR_BLINKM      0x60
94 #define CUR_OFF         CUR_BLANK
95 #define CUR_ON          CUR_BLINKS
96 #define CUR_START       10
97 #define CUR_END         11
98 #define CUR_HI          14
99 #define CUR_LO          15
100 
101 //static	uint16_t curcmd, curx, cury, hotX, hotY;
102 static	int     bgmask, fgmask;
103 
104 static	int     qv_match(device_t, cfdata_t, void *);
105 static	void    qv_attach(device_t, device_t, void *);
106 
107 static void	qv_cursor(void *, int, int, int);
108 static int	qv_mapchar(void *, int, unsigned int *);
109 static void	qv_putchar(void *, int, int, u_int, long);
110 static void	qv_copycols(void *, int, int, int,int);
111 static void	qv_erasecols(void *, int, int, int, long);
112 static void	qv_copyrows(void *, int, int, int);
113 static void	qv_eraserows(void *, int, int, long);
114 static int	qv_allocattr(void *, int, int, int, long *);
115 
116 const struct wsdisplay_emulops qv_emulops = {
117 	.cursor = qv_cursor,
118 	.mapchar = qv_mapchar,
119 	.putchar = qv_putchar,
120 	.copycols = qv_copycols,
121 	.erasecols = qv_erasecols,
122 	.copyrows = qv_copyrows,
123 	.eraserows = qv_eraserows,
124 	.allocattr = qv_allocattr
125 };
126 
127 struct _wsscreen_descr {
128         const struct wsscreen_descr qv_stdscreen;       /* MUST BE FIRST */
129         const uint16_t qv_crtc_param[16];
130 };
131 
132 /*
133  * Notes from the original Ultrix drivers
134  *
135  * Screen controller initialization parameters. The definations [sic] and use
136  * of these parameters can be found in the Motorola 68045 [sic] crtc specs. In
137  * essence they set the display parameters for the chip. The first set is
138  * for the 15" screen and the second is for the 19" separate sync. There
139  * is also a third set for a 19" composite sync monitor which we have not
140  * tested and which is not supported.
141  */
142 
143 const struct _wsscreen_descr qv_stdscreen[] = {
144         { { "80x30", 80, 30, &qv_emulops, 8,
145                 QV_CHEIGHT, WSSCREEN_UNDERLINE|WSSCREEN_REVERSE },
146             { 31, 25, 27, 0142, 31, 13, 30, 31, 4, 15, 040, 0, 0, 0, 0, 0 } },
147         { { "120x55", 120, 55, &qv_emulops, 8,
148                 QV_CHEIGHT, WSSCREEN_UNDERLINE|WSSCREEN_REVERSE },
149             { 39, 30, 32, 0262, 55, 5, 54, 54, 4, 15, 040, 0, 0, 0, 0, 0 } },
150         { { "128x57", QV_COLS, QV_ROWS, &qv_emulops, 8,
151                 QV_CHEIGHT, WSSCREEN_UNDERLINE|WSSCREEN_REVERSE },
152             { 39, 32, 33, 0264, 56, 5, 54, 54, 4, 15, 040, 0, 0, 0, 0, 0 } },
153 };
154 
155 const struct wsscreen_descr *_qv_scrlist[] = {
156 	&qv_stdscreen[2].qv_stdscreen,	/* default */
157 	&qv_stdscreen[1].qv_stdscreen,
158 	&qv_stdscreen[0].qv_stdscreen,
159 };
160 
161 const struct wsscreen_list qv_screenlist = {
162 	.nscreens = __arraycount(_qv_scrlist),
163 	.screens = _qv_scrlist,
164 };
165 
166 struct qv_softc {
167         device_t sc_dev;                /* device pointer */
168         bus_space_tag_t sc_iot;         /* register base */
169         bus_space_handle_t sc_ioh;
170         bus_space_tag_t sc_fbt;         /* frame buffer base */
171         bus_space_handle_t sc_fbh;
172         paddr_t sc_fbphys;              /* frame buffer phys addr */
173         char *sc_fb;                    /* frame buffer virt addr */
174 	uint16_t *sc_scanmap;		/* scan map virt addr */
175         char *sc_font;                  /* font glyph table */
176 
177         uint8_t sc_curon;               /* cursor on */
178         uint16_t sc_curx;               /* cursor x position */
179         uint16_t sc_cury;               /* cursor y position */
180         uint16_t sc_curhotX;            /* cursor x hot spot */
181         uint16_t sc_curhotY;            /* cursor y hot spot */
182 
183         struct qv_screen *sc_curscr;    /* current screen */
184 };
185 
186 #if 0
187 struct genfb_qv_softc {
188 	struct genfb_softc      sc_gen;
189 	bus_space_tag_t         sc_iot;
190 	bus_space_handle_t	sc_ioh;
191 	uint32_t		sc_wstype;
192 };
193 #endif
194 
195 static void     qv_crtc_wr(struct qv_softc *, uint16_t, uint16_t);
196 static void     qv_setcrtc(struct qv_softc *, const uint16_t *);
197 
198 static int	qv_ioctl(void *, void *, u_long, void *, int, struct lwp *);
199 static paddr_t	qv_mmap(void *, void *, off_t, int);
200 static int	qv_alloc_screen(void *, const struct wsscreen_descr *,
201                          void **, int *, int *, long *);
202 static void	qv_free_screen(void *, void *);
203 static int	qv_show_screen(void *, void *, int,
204 				     void (*) (void *, int, int), void *);
205 //static void	qv_crsr_blink(void *);
206 
207 int             qvauxprint(void *, const char *);
208 
209 const struct wsdisplay_accessops qv_accessops = {
210 	.ioctl = qv_ioctl,
211 	.mmap = qv_mmap,
212 	.alloc_screen = qv_alloc_screen,
213 	.free_screen = qv_free_screen,
214 	.show_screen = qv_show_screen,
215 };
216 
217 struct	qv_screen {
218         struct qv_softc *ss_sc;
219         const struct wsscreen_descr *ss_type;
220 	int	        ss_curx;
221 	int	        ss_cury;
222 	u_char	        ss_image[QV_ROWS][QV_COLS];	/* Image of screen */
223 	u_char	        ss_attr[QV_ROWS][QV_COLS];	/* Reversed etc... */
224 };
225 
226 static	struct qv_screen qv_conscreen; /* XXX no console support */
227 
228 static	callout_t qv_cursor_ch;
229 
230 CFATTACH_DECL_NEW(qv, sizeof(struct qv_softc),
231     qv_match, qv_attach, NULL, NULL);
232 #if 0
233 static int	genfb_match_qv(device_t, cfdata_t, void *);
234 static void	genfb_attach_qv(device_t, device_t, void *);
235 static int	genfb_ioctl_qv(void *, void *, u_long, void *, int,
236     struct lwp*);
237 static paddr_t	genfb_mmap_qv(void *, void *, off_t, int);
238 static int      genfb_borrow_qv(void *, bus_addr_t, bus_space_handle_t *);
239 
240 CFATTACH_DECL_NEW(genfb_qv, sizeof(struct genfb_qv_softc),
241     genfb_match_qv, genfb_attach_qv, NULL, NULL);
242 #endif
243 
244 /*
245  * autoconf match function
246  */
247 int
248 qv_match(device_t parent, cfdata_t match, void *aux)
249 {
250         struct uba_attach_args *ua = aux;
251         struct uba_softc *uh = device_private(parent);
252 
253         /* set up interrupts so the vector can be detected */
254 
255  	/* initialize qv interrupt controller */
256  	qv_ic_init(ua, QV_IC);
257 
258         /* set vertical retrace interrupt */
259         qv_ic_setvec(ua, QV_IC, QV_SYNC_VEC, uh->uh_lastiv - 4);
260         qv_ic_enable(ua, QV_IC, QV_SYNC_VEC, QV_IC_ENA);
261         qv_ic_arm(ua, QV_IC, QV_SYNC_VEC);
262 
263         /* enable interrupts */
264 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, QV_CSR,
265 	        bus_space_read_2(ua->ua_iot, ua->ua_ioh, QV_CSR) | (1 << 6));
266 
267         qv_ic_force(ua, QV_IC, QV_SYNC_VEC);
268 
269 	DELAY(20000);
270 
271         /* disable interrupts */
272         qv_ic_enable(ua, QV_IC, QV_SYNC_VEC, QV_IC_DIS);
273 
274         return 1;
275 }
276 
277 /* controller register write helper function */
278 static inline void
279 qv_reg_wr(struct qv_softc *sc, uint16_t addr, uint16_t data)
280 {
281         bus_space_write_2(sc->sc_iot, sc->sc_ioh, addr, data);
282 }
283 
284 /* controller register read helper function */
285 static inline uint16_t
286 qv_reg_rd(struct qv_softc *sc, uint16_t addr)
287 {
288         return bus_space_read_2(sc->sc_iot, sc->sc_ioh, addr);
289 }
290 
291 /*
292  * write a 6845 CRT controller register
293  */
294 static  void
295 qv_crtc_wr(struct qv_softc *sc, uint16_t addr, uint16_t data)
296 {
297         qv_reg_wr(sc, QV_CRTC_AR, addr);
298         qv_reg_wr(sc, QV_CRTC_DR, data);
299 }
300 
301 /*
302  * write a set of a set of video timing parameters to the CRTC
303  */
304 static void
305 qv_setcrtc(struct qv_softc *sc, const uint16_t *pp)
306 {
307         int i;
308 
309         for (i = 0; i < 14; i++)
310                 qv_crtc_wr(sc, i, pp[i]);
311 }
312 
313 static void inline
314 qv_ic_write(struct uba_attach_args *ua, bus_size_t offs, uint16_t value)
315 {
316         bus_space_write_2(ua->ua_iot, ua->ua_ioh, offs, value);
317 }
318 
319 void
320 qv_ic_init(struct uba_attach_args *ua, bus_size_t offs)
321 {
322         static int initted;
323         int i;
324 
325         if (!initted) {
326 		/* init the interrupt controller */
327 	        qv_ic_write(ua, QV_IC_SR + offs, QV_IC_RESET);
328 		/* reset irr			 */
329 	        qv_ic_write(ua, QV_IC_SR + offs, QV_IC_CLRIRR);
330 		/* specify individual vectors	 */
331 	        qv_ic_write(ua, QV_IC_SR + offs, QV_IC_MODE);
332 		/* preset autoclear data	 */
333 	        qv_ic_write(ua, QV_IC_SR + offs, QV_IC_ACREG);
334 		/* all setup as autoclear	 */
335 	        qv_ic_write(ua, QV_IC_DR + offs, 0xff);
336 
337 		/* clear all vector addresses */
338                 for (i = 0; i < 8; i++)
339                         qv_ic_setvec(ua, offs, i, 0);
340 
341                 initted = 1;
342         }
343 }
344 
345 void
346 qv_ic_setvec(struct uba_attach_args *ua, bus_size_t offs, int ic_vec,
347     int vecnum)
348 {
349 	/* preset vector address	*/
350 	qv_ic_write(ua, QV_IC_SR + offs, QV_IC_RMEM | RMEM_BC_1 | ic_vec);
351 
352 	/* give it the vector number	*/
353 	qv_ic_write(ua, QV_IC_DR + offs, vecnum);
354 }
355 
356 void
357 qv_ic_enable(struct uba_attach_args *ua, bus_size_t offs, int ic_vec,
358     int enable)
359 {
360         if (enable)
361 		/* enable the interrupt */
362 	        qv_ic_write(ua, QV_IC_SR + offs, QV_IC_CIMR | ic_vec);
363         else
364 		/* disable the interrupt */
365 	        qv_ic_write(ua, QV_IC_SR + offs, QV_IC_SIMR | ic_vec);
366 }
367 
368 void
369 qv_ic_arm(struct uba_attach_args *ua, bus_size_t offs, int arm)
370 {
371         if (arm)
372 		/* arm the interrupt ctrl	*/
373                 qv_ic_write(ua, QV_IC_SR + offs, QV_IC_ARM);
374         else
375 		/* disarm the interrupt ctrl	*/
376                 qv_ic_write(ua, QV_IC_SR + offs, QV_IC_DISARM);
377 }
378 
379 void
380 qv_ic_force(struct uba_attach_args *ua, bus_size_t offs, int ic_vec)
381 {
382 	/* force an interrupt	*/
383 	qv_ic_write(ua, QV_IC_SR + offs, QV_IC_SIRR | ic_vec);
384 }
385 
386 /*
387  * print attachment message
388  */
389 int
390 qvauxprint(void *aux, const char *pnp)
391 {
392         if (pnp) {
393                 aprint_normal("qvaux at %s", pnp);
394                 return (UNCONF);
395         }
396         return 0;
397 }
398 
399 /*
400  * autoconf attach function
401  */
402 void
403 qv_attach(device_t parent, device_t self, void *aux)
404 {
405         struct qv_softc *sc = device_private(self);
406         struct uba_softc *uh = device_private(parent);
407         struct uba_attach_args *ua = aux;
408         struct uba_attach_args aa;
409 	int fcookie;
410         struct wsemuldisplaydev_attach_args emulaa;
411 //        struct wsdisplaydev_attach_args dispaa;
412         struct wsdisplay_font *console_font;
413 	int line;
414 
415         sc->sc_dev = self;
416         sc->sc_iot = ua->ua_iot;
417         sc->sc_ioh = ua->ua_ioh;
418         sc->sc_fbt = sc->sc_iot;
419         sc->sc_fbphys = QMEMBASE + ((qv_reg_rd(sc, QV_CSR) & QV_CSR_BANK) << 7);
420 	if (bus_space_map(sc->sc_fbt, sc->sc_fbphys, QVSIZE,
421 			BUS_SPACE_MAP_LINEAR, &sc->sc_fbh)) {
422 		aprint_error_dev(self, "Couldn't alloc graphics memory.\n");
423 		return;
424 	}
425 
426 	aprint_normal(": fb %8lo", sc->sc_fbphys & 0x3fffff);
427 	sc->sc_fb = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
428 	sc->sc_scanmap = (uint16_t *)&sc->sc_fb[QV_SCANMAP];
429 #if 0
430 	if (extent_alloc_region(((struct uba_vsoftc*)uh)->uv_sgmap.aps_ex,
431 			sc->sc_fbphys & 0x3fffff, QVSIZE, EX_NOWAIT)) {
432 		aprint_error_dev(self,
433 			"Couldn't alloc graphics memory in sgmap.\n");
434 		return;
435 	}
436 #endif
437 	//aprint_normal(": fb 0x%08lx", sc->sc_fbphys & 0x3fffff);
438 
439 	bzero(sc->sc_fb, QVSIZE);
440 
441 	for (line = 0; line < QV_YWIDTH; line++) {
442 	        sc->sc_scanmap[line] = line;
443 	}
444 
445         /* program crtc */
446         qv_setcrtc(sc, qv_stdscreen[2].qv_crtc_param);
447 
448         /* enable video output */
449         qv_reg_wr(sc, QV_CSR, qv_reg_rd(sc, QV_CSR) | (1 << 2) | (1 << 3));
450 #if 0
451 	if (sc->sc_curscr == NULL)
452 		callout_init(&qv_cursor_ch, 0);
453 	sc->sc_curscr = &qv_conscreen;
454 
455 	callout_reset(&qv_cursor_ch, hz / 2, qv_crsr_blink, sc);
456 #endif
457         /* interrupt handlers - XXX */
458 
459         uh->uh_lastiv -= 4;
460 
461         wsfont_init();
462 	if ((fcookie = wsfont_find(NULL, 8, 15, 0, WSDISPLAY_FONTORDER_R2L,
463 	    WSDISPLAY_FONTORDER_L2R, WSFONT_FIND_BITMAP)) < 0) {
464 		aprint_error_dev(self, "could not find 8x15 font\n");
465 		return;
466 	}
467 	if (wsfont_lock(fcookie, &console_font) != 0) {
468 		aprint_error_dev(self, "could not lock 8x15 font\n");
469 		return;
470 	}
471 	sc->sc_font = console_font->data;
472 
473         aprint_normal("\n");
474 
475 	aa.ua_iot = ua->ua_iot;
476 	aa.ua_ioh = ua->ua_ioh + 32; // offset
477 	aa.ua_cvec = ua->ua_cvec - 4;
478 	if (config_search_ia(NULL, self, "qv", &aa) != NULL) {
479 	        config_found_ia(self, "qv", &aa, qvauxprint);
480                 uh->uh_lastiv -= 4;
481 	}
482 
483 	emulaa.console = 0; /* Not console */
484 	emulaa.scrdata = &qv_screenlist;
485 	emulaa.accessops = &qv_accessops;
486 	emulaa.accesscookie = self;
487 	if (config_search_ia(NULL, self, "wsemuldisplaydev", &emulaa) != NULL) {
488 	        config_found_ia(self, "wsemuldisplaydev", &emulaa,
489 	            wsemuldisplaydevprint);
490 	}
491 
492         //console_debugger();
493 	return;
494 }
495 
496 /* QVSS frame buffer */
497 
498 /*      uint_32 is stored little endian in frame buffer */
499 /*      bits are stored little endian in frame buffer   */
500 
501 /*      uint_32 *fb;                                     */
502 /*      fb = (int *)phystova(0x303c0000);                */
503 /*      *fb = 0x00000001; */ /* sets bit in first column */
504 
505 /* Frame Buffer Usage */
506 
507 /* characters are 8 bits wide and QVHEIGHT high */
508 /* the scan map is allocated in terms of character height, */
509 /* so a pointer to the top line of a character can step to the */
510 /* next row without looking up the memory location in the scan map */
511 
512 static	char *cursor;
513 static	int cur_on;
514 
515 /*
516  * return pointer to line in character glyph
517  */
518 static inline char *
519 qv_font(struct qv_softc *sc, int c, int line)
520 {
521         /* map char to font table offset */
522         if (c < 32)
523                 c = 32;
524         else if (c > 127)
525                 c -= 66;
526         else
527                 c -= 32;
528 
529         /* return pointer line in font glyph */
530         return &sc->sc_font[c*QV_CHEIGHT + line];
531 }
532 
533 /*
534  * return pointer to character line in frame buffer
535  */
536 static inline char *
537 qv_fbp(struct qv_softc *sc, int row, int col, int line)
538 {
539         return &sc->sc_fb[col + sc->sc_scanmap[row*QV_CHEIGHT + line]*QV_COLS];
540 }
541 
542 /*
543  * callout callback function to blink cursor
544  */
545 #if 0
546 static void
547 qv_crsr_blink(void *arg)
548 {
549         struct qv_softc *sc = arg;
550 
551 	if (cur_on)
552 		*cursor ^= 255;
553 	callout_reset(&qv_cursor_ch, hz / 2, qv_crsr_blink, sc);
554 }
555 #endif
556 /*
557  * emulop cursor
558  */
559 void
560 qv_cursor(void *id, int on, int row, int col)
561 {
562 	struct qv_screen * const ss = id;
563 
564 	if (ss == ss->ss_sc->sc_curscr) {
565 		*qv_fbp(ss->ss_sc, ss->ss_cury, ss->ss_curx, 14)
566 		    = *qv_font(ss->ss_sc,
567 		        ss->ss_image[ss->ss_cury][ss->ss_curx], 14);
568 		cursor = qv_fbp(ss->ss_sc, row, col, 14);
569 		if ((cur_on = on))
570 			*cursor ^= 255;
571 	}
572 	ss->ss_curx = col;
573 	ss->ss_cury = row;
574 }
575 
576 /*
577  * emulop mapchar
578  */
579 int
580 qv_mapchar(void *id, int uni, unsigned int *index)
581 {
582 	if (uni < 256) {
583 		*index = uni;
584 		return (5);
585 	}
586 	*index = ' ';
587 	return (0);
588 }
589 
590 /*
591  * emulop putchar
592  */
593 static void
594 qv_putchar(void *id, int row, int col, u_int c, long attr)
595 {
596 	struct qv_screen * const ss = id;
597 	int i;
598         char *gp;
599         char *fp;
600         char rvid;
601 
602 	c &= 0xff;
603 
604 	ss->ss_image[row][col] = c;
605 	ss->ss_attr[row][col] = attr;
606 	if (ss != ss->ss_sc->sc_curscr)
607 		return;
608 
609         gp = qv_font(ss->ss_sc, c, 0);
610         fp = qv_fbp(ss->ss_sc, row, col, 0);
611         rvid = (attr & WSATTR_REVERSE) ? 0xff : 0x00;
612         for (i = 0; i < QV_CHEIGHT; i++) {
613                 *fp = *gp++ ^ rvid;
614                 fp += QV_COLS;
615         }
616 
617 	if (attr & WSATTR_UNDERLINE)
618 	        *qv_fbp(ss->ss_sc, row, col, 14)
619 	            ^= *qv_fbp(ss->ss_sc, row, col, 14);
620 }
621 
622 /*
623  * emulop copy columns - copies columns inside a row
624  */
625 static void
626 qv_copycols(void *id, int row, int srccol, int dstcol, int ncols)
627 {
628 	struct qv_screen * const ss = id;
629 	int i;
630 
631 	memcpy(&ss->ss_image[row][dstcol], &ss->ss_image[row][srccol], ncols);
632 	memcpy(&ss->ss_attr[row][dstcol], &ss->ss_attr[row][srccol], ncols);
633 	if (ss != ss->ss_sc->sc_curscr)
634 		return;
635 	for (i = 0; i < QV_CHEIGHT; i++)
636 	        memcpy(qv_fbp(ss->ss_sc, row, dstcol, i),
637 	            qv_fbp(ss->ss_sc, row, srccol, i), ncols);
638 }
639 
640 /*
641  * emulop erase columns - erases a bunch of chars inside one row
642  */
643 static void
644 qv_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
645 {
646 	struct qv_screen * const ss = id;
647 	int i;
648 
649 	memset(&ss->ss_image[row][startcol], 0, ncols);
650 	memset(&ss->ss_attr[row][startcol], 0, ncols);
651 	if (ss != ss->ss_sc->sc_curscr)
652 		return;
653 	for (i = 0; i < QV_CHEIGHT; i++)
654 	        memset(qv_fbp(ss->ss_sc, row, startcol, i), 0, ncols);
655 }
656 
657 /*
658  * overlap check
659  * return 0 if no overlap
660  *        -1 if overlap and dst is less than src (move up)
661  * 	  +1 if overlap and src is less than dst (move down)
662  */
663 static inline int
664 qv_rows_overlap(int srcrow, int dstrow, int nrows)
665 {
666 	if (dstrow < srcrow) {
667 		if (dstrow + nrows <= srcrow)
668 			return 0;
669 		else
670 			return -1;
671 	}
672 	else {
673 		if (srcrow + nrows <= dstrow)
674 			return 0;
675 		else
676 			return 1;
677 	}
678 }
679 
680 /*
681  * emulop copyrows - copy entire rows
682  */
683 static void
684 qv_copyrows(void *id, int srcrow, int dstrow, int nrows)
685 {
686 	struct qv_screen * const ss = id;
687 	int ol;
688 	int n;
689 	int line;
690 	int tmp;
691 	uint16_t *sp;
692 	uint16_t *dp;
693 
694 	memcpy(&ss->ss_image[dstrow][0], &ss->ss_image[srcrow][0],
695 	    nrows * QV_COLS);
696 	memcpy(&ss->ss_attr[dstrow][0], &ss->ss_attr[srcrow][0],
697 	    nrows * QV_COLS);
698 	if (ss != ss->ss_sc->sc_curscr)
699 		return;
700 
701 	ol = qv_rows_overlap(srcrow, dstrow, nrows);
702 	if (ol == 0)
703 		for (n = 0; n < nrows; n++)
704 			bcopy(qv_fbp(ss->ss_sc, srcrow + n, 0, 0),
705 	    		    qv_fbp(ss->ss_sc, dstrow + n, 0, 0), QV_NEXTROW);
706 	else if (ol < 0) {
707 	 	for (n = 0; n < nrows; n++) {
708 			dp = &ss->ss_sc->sc_scanmap[(dstrow + n)*QV_CHEIGHT];
709 			sp = &ss->ss_sc->sc_scanmap[(srcrow + n)*QV_CHEIGHT];
710 			for (line = 0; line < QV_CHEIGHT; line++) {
711 				tmp = *dp;
712 				*dp = *sp;
713 				*sp = tmp;
714 				dp++;
715 				sp++;
716 			}
717 		}
718 		qv_copyrows(id, dstrow + nrows - srcrow + dstrow,
719 		    dstrow + nrows, srcrow - dstrow);
720 	}
721 	else {
722 	 	for (n = nrows - 1; n >= 0; n--) {
723 			dp = &ss->ss_sc->sc_scanmap[(dstrow + n)*QV_CHEIGHT];
724 			sp = &ss->ss_sc->sc_scanmap[(srcrow + n)*QV_CHEIGHT];
725 			for (line = 0; line < QV_CHEIGHT; line++) {
726 				tmp = *dp;
727 				*dp = *sp;
728 				*sp = tmp;
729 				dp++;
730 				sp++;
731 			}
732 		}
733 		qv_copyrows(id, srcrow, dstrow, dstrow - srcrow);
734 	}
735 }
736 
737 /*
738  * emulop eraserows - erase a number of entire rows
739  */
740 static void
741 qv_eraserows(void *id, int startrow, int nrows, long fillattr)
742 {
743 	struct qv_screen * const ss = id;
744 	int row;
745 
746 	memset(&ss->ss_image[startrow][0], 0, nrows * QV_COLS);
747 	memset(&ss->ss_attr[startrow][0], 0, nrows * QV_COLS);
748 	if (ss != ss->ss_sc->sc_curscr)
749 		return;
750 
751 	for (row = startrow; row < startrow + nrows; row++) {
752 	        memset(qv_fbp(ss->ss_sc, row, 0, 0), 0, QV_NEXTROW);
753 	}
754 }
755 
756 /*
757  * emulop allocattr
758  */
759 static int
760 qv_allocattr(void *id, int fg, int bg, int flags, long *attrp)
761 {
762 	*attrp = flags;
763 	return 0;
764 }
765 
766 /*
767  * emulop setcursor
768  */
769 static void
770 qv_setcursor(struct qv_softc *sc, struct wsdisplay_cursor *v)
771 {
772 	uint16_t red, green, blue;
773 	uint32_t curfg[16], curmask[16];
774 	uint16_t *curp;
775 	int i;
776 
777 	/* Enable cursor */
778 	if (v->which & WSDISPLAY_CURSOR_DOCUR) {
779 	        sc->sc_curon = (v->enable) ? CUR_ON : CUR_OFF;
780 	        qv_crtc_wr(sc, CUR_START, sc->sc_curon | (sc->sc_cury & 0x0f));
781 	}
782 	if (v->which & WSDISPLAY_CURSOR_DOHOT) {
783 		sc->sc_curhotX = v->hot.x;
784 		sc->sc_curhotY = v->hot.y;
785 	}
786 	if (v->which & WSDISPLAY_CURSOR_DOCMAP) {
787 		/* First background */
788 		red = fusword(v->cmap.red);
789 		green = fusword(v->cmap.green);
790 		blue = fusword(v->cmap.blue);
791 		bgmask = (((30L * red + 59L * green + 11L * blue) >> 8) >=
792 		    (((1<<8)-1)*50)) ? ~0 : 0;
793 		red = fusword(v->cmap.red+2);
794 		green = fusword(v->cmap.green+2);
795 		blue = fusword(v->cmap.blue+2);
796 		fgmask = (((30L * red + 59L * green + 11L * blue) >> 8) >=
797 		    (((1<<8)-1)*50)) ? ~0 : 0;
798 	}
799 	if (v->which & WSDISPLAY_CURSOR_DOSHAPE) {
800 		copyin(v->image, curfg, sizeof(curfg));
801 		copyin(v->mask, curmask, sizeof(curmask));      /* not used */
802 	        curp = (uint16_t *) &(sc->sc_fb)[QV_CURSRAM];
803 		for (i = 0; i < sizeof(curfg)/sizeof(curfg[0]); i++) {
804 		        curp[i] = (uint16_t)curfg[i];
805 		}
806 	}
807 }
808 
809 /*
810  * emulop ioctl
811  */
812 int
813 qv_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
814 {
815 	struct wsdisplay_fbinfo *fb = (void *)data;
816 	//static uint16_t curc;
817         struct qv_softc *sc = device_private(v);
818 
819 	switch (cmd) {
820 	case WSDISPLAYIO_GTYPE:
821 		*(u_int *)data = WSDISPLAY_TYPE_VAX_MONO;
822 		break;
823 
824 	case WSDISPLAYIO_GINFO:
825 		fb->height = QV_YWIDTH;
826 		fb->width = QV_XWIDTH;
827 		fb->depth = 1;
828 		fb->cmsize = 2;
829 		break;
830 
831 	case WSDISPLAYIO_SVIDEO:
832 		if (*(u_int *)data == WSDISPLAYIO_VIDEO_ON) {
833                         /* enable video output */
834   		        qv_reg_wr(sc, QV_CSR,
835   		            qv_reg_rd(sc, QV_CSR) | (1 << 2));
836 		} else {
837                         /* disable video output */
838                         qv_reg_wr(sc, QV_CSR,
839                             qv_reg_rd(sc, QV_CSR) & ~(1 << 2));
840 		}
841 		break;
842 
843 	case WSDISPLAYIO_GVIDEO:
844 		*(u_int *)data = (qv_reg_rd(sc, QV_CSR) & (1 << 2))
845 		    ? WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
846 		break;
847 
848 	case WSDISPLAYIO_SCURSOR:
849 		qv_setcursor(sc, (struct wsdisplay_cursor *)data);
850 		break;
851 
852 	case WSDISPLAYIO_SCURPOS:
853 	        sc->sc_curx = ((struct wsdisplay_curpos *)data)->x;
854 		sc->sc_cury = ((struct wsdisplay_curpos *)data)->y;
855                 qv_crtc_wr(sc, CUR_START, CUR_OFF | (sc->sc_cury & 0x0f));
856                 qv_crtc_wr(sc, CUR_HI, sc->sc_cury >> 4);
857                 qv_reg_wr(sc, QV_CUR_X, sc->sc_curx);
858                 qv_crtc_wr(sc, CUR_START,
859                     sc->sc_curon | (sc->sc_cury & 0x0f));
860 		break;
861 
862 	case WSDISPLAYIO_GCURPOS:
863 		((struct wsdisplay_curpos *)data)->x = sc->sc_curx;
864 		((struct wsdisplay_curpos *)data)->y = sc->sc_cury;
865 		break;
866 
867 	case WSDISPLAYIO_GCURMAX:
868 		((struct wsdisplay_curpos *)data)->x = 16;
869 		((struct wsdisplay_curpos *)data)->y = 16;
870 		break;
871 
872 	default:
873 		return EPASSTHROUGH;
874 	}
875 	return 0;
876 }
877 
878 /*
879  * emulop mmap
880  */
881 static paddr_t
882 qv_mmap(void *v, void *vs, off_t offset, int prot)
883 {
884         struct qv_softc *sc = device_private(v);
885 
886 	if (offset >= QVSIZE || offset < 0)
887 		return -1;
888 	return (sc->sc_fbphys) >> PGSHIFT;
889 }
890 
891 /*
892  * emulop allocate screen
893  */
894 int
895 qv_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
896     int *curxp, int *curyp, long *defattrp)
897 {
898         struct qv_softc *sc = device_private(v);
899         struct qv_screen *ss;
900 
901 	ss = malloc(sizeof(struct qv_screen), M_DEVBUF, M_WAITOK|M_ZERO);
902 	ss->ss_sc = sc;
903 	ss->ss_type = type;
904 	*cookiep = ss;
905 	*curxp = *curyp = *defattrp = 0;
906         printf("qv_alloc_screen: \"%s\" %p\n", type->name, ss);
907 	return 0;
908 }
909 
910 /*
911  * emulop free screen
912  */
913 void
914 qv_free_screen(void *v, void *cookie)
915 {
916         printf("qv_free_screen: %p\n", cookie);
917         free(cookie, M_DEVBUF);
918 }
919 
920 /*
921  * emulop show screen
922  */
923 int
924 qv_show_screen(void *v, void *cookie, int waitok,
925     void (*cb)(void *, int, int), void *cbarg)
926 {
927 	struct qv_screen *ss = cookie;
928 	const struct _wsscreen_descr *descr;
929 	int row, col, line;
930         printf("qv_show_screen: %p\n", cookie);
931 
932 	if (ss == ss->ss_sc->sc_curscr)
933 		return (0);
934 
935         descr = (const struct _wsscreen_descr *)(ss->ss_type);
936         qv_setcrtc(ss->ss_sc, descr->qv_crtc_param);
937 	for (row = 0; row < QV_ROWS; row++)
938 		for (line = 0; line < QV_CHEIGHT; line++) {
939 			for (col = 0; col < QV_COLS; col++) {
940 				u_char s, c = ss->ss_image[row][col];
941 
942 				if (c < 32)
943 					c = 32;
944 				s = *qv_font(ss->ss_sc, c, line);
945 				if (ss->ss_attr[row][col] & WSATTR_REVERSE)
946 					s ^= 255;
947 				*qv_fbp(ss->ss_sc, row, col, line) = s;
948 
949 				if (ss->ss_attr[row][col] & WSATTR_UNDERLINE)
950 					*qv_fbp(ss->ss_sc, row, col, line)
951 					    = 255;
952 			}
953 		}
954         cursor = qv_fbp(ss->ss_sc, ss->ss_cury, ss->ss_curx, 14);
955 	ss->ss_sc->sc_curscr = ss;
956 	return (0);
957 }
958 
959 #if 0
960 void
961 qv_reset_establish(void (*reset)(device_t), device_t dev)
962 {
963         uba_reset_establish(reset, device_parent(dev));
964 }
965 #endif
966 cons_decl(qv);
967 
968 void
969 qvcninit(struct consdev *cndev)
970 {
971 	int fcookie;
972 	struct wsdisplay_font *console_font;
973 	extern void lkccninit(struct consdev *);
974 	extern int lkccngetc(dev_t);
975 	extern int dz_vsbus_lk201_cnattach(int);
976 
977         //printf("qvcninit: \n");
978 	/* Clear screen */
979 	//memset(qv_addr, 0, 128*864);
980 
981 	callout_init(&qv_cursor_ch, 0);
982 	//curscr = &qv_conscreen;
983 	wsdisplay_cnattach(&qv_stdscreen[0].qv_stdscreen,
984 	    &qv_conscreen, 0, 0, 0);
985 	cn_tab->cn_pri = CN_INTERNAL;
986 	wsfont_init();
987 	if ((fcookie = wsfont_find(NULL, 8, 15, 0, WSDISPLAY_FONTORDER_R2L,
988 	    WSDISPLAY_FONTORDER_L2R, WSFONT_FIND_BITMAP)) < 0)
989 	{
990 		printf("qv: could not find 8x15 font\n");
991 		return;
992 	}
993 	if (wsfont_lock(fcookie, &console_font) != 0) {
994 		printf("qv: could not lock 8x15 font\n");
995 		return;
996 	}
997 	//qf = console_font->data;
998 
999 #if NQVKBD > 0 && 0
1000 	qvkbd_cnattach(0); /* Connect keyboard and screen together */
1001 #endif
1002 }
1003 
1004 /*
1005  * Called very early to setup the glass tty as console.
1006  * Because it's called before the VM system is inited, virtual memory
1007  * for the framebuffer can be stolen directly without disturbing anything.
1008  */
1009 void
1010 qvcnprobe(struct consdev *cndev)
1011 {
1012         printf("qvcnprobe: \n");
1013 #if 0
1014 	extern vaddr_t virtual_avail;
1015 	extern const struct cdevsw wsdisplay_cdevsw;
1016 
1017 	switch (vax_boardtype) {
1018 	case VAX_BTYP_410:
1019 	case VAX_BTYP_420:
1020 	case VAX_BTYP_43:
1021 		if ((vax_confdata & KA420_CFG_L3CON) ||
1022 		    (vax_confdata & KA420_CFG_MULTU))
1023 			break; /* doesn't use graphics console */
1024 		qv_addr = (void *)virtual_avail;
1025 		virtual_avail += QVSIZE;
1026 		ioaccess((vaddr_t)qv_addr, QVADDR, (QVSIZE/VAX_NBPG));
1027 		cndev->cn_pri = CN_INTERNAL;
1028 		cndev->cn_dev = makedev(cdevsw_lookup_major(&wsdisplay_cdevsw),
1029 					0);
1030 		break;
1031 	default:
1032 		break;
1033 	}
1034 #endif
1035 }
1036