Home | History | Annotate | Line # | Download | only in dev
hpib.c revision 1.36
      1 /*	$NetBSD: hpib.c,v 1.36 2008/03/29 06:47:07 tsutsui 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.36 2008/03/29 06:47:07 tsutsui 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(device_t, cfdata_t, void *);
     93 static void	hpibbusattach(device_t, device_t, void *);
     94 
     95 CFATTACH_DECL_NEW(hpibbus, sizeof(struct hpibbus_softc),
     96     hpibbusmatch, hpibbusattach, NULL, NULL);
     97 
     98 static void	hpibbus_attach_children(struct hpibbus_softc *);
     99 static int	hpibbussearch(device_t, cfdata_t, const int *, void *);
    100 static int	hpibbusprint(void *, const char *);
    101 
    102 static int	hpibbus_alloc(struct hpibbus_softc *, int, int);
    103 #if 0
    104 static void	hpibbus_free(struct hpibbus_softc *, int, int);
    105 #endif
    106 
    107 static void	hpibstart(void *);
    108 static void	hpibdone(void *);
    109 
    110 int	hpibtimeout = 100000;	/* # of status tests before we give up */
    111 int	hpibidtimeout = 10000;	/* # of status tests for hpibid() calls */
    112 int	hpibdmathresh = 3;	/* byte count beyond which to attempt dma */
    113 
    114 /*
    115  * HP-IB is essentially an IEEE 488 bus, with an HP command
    116  * set (CS/80 on `newer' devices, Amigo on before-you-were-born
    117  * devices) thrown on top.  Devices that respond to CS/80 (and
    118  * probably Amigo, too) are tagged with a 16-bit ID.
    119  *
    120  * HP-IB has a 2-level addressing scheme; slave, the analog
    121  * of a SCSI ID, and punit, the analog of a SCSI LUN.  Unforunately,
    122  * IDs are on a per-slave basis; punits are often used for disk
    123  * drives that have an accompanying tape drive on the second punit.
    124  *
    125  * In addition, not all HP-IB devices speak CS/80 or Amigo.
    126  * Examples of such devices are HP-IB plotters, which simply
    127  * take raw plotter commands over 488.  These devices do not
    128  * have ID tags, and often the host cannot even tell if such
    129  * a device is attached to the system!
    130  *
    131  * These two nasty bits mean that we have to treat HP-IB as
    132  * an indirect bus.  However, since we are given some ID
    133  * information, it is unreasonable to disallow cloning of
    134  * CS/80 devices.
    135  *
    136  * To deal with all of this, we use the semi-twisted scheme
    137  * in hpibbus_attach_children().  For each HP-IB slave, we loop
    138  * through all of the possibly-configured children, allowing
    139  * them to modify the punit parameter (but NOT the slave!).
    140  *
    141  * This is evil, but what can you do?
    142  */
    143 
    144 static int
    145 hpibbusmatch(device_t parent, cfdata_t cf, void *aux)
    146 {
    147 
    148 	return 1;
    149 }
    150 
    151 static void
    152 hpibbusattach(device_t parent, device_t self, void *aux)
    153 {
    154 	struct hpibbus_softc *sc = device_private(self);
    155 	struct hpibdev_attach_args *ha = aux;
    156 
    157 	sc->sc_dev = self;
    158 	aprint_normal("\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(device_unit(self));		/* XXX souldn't be here */
    167 
    168 	/*
    169 	 * Initialize the DMA queue entry.
    170 	 */
    171 	sc->sc_dq = malloc(sizeof(struct dmaqueue), M_DEVBUF, M_NOWAIT);
    172 	if (sc->sc_dq == NULL) {
    173 		aprint_error_dev(self, "can't allocate DMA queue entry\n");
    174 		return;
    175 	}
    176 	sc->sc_dq->dq_softc = sc;
    177 	sc->sc_dq->dq_start = hpibstart;
    178 	sc->sc_dq->dq_done = hpibdone;
    179 
    180 	/* Initialize the slave request queue. */
    181 	TAILQ_INIT(&sc->sc_queue);
    182 
    183 	/* Attach any devices on the bus. */
    184 	hpibbus_attach_children(sc);
    185 }
    186 
    187 static void
    188 hpibbus_attach_children(struct hpibbus_softc *sc)
    189 {
    190 	struct hpibbus_attach_args ha;
    191 	int slave;
    192 
    193 	for (slave = 0; slave < 8; slave++) {
    194 		/*
    195 		 * Get the ID tag for the device, if any.
    196 		 * Plotters won't identify themselves, and
    197 		 * get the same value as non-existent devices.
    198 		 */
    199 		ha.ha_id = hpibid(device_unit(sc->sc_dev), slave);
    200 
    201 		ha.ha_slave = slave;	/* not to be modified by children */
    202 		ha.ha_punit = 0;	/* children modify this */
    203 
    204 		/*
    205 		 * Search though all configured children for this bus.
    206 		 */
    207 		config_search_ia(hpibbussearch, sc->sc_dev, "hpibbus", &ha);
    208 	}
    209 }
    210 
    211 static int
    212 hpibbussearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    213 {
    214 	struct hpibbus_softc *sc = device_private(parent);
    215 	struct hpibbus_attach_args *ha = aux;
    216 
    217 	/* Make sure this is in a consistent state. */
    218 	ha->ha_punit = 0;
    219 
    220 	if (config_match(parent, cf, ha) > 0) {
    221 		/*
    222 		 * The device probe has succeeded, and filled in
    223 		 * the punit information.  Make sure the configuration
    224 		 * allows for this slave/punit combination.
    225 		 */
    226 		if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
    227 		    cf->hpibbuscf_slave != ha->ha_slave)
    228 			goto out;
    229 		if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
    230 		    cf->hpibbuscf_punit != ha->ha_punit)
    231 			goto out;
    232 
    233 		/*
    234 		 * Allocate the device's address from the bus's
    235 		 * resource map.
    236 		 */
    237 		if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit))
    238 			goto out;
    239 
    240 		/*
    241 		 * This device is allowed; attach it.
    242 		 */
    243 		config_attach(parent, cf, ha, hpibbusprint);
    244 	}
    245  out:
    246 	return 0;
    247 }
    248 
    249 static int
    250 hpibbusprint(void *aux, const char *pnp)
    251 {
    252 	struct hpibbus_attach_args *ha = aux;
    253 
    254 	aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
    255 	return UNCONF;
    256 }
    257 
    258 int
    259 hpibdevprint(void *aux, const char *pnp)
    260 {
    261 
    262 	/* only hpibbus's can attach to hpibdev's -- easy. */
    263 	if (pnp != NULL)
    264 		aprint_normal("hpibbus at %s", pnp);
    265 	return UNCONF;
    266 }
    267 
    268 void
    269 hpibreset(int unit)
    270 {
    271 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    272 
    273 	(*sc->sc_ops->hpib_reset)(sc);
    274 }
    275 
    276 int
    277 hpibreq(struct device *pdev, struct hpibqueue *hq)
    278 {
    279 	struct hpibbus_softc *sc = device_private(pdev);
    280 	int s;
    281 
    282 	s = splhigh();	/* XXXthorpej */
    283 	TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
    284 	splx(s);
    285 
    286 	if (TAILQ_FIRST(&sc->sc_queue) == hq)
    287 		return 1;
    288 
    289 	return 0;
    290 }
    291 
    292 void
    293 hpibfree(struct device *pdev, struct hpibqueue *hq)
    294 {
    295 	struct hpibbus_softc *sc = device_private(pdev);
    296 	int s;
    297 
    298 	s = splhigh();	/* XXXthorpej */
    299 	TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
    300 	splx(s);
    301 
    302 	if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    303 		(*hq->hq_start)(hq->hq_softc);
    304 }
    305 
    306 int
    307 hpibid(int unit, int slave)
    308 {
    309 	short id;
    310 	int ohpibtimeout;
    311 
    312 	/*
    313 	 * XXX shorten timeout value so autoconfig doesn't
    314 	 * take forever on slow CPUs.
    315 	 */
    316 	ohpibtimeout = hpibtimeout;
    317 	hpibtimeout = hpibidtimeout * (cpuspeed / 8);
    318 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
    319 		id = 0;
    320 	hpibtimeout = ohpibtimeout;
    321 	return id;
    322 }
    323 
    324 int
    325 hpibsend(int unit, int slave, int sec, void *addr, int cnt)
    326 {
    327 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    328 
    329 	return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt);
    330 }
    331 
    332 int
    333 hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
    334 {
    335 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    336 
    337 	return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt);
    338 }
    339 
    340 int
    341 hpibpptest(int unit, int slave)
    342 {
    343 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    344 
    345 	return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave);
    346 }
    347 
    348 void
    349 hpibppclear(int unit)
    350 {
    351 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    352 
    353 	sc->sc_flags &= ~HPIBF_PPOLL;
    354 }
    355 
    356 void
    357 hpibawait(int unit)
    358 {
    359 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    360 
    361 	sc->sc_flags |= HPIBF_PPOLL;
    362 	(*sc->sc_ops->hpib_ppwatch)(sc);
    363 }
    364 
    365 int
    366 hpibswait(int unit, int slave)
    367 {
    368 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    369 	int timo = hpibtimeout;
    370 	int mask, (*ppoll)(struct hpibbus_softc *);
    371 
    372 	ppoll = sc->sc_ops->hpib_ppoll;
    373 	mask = 0x80 >> slave;
    374 	while (((*ppoll)(sc) & mask) == 0) {
    375 		if (--timo == 0) {
    376 			printf("%s: swait timeout\n", device_xname(sc->sc_dev));
    377 			return -1;
    378 		}
    379 	}
    380 	return 0;
    381 }
    382 
    383 int
    384 hpibustart(int unit)
    385 {
    386 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    387 
    388 	if (sc->sc_type == HPIBA)
    389 		sc->sc_dq->dq_chan = DMA0;
    390 	else
    391 		sc->sc_dq->dq_chan = DMA0 | DMA1;
    392 	if (dmareq(sc->sc_dq))
    393 		return 1;
    394 	return 0;
    395 }
    396 
    397 static void
    398 hpibstart(void *arg)
    399 {
    400 	struct hpibbus_softc *sc = arg;
    401 	struct hpibqueue *hq;
    402 
    403 	hq = TAILQ_FIRST(&sc->sc_queue);
    404 	(*hq->hq_go)(hq->hq_softc);
    405 }
    406 
    407 void
    408 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
    409 {
    410 	struct hpibbus_softc *sc = device_private(hpibbus_cd.cd_devs[unit]);
    411 
    412 	(*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
    413 }
    414 
    415 static void
    416 hpibdone(void *arg)
    417 {
    418 	struct hpibbus_softc *sc = arg;
    419 
    420 	(*sc->sc_ops->hpib_done)(sc);
    421 }
    422 
    423 int
    424 hpibintr(void *arg)
    425 {
    426 	struct hpibbus_softc *sc = arg;
    427 
    428 	return (sc->sc_ops->hpib_intr)(arg);
    429 }
    430 
    431 static int
    432 hpibbus_alloc(struct hpibbus_softc *sc, int slave, int punit)
    433 {
    434 
    435 	if (slave >= HPIB_NSLAVES ||
    436 	    punit >= HPIB_NPUNITS)
    437 		panic("hpibbus_alloc: device address out of range");
    438 
    439 	if (sc->sc_rmap[slave][punit] == 0) {
    440 		sc->sc_rmap[slave][punit] = 1;
    441 		return 0;
    442 	}
    443 	return 1;
    444 }
    445 
    446 #if 0
    447 static void
    448 hpibbus_free(struct hpibbus_softc *sc, int slave, int punit)
    449 {
    450 
    451 	if (slave >= HPIB_NSLAVES ||
    452 	    punit >= HPIB_NPUNITS)
    453 		panic("hpibbus_free: device address out of range");
    454 
    455 #ifdef DIAGNOSTIC
    456 	if (sc->sc_rmap[slave][punit] == 0)
    457 		panic("hpibbus_free: not allocated");
    458 #endif
    459 
    460 	sc->sc_rmap[slave][punit] = 0;
    461 }
    462 #endif
    463