Home | History | Annotate | Line # | Download | only in uba
      1 /* $NetBSD: qv.c,v 1.38 2021/08/07 16:19:07 thorpej 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, "$NetBSD: qv.c,v 1.38 2021/08/07 16:19:07 thorpej 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/kmem.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_WAITOK)) {
    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(self, &aa,
    479 			  CFARGS(.iattr = "qv")) != NULL) {
    480 	        config_found(self, &aa, qvauxprint,
    481 		    CFARGS(.iattr = "qv"));
    482                 uh->uh_lastiv -= 4;
    483 	}
    484 
    485 	emulaa.console = 0; /* Not console */
    486 	emulaa.scrdata = &qv_screenlist;
    487 	emulaa.accessops = &qv_accessops;
    488 	emulaa.accesscookie = self;
    489 	if (config_search(self, &emulaa,
    490 			  CFARGS(.iattr = "wsemuldisplaydev")) != NULL) {
    491 	        config_found(self, &emulaa, wsemuldisplaydevprint,
    492 		    CFARGS(.iattr = "wsemuldisplaydev"));
    493 	}
    494 
    495         //console_debugger();
    496 	return;
    497 }
    498 
    499 /* QVSS frame buffer */
    500 
    501 /*      uint_32 is stored little endian in frame buffer */
    502 /*      bits are stored little endian in frame buffer   */
    503 
    504 /*      uint_32 *fb;                                     */
    505 /*      fb = (int *)phystova(0x303c0000);                */
    506 /*      *fb = 0x00000001; */ /* sets bit in first column */
    507 
    508 /* Frame Buffer Usage */
    509 
    510 /* characters are 8 bits wide and QVHEIGHT high */
    511 /* the scan map is allocated in terms of character height, */
    512 /* so a pointer to the top line of a character can step to the */
    513 /* next row without looking up the memory location in the scan map */
    514 
    515 static	char *cursor;
    516 static	int cur_on;
    517 
    518 /*
    519  * return pointer to line in character glyph
    520  */
    521 static inline char *
    522 qv_font(struct qv_softc *sc, int c, int line)
    523 {
    524         /* map char to font table offset */
    525         if (c < 32)
    526                 c = 32;
    527         else if (c > 127)
    528                 c -= 66;
    529         else
    530                 c -= 32;
    531 
    532         /* return pointer line in font glyph */
    533         return &sc->sc_font[c*QV_CHEIGHT + line];
    534 }
    535 
    536 /*
    537  * return pointer to character line in frame buffer
    538  */
    539 static inline char *
    540 qv_fbp(struct qv_softc *sc, int row, int col, int line)
    541 {
    542         return &sc->sc_fb[col + sc->sc_scanmap[row*QV_CHEIGHT + line]*QV_COLS];
    543 }
    544 
    545 /*
    546  * callout callback function to blink cursor
    547  */
    548 #if 0
    549 static void
    550 qv_crsr_blink(void *arg)
    551 {
    552         struct qv_softc *sc = arg;
    553 
    554 	if (cur_on)
    555 		*cursor ^= 255;
    556 	callout_reset(&qv_cursor_ch, hz / 2, qv_crsr_blink, sc);
    557 }
    558 #endif
    559 /*
    560  * emulop cursor
    561  */
    562 void
    563 qv_cursor(void *id, int on, int row, int col)
    564 {
    565 	struct qv_screen * const ss = id;
    566 
    567 	if (ss == ss->ss_sc->sc_curscr) {
    568 		*qv_fbp(ss->ss_sc, ss->ss_cury, ss->ss_curx, 14)
    569 		    = *qv_font(ss->ss_sc,
    570 		        ss->ss_image[ss->ss_cury][ss->ss_curx], 14);
    571 		cursor = qv_fbp(ss->ss_sc, row, col, 14);
    572 		if ((cur_on = on))
    573 			*cursor ^= 255;
    574 	}
    575 	ss->ss_curx = col;
    576 	ss->ss_cury = row;
    577 }
    578 
    579 /*
    580  * emulop mapchar
    581  */
    582 int
    583 qv_mapchar(void *id, int uni, unsigned int *index)
    584 {
    585 	if (uni < 256) {
    586 		*index = uni;
    587 		return (5);
    588 	}
    589 	*index = ' ';
    590 	return (0);
    591 }
    592 
    593 /*
    594  * emulop putchar
    595  */
    596 static void
    597 qv_putchar(void *id, int row, int col, u_int c, long attr)
    598 {
    599 	struct qv_screen * const ss = id;
    600 	int i;
    601         char *gp;
    602         char *fp;
    603         char rvid;
    604 
    605 	c &= 0xff;
    606 
    607 	ss->ss_image[row][col] = c;
    608 	ss->ss_attr[row][col] = attr;
    609 	if (ss != ss->ss_sc->sc_curscr)
    610 		return;
    611 
    612         gp = qv_font(ss->ss_sc, c, 0);
    613         fp = qv_fbp(ss->ss_sc, row, col, 0);
    614         rvid = (attr & WSATTR_REVERSE) ? 0xff : 0x00;
    615         for (i = 0; i < QV_CHEIGHT; i++) {
    616                 *fp = *gp++ ^ rvid;
    617                 fp += QV_COLS;
    618         }
    619 
    620 	if (attr & WSATTR_UNDERLINE)
    621 	        *qv_fbp(ss->ss_sc, row, col, 14)
    622 	            ^= *qv_fbp(ss->ss_sc, row, col, 14);
    623 }
    624 
    625 /*
    626  * emulop copy columns - copies columns inside a row
    627  */
    628 static void
    629 qv_copycols(void *id, int row, int srccol, int dstcol, int ncols)
    630 {
    631 	struct qv_screen * const ss = id;
    632 	int i;
    633 
    634 	memcpy(&ss->ss_image[row][dstcol], &ss->ss_image[row][srccol], ncols);
    635 	memcpy(&ss->ss_attr[row][dstcol], &ss->ss_attr[row][srccol], ncols);
    636 	if (ss != ss->ss_sc->sc_curscr)
    637 		return;
    638 	for (i = 0; i < QV_CHEIGHT; i++)
    639 	        memcpy(qv_fbp(ss->ss_sc, row, dstcol, i),
    640 	            qv_fbp(ss->ss_sc, row, srccol, i), ncols);
    641 }
    642 
    643 /*
    644  * emulop erase columns - erases a bunch of chars inside one row
    645  */
    646 static void
    647 qv_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
    648 {
    649 	struct qv_screen * const ss = id;
    650 	int i;
    651 
    652 	memset(&ss->ss_image[row][startcol], 0, ncols);
    653 	memset(&ss->ss_attr[row][startcol], 0, ncols);
    654 	if (ss != ss->ss_sc->sc_curscr)
    655 		return;
    656 	for (i = 0; i < QV_CHEIGHT; i++)
    657 	        memset(qv_fbp(ss->ss_sc, row, startcol, i), 0, ncols);
    658 }
    659 
    660 /*
    661  * overlap check
    662  * return 0 if no overlap
    663  *        -1 if overlap and dst is less than src (move up)
    664  * 	  +1 if overlap and src is less than dst (move down)
    665  */
    666 static inline int
    667 qv_rows_overlap(int srcrow, int dstrow, int nrows)
    668 {
    669 	if (dstrow < srcrow) {
    670 		if (dstrow + nrows <= srcrow)
    671 			return 0;
    672 		else
    673 			return -1;
    674 	}
    675 	else {
    676 		if (srcrow + nrows <= dstrow)
    677 			return 0;
    678 		else
    679 			return 1;
    680 	}
    681 }
    682 
    683 /*
    684  * emulop copyrows - copy entire rows
    685  */
    686 static void
    687 qv_copyrows(void *id, int srcrow, int dstrow, int nrows)
    688 {
    689 	struct qv_screen * const ss = id;
    690 	int ol;
    691 	int n;
    692 	int line;
    693 	int tmp;
    694 	uint16_t *sp;
    695 	uint16_t *dp;
    696 
    697 	memcpy(&ss->ss_image[dstrow][0], &ss->ss_image[srcrow][0],
    698 	    nrows * QV_COLS);
    699 	memcpy(&ss->ss_attr[dstrow][0], &ss->ss_attr[srcrow][0],
    700 	    nrows * QV_COLS);
    701 	if (ss != ss->ss_sc->sc_curscr)
    702 		return;
    703 
    704 	ol = qv_rows_overlap(srcrow, dstrow, nrows);
    705 	if (ol == 0)
    706 		for (n = 0; n < nrows; n++)
    707 			bcopy(qv_fbp(ss->ss_sc, srcrow + n, 0, 0),
    708 	    		    qv_fbp(ss->ss_sc, dstrow + n, 0, 0), QV_NEXTROW);
    709 	else if (ol < 0) {
    710 	 	for (n = 0; n < nrows; n++) {
    711 			dp = &ss->ss_sc->sc_scanmap[(dstrow + n)*QV_CHEIGHT];
    712 			sp = &ss->ss_sc->sc_scanmap[(srcrow + n)*QV_CHEIGHT];
    713 			for (line = 0; line < QV_CHEIGHT; line++) {
    714 				tmp = *dp;
    715 				*dp = *sp;
    716 				*sp = tmp;
    717 				dp++;
    718 				sp++;
    719 			}
    720 		}
    721 		qv_copyrows(id, dstrow + nrows - srcrow + dstrow,
    722 		    dstrow + nrows, srcrow - dstrow);
    723 	}
    724 	else {
    725 	 	for (n = nrows - 1; n >= 0; n--) {
    726 			dp = &ss->ss_sc->sc_scanmap[(dstrow + n)*QV_CHEIGHT];
    727 			sp = &ss->ss_sc->sc_scanmap[(srcrow + n)*QV_CHEIGHT];
    728 			for (line = 0; line < QV_CHEIGHT; line++) {
    729 				tmp = *dp;
    730 				*dp = *sp;
    731 				*sp = tmp;
    732 				dp++;
    733 				sp++;
    734 			}
    735 		}
    736 		qv_copyrows(id, srcrow, dstrow, dstrow - srcrow);
    737 	}
    738 }
    739 
    740 /*
    741  * emulop eraserows - erase a number of entire rows
    742  */
    743 static void
    744 qv_eraserows(void *id, int startrow, int nrows, long fillattr)
    745 {
    746 	struct qv_screen * const ss = id;
    747 	int row;
    748 
    749 	memset(&ss->ss_image[startrow][0], 0, nrows * QV_COLS);
    750 	memset(&ss->ss_attr[startrow][0], 0, nrows * QV_COLS);
    751 	if (ss != ss->ss_sc->sc_curscr)
    752 		return;
    753 
    754 	for (row = startrow; row < startrow + nrows; row++) {
    755 	        memset(qv_fbp(ss->ss_sc, row, 0, 0), 0, QV_NEXTROW);
    756 	}
    757 }
    758 
    759 /*
    760  * emulop allocattr
    761  */
    762 static int
    763 qv_allocattr(void *id, int fg, int bg, int flags, long *attrp)
    764 {
    765 	*attrp = flags;
    766 	return 0;
    767 }
    768 
    769 /*
    770  * emulop setcursor
    771  */
    772 static void
    773 qv_setcursor(struct qv_softc *sc, struct wsdisplay_cursor *v)
    774 {
    775 	uint16_t red, green, blue;
    776 	uint32_t curfg[16], curmask[16];
    777 	uint16_t *curp;
    778 	int i;
    779 
    780 	/* Enable cursor */
    781 	if (v->which & WSDISPLAY_CURSOR_DOCUR) {
    782 	        sc->sc_curon = (v->enable) ? CUR_ON : CUR_OFF;
    783 	        qv_crtc_wr(sc, CUR_START, sc->sc_curon | (sc->sc_cury & 0x0f));
    784 	}
    785 	if (v->which & WSDISPLAY_CURSOR_DOHOT) {
    786 		sc->sc_curhotX = v->hot.x;
    787 		sc->sc_curhotY = v->hot.y;
    788 	}
    789 	if (v->which & WSDISPLAY_CURSOR_DOCMAP) {
    790 		/* First background */
    791 		if (copyin(v->cmap.red, &red, sizeof(red)) == 0 &&
    792 		    copyin(v->cmap.green, &green, sizeof(green)) == 0 &&
    793 		    copyin(v->cmap.blue, &blue, sizeof(blue)) == 0) {
    794 			bgmask = (((30L * red + 59L * green + 11L * blue) >> 8)
    795 			    >= (((1<<8)-1)*50)) ? ~0 : 0;
    796 		}
    797 		if (copyin(v->cmap.red + 2, &red, sizeof(red)) == 0 &&
    798 		    copyin(v->cmap.green + 2, &green, sizeof(green)) == 0 &&
    799 		    copyin(v->cmap.blue + 2, &blue, sizeof(blue)) == 0) {
    800 			fgmask = (((30L * red + 59L * green + 11L * blue) >> 8)
    801 			    >= (((1<<8)-1)*50)) ? ~0 : 0;
    802 		}
    803 	}
    804 	if (v->which & WSDISPLAY_CURSOR_DOSHAPE) {
    805 		copyin(v->image, curfg, sizeof(curfg));
    806 		copyin(v->mask, curmask, sizeof(curmask));      /* not used */
    807 	        curp = (uint16_t *) &(sc->sc_fb)[QV_CURSRAM];
    808 		for (i = 0; i < sizeof(curfg)/sizeof(curfg[0]); i++) {
    809 		        curp[i] = (uint16_t)curfg[i];
    810 		}
    811 	}
    812 }
    813 
    814 /*
    815  * emulop ioctl
    816  */
    817 int
    818 qv_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    819 {
    820 	struct wsdisplay_fbinfo *fb = (void *)data;
    821 	//static uint16_t curc;
    822         struct qv_softc *sc = device_private(v);
    823 
    824 	switch (cmd) {
    825 	case WSDISPLAYIO_GTYPE:
    826 		*(u_int *)data = WSDISPLAY_TYPE_VAX_MONO;
    827 		break;
    828 
    829 	case WSDISPLAYIO_GINFO:
    830 		fb->height = QV_YWIDTH;
    831 		fb->width = QV_XWIDTH;
    832 		fb->depth = 1;
    833 		fb->cmsize = 2;
    834 		break;
    835 
    836 	case WSDISPLAYIO_SVIDEO:
    837 		if (*(u_int *)data == WSDISPLAYIO_VIDEO_ON) {
    838                         /* enable video output */
    839   		        qv_reg_wr(sc, QV_CSR,
    840   		            qv_reg_rd(sc, QV_CSR) | (1 << 2));
    841 		} else {
    842                         /* disable video output */
    843                         qv_reg_wr(sc, QV_CSR,
    844                             qv_reg_rd(sc, QV_CSR) & ~(1 << 2));
    845 		}
    846 		break;
    847 
    848 	case WSDISPLAYIO_GVIDEO:
    849 		*(u_int *)data = (qv_reg_rd(sc, QV_CSR) & (1 << 2))
    850 		    ? WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    851 		break;
    852 
    853 	case WSDISPLAYIO_SCURSOR:
    854 		qv_setcursor(sc, (struct wsdisplay_cursor *)data);
    855 		break;
    856 
    857 	case WSDISPLAYIO_SCURPOS:
    858 	        sc->sc_curx = ((struct wsdisplay_curpos *)data)->x;
    859 		sc->sc_cury = ((struct wsdisplay_curpos *)data)->y;
    860                 qv_crtc_wr(sc, CUR_START, CUR_OFF | (sc->sc_cury & 0x0f));
    861                 qv_crtc_wr(sc, CUR_HI, sc->sc_cury >> 4);
    862                 qv_reg_wr(sc, QV_CUR_X, sc->sc_curx);
    863                 qv_crtc_wr(sc, CUR_START,
    864                     sc->sc_curon | (sc->sc_cury & 0x0f));
    865 		break;
    866 
    867 	case WSDISPLAYIO_GCURPOS:
    868 		((struct wsdisplay_curpos *)data)->x = sc->sc_curx;
    869 		((struct wsdisplay_curpos *)data)->y = sc->sc_cury;
    870 		break;
    871 
    872 	case WSDISPLAYIO_GCURMAX:
    873 		((struct wsdisplay_curpos *)data)->x = 16;
    874 		((struct wsdisplay_curpos *)data)->y = 16;
    875 		break;
    876 
    877 	default:
    878 		return EPASSTHROUGH;
    879 	}
    880 	return 0;
    881 }
    882 
    883 /*
    884  * emulop mmap
    885  */
    886 static paddr_t
    887 qv_mmap(void *v, void *vs, off_t offset, int prot)
    888 {
    889         struct qv_softc *sc = device_private(v);
    890 
    891 	if (offset >= QVSIZE || offset < 0)
    892 		return -1;
    893 	return (sc->sc_fbphys) >> PGSHIFT;
    894 }
    895 
    896 /*
    897  * emulop allocate screen
    898  */
    899 int
    900 qv_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    901     int *curxp, int *curyp, long *defattrp)
    902 {
    903         struct qv_softc *sc = device_private(v);
    904         struct qv_screen *ss;
    905 
    906 	ss = kmem_zalloc(sizeof(struct qv_screen), KM_SLEEP);
    907 	ss->ss_sc = sc;
    908 	ss->ss_type = type;
    909 	*cookiep = ss;
    910 	*curxp = *curyp = *defattrp = 0;
    911         printf("qv_alloc_screen: \"%s\" %p\n", type->name, ss);
    912 	return 0;
    913 }
    914 
    915 /*
    916  * emulop free screen
    917  */
    918 void
    919 qv_free_screen(void *v, void *cookie)
    920 {
    921         printf("qv_free_screen: %p\n", cookie);
    922         kmem_free(cookie, sizeof(struct qv_screen));
    923 }
    924 
    925 /*
    926  * emulop show screen
    927  */
    928 int
    929 qv_show_screen(void *v, void *cookie, int waitok,
    930     void (*cb)(void *, int, int), void *cbarg)
    931 {
    932 	struct qv_screen *ss = cookie;
    933 	const struct _wsscreen_descr *descr;
    934 	int row, col, line;
    935         printf("qv_show_screen: %p\n", cookie);
    936 
    937 	if (ss == ss->ss_sc->sc_curscr)
    938 		return (0);
    939 
    940         descr = (const struct _wsscreen_descr *)(ss->ss_type);
    941         qv_setcrtc(ss->ss_sc, descr->qv_crtc_param);
    942 	for (row = 0; row < QV_ROWS; row++)
    943 		for (line = 0; line < QV_CHEIGHT; line++) {
    944 			for (col = 0; col < QV_COLS; col++) {
    945 				u_char s, c = ss->ss_image[row][col];
    946 
    947 				if (c < 32)
    948 					c = 32;
    949 				s = *qv_font(ss->ss_sc, c, line);
    950 				if (ss->ss_attr[row][col] & WSATTR_REVERSE)
    951 					s ^= 255;
    952 				*qv_fbp(ss->ss_sc, row, col, line) = s;
    953 
    954 				if (ss->ss_attr[row][col] & WSATTR_UNDERLINE)
    955 					*qv_fbp(ss->ss_sc, row, col, line)
    956 					    = 255;
    957 			}
    958 		}
    959         cursor = qv_fbp(ss->ss_sc, ss->ss_cury, ss->ss_curx, 14);
    960 	ss->ss_sc->sc_curscr = ss;
    961 	return (0);
    962 }
    963 
    964 #if 0
    965 void
    966 qv_reset_establish(void (*reset)(device_t), device_t dev)
    967 {
    968         uba_reset_establish(reset, device_parent(dev));
    969 }
    970 #endif
    971 cons_decl(qv);
    972 
    973 void
    974 qvcninit(struct consdev *cndev)
    975 {
    976 	int fcookie;
    977 	struct wsdisplay_font *console_font;
    978 	extern void lkccninit(struct consdev *);
    979 	extern int lkccngetc(dev_t);
    980 	extern int dz_vsbus_lk201_cnattach(int);
    981 
    982         //printf("qvcninit: \n");
    983 	/* Clear screen */
    984 	//memset(qv_addr, 0, 128*864);
    985 
    986 	callout_init(&qv_cursor_ch, 0);
    987 	//curscr = &qv_conscreen;
    988 	wsdisplay_cnattach(&qv_stdscreen[0].qv_stdscreen,
    989 	    &qv_conscreen, 0, 0, 0);
    990 	cn_tab->cn_pri = CN_INTERNAL;
    991 	wsfont_init();
    992 	if ((fcookie = wsfont_find(NULL, 8, 15, 0, WSDISPLAY_FONTORDER_R2L,
    993 	    WSDISPLAY_FONTORDER_L2R, WSFONT_FIND_BITMAP)) < 0)
    994 	{
    995 		printf("qv: could not find 8x15 font\n");
    996 		return;
    997 	}
    998 	if (wsfont_lock(fcookie, &console_font) != 0) {
    999 		printf("qv: could not lock 8x15 font\n");
   1000 		return;
   1001 	}
   1002 	//qf = console_font->data;
   1003 
   1004 #if NQVKBD > 0 && 0
   1005 	qvkbd_cnattach(0); /* Connect keyboard and screen together */
   1006 #endif
   1007 }
   1008 
   1009 /*
   1010  * Called very early to setup the glass tty as console.
   1011  * Because it's called before the VM system is inited, virtual memory
   1012  * for the framebuffer can be stolen directly without disturbing anything.
   1013  */
   1014 void
   1015 qvcnprobe(struct consdev *cndev)
   1016 {
   1017         printf("qvcnprobe: \n");
   1018 #if 0
   1019 	extern vaddr_t virtual_avail;
   1020 	extern const struct cdevsw wsdisplay_cdevsw;
   1021 
   1022 	switch (vax_boardtype) {
   1023 	case VAX_BTYP_410:
   1024 	case VAX_BTYP_420:
   1025 	case VAX_BTYP_43:
   1026 		if ((vax_confdata & KA420_CFG_L3CON) ||
   1027 		    (vax_confdata & KA420_CFG_MULTU))
   1028 			break; /* doesn't use graphics console */
   1029 		qv_addr = (void *)virtual_avail;
   1030 		virtual_avail += QVSIZE;
   1031 		ioaccess((vaddr_t)qv_addr, QVADDR, (QVSIZE/VAX_NBPG));
   1032 		cndev->cn_pri = CN_INTERNAL;
   1033 		cndev->cn_dev = makedev(cdevsw_lookup_major(&wsdisplay_cdevsw),
   1034 					0);
   1035 		break;
   1036 	default:
   1037 		break;
   1038 	}
   1039 #endif
   1040 }
   1041