Home | History | Annotate | Line # | Download | only in vme
leo.c revision 1.3.2.1
      1 /*	$NetBSD: leo.c,v 1.3.2.1 2001/10/10 11:56:00 fvdl 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 <sys/vnode.h>
     64 #include <machine/cpu.h>
     65 #include <machine/bus.h>
     66 #include <machine/iomap.h>
     67 #include <machine/scu.h>
     68 #include <atari/vme/vmevar.h>
     69 #include <atari/vme/leovar.h>
     70 #include <atari/vme/leoioctl.h>
     71 
     72 static struct leo_addresses {
     73 	u_long reg_addr;
     74 	u_int reg_size;
     75 	u_long mem_addr;
     76 	u_int mem_size;
     77 } leostd[] = {
     78 	{ 0xfed90000, 0x100, 0xfec00000, 0x100000 }
     79 };
     80 
     81 #define NLEOSTD (sizeof(leostd) / sizeof(leostd[0]))
     82 
     83 struct leo_softc {
     84 	struct device sc_dev;		/* XXX what goes here? */
     85 	bus_space_tag_t sc_iot;
     86 	bus_space_tag_t sc_memt;
     87 	bus_space_handle_t sc_ioh;
     88 	bus_space_handle_t sc_memh;
     89 	int sc_flags;
     90 	int sc_maddr;
     91 	u_int sc_msize;
     92 };
     93 
     94 #define LEO_SC_FLAGS_INUSE 1
     95 
     96 static int leo_match __P((struct device *, struct cfdata *, void *));
     97 static void leo_attach __P((struct device *, struct device *, void *));
     98 static int leo_probe __P((bus_space_tag_t *, bus_space_tag_t *,
     99 			  bus_space_handle_t *, bus_space_handle_t *,
    100 			  u_int, u_int));
    101 static int leo_init __P((struct leo_softc *, int));
    102 static int leo_scroll __P((struct leo_softc *, int));
    103 static int leomove __P((struct vnode *, struct uio *, int));
    104 
    105 dev_decl(leo,open);
    106 dev_decl(leo,close);
    107 dev_decl(leo,read);
    108 dev_decl(leo,write);
    109 dev_decl(leo,ioctl);
    110 dev_decl(leo,mmap);
    111 
    112 struct cfattach leo_ca = {
    113 	sizeof(struct leo_softc), leo_match, leo_attach
    114 };
    115 
    116 extern struct cfdriver leo_cd;
    117 
    118 static int
    119 leo_match(parent, cfp, aux)
    120 	struct device *parent;
    121 	struct cfdata *cfp;
    122 	void *aux;
    123 {
    124 	struct vme_attach_args *va = aux;
    125 	int i;
    126 	bus_space_tag_t iot;
    127 	bus_space_tag_t memt;
    128 	bus_space_handle_t ioh;
    129 	bus_space_handle_t memh;
    130 
    131 	/*
    132 	 * We are passed our configuration in the attachment arguments.
    133 	 * The configuration information may be partially unspecified.
    134 	 * For any unspecified configuration parameters, we fill in those
    135 	 * parameters with data for a "standard" configuration.
    136 	 * Once we have a fully specified configuration, we try to probe
    137 	 * a card with that configuration.
    138 	 * The Leonardo only has one configuration and it isn't likely
    139 	 * to change, but this routine doesn't assume that's the case.
    140 	 */
    141 	iot = va->va_iot;
    142 	memt = va->va_memt;
    143 	for (i = 0; i < NLEOSTD; i++) {
    144 		struct leo_addresses *leo_ap = &leostd[i];
    145 		int found = 0;
    146 		struct vme_attach_args vat = *va;
    147 
    148 		if (vat.va_irq != VMECF_IRQ_DEFAULT) {
    149 			printf("leo_match: config error: no irq support\n");
    150 			return 0;
    151 		}
    152 		if (vat.va_iobase == VMECF_IOPORT_DEFAULT)
    153 			vat.va_iobase = leo_ap->reg_addr;
    154 		if (vat.va_maddr == VMECF_MEM_DEFAULT)
    155 			vat.va_maddr = leo_ap->mem_addr;
    156 		if (vat.va_iosize == VMECF_IOSIZE_DEFAULT)
    157 			vat.va_iosize = leo_ap->reg_size;
    158 		if (vat.va_msize == VMECF_MEMSIZ_DEFAULT)
    159 			vat.va_msize = leo_ap->mem_size;
    160 		if (bus_space_map(iot, vat.va_iobase, vat.va_iosize, 0, &ioh)) {
    161 			printf("leo_match: cannot map io area\n");
    162 			return 0;
    163 		}
    164 		if (bus_space_map(memt, vat.va_maddr, vat.va_msize,
    165 			  	  BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE,
    166 			  	  &memh)) {
    167 			bus_space_unmap(iot, ioh, 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, ioh, vat.va_iosize);
    174 		bus_space_unmap(memt, memh, 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(devvp, flags, devtype, p)
    254 	struct vnode *devvp;
    255 	int flags, devtype;
    256 	struct proc *p;
    257 {
    258 	dev_t dev = vdev_rdev(devvp);
    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 
    277 	vdev_setprivdata(devvp, sc);
    278 
    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(devvp, flags, devtype, p)
    357 	struct vnode *devvp;
    358 	int flags, devtype;
    359 	struct proc *p;
    360 {
    361 	struct leo_softc *sc;
    362 
    363 	sc = vdev_privdata(devvp);
    364 	sc->sc_flags &= ~LEO_SC_FLAGS_INUSE;
    365 	return 0;
    366 }
    367 
    368 #define SMALLBSIZE      32
    369 
    370 static int
    371 leomove(devvp, uio, flags)
    372 	struct vnode *devvp;
    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 = vdev_privdata(devvp);
    382 
    383         if (uio->uio_offset > sc->sc_msize)
    384                 return 0;
    385         length = sc->sc_msize - uio->uio_offset;
    386         if (length > uio->uio_resid)
    387                 length = uio->uio_resid;
    388         while (length > 0) {
    389                 size = length;
    390                 if (size > SMALLBSIZE)
    391                         size = SMALLBSIZE;
    392                 length -= size;
    393 		offset = uio->uio_offset;
    394                 if (uio->uio_rw == UIO_READ)
    395                         bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
    396                                         offset, smallbuf, size);
    397                 if ((error = uiomove((caddr_t)smallbuf, size, uio)))
    398                         return (error);
    399                 if (uio->uio_rw == UIO_WRITE)
    400                         bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
    401                                         offset, smallbuf, size);
    402         }
    403         return 0;
    404 }
    405 
    406 int
    407 leoread(devvp, uio, flags)
    408 	struct vnode *devvp;
    409 	struct uio *uio;
    410 	int flags;
    411 {
    412 
    413 	return leomove(devvp, uio, flags);
    414 }
    415 
    416 int
    417 leowrite(devvp, uio, flags)
    418 	struct vnode *devvp;
    419 	struct uio *uio;
    420 	int flags;
    421 {
    422 
    423 	return leomove(devvp, uio, flags);
    424 }
    425 
    426 int
    427 leoioctl(devvp, cmd, data, flags, p)
    428 	struct vnode *devvp;
    429 	u_long cmd;
    430 	caddr_t data;
    431 	int flags;
    432 	struct proc *p;
    433 {
    434 	struct leo_softc *sc;
    435 
    436 	sc = vdev_privdata(devvp);
    437 
    438         switch (cmd) {
    439         case LIOCYRES:
    440 		return leo_init(sc, *(int *)data);
    441 		break;
    442         case LIOCSCRL:
    443 		return leo_scroll(sc, *(int *)data);
    444 		break;
    445 	default:
    446 		return EINVAL;
    447 		break;
    448 	}
    449 }
    450 
    451 paddr_t
    452 leommap(devvp, offset, prot)
    453 	struct vnode *devvp;
    454 	off_t offset;
    455 	int prot;
    456 {
    457 	struct leo_softc *sc;
    458 
    459 	sc = vdev_privdata(devvp);
    460 	if (offset >= 0 && offset < sc->sc_msize)
    461 		return m68k_btop(sc->sc_maddr + offset);
    462 	return -1;
    463 }
    464