1/*	$NetBSD: grf_cc.c,v 1.44 2022/03/28 12:38:57 riastradh Exp $ */
2
3/*
4 * Copyright (c) 1994 Christian E. Hopps
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 Christian E. Hopps.
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: grf_cc.c,v 1.44 2022/03/28 12:38:57 riastradh Exp $");
35
36#include "grfcc.h"
37#include "ite.h"
38#if NGRFCC > 0
39/*
40 * currently this is a backward compat hack that interface to
41 * view.c
42 */
43
44#include <sys/param.h>
45#include <sys/proc.h>
46#include <sys/errno.h>
47#include <sys/ioctl.h>
48#include <sys/queue.h>
49#include <sys/device.h>
50#include <sys/device_impl.h>	/* XXX autoconf abuse */
51#include <sys/systm.h>
52#include <sys/conf.h>
53#include <machine/cpu.h>
54#include <amiga/amiga/color.h>	/* DEBUG */
55#include <amiga/amiga/device.h>
56#include <amiga/amiga/custom.h>
57#include <amiga/amiga/cia.h>
58#include <amiga/dev/grfioctl.h>
59#include <amiga/dev/grfvar.h>
60#include <amiga/dev/grf_ccreg.h>
61#include <amiga/dev/grfabs_reg.h>
62#include <amiga/dev/viewioctl.h>
63
64#include "view.h"
65
66int grfccmatch(device_t, cfdata_t, void *);
67int grfccprint(void *, const char *);
68void grfccattach(device_t, device_t, void *);
69void grf_cc_on(struct grf_softc *);
70
71CFATTACH_DECL_NEW(grfcc, sizeof(struct grf_softc),
72    grfccmatch, grfccattach, NULL, NULL);
73
74/*
75 * only used in console init
76 */
77static cfdata_t cfdata;
78
79/*
80 * we make sure to only init things once.  this is somewhat
81 * tricky regarding the console.
82 */
83int
84grfccmatch(device_t parent, cfdata_t cf, void *aux)
85{
86	static int ccconunit = -1;
87	char *mainbus_name = aux;
88	extern const struct cdevsw view_cdevsw;
89
90	/*
91	 * allow only one cc console
92	 */
93	if (amiga_realconfig == 0 && ccconunit != -1)
94		return(0);
95	if (matchname("grfcc", mainbus_name) == 0)
96		return(0);
97	if (amiga_realconfig == 0 || ccconunit != cf->cf_unit) {
98		if (grfcc_probe() == 0)
99			return(0);
100		viewprobe();
101		/*
102		 * XXX nasty hack. opens view[0] and never closes.
103		 */
104		if ((*view_cdevsw.d_open)(0, 0, 0, NULL))
105			return(0);
106		if (amiga_realconfig == 0) {
107			ccconunit = cf->cf_unit;
108			cfdata = cf;
109		}
110	}
111	return(1);
112}
113
114/*
115 * attach to the grfbus (mainbus)
116 */
117void
118grfccattach(device_t parent, device_t self, void *aux)
119{
120	static struct grf_softc congrf;
121	struct device temp;
122	struct grf_softc *gp;
123
124	if (self == NULL) {
125		gp = &congrf;
126		gp->g_device = &temp;
127		temp.dv_private = gp;
128	} else {
129		gp = device_private(self);
130		gp->g_device = self;
131	}
132
133	if (self != NULL && congrf.g_regkva != 0) {
134		/*
135		 * we inited earlier just copy the info
136		 * take care not to copy the device struct though.
137		 */
138		memcpy(&gp->g_display, &congrf.g_display,
139		    (char *)&gp[1] - (char *)&gp->g_display);
140	} else {
141		gp->g_unit = GRF_CC_UNIT;
142		gp->g_flags = GF_ALIVE;
143		gp->g_mode = cc_mode;
144#if NITE > 0
145		gp->g_conpri = grfcc_cnprobe();
146		grfcc_iteinit(gp);
147#endif
148		grf_cc_on(gp);
149	}
150	if (self != NULL)
151		printf("\n");
152	/*
153	 * attach grf
154	 */
155	amiga_config_found(cfdata, gp->g_device, gp, grfccprint, CFARGS_NONE);
156}
157
158int
159grfccprint(void *aux, const char *pnp)
160{
161	if (pnp)
162		aprint_normal("grf%d at %s", ((struct grf_softc *)aux)->g_unit,
163			pnp);
164	return(UNCONF);
165}
166
167/*
168 * Change the mode of the display.
169 * Right now all we can do is grfon/grfoff.
170 * Return a UNIX error number or 0 for success.
171 */
172/*ARGSUSED*/
173int
174cc_mode(struct grf_softc *gp, u_long cmd, void *arg, u_long a2, int a3)
175{
176	extern const struct cdevsw view_cdevsw;
177
178	switch (cmd) {
179	case GM_GRFON:
180		grf_cc_on(gp);
181		return(0);
182	case GM_GRFOFF:
183		(*view_cdevsw.d_ioctl)(0, VIOCREMOVE, NULL, -1, NULL);
184		return(0);
185	case GM_GRFCONFIG:
186	default:
187		break;
188	}
189	return(EPASSTHROUGH);
190}
191
192void
193grf_cc_on(struct grf_softc *gp)
194{
195	struct view_size vs;
196	bmap_t bm;
197	struct grfinfo *gi;
198	extern const struct cdevsw view_cdevsw;
199
200	gi = &gp->g_display;
201
202	/* XXX type of bm ? */
203	(*view_cdevsw.d_ioctl)(0, VIOCGBMAP, (void *)&bm, -1, NULL);
204
205	gp->g_data = (void *) 0xDeadBeaf; /* not particularly clean.. */
206
207	gi->gd_regaddr = (void *) 0xdff000;	/* depricated */
208	gi->gd_regsize = round_page(sizeof (custom));
209	gi->gd_fbaddr  = bm.hardware_address;
210	gi->gd_fbsize  = bm.depth*bm.bytes_per_row*bm.rows;
211
212	if ((*view_cdevsw.d_ioctl)(0, VIOCGSIZE, (void *)&vs, -1, NULL)) {
213		/* XXX type of vs ? */
214		/* fill in some default values... XXX */
215		vs.width = 640;
216		vs.height = 400;
217		vs.depth = 2;
218	}
219	gi->gd_colors = 1 << vs.depth;
220	gi->gd_planes = vs.depth;
221
222	gi->gd_fbwidth = vs.width;
223	gi->gd_fbheight = vs.height;
224	gi->gd_fbx = 0;
225	gi->gd_fby = 0;
226	gi->gd_dwidth = vs.width;
227	gi->gd_dheight = vs.height;
228	gi->gd_dx = 0;
229	gi->gd_dy = 0;
230
231	gp->g_regkva = (void *)0xDeadBeaf;	/* builtin */
232	gp->g_fbkva = NULL;		/* not needed, view internal */
233
234	(*view_cdevsw.d_ioctl)(0, VIOCDISPLAY, NULL, -1, NULL);
235}
236#endif
237