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