fhpib.c revision 1.7 1 /* $NetBSD: fhpib.c,v 1.7 1995/11/19 17:57:15 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)fhpib.c 8.2 (Berkeley) 1/12/94
36 */
37
38 /*
39 * 98625A/B HPIB driver
40 */
41 #include "hpib.h"
42 #if NHPIB > 0
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/buf.h>
48
49 #include <hp300/dev/device.h>
50 #include <hp300/dev/fhpibreg.h>
51 #include <hp300/dev/hpibvar.h>
52 #include <hp300/dev/dmavar.h>
53
54 /*
55 * Inline version of fhpibwait to be used in places where
56 * we don't worry about getting hung.
57 */
58 #define FHPIBWAIT(hd, m) while (((hd)->hpib_intr & (m)) == 0) DELAY(1)
59
60 #ifdef DEBUG
61 int fhpibdebugunit = -1;
62 int fhpibdebug = 0;
63 #define FDB_FAIL 0x01
64 #define FDB_DMA 0x02
65 #define FDB_WAIT 0x04
66 #define FDB_PPOLL 0x08
67
68 int dopriodma = 0; /* use high priority DMA */
69 int doworddma = 1; /* non-zero if we should attempt word dma */
70 int doppollint = 1; /* use ppoll interrupts instead of watchdog */
71 int fhpibppolldelay = 50;
72
73 long fhpibbadint[2] = { 0 };
74 long fhpibtransfer[NHPIB] = { 0 };
75 long fhpibnondma[NHPIB] = { 0 };
76 long fhpibworddma[NHPIB] = { 0 };
77 long fhpibppollfail[NHPIB] = { 0 };
78 #endif
79
80 int fhpibcmd[NHPIB];
81
82 void fhpibreset __P((int));
83 int fhpibsend __P((int, int, int, void *, int));
84 int fhpibrecv __P((int, int, int, void *, int));
85 int fhpibppoll __P((int));
86 void fhpibppwatch __P((void *));
87 void fhpibgo __P((int, int, int, void *, int, int, int));
88 void fhpibdone __P((int));
89 int fhpibintr __P((int));
90
91 /*
92 * Our controller ops structure.
93 */
94 struct hpib_controller fhpib_controller = {
95 fhpibreset,
96 fhpibsend,
97 fhpibrecv,
98 fhpibppoll,
99 fhpibppwatch,
100 fhpibgo,
101 fhpibdone,
102 fhpibintr
103 };
104
105 int
106 fhpibtype(hc)
107 register struct hp_ctlr *hc;
108 {
109 register struct hpib_softc *hs = &hpib_softc[hc->hp_unit];
110 register struct fhpibdevice *hd = (struct fhpibdevice *)hc->hp_addr;
111
112 if (hd->hpib_cid != HPIBC)
113 return(0);
114
115 hs->sc_type = HPIBC;
116 hs->sc_ba = HPIBC_BA;
117 hc->hp_ipl = HPIB_IPL(hd->hpib_ids);
118
119 hs->sc_controller = &fhpib_controller;
120 hs->sc_descrip = "98625A or 98625B fast HP-IB";
121 return(1);
122 }
123
124 void
125 fhpibreset(unit)
126 int unit;
127 {
128 register struct hpib_softc *hs = &hpib_softc[unit];
129 register struct fhpibdevice *hd;
130
131 hd = (struct fhpibdevice *)hs->sc_hc->hp_addr;
132 hd->hpib_cid = 0xFF;
133 DELAY(100);
134 hd->hpib_cmd = CT_8BIT;
135 hd->hpib_ar = AR_ARONC;
136 fhpibifc(hd);
137 hd->hpib_ie = IDS_IE;
138 hd->hpib_data = C_DCL;
139 DELAY(100000);
140 /*
141 * See if we can do word dma.
142 * If so, we should be able to write and read back the appropos bit.
143 */
144 hd->hpib_ie |= IDS_WDMA;
145 if (hd->hpib_ie & IDS_WDMA) {
146 hd->hpib_ie &= ~IDS_WDMA;
147 hs->sc_flags |= HPIBF_DMA16;
148 #ifdef DEBUG
149 if (fhpibdebug & FDB_DMA)
150 printf("fhpibtype: unit %d has word dma\n", unit);
151
152 #endif
153 }
154 }
155
156 fhpibifc(hd)
157 register struct fhpibdevice *hd;
158 {
159 hd->hpib_cmd |= CT_IFC;
160 hd->hpib_cmd |= CT_INITFIFO;
161 DELAY(100);
162 hd->hpib_cmd &= ~CT_IFC;
163 hd->hpib_cmd |= CT_REN;
164 hd->hpib_stat = ST_ATN;
165 }
166
167 int
168 fhpibsend(unit, slave, sec, ptr, origcnt)
169 int unit, slave, sec, origcnt;
170 void *ptr;
171 {
172 register struct hpib_softc *hs = &hpib_softc[unit];
173 register struct fhpibdevice *hd;
174 register int cnt = origcnt;
175 register int timo;
176 char *addr = ptr;
177
178 hd = (struct fhpibdevice *)hs->sc_hc->hp_addr;
179 hd->hpib_stat = 0;
180 hd->hpib_imask = IM_IDLE | IM_ROOM;
181 if (fhpibwait(hd, IM_IDLE) < 0)
182 goto senderr;
183 hd->hpib_stat = ST_ATN;
184 hd->hpib_data = C_UNL;
185 hd->hpib_data = C_TAG + hs->sc_ba;
186 hd->hpib_data = C_LAG + slave;
187 if (sec < 0) {
188 if (sec == -2) /* selected device clear KLUDGE */
189 hd->hpib_data = C_SDC;
190 } else
191 hd->hpib_data = C_SCG + sec;
192 if (fhpibwait(hd, IM_IDLE) < 0)
193 goto senderr;
194 if (cnt) {
195 hd->hpib_stat = ST_WRITE;
196 while (--cnt) {
197 hd->hpib_data = *addr++;
198 timo = hpibtimeout;
199 while ((hd->hpib_intr & IM_ROOM) == 0) {
200 if (--timo <= 0)
201 goto senderr;
202 DELAY(1);
203 }
204 }
205 hd->hpib_stat = ST_EOI;
206 hd->hpib_data = *addr;
207 FHPIBWAIT(hd, IM_ROOM);
208 hd->hpib_stat = ST_ATN;
209 /* XXX: HP-UX claims bug with CS80 transparent messages */
210 if (sec == 0x12)
211 DELAY(150);
212 hd->hpib_data = C_UNL;
213 (void) fhpibwait(hd, IM_IDLE);
214 }
215 hd->hpib_imask = 0;
216 return (origcnt);
217
218 senderr:
219 hd->hpib_imask = 0;
220 fhpibifc(hd);
221 #ifdef DEBUG
222 if (fhpibdebug & FDB_FAIL) {
223 printf("hpib%d: fhpibsend failed: slave %d, sec %x, ",
224 unit, slave, sec);
225 printf("sent %d of %d bytes\n", origcnt-cnt-1, origcnt);
226 }
227 #endif
228 return (origcnt - cnt - 1);
229 }
230
231 int
232 fhpibrecv(unit, slave, sec, ptr, origcnt)
233 int unit, slave, sec, origcnt;
234 void *ptr;
235 {
236 register struct hpib_softc *hs = &hpib_softc[unit];
237 register struct fhpibdevice *hd;
238 register int cnt = origcnt;
239 register int timo;
240 char *addr = ptr;
241
242 hd = (struct fhpibdevice *)hs->sc_hc->hp_addr;
243 /*
244 * Slave < 0 implies continuation of a previous receive
245 * that probably timed out.
246 */
247 if (slave >= 0) {
248 hd->hpib_stat = 0;
249 hd->hpib_imask = IM_IDLE | IM_ROOM | IM_BYTE;
250 if (fhpibwait(hd, IM_IDLE) < 0)
251 goto recverror;
252 hd->hpib_stat = ST_ATN;
253 hd->hpib_data = C_UNL;
254 hd->hpib_data = C_LAG + hs->sc_ba;
255 hd->hpib_data = C_TAG + slave;
256 if (sec != -1)
257 hd->hpib_data = C_SCG + sec;
258 if (fhpibwait(hd, IM_IDLE) < 0)
259 goto recverror;
260 hd->hpib_stat = ST_READ0;
261 hd->hpib_data = 0;
262 }
263 if (cnt) {
264 while (--cnt >= 0) {
265 timo = hpibtimeout;
266 while ((hd->hpib_intr & IM_BYTE) == 0) {
267 if (--timo == 0)
268 goto recvbyteserror;
269 DELAY(1);
270 }
271 *addr++ = hd->hpib_data;
272 }
273 FHPIBWAIT(hd, IM_ROOM);
274 hd->hpib_stat = ST_ATN;
275 hd->hpib_data = (slave == 31) ? C_UNA : C_UNT;
276 (void) fhpibwait(hd, IM_IDLE);
277 }
278 hd->hpib_imask = 0;
279 return (origcnt);
280
281 recverror:
282 fhpibifc(hd);
283 recvbyteserror:
284 hd->hpib_imask = 0;
285 #ifdef DEBUG
286 if (fhpibdebug & FDB_FAIL) {
287 printf("hpib%d: fhpibrecv failed: slave %d, sec %x, ",
288 unit, slave, sec);
289 printf("got %d of %d bytes\n", origcnt-cnt-1, origcnt);
290 }
291 #endif
292 return (origcnt - cnt - 1);
293 }
294
295 void
296 fhpibgo(unit, slave, sec, ptr, count, rw, timo)
297 int unit, slave, sec, count, rw, timo;
298 void *ptr;
299 {
300 register struct hpib_softc *hs = &hpib_softc[unit];
301 register struct fhpibdevice *hd;
302 register int i;
303 char *addr = ptr;
304 int flags = 0;
305
306 hd = (struct fhpibdevice *)hs->sc_hc->hp_addr;
307 hs->sc_flags |= HPIBF_IO;
308 if (timo)
309 hs->sc_flags |= HPIBF_TIMO;
310 if (rw == B_READ)
311 hs->sc_flags |= HPIBF_READ;
312 #ifdef DEBUG
313 else if (hs->sc_flags & HPIBF_READ) {
314 printf("fhpibgo: HPIBF_READ still set\n");
315 hs->sc_flags &= ~HPIBF_READ;
316 }
317 #endif
318 hs->sc_count = count;
319 hs->sc_addr = addr;
320 #ifdef DEBUG
321 fhpibtransfer[unit]++;
322 #endif
323 if ((hs->sc_flags & HPIBF_DMA16) &&
324 ((int)addr & 1) == 0 && count && (count & 1) == 0
325 #ifdef DEBUG
326 && doworddma
327 #endif
328 ) {
329 #ifdef DEBUG
330 fhpibworddma[unit]++;
331 #endif
332 flags |= DMAGO_WORD;
333 hd->hpib_latch = 0;
334 }
335 #ifdef DEBUG
336 if (dopriodma)
337 flags |= DMAGO_PRI;
338 #endif
339 if (hs->sc_flags & HPIBF_READ) {
340 fhpibcmd[unit] = CT_REN | CT_8BIT;
341 hs->sc_curcnt = count;
342 dmago(hs->sc_dq.dq_ctlr, addr, count, flags|DMAGO_READ);
343 if (fhpibrecv(unit, slave, sec, 0, 0) < 0) {
344 #ifdef DEBUG
345 printf("fhpibgo: recv failed, retrying...\n");
346 #endif
347 (void) fhpibrecv(unit, slave, sec, 0, 0);
348 }
349 i = hd->hpib_cmd;
350 hd->hpib_cmd = fhpibcmd[unit];
351 hd->hpib_ie = IDS_DMA(hs->sc_dq.dq_ctlr) |
352 ((flags & DMAGO_WORD) ? IDS_WDMA : 0);
353 return;
354 }
355 fhpibcmd[unit] = CT_REN | CT_8BIT | CT_FIFOSEL;
356 if (count < hpibdmathresh) {
357 #ifdef DEBUG
358 fhpibnondma[unit]++;
359 if (flags & DMAGO_WORD)
360 fhpibworddma[unit]--;
361 #endif
362 hs->sc_curcnt = count;
363 (void) fhpibsend(unit, slave, sec, addr, count);
364 fhpibdone(unit);
365 return;
366 }
367 count -= (flags & DMAGO_WORD) ? 2 : 1;
368 hs->sc_curcnt = count;
369 dmago(hs->sc_dq.dq_ctlr, addr, count, flags);
370 if (fhpibsend(unit, slave, sec, 0, 0) < 0) {
371 #ifdef DEBUG
372 printf("fhpibgo: send failed, retrying...\n");
373 #endif
374 (void) fhpibsend(unit, slave, sec, 0, 0);
375 }
376 i = hd->hpib_cmd;
377 hd->hpib_cmd = fhpibcmd[unit];
378 hd->hpib_ie = IDS_DMA(hs->sc_dq.dq_ctlr) | IDS_WRITE |
379 ((flags & DMAGO_WORD) ? IDS_WDMA : 0);
380 }
381
382 /*
383 * A DMA read can finish but the device can still be waiting (MAG-tape
384 * with more data than we're waiting for). This timeout routine
385 * takes care of that. Somehow, the thing gets hosed. For now, since
386 * this should be a very rare occurence, we RESET it.
387 */
388 void
389 fhpibdmadone(arg)
390 void *arg;
391 {
392 register int unit;
393 register struct hpib_softc *hs;
394 int s = splbio();
395
396 unit = (int)arg;
397 hs = &hpib_softc[unit];
398 if (hs->sc_flags & HPIBF_IO) {
399 register struct fhpibdevice *hd;
400 register struct devqueue *dq;
401
402 hd = (struct fhpibdevice *)hs->sc_hc->hp_addr;
403 hd->hpib_imask = 0;
404 hd->hpib_cid = 0xFF;
405 DELAY(100);
406 hd->hpib_cmd = CT_8BIT;
407 hd->hpib_ar = AR_ARONC;
408 fhpibifc(hd);
409 hd->hpib_ie = IDS_IE;
410 hs->sc_flags &= ~(HPIBF_DONE|HPIBF_IO|HPIBF_READ|HPIBF_TIMO);
411 dmafree(&hs->sc_dq);
412 dq = hs->sc_sq.dq_forw;
413 (dq->dq_driver->d_intr)(dq->dq_unit);
414 }
415 (void) splx(s);
416 }
417
418 void
419 fhpibdone(unit)
420 int unit;
421 {
422 register struct hpib_softc *hs = &hpib_softc[unit];
423 register struct fhpibdevice *hd;
424 register char *addr;
425 register int cnt;
426
427 hd = (struct fhpibdevice *)hs->sc_hc->hp_addr;
428 cnt = hs->sc_curcnt;
429 hs->sc_addr += cnt;
430 hs->sc_count -= cnt;
431 #ifdef DEBUG
432 if ((fhpibdebug & FDB_DMA) && fhpibdebugunit == unit)
433 printf("fhpibdone: addr %x cnt %d\n",
434 hs->sc_addr, hs->sc_count);
435 #endif
436 if (hs->sc_flags & HPIBF_READ) {
437 hd->hpib_imask = IM_IDLE | IM_BYTE;
438 if (hs->sc_flags & HPIBF_TIMO)
439 timeout(fhpibdmadone, (void *)unit, hz >> 2);
440 } else {
441 cnt = hs->sc_count;
442 if (cnt) {
443 addr = hs->sc_addr;
444 hd->hpib_imask = IM_IDLE | IM_ROOM;
445 FHPIBWAIT(hd, IM_IDLE);
446 hd->hpib_stat = ST_WRITE;
447 while (--cnt) {
448 hd->hpib_data = *addr++;
449 FHPIBWAIT(hd, IM_ROOM);
450 }
451 hd->hpib_stat = ST_EOI;
452 hd->hpib_data = *addr;
453 }
454 hd->hpib_imask = IM_IDLE;
455 }
456 hs->sc_flags |= HPIBF_DONE;
457 hd->hpib_stat = ST_IENAB;
458 hd->hpib_ie = IDS_IE;
459 }
460
461 int
462 fhpibintr(unit)
463 register int unit;
464 {
465 register struct hpib_softc *hs = &hpib_softc[unit];
466 register struct fhpibdevice *hd;
467 register struct devqueue *dq;
468 register int stat0;
469
470 hd = (struct fhpibdevice *)hs->sc_hc->hp_addr;
471 stat0 = hd->hpib_ids;
472 if ((stat0 & (IDS_IE|IDS_IR)) != (IDS_IE|IDS_IR)) {
473 #ifdef DEBUG
474 if ((fhpibdebug & FDB_FAIL) && (stat0 & IDS_IR) &&
475 (hs->sc_flags & (HPIBF_IO|HPIBF_DONE)) != HPIBF_IO)
476 printf("hpib%d: fhpibintr: bad status %x\n",
477 unit, stat0);
478 fhpibbadint[0]++;
479 #endif
480 return(0);
481 }
482 if ((hs->sc_flags & (HPIBF_IO|HPIBF_DONE)) == HPIBF_IO) {
483 #ifdef DEBUG
484 fhpibbadint[1]++;
485 #endif
486 return(0);
487 }
488 #ifdef DEBUG
489 if ((fhpibdebug & FDB_DMA) && fhpibdebugunit == unit)
490 printf("fhpibintr: flags %x\n", hs->sc_flags);
491 #endif
492 dq = hs->sc_sq.dq_forw;
493 if (hs->sc_flags & HPIBF_IO) {
494 if (hs->sc_flags & HPIBF_TIMO)
495 untimeout(fhpibdmadone, (void *)unit);
496 stat0 = hd->hpib_cmd;
497 hd->hpib_cmd = fhpibcmd[unit] & ~CT_8BIT;
498 hd->hpib_stat = 0;
499 hd->hpib_cmd = CT_REN | CT_8BIT;
500 stat0 = hd->hpib_intr;
501 hd->hpib_imask = 0;
502 hs->sc_flags &= ~(HPIBF_DONE|HPIBF_IO|HPIBF_READ|HPIBF_TIMO);
503 dmafree(&hs->sc_dq);
504 (dq->dq_driver->d_intr)(dq->dq_unit);
505 } else if (hs->sc_flags & HPIBF_PPOLL) {
506 stat0 = hd->hpib_intr;
507 #ifdef DEBUG
508 if ((fhpibdebug & FDB_FAIL) &&
509 doppollint && (stat0 & IM_PPRESP) == 0)
510 printf("hpib%d: fhpibintr: bad intr reg %x\n",
511 unit, stat0);
512 #endif
513 hd->hpib_stat = 0;
514 hd->hpib_imask = 0;
515 #ifdef DEBUG
516 stat0 = fhpibppoll(unit);
517 if ((fhpibdebug & FDB_PPOLL) && unit == fhpibdebugunit)
518 printf("fhpibintr: got PPOLL status %x\n", stat0);
519 if ((stat0 & (0x80 >> dq->dq_slave)) == 0) {
520 /*
521 * XXX give it another shot (68040)
522 */
523 fhpibppollfail[unit]++;
524 DELAY(fhpibppolldelay);
525 stat0 = fhpibppoll(unit);
526 if ((stat0 & (0x80 >> dq->dq_slave)) == 0 &&
527 (fhpibdebug & FDB_PPOLL) && unit == fhpibdebugunit)
528 printf("fhpibintr: PPOLL: unit %d slave %d stat %x\n",
529 unit, dq->dq_slave, stat0);
530 }
531 #endif
532 hs->sc_flags &= ~HPIBF_PPOLL;
533 (dq->dq_driver->d_intr)(dq->dq_unit);
534 }
535 return(1);
536 }
537
538 int
539 fhpibppoll(unit)
540 int unit;
541 {
542 register struct fhpibdevice *hd;
543 register int ppoll;
544
545 hd = (struct fhpibdevice *)hpib_softc[unit].sc_hc->hp_addr;
546 hd->hpib_stat = 0;
547 hd->hpib_psense = 0;
548 hd->hpib_pmask = 0xFF;
549 hd->hpib_imask = IM_PPRESP | IM_PABORT;
550 DELAY(25);
551 hd->hpib_intr = IM_PABORT;
552 ppoll = hd->hpib_data;
553 if (hd->hpib_intr & IM_PABORT)
554 ppoll = 0;
555 hd->hpib_imask = 0;
556 hd->hpib_pmask = 0;
557 hd->hpib_stat = ST_IENAB;
558 return(ppoll);
559 }
560
561 int
562 fhpibwait(hd, x)
563 register struct fhpibdevice *hd;
564 int x;
565 {
566 register int timo = hpibtimeout;
567
568 while ((hd->hpib_intr & x) == 0 && --timo)
569 DELAY(1);
570 if (timo == 0) {
571 #ifdef DEBUG
572 if (fhpibdebug & FDB_FAIL)
573 printf("fhpibwait(%x, %x) timeout\n", hd, x);
574 #endif
575 return(-1);
576 }
577 return(0);
578 }
579
580 /*
581 * XXX: this will have to change if we ever allow more than one
582 * pending operation per HP-IB.
583 */
584 void
585 fhpibppwatch(arg)
586 void *arg;
587 {
588 register int unit;
589 register struct hpib_softc *hs;
590 register struct fhpibdevice *hd;
591 register int slave;
592
593 unit = (int)arg;
594 hs = &hpib_softc[unit];
595 if ((hs->sc_flags & HPIBF_PPOLL) == 0)
596 return;
597 hd = (struct fhpibdevice *)hs->sc_hc->hp_addr;
598 slave = (0x80 >> hs->sc_sq.dq_forw->dq_slave);
599 #ifdef DEBUG
600 if (!doppollint) {
601 if (fhpibppoll(unit) & slave) {
602 hd->hpib_stat = ST_IENAB;
603 hd->hpib_imask = IM_IDLE | IM_ROOM;
604 } else
605 timeout(fhpibppwatch, (void *)unit, 1);
606 return;
607 }
608 if ((fhpibdebug & FDB_PPOLL) && unit == fhpibdebugunit)
609 printf("fhpibppwatch: sense request on %d\n", unit);
610 #endif
611 hd->hpib_psense = ~slave;
612 hd->hpib_pmask = slave;
613 hd->hpib_stat = ST_IENAB;
614 hd->hpib_imask = IM_PPRESP | IM_PABORT;
615 hd->hpib_ie = IDS_IE;
616 }
617 #endif /* NHPIB > 0 */
618