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