Home | History | Annotate | Line # | Download | only in vme
leo.c revision 1.12
      1 /*	$NetBSD: leo.c,v 1.12 2008/04/28 20:23:15 martin 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  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Driver for the Circad Leonardo 1.2 from Lexicor, a 24-bit true color
     32  * VME graphics card based on the Texas Instruments TMS34061.
     33  *
     34  * Written by maximum entropy <entropy (at) zippy.bernstein.com>, December 5, 1997.
     35  *
     36  * This driver was written from scratch, but I referred to several other
     37  * drivers in the NetBSD distribution as examples.  The file I referred to
     38  * the most was /sys/arch/atari/vme/if_le_vme.c.  Due credits:
     39  * Copyright (c) 1997 Leo Weppelman.  All rights reserved.
     40  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
     41  * Copyright (c) 1992, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  * This code is derived from software contributed to Berkeley by
     44  * 	Ralph Campbell and Rick Macklem.
     45  * This product includes software developed by the University of
     46  *	California, Berkeley and its contributors.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: leo.c,v 1.12 2008/04/28 20:23:15 martin Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/proc.h>
     55 #include <sys/errno.h>
     56 #include <sys/device.h>
     57 #include <sys/conf.h>
     58 #include <sys/ioctl.h>
     59 #include <machine/cpu.h>
     60 #include <machine/bus.h>
     61 #include <machine/iomap.h>
     62 #include <machine/scu.h>
     63 #include <atari/vme/vmevar.h>
     64 #include <atari/vme/leovar.h>
     65 #include <atari/vme/leoioctl.h>
     66 
     67 static struct leo_addresses {
     68 	u_long reg_addr;
     69 	u_int reg_size;
     70 	u_long mem_addr;
     71 	u_int mem_size;
     72 } leostd[] = {
     73 	{ 0xfed90000, 0x100, 0xfec00000, 0x100000 }
     74 };
     75 
     76 #define NLEOSTD (sizeof(leostd) / sizeof(leostd[0]))
     77 
     78 struct leo_softc {
     79 	struct device sc_dev;		/* XXX what goes here? */
     80 	bus_space_tag_t sc_iot;
     81 	bus_space_tag_t sc_memt;
     82 	bus_space_handle_t sc_ioh;
     83 	bus_space_handle_t sc_memh;
     84 	int sc_flags;
     85 	int sc_maddr;
     86 	u_int sc_msize;
     87 };
     88 
     89 #define LEO_SC_FLAGS_INUSE 1
     90 
     91 static int leo_match __P((struct device *, struct cfdata *, void *));
     92 static void leo_attach __P((struct device *, struct device *, void *));
     93 static int leo_probe __P((bus_space_tag_t *, bus_space_tag_t *,
     94 			  bus_space_handle_t *, bus_space_handle_t *,
     95 			  u_int, u_int));
     96 static int leo_init __P((struct leo_softc *, int));
     97 static int leo_scroll __P((struct leo_softc *, int));
     98 
     99 CFATTACH_DECL(leo, sizeof(struct leo_softc),
    100     leo_match, leo_attach, NULL, NULL);
    101 
    102 extern struct cfdriver leo_cd;
    103 
    104 dev_type_open(leoopen);
    105 dev_type_close(leoclose);
    106 dev_type_read(leomove);
    107 dev_type_ioctl(leoioctl);
    108 dev_type_mmap(leommap);
    109 
    110 const struct cdevsw leo_cdevsw = {
    111 	leoopen, leoclose, leomove, leomove, leoioctl,
    112 	nostop, notty, nopoll, leommap, nokqfilter,
    113 };
    114 
    115 static int
    116 leo_match(parent, cfp, aux)
    117 	struct device *parent;
    118 	struct cfdata *cfp;
    119 	void *aux;
    120 {
    121 	struct vme_attach_args *va = aux;
    122 	int i;
    123 	bus_space_tag_t iot;
    124 	bus_space_tag_t memt;
    125 	bus_space_handle_t ioh;
    126 	bus_space_handle_t memh;
    127 
    128 	/*
    129 	 * We are passed our configuration in the attachment arguments.
    130 	 * The configuration information may be partially unspecified.
    131 	 * For any unspecified configuration parameters, we fill in those
    132 	 * parameters with data for a "standard" configuration.
    133 	 * Once we have a fully specified configuration, we try to probe
    134 	 * a card with that configuration.
    135 	 * The Leonardo only has one configuration and it isn't likely
    136 	 * to change, but this routine doesn't assume that's the case.
    137 	 */
    138 	iot = va->va_iot;
    139 	memt = va->va_memt;
    140 	for (i = 0; i < NLEOSTD; i++) {
    141 		struct leo_addresses *leo_ap = &leostd[i];
    142 		int found = 0;
    143 		struct vme_attach_args vat = *va;
    144 
    145 		if (vat.va_irq != VMECF_IRQ_DEFAULT) {
    146 			printf("leo_match: config error: no irq support\n");
    147 			return 0;
    148 		}
    149 		if (vat.va_iobase == VMECF_IOPORT_DEFAULT)
    150 			vat.va_iobase = leo_ap->reg_addr;
    151 		if (vat.va_maddr == VMECF_MEM_DEFAULT)
    152 			vat.va_maddr = leo_ap->mem_addr;
    153 		if (vat.va_iosize == VMECF_IOSIZE_DEFAULT)
    154 			vat.va_iosize = leo_ap->reg_size;
    155 		if (vat.va_msize == VMECF_MEMSIZ_DEFAULT)
    156 			vat.va_msize = leo_ap->mem_size;
    157 		if (bus_space_map(iot, vat.va_iobase, vat.va_iosize, 0, &ioh)) {
    158 			printf("leo_match: cannot map io area\n");
    159 			return 0;
    160 		}
    161 		if (bus_space_map(memt, vat.va_maddr, vat.va_msize,
    162 			  	  BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE,
    163 			  	  &memh)) {
    164 			bus_space_unmap(iot, ioh, vat.va_iosize);
    165 			printf("leo_match: cannot map memory area\n");
    166 			return 0;
    167 		}
    168 		found = leo_probe(&iot, &memt, &ioh, &memh,
    169 				  vat.va_iosize, vat.va_msize);
    170 		bus_space_unmap(iot, ioh, vat.va_iosize);
    171 		bus_space_unmap(memt, memh, vat.va_msize);
    172 		if (found) {
    173 			*va = vat;
    174 			return 1;
    175 		}
    176 	}
    177 	return 0;
    178 }
    179 
    180 static int
    181 leo_probe(iot, memt, ioh, memh, iosize, msize)
    182 	bus_space_tag_t *iot, *memt;
    183 	bus_space_handle_t *ioh, *memh;
    184 	u_int iosize, msize;
    185 {
    186 
    187 	/* Test that our highest register is within the io range. */
    188 	if (0xca > iosize) /* XXX */
    189 		return 0;
    190 	/* Test if we can peek each register. */
    191 	if (!bus_space_peek_1(*iot, *ioh, LEO_REG_MSBSCROLL))
    192 		return 0;
    193 	if (!bus_space_peek_1(*iot, *ioh, LEO_REG_LSBSCROLL))
    194 		return 0;
    195 	/*
    196 	 * Write a test pattern at the start and end of the memory region,
    197 	 * and test if the pattern can be read back.  If so, the region is
    198 	 * backed by memory (i.e. the card is present).
    199 	 * On the Leonardo, the first byte of each longword isn't backed by
    200 	 * physical memory, so we only compare the three low-order bytes
    201 	 * with the test pattern.
    202 	 */
    203 	bus_space_write_4(*memt, *memh, 0, 0xa5a5a5a5);
    204 	if ((bus_space_read_4(*memt, *memh, 0) & 0xffffff) != 0xa5a5a5)
    205 		return 0;
    206 	bus_space_write_4(*memt, *memh, msize - 4, 0xa5a5a5a5);
    207 	if ((bus_space_read_4(*memt, *memh, msize - 4) & 0xffffff)
    208 		!= 0xa5a5a5)
    209 		return 0;
    210 	return 1;
    211 }
    212 
    213 static void
    214 leo_attach(parent, self, aux)
    215 	struct device *parent, *self;
    216 	void *aux;
    217 {
    218 	struct leo_softc *sc = (struct leo_softc *)self;
    219 	struct vme_attach_args *va = aux;
    220 	bus_space_handle_t ioh;
    221 	bus_space_handle_t memh;
    222 #ifndef SET_REGION
    223 	int i;
    224 #endif
    225 
    226 	printf("\n");
    227 	if (bus_space_map(va->va_iot, va->va_iobase, va->va_iosize, 0, &ioh))
    228 		panic("leo_attach: cannot map io area");
    229 	if (bus_space_map(va->va_memt, va->va_maddr, va->va_msize,
    230 			  BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE, &memh))
    231 		panic("leo_attach: cannot map memory area");
    232 #ifdef SET_REGION /* XXX seems to be unimplemented on atari? */
    233 	bus_space_set_region_4(va->va_memt, memh, 0, 0, va->va_msize >> 2);
    234 #else
    235 	for (i = 0; i < (va->va_msize >> 2); i++)
    236 		bus_space_write_4(va->va_memt, memh, i << 2, 0);
    237 #endif
    238 	sc->sc_iot = va->va_iot;
    239 	sc->sc_ioh = ioh;
    240 	sc->sc_memt = va->va_memt;
    241 	sc->sc_memh = memh;
    242 	sc->sc_flags = 0;
    243 	sc->sc_maddr = va->va_maddr;
    244 	sc->sc_msize = va->va_msize;
    245 	leo_init(sc, 512);
    246 	leo_scroll(sc, 0);
    247 }
    248 
    249 int
    250 leoopen(dev, flags, devtype, p)
    251 	dev_t dev;
    252 	int flags, devtype;
    253 	struct proc *p;
    254 {
    255 	int unit = minor(dev);
    256 	struct leo_softc *sc;
    257 	int r;
    258 
    259 	if (unit >= leo_cd.cd_ndevs)
    260 		return ENXIO;
    261 	sc = leo_cd.cd_devs[unit];
    262 	if (!sc)
    263 		return ENXIO;
    264 	if (sc->sc_flags & LEO_SC_FLAGS_INUSE)
    265 		return EBUSY;
    266 	r = leo_init(sc, 512);
    267 	if (r != 0)
    268 		return r;
    269 	r = leo_scroll(sc, 0);
    270 	if (r != 0)
    271 		return r;
    272 	sc->sc_flags |= LEO_SC_FLAGS_INUSE;
    273 	return 0;
    274 }
    275 
    276 static int
    277 leo_init(sc, ysize)
    278 	struct leo_softc *sc;
    279 	int ysize;
    280 {
    281 
    282 	if ((ysize != 256) && (ysize != 384) && (ysize != 512))
    283 		return EINVAL;
    284 	/* XXX */
    285 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x00, 0x6);
    286 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x08, 0x0);
    287 	if (ysize == 384)
    288 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x10);
    289 	else
    290 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x11);
    291 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x18, 0x0);
    292 	if (ysize == 384)
    293 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x50);
    294 	else
    295 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x51);
    296 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x28, 0x0);
    297 	if (ysize == 384)
    298 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x56);
    299 	else
    300 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x57);
    301 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x38, 0x0);
    302 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x40, 0x6);
    303 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x48, 0x0);
    304 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x50, 0x25);
    305 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x58, 0x0);
    306 	if (ysize == 256) {
    307 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1f);
    308 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x1);
    309 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x29);
    310 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
    311 	} else if (ysize == 384) {
    312 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0xa5);
    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, 0xa7);
    315 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
    316 	} else {
    317 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1d);
    318 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x2);
    319 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x27);
    320 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x2);
    321 	}
    322 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb8, 0x10);
    323 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb0, 0x10);
    324 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x80, 0x4);
    325 	if (ysize == 384)
    326 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x21);
    327 	else
    328 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x20);
    329 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc0, 0x40);
    330 	return 0;
    331 }
    332 
    333 static int
    334 leo_scroll(sc, scroll)
    335 	struct leo_softc *sc;
    336 	int scroll;
    337 {
    338 
    339 	if ((scroll < 0) || (scroll > 255))
    340 		return EINVAL;
    341         bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_MSBSCROLL,
    342 			  (scroll >> 6) && 0xff);
    343         bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_LSBSCROLL,
    344 			  (scroll << 2) && 0xff);
    345 	return 0;
    346 }
    347 
    348 int
    349 leoclose(dev, flags, devtype, p)
    350 	dev_t dev;
    351 	int flags, devtype;
    352 	struct proc *p;
    353 {
    354 	struct leo_softc *sc;
    355 
    356 	sc = leo_cd.cd_devs[minor(dev)];
    357 	sc->sc_flags &= ~LEO_SC_FLAGS_INUSE;
    358 	return 0;
    359 }
    360 
    361 #define SMALLBSIZE      32
    362 
    363 int
    364 leomove(dev, uio, flags)
    365         dev_t dev;
    366         struct uio *uio;
    367         int flags;
    368 {
    369         struct leo_softc *sc;
    370         int length, size, error;
    371         u_int8_t smallbuf[SMALLBSIZE];
    372 	off_t offset;
    373 
    374         sc = leo_cd.cd_devs[minor(dev)];
    375         if (uio->uio_offset > sc->sc_msize)
    376                 return 0;
    377         length = sc->sc_msize - uio->uio_offset;
    378         if (length > uio->uio_resid)
    379                 length = uio->uio_resid;
    380         while (length > 0) {
    381                 size = length;
    382                 if (size > SMALLBSIZE)
    383                         size = SMALLBSIZE;
    384                 length -= size;
    385 		offset = uio->uio_offset;
    386                 if (uio->uio_rw == UIO_READ)
    387                         bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
    388                                         offset, smallbuf, size);
    389                 if ((error = uiomove((void *)smallbuf, size, uio)))
    390                         return (error);
    391                 if (uio->uio_rw == UIO_WRITE)
    392                         bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
    393                                         offset, smallbuf, size);
    394         }
    395         return 0;
    396 }
    397 
    398 int
    399 leoioctl(dev, cmd, data, flags, p)
    400 	dev_t dev;
    401 	u_long cmd;
    402 	void *data;
    403 	int flags;
    404 	struct proc *p;
    405 {
    406 	struct leo_softc *sc;
    407 
    408 	sc = leo_cd.cd_devs[minor(dev)];
    409         switch (cmd) {
    410         case LIOCYRES:
    411 		return leo_init(sc, *(int *)data);
    412 		break;
    413         case LIOCSCRL:
    414 		return leo_scroll(sc, *(int *)data);
    415 		break;
    416 	default:
    417 		return EINVAL;
    418 		break;
    419 	}
    420 }
    421 
    422 paddr_t
    423 leommap(dev, offset, prot)
    424 	dev_t dev;
    425 	off_t offset;
    426 	int prot;
    427 {
    428 	struct leo_softc *sc;
    429 
    430 	sc = leo_cd.cd_devs[minor(dev)];
    431 	if (offset >= 0 && offset < sc->sc_msize)
    432 		return m68k_btop(sc->sc_maddr + offset);
    433 	return -1;
    434 }
    435