smg.c revision 1.20 1 /* $NetBSD: smg.c,v 1.20 1999/12/12 14:40:55 ragge 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/time.h>
37 #include <sys/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40
41 #include <dev/cons.h>
42
43 #include <dev/wscons/wsdisplayvar.h>
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wscons/wscons_callbacks.h>
46
47 #include <machine/vsbus.h>
48 #include <machine/sid.h>
49 #include <machine/cpu.h>
50 #include <machine/ka420.h>
51
52 #include "lkc.h"
53
54 #define SM_COLS 128 /* char width of screen */
55 #define SM_ROWS 57 /* rows of char on screen */
56 #define SM_CHEIGHT 15 /* lines a char consists of */
57 #define SM_NEXTROW (SM_COLS * SM_CHEIGHT)
58
59 static int smg_match __P((struct device *, struct cfdata *, void *));
60 static void smg_attach __P((struct device *, struct device *, void *));
61
62 struct smg_softc {
63 struct device ss_dev;
64 };
65
66 struct cfattach smg_ca = {
67 sizeof(struct smg_softc), smg_match, smg_attach,
68 };
69
70 static void smg_cursor __P((void *, int, int, int));
71 static int smg_mapchar __P((void *, int, unsigned int *));
72 static void smg_putchar __P((void *, int, int, u_int, long));
73 static void smg_copycols __P((void *, int, int, int,int));
74 static void smg_erasecols __P((void *, int, int, int, long));
75 static void smg_copyrows __P((void *, int, int, int));
76 static void smg_eraserows __P((void *, int, int, long));
77 static int smg_alloc_attr __P((void *, int, int, int, long *));
78
79 const struct wsdisplay_emulops smg_emulops = {
80 smg_cursor,
81 smg_mapchar,
82 smg_putchar,
83 smg_copycols,
84 smg_erasecols,
85 smg_copyrows,
86 smg_eraserows,
87 smg_alloc_attr
88 };
89
90 const struct wsscreen_descr smg_stdscreen = {
91 "128x57", SM_COLS, SM_ROWS,
92 &smg_emulops,
93 8, SM_CHEIGHT,
94 WSSCREEN_UNDERLINE|WSSCREEN_REVERSE,
95 };
96
97 const struct wsscreen_descr *_smg_scrlist[] = {
98 &smg_stdscreen,
99 };
100
101 const struct wsscreen_list smg_screenlist = {
102 sizeof(_smg_scrlist) / sizeof(struct wsscreen_descr *),
103 _smg_scrlist,
104 };
105
106 static caddr_t sm_addr;
107
108 extern char q_font[];
109 #define QCHAR(c) (c < 32 ? 32 : (c > 127 ? c - 66 : c - 32))
110 #define QFONT(c,line) q_font[QCHAR(c) * 15 + line]
111 #define SM_ADDR(row, col, line) \
112 sm_addr[col + (row * SM_CHEIGHT * SM_COLS) + line * SM_COLS]
113
114
115 static int smg_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
116 static int smg_mmap __P((void *, off_t, int));
117 static int smg_alloc_screen __P((void *, const struct wsscreen_descr *,
118 void **, int *, int *, long *));
119 static void smg_free_screen __P((void *, void *));
120 static int smg_show_screen __P((void *, void *, int,
121 void (*) (void *, int, int), void *));
122 static void smg_crsr_blink __P((void *));
123
124 const struct wsdisplay_accessops smg_accessops = {
125 smg_ioctl,
126 smg_mmap,
127 smg_alloc_screen,
128 smg_free_screen,
129 smg_show_screen,
130 0 /* load_font */
131 };
132
133 struct smg_screen {
134 int ss_curx;
135 int ss_cury;
136 u_char ss_image[SM_ROWS][SM_COLS]; /* Image of current screen */
137 u_char ss_attr[SM_ROWS][SM_COLS]; /* Reversed etc... */
138 };
139
140 static struct smg_screen smg_conscreen;
141 static struct smg_screen *curscr;
142
143 int
144 smg_match(parent, match, aux)
145 struct device *parent;
146 struct cfdata *match;
147 void *aux;
148 {
149 struct vsbus_attach_args *va = aux;
150 volatile short *curcmd;
151 volatile short *cfgtst;
152 short tmp, tmp2;
153
154 if (vax_boardtype == VAX_BTYP_49)
155 return 0;
156
157 curcmd = (short *)va->va_addr;
158 cfgtst = (short *)vax_map_physmem(VS_CFGTST, 1);
159 /*
160 * Try to find the cursor chip by testing the flip-flop.
161 * If nonexistent, no glass tty.
162 */
163 curcmd[0] = 0x7fff;
164 DELAY(300000);
165 tmp = cfgtst[0];
166 curcmd[0] = 0x8000;
167 DELAY(300000);
168 tmp2 = cfgtst[0];
169 vax_unmap_physmem((vaddr_t)cfgtst, 1);
170
171 if (tmp2 != tmp)
172 return 20; /* Using periodic interrupt */
173 else
174 return 0;
175 }
176
177 void
178 smg_attach(parent, self, aux)
179 struct device *parent, *self;
180 void *aux;
181 {
182 struct wsemuldisplaydev_attach_args aa;
183
184 printf("\n");
185 sm_addr = (caddr_t)vax_map_physmem(SMADDR, (SMSIZE/VAX_NBPG));
186 if (sm_addr == 0) {
187 printf("%s: Couldn't alloc graphics memory.\n", self->dv_xname);
188 return;
189 }
190 curscr = &smg_conscreen;
191 aa.console = !(vax_confdata & 0x20);
192 aa.scrdata = &smg_screenlist;
193 aa.accessops = &smg_accessops;
194 timeout(smg_crsr_blink, 0, hz/2);
195
196 config_found(self, &aa, wsemuldisplaydevprint);
197 }
198
199 static u_char *cursor;
200 static int cur_on;
201
202 static void
203 smg_crsr_blink(arg)
204 void *arg;
205 {
206 if (cur_on)
207 *cursor ^= 255;
208 timeout(smg_crsr_blink, 0, hz/2);
209 }
210
211 void
212 smg_cursor(id, on, row, col)
213 void *id;
214 int on, row, col;
215 {
216 struct smg_screen *ss = id;
217
218 if (ss == curscr) {
219 SM_ADDR(ss->ss_cury, ss->ss_curx, 14) =
220 QFONT(ss->ss_image[ss->ss_cury][ss->ss_curx], 14);
221 cursor = &SM_ADDR(row, col, 14);
222 if ((cur_on = on))
223 *cursor ^= 255;
224 }
225 ss->ss_curx = col;
226 ss->ss_cury = row;
227 }
228
229 int
230 smg_mapchar(id, uni, index)
231 void *id;
232 int uni;
233 unsigned int *index;
234 {
235 if (uni < 256) {
236 *index = uni;
237 return (5);
238 }
239 *index = ' ';
240 return (0);
241 }
242
243 static void
244 smg_putchar(id, row, col, c, attr)
245 void *id;
246 int row, col;
247 u_int c;
248 long attr;
249 {
250 struct smg_screen *ss = id;
251 int i;
252
253 c &= 0xff;
254
255 ss->ss_image[row][col] = c;
256 ss->ss_attr[row][col] = attr;
257 if (ss != curscr)
258 return;
259 for (i = 0; i < 15; i++) {
260 unsigned char ch = QFONT(c, i);
261
262 SM_ADDR(row, col, i) = (attr & WSATTR_REVERSE ? ~ch : ch);
263
264 }
265 if (attr & WSATTR_UNDERLINE)
266 SM_ADDR(row, col, 14) ^= SM_ADDR(row, col, 14);
267 }
268
269 /*
270 * copies columns inside a row.
271 */
272 static void
273 smg_copycols(id, row, srccol, dstcol, ncols)
274 void *id;
275 int row, srccol, dstcol, ncols;
276 {
277 struct smg_screen *ss = id;
278 int i;
279
280 bcopy(&ss->ss_image[row][srccol], &ss->ss_image[row][dstcol], ncols);
281 bcopy(&ss->ss_attr[row][srccol], &ss->ss_attr[row][dstcol], ncols);
282 if (ss != curscr)
283 return;
284 for (i = 0; i < SM_CHEIGHT; i++)
285 bcopy(&SM_ADDR(row,srccol, i), &SM_ADDR(row, dstcol, i),ncols);
286 }
287
288 /*
289 * Erases a bunch of chars inside one row.
290 */
291 static void
292 smg_erasecols(id, row, startcol, ncols, fillattr)
293 void *id;
294 int row, startcol, ncols;
295 long fillattr;
296 {
297 struct smg_screen *ss = id;
298 int i;
299
300 bzero(&ss->ss_image[row][startcol], ncols);
301 bzero(&ss->ss_attr[row][startcol], ncols);
302 if (ss != curscr)
303 return;
304 for (i = 0; i < SM_CHEIGHT; i++)
305 bzero(&SM_ADDR(row, startcol, i), ncols);
306 }
307
308 static void
309 smg_copyrows(id, srcrow, dstrow, nrows)
310 void *id;
311 int srcrow, dstrow, nrows;
312 {
313 struct smg_screen *ss = id;
314 int frows;
315
316 bcopy(&ss->ss_image[srcrow][0], &ss->ss_image[dstrow][0],
317 nrows * SM_COLS);
318 bcopy(&ss->ss_attr[srcrow][0], &ss->ss_attr[dstrow][0],
319 nrows * SM_COLS);
320 if (ss != curscr)
321 return;
322 if (nrows > 25) {
323 frows = nrows >> 1;
324 if (srcrow > dstrow) {
325 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
326 &sm_addr[(dstrow * SM_NEXTROW)],
327 frows * SM_NEXTROW);
328 bcopy(&sm_addr[((srcrow + frows) * SM_NEXTROW)],
329 &sm_addr[((dstrow + frows) * SM_NEXTROW)],
330 (nrows - frows) * SM_NEXTROW);
331 } else {
332 bcopy(&sm_addr[((srcrow + frows) * SM_NEXTROW)],
333 &sm_addr[((dstrow + frows) * SM_NEXTROW)],
334 (nrows - frows) * SM_NEXTROW);
335 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
336 &sm_addr[(dstrow * SM_NEXTROW)],
337 frows * SM_NEXTROW);
338 }
339 } else
340 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
341 &sm_addr[(dstrow * SM_NEXTROW)], nrows * SM_NEXTROW);
342 }
343
344 static void
345 smg_eraserows(id, startrow, nrows, fillattr)
346 void *id;
347 int startrow, nrows;
348 long fillattr;
349 {
350 struct smg_screen *ss = id;
351 int frows;
352
353 bzero(&ss->ss_image[startrow][0], nrows * SM_COLS);
354 bzero(&ss->ss_attr[startrow][0], nrows * SM_COLS);
355 if (ss != curscr)
356 return;
357 if (nrows > 25) {
358 frows = nrows >> 1;
359 bzero(&sm_addr[(startrow * SM_NEXTROW)], frows * SM_NEXTROW);
360 bzero(&sm_addr[((startrow + frows) * SM_NEXTROW)],
361 (nrows - frows) * SM_NEXTROW);
362 } else
363 bzero(&sm_addr[(startrow * SM_NEXTROW)], nrows * SM_NEXTROW);
364 }
365
366 static int
367 smg_alloc_attr(id, fg, bg, flags, attrp)
368 void *id;
369 int fg, bg;
370 int flags;
371 long *attrp;
372 {
373 *attrp = flags;
374 return 0;
375 }
376
377 int
378 smg_ioctl(v, cmd, data, flag, p)
379 void *v;
380 u_long cmd;
381 caddr_t data;
382 int flag;
383 struct proc *p;
384 {
385 struct wsdisplay_fbinfo fb;
386
387 switch (cmd) {
388 case WSDISPLAYIO_GTYPE:
389 *(u_int *)data = WSDISPLAY_TYPE_PM_MONO;
390 break;
391
392 case WSDISPLAYIO_GINFO:
393 fb.height = 864;
394 fb.width = 1024;
395 return copyout(&fb, data, sizeof(struct wsdisplay_fbinfo));
396
397
398 default:
399 return -1;
400 }
401 return 0;
402 }
403
404 static int
405 smg_mmap(v, offset, prot)
406 void *v;
407 off_t offset;
408 int prot;
409 {
410 if (offset >= SMSIZE || offset < 0)
411 return -1;
412 return (SMADDR + offset) >> PGSHIFT;
413 }
414
415 int
416 smg_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
417 void *v;
418 const struct wsscreen_descr *type;
419 void **cookiep;
420 int *curxp, *curyp;
421 long *defattrp;
422 {
423 *cookiep = malloc(sizeof(struct smg_screen), M_DEVBUF, M_WAITOK);
424 bzero(*cookiep, sizeof(struct smg_screen));
425 *curxp = *curyp = *defattrp = 0;
426 return 0;
427 }
428
429 void
430 smg_free_screen(v, cookie)
431 void *v;
432 void *cookie;
433 {
434 }
435
436 int
437 smg_show_screen(v, cookie, waitok, cb, cbarg)
438 void *v;
439 void *cookie;
440 int waitok;
441 void (*cb) __P((void *, int, int));
442 void *cbarg;
443 {
444 struct smg_screen *ss = cookie;
445 int row, col, line;
446
447 if (ss == curscr)
448 return (0);
449
450 for (row = 0; row < SM_ROWS; row++)
451 for (line = 0; line < SM_CHEIGHT; line++) {
452 for (col = 0; col < SM_COLS; col++) {
453 u_char s, c = ss->ss_image[row][col];
454
455 if (c < 32)
456 c = 32;
457 s = QFONT(c, line);
458 if (ss->ss_attr[row][col] & WSATTR_REVERSE)
459 s ^= 255;
460 SM_ADDR(row, col, line) = s;
461 }
462 if (ss->ss_attr[row][col] & WSATTR_UNDERLINE)
463 SM_ADDR(row, col, line) = 255;
464 }
465 cursor = &sm_addr[(ss->ss_cury * SM_CHEIGHT * SM_COLS) + ss->ss_curx +
466 ((SM_CHEIGHT - 1) * SM_COLS)];
467 curscr = ss;
468 return (0);
469 }
470
471 cons_decl(smg);
472
473 #define WSCONSOLEMAJOR 68
474
475 void
476 smgcninit(cndev)
477 struct consdev *cndev;
478 {
479 extern void lkccninit __P((struct consdev *));
480 extern int lkccngetc __P((dev_t));
481 /* Clear screen */
482 memset(sm_addr, 0, 128*864);
483
484 curscr = &smg_conscreen;
485 wsdisplay_cnattach(&smg_stdscreen, &smg_conscreen, 0, 0, 0);
486 cn_tab->cn_pri = CN_INTERNAL;
487 #if 0
488 lkccninit(cndev);
489 wsdisplay_set_cons_kbd(lkccngetc, nullcnpollc);
490 #endif
491 }
492
493 /*
494 * Called very early to setup the glass tty as console.
495 * Because it's called before the VM system is inited, virtual memory
496 * for the framebuffer can be stolen directly without disturbing anything.
497 */
498 void
499 smgcnprobe(cndev)
500 struct consdev *cndev;
501 {
502 extern vaddr_t virtual_avail;
503
504 switch (vax_boardtype) {
505 case VAX_BTYP_410:
506 case VAX_BTYP_420:
507 case VAX_BTYP_43:
508 if ((vax_confdata & KA420_CFG_L3CON) ||
509 (vax_confdata & KA420_CFG_MULTU))
510 break; /* doesn't use graphics console */
511 sm_addr = (caddr_t)virtual_avail;
512 virtual_avail += SMSIZE;
513 ioaccess((vaddr_t)sm_addr, SMADDR, (SMSIZE/VAX_NBPG));
514 cndev->cn_pri = CN_INTERNAL;
515 cndev->cn_dev = makedev(WSCONSOLEMAJOR, 0);
516 break;
517
518 default:
519 break;
520 }
521 }
522