xref: /openbsd-src/sys/dev/sbus/agten.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: agten.c,v 1.10 2013/10/20 20:07:30 miod Exp $	*/
2 /*
3  * Copyright (c) 2002, 2003, Miodrag Vallat.
4  * 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 /*
30  * Fujitsu AG-10 framebuffer driver.
31  *
32  * The AG-10 is mostly made of:
33  * - a 3DLabs 300SX Glint chip, with two 6MB independent framebuffer spaces
34  * - a Number Nine Imagine 128 chip with its own 4MB framebuffer space
35  * - a Weitek P9100 with its own 2MB of framebuffer memory
36  * - an IBM PaletteDAC 561 ramdac
37  * - an Analog Devices ADSP-21062
38  *
39  * All of these chips (memory, registers, etc) are mapped in the SBus
40  * memory space associated to the board. What is unexpected, however, is
41  * that there are also PCI registers mappings for the first three chips!
42  *
43  * The three graphics chips act as overlays of each other, for the final
44  * video output.
45  *
46  * The PROM initialization will use the I128 framebuffer memory for output,
47  * which is ``above'' the P9100. The P9100 seems to only be there to provide
48  * a simple RAMDAC interface, but its frame buffer memory is accessible and
49  * will appear as an ``underlay'' plane.
50  */
51 
52 /*
53  * TODO
54  * - initialize the I128 in 32bit mode
55  * - use the i128 acceleration features
56  */
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/buf.h>
61 #include <sys/device.h>
62 #include <sys/ioctl.h>
63 #include <sys/malloc.h>
64 #include <sys/mman.h>
65 #include <sys/tty.h>
66 #include <sys/conf.h>
67 
68 #include <uvm/uvm_extern.h>
69 
70 #include <machine/autoconf.h>
71 #include <machine/pmap.h>
72 #include <machine/cpu.h>
73 #include <machine/conf.h>
74 
75 #include <dev/wscons/wsconsio.h>
76 #include <dev/wscons/wsdisplayvar.h>
77 #include <dev/rasops/rasops.h>
78 #include <machine/fbvar.h>
79 
80 #include <dev/ic/p9000.h>
81 #include <dev/ic/ibm561reg.h>
82 
83 #include <dev/sbus/sbusvar.h>
84 
85 struct agten_cmap {
86 	u_int8_t	cm_red[256];
87 	u_int8_t	cm_green[256];
88 	u_int8_t	cm_blue[256];
89 };
90 
91 /* per-display variables */
92 struct agten_softc {
93 	struct	sunfb sc_sunfb;			/* common base part */
94 
95 	bus_space_tag_t	sc_bustag;
96 	bus_addr_t	sc_paddr;
97 	off_t		sc_physoffset;		/* offset for frame buffer */
98 
99 	volatile u_int8_t *sc_p9100;
100 	struct agten_cmap sc_cmap;		/* shadow color map */
101 
102 	volatile u_int32_t *sc_i128_fb;
103 
104 	int	sc_nscreens;
105 };
106 
107 int agten_ioctl(void *, u_long, caddr_t, int, struct proc *);
108 paddr_t agten_mmap(void *, off_t, int);
109 void agten_reset(struct agten_softc *);
110 void agten_setcolor(void *, u_int, u_int8_t, u_int8_t, u_int8_t);
111 
112 static __inline__ void ibm561_write(struct agten_softc *, u_int32_t, u_int32_t);
113 int agten_getcmap(struct agten_cmap *, struct wsdisplay_cmap *);
114 int agten_putcmap(struct agten_cmap *, struct wsdisplay_cmap *);
115 void agten_loadcmap(struct agten_softc *, u_int, u_int);
116 
117 struct wsdisplay_accessops agten_accessops = {
118 	.ioctl = agten_ioctl,
119 	.mmap = agten_mmap
120 };
121 
122 int agtenmatch(struct device *, void *, void *);
123 void agtenattach(struct device *, struct device *, void *);
124 
125 struct cfattach agten_ca = {
126 	sizeof(struct agten_softc), agtenmatch, agtenattach
127 };
128 
129 struct cfdriver agten_cd = {
130 	NULL, "agten", DV_DULL
131 };
132 
133 int
134 agtenmatch(struct device *parent, void *vcf, void *aux)
135 {
136 	struct sbus_attach_args *sa = aux;
137 
138 	if (strcmp(sa->sa_name, "PFU,aga") != 0)
139 		return (0);
140 
141 	return (1);
142 }
143 
144 void
145 agtenattach(struct device *parent, struct device *self, void *args)
146 {
147 	struct agten_softc *sc = (struct agten_softc *)self;
148 	struct sbus_attach_args *sa = args;
149 	bus_space_tag_t bt;
150 	bus_space_handle_t bh;
151 	int node, isconsole;
152 	char *nam;
153 
154 	bt = sa->sa_bustag;
155 	node = sa->sa_node;
156 	nam = getpropstring(node, "model");
157 	printf(": model %s", nam);
158 
159 	isconsole = node == fbnode;
160 
161 	/*
162 	 * Map the various beasts of this card we are interested in.
163 	 */
164 
165 	sc->sc_bustag = bt;
166 	sc->sc_paddr = sbus_bus_addr(bt, sa->sa_slot, sa->sa_offset);
167 
168 	sc->sc_physoffset =
169 	    (off_t)getpropint(node, "i128_fb_physaddr", 0x8000000);
170 
171 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + sc->sc_physoffset,
172 	    getpropint(node, "i128_fb_size", 0x400000), BUS_SPACE_MAP_LINEAR,
173 	    0, &bh) != 0) {
174 		printf("\n%s: couldn't map video memory\n", self->dv_xname);
175 		return;
176 	}
177 	sc->sc_i128_fb = bus_space_vaddr(bt, bh);
178 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset +
179 	    getpropint(node, "p9100_reg_physaddr", 0x10a0000), 0x4000,
180 	    BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
181 		printf("\n%s: couldn't map control registers\n", self->dv_xname);
182 		return;
183 	}
184 	sc->sc_p9100 = bus_space_vaddr(bt, bh);
185 
186 	/*
187 	 * For some reason the agten does not use the canonical name for
188 	 * properties, but uses an ffb_ prefix; and the linebytes property is
189 	 * missing.
190 	 * The following is a specific version of
191 	 *   fb_setsize(&sc->sc_sunfb, 8, 1152, 900, node, BUS_SBUS);
192 	 * using the correct property names.
193 	 */
194 #ifdef notyet
195 	sc->sc_sunfb.sf_depth = 32;
196 #else
197 	sc->sc_sunfb.sf_depth = getpropint(node, "ffb_depth", 8);
198 #endif
199 	sc->sc_sunfb.sf_width = getpropint(node, "ffb_width", 1152);
200 	sc->sc_sunfb.sf_height = getpropint(node, "ffb_height", 900);
201 	sc->sc_sunfb.sf_linebytes =
202 	    roundup(sc->sc_sunfb.sf_width, sc->sc_sunfb.sf_depth) *
203 	        sc->sc_sunfb.sf_depth / 8;
204 	sc->sc_sunfb.sf_fbsize =
205 	    sc->sc_sunfb.sf_height * sc->sc_sunfb.sf_linebytes;
206 
207 	printf(", %dx%d, depth %d\n",
208 	    sc->sc_sunfb.sf_width, sc->sc_sunfb.sf_height,
209 	    sc->sc_sunfb.sf_depth);
210 
211 	sc->sc_sunfb.sf_ro.ri_bits = (void *)sc->sc_i128_fb;
212 
213 	sc->sc_sunfb.sf_ro.ri_hw = sc;
214 	fbwscons_init(&sc->sc_sunfb, 0, isconsole);
215 	fbwscons_setcolormap(&sc->sc_sunfb, agten_setcolor);
216 
217 	if (isconsole)
218 		fbwscons_console_init(&sc->sc_sunfb, -1);
219 
220 	fbwscons_attach(&sc->sc_sunfb, &agten_accessops, isconsole);
221 }
222 
223 int
224 agten_ioctl(dev, cmd, data, flags, p)
225 	void *dev;
226 	u_long cmd;
227 	caddr_t data;
228 	int flags;
229 	struct proc *p;
230 {
231 	struct agten_softc *sc = dev;
232 	struct wsdisplay_cmap *cm;
233 	struct wsdisplay_fbinfo *wdf;
234 	int error;
235 
236 	switch (cmd) {
237 	case WSDISPLAYIO_GTYPE:
238 		*(u_int *)data = WSDISPLAY_TYPE_AGTEN;
239 		break;
240 	case WSDISPLAYIO_GINFO:
241 		wdf = (struct wsdisplay_fbinfo *)data;
242 		wdf->height = sc->sc_sunfb.sf_height;
243 		wdf->width = sc->sc_sunfb.sf_width;
244 		wdf->depth = sc->sc_sunfb.sf_depth;
245 		wdf->cmsize = (sc->sc_sunfb.sf_depth == 8) ? 256 : 0;
246 		break;
247 	case WSDISPLAYIO_LINEBYTES:
248 		*(u_int *)data = sc->sc_sunfb.sf_linebytes;
249 		break;
250 
251 	case WSDISPLAYIO_GETCMAP:
252 		if (sc->sc_sunfb.sf_depth == 8) {
253 			cm = (struct wsdisplay_cmap *)data;
254 			error = agten_getcmap(&sc->sc_cmap, cm);
255 			if (error)
256 				return (error);
257 		}
258 		break;
259 	case WSDISPLAYIO_PUTCMAP:
260 		if (sc->sc_sunfb.sf_depth == 8) {
261 			cm = (struct wsdisplay_cmap *)data;
262 			error = agten_putcmap(&sc->sc_cmap, cm);
263 			if (error)
264 				return (error);
265 			agten_loadcmap(sc, 0, 256);
266 		}
267 		break;
268 
269 	case WSDISPLAYIO_SVIDEO:
270 	case WSDISPLAYIO_GVIDEO:
271 		break;
272 
273 	case WSDISPLAYIO_GCURPOS:
274 	case WSDISPLAYIO_SCURPOS:
275 	case WSDISPLAYIO_GCURMAX:
276 	case WSDISPLAYIO_GCURSOR:
277 	case WSDISPLAYIO_SCURSOR:
278 	default:
279 		return (-1);	/* not supported yet */
280 	}
281 
282 	return (0);
283 }
284 
285 /*
286  * Return the address that would map the given device at the given
287  * offset, allowing for the given protection, or return -1 for error.
288  */
289 paddr_t
290 agten_mmap(v, offset, prot)
291 	void *v;
292 	off_t offset;
293 	int prot;
294 {
295 	struct agten_softc *sc = v;
296 
297 	if (offset & PGOFSET)
298 		return (-1);
299 
300 	/* Allow mapping as a dumb framebuffer from offset 0 */
301 	if (offset >= 0 && offset < sc->sc_sunfb.sf_fbsize) {
302 		return (bus_space_mmap(sc->sc_bustag, sc->sc_paddr,
303 		    sc->sc_physoffset + offset, prot, BUS_SPACE_MAP_LINEAR));
304 	}
305 
306 	return (-1);	/* not a user-map offset */
307 }
308 
309 void
310 agten_setcolor(v, index, r, g, b)
311 	void *v;
312 	u_int index;
313 	u_int8_t r, g, b;
314 {
315 	struct agten_softc *sc = v;
316 
317 	sc->sc_cmap.cm_red[index] = r;
318 	sc->sc_cmap.cm_green[index] = g;
319 	sc->sc_cmap.cm_blue[index] = b;
320 
321 	agten_loadcmap(sc, index, 1);
322 }
323 
324 int
325 agten_getcmap(struct agten_cmap *cm, struct wsdisplay_cmap *rcm)
326 {
327 	u_int index = rcm->index, count = rcm->count;
328 	int error;
329 
330 	if (index >= 256 || count > 256 - index)
331 		return (EINVAL);
332 
333 	if ((error = copyout(&cm->cm_red[index], rcm->red, count)) != 0)
334 		return (error);
335 	if ((error = copyout(&cm->cm_green[index], rcm->green, count)) != 0)
336 		return (error);
337 	if ((error = copyout(&cm->cm_blue[index], rcm->blue, count)) != 0)
338 		return (error);
339 
340 	return (0);
341 }
342 
343 int
344 agten_putcmap(struct agten_cmap *cm, struct wsdisplay_cmap *rcm)
345 {
346 	u_int index = rcm->index, count = rcm->count;
347 	int error;
348 
349 	if (index >= 256 || count > 256 - index)
350 		return (EINVAL);
351 
352 	if ((error = copyin(rcm->red, &cm->cm_red[index], count)) != 0)
353 		return (error);
354 	if ((error = copyin(rcm->green, &cm->cm_green[index], count)) != 0)
355 		return (error);
356 	if ((error = copyin(rcm->blue, &cm->cm_blue[index], count)) != 0)
357 		return (error);
358 
359 	return (0);
360 }
361 
362 static __inline__ void
363 ibm561_write(struct agten_softc *sc, u_int32_t reg, u_int32_t value)
364 {
365 	/*
366 	 * For some design reason the IBM561 PaletteDac needs to be fed
367 	 * values shifted left by 16 bits. What happened to simplicity?
368 	 */
369 	*(volatile u_int32_t *)(sc->sc_p9100 + P9100_RAMDAC_REGISTER(reg)) =
370 	    (value) << 16;
371 }
372 
373 void
374 agten_loadcmap(struct agten_softc *sc, u_int start, u_int ncolors)
375 {
376 	int i;
377 	u_int8_t *red, *green, *blue;
378 
379 	ibm561_write(sc, IBM561_ADDR_LOW,
380 	    (IBM561_CMAP_TABLE + start) & 0xff);
381 	ibm561_write(sc, IBM561_ADDR_HIGH,
382 	    ((IBM561_CMAP_TABLE + start) >> 8) & 0xff);
383 
384 	red = sc->sc_cmap.cm_red;
385 	green = sc->sc_cmap.cm_green;
386 	blue = sc->sc_cmap.cm_blue;
387 	for (i = start; i < start + ncolors; i++) {
388 		ibm561_write(sc, IBM561_CMD_CMAP, *red++);
389 		ibm561_write(sc, IBM561_CMD_CMAP, *green++);
390 		ibm561_write(sc, IBM561_CMD_CMAP, *blue++);
391 	}
392 }
393