xref: /netbsd-src/sys/arch/mac68k/obio/grf_obio.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: grf_obio.c,v 1.52 2005/12/11 12:18:03 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 1998 Scott Reynolds
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. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*
30  * Copyright (c) 1995 Allen Briggs.  All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  * 3. All advertising materials mentioning features or use of this software
41  *    must display the following acknowledgement:
42  *	This product includes software developed by Allen Briggs.
43  * 4. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 /*
58  * Graphics display driver for the Macintosh internal video for machines
59  * that don't map it into a fake nubus card.
60  */
61 
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: grf_obio.c,v 1.52 2005/12/11 12:18:03 christos Exp $");
64 
65 #include <sys/param.h>
66 #include <sys/device.h>
67 #include <sys/ioctl.h>
68 #include <sys/file.h>
69 #include <sys/malloc.h>
70 #include <sys/mman.h>
71 #include <sys/proc.h>
72 #include <sys/systm.h>
73 
74 #include <machine/autoconf.h>
75 #include <machine/bus.h>
76 #include <machine/cpu.h>
77 #include <machine/grfioctl.h>
78 #include <machine/viareg.h>
79 
80 #include <mac68k/nubus/nubus.h>
81 #include <mac68k/obio/obiovar.h>
82 #include <mac68k/dev/grfvar.h>
83 
84 extern u_int32_t	mac68k_vidphys;
85 extern u_int32_t	mac68k_vidlen;
86 extern long		videoaddr;
87 extern long		videorowbytes;
88 extern long		videobitdepth;
89 extern u_long		videosize;
90 
91 static int	grfiv_mode(struct grf_softc *, int, void *);
92 static int	grfiv_match(struct device *, struct cfdata *, void *);
93 static void	grfiv_attach(struct device *, struct device *, void *);
94 
95 CFATTACH_DECL(intvid, sizeof(struct grfbus_softc),
96     grfiv_match, grfiv_attach, NULL, NULL);
97 
98 #define	DAFB_BASE		0xf9000000
99 #define DAFB_CONTROL_BASE	0xf9800000
100 #define	CIVIC_BASE		0x50100000
101 #define CIVIC_CONTROL_BASE	0x50036000
102 #define	VALKYRIE_BASE		0xf9000000
103 #define VALKYRIE_CONTROL_BASE	0x50f2a000
104 
105 static int
106 grfiv_match(struct device *parent, struct cfdata *cf, void *aux)
107 {
108 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
109 	bus_space_handle_t bsh;
110 	int found;
111 	u_int base;
112 
113 	found = 1;
114 
115 	switch (current_mac_model->class) {
116 	case MACH_CLASSQ2:
117 		if (current_mac_model->machineid != MACH_MACLC575) {
118 			base = VALKYRIE_CONTROL_BASE;
119 
120 			if (bus_space_map(oa->oa_tag, base, 0x40, 0, &bsh))
121 				return 0;
122 
123 			/* Disable interrupts */
124 			bus_space_write_1(oa->oa_tag, bsh, 0x18, 0x1);
125 
126 			bus_space_unmap(oa->oa_tag, bsh, 0x40);
127 			break;
128 		}
129 		/*
130 		 * Note:  the only system in this class that does not have
131 		 * the Valkyrie chip -- at least, that we know of -- is
132 		 * the Performa/LC 57x series.  This system has a version
133 		 * of the DAFB controller, instead.
134 		 *
135 		 * If this assumption proves false, we'll have to be more
136 		 * intelligent here.
137 		 */
138 		/*FALLTHROUGH*/
139 	case MACH_CLASSQ:
140 		/*
141 		 * Assume DAFB for all of these, unless we can't
142 		 * access the memory.
143 		 */
144 		base = DAFB_CONTROL_BASE;
145 
146 		if (bus_space_map(oa->oa_tag, base, 0x20, 0, &bsh))
147 			return 0;
148 
149 		if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0x1c, 4) == 0) {
150 			bus_space_unmap(oa->oa_tag, bsh, 0x20);
151 			return 0;
152 		}
153 
154 		bus_space_unmap(oa->oa_tag, bsh, 0x20);
155 
156 		if (bus_space_map(oa->oa_tag, base + 0x100, 0x20, 0, &bsh))
157 			return 0;
158 
159 		if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0x04, 4) == 0) {
160 			bus_space_unmap(oa->oa_tag, bsh, 0x20);
161 			return 0;
162 		}
163 
164 		/* Disable interrupts */
165 		bus_space_write_4(oa->oa_tag, bsh, 0x04, 0);
166 
167 		/* Clear any interrupts */
168 		bus_space_write_4(oa->oa_tag, bsh, 0x0C, 0);
169 		bus_space_write_4(oa->oa_tag, bsh, 0x10, 0);
170 		bus_space_write_4(oa->oa_tag, bsh, 0x14, 0);
171 
172 		bus_space_unmap(oa->oa_tag, bsh, 0x20);
173 		break;
174 	case MACH_CLASSAV:
175 		base = CIVIC_CONTROL_BASE;
176 
177 		if (bus_space_map(oa->oa_tag, base, 0x1000, 0, &bsh))
178 			return 0;
179 
180 		/* Disable interrupts */
181 		bus_space_write_1(oa->oa_tag, bsh, 0x120, 0);
182 
183 		bus_space_unmap(oa->oa_tag, bsh, 0x1000);
184 		break;
185 	case MACH_CLASSIIci:
186 	case MACH_CLASSIIsi:
187 		if (mac68k_vidlen == 0 ||
188 		    (via2_reg(rMonitor) & RBVMonitorMask) == RBVMonIDNone)
189 			found = 0;
190 		break;
191 	default:
192 		if (mac68k_vidlen == 0)
193 			found = 0;
194 		break;
195 	}
196 
197 	return found;
198 }
199 
200 static void
201 grfiv_attach(struct device *parent, struct device *self, void *aux)
202 {
203 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
204 	struct grfbus_softc *sc;
205 	struct grfmode *gm;
206 	u_long base, length;
207 	u_int32_t vbase1, vbase2;
208 
209 	sc = (struct grfbus_softc *)self;
210 
211 	sc->card_id = 0;
212 
213 	switch (current_mac_model->class) {
214 	case MACH_CLASSQ2:
215 		if (current_mac_model->machineid != MACH_MACLC575) {
216 			sc->sc_basepa = VALKYRIE_BASE;
217 			length = 0x00100000;		/* 1MB */
218 
219 			if (sc->sc_basepa <= mac68k_vidphys &&
220 			    mac68k_vidphys < (sc->sc_basepa + length)) {
221 				sc->sc_fbofs = mac68k_vidphys - sc->sc_basepa;
222 			} else {
223 				sc->sc_basepa = m68k_trunc_page(mac68k_vidphys);
224 				sc->sc_fbofs = m68k_page_offset(mac68k_vidphys);
225 				length = mac68k_vidlen + sc->sc_fbofs;
226 			}
227 
228 			printf(" @ %lx: Valkyrie video subsystem\n",
229 			    sc->sc_basepa + sc->sc_fbofs);
230 			break;
231 		}
232 		/* See note in grfiv_match() */
233 		/*FALLTHROUGH*/
234 	case MACH_CLASSQ:
235 		base = DAFB_CONTROL_BASE;
236 		sc->sc_tag = oa->oa_tag;
237 		if (bus_space_map(sc->sc_tag, base, 0x20, 0, &sc->sc_regh)) {
238 			printf(": failed to map DAFB register space\n");
239 			return;
240 		}
241 
242 		sc->sc_basepa = DAFB_BASE;
243 		length = 0x00100000;		/* 1MB */
244 
245 		/* Compute the current frame buffer offset */
246 		vbase1 = bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x0) & 0xfff;
247 #if 1
248 		/*
249 		 * XXX The following exists because the DAFB v7 in these
250 		 * systems doesn't return reasonable values to use for fbofs.
251 		 * Ken'ichi Ishizaka gets credit for this hack.  (sar 19990426)
252 		 * (Does this get us the correct result for _all_ DAFB-
253 		 * equipped systems and monitor combinations?  It seems
254 		 * possible, if not likely...)
255 		 */
256 		switch (current_mac_model->machineid) {
257 		case MACH_MACLC475:
258 		case MACH_MACLC475_33:
259 		case MACH_MACLC575:
260 		case MACH_MACQ605:
261 		case MACH_MACQ605_33:
262 			vbase1 &= 0x3f;
263 			break;
264 		}
265 #endif
266 		vbase2 = bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x4) & 0xf;
267 		sc->sc_fbofs = (vbase1 << 9) | (vbase2 << 5);
268 
269 		printf(" @ %lx: DAFB video subsystem, monitor sense %x\n",
270 		    sc->sc_basepa + sc->sc_fbofs,
271 		    (bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x1c) & 0x7));
272 
273 		bus_space_unmap(sc->sc_tag, sc->sc_regh, 0x20);
274 		break;
275 	case MACH_CLASSAV:
276 		sc->sc_basepa = CIVIC_BASE;
277 		length = 0x00200000;		/* 2MB */
278 		if (mac68k_vidphys >= sc->sc_basepa &&
279 		    mac68k_vidphys < (sc->sc_basepa + length)) {
280 			sc->sc_fbofs = mac68k_vidphys - sc->sc_basepa;
281 		} else {
282 			sc->sc_basepa = m68k_trunc_page(mac68k_vidphys);
283 			sc->sc_fbofs = m68k_page_offset(mac68k_vidphys);
284 			length = mac68k_vidlen + sc->sc_fbofs;
285 		}
286 
287 		printf(" @ %lx: CIVIC video subsystem\n",
288 		    sc->sc_basepa + sc->sc_fbofs);
289 		break;
290 	case MACH_CLASSIIci:
291 	case MACH_CLASSIIsi:
292 		sc->sc_basepa = m68k_trunc_page(mac68k_vidphys);
293 		sc->sc_fbofs = m68k_page_offset(mac68k_vidphys);
294 		length = mac68k_vidlen + sc->sc_fbofs;
295 
296 		printf(" @ %lx: RBV video subsystem, ",
297 		    sc->sc_basepa + sc->sc_fbofs);
298 		switch (via2_reg(rMonitor) & RBVMonitorMask) {
299 		case RBVMonIDBWP:
300 			printf("15\" monochrome portrait");
301 			break;
302 		case RBVMonIDRGB12:
303 			printf("12\" color");
304 			break;
305 		case RBVMonIDRGB15:
306 			printf("15\" color");
307 			break;
308 		case RBVMonIDStd:
309 			printf("Macintosh II");
310 			break;
311 		default:
312 			printf("unrecognized");
313 			break;
314 		}
315 		printf(" display\n");
316 
317 		break;
318 	default:
319 		sc->sc_basepa = m68k_trunc_page(mac68k_vidphys);
320 		sc->sc_fbofs = m68k_page_offset(mac68k_vidphys);
321 		length = mac68k_vidlen + sc->sc_fbofs;
322 
323 		printf(" @ %lx: On-board video\n",
324 		    sc->sc_basepa + sc->sc_fbofs);
325 		break;
326 	}
327 
328 	if (bus_space_map(sc->sc_tag, sc->sc_basepa, length, 0,
329 	    &sc->sc_handle)) {
330 		printf("%s: failed to map video RAM\n", sc->sc_dev.dv_xname);
331 		return;
332 	}
333 
334 	if (sc->sc_basepa <= mac68k_vidphys &&
335 	    mac68k_vidphys < (sc->sc_basepa + length))
336 		videoaddr = sc->sc_handle.base + sc->sc_fbofs; /* XXX hack */
337 
338 	gm = &(sc->curr_mode);
339 	gm->mode_id = 0;
340 	gm->psize = videobitdepth;
341 	gm->ptype = 0;
342 	gm->width = videosize & 0xffff;
343 	gm->height = (videosize >> 16) & 0xffff;
344 	gm->rowbytes = videorowbytes;
345 	gm->hres = 80;				/* XXX hack */
346 	gm->vres = 80;				/* XXX hack */
347 	gm->fbsize = gm->height * gm->rowbytes;
348 	gm->fbbase = (caddr_t)sc->sc_handle.base; /* XXX yet another hack */
349 	gm->fboff = sc->sc_fbofs;
350 
351 	/* Perform common video attachment. */
352 	grf_establish(sc, NULL, grfiv_mode);
353 }
354 
355 static int
356 grfiv_mode(struct grf_softc *sc, int cmd, void *arg)
357 {
358 	switch (cmd) {
359 	case GM_GRFON:
360 	case GM_GRFOFF:
361 		return 0;
362 	case GM_CURRMODE:
363 		break;
364 	case GM_NEWMODE:
365 		break;
366 	case GM_LISTMODES:
367 		break;
368 	}
369 	return EINVAL;
370 }
371