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