1/*	$NetBSD: grfabs_tt.c,v 1.25 2023/01/06 10:28:28 tsutsui 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: grfabs_tt.c,v 1.25 2023/01/06 10:28:28 tsutsui Exp $");
30
31#ifdef TT_VIDEO
32/*
33 *  atari abstract graphics driver: TT-interface
34 */
35#include <sys/param.h>
36#include <sys/queue.h>
37#include <sys/malloc.h>
38#include <sys/device.h>
39#include <sys/systm.h>
40
41#include <uvm/uvm_extern.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 */
54static void       init_view(view_t *, bmap_t *, dmode_t *, box_t *);
55static bmap_t	  *alloc_bitmap(u_long, u_long, u_char);
56static colormap_t *alloc_colormap(dmode_t *);
57static void	  free_bitmap(bmap_t *);
58static void	  tt_display_view(view_t *);
59static view_t	  *tt_alloc_view(dmode_t *, dimen_t *, u_char);
60static void	  tt_free_view(view_t *);
61static void	  tt_remove_view(view_t *);
62static void	  tt_save_view(view_t *);
63static int	  tt_use_colormap(view_t *, colormap_t *);
64
65/*
66 * Our function switch table
67 */
68struct 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
77struct 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 */
86static 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
95static 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
110void
111tt_probe_video(MODES *modelp)
112{
113	dmode_t	*dm;
114	int	i;
115	int	has_mono;
116
117	/*
118	 * First find out what kind of monitor is attached. DMA-sound
119	 * should be off because the 'sound-done' and 'monochrome-detect'
120	 * are xor-ed together. I think that shutting it down here is the
121	 * wrong place.
122	 */
123	has_mono = (MFP->mf_gpip & IA_MONO) == 0;
124
125	for (i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
126		dm->data = (void *)&tt_hwregs[i];
127		if (has_mono && (vm_reg(dm) != RES_TTHIGH))
128			continue;
129		if (!has_mono && (vm_reg(dm) == RES_TTHIGH))
130			continue;
131		LIST_INSERT_HEAD(modelp, dm, link);
132	}
133
134	for (i=0; i < 16; i++)
135		VIDEO->vd_tt_rgb[i] = CM_L2TT(gra_def_color16[i]);
136}
137
138static void
139tt_display_view(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
162void
163tt_remove_view(view_t *v)
164{
165	dmode_t *mode = v->mode;
166
167	if (mode->current_view == v) {
168#if 0
169		if (v->flags & VF_DISPLAY)
170			panic("Cannot shutdown display"); /* XXX */
171#endif
172		mode->current_view = NULL;
173	}
174	v->flags &= ~VF_DISPLAY;
175}
176
177void
178tt_save_view(view_t *v)
179{
180}
181
182void
183tt_free_view(view_t *v)
184{
185
186	if (v) {
187		tt_remove_view(v);
188		if (v->colormap != &gra_con_cmap)
189			free(v->colormap, M_DEVBUF);
190		free_bitmap(v->bitmap);
191		if (v != &gra_con_view)
192			free(v, M_DEVBUF);
193	}
194}
195
196static int
197tt_use_colormap(view_t *v, colormap_t *cm)
198{
199	dmode_t			*dm;
200	volatile u_short	*creg;
201	u_long			*src;
202	colormap_t		*vcm;
203	u_long			*vcreg;
204	u_short			ncreg;
205	int			i;
206
207	dm  = v->mode;
208	vcm = v->colormap;
209
210	/*
211	 * I guess it seems reasonable to require the maps to be
212	 * of the same type...
213	 */
214	if (cm->type != vcm->type)
215		return(EINVAL);
216
217	/*
218	 * First figure out where the actual colormap resides and
219	 * howmany colors are in it.
220	 */
221	switch (vm_reg(dm)) {
222		case RES_STLOW:
223			creg  = &VIDEO->vd_tt_rgb[0];
224			ncreg = 16;
225			break;
226		case RES_STMID:
227			creg  = &VIDEO->vd_tt_rgb[0];
228			ncreg = 4;
229			break;
230		case RES_STHIGH:
231			creg  = &VIDEO->vd_tt_rgb[254];
232			ncreg = 2;
233			break;
234		case RES_TTLOW:
235			creg  = &VIDEO->vd_tt_rgb[0];
236			ncreg = 256;
237			break;
238		case RES_TTMID:
239			creg  = &VIDEO->vd_tt_rgb[0];
240			ncreg = 16;
241			break;
242		case RES_TTHIGH:
243			return(0);	/* No colors	*/
244		default:
245			panic("grf_tt:use_colormap: wrong mode!?");
246	}
247
248	/* If first entry specified beyond capabilities -> error */
249	if (cm->first >= ncreg)
250		return(EINVAL);
251
252	/*
253	 * A little tricky, the actual colormap pointer will be NULL
254	 * when view is not displaying, valid otherwise.
255	 */
256	if (v->flags & VF_DISPLAY)
257		creg = &creg[cm->first];
258	else
259		creg = NULL;
260
261	vcreg  = &vcm->entry[cm->first];
262	ncreg -= cm->first;
263	if (cm->size > ncreg)
264		return(EINVAL);
265	ncreg = cm->size;
266
267	for (i = 0, src = cm->entry; i < ncreg; i++, vcreg++) {
268		*vcreg = *src++;
269
270		/*
271		 * If displaying, also update actual color registers.
272		 */
273		if (creg != NULL)
274			*creg++ = CM_L2TT(*vcreg);
275	}
276	return (0);
277}
278
279static view_t *
280tt_alloc_view(dmode_t *mode, dimen_t *dim, u_char depth)
281{
282	view_t *v;
283	bmap_t *bm;
284
285	if (!atari_realconfig)
286		v = &gra_con_view;
287	else
288		v = malloc(sizeof(*v), M_DEVBUF, M_NOWAIT);
289	if (v == NULL)
290		return (NULL);
291
292	bm = alloc_bitmap(mode->size.width, mode->size.height, mode->depth);
293	if (bm) {
294		box_t   box;
295
296		v->colormap = alloc_colormap(mode);
297		if (v->colormap) {
298			INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
299			init_view(v, bm, mode, &box);
300			return (v);
301		}
302		free_bitmap(bm);
303	}
304	if (v != &gra_con_view)
305		free(v, M_DEVBUF);
306	return (NULL);
307}
308
309static void
310init_view(view_t *v, bmap_t *bm, dmode_t *mode, box_t *dbox)
311{
312	v->bitmap = bm;
313	v->mode   = mode;
314	v->flags  = 0;
315	memcpy(&v->display, dbox, sizeof(box_t));
316}
317
318/* bitmap functions */
319
320static bmap_t *
321alloc_bitmap(u_long width, u_long height, u_char depth)
322{
323	u_long  total_size, bm_size;
324	void	*hw_address;
325	bmap_t	*bm;
326
327	/*
328	 * Sigh, it seems for mapping to work we need the bitplane data to
329	 *  1: be aligned on a page boundary.
330	 *  2: be n pages large.
331	 *
332	 * why? because the user gets a page aligned address, if this is before
333	 * your allocation, too bad.  Also it seems that the mapping routines
334	 * do not watch to closely to the allowable length. so if you go over
335	 * n pages by less than another page, the user gets to write all over
336	 * the entire page. Since you did not allocate up to a page boundary
337	 * (or more) the user writes into someone elses memory. -ch
338	 */
339	bm_size    = m68k_round_page((width * height * depth) / NBBY);
340	total_size = bm_size + sizeof(bmap_t) + PAGE_SIZE;
341
342	if ((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
343		return(NULL);
344
345	bm->plane         = (u_char*)bm + sizeof(bmap_t);
346	bm->plane         = (u_char*)m68k_round_page(bm->plane);
347	bm->hw_address    = (u_char*)hw_address + sizeof(bmap_t);
348	bm->hw_address    = (u_char*)m68k_round_page(bm->hw_address);
349	bm->bytes_per_row = (width * depth) / NBBY;
350	bm->rows          = height;
351	bm->depth         = depth;
352	bm->regs          = NULL;
353	bm->hw_regs       = NULL;
354	bm->reg_size      = 0;
355	bm->phys_mappable = (depth * width * height) / NBBY;
356	bm->lin_base      = 0;
357	bm->vga_address   = NULL;
358	bm->vga_mappable  = 0;
359	bm->vga_base      = 0;
360
361	memset(bm->plane, 0, bm_size);
362	return (bm);
363}
364
365static void
366free_bitmap(bmap_t *bm)
367{
368	if (bm)
369		free_stmem(bm);
370}
371
372static colormap_t *
373alloc_colormap(dmode_t *dm)
374{
375	int		nentries, i;
376	colormap_t	*cm;
377	u_char		type = CM_COLOR;
378
379	switch (vm_reg(dm)) {
380		case RES_STLOW:
381		case RES_TTMID:
382			nentries = 16;
383			break;
384		case RES_STMID:
385			nentries = 4;
386			break;
387		case RES_STHIGH:
388			nentries = 2;
389			break;
390		case RES_TTLOW:
391			nentries = 256;
392			break;
393		case RES_TTHIGH:
394			type     = CM_MONO;
395			nentries = 0;
396			break;
397		default:
398			panic("grf_tt:alloc_colormap: wrong mode!?");
399	}
400	if (!atari_realconfig) {
401		cm = &gra_con_cmap;
402		cm->entry = gra_con_colors;
403	} else {
404		int size;
405
406		size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
407		cm   = malloc(size, M_DEVBUF, M_NOWAIT);
408		if (cm == NULL)
409			return (NULL);
410		cm->entry = (long *)&cm[1];
411
412	}
413	if ((cm->type = type) == CM_COLOR)
414		cm->red_mask = cm->green_mask = cm->blue_mask = 0xf;
415	cm->first = 0;
416	cm->size  = nentries;
417
418	for (i = 0; i < nentries; i++)
419		cm->entry[i] = gra_def_color16[i % 16];
420	return (cm);
421}
422#endif /* TT_VIDEO */
423