Home | History | Annotate | Line # | Download | only in vme
leo.c revision 1.20
      1 /*	$NetBSD: leo.c,v 1.20 2014/03/16 05:20:23 dholland 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.20 2014/03/16 05:20:23 dholland 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 <sys/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 #include "ioconf.h"
     68 
     69 static struct leo_addresses {
     70 	u_long reg_addr;
     71 	u_int reg_size;
     72 	u_long mem_addr;
     73 	u_int mem_size;
     74 } leostd[] = {
     75 	{ 0xfed90000, 0x100, 0xfec00000, 0x100000 }
     76 };
     77 
     78 #define NLEOSTD (sizeof(leostd) / sizeof(leostd[0]))
     79 
     80 struct leo_softc {
     81 	device_t sc_dev;		/* XXX what goes here? */
     82 	bus_space_tag_t sc_iot;
     83 	bus_space_tag_t sc_memt;
     84 	bus_space_handle_t sc_ioh;
     85 	bus_space_handle_t sc_memh;
     86 	int sc_flags;
     87 	int sc_maddr;
     88 	u_int sc_msize;
     89 };
     90 
     91 #define LEO_SC_FLAGS_INUSE 1
     92 
     93 static int leo_match(device_t, cfdata_t, void *);
     94 static void leo_attach(device_t, device_t, void *);
     95 static int leo_probe(bus_space_tag_t *, bus_space_tag_t *,
     96 			  bus_space_handle_t *, bus_space_handle_t *,
     97 			  u_int, u_int);
     98 static int leo_init(struct leo_softc *, int);
     99 static int leo_scroll(struct leo_softc *, int);
    100 
    101 CFATTACH_DECL_NEW(leo, sizeof(struct leo_softc),
    102     leo_match, leo_attach, NULL, NULL);
    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 	.d_open = leoopen,
    112 	.d_close = leoclose,
    113 	.d_read = leomove,
    114 	.d_write = leomove,
    115 	.d_ioctl = leoioctl,
    116 	.d_stop = nostop,
    117 	.d_tty = notty,
    118 	.d_poll = nopoll,
    119 	.d_mmap = leommap,
    120 	.d_kqfilter = nokqfilter,
    121 	.d_flag = 0
    122 };
    123 
    124 static int
    125 leo_match(device_t parent, cfdata_t cf, void *aux)
    126 {
    127 	struct vme_attach_args *va = aux;
    128 	int i;
    129 	bus_space_tag_t iot;
    130 	bus_space_tag_t memt;
    131 	bus_space_handle_t ioh;
    132 	bus_space_handle_t memh;
    133 
    134 	/*
    135 	 * We are passed our configuration in the attachment arguments.
    136 	 * The configuration information may be partially unspecified.
    137 	 * For any unspecified configuration parameters, we fill in those
    138 	 * parameters with data for a "standard" configuration.
    139 	 * Once we have a fully specified configuration, we try to probe
    140 	 * a card with that configuration.
    141 	 * The Leonardo only has one configuration and it isn't likely
    142 	 * to change, but this routine doesn't assume that's the case.
    143 	 */
    144 	iot = va->va_iot;
    145 	memt = va->va_memt;
    146 	for (i = 0; i < NLEOSTD; i++) {
    147 		struct leo_addresses *leo_ap = &leostd[i];
    148 		int found = 0;
    149 		struct vme_attach_args vat = *va;
    150 
    151 		if (vat.va_irq != VMECF_IRQ_DEFAULT) {
    152 			printf("leo_match: config error: no irq support\n");
    153 			return 0;
    154 		}
    155 		if (vat.va_iobase == VMECF_IOPORT_DEFAULT)
    156 			vat.va_iobase = leo_ap->reg_addr;
    157 		if (vat.va_maddr == VMECF_MEM_DEFAULT)
    158 			vat.va_maddr = leo_ap->mem_addr;
    159 		if (vat.va_iosize == VMECF_IOSIZE_DEFAULT)
    160 			vat.va_iosize = leo_ap->reg_size;
    161 		if (vat.va_msize == VMECF_MEMSIZ_DEFAULT)
    162 			vat.va_msize = leo_ap->mem_size;
    163 		if (bus_space_map(iot, vat.va_iobase, vat.va_iosize, 0, &ioh)) {
    164 			printf("leo_match: cannot map io area\n");
    165 			return 0;
    166 		}
    167 		if (bus_space_map(memt, vat.va_maddr, vat.va_msize,
    168 			  	  BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_CACHEABLE,
    169 			  	  &memh)) {
    170 			bus_space_unmap(iot, ioh, vat.va_iosize);
    171 			printf("leo_match: cannot map memory area\n");
    172 			return 0;
    173 		}
    174 		found = leo_probe(&iot, &memt, &ioh, &memh,
    175 				  vat.va_iosize, vat.va_msize);
    176 		bus_space_unmap(iot, ioh, vat.va_iosize);
    177 		bus_space_unmap(memt, memh, vat.va_msize);
    178 		if (found) {
    179 			*va = vat;
    180 			return 1;
    181 		}
    182 	}
    183 	return 0;
    184 }
    185 
    186 static int
    187 leo_probe(bus_space_tag_t *iot, bus_space_tag_t *memt, bus_space_handle_t *ioh, bus_space_handle_t *memh, u_int iosize, u_int 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(device_t parent, device_t self, void *aux)
    218 {
    219 	struct leo_softc *sc = device_private(self);
    220 	struct vme_attach_args *va = aux;
    221 	bus_space_handle_t ioh;
    222 	bus_space_handle_t memh;
    223 #ifndef SET_REGION
    224 	int i;
    225 #endif
    226 
    227 	sc->sc_dev = self;
    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");
    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");
    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_t dev, int flags, int devtype, struct lwp *l)
    254 {
    255 	struct leo_softc *sc;
    256 	int r;
    257 
    258 	sc = device_lookup_private(&leo_cd, minor(dev));
    259 	if (!sc)
    260 		return ENXIO;
    261 	if (sc->sc_flags & LEO_SC_FLAGS_INUSE)
    262 		return EBUSY;
    263 	r = leo_init(sc, 512);
    264 	if (r != 0)
    265 		return r;
    266 	r = leo_scroll(sc, 0);
    267 	if (r != 0)
    268 		return r;
    269 	sc->sc_flags |= LEO_SC_FLAGS_INUSE;
    270 	return 0;
    271 }
    272 
    273 static int
    274 leo_init(struct leo_softc *sc, int ysize)
    275 {
    276 
    277 	if ((ysize != 256) && (ysize != 384) && (ysize != 512))
    278 		return EINVAL;
    279 	/* XXX */
    280 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x00, 0x6);
    281 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x08, 0x0);
    282 	if (ysize == 384)
    283 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x10);
    284 	else
    285 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x10, 0x11);
    286 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x18, 0x0);
    287 	if (ysize == 384)
    288 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x50);
    289 	else
    290 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x20, 0x51);
    291 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x28, 0x0);
    292 	if (ysize == 384)
    293 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x56);
    294 	else
    295 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x30, 0x57);
    296 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x38, 0x0);
    297 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x40, 0x6);
    298 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x48, 0x0);
    299 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x50, 0x25);
    300 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x58, 0x0);
    301 	if (ysize == 256) {
    302 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1f);
    303 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x1);
    304 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x29);
    305 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
    306 	} else if (ysize == 384) {
    307 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0xa5);
    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, 0xa7);
    310 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x1);
    311 	} else {
    312 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x60, 0x1d);
    313 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x68, 0x2);
    314 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x70, 0x27);
    315 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x78, 0x2);
    316 	}
    317 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb8, 0x10);
    318 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xb0, 0x10);
    319 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x80, 0x4);
    320 	if (ysize == 384)
    321 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x21);
    322 	else
    323 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc8, 0x20);
    324 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0xc0, 0x40);
    325 	return 0;
    326 }
    327 
    328 static int
    329 leo_scroll(struct leo_softc *sc, int scroll)
    330 {
    331 
    332 	if ((scroll < 0) || (scroll > 255))
    333 		return EINVAL;
    334         bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_MSBSCROLL,
    335 			  (scroll >> 6) && 0xff);
    336         bus_space_write_1(sc->sc_iot, sc->sc_ioh, LEO_REG_LSBSCROLL,
    337 			  (scroll << 2) && 0xff);
    338 	return 0;
    339 }
    340 
    341 int
    342 leoclose(dev_t dev, int flags, int devtype, struct lwp *l)
    343 {
    344 	struct leo_softc *sc;
    345 
    346 	sc = device_lookup_private(&leo_cd, minor(dev));
    347 	sc->sc_flags &= ~LEO_SC_FLAGS_INUSE;
    348 	return 0;
    349 }
    350 
    351 #define SMALLBSIZE      32
    352 
    353 int
    354 leomove(dev_t dev, struct uio *uio, int flags)
    355 {
    356         struct leo_softc *sc;
    357         int length, size, error;
    358         u_int8_t smallbuf[SMALLBSIZE];
    359 	off_t offset;
    360 
    361         sc = device_lookup_private(&leo_cd,minor(dev));
    362         if (uio->uio_offset > sc->sc_msize)
    363                 return 0;
    364         length = sc->sc_msize - uio->uio_offset;
    365         if (length > uio->uio_resid)
    366                 length = uio->uio_resid;
    367         while (length > 0) {
    368                 size = length;
    369                 if (size > SMALLBSIZE)
    370                         size = SMALLBSIZE;
    371                 length -= size;
    372 		offset = uio->uio_offset;
    373                 if (uio->uio_rw == UIO_READ)
    374                         bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
    375                                         offset, smallbuf, size);
    376                 if ((error = uiomove((void *)smallbuf, size, uio)))
    377                         return (error);
    378                 if (uio->uio_rw == UIO_WRITE)
    379                         bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
    380                                         offset, smallbuf, size);
    381         }
    382         return 0;
    383 }
    384 
    385 int
    386 leoioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    387 {
    388 	struct leo_softc *sc;
    389 
    390 	sc = device_lookup_private(&leo_cd,minor(dev));
    391         switch (cmd) {
    392         case LIOCYRES:
    393 		return leo_init(sc, *(int *)data);
    394 		break;
    395         case LIOCSCRL:
    396 		return leo_scroll(sc, *(int *)data);
    397 		break;
    398 	default:
    399 		return EINVAL;
    400 		break;
    401 	}
    402 }
    403 
    404 paddr_t
    405 leommap(dev_t dev, off_t offset, int prot)
    406 {
    407 	struct leo_softc *sc;
    408 
    409 	sc = device_lookup_private(&leo_cd, minor(dev));
    410 	if (offset >= 0 && offset < sc->sc_msize)
    411 		return m68k_btop(sc->sc_maddr + offset);
    412 	return -1;
    413 }
    414