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