Home | History | Annotate | Line # | Download | only in dev
hpib.c revision 1.29
      1 /*	$NetBSD: hpib.c,v 1.29 2005/07/04 15:18:17 drochner Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1982, 1990, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	@(#)hpib.c	8.2 (Berkeley) 1/12/94
     68  */
     69 
     70 /*
     71  * HP-IB bus driver
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __KERNEL_RCSID(0, "$NetBSD: hpib.c,v 1.29 2005/07/04 15:18:17 drochner Exp $");
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/buf.h>
     80 #include <sys/malloc.h>
     81 #include <sys/device.h>
     82 
     83 #include <hp300/dev/dmavar.h>
     84 
     85 #include <hp300/dev/hpibvar.h>
     86 
     87 #include <machine/cpu.h>
     88 #include <machine/hp300spu.h>
     89 
     90 #include "ioconf.h"
     91 
     92 static int	hpibbusmatch(struct device *, struct cfdata *, void *);
     93 static void	hpibbusattach(struct device *, struct device *, void *);
     94 
     95 CFATTACH_DECL(hpibbus, sizeof(struct hpibbus_softc),
     96     hpibbusmatch, hpibbusattach, NULL, NULL);
     97 
     98 static void	hpibbus_attach_children(struct hpibbus_softc *);
     99 static int	hpibbussearch(struct device *, struct cfdata *,
    100 			      const locdesc_t *, void *);
    101 static int	hpibbusprint(void *, const char *);
    102 
    103 static int	hpibbus_alloc(struct hpibbus_softc *, int, int);
    104 #if 0
    105 static void	hpibbus_free(struct hpibbus_softc *, int, int);
    106 #endif
    107 
    108 static void	hpibstart(void *);
    109 static void	hpibdone(void *);
    110 
    111 int	hpibtimeout = 100000;	/* # of status tests before we give up */
    112 int	hpibidtimeout = 10000;	/* # of status tests for hpibid() calls */
    113 int	hpibdmathresh = 3;	/* byte count beyond which to attempt dma */
    114 
    115 /*
    116  * HP-IB is essentially an IEEE 488 bus, with an HP command
    117  * set (CS/80 on `newer' devices, Amigo on before-you-were-born
    118  * devices) thrown on top.  Devices that respond to CS/80 (and
    119  * probably Amigo, too) are tagged with a 16-bit ID.
    120  *
    121  * HP-IB has a 2-level addressing scheme; slave, the analog
    122  * of a SCSI ID, and punit, the analog of a SCSI LUN.  Unforunately,
    123  * IDs are on a per-slave basis; punits are often used for disk
    124  * drives that have an accompanying tape drive on the second punit.
    125  *
    126  * In addition, not all HP-IB devices speak CS/80 or Amigo.
    127  * Examples of such devices are HP-IB plotters, which simply
    128  * take raw plotter commands over 488.  These devices do not
    129  * have ID tags, and often the host cannot even tell if such
    130  * a device is attached to the system!
    131  *
    132  * These two nasty bits mean that we have to treat HP-IB as
    133  * an indirect bus.  However, since we are given some ID
    134  * information, it is unreasonable to disallow cloning of
    135  * CS/80 devices.
    136  *
    137  * To deal with all of this, we use the semi-twisted scheme
    138  * in hpibbus_attach_children().  For each HP-IB slave, we loop
    139  * through all of the possibly-configured children, allowing
    140  * them to modify the punit parameter (but NOT the slave!).
    141  *
    142  * This is evil, but what can you do?
    143  */
    144 
    145 static int
    146 hpibbusmatch(struct device *parent, struct cfdata *match, void *aux)
    147 {
    148 
    149 	return (1);
    150 }
    151 
    152 static void
    153 hpibbusattach(struct device *parent, struct device *self, void *aux)
    154 {
    155 	struct hpibbus_softc *sc = (struct hpibbus_softc *)self;
    156 	struct hpibdev_attach_args *ha = aux;
    157 
    158 	printf("\n");
    159 
    160 	/* Get the operations vector for the controller. */
    161 	sc->sc_ops = ha->ha_ops;
    162 	sc->sc_type = ha->ha_type;		/* XXX */
    163 	sc->sc_ba = ha->ha_ba;
    164 	*(ha->ha_softcpp) = sc;			/* XXX */
    165 
    166 	hpibreset(self->dv_unit);		/* XXX souldn't be here */
    167 
    168 	/*
    169 	 * Initialize the DMA queue entry.
    170 	 */
    171 	MALLOC(sc->sc_dq, struct dmaqueue *, sizeof(struct dmaqueue),
    172 	    M_DEVBUF, M_NOWAIT);
    173 	if (sc->sc_dq == NULL) {
    174 		printf("%s: can't allocate DMA queue entry\n", self->dv_xname);
    175 		return;
    176 	}
    177 	sc->sc_dq->dq_softc = sc;
    178 	sc->sc_dq->dq_start = hpibstart;
    179 	sc->sc_dq->dq_done = hpibdone;
    180 
    181 	/* Initialize the slave request queue. */
    182 	TAILQ_INIT(&sc->sc_queue);
    183 
    184 	/* Attach any devices on the bus. */
    185 	hpibbus_attach_children(sc);
    186 }
    187 
    188 static void
    189 hpibbus_attach_children(struct hpibbus_softc *sc)
    190 {
    191 	struct hpibbus_attach_args ha;
    192 	int slave;
    193 
    194 	for (slave = 0; slave < 8; slave++) {
    195 		/*
    196 		 * Get the ID tag for the device, if any.
    197 		 * Plotters won't identify themselves, and
    198 		 * get the same value as non-existent devices.
    199 		 */
    200 		ha.ha_id = hpibid(sc->sc_dev.dv_unit, slave);
    201 
    202 		ha.ha_slave = slave;	/* not to be modified by children */
    203 		ha.ha_punit = 0;	/* children modify this */
    204 
    205 		/*
    206 		 * Search though all configured children for this bus.
    207 		 */
    208 		config_search_ia(hpibbussearch, &sc->sc_dev, "hpibbus", &ha);
    209 	}
    210 }
    211 
    212 static int
    213 hpibbussearch(struct device *parent, struct cfdata *cf,
    214 	      const locdesc_t *ldesc, void *aux)
    215 {
    216 	struct hpibbus_softc *sc = (struct hpibbus_softc *)parent;
    217 	struct hpibbus_attach_args *ha = aux;
    218 
    219 	/* Make sure this is in a consistent state. */
    220 	ha->ha_punit = 0;
    221 
    222 	if (config_match(parent, cf, ha) > 0) {
    223 		/*
    224 		 * The device probe has succeeded, and filled in
    225 		 * the punit information.  Make sure the configuration
    226 		 * allows for this slave/punit combination.
    227 		 */
    228 		if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
    229 		    cf->hpibbuscf_slave != ha->ha_slave)
    230 			goto out;
    231 		if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
    232 		    cf->hpibbuscf_punit != ha->ha_punit)
    233 			goto out;
    234 
    235 		/*
    236 		 * Allocate the device's address from the bus's
    237 		 * resource map.
    238 		 */
    239 		if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit))
    240 			goto out;
    241 
    242 		/*
    243 		 * This device is allowed; attach it.
    244 		 */
    245 		config_attach(parent, cf, ha, hpibbusprint);
    246 	}
    247  out:
    248 	return (0);
    249 }
    250 
    251 static int
    252 hpibbusprint(void *aux, const char *pnp)
    253 {
    254 	struct hpibbus_attach_args *ha = aux;
    255 
    256 	aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
    257 	return (UNCONF);
    258 }
    259 
    260 int
    261 hpibdevprint(void *aux, const char *pnp)
    262 {
    263 
    264 	/* only hpibbus's can attach to hpibdev's -- easy. */
    265 	if (pnp != NULL)
    266 		aprint_normal("hpibbus at %s", pnp);
    267 	return (UNCONF);
    268 }
    269 
    270 void
    271 hpibreset(int unit)
    272 {
    273 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    274 
    275 	(*sc->sc_ops->hpib_reset)(sc);
    276 }
    277 
    278 int
    279 hpibreq(struct device *pdev, struct hpibqueue *hq)
    280 {
    281 	struct hpibbus_softc *sc = (struct hpibbus_softc *)pdev;
    282 	int s;
    283 
    284 	s = splhigh();	/* XXXthorpej */
    285 	TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
    286 	splx(s);
    287 
    288 	if (sc->sc_queue.tqh_first == hq)
    289 		return (1);
    290 
    291 	return (0);
    292 }
    293 
    294 void
    295 hpibfree(struct device *pdev, struct hpibqueue *hq)
    296 {
    297 	struct hpibbus_softc *sc = (struct hpibbus_softc *)pdev;
    298 	int s;
    299 
    300 	s = splhigh();	/* XXXthorpej */
    301 	TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
    302 	splx(s);
    303 
    304 	if ((hq = sc->sc_queue.tqh_first) != NULL)
    305 		(*hq->hq_start)(hq->hq_softc);
    306 }
    307 
    308 int
    309 hpibid(int unit, int slave)
    310 {
    311 	short id;
    312 	int ohpibtimeout;
    313 
    314 	/*
    315 	 * XXX shorten timeout value so autoconfig doesn't
    316 	 * take forever on slow CPUs.
    317 	 */
    318 	ohpibtimeout = hpibtimeout;
    319 	hpibtimeout = hpibidtimeout * (cpuspeed / 8);
    320 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
    321 		id = 0;
    322 	hpibtimeout = ohpibtimeout;
    323 	return(id);
    324 }
    325 
    326 int
    327 hpibsend(int unit, int slave, int sec, void *addr, int cnt)
    328 {
    329 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    330 
    331 	return ((*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt));
    332 }
    333 
    334 int
    335 hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
    336 {
    337 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    338 
    339 	return ((*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt));
    340 }
    341 
    342 int
    343 hpibpptest(int unit, int slave)
    344 {
    345 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    346 
    347 	return ((*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave));
    348 }
    349 
    350 void
    351 hpibppclear(int unit)
    352 {
    353 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    354 
    355 	sc->sc_flags &= ~HPIBF_PPOLL;
    356 }
    357 
    358 void
    359 hpibawait(int unit)
    360 {
    361 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    362 
    363 	sc->sc_flags |= HPIBF_PPOLL;
    364 	(*sc->sc_ops->hpib_ppwatch)(sc);
    365 }
    366 
    367 int
    368 hpibswait(int unit, int slave)
    369 {
    370 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    371 	int timo = hpibtimeout;
    372 	int mask, (*ppoll)(struct hpibbus_softc *);
    373 
    374 	ppoll = sc->sc_ops->hpib_ppoll;
    375 	mask = 0x80 >> slave;
    376 	while (((*ppoll)(sc) & mask) == 0) {
    377 		if (--timo == 0) {
    378 			printf("%s: swait timeout\n", sc->sc_dev.dv_xname);
    379 			return(-1);
    380 		}
    381 	}
    382 	return(0);
    383 }
    384 
    385 int
    386 hpibustart(int unit)
    387 {
    388 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    389 
    390 	if (sc->sc_type == HPIBA)
    391 		sc->sc_dq->dq_chan = DMA0;
    392 	else
    393 		sc->sc_dq->dq_chan = DMA0 | DMA1;
    394 	if (dmareq(sc->sc_dq))
    395 		return(1);
    396 	return(0);
    397 }
    398 
    399 static void
    400 hpibstart(void *arg)
    401 {
    402 	struct hpibbus_softc *sc = arg;
    403 	struct hpibqueue *hq;
    404 
    405 	hq = sc->sc_queue.tqh_first;
    406 	(*hq->hq_go)(hq->hq_softc);
    407 }
    408 
    409 void
    410 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
    411 {
    412 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
    413 
    414 	(*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
    415 }
    416 
    417 static void
    418 hpibdone(void *arg)
    419 {
    420 	struct hpibbus_softc *sc = arg;
    421 
    422 	(*sc->sc_ops->hpib_done)(sc);
    423 }
    424 
    425 int
    426 hpibintr(void *arg)
    427 {
    428 	struct hpibbus_softc *sc = arg;
    429 
    430 	return ((sc->sc_ops->hpib_intr)(arg));
    431 }
    432 
    433 static int
    434 hpibbus_alloc(struct hpibbus_softc *sc, int slave, int punit)
    435 {
    436 
    437 	if (slave >= HPIB_NSLAVES ||
    438 	    punit >= HPIB_NPUNITS)
    439 		panic("hpibbus_alloc: device address out of range");
    440 
    441 	if (sc->sc_rmap[slave][punit] == 0) {
    442 		sc->sc_rmap[slave][punit] = 1;
    443 		return (0);
    444 	}
    445 	return (1);
    446 }
    447 
    448 #if 0
    449 static void
    450 hpibbus_free(struct hpibbus_softc *sc, int slave, int punit)
    451 {
    452 
    453 	if (slave >= HPIB_NSLAVES ||
    454 	    punit >= HPIB_NPUNITS)
    455 		panic("hpibbus_free: device address out of range");
    456 
    457 #ifdef DIAGNOSTIC
    458 	if (sc->sc_rmap[slave][punit] == 0)
    459 		panic("hpibbus_free: not allocated");
    460 #endif
    461 
    462 	sc->sc_rmap[slave][punit] = 0;
    463 }
    464 #endif
    465