No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hp300 / dev / hpib.c
blobe4275ba3899f6f5226111a8799a5f67ac737fc59
1 /* $NetBSD: hpib.c,v 1.37 2008/04/28 20:23:19 martin Exp $ */
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
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.
33 * Copyright (c) 1982, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
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.
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.
60 * @(#)hpib.c 8.2 (Berkeley) 1/12/94
64 * HP-IB bus driver
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: hpib.c,v 1.37 2008/04/28 20:23:19 martin Exp $");
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>
76 #include <hp300/dev/dmavar.h>
78 #include <hp300/dev/hpibvar.h>
80 #include <machine/cpu.h>
81 #include <machine/hp300spu.h>
83 #include "ioconf.h"
85 static int hpibbusmatch(device_t, cfdata_t, void *);
86 static void hpibbusattach(device_t, device_t, void *);
88 CFATTACH_DECL_NEW(hpibbus, sizeof(struct hpibbus_softc),
89 hpibbusmatch, hpibbusattach, NULL, NULL);
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 *);
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
100 static void hpibstart(void *);
101 static void hpibdone(void *);
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 */
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.
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.
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!
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.
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!).
134 * This is evil, but what can you do?
137 static int
138 hpibbusmatch(device_t parent, cfdata_t cf, void *aux)
141 return 1;
144 static void
145 hpibbusattach(device_t parent, device_t self, void *aux)
147 struct hpibbus_softc *sc = device_private(self);
148 struct hpibdev_attach_args *ha = aux;
150 sc->sc_dev = self;
151 aprint_normal("\n");
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 */
159 hpibreset(device_unit(self)); /* XXX souldn't be here */
162 * Initialize the DMA queue entry.
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;
169 sc->sc_dq->dq_softc = sc;
170 sc->sc_dq->dq_start = hpibstart;
171 sc->sc_dq->dq_done = hpibdone;
173 /* Initialize the slave request queue. */
174 TAILQ_INIT(&sc->sc_queue);
176 /* Attach any devices on the bus. */
177 hpibbus_attach_children(sc);
180 static void
181 hpibbus_attach_children(struct hpibbus_softc *sc)
183 struct hpibbus_attach_args ha;
184 int slave;
186 for (slave = 0; slave < 8; slave++) {
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.
192 ha.ha_id = hpibid(device_unit(sc->sc_dev), slave);
194 ha.ha_slave = slave; /* not to be modified by children */
195 ha.ha_punit = 0; /* children modify this */
198 * Search though all configured children for this bus.
200 config_search_ia(hpibbussearch, sc->sc_dev, "hpibbus", &ha);
204 static int
205 hpibbussearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
207 struct hpibbus_softc *sc = device_private(parent);
208 struct hpibbus_attach_args *ha = aux;
210 /* Make sure this is in a consistent state. */
211 ha->ha_punit = 0;
213 if (config_match(parent, cf, ha) > 0) {
215 * The device probe has succeeded, and filled in
216 * the punit information. Make sure the configuration
217 * allows for this slave/punit combination.
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;
227 * Allocate the device's address from the bus's
228 * resource map.
230 if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit))
231 goto out;
234 * This device is allowed; attach it.
236 config_attach(parent, cf, ha, hpibbusprint);
238 out:
239 return 0;
242 static int
243 hpibbusprint(void *aux, const char *pnp)
245 struct hpibbus_attach_args *ha = aux;
247 aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
248 return UNCONF;
252 hpibdevprint(void *aux, const char *pnp)
255 /* only hpibbus's can attach to hpibdev's -- easy. */
256 if (pnp != NULL)
257 aprint_normal("hpibbus at %s", pnp);
258 return UNCONF;
261 void
262 hpibreset(int unit)
264 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
266 (*sc->sc_ops->hpib_reset)(sc);
270 hpibreq(struct device *pdev, struct hpibqueue *hq)
272 struct hpibbus_softc *sc = device_private(pdev);
273 int s;
275 s = splhigh(); /* XXXthorpej */
276 TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
277 splx(s);
279 if (TAILQ_FIRST(&sc->sc_queue) == hq)
280 return 1;
282 return 0;
285 void
286 hpibfree(struct device *pdev, struct hpibqueue *hq)
288 struct hpibbus_softc *sc = device_private(pdev);
289 int s;
291 s = splhigh(); /* XXXthorpej */
292 TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
293 splx(s);
295 if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL)
296 (*hq->hq_start)(hq->hq_softc);
300 hpibid(int unit, int slave)
302 short id;
303 int ohpibtimeout;
306 * XXX shorten timeout value so autoconfig doesn't
307 * take forever on slow CPUs.
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;
318 hpibsend(int unit, int slave, int sec, void *addr, int cnt)
320 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
322 return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt);
326 hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
328 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
330 return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt);
334 hpibpptest(int unit, int slave)
336 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
338 return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave);
341 void
342 hpibppclear(int unit)
344 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
346 sc->sc_flags &= ~HPIBF_PPOLL;
349 void
350 hpibawait(int unit)
352 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
354 sc->sc_flags |= HPIBF_PPOLL;
355 (*sc->sc_ops->hpib_ppwatch)(sc);
359 hpibswait(int unit, int slave)
361 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
362 int timo = hpibtimeout;
363 int mask, (*ppoll)(struct hpibbus_softc *);
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;
373 return 0;
377 hpibustart(int unit)
379 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
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;
390 static void
391 hpibstart(void *arg)
393 struct hpibbus_softc *sc = arg;
394 struct hpibqueue *hq;
396 hq = TAILQ_FIRST(&sc->sc_queue);
397 (*hq->hq_go)(hq->hq_softc);
400 void
401 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
403 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
405 (*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
408 static void
409 hpibdone(void *arg)
411 struct hpibbus_softc *sc = arg;
413 (*sc->sc_ops->hpib_done)(sc);
417 hpibintr(void *arg)
419 struct hpibbus_softc *sc = arg;
421 return (sc->sc_ops->hpib_intr)(arg);
424 static int
425 hpibbus_alloc(struct hpibbus_softc *sc, int slave, int punit)
428 if (slave >= HPIB_NSLAVES ||
429 punit >= HPIB_NPUNITS)
430 panic("hpibbus_alloc: device address out of range");
432 if (sc->sc_rmap[slave][punit] == 0) {
433 sc->sc_rmap[slave][punit] = 1;
434 return 0;
436 return 1;
439 #if 0
440 static void
441 hpibbus_free(struct hpibbus_softc *sc, int slave, int punit)
444 if (slave >= HPIB_NSLAVES ||
445 punit >= HPIB_NPUNITS)
446 panic("hpibbus_free: device address out of range");
448 #ifdef DIAGNOSTIC
449 if (sc->sc_rmap[slave][punit] == 0)
450 panic("hpibbus_free: not allocated");
451 #endif
453 sc->sc_rmap[slave][punit] = 0;
455 #endif