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