bwtwo.c revision 1.10
1/*	$OpenBSD: bwtwo.c,v 1.10 2003/06/27 01:36:53 jason 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/wscons/wscons_raster.h>
51#include <dev/rasops/rasops.h>
52#include <machine/fbvar.h>
53
54#define	BWTWO_CTRL_OFFSET	0x400000
55#define	BWTWO_CTRL_SIZE	(sizeof(u_int32_t) * 8)
56#define	BWTWO_VID_OFFSET	0x800000
57#define	BWTWO_VID_SIZE	(1024 * 1024)
58
59#define	FBC_CTRL	0x10		/* control */
60#define	FBC_STAT	0x11		/* status */
61#define	FBC_START	0x12		/* cursor start */
62#define	FBC_END		0x13		/* cursor end */
63#define	FBC_VCTRL	0x14		/* 12 bytes of timing goo */
64
65#define	FBC_CTRL_IENAB		0x80	/* interrupt enable */
66#define	FBC_CTRL_VENAB		0x40	/* video enable */
67#define	FBC_CTRL_TIME		0x20	/* timing enable */
68#define	FBC_CTRL_CURS		0x10	/* cursor compare enable */
69#define	FBC_CTRL_XTAL		0x0c	/* xtal select (0,1,2,test): */
70#define	FBC_CTRL_XTAL_0		0x00	/*  0 */
71#define	FBC_CTRL_XTAL_1		0x04	/*  0 */
72#define	FBC_CTRL_XTAL_2		0x08	/*  0 */
73#define	FBC_CTRL_XTAL_TEST	0x0c	/*  0 */
74#define	FBC_CTRL_DIV		0x03	/* divisor (1,2,3,4): */
75#define	FBC_CTRL_DIV_1		0x00	/*  / 1 */
76#define	FBC_CTRL_DIV_2		0x01	/*  / 2 */
77#define	FBC_CTRL_DIV_3		0x02	/*  / 3 */
78#define	FBC_CTRL_DIV_4		0x03	/*  / 4 */
79
80#define	FBC_STAT_INTR		0x80	/* interrupt pending */
81#define	FBC_STAT_RES		0x70	/* monitor sense: */
82#define	FBC_STAT_RES_1024	0x10	/*  1024x768 */
83#define	FBC_STAT_RES_1280	0x40	/*  1280x1024 */
84#define	FBC_STAT_RES_1152	0x30	/*  1152x900 */
85#define	FBC_STAT_RES_1152A	0x40	/*  1152x900x76, A */
86#define	FBC_STAT_RES_1600	0x50	/*  1600x1200 */
87#define	FBC_STAT_RES_1152B	0x60	/*  1152x900x86, B */
88#define	FBC_STAT_ID		0x0f	/* id mask: */
89#define	FBC_STAT_ID_COLOR	0x01	/*  color */
90#define	FBC_STAT_ID_MONO	0x02	/*  monochrome */
91#define	FBC_STAT_ID_MONOECL	0x03	/*  monochrome, ecl */
92
93#define	FBC_READ(sc, reg) \
94    bus_space_read_1((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg))
95#define	FBC_WRITE(sc, reg, val) \
96    bus_space_write_1((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg), (val))
97
98struct bwtwo_softc {
99	struct sunfb sc_sunfb;
100	struct sbusdev sc_sd;
101	bus_space_tag_t sc_bustag;
102	bus_addr_t sc_paddr;
103	bus_space_handle_t sc_ctrl_regs;
104	bus_space_handle_t sc_vid_regs;
105	int sc_nscreens;
106};
107
108struct wsscreen_descr bwtwo_stdscreen = {
109	"std",
110};
111
112const struct wsscreen_descr *bwtwo_scrlist[] = {
113	&bwtwo_stdscreen,
114	/* XXX other formats? */
115};
116
117struct wsscreen_list bwtwo_screenlist = {
118	sizeof(bwtwo_scrlist) / sizeof(struct wsscreen_descr *), bwtwo_scrlist
119};
120
121int bwtwo_ioctl(void *, u_long, caddr_t, int, struct proc *);
122int bwtwo_alloc_screen(void *, const struct wsscreen_descr *, void **,
123    int *, int *, long *);
124void bwtwo_free_screen(void *, void *);
125int bwtwo_show_screen(void *, void *, int, void (*cb)(void *, int, int),
126    void *);
127paddr_t bwtwo_mmap(void *, off_t, int);
128int bwtwo_is_console(int);
129void bwtwo_burner(void *, u_int, u_int);
130void bwtwo_updatecursor(struct rasops_info *);
131
132struct wsdisplay_accessops bwtwo_accessops = {
133	bwtwo_ioctl,
134	bwtwo_mmap,
135	bwtwo_alloc_screen,
136	bwtwo_free_screen,
137	bwtwo_show_screen,
138	NULL,	/* load_font */
139	NULL,	/* scrollback */
140	NULL,	/* getchar */
141	bwtwo_burner,
142};
143
144int	bwtwomatch(struct device *, void *, void *);
145void	bwtwoattach(struct device *, struct device *, void *);
146
147struct cfattach bwtwo_ca = {
148	sizeof (struct bwtwo_softc), bwtwomatch, bwtwoattach
149};
150
151struct cfdriver bwtwo_cd = {
152	NULL, "bwtwo", DV_DULL
153};
154
155int
156bwtwomatch(parent, vcf, aux)
157	struct device *parent;
158	void *vcf, *aux;
159{
160	struct cfdata *cf = vcf;
161	struct sbus_attach_args *sa = aux;
162
163	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
164}
165
166void
167bwtwoattach(parent, self, aux)
168	struct device *parent, *self;
169	void *aux;
170{
171	struct bwtwo_softc *sc = (struct bwtwo_softc *)self;
172	struct sbus_attach_args *sa = aux;
173	struct wsemuldisplaydev_attach_args waa;
174	int console;
175
176	sc->sc_bustag = sa->sa_bustag;
177	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset);
178
179	fb_setsize(&sc->sc_sunfb, 1, 1152, 900, sa->sa_node, 0);
180
181	if (sa->sa_nreg != 1) {
182		printf(": expected %d registers, got %d\n", 1, sa->sa_nreg);
183		goto fail;
184	}
185
186	/*
187	 * Map just CTRL and video RAM.
188	 */
189	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
190	    sa->sa_reg[0].sbr_offset + BWTWO_CTRL_OFFSET,
191	    BWTWO_CTRL_SIZE, 0, 0, &sc->sc_ctrl_regs) != 0) {
192		printf(": cannot map ctrl registers\n");
193		goto fail_ctrl;
194	}
195
196	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
197	    sa->sa_reg[0].sbr_offset + BWTWO_VID_OFFSET,
198	    sc->sc_sunfb.sf_fbsize, BUS_SPACE_MAP_LINEAR,
199	    0, &sc->sc_vid_regs) != 0) {
200		printf(": cannot map vid registers\n");
201		goto fail_vid;
202	}
203
204	console = bwtwo_is_console(sa->sa_node);
205
206	sbus_establish(&sc->sc_sd, &sc->sc_sunfb.sf_dev);
207
208	bwtwo_burner(sc, 1, 0);
209
210	printf("\n");
211
212	sc->sc_sunfb.sf_ro.ri_bits = (void *)bus_space_vaddr(sc->sc_bustag,
213	    sc->sc_vid_regs);
214	sc->sc_sunfb.sf_ro.ri_hw = sc;
215	fbwscons_init(&sc->sc_sunfb, console ? 0 : RI_CLEAR);
216
217	bwtwo_stdscreen.capabilities = sc->sc_sunfb.sf_ro.ri_caps;
218	bwtwo_stdscreen.nrows = sc->sc_sunfb.sf_ro.ri_rows;
219	bwtwo_stdscreen.ncols = sc->sc_sunfb.sf_ro.ri_cols;
220	bwtwo_stdscreen.textops = &sc->sc_sunfb.sf_ro.ri_ops;
221
222	if (console) {
223		sc->sc_sunfb.sf_ro.ri_updatecursor = bwtwo_updatecursor;
224		fbwscons_console_init(&sc->sc_sunfb, &bwtwo_stdscreen, -1,
225		    bwtwo_burner);
226	}
227
228	waa.console = console;
229	waa.scrdata = &bwtwo_screenlist;
230	waa.accessops = &bwtwo_accessops;
231	waa.accesscookie = sc;
232	config_found(self, &waa, wsemuldisplaydevprint);
233
234	return;
235
236fail_vid:
237	bus_space_unmap(sa->sa_bustag, sc->sc_ctrl_regs, BWTWO_CTRL_SIZE);
238fail_ctrl:
239fail:
240;
241}
242
243int
244bwtwo_ioctl(v, cmd, data, flags, p)
245	void *v;
246	u_long cmd;
247	caddr_t data;
248	int flags;
249	struct proc *p;
250{
251	struct bwtwo_softc *sc = v;
252	struct wsdisplay_fbinfo *wdf;
253
254	switch (cmd) {
255	case WSDISPLAYIO_GTYPE:
256		*(u_int *)data = WSDISPLAY_TYPE_SUNBW;
257		break;
258	case WSDISPLAYIO_GINFO:
259		wdf = (void *)data;
260		wdf->height = sc->sc_sunfb.sf_height;
261		wdf->width  = sc->sc_sunfb.sf_width;
262		wdf->depth  = sc->sc_sunfb.sf_depth;
263		wdf->cmsize = 0;
264		break;
265	case WSDISPLAYIO_LINEBYTES:
266		*(u_int *)data = sc->sc_sunfb.sf_linebytes;
267		break;
268
269	case WSDISPLAYIO_GETCMAP:
270	case WSDISPLAYIO_PUTCMAP:
271		break;
272
273	case WSDISPLAYIO_SVIDEO:
274	case WSDISPLAYIO_GVIDEO:
275	case WSDISPLAYIO_GCURPOS:
276	case WSDISPLAYIO_SCURPOS:
277	case WSDISPLAYIO_GCURMAX:
278	case WSDISPLAYIO_GCURSOR:
279	case WSDISPLAYIO_SCURSOR:
280	default:
281		return -1; /* not supported yet */
282        }
283
284	return (0);
285}
286
287int
288bwtwo_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
289	void *v;
290	const struct wsscreen_descr *type;
291	void **cookiep;
292	int *curxp, *curyp;
293	long *attrp;
294{
295	struct bwtwo_softc *sc = v;
296
297	if (sc->sc_nscreens > 0)
298		return (ENOMEM);
299
300	*cookiep = &sc->sc_sunfb.sf_ro;
301	*curyp = 0;
302	*curxp = 0;
303	sc->sc_sunfb.sf_ro.ri_ops.alloc_attr(&sc->sc_sunfb.sf_ro,
304	    0, 0, 0, attrp);
305	sc->sc_nscreens++;
306	return (0);
307}
308
309void
310bwtwo_free_screen(v, cookie)
311	void *v;
312	void *cookie;
313{
314	struct bwtwo_softc *sc = v;
315
316	sc->sc_nscreens--;
317}
318
319int
320bwtwo_show_screen(v, cookie, waitok, cb, cbarg)
321	void *v;
322	void *cookie;
323	int waitok;
324	void (*cb)(void *, int, int);
325	void *cbarg;
326{
327	return (0);
328}
329
330#define	START		(128 * 1024 + 128 * 1024)
331#define	NOOVERLAY	(0x04000000)
332
333paddr_t
334bwtwo_mmap(v, offset, prot)
335	void *v;
336	off_t offset;
337	int prot;
338{
339	struct bwtwo_softc *sc = v;
340
341	if (offset & PGOFSET)
342		return (-1);
343
344	if (offset >= 0 && offset < sc->sc_sunfb.sf_fbsize)
345		return (bus_space_mmap(sc->sc_bustag, sc->sc_paddr,
346		    BWTWO_VID_OFFSET + offset, prot, BUS_SPACE_MAP_LINEAR));
347
348	return (-1);
349}
350
351int
352bwtwo_is_console(node)
353	int node;
354{
355	extern int fbnode;
356
357	return (fbnode == node);
358}
359
360void
361bwtwo_burner(vsc, on, flags)
362	void *vsc;
363	u_int on, flags;
364{
365	struct bwtwo_softc *sc = vsc;
366	int s;
367	u_int8_t fbc;
368
369	s = splhigh();
370	fbc = FBC_READ(sc, FBC_CTRL);
371	if (on)
372		fbc |= FBC_CTRL_VENAB | FBC_CTRL_TIME;
373	else {
374		fbc &= ~FBC_CTRL_VENAB;
375		if (flags & WSDISPLAY_BURN_VBLANK)
376			fbc &= ~FBC_CTRL_TIME;
377	}
378	FBC_WRITE(sc, FBC_CTRL, fbc);
379	splx(s);
380}
381
382void
383bwtwo_updatecursor(ri)
384	struct rasops_info *ri;
385{
386	struct bwtwo_softc *sc = ri->ri_hw;
387
388	if (sc->sc_sunfb.sf_crowp != NULL)
389		*sc->sc_sunfb.sf_crowp = ri->ri_crow;
390	if (sc->sc_sunfb.sf_ccolp != NULL)
391		*sc->sc_sunfb.sf_ccolp = ri->ri_ccol;
392}
393