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