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