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