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