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