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