ite_et.c revision 1.7 1 /* $NetBSD: ite_et.c,v 1.7 1997/07/15 06:48:06 leo Exp $ */
2
3 /*
4 * Copyright (c) 1996 Leo Weppelman.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Leo Weppelman.
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 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/ioctl.h>
36 #include <sys/malloc.h>
37 #include <sys/device.h>
38 #include <dev/cons.h>
39
40 #include <machine/cpu.h>
41
42 #include <atari/atari/device.h>
43
44 #include <atari/dev/itevar.h>
45 #include <atari/dev/iteioctl.h>
46 #include <atari/dev/grfioctl.h>
47 #include <atari/dev/grf_etreg.h>
48 #include <atari/dev/grfabs_reg.h>
49 #include <atari/dev/grfabs_et.h>
50 #include <atari/dev/grfvar.h>
51 #include <atari/dev/font.h>
52 #include <atari/dev/viewioctl.h>
53 #include <atari/dev/viewvar.h>
54
55 #include "grfet.h"
56
57 /*
58 * This is what ip->priv points to;
59 * it contains local variables for custom-chip ites.
60 */
61 struct ite_priv {
62 volatile u_char *regkva;
63 };
64
65 typedef struct ite_priv ipriv_t;
66
67 static ipriv_t con_ipriv;
68
69 /* Console colors */
70 static u_char etconscolors[3][3] = { /* background, foreground, hilite */
71 {0x0, 0x0, 0x0}, {0x30, 0x30, 0x30}, { 0x3f, 0x3f, 0x3f}
72 };
73
74 /* XXX: Shouldn't these be in font.h???? */
75 extern font_info font_info_8x8;
76 extern font_info font_info_8x16;
77
78 static void grfet_iteinit __P((struct grf_softc *));
79 static void view_init __P((struct ite_softc *));
80 static void view_deinit __P((struct ite_softc *));
81 static int iteet_ioctl __P((struct ite_softc *, u_long, caddr_t, int,
82 struct proc *));
83 static int ite_newsize __P((struct ite_softc *, struct itewinsize *));
84 static void et_inittextmode __P((struct ite_softc *, et_sv_reg_t *, int));
85 void et_cursor __P((struct ite_softc *ip, int flag));
86 void et_clear __P((struct ite_softc *ip, int sy, int sx, int h, int w));
87 void et_putc __P((struct ite_softc *ip, int c, int dy, int dx, int mode));
88 void et_scroll __P((struct ite_softc *ip, int sy, int sx, int count,
89 int dir));
90
91 /* XXX: move to ite.c */
92 extern int ite_default_x;
93 extern int ite_default_y;
94 extern int ite_default_width;
95 extern int ite_default_depth;
96 extern int ite_default_height;
97
98 /*
99 * grfet config stuff
100 */
101 void grfetattach __P((struct device *, struct device *, void *));
102 int grfetmatch __P((struct device *, struct cfdata *, void *));
103 int grfetprint __P((void *, const char *));
104
105 struct cfattach grfet_ca = {
106 sizeof(struct grf_softc), grfetmatch, grfetattach
107 };
108
109 struct cfdriver grfet_cd = {
110 NULL, "grfet", DV_DULL
111 };
112
113 /*
114 * only used in console init.
115 */
116 static struct cfdata *cfdata_grf = NULL;
117
118 int
119 grfetmatch(pdp, cfp, auxp)
120 struct device *pdp;
121 struct cfdata *cfp;
122 void *auxp;
123 {
124 static int card_probed = -1;
125 grf_auxp_t *grf_auxp = auxp;
126
127 if (card_probed <= 0) {
128 if (card_probed == 0) /* Probed but failed */
129 return 0;
130 card_probed = 0;
131
132 /*
133 * Check if the layers we depend on exist
134 */
135 if(!(machineid & ATARI_HADES))
136 return 0;
137 if (!et_probe_card())
138 return 0;
139 if (grfabs_probe(&et_probe_video) == 0)
140 return 0;
141 viewprobe();
142 card_probed = 1; /* Probed and found */
143 }
144
145 if (atari_realconfig == 0) {
146 /*
147 * Early console init. Only match unit 0.
148 */
149 if (cfp->cf_unit != 0)
150 return 0;
151 if (viewopen(0, 0, 0, NULL))
152 return 0;
153 cfdata_grf = cfp;
154 return 1;
155 }
156
157 /*
158 * Normal config. When we are called directly from the grfbus,
159 * we only match unit 0. The attach function will call us for
160 * the other configured units.
161 */
162 if (grf_auxp->from_bus_match
163 && ((cfp->cf_unit != 0) || !et_probe_card()))
164 return 0;
165
166 if (!grf_auxp->from_bus_match && (grf_auxp->unit != cfp->cf_unit))
167 return 0;
168
169 /*
170 * Final constraint: each grf needs a view....
171 */
172 if((cfdata_grf == NULL) || (cfp->cf_unit != 0)) {
173 if(viewopen(cfp->cf_unit, 0, 0, NULL))
174 return 0;
175 }
176 return 1;
177 }
178
179 /*
180 * attach: initialize the grf-structure and try to attach an ite to us.
181 * note : dp is NULL during early console init.
182 */
183 void
184 grfetattach(pdp, dp, auxp)
185 struct device *pdp, *dp;
186 void *auxp;
187 {
188 static struct grf_softc congrf;
189 grf_auxp_t *grf_bus_auxp = auxp;
190 grf_auxp_t grf_auxp;
191 struct grf_softc *gp;
192 int maj;
193
194 /*
195 * find our major device number
196 */
197 for(maj = 0; maj < nchrdev; maj++)
198 if (cdevsw[maj].d_open == grfopen)
199 break;
200
201 /*
202 * Handle exeption case: early console init
203 */
204 if(dp == NULL) {
205 congrf.g_unit = 0;
206 congrf.g_grfdev = makedev(maj, 0);
207 congrf.g_itedev = (dev_t)-1;
208 congrf.g_flags = GF_ALIVE;
209 congrf.g_mode = grf_mode;
210 congrf.g_conpri = CN_INTERNAL;
211 congrf.g_viewdev = congrf.g_unit;
212 grfet_iteinit(&congrf);
213 grf_viewsync(&congrf);
214
215 /* Attach console ite */
216 atari_config_found(cfdata_grf, NULL, &congrf, grfetprint);
217 return;
218 }
219
220 gp = (struct grf_softc *)dp;
221 gp->g_unit = gp->g_device.dv_unit;
222 grfsp[gp->g_unit] = gp;
223
224 if((cfdata_grf != NULL) && (gp->g_unit == 0)) {
225 /*
226 * We inited earlier just copy the info, take care
227 * not to copy the device struct though.
228 */
229 bcopy(&congrf.g_display, &gp->g_display,
230 (char *)&gp[1] - (char *)&gp->g_display);
231 }
232 else {
233 gp->g_grfdev = makedev(maj, gp->g_unit);
234 gp->g_itedev = (dev_t)-1;
235 gp->g_flags = GF_ALIVE;
236 gp->g_mode = grf_mode;
237 gp->g_conpri = 0;
238 gp->g_viewdev = gp->g_unit;
239 grfet_iteinit(gp);
240 grf_viewsync(gp);
241 }
242
243 printf(": %dx%d", gp->g_display.gd_dwidth, gp->g_display.gd_dheight);
244 if(gp->g_display.gd_colors == 2)
245 printf(" monochrome\n");
246 else printf(" colors %d\n", gp->g_display.gd_colors);
247
248 /*
249 * try and attach an ite
250 */
251 config_found(dp, gp, grfetprint);
252
253 /*
254 * If attaching unit 0, go ahead and 'find' the rest of us
255 */
256 if (gp->g_unit == 0) {
257 grf_auxp.from_bus_match = 0;
258 for (grf_auxp.unit=0; grf_auxp.unit < NGRFET; grf_auxp.unit++) {
259 config_found(pdp, (void*)&grf_auxp, grf_bus_auxp->busprint);
260 }
261 }
262 }
263
264 int
265 grfetprint(auxp, pnp)
266 void *auxp;
267 const char *pnp;
268 {
269 if(pnp) /* XXX */
270 printf("ite at %s", pnp);
271 return(UNCONF);
272 }
273
274 /*
275 * Init ite portion of grf_softc struct
276 */
277 static void
278 grfet_iteinit(gp)
279 struct grf_softc *gp;
280 {
281
282 gp->g_itecursor = et_cursor;
283 gp->g_iteputc = et_putc;
284 gp->g_iteclear = et_clear;
285 gp->g_itescroll = et_scroll;
286 gp->g_iteinit = view_init;
287 gp->g_itedeinit = view_deinit;
288 }
289
290 static void
291 view_deinit(ip)
292 struct ite_softc *ip;
293 {
294 ip->flags &= ~ITE_INITED;
295 }
296
297 static void
298 view_init(ip)
299 register struct ite_softc *ip;
300 {
301 struct itewinsize wsz;
302 ipriv_t *cci;
303 view_t *view;
304 save_area_t *et_save;
305
306 if((cci = ip->priv) != NULL)
307 return;
308
309 ip->itexx_ioctl = iteet_ioctl;
310
311 #if defined(KFONT_8X8)
312 ip->font = font_info_8x8;
313 #else
314 ip->font = font_info_8x16;
315 #endif
316
317 /* Find the correct set of rendering routines for this font. */
318 if(ip->font.width != 8)
319 panic("kernel font size not supported");
320
321 if(!atari_realconfig)
322 ip->priv = cci = &con_ipriv;
323 else ip->priv = cci = (ipriv_t*)malloc(sizeof(*cci), M_DEVBUF,M_WAITOK);
324 if(cci == NULL)
325 panic("No memory for ite-view");
326 bzero(cci, sizeof(*cci));
327
328 wsz.x = ite_default_x;
329 wsz.y = ite_default_y;
330 wsz.width = ite_default_width;
331 wsz.height = ite_default_height;
332 wsz.depth = ite_default_depth;
333
334 ite_newsize (ip, &wsz);
335
336 view = viewview(ip->grf->g_viewdev);
337 cci->regkva = view->bitmap->regs;
338
339 /*
340 * Only console will be turned on by default..
341 */
342 if(ip->flags & ITE_ISCONS)
343 ip->grf->g_mode(ip->grf, GM_GRFON, NULL, 0, 0);
344
345 /*
346 * Activate text-mode settings
347 */
348 et_save = (save_area_t *)view->save_area;
349 if (et_save == NULL)
350 et_inittextmode(ip, NULL, view->flags & VF_DISPLAY);
351 else {
352 et_inittextmode(ip, &et_save->sv_regs, view->flags&VF_DISPLAY);
353 et_save->fb_size = ip->cols * ip->rows;
354 }
355 }
356
357 static int
358 ite_newsize(ip, winsz)
359 struct ite_softc *ip;
360 struct itewinsize *winsz;
361 {
362 struct view_size vs;
363 int error = 0;
364 save_area_t *et_save;
365 view_t *view;
366
367 vs.x = winsz->x;
368 vs.y = winsz->y;
369 vs.width = winsz->width;
370 vs.height = winsz->height;
371 vs.depth = winsz->depth;
372
373 error = viewioctl(ip->grf->g_viewdev, VIOCSSIZE, (caddr_t)&vs, 0,
374 NOPROC);
375 view = viewview(ip->grf->g_viewdev);
376
377 /*
378 * Reinitialize our structs
379 */
380 ip->cols = view->display.width / ip->font.width;
381 ip->rows = view->display.height / ip->font.height;
382
383 /*
384 * save new values so that future opens use them
385 * this may not be correct when we implement Virtual Consoles
386 */
387 ite_default_height = view->display.height;
388 ite_default_width = view->display.width;
389 ite_default_x = view->display.x;
390 ite_default_y = view->display.y;
391 ite_default_depth = view->bitmap->depth;
392
393 et_save = (save_area_t *)view->save_area;
394 if (et_save == NULL)
395 et_inittextmode(ip, NULL, view->flags & VF_DISPLAY);
396 else {
397 et_inittextmode(ip, &et_save->sv_regs, view->flags & VF_DISPLAY);
398 et_save->fb_size = ip->cols * ip->rows;
399 }
400 et_clear(ip, 0, 0, ip->rows, ip->cols);
401
402 return(error);
403 }
404
405 int
406 iteet_ioctl(ip, cmd, addr, flag, p)
407 struct ite_softc *ip;
408 u_long cmd;
409 caddr_t addr;
410 int flag;
411 struct proc *p;
412 {
413 struct winsize ws;
414 struct itewinsize *is;
415 int error = 0;
416 view_t *view = viewview(ip->grf->g_viewdev);
417
418 switch (cmd) {
419 case ITEIOCSWINSZ:
420 is = (struct itewinsize *)addr;
421
422 if(ite_newsize(ip, is))
423 error = ENOMEM;
424 else {
425 view = viewview(ip->grf->g_viewdev);
426 ws.ws_row = ip->rows;
427 ws.ws_col = ip->cols;
428 ws.ws_xpixel = view->display.width;
429 ws.ws_ypixel = view->display.height;
430 ite_reset(ip);
431 /*
432 * XXX tell tty about the change
433 * XXX this is messy, but works
434 */
435 iteioctl(ip->grf->g_itedev,TIOCSWINSZ,(caddr_t)&ws,0,p);
436 }
437 break;
438 case VIOCSCMAP:
439 case VIOCGCMAP:
440 /*
441 * XXX watchout for that NOPROC. its not really the kernel
442 * XXX talking these two commands don't use the proc pointer
443 * XXX though.
444 */
445 error = viewioctl(ip->grf->g_viewdev, cmd, addr, flag, NOPROC);
446 break;
447 default:
448 error = -1;
449 break;
450 }
451 return (error);
452 }
453
454 void
455 et_cursor(ip, flag)
456 struct ite_softc *ip;
457 int flag;
458 {
459 volatile u_char *ba;
460 view_t *v;
461 u_long cpos;
462
463 ba = ((ipriv_t*)ip->priv)->regkva;
464 v = viewview(ip->grf->g_viewdev);
465
466 /*
467 * Don't update the cursor when not on display
468 */
469 if (!(v->flags & VF_DISPLAY))
470 return;
471
472 switch (flag) {
473 case DRAW_CURSOR:
474 /*WCrt(ba, CRT_ID_CURSOR_START, & ~0x20); */
475 case MOVE_CURSOR:
476 cpos = RCrt(ba, CRT_ID_START_ADDR_LOW) & 0xff;
477 cpos |= (RCrt(ba, CRT_ID_START_ADDR_HIGH) & 0xff) << 8;
478 cpos += ip->curx + ip->cury * ip->cols;
479 WCrt(ba, CRT_ID_CURSOR_LOC_LOW, cpos & 0xff);
480 WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, (cpos >> 8) & 0xff);
481 WCrt(ba, CTR_ID_EXT_START, (cpos >> (16-2)) & 0x0c);
482
483 ip->cursorx = ip->curx;
484 ip->cursory = ip->cury;
485 break;
486 case ERASE_CURSOR:
487 /*WCrt(ba, CRT_ID_CURSOR_START, | 0x20); */
488 case START_CURSOROPT:
489 case END_CURSOROPT:
490 default:
491 break;
492 }
493 }
494
495 void
496 et_putc(ip, c, dy, dx, mode)
497 struct ite_softc *ip;
498 int c;
499 int dy;
500 int dx;
501 int mode;
502 {
503 view_t *v = viewview(ip->grf->g_viewdev);
504 u_char attr;
505 u_short *cp;
506
507 attr = (unsigned char) ((mode & ATTR_INV) ? (0x70) : (0x07));
508 if (mode & ATTR_UL) attr |= 0x01;
509 if (mode & ATTR_BOLD) attr |= 0x08;
510 if (mode & ATTR_BLINK) attr |= 0x80;
511
512 cp = (u_short*)v->bitmap->plane;
513 cp[(dy * ip->cols) + dx] = (c << 8) | attr;
514 }
515
516 void
517 et_clear(ip, sy, sx, h, w)
518 struct ite_softc *ip;
519 int sy;
520 int sx;
521 int h;
522 int w;
523 {
524 /* cl_clear and cl_scroll both rely on ite passing arguments
525 * which describe continuous regions. For a VT200 terminal,
526 * this is safe behavior.
527 */
528 view_t *v = viewview(ip->grf->g_viewdev);
529 u_short *dest;
530 int len;
531
532 dest = (u_short *)v->bitmap->plane + (sy * ip->cols) + sx;
533 for(len = w * h; len-- ;)
534 *dest++ = 0x2007;
535 }
536
537 void
538 et_scroll(ip, sy, sx, count, dir)
539 struct ite_softc *ip;
540 int sy;
541 int sx;
542 int count;
543 int dir;
544 {
545 view_t *v = viewview(ip->grf->g_viewdev);
546 u_short *fb;
547 u_short *src, *dst;
548 int len;
549
550 fb = (u_short*)v->bitmap->plane + sy * ip->cols;
551 switch (dir) {
552 case SCROLL_UP:
553 src = fb;
554 dst = fb - count * ip->cols;
555 len = (ip->bottom_margin + 1 - sy) * ip->cols;
556 break;
557 case SCROLL_DOWN:
558 src = fb;
559 dst = fb + count * ip->cols;
560 len = (ip->bottom_margin + 1 - (sy + count)) * ip->cols;
561 break;
562 case SCROLL_RIGHT:
563 src = fb + sx;
564 dst = fb + sx + count;
565 len = ip->cols - sx + count;
566 break;
567 case SCROLL_LEFT:
568 src = fb + sx;
569 dst = fb + sx - count;
570 len = ip->cols - sx;
571 break;
572 default:
573 return;
574 }
575 if (src > dst) {
576 while (len--)
577 *dst++ = *src++;
578 }
579 else {
580 src = &src[len];
581 dst = &dst[len];
582 while (len--)
583 *--dst = *--src;
584 }
585 }
586
587 static void
588 et_inittextmode(ip, etregs, loadfont)
589 struct ite_softc *ip;
590 et_sv_reg_t *etregs;
591 int loadfont;
592 {
593 volatile u_char *ba;
594 font_info *fd;
595 u_char *fb;
596 u_char *c, *f, tmp;
597 u_short z, y;
598 int s;
599 view_t *v = viewview(ip->grf->g_viewdev);
600 et_sv_reg_t loc_regs;
601
602 if (etregs == NULL) {
603 etregs = &loc_regs;
604 et_hwsave(etregs);
605 }
606
607 ba = ((ipriv_t*)ip->priv)->regkva;
608 fb = v->bitmap->plane;
609
610 #if defined(KFONT_8X8)
611 fd = &font_info_8x8;
612 #else
613 fd = &font_info_8x16;
614 #endif
615
616 if (loadfont) { /* XXX: We should set the colormap */
617 /*
618 * set colors (B&W)
619 */
620 vgaw(ba, VDAC_ADDRESS_W, 0);
621 for (z = 0; z < 256; z++) {
622 y = (z & 1) ? ((z > 7) ? 2 : 1) : 0;
623
624 vgaw(ba, VDAC_DATA, etconscolors[y][0]);
625 vgaw(ba, VDAC_DATA, etconscolors[y][1]);
626 vgaw(ba, VDAC_DATA, etconscolors[y][2]);
627 }
628
629 /*
630 * Enter a suitable mode to download the font. This
631 * basically means sequential addressing mode
632 */
633 s = splhigh();
634
635 WAttr(ba, 0x20 | ACT_ID_ATTR_MODE_CNTL, 0x0a);
636 WSeq(ba, SEQ_ID_MAP_MASK, 0x04);
637 WSeq(ba, SEQ_ID_MEMORY_MODE, 0x06);
638 WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x02);
639 WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00);
640 WGfx(ba, GCT_ID_MISC, 0x04);
641 splx(s);
642
643 /*
644 * load text font into beginning of display memory. Each
645 * character cell is 32 bytes long (enough for 4 planes)
646 */
647 for (z = 0, c = fb; z < 256 * 32; z++)
648 *c++ = 0;
649
650 c = (unsigned char *) (fb) + (32 * fd->font_lo);
651 f = fd->font_p;
652 z = fd->font_lo;
653 for (; z <= fd->font_hi; z++, c += (32 - fd->height))
654 for (y = 0; y < fd->height; y++) {
655 *c++ = *f++;
656 }
657 }
658
659 /*
660 * Odd/Even addressing
661 */
662 etregs->seq[SEQ_ID_MAP_MASK] = 0x03;
663 etregs->seq[SEQ_ID_MEMORY_MODE] = 0x03;
664 etregs->grf[GCT_ID_READ_MAP_SELECT] = 0x00;
665 etregs->grf[GCT_ID_GRAPHICS_MODE] = 0x10;
666 etregs->grf[GCT_ID_MISC] = 0x06;
667
668 /*
669 * Font height + underline location
670 */
671 tmp = etregs->crt[CRT_ID_MAX_ROW_ADDRESS] & 0xe0;
672 etregs->crt[CRT_ID_MAX_ROW_ADDRESS] = tmp | (fd->height - 1);
673 tmp = etregs->crt[CRT_ID_UNDERLINE_LOC] & 0xe0;
674 etregs->crt[CRT_ID_UNDERLINE_LOC] = tmp | (fd->height - 1);
675
676 /*
677 * Cursor setup
678 */
679 etregs->crt[CRT_ID_CURSOR_START] = 0x00;
680 etregs->crt[CRT_ID_CURSOR_END] = fd->height - 1;
681 etregs->crt[CRT_ID_CURSOR_LOC_HIGH] = 0x00;
682 etregs->crt[CRT_ID_CURSOR_LOC_LOW] = 0x00;
683
684 /*
685 * Enter text mode
686 */
687 etregs->crt[CRT_ID_MODE_CONTROL] = 0xa3;
688 etregs->attr[ACT_ID_ATTR_MODE_CNTL] = 0x0a;
689
690 #if 1
691 if (loadfont || (etregs == &loc_regs))
692 #else
693 if (etregs == &loc_regs)
694 #endif
695 et_hwrest(etregs);
696 }
697