xref: /netbsd-src/sys/arch/atari/dev/grfabs_tt.c (revision 81b108b45f75f89f1e3ffad9fb6f074e771c0935)
1 /*	$NetBSD: grfabs_tt.c,v 1.5 1996/09/16 06:43:36 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 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 #ifdef TT_VIDEO
34 /*
35  *  atari abstract graphics driver: TT-interface
36  */
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/malloc.h>
40 #include <sys/device.h>
41 #include <sys/systm.h>
42 
43 #include <machine/iomap.h>
44 #include <machine/video.h>
45 #include <machine/mfp.h>
46 #include <atari/atari/device.h>
47 #include <atari/atari/stalloc.h>
48 #include <atari/dev/grfabs_reg.h>
49 #include <atari/dev/grfabs_tt.h>
50 
51 /*
52  * Function decls
53  */
54 static void       init_view __P((view_t *, bmap_t *, dmode_t *, box_t *));
55 static bmap_t	  *alloc_bitmap __P((u_long, u_long, u_char));
56 static colormap_t *alloc_colormap __P((dmode_t *));
57 static void 	  free_bitmap __P((bmap_t *));
58 static void	  tt_display_view __P((view_t *));
59 static view_t	  *tt_alloc_view __P((dmode_t *, dimen_t *, u_char));
60 static void	  tt_free_view __P((view_t *));
61 static void	  tt_remove_view __P((view_t *));
62 static int	  tt_use_colormap __P((view_t *, colormap_t *));
63 
64 /*
65  * Our function switch table
66  */
67 struct grfabs_sw tt_vid_sw = {
68 	tt_display_view,
69 	tt_alloc_view,
70 	tt_free_view,
71 	tt_remove_view,
72 	tt_use_colormap
73 };
74 
75 struct tt_hwregs {
76 	u_short		tt_reg;	/* video mode-register TT	*/
77 };
78 #define vm_reg(dm)	(((struct tt_hwregs*)(dm->data))->tt_reg)
79 
80 /*
81  * Note that the order of this table *must* match the order of
82  * the table below!
83  */
84 static struct tt_hwregs tt_hwregs[] = {
85 	{ RES_STHIGH },
86 	{ RES_TTHIGH },
87 	{ RES_STMID  },
88 	{ RES_STLOW  },
89 	{ RES_TTMID  },
90 	{ RES_TTLOW  }
91 };
92 
93 static dmode_t vid_modes[] = {
94     { { NULL, NULL }, "sthigh", { 640,  400 }, 1, NULL, &tt_vid_sw },
95     { { NULL, NULL }, "tthigh", { 1280, 960 }, 1, NULL, &tt_vid_sw },
96     { { NULL, NULL }, "stmid",  { 640,  200 }, 2, NULL, &tt_vid_sw },
97     { { NULL, NULL }, "stlow",  { 320,  200 }, 4, NULL, &tt_vid_sw },
98     { { NULL, NULL }, "ttmid",  { 640,  480 }, 4, NULL, &tt_vid_sw },
99     { { NULL, NULL }, "ttlow",  { 320,  480 }, 8, NULL, &tt_vid_sw },
100     { { NULL, NULL }, NULL,  }
101 };
102 
103 /*
104  * XXX: called from ite console init routine.
105  * Initialize list of posible video modes.
106  */
107 
108 void
109 tt_probe_video(modelp)
110 MODES	*modelp;
111 {
112 	dmode_t	*dm;
113 	int	i;
114 	int	has_mono;
115 
116 	/*
117 	 * First find out what kind of monitor is attached. Dma-sound
118 	 * should be off because the 'sound-done' and 'monochrome-detect'
119 	 * are xor-ed together. I think that shutting it down here is the
120 	 * wrong place.
121 	 */
122 	has_mono = (MFP->mf_gpip & IA_MONO) == 0;
123 
124 	for (i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
125 		dm->data = (void *)&tt_hwregs[i];
126 		if (has_mono && (vm_reg(dm) != RES_TTHIGH))
127 			continue;
128 		if (!has_mono && (vm_reg(dm) == RES_TTHIGH))
129 			continue;
130 		LIST_INSERT_HEAD(modelp, dm, link);
131 	}
132 
133 	for (i=0; i < 16; i++)
134 		VIDEO->vd_tt_rgb[i] = CM_L2TT(gra_def_color16[i]);
135 }
136 
137 static void
138 tt_display_view(v)
139 view_t *v;
140 {
141 	dmode_t	*dm = v->mode;
142 	bmap_t	*bm = v->bitmap;
143 
144 	if (dm->current_view) {
145 		/*
146 		 * Mark current view for this mode as no longer displayed
147 		 */
148 		dm->current_view->flags &= ~VF_DISPLAY;
149 	}
150 	dm->current_view = v;
151 	v->flags |= VF_DISPLAY;
152 
153 	tt_use_colormap(v, v->colormap);
154 
155 	/* XXX: should use vbl for this	*/
156 	VIDEO->vd_tt_res = vm_reg(dm);
157 	VIDEO->vd_raml   =  (u_long)bm->hw_address & 0xff;
158 	VIDEO->vd_ramm   = ((u_long)bm->hw_address >>  8) & 0xff;
159 	VIDEO->vd_ramh   = ((u_long)bm->hw_address >> 16) & 0xff;
160 }
161 
162 void
163 tt_remove_view(v)
164 view_t *v;
165 {
166 	dmode_t *mode = v->mode;
167 
168 	if (mode->current_view == v) {
169 #if 0
170 		if (v->flags & VF_DISPLAY)
171 			panic("Cannot shutdown display\n"); /* XXX */
172 #endif
173 		mode->current_view = NULL;
174 	}
175 	v->flags &= ~VF_DISPLAY;
176 }
177 
178 void
179 tt_free_view(v)
180 view_t *v;
181 {
182 	if(v) {
183 		tt_remove_view(v);
184 		if (v->colormap != &gra_con_cmap)
185 			free(v->colormap, M_DEVBUF);
186 		free_bitmap(v->bitmap);
187 		if (v != &gra_con_view)
188 			free(v, M_DEVBUF);
189 	}
190 }
191 
192 static int
193 tt_use_colormap(v, cm)
194 view_t		*v;
195 colormap_t	*cm;
196 {
197 	dmode_t			*dm;
198 	volatile u_short	*creg;
199 	u_long			*src;
200 	colormap_t		*vcm;
201 	u_long			*vcreg;
202 	u_short			ncreg;
203 	int			i;
204 
205 	dm  = v->mode;
206 	vcm = v->colormap;
207 
208 	/*
209 	 * I guess it seems reasonable to require the maps to be
210 	 * of the same type...
211 	 */
212 	if (cm->type != vcm->type)
213 		return(EINVAL);
214 
215 	/*
216 	 * First figure out where the actual colormap resides and
217 	 * howmany colors are in it.
218 	 */
219 	switch (vm_reg(dm)) {
220 		case RES_STLOW:
221 			creg  = &VIDEO->vd_tt_rgb[0];
222 			ncreg = 16;
223 			break;
224 		case RES_STMID:
225 			creg  = &VIDEO->vd_tt_rgb[0];
226 			ncreg = 4;
227 			break;
228 		case RES_STHIGH:
229 			creg  = &VIDEO->vd_tt_rgb[254];
230 			ncreg = 2;
231 			break;
232 		case RES_TTLOW:
233 			creg  = &VIDEO->vd_tt_rgb[0];
234 			ncreg = 256;
235 			break;
236 		case RES_TTMID:
237 			creg  = &VIDEO->vd_tt_rgb[0];
238 			ncreg = 16;
239 			break;
240 		case RES_TTHIGH:
241 			return(0);	/* No colors	*/
242 		default:
243 			panic("grf_tt:use_colormap: wrong mode!?");
244 	}
245 
246 	/* If first entry specified beyond capabilities -> error */
247 	if (cm->first >= ncreg)
248 		return(EINVAL);
249 
250 	/*
251 	 * A little tricky, the actual colormap pointer will be NULL
252 	 * when view is not displaying, valid otherwise.
253 	 */
254 	if (v->flags & VF_DISPLAY)
255 		creg = &creg[cm->first];
256 	else creg = NULL;
257 
258 	vcreg  = &vcm->entry[cm->first];
259 	ncreg -= cm->first;
260 	if (cm->size > ncreg)
261 		return(EINVAL);
262 	ncreg = cm->size;
263 
264 	for (i = 0, src = cm->entry; i < ncreg; i++, vcreg++) {
265 		*vcreg = *src++;
266 
267 		/*
268 		 * If displaying, also update actual color registers.
269 		 */
270 		if (creg != NULL)
271 			*creg++ = CM_L2TT(*vcreg);
272 	}
273 	return (0);
274 }
275 
276 static view_t *
277 tt_alloc_view(mode, dim, depth)
278 dmode_t	*mode;
279 dimen_t	*dim;
280 u_char   depth;
281 {
282 	view_t *v;
283 	bmap_t *bm;
284 
285 	if (!atari_realconfig)
286 		v = &gra_con_view;
287 	else v = malloc(sizeof(*v), M_DEVBUF, M_NOWAIT);
288 	if(v == NULL)
289 		return (NULL);
290 
291 	bm = alloc_bitmap(mode->size.width, mode->size.height, mode->depth);
292 	if (bm) {
293 		box_t   box;
294 
295 		v->colormap = alloc_colormap(mode);
296 		if (v->colormap) {
297 			INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
298 			init_view(v, bm, mode, &box);
299 			return (v);
300 		}
301 		free_bitmap(bm);
302 	}
303 	if (v != &gra_con_view)
304 		free(v, M_DEVBUF);
305 	return (NULL);
306 }
307 
308 static void
309 init_view(v, bm, mode, dbox)
310 view_t	*v;
311 bmap_t	*bm;
312 dmode_t	*mode;
313 box_t	*dbox;
314 {
315 	v->bitmap = bm;
316 	v->mode   = mode;
317 	v->flags  = 0;
318 	bcopy(dbox, &v->display, sizeof(box_t));
319 }
320 
321 /* bitmap functions */
322 
323 static bmap_t *
324 alloc_bitmap(width, height, depth)
325 u_long	width, height;
326 u_char	depth;
327 {
328 	u_long  total_size, bm_size;
329 	void	*hw_address;
330 	bmap_t	*bm;
331 
332 	/*
333 	 * Sigh, it seems for mapping to work we need the bitplane data to
334 	 *  1: be aligned on a page boundry.
335 	 *  2: be n pages large.
336 	 *
337 	 * why? because the user gets a page aligned address, if this is before
338 	 * your allocation, too bad.  Also it seems that the mapping routines
339 	 * do not watch to closely to the allowable length. so if you go over
340 	 * n pages by less than another page, the user gets to write all over
341 	 * the entire page. Since you did not allocate up to a page boundry
342 	 * (or more) the user writes into someone elses memory. -ch
343 	 */
344 	bm_size    = atari_round_page((width * height * depth) / NBBY);
345 	total_size = bm_size + sizeof(bmap_t) + NBPG;
346 
347 	if ((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
348 		return(NULL);
349 
350 	bm->plane         = (u_char*)bm + sizeof(bmap_t);
351 	bm->plane         = (u_char*)atari_round_page(bm->plane);
352 	bm->hw_address    = (u_char*)hw_address + sizeof(bmap_t);
353 	bm->hw_address    = (u_char*)atari_round_page(bm->hw_address);
354 	bm->bytes_per_row = (width * depth) / NBBY;
355 	bm->rows          = height;
356 	bm->depth         = depth;
357 	bm->regs          = NULL;
358 	bm->hw_regs       = NULL;
359 	bm->reg_size      = 0;
360 
361 	bzero(bm->plane, bm_size);
362 	return (bm);
363 }
364 
365 static void
366 free_bitmap(bm)
367 bmap_t *bm;
368 {
369 	if (bm)
370 		free_stmem(bm);
371 }
372 
373 static colormap_t *
374 alloc_colormap(dm)
375 dmode_t		*dm;
376 {
377 	int		nentries, i;
378 	colormap_t	*cm;
379 	u_char		type = CM_COLOR;
380 
381 	switch (vm_reg(dm)) {
382 		case RES_STLOW:
383 		case RES_TTMID:
384 			nentries = 16;
385 			break;
386 		case RES_STMID:
387 			nentries = 4;
388 			break;
389 		case RES_STHIGH:
390 			nentries = 2;
391 			break;
392 		case RES_TTLOW:
393 			nentries = 256;
394 			break;
395 		case RES_TTHIGH:
396 			type     = CM_MONO;
397 			nentries = 0;
398 			break;
399 		default:
400 			panic("grf_tt:alloc_colormap: wrong mode!?");
401 	}
402 	if (!atari_realconfig) {
403 		cm = &gra_con_cmap;
404 		cm->entry = gra_con_colors;
405 	}
406 	else {
407 		int size;
408 
409 		size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
410 		cm   = malloc(size, M_DEVBUF, M_NOWAIT);
411 		if (cm == NULL)
412 			return (NULL);
413 		cm->entry = (long *)&cm[1];
414 
415 	}
416 	if ((cm->type = type) == CM_COLOR)
417 		cm->red_mask = cm->green_mask = cm->blue_mask = 0xf;
418 	cm->first = 0;
419 	cm->size  = nentries;
420 
421 	for (i = 0; i < nentries; i++)
422 		cm->entry[i] = gra_def_color16[i % 16];
423 	return (cm);
424 }
425 #endif /* TT_VIDEO */
426