leo.c revision 1.1 1 /* $NetBSD: leo.c,v 1.1 1998/08/18 07:45:09 leo Exp $ */
2
3 /*-
4 * Copyright (c) 1997 maximum entropy <entropy (at) 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 (at) 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, (caddr_t) vat.va_iobase,
167 vat.va_iosize);
168 printf("leo_match: cannot map memory area\n");
169 return 0;
170 }
171 found = leo_probe(&iot, &memt, &ioh, &memh,
172 vat.va_iosize, vat.va_msize);
173 bus_space_unmap(iot, (caddr_t) vat.va_iobase, vat.va_iosize);
174 bus_space_unmap(memt, (caddr_t) vat.va_maddr, vat.va_msize);
175 if (found) {
176 *va = vat;
177 return 1;
178 }
179 }
180 return 0;
181 }
182
183 static int
184 leo_probe(iot, memt, ioh, memh, iosize, msize)
185 bus_space_tag_t *iot, *memt;
186 bus_space_handle_t *ioh, *memh;
187 u_int iosize, msize;
188 {
189
190 /* Test that our highest register is within the io range. */
191 if (0xca > iosize) /* XXX */
192 return 0;
193 /* Test if we can peek each register. */
194 if (!bus_space_peek_1(*iot, *ioh, LEO_REG_MSBSCROLL))
195 return 0;
196 if (!bus_space_peek_1(*iot, *ioh, LEO_REG_LSBSCROLL))
197 return 0;
198 /*
199 * Write a test pattern at the start and end of the memory region,
200 * and test if the pattern can be read back. If so, the region is
201 * backed by memory (i.e. the card is present).
202 * On the Leonardo, the first byte of each longword isn't backed by
203 * physical memory, so we only compare the three low-order bytes
204 * with the test pattern.
205 */
206 bus_space_write_4(*memt, *memh, 0, 0xa5a5a5a5);
207 if ((bus_space_read_4(*memt, *memh, 0) & 0xffffff) != 0xa5a5a5)
208 return 0;
209 bus_space_write_4(*memt, *memh, msize - 4, 0xa5a5a5a5);
210 if ((bus_space_read_4(*memt, *memh, msize - 4) & 0xffffff)
211 != 0xa5a5a5)
212 return 0;
213 return 1;
214 }
215
216 static void
217 leo_attach(parent, self, aux)
218 struct device *parent, *self;
219 void *aux;
220 {
221 struct leo_softc *sc = (struct leo_softc *)self;
222 struct vme_attach_args *va = aux;
223 bus_space_handle_t ioh;
224 bus_space_handle_t memh;
225 #ifndef SET_REGION
226 int i;
227 #endif
228
229 printf("\n");
230 if (bus_space_map(va->va_iot, va->va_iobase, va->va_iosize, 0, &ioh))
231 panic("leo_attach: cannot map io area\n");
232 if (bus_space_map(va->va_memt, va->va_maddr, va->va_msize,
233 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE, &memh))
234 panic("leo_attach: cannot map memory area\n");
235 #ifdef SET_REGION /* XXX seems to be unimplemented on atari? */
236 bus_space_set_region_4(va->va_memt, memh, 0, 0, va->va_msize >> 2);
237 #else
238 for (i = 0; i < (va->va_msize >> 2); i++)
239 bus_space_write_4(va->va_memt, memh, i << 2, 0);
240 #endif
241 sc->sc_iot = va->va_iot;
242 sc->sc_ioh = ioh;
243 sc->sc_memt = va->va_memt;
244 sc->sc_memh = memh;
245 sc->sc_flags = 0;
246 sc->sc_maddr = va->va_maddr;
247 sc->sc_msize = va->va_msize;
248 leo_init(sc, 512);
249 leo_scroll(sc, 0);
250 }
251
252 int
253 leoopen(dev, flags, devtype, p)
254 dev_t dev;
255 int flags, devtype;
256 struct proc *p;
257 {
258 int unit = minor(dev);
259 struct leo_softc *sc;
260 int r;
261
262 if (unit >= leo_cd.cd_ndevs)
263 return ENXIO;
264 sc = leo_cd.cd_devs[unit];
265 if (!sc)
266 return ENXIO;
267 if (sc->sc_flags & LEO_SC_FLAGS_INUSE)
268 return EBUSY;
269 r = leo_init(sc, 512);
270 if (r != 0)
271 return r;
272 r = leo_scroll(sc, 0);
273 if (r != 0)
274 return r;
275 sc->sc_flags |= LEO_SC_FLAGS_INUSE;
276 return 0;
277 }
278
279 static int
280 leo_init(sc, ysize)
281 struct leo_softc *sc;
282 int ysize;
283 {
284
285 if ((ysize != 256) && (ysize != 384) && (ysize != 512))
286 return EINVAL;
287 /* XXX */
288 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x00, 0x6);
289 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x08, 0x0);
290 if (ysize == 384)
291 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x10);
292 else
293 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x11);
294 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x18, 0x0);
295 if (ysize == 384)
296 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x50);
297 else
298 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x51);
299 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x28, 0x0);
300 if (ysize == 384)
301 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x56);
302 else
303 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x57);
304 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x38, 0x0);
305 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x40, 0x6);
306 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x48, 0x0);
307 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x50, 0x25);
308 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x58, 0x0);
309 if (ysize == 256) {
310 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1f);
311 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x1);
312 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x29);
313 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
314 } else if (ysize == 384) {
315 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0xa5);
316 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x1);
317 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0xa7);
318 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
319 } else {
320 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1d);
321 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x2);
322 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x27);
323 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x2);
324 }
325 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb8, 0x10);
326 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb0, 0x10);
327 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x80, 0x4);
328 if (ysize == 384)
329 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x21);
330 else
331 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x20);
332 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc0, 0x40);
333 return 0;
334 }
335
336 static int
337 leo_scroll(sc, scroll)
338 struct leo_softc *sc;
339 int scroll;
340 {
341
342 if ((scroll < 0) || (scroll > 255))
343 return EINVAL;
344 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_MSBSCROLL,
345 (scroll >> 6) && 0xff);
346 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_LSBSCROLL,
347 (scroll << 2) && 0xff);
348 return 0;
349 }
350
351 int
352 leoclose(dev, flags, devtype, p)
353 dev_t dev;
354 int flags, devtype;
355 struct proc *p;
356 {
357 struct leo_softc *sc;
358
359 sc = leo_cd.cd_devs[minor(dev)];
360 sc->sc_flags &= ~LEO_SC_FLAGS_INUSE;
361 return 0;
362 }
363
364 #define SMALLBSIZE 32
365
366 static int
367 leomove(dev, uio, flags)
368 dev_t dev;
369 struct uio *uio;
370 int flags;
371 {
372 struct leo_softc *sc;
373 int length, size, error;
374 u_int8_t smallbuf[SMALLBSIZE];
375 off_t offset;
376
377 sc = leo_cd.cd_devs[minor(dev)];
378 if (uio->uio_offset > sc->sc_msize)
379 return 0;
380 length = sc->sc_msize - uio->uio_offset;
381 if (length > uio->uio_resid)
382 length = uio->uio_resid;
383 while (length > 0) {
384 size = length;
385 if (size > SMALLBSIZE)
386 size = SMALLBSIZE;
387 length -= size;
388 offset = uio->uio_offset;
389 if (uio->uio_rw == UIO_READ)
390 bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
391 offset, smallbuf, size);
392 if ((error = uiomove((caddr_t)smallbuf, size, uio)))
393 return (error);
394 if (uio->uio_rw == UIO_WRITE)
395 bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
396 offset, smallbuf, size);
397 }
398 return 0;
399 }
400
401 int
402 leoread(dev, uio, flags)
403 dev_t dev;
404 struct uio *uio;
405 int flags;
406 {
407
408 return leomove(dev, uio, flags);
409 }
410
411 int
412 leowrite(dev, uio, flags)
413 dev_t dev;
414 struct uio *uio;
415 int flags;
416 {
417
418 return leomove(dev, uio, flags);
419 }
420
421 int
422 leoioctl(dev, cmd, data, flags, p)
423 dev_t dev;
424 u_long cmd;
425 caddr_t data;
426 int flags;
427 struct proc *p;
428 {
429 struct leo_softc *sc;
430
431 sc = leo_cd.cd_devs[minor(dev)];
432 switch (cmd) {
433 case LIOCYRES:
434 return leo_init(sc, *(int *)data);
435 break;
436 case LIOCSCRL:
437 return leo_scroll(sc, *(int *)data);
438 break;
439 default:
440 return EINVAL;
441 break;
442 }
443 }
444
445 int
446 leommap(dev, offset, prot)
447 dev_t dev;
448 int offset;
449 int prot;
450 {
451 struct leo_softc *sc;
452
453 sc = leo_cd.cd_devs[minor(dev)];
454 if (offset >= 0 && offset < sc->sc_msize)
455 return m68k_btop(sc->sc_maddr + offset);
456 return -1;
457 }
458