xref: /openbsd-src/sys/dev/sbus/bwtwo.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: bwtwo.c,v 1.18 2008/12/27 17:23:03 miod Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Effort sponsored in part by the Defense Advanced Research Projects
29  * Agency (DARPA) and Air Force Research Laboratory, Air Force
30  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
31  *
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/errno.h>
38 #include <sys/device.h>
39 #include <sys/ioctl.h>
40 #include <sys/malloc.h>
41 
42 #include <machine/bus.h>
43 #include <machine/intr.h>
44 #include <machine/autoconf.h>
45 #include <machine/openfirm.h>
46 
47 #include <dev/sbus/sbusvar.h>
48 #include <dev/wscons/wsconsio.h>
49 #include <dev/wscons/wsdisplayvar.h>
50 #include <dev/rasops/rasops.h>
51 #include <machine/fbvar.h>
52 
53 #define	BWTWO_CTRL_OFFSET	0x400000
54 #define	BWTWO_CTRL_SIZE	(sizeof(u_int32_t) * 8)
55 #define	BWTWO_VID_OFFSET	0x800000
56 #define	BWTWO_VID_SIZE	(1024 * 1024)
57 
58 #define	FBC_CTRL	0x10		/* control */
59 #define	FBC_STAT	0x11		/* status */
60 #define	FBC_START	0x12		/* cursor start */
61 #define	FBC_END		0x13		/* cursor end */
62 #define	FBC_VCTRL	0x14		/* 12 bytes of timing goo */
63 
64 #define	FBC_CTRL_IENAB		0x80	/* interrupt enable */
65 #define	FBC_CTRL_VENAB		0x40	/* video enable */
66 #define	FBC_CTRL_TIME		0x20	/* timing enable */
67 #define	FBC_CTRL_CURS		0x10	/* cursor compare enable */
68 #define	FBC_CTRL_XTAL		0x0c	/* xtal select (0,1,2,test): */
69 #define	FBC_CTRL_XTAL_0		0x00	/*  0 */
70 #define	FBC_CTRL_XTAL_1		0x04	/*  0 */
71 #define	FBC_CTRL_XTAL_2		0x08	/*  0 */
72 #define	FBC_CTRL_XTAL_TEST	0x0c	/*  0 */
73 #define	FBC_CTRL_DIV		0x03	/* divisor (1,2,3,4): */
74 #define	FBC_CTRL_DIV_1		0x00	/*  / 1 */
75 #define	FBC_CTRL_DIV_2		0x01	/*  / 2 */
76 #define	FBC_CTRL_DIV_3		0x02	/*  / 3 */
77 #define	FBC_CTRL_DIV_4		0x03	/*  / 4 */
78 
79 #define	FBC_STAT_INTR		0x80	/* interrupt pending */
80 #define	FBC_STAT_RES		0x70	/* monitor sense: */
81 #define	FBC_STAT_RES_1024	0x10	/*  1024x768 */
82 #define	FBC_STAT_RES_1280	0x40	/*  1280x1024 */
83 #define	FBC_STAT_RES_1152	0x30	/*  1152x900 */
84 #define	FBC_STAT_RES_1152A	0x40	/*  1152x900x76, A */
85 #define	FBC_STAT_RES_1600	0x50	/*  1600x1200 */
86 #define	FBC_STAT_RES_1152B	0x60	/*  1152x900x86, B */
87 #define	FBC_STAT_ID		0x0f	/* id mask: */
88 #define	FBC_STAT_ID_COLOR	0x01	/*  color */
89 #define	FBC_STAT_ID_MONO	0x02	/*  monochrome */
90 #define	FBC_STAT_ID_MONOECL	0x03	/*  monochrome, ecl */
91 
92 #define	FBC_READ(sc, reg) \
93     bus_space_read_1((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg))
94 #define	FBC_WRITE(sc, reg, val) \
95     bus_space_write_1((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg), (val))
96 
97 struct bwtwo_softc {
98 	struct sunfb sc_sunfb;
99 	bus_space_tag_t sc_bustag;
100 	bus_addr_t sc_paddr;
101 	bus_space_handle_t sc_ctrl_regs;
102 	bus_space_handle_t sc_vid_regs;
103 	int sc_nscreens;
104 };
105 
106 int bwtwo_ioctl(void *, u_long, caddr_t, int, struct proc *);
107 paddr_t bwtwo_mmap(void *, off_t, int);
108 int bwtwo_is_console(int);
109 void bwtwo_burner(void *, u_int, u_int);
110 
111 struct wsdisplay_accessops bwtwo_accessops = {
112 	bwtwo_ioctl,
113 	bwtwo_mmap,
114 	NULL,	/* alloc_screen */
115 	NULL,	/* free_screen */
116 	NULL,	/* show_screen */
117 	NULL,	/* load_font */
118 	NULL,	/* scrollback */
119 	NULL,	/* getchar */
120 	bwtwo_burner,
121 };
122 
123 int	bwtwomatch(struct device *, void *, void *);
124 void	bwtwoattach(struct device *, struct device *, void *);
125 
126 struct cfattach bwtwo_ca = {
127 	sizeof (struct bwtwo_softc), bwtwomatch, bwtwoattach
128 };
129 
130 struct cfdriver bwtwo_cd = {
131 	NULL, "bwtwo", DV_DULL
132 };
133 
134 int
135 bwtwomatch(parent, vcf, aux)
136 	struct device *parent;
137 	void *vcf, *aux;
138 {
139 	struct cfdata *cf = vcf;
140 	struct sbus_attach_args *sa = aux;
141 
142 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
143 }
144 
145 void
146 bwtwoattach(parent, self, aux)
147 	struct device *parent, *self;
148 	void *aux;
149 {
150 	struct bwtwo_softc *sc = (struct bwtwo_softc *)self;
151 	struct sbus_attach_args *sa = aux;
152 	int node, console;
153 	const char *nam;
154 
155 	node = sa->sa_node;
156 	sc->sc_bustag = sa->sa_bustag;
157 	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset);
158 
159 	fb_setsize(&sc->sc_sunfb, 1, 1152, 900, node, 0);
160 
161 	if (sa->sa_nreg != 1) {
162 		printf(": expected %d registers, got %d\n", 1, sa->sa_nreg);
163 		goto fail;
164 	}
165 
166 	/*
167 	 * Map just CTRL and video RAM.
168 	 */
169 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
170 	    sa->sa_reg[0].sbr_offset + BWTWO_CTRL_OFFSET,
171 	    BWTWO_CTRL_SIZE, 0, 0, &sc->sc_ctrl_regs) != 0) {
172 		printf(": cannot map ctrl registers\n");
173 		goto fail_ctrl;
174 	}
175 
176 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
177 	    sa->sa_reg[0].sbr_offset + BWTWO_VID_OFFSET,
178 	    sc->sc_sunfb.sf_fbsize, BUS_SPACE_MAP_LINEAR,
179 	    0, &sc->sc_vid_regs) != 0) {
180 		printf(": cannot map vid registers\n");
181 		goto fail_vid;
182 	}
183 
184 	nam = getpropstring(node, "model");
185 	if (*nam == '\0')
186 		nam = sa->sa_name;
187 	printf(": %s", nam);
188 
189 	console = bwtwo_is_console(node);
190 
191 	bwtwo_burner(sc, 1, 0);
192 
193 	printf(", %dx%d\n", sc->sc_sunfb.sf_width, sc->sc_sunfb.sf_height);
194 
195 	sc->sc_sunfb.sf_ro.ri_bits = (void *)bus_space_vaddr(sc->sc_bustag,
196 	    sc->sc_vid_regs);
197 	sc->sc_sunfb.sf_ro.ri_hw = sc;
198 	fbwscons_init(&sc->sc_sunfb, 0, console);
199 
200 	if (console)
201 		fbwscons_console_init(&sc->sc_sunfb, -1);
202 
203 	fbwscons_attach(&sc->sc_sunfb, &bwtwo_accessops, console);
204 
205 	return;
206 
207 fail_vid:
208 	bus_space_unmap(sa->sa_bustag, sc->sc_ctrl_regs, BWTWO_CTRL_SIZE);
209 fail_ctrl:
210 fail:
211 ;
212 }
213 
214 int
215 bwtwo_ioctl(v, cmd, data, flags, p)
216 	void *v;
217 	u_long cmd;
218 	caddr_t data;
219 	int flags;
220 	struct proc *p;
221 {
222 	struct bwtwo_softc *sc = v;
223 	struct wsdisplay_fbinfo *wdf;
224 
225 	switch (cmd) {
226 	case WSDISPLAYIO_GTYPE:
227 		*(u_int *)data = WSDISPLAY_TYPE_SUNBW;
228 		break;
229 	case WSDISPLAYIO_GINFO:
230 		wdf = (void *)data;
231 		wdf->height = sc->sc_sunfb.sf_height;
232 		wdf->width  = sc->sc_sunfb.sf_width;
233 		wdf->depth  = sc->sc_sunfb.sf_depth;
234 		wdf->cmsize = 0;
235 		break;
236 	case WSDISPLAYIO_LINEBYTES:
237 		*(u_int *)data = sc->sc_sunfb.sf_linebytes;
238 		break;
239 
240 	case WSDISPLAYIO_GETCMAP:
241 	case WSDISPLAYIO_PUTCMAP:
242 		break;
243 
244 	case WSDISPLAYIO_SVIDEO:
245 	case WSDISPLAYIO_GVIDEO:
246 		break;
247 
248 	case WSDISPLAYIO_GCURPOS:
249 	case WSDISPLAYIO_SCURPOS:
250 	case WSDISPLAYIO_GCURMAX:
251 	case WSDISPLAYIO_GCURSOR:
252 	case WSDISPLAYIO_SCURSOR:
253 	default:
254 		return -1; /* not supported yet */
255         }
256 
257 	return (0);
258 }
259 
260 paddr_t
261 bwtwo_mmap(v, offset, prot)
262 	void *v;
263 	off_t offset;
264 	int prot;
265 {
266 	struct bwtwo_softc *sc = v;
267 
268 	if (offset & PGOFSET)
269 		return (-1);
270 
271 	if (offset >= 0 && offset < sc->sc_sunfb.sf_fbsize)
272 		return (bus_space_mmap(sc->sc_bustag, sc->sc_paddr,
273 		    BWTWO_VID_OFFSET + offset, prot, BUS_SPACE_MAP_LINEAR));
274 
275 	return (-1);
276 }
277 
278 int
279 bwtwo_is_console(node)
280 	int node;
281 {
282 	extern int fbnode;
283 
284 	return (fbnode == node);
285 }
286 
287 void
288 bwtwo_burner(vsc, on, flags)
289 	void *vsc;
290 	u_int on, flags;
291 {
292 	struct bwtwo_softc *sc = vsc;
293 	int s;
294 	u_int8_t fbc;
295 
296 	s = splhigh();
297 	fbc = FBC_READ(sc, FBC_CTRL);
298 	if (on)
299 		fbc |= FBC_CTRL_VENAB | FBC_CTRL_TIME;
300 	else {
301 		fbc &= ~FBC_CTRL_VENAB;
302 		if (flags & WSDISPLAY_BURN_VBLANK)
303 			fbc &= ~FBC_CTRL_TIME;
304 	}
305 	FBC_WRITE(sc, FBC_CTRL, fbc);
306 	splx(s);
307 }
308