xref: /openbsd-src/sys/dev/sbus/cgtwelve.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: cgtwelve.c,v 1.6 2008/12/27 17:23:03 miod Exp $	*/
2 
3 /*
4  * Copyright (c) 2002, 2003 Miodrag Vallat.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * cgtwelve (GS) accelerated 24-bit framebuffer driver.
30  *
31  * Enough experiments and SMI's cg12reg.h made this possible.
32  */
33 
34 /*
35  * The cgtwelve framebuffer is a 3-slot SBUS card, that will fit only in
36  * SPARCstation 1, 1+, 2 and 5, or in an xbox SBUS extension.
37  *
38  * It is a 24-bit 3D accelerated framebuffer made by Matrox, featuring 4MB
39  * (regular model) or 8MB (high-res model) of video memory, a complex
40  * windowing engine, double buffering modes, three video planes (overlay,
41  * 8 bit and 24 bit color), and a lot of colormap combinations.
42  *
43  * All of this is driven by a set of three Bt462 ramdacs (latched unless
44  * explicitely programmed), and a couple of other Matrox-specific chips.
45  *
46  * XXX The high res card is untested.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/buf.h>
52 #include <sys/device.h>
53 #include <sys/ioctl.h>
54 #include <sys/conf.h>
55 
56 #include <uvm/uvm_extern.h>
57 
58 #include <machine/autoconf.h>
59 #include <machine/bus.h>
60 #include <machine/pmap.h>
61 #include <machine/cpu.h>
62 #include <machine/conf.h>
63 
64 #include <dev/wscons/wsconsio.h>
65 #include <dev/wscons/wsdisplayvar.h>
66 #include <dev/rasops/rasops.h>
67 #include <machine/fbvar.h>
68 
69 #include <dev/sbus/sbusvar.h>
70 
71 #include <dev/sbus/cgtwelvereg.h>
72 
73 #include <dev/cons.h>	/* for prom console hook */
74 
75 /* per-display variables */
76 struct cgtwelve_softc {
77 	struct	sunfb	sc_sunfb;	/* common base device */
78 	bus_space_tag_t	sc_bustag;
79 	bus_addr_t	sc_paddr;
80 
81 	volatile struct cgtwelve_dpu *sc_dpu;
82 	volatile struct cgtwelve_apu *sc_apu;
83 	volatile struct cgtwelve_dac *sc_ramdac;	/* RAMDAC registers */
84 	volatile u_char *sc_overlay;	/* overlay or enable plane */
85 	volatile u_long *sc_inten;	/* true color plane */
86 
87 	int	sc_highres;
88 	int	sc_nscreens;
89 };
90 
91 int	cgtwelve_ioctl(void *, u_long, caddr_t, int, struct proc *);
92 paddr_t	cgtwelve_mmap(void *, off_t, int);
93 void	cgtwelve_reset(struct cgtwelve_softc *, int);
94 void	cgtwelve_prom(void *);
95 
96 static __inline__ void cgtwelve_ramdac_wraddr(struct cgtwelve_softc *sc,
97 	    u_int32_t addr);
98 
99 struct wsdisplay_accessops cgtwelve_accessops = {
100 	cgtwelve_ioctl,
101 	cgtwelve_mmap,
102 	NULL,	/* alloc_screen */
103 	NULL,	/* free_screen */
104 	NULL,	/* show_screen */
105 	NULL,	/* load_font */
106 	NULL,	/* scrollback */
107 	NULL,	/* getchar */
108 	NULL	/* burner */
109 };
110 
111 int	cgtwelvematch(struct device *, void *, void *);
112 void	cgtwelveattach(struct device *, struct device *, void *);
113 
114 struct cfattach cgtwelve_ca = {
115 	sizeof(struct cgtwelve_softc), cgtwelvematch, cgtwelveattach
116 };
117 
118 struct cfdriver cgtwelve_cd = {
119 	NULL, "cgtwelve", DV_DULL
120 };
121 
122 
123 /*
124  * Match a cgtwelve.
125  */
126 int
127 cgtwelvematch(struct device *parent, void *vcf, void *aux)
128 {
129 	struct cfdata *cf = vcf;
130 	struct sbus_attach_args *sa = aux;
131 
132 	if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
133 		return (0);
134 
135 	return (1);
136 }
137 
138 /*
139  * Attach and initialize a cgtwelve.
140  */
141 void
142 cgtwelveattach(struct device *parent, struct device *self, void *args)
143 {
144 	struct cgtwelve_softc *sc = (struct cgtwelve_softc *)self;
145 	struct sbus_attach_args *sa = args;
146 	bus_space_tag_t bt;
147 	bus_space_handle_t bh;
148 	int node, isconsole = 0;
149 	char *ps;
150 
151 	bt = sa->sa_bustag;
152 	node = sa->sa_node;
153 
154 	printf(": %s", getpropstring(node, "model"));
155 	ps = getpropstring(node, "dev_id");
156 	if (*ps != '\0')
157 		printf(" (%s)", ps);
158 	printf("\n");
159 
160 	isconsole = node == fbnode;
161 
162 	if (sa->sa_nreg == 0) {
163 		printf("%s: no SBus registers!\n", self->dv_xname);
164 		return;
165 	}
166 
167 	sc->sc_bustag = bt;
168 
169 	/*
170 	 * Map registers
171 	 */
172 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset +
173 	    CG12_OFF_DPU, sizeof(struct cgtwelve_dpu),
174 	    BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
175 		printf("%s: can't map DPU registers\n", self->dv_xname);
176 		return;
177 	}
178 	sc->sc_dpu = bus_space_vaddr(bt, bh);
179 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset +
180 	    CG12_OFF_APU, sizeof(struct cgtwelve_apu),
181 	    BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
182 		printf("%s: can't map APU registers\n", self->dv_xname);
183 		return;
184 	}
185 	sc->sc_apu = bus_space_vaddr(bt, bh);
186 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset +
187 	    CG12_OFF_DAC, sizeof(struct cgtwelve_dac),
188 	    BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
189 		printf("%s: can't map RAMDAC registers\n", self->dv_xname);
190 		return;
191 	}
192 	sc->sc_ramdac = bus_space_vaddr(bt, bh);
193 
194 	/*
195 	 * The console is using the 1-bit overlay plane, while the prom
196 	 * will correctly report 32 bit depth.
197 	 */
198 	fb_setsize(&sc->sc_sunfb, 1, CG12_WIDTH, CG12_HEIGHT,
199 	    node, 0);
200 	sc->sc_sunfb.sf_depth = 1;
201 	sc->sc_sunfb.sf_linebytes = sc->sc_sunfb.sf_width / 8;
202 	sc->sc_sunfb.sf_fbsize = sc->sc_sunfb.sf_height *
203 	    sc->sc_sunfb.sf_linebytes;
204 
205 	sc->sc_highres = sc->sc_sunfb.sf_width == CG12_WIDTH_HR;
206 
207 	/*
208 	 * Map planes
209 	 */
210 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset +
211 	    (sc->sc_highres ? CG12_OFF_OVERLAY0_HR : CG12_OFF_OVERLAY0),
212 	    round_page(sc->sc_highres ? CG12_SIZE_OVERLAY_HR :
213 	        CG12_SIZE_OVERLAY), BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
214 		printf("%s: can't map overlay plane\n", self->dv_xname);
215 		return;
216 	}
217 	sc->sc_overlay = bus_space_vaddr(bt, bh);
218 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset +
219 	    (sc->sc_highres ? CG12_OFF_INTEN_HR : CG12_OFF_INTEN),
220 	    round_page(sc->sc_highres ? CG12_SIZE_COLOR24_HR :
221 	        CG12_SIZE_COLOR24), BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
222 		printf("%s: can't map color plane\n", self->dv_xname);
223 		return;
224 	}
225 	sc->sc_inten = bus_space_vaddr(bt, bh);
226 	sc->sc_paddr = sbus_bus_addr(bt, sa->sa_slot, sa->sa_offset +
227 	    (sc->sc_highres ? CG12_OFF_INTEN_HR : CG12_OFF_INTEN));
228 
229 	/* reset cursor & frame buffer controls */
230 	sc->sc_sunfb.sf_depth = 0;	/* force action */
231 	cgtwelve_reset(sc, 1);
232 
233 	sc->sc_sunfb.sf_ro.ri_bits = (void *)sc->sc_overlay;
234 	sc->sc_sunfb.sf_ro.ri_hw = sc;
235 	fbwscons_init(&sc->sc_sunfb, 0, isconsole);
236 
237 	if (isconsole) {
238 		fbwscons_console_init(&sc->sc_sunfb, -1);
239 		shutdownhook_establish(cgtwelve_prom, sc);
240 	}
241 
242 	printf("%s: %dx%d", self->dv_xname,
243 	    sc->sc_sunfb.sf_width, sc->sc_sunfb.sf_height);
244 	ps = getpropstring(node, "ucoderev");
245 	if (*ps != '\0')
246 		printf(", microcode rev. %s", ps);
247 	printf("\n");
248 
249 	fbwscons_attach(&sc->sc_sunfb, &cgtwelve_accessops, isconsole);
250 }
251 
252 int
253 cgtwelve_ioctl(void *dev, u_long cmd, caddr_t data, int flags, struct proc *p)
254 {
255 	struct cgtwelve_softc *sc = dev;
256 	struct wsdisplay_fbinfo *wdf;
257 
258 	/*
259 	 * Note that, although the emulation (text) mode is running in the
260 	 * overlay plane, we advertize the frame buffer as the full-blown
261 	 * 32-bit beast it is.
262 	 */
263 	switch (cmd) {
264 	case WSDISPLAYIO_GTYPE:
265 		*(u_int *)data = WSDISPLAY_TYPE_SUNCG12;
266 		break;
267 	case WSDISPLAYIO_GINFO:
268 		wdf = (struct wsdisplay_fbinfo *)data;
269 		wdf->height = sc->sc_sunfb.sf_height;
270 		wdf->width = sc->sc_sunfb.sf_width;
271 		wdf->depth = 32;
272 		wdf->cmsize = 0;
273 		break;
274 	case WSDISPLAYIO_GETSUPPORTEDDEPTH:
275 		*(u_int *)data = WSDISPLAYIO_DEPTH_24_32;
276 		break;
277 	case WSDISPLAYIO_LINEBYTES:
278 		*(u_int *)data = sc->sc_sunfb.sf_linebytes * 32;
279 		break;
280 
281 	case WSDISPLAYIO_GETCMAP:
282 	case WSDISPLAYIO_PUTCMAP:
283 		break;
284 
285 	case WSDISPLAYIO_SMODE:
286 		if (*(int *)data == WSDISPLAYIO_MODE_EMUL) {
287 			/* Back from X11 to text mode */
288 			cgtwelve_reset(sc, 1);
289 		} else {
290 			/* Starting X11, switch to 32 bit mode */
291 			cgtwelve_reset(sc, 32);
292 		}
293 		break;
294 
295 	case WSDISPLAYIO_SVIDEO:
296 	case WSDISPLAYIO_GVIDEO:
297 		break;
298 
299 	default:
300 		return (-1);	/* not supported yet */
301 	}
302 
303 	return (0);
304 }
305 
306 /*
307  * Clean up hardware state (e.g., after bootup or after X crashes).
308  */
309 void
310 cgtwelve_reset(struct cgtwelve_softc *sc, int depth)
311 {
312 	u_int32_t c;
313 
314 	if (sc->sc_sunfb.sf_depth != depth) {
315 		if (depth == 1) {
316 			/*
317 			 * Select the enable plane as sc_overlay, and fill it.
318 			 */
319 			sc->sc_apu->hpage = sc->sc_highres ?
320 			    CG12_HPAGE_ENABLE_HR : CG12_HPAGE_ENABLE;
321 			sc->sc_apu->haccess = CG12_HACCESS_ENABLE;
322 			sc->sc_dpu->pln_sl_host = CG12_PLN_SL_ENABLE;
323 			sc->sc_dpu->pln_rd_msk_host = CG12_PLN_RD_ENABLE;
324 			sc->sc_dpu->pln_wr_msk_host = CG12_PLN_WR_ENABLE;
325 
326 			memset((void *)sc->sc_overlay, 0xff, sc->sc_highres ?
327 			    CG12_SIZE_ENABLE_HR : CG12_SIZE_ENABLE);
328 
329 			/*
330 			 * Select the overlay plane as sc_overlay.
331 			 */
332 			sc->sc_apu->hpage = sc->sc_highres ?
333 			    CG12_HPAGE_OVERLAY_HR : CG12_HPAGE_OVERLAY;
334 			sc->sc_apu->haccess = CG12_HACCESS_OVERLAY;
335 			sc->sc_dpu->pln_sl_host = CG12_PLN_SL_OVERLAY;
336 			sc->sc_dpu->pln_rd_msk_host = CG12_PLN_RD_OVERLAY;
337 			sc->sc_dpu->pln_wr_msk_host = CG12_PLN_WR_OVERLAY;
338 
339 			/*
340 			 * Upload a strict mono colormap, or the text
341 			 * upon returning from 32 bit mode would appear
342 			 * as (slightly dark) white on white.
343 			 */
344 			cgtwelve_ramdac_wraddr(sc, 0);
345 			sc->sc_ramdac->color = 0x00000000;
346 			for (c = 1; c < 256; c++)
347 				sc->sc_ramdac->color = 0x00ffffff;
348 		} else {
349 			/*
350 			 * Select the overlay plane as sc_overlay.
351 			 */
352 			sc->sc_apu->hpage = sc->sc_highres ?
353 			    CG12_HPAGE_OVERLAY_HR : CG12_HPAGE_OVERLAY;
354 			sc->sc_apu->haccess = CG12_HACCESS_OVERLAY;
355 			sc->sc_dpu->pln_sl_host = CG12_PLN_SL_OVERLAY;
356 			sc->sc_dpu->pln_rd_msk_host = CG12_PLN_RD_OVERLAY;
357 			sc->sc_dpu->pln_wr_msk_host = CG12_PLN_WR_OVERLAY;
358 
359 			/*
360 			 * Do not attempt to somewhat preserve screen
361 			 * contents - reading the overlay plane and writing
362 			 * to the color plane at the same time is not
363 			 * reliable, and allocating memory to save a copy
364 			 * of the overlay plane would be awful.
365 			 */
366 			bzero((void *)sc->sc_overlay, sc->sc_highres ?
367 			    CG12_SIZE_OVERLAY_HR : CG12_SIZE_OVERLAY);
368 
369 			/*
370 			 * Select the enable plane as sc_overlay, and clear it.
371 			 */
372 			sc->sc_apu->hpage = sc->sc_highres ?
373 			    CG12_HPAGE_ENABLE_HR : CG12_HPAGE_ENABLE;
374 			sc->sc_apu->haccess = CG12_HACCESS_ENABLE;
375 			sc->sc_dpu->pln_sl_host = CG12_PLN_SL_ENABLE;
376 			sc->sc_dpu->pln_rd_msk_host = CG12_PLN_RD_ENABLE;
377 			sc->sc_dpu->pln_wr_msk_host = CG12_PLN_WR_ENABLE;
378 
379 			bzero((void *)sc->sc_overlay, sc->sc_highres ?
380 			    CG12_SIZE_ENABLE_HR : CG12_SIZE_ENABLE);
381 
382 			/*
383 			 * Select the intensity (color) plane, and clear it.
384 			 */
385 			sc->sc_apu->hpage = sc->sc_highres ?
386 			    CG12_HPAGE_24BIT_HR : CG12_HPAGE_24BIT;
387 			sc->sc_apu->haccess = CG12_HACCESS_24BIT;
388 			sc->sc_dpu->pln_sl_host = CG12_PLN_SL_24BIT;
389 			sc->sc_dpu->pln_rd_msk_host = CG12_PLN_RD_24BIT;
390 			sc->sc_dpu->pln_wr_msk_host = CG12_PLN_WR_24BIT;
391 
392 			memset((void *)sc->sc_inten, 0x00ffffff,
393 			    sc->sc_highres ?
394 			      CG12_SIZE_COLOR24_HR : CG12_SIZE_COLOR24);
395 
396 			/*
397 			 * Use a direct colormap (ramp)
398 			 */
399 			cgtwelve_ramdac_wraddr(sc, 0);
400 			for (c = 0; c < 256; c++)
401 				sc->sc_ramdac->color = c | (c << 8) | (c << 16);
402 		}
403 	}
404 
405 	sc->sc_sunfb.sf_depth = depth;
406 }
407 
408 /*
409  * Return the address that would map the given device at the given
410  * offset, allowing for the given protection, or return -1 for error.
411  */
412 paddr_t
413 cgtwelve_mmap(void *v, off_t offset, int prot)
414 {
415 	struct cgtwelve_softc *sc = v;
416 
417 	if (offset & PGOFSET || offset < 0)
418 		return (-1);
419 
420 	/*
421 	 * Note that mmap() will invoke this function only if we are NOT
422 	 * in emulation mode, so we can assume 32 bit mode safely here.
423 	 */
424 	if (offset < sc->sc_sunfb.sf_fbsize * 32) {
425 		return (bus_space_mmap(sc->sc_bustag, sc->sc_paddr, offset,
426 		    prot, BUS_SPACE_MAP_LINEAR));
427 	}
428 
429 	return (-1);
430 }
431 
432 /*
433  * Simple Bt462 programming routines.
434  */
435 
436 static __inline__ void
437 cgtwelve_ramdac_wraddr(struct cgtwelve_softc *sc, u_int32_t addr)
438 {
439 	sc->sc_ramdac->addr_lo = (addr & 0xff);
440 	sc->sc_ramdac->addr_hi = ((addr >> 8) & 0xff);
441 }
442 
443 /*
444  * Shutdown hook used to restore PROM-compatible video mode on shutdown,
445  * so that the PROM prompt is visible again.
446  */
447 void
448 cgtwelve_prom(void *v)
449 {
450 	struct cgtwelve_softc *sc = v;
451 	extern struct consdev consdev_prom;
452 
453 	if (sc->sc_sunfb.sf_depth != 1) {
454 		cgtwelve_reset(sc, 1);
455 
456 		/*
457 		 * Go back to prom output for the last few messages, so they
458 		 * will be displayed correctly.
459 		 */
460 		cn_tab = &consdev_prom;
461 	}
462 }
463