smg.c revision 1.27.4.4 1 /* $NetBSD: smg.c,v 1.27.4.4 2002/10/10 18:37:26 jdolecek Exp $ */
2 /*
3 * Copyright (c) 1998 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed at Ludd, University of
17 * Lule}, Sweden and its contributors.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/callout.h>
37 #include <sys/time.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/kernel.h>
41
42 #include <machine/vsbus.h>
43 #include <machine/sid.h>
44 #include <machine/cpu.h>
45 #include <machine/ka420.h>
46
47 #include <dev/cons.h>
48
49 #include <dev/dec/dzreg.h>
50 #include <dev/dec/dzvar.h>
51 #include <dev/dec/dzkbdvar.h>
52
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wsconsio.h>
55 #include <dev/wscons/wscons_callbacks.h>
56
57 #include "dzkbd.h"
58 #include "opt_wsfont.h"
59
60 /* Safety guard */
61 #ifndef FONT_QVSS8x15
62 #include <dev/wsfont/qvss8x15.h>
63 #endif
64
65 /* Screen hardware defs */
66 #define SM_COLS 128 /* char width of screen */
67 #define SM_ROWS 57 /* rows of char on screen */
68 #define SM_CHEIGHT 15 /* lines a char consists of */
69 #define SM_NEXTROW (SM_COLS * SM_CHEIGHT)
70 #define SM_YWIDTH 864
71 #define SM_XWIDTH 1024
72
73 /* Cursor register definitions */
74 #define CUR_CMD 0
75 #define CUR_XPOS 4
76 #define CUR_YPOS 8
77 #define CUR_XMIN_1 12
78 #define CUR_XMAX_1 16
79 #define CUR_YMIN_1 20
80 #define CUR_YMAX_1 24
81 #define CUR_XMIN_2 28
82 #define CUR_XMAX_2 32
83 #define CUR_YMIN_2 36
84 #define CUR_YMAX_2 40
85 #define CUR_LOAD 44
86
87 #define CUR_CMD_TEST 0x8000
88 #define CUR_CMD_HSHI 0x4000
89 #define CUR_CMD_VBHI 0x2000
90 #define CUR_CMD_LODSA 0x1000
91 #define CUR_CMD_FORG2 0x0800
92 #define CUR_CMD_ENRG2 0x0400
93 #define CUR_CMD_FORG1 0x0200
94 #define CUR_CMD_ENRG1 0x0100
95 #define CUR_CMD_XHWID 0x0080
96 #define CUR_CMD_XHCL1 0x0040
97 #define CUR_CMD_XHCLP 0x0020
98 #define CUR_CMD_XHAIR 0x0010
99 #define CUR_CMD_FOPB 0x0008
100 #define CUR_CMD_ENPB 0x0004
101 #define CUR_CMD_FOPA 0x0002
102 #define CUR_CMD_ENPA 0x0001
103
104 #define CUR_XBIAS 216 /* Add to cursor position */
105 #define CUR_YBIAS 33
106
107 #define WRITECUR(addr, val) *(volatile short *)(curaddr + (addr)) = (val)
108 static caddr_t curaddr;
109 static short curcmd, curx, cury, hotX, hotY;
110 static int bgmask, fgmask;
111
112 static int smg_match(struct device *, struct cfdata *, void *);
113 static void smg_attach(struct device *, struct device *, void *);
114
115 struct smg_softc {
116 struct device ss_dev;
117 };
118
119 CFATTACH_DECL(smg, sizeof(struct smg_softc),
120 smg_match, smg_attach, NULL, NULL);
121
122 static void smg_cursor(void *, int, int, int);
123 static int smg_mapchar(void *, int, unsigned int *);
124 static void smg_putchar(void *, int, int, u_int, long);
125 static void smg_copycols(void *, int, int, int,int);
126 static void smg_erasecols(void *, int, int, int, long);
127 static void smg_copyrows(void *, int, int, int);
128 static void smg_eraserows(void *, int, int, long);
129 static int smg_allocattr(void *, int, int, int, long *);
130
131 const struct wsdisplay_emulops smg_emulops = {
132 smg_cursor,
133 smg_mapchar,
134 smg_putchar,
135 smg_copycols,
136 smg_erasecols,
137 smg_copyrows,
138 smg_eraserows,
139 smg_allocattr
140 };
141
142 const struct wsscreen_descr smg_stdscreen = {
143 "128x57", SM_COLS, SM_ROWS,
144 &smg_emulops,
145 8, SM_CHEIGHT,
146 WSSCREEN_UNDERLINE|WSSCREEN_REVERSE,
147 };
148
149 const struct wsscreen_descr *_smg_scrlist[] = {
150 &smg_stdscreen,
151 };
152
153 const struct wsscreen_list smg_screenlist = {
154 sizeof(_smg_scrlist) / sizeof(struct wsscreen_descr *),
155 _smg_scrlist,
156 };
157
158 static caddr_t sm_addr;
159
160 extern struct wsdisplay_font qvss8x15;
161 static u_char *qf;
162
163 #define QCHAR(c) (c < 32 ? 32 : (c > 127 ? c - 66 : c - 32))
164 #define QFONT(c,line) qf[QCHAR(c) * 15 + line]
165 #define SM_ADDR(row, col, line) \
166 sm_addr[col + (row * SM_CHEIGHT * SM_COLS) + line * SM_COLS]
167
168
169 static int smg_ioctl(void *, u_long, caddr_t, int, struct proc *);
170 static paddr_t smg_mmap(void *, off_t, int);
171 static int smg_alloc_screen(void *, const struct wsscreen_descr *,
172 void **, int *, int *, long *);
173 static void smg_free_screen(void *, void *);
174 static int smg_show_screen(void *, void *, int,
175 void (*) (void *, int, int), void *);
176 static void smg_crsr_blink(void *);
177
178 const struct wsdisplay_accessops smg_accessops = {
179 smg_ioctl,
180 smg_mmap,
181 smg_alloc_screen,
182 smg_free_screen,
183 smg_show_screen,
184 0 /* load_font */
185 };
186
187 struct smg_screen {
188 int ss_curx;
189 int ss_cury;
190 u_char ss_image[SM_ROWS][SM_COLS]; /* Image of current screen */
191 u_char ss_attr[SM_ROWS][SM_COLS]; /* Reversed etc... */
192 };
193
194 static struct smg_screen smg_conscreen;
195 static struct smg_screen *curscr;
196
197 static struct callout smg_cursor_ch = CALLOUT_INITIALIZER;
198
199 int
200 smg_match(struct device *parent, struct cfdata *match, void *aux)
201 {
202 struct vsbus_attach_args *va = aux;
203 volatile short *curcmd;
204 volatile short *cfgtst;
205 short tmp, tmp2;
206
207 if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_53)
208 return 0;
209
210 curcmd = (short *)va->va_addr;
211 cfgtst = (short *)vax_map_physmem(VS_CFGTST, 1);
212 /*
213 * Try to find the cursor chip by testing the flip-flop.
214 * If nonexistent, no glass tty.
215 */
216 curcmd[0] = CUR_CMD_HSHI|CUR_CMD_FOPB;
217 DELAY(300000);
218 tmp = cfgtst[0];
219 curcmd[0] = CUR_CMD_TEST|CUR_CMD_HSHI;
220 DELAY(300000);
221 tmp2 = cfgtst[0];
222 vax_unmap_physmem((vaddr_t)cfgtst, 1);
223
224 if (tmp2 != tmp)
225 return 20; /* Using periodic interrupt */
226 else
227 return 0;
228 }
229
230 void
231 smg_attach(struct device *parent, struct device *self, void *aux)
232 {
233 struct wsemuldisplaydev_attach_args aa;
234
235 printf("\n");
236 sm_addr = (caddr_t)vax_map_physmem(SMADDR, (SMSIZE/VAX_NBPG));
237 curaddr = (caddr_t)vax_map_physmem(KA420_CUR_BASE, 1);
238 if (sm_addr == 0) {
239 printf("%s: Couldn't alloc graphics memory.\n", self->dv_xname);
240 return;
241 }
242 curscr = &smg_conscreen;
243 aa.console = (vax_confdata & (KA420_CFG_L3CON|KA420_CFG_MULTU)) == 0;
244
245 aa.scrdata = &smg_screenlist;
246 aa.accessops = &smg_accessops;
247 callout_reset(&smg_cursor_ch, hz / 2, smg_crsr_blink, NULL);
248 curcmd = CUR_CMD_HSHI;
249 WRITECUR(CUR_CMD, curcmd);
250 qf = qvss8x15.data;
251
252 config_found(self, &aa, wsemuldisplaydevprint);
253 }
254
255 static u_char *cursor;
256 static int cur_on;
257
258 static void
259 smg_crsr_blink(void *arg)
260 {
261 if (cur_on)
262 *cursor ^= 255;
263 callout_reset(&smg_cursor_ch, hz / 2, smg_crsr_blink, NULL);
264 }
265
266 void
267 smg_cursor(void *id, int on, int row, int col)
268 {
269 struct smg_screen *ss = id;
270
271 if (ss == curscr) {
272 SM_ADDR(ss->ss_cury, ss->ss_curx, 14) =
273 QFONT(ss->ss_image[ss->ss_cury][ss->ss_curx], 14);
274 cursor = &SM_ADDR(row, col, 14);
275 if ((cur_on = on))
276 *cursor ^= 255;
277 }
278 ss->ss_curx = col;
279 ss->ss_cury = row;
280 }
281
282 int
283 smg_mapchar(void *id, int uni, unsigned int *index)
284 {
285 if (uni < 256) {
286 *index = uni;
287 return (5);
288 }
289 *index = ' ';
290 return (0);
291 }
292
293 static void
294 smg_putchar(void *id, int row, int col, u_int c, long attr)
295 {
296 struct smg_screen *ss = id;
297 int i;
298
299 c &= 0xff;
300
301 ss->ss_image[row][col] = c;
302 ss->ss_attr[row][col] = attr;
303 if (ss != curscr)
304 return;
305 for (i = 0; i < 15; i++) {
306 unsigned char ch = QFONT(c, i);
307
308 SM_ADDR(row, col, i) = (attr & WSATTR_REVERSE ? ~ch : ch);
309
310 }
311 if (attr & WSATTR_UNDERLINE)
312 SM_ADDR(row, col, 14) ^= SM_ADDR(row, col, 14);
313 }
314
315 /*
316 * copies columns inside a row.
317 */
318 static void
319 smg_copycols(void *id, int row, int srccol, int dstcol, int ncols)
320 {
321 struct smg_screen *ss = id;
322 int i;
323
324 bcopy(&ss->ss_image[row][srccol], &ss->ss_image[row][dstcol], ncols);
325 bcopy(&ss->ss_attr[row][srccol], &ss->ss_attr[row][dstcol], ncols);
326 if (ss != curscr)
327 return;
328 for (i = 0; i < SM_CHEIGHT; i++)
329 bcopy(&SM_ADDR(row,srccol, i), &SM_ADDR(row, dstcol, i),ncols);
330 }
331
332 /*
333 * Erases a bunch of chars inside one row.
334 */
335 static void
336 smg_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
337 {
338 struct smg_screen *ss = id;
339 int i;
340
341 bzero(&ss->ss_image[row][startcol], ncols);
342 bzero(&ss->ss_attr[row][startcol], ncols);
343 if (ss != curscr)
344 return;
345 for (i = 0; i < SM_CHEIGHT; i++)
346 bzero(&SM_ADDR(row, startcol, i), ncols);
347 }
348
349 static void
350 smg_copyrows(void *id, int srcrow, int dstrow, int nrows)
351 {
352 struct smg_screen *ss = id;
353 int frows;
354
355 bcopy(&ss->ss_image[srcrow][0], &ss->ss_image[dstrow][0],
356 nrows * SM_COLS);
357 bcopy(&ss->ss_attr[srcrow][0], &ss->ss_attr[dstrow][0],
358 nrows * SM_COLS);
359 if (ss != curscr)
360 return;
361 if (nrows > 25) {
362 frows = nrows >> 1;
363 if (srcrow > dstrow) {
364 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
365 &sm_addr[(dstrow * SM_NEXTROW)],
366 frows * SM_NEXTROW);
367 bcopy(&sm_addr[((srcrow + frows) * SM_NEXTROW)],
368 &sm_addr[((dstrow + frows) * SM_NEXTROW)],
369 (nrows - frows) * SM_NEXTROW);
370 } else {
371 bcopy(&sm_addr[((srcrow + frows) * SM_NEXTROW)],
372 &sm_addr[((dstrow + frows) * SM_NEXTROW)],
373 (nrows - frows) * SM_NEXTROW);
374 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
375 &sm_addr[(dstrow * SM_NEXTROW)],
376 frows * SM_NEXTROW);
377 }
378 } else
379 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
380 &sm_addr[(dstrow * SM_NEXTROW)], nrows * SM_NEXTROW);
381 }
382
383 static void
384 smg_eraserows(void *id, int startrow, int nrows, long fillattr)
385 {
386 struct smg_screen *ss = id;
387 int frows;
388
389 bzero(&ss->ss_image[startrow][0], nrows * SM_COLS);
390 bzero(&ss->ss_attr[startrow][0], nrows * SM_COLS);
391 if (ss != curscr)
392 return;
393 if (nrows > 25) {
394 frows = nrows >> 1;
395 bzero(&sm_addr[(startrow * SM_NEXTROW)], frows * SM_NEXTROW);
396 bzero(&sm_addr[((startrow + frows) * SM_NEXTROW)],
397 (nrows - frows) * SM_NEXTROW);
398 } else
399 bzero(&sm_addr[(startrow * SM_NEXTROW)], nrows * SM_NEXTROW);
400 }
401
402 static int
403 smg_allocattr(void *id, int fg, int bg, int flags, long *attrp)
404 {
405 *attrp = flags;
406 return 0;
407 }
408
409 static void
410 setcursor(struct wsdisplay_cursor *v)
411 {
412 u_short red, green, blue, curfg[16], curmask[16];
413 int i;
414
415 /* Enable cursor */
416 if (v->which & WSDISPLAY_CURSOR_DOCUR) {
417 if (v->enable)
418 curcmd |= CUR_CMD_ENPB|CUR_CMD_ENPA;
419 else
420 curcmd &= ~(CUR_CMD_ENPB|CUR_CMD_ENPA);
421 WRITECUR(CUR_CMD, curcmd);
422 }
423 if (v->which & WSDISPLAY_CURSOR_DOHOT) {
424 hotX = v->hot.x;
425 hotY = v->hot.y;
426 }
427 if (v->which & WSDISPLAY_CURSOR_DOCMAP) {
428 /* First background */
429 red = fusword(v->cmap.red);
430 green = fusword(v->cmap.green);
431 blue = fusword(v->cmap.blue);
432 bgmask = (((30L * red + 59L * green + 11L * blue) >> 8) >=
433 (((1<<8)-1)*50)) ? ~0 : 0;
434 red = fusword(v->cmap.red+2);
435 green = fusword(v->cmap.green+2);
436 blue = fusword(v->cmap.blue+2);
437 fgmask = (((30L * red + 59L * green + 11L * blue) >> 8) >=
438 (((1<<8)-1)*50)) ? ~0 : 0;
439 }
440 if (v->which & WSDISPLAY_CURSOR_DOSHAPE) {
441 WRITECUR(CUR_CMD, curcmd | CUR_CMD_LODSA);
442 copyin(v->image, curfg, sizeof(curfg));
443 copyin(v->mask, curmask, sizeof(curmask));
444 for (i = 0; i < sizeof(curfg)/2; i++) {
445 WRITECUR(CUR_LOAD, (curfg[i] & fgmask) |
446 ((curmask[i] & ~curfg[i]) & bgmask));
447 }
448 for (i = 0; i < sizeof(curmask)/2; i++) {
449 WRITECUR(CUR_LOAD, curmask[i]);
450 }
451 WRITECUR(CUR_CMD, curcmd);
452 }
453 }
454
455 int
456 smg_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
457 {
458 struct wsdisplay_fbinfo *fb = (void *)data;
459 static short curc;
460
461 switch (cmd) {
462 case WSDISPLAYIO_GTYPE:
463 *(u_int *)data = WSDISPLAY_TYPE_VAX_MONO;
464 break;
465
466 case WSDISPLAYIO_GINFO:
467 fb->height = SM_YWIDTH;
468 fb->width = SM_XWIDTH;
469 fb->depth = 1;
470 fb->cmsize = 2;
471 break;
472
473 case WSDISPLAYIO_SVIDEO:
474 if (*(u_int *)data == WSDISPLAYIO_VIDEO_ON) {
475 curcmd = curc;
476 } else {
477 curc = curcmd;
478 curcmd &= ~(CUR_CMD_FOPA|CUR_CMD_ENPA);
479 curcmd |= CUR_CMD_FOPB;
480 }
481 WRITECUR(CUR_CMD, curcmd);
482 break;
483
484 case WSDISPLAYIO_GVIDEO:
485 *(u_int *)data = (curcmd & CUR_CMD_FOPB ?
486 WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON);
487 break;
488
489 case WSDISPLAYIO_SCURSOR:
490 setcursor((struct wsdisplay_cursor *)data);
491 break;
492
493 case WSDISPLAYIO_SCURPOS:
494 curx = ((struct wsdisplay_curpos *)data)->x;
495 cury = ((struct wsdisplay_curpos *)data)->y;
496 WRITECUR(CUR_XPOS, curx + CUR_XBIAS);
497 WRITECUR(CUR_YPOS, cury + CUR_YBIAS);
498 break;
499
500 case WSDISPLAYIO_GCURPOS:
501 ((struct wsdisplay_curpos *)data)->x = curx;
502 ((struct wsdisplay_curpos *)data)->y = cury;
503 break;
504
505 default:
506 return EPASSTHROUGH;
507 }
508 return 0;
509 }
510
511 static paddr_t
512 smg_mmap(void *v, off_t offset, int prot)
513 {
514 if (offset >= SMSIZE || offset < 0)
515 return -1;
516 return (SMADDR + offset) >> PGSHIFT;
517 }
518
519 int
520 smg_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
521 int *curxp, int *curyp, long *defattrp)
522 {
523 *cookiep = malloc(sizeof(struct smg_screen), M_DEVBUF, M_WAITOK);
524 bzero(*cookiep, sizeof(struct smg_screen));
525 *curxp = *curyp = *defattrp = 0;
526 return 0;
527 }
528
529 void
530 smg_free_screen(void *v, void *cookie)
531 {
532 }
533
534 int
535 smg_show_screen(void *v, void *cookie, int waitok,
536 void (*cb)(void *, int, int), void *cbarg)
537 {
538 struct smg_screen *ss = cookie;
539 int row, col, line;
540
541 if (ss == curscr)
542 return (0);
543
544 for (row = 0; row < SM_ROWS; row++)
545 for (line = 0; line < SM_CHEIGHT; line++) {
546 for (col = 0; col < SM_COLS; col++) {
547 u_char s, c = ss->ss_image[row][col];
548
549 if (c < 32)
550 c = 32;
551 s = QFONT(c, line);
552 if (ss->ss_attr[row][col] & WSATTR_REVERSE)
553 s ^= 255;
554 SM_ADDR(row, col, line) = s;
555 }
556 if (ss->ss_attr[row][col] & WSATTR_UNDERLINE)
557 SM_ADDR(row, col, line) = 255;
558 }
559 cursor = &sm_addr[(ss->ss_cury * SM_CHEIGHT * SM_COLS) + ss->ss_curx +
560 ((SM_CHEIGHT - 1) * SM_COLS)];
561 curscr = ss;
562 return (0);
563 }
564
565 cons_decl(smg);
566
567 void
568 smgcninit(cndev)
569 struct consdev *cndev;
570 {
571 extern void lkccninit(struct consdev *);
572 extern int lkccngetc(dev_t);
573 extern int dz_vsbus_lk201_cnattach __P((int));
574 /* Clear screen */
575 memset(sm_addr, 0, 128*864);
576
577 curscr = &smg_conscreen;
578 wsdisplay_cnattach(&smg_stdscreen, &smg_conscreen, 0, 0, 0);
579 cn_tab->cn_pri = CN_INTERNAL;
580 qf = qvss8x15.data;
581
582 #if NDZKBD > 0
583 dzkbd_cnattach(0); /* Connect keyboard and screen together */
584 #endif
585 }
586
587 /*
588 * Called very early to setup the glass tty as console.
589 * Because it's called before the VM system is inited, virtual memory
590 * for the framebuffer can be stolen directly without disturbing anything.
591 */
592 void
593 smgcnprobe(cndev)
594 struct consdev *cndev;
595 {
596 extern vaddr_t virtual_avail;
597 extern const struct cdevsw wsdisplay_cdevsw;
598
599 switch (vax_boardtype) {
600 case VAX_BTYP_410:
601 case VAX_BTYP_420:
602 case VAX_BTYP_43:
603 if ((vax_confdata & KA420_CFG_L3CON) ||
604 (vax_confdata & KA420_CFG_MULTU))
605 break; /* doesn't use graphics console */
606 sm_addr = (caddr_t)virtual_avail;
607 virtual_avail += SMSIZE;
608 ioaccess((vaddr_t)sm_addr, SMADDR, (SMSIZE/VAX_NBPG));
609 cndev->cn_pri = CN_INTERNAL;
610 cndev->cn_dev = makedev(cdevsw_lookup_major(&wsdisplay_cdevsw),
611 0);
612 break;
613
614 default:
615 break;
616 }
617 }
618