xref: /netbsd/sys/arch/sun3/dev/bw2.c (revision c4a72b64)
1 /*	$NetBSD: bw2.c,v 1.23 2002/10/23 09:12:23 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratory.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)bwtwo.c	8.1 (Berkeley) 6/11/93
45  */
46 
47 /*
48  * black&white display (bw2) driver.
49  *
50  * Does not handle interrupts, even though they can occur.
51  */
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/conf.h>
56 #include <sys/device.h>
57 #include <sys/ioctl.h>
58 #include <sys/malloc.h>
59 #include <sys/mman.h>
60 #include <sys/proc.h>
61 #include <sys/tty.h>
62 
63 #include <uvm/uvm_extern.h>
64 
65 #include <machine/autoconf.h>
66 #include <machine/cpu.h>
67 #include <dev/sun/fbio.h>
68 #include <machine/idprom.h>
69 #include <machine/pmap.h>
70 
71 #include <sun3/dev/fbvar.h>
72 #include <sun3/dev/bw2reg.h>
73 #include <sun3/dev/p4reg.h>
74 
75 /* per-display variables */
76 struct bw2_softc {
77 	struct	device sc_dev;		/* base device */
78 	struct	fbdevice sc_fb;		/* frame buffer device */
79 	int 	sc_phys;		/* display RAM (phys addr) */
80 	/* If using overlay plane of something, it is... */
81 	int	sc_ovtype;		/* ... this type. */
82 	int sc_video_on;
83 };
84 
85 /* autoconfiguration driver */
86 static void	bw2attach __P((struct device *, struct device *, void *));
87 static int	bw2match __P((struct device *, struct cfdata *, void *));
88 
89 CFATTACH_DECL(bwtwo, sizeof(struct bw2_softc),
90     bw2match, bw2attach, NULL, NULL);
91 
92 extern struct cfdriver bwtwo_cd;
93 
94 dev_type_open(bw2open);
95 dev_type_ioctl(bw2ioctl);
96 dev_type_mmap(bw2mmap);
97 
98 const struct cdevsw bwtwo_cdevsw = {
99 	bw2open, nullclose, noread, nowrite, bw2ioctl,
100 	nostop, notty, nopoll, bw2mmap, nokqfilter,
101 };
102 
103 /* XXX we do not handle frame buffer interrupts */
104 
105 static int bw2gvideo __P((struct fbdevice *, void *));
106 static int bw2svideo __P((struct fbdevice *, void *));
107 
108 static struct fbdriver bw2fbdriver = {
109 	bw2open, nullclose, bw2mmap, nokqfilter,
110 	fb_noioctl,
111 	bw2gvideo, bw2svideo,
112 	fb_noioctl, fb_noioctl, };
113 
114 static int
115 bw2match(parent, cf, args)
116 	struct device *parent;
117 	struct cfdata *cf;
118 	void *args;
119 {
120 	struct confargs *ca = args;
121 	int mid, p4id, peekval;
122 	void *p4reg;
123 
124 	/* No default address support. */
125 	if (ca->ca_paddr == -1)
126 		return (0);
127 
128 	/*
129 	 * Slight hack here:  The low four bits of the
130 	 * config flags, if set, restrict the match to
131 	 * that machine "implementation" only.
132 	 */
133 	mid = cf->cf_flags & IDM_IMPL_MASK;
134 	if (mid && (mid != (cpu_machine_id & IDM_IMPL_MASK)))
135 		return (0);
136 
137 	/*
138 	 * Make sure something is there, and if so,
139 	 * see if it looks like a P4 register.
140 	 */
141 	p4reg = bus_tmapin(ca->ca_bustype, ca->ca_paddr);
142 	peekval = peek_long(p4reg);
143 	p4id = (peekval == -1) ?
144 		P4_NOTFOUND : fb_pfour_id(p4reg);
145 	bus_tmapout(p4reg);
146 	if (peekval == -1)
147 		return (0);
148 
149 	/*
150 	 * The config flag 0x40 if set means we should match
151 	 * only on a CG? overlay plane.  We can use only the
152 	 * CG4 and CG8, which both have a P4 register.
153 	 */
154 	if (cf->cf_flags & 0x40) {
155 		switch (p4id) {
156 		case P4_ID_COLOR8P1:
157 		case P4_ID_COLOR24:
158 			return (1);
159 		case P4_NOTFOUND:
160 		default:
161 			return (0);
162 		}
163 	}
164 
165 	/*
166 	 * OK, we are expecting a plain old BW2, and
167 	 * there may or may not be a P4 register.
168 	 */
169 	switch (p4id) {
170 	case P4_ID_BW:
171 	case P4_NOTFOUND:
172 		return (1);
173 	default:
174 #ifdef	DEBUG
175 		printf("bwtwo at 0x%x match p4id=0x%x fails\n",
176 			   ca->ca_paddr, p4id & 0xFF);
177 #endif
178 		break;
179 	}
180 
181 	return (0);
182 }
183 
184 /*
185  * Attach a display.  We need to notice if it is the console, too.
186  */
187 static void
188 bw2attach(parent, self, args)
189 	struct device *parent, *self;
190 	void *args;
191 {
192 	struct bw2_softc *sc = (struct bw2_softc *)self;
193 	struct fbdevice *fb = &sc->sc_fb;
194 	struct confargs *ca = args;
195 	struct fbtype *fbt;
196 	void *p4reg;
197 	int p4id, tmp;
198 	int pixeloffset;		/* offset to framebuffer */
199 
200 	fbt = &fb->fb_fbtype;
201 	fbt->fb_type = FBTYPE_SUN2BW;
202 	fbt->fb_width = 1152;	/* default - see below */
203 	fbt->fb_height = 900;	/* default - see below */
204 	fbt->fb_depth = 1;
205 	fbt->fb_cmsize = 0;
206 	fbt->fb_size = BW2_FBSIZE;	/* default - see below */
207 	fb->fb_driver = &bw2fbdriver;
208 	fb->fb_private = sc;
209 	fb->fb_name  = sc->sc_dev.dv_xname;
210 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
211 
212 	/* Set up default pixel offset.  May be changed below. */
213 	pixeloffset = 0;
214 
215 	/* Does it have a P4 register? */
216 	p4reg = bus_mapin(ca->ca_bustype, ca->ca_paddr, 4);
217 	p4id = fb_pfour_id(p4reg);
218 	if (p4id != P4_NOTFOUND)
219 		fb->fb_pfour = p4reg;
220 	else
221 		bus_mapout(p4reg, 4);
222 
223 	switch (p4id) {
224 	case P4_NOTFOUND:
225 		pixeloffset = 0;
226 		break;
227 
228 	case P4_ID_BW:
229 		pixeloffset = P4_BW_OFF;
230 		break;
231 
232 	default:
233 		printf("%s: bad p4id=0x%x\n", fb->fb_name, p4id);
234 		/* Must be some kinda color... */
235 		/* fall through */
236 	case P4_ID_COLOR8P1:
237 	case P4_ID_COLOR24:
238 		sc->sc_ovtype = p4id;
239 		pixeloffset = P4_COLOR_OFF_OVERLAY;
240 		break;
241 	}
242 	sc->sc_phys = ca->ca_paddr + pixeloffset;
243 
244 	/*
245 	 * Determine width and height as follows:
246 	 * If it has a P4 register, use that;
247 	 * else if unit==0, use the EEPROM size,
248 	 * else make our best guess.
249 	 */
250 	if (fb->fb_pfour)
251 		fb_pfour_setsize(fb);
252 	else if (sc->sc_dev.dv_unit == 0)
253 		fb_eeprom_setsize(fb);
254 	else {
255 		/* Guess based on machine ID. */
256 		switch (cpu_machine_id) {
257 #ifdef	_SUN3_
258 		case SUN3_MACH_60:
259 			/*
260 			 * Only the model 60 can have hi-res.
261 			 * Look at the "resolution" jumper.
262 			 */
263 			tmp = bus_peek(BUS_OBMEM, BW2_CR_PADDR, 1);
264 			if ((tmp != -1) && (tmp & 0x80) == 0)
265 				goto high_res;
266 			break;
267 
268 		case SUN3_MACH_260:
269 			/* The Sun3/260 is ALWAYS high-resolution! */
270 			/* fall through */
271 		high_res:
272 			fbt->fb_width = 1600;
273 			fbt->fb_height = 1280;
274 			fbt->fb_size = BW2_FBSIZE_HIRES;
275 			break;
276 #endif	/* SUN3 */
277 
278 		default:
279 			/* Leave the defaults set above. */
280 			break;
281 		}
282 	}
283 	printf(" (%dx%d)\n", fbt->fb_width, fbt->fb_height);
284 
285 	/* Make sure video is on. */
286 	tmp = 1;
287 	bw2svideo(fb, &tmp);
288 
289 	/* Let /dev/fb know we are here. */
290 	fb_attach(fb, 1);
291 }
292 
293 int
294 bw2open(dev, flags, mode, p)
295 	dev_t dev;
296 	int flags, mode;
297 	struct proc *p;
298 {
299 	int unit = minor(dev);
300 
301 	if (unit >= bwtwo_cd.cd_ndevs || bwtwo_cd.cd_devs[unit] == NULL)
302 		return (ENXIO);
303 	return (0);
304 }
305 
306 int
307 bw2ioctl(dev, cmd, data, flags, p)
308 	dev_t dev;
309 	u_long cmd;
310 	caddr_t data;
311 	int flags;
312 	struct proc *p;
313 {
314 	struct bw2_softc *sc = bwtwo_cd.cd_devs[minor(dev)];
315 
316 	return (fbioctlfb(&sc->sc_fb, cmd, data));
317 }
318 
319 /*
320  * Return the address that would map the given device at the given
321  * offset, allowing for the given protection, or return -1 for error.
322  */
323 paddr_t
324 bw2mmap(dev, off, prot)
325 	dev_t dev;
326 	off_t off;
327 	int prot;
328 {
329 	struct bw2_softc *sc = bwtwo_cd.cd_devs[minor(dev)];
330 	int size = sc->sc_fb.fb_fbtype.fb_size;
331 
332 	if (off & PGOFSET)
333 		panic("bw2mmap");
334 
335 	if ((off < 0) || (off >= size))
336 		return (-1);
337 
338 	/*
339 	 * I turned on PMAP_NC here to disable the cache as I was
340 	 * getting horribly broken behaviour without it.
341 	 */
342 	return ((sc->sc_phys + off) | PMAP_NC);
343 }
344 
345 /* FBIOGVIDEO: */
346 static int bw2gvideo(fb, data)
347 	struct fbdevice *fb;
348 	void *data;
349 {
350 	struct bw2_softc *sc = fb->fb_private;
351 	int *on = data;
352 
353 	*on = sc->sc_video_on;
354 	return (0);
355 }
356 
357 /* FBIOSVIDEO: */
358 static int bw2svideo(fb, data)
359 	struct fbdevice *fb;
360 	void *data;
361 {
362 	struct bw2_softc *sc = fb->fb_private;
363 	int *on = data;
364 
365 	if (sc->sc_video_on == *on)
366 		return (0);
367 	sc->sc_video_on = *on;
368 
369 	if (fb->fb_pfour)
370 		fb_pfour_set_video(fb, sc->sc_video_on);
371 	else
372 		enable_video(sc->sc_video_on);
373 
374 	return(0);
375 }
376 
377