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