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