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