xref: /netbsd/sys/arch/atari/vme/leo.c (revision c4a72b64)
1 /*	$NetBSD: leo.c,v 1.8 2002/10/23 09:10:55 jdolecek Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 maximum entropy <entropy@zippy.bernstein.com>
5  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *        This product includes software developed by the NetBSD
19  *        Foundation, Inc. and its contributors.
20  * 4. Neither the name of The NetBSD Foundation nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Driver for the Circad Leonardo 1.2 from Lexicor, a 24-bit true color
39  * VME graphics card based on the Texas Instruments TMS34061.
40  *
41  * Written by maximum entropy <entropy@zippy.bernstein.com>, December 5, 1997.
42  *
43  * This driver was written from scratch, but I referred to several other
44  * drivers in the NetBSD distribution as examples.  The file I referred to
45  * the most was /sys/arch/atari/vme/if_le_vme.c.  Due credits:
46  * Copyright (c) 1997 Leo Weppelman.  All rights reserved.
47  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
48  * Copyright (c) 1992, 1993
49  *	The Regents of the University of California.  All rights reserved.
50  * This code is derived from software contributed to Berkeley by
51  * 	Ralph Campbell and Rick Macklem.
52  * This product includes software developed by the University of
53  *	California, Berkeley and its contributors.
54  */
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/errno.h>
60 #include <sys/device.h>
61 #include <sys/conf.h>
62 #include <sys/ioctl.h>
63 #include <machine/cpu.h>
64 #include <machine/bus.h>
65 #include <machine/iomap.h>
66 #include <machine/scu.h>
67 #include <atari/vme/vmevar.h>
68 #include <atari/vme/leovar.h>
69 #include <atari/vme/leoioctl.h>
70 
71 static struct leo_addresses {
72 	u_long reg_addr;
73 	u_int reg_size;
74 	u_long mem_addr;
75 	u_int mem_size;
76 } leostd[] = {
77 	{ 0xfed90000, 0x100, 0xfec00000, 0x100000 }
78 };
79 
80 #define NLEOSTD (sizeof(leostd) / sizeof(leostd[0]))
81 
82 struct leo_softc {
83 	struct device sc_dev;		/* XXX what goes here? */
84 	bus_space_tag_t sc_iot;
85 	bus_space_tag_t sc_memt;
86 	bus_space_handle_t sc_ioh;
87 	bus_space_handle_t sc_memh;
88 	int sc_flags;
89 	int sc_maddr;
90 	u_int sc_msize;
91 };
92 
93 #define LEO_SC_FLAGS_INUSE 1
94 
95 static int leo_match __P((struct device *, struct cfdata *, void *));
96 static void leo_attach __P((struct device *, struct device *, void *));
97 static int leo_probe __P((bus_space_tag_t *, bus_space_tag_t *,
98 			  bus_space_handle_t *, bus_space_handle_t *,
99 			  u_int, u_int));
100 static int leo_init __P((struct leo_softc *, int));
101 static int leo_scroll __P((struct leo_softc *, int));
102 
103 CFATTACH_DECL(leo, sizeof(struct leo_softc),
104     leo_match, leo_attach, NULL, NULL);
105 
106 extern struct cfdriver leo_cd;
107 
108 dev_type_open(leoopen);
109 dev_type_close(leoclose);
110 dev_type_read(leomove);
111 dev_type_ioctl(leoioctl);
112 dev_type_mmap(leommap);
113 
114 const struct cdevsw leo_cdevsw = {
115 	leoopen, leoclose, leomove, leomove, leoioctl,
116 	nostop, notty, nopoll, leommap, nokqfilter,
117 };
118 
119 static int
120 leo_match(parent, cfp, aux)
121 	struct device *parent;
122 	struct cfdata *cfp;
123 	void *aux;
124 {
125 	struct vme_attach_args *va = aux;
126 	int i;
127 	bus_space_tag_t iot;
128 	bus_space_tag_t memt;
129 	bus_space_handle_t ioh;
130 	bus_space_handle_t memh;
131 
132 	/*
133 	 * We are passed our configuration in the attachment arguments.
134 	 * The configuration information may be partially unspecified.
135 	 * For any unspecified configuration parameters, we fill in those
136 	 * parameters with data for a "standard" configuration.
137 	 * Once we have a fully specified configuration, we try to probe
138 	 * a card with that configuration.
139 	 * The Leonardo only has one configuration and it isn't likely
140 	 * to change, but this routine doesn't assume that's the case.
141 	 */
142 	iot = va->va_iot;
143 	memt = va->va_memt;
144 	for (i = 0; i < NLEOSTD; i++) {
145 		struct leo_addresses *leo_ap = &leostd[i];
146 		int found = 0;
147 		struct vme_attach_args vat = *va;
148 
149 		if (vat.va_irq != VMECF_IRQ_DEFAULT) {
150 			printf("leo_match: config error: no irq support\n");
151 			return 0;
152 		}
153 		if (vat.va_iobase == VMECF_IOPORT_DEFAULT)
154 			vat.va_iobase = leo_ap->reg_addr;
155 		if (vat.va_maddr == VMECF_MEM_DEFAULT)
156 			vat.va_maddr = leo_ap->mem_addr;
157 		if (vat.va_iosize == VMECF_IOSIZE_DEFAULT)
158 			vat.va_iosize = leo_ap->reg_size;
159 		if (vat.va_msize == VMECF_MEMSIZ_DEFAULT)
160 			vat.va_msize = leo_ap->mem_size;
161 		if (bus_space_map(iot, vat.va_iobase, vat.va_iosize, 0, &ioh)) {
162 			printf("leo_match: cannot map io area\n");
163 			return 0;
164 		}
165 		if (bus_space_map(memt, vat.va_maddr, vat.va_msize,
166 			  	  BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE,
167 			  	  &memh)) {
168 			bus_space_unmap(iot, ioh, vat.va_iosize);
169 			printf("leo_match: cannot map memory area\n");
170 			return 0;
171 		}
172 		found = leo_probe(&iot, &memt, &ioh, &memh,
173 				  vat.va_iosize, vat.va_msize);
174 		bus_space_unmap(iot, ioh, vat.va_iosize);
175 		bus_space_unmap(memt, memh, vat.va_msize);
176 		if (found) {
177 			*va = vat;
178 			return 1;
179 		}
180 	}
181 	return 0;
182 }
183 
184 static int
185 leo_probe(iot, memt, ioh, memh, iosize, msize)
186 	bus_space_tag_t *iot, *memt;
187 	bus_space_handle_t *ioh, *memh;
188 	u_int iosize, msize;
189 {
190 
191 	/* Test that our highest register is within the io range. */
192 	if (0xca > iosize) /* XXX */
193 		return 0;
194 	/* Test if we can peek each register. */
195 	if (!bus_space_peek_1(*iot, *ioh, LEO_REG_MSBSCROLL))
196 		return 0;
197 	if (!bus_space_peek_1(*iot, *ioh, LEO_REG_LSBSCROLL))
198 		return 0;
199 	/*
200 	 * Write a test pattern at the start and end of the memory region,
201 	 * and test if the pattern can be read back.  If so, the region is
202 	 * backed by memory (i.e. the card is present).
203 	 * On the Leonardo, the first byte of each longword isn't backed by
204 	 * physical memory, so we only compare the three low-order bytes
205 	 * with the test pattern.
206 	 */
207 	bus_space_write_4(*memt, *memh, 0, 0xa5a5a5a5);
208 	if ((bus_space_read_4(*memt, *memh, 0) & 0xffffff) != 0xa5a5a5)
209 		return 0;
210 	bus_space_write_4(*memt, *memh, msize - 4, 0xa5a5a5a5);
211 	if ((bus_space_read_4(*memt, *memh, msize - 4) & 0xffffff)
212 		!= 0xa5a5a5)
213 		return 0;
214 	return 1;
215 }
216 
217 static void
218 leo_attach(parent, self, aux)
219 	struct device *parent, *self;
220 	void *aux;
221 {
222 	struct leo_softc *sc = (struct leo_softc *)self;
223 	struct vme_attach_args *va = aux;
224 	bus_space_handle_t ioh;
225 	bus_space_handle_t memh;
226 #ifndef SET_REGION
227 	int i;
228 #endif
229 
230 	printf("\n");
231 	if (bus_space_map(va->va_iot, va->va_iobase, va->va_iosize, 0, &ioh))
232 		panic("leo_attach: cannot map io area");
233 	if (bus_space_map(va->va_memt, va->va_maddr, va->va_msize,
234 			  BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE, &memh))
235 		panic("leo_attach: cannot map memory area");
236 #ifdef SET_REGION /* XXX seems to be unimplemented on atari? */
237 	bus_space_set_region_4(va->va_memt, memh, 0, 0, va->va_msize >> 2);
238 #else
239 	for (i = 0; i < (va->va_msize >> 2); i++)
240 		bus_space_write_4(va->va_memt, memh, i << 2, 0);
241 #endif
242 	sc->sc_iot = va->va_iot;
243 	sc->sc_ioh = ioh;
244 	sc->sc_memt = va->va_memt;
245 	sc->sc_memh = memh;
246 	sc->sc_flags = 0;
247 	sc->sc_maddr = va->va_maddr;
248 	sc->sc_msize = va->va_msize;
249 	leo_init(sc, 512);
250 	leo_scroll(sc, 0);
251 }
252 
253 int
254 leoopen(dev, flags, devtype, p)
255 	dev_t dev;
256 	int flags, devtype;
257 	struct proc *p;
258 {
259 	int unit = minor(dev);
260 	struct leo_softc *sc;
261 	int r;
262 
263 	if (unit >= leo_cd.cd_ndevs)
264 		return ENXIO;
265 	sc = leo_cd.cd_devs[unit];
266 	if (!sc)
267 		return ENXIO;
268 	if (sc->sc_flags & LEO_SC_FLAGS_INUSE)
269 		return EBUSY;
270 	r = leo_init(sc, 512);
271 	if (r != 0)
272 		return r;
273 	r = leo_scroll(sc, 0);
274 	if (r != 0)
275 		return r;
276 	sc->sc_flags |= LEO_SC_FLAGS_INUSE;
277 	return 0;
278 }
279 
280 static int
281 leo_init(sc, ysize)
282 	struct leo_softc *sc;
283 	int ysize;
284 {
285 
286 	if ((ysize != 256) && (ysize != 384) && (ysize != 512))
287 		return EINVAL;
288 	/* XXX */
289 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x00, 0x6);
290 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x08, 0x0);
291 	if (ysize == 384)
292 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x10);
293 	else
294 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x11);
295 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x18, 0x0);
296 	if (ysize == 384)
297 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x50);
298 	else
299 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x51);
300 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x28, 0x0);
301 	if (ysize == 384)
302 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x56);
303 	else
304 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x57);
305 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x38, 0x0);
306 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x40, 0x6);
307 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x48, 0x0);
308 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x50, 0x25);
309 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x58, 0x0);
310 	if (ysize == 256) {
311 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1f);
312 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x1);
313 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x29);
314 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
315 	} else if (ysize == 384) {
316 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0xa5);
317 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x1);
318 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0xa7);
319 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
320 	} else {
321 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1d);
322 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x2);
323 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x27);
324 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x2);
325 	}
326 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb8, 0x10);
327 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb0, 0x10);
328 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x80, 0x4);
329 	if (ysize == 384)
330 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x21);
331 	else
332 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x20);
333 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc0, 0x40);
334 	return 0;
335 }
336 
337 static int
338 leo_scroll(sc, scroll)
339 	struct leo_softc *sc;
340 	int scroll;
341 {
342 
343 	if ((scroll < 0) || (scroll > 255))
344 		return EINVAL;
345         bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_MSBSCROLL,
346 			  (scroll >> 6) && 0xff);
347         bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_LSBSCROLL,
348 			  (scroll << 2) && 0xff);
349 	return 0;
350 }
351 
352 int
353 leoclose(dev, flags, devtype, p)
354 	dev_t dev;
355 	int flags, devtype;
356 	struct proc *p;
357 {
358 	struct leo_softc *sc;
359 
360 	sc = leo_cd.cd_devs[minor(dev)];
361 	sc->sc_flags &= ~LEO_SC_FLAGS_INUSE;
362 	return 0;
363 }
364 
365 #define SMALLBSIZE      32
366 
367 int
368 leomove(dev, uio, flags)
369         dev_t dev;
370         struct uio *uio;
371         int flags;
372 {
373         struct leo_softc *sc;
374         int length, size, error;
375         u_int8_t smallbuf[SMALLBSIZE];
376 	off_t offset;
377 
378         sc = leo_cd.cd_devs[minor(dev)];
379         if (uio->uio_offset > sc->sc_msize)
380                 return 0;
381         length = sc->sc_msize - uio->uio_offset;
382         if (length > uio->uio_resid)
383                 length = uio->uio_resid;
384         while (length > 0) {
385                 size = length;
386                 if (size > SMALLBSIZE)
387                         size = SMALLBSIZE;
388                 length -= size;
389 		offset = uio->uio_offset;
390                 if (uio->uio_rw == UIO_READ)
391                         bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
392                                         offset, smallbuf, size);
393                 if ((error = uiomove((caddr_t)smallbuf, size, uio)))
394                         return (error);
395                 if (uio->uio_rw == UIO_WRITE)
396                         bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
397                                         offset, smallbuf, size);
398         }
399         return 0;
400 }
401 
402 int
403 leoioctl(dev, cmd, data, flags, p)
404 	dev_t dev;
405 	u_long cmd;
406 	caddr_t data;
407 	int flags;
408 	struct proc *p;
409 {
410 	struct leo_softc *sc;
411 
412 	sc = leo_cd.cd_devs[minor(dev)];
413         switch (cmd) {
414         case LIOCYRES:
415 		return leo_init(sc, *(int *)data);
416 		break;
417         case LIOCSCRL:
418 		return leo_scroll(sc, *(int *)data);
419 		break;
420 	default:
421 		return EINVAL;
422 		break;
423 	}
424 }
425 
426 paddr_t
427 leommap(dev, offset, prot)
428 	dev_t dev;
429 	off_t offset;
430 	int prot;
431 {
432 	struct leo_softc *sc;
433 
434 	sc = leo_cd.cd_devs[minor(dev)];
435 	if (offset >= 0 && offset < sc->sc_msize)
436 		return m68k_btop(sc->sc_maddr + offset);
437 	return -1;
438 }
439