Home | History | Annotate | Line # | Download | only in dev
hpib.c revision 1.41.2.1
      1 /*	$NetBSD: hpib.c,v 1.41.2.1 2021/03/20 19:33:34 thorpej 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.41.2.1 2021/03/20 19:33:34 thorpej Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/buf.h>
     73 #include <sys/kmem.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 = kmem_alloc(sizeof(struct dmaqueue), KM_SLEEP);
    165 	sc->sc_dq->dq_softc = sc;
    166 	sc->sc_dq->dq_start = hpibstart;
    167 	sc->sc_dq->dq_done = hpibdone;
    168 
    169 	/* Initialize the slave request queue. */
    170 	TAILQ_INIT(&sc->sc_queue);
    171 
    172 	/* Attach any devices on the bus. */
    173 	hpibbus_attach_children(sc);
    174 }
    175 
    176 static void
    177 hpibbus_attach_children(struct hpibbus_softc *sc)
    178 {
    179 	struct hpibbus_attach_args ha;
    180 	int slave;
    181 
    182 	for (slave = 0; slave < 8; slave++) {
    183 		/*
    184 		 * Get the ID tag for the device, if any.
    185 		 * Plotters won't identify themselves, and
    186 		 * get the same value as non-existent devices.
    187 		 */
    188 		ha.ha_id = hpibid(device_unit(sc->sc_dev), slave);
    189 
    190 		ha.ha_slave = slave;	/* not to be modified by children */
    191 		ha.ha_punit = 0;	/* children modify this */
    192 
    193 		/*
    194 		 * Search though all configured children for this bus.
    195 		 */
    196 		config_search(sc->sc_dev, &ha,
    197 		    CFARG_SUBMATCH, hpibbussearch,
    198 		    CFARG_IATTR, "hpibbus",
    199 		    CFARG_EOL);
    200 	}
    201 }
    202 
    203 static int
    204 hpibbussearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    205 {
    206 	struct hpibbus_softc *sc = device_private(parent);
    207 	struct hpibbus_attach_args *ha = aux;
    208 
    209 	/* Make sure this is in a consistent state. */
    210 	ha->ha_punit = 0;
    211 
    212 	if (config_match(parent, cf, ha) > 0) {
    213 		/*
    214 		 * The device probe has succeeded, and filled in
    215 		 * the punit information.  Make sure the configuration
    216 		 * allows for this slave/punit combination.
    217 		 */
    218 		if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
    219 		    cf->hpibbuscf_slave != ha->ha_slave)
    220 			goto out;
    221 		if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
    222 		    cf->hpibbuscf_punit != ha->ha_punit)
    223 			goto out;
    224 
    225 		/*
    226 		 * Allocate the device's address from the bus's
    227 		 * resource map.
    228 		 */
    229 		if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit))
    230 			goto out;
    231 
    232 		/*
    233 		 * This device is allowed; attach it.
    234 		 */
    235 		config_attach(parent, cf, ha, hpibbusprint);
    236 	}
    237  out:
    238 	return 0;
    239 }
    240 
    241 static int
    242 hpibbusprint(void *aux, const char *pnp)
    243 {
    244 	struct hpibbus_attach_args *ha = aux;
    245 
    246 	aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
    247 	return UNCONF;
    248 }
    249 
    250 int
    251 hpibdevprint(void *aux, const char *pnp)
    252 {
    253 
    254 	/* only hpibbus's can attach to hpibdev's -- easy. */
    255 	if (pnp != NULL)
    256 		aprint_normal("hpibbus at %s", pnp);
    257 	return UNCONF;
    258 }
    259 
    260 void
    261 hpibreset(int unit)
    262 {
    263 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
    264 
    265 	(*sc->sc_ops->hpib_reset)(sc);
    266 }
    267 
    268 int
    269 hpibreq(device_t pdev, struct hpibqueue *hq)
    270 {
    271 	struct hpibbus_softc *sc = device_private(pdev);
    272 	int s;
    273 
    274 	s = splhigh();	/* XXXthorpej */
    275 	TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
    276 	splx(s);
    277 
    278 	if (TAILQ_FIRST(&sc->sc_queue) == hq)
    279 		return 1;
    280 
    281 	return 0;
    282 }
    283 
    284 void
    285 hpibfree(device_t pdev, struct hpibqueue *hq)
    286 {
    287 	struct hpibbus_softc *sc = device_private(pdev);
    288 	int s;
    289 
    290 	s = splhigh();	/* XXXthorpej */
    291 	TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
    292 	splx(s);
    293 
    294 	if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    295 		(*hq->hq_start)(hq->hq_softc);
    296 }
    297 
    298 int
    299 hpibid(int unit, int slave)
    300 {
    301 	short id;
    302 	int ohpibtimeout;
    303 
    304 	/*
    305 	 * XXX shorten timeout value so autoconfig doesn't
    306 	 * take forever on slow CPUs.
    307 	 */
    308 	ohpibtimeout = hpibtimeout;
    309 	hpibtimeout = hpibidtimeout * (cpuspeed / 8);
    310 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
    311 		id = 0;
    312 	hpibtimeout = ohpibtimeout;
    313 	return id;
    314 }
    315 
    316 int
    317 hpibsend(int unit, int slave, int sec, void *addr, int cnt)
    318 {
    319 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
    320 
    321 	return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt);
    322 }
    323 
    324 int
    325 hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
    326 {
    327 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
    328 
    329 	return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt);
    330 }
    331 
    332 int
    333 hpibpptest(int unit, int slave)
    334 {
    335 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
    336 
    337 	return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave);
    338 }
    339 
    340 void
    341 hpibppclear(int unit)
    342 {
    343 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
    344 
    345 	sc->sc_flags &= ~HPIBF_PPOLL;
    346 }
    347 
    348 void
    349 hpibawait(int unit)
    350 {
    351 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
    352 
    353 	sc->sc_flags |= HPIBF_PPOLL;
    354 	(*sc->sc_ops->hpib_ppwatch)(sc);
    355 }
    356 
    357 int
    358 hpibswait(int unit, int slave)
    359 {
    360 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
    361 	int timo = hpibtimeout;
    362 	int mask, (*ppoll)(struct hpibbus_softc *);
    363 
    364 	ppoll = sc->sc_ops->hpib_ppoll;
    365 	mask = 0x80 >> slave;
    366 	while (((*ppoll)(sc) & mask) == 0) {
    367 		if (--timo == 0) {
    368 			printf("%s: swait timeout\n", device_xname(sc->sc_dev));
    369 			return -1;
    370 		}
    371 	}
    372 	return 0;
    373 }
    374 
    375 int
    376 hpibustart(int unit)
    377 {
    378 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
    379 
    380 	if (sc->sc_type == HPIBA)
    381 		sc->sc_dq->dq_chan = DMA0;
    382 	else
    383 		sc->sc_dq->dq_chan = DMA0 | DMA1;
    384 	if (dmareq(sc->sc_dq))
    385 		return 1;
    386 	return 0;
    387 }
    388 
    389 static void
    390 hpibstart(void *arg)
    391 {
    392 	struct hpibbus_softc *sc = arg;
    393 	struct hpibqueue *hq;
    394 
    395 	hq = TAILQ_FIRST(&sc->sc_queue);
    396 	(*hq->hq_go)(hq->hq_softc);
    397 }
    398 
    399 void
    400 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
    401 {
    402 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
    403 
    404 	(*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
    405 }
    406 
    407 static void
    408 hpibdone(void *arg)
    409 {
    410 	struct hpibbus_softc *sc = arg;
    411 
    412 	(*sc->sc_ops->hpib_done)(sc);
    413 }
    414 
    415 int
    416 hpibintr(void *arg)
    417 {
    418 	struct hpibbus_softc *sc = arg;
    419 
    420 	return (sc->sc_ops->hpib_intr)(arg);
    421 }
    422 
    423 static int
    424 hpibbus_alloc(struct hpibbus_softc *sc, int slave, int punit)
    425 {
    426 
    427 	if (slave >= HPIB_NSLAVES ||
    428 	    punit >= HPIB_NPUNITS)
    429 		panic("hpibbus_alloc: device address out of range");
    430 
    431 	if (sc->sc_rmap[slave][punit] == 0) {
    432 		sc->sc_rmap[slave][punit] = 1;
    433 		return 0;
    434 	}
    435 	return 1;
    436 }
    437 
    438 #if 0
    439 static void
    440 hpibbus_free(struct hpibbus_softc *sc, int slave, int punit)
    441 {
    442 
    443 	if (slave >= HPIB_NSLAVES ||
    444 	    punit >= HPIB_NPUNITS)
    445 		panic("hpibbus_free: device address out of range");
    446 
    447 #ifdef DIAGNOSTIC
    448 	if (sc->sc_rmap[slave][punit] == 0)
    449 		panic("hpibbus_free: not allocated");
    450 #endif
    451 
    452 	sc->sc_rmap[slave][punit] = 0;
    453 }
    454 #endif
    455