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