xref: /netbsd-src/sys/arch/atari/vme/leo.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: leo.c,v 1.4 2002/09/06 13:18:43 gehenna 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 struct cfattach leo_ca = {
104 	sizeof(struct leo_softc), leo_match, leo_attach
105 };
106 
107 extern struct cfdriver leo_cd;
108 
109 dev_type_open(leoopen);
110 dev_type_close(leoclose);
111 dev_type_read(leomove);
112 dev_type_ioctl(leoioctl);
113 dev_type_mmap(leommap);
114 
115 const struct cdevsw leo_cdevsw = {
116 	leoopen, leoclose, leomove, leomove, leoioctl,
117 	nostop, notty, nopoll, leommap,
118 };
119 
120 static int
121 leo_match(parent, cfp, aux)
122 	struct device *parent;
123 	struct cfdata *cfp;
124 	void *aux;
125 {
126 	struct vme_attach_args *va = aux;
127 	int i;
128 	bus_space_tag_t iot;
129 	bus_space_tag_t memt;
130 	bus_space_handle_t ioh;
131 	bus_space_handle_t memh;
132 
133 	/*
134 	 * We are passed our configuration in the attachment arguments.
135 	 * The configuration information may be partially unspecified.
136 	 * For any unspecified configuration parameters, we fill in those
137 	 * parameters with data for a "standard" configuration.
138 	 * Once we have a fully specified configuration, we try to probe
139 	 * a card with that configuration.
140 	 * The Leonardo only has one configuration and it isn't likely
141 	 * to change, but this routine doesn't assume that's the case.
142 	 */
143 	iot = va->va_iot;
144 	memt = va->va_memt;
145 	for (i = 0; i < NLEOSTD; i++) {
146 		struct leo_addresses *leo_ap = &leostd[i];
147 		int found = 0;
148 		struct vme_attach_args vat = *va;
149 
150 		if (vat.va_irq != VMECF_IRQ_DEFAULT) {
151 			printf("leo_match: config error: no irq support\n");
152 			return 0;
153 		}
154 		if (vat.va_iobase == VMECF_IOPORT_DEFAULT)
155 			vat.va_iobase = leo_ap->reg_addr;
156 		if (vat.va_maddr == VMECF_MEM_DEFAULT)
157 			vat.va_maddr = leo_ap->mem_addr;
158 		if (vat.va_iosize == VMECF_IOSIZE_DEFAULT)
159 			vat.va_iosize = leo_ap->reg_size;
160 		if (vat.va_msize == VMECF_MEMSIZ_DEFAULT)
161 			vat.va_msize = leo_ap->mem_size;
162 		if (bus_space_map(iot, vat.va_iobase, vat.va_iosize, 0, &ioh)) {
163 			printf("leo_match: cannot map io area\n");
164 			return 0;
165 		}
166 		if (bus_space_map(memt, vat.va_maddr, vat.va_msize,
167 			  	  BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE,
168 			  	  &memh)) {
169 			bus_space_unmap(iot, ioh, vat.va_iosize);
170 			printf("leo_match: cannot map memory area\n");
171 			return 0;
172 		}
173 		found = leo_probe(&iot, &memt, &ioh, &memh,
174 				  vat.va_iosize, vat.va_msize);
175 		bus_space_unmap(iot, ioh, vat.va_iosize);
176 		bus_space_unmap(memt, memh, vat.va_msize);
177 		if (found) {
178 			*va = vat;
179 			return 1;
180 		}
181 	}
182 	return 0;
183 }
184 
185 static int
186 leo_probe(iot, memt, ioh, memh, iosize, msize)
187 	bus_space_tag_t *iot, *memt;
188 	bus_space_handle_t *ioh, *memh;
189 	u_int iosize, msize;
190 {
191 
192 	/* Test that our highest register is within the io range. */
193 	if (0xca > iosize) /* XXX */
194 		return 0;
195 	/* Test if we can peek each register. */
196 	if (!bus_space_peek_1(*iot, *ioh, LEO_REG_MSBSCROLL))
197 		return 0;
198 	if (!bus_space_peek_1(*iot, *ioh, LEO_REG_LSBSCROLL))
199 		return 0;
200 	/*
201 	 * Write a test pattern at the start and end of the memory region,
202 	 * and test if the pattern can be read back.  If so, the region is
203 	 * backed by memory (i.e. the card is present).
204 	 * On the Leonardo, the first byte of each longword isn't backed by
205 	 * physical memory, so we only compare the three low-order bytes
206 	 * with the test pattern.
207 	 */
208 	bus_space_write_4(*memt, *memh, 0, 0xa5a5a5a5);
209 	if ((bus_space_read_4(*memt, *memh, 0) & 0xffffff) != 0xa5a5a5)
210 		return 0;
211 	bus_space_write_4(*memt, *memh, msize - 4, 0xa5a5a5a5);
212 	if ((bus_space_read_4(*memt, *memh, msize - 4) & 0xffffff)
213 		!= 0xa5a5a5)
214 		return 0;
215 	return 1;
216 }
217 
218 static void
219 leo_attach(parent, self, aux)
220 	struct device *parent, *self;
221 	void *aux;
222 {
223 	struct leo_softc *sc = (struct leo_softc *)self;
224 	struct vme_attach_args *va = aux;
225 	bus_space_handle_t ioh;
226 	bus_space_handle_t memh;
227 #ifndef SET_REGION
228 	int i;
229 #endif
230 
231 	printf("\n");
232 	if (bus_space_map(va->va_iot, va->va_iobase, va->va_iosize, 0, &ioh))
233 		panic("leo_attach: cannot map io area\n");
234 	if (bus_space_map(va->va_memt, va->va_maddr, va->va_msize,
235 			  BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE, &memh))
236 		panic("leo_attach: cannot map memory area\n");
237 #ifdef SET_REGION /* XXX seems to be unimplemented on atari? */
238 	bus_space_set_region_4(va->va_memt, memh, 0, 0, va->va_msize >> 2);
239 #else
240 	for (i = 0; i < (va->va_msize >> 2); i++)
241 		bus_space_write_4(va->va_memt, memh, i << 2, 0);
242 #endif
243 	sc->sc_iot = va->va_iot;
244 	sc->sc_ioh = ioh;
245 	sc->sc_memt = va->va_memt;
246 	sc->sc_memh = memh;
247 	sc->sc_flags = 0;
248 	sc->sc_maddr = va->va_maddr;
249 	sc->sc_msize = va->va_msize;
250 	leo_init(sc, 512);
251 	leo_scroll(sc, 0);
252 }
253 
254 int
255 leoopen(dev, flags, devtype, p)
256 	dev_t dev;
257 	int flags, devtype;
258 	struct proc *p;
259 {
260 	int unit = minor(dev);
261 	struct leo_softc *sc;
262 	int r;
263 
264 	if (unit >= leo_cd.cd_ndevs)
265 		return ENXIO;
266 	sc = leo_cd.cd_devs[unit];
267 	if (!sc)
268 		return ENXIO;
269 	if (sc->sc_flags & LEO_SC_FLAGS_INUSE)
270 		return EBUSY;
271 	r = leo_init(sc, 512);
272 	if (r != 0)
273 		return r;
274 	r = leo_scroll(sc, 0);
275 	if (r != 0)
276 		return r;
277 	sc->sc_flags |= LEO_SC_FLAGS_INUSE;
278 	return 0;
279 }
280 
281 static int
282 leo_init(sc, ysize)
283 	struct leo_softc *sc;
284 	int ysize;
285 {
286 
287 	if ((ysize != 256) && (ysize != 384) && (ysize != 512))
288 		return EINVAL;
289 	/* XXX */
290 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x00, 0x6);
291 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x08, 0x0);
292 	if (ysize == 384)
293 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x10);
294 	else
295 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x11);
296 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x18, 0x0);
297 	if (ysize == 384)
298 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x50);
299 	else
300 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x51);
301 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x28, 0x0);
302 	if (ysize == 384)
303 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x56);
304 	else
305 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x57);
306 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x38, 0x0);
307 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x40, 0x6);
308 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x48, 0x0);
309 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x50, 0x25);
310 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x58, 0x0);
311 	if (ysize == 256) {
312 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1f);
313 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x1);
314 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x29);
315 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
316 	} else if (ysize == 384) {
317 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0xa5);
318 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x1);
319 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0xa7);
320 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
321 	} else {
322 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1d);
323 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x2);
324 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x27);
325 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x2);
326 	}
327 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb8, 0x10);
328 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb0, 0x10);
329 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x80, 0x4);
330 	if (ysize == 384)
331 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x21);
332 	else
333 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x20);
334 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc0, 0x40);
335 	return 0;
336 }
337 
338 static int
339 leo_scroll(sc, scroll)
340 	struct leo_softc *sc;
341 	int scroll;
342 {
343 
344 	if ((scroll < 0) || (scroll > 255))
345 		return EINVAL;
346         bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_MSBSCROLL,
347 			  (scroll >> 6) && 0xff);
348         bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_LSBSCROLL,
349 			  (scroll << 2) && 0xff);
350 	return 0;
351 }
352 
353 int
354 leoclose(dev, flags, devtype, p)
355 	dev_t dev;
356 	int flags, devtype;
357 	struct proc *p;
358 {
359 	struct leo_softc *sc;
360 
361 	sc = leo_cd.cd_devs[minor(dev)];
362 	sc->sc_flags &= ~LEO_SC_FLAGS_INUSE;
363 	return 0;
364 }
365 
366 #define SMALLBSIZE      32
367 
368 int
369 leomove(dev, uio, flags)
370         dev_t dev;
371         struct uio *uio;
372         int flags;
373 {
374         struct leo_softc *sc;
375         int length, size, error;
376         u_int8_t smallbuf[SMALLBSIZE];
377 	off_t offset;
378 
379         sc = leo_cd.cd_devs[minor(dev)];
380         if (uio->uio_offset > sc->sc_msize)
381                 return 0;
382         length = sc->sc_msize - uio->uio_offset;
383         if (length > uio->uio_resid)
384                 length = uio->uio_resid;
385         while (length > 0) {
386                 size = length;
387                 if (size > SMALLBSIZE)
388                         size = SMALLBSIZE;
389                 length -= size;
390 		offset = uio->uio_offset;
391                 if (uio->uio_rw == UIO_READ)
392                         bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
393                                         offset, smallbuf, size);
394                 if ((error = uiomove((caddr_t)smallbuf, size, uio)))
395                         return (error);
396                 if (uio->uio_rw == UIO_WRITE)
397                         bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
398                                         offset, smallbuf, size);
399         }
400         return 0;
401 }
402 
403 int
404 leoioctl(dev, cmd, data, flags, p)
405 	dev_t dev;
406 	u_long cmd;
407 	caddr_t data;
408 	int flags;
409 	struct proc *p;
410 {
411 	struct leo_softc *sc;
412 
413 	sc = leo_cd.cd_devs[minor(dev)];
414         switch (cmd) {
415         case LIOCYRES:
416 		return leo_init(sc, *(int *)data);
417 		break;
418         case LIOCSCRL:
419 		return leo_scroll(sc, *(int *)data);
420 		break;
421 	default:
422 		return EINVAL;
423 		break;
424 	}
425 }
426 
427 paddr_t
428 leommap(dev, offset, prot)
429 	dev_t dev;
430 	off_t offset;
431 	int prot;
432 {
433 	struct leo_softc *sc;
434 
435 	sc = leo_cd.cd_devs[minor(dev)];
436 	if (offset >= 0 && offset < sc->sc_msize)
437 		return m68k_btop(sc->sc_maddr + offset);
438 	return -1;
439 }
440