smg.c revision 1.15.4.1 1 /* $NetBSD: smg.c,v 1.15.4.1 1999/06/21 01:03:47 thorpej 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 void smg_show_screen __P((void *, void *));
121 static void smg_crsr_blink __P((void *));
122
123 const struct wsdisplay_accessops smg_accessops = {
124 smg_ioctl,
125 smg_mmap,
126 smg_alloc_screen,
127 smg_free_screen,
128 smg_show_screen,
129 0 /* load_font */
130 };
131
132 struct smg_screen {
133 int ss_curx;
134 int ss_cury;
135 u_char ss_image[SM_ROWS][SM_COLS]; /* Image of current screen */
136 u_char ss_attr[SM_ROWS][SM_COLS]; /* Reversed etc... */
137 };
138
139 static struct smg_screen smg_conscreen;
140 static struct smg_screen *curscr;
141
142 int
143 smg_match(parent, match, aux)
144 struct device *parent;
145 struct cfdata *match;
146 void *aux;
147 {
148 struct vsbus_attach_args *va = aux;
149 volatile short *curcmd = (short *)va->va_addr;
150 volatile short *cfgtst = (short *)vax_map_physmem(VS_CFGTST, 1);
151 short tmp, tmp2;
152
153 /*
154 * Try to find the cursor chip by testing the flip-flop.
155 * If nonexistent, no glass tty.
156 */
157 curcmd[0] = 0x7fff;
158 DELAY(300000);
159 tmp = cfgtst[0];
160 curcmd[0] = 0x8000;
161 DELAY(300000);
162 tmp2 = cfgtst[0];
163 vax_unmap_physmem((vaddr_t)cfgtst, 1);
164
165 if (tmp2 != tmp)
166 return 20; /* Using periodic interrupt */
167 else
168 return 0;
169 }
170
171 void
172 smg_attach(parent, self, aux)
173 struct device *parent, *self;
174 void *aux;
175 {
176 struct wsemuldisplaydev_attach_args aa;
177
178 printf("\n");
179 sm_addr = (caddr_t)vax_map_physmem(SMADDR, (SMSIZE/VAX_NBPG));
180 if (sm_addr == 0) {
181 printf("%s: Couldn't alloc graphics memory.\n", self->dv_xname);
182 return;
183 }
184 curscr = &smg_conscreen;
185 aa.console = !(vax_confdata & 0x20);
186 aa.scrdata = &smg_screenlist;
187 aa.accessops = &smg_accessops;
188 timeout(smg_crsr_blink, 0, hz/2);
189
190 config_found(self, &aa, wsemuldisplaydevprint);
191 }
192
193 static u_char *cursor;
194 static int cur_on;
195
196 static void
197 smg_crsr_blink(arg)
198 void *arg;
199 {
200 if (cur_on)
201 *cursor ^= 255;
202 timeout(smg_crsr_blink, 0, hz/2);
203 }
204
205 void
206 smg_cursor(id, on, row, col)
207 void *id;
208 int on, row, col;
209 {
210 struct smg_screen *ss = id;
211
212 if (ss == curscr) {
213 SM_ADDR(ss->ss_cury, ss->ss_curx, 14) =
214 QFONT(ss->ss_image[ss->ss_cury][ss->ss_curx], 14);
215 cursor = &SM_ADDR(row, col, 14);
216 if ((cur_on = on))
217 *cursor ^= 255;
218 }
219 ss->ss_curx = col;
220 ss->ss_cury = row;
221 }
222
223 int
224 smg_mapchar(id, uni, index)
225 void *id;
226 int uni;
227 unsigned int *index;
228 {
229 if (uni < 256) {
230 *index = uni;
231 return (5);
232 }
233 *index = ' ';
234 return (0);
235 }
236
237 static void
238 smg_putchar(id, row, col, c, attr)
239 void *id;
240 int row, col;
241 u_int c;
242 long attr;
243 {
244 struct smg_screen *ss = id;
245 int i;
246
247 c &= 0xff;
248
249 ss->ss_image[row][col] = c;
250 ss->ss_attr[row][col] = attr;
251 if (ss != curscr)
252 return;
253 for (i = 0; i < 15; i++) {
254 unsigned char ch = QFONT(c, i);
255
256 SM_ADDR(row, col, i) = (attr & WSATTR_REVERSE ? ~ch : ch);
257
258 }
259 if (attr & WSATTR_UNDERLINE)
260 SM_ADDR(row, col, 14) ^= SM_ADDR(row, col, 14);
261 }
262
263 /*
264 * copies columns inside a row.
265 */
266 static void
267 smg_copycols(id, row, srccol, dstcol, ncols)
268 void *id;
269 int row, srccol, dstcol, ncols;
270 {
271 struct smg_screen *ss = id;
272 int i;
273
274 bcopy(&ss->ss_image[row][srccol], &ss->ss_image[row][dstcol], ncols);
275 bcopy(&ss->ss_attr[row][srccol], &ss->ss_attr[row][dstcol], ncols);
276 if (ss != curscr)
277 return;
278 for (i = 0; i < SM_CHEIGHT; i++)
279 bcopy(&SM_ADDR(row,srccol, i), &SM_ADDR(row, dstcol, i),ncols);
280 }
281
282 /*
283 * Erases a bunch of chars inside one row.
284 */
285 static void
286 smg_erasecols(id, row, startcol, ncols, fillattr)
287 void *id;
288 int row, startcol, ncols;
289 long fillattr;
290 {
291 struct smg_screen *ss = id;
292 int i;
293
294 bzero(&ss->ss_image[row][startcol], ncols);
295 bzero(&ss->ss_attr[row][startcol], ncols);
296 if (ss != curscr)
297 return;
298 for (i = 0; i < SM_CHEIGHT; i++)
299 bzero(&SM_ADDR(row, startcol, i), ncols);
300 }
301
302 static void
303 smg_copyrows(id, srcrow, dstrow, nrows)
304 void *id;
305 int srcrow, dstrow, nrows;
306 {
307 struct smg_screen *ss = id;
308 int frows;
309
310 bcopy(&ss->ss_image[srcrow][0], &ss->ss_image[dstrow][0],
311 nrows * SM_COLS);
312 bcopy(&ss->ss_attr[srcrow][0], &ss->ss_attr[dstrow][0],
313 nrows * SM_COLS);
314 if (ss != curscr)
315 return;
316 if (nrows > 25) {
317 frows = nrows >> 1;
318 if (srcrow > dstrow) {
319 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
320 &sm_addr[(dstrow * SM_NEXTROW)],
321 frows * SM_NEXTROW);
322 bcopy(&sm_addr[((srcrow + frows) * SM_NEXTROW)],
323 &sm_addr[((dstrow + frows) * SM_NEXTROW)],
324 (nrows - frows) * SM_NEXTROW);
325 } else {
326 bcopy(&sm_addr[((srcrow + frows) * SM_NEXTROW)],
327 &sm_addr[((dstrow + frows) * SM_NEXTROW)],
328 (nrows - frows) * SM_NEXTROW);
329 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
330 &sm_addr[(dstrow * SM_NEXTROW)],
331 frows * SM_NEXTROW);
332 }
333 } else
334 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
335 &sm_addr[(dstrow * SM_NEXTROW)], nrows * SM_NEXTROW);
336 }
337
338 static void
339 smg_eraserows(id, startrow, nrows, fillattr)
340 void *id;
341 int startrow, nrows;
342 long fillattr;
343 {
344 struct smg_screen *ss = id;
345 int frows;
346
347 bzero(&ss->ss_image[startrow][0], nrows * SM_COLS);
348 bzero(&ss->ss_attr[startrow][0], nrows * SM_COLS);
349 if (ss != curscr)
350 return;
351 if (nrows > 25) {
352 frows = nrows >> 1;
353 bzero(&sm_addr[(startrow * SM_NEXTROW)], frows * SM_NEXTROW);
354 bzero(&sm_addr[((startrow + frows) * SM_NEXTROW)],
355 (nrows - frows) * SM_NEXTROW);
356 } else
357 bzero(&sm_addr[(startrow * SM_NEXTROW)], nrows * SM_NEXTROW);
358 }
359
360 static int
361 smg_alloc_attr(id, fg, bg, flags, attrp)
362 void *id;
363 int fg, bg;
364 int flags;
365 long *attrp;
366 {
367 *attrp = flags;
368 return 0;
369 }
370
371 int
372 smg_ioctl(v, cmd, data, flag, p)
373 void *v;
374 u_long cmd;
375 caddr_t data;
376 int flag;
377 struct proc *p;
378 {
379 struct wsdisplay_fbinfo fb;
380
381 switch (cmd) {
382 case WSDISPLAYIO_GTYPE:
383 *(u_int *)data = WSDISPLAY_TYPE_PM_MONO;
384 break;
385
386 case WSDISPLAYIO_GINFO:
387 fb.height = 864;
388 fb.width = 1024;
389 return copyout(&fb, data, sizeof(struct wsdisplay_fbinfo));
390
391
392 default:
393 return -1;
394 }
395 return 0;
396 }
397
398 static int
399 smg_mmap(v, offset, prot)
400 void *v;
401 off_t offset;
402 int prot;
403 {
404 if (offset >= SMSIZE || offset < 0)
405 return -1;
406 return (SMADDR + offset) >> CLSHIFT;
407 }
408
409 int
410 smg_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
411 void *v;
412 const struct wsscreen_descr *type;
413 void **cookiep;
414 int *curxp, *curyp;
415 long *defattrp;
416 {
417 *cookiep = malloc(sizeof(struct smg_screen), M_DEVBUF, M_WAITOK);
418 bzero(*cookiep, sizeof(struct smg_screen));
419 *curxp = *curyp = *defattrp = 0;
420 return 0;
421 }
422
423 void
424 smg_free_screen(v, cookie)
425 void *v;
426 void *cookie;
427 {
428 }
429
430 void
431 smg_show_screen(v, cookie)
432 void *v;
433 void *cookie;
434 {
435 struct smg_screen *ss = cookie;
436 int row, col, line;
437
438 if (ss == curscr)
439 return;
440
441 for (row = 0; row < SM_ROWS; row++)
442 for (line = 0; line < SM_CHEIGHT; line++) {
443 for (col = 0; col < SM_COLS; col++) {
444 u_char s, c = ss->ss_image[row][col];
445
446 if (c < 32)
447 c = 32;
448 s = QFONT(c, line);
449 if (ss->ss_attr[row][col] & WSATTR_REVERSE)
450 s ^= 255;
451 SM_ADDR(row, col, line) = s;
452 }
453 if (ss->ss_attr[row][col] & WSATTR_UNDERLINE)
454 SM_ADDR(row, col, line) = 255;
455 }
456 cursor = &sm_addr[(ss->ss_cury * SM_CHEIGHT * SM_COLS) + ss->ss_curx +
457 ((SM_CHEIGHT - 1) * SM_COLS)];
458 curscr = ss;
459 }
460
461 cons_decl(smg);
462
463 #define WSCONSOLEMAJOR 68
464
465 void
466 smgcninit(cndev)
467 struct consdev *cndev;
468 {
469 extern void lkccninit __P((struct consdev *));
470 extern int lkccngetc __P((dev_t));
471 /* Clear screen */
472 memset(sm_addr, 0, 128*864);
473
474 curscr = &smg_conscreen;
475 wsdisplay_cnattach(&smg_stdscreen, &smg_conscreen, 0, 0, 0);
476 cn_tab->cn_dev = makedev(WSCONSOLEMAJOR, 0);
477 #if NLKC
478 lkccninit(cndev);
479 wsdisplay_set_cons_kbd(lkccngetc, nullcnpollc);
480 #endif
481 }
482
483 int smgprobe(void);
484 int
485 smgprobe()
486 {
487 switch (vax_boardtype) {
488 case VAX_BTYP_410:
489 case VAX_BTYP_420:
490 case VAX_BTYP_43:
491 if ((vax_confdata & KA420_CFG_L3CON) ||
492 (vax_confdata & KA420_CFG_MULTU))
493 break; /* doesn't use graphics console */
494 sm_addr = (caddr_t)vax_map_physmem(SMADDR, (SMSIZE/VAX_NBPG));
495 if (sm_addr == 0)
496 return 0;
497
498 return 1;
499
500 default:
501 break;
502 }
503 return 0;
504 }
505